Skip to contentSkip to conversation

userns: clean up dead code on error paths

PATCH Applied Tao Cui 4 participants 10 messages Aug 28 – Aug 31

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

cover userns: clean up dead code on error paths

From: Tao Cui <[email protected]>

Two small cleanups on the create_user_ns() and map_write() error paths. Both remove code that provably cannot have an effect; no behavior change.

user_namespace.c:164-167: the key_put() at fail_keyring has been dead since it was copy-pasted from free_user_ns() in 2016 (dbec28460a89); persistent_keyring_register is only ever assigned lazily from key_get_persistent(), long after create_user_ns().

user_namespace.c:1110-1112: the map clearing at the out: label of map_write() only ever zeroes an already-zero map; the destination map is untouched on every error path, and a second write to a mapped namespace is rejected before parsing.

Compile-tested and A/B-tested on next-20260827: identical behavior on vanilla and patched kernels for single-write map failures, recovery writes and 300 rounds of userns create/destroy.

Tao Cui (2):
userns: remove dead key_put() on the create_user_ns() error path
userns: don't clear the install target map on map_write() failure

 kernel/user_namespace.c | 6 ------
 1 file changed, 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 userns: remove dead key_put() on the create_user_ns() error path

From: Tao Cui <[email protected]>

create_user_ns() jumps to fail_keyring when setup_userns_sysctls() fails. At that point ns was freshly allocated with kmem_cache_zalloc() and ns->persistent_keyring_register is only ever assigned later, lazily, from key_get_persistent() (security/keys/persistent.c). The key_put() therefore always receives NULL.

Remove the dead call.

---
 kernel/user_namespace.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/user_namespace.c +0 -3
index 0bed462e9b2a..e9e04ce167df 100644
@@ -162,9 +162,6 @@ int create_user_ns(struct cred *new)
 	ns_tree_add(ns);
 	return 0;
 fail_keyring:
-#ifdef CONFIG_PERSISTENT_KEYRINGS
-	key_put(ns->persistent_keyring_register);
-#endif
 	ns_common_free(ns);
 fail_free:
 	kmem_cache_free(user_ns_cachep, ns);
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH 2/2 userns: don't clear the install target map on map_write() failure

In reply to Tao Cui

From: Tao Cui <[email protected]>

At the out: label of map_write() the destination map is also cleared (map->forward = NULL; map->reverse = NULL; map->nr_extents = 0) whenever the write failed and the extent arrays had to be freed.

However, the destination map is written by the successful install block above, which has no failure exit; every error path reaches
out: without having touched it. A second write to a mapped namespace is also rejected with -EPERM before any parsing happens, so the clearing can never roll back a previously installed map either.

The three assignments just zero an already-zero map. Remove them.

---
 kernel/user_namespace.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/user_namespace.c +0 -3
index e9e04ce167df..cef5e71779f6 100644
@@ -1104,9 +1104,6 @@ static ssize_t map_write(struct file *file, const char __user *buf,
 	if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 		kfree(new_map.forward);
 		kfree(new_map.reverse);
-		map->forward = NULL;
-		map->reverse = NULL;
-		map->nr_extents = 0;
 	}
 
 	mutex_unlock(&userns_state_mutex);
Signature · 1 line
2.43.0
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] userns: remove dead key_put() on the create_user_ns() error path

In reply to Tao Cui
Quoted 9 lines · Tao Cui

From: Tao Cui <[email protected]>

create_user_ns() jumps to fail_keyring when setup_userns_sysctls() fails. At that point ns was freshly allocated with kmem_cache_zalloc() and ns->persistent_keyring_register is only ever assigned later, lazily, from key_get_persistent() (security/keys/persistent.c). The key_put() therefore always receives NULL.

Quoted 3 lines

Remove the dead call.

Adding Eric to CC for verification but yes, that code seems to be dead since its introduction by Eric 10 years ago. Feel free to add:

Honza

Quoted 21 lines
---
 kernel/user_namespace.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/user_namespace.c +0 -3
index 0bed462e9b2a..e9e04ce167df 100644
@@ -162,9 +162,6 @@ int create_user_ns(struct cred *new)
 	ns_tree_add(ns);
 	return 0;
 fail_keyring:
-#ifdef CONFIG_PERSISTENT_KEYRINGS
-	key_put(ns->persistent_keyring_register);
-#endif
 	ns_common_free(ns);
 fail_free:
 	kmem_cache_free(user_ns_cachep, ns);
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#5 lore raw

Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure

In reply to Tao Cui
Quoted 13 lines · Tao Cui

From: Tao Cui <[email protected]>

At the out: label of map_write() the destination map is also cleared (map->forward = NULL; map->reverse = NULL; map->nr_extents = 0) whenever the write failed and the extent arrays had to be freed.

However, the destination map is written by the successful install block above, which has no failure exit; every error path reaches
out: without having touched it. A second write to a mapped namespace is also rejected with -EPERM before any parsing happens, so the clearing can never roll back a previously installed map either.

Quoted 3 lines

The three assignments just zero an already-zero map. Remove them.

I agree this is a dead code but it kind of makes it easier to argue about the correctness of the error handling branch. I guess I'll leave it to for Christian to decide whether he wants to take this or not.

Honza

Quoted 21 lines
---
 kernel/user_namespace.c | 3 ---
 1 file changed, 3 deletions(-)
kernel/user_namespace.c +0 -3
index e9e04ce167df..cef5e71779f6 100644
@@ -1104,9 +1104,6 @@ static ssize_t map_write(struct file *file, const char __user *buf,
 	if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 		kfree(new_map.forward);
 		kfree(new_map.reverse);
-		map->forward = NULL;
-		map->reverse = NULL;
-		map->nr_extents = 0;
 	}
 
 	mutex_unlock(&userns_state_mutex);
Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] userns: remove dead key_put() on the create_user_ns() error path

In reply to Tao Cui
Quoted 11 lines · Tao Cui

From: Tao Cui <[email protected]>

create_user_ns() jumps to fail_keyring when setup_userns_sysctls() fails. At that point ns was freshly allocated with kmem_cache_zalloc() and ns->persistent_keyring_register is only ever assigned later, lazily, from key_get_persistent() (security/keys/persistent.c). The key_put() therefore always receives NULL.

Remove the dead call.

Agreed,

Quoted 16 lines

---
kernel/user_namespace.c | 3 ---

1 file changed, 3 deletions(-)
kernel/user_namespace.c +0 -0
index 0bed462e9b2a..e9e04ce167df 100644
@@ -162,9 +162,6 @@ int create_user_ns(struct cred *new)
 	ns_tree_add(ns);
 	return 0;

fail_keyring:

kernel/user_namespace.c +0 -3
-#ifdef CONFIG_PERSISTENT_KEYRINGS
-	key_put(ns->persistent_keyring_register);
-#endif
Quoted 4 lines
	ns_common_free(ns);
fail_free:
	kmem_cache_free(user_ns_cachep, ns);

--- Thanks!
https://lore.kernel.org/all/[email protected]/

#8 lore raw

Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure

In reply to Tao Cui
Quoted 6 lines · Tao Cui

From: Tao Cui <[email protected]>

At the out: label of map_write() the destination map is also cleared (map->forward = NULL; map->reverse = NULL; map->nr_extents = 0) whenever the write failed and the extent arrays had to be freed.

Quoted 9 lines

However, the destination map is written by the successful install block above, which has no failure exit; every error path reaches
out: without having touched it. A second write to a mapped namespace is also rejected with -EPERM before any parsing happens, so the clearing can never roll back a previously installed map either.

The three assignments just zero an already-zero map. Remove them.

Thinking about NAKing, but I have comments.

Quoted 10 lines

---
kernel/user_namespace.c | 3 ---

1 file changed, 3 deletions(-)
kernel/user_namespace.c +0 -0
index e9e04ce167df..cef5e71779f6 100644

kernel/user_namespace.c static ssize_t map_write(struct file *file, const char __user *buf, · line 1109

Quoted 6 lines
kernel/user_namespace.c +0 -3
@@ -1104,9 +1104,6 @@ static ssize_t map_write(struct file *file, const char __user *buf,
 	if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
 		kfree(new_map.forward);
 		kfree(new_map.reverse);
-		map->forward = NULL;
-		map->reverse = NULL;
-		map->nr_extents = 0;

Yup, that's dead code, and I would add a tag here, but the thing is, this code is a defensive pattern, so if some bloke decided "hmmmm let's add a failure path", then that would be stupid if this code was gone.

If I was the maintainer, I wouldn't take it.

kernel/user_namespace.c static ssize_t map_write(struct file *file, const char __user *buf, · line 1109

Quoted 4 lines
kernel/user_namespace.c +0 -0
 	}
 
 	mutex_unlock(&userns_state_mutex);

--- Thanks!
https://lore.kernel.org/all/[email protected]/

#6 lore raw

Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure

In reply to Jan Kara
Quoted 14 lines · Jan Kara
Quoted 13 lines · Tao Cui

From: Tao Cui <[email protected]>

At the out: label of map_write() the destination map is also cleared (map->forward = NULL; map->reverse = NULL; map->nr_extents = 0) whenever the write failed and the extent arrays had to be freed.

However, the destination map is written by the successful install block above, which has no failure exit; every error path reaches
out: without having touched it. A second write to a mapped namespace is also rejected with -EPERM before any parsing happens, so the clearing can never roll back a previously installed map either.

Quoted 7 lines
Quoted 3 lines

The three assignments just zero an already-zero map. Remove them.

I agree this is a dead code but it kind of makes it easier to argue about the correctness of the error handling branch. I guess I'll leave it to for Christian to decide whether he wants to take this or not.

Nah, let's leave this as is. :)

#9 lore raw

Re: (subset) [PATCH 0/2] userns: clean up dead code on error paths

In reply to Tao Cui
Quoted 6 lines · Tao Cui

From: Tao Cui <[email protected]>

Two small cleanups on the create_user_ns() and map_write() error paths. Both remove code that provably cannot have an effect; no behavior change.

Quoted 6 lines

user_namespace.c:164-167: the key_put() at fail_keyring has been dead since it was copy-pasted from free_user_ns() in 2016 (dbec28460a89); persistent_keyring_register is only ever assigned lazily from key_get_persistent(), long after create_user_ns().

[...]

Applied to the vfs-7.4.namespace branch of the vfs/vfs.git tree. Patches in the vfs-7.4.namespace 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: master

[1/2] userns: remove dead key_put() on the create_user_ns() error path

      https://git.kernel.org/vfs/vfs/c/fec2c3c0744b
author
#7 lore raw

Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure

In reply to Christian Brauner
Quoted 17 lines · Christian Brauner
Quoted 16 lines · Jan Kara
Quoted 15 lines · Tao Cui

From: Tao Cui <[email protected]>

At the out: label of map_write() the destination map is also cleared (map->forward = NULL; map->reverse = NULL; map->nr_extents = 0) whenever the write failed and the extent arrays had to be freed.

However, the destination map is written by the successful install block above, which has no failure exit; every error path reaches
out: without having touched it. A second write to a mapped namespace is also rejected with -EPERM before any parsing happens, so the clearing can never roll back a previously installed map either.

The three assignments just zero an already-zero map. Remove them.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I agree this is a dead code but it kind of makes it easier to argue about the correctness of the error handling branch. I guess I'll leave it to for Christian to decide whether he wants to take this or not.

Nah, let's leave this as is. :)

Understood, that's a fair point. I'm fine dropping this one.

Thanks for the review, Jan. Thanks for picking up 1/2, Christian. And thanks for the Reviewed-by tag, Bradley.

--
Tao