git: 1e5d774279e1 - stable/15 - kern/sys_ptrace: do not skip P2_PTRACEREQ wait for PT_CLEARSTEP/PT_GET_CHILDREN

Konstantin Belousov <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src
Message-ID <6a66a690.27e47.4ad73368__26156.9459618599$1785112590$gmane$org@gitrepo.freebsd.org>
The branch stable/15 has been updated by kib:

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

commit 1e5d774279e193eba2fb36386b637f994eba038f
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-07-20 22:09:59 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-07-27 00:28:43 +0000

    kern/sys_ptrace: do not skip P2_PTRACEREQ wait for PT_CLEARSTEP/PT_GET_CHILDREN
    
    (cherry picked from commit eca7b25c101a240472c4c274e725bc294284c827)
---
 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 b5cba235de47..43cea67d1e1a 100644
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -1031,7 +1031,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;
@@ -1124,6 +1124,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:
 		/*
@@ -1166,26 +1167,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
@@ -1203,7 +1202,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;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.