Skip to contentSkip to conversation

Rebase device_cgroup v2 patchset

PATCH In review Aristeu Rozanski 7 participants 20 messages Oct 22, 2012 – May 16, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Rebase device_cgroup v2 patchset

This patchset rebases the v2 of the patchset since the v1 was pushed into -rc1 instead. The last patch, not present on previous patchset, fixes the permission check when allowing everything in a cgroup.

 device_cgroup.c |   87 +++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 61 insertions(+), 26 deletions(-)
Cc Dave Jones <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Cc Jiri Slaby <[email protected]>
Signature · 1 line
Aristeu
#1 lore raw

PATCH 1/4 cgroup: fix invalid rcu dereference

From: Jiri Slaby <[email protected]>

Commit "device_cgroup: convert device_cgroup internally to policy + exceptions" removed rcu locks which are needed in task_devcgroup called in this chain: devcgroup_inode_mknod OR __devcgroup_inode_permission -> __devcgroup_inode_permission -> task_devcgroup -> task_subsys_state -> task_subsys_state_check.

Change the code so that task_devcgroup is safely called with rcu read lock held.

===============================
[ INFO: suspicious RCU usage. ]
3.6.0-rc5-next-20120913+ #42 Not tainted
-------------------------------
include/linux/cgroup.h:553 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 0
2 locks held by kdevtmpfs/23:
#0: (sb_writers){.+.+.+}, at: [<ffffffff8116873f>]

mnt_want_write+0x1f/0x50
 #1:  (&sb->s_type->i_mutex_key#3/1){+.+.+.}, at: [<ffffffff811558af>]
kern_path_create+0x7f/0x170

stack backtrace:
Pid: 23, comm: kdevtmpfs Not tainted 3.6.0-rc5-next-20120913+ #42
Call Trace:
 [<ffffffff810c638d>] lockdep_rcu_suspicious+0xfd/0x130
 [<ffffffff8121541d>] devcgroup_inode_mknod+0x19d/0x240
 [<ffffffff8107bf54>] ? ns_capable+0x44/0x80
 [<ffffffff81156b21>] vfs_mknod+0x71/0xf0
 [<ffffffff813a8332>] handle_create.isra.2+0x72/0x200
 [<ffffffff813a85d4>] devtmpfsd+0x114/0x140
 [<ffffffff813a84c0>] ? handle_create.isra.2+0x200/0x200
 [<ffffffff81093ad6>] kthread+0xd6/0xe0
 [<ffffffff81654f24>] kernel_thread_helper+0x4/0x10
 [<ffffffff8165369d>] ? retint_restore_args+0xe/0xe
 [<ffffffff81093a00>] ? kthread_create_on_node+0x140/0x140
 [<ffffffff81654f20>] ? gs_change+0xb/0xb
Cc Dave Jones <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>

---

And this should fix it.

 security/device_cgroup.c |   15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
security/device_cgroup.c +7 -8
@@ -533,10 +533,10 @@ struct cgroup_subsys devices_subsys = {
  *
  * returns 0 on success, -EPERM case the operation is not permitted
  */
-static int __devcgroup_check_permission(struct dev_cgroup *dev_cgroup,
-					short type, u32 major, u32 minor,
+static int __devcgroup_check_permission(short type, u32 major, u32 minor,
 				        short access)
 {
+	struct dev_cgroup *dev_cgroup;
 	struct dev_exception_item ex;
 	int rc;
 
@@ -547,6 +547,7 @@ 	memset(&ex, 0, sizeof(ex));
 	ex.access = access;
 
 	rcu_read_lock();
+	dev_cgroup = task_devcgroup(current);
 	rc = may_access(dev_cgroup, &ex);
 	rcu_read_unlock();
 
@@ -558,7 +559,6 @@ 	return 0;
 
 int __devcgroup_inode_permission(struct inode *inode, int mask)
 {
-	struct dev_cgroup *dev_cgroup = task_devcgroup(current);
 	short type, access = 0;
 
 	if (S_ISBLK(inode->i_mode))
@@ -570,13 +570,12 @@ 	short type, access = 0;
 	if (mask & MAY_READ)
 		access |= ACC_READ;
 
-	return __devcgroup_check_permission(dev_cgroup, type, imajor(inode),
-					    iminor(inode), access);
+	return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
+			access);
 }
 
 int devcgroup_inode_mknod(int mode, dev_t dev)
 {
-	struct dev_cgroup *dev_cgroup = task_devcgroup(current);
 	short type;
 
 	if (!S_ISBLK(mode) && !S_ISCHR(mode))
@@ -587,7 +586,7 @@ 		return 0;
 	else
 		type = DEV_CHAR;
 
-	return __devcgroup_check_permission(dev_cgroup, type, MAJOR(dev),
-					    MINOR(dev), ACC_MKNOD);
+	return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
+			ACC_MKNOD);
 
 }
#6 lore raw

PATCH 2/4 device_cgroup: rename deny_all to behavior

In reply to Aristeu Rozanski

This was done in a v2 patch but v1 ended up being committed. The variable name is less confusing and stores the default behavior when no matching exception exists.

Cc Dave Jones <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Cc Jiri Slaby <[email protected]>
---
 security/device_cgroup.c |   25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
security/device_cgroup.c +14 -11
@@ -42,7 +42,10 @@ struct dev_exception_item {
 struct dev_cgroup {
 	struct cgroup_subsys_state css;
 	struct list_head exceptions;
-	bool deny_all;
+	enum {
+		DEVCG_DEFAULT_ALLOW,
+		DEVCG_DEFAULT_DENY,
+	} behavior;
 };
 
 static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
@@ -182,13 +185,13 @@ static struct cgroup_subsys_state *devcg
 	parent_cgroup = cgroup->parent;
 
 	if (parent_cgroup == NULL)
-		dev_cgroup->deny_all = false;
+		dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
 	else {
 		parent_dev_cgroup = cgroup_to_devcgroup(parent_cgroup);
 		mutex_lock(&devcgroup_mutex);
 		ret = dev_exceptions_copy(&dev_cgroup->exceptions,
 					  &parent_dev_cgroup->exceptions);
-		dev_cgroup->deny_all = parent_dev_cgroup->deny_all;
+		dev_cgroup->behavior = parent_dev_cgroup->behavior;
 		mutex_unlock(&devcgroup_mutex);
 		if (ret) {
 			kfree(dev_cgroup);
@@ -260,7 +263,7 @@ static int devcgroup_seq_read(struct cgr
 	 * - List the exceptions in case the default policy is to deny
 	 * This way, the file remains as a "whitelist of devices"
 	 */
-	if (devcgroup->deny_all == false) {
+	if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
 		set_access(acc, ACC_MASK);
 		set_majmin(maj, ~0);
 		set_majmin(min, ~0);
@@ -314,12 +317,12 @@ 		if (ex->minor != ~0 && ex->minor != re
 	 * In two cases we'll consider this new exception valid:
 	 * - the dev cgroup has its default policy to allow + exception list:
 	 *   the new exception should *not* match any of the exceptions
-	 *   (!deny_all, !match)
+	 *   (behavior == DEVCG_DEFAULT_ALLOW, !match)
 	 * - the dev cgroup has its default policy to deny + exception list:
 	 *   the new exception *should* match the exceptions
-	 *   (deny_all, match)
+	 *   (behavior == DEVCG_DEFAULT_DENY, match)
 	 */
-	if (dev_cgroup->deny_all == match)
+	if ((dev_cgroup->behavior == DEVCG_DEFAULT_DENY) == match)
 		return 1;
 	return 0;
 }
@@ -375,11 +378,11 @@ 	memset(&ex, 0, sizeof(ex));
 			if (!parent_has_perm(devcgroup, &ex))
 				return -EPERM;
 			dev_exception_clean(devcgroup);
-			devcgroup->deny_all = false;
+			devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
 			break;
 		case DEVCG_DENY:
 			dev_exception_clean(devcgroup);
-			devcgroup->deny_all = true;
+			devcgroup->behavior = DEVCG_DEFAULT_DENY;
 			break;
 		default:
 			return -EINVAL;
@@ -452,7 +455,7 @@ 		case '\0':
 		 * an matching exception instead. And be silent about it: we
 		 * don't want to break compatibility
 		 */
-		if (devcgroup->deny_all == false) {
+		if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
 			dev_exception_rm(devcgroup, &ex);
 			return 0;
 		}
@@ -463,7 +466,7 @@ 			return 0;
 		 * an matching exception instead. And be silent about it: we
 		 * don't want to break compatibility
 		 */
-		if (devcgroup->deny_all == true) {
+		if (devcgroup->behavior == DEVCG_DEFAULT_DENY) {
 			dev_exception_rm(devcgroup, &ex);
 			return 0;
 		}
#8 lore raw

PATCH 3/4 device_cgroup: stop using simple_strtoul()

In reply to Aristeu Rozanski

This patch converts the code to use kstrtou32() instead of simple_strtoul() which is deprecated. The real size of the variables are u32, so use kstrtou32 instead of kstrtoul

Cc Dave Jones <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Cc Jiri Slaby <[email protected]>
---
 security/device_cgroup.c |   28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
security/device_cgroup.c +22 -6
@@ -361,8 +361,8 @@ static int devcgroup_update_access(struc
 				   int filetype, const char *buffer)
 {
 	const char *b;
-	char *endp;
-	int count;
+	char temp[12];		/* 11 + 1 characters needed for a u32 */
+	int count, rc;
 	struct dev_exception_item ex;
 
 	if (!capable(CAP_SYS_ADMIN))
@@ -405,8 +405,16 @@ 		return 0;
 		ex.major = ~0;
 		b++;
 	} else if (isdigit(*b)) {
-		ex.major = simple_strtoul(b, &endp, 10);
-		b = endp;
+		memset(temp, 0, sizeof(temp));
+		for (count = 0; count < sizeof(temp) - 1; count++) {
+			temp[count] = *b;
+			b++;
+			if (!isdigit(*b))
+				break;
+		}
+		rc = kstrtou32(temp, 10, &ex.major);
+		if (rc)
+			return -EINVAL;
 	} else {
 		return -EINVAL;
 	}
@@ -419,8 +427,16 @@ 		ex.major = simple_strtoul(b, &endp, 10
 		ex.minor = ~0;
 		b++;
 	} else if (isdigit(*b)) {
-		ex.minor = simple_strtoul(b, &endp, 10);
-		b = endp;
+		memset(temp, 0, sizeof(temp));
+		for (count = 0; count < sizeof(temp) - 1; count++) {
+			temp[count] = *b;
+			b++;
+			if (!isdigit(*b))
+				break;
+		}
+		rc = kstrtou32(temp, 10, &ex.minor);
+		if (rc)
+			return -EINVAL;
 	} else {
 		return -EINVAL;
 	}
#10 lore raw

PATCH 4/4 device_cgroup: add proper checking when changing default behavior

In reply to Aristeu Rozanski

Before changing a group's default behavior to ALLOW, we must check if its parent's behavior is also ALLOW.

Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Cc Jiri Slaby <[email protected]>
---
 security/device_cgroup.c |   19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
security/device_cgroup.c +18 -1
@@ -344,6 +344,17 @@ static int parent_has_perm(struct dev_cg
 	return may_access(parent, ex);
 }
 
+/**
+ * may_allow_all - checks if it's possible to change the behavior to
+ *		   allow based on parent's rules.
+ * @parent: device cgroup's parent
+ * returns: != 0 in case it's allowed, 0 otherwise
+ */
+static inline int may_allow_all(struct dev_cgroup *parent)
+{
+	return parent->behavior == DEVCG_DEFAULT_ALLOW;
+}
+
 /*
  * Modify the exception list using allow/deny rules.
  * CAP_SYS_ADMIN is needed for this.  It's at least separate from CAP_MKNOD
@@ -364,6 +375,8 @@ static int devcgroup_update_access(struc
 	char temp[12];		/* 11 + 1 characters needed for a u32 */
 	int count, rc;
 	struct dev_exception_item ex;
+	struct cgroup *p = devcgroup->css.cgroup;
+	struct dev_cgroup *parent = cgroup_to_devcgroup(p->parent);
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -375,9 +388,13 @@ 	memset(&ex, 0, sizeof(ex));
 	case 'a':
 		switch (filetype) {
 		case DEVCG_ALLOW:
-			if (!parent_has_perm(devcgroup, &ex))
+			if (!may_allow_all(parent))
 				return -EPERM;
 			dev_exception_clean(devcgroup);
+			rc = dev_exceptions_copy(&devcgroup->exceptions,
+						 &parent->exceptions);
+			if (rc)
+				return rc;
 			devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
 			break;
 		case DEVCG_DENY:
Acked-by Serge E. Hallyn on v1 1/4
#2 lore raw

Re: [PATCH 1/4] cgroup: fix invalid rcu dereference

In reply to Aristeu Rozanski
Quoted 42 lines · Aristeu Rozanski

From: Jiri Slaby <[email protected]>

Commit "device_cgroup: convert device_cgroup internally to policy + exceptions" removed rcu locks which are needed in task_devcgroup called in this chain: devcgroup_inode_mknod OR __devcgroup_inode_permission -> __devcgroup_inode_permission -> task_devcgroup -> task_subsys_state -> task_subsys_state_check.

Change the code so that task_devcgroup is safely called with rcu read lock held.

===============================
[ INFO: suspicious RCU usage. ]
3.6.0-rc5-next-20120913+ #42 Not tainted
-------------------------------
include/linux/cgroup.h:553 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 0
2 locks held by kdevtmpfs/23:
#0: (sb_writers){.+.+.+}, at: [<ffffffff8116873f>]

mnt_want_write+0x1f/0x50
 #1:  (&sb->s_type->i_mutex_key#3/1){+.+.+.}, at: [<ffffffff811558af>]
kern_path_create+0x7f/0x170

stack backtrace:
Pid: 23, comm: kdevtmpfs Not tainted 3.6.0-rc5-next-20120913+ #42
Call Trace:
 [<ffffffff810c638d>] lockdep_rcu_suspicious+0xfd/0x130
 [<ffffffff8121541d>] devcgroup_inode_mknod+0x19d/0x240
 [<ffffffff8107bf54>] ? ns_capable+0x44/0x80
 [<ffffffff81156b21>] vfs_mknod+0x71/0xf0
 [<ffffffff813a8332>] handle_create.isra.2+0x72/0x200
 [<ffffffff813a85d4>] devtmpfsd+0x114/0x140
 [<ffffffff813a84c0>] ? handle_create.isra.2+0x200/0x200
 [<ffffffff81093ad6>] kthread+0xd6/0xe0
 [<ffffffff81654f24>] kernel_thread_helper+0x4/0x10
 [<ffffffff8165369d>] ? retint_restore_args+0xe/0xe
 [<ffffffff81093a00>] ? kthread_create_on_node+0x140/0x140
 [<ffffffff81654f20>] ? gs_change+0xb/0xb
Quoted 7 lines
Cc Dave Jones <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <jmorris-gx6/[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Quoted 67 lines

---

And this should fix it.

 security/device_cgroup.c |   15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
security/device_cgroup.c +7 -8
@@ -533,10 +533,10 @@ struct cgroup_subsys devices_subsys = {
  *
  * returns 0 on success, -EPERM case the operation is not permitted
  */
-static int __devcgroup_check_permission(struct dev_cgroup *dev_cgroup,
-					short type, u32 major, u32 minor,
+static int __devcgroup_check_permission(short type, u32 major, u32 minor,
 				        short access)
 {
+	struct dev_cgroup *dev_cgroup;
 	struct dev_exception_item ex;
 	int rc;
 
@@ -547,6 +547,7 @@ 	memset(&ex, 0, sizeof(ex));
 	ex.access = access;
 
 	rcu_read_lock();
+	dev_cgroup = task_devcgroup(current);
 	rc = may_access(dev_cgroup, &ex);
 	rcu_read_unlock();
 
@@ -558,7 +559,6 @@ 	return 0;
 
 int __devcgroup_inode_permission(struct inode *inode, int mask)
 {
-	struct dev_cgroup *dev_cgroup = task_devcgroup(current);
 	short type, access = 0;
 
 	if (S_ISBLK(inode->i_mode))
@@ -570,13 +570,12 @@ 	short type, access = 0;
 	if (mask & MAY_READ)
 		access |= ACC_READ;
 
-	return __devcgroup_check_permission(dev_cgroup, type, imajor(inode),
-					    iminor(inode), access);
+	return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
+			access);
 }
 
 int devcgroup_inode_mknod(int mode, dev_t dev)
 {
-	struct dev_cgroup *dev_cgroup = task_devcgroup(current);
 	short type;
 
 	if (!S_ISBLK(mode) && !S_ISCHR(mode))
@@ -587,7 +586,7 @@ 		return 0;
 	else
 		type = DEV_CHAR;
 
-	return __devcgroup_check_permission(dev_cgroup, type, MAJOR(dev),
-					    MINOR(dev), ACC_MKNOD);
+	return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
+			ACC_MKNOD);
 
 }
Acked-by Serge E. Hallyn on v1 2/4
#7 lore raw

Re: [PATCH 2/4] device_cgroup: rename deny_all to behavior

In reply to Aristeu Rozanski
Quoted 5 lines · Aristeu Rozanski

This was done in a v2 patch but v1 ended up being committed. The variable name is less confusing and stores the default behavior when no matching exception exists.

Quoted 7 lines
Cc Dave Jones <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Quoted 95 lines
---
 security/device_cgroup.c |   25 ++++++++++++++-----------
 1 file changed, 14 insertions(+), 11 deletions(-)
security/device_cgroup.c +14 -11
@@ -42,7 +42,10 @@ struct dev_exception_item {
 struct dev_cgroup {
 	struct cgroup_subsys_state css;
 	struct list_head exceptions;
-	bool deny_all;
+	enum {
+		DEVCG_DEFAULT_ALLOW,
+		DEVCG_DEFAULT_DENY,
+	} behavior;
 };
 
 static inline struct dev_cgroup *css_to_devcgroup(struct cgroup_subsys_state *s)
@@ -182,13 +185,13 @@ static struct cgroup_subsys_state *devcg
 	parent_cgroup = cgroup->parent;
 
 	if (parent_cgroup == NULL)
-		dev_cgroup->deny_all = false;
+		dev_cgroup->behavior = DEVCG_DEFAULT_ALLOW;
 	else {
 		parent_dev_cgroup = cgroup_to_devcgroup(parent_cgroup);
 		mutex_lock(&devcgroup_mutex);
 		ret = dev_exceptions_copy(&dev_cgroup->exceptions,
 					  &parent_dev_cgroup->exceptions);
-		dev_cgroup->deny_all = parent_dev_cgroup->deny_all;
+		dev_cgroup->behavior = parent_dev_cgroup->behavior;
 		mutex_unlock(&devcgroup_mutex);
 		if (ret) {
 			kfree(dev_cgroup);
@@ -260,7 +263,7 @@ static int devcgroup_seq_read(struct cgr
 	 * - List the exceptions in case the default policy is to deny
 	 * This way, the file remains as a "whitelist of devices"
 	 */
-	if (devcgroup->deny_all == false) {
+	if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
 		set_access(acc, ACC_MASK);
 		set_majmin(maj, ~0);
 		set_majmin(min, ~0);
@@ -314,12 +317,12 @@ 		if (ex->minor != ~0 && ex->minor != re
 	 * In two cases we'll consider this new exception valid:
 	 * - the dev cgroup has its default policy to allow + exception list:
 	 *   the new exception should *not* match any of the exceptions
-	 *   (!deny_all, !match)
+	 *   (behavior == DEVCG_DEFAULT_ALLOW, !match)
 	 * - the dev cgroup has its default policy to deny + exception list:
 	 *   the new exception *should* match the exceptions
-	 *   (deny_all, match)
+	 *   (behavior == DEVCG_DEFAULT_DENY, match)
 	 */
-	if (dev_cgroup->deny_all == match)
+	if ((dev_cgroup->behavior == DEVCG_DEFAULT_DENY) == match)
 		return 1;
 	return 0;
 }
@@ -375,11 +378,11 @@ 	memset(&ex, 0, sizeof(ex));
 			if (!parent_has_perm(devcgroup, &ex))
 				return -EPERM;
 			dev_exception_clean(devcgroup);
-			devcgroup->deny_all = false;
+			devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
 			break;
 		case DEVCG_DENY:
 			dev_exception_clean(devcgroup);
-			devcgroup->deny_all = true;
+			devcgroup->behavior = DEVCG_DEFAULT_DENY;
 			break;
 		default:
 			return -EINVAL;
@@ -452,7 +455,7 @@ 		case '\0':
 		 * an matching exception instead. And be silent about it: we
 		 * don't want to break compatibility
 		 */
-		if (devcgroup->deny_all == false) {
+		if (devcgroup->behavior == DEVCG_DEFAULT_ALLOW) {
 			dev_exception_rm(devcgroup, &ex);
 			return 0;
 		}
@@ -463,7 +466,7 @@ 			return 0;
 		 * an matching exception instead. And be silent about it: we
 		 * don't want to break compatibility
 		 */
-		if (devcgroup->deny_all == true) {
+		if (devcgroup->behavior == DEVCG_DEFAULT_DENY) {
 			dev_exception_rm(devcgroup, &ex);
 			return 0;
 		}
Acked-by Serge E. Hallyn on v1 3/4
#9 lore raw

Re: [PATCH 3/4] device_cgroup: stop using simple_strtoul()

In reply to Aristeu Rozanski
Quoted 5 lines · Aristeu Rozanski

This patch converts the code to use kstrtou32() instead of simple_strtoul() which is deprecated. The real size of the variables are u32, so use kstrtou32 instead of kstrtoul

Quoted 7 lines
Cc Dave Jones <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <jmorris-gx6/[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>
Quoted 59 lines
---
 security/device_cgroup.c |   28 ++++++++++++++++++++++------
 1 file changed, 22 insertions(+), 6 deletions(-)
security/device_cgroup.c +22 -6
@@ -361,8 +361,8 @@ static int devcgroup_update_access(struc
 				   int filetype, const char *buffer)
 {
 	const char *b;
-	char *endp;
-	int count;
+	char temp[12];		/* 11 + 1 characters needed for a u32 */
+	int count, rc;
 	struct dev_exception_item ex;
 
 	if (!capable(CAP_SYS_ADMIN))
@@ -405,8 +405,16 @@ 		return 0;
 		ex.major = ~0;
 		b++;
 	} else if (isdigit(*b)) {
-		ex.major = simple_strtoul(b, &endp, 10);
-		b = endp;
+		memset(temp, 0, sizeof(temp));
+		for (count = 0; count < sizeof(temp) - 1; count++) {
+			temp[count] = *b;
+			b++;
+			if (!isdigit(*b))
+				break;
+		}
+		rc = kstrtou32(temp, 10, &ex.major);
+		if (rc)
+			return -EINVAL;
 	} else {
 		return -EINVAL;
 	}
@@ -419,8 +427,16 @@ 		ex.major = simple_strtoul(b, &endp, 10
 		ex.minor = ~0;
 		b++;
 	} else if (isdigit(*b)) {
-		ex.minor = simple_strtoul(b, &endp, 10);
-		b = endp;
+		memset(temp, 0, sizeof(temp));
+		for (count = 0; count < sizeof(temp) - 1; count++) {
+			temp[count] = *b;
+			b++;
+			if (!isdigit(*b))
+				break;
+		}
+		rc = kstrtou32(temp, 10, &ex.minor);
+		if (rc)
+			return -EINVAL;
 	} else {
 		return -EINVAL;
 	}
Acked-by Serge E. Hallyn on v1 4/4
#11 lore raw

Re: [PATCH 4/4] device_cgroup: add proper checking when changing default behavior

In reply to Aristeu Rozanski
Quoted 8 lines · Aristeu Rozanski

Before changing a group's default behavior to ALLOW, we must check if its parent's behavior is also ALLOW.

Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <jmorris-gx6/[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>

Thanks, Aristeu.

Quoted 52 lines
---
 security/device_cgroup.c |   19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
security/device_cgroup.c +18 -1
@@ -344,6 +344,17 @@ static int parent_has_perm(struct dev_cg
 	return may_access(parent, ex);
 }
 
+/**
+ * may_allow_all - checks if it's possible to change the behavior to
+ *		   allow based on parent's rules.
+ * @parent: device cgroup's parent
+ * returns: != 0 in case it's allowed, 0 otherwise
+ */
+static inline int may_allow_all(struct dev_cgroup *parent)
+{
+	return parent->behavior == DEVCG_DEFAULT_ALLOW;
+}
+
 /*
  * Modify the exception list using allow/deny rules.
  * CAP_SYS_ADMIN is needed for this.  It's at least separate from CAP_MKNOD
@@ -364,6 +375,8 @@ static int devcgroup_update_access(struc
 	char temp[12];		/* 11 + 1 characters needed for a u32 */
 	int count, rc;
 	struct dev_exception_item ex;
+	struct cgroup *p = devcgroup->css.cgroup;
+	struct dev_cgroup *parent = cgroup_to_devcgroup(p->parent);
 
 	if (!capable(CAP_SYS_ADMIN))
 		return -EPERM;
@@ -375,9 +388,13 @@ 	memset(&ex, 0, sizeof(ex));
 	case 'a':
 		switch (filetype) {
 		case DEVCG_ALLOW:
-			if (!parent_has_perm(devcgroup, &ex))
+			if (!may_allow_all(parent))
 				return -EPERM;
 			dev_exception_clean(devcgroup);
+			rc = dev_exceptions_copy(&devcgroup->exceptions,
+						 &parent->exceptions);
+			if (rc)
+				return rc;
 			devcgroup->behavior = DEVCG_DEFAULT_ALLOW;
 			break;
 		case DEVCG_DENY:
#12 lore raw
In reply to Aristeu Rozanski
Quoted 4 lines · On Mon, 22 Oct 2012 09:45:36 -0400 Aristeu Rozanski…

This patchset rebases the v2 of the patchset since the v1 was pushed into -rc1 instead. The last patch, not present on previous patchset, fixes the permission check when allowing everything in a cgroup.

I grabbed all four, thanks. Shall send them on to Linus in a week or so, after a bit more review and some linux-next testing.

I've been trying to work out why I merged the v1 patchset rather than v2 and cannot find that v2 patchset anywhere. When was it sent out?

#13 lore raw

Hi Andrew,

Quoted 10 lines · Andrew Morton
Quoted 4 lines · On Mon, 22 Oct 2012 09:45:36 -0400 Aristeu Rozanski…

This patchset rebases the v2 of the patchset since the v1 was pushed into -rc1 instead. The last patch, not present on previous patchset, fixes the permission check when allowing everything in a cgroup.

I grabbed all four, thanks. Shall send them on to Linus in a week or so, after a bit more review and some linux-next testing.

thanks, much appreciated

Quoted 2 lines

I've been trying to work out why I merged the v1 patchset rather than v2 and cannot find that v2 patchset anywhere. When was it sent out?

It was sent to linux-kernel and cgroups@vger on Sep 04th, here's the msgid of patch 0 for reference:
<20120904143419.892872876-cd6kKtb6gxi3M6m420IelR/[email protected]>

Signature · 1 line
Aristeu
#3 lore raw

Re: [PATCH 1/4] cgroup: fix invalid rcu dereference

In reply to Aristeu Rozanski
Quoted 1 line · Aristeu Rozanski

From: Jiri Slaby <[email protected]>

No, I'm *not* the author of the patch... Or at least I don't remember :P. Where did you take it from?

Quoted 114 lines

Commit "device_cgroup: convert device_cgroup internally to policy + exceptions" removed rcu locks which are needed in task_devcgroup called in this chain: devcgroup_inode_mknod OR __devcgroup_inode_permission -> __devcgroup_inode_permission -> task_devcgroup -> task_subsys_state -> task_subsys_state_check.

Change the code so that task_devcgroup is safely called with rcu read lock held.

===============================
[ INFO: suspicious RCU usage. ]
3.6.0-rc5-next-20120913+ #42 Not tainted
-------------------------------
include/linux/cgroup.h:553 suspicious rcu_dereference_check() usage!

other info that might help us debug this:

rcu_scheduler_active = 1, debug_locks = 0
2 locks held by kdevtmpfs/23:
#0: (sb_writers){.+.+.+}, at: [<ffffffff8116873f>]

mnt_want_write+0x1f/0x50
 #1:  (&sb->s_type->i_mutex_key#3/1){+.+.+.}, at: [<ffffffff811558af>]
kern_path_create+0x7f/0x170

stack backtrace:
Pid: 23, comm: kdevtmpfs Not tainted 3.6.0-rc5-next-20120913+ #42
Call Trace:
 [<ffffffff810c638d>] lockdep_rcu_suspicious+0xfd/0x130
 [<ffffffff8121541d>] devcgroup_inode_mknod+0x19d/0x240
 [<ffffffff8107bf54>] ? ns_capable+0x44/0x80
 [<ffffffff81156b21>] vfs_mknod+0x71/0xf0
 [<ffffffff813a8332>] handle_create.isra.2+0x72/0x200
 [<ffffffff813a85d4>] devtmpfsd+0x114/0x140
 [<ffffffff813a84c0>] ? handle_create.isra.2+0x200/0x200
 [<ffffffff81093ad6>] kthread+0xd6/0xe0
 [<ffffffff81654f24>] kernel_thread_helper+0x4/0x10
 [<ffffffff8165369d>] ? retint_restore_args+0xe/0xe
 [<ffffffff81093a00>] ? kthread_create_on_node+0x140/0x140
 [<ffffffff81654f20>] ? gs_change+0xb/0xb
Cc Dave Jones <[email protected]>
Cc Andrew Morton <akpm-de/[email protected]>
Cc Tejun Heo <[email protected]>
Cc Li Zefan <[email protected]>
Cc James Morris <jmorris-gx6/[email protected]>
Cc Pavel Emelyanov <[email protected]>
Cc Serge Hallyn <[email protected]>

---

And this should fix it.

 security/device_cgroup.c |   15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
security/device_cgroup.c +7 -8
@@ -533,10 +533,10 @@ struct cgroup_subsys devices_subsys = {
  *
  * returns 0 on success, -EPERM case the operation is not permitted
  */
-static int __devcgroup_check_permission(struct dev_cgroup *dev_cgroup,
-					short type, u32 major, u32 minor,
+static int __devcgroup_check_permission(short type, u32 major, u32 minor,
 				        short access)
 {
+	struct dev_cgroup *dev_cgroup;
 	struct dev_exception_item ex;
 	int rc;
 
@@ -547,6 +547,7 @@ 	memset(&ex, 0, sizeof(ex));
 	ex.access = access;
 
 	rcu_read_lock();
+	dev_cgroup = task_devcgroup(current);
 	rc = may_access(dev_cgroup, &ex);
 	rcu_read_unlock();
 
@@ -558,7 +559,6 @@ 	return 0;
 
 int __devcgroup_inode_permission(struct inode *inode, int mask)
 {
-	struct dev_cgroup *dev_cgroup = task_devcgroup(current);
 	short type, access = 0;
 
 	if (S_ISBLK(inode->i_mode))
@@ -570,13 +570,12 @@ 	short type, access = 0;
 	if (mask & MAY_READ)
 		access |= ACC_READ;
 
-	return __devcgroup_check_permission(dev_cgroup, type, imajor(inode),
-					    iminor(inode), access);
+	return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
+			access);
 }
 
 int devcgroup_inode_mknod(int mode, dev_t dev)
 {
-	struct dev_cgroup *dev_cgroup = task_devcgroup(current);
 	short type;
 
 	if (!S_ISBLK(mode) && !S_ISCHR(mode))
@@ -587,7 +586,7 @@ 		return 0;
 	else
 		type = DEV_CHAR;
 
-	return __devcgroup_check_permission(dev_cgroup, type, MAJOR(dev),
-					    MINOR(dev), ACC_MKNOD);
+	return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
+			ACC_MKNOD);
 
 }
Signature · 2 lines
js
suse labs
#4 lore raw

Re: [PATCH 1/4] cgroup: fix invalid rcu dereference

Quoted 2 lines · Jiri Slaby

No, I'm *not* the author of the patch... Or at least I don't remember :P. Where did you take it from?

<1347615612-11450-1-git-send-email-jslaby-AlSwsSmVLrQ@public.gmane.org>
Date: Fri, 14 Sep 2012 11:40:12 +0200

you didn't include a "From:" on your email, so I assumed it was from you. it was sent to akpm with others and linux-kernel in cc

Signature · 1 line
Aristeu
#5 lore raw

Re: [PATCH 1/4] cgroup: fix invalid rcu dereference

Quoted 9 lines · Aristeu Rozanski
Quoted 2 lines · Jiri Slaby

No, I'm *not* the author of the patch... Or at least I don't remember :P. Where did you take it from?

<1347615612-11450-1-git-send-email-jslaby-AlSwsSmVLrQ@public.gmane.org>
Date: Fri, 14 Sep 2012 11:40:12 +0200

you didn't include a "From:" on your email, so I assumed it was from you. it was sent to akpm with others and linux-kernel in cc

Ah, ok, you convinced me that I'm the author of the patch :).

Signature · 2 lines
js
suse labs
#14 lore raw
In reply to Aristeu Rozanski

Hi,

so now that the device cgroup properly respects hierarchy, not allowing a cgroup to be given greater permission than its parent, should we consider relaxing the capability checks?

There are two capable(CAP_SYS_ADMIN) checks in deice_cgroup.c: one in devcgroup_can_attach() to protect changing another task's cgroup, and one in devcgroup_update_access() to protect writes to the devices.allow and devices.deny files.

I think the first should be changed to a check for ns_capable() to the victim's user_ns. Something like

security/device_cgroup.c +9 -3
@@ -70,10 +70,16 @@ static int devcgroup_can_attach(struct cgroup *new_cgrp,
                                struct cgroup_taskset *set)
 {
        struct task_struct *task = cgroup_taskset_first(set);
+       struct user_namespace *ns;
+       int ret = -EPERM;
 
-       if (current != task && !capable(CAP_SYS_ADMIN))
-               return -EPERM;
-       return 0;
+       if (current == task)
+               return 0;
+
+       ns = userns_get(task);;
+       ret = ns_capable(ns, CAP_SYS_ADMIN) ? 0 : -EPERM;
+       put_user_ns(ns);
+       return ret;
 }
 
 /*

For the second, the hierarchy support should let us ignore concerns about unprivileged users escalating privilege, but I'm trying to decide whether we need to worry about the sendmail capability class of bugs. My sense is actually the answer is no, and we can drop the capable() check altogether. The reason is that while userspace frequently doesn't properly handle a failing system call due to unexpected lack of partial privilege, I wouldn't expect any setuid root program to ignore failure to open or mknod a device file (and proceed into a bad failure mode). Does this sound rasonable, or a recipe for disaster?

-serge

#15 lore raw
Quoted 22 lines · Serge Hallyn

so now that the device cgroup properly respects hierarchy, not allowing a cgroup to be given greater permission than its parent, should we consider relaxing the capability checks?

There are two capable(CAP_SYS_ADMIN) checks in deice_cgroup.c: one in devcgroup_can_attach() to protect changing another task's cgroup, and one in devcgroup_update_access() to protect writes to the devices.allow and devices.deny files.

I think the first should be changed to a check for ns_capable() to the victim's user_ns. Something like

security/device_cgroup.c +2 -1
@@ -70,10 +70,16 @@ static int devcgroup_can_attach(struct cgroup *new_cgrp,
                                struct cgroup_taskset *set)
 {
        struct task_struct *task = cgroup_taskset_first(set);
+       struct user_namespace *ns;
+       int ret = -EPERM;
 
-       if (current != task && !capable(CAP_SYS_ADMIN))

security/device_cgroup.c static int devcgroup_can_attach(struct cgroup *new_cgrp, · line 83

Quoted 10 lines
security/device_cgroup.c +7 -2
-               return -EPERM;
-       return 0;
+       if (current == task)
+               return 0;
+
+       ns = userns_get(task);;
+       ret = ns_capable(ns, CAP_SYS_ADMIN) ? 0 : -EPERM;
+       put_user_ns(ns);
+       return ret;
 }

wouldn't this allow a userns root to move a task in the same userns into a parent cgroup? I believe than anything but moving down the hierarchy would be very complicated to verify (how far up can you go).

Quoted 3 lines

For the second, the hierarchy support should let us ignore concerns about unprivileged users escalating privilege, but I'm trying to decide whether we need to worry about the sendmail capability class of bugs.

You have a pointer for more information on those?

Quoted 6 lines

My sense is actually the answer is no, and we can drop the capable() check altogether. The reason is that while userspace frequently doesn't properly handle a failing system call due to unexpected lack of partial privilege, I wouldn't expect any setuid root program to ignore failure to open or mknod a device file (and proceed into a bad failure mode). Does this sound rasonable, or a recipe for disaster?

The second case sounds ok to me

Signature · 1 line
Aristeu
#16 lore raw
Quoted 22 lines · Aristeu Rozanski
Quoted 21 lines · Serge Hallyn

so now that the device cgroup properly respects hierarchy, not allowing a cgroup to be given greater permission than its parent, should we consider relaxing the capability checks?

There are two capable(CAP_SYS_ADMIN) checks in deice_cgroup.c: one in devcgroup_can_attach() to protect changing another task's cgroup, and one in devcgroup_update_access() to protect writes to the devices.allow and devices.deny files.

I think the first should be changed to a check for ns_capable() to the victim's user_ns. Something like

security/device_cgroup.c +2 -0
@@ -70,10 +70,16 @@ static int devcgroup_can_attach(struct cgroup *new_cgrp,
                                struct cgroup_taskset *set)
 {
        struct task_struct *task = cgroup_taskset_first(set);
+       struct user_namespace *ns;
+       int ret = -EPERM;
 
Quoted 15 lines

security/device_cgroup.c static int devcgroup_can_attach(struct cgroup *new_cgrp, · line 83

Quoted 11 lines
security/device_cgroup.c +7 -3
-       if (current != task && !capable(CAP_SYS_ADMIN))
-               return -EPERM;
-       return 0;
+       if (current == task)
+               return 0;
+
+       ns = userns_get(task);;
+       ret = ns_capable(ns, CAP_SYS_ADMIN) ? 0 : -EPERM;
+       put_user_ns(ns);
+       return ret;
 }

wouldn't this allow a userns root to move a task in the same userns into a parent cgroup? I believe than anything but moving down the hierarchy would be very complicated to verify (how far up can you go).

But only if they are able to open the tasks file for writing, which they shouldn't be able to do, right?

Quoted 5 lines
Quoted 3 lines

For the second, the hierarchy support should let us ignore concerns about unprivileged users escalating privilege, but I'm trying to decide whether we need to worry about the sendmail capability class of bugs.

You have a pointer for more information on those?

Darn - unfortunately the best description of it, which was at
http://userweb.kernel.org/~morgan/sendmail-capabilities-war-story.html
is no longer there since userweb was taken down, and it was never captured by archive.org. There's a brief description in
http://lwn.net/Articles/280279/ at the paragraph starting with "The memory of the sendmail-capabilities bug from 2000..."

Quoted 8 lines
Quoted 6 lines

My sense is actually the answer is no, and we can drop the capable() check altogether. The reason is that while userspace frequently doesn't properly handle a failing system call due to unexpected lack of partial privilege, I wouldn't expect any setuid root program to ignore failure to open or mknod a device file (and proceed into a bad failure mode). Does this sound rasonable, or a recipe for disaster?

The second case sounds ok to me

-serge

#17 lore raw
Quoted 39 lines · Serge Hallyn
Quoted 22 lines · Aristeu Rozanski
Quoted 21 lines · Serge Hallyn

so now that the device cgroup properly respects hierarchy, not allowing a cgroup to be given greater permission than its parent, should we consider relaxing the capability checks?

There are two capable(CAP_SYS_ADMIN) checks in deice_cgroup.c: one in devcgroup_can_attach() to protect changing another task's cgroup, and one in devcgroup_update_access() to protect writes to the devices.allow and devices.deny files.

I think the first should be changed to a check for ns_capable() to the victim's user_ns. Something like

security/device_cgroup.c +2 -0
@@ -70,10 +70,16 @@ static int devcgroup_can_attach(struct cgroup *new_cgrp,
                                struct cgroup_taskset *set)
 {
        struct task_struct *task = cgroup_taskset_first(set);
+       struct user_namespace *ns;
+       int ret = -EPERM;
 
Quoted 15 lines

security/device_cgroup.c static int devcgroup_can_attach(struct cgroup *new_cgrp, · line 83

Quoted 11 lines
security/device_cgroup.c +7 -3
-       if (current != task && !capable(CAP_SYS_ADMIN))
-               return -EPERM;
-       return 0;
+       if (current == task)
+               return 0;
+
+       ns = userns_get(task);;
+       ret = ns_capable(ns, CAP_SYS_ADMIN) ? 0 : -EPERM;
+       put_user_ns(ns);
+       return ret;
 }

wouldn't this allow a userns root to move a task in the same userns into a parent cgroup? I believe than anything but moving down the hierarchy would be very complicated to verify (how far up can you go).

Quoted 2 lines

But only if they are able to open the tasks file for writing, which they shouldn't be able to do, right?

That should be looked at very closely. There are some funny exploits of setuid root applications writing to files that have required some additional permission checks on /proc/<pid>/uid_map. I think the cgroups files may be vulnerable to some of the same kind of exploits.

Certainly we should be verifying that the opener of the file had the capabilities we are trying to use to avoid being open to those kinds of problems.

I am trying to see the utilitity of the proposed patch. It doesn't allow mknod. So what is the benefit of having the user namespace bits?

Is the point to allow the userns root to remove access to selected devices from it's children even if the DAC permissions would allow the access?

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

For the second, the hierarchy support should let us ignore concerns about unprivileged users escalating privilege, but I'm trying to decide whether we need to worry about the sendmail capability class of bugs.

Quoted 8 lines
Quoted 1 line

You have a pointer for more information on those?

Darn - unfortunately the best description of it, which was at
http://userweb.kernel.org/~morgan/sendmail-capabilities-war-story.html
is no longer there since userweb was taken down, and it was never captured by archive.org. There's a brief description in
http://lwn.net/Articles/280279/ at the paragraph starting with "The memory of the sendmail-capabilities bug from 2000..."

I think the sendmail website has something as well. The core problem was programs not dealing safely with having some of their privileges removed. As I recall an unprivileged attacker at one point could drop the CAP_SYS_SETUID on sendmail and get it to deliver mail as root.

I took a good hard look at this issue before implementing limits on setuid in the user namespace and it appears that the authors of setuid application learned from this as well and I could not find an single program that would call setuid without out checking it's return code.

That said I haven't looked at open or mknod, and usually we are talking about calls that aren't made by suid apps so I think there is a fair chance that dropping some of those permissions could cause issues.

The first danger that crosses my mind is what happens if you remove access to /dev/tty from a normal application that would trying and log strange goings on to a user if they could.

Shrug mostly I don't see the advantage of this change.

Eric

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines

My sense is actually the answer is no, and we can drop the capable() check altogether. The reason is that while userspace frequently doesn't properly handle a failing system call due to unexpected lack of partial privilege, I wouldn't expect any setuid root program to ignore failure to open or mknod a device file (and proceed into a bad failure mode). Does this sound rasonable, or a recipe for disaster?

The second case sounds ok to me

-serge

maintainer
#18 lore raw
Quoted 49 lines · Eric W. Biederman
Quoted 39 lines · Serge Hallyn
Quoted 22 lines · Aristeu Rozanski
Quoted 21 lines · Serge Hallyn

so now that the device cgroup properly respects hierarchy, not allowing a cgroup to be given greater permission than its parent, should we consider relaxing the capability checks?

There are two capable(CAP_SYS_ADMIN) checks in deice_cgroup.c: one in devcgroup_can_attach() to protect changing another task's cgroup, and one in devcgroup_update_access() to protect writes to the devices.allow and devices.deny files.

I think the first should be changed to a check for ns_capable() to the victim's user_ns. Something like

security/device_cgroup.c +2 -0
@@ -70,10 +70,16 @@ static int devcgroup_can_attach(struct cgroup *new_cgrp,
                                struct cgroup_taskset *set)
 {
        struct task_struct *task = cgroup_taskset_first(set);
+       struct user_namespace *ns;
+       int ret = -EPERM;
 
Quoted 15 lines

security/device_cgroup.c static int devcgroup_can_attach(struct cgroup *new_cgrp, · line 83

Quoted 11 lines
security/device_cgroup.c +7 -3
-       if (current != task && !capable(CAP_SYS_ADMIN))
-               return -EPERM;
-       return 0;
+       if (current == task)
+               return 0;
+
+       ns = userns_get(task);;
+       ret = ns_capable(ns, CAP_SYS_ADMIN) ? 0 : -EPERM;
+       put_user_ns(ns);
+       return ret;
 }

wouldn't this allow a userns root to move a task in the same userns into a parent cgroup? I believe than anything but moving down the hierarchy would be very complicated to verify (how far up can you go).

Quoted 2 lines

But only if they are able to open the tasks file for writing, which they shouldn't be able to do, right?

That should be looked at very closely. There are some funny exploits of setuid root applications writing to files that have required some additional permission checks on /proc/<pid>/uid_map. I think the cgroups files may be vulnerable to some of the same kind of exploits.

Quoted 6 lines

Certainly we should be verifying that the opener of the file had the capabilities we are trying to use to avoid being open to those kinds of problems.

I am trying to see the utilitity of the proposed patch. It doesn't allow mknod. So what is the benefit of having the user namespace bits?

I'm still thinking through it, which is why I haven't sent a real patch. What I'm working on is the unprivileged startup of a container. Right now most things are not allowed in a private user ns, so device cgroup is not as useful. But it should be possible eventually to use block devices, which the original unprivileged user owned, by chowning the blockdev to a user mapped into the target userns.

The unprivileged user may want to use devices cgroup so he can chown the loop file into the container, but only allow read-only mounts, for instance.

Quoted 3 lines

Is the point to allow the userns root to remove access to selected devices from it's children even if the DAC permissions would allow the access?

Yes I think that's it - except userns root before forking the container init (and venturing into the really untrusted category).

...

Quoted 6 lines

That said I haven't looked at open or mknod, and usually we are talking about calls that aren't made by suid apps so I think there is a fair chance that dropping some of those permissions could cause issues. The first danger that crosses my mind is what happens if you remove access to /dev/tty from a normal application that would trying and log strange goings on to a user if they could.

If they were going to do that over tty, that would be to the malicious user anyway, so that should just either be ignored, or result in the program exiting early.

Quoted 1 line

Shrug mostly I don't see the advantage of this change.

It's also possible that this will end up being worked around by the new (not-yet-designed) interface/library which Tejun wants people to use, sitting above the cgroupfs. At least at a first layer.

Anyway this isn't urgent, as it's not in the way for general unprivileged container creation. But in general if we don't need the check to be capable(), it would be better to introduce the right check.

-serge

maintainer
#19 lore raw
Quoted 94 lines · Serge E. Hallyn
Quoted 49 lines · Eric W. Biederman
Quoted 39 lines · Serge Hallyn
Quoted 22 lines · Aristeu Rozanski
Quoted 21 lines · Serge Hallyn

so now that the device cgroup properly respects hierarchy, not allowing a cgroup to be given greater permission than its parent, should we consider relaxing the capability checks?

There are two capable(CAP_SYS_ADMIN) checks in deice_cgroup.c: one in devcgroup_can_attach() to protect changing another task's cgroup, and one in devcgroup_update_access() to protect writes to the devices.allow and devices.deny files.

I think the first should be changed to a check for ns_capable() to the victim's user_ns. Something like

security/device_cgroup.c +2 -0
@@ -70,10 +70,16 @@ static int devcgroup_can_attach(struct cgroup *new_cgrp,
                                struct cgroup_taskset *set)
 {
        struct task_struct *task = cgroup_taskset_first(set);
+       struct user_namespace *ns;
+       int ret = -EPERM;
 
Quoted 15 lines

security/device_cgroup.c static int devcgroup_can_attach(struct cgroup *new_cgrp, · line 83

Quoted 11 lines
security/device_cgroup.c +7 -3
-       if (current != task && !capable(CAP_SYS_ADMIN))
-               return -EPERM;
-       return 0;
+       if (current == task)
+               return 0;
+
+       ns = userns_get(task);;
+       ret = ns_capable(ns, CAP_SYS_ADMIN) ? 0 : -EPERM;
+       put_user_ns(ns);
+       return ret;
 }

wouldn't this allow a userns root to move a task in the same userns into a parent cgroup? I believe than anything but moving down the hierarchy would be very complicated to verify (how far up can you go).

Quoted 2 lines

But only if they are able to open the tasks file for writing, which they shouldn't be able to do, right?

That should be looked at very closely. There are some funny exploits of setuid root applications writing to files that have required some additional permission checks on /proc/<pid>/uid_map. I think the cgroups files may be vulnerable to some of the same kind of exploits.

Quoted 6 lines

Certainly we should be verifying that the opener of the file had the capabilities we are trying to use to avoid being open to those kinds of problems.

I am trying to see the utilitity of the proposed patch. It doesn't allow mknod. So what is the benefit of having the user namespace bits?

I'm still thinking through it, which is why I haven't sent a real patch. What I'm working on is the unprivileged startup of a container. Right now most things are not allowed in a private user ns, so device cgroup is not as useful. But it should be possible eventually to use block devices, which the original unprivileged user owned, by chowning the blockdev to a user mapped into the target userns.

The unprivileged user may want to use devices cgroup so he can chown the loop file into the container, but only allow read-only mounts, for instance.

Quoted 3 lines

Is the point to allow the userns root to remove access to selected devices from it's children even if the DAC permissions would allow the access?

Yes I think that's it - except userns root before forking the container init (and venturing into the really untrusted category).

...

Quoted 6 lines

That said I haven't looked at open or mknod, and usually we are talking about calls that aren't made by suid apps so I think there is a fair chance that dropping some of those permissions could cause issues. The first danger that crosses my mind is what happens if you remove access to /dev/tty from a normal application that would trying and log strange goings on to a user if they could.

If they were going to do that over tty, that would be to the malicious user anyway, so that should just either be ignored, or result in the program exiting early.

Quoted 1 line

Shrug mostly I don't see the advantage of this change.

It's also possible that this will end up being worked around by the new (not-yet-designed) interface/library which Tejun wants people to use, sitting above the cgroupfs. At least at a first layer.

Quoted 5 lines

Anyway this isn't urgent, as it's not in the way for general unprivileged container creation. But in general if we don't need the check to be capable(), it would be better to introduce the right check.

-serge

I'm terribly sorry, Andrew, I have no idea how that address for you got into my address book. (Corrected) fwiw the thread can be followed at
https://lkml.org/lkml/2013/5/14/363 .

-serge