Skip to contentSkip to conversation

There is a problem where packets being sent by the TUN driver are not

RFC PATCH In review Paul Moore 2 participants 3 messages Jul 1, 2009 – Jul 2, 2009

Subsystems (4)
lore
Share & tools
lore

Paul Moore

author
#0 lore raw

RFC PATCH 1/1 There is a problem where packets being sent by the TUN driver are not

correctly handled by SELinux in the postrouting code. The issue is that the SELinux network access controls rely on a packet's associated sock, when present, for it's security label. The TUN driver does create a sock to send network traffic but it only calls into the LSM/SELinux code once via the security_sk_alloc() hook which never fully initializes the sock's label. This patch attempts to correct this problem by adding additional checks in the SELinux security_sk_alloc() alloc code to determine if the TUN driver is allocating a socket and if so, set the socket's label.

NOTE: this is an RFC patch intended to demonstrate a possible solution not discussed earlier; it is crude, untested and only deals with SELinux. Please take a look and see if this approach is even worth pursuing ... thanks.
---

 include/linux/security.h |    7 ++++---
 net/core/sock.c          |    2 +-
 security/capability.c    |    2 +-
 security/security.c      |    4 ++--
 security/selinux/hooks.c |   43 ++++++++++++++++++++++++-------------------
 5 files changed, 32 insertions(+), 26 deletions(-)
include/linux/security.h +4 -3
index 5eff459..3b9ea6e 100644
@@ -44,6 +44,7 @@
 
 struct ctl_table;
 struct audit_krule;
+struct proto;
 
 /*
  * These functions are in security/capability.c and are used
@@ -1562,7 +1563,7 @@ struct security_operations {
 	int (*socket_sock_rcv_skb) (struct sock *sk, struct sk_buff *skb);
 	int (*socket_getpeersec_stream) (struct socket *sock, char __user *optval, int __user *optlen, unsigned len);
 	int (*socket_getpeersec_dgram) (struct socket *sock, struct sk_buff *skb, u32 *secid);
-	int (*sk_alloc_security) (struct sock *sk, int family, gfp_t priority);
+	int (*sk_alloc_security) (struct sock *sk, int family, const struct proto *prot, gfp_t priority);
 	void (*sk_free_security) (struct sock *sk);
 	void (*sk_clone_security) (const struct sock *sk, struct sock *newsk);
 	void (*sk_getsecid) (struct sock *sk, u32 *secid);
@@ -2545,7 +2546,7 @@ int security_sock_rcv_skb(struct sock *sk, struct sk_buff *skb);
 int security_socket_getpeersec_stream(struct socket *sock, char __user *optval,
 				      int __user *optlen, unsigned len);
 int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u32 *secid);
-int security_sk_alloc(struct sock *sk, int family, gfp_t priority);
+int security_sk_alloc(struct sock *sk, int family, const struct proto *prot, gfp_t priority);
 void security_sk_free(struct sock *sk);
 void security_sk_clone(const struct sock *sk, struct sock *newsk);
 void security_sk_classify_flow(struct sock *sk, struct flowi *fl);
@@ -2667,7 +2668,7 @@ static inline int security_socket_getpeersec_dgram(struct socket *sock, struct s
 	return -ENOPROTOOPT;
 }
 
-static inline int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
+static inline int security_sk_alloc(struct sock *sk, int family, const struct proto *prot, gfp_t priority)
 {
 	return 0;
 }
net/core/sock.c +1 -1
index b0ba569..327513c 100644
@@ -947,7 +947,7 @@ static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
 	if (sk != NULL) {
 		kmemcheck_annotate_bitfield(sk, flags);
 
-		if (security_sk_alloc(sk, family, priority))
+		if (security_sk_alloc(sk, family, prot, priority))
 			goto out_free;
 
 		if (!try_module_get(prot->owner))
security/capability.c +1 -1
index 21b6cea..a57eb88 100644
@@ -674,7 +674,7 @@ static int cap_socket_getpeersec_dgram(struct socket *sock,
 	return -ENOPROTOOPT;
 }
 
-static int cap_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
+static int cap_sk_alloc_security(struct sock *sk, int family, const struct proto *prot, gfp_t priority)
 {
 	return 0;
 }
security/security.c +2 -2
index dc7674f..17c5419 100644
@@ -1060,9 +1060,9 @@ int security_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *skb, u
 }
 EXPORT_SYMBOL(security_socket_getpeersec_dgram);
 
-int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
+int security_sk_alloc(struct sock *sk, int family, const struct proto *prot, gfp_t priority)
 {
-	return security_ops->sk_alloc_security(sk, family, priority);
+	return security_ops->sk_alloc_security(sk, family, prot, priority);
 }
 
 void security_sk_free(struct sock *sk)
security/selinux/hooks.c +24 -19

81 lines not on this page · raw message

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 31 lines · Paul Moore

correctly handled by SELinux in the postrouting code. The issue is that the SELinux network access controls rely on a packet's associated sock, when present, for it's security label. The TUN driver does create a sock to send network traffic but it only calls into the LSM/SELinux code once via the security_sk_alloc() hook which never fully initializes the sock's label. This patch attempts to correct this problem by adding additional checks in the SELinux security_sk_alloc() alloc code to determine if the TUN driver is allocating a socket and if so, set the socket's label.

NOTE: this is an RFC patch intended to demonstrate a possible solution not discussed earlier; it is crude, untested and only deals with SELinux. Please take a look and see if this approach is even worth pursuing ... thanks.
---

 include/linux/security.h |    7 ++++---
 net/core/sock.c          |    2 +-
 security/capability.c    |    2 +-
 security/security.c      |    4 ++--
 security/selinux/hooks.c |   43 ++++++++++++++++++++++++-------------------
 5 files changed, 32 insertions(+), 26 deletions(-)
security/selinux/hooks.c +0 -1
index 15c2a08..dde57d1 100644
@@ -298,16 +298,21 @@ static void superblock_free_security(struct super_block *sb)
 	kfree(sbsec);
 }
 
-static int sk_alloc_security(struct sock *sk, int family, gfp_t priority)

security/selinux/hooks.c static void superblock_free_security(struct super_block *sb) · line 310

Quoted 10 lines
security/selinux/hooks.c +3 -0
+static int sk_alloc_security(struct sock *sk, int family, const struct proto *prot, gfp_t priority)
 {
 	struct sk_security_struct *ssec;
+	const struct task_security_struct *tsec = current_security();
 
 	ssec = kzalloc(sizeof(*ssec), priority);
 	if (!ssec)
 		return -ENOMEM;
 
+	if (strcmp(prot->name, "tun") == 0) {

I'm sure that won't go over well.

Question: The allocated sock is then associated with a struct socket in the tun_struct. Should that socket get set up in the usual way?

security/selinux/hooks.c static void superblock_free_security(struct super_block *sb) · line 312

Quoted 2 lines
security/selinux/hooks.c +2 -0
+		ssec->sid = (tsec->sockcreate_sid ? : tsec->sid);
+		/* XXX - probably want to label the socket here too */

Not associated yet, so we can't.

Quoted 30 lines
security/selinux/hooks.c +9 -9
+	} else
+		ssec->sid = SECINITSID_UNLABELED;
 	ssec->peer_sid = SECINITSID_UNLABELED;
-	ssec->sid = SECINITSID_UNLABELED;
 	sk->sk_security = ssec;
 
 	selinux_netlbl_sk_security_reset(ssec);
@@ -3673,32 +3678,32 @@ out:
 static int selinux_socket_post_create(struct socket *sock, int family,
 				      int type, int protocol, int kern)
 {
-	const struct cred *cred = current_cred();
-	const struct task_security_struct *tsec = cred->security;
-	struct inode_security_struct *isec;
+	const struct task_security_struct *tsec = current_security();
+	struct inode_security_struct *isec = SOCK_INODE(sock)->i_security;
 	struct sk_security_struct *sksec;
-	u32 sid, newsid;
+	u32 sid;
 	int err = 0;
 
-	sid = tsec->sid;
-	newsid = tsec->sockcreate_sid;
-
-	isec = SOCK_INODE(sock)->i_security;
+	if (sock->sk)
+		sksec = sock->sk->sk_security;
+	else
+		sksec = NULL;
 

security/selinux/hooks.c out: · line 3697

Quoted 10 lines
security/selinux/hooks.c +4 -4
 	if (kern)
-		isec->sid = SECINITSID_KERNEL;
-	else if (newsid)
-		isec->sid = newsid;
+		sid = SECINITSID_KERNEL;
+	else if (sksec && sksec->sid != SECINITSID_UNLABELED)
+		sid = sksec->sid;
 	else
-		isec->sid = sid;
+		sid = (tsec->sockcreate_sid ? : tsec->sid);

Something went wrong here - isec->sid never gets set now.

Quoted 33 lines
security/selinux/hooks.c +6 -6
 	isec->sclass = socket_type_to_security_class(family, type, protocol);
 	isec->initialized = 1;
 
-	if (sock->sk) {
-		sksec = sock->sk->sk_security;
-		sksec->sid = isec->sid;
+	if (sksec) {
+		/* XXX - end up changing this if kern is true - problem? */
+		sksec->sid = sid;
 		sksec->sclass = isec->sclass;
+		/* XXX - this will be tricky depending on how things work out */
 		err = selinux_netlbl_socket_post_create(sock->sk, family);
 	}
 
@@ -4187,9 +4192,9 @@ out:
 	return 0;
 }
 
-static int selinux_sk_alloc_security(struct sock *sk, int family, gfp_t priority)
+static int selinux_sk_alloc_security(struct sock *sk, int family, const struct proto *prot, gfp_t priority)
 {
-	return sk_alloc_security(sk, family, priority);
+	return sk_alloc_security(sk, family, prot, priority);
 }
 
 static void selinux_sk_free_security(struct sock *sk)
 
 
--

This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

Signature · 8 lines
Stephen Smalley
National Security Agency


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.
author
#2 lore raw
Quoted 38 lines · Stephen Smalley
Quoted 37 lines · Paul Moore

correctly handled by SELinux in the postrouting code. The issue is that the SELinux network access controls rely on a packet's associated sock, when present, for it's security label. The TUN driver does create a sock to send network traffic but it only calls into the LSM/SELinux code once via the security_sk_alloc() hook which never fully initializes the sock's label. This patch attempts to correct this problem by adding additional checks in the SELinux security_sk_alloc() alloc code to determine if the TUN driver is allocating a socket and if so, set the socket's label.

NOTE: this is an RFC patch intended to demonstrate a possible solution not discussed earlier; it is crude, untested and only deals with SELinux.
Please take a look and see if this approach is even worth pursuing ...
thanks. ---

 include/linux/security.h |    7 ++++---
 net/core/sock.c          |    2 +-
 security/capability.c    |    2 +-
 security/security.c      |    4 ++--
 security/selinux/hooks.c |   43

++++++++++++++++++++++++------------------- 5 files changed, 32 insertions(+), 26 deletions(-)

security/selinux/hooks.c +1 -1
index 15c2a08..dde57d1 100644
@@ -298,16 +298,21 @@ static void superblock_free_security(struct
 super_block *sb) kfree(sbsec);
 }
 
-static int sk_alloc_security(struct sock *sk, int family, gfp_t
 priority) +static int sk_alloc_security(struct sock *sk, int family,
 const struct proto *prot, gfp_t priority) {
 	struct sk_security_struct *ssec;
+	const struct task_security_struct *tsec = current_security();
 
Quoted 7 lines

security/selinux/hooks.c static void superblock_free_security(struct super_block *sb) · line 310

Quoted 5 lines
security/selinux/hooks.c +1 -0
 	ssec = kzalloc(sizeof(*ssec), priority);
 	if (!ssec)
 		return -ENOMEM;
 
+	if (strcmp(prot->name, "tun") == 0) {

I'm sure that won't go over well.

Yeah, I'm not real excited about that either.

Quoted 2 lines

Question: The allocated sock is then associated with a struct socket in the tun_struct. Should that socket get set up in the usual way?

For some reason I completely missed the socket in tun_struct, I was fixated on the sock. Let me think about this a bit more and I'll send out another patch, probably next week.

Signature · 8 lines
paul moore
linux @ hp


--
This message was distributed to subscribers of the selinux mailing list.
If you no longer wish to subscribe, send mail to [email protected] with
the words "unsubscribe selinux" without quotes as the message.