git: fa577dbc5fdc - main - kern: pwd_hold_proc() might dereference the NULL p_pd

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 23 Nov 2026 19:09:18 UTC
The branch main has been updated by kib:

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

commit fa577dbc5fdc9b282c0fb97b5da7d44ff2fddcb3
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-23 01:39:14 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 19:05:55 +0000

    kern: pwd_hold_proc() might dereference the NULL p_pd
    
    pdunshare() calls pdescfree(), which stores NULL into p->p_pd, and only
    then stores the new pwddesc.  pwd_hold_proc() running in that window gets
    NULL from pdhold() and dereferences it.  pwd_hold_proc() assumed p_pd is
    never NULL for a held process.  Similarly, fork_norfproc() calls
    pdescfree() before installing a new pwddesc for the current process
    (rfork(RFCFDG) without RFPROC).
    
    Close the gap with pdswap(), which publishes the new pwddesc before
    releasing the old one.
    
    Reported by:    llm-postcr, markj
    Reviewed by:    markj
    Fixes:  85c855d31b18 ("fd: add pwd_hold_proc")
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61934
---
 sys/kern/kern_descrip.c | 19 ++++++++-----------
 sys/kern/kern_fork.c    |  9 +++++----
 2 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/sys/kern/kern_descrip.c b/sys/kern/kern_descrip.c
index d1cc35894cd2..af87412c1440 100644
--- a/sys/kern/kern_descrip.c
+++ b/sys/kern/kern_descrip.c
@@ -2603,17 +2603,18 @@ pdswap(struct proc *p, struct pwddesc *new_pdp, struct pwddesc *old_pdp)
 void
 pdunshare(struct thread *td)
 {
-	struct pwddesc *pdp;
+	struct pwddesc *old_pdp, *new_pdp;
 	struct proc *p;
 
 	p = td->td_proc;
+	old_pdp = p->p_pd;
+
 	/* Not shared. */
-	if (refcount_load(&p->p_pd->pd_sharecount) == 1)
+	if (refcount_load(&old_pdp->pd_sharecount) == 1)
 		return;
 
-	pdp = pdcopy(p->p_pd);
-	pdescfree(td);
-	p->p_pd = pdp;
+	new_pdp = pdcopy(old_pdp);
+	pdswap(p, new_pdp, old_pdp);
 }
 
 /*
@@ -2902,14 +2903,10 @@ pdescfree(struct thread *td)
 	MPASS(pdp != NULL);
 
 	/*
-	 * Check pdhold for an explanation.
+	 * Check pdhold() for an explanation.
 	 */
 	atomic_store_ptr(&p->p_pd, NULL);
-	atomic_thread_fence_seq_cst();
-	PROC_WAIT_UNLOCKED(p);
-
-	(void)refcount_release(&pdp->pd_sharecount);
-	pddrop(pdp);
+	pdescfree_tail(p, pdp);
 }
 
 /*
diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c
index 25aebe3dad3c..21b5eb87e4e7 100644
--- a/sys/kern/kern_fork.c
+++ b/sys/kern/kern_fork.c
@@ -414,14 +414,15 @@ fork_norfproc(struct thread *td, int flags)
 	 */
 	if ((flags & RFCFDG) != 0) {
 		struct filedesc *fdtmp;
-		struct pwddesc *pdtmp;
+		struct pwddesc *new_pdp, *old_pdp;
+
+		old_pdp = p1->p_pd;
+		new_pdp = pdinit(old_pdp, false);
+		pdswap(p1, new_pdp, old_pdp);
 
-		pdtmp = pdinit(td->td_proc->p_pd, false);
 		fdtmp = fdinit();
-		pdescfree(td);
 		fdescfree(td);
 		p1->p_fd = fdtmp;
-		p1->p_pd = pdtmp;
 	}
 
 	/*