From nobody Sun Aug 16 02:46:58 2026 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4hN0jZ6Wk1z6pH27 for ; Sun, 16 Aug 2026 02:46:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR2" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hN0jZ4Zyxz3fH3 for ; Sun, 16 Aug 2026 02:46:58 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786848418; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8pz36BhN3dgvVsoqXpcwOubdLzm9PEmwKvCrFHvQkHc=; b=yI58liANksx/IebitFQ1cocQ8xu3Q2nKJyDMG3ub55L+Q29/Ef1w7VvXRzjMluEaDruwgX /NWQEvJtl0FdAV3Xb06bHcgkAh6jrzNR51Fl32+f/EBHaVoMPm4WLsYb9CJo1tKJHCGnQz Lo+hzimYx8ZuPE9MND24W/6VgRcdBwxlZSHGAwIiY+qPYyL1TLdHn6vDbLQkW6YFpVJiH7 sVKbQt38vEdK6QqWkhtjD4CW9K9Ciqt33Q62p6Fgn7jZOSl1OGKmzoCbos0XppOu1poWpp RfrrmDCcTao2TQijAYdeJbTLgglekD988Nkk5K7SC6DFUk+QiRApQ7ILQnEgoQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1786848418; a=rsa-sha256; cv=none; b=LuDv6Zk5EFgxgBLEkiodRBfBrCW+H80DLhiGBCbPx/6I2ojm/l2FXGYRp3fIKVIIX3dOll HviSKAA8JmlwWf+9t+Tn+8neyhUUJhchQFlakmPs9C9UGEklnyqh5tvwG4vVBx+Y5lypNl rMiAmgZRBJV8+oEUWIgkzLhj7zTzRugXRI+AT7WHxiGhb4nJ4YXK/SNDkl2hrzkJlf+C0p FE/AzN+xEvJHFoqm5CiEvND31WINMyDEBjSG0FZ8tVqGgS3nL4Ej8UUtJtY5Viy6VY7VZv /74niO9m1Fb5O1aifkgbPiXwl70IJFeTywErrrunT8P/ZzCr/gDxhRfF5J35hQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786848418; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=8pz36BhN3dgvVsoqXpcwOubdLzm9PEmwKvCrFHvQkHc=; b=Iw9W2utJrmo3QoCSmLg4UWDvEi+72DKF62hYiiWzYMV9B9ZlpFI2jM/ybG7x36/Wi55TP/ guRfuHICr78czqeaPS52vm0mmjo0tj7u/USgHHQC6iU40dp5lPT9TUlzmyZTeatLPG+VEI HYSVfponKfNGJMOu8Lo+8bS4yiPIJgp3dFAO7efZOzaXNrKOM58j0NL/SO4Sqk8TkgMYks BIsaAbP3cZG2pHNHGOdy+OLoKyMfSvTXq0yFTd+PcY0AO7LjlxOr9KH+g9TJs02E47GIDG idqBfXDeBa6mDaaZIrVQZSiRojHN9VUjTGHdTzfdA+VmZrghLAy6/9wZbo2O1g== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hN0jZ3T0Vz17Qy for ; Sun, 16 Aug 2026 02:46:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 436e0 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 16 Aug 2026 02:46:58 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: c66b6867d4b9 - stable/15 - kern: add fget_procdesc() List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/15 X-Git-Reftype: branch X-Git-Commit: c66b6867d4b90b12e1cd0789cb60ec0959d99a05 Auto-Submitted: auto-generated Date: Sun, 16 Aug 2026 02:46:58 +0000 Message-Id: <6a8124a2.436e0.2ca628a7@gitrepo.freebsd.org> The branch stable/15 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=c66b6867d4b90b12e1cd0789cb60ec0959d99a05 commit c66b6867d4b90b12e1cd0789cb60ec0959d99a05 Author: Konstantin Belousov AuthorDate: 2026-07-08 14:21:58 +0000 Commit: Konstantin Belousov CommitDate: 2026-08-16 02:41:30 +0000 kern: add fget_procdesc() (cherry picked from commit 97de8330e1683e41883887d19ea489bda90e5c61) --- sys/kern/kern_exit.c | 10 +-- sys/kern/kern_sig.c | 14 +++-- sys/kern/sys_procdesc.c | 160 +++++++++++++++++++++++------------------------- sys/sys/procdesc.h | 6 +- 4 files changed, 93 insertions(+), 97 deletions(-) diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index 73c5a6f9f224..8fcbea8f8cf8 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -1566,14 +1566,9 @@ kern_pdwait(struct thread *td, int fd, int *status, if (error != 0) return (error); - error = fget(td, fd, &cap_pdwait_rights, &fp); + error = fget_procdesc(td, fd, &cap_pdwait_rights, &fp, &pd, NULL); if (error != 0) - return (error); - if (fp->f_type != DTYPE_PROCDESC) { - error = EINVAL; goto exit_unlocked; - } - pd = fp->f_data; for (;;) { /* We own a reference on the procdesc file. */ @@ -1624,7 +1619,8 @@ kern_pdwait(struct thread *td, int fd, int *status, exit_tree_locked: sx_xunlock(&proctree_lock); exit_unlocked: - fdrop(fp, td); + if (fp != NULL) + fdrop(fp, td); return (error); } diff --git a/sys/kern/kern_sig.c b/sys/kern/kern_sig.c index c2561b490f78..27ab109d2ac5 100644 --- a/sys/kern/kern_sig.c +++ b/sys/kern/kern_sig.c @@ -1956,6 +1956,7 @@ int sys_pdkill(struct thread *td, struct pdkill_args *uap) { struct proc *p; + struct file *fp; int error; AUDIT_ARG_SIGNUM(uap->signum); @@ -1963,14 +1964,19 @@ sys_pdkill(struct thread *td, struct pdkill_args *uap) if ((u_int)uap->signum > _SIG_MAXSIG) return (EINVAL); - error = procdesc_find(td, uap->fd, &cap_pdkill_rights, &p); - if (error) - return (error); + sx_slock(&proctree_lock); + error = fget_procdesc(td, uap->fd, &cap_pdkill_rights, &fp, NULL, &p); + sx_sunlock(&proctree_lock); + if (error != 0) + goto out; AUDIT_ARG_PROCESS(p); error = p_cansignal(td, p, uap->signum); - if (error == 0 && uap->signum) + if (error == 0 && uap->signum != 0) kern_psignal(p, uap->signum); PROC_UNLOCK(p); +out: + if (fp != NULL) + fdrop(fp, td); return (error); } diff --git a/sys/kern/sys_procdesc.c b/sys/kern/sys_procdesc.c index dd8119296930..ad2003051fd8 100644 --- a/sys/kern/sys_procdesc.c +++ b/sys/kern/sys_procdesc.c @@ -116,38 +116,6 @@ static const struct fileops procdesc_ops = { .fo_flags = DFLAG_PASSABLE, }; -/* - * Return a locked process given a process descriptor, or ESRCH if it has - * died. - */ -int -procdesc_find(struct thread *td, int fd, const cap_rights_t *rightsp, - struct proc **p) -{ - struct procdesc *pd; - struct file *fp; - int error; - - error = fget(td, fd, rightsp, &fp); - if (error) - return (error); - if (fp->f_type != DTYPE_PROCDESC) { - error = EINVAL; - goto out; - } - pd = fp->f_data; - sx_slock(&proctree_lock); - if (pd->pd_proc != NULL) { - *p = pd->pd_proc; - PROC_LOCK(*p); - } else - error = ESRCH; - sx_sunlock(&proctree_lock); -out: - fdrop(fp, td); - return (error); -} - /* * Function to be used by procstat(1) sysctls when returning procdesc * information. @@ -174,16 +142,11 @@ kern_pdgetpid(struct thread *td, int fd, const cap_rights_t *rightsp, struct file *fp; int error; - error = fget(td, fd, rightsp, &fp); - if (error) - return (error); - if (fp->f_type != DTYPE_PROCDESC) { - error = EBADF; - goto out; - } - *pidp = procdesc_pid(fp); -out: - fdrop(fp, td); + error = fget_procdesc(td, fd, rightsp, &fp, NULL, NULL); + if (error == 0) + *pidp = procdesc_pid(fp); + if (fp != NULL) + fdrop(fp, td); return (error); } @@ -725,59 +688,88 @@ sys_pdopenpid(struct thread *td, struct pdopenpid_args *args) return (kern_pdopenpid(td, args->pid, args->flags)); } +/* + * Get the file/process descriptor/process from the procdesc file + * descriptor. The process descriptor and process returns are + * optional. If requested to return the process, the proctree lock + * must be held, and the process will be returned locked. + * + * The caller must fdrop(*pfp) if *pfp != NULL, regardless of the + * error returned, after the proctree_lock is unlocked. + * procdesc_close() takes the proctree_lock. + */ +int +fget_procdesc(struct thread *td, int pdfd, const cap_rights_t *cap_rights, + struct file **pfp, struct procdesc **pdp, struct proc **pp) +{ + struct file *fp; + struct procdesc *pd; + struct proc *p; + int error; + + if (pp != NULL) + sx_assert(&proctree_lock, SX_LOCKED); + + *pfp = NULL; + error = fget(td, pdfd, cap_rights, &fp); + if (error != 0) + return (error); + *pfp = fp; + if (fp->f_type != DTYPE_PROCDESC) + return (EBADF); + pd = fp->f_data; + if (pp != NULL) { + p = pd->pd_proc; + if (p == NULL) { + return (ESRCH); + } else { + *pp = p; + PROC_LOCK(p); + } + } + if (pdp != NULL) + *pdp = pd; + return (0); +} + static int kern_pddupfd(struct thread *td, int pdfd, int fd, int flags) { struct proc *p; struct file *fp, *pfp; - struct procdesc *pd; struct filecaps fcaps; uint8_t fd_flags; int error, fdr; - error = fget(td, pdfd, &cap_pddupfd_rights, &pfp); - if (error != 0) - return (error); - if (pfp->f_type != DTYPE_PROCDESC) { - error = EBADF; - goto out; - } - pd = pfp->f_data; -again: sx_slock(&proctree_lock); - p = pd->pd_proc; - if (p != NULL) { - AUDIT_ARG_PROCESS(p); - PROC_LOCK(p); - sx_sunlock(&proctree_lock); + error = fget_procdesc(td, pdfd, &cap_pddupfd_rights, &pfp, NULL, &p); + if (error == 0) { if ((p->p_flag & P_WEXIT) != 0) { error = ESRCH; + PROC_UNLOCK(p); } else { - /* - * Block the target process from entering - * execve(). We need to ensure that the - * p_candebug() predicate is stable until the - * fget_remote() call ends even after the - * process lock is dropped. For that, the - * process must not change uid/suid. - */ - if (!execve_block(td, p)) - goto again; - error = p_candebug(td, p); - if (error == 0) - _PHOLD(p); - else - execve_unblock(td, p); + _PHOLD(p); } - PROC_UNLOCK(p); - if (error != 0) - goto out; + } + sx_sunlock(&proctree_lock); + if (error != 0) + goto out; + AUDIT_ARG_PROCESS(p); + PROC_LOCK_ASSERT(p, MA_OWNED); - error = fget_remote(td, p, fd, &fcaps, &fd_flags, &fp); - PROC_LOCK(p); - execve_unblock(td, p); - _PRELE(p); + /* + * Block the target process from entering execve(). + * We need to ensure that the p_candebug() predicate + * is stable until the fget_remote() call ends even + * after the process lock is dropped. For that, the + * process must not change uid/suid. + */ + execve_block_wait(td, p); + error = p_candebug(td, p); + + if (error == 0) { PROC_UNLOCK(p); + error = fget_remote(td, p, fd, &fcaps, &fd_flags, &fp); if (error == 0) { error = finstall_refed(td, fp, &fdr, O_CLOEXEC | ((fd_flags & FD_RESOLVE_BENEATH) != 0 ? @@ -789,12 +781,14 @@ again: td->td_retval[0] = fdr; } } - } else { - sx_sunlock(&proctree_lock); - error = ESRCH; + PROC_LOCK(p); } + execve_unblock(td, p); + _PRELE(p); + PROC_UNLOCK(p); out: - fdrop(pfp, td); + if (pfp != NULL) + fdrop(pfp, td); return (error); } diff --git a/sys/sys/procdesc.h b/sys/sys/procdesc.h index dafa3326384d..715232d5a00e 100644 --- a/sys/sys/procdesc.h +++ b/sys/sys/procdesc.h @@ -102,8 +102,6 @@ struct procdesc { * In-kernel interfaces to process descriptors. */ int procdesc_exit(struct proc *); -int procdesc_find(struct thread *, int fd, const cap_rights_t *, - struct proc **); void procdesc_jobstate(struct proc *p); int kern_pdgetpid(struct thread *, int fd, const cap_rights_t *, pid_t *pidp); @@ -114,7 +112,9 @@ void procdesc_reap(struct proc *); int procdesc_falloc(struct thread *, struct file **, int *, int, struct filecaps *); - +int fget_procdesc(struct thread *td, int pfd, + const cap_rights_t *cap_rights, struct file **pfp, + struct procdesc **pdp, struct proc **pp); #else /* !_KERNEL */ #include