Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 10 May 2021 01:14:52 GMT
From:      Konstantin Belousov <kib@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 57d7b30ea855 - stable/13 - kern_ptrace(): extract code to determine ptrace eligibility into helper
Message-ID:  <202105100114.14A1Eqit028690@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by kib:

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

commit 57d7b30ea855ee506b86ccba9b753bcdecda14a2
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2021-04-24 11:52:11 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2021-05-10 01:02:40 +0000

    kern_ptrace(): extract code to determine ptrace eligibility into helper
    
    (cherry picked from commit 54c8baa021957bc026406b3a424296e84b28baa5)
---
 sys/kern/sys_process.c | 46 +++++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
index cc440d932eb5..c93bfe15324c 100644
--- a/sys/kern/sys_process.c
+++ b/sys/kern/sys_process.c
@@ -601,6 +601,30 @@ proc_set_traced(struct proc *p, bool stop)
 	p->p_ptevents = PTRACE_DEFAULT;
 }
 
+static int
+proc_can_ptrace(struct thread *td, struct proc *p)
+{
+	PROC_LOCK_ASSERT(p, MA_OWNED);
+
+	if ((p->p_flag & P_WEXIT) != 0)
+		return (ESRCH);
+
+	/* not being traced... */
+	if ((p->p_flag & P_TRACED) == 0)
+		return (EPERM);
+
+	/* not being traced by YOU */
+	if (p->p_pptr != td->td_proc)
+		return (EBUSY);
+
+	/* not currently stopped */
+	if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
+	    p->p_suspcount != p->p_numthreads  ||
+	    (p->p_flag & P_WAITED) == 0)
+		return (EBUSY);
+
+	return (0);
+}
 int
 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
 {
@@ -758,27 +782,11 @@ kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
 
 		/* FALLTHROUGH */
 	default:
-		/* not being traced... */
-		if ((p->p_flag & P_TRACED) == 0) {
-			error = EPERM;
+		error = proc_can_ptrace(td, p);
+		if (error != 0)
 			goto fail;
-		}
-
-		/* not being traced by YOU */
-		if (p->p_pptr != td->td_proc) {
-			error = EBUSY;
-			goto fail;
-		}
 
-		/* not currently stopped */
-		if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
-		    p->p_suspcount != p->p_numthreads  ||
-		    (p->p_flag & P_WAITED) == 0) {
-			error = EBUSY;
-			goto fail;
-		}
-
-		/* OK */
+		/* Ok */
 		break;
 	}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202105100114.14A1Eqit028690>