Hi, As per request from Stephen, I have enclosed the patch for Unix Datagram getpeersec.
As always, comments are welcome! thanks, Catherine -- From: [EMAIL PROTECTED] This patch implements an API whereby an application can determine the label of its peer's Unix datagram sockets via the auxiliary data mechanism of recvmsg. Patch purpose: This patch enables a security-aware application to retrieve the security context of the peer of a Unix datagram socket. The application can then use this security context to determine the security context for processing on behalf of the peer who sent the packet. Patch design and implementation: The design and implementation is very similar to the UDP case for INET sockets. Basically we build upon the existing Unix domain socket API for retrieving user credentials. Linux offers the API for obtaining user credentials via ancillary messages (i.e., out of band/control messages that are bundled together with a normal message). To retrieve the security context, the application first indicates to the kernel such desire by setting the SO_PASSSEC option via getsockopt. Then the application retrieves the security context using the auxiliary data mechanism. An example server application for Unix datagram socket should look like this: toggle = 1; toggle_len = sizeof(toggle); setsockopt(sockfd, SOL_SOCKET, SO_PASSSEC, &toggle, &toggle_len); recvmsg(sockfd, &msg_hdr, 0); if (msg_hdr.msg_controllen > sizeof(struct cmsghdr)) { cmsg_hdr = CMSG_FIRSTHDR(&msg_hdr); if (cmsg_hdr->cmsg_len <= CMSG_LEN(sizeof(scontext)) && cmsg_hdr->cmsg_level == SOL_SOCKET && cmsg_hdr->cmsg_type == SCM_SECURITY) { memcpy(&scontext, CMSG_DATA(cmsg_hdr), sizeof(scontext)); } } sock_setsockopt is enhanced with a new socket option SOCK_PASSSEC to allow a server socket to receive security context of the peer. Testing: We have tested the patch by setting up Unix datagram client and server applications. We verified that the server can retrieve the security context using the auxiliary data mechanism of recvmsg. --- include/asm-i386/socket.h | 1 + include/asm-ia64/socket.h | 1 + include/asm-powerpc/socket.h | 1 + include/asm-x86_64/socket.h | 1 + include/linux/net.h | 1 + include/net/af_unix.h | 2 ++ include/net/scm.h | 12 ++++++++++++ net/core/sock.c | 11 +++++++++++ net/unix/af_unix.c | 2 ++ 9 files changed, 32 insertions(+) diff -puN include/linux/net.h~lsm-secpeer-unix include/linux/net.h --- linux-2.6.16-rc1/include/linux/net.h~lsm-secpeer-unix 2006-03-07 18:55:59.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/linux/net.h 2006-03-07 18:57:58.000000000 -0500 @@ -62,6 +62,7 @@ typedef enum { #define SOCK_ASYNC_WAITDATA 1 #define SOCK_NOSPACE 2 #define SOCK_PASSCRED 3 +#define SOCK_PASSSEC 4 #ifndef ARCH_HAS_SOCKET_TYPES /** diff -puN include/asm-i386/socket.h~lsm-secpeer-unix include/asm-i386/socket.h --- linux-2.6.16-rc1/include/asm-i386/socket.h~lsm-secpeer-unix 2006-03-07 18:58:38.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/asm-i386/socket.h 2006-03-07 18:59:31.000000000 -0500 @@ -48,5 +48,6 @@ #define SO_ACCEPTCONN 30 #define SO_PEERSEC 31 +#define SO_PASSSEC 34 #endif /* _ASM_SOCKET_H */ diff -puN include/asm-x86_64/socket.h~lsm-secpeer-unix include/asm-x86_64/socket.h --- linux-2.6.16-rc1/include/asm-x86_64/socket.h~lsm-secpeer-unix 2006-03-07 19:00:58.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/asm-x86_64/socket.h 2006-03-07 19:06:09.000000000 -0500 @@ -48,5 +48,6 @@ #define SO_ACCEPTCONN 30 #define SO_PEERSEC 31 +#define SO_PASSSEC 34 #endif /* _ASM_SOCKET_H */ diff -puN include/asm-ia64/socket.h~lsm-secpeer-unix include/asm-ia64/socket.h --- linux-2.6.16-rc1/include/asm-ia64/socket.h~lsm-secpeer-unix 2006-03-07 19:02:27.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/asm-ia64/socket.h 2006-03-07 19:02:44.000000000 -0500 @@ -57,5 +57,6 @@ #define SO_ACCEPTCONN 30 #define SO_PEERSEC 31 +#define SO_PASSSEC 34 #endif /* _ASM_IA64_SOCKET_H */ diff -puN include/asm-powerpc/socket.h~lsm-secpeer-unix include/asm-powerpc/socket.h --- linux-2.6.16-rc1/include/asm-powerpc/socket.h~lsm-secpeer-unix 2006-03-07 19:04:00.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/asm-powerpc/socket.h 2006-03-07 19:04:17.000000000 -0500 @@ -55,5 +55,6 @@ #define SO_ACCEPTCONN 30 #define SO_PEERSEC 31 +#define SO_PASSSEC 34 #endif /* _ASM_POWERPC_SOCKET_H */ diff -puN include/net/af_unix.h~lsm-secpeer-unix include/net/af_unix.h --- linux-2.6.16-rc1/include/net/af_unix.h~lsm-secpeer-unix 2006-03-07 19:19:08.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/net/af_unix.h 2006-03-09 15:30:15.000000000 -0500 @@ -53,10 +53,12 @@ struct unix_address { struct unix_skb_parms { struct ucred creds; /* Skb credentials */ struct scm_fp_list *fp; /* Passed files */ + u32 sid; /* Security ID */ }; #define UNIXCB(skb) (*(struct unix_skb_parms*)&((skb)->cb)) #define UNIXCREDS(skb) (&UNIXCB((skb)).creds) +#define UNIXSID(skb) (&UNIXCB((skb)).sid) #define unix_state_rlock(s) spin_lock(&unix_sk(s)->lock) #define unix_state_runlock(s) spin_unlock(&unix_sk(s)->lock) diff -puN include/net/scm.h~lsm-secpeer-unix include/net/scm.h --- linux-2.6.16-rc1/include/net/scm.h~lsm-secpeer-unix 2006-03-07 19:25:41.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/include/net/scm.h 2006-03-09 13:34:25.000000000 -0500 @@ -3,6 +3,7 @@ #include <linux/limits.h> #include <linux/net.h> +#include <linux/security.h> /* Well, we should have at least one descriptor open * to accept passed FDs 8) @@ -19,6 +20,7 @@ struct scm_cookie { struct ucred creds; /* Skb credentials */ struct scm_fp_list *fp; /* Passed files */ + u32 sid; /* Passed security ID */ unsigned long seq; /* Connection seqno */ }; @@ -41,6 +43,7 @@ static __inline__ int scm_send(struct so scm->creds.uid = current->uid; scm->creds.gid = current->gid; scm->creds.pid = current->tgid; + scm->sid = security_sk_sid(sock->sk, (struct flowi*)NULL, 0); if (msg->msg_controllen <= 0) return 0; return __scm_send(sock, msg, scm); @@ -49,6 +52,9 @@ static __inline__ int scm_send(struct so static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm, int flags) { + char *scontext; + int scontext_len, err; + if (!msg->msg_control) { if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp) @@ -60,6 +66,12 @@ static __inline__ void scm_recv(struct s if (test_bit(SOCK_PASSCRED, &sock->flags)) put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(scm->creds), &scm->creds); + if (test_bit(SOCK_PASSSEC, &sock->flags)) { + err = security_sid_to_context(scm->sid, &scontext, &scontext_len); + if (!err) + put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, scontext_len, scontext); + } + if (!scm->fp) return; diff -puN net/core/sock.c~lsm-secpeer-unix net/core/sock.c --- linux-2.6.16-rc1/net/core/sock.c~lsm-secpeer-unix 2006-03-07 19:36:05.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/net/core/sock.c 2006-03-07 19:40:05.000000000 -0500 @@ -457,6 +457,13 @@ set_rcvbuf: ret = -ENONET; break; + case SO_PASSSEC: + if (valbool) + set_bit(SOCK_PASSSEC, &sock->flags); + else + clear_bit(SOCK_PASSSEC, &sock->flags); + break; + /* We implement the SO_SNDLOWAT etc to not be settable (1003.1g 5.3) */ default: @@ -615,6 +622,10 @@ int sock_getsockopt(struct socket *sock, v.val = sk->sk_state == TCP_LISTEN; break; + case SO_PASSSEC: + v.val = test_bit(SOCK_PASSSEC, &sock->flags) ? 1 : 0; + break; + case SO_PEERSEC: return security_socket_getpeersec_stream(sock, optval, optlen, len); diff -puN net/unix/af_unix.c~lsm-secpeer-unix net/unix/af_unix.c --- linux-2.6.16-rc1/net/unix/af_unix.c~lsm-secpeer-unix 2006-03-07 19:40:22.000000000 -0500 +++ linux-2.6.16-rc1-cxzhang/net/unix/af_unix.c 2006-03-08 00:54:14.000000000 -0500 @@ -1290,6 +1290,7 @@ static int unix_dgram_sendmsg(struct kio memcpy(UNIXCREDS(skb), &siocb->scm->creds, sizeof(struct ucred)); if (siocb->scm->fp) unix_attach_fds(siocb->scm, skb); + memcpy(UNIXSID(skb), &siocb->scm->sid, sizeof(u32)); skb->h.raw = skb->data; err = memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len); @@ -1570,6 +1571,7 @@ static int unix_dgram_recvmsg(struct kio memset(&tmp_scm, 0, sizeof(tmp_scm)); } siocb->scm->creds = *UNIXCREDS(skb); + siocb->scm->sid = *UNIXSID(skb); if (!(flags & MSG_PEEK)) { _ - To unsubscribe from this list: send the line "unsubscribe netdev" in the body of a message to [EMAIL PROTECTED] More majordomo info at http://vger.kernel.org/majordomo-info.html