git: 684531038126 - main - kern: fix use after free in sysctl_kern_proc_umask()

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

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

commit 6845310381261099da1f39386657a9efd7122070
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2026-11-23 19:25:53 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-11-23 19:43:37 +0000

    kern: fix use after free in sysctl_kern_proc_umask()
    
    sysctl_kern_proc_umask() held the target process and then read
    p->p_pd->pd_cmask without the process lock and without a reference on
    the pwddesc. The hold keeps exit1() from freeing p_pd, but it does not
    stop pdswap().  pdswap() releases the old pwddesc, which can then be
    freed while the sysctl still reads it.
    
    Read pd_cmask with the process lock held instead of holding the process.
    pdescfree_tail() waits for the process lock to be dropped before it
    releases the old pwddesc, so the pwddesc read from p_pd under the lock
    stays valid until the unlock.  pget() checks P_WEXIT under the same
    lock, and exit1() sets P_WEXIT before pdescfree() clears p_pd, so p_pd
    cannot be NULL either.
    
    Reported by:    llm-postcr
    Reviewed by:    markj
    Fixes:  6ce13747dc3c ("Add sysctl to retrieve or set umask of another process.")
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D61955
---
 sys/kern/kern_proc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c
index 398de3a182f5..c0a6f9767db2 100644
--- a/sys/kern/kern_proc.c
+++ b/sys/kern/kern_proc.c
@@ -3115,12 +3115,12 @@ sysctl_kern_proc_umask(SYSCTL_HANDLER_ARGS)
 		goto out;
 	}
 
-	error = pget(pid, PGET_WANTREAD, &p);
+	error = pget(pid, PGET_CANDEBUG | PGET_NOTWEXIT, &p);
 	if (error != 0)
 		return (error);
 
 	cmask = p->p_pd->pd_cmask;
-	PRELE(p);
+	PROC_UNLOCK(p);
 out:
 	error = SYSCTL_OUT(req, &cmask, sizeof(cmask));
 	return (error);