From patchwork Tue Jan 14 20:24:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Celeste Liu X-Patchwork-Id: 13939536 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E18AAE77188 for ; Tue, 14 Jan 2025 21:32:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=MUi3yzpnvobWjCl5se0gopeljYn4/O/ETJ2vllaMyX0=; b=wxHJi4wWhH2QTs weuZjmq7AQMIUhVGtlNzK9W7qUARXR9Bw/5y6RM7Pw3VcCvk+NZJjGDqvrmq+j41HTZwysztm0VRu MyNBPBQ28KvZ2dLNMRElyirFcV8lrG4/kUEDrUntLqPT3WOJbyRFP2F9FLwEN1beATxYBCxIsqv0l tlOdvSg9KqeiRcNo4789+Hq2w1/t2JrKaySDY+IPdGrTfdKLUX+EXYqt4PzVbQqhRsm/LEcEHrrcS BjXaAHBhBFyeFf0SOPFRd8GvJfFoxEKRB1P+VnZqJMTQ90p++pbQ8SBOREhJwklnpgJiI0h36yXmm 02WBTPN+mHISxnZ+5K8Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tXoWS-00000009oio-1FPM; Tue, 14 Jan 2025 21:32:28 +0000 Received: from flow-b6-smtp.messagingengine.com ([202.12.124.141]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tXnTj-00000009f7z-3dSY for linux-riscv@lists.infradead.org; Tue, 14 Jan 2025 20:25:37 +0000 Received: from phl-compute-02.internal (phl-compute-02.phl.internal [10.202.2.42]) by mailflow.stl.internal (Postfix) with ESMTP id 6FEFC1D40BF4; Tue, 14 Jan 2025 15:25:32 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-02.internal (MEProxy); Tue, 14 Jan 2025 15:25:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= coelacanthus.name; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1736886332; x=1736893532; bh=X84OC6j3D/ EcHmFCrIItppTILcpGDU3BEqt5Odrm7tQ=; b=WgZDBvrE4myYXVdo8lmC9V7go1 MBNUZAT+PvPqb5oGn6x9Kl2qG1YUJMqvH5XJ+XPxutA33rnDI7pZZZtx57DmYIib YuT54hy8qxi3+XWsBqM7U0e1GLKfGaZleF7Ejy5/5FSWckl4EvWvr8A7AT58wlvG tTWWAKVrLJCC5avhWp0dLXQ6ISFZcvPIaK4g1SRw0RlmPygLLxc2p2zZZ4cT1VDL fP5mjcKT04he4UlDi7KwhjSwqyj0Jkyt9xYb8v7waNYmBNs8rBzw0co39X5iAocG Gom4h+dGGkIgaicrLNfqShZH9+5/y3am1u/BnXWVG7moTaDfTmtrFdFe4hzw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1736886332; x= 1736893532; bh=X84OC6j3D/EcHmFCrIItppTILcpGDU3BEqt5Odrm7tQ=; b=H 5KT5zYVPKM7Nwr0zWxaG2QUwLi0MZsWF61JiqEvq8WKbOPfmItvqOF9o8ZZTb3CA h6VjYCzB9weaEq0/uZVUzwq7HdgWBbhEhs3b9y5y5WRR1hJKyKDndqtuupTHV7gw 6ept+8nqMi0uqejwWxkJsxNYoztaUgZo/1HdVILdsQo1zTa+38qukLR+vOPxbRJG yDLLPHayjZ6Q5Lh367RG/+sUEG5GxW/YIuWfi1oyCz2SiDgeTO3HmEaVpt1l8VpB HnulfY5HvNvtwGRpKfD33Tmdwl6de1+9j3i8jDlrHIeDpAXVEZEiy1j3wIa2L9Og PVe2hYZIVkt4C/jya4Qew== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrudehiedgudefhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhfffugggtgffkfhgjvfevofesthekredtredt jeenucfhrhhomhepvegvlhgvshhtvgcunfhiuhcuoehufihusegtohgvlhgrtggrnhhthh hushdrnhgrmhgvqeenucggtffrrghtthgvrhhnpeeiudegfffhtdeuuddufefhfefhheej jefhueekhefffeeugfeuveetieeuvddtfeenucevlhhushhtvghrufhiiigvpedtnecurf grrhgrmhepmhgrihhlfhhrohhmpehufihusegtohgvlhgrtggrnhhthhhushdrnhgrmhgv pdhnsggprhgtphhtthhopedviedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheplh hinhhugidqkhgvrhhnvghlsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohep phgrlhhmvghrsegurggssggvlhhtrdgtohhmpdhrtghpthhtohepfhgvlhhigihonhhmrg hrshesrghrtghhlhhinhhugidrohhrghdprhgtphhtthhopehlughvsehsthhrrggtvgdr ihhopdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidruggvpdhrtghpthhtoh epuhifuhestghovghlrggtrghnthhhuhhsrdhnrghmvgdprhgtphhtthhopehshhhurghh sehkvghrnhgvlhdrohhrghdprhgtphhtthhopehkvggvsheskhgvrhhnvghlrdhorhhgpd hrtghpthhtohepiihhohhuqhhurghnsehishgtrghsrdgrtgdrtghn X-ME-Proxy: Feedback-ID: i95c648bc:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 14 Jan 2025 15:25:29 -0500 (EST) From: Celeste Liu Date: Wed, 15 Jan 2025 04:24:59 +0800 Subject: [PATCH v5 2/2] riscv: selftests: Add a ptrace test to verify a0 and orig_a0 access MIME-Version: 1.0 Message-Id: <20250115-riscv-new-regset-v5-2-d0e6ec031a23@coelacanthus.name> References: <20250115-riscv-new-regset-v5-0-d0e6ec031a23@coelacanthus.name> In-Reply-To: <20250115-riscv-new-regset-v5-0-d0e6ec031a23@coelacanthus.name> To: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou Cc: Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , Andrew Jones , Quan Zhou , Felix Yan , Ruizhe Pan , Guo Ren , Yao Zi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Celeste Liu , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=7877; i=uwu@coelacanthus.name; h=from:subject:message-id; bh=JHyWpvTLVV/62CjvMtPU1ES86qpvTbLYXerWGMWdLcU=; b=kA0DAAoWT2Qv82QkrXgByyZiAGeGyCiiQ/UipLwCo1ZxlhWCvh+tEMPj8P8eGMLet11Mo7+mP Yh1BAAWCgAdFiEEbnQE+QnSzYu9iLeKT2Qv82QkrXgFAmeGyCgACgkQT2Qv82QkrXgtRAD8DQ3t m4IYDX1buEhi22FOFeG2lTxpLFGOsmFktIorlmEA/ioVOvQeEyBhz+AI3kUPJbx96RWmT6t9/op IjMuhOLIC X-Developer-Key: i=uwu@coelacanthus.name; a=openpgp; fpr=892EBC7DC392DFF9C9C03F1D15F4180E73787863 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250114_122536_418665_4570E767 X-CRM114-Status: GOOD ( 19.51 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org This test checks that orig_a0 and a0 can be modified and accessed independently. Co-developed-by: Quan Zhou Signed-off-by: Quan Zhou Co-developed-by: Charlie Jenkins Signed-off-by: Charlie Jenkins Reviewed-by: Björn Töpel Signed-off-by: Celeste Liu --- tools/testing/selftests/riscv/abi/.gitignore | 1 + tools/testing/selftests/riscv/abi/Makefile | 6 +- tools/testing/selftests/riscv/abi/ptrace.c | 201 +++++++++++++++++++++++++++ 3 files changed, 207 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/riscv/abi/.gitignore b/tools/testing/selftests/riscv/abi/.gitignore index b38358f91c4d2240ae64892871d9ca98bda1ae58..378c605919a3b3d58eec2701eb7af430cfe315d6 100644 --- a/tools/testing/selftests/riscv/abi/.gitignore +++ b/tools/testing/selftests/riscv/abi/.gitignore @@ -1 +1,2 @@ pointer_masking +ptrace diff --git a/tools/testing/selftests/riscv/abi/Makefile b/tools/testing/selftests/riscv/abi/Makefile index ed82ff9c664e7eb3f760cbab81fb957ff72579c5..359a082c88a401883fb3776b35e4dacf69beaaaa 100644 --- a/tools/testing/selftests/riscv/abi/Makefile +++ b/tools/testing/selftests/riscv/abi/Makefile @@ -1,10 +1,14 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS += -I$(top_srcdir)/tools/include +CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := pointer_masking +TEST_GEN_PROGS := pointer_masking ptrace include ../../lib.mk $(OUTPUT)/pointer_masking: pointer_masking.c $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ + +$(OUTPUT)/ptrace: ptrace.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/abi/ptrace.c b/tools/testing/selftests/riscv/abi/ptrace.c new file mode 100644 index 0000000000000000000000000000000000000000..f1a0458adccdd040bfaa350e2e8d98b1ef34c0ad --- /dev/null +++ b/tools/testing/selftests/riscv/abi/ptrace.c @@ -0,0 +1,201 @@ +// SPDX-License-Identifier: GPL-2.0-only +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest_harness.h" + +#ifndef sizeof_field +#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) +#endif +#ifndef offsetofend +#define offsetofend(TYPE, MEMBER) \ + (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) +#endif + + +#define ORIG_A0_MODIFY 0x01 +#define A0_MODIFY 0x02 +#define A0_OLD 0xbadbeefbeeff +#define A0_NEW 0xffeebfeebdab + + +struct a0_regs { + __s64 orig_a0; + __u64 a0; +}; + +#define perr_and_exit(fmt, ...) \ + ({ \ + char buf[256]; \ + snprintf(buf, sizeof(buf), "%s:%d:" fmt ": %m\n", \ + __func__, __LINE__, ##__VA_ARGS__); \ + ksft_exit_fail_perror(buf); \ + }) + +static void ptrace_test(int opt, struct a0_regs result[]) +{ + int status; + long rc; + pid_t pid; + struct user_regs_struct regs; + struct iovec iov = { + .iov_base = ®s, + .iov_len = sizeof(regs), + }; + + unsigned long orig_a0; + struct iovec a0_iov = { + .iov_base = &orig_a0, + .iov_len = sizeof(orig_a0), + }; + struct ptrace_syscall_info syscall_info = { + .op = 0xff, + }; + const unsigned int expected_sci_entry_size = + offsetofend(struct ptrace_syscall_info, entry.args); + const unsigned int expected_sci_exit_size = + offsetofend(struct ptrace_syscall_info, exit.is_error); + + pid = fork(); + if (pid == 0) { + /* Mark oneself being traced */ + long val = ptrace(PTRACE_TRACEME, 0, 0, 0); + + if (val < 0) + perr_and_exit("failed to request for tracer to trace me: %ld", val); + + kill(getpid(), SIGSTOP); + + /* Perform chdir syscall that will be intercepted */ + syscall(__NR_chdir, A0_OLD); + + exit(0); + } + + if (pid < 0) + ksft_exit_fail_perror("failed to fork"); + + for (int i = 0; i < 3; i++) { + if (waitpid(pid, &status, 0) != pid) + perr_and_exit("failed to wait for the tracee %d", pid); + if (WIFSTOPPED(status)) { + switch (WSTOPSIG(status)) { + case SIGSTOP: + if (ptrace(PTRACE_SETOPTIONS, pid, 0, PTRACE_O_TRACESYSGOOD) < 0) + perr_and_exit("failed to set PTRACE_O_TRACESYSGOOD"); + break; + case SIGTRAP|0x80: + /* Modify twice so GET_SYSCALL_INFO get modified a0 and orig_a0 */ + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to get tracee registers"); + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov)) + perr_and_exit("failed to get tracee registers"); + + /* Modify a0/orig_a0 for the syscall */ + switch (opt) { + case A0_MODIFY: + regs.a0 = A0_NEW; + break; + case ORIG_A0_MODIFY: + orig_a0 = A0_NEW; + break; + } + + if (ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to set tracee registers"); + if (ptrace(PTRACE_SETREGSET, pid, NT_RISCV_ORIG_A0, &a0_iov)) + perr_and_exit("failed to set tracee registers"); + switch (i) { + case 1: + /* Stop at the beginning of syscall */ + rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, + sizeof(syscall_info), &syscall_info); + if (rc < 0) + perr_and_exit("failed to get syscall info of entry"); + if (rc < expected_sci_entry_size + || syscall_info.op != PTRACE_SYSCALL_INFO_ENTRY) + perr_and_exit("stop position of entry mismatched"); + result[0].orig_a0 = syscall_info.entry.args[0]; + break; + + case 2: + /* Stop at the end of syscall */ + rc = ptrace(PTRACE_GET_SYSCALL_INFO, pid, + sizeof(syscall_info), &syscall_info); + if (rc < 0) + perr_and_exit("failed to get syscall info of entry"); + if (rc < expected_sci_exit_size + || syscall_info.op != PTRACE_SYSCALL_INFO_EXIT) + perr_and_exit("stop position of exit mismatched"); + result[0].a0 = syscall_info.exit.rval; + + if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov)) + perr_and_exit("failed to get tracee registers"); + result[1].a0 = regs.a0; + if (ptrace(PTRACE_GETREGSET, pid, NT_RISCV_ORIG_A0, + &a0_iov)) + perr_and_exit("failed to get tracee registers"); + result[1].orig_a0 = orig_a0; + } + } + if (ptrace(PTRACE_SYSCALL, pid, 0, 0) < 0) + perr_and_exit("failed to resume tracee"); + } + } + + /* Resume the tracee */ + ptrace(PTRACE_CONT, pid, 0, 0); + if (waitpid(pid, &status, 0) != pid) + perr_and_exit("failed to wait for the tracee"); + +} + +TEST(ptrace_access_a0) +{ + struct a0_regs result[2]; + + ptrace_test(A0_MODIFY, result); + + /* Verify PTRACE_SETREGSET */ + /* The modification of a0 cannot affect the first argument of the syscall */ + EXPECT_EQ(A0_OLD, result[0].orig_a0); + EXPECT_EQ(A0_NEW, result[0].a0); + + /* Verify PTRACE_GETREGSET */ + EXPECT_EQ(result[1].orig_a0, result[0].orig_a0); + EXPECT_EQ(result[1].a0, result[0].a0); +} + +TEST(ptrace_access_orig_a0) +{ + struct a0_regs result[2]; + + ptrace_test(ORIG_A0_MODIFY, result); + + /* Verify PTRACE_SETREGSET */ + /* Only modify orig_a0 to change the first argument of the syscall */ + EXPECT_EQ(A0_NEW, result[0].orig_a0); + /* a0 will keep default value, orig_a0 or -ENOSYS, depends on internal. */ + EXPECT_NE(A0_NEW, result[0].a0); + + /* Verify PTRACE_GETREGSET */ + EXPECT_EQ(result[1].orig_a0, result[0].orig_a0); + EXPECT_EQ(result[1].a0, result[0].a0); +} + +TEST_HARNESS_MAIN