proc_set_cred(): Allow 'newcred' to have multiple references

This is an extension needed by next commit, where some additional
reference is kept on the credentials to be set on a process in order to
keep these credentials alive even after the process lock is released (an
intervening reset of process credentials could release the reference
that the process holds).

Only 'cr_users' is incremented, as the reference (counted in 'cr_ref')
comes from the caller, who passes it to the process.

Reviewed by:    kib, markj
MFC after:      1 week
Sponsored by:   The FreeBSD Foundation
Differential Revision:  https://reviews.freebsd.org/D53636
This commit is contained in:
Olivier Certner
2025-11-06 14:25:57 -08:00
parent 39acb7fd86
commit 5d46d11772
+3 -5
View File
@@ -2792,10 +2792,6 @@ _proc_set_cred(struct proc *p, struct ucred *newcred, bool enforce_proc_lim)
MPASS(oldcred != NULL);
PROC_LOCK_ASSERT(p, MA_OWNED);
KASSERT(newcred->cr_users == 0, ("%s: users %d not 0 on cred %p",
__func__, newcred->cr_users, newcred));
KASSERT(newcred->cr_ref == 1, ("%s: ref %ld not 1 on cred %p",
__func__, newcred->cr_ref, newcred));
if (newcred->cr_ruidinfo != oldcred->cr_ruidinfo) {
/*
@@ -2821,8 +2817,10 @@ _proc_set_cred(struct proc *p, struct ucred *newcred, bool enforce_proc_lim)
__func__, oldcred->cr_users, oldcred));
oldcred->cr_users--;
mtx_unlock(&oldcred->cr_mtx);
mtx_lock(&newcred->cr_mtx);
newcred->cr_users++;
mtx_unlock(&newcred->cr_mtx);
p->p_ucred = newcred;
newcred->cr_users = 1;
PROC_UPDATE_COW(p);
if (newcred->cr_ruidinfo != oldcred->cr_ruidinfo)
(void)chgproccnt(oldcred->cr_ruidinfo, -1, 0);