unshare: Fix nsproxy leak on set_cred_ucounts() error path
Subsystems (5)
PATCH 1/1 unshare: Fix nsproxy leak on set_cred_ucounts() error path
If unshare_nsproxy_namespaces() successfully creates the new_nsproxy, but then set_cred_ucounts() fails, on its error path there is no cleanup for new_nsproxy, so it is leaked. Let's fix that by freeing new_nsproxy if it's not NULL on this error path.
--- kernel/fork.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
kernel/fork.c +4 -1
@@ -3133,8 +3133,11 @@ int ksys_unshare(unsigned long unshare_flags)if (new_cred) { err =set_cred_ucounts (new_cred); -if (err) +if (err) { +if (new_nsproxy) +free_nsproxy (new_nsproxy); goto bad_unshare_cleanup_cred; + } }if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {
Signature · 1 line
2.51.1
Quoted 7 lines · Pavel Tikhomirov
If
unshare_nsproxy_namespaces()successfully creates thenew_nsproxy, but thenset_cred_ucounts()fails, on its error path there is no cleanup fornew_nsproxy, so it is leaked. Let's fix that by freeingnew_nsproxyif it's not NULL on this error path.Fixes 905ae01c4ae2a ("Add a reference to ucounts for each cred")Signed-off-by Pavel Tikhomirov <[email protected]>
Quoted 24 lines
--- kernel/fork.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)kernel/fork.c +4
-1@@ -3133,8 +3133,11 @@ int ksys_unshare(unsigned long unshare_flags)if (new_cred) { err =set_cred_ucounts (new_cred); -if (err) +if (err) { +if (new_nsproxy) +free_nsproxy (new_nsproxy); goto bad_unshare_cleanup_cred; + } }if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {Signature · 1 line
2.51.1
Signature · 1 line
Rgrds, legion
Quoted 4 lines · Pavel Tikhomirov
If
unshare_nsproxy_namespaces()successfully creates thenew_nsproxy, but thenset_cred_ucounts()fails, on its error path there is no cleanup fornew_nsproxy, so it is leaked. Let's fix that by freeingnew_nsproxyif it's not NULL on this error path.
new_nsproxy may be set to an error pointer, but that case is handled earlier in this function. That's a pretty subtle detail. unshare_nsproxy_namespaces() should probably set it to NULL if it's an error.. everywhere else looks fine.
This fix looks good!
Quoted 27 lines
Fixes 905ae01c4ae2a ("Add a reference to ucounts for each cred")Signed-off-by Pavel Tikhomirov <[email protected]>--- kernel/fork.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)kernel/fork.c +4
-1@@ -3133,8 +3133,11 @@ int ksys_unshare(unsigned long unshare_flags)if (new_cred) { err =set_cred_ucounts (new_cred); -if (err) +if (err) { +if (new_nsproxy) +free_nsproxy (new_nsproxy); goto bad_unshare_cleanup_cred; + } }if (new_fs || new_fd || do_sysvsem || new_cred || new_nsproxy) {Signature · 1 line
2.51.1