Skip to content

Commit 34dc2fd

Browse files
committed
ucounts: Proper error handling in set_cred_ucounts
Instead of leaking the ucounts in new if alloc_ucounts fails, store the result of alloc_ucounts into a temporary variable, which is later assigned to new->ucounts. Cc: [email protected] Fixes: 905ae01 ("Add a reference to ucounts for each cred") Link: https://lkml.kernel.org/r/87pms2s0v8.fsf_-_@disp2133 Tested-by: Yu Zhao <[email protected]> Reviewed-by: Alexey Gladkov <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
1 parent 629715a commit 34dc2fd

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

kernel/cred.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ int set_cred_ucounts(struct cred *new)
669669
{
670670
struct task_struct *task = current;
671671
const struct cred *old = task->real_cred;
672-
struct ucounts *old_ucounts = new->ucounts;
672+
struct ucounts *new_ucounts, *old_ucounts = new->ucounts;
673673

674674
if (new->user == old->user && new->user_ns == old->user_ns)
675675
return 0;
@@ -681,9 +681,10 @@ int set_cred_ucounts(struct cred *new)
681681
if (old_ucounts && old_ucounts->ns == new->user_ns && uid_eq(old_ucounts->uid, new->euid))
682682
return 0;
683683

684-
if (!(new->ucounts = alloc_ucounts(new->user_ns, new->euid)))
684+
if (!(new_ucounts = alloc_ucounts(new->user_ns, new->euid)))
685685
return -EAGAIN;
686686

687+
new->ucounts = new_ucounts;
687688
if (old_ucounts)
688689
put_ucounts(old_ucounts);
689690

0 commit comments

Comments
 (0)