git: 1ae0a4f52af6 - stable/14 - Open-code proc_set_cred_init()

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Thu, 16 Jan 2025 18:07:58 UTC
The branch stable/14 has been updated by olce:

URL: https://cgit.FreeBSD.org/src/commit/?id=1ae0a4f52af67d345d9829422b3c7cf9e704da0e

commit 1ae0a4f52af67d345d9829422b3c7cf9e704da0e
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2023-09-25 08:48:49 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-01-16 18:06:46 +0000

    Open-code proc_set_cred_init()
    
    This function is to be called only when initializing a new process (so,
    'proc0' and at fork), and not in any other circumstances.  Setting the
    process' 'p_ucred' field to the result of crcowget() on the original
    credentials is the only thing it does, hiding the fact that the process'
    'p_ucred' field is crushed by the call.  Moreover, most of the code it
    executes is already encapsulated in crcowget().
    
    To prevent misuse and improve code readability, just remove this
    function and replace it with a direct assignment to 'p_ucred'.
    
    Reviewed by:            markj (earlier version), kib
    MFC after:              1 week
    Sponsored by:           The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D42255
    
    (cherry picked from commit 92541c12bc25c59333d7f3b0721b6b16aaff3644)
---
 sys/kern/init_main.c |  2 +-
 sys/kern/kern_fork.c |  2 +-
 sys/kern/kern_prot.c | 11 -----------
 sys/sys/ucred.h      |  1 -
 4 files changed, 2 insertions(+), 14 deletions(-)

diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c
index 23a1eb944b7b..5835db44b853 100644
--- a/sys/kern/init_main.c
+++ b/sys/kern/init_main.c
@@ -562,7 +562,7 @@ proc0_init(void *dummy __unused)
 	curthread->td_ucred = NULL;
 	newcred->cr_prison = &prison0;
 	newcred->cr_users++; /* avoid assertion failure */
-	proc_set_cred_init(p, newcred);
+	p->p_ucred = crcowget(newcred);
 	newcred->cr_users--;
 	crfree(newcred);
 #ifdef AUDIT
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 9bbe7c8a8153..8054f93c2c9d 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -1060,7 +1060,7 @@ fork1(struct thread *td, struct fork_req *fr)
 	 * XXX: This is ugly; when we copy resource usage, we need to bump
 	 *      per-cred resource counters.
 	 */
-	proc_set_cred_init(newproc, td->td_ucred);
+	newproc->p_ucred = crcowget(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 85c38c474df5..44a1f26767ed 100644
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
@@ -2278,17 +2278,6 @@ cru2xt(struct thread *td, struct xucred *xcr)
 	xcr->cr_pid = td->td_proc->p_pid;
 }
 
-/*
- * 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 = crcowget(newcred);
-}
-
 /*
  * Change process credentials.
  * Callers are responsible for providing the reference for passed credentials
diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h
index e7f8d7328dda..8724cfcdc1e6 100644
--- a/sys/sys/ucred.h
+++ b/sys/sys/ucred.h
@@ -158,7 +158,6 @@ void	crcopy(struct ucred *dest, struct ucred *src);
 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
 struct ucred	*crdup(struct ucred *cr);
 void	crextend(struct ucred *cr, int n);
-void	proc_set_cred_init(struct proc *p, struct ucred *cr);
 void	proc_set_cred(struct proc *p, struct ucred *cr);
 void	proc_unset_cred(struct proc *p);
 void	crfree(struct ucred *cr);