From nobody Thu May 18 17:02:54 2023 X-Original-To: dev-commits-src-main@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 4QMbrZ5gp5z4Bn65; Thu, 18 May 2023 17:02:54 +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 4QMbrZ2sVZz4VQG; Thu, 18 May 2023 17:02:54 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684429374; 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=RNzAdCmahVV3RVOlovjGH2jHT562OBARUDPy9/tAF6g=; b=FN2r6MbVFM7k7r5HkecksgrIRWXRZzx16kojH/jyIWSZp+kh6tS8pifSln8ouXGZAL9HBn 6sZgzueTb4hB+PQzUENsqj3TezS04wy3svyAzZ+T6S0d5oFnJ2hdOiH/XFBhHDXlq4ISxu kaVPV2nbunDE9ncATYCQyHctMMwohk8u3oW059mFSZVwZSSMX0cy5dWhOh4bzgSxVlke1n lcwd/mEOeTE+YIqjaKC+TdnNHeMUVL/2Di+gMmSW/7LyEtbBVzCXCmysaFNF1B/LlYHwom NdxWJw5Md0oOkuRBhDcfhahQkpozxsvI0j8QwMg+oWs5auTVlAlsot6jEBU/Kw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1684429374; 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=RNzAdCmahVV3RVOlovjGH2jHT562OBARUDPy9/tAF6g=; b=jKRSrMWggZh56RvfbFNgJcU4xNyfwcB5ASXZs7X/FgjmERFKlezlhdHi+2o9rYHxeJbK9F QR27CE8lyG6HItjm2wPd/O617UPYTBVAU9x9B/xpbsy1h15sQ1Aa4tFagHnM13+NHtct7N rCI7jDN7HRFpc8f2o1HwYV2TlMq671BJLvjyvDU+GuYecSNDiYIcMxXFEGTKLOQwT2Efsb iPPnyH8lAyFeozT5P2BqvXYhTEKJ0qgur4S5b2KHW1fnJm72u9/+YJUwHew2NHyWCpiUAv Ea8uTtNmz/1w8ukFUnNlcLlEqZO7T1525n56kBCkna+degn0gj7JNBLnN0U2qQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1684429374; a=rsa-sha256; cv=none; b=dI0+CPMGsLotUZ54YvPtJfPK0G8KP9ITxcB3N5oZrVaZ7Fld4QsW27euxhEF7rvXnWy/7E orfei0XCTq/XvmXDB6oI2nOMsx5byOeTbQ+kT5WFBmPbuNBkH1n/hJDaG5BQ5tvxzEnOC8 XvnzVltYRe55Do7+5jIS54CQpeYwDB9tNfmVeGUKnRZ+Dr61SxcfFuDReE5LjlSqIev/kK QpXaL7WuxyCg/P00GPlhNoJ+KRrIP+ZfewggnfYa8X47LcvU4Z/ceHFcSDxVCSKDZsjyLO f8NVYngFNoyfOEuugkXe6ontTnmfExQBrlKLvbpky6FgNM87D5r6X6xnnA5MGA== 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 4QMbrZ1fXcztgf; Thu, 18 May 2023 17:02:54 +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 34IH2siH018687; Thu, 18 May 2023 17:02:54 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 34IH2sLZ018686; Thu, 18 May 2023 17:02:54 GMT (envelope-from git) Date: Thu, 18 May 2023 17:02:54 GMT Message-Id: <202305181702.34IH2sLZ018686@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dmitry Chagin Subject: git: 1d76741520c0 - main - linux(4): Implement ptrace_pokeusr for x86_64 List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 1d76741520c031730319ed976a6c394213991504 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by dchagin: URL: https://cgit.FreeBSD.org/src/commit/?id=1d76741520c031730319ed976a6c394213991504 commit 1d76741520c031730319ed976a6c394213991504 Author: Dmitry Chagin AuthorDate: 2023-05-18 17:02:35 +0000 Commit: Dmitry Chagin CommitDate: 2023-05-18 17:02:35 +0000 linux(4): Implement ptrace_pokeusr for x86_64 Differential Revision: https://reviews.freebsd.org/D40097 MFC after: 1 week --- 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