2005-04-16 18:20:36 -04:00
|
|
|
#ifndef __LINUX_NET_SCM_H
|
|
|
|
#define __LINUX_NET_SCM_H
|
|
|
|
|
|
|
|
#include <linux/limits.h>
|
|
|
|
#include <linux/net.h>
|
2006-08-02 17:12:06 -04:00
|
|
|
#include <linux/security.h>
|
2007-10-19 02:40:14 -04:00
|
|
|
#include <linux/pid.h>
|
|
|
|
#include <linux/nsproxy.h>
|
2005-04-16 18:20:36 -04:00
|
|
|
|
|
|
|
/* Well, we should have at least one descriptor open
|
|
|
|
* to accept passed FDs 8)
|
|
|
|
*/
|
2007-07-17 07:03:48 -04:00
|
|
|
#define SCM_MAX_FD 255
|
2005-04-16 18:20:36 -04:00
|
|
|
|
2009-11-02 22:26:03 -05:00
|
|
|
struct scm_fp_list {
|
2008-11-06 03:37:40 -05:00
|
|
|
struct list_head list;
|
|
|
|
int count;
|
|
|
|
struct file *fp[SCM_MAX_FD];
|
2005-04-16 18:20:36 -04:00
|
|
|
};
|
|
|
|
|
2009-11-02 22:26:03 -05:00
|
|
|
struct scm_cookie {
|
2010-06-12 23:32:34 -04:00
|
|
|
struct pid *pid; /* Skb credentials */
|
|
|
|
const struct cred *cred;
|
2005-04-16 18:20:36 -04:00
|
|
|
struct scm_fp_list *fp; /* Passed files */
|
2010-06-12 23:27:04 -04:00
|
|
|
struct ucred creds; /* Skb credentials */
|
[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 15:27:47 -04:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
2006-08-02 17:12:06 -04: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 15:27:47 -04:00
|
|
|
#endif
|
2005-04-16 18:20:36 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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 17:12:06 -04: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 */
|
|
|
|
|
2010-06-12 23:32:34 -04:00
|
|
|
static __inline__ void scm_set_cred(struct scm_cookie *scm,
|
|
|
|
struct pid *pid, const struct cred *cred)
|
|
|
|
{
|
|
|
|
scm->pid = get_pid(pid);
|
|
|
|
scm->cred = get_cred(cred);
|
|
|
|
cred_to_ucred(pid, cred, &scm->creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ void scm_destroy_cred(struct scm_cookie *scm)
|
|
|
|
{
|
|
|
|
put_pid(scm->pid);
|
|
|
|
scm->pid = NULL;
|
|
|
|
|
|
|
|
if (scm->cred)
|
|
|
|
put_cred(scm->cred);
|
|
|
|
scm->cred = NULL;
|
|
|
|
}
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
static __inline__ void scm_destroy(struct scm_cookie *scm)
|
|
|
|
{
|
2010-06-12 23:32:34 -04:00
|
|
|
scm_destroy_cred(scm);
|
2005-04-16 18:20:36 -04:00
|
|
|
if (scm && scm->fp)
|
|
|
|
__scm_destroy(scm);
|
|
|
|
}
|
|
|
|
|
|
|
|
static __inline__ int scm_send(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct scm_cookie *scm)
|
|
|
|
{
|
2010-06-12 23:32:34 -04:00
|
|
|
scm_set_cred(scm, task_tgid(current), current_cred());
|
2006-03-21 00:31:51 -05:00
|
|
|
scm->fp = NULL;
|
2006-08-02 17:12:06 -04:00
|
|
|
unix_get_peersec_dgram(sock, scm);
|
2005-04-16 18:20:36 -04: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 15:27:47 -04:00
|
|
|
#ifdef CONFIG_SECURITY_NETWORK
|
|
|
|
static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
|
|
|
|
{
|
2006-08-02 17:12:06 -04: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 15:27:47 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline void scm_passec(struct socket *sock, struct msghdr *msg, struct scm_cookie *scm)
|
|
|
|
{ }
|
|
|
|
#endif /* CONFIG_SECURITY_NETWORK */
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
static __inline__ void scm_recv(struct socket *sock, struct msghdr *msg,
|
|
|
|
struct scm_cookie *scm, int flags)
|
|
|
|
{
|
2009-11-02 22:26:03 -05:00
|
|
|
if (!msg->msg_control) {
|
2005-04-16 18:20:36 -04:00
|
|
|
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);
|
|
|
|
|
2010-06-12 23:32:34 -04:00
|
|
|
scm_destroy_cred(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 15:27:47 -04:00
|
|
|
scm_passec(sock, msg, scm);
|
|
|
|
|
2005-04-16 18:20:36 -04:00
|
|
|
if (!scm->fp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
scm_detach_fds(msg, scm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* __LINUX_NET_SCM_H */
|
|
|
|
|