Skip to contentSkip to conversation

userns: Let LSMs deny the CAP_SETFCAP snapshot for mapping uid 0

PATCH Applied Mickaël Salaün 6 participants 7 messages Nov 10 – Nov 11

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

PATCH 1/1 userns: Let LSMs deny the CAP_SETFCAP snapshot for mapping uid 0

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

Use security_capable() against the parent namespace instead: the creator's credentials still belong to it at this point, so commoncap returns the same result, and only LSM denials are added.

Do not audit this check: creating a user namespace does not use CAP_SETFCAP, and most namespaces never map uid 0. The uid_map write that later fails has no LSM audit record.

Checking CAP_SETFCAP when the map is written would not work: the writer's credentials then belong to the new namespace, where commoncap grants the creator all capabilities.

Cc Christian Brauner <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge Hallyn <[email protected]>
Fixes db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")

---

This is independent from Josef Bacik's series, which reworks this code to close other ways around the CAP_SETFCAP rule:
https://patch.msgid.link/[email protected]

That series still computes the creator's CAP_SETFCAP with cap_capable() and cap_raised() in cap_setfcap_level(), so it would need the same change.

---
 include/linux/user_namespace.h | 5 +++--
 kernel/user_namespace.c        | 8 +++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/user_namespace.h +3 -2
index 91232053775d..370961fde45f 100644
@@ -86,8 +86,9 @@ struct user_namespace {
 	kgid_t			group;
 	struct ns_common	ns;
 	unsigned long		flags;
-	/* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
-	 * in its effective capability set at the child ns creation time. */
+	/* parent_could_setfcap: true if the creator of this ns could use
+	 * CAP_SETFCAP in the parent ns at the child ns creation time.
+	 */
 	bool			parent_could_setfcap;
 
 #ifdef CONFIG_KEYS
kernel/user_namespace.c +7 -1
index cd748e1cbace..57b7f10be74e 100644
@@ -128,7 +128,13 @@ int create_user_ns(struct cred *new)
 	if (!ns)
 		goto fail_dec;
 
-	ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
+	/*
+	 * verify_root_map() relies on this once the creator only has
+	 * capabilities in the new namespace.  Let LSMs deny it, but do not
+	 * audit: the creator may never map uid 0.
+	 */
+	ns->parent_could_setfcap =
+		!security_capable(new, parent_ns, CAP_SETFCAP, CAP_OPT_NOAUDIT);
 
 	ret = ns_common_init(ns);
 	if (ret)
Signature · 1 line
2.56.0
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

Use security_capable() against the parent namespace instead: the creator's credentials still belong to it at this point, so commoncap returns the same result, and only LSM denials are added.

Do not audit this check: creating a user namespace does not use CAP_SETFCAP, and most namespaces never map uid 0. The uid_map write that later fails has no LSM audit record.

Checking CAP_SETFCAP when the map is written would not work: the writer's credentials then belong to the new namespace, where commoncap grants the creator all capabilities.

Quoted 7 lines
Cc Christian Brauner <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge Hallyn <[email protected]>
Fixes db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Mickaël Salaün
Quoted 33 lines · Mickaël Salaün

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

Use security_capable() against the parent namespace instead: the creator's credentials still belong to it at this point, so commoncap returns the same result, and only LSM denials are added.

Do not audit this check: creating a user namespace does not use CAP_SETFCAP, and most namespaces never map uid 0. The uid_map write that later fails has no LSM audit record.

Checking CAP_SETFCAP when the map is written would not work: the writer's credentials then belong to the new namespace, where commoncap grants the creator all capabilities.

Cc Christian Brauner <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge Hallyn <[email protected]>
Fixes db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")

---

This is independent from Josef Bacik's series, which reworks this code to close other ways around the CAP_SETFCAP rule:
https://patch.msgid.link/[email protected]

Quoted 7 lines

That series still computes the creator's CAP_SETFCAP with cap_capable() and cap_raised() in cap_setfcap_level(), so it would need the same change.

---
 include/linux/user_namespace.h | 5 +++--
 kernel/user_namespace.c        | 8 +++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)

Looks reasonable to me.

Signature · 1 line
paul-moore.com
maintainer Reviewed-by on v1 1/1
#3 lore raw
In reply to Mickaël Salaün
Quoted 15 lines · Mickaël Salaün

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

Use security_capable() against the parent namespace instead: the creator's credentials still belong to it at this point, so commoncap returns the same result, and only LSM denials are added.

Do not audit this check: creating a user namespace does not use CAP_SETFCAP, and most namespaces never map uid 0. The uid_map write that later fails has no LSM audit record.

Quoted 8 lines

Checking CAP_SETFCAP when the map is written would not work: the writer's credentials then belong to the new namespace, where commoncap grants the creator all capabilities.

Cc Christian Brauner <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge Hallyn <[email protected]>

I wonder whether parent_could_setfcap should be renamed creator_could_setfcap, as now, years later, the name makes me think "the user namespace's parent ns could do it".

Quoted 54 lines
Fixes db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")

---

This is independent from Josef Bacik's series, which reworks this code to close other ways around the CAP_SETFCAP rule:
https://patch.msgid.link/[email protected]

That series still computes the creator's CAP_SETFCAP with cap_capable() and cap_raised() in cap_setfcap_level(), so it would need the same change.

---
 include/linux/user_namespace.h | 5 +++--
 kernel/user_namespace.c        | 8 +++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/user_namespace.h +3 -2
index 91232053775d..370961fde45f 100644
@@ -86,8 +86,9 @@ struct user_namespace {
 	kgid_t			group;
 	struct ns_common	ns;
 	unsigned long		flags;
-	/* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
-	 * in its effective capability set at the child ns creation time. */
+	/* parent_could_setfcap: true if the creator of this ns could use
+	 * CAP_SETFCAP in the parent ns at the child ns creation time.
+	 */
 	bool			parent_could_setfcap;
 
 #ifdef CONFIG_KEYS
kernel/user_namespace.c +7 -1
index cd748e1cbace..57b7f10be74e 100644
@@ -128,7 +128,13 @@ int create_user_ns(struct cred *new)
 	if (!ns)
 		goto fail_dec;
 
-	ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
+	/*
+	 * verify_root_map() relies on this once the creator only has
+	 * capabilities in the new namespace.  Let LSMs deny it, but do not
+	 * audit: the creator may never map uid 0.
+	 */
+	ns->parent_could_setfcap =
+		!security_capable(new, parent_ns, CAP_SETFCAP, CAP_OPT_NOAUDIT);
 
 	ret = ns_common_init(ns);
 	if (ret)
Signature · 1 line
2.56.0
Reviewed-by on v1 1/1
#4 lore raw
In reply to Mickaël Salaün
Quoted 15 lines · Mickaël Salaün

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

Use security_capable() against the parent namespace instead: the creator's credentials still belong to it at this point, so commoncap returns the same result, and only LSM denials are added.

Do not audit this check: creating a user namespace does not use CAP_SETFCAP, and most namespaces never map uid 0. The uid_map write that later fails has no LSM audit record.

Quoted 8 lines

Checking CAP_SETFCAP when the map is written would not work: the writer's credentials then belong to the new namespace, where commoncap grants the creator all capabilities.

Cc Christian Brauner <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge Hallyn <[email protected]>

I'm logging out of mainlining.org (sigh) anything meant to be cc'ing me, e.g: get maintainers should cc the linux.dev email instead, annoying brr.

Quoted 43 lines
Fixes db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")

---

This is independent from Josef Bacik's series, which reworks this code to close other ways around the CAP_SETFCAP rule:
https://patch.msgid.link/[email protected]

That series still computes the creator's CAP_SETFCAP with cap_capable() and cap_raised() in cap_setfcap_level(), so it would need the same change.
---
include/linux/user_namespace.h | 5 +++--
kernel/user_namespace.c | 8 +++++++-

2 files changed, 10 insertions(+), 3 deletions(-)
include/linux/user_namespace.h +3 -2
index 91232053775d..370961fde45f 100644
@@ -86,8 +86,9 @@ struct user_namespace {
 	kgid_t			group;
 	struct ns_common	ns;
 	unsigned long		flags;
-	/* parent_could_setfcap: true if the creator if this ns had CAP_SETFCAP
-	 * in its effective capability set at the child ns creation time. */
+	/* parent_could_setfcap: true if the creator of this ns could use
+	 * CAP_SETFCAP in the parent ns at the child ns creation time.
+	 */
 	bool			parent_could_setfcap;
 
 #ifdef CONFIG_KEYS
kernel/user_namespace.c +0 -1
index cd748e1cbace..57b7f10be74e 100644
@@ -128,7 +128,13 @@ int create_user_ns(struct cred *new)
 	if (!ns)
 		goto fail_dec;
 
-	ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);

kernel/user_namespace.c int create_user_ns(struct cred *new) · line 140

Quoted 10 lines
kernel/user_namespace.c +7 -0
+	/*
+	 * verify_root_map() relies on this once the creator only has
+	 * capabilities in the new namespace.  Let LSMs deny it, but do not
+	 * audit: the creator may never map uid 0.
+	 */
+	ns->parent_could_setfcap =
+		!security_capable(new, parent_ns, CAP_SETFCAP, CAP_OPT_NOAUDIT);
 
 	ret = ns_common_init(ns);
 	if (ret)

--- Thanks!

#5 lore raw
In reply to Mickaël Salaün
Quoted 19 lines · Mickaël Salaün

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

Use security_capable() against the parent namespace instead: the creator's credentials still belong to it at this point, so commoncap returns the same result, and only LSM denials are added.

Do not audit this check: creating a user namespace does not use CAP_SETFCAP, and most namespaces never map uid 0. The uid_map write that later fails has no LSM audit record.

Checking CAP_SETFCAP when the map is written would not work: the writer's credentials then belong to the new namespace, where commoncap grants the creator all capabilities.

Quoted 8 lines
Cc Christian Brauner <[email protected]>
Cc Josef Bacik <[email protected]>
Cc Paul Moore <[email protected]>
Cc Serge Hallyn <[email protected]>
Fixes db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0")

---

This seems fine but is another instance where what we really really really want is for the LSM to be informed where exactly security_capable() is called from. IOW, it would be nice if the LSM would know that it is called from the user namespace creation path.

Can be a follow-up but would be nice.

maintainer Applied
#6 lore raw
In reply to Mickaël Salaün
Quoted 8 lines · Mickaël Salaün

Commit db2e718a4798 ("capabilities: require CAP_SETFCAP to map uid 0") records in parent_could_setfcap whether the creator of a user namespace had CAP_SETFCAP, because a creator writing its own uid_map only has capabilities in the new namespace. The snapshot uses cap_raised(), so a task whose LSM policy denies CAP_SETFCAP can still map uid 0 in a namespace it creates.

[...]

Applied to the namespace-7.5.misc branch of the vfs/vfs.git tree. Patches in the namespace-7.5.misc branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: namespace-7.5.misc

[1/1] userns: Let LSMs deny the CAP_SETFCAP snapshot for mapping uid 0

      https://git.kernel.org/vfs/vfs/c/2397c70f7645