[PATCH 2/3] cred: add proc_set_cred_init helper

From: Mateusz Guzik <mjguzik_at_gmail.com>
Date: Sat, 21 Mar 2015 02:00:39 +0100
From: Mateusz Guzik <mjg_at_freebsd.org>

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.
---
 sys/kern/init_main.c |  2 +-
 sys/kern/kern_fork.c |  2 +-
 sys/kern/kern_prot.c | 16 ++++++++++++++--
 sys/sys/ucred.h      |  1 +
 4 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 82cf63f..88cd44c 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
_at__at_ -515,7 +515,7 _at__at_ 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
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 15833fd..a3a70b8 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
_at__at_ -867,7 +867,7 _at__at_ 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.
diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c
index 72c9f65..9c49f71 100644
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
_at__at_ -1954,8 +1954,19 _at__at_ cred_update_thread(struct thread *td)
 }
 
 /*
+ * 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
_at__at_ -1968,9 +1979,10 _at__at_ 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;
diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h
index 2b42b01..9a45308 100644
--- a/sys/sys/ucred.h
+++ b/sys/sys/ucred.h
_at__at_ -106,6 +106,7 _at__at_ 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);
-- 
2.3.2
Received on Sat Mar 21 2015 - 00:00:48 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:56 UTC