fork: fix active reference underflow on unshare() failure
Subsystems (3)
PATCH 1/1 fork: fix active reference underflow on unshare() failure
From: Tao Cui <[email protected]>
When set_cred_ucounts() fails in ksys_unshare() the new nsproxy is released with put_nsproxy(). But the nsproxy has not been installed at this point; it is only marked as active once switch_task_namespaces() installs it, which happens after all failure points. Dropping the last reference therefore deactivates it and puts the active reference of every namespace it points to: for namespaces created by this unshare() call the active count underflows to -1, and for namespaces carried over from the caller a live active reference of the still-running task is dropped, which makes them disappear from nsfs file handles and listns and causes a cascading underflow at task exit.
Use nsproxy_free() on this error path, which drops the regular references without touching the active ones, like the setns() failure paths already do; see commit cefd55bd2159 ("nsproxy: fix free_nsproxy() and simplify create_new_namespaces()").
--- include/linux/nsproxy.h | 1 + kernel/fork.c | 2 +- kernel/nsproxy.c | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-)
include/linux/nsproxy.h +1 -0
@@ -100,6 +100,7 @@ void exit_cred_namespaces(struct task_struct *tsk);void switch_task_namespaces (struct task_struct *tsk,struct nsproxy *new);int exec_task_namespaces (void );void deactivate_nsproxy (struct nsproxy *ns); +void nsproxy_free (struct nsproxy *ns);int unshare_nsproxy_namespaces (unsigned long ,struct nsproxy **,struct cred *,struct fs_struct *);int __init nsproxy_cache_init (void );
kernel/fork.c +1 -1
@@ -3324,7 +3324,7 @@ int ksys_unshare(unsigned long unshare_flags) bad_unshare_cleanup_nsproxy:if (new_nsproxy) -put_nsproxy (new_nsproxy); +nsproxy_free (new_nsproxy); bad_unshare_cleanup_cred:if (new_cred)put_cred (new_cred);
kernel/nsproxy.c +1 -1
@@ -61,7 +61,7 @@ static inline struct nsproxy *create_nsproxy(void)return nsproxy; } -static inline void nsproxy_free (struct nsproxy *ns) +void nsproxy_free (struct nsproxy *ns) {put_mnt_ns (ns->mnt_ns);put_uts_ns (ns->uts_ns);
Signature · 1 line
2.43.0
Quoted 14 lines · Tao Cui
From: Tao Cui <[email protected]>
When
set_cred_ucounts()fails inksys_unshare()the new nsproxy is released withput_nsproxy(). But the nsproxy has not been installed at this point; it is only marked as active onceswitch_task_namespaces()installs it, which happens after all failure points. Dropping the last reference therefore deactivates it and puts the active reference of every namespace it points to: for namespaces created by thisunshare()call the active count underflows to -1, and for namespaces carried over from the caller a live active reference of the still-running task is dropped, which makes them disappear from nsfs file handles and listns and causes a cascading underflow at task exit.
Quoted 8 lines
Use
nsproxy_free()on this error path, which drops the regular references without touching the active ones, like thesetns()failure paths already do; see commitcefd55bd2159("nsproxy: fixfree_nsproxy()and simplifycreate_new_namespaces()").Fixes a98621a0f187a ("unshare: fix nsproxy leak in ksys_unshare() on set_cred_ucounts() failure")
Splat:
WARNING: kernel/nscommon.c:171 at __ns_ref_active_put (VFS_WARN_ON_ONCE, active < 0) NIP: __ns_ref_active_put+0x11c LR: nsproxy_ns_active_put+0x30 Call Trace: deactivate_nsproxy+0x24 <- ksys_unshare+0x528
followed by a second WARNING at put_mnt_ns (ns_common.h:78)
Quoted 39 lines
Signed-off-by Tao Cui <[email protected]>---
include/linux/nsproxy.h | 1 + kernel/fork.c | 2 +- kernel/nsproxy.c | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-)include/linux/nsproxy.h +0
-0@@ -100,6 +100,7 @@ void exit_cred_namespaces(struct task_struct *tsk);void
switch_task_namespaces(struct task_struct *tsk, struct nsproxy *new);
intexec_task_namespaces(void);
voiddeactivate_nsproxy(struct nsproxy *ns);
+voidnsproxy_free(struct nsproxy *ns);
int unshare_nsproxy_namespaces(unsigned long, struct nsproxy **,struct cred *, struct fs_struct *);int
__initnsproxy_cache_init(void);kernel/fork.c +0
-0@@ -3324,7 +3324,7 @@ int ksys_unshare(unsigned long unshare_flags)
bad_unshare_cleanup_nsproxy:
if (new_nsproxy)kernel/fork.c +1
-1-put_nsproxy (new_nsproxy); +nsproxy_free (new_nsproxy);
bad_unshare_cleanup_cred:if (new_cred) put_cred(new_cred);kernel/nsproxy.c +0
-0
kernel/nsproxy.c static inline struct nsproxy *create_nsproxy(void) · line 67
Quoted 8 lines
kernel/nsproxy.c +1
-1@@ -61,7 +61,7 @@ static inline struct nsproxy *create_nsproxy(void)return nsproxy; } -static inline void nsproxy_free (struct nsproxy *ns) +void nsproxy_free (struct nsproxy *ns) {put_mnt_ns (ns->mnt_ns);put_uts_ns (ns->uts_ns);
--- Thanks!
"I'm not a very positive person" - Linus torvalds
Quoted 14 lines · Tao Cui
From: Tao Cui <[email protected]>
When
set_cred_ucounts()fails inksys_unshare()the new nsproxy is released withput_nsproxy(). But the nsproxy has not been installed at this point; it is only marked as active onceswitch_task_namespaces()installs it, which happens after all failure points. Dropping the last reference therefore deactivates it and puts the active reference of every namespace it points to: for namespaces created by thisunshare()call the active count underflows to -1, and for namespaces carried over from the caller a live active reference of the still-running task is dropped, which makes them disappear from nsfs file handles and listns and causes a cascading underflow at task exit.
Quoted 8 lines
Use
nsproxy_free()on this error path, which drops the regular references without touching the active ones, like thesetns()failure paths already do; see commitcefd55bd2159("nsproxy: fixfree_nsproxy()and simplifycreate_new_namespaces()").Fixes a98621a0f187a ("unshare: fix nsproxy leak in ksys_unshare() on set_cred_ucounts() failure")
Quoted 45 lines
Signed-off-by Tao Cui <[email protected]>--- include/linux/nsproxy.h | 1 + kernel/fork.c | 2 +- kernel/nsproxy.c | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-)include/linux/nsproxy.h +1
-0@@ -100,6 +100,7 @@ void exit_cred_namespaces(struct task_struct *tsk);void switch_task_namespaces (struct task_struct *tsk,struct nsproxy *new);int exec_task_namespaces (void );void deactivate_nsproxy (struct nsproxy *ns); +void nsproxy_free (struct nsproxy *ns);int unshare_nsproxy_namespaces (unsigned long ,struct nsproxy **,struct cred *,struct fs_struct *);int __init nsproxy_cache_init (void );kernel/fork.c +1
-1@@ -3324,7 +3324,7 @@ int ksys_unshare(unsigned long unshare_flags) bad_unshare_cleanup_nsproxy:if (new_nsproxy) -put_nsproxy (new_nsproxy); +nsproxy_free (new_nsproxy); bad_unshare_cleanup_cred:if (new_cred)put_cred (new_cred);kernel/nsproxy.c +1
-1@@ -61,7 +61,7 @@ static inline struct nsproxy *create_nsproxy(void)return nsproxy; } -static inline void nsproxy_free (struct nsproxy *ns) +void nsproxy_free (struct nsproxy *ns) {put_mnt_ns (ns->mnt_ns);put_uts_ns (ns->uts_ns);
Signature · 2 lines
Best regards, Pavel Tikhomirov Senior Software Developer, Virtuozzo.
Quoted 22 lines · Pavel Tikhomirov
Quoted 19 lines · Tao Cui
From: Tao Cui <[email protected]>
When
set_cred_ucounts()fails inksys_unshare()the new nsproxy is released withput_nsproxy(). But the nsproxy has not been installed at this point; it is only marked as active onceswitch_task_namespaces()installs it, which happens after all failure points. Dropping the last reference therefore deactivates it and puts the active reference of every namespace it points to: for namespaces created by thisunshare()call the active count underflows to -1, and for namespaces carried over from the caller a live active reference of the still-running task is dropped, which makes them disappear from nsfs file handles and listns and causes a cascading underflow at task exit.Use
nsproxy_free()on this error path, which drops the regular references without touching the active ones, like thesetns()failure paths already do; see commitcefd55bd2159("nsproxy: fixfree_nsproxy()and simplifycreate_new_namespaces()").
Quoted 5 lines
Quoted 3 lines
Fixes a98621a0f187a ("unshare: fix nsproxy leak in ksys_unshare() on set_cred_ucounts() failure")Reviewed-by Pavel Tikhomirov <[email protected]>
Thanks, already fixed upstream! :)