From owner-dev-commits-src-main@freebsd.org Mon May 3 16:20:43 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B81616366FA; Mon, 3 May 2021 16:20:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FYp9l3Hrmz3DR0; Mon, 3 May 2021 16:20:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4222821381; Mon, 3 May 2021 16:20:43 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 143GKhCr022307; Mon, 3 May 2021 16:20:43 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 143GKhoI022306; Mon, 3 May 2021 16:20:43 GMT (envelope-from git) Date: Mon, 3 May 2021 16:20:43 GMT Message-Id: <202105031620.143GKhoI022306@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 54c8baa02195 - main - kern_ptrace(): extract code to determine ptrace eligibility into helper MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 54c8baa021957bc026406b3a424296e84b28baa5 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 May 2021 16:20:44 -0000 The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=54c8baa021957bc026406b3a424296e84b28baa5 commit 54c8baa021957bc026406b3a424296e84b28baa5 Author: Konstantin Belousov AuthorDate: 2021-04-24 11:52:11 +0000 Commit: Konstantin Belousov CommitDate: 2021-05-03 16:13:48 +0000 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 --- 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 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; }