From nobody Thu Jun 29 08:20:37 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QsBGZ1f4kz4knkL; Thu, 29 Jun 2023 08:20:38 +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 4QsBGZ11vZz3vff; Thu, 29 Jun 2023 08:20:38 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688026838; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+oAVEwyEn5ejVGU54Rc3hkUC+mcdiOaSH8JVxdsUIhM=; b=ywp5B4i5kh+PtG3l4xPakJKp6k+5aVpj4FHdRIITQudEThTyuuxelk5GxA2xbpUT45OYHX 6wswtVmZBpBGWdriSZFH4rP553pL/gaH4whigmdJGpiEyB6ne22zmTxpy1zV/MWVSyeUiu CRf3zPTbfSvEdP76Nz9WIeXUv2AcZl9UkwgvshfF3xLGDjWXtH2HnYEydxuO4TMydooc96 7wjfO28HFO2regAVT16IUQPZ65eTAplid/sHv3qzGK3apmbYO/AHP9mYJH7sljzxBanwXG ZAjKw2JjV9ANJTXKzYHPTzlqQLlztAEL1rm7XQG/iDcc1UIcX9FZh5sfTN9gkQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1688026838; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=+oAVEwyEn5ejVGU54Rc3hkUC+mcdiOaSH8JVxdsUIhM=; b=fw8I5HB5vSUDlXQ4PlduZGGZR9lP4KJYcxy6Ia5MoTuKGDcJ+ludlRHPA4YDxysmUY4y7e lNiglHBt9duBtOAverX2E/VSJo2JuZIZisNJQC6P/8oLGcTa4Sx1NTorBc8wTtnaa1ep1p dgvaLPyp5I10ZoKle+ksTOv5uWwIhUgjv/MFWIxgxnwWK3rcLfu4+gmX2vJjkDX86nRN/i Wcl3pknMVeU6K0TEBeW+baHyUZ4XMIGY5ugcWPkQrRuP7lRDVFlT831rJKHQ54Sc6lnx3X rLjZnZzMAtkp3tgPTMS/kUCTHjkxjs0fGZkI6I91011r4w/Lv97j9BLz7Vls+A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1688026838; a=rsa-sha256; cv=none; b=tOu62MnGv2pcGRmV7HFnYpbCP4cALzT6jNSz7/HTOT6FMgqFZvZLR3Pg56zQXMzwNtffzW EoXPcol/23bSMM3+fHjiGE6UBnKyeFnOJPwdUasH9+nt3YgW099KPqO6/P/ej/dsuDmsPo qyddaz4vhSVl5Tgs7WOpAzarOny/rt/sQkl00CpGHoF/JeNC70yxo3I8Z93kdhOuPcVE0e G7Py7dgdHulzJazF8N60xP7EawauNc1ATLykHJye57KauKO7rjhOQBZ8ARJV3LhJTQp/Pf MgmjXfJQp5xaF3CMeqIJM2da1aJj+MBrvh/G3C5JZUvtVWhCkiUAaPbNpKxq5Q== 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 4QsBGZ06rMz181j; Thu, 29 Jun 2023 08:20:38 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 35T8Kb9K084548; Thu, 29 Jun 2023 08:20:37 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 35T8Kb3K084547; Thu, 29 Jun 2023 08:20:37 GMT (envelope-from git) Date: Thu, 29 Jun 2023 08:20:37 GMT Message-Id: <202306290820.35T8Kb3K084547@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Dmitry Chagin Subject: git: 050cd99a65a5 - stable/13 - linux(4): Implement ptrace_pokeusr for x86_64 List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: dchagin X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 050cd99a65a565b678d47c0e6980e14f4fca9112 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=050cd99a65a565b678d47c0e6980e14f4fca9112 commit 050cd99a65a565b678d47c0e6980e14f4fca9112 Author: Dmitry Chagin AuthorDate: 2023-05-18 17:02:35 +0000 Commit: Dmitry Chagin CommitDate: 2023-06-29 08:15:59 +0000 linux(4): Implement ptrace_pokeusr for x86_64 Differential Revision: https://reviews.freebsd.org/D40097 MFC after: 1 week (cherry picked from commit 1d76741520c031730319ed976a6c394213991504) --- sys/amd64/linux/linux_machdep.c | 73 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 3 deletions(-) diff --git a/sys/amd64/linux/linux_machdep.c b/sys/amd64/linux/linux_machdep.c index ddb291169a03..6ac0ab0cd3d7 100644 --- a/sys/amd64/linux/linux_machdep.c +++ b/sys/amd64/linux/linux_machdep.c @@ -397,12 +397,79 @@ linux_ptrace_peekuser(struct thread *td, pid_t pid, void *addr, void *data) return (copyout(&val, data, sizeof(val))); } +static inline bool +linux_invalid_selector(u_short val) +{ + + return (val != 0 && ISPL(val) != SEL_UPL); +} + +struct linux_segreg_off { + uintptr_t reg; + bool is0; +}; + +const struct linux_segreg_off linux_segregs_off[] = { + { + .reg = offsetof(struct linux_pt_regset, gs), + .is0 = true, + }, + { + .reg = offsetof(struct linux_pt_regset, fs), + .is0 = true, + }, + { + .reg = offsetof(struct linux_pt_regset, ds), + .is0 = true, + }, + { + .reg = offsetof(struct linux_pt_regset, es), + .is0 = true, + }, + { + .reg = offsetof(struct linux_pt_regset, cs), + .is0 = false, + }, + { + .reg = offsetof(struct linux_pt_regset, ss), + .is0 = false, + }, +}; + int linux_ptrace_pokeuser(struct thread *td, pid_t pid, void *addr, void *data) { + struct linux_pt_regset reg; + struct reg b_reg, b_reg1; + int error, i; - LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld " - "not implemented; returning EINVAL", (uintptr_t)addr); - return (EINVAL); + if ((uintptr_t)addr & (sizeof(data) -1) || (uintptr_t)addr < 0) + return (EIO); + if ((uintptr_t)addr >= sizeof(struct linux_pt_regset)) { + LINUX_RATELIMIT_MSG_OPT1("PTRACE_POKEUSER offset %ld " + "not implemented; returning EINVAL", (uintptr_t)addr); + return (EINVAL); + } + + if (LINUX_URO(addr, fs_base)) + return (kern_ptrace(td, PT_SETFSBASE, pid, data, 0)); + if (LINUX_URO(addr, gs_base)) + return (kern_ptrace(td, PT_SETGSBASE, pid, data, 0)); + for (i = 0; i < nitems(linux_segregs_off); i++) { + if ((uintptr_t)addr == linux_segregs_off[i].reg) { + if (linux_invalid_selector((uintptr_t)data)) + return (EIO); + if (!linux_segregs_off[i].is0 && (uintptr_t)data == 0) + return (EIO); + } + } + if ((error = kern_ptrace(td, PT_GETREGS, pid, &b_reg, 0)) != 0) + return (error); + bsd_to_linux_regset(&b_reg, ®); + *(®.r15 + ((uintptr_t)addr / sizeof(reg.r15))) = (uint64_t)data; + linux_to_bsd_regset(&b_reg1, ®); + b_reg1.r_err = b_reg.r_err; + b_reg1.r_trapno = b_reg.r_trapno; + return (kern_ptrace(td, PT_SETREGS, pid, &b_reg, 0)); } #undef LINUX_URO