Skip to content

Commit c923a8e

Browse files
committed
ucounts: Move RLIMIT_NPROC handling after set_user
During set*id() which cred->ucounts to charge the the current process to is not known until after set_cred_ucounts. So move the RLIMIT_NPROC checking into a new helper flag_nproc_exceeded and call flag_nproc_exceeded after set_cred_ucounts. This is very much an arbitrary subset of the places where we currently change the RLIMIT_NPROC accounting, designed to preserve the existing logic. Fixing the existing logic will be the subject of another series of changes. Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20220216155832.680775-4-ebiederm@xmission.com Fixes: 21d1c5e ("Reimplement RLIMIT_NPROC on top of ucounts") Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent a55d072 commit c923a8e

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

kernel/sys.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,16 @@ static int set_user(struct cred *new)
472472
if (!new_user)
473473
return -EAGAIN;
474474

475+
free_uid(new->user);
476+
new->user = new_user;
477+
return 0;
478+
}
479+
480+
static void flag_nproc_exceeded(struct cred *new)
481+
{
482+
if (new->ucounts == current_ucounts())
483+
return;
484+
475485
/*
476486
* We don't fail in case of NPROC limit excess here because too many
477487
* poorly written programs don't check set*uid() return code, assuming
@@ -480,14 +490,10 @@ static int set_user(struct cred *new)
480490
* failure to the execve() stage.
481491
*/
482492
if (is_ucounts_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) &&
483-
new_user != INIT_USER)
493+
new->user != INIT_USER)
484494
current->flags |= PF_NPROC_EXCEEDED;
485495
else
486496
current->flags &= ~PF_NPROC_EXCEEDED;
487-
488-
free_uid(new->user);
489-
new->user = new_user;
490-
return 0;
491497
}
492498

493499
/*
@@ -562,6 +568,7 @@ long __sys_setreuid(uid_t ruid, uid_t euid)
562568
if (retval < 0)
563569
goto error;
564570

571+
flag_nproc_exceeded(new);
565572
return commit_creds(new);
566573

567574
error:
@@ -624,6 +631,7 @@ long __sys_setuid(uid_t uid)
624631
if (retval < 0)
625632
goto error;
626633

634+
flag_nproc_exceeded(new);
627635
return commit_creds(new);
628636

629637
error:
@@ -703,6 +711,7 @@ long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
703711
if (retval < 0)
704712
goto error;
705713

714+
flag_nproc_exceeded(new);
706715
return commit_creds(new);
707716

708717
error:

0 commit comments

Comments
 (0)