2005-04-17 00:20:36 +02:00
|
|
|
#ifndef __LINUX_NET_SCM_H
|
|
|
|
#define __LINUX_NET_SCM_H
|
|
|
|
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <linux/net.h>
|
2006-08-02 23:12:06 +02:00
|
|
|
#include <linux/security.h>
|
2007-10-19 08:40:14 +02:00
|
|
|
#include <linux/pid.h>
|
|
|
|
#include <linux/nsproxy.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/* Well, we should have at least one descriptor open
|
|
|
|
* to accept passed FDs 8)
|
|
|
|
*/
|
2007-07-17 13:03:48 +02:00
|
|
|
#define SCM_MAX_FD 255
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
struct scm_fp_list
|
|
|
|
{
|
2008-11-06 09:37:40 +01:00
|
|
|
struct list_head list;
|
|
|
|
int count;
|
|
|
|
struct file *fp[SCM_MAX_FD];
|
2005-04-17 00:20:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct scm_cookie
|
|
|
|
{
|
|
|
|
struct ucred creds; /* Skb credentials */
|
|
|
|
struct scm_fp_list *fp; /* Passed files */
|
[AF_UNIX]: Datagram getpeersec
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.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-06-29 21:27:47 +02:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
2006-08-02 23:12:06 +02:00
|
|
|
u32 secid; /* Passed security ID */
|
[AF_UNIX]: Datagram getpeersec
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.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-06-29 21:27:47 +02:00
|
|
|
#endif
|
2005-04-17 00:20:36 +02:00
|
|
|
unsigned long seq; /* Connection seqno */
|
|
|
|
};
|
|
|
|
|
|
|
|
extern void scm_detach_fds(struct msghdr *msg, struct scm_cookie *scm);
|
|
|
|
extern void scm_detach_fds_compat(struct msghdr *msg, struct scm_cookie *scm);
|
|
|
|
extern int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm);
|
|
|
|
extern void __scm_destroy(struct scm_cookie *scm);
|
|
|
|
extern struct scm_fp_list * scm_fp_dup(struct scm_fp_list *fpl);
|
|
|
|
|
2006-08-02 23:12:06 +02:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
|
|
|
static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
|
|
|
|
{
|
|
|
|
security_socket_getpeersec_dgram(sock, NULL, &scm->secid);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static __inline__ void unix_get_peersec_dgram(struct socket *sock, struct scm_cookie *scm)
|
|
|
|
{ }
|
|
|
|
#endif /* CONFIG_SECURITY_NETWORK */
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static __inline__ void scm_destroy(struct scm_cookie *scm)
|
|
|
|
{
|
|
|
|
if (scm && scm->fp)
|
|
|
|
__scm_destroy(scm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct scm_cookie *scm)
|
|
|
|
{
|
2006-03-21 06:31:51 +01:00
|
|
|
struct task_struct *p = current;
|
2008-11-14 00:39:10 +01:00
|
|
|
scm->creds.uid = current_uid();
|
|
|
|
scm->creds.gid = current_gid();
|
2007-10-19 08:40:14 +02:00
|
|
|
scm->creds.pid = task_tgid_vnr(p);
|
2006-03-21 06:31:51 +01:00
|
|
|
scm->fp = NULL;
|
|
|
|
scm->seq = 0;
|
2006-08-02 23:12:06 +02:00
|
|
|
unix_get_peersec_dgram(sock, scm);
|
2005-04-17 00:20:36 +02:00
|
|
|
if (msg->msg_controllen <= 0)
|
|
|
|
return 0;
|
|
|
|
return __scm_send(sock, msg, scm);
|
|
|
|
}
|
|
|
|
|
[AF_UNIX]: Datagram getpeersec
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.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-06-29 21:27:47 +02:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
|
|
|
static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
|
|
|
|
{
|
2006-08-02 23:12:06 +02:00
|
|
|
char *secdata;
|
|
|
|
u32 seclen;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (test_bit(SOCK_PASSSEC, &sock->flags)) {
|
|
|
|
err = security_secid_to_secctx(scm->secid, &secdata, &seclen);
|
|
|
|
|
|
|
|
if (!err) {
|
|
|
|
put_cmsg(msg, SOL_SOCKET, SCM_SECURITY, seclen, secdata);
|
|
|
|
security_release_secctx(secdata, seclen);
|
|
|
|
}
|
|
|
|
}
|
[AF_UNIX]: Datagram getpeersec
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.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-06-29 21:27:47 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
|
|
|
|
{ }
|
|
|
|
#endif /* CONFIG_SECURITY_NETWORK */
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct scm_cookie *scm, int flags)
|
|
|
|
{
|
|
|
|
if (!msg->msg_control)
|
|
|
|
{
|
|
|
|
if (test_bit(SOCK_PASSCRED, &sock->flags) || scm->fp)
|
|
|
|
msg->msg_flags |= MSG_CTRUNC;
|
|
|
|
scm_destroy(scm);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test_bit(SOCK_PASSCRED, &sock->flags))
|
|
|
|
put_cmsg(msg, SOL_SOCKET, SCM_CREDENTIALS, sizeof(scm->creds), &scm->creds);
|
|
|
|
|
[AF_UNIX]: Datagram getpeersec
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.
Signed-off-by: Catherine Zhang <cxzhang@watson.ibm.com>
Acked-by: Acked-by: James Morris <jmorris@namei.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
2006-06-29 21:27:47 +02:00
|
|
|
scm_passec(sock, msg, scm);
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
if (!scm->fp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scm_detach_fds(msg, scm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* __LINUX_NET_SCM_H */
|
|
|
|
|