cred: add proc_set_cred_init helper

proc_set_cred_init can be used to set first credentials of a new
process.

Update proc_set_cred assertions so that it only expects already used
processes.

This fixes panics where p_ucred of a new process happens to be non-NULL.

Reviewed by:	kib
This commit is contained in:
Mateusz Guzik 2015-03-21 20:24:54 +00:00
parent 12cec311e6
commit ffb34484ee
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=280331
4 changed files with 17 additions and 4 deletions

View File

@ -515,7 +515,7 @@ proc0_init(void *dummy __unused)
newcred->cr_ruidinfo = uifind(0);
newcred->cr_prison = &prison0;
newcred->cr_loginclass = loginclass_find("default");
proc_set_cred(p, newcred);
proc_set_cred_init(p, newcred);
#ifdef AUDIT
audit_cred_kproc0(newcred);
#endif

View File

@ -867,7 +867,7 @@ fork1(struct thread *td, int flags, int pages, struct proc **procp,
* XXX: This is ugly; when we copy resource usage, we need to bump
* per-cred resource counters.
*/
proc_set_cred(newproc, crhold(td->td_ucred));
proc_set_cred_init(newproc, crhold(td->td_ucred));
/*
* Initialize resource accounting for the child process.

View File

@ -1953,9 +1953,20 @@ cred_update_thread(struct thread *td)
crfree(cred);
}
/*
* Set initial process credentials.
* Callers are responsible for providing the reference for provided credentials.
*/
void
proc_set_cred_init(struct proc *p, struct ucred *newcred)
{
p->p_ucred = newcred;
}
/*
* Change process credentials.
* Callers are responsible for providing the reference for current credentials
* Callers are responsible for providing the reference for passed credentials
* and for freeing old ones.
*
* Process has to be locked except when it does not have credentials (as it
@ -1968,9 +1979,10 @@ proc_set_cred(struct proc *p, struct ucred *newcred)
{
struct ucred *oldcred;
MPASS(p->p_ucred != NULL);
if (newcred == NULL)
MPASS(p->p_state == PRS_ZOMBIE);
else if (p->p_ucred != NULL)
else
PROC_LOCK_ASSERT(p, MA_OWNED);
oldcred = p->p_ucred;

View File

@ -106,6 +106,7 @@ void crcopy(struct ucred *dest, struct ucred *src);
struct ucred *crcopysafe(struct proc *p, struct ucred *cr);
struct ucred *crdup(struct ucred *cr);
void cred_update_thread(struct thread *td);
void proc_set_cred_init(struct proc *p, struct ucred *cr);
struct ucred *proc_set_cred(struct proc *p, struct ucred *cr);
void crfree(struct ucred *cr);
struct ucred *crget(void);