From mboxrd@z Thu Jan 1 00:00:00 1970 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 22E18C02185 for ; Wed, 15 Jan 2025 11:37:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ABA60280007; Wed, 15 Jan 2025 06:37:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A42AA280002; Wed, 15 Jan 2025 06:37:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 895CD280007; Wed, 15 Jan 2025 06:37:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 689BE280002 for ; Wed, 15 Jan 2025 06:37:23 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2FD12A1213 for ; Wed, 15 Jan 2025 11:37:23 +0000 (UTC) X-FDA: 83009485566.12.F69D107 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf21.hostedemail.com (Postfix) with ESMTP id 286F71C0019 for ; Wed, 15 Jan 2025 11:37:20 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=ventanamicro.com header.s=google header.b=cw1RKs1Z; spf=pass (imf21.hostedemail.com: domain of ajones@ventanamicro.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=ajones@ventanamicro.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736941041; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bR+WWYwGVwj9iq4wp1piIxfs/pyaIAjlJdJ0bpNK8EE=; b=46WDiRMyU5StLpav9LdUgLPiA7jxE+0QWpvhVuwE8tPbz9J6/2XeDPIdqRgTxIZ5FaS4sT fuQzJt9kyqwOA+mAV8ZUW0tglGWS8wVJ02HjCvh3IyPX4WR8E9WoOXZBnuaDQQErvfqrPQ FaWPCGFn19ixOtA+2303DGqVrpcK7ow= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736941041; a=rsa-sha256; cv=none; b=5CZ6U58rodimwC9S6jHbRno4ZGrUNjAwFt/1HZsRaBNr2j8RJel0hNWCJHdFYlCIP9hezz xvN8CWTrn+ZujkmAyphlv3ZTElKuErj2IHR+yKITi+dW0KC4S6veye0op8M+JdMDtRDTRX nvUPY1SrYxtSWjnmvYbhsXXkGJ4AK78= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=ventanamicro.com header.s=google header.b=cw1RKs1Z; spf=pass (imf21.hostedemail.com: domain of ajones@ventanamicro.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=ajones@ventanamicro.com; dmarc=none Received: by mail-ej1-f47.google.com with SMTP id a640c23a62f3a-aaeec07b705so1048040066b.2 for ; Wed, 15 Jan 2025 03:37:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1736941040; x=1737545840; darn=kvack.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=bR+WWYwGVwj9iq4wp1piIxfs/pyaIAjlJdJ0bpNK8EE=; b=cw1RKs1ZJRuLKQfhKlR6BzWLo32wL1oxxDqTsNOe4USk/bAs/avfjKk4HKtVxX8qZ0 2JefJmo9ZMLzYGw0Mt4PBlPnQ0guKhU2C7WAio2pGp2Z4r6UvZteX1gEUeZVHhCGmy4w n/aHr4XMIWT7uw+MjfolN0EcgtVAWNyFSk1heECVUhNfJIwhVs0BMqTn5rPu4NUtfW2/ nnrPJk1S4jsUCBQYqwtUNvtbG8voBV01Bt1njB0ICGrBVLwPigKRAD8c/zWGtDOQWT3A tgT/BJKu3Gdr8VtujP+k99YInM6lmRAG4+Q/J9BfpmF8oIQP6hWUUDIur5Jovycshh/G g3Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736941040; x=1737545840; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bR+WWYwGVwj9iq4wp1piIxfs/pyaIAjlJdJ0bpNK8EE=; b=KXAsXQ6iYwSxTvgRtwdDGmdrpiuXa9NhYxAGpEZsRg6Oy5Lcua4plRWEcKsWTeYJqw I7sSzBOpJYflcPUeKYoxJxhf2jPcGhnqcKXRBYqxLexd7Bb+Kq0FuhvVkNvD8s+0kBUu 72Bm5AqOalWX1alxbvkzZu9cPO2KR2zWPu68qOdCLX2E1L5ZxVcDqbKt72J5gbjMZKhy qWhla5dkE3oialHIuK74sxeQjN4+xjhTPk/zYzs7nLBDDVtqNfr7g/b476R33Y4PqTz0 i3DiQYmQtmYwxds7pBZGj7ZN+PeQI0PXFHFCQhdXMo4+yf22PyRtIUGSrhy5Zkl1sEBX nEOg== X-Forwarded-Encrypted: i=1; AJvYcCVjBrCYfGt3KW96aS6D9XR+vWcWyJfQQWYx1WB8cKoNF4x7m7XUdGSG0aWB3mw661rxUh/K/aXtmw==@kvack.org X-Gm-Message-State: AOJu0Yw5ZfY9m/+l6WWQjHdkRM7BLnsSEi0jnOwQUvVC3n3hPmKjbFKO sDfFafbgOOGFnXVn3jpivlTaYoA0BfybBrlN4BFGP3/vLY05YogGTjiu3Ax7Tw0= X-Gm-Gg: ASbGnctpHrf4gqfL7CGIMgq8vAhTK4bH4Tal9oTn4bBqNAEbfNey4C80TbJa8X7jJGu J/tdogTnUqJ+L4e4DyUepTk7ZxJiEs1kNCh9HFVeKFPlN7iMimD/dq9FJEiTVQM+6ftfJTjZiLC BDrjeYj6OtiVB3XM8O3OT6ii+cnrpG1Ltcf5gbD7Q+ND6YpKMzYTMv04pvTucJeYwvIZUqMo+V1 jS+vmR6xJnGk2mP8CZfn2nou12C1cPzqECFoKFUIOl6z8y/jIw0YmOd+HbZRcUDApB/9WqLpX5M AiuDUR/3xsSTvRWNVoTNZvATLnP3W2Q+fisIvxrW/w== X-Google-Smtp-Source: AGHT+IEOkZVi43hTcd5c9zWbi3HxwwGowdbRYih1cWc4DFfPmKBxVNAcGDdIhoBiy/DfEt+4yE+XqA== X-Received: by 2002:a17:906:f58e:b0:aa5:225f:47d9 with SMTP id a640c23a62f3a-ab2ab749f27mr2166727066b.29.1736941039534; Wed, 15 Jan 2025 03:37:19 -0800 (PST) Received: from localhost (2001-1ae9-1c2-4c00-20f-c6b4-1e57-7965.ip6.tmcz.cz. [2001:1ae9:1c2:4c00:20f:c6b4:1e57:7965]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ab2c95af65dsm746359666b.140.2025.01.15.03.37.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jan 2025 03:37:19 -0800 (PST) Date: Wed, 15 Jan 2025 12:37:18 +0100 From: Andrew Jones To: Celeste Liu Cc: Oleg Nesterov , Paul Walmsley , Palmer Dabbelt , Eric Biederman , Kees Cook , Shuah Khan , Albert Ou , Alexandre Ghiti , "Dmitry V. Levin" , Andrea Bolognani , =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= , Thomas Gleixner , Ron Economos , Charlie Jenkins , 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, =?utf-8?B?QmrDtnJuIFTDtnBlbA==?= Subject: Re: [PATCH v6 3/3] riscv: selftests: Add a ptrace test to verify a0 and orig_a0 access Message-ID: <20250115-13cc73c36c7bb3b9f046f614@orel> References: <20250115-riscv-new-regset-v6-0-59bfddd33525@coelacanthus.name> <20250115-riscv-new-regset-v6-3-59bfddd33525@coelacanthus.name> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20250115-riscv-new-regset-v6-3-59bfddd33525@coelacanthus.name> X-Rspamd-Queue-Id: 286F71C0019 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: czhef3fsexnbn4m6dtkmxf4qzsewd9zo X-HE-Tag: 1736941040-234364 X-HE-Meta: U2FsdGVkX1/8JVDLnnW1uYftYxD06TN66yqdxmmQfUPDdtff3ojg7MtC9Cv/nq2MU3kIJefFnmax4ROPIYExY0NaEwKlYmFhSPm45GKHHKymjQXZM55qnJhHoUfynTPudIoxnjgigrd83kubcb2CjoBz6swjmZRWvsoVV/BcVCWnRsYP37SvSfyAlfRhSbCWnHLNamuOMa/DYJbv1gYZYLOyJm3Oll0IULOeHi8292K1eFLWja6n25tr/adbzs2B58xRfWiK8pwCtUxykkTCdNZR7/nzPVM1FE5fEcj6okFE0ym//WObYgx927rqSHPlxlVLkzwLSlyBEBrkivn4ut+fYxoSd77A60ph1iqOYuRJcoUT3HC3ecbHSevWKnHQ20YcFKISQ/VZ60lq6PBLqwx5r0Yifvj1zD2dTgniqjQyf5pahEaUezFjemGY5QLOhNq9PJ5BYSgxuGkwPBcXZiGqSmnS48tAgALhvwOscnazXFhiRz8TTsTq9b0CRVCEB5tp/AqD1DE3/6LMYm0fL1SzXmVrVsc4B0cMq3dVAmSFivEALJhljUnzeHOeI0usPwwPNdHSU6pcY01zgZ9hET58v7dCnf5KUJEeUmfabhryzd+BudelsR6jY04CubpcAu+FXrWSLsNJZj9b8aVnemFHR2V7SQRLbL3dr503XDLaKgbEdi0zzn8NWvuROhV724jgBVw0LQx3popmOHu4wBjfIoAkOh594DSMEW93f1nlwmS7iVIlghTeS1kvxNuPkl3CT9IKhBWaU4NXJjcHZhfuaD84nvZnLk6SdgRD7a2+32wU23GJEhy6i3MZbIgnRqiqJDqv1EHgjK31YbpX+yPxprkx9K79r6yJWiMdiCaXtB3gDSgvr2BW9wbGv3GKLBlWKcnR1PbPotYWP9hFYI5zLFISaDY0S6TIXTYaHAS0VGKndrHCG4sOicto8H6nh4EYEjBnk/sdVY0bL9t 7EKEkZhS 49767g9qSrGHTWD/qJsE/OJBubFv9WoVB1Atim10aNa7tMwgizengmMHpmcBnJCA1Z7S/M5aNlwAnHEyyRkv1S4cDk/xsUkKj8x8pmPRFghNKtBOSH/bAMSIeJYXBSeyyZXLuzXfeha2FBX+EQnh6Mr6g4hi+kRadz3KbIG+ZGdSl3t2d74vHX5a/rmlN+D2LACGESxqxLvfvUTDXIGZPjkVjIlTNKpRbgq0PZP/u+5Pi+u507ZcTtJo7NogTkA/74qeltmIyz+NM80tuqJq/UcrLZ0y91bJh4Cj+HmqkXf75EZ41JDSy4JeyfsYupmjHT4IiLMLxAlgYanb1sXT7+nDxhIhE+tFA/JNJkGHRZJIO57RRuTNca8oxX9hDwb5WeTCJrm0Aml1XPn84bSqE2Sl/eL93COQDp1U65DhMbpjB4DPDsFBhbCGZMeDB0D7lvRmnGRYCamBlMAxSaN7PTy8NAjmdhH0HlbFT5RoKJiV/8oomUv3RDctBQ7b4AWzn8QbSAW4vP3IfbgY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000021, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Jan 15, 2025 at 07:13:29PM +0800, Celeste Liu wrote: > 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 | 193 +++++++++++++++++++++++++++ > 3 files changed, 199 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..cbd60462021165ca2bcc38be24b8423c96047b93 > --- /dev/null > +++ b/tools/testing/selftests/riscv/abi/ptrace.c > @@ -0,0 +1,193 @@ > +// 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 > + > +#include "../../kselftest_harness.h" > + > +#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 not be affected */ > + 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 > > -- > 2.48.0 > Reviewed-by: Andrew Jones