userns: clean up dead code on error paths
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
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
@@ -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
Re: [PATCH 1/2] userns: remove dead key_put() on the create_user_ns() error path
Quoted 9 lines · Tao Cui
From: Tao Cui <[email protected]>
create_user_ns()jumps tofail_keyringwhensetup_userns_sysctls()fails. At that point ns was freshly allocated withkmem_cache_zalloc()andns->persistent_keyring_registeris only ever assigned later, lazily, fromkey_get_persistent()(security/keys/persistent.c). Thekey_put()therefore always receives NULL.
Quoted 3 lines
Remove the dead call.
Signed-off-by Tao Cui <[email protected]>
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@@ -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
Re: [PATCH 1/2] userns: remove dead key_put() on the create_user_ns() error path
In reply to Tao CuiQuoted 11 lines · Tao Cui
From: Tao Cui <[email protected]>
create_user_ns()jumps tofail_keyringwhensetup_userns_sysctls()fails. At that point ns was freshly allocated withkmem_cache_zalloc()andns->persistent_keyring_registeris only ever assigned later, lazily, fromkey_get_persistent()(security/keys/persistent.c). Thekey_put()therefore always receives NULL.Remove the dead call.
Agreed,
Quoted 16 lines
Signed-off-by Tao Cui <[email protected]>---
kernel/user_namespace.c| 3 ---1 file changed, 3 deletions(-)kernel/user_namespace.c +0
-0@@ -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);
PATCH 2/2 userns: don't clear the install target map on map_write() failure
In reply to Tao CuiFrom: 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
@@ -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
Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure
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-EPERMbefore 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.
Signed-off-by Tao Cui <[email protected]>
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@@ -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
Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure
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-EPERMbefore 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.
Signed-off-by Tao Cui <[email protected]>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. :)
Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure
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-EPERMbefore 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
Signed-off-by Tao Cui <[email protected]>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
Re: [PATCH 2/2] userns: don't clear the install target map on map_write() failure
In reply to Tao CuiQuoted 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-EPERMbefore 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
Signed-off-by Tao Cui <[email protected]>---
kernel/user_namespace.c| 3 ---1 file changed, 3 deletions(-)kernel/user_namespace.c +0
-0
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);
Christian Brauner
AppliedRe: (subset) [PATCH 0/2] userns: clean up dead code on error paths
In reply to Tao CuiQuoted 6 lines · Tao Cui
From: Tao Cui <[email protected]>
Two small cleanups on the
create_user_ns()andmap_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()atfail_keyringhas been dead since it was copy-pasted fromfree_user_ns()in 2016 (dbec28460a89);persistent_keyring_registeris only ever assigned lazily fromkey_get_persistent(), long aftercreate_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