The branch main has been updated by kib:

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

commit eca7b25c101a240472c4c274e725bc294284c827
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-07-20 22:09:59 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-07-21 02:58:30 +0000

    kern/sys_ptrace: do not skip P2_PTRACEREQ wait for 
PT_CLEARSTEP/PT_GET_CHILDREN
    
    Reported and reviewed by:       markj
    Fixes:  d3b7bbee9275 ("ptrace(2): add PT_GET_CHILDREN")
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D58364
---
 sys/kern/sys_process.c | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
index fc81ac4ca6c8..73e6b64e900b 100644
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -1075,7 +1075,7 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void 
*addr, int data)
 #ifdef COMPAT_FREEBSD32
        int wrap32 = 0, safe = 0;
 #endif
-       bool proctree_locked, p2_req_set;
+       bool need_can_ptrace, proctree_locked, p2_req_set;
 
        curp = td->td_proc;
        proctree_locked = false;
@@ -1162,6 +1162,7 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void 
*addr, int data)
        /*
         * Permissions check
         */
+       need_can_ptrace = true;
        switch (req) {
        case PT_TRACE_ME:
                /*
@@ -1204,26 +1205,24 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void 
*addr, int data)
                /* OK */
                break;
 
-       case PT_CLEARSTEP:
-               /* Allow thread to clear single step for itself */
-               if (td->td_tid == tid)
-                       break;
-               goto default_check;
-
-       case PT_GET_CHILDREN:
-               if (p == curp)
-                       break;
-               goto default_check;
-
-default_check:
        default:
+               /*
+                * Allow thread to clear single step for itself.
+                * PT_GET_CHILDREN on itself does not need P_TRACED.
+                */
+               if ((req == PT_CLEARSTEP && td->td_tid == tid) ||
+                   (req == PT_GET_CHILDREN && p == curp))
+                       need_can_ptrace = false;
+
                /*
                 * Check for ptrace eligibility before waiting for
                 * holds to drain.
                 */
-               error = proc_can_ptrace(td, p);
-               if (error != 0)
-                       goto fail;
+               if (need_can_ptrace) {
+                       error = proc_can_ptrace(td, p);
+                       if (error != 0)
+                               goto fail;
+               }
 
                /*
                 * Block parallel ptrace requests.  Most important, do
@@ -1241,7 +1240,7 @@ default_check:
                        }
                        if (error == 0 && td2->td_proc != p)
                                error = ESRCH;
-                       if (error == 0)
+                       if (error == 0 && need_can_ptrace)
                                error = proc_can_ptrace(td, p);
                        if (error != 0)
                                goto fail;

Reply via email to