aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonstantin Belousov <kib@FreeBSD.org>2021-04-24 11:52:11 +0000
committerKonstantin Belousov <kib@FreeBSD.org>2021-05-03 16:13:48 +0000
commit54c8baa021957bc026406b3a424296e84b28baa5 (patch)
treec018321c620ed2e34820b4214122a2d2461b81cb
parent2bd0506c8d94a2c581dd16407f3a1c159a38769b (diff)
downloadsrc-54c8baa021957bc026406b3a424296e84b28baa5.tar.gz
src-54c8baa021957bc026406b3a424296e84b28baa5.zip
kern_ptrace(): extract code to determine ptrace eligibility into helper
Reviewed by: markj Tested by: pho Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D29955
-rw-r--r--sys/kern/sys_process.c46
1 files changed, 27 insertions, 19 deletions
diff --git a/sys/kern/sys_process.c b/sys/kern/sys_process.c
index 492ef287ca53..4eb7383ca8f0 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;
}