Skip to contentSkip to conversation

Fix some multiqueue TUN problems

RFC PATCH Superseded Paul Moore 3 participants 20 messages Nov 29, 2012 – Dec 6, 2012

Subsystems (5)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Paul Moore

author
#0 lore raw

cover Fix some multiqueue TUN problems

A relatively short patchset to fix some problems that have arisen from the multiqueue TUN work. I'm sending this a RFC for now as it changes the LSM TUN interfaces and therefore crosses a few boundaries, e.g. SELinux, although it does so only in an effort to restore the original behavior which was lost in the multiqueue conversion.

I'm not particularly enthused with the idea of passing a void** as a parameter to security_tun_dev_alloc_security() but the alternatives would be to move the tun_struct out of tun.c or have the LSM interface return a pointer (which would require us to return a "fake" non-NULL pointer when the LSM was disabled) ... neither of these seemed like good alternatives to me.

I've compiled the code and booted it without drama, but I haven't really stressed it too much so buyer beware at this point. Comments are always welcome ...

---

Paul Moore (2):
tun: correctly report an error in tun_flow_init()
tun: fix LSM/SELinux labeling of tun/tap devices

 drivers/net/tun.c                 |   16 ++++++++++----
 include/linux/security.h          |   37 ++++++++++++++++++++++-----------
 security/capability.c             |   14 +++++++++---
 security/security.c               |   22 ++++++++++++-------
 security/selinux/hooks.c          |   42 +++++++++++++++++++++++--------------
 security/selinux/include/objsec.h |    4 ++++
 6 files changed, 90 insertions(+), 45 deletions(-)
author
#1 lore raw

RFC PATCH 1/2 tun: correctly report an error in tun_flow_init()

On error, the error code from tun_flow_init() is lost inside tun_set_iff(), this patch fixes this by assigning the tun_flow_init() error code to the "err" variable which is returned by
the tun_flow_init() function on error.

---
 drivers/net/tun.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +2 -1
index 607a3a5..877ffe2 100644
@@ -1605,7 +1605,8 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
 		tun_net_init(dev);
 
-		if (tun_flow_init(tun))
+		err = tun_flow_init(tun);
+		if (err < 0)
 			goto err_free_dev;
 
 		dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |
author
#4 lore raw

RFC PATCH 2/2 tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

---
 drivers/net/tun.c                 |   13 ++++++++---
 include/linux/security.h          |   37 ++++++++++++++++++++++-----------
 security/capability.c             |   14 +++++++++---
 security/security.c               |   22 ++++++++++++-------
 security/selinux/hooks.c          |   42 +++++++++++++++++++++++--------------
 security/selinux/include/objsec.h |    4 ++++
 6 files changed, 88 insertions(+), 44 deletions(-)
drivers/net/tun.c +9 -4
index 877ffe2..85cc924 100644
@@ -182,6 +182,7 @@ struct tun_struct {
 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
 	struct timer_list flow_gc_timer;
 	unsigned long ageing_time;
+	void *security;
 };
 
 static inline u32 tun_hashfn(u32 rxhash)
@@ -465,6 +466,10 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
 	struct tun_file *tfile = file->private_data;
 	int err;
 
+	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
+	if (err < 0)
+		goto out;
+
 	err = -EINVAL;
 	if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
 		goto out;
@@ -1365,6 +1370,7 @@ static void tun_free_netdev(struct net_device *dev)
 	struct tun_struct *tun = netdev_priv(dev);
 
 	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);
 	free_netdev(dev);
 }
 
@@ -1548,9 +1554,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
 		if (tun_not_capable(tun))
 			return -EPERM;
-		err = security_tun_dev_attach(tfile->socket.sk);
-		if (err < 0)
-			return err;
 
 		err = tun_attach(tun, file);
 		if (err < 0)
@@ -1601,7 +1604,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
 		spin_lock_init(&tun->lock);
 
-		security_tun_dev_post_create(&tfile->sk);
+		err = security_tun_dev_alloc_security(&tun->security);
+		if (err < 0)
+			goto err_free_dev;
 
 		tun_net_init(dev);
 
include/linux/security.h +25 -12
index 05e88bd..260e151 100644
@@ -983,17 +983,23 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
  *	tells the LSM to decrement the number of secmark labeling rules loaded
  * @req_classify_flow:
  *	Sets the flow's sid to the openreq sid.
+ * @tun_dev_alloc_security:
+ *	This hook allows a module to allocate a security structure for a TUN
+ *	device.
+ *	@security pointer to a security structure pointer.
+ *	Returns a zero on success, negative values on failure.
+ * @tun_dev_free_security:
+ *	This hook allows a module to free the security structure for a TUN
+ *	device.
+ *	@security pointer to the TUN device's security structure
  * @tun_dev_create:
  *	Check permissions prior to creating a new TUN device.
- * @tun_dev_post_create:
- *	This hook allows a module to update or allocate a per-socket security
- *	structure.
- *	@sk contains the newly created sock structure.
  * @tun_dev_attach:
  *	Check permissions prior to attaching to a persistent TUN device.  This
  *	hook can also be used by the module to update any security state
  *	associated with the TUN device's sock structure.
  *	@sk contains the existing sock structure.
+ *	@security pointer to the TUN device's security structure.
  *
  * Security hooks for XFRM operations.
  *
@@ -1613,9 +1619,10 @@ struct security_operations {
 	void (*secmark_refcount_inc) (void);
 	void (*secmark_refcount_dec) (void);
 	void (*req_classify_flow) (const struct request_sock *req, struct flowi *fl);
-	int (*tun_dev_create)(void);
-	void (*tun_dev_post_create)(struct sock *sk);
-	int (*tun_dev_attach)(struct sock *sk);
+	int (*tun_dev_alloc_security) (void **security);
+	void (*tun_dev_free_security) (void *security);
+	int (*tun_dev_create) (void);
+	int (*tun_dev_attach) (struct sock *sk, void *security);
 #endif	/* CONFIG_SECURITY_NETWORK */
 
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
@@ -2553,9 +2560,10 @@ void security_inet_conn_established(struct sock *sk,
 int security_secmark_relabel_packet(u32 secid);
 void security_secmark_refcount_inc(void);
 void security_secmark_refcount_dec(void);
+int security_tun_dev_alloc_security(void **security);
+void security_tun_dev_free_security(void *security);
 int security_tun_dev_create(void);
-void security_tun_dev_post_create(struct sock *sk);
-int security_tun_dev_attach(struct sock *sk);
+int security_tun_dev_attach(struct sock *sk, void *security);
 
 #else	/* CONFIG_SECURITY_NETWORK */
 static inline int security_unix_stream_connect(struct sock *sock,
@@ -2720,16 +2728,21 @@ static inline void security_secmark_refcount_dec(void)
 {
 }
 
-static inline int security_tun_dev_create(void)
+static inline int security_tun_dev_alloc_security(void **security)
 {
 	return 0;
 }
 
-static inline void security_tun_dev_post_create(struct sock *sk)
+static inline void security_tun_dev_free_security(void *security)
 {
 }
 
-static inline int security_tun_dev_attach(struct sock *sk)
+static inline int security_tun_dev_create(void)
+{
+	return 0;
+}
+
+static inline int security_tun_dev_attach(struct sock *sk, void *security)
 {
 	return 0;
 }
security/capability.c +10 -4
index b14a30c..fd6e2dc 100644
@@ -704,16 +704,21 @@ static void cap_req_classify_flow(const struct request_sock *req,
 {
 }
 
-static int cap_tun_dev_create(void)
+static int cap_tun_dev_alloc_security(void **security)
 {
 	return 0;
 }
 
-static void cap_tun_dev_post_create(struct sock *sk)
+static void cap_tun_dev_free_security(void *security)
+{
+}
+
+static int cap_tun_dev_create(void)
 {
+	return 0;
 }
 
-static int cap_tun_dev_attach(struct sock *sk)
+static int cap_tun_dev_attach(struct sock *sk, void *security)
 {
 	return 0;
 }
@@ -1044,8 +1049,9 @@ void __init security_fixup_ops(struct security_operations *ops)
 	set_to_cap_if_null(ops, secmark_refcount_inc);
 	set_to_cap_if_null(ops, secmark_refcount_dec);
 	set_to_cap_if_null(ops, req_classify_flow);
+	set_to_cap_if_null(ops, tun_dev_alloc_security);
+	set_to_cap_if_null(ops, tun_dev_free_security);
 	set_to_cap_if_null(ops, tun_dev_create);
-	set_to_cap_if_null(ops, tun_dev_post_create);
 	set_to_cap_if_null(ops, tun_dev_attach);
 #endif	/* CONFIG_SECURITY_NETWORK */
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
security/security.c +14 -8
index 8dcd4ae..613ad36 100644
@@ -1244,21 +1244,27 @@ void security_secmark_refcount_dec(void)
 }
 EXPORT_SYMBOL(security_secmark_refcount_dec);
 
-int security_tun_dev_create(void)
+int security_tun_dev_alloc_security(void **security)
 {
-	return security_ops->tun_dev_create();
+	return security_ops->tun_dev_alloc_security(security);
 }
-EXPORT_SYMBOL(security_tun_dev_create);
+EXPORT_SYMBOL(security_tun_dev_alloc_security);
 
-void security_tun_dev_post_create(struct sock *sk)
+void security_tun_dev_free_security(void *security)
 {
-	return security_ops->tun_dev_post_create(sk);
+	security_ops->tun_dev_free_security(security);
 }
-EXPORT_SYMBOL(security_tun_dev_post_create);
+EXPORT_SYMBOL(security_tun_dev_free_security);
+
+int security_tun_dev_create(void)
+{
+	return security_ops->tun_dev_create();
+}
+EXPORT_SYMBOL(security_tun_dev_create);
 
-int security_tun_dev_attach(struct sock *sk)
+int security_tun_dev_attach(struct sock *sk, void *security)
 {
-	return security_ops->tun_dev_attach(sk);
+	return security_ops->tun_dev_attach(sk, security);
 }
 EXPORT_SYMBOL(security_tun_dev_attach);
 
security/selinux/hooks.c +26 -16

74 lines not on this page · raw message

security/selinux/include/objsec.h +4 -0
index 26c7eee..aa47bca 100644
@@ -110,6 +110,10 @@ struct sk_security_struct {
 	u16 sclass;			/* sock security class */
 };
 
+struct tun_security_struct {
+	u32 sid;			/* SID for the tun device sockets */
+};
+
 struct key_security_struct {
 	u32 sid;	/* SID of key */
 };
#5 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 9 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

Quoted 4 lines

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

Hi Paul, thanks for the patchset. I've one question, see below.

Quoted 283 lines
---
 drivers/net/tun.c                 |   13 ++++++++---
 include/linux/security.h          |   37 ++++++++++++++++++++++-----------
 security/capability.c             |   14 +++++++++---
 security/security.c               |   22 ++++++++++++-------
 security/selinux/hooks.c          |   42 +++++++++++++++++++++++--------------
 security/selinux/include/objsec.h |    4 ++++
 6 files changed, 88 insertions(+), 44 deletions(-)
drivers/net/tun.c +9 -4
index 877ffe2..85cc924 100644
@@ -182,6 +182,7 @@ struct tun_struct {
 	struct hlist_head flows[TUN_NUM_FLOW_ENTRIES];
 	struct timer_list flow_gc_timer;
 	unsigned long ageing_time;
+	void *security;
 };
 
 static inline u32 tun_hashfn(u32 rxhash)
@@ -465,6 +466,10 @@ static int tun_attach(struct tun_struct *tun, struct file *file)
 	struct tun_file *tfile = file->private_data;
 	int err;
 
+	err = security_tun_dev_attach(tfile->socket.sk, tun->security);
+	if (err < 0)
+		goto out;
+
 	err = -EINVAL;
 	if (rcu_dereference_protected(tfile->tun, lockdep_rtnl_is_held()))
 		goto out;
@@ -1365,6 +1370,7 @@ static void tun_free_netdev(struct net_device *dev)
 	struct tun_struct *tun = netdev_priv(dev);
 
 	tun_flow_uninit(tun);
+	security_tun_dev_free_security(tun->security);
 	free_netdev(dev);
 }
 
@@ -1548,9 +1554,6 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
 		if (tun_not_capable(tun))
 			return -EPERM;
-		err = security_tun_dev_attach(tfile->socket.sk);
-		if (err < 0)
-			return err;
 
 		err = tun_attach(tun, file);
 		if (err < 0)
@@ -1601,7 +1604,9 @@ static int tun_set_iff(struct net *net, struct file *file, struct ifreq *ifr)
 
 		spin_lock_init(&tun->lock);
 
-		security_tun_dev_post_create(&tfile->sk);
+		err = security_tun_dev_alloc_security(&tun->security);
+		if (err < 0)
+			goto err_free_dev;
 
 		tun_net_init(dev);
 
include/linux/security.h +25 -12
index 05e88bd..260e151 100644
@@ -983,17 +983,23 @@ static inline void security_free_mnt_opts(struct security_mnt_opts *opts)
  *	tells the LSM to decrement the number of secmark labeling rules loaded
  * @req_classify_flow:
  *	Sets the flow's sid to the openreq sid.
+ * @tun_dev_alloc_security:
+ *	This hook allows a module to allocate a security structure for a TUN
+ *	device.
+ *	@security pointer to a security structure pointer.
+ *	Returns a zero on success, negative values on failure.
+ * @tun_dev_free_security:
+ *	This hook allows a module to free the security structure for a TUN
+ *	device.
+ *	@security pointer to the TUN device's security structure
  * @tun_dev_create:
  *	Check permissions prior to creating a new TUN device.
- * @tun_dev_post_create:
- *	This hook allows a module to update or allocate a per-socket security
- *	structure.
- *	@sk contains the newly created sock structure.
  * @tun_dev_attach:
  *	Check permissions prior to attaching to a persistent TUN device.  This
  *	hook can also be used by the module to update any security state
  *	associated with the TUN device's sock structure.
  *	@sk contains the existing sock structure.
+ *	@security pointer to the TUN device's security structure.
  *
  * Security hooks for XFRM operations.
  *
@@ -1613,9 +1619,10 @@ struct security_operations {
 	void (*secmark_refcount_inc) (void);
 	void (*secmark_refcount_dec) (void);
 	void (*req_classify_flow) (const struct request_sock *req, struct flowi *fl);
-	int (*tun_dev_create)(void);
-	void (*tun_dev_post_create)(struct sock *sk);
-	int (*tun_dev_attach)(struct sock *sk);
+	int (*tun_dev_alloc_security) (void **security);
+	void (*tun_dev_free_security) (void *security);
+	int (*tun_dev_create) (void);
+	int (*tun_dev_attach) (struct sock *sk, void *security);
 #endif	/* CONFIG_SECURITY_NETWORK */
 
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
@@ -2553,9 +2560,10 @@ void security_inet_conn_established(struct sock *sk,
 int security_secmark_relabel_packet(u32 secid);
 void security_secmark_refcount_inc(void);
 void security_secmark_refcount_dec(void);
+int security_tun_dev_alloc_security(void **security);
+void security_tun_dev_free_security(void *security);
 int security_tun_dev_create(void);
-void security_tun_dev_post_create(struct sock *sk);
-int security_tun_dev_attach(struct sock *sk);
+int security_tun_dev_attach(struct sock *sk, void *security);
 
 #else	/* CONFIG_SECURITY_NETWORK */
 static inline int security_unix_stream_connect(struct sock *sock,
@@ -2720,16 +2728,21 @@ static inline void security_secmark_refcount_dec(void)
 {
 }
 
-static inline int security_tun_dev_create(void)
+static inline int security_tun_dev_alloc_security(void **security)
 {
 	return 0;
 }
 
-static inline void security_tun_dev_post_create(struct sock *sk)
+static inline void security_tun_dev_free_security(void *security)
 {
 }
 
-static inline int security_tun_dev_attach(struct sock *sk)
+static inline int security_tun_dev_create(void)
+{
+	return 0;
+}
+
+static inline int security_tun_dev_attach(struct sock *sk, void *security)
 {
 	return 0;
 }
security/capability.c +10 -4
index b14a30c..fd6e2dc 100644
@@ -704,16 +704,21 @@ static void cap_req_classify_flow(const struct request_sock *req,
 {
 }
 
-static int cap_tun_dev_create(void)
+static int cap_tun_dev_alloc_security(void **security)
 {
 	return 0;
 }
 
-static void cap_tun_dev_post_create(struct sock *sk)
+static void cap_tun_dev_free_security(void *security)
+{
+}
+
+static int cap_tun_dev_create(void)
 {
+	return 0;
 }
 
-static int cap_tun_dev_attach(struct sock *sk)
+static int cap_tun_dev_attach(struct sock *sk, void *security)
 {
 	return 0;
 }
@@ -1044,8 +1049,9 @@ void __init security_fixup_ops(struct security_operations *ops)
 	set_to_cap_if_null(ops, secmark_refcount_inc);
 	set_to_cap_if_null(ops, secmark_refcount_dec);
 	set_to_cap_if_null(ops, req_classify_flow);
+	set_to_cap_if_null(ops, tun_dev_alloc_security);
+	set_to_cap_if_null(ops, tun_dev_free_security);
 	set_to_cap_if_null(ops, tun_dev_create);
-	set_to_cap_if_null(ops, tun_dev_post_create);
 	set_to_cap_if_null(ops, tun_dev_attach);
 #endif	/* CONFIG_SECURITY_NETWORK */
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
security/security.c +14 -8
index 8dcd4ae..613ad36 100644
@@ -1244,21 +1244,27 @@ void security_secmark_refcount_dec(void)
 }
 EXPORT_SYMBOL(security_secmark_refcount_dec);
 
-int security_tun_dev_create(void)
+int security_tun_dev_alloc_security(void **security)
 {
-	return security_ops->tun_dev_create();
+	return security_ops->tun_dev_alloc_security(security);
 }
-EXPORT_SYMBOL(security_tun_dev_create);
+EXPORT_SYMBOL(security_tun_dev_alloc_security);
 
-void security_tun_dev_post_create(struct sock *sk)
+void security_tun_dev_free_security(void *security)
 {
-	return security_ops->tun_dev_post_create(sk);
+	security_ops->tun_dev_free_security(security);
 }
-EXPORT_SYMBOL(security_tun_dev_post_create);
+EXPORT_SYMBOL(security_tun_dev_free_security);
+
+int security_tun_dev_create(void)
+{
+	return security_ops->tun_dev_create();
+}
+EXPORT_SYMBOL(security_tun_dev_create);
 
-int security_tun_dev_attach(struct sock *sk)
+int security_tun_dev_attach(struct sock *sk, void *security)
 {
-	return security_ops->tun_dev_attach(sk);
+	return security_ops->tun_dev_attach(sk, security);
 }
 EXPORT_SYMBOL(security_tun_dev_attach);
 
security/selinux/hooks.c +21 -13
index 61a5336..67b3423 100644
@@ -4414,40 +4414,49 @@ static int selinux_tun_dev_create(void)
 			    NULL);
 }
 
-static void selinux_tun_dev_post_create(struct sock *sk)
+static int selinux_tun_dev_alloc_security(void **security)
 {
-	struct sk_security_struct *sksec = sk->sk_security;
+	struct tun_security_struct *tunsec;
 
-	/* we don't currently perform any NetLabel based labeling here and it
-	 * isn't clear that we would want to do so anyway; while we could apply
-	 * labeling without the support of the TUN user the resulting labeled
-	 * traffic from the other end of the connection would almost certainly
-	 * cause confusion to the TUN user that had no idea network labeling
-	 * protocols were being used */
+	tunsec = kzalloc(sizeof(*tunsec), GFP_KERNEL);
+	if (!tunsec)
+		return -ENOMEM;
+	tunsec->sid = current_sid();
 
-	/* see the comments in selinux_tun_dev_create() about why we don't use
-	 * the sockcreate SID here */
+	*security = tunsec;
+	return 0;
+}
 
-	sksec->sid = current_sid();
-	sksec->sclass = SECCLASS_TUN_SOCKET;
+static void selinux_tun_dev_free_security(void *security)
+{
+	kfree(security);
 }
 
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 {
+	struct tun_security_struct *tunsec = security;
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here and it
+	 * isn't clear that we would want to do so anyway; while we could apply
+	 * labeling without the support of the TUN user the resulting labeled
+	 * traffic from the other end of the connection would almost certainly
+	 * cause confusion to the TUN user that had no idea network labeling
+	 * protocols were being used */
+
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 			   TUN_SOCKET__RELABELFROM, NULL);
 	if (err)

security/selinux/hooks.c static int selinux_tun_dev_create(void) · line 4459

Quoted 10 lines
security/selinux/hooks.c +3 -2
 		return err;
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 			   TUN_SOCKET__RELABELTO, NULL);
 	if (err)
 		return err;
 
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent device, looks like we need change the sid of tunsec like in the past.

Thanks

Quoted 34 lines
security/selinux/hooks.c +2 -1
 	return 0;
 }
@@ -5642,8 +5651,9 @@ static struct security_operations selinux_ops = {
 	.secmark_refcount_inc =		selinux_secmark_refcount_inc,
 	.secmark_refcount_dec =		selinux_secmark_refcount_dec,
 	.req_classify_flow =		selinux_req_classify_flow,
+	.tun_dev_alloc_security =	selinux_tun_dev_alloc_security,
+	.tun_dev_free_security =	selinux_tun_dev_free_security,
 	.tun_dev_create =		selinux_tun_dev_create,
-	.tun_dev_post_create = 		selinux_tun_dev_post_create,
 	.tun_dev_attach =		selinux_tun_dev_attach,
 
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
security/selinux/include/objsec.h +4 -1
index 26c7eee..aa47bca 100644
@@ -110,6 +110,10 @@ struct sk_security_struct {
 	u16 sclass;			/* sock security class */
 };
 
+struct tun_security_struct {
+	u32 sid;			/* SID for the tun device sockets */
+};
+
 struct key_security_struct {
 	u32 sid;	/* SID of key */
 };
 
--

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

author
#6 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 14 lines · Jason Wang
Quoted 13 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

...

Quoted 30 lines
Quoted 30 lines
(quoted diff) +4 -2
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 
 {
 
+	struct tun_security_struct *tunsec = security;
 
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here ...
 
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELFROM, NULL);
 
 	if (err)
 
 		return err;
 
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELTO, NULL);
 
 	if (err)
 
 		return err;
Quoted 7 lines

security/selinux/hooks.c static int selinux_tun_dev_create(void) · line 4459

Quoted 3 lines
security/selinux/hooks.c +2 -1
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent device, looks like we need change the sid of tunsec like in the past.

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

Signature · 2 lines
paul moore
security and virtualization @ redhat
#7 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 56 lines · Paul Moore
Quoted 14 lines · Jason Wang
Quoted 13 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

...

Quoted 30 lines
Quoted 30 lines
(quoted diff) +4 -2
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 
 {
 
+	struct tun_security_struct *tunsec = security;
 
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here ...
 
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELFROM, NULL);
 
 	if (err)
 
 		return err;
 
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELTO, NULL);
 
 	if (err)
 
 		return err;
Quoted 7 lines

security/selinux/hooks.c static int selinux_tun_dev_create(void) · line 4459

Quoted 3 lines
security/selinux/hooks.c +2 -1
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent device, looks like we need change the sid of tunsec like in the past.

Quoted 2 lines

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Quoted 6 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Thanks

#8 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 68 lines · Jason Wang
Quoted 56 lines · Paul Moore
Quoted 14 lines · Jason Wang
Quoted 13 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

...

Quoted 30 lines
Quoted 30 lines
(quoted diff) +4 -2
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 
 {
 
+	struct tun_security_struct *tunsec = security;
 
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here ...
 
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELFROM, NULL);
 
 	if (err)
 
 		return err;
 
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELTO, NULL);
 
 	if (err)
 
 		return err;
Quoted 7 lines

security/selinux/hooks.c static int selinux_tun_dev_create(void) · line 4459

Quoted 3 lines
security/selinux/hooks.c +2 -1
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent device, looks like we need change the sid of tunsec like in the past.

Quoted 2 lines

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

Quoted 7 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Basically, it looks like currently once you get a tun fd, you can attach it to any device even if normally selinux would prevent you from accessing it.

If we reuse selinux_tun_dev_attach, we won't need to
change selinux policy, with a new capability we will need to change it to allow libvirt to do TUNSETQUEUE.

Quoted 3 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Is this a problem? You can always
attach
set queue
detach

and it would be hard to prevent this ...

Quoted 12 lines
Quoted 6 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Thanks

author
#12 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Jason Wang
Quoted 12 lines · Jason Wang
Quoted 2 lines · Paul Moore

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

Quoted 7 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct LSM blob and SELinux sets some internal blob state to record the label of the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process. *** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

1. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob set during either A2 or B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 2 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Since you wrote that code will you be submitting a patch to fix that problem?

Quoted 9 lines
Quoted 5 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Unless I've missed something in your changes, the multiqueue code never calls any socket code which ends up calling {security,selinux}_socket_post_create(); I believe you only call sk_alloc() which ends up calling {security,selinux}_sk_alloc() which sets SECINITSID_UNLABELED (I mistakenly wrote 0 instead in my earlier email which is techincally SECSID_NULL). Either way, I still think the logic I originally described above is correct.

Signature · 2 lines
paul moore
security and virtualization @ redhat
#13 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 49 lines · Paul Moore
Quoted 12 lines · Jason Wang
Quoted 2 lines · Paul Moore

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

Quoted 7 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct LSM blob and SELinux sets some internal blob state to record the label of the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process. *** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 7 lines

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

1. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob set during either A2 or B1. No authorization is done at this point since the socket is new/unlabeled.

Here's what bothers me. libvirt currently opens tun and passes fd to qemu.
What would prevent qemu from attaching fd using TUNSETQUEUE to another device it does not own?

Quoted 25 lines
Quoted 2 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Since you wrote that code will you be submitting a patch to fix that problem?

Quoted 9 lines
Quoted 5 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Unless I've missed something in your changes, the multiqueue code never calls any socket code which ends up calling {security,selinux}_socket_post_create(); I believe you only call sk_alloc() which ends up calling {security,selinux}_sk_alloc() which sets SECINITSID_UNLABELED (I mistakenly wrote 0 instead in my earlier email which is techincally SECSID_NULL). Either way, I still think the logic I originally described above is correct.

Signature · 2 lines
paul moore
security and virtualization @ redhat
author
#14 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 33 lines · Michael S. Tsirkin
Quoted 32 lines · Paul Moore

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct LSM blob and SELinux sets some internal blob state to record the label of the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process. *** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

Quoted 8 lines
Quoted 4 lines

1. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob set during either A2 or B1. No authorization is done at this point since the socket is new/unlabeled.

Here's what bothers me. libvirt currently opens tun and passes fd to qemu. What would prevent qemu from attaching fd using TUNSETQUEUE to another device it does not own?

True, assuming all the above is correct and that I'm understanding it correctly (Jason?), we should probably add a new SELinux access control for TUNSETQUEUE.

The current DAC code exists in tun_not_capable().

Signature · 2 lines
paul moore
security and virtualization @ redhat
#19 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Paul Moore
Quoted 54 lines · Paul Moore
Quoted 16 lines · Jason Wang
Quoted 3 lines · Paul Moore

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you
elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in
fact also the device's since the socket were persistent also) to the sid of
process that calls TUNSETIFF.

Quoted 8 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach
more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct LSM blob and SELinux sets some internal blob state to record the label of the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process. *** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 8 lines

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

1. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob set during either A2 or B1. No authorization is done at this point since the socket is new/unlabeled.

This looks fine to me.

Quoted 5 lines
Quoted 2 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Since you wrote that code will you be submitting a patch to fix that problem?

Yes, I will fix it.

Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case
where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Quoted 8 lines

Unless I've missed something in your changes, the multiqueue code never calls any socket code which ends up calling {security,selinux}_socket_post_create(); I believe you only call sk_alloc() which ends up calling
{security,selinux}_sk_alloc() which sets SECINITSID_UNLABELED (I mistakenly wrote 0 instead in my earlier email which is techincally SECSID_NULL). Either way, I still think the logic I originally described above is correct.

Yes, I was wrong. Thanks for the checking.

#9 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Michael S. Tsirkin
Quoted 72 lines · Michael S. Tsirkin
Quoted 64 lines · Jason Wang
Quoted 47 lines · Paul Moore
Quoted 14 lines · Jason Wang
Quoted 13 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

...

Quoted 30 lines
Quoted 30 lines
(quoted diff) +4 -2
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 
 {
 
+	struct tun_security_struct *tunsec = security;
 
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here ...
 
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELFROM, NULL);
 
 	if (err)
 
 		return err;
 
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELTO, NULL);
 
 	if (err)
 
 		return err;
Quoted 8 lines
Quoted 6 lines

security/selinux/hooks.c static int selinux_tun_dev_create(void) · line 4459

Quoted 3 lines
security/selinux/hooks.c +2 -1
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent device, looks like we need change the sid of tunsec like in the past.

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

Quoted 7 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Quoted 3 lines

Basically, it looks like currently once you get a tun fd, you can attach it to any device even if normally selinux would prevent you from accessing it.

Yes some checking during TUNSETQUEUE is missed.

Quoted 4 lines

If we reuse selinux_tun_dev_attach, we won't need to
change selinux policy, with a new capability we will need to change it to allow libvirt to do TUNSETQUEUE.

Also needed for qemu too since it may call TUNSETQUEUE when guest wants to change the number of queues.

Quoted 8 lines
Quoted 2 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Is this a problem? You can always
attach
set queue
detach

and it would be hard to prevent this ...

Currently, the following steps is allowed:

1. fd1 = open("/dev/net/tun");
2. tunsetiff(fd1, "tap0");
3. tunsetpersistent("tap0");
4. close(fd1);
5. fd2 = open("/dev/net/tun");
6. tunsetqueue(fd2, "tap0);

Looks like step 6 should be forbidden since:

- no fd/sockets were attached to the device, we need use TUNSETIFF instead to keep the API as we used do in single queue tun
- we need update the security information in tun_struct just like what we discussed in this mail
- it may also miss checks in TUNSETIFF

Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Thanks

#15 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Paul Moore
Quoted 38 lines · Paul Moore
Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Paul Moore

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct LSM blob and SELinux sets some internal blob state to record the label of the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process. *** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

1. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob set during either A2 or B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 6 lines
Quoted 3 lines

Here's what bothers me. libvirt currently opens tun and passes fd to qemu. What would prevent qemu from attaching fd using TUNSETQUEUE to another device it does not own?

True, assuming all the above is correct and that I'm understanding it correctly (Jason?), we should probably add a new SELinux access control for TUNSETQUEUE.

Yes, we need make sure qemu can call TUNSETQUEUE for the device it does not own.

Quoted 3 lines

The current DAC code exists in tun_not_capable().

#10 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Jason Wang
Quoted 77 lines · Jason Wang
Quoted 72 lines · Michael S. Tsirkin
Quoted 64 lines · Jason Wang
Quoted 47 lines · Paul Moore
Quoted 14 lines · Jason Wang
Quoted 13 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced with the multiqueue patchset. The problem stems from the fact that the multiqueue work changed the relationship between the tun device and its associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted for the life of the userspace connection (fd open). For non-persistent devices this is not an issue, but for persistent devices this can cause the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g. SELinux labeling information, persist for the lifetime of the tun device.

...

Quoted 30 lines
Quoted 30 lines
(quoted diff) +4 -2
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 
 {
 
+	struct tun_security_struct *tunsec = security;
 
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here ...
 
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELFROM, NULL);
 
 	if (err)
 
 		return err;
 
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELTO, NULL);
 
 	if (err)
 
 		return err;
Quoted 8 lines
Quoted 6 lines

security/selinux/hooks.c static int selinux_tun_dev_create(void) · line 4459

Quoted 3 lines
security/selinux/hooks.c +2 -1
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent device, looks like we need change the sid of tunsec like in the past.

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

Quoted 7 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Quoted 3 lines

Basically, it looks like currently once you get a tun fd, you can attach it to any device even if normally selinux would prevent you from accessing it.

Quoted 8 lines

Yes some checking during TUNSETQUEUE is missed.

Quoted 4 lines

If we reuse selinux_tun_dev_attach, we won't need to
change selinux policy, with a new capability we will need to change it to allow libvirt to do TUNSETQUEUE.

Also needed for qemu too since it may call TUNSETQUEUE when guest wants to change the number of queues.

Hmm that's nasty. If you allow qemu to do TUNSETQUEUE then how do you prevent it from attaching to some other tun? Maybe we can extend TUNSETQUEUE or add another ioctl to mark a queue active/inactive? Probably control transmit and receive being active separately as well.

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap device with no file attached. It should be a bug and need to be fixed.

Is this a problem? You can always
attach
set queue
detach

and it would be hard to prevent this ...

Quoted 8 lines

Currently, the following steps is allowed:

1. fd1 = open("/dev/net/tun");
2. tunsetiff(fd1, "tap0");
3. tunsetpersistent("tap0");
4. close(fd1);
5. fd2 = open("/dev/net/tun");
6. tunsetqueue(fd2, "tap0);

Allowed for libvirt, right? Not for qemu.

Quoted 7 lines

Looks like step 6 should be forbidden since:

- no fd/sockets were attached to the device, we need use TUNSETIFF instead to keep the API as we used do in single queue tun
- we need update the security information in tun_struct just like what we discussed in this mail
- it may also miss checks in TUNSETIFF

We need to fix the checks anyway. Basically if you allow a queue to be the only fd that is attached
(and I don't see how you can prevent this since userspace does not expect close to fail),
I don't see why one way to get to this state should be legal and another illegal.

The rest is implementation detail.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 5 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the TUN device label regardless in this case.

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Thanks

#16 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Jason Wang
Quoted 42 lines · Jason Wang
Quoted 41 lines · Paul Moore
Quoted 40 lines · Michael S. Tsirkin
Quoted 28 lines · Paul Moore

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct LSM blob and SELinux sets some internal blob state to record the label of the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process. *** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 8 lines

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

1. Call security_tun_dev_attach() which sets the label of the TUN socket to match the label stored in the tun_struct LSM blob set during either A2 or B1. No authorization is done at this point since the socket is new/unlabeled.

Here's what bothers me. libvirt currently opens tun and passes fd to qemu. What would prevent qemu from attaching fd using TUNSETQUEUE to another device it does not own?

Quoted 6 lines
Quoted 3 lines

True, assuming all the above is correct and that I'm understanding it correctly (Jason?), we should probably add a new SELinux access control for TUNSETQUEUE.

Yes, we need make sure qemu can call TUNSETQUEUE for the device it does not own.

Meaning can *not* call?

Quoted 3 lines
Quoted 3 lines

The current DAC code exists in tun_not_capable().

#11 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Michael S. Tsirkin
Quoted 101 lines · Michael S. Tsirkin
Quoted 100 lines · Jason Wang
Quoted 89 lines · Michael S. Tsirkin
Quoted 79 lines · Jason Wang
Quoted 65 lines · Paul Moore
Quoted 21 lines · Jason Wang
Quoted 20 lines · Paul Moore

This patch corrects some problems with LSM/SELinux that were introduced
with the multiqueue patchset. The problem stems from the fact that the
multiqueue work changed the relationship between the tun device and its
associated socket; before the socket persisted for the life of the device, however after the multiqueue changes the socket only persisted
for the life of the userspace connection (fd open). For non-persistent
devices this is not an issue, but for persistent devices this can cause
the tun device to lose its SELinux label.

We correct this problem by adding an opaque LSM security blob to the tun device struct which allows us to have the LSM security state, e.g.
SELinux labeling information, persist for the lifetime of the tun device.

...

Quoted 30 lines
Quoted 30 lines
(quoted diff) +4 -2
-static int selinux_tun_dev_attach(struct sock *sk)
+static int selinux_tun_dev_attach(struct sock *sk, void *security)
 
 {
 
+	struct tun_security_struct *tunsec = security;
 
 	struct sk_security_struct *sksec = sk->sk_security;
 	u32 sid = current_sid();
 	int err;
 
+	/* we don't currently perform any NetLabel based labeling here ...
 
 	err = avc_has_perm(sid, sksec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELFROM, NULL);
 
 	if (err)
 
 		return err;
 
-	err = avc_has_perm(sid, sid, SECCLASS_TUN_SOCKET,
+	err = avc_has_perm(sid, tunsec->sid, SECCLASS_TUN_SOCKET,
 
 			   TUN_SOCKET__RELABELTO, NULL);
 
 	if (err)
 
 		return err;
Quoted 9 lines

Review

Quoted 3 lines
(quoted diff) +2 -1
-	sksec->sid = sid;
+	sksec->sid = tunsec->sid;
+	sksec->sclass = SECCLASS_TUN_SOCKET;

I'm not sure whether this is correct, looks like we need to differ between
TUNSETQUEUE and TUNSETIFF. When userspace call TUNSETIFF for persistent
device, looks like we need change the sid of tunsec like in the past.

Quoted 3 lines

It may be that I'm misunderstanding TUNSETQUEUE and/or TUNSETIFF. Can you
elaborate as to why they should be different?

If I understand correctly, before multiqueue patchset, TUNSETIFF is used to:

1) Create the tun/tap network device
2) For persistent device, re-attach the fd to the network device / socket. In this case, we call selinux_tun_dev_attch() to relabel the socket sid (in fact also the device's since the socket were persistent also) to the sid of process that calls TUNSETIFF.

Quoted 8 lines

So, after the changes of multiqueue, we need try to preserve those policy. The interesting part is the introducing of TUNSETQUEUE, it's used to attach more file descriptors/sockets to a tun/tap device after at least one file descriptor were attached to the tun/tap device through TUNSETIFF. So I think maybe we need differ those two ioctls. This patch looks fine for TUNSETQUEUE, but for TUNSETIFF, we need relabel the tunsec to the process that calling TUNSETIFF for persistent device?

Quoted 3 lines

Basically, it looks like currently once you get a tun fd, you can attach it to any device even if normally selinux would prevent you from accessing it.

Yes some checking during TUNSETQUEUE is missed.

Quoted 3 lines

If we reuse selinux_tun_dev_attach, we won't need to
change selinux policy, with a new capability we will need to change it to allow libvirt to do TUNSETQUEUE.

Quoted 5 lines
Quoted 2 lines

Also needed for qemu too since it may call TUNSETQUEUE when guest wants to change the number of queues.

Hmm that's nasty. If you allow qemu to do TUNSETQUEUE then how do you prevent it from attaching to some other tun?

Currently, we do the uid & gid check in tun_not_capable() also for TUNSETQUEUE. So the fd can't be attach to the device if current process is not the owner. And if it's the owner, is it harm to allow the fd to be attached to another device?

There's no state information of the tun stored in the tun_file, and it was something like after you open a fd of tun, you can call TUNSETIFF to any tun if the owner is current process.

Quoted 3 lines

Maybe we can extend TUNSETQUEUE or add another ioctl to mark a queue active/inactive? Probably control transmit and receive being active separately as well.

It's better to extend current TUNSETQUEUE if needed, for controling transmitting and receiving, we can add more parameters to this ioctl (e.g. IFF_TX_ENABLE, IFF_TX_DISABLE)

Quoted 14 lines
Quoted 14 lines
Quoted 11 lines
Quoted 4 lines

btw. Current code does allow calling TUNSETQUEUE to a persistent tun/tap
device with no file attached. It should be a bug and need to be fixed.

Is this a problem? You can always
attach
set queue
detach

and it would be hard to prevent this ...

Currently, the following steps is allowed:

Quoted 8 lines
Quoted 6 lines

1. fd1 = open("/dev/net/tun");
2. tunsetiff(fd1, "tap0");
3. tunsetpersistent("tap0");
4. close(fd1);
5. fd2 = open("/dev/net/tun");
6. tunsetqueue(fd2, "tap0);

Allowed for libvirt, right? Not for qemu.

But what if step 1-4 is done by one process, step 5-6 is done by another? The issuse only existed for persistent device with no fd attached.

I agree we can add checks and let TUNSETQUEUE and let it to differ those conditions. But for simplicity, I suggest is to let userspace call TUNSETIFF first just like what we did when only single queue is supported in tun, and relabel the security of tun_struct there. And we can use different check (e.g. selinux for TUNSETQUEUE and TUNSETIFF) to enable the qemu to does TUNSETQUEUE itself.

Quoted 9 lines
Quoted 8 lines

Looks like step 6 should be forbidden since:

- no fd/sockets were attached to the device, we need use TUNSETIFF instead to keep the API as we used do in single queue tun
- we need update the security information in tun_struct just like what we discussed in this mail
- it may also miss checks in TUNSETIFF

Quoted 6 lines

We need to fix the checks anyway. Basically if you allow a queue to be the only fd that is attached
(and I don't see how you can prevent this since userspace does not expect close to fail),
I don't see why one way to get to this state should be legal and another illegal.

Well, we can return -EPERM when userspace call TUNSETQUEUE to a tun_struct with zero fd/queues.

Quoted 21 lines

The rest is implementation detail.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

One thing that I think we probably should change is the relabelto/from permissions in the function above (selinux_tun_dev_attach()); in the case
where the socket does not yet have a label, e.g. 'sksec->sid == 0', we should probably skip the relabel permissions since we want to assign the
TUN device label regardless in this case.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

I'm not familiar with the selinux, have a quick glance of the code, looks like the label has been initialized to SECINITSID_KERNEL in selinux_socket_post_create().

Thanks

--
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

#17 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

In reply to Michael S. Tsirkin
Quoted 59 lines · Michael S. Tsirkin
Quoted 58 lines · Jason Wang
Quoted 56 lines · Paul Moore
Quoted 47 lines · Michael S. Tsirkin
Quoted 37 lines · Paul Moore

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I
believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct
LSM blob and SELinux sets some internal blob state to record the label of
the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket
to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process,
use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently stored value, set during A2, to the label of the current calling process.
*** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket
to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

C. TUNSETQUEUE

Quoted 8 lines

[Use the existing tun_struct LSM state to label the new TUN socket.]

1. Call security_tun_dev_attach() which sets the label of the TUN socket
to match the label stored in the tun_struct LSM blob set during either A2
or B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 3 lines

Here's what bothers me. libvirt currently opens tun and passes fd to qemu. What would prevent qemu from attaching fd using TUNSETQUEUE to another device it does not own?

True, assuming all the above is correct and that I'm understanding it correctly (Jason?), we should probably add a new SELinux access control for
TUNSETQUEUE.

Quoted 4 lines
Quoted 2 lines

Yes, we need make sure qemu can call TUNSETQUEUE for the device it does not own.

Meaning can *not* call?

Sorry for not being clear, I mean qemu can call TUNSETQUEUE for the device it owns and for the device it does not own, it can't call.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The current DAC code exists in tun_not_capable().

author
#18 lore raw

Re: [RFC PATCH 2/2] tun: fix LSM/SELinux labeling of tun/tap devices

Quoted 68 lines · Jason Wang
Quoted 67 lines · Michael S. Tsirkin
Quoted 66 lines · Jason Wang
Quoted 64 lines · Paul Moore
Quoted 53 lines · Michael S. Tsirkin
Quoted 44 lines · Paul Moore

Okay, based on your explanation of TUNSETQUEUE, the steps below are what I
believe we need to do ... if you disagree speak up quickly please.

A. TUNSETIFF (new, non-persistent device)

[Allocate and initialize the tun_struct LSM state based on the calling
process, use this state to label the TUN socket.]

1. Call security_tun_dev_create() which authorizes the action.
2. Call security_tun_dev_alloc_security() which allocates the tun_struct
LSM blob and SELinux sets some internal blob state to record the label
of
the calling process.
3. Call security_tun_dev_attach() which sets the label of the TUN socket
to match the label stored in the tun_struct LSM blob during A2. No authorization is done at this point since the socket is new/unlabeled.

B. TUNSETIFF (existing, persistent device)

[Relabel the existing tun_struct LSM state based on the calling process,
use this state to label the TUN socket.]

1. Attempt to relabel/reset the tun_struct LSM blob from the currently
stored value, set during A2, to the label of the current calling process.
*** THIS IS NOT CURRENTLY DONE IN THE RFC PATCH ***
2. Call security_tun_dev_attach() which sets the label of the TUN socket
to match the label stored in the tun_struct LSM blob during B1. No authorization is done at this point since the socket is new/unlabeled.

C. TUNSETQUEUE

[Use the existing tun_struct LSM state to label the new TUN socket.]

Quoted 7 lines

1. Call security_tun_dev_attach() which sets the label of the TUN socket
to match the label stored in the tun_struct LSM blob set during either
A2
or B1. No authorization is done at this point since the socket is new/unlabeled.

Quoted 4 lines

Here's what bothers me. libvirt currently opens tun and passes fd to qemu. What would prevent qemu from attaching fd using TUNSETQUEUE
to another device it does not own?

True, assuming all the above is correct and that I'm understanding it correctly (Jason?), we should probably add a new SELinux access control
for
TUNSETQUEUE.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Yes, we need make sure qemu can call TUNSETQUEUE for the device it does not own.

Meaning can *not* call?

Sorry for not being clear, I mean qemu can call TUNSETQUEUE for the device it owns and for the device it does not own, it can't call.

Okay, let me add a access control for TUNSETQUEUE and I'll post an updated patchset later today.

Signature · 2 lines
paul moore
security and virtualization @ redhat
author
#2 lore raw

Re: [RFC PATCH 1/2] tun: correctly report an error in tun_flow_init()

In reply to Paul Moore
Quoted 6 lines · Paul Moore

On error, the error code from tun_flow_init() is lost inside tun_set_iff(), this patch fixes this by assigning the tun_flow_init() error code to the "err" variable which is returned by
the tun_flow_init() function on error.

Jason, we've had some good discussion around patch 2/2 but nothing on this fix; can I assume you are okay with this patch? If so I think we should go ahead and apply this ...

Quoted 19 lines
---
 drivers/net/tun.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +2 -1
index 607a3a5..877ffe2 100644
@@ -1605,7 +1605,8 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 
 		tun_net_init(dev);
 
-		if (tun_flow_init(tun))
+		err = tun_flow_init(tun);
+		if (err < 0)
 			goto err_free_dev;
 

dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |

Signature · 2 lines
paul moore
security and virtualization @ redhat
#3 lore raw

Re: [RFC PATCH 1/2] tun: correctly report an error in tun_flow_init()

Quoted 11 lines · Paul Moore
Quoted 6 lines · Paul Moore

On error, the error code from tun_flow_init() is lost inside tun_set_iff(), this patch fixes this by assigning the tun_flow_init() error code to the "err" variable which is returned by
the tun_flow_init() function on error.

Jason, we've had some good discussion around patch 2/2 but nothing on this fix; can I assume you are okay with this patch? If so I think we should go ahead and apply this ...

Yes, it looks good. Maybe we can separate this patch from this RFC series and tag it as "net-next" to let David apply it soon.

Thanks

Quoted 22 lines
Quoted 22 lines

---

 drivers/net/tun.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/net/tun.c +2 -1
index 607a3a5..877ffe2 100644
@@ -1605,7 +1605,8 @@ static int tun_set_iff(struct net *net, struct file
 *file, struct ifreq *ifr)
 
 		tun_net_init(dev);
 
-		if (tun_flow_init(tun))
+		err = tun_flow_init(tun);
+		if (err < 0)
 
 			goto err_free_dev;

dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST |