git: 88409417be88 - stable/15 - kern_ptrace(): reduce code duplication

Konstantin Belousov <[email protected]>
Newsgroups gmane.os.freebsd.devel.cvs.src,gmane.os.freebsd.devel.stable.scm
Message-ID <[email protected]>
The branch stable/15 has been updated by kib:

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

commit 88409417be8847b94fa2702eb727d6102f620af2
Author:     Konstantin Belousov <[email protected]>
AuthorDate: 2026-07-19 01:14:39 +0000
Commit:     Konstantin Belousov <[email protected]>
CommitDate: 2026-07-27 00:28:44 +0000

    kern_ptrace(): reduce code duplication
    
    (cherry picked from commit 9b21a52495758294e3a50542a59bc47a0c184173)
---
 sys/kern/sys_process.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
index 43cea67d1e1a..9ba8ec33e5dc 100644
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -906,7 +906,7 @@ ptrace_unsuspend(struct proc *p)
 }
 
 static int
-proc_can_ptrace(struct thread *td, struct proc *p)
+proc_can_ptrace1(struct thread *td, struct proc *p)
 {
 	int error;
 
@@ -914,11 +914,22 @@ proc_can_ptrace(struct thread *td, struct proc *p)
 
 	if ((p->p_flag & P_WEXIT) != 0)
 		return (ESRCH);
-
 	if ((error = p_cansee(td, p)) != 0)
 		return (error);
 	if ((error = p_candebug(td, p)) != 0)
 		return (error);
+	return (0);
+}
+
+static int
+proc_can_ptrace(struct thread *td, struct proc *p)
+{
+	int error;
+
+	PROC_LOCK_ASSERT(p, MA_OWNED);
+
+	if ((error = proc_can_ptrace1(td, p)) != 0)
+		return (error);
 
 	/* not being traced... */
 	if ((p->p_flag & P_TRACED) == 0)
@@ -1084,14 +1095,8 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
 	}
 	AUDIT_ARG_PROCESS(p);
 
-	if ((p->p_flag & P_WEXIT) != 0) {
-		error = ESRCH;
-		goto fail;
-	}
-	if ((error = p_cansee(td, p)) != 0)
-		goto fail;
-
-	if ((error = p_candebug(td, p)) != 0)
+	error = proc_can_ptrace1(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.