6f694de
From 4a49d45dd58994f4fc9b40c502252403caadee88 Mon Sep 17 00:00:00 2001
6f694de
From: Stephen Smalley <sds@tycho.nsa.gov>
6f694de
Date: Thu, 8 Dec 2016 09:14:47 -0500
6f694de
Subject: [PATCH] selinux: allow context mounts on tmpfs, ramfs, devpts within
6f694de
 user namespaces
6f694de
6f694de
commit aad82892af261b9903cc11c55be3ecf5f0b0b4f8 ("selinux: Add support for
6f694de
unprivileged mounts from user namespaces") prohibited any use of context
6f694de
mount options within non-init user namespaces.  However, this breaks
6f694de
use of context mount options for tmpfs mounts within user namespaces,
6f694de
which are being used by Docker/runc.  There is no reason to block such
6f694de
usage for tmpfs, ramfs or devpts.  Exempt these filesystem types
6f694de
from this restriction.
6f694de
6f694de
Before:
6f694de
sh$ userns_child_exec  -p -m -U -M '0 1000 1' -G '0 1000 1' bash
6f694de
sh# mount -t tmpfs -o context=system_u:object_r:user_tmp_t:s0:c13 none /tmp
6f694de
mount: tmpfs is write-protected, mounting read-only
6f694de
mount: cannot mount tmpfs read-only
6f694de
6f694de
After:
6f694de
sh$ userns_child_exec  -p -m -U -M '0 1000 1' -G '0 1000 1' bash
6f694de
sh# mount -t tmpfs -o context=system_u:object_r:user_tmp_t:s0:c13 none /tmp
6f694de
sh# ls -Zd /tmp
6f694de
unconfined_u:object_r:user_tmp_t:s0:c13 /tmp
6f694de
6f694de
Signed-off-by: Stephen Smalley <sds@tycho.nsa.gov>
6f694de
Signed-off-by: Paul Moore <paul@paul-moore.com>
6f694de
---
6f694de
 security/selinux/hooks.c | 10 +++++++---
6f694de
 1 file changed, 7 insertions(+), 3 deletions(-)
6f694de
6f694de
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
6f694de
index b508a5a..e7c5481 100644
6f694de
--- a/security/selinux/hooks.c
6f694de
+++ b/security/selinux/hooks.c
6f694de
@@ -834,10 +834,14 @@ static int selinux_set_mnt_opts(struct super_block *sb,
6f694de
 	}
6f694de
 
6f694de
 	/*
6f694de
-	 * If this is a user namespace mount, no contexts are allowed
6f694de
-	 * on the command line and security labels must be ignored.
6f694de
+	 * If this is a user namespace mount and the filesystem type is not
6f694de
+	 * explicitly whitelisted, then no contexts are allowed on the command
6f694de
+	 * line and security labels must be ignored.
6f694de
 	 */
6f694de
-	if (sb->s_user_ns != &init_user_ns) {
6f694de
+	if (sb->s_user_ns != &init_user_ns &&
6f694de
+	    strcmp(sb->s_type->name, "tmpfs") &&
6f694de
+	    strcmp(sb->s_type->name, "ramfs") &&
6f694de
+	    strcmp(sb->s_type->name, "devpts")) {
6f694de
 		if (context_sid || fscontext_sid || rootcontext_sid ||
6f694de
 		    defcontext_sid) {
6f694de
 			rc = -EACCES;
6f694de
-- 
6f694de
2.4.11
6f694de