From nobody Wed Dec 17 23:56:07 2025 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C363F180A83 for ; Tue, 25 Jun 2024 21:09:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719349790; cv=none; b=qJM+W4K7S38zKtGnVxygg/dv7VXh7XVWn0rI0SDVGxZwoEoU7tdsIjyp2+LrKFFYrZDC/SM/uwEdPyTbnPfPtF4RR8CAd0I2trrmHbobzs62gfksyWdAF7lgUGEeveSgGmrLmCnLq0fOgyy1jd1CrEi+URW7KTHQljKUEMDeiOg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1719349790; c=relaxed/simple; bh=B838lKFRM36y+A9Ai6vY9X0NS4CpgexhhltQlHwHHCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aobkbkHp8KTmqNYxFpGBXmEIsYGVAF9Au93b3HOML9LGivXUVlGv1XeXLePLwUoKwCAITZBP85WNsm8SRPfvrTMnPAsMfySOG+QScJbak97SKlEZgz/tNqglMTm9jWdv134oyeuRo378/9hpWVAzyehu03at9bbPvdxAjhv04nE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=Fhe/yW8o; arc=none smtp.client-ip=209.85.214.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="Fhe/yW8o" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-1fa244db0b2so24672615ad.3 for ; Tue, 25 Jun 2024 14:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1719349788; x=1719954588; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TPuofY2kiE0LkR878/Zu2IFTHH09jSBTsb0Rtm6Nb1g=; b=Fhe/yW8o0p2k5fJYmE2EyvOVAy+dTxfv4YHnTwiphZeKcY/rUcKS6GjjaHKIDpBXuG bis5E/zF0pY3IiWJE2iRBQ7CGZC7Prg0ckxiMOoh8B1AAMZzkrWfR9YwZeOM+kHdwcFA 4XPfugkjXSxO3BmJYaAwmGdFMSeSTTieIjEyaJOClbXRG7u+lU/qWwGXa2QFrAjIs353 TRW1Szhs27dr3wmjdajooMc8LId/LGLTRXoXe/dVxepNcn2rS4HHds4ruXp/mqO1WJmN AGQd+frJH9YbPLf0GZc/2zN0Mi4eDrNeY/EhDHvk2NWPDmpTnsbAwH+GuKox1NmypGwx VfJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719349788; x=1719954588; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TPuofY2kiE0LkR878/Zu2IFTHH09jSBTsb0Rtm6Nb1g=; b=SBSHVceHkcKp7DlHfo9NkPfXU15KGC4tp9+6cU83c/2J8WEccoXeEz5wgwwMZi3XmX hTuK32nd8NDb/6IZsBYA2gXUi2YzktkQbLD1sGQWBMxdhUe/JEGmvhKGWVj1wh+ZrQ5E MRsHKkuqCgmE2XyDCFuJzgr9rj2H2/+yJDINjzZMgjP5KUca3FniYzftrgRlK8xb0KQr Apdw2jRpPT+BtdDonklZycyxBMSE5ZtzGyasekMU7hl3JkgV7jte2CqZ/lyHerJM++fD V1A9qOhcNio4BC4SUUcA4RCqArZqdZJvLGd97VUnbiDAwcO+6tnMtm5O4teXJ1fxzYmB /5vg== X-Forwarded-Encrypted: i=1; AJvYcCXK/fC3obZI2TRjGkIEfyrOpXtjuMp6z9nTxUcifVHJHbIPuvWErtAxhVi8aOMPPWbqTuKF/N1/8FX++i1XK+uh0uVMWegh2liMzLXk X-Gm-Message-State: AOJu0Yxalvz1t+e2NxF7epuA/rux5FYQuV2G0w/IjF721DOH+53N+rEx ctQk+9xDRQDTVNbYRlrgkq2otFrRBppUUiHr6ELR/IYrXvkuve+SmhtsSMj3G06Gyw4sI8oMGx1 0 X-Google-Smtp-Source: AGHT+IHBzWveHbkEb4O4bsOdc9hCbynxG7YU0zM8VzUNtP3uAB5HdQx6amHwmHSg9LkKRKIz3BejeA== X-Received: by 2002:a17:902:c950:b0:1f9:e7b4:5df6 with SMTP id d9443c01a7336-1fa158d0d2emr100281835ad.3.1719349788079; Tue, 25 Jun 2024 14:09:48 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f9eb328f57sm85873455ad.110.2024.06.25.14.09.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jun 2024 14:09:47 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt , linux-riscv@lists.infradead.org Cc: devicetree@vger.kernel.org, Catalin Marinas , linux-kernel@vger.kernel.org, Anup Patel , Conor Dooley , kasan-dev@googlegroups.com, Atish Patra , Evgenii Stepanov , Krzysztof Kozlowski , Rob Herring , "Kirill A . Shutemov" , Samuel Holland Subject: [PATCH v2 07/10] selftests: riscv: Add a pointer masking test Date: Tue, 25 Jun 2024 14:09:18 -0700 Message-ID: <20240625210933.1620802-8-samuel.holland@sifive.com> X-Mailer: git-send-email 2.44.1 In-Reply-To: <20240625210933.1620802-1-samuel.holland@sifive.com> References: <20240625210933.1620802-1-samuel.holland@sifive.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This test covers the behavior of the PR_SET_TAGGED_ADDR_CTRL and PR_GET_TAGGED_ADDR_CTRL prctl() operations, their effects on the userspace ABI, and their effects on the system call ABI. Signed-off-by: Samuel Holland --- Changes in v2: - Rename "tags" directory to "pm" to avoid .gitignore rules - Add .gitignore file to ignore the compiled selftest binary - Write to a pipe to force dereferencing the user pointer - Handle SIGSEGV in the child process to reduce dmesg noise tools/testing/selftests/riscv/Makefile | 2 +- tools/testing/selftests/riscv/pm/.gitignore | 1 + tools/testing/selftests/riscv/pm/Makefile | 10 + .../selftests/riscv/pm/pointer_masking.c | 330 ++++++++++++++++++ 4 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/riscv/pm/.gitignore create mode 100644 tools/testing/selftests/riscv/pm/Makefile create mode 100644 tools/testing/selftests/riscv/pm/pointer_masking.c diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftes= ts/riscv/Makefile index 7ce03d832b64..2ee1d1548c5f 100644 --- a/tools/testing/selftests/riscv/Makefile +++ b/tools/testing/selftests/riscv/Makefile @@ -5,7 +5,7 @@ ARCH ?=3D $(shell uname -m 2>/dev/null || echo not) =20 ifneq (,$(filter $(ARCH),riscv)) -RISCV_SUBTARGETS ?=3D hwprobe vector mm sigreturn +RISCV_SUBTARGETS ?=3D hwprobe mm pm sigreturn vector else RISCV_SUBTARGETS :=3D endif diff --git a/tools/testing/selftests/riscv/pm/.gitignore b/tools/testing/se= lftests/riscv/pm/.gitignore new file mode 100644 index 000000000000..b38358f91c4d --- /dev/null +++ b/tools/testing/selftests/riscv/pm/.gitignore @@ -0,0 +1 @@ +pointer_masking diff --git a/tools/testing/selftests/riscv/pm/Makefile b/tools/testing/self= tests/riscv/pm/Makefile new file mode 100644 index 000000000000..ed82ff9c664e --- /dev/null +++ b/tools/testing/selftests/riscv/pm/Makefile @@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0 + +CFLAGS +=3D -I$(top_srcdir)/tools/include + +TEST_GEN_PROGS :=3D pointer_masking + +include ../../lib.mk + +$(OUTPUT)/pointer_masking: pointer_masking.c + $(CC) -static -o$@ $(CFLAGS) $(LDFLAGS) $^ diff --git a/tools/testing/selftests/riscv/pm/pointer_masking.c b/tools/tes= ting/selftests/riscv/pm/pointer_masking.c new file mode 100644 index 000000000000..0fe80f963ace --- /dev/null +++ b/tools/testing/selftests/riscv/pm/pointer_masking.c @@ -0,0 +1,330 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../kselftest.h" + +#ifndef PR_PMLEN_SHIFT +#define PR_PMLEN_SHIFT 24 +#endif +#ifndef PR_PMLEN_MASK +#define PR_PMLEN_MASK (0x7fUL << PR_PMLEN_SHIFT) +#endif + +static int dev_zero; + +static int pipefd[2]; + +static sigjmp_buf jmpbuf; + +static void sigsegv_handler(int sig) +{ + siglongjmp(jmpbuf, 1); +} + +static int min_pmlen; +static int max_pmlen; + +static inline bool valid_pmlen(int pmlen) +{ + return pmlen =3D=3D 0 || pmlen =3D=3D 7 || pmlen =3D=3D 16; +} + +static void test_pmlen(void) +{ + ksft_print_msg("Testing available PMLEN values\n"); + + for (int request =3D 0; request <=3D 16; request++) { + int pmlen, ret; + + ret =3D prctl(PR_SET_TAGGED_ADDR_CTRL, request << PR_PMLEN_SHIFT, 0, 0, = 0); + if (ret) + goto pr_set_error; + + ret =3D prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); + ksft_test_result(ret >=3D 0, "PMLEN=3D%d PR_GET_TAGGED_ADDR_CTRL\n", req= uest); + if (ret < 0) + goto pr_get_error; + + pmlen =3D (ret & PR_PMLEN_MASK) >> PR_PMLEN_SHIFT; + ksft_test_result(pmlen >=3D request, "PMLEN=3D%d constraint\n", request); + ksft_test_result(valid_pmlen(pmlen), "PMLEN=3D%d validity\n", request); + + if (min_pmlen =3D=3D 0) + min_pmlen =3D pmlen; + if (max_pmlen < pmlen) + max_pmlen =3D pmlen; + + continue; + +pr_set_error: + ksft_test_result_skip("PMLEN=3D%d PR_GET_TAGGED_ADDR_CTRL\n", request); +pr_get_error: + ksft_test_result_skip("PMLEN=3D%d constraint\n", request); + ksft_test_result_skip("PMLEN=3D%d validity\n", request); + } + + if (max_pmlen =3D=3D 0) + ksft_exit_fail_msg("Failed to enable pointer masking\n"); +} + +static int set_tagged_addr_ctrl(int pmlen, bool tagged_addr_abi) +{ + int arg, ret; + + arg =3D pmlen << PR_PMLEN_SHIFT | tagged_addr_abi; + ret =3D prctl(PR_SET_TAGGED_ADDR_CTRL, arg, 0, 0, 0); + if (!ret) { + ret =3D prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); + if (ret =3D=3D arg) + return 0; + } + + return ret < 0 ? -errno : -ENODATA; +} + +static void test_dereference_pmlen(int pmlen) +{ + static volatile int i; + volatile int *p; + int ret; + + ret =3D set_tagged_addr_ctrl(pmlen, false); + if (ret) + return ksft_test_result_error("PMLEN=3D%d setup (%d)\n", pmlen, ret); + + i =3D pmlen; + + if (pmlen) { + p =3D (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen); + + /* These dereferences should succeed. */ + if (sigsetjmp(jmpbuf, 1)) + return ksft_test_result_fail("PMLEN=3D%d valid tag\n", pmlen); + if (*p !=3D pmlen) + return ksft_test_result_fail("PMLEN=3D%d bad value\n", pmlen); + *p++; + } + + p =3D (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen - 1); + + /* These dereferences should raise SIGSEGV. */ + if (sigsetjmp(jmpbuf, 1)) + return ksft_test_result_pass("PMLEN=3D%d dereference\n", pmlen); + *p++; + ksft_test_result_fail("PMLEN=3D%d invalid tag\n", pmlen); +} + +static void test_dereference(void) +{ + ksft_print_msg("Testing userspace pointer dereference\n"); + + signal(SIGSEGV, sigsegv_handler); + + test_dereference_pmlen(0); + test_dereference_pmlen(min_pmlen); + test_dereference_pmlen(max_pmlen); + + signal(SIGSEGV, SIG_DFL); +} + +static void execve_child_sigsegv_handler(int sig) +{ + exit(42); +} + +static int execve_child(void) +{ + static volatile int i; + volatile int *p =3D (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen = - 7); + + signal(SIGSEGV, execve_child_sigsegv_handler); + + /* This dereference should raise SIGSEGV. */ + return *p; +} + +static void test_fork_exec(void) +{ + int ret, status; + + ksft_print_msg("Testing fork/exec behavior\n"); + + ret =3D set_tagged_addr_ctrl(min_pmlen, false); + if (ret) + return ksft_test_result_error("setup (%d)\n", ret); + + if (fork()) { + wait(&status); + ksft_test_result(WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 42, + "dereference after fork\n"); + } else { + static volatile int i =3D 42; + volatile int *p =3D (volatile int *)((uintptr_t)&i | 1UL << __riscv_xlen= - min_pmlen); + + /* This dereference should succeed. */ + exit(*p); + } + + if (fork()) { + wait(&status); + ksft_test_result(WIFEXITED(status) && WEXITSTATUS(status) =3D=3D 42, + "dereference after fork+exec\n"); + } else { + /* Will call execve_child(). */ + execve("/proc/self/exe", (char *const []) { "", NULL }, NULL); + } +} + +static void test_tagged_addr_abi_sysctl(void) +{ + char value; + int fd; + + ksft_print_msg("Testing tagged address ABI sysctl\n"); + + fd =3D open("/proc/sys/abi/tagged_addr_disabled", O_WRONLY); + if (fd < 0) { + ksft_test_result_skip("failed to open sysctl file\n"); + ksft_test_result_skip("failed to open sysctl file\n"); + return; + } + + value =3D '1'; + pwrite(fd, &value, 1, 0); + ksft_test_result(set_tagged_addr_ctrl(min_pmlen, true) =3D=3D -EINVAL, + "sysctl disabled\n"); + + value =3D '0'; + pwrite(fd, &value, 1, 0); + ksft_test_result(set_tagged_addr_ctrl(min_pmlen, true) =3D=3D 0, + "sysctl enabled\n"); + + set_tagged_addr_ctrl(0, false); + + close(fd); +} + +static void test_tagged_addr_abi_pmlen(int pmlen) +{ + int i, *p, ret; + + i =3D ~pmlen; + + if (pmlen) { + p =3D (int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen); + + ret =3D set_tagged_addr_ctrl(pmlen, false); + if (ret) + return ksft_test_result_error("PMLEN=3D%d ABI disabled setup (%d)\n", + pmlen, ret); + + ret =3D write(pipefd[1], p, sizeof(*p)); + if (ret >=3D 0 || errno !=3D EFAULT) + return ksft_test_result_fail("PMLEN=3D%d ABI disabled write\n", pmlen); + + ret =3D read(dev_zero, p, sizeof(*p)); + if (ret >=3D 0 || errno !=3D EFAULT) + return ksft_test_result_fail("PMLEN=3D%d ABI disabled read\n", pmlen); + + if (i !=3D ~pmlen) + return ksft_test_result_fail("PMLEN=3D%d ABI disabled value\n", pmlen); + + ret =3D set_tagged_addr_ctrl(pmlen, true); + if (ret) + return ksft_test_result_error("PMLEN=3D%d ABI enabled setup (%d)\n", + pmlen, ret); + + ret =3D write(pipefd[1], p, sizeof(*p)); + if (ret !=3D sizeof(*p)) + return ksft_test_result_fail("PMLEN=3D%d ABI enabled write\n", pmlen); + + ret =3D read(dev_zero, p, sizeof(*p)); + if (ret !=3D sizeof(*p)) + return ksft_test_result_fail("PMLEN=3D%d ABI enabled read\n", pmlen); + + if (i) + return ksft_test_result_fail("PMLEN=3D%d ABI enabled value\n", pmlen); + + i =3D ~pmlen; + } else { + /* The tagged address ABI cannot be enabled when PMLEN =3D=3D 0. */ + ret =3D set_tagged_addr_ctrl(pmlen, true); + if (ret !=3D -EINVAL) + return ksft_test_result_error("PMLEN=3D%d ABI setup (%d)\n", + pmlen, ret); + } + + p =3D (int *)((uintptr_t)&i | 1UL << __riscv_xlen - pmlen - 1); + + ret =3D write(pipefd[1], p, sizeof(*p)); + if (ret >=3D 0 || errno !=3D EFAULT) + return ksft_test_result_fail("PMLEN=3D%d invalid tag write (%d)\n", pmle= n, errno); + + ret =3D read(dev_zero, p, sizeof(*p)); + if (ret >=3D 0 || errno !=3D EFAULT) + return ksft_test_result_fail("PMLEN=3D%d invalid tag read\n", pmlen); + + if (i !=3D ~pmlen) + return ksft_test_result_fail("PMLEN=3D%d invalid tag value\n", pmlen); + + ksft_test_result_pass("PMLEN=3D%d tagged address ABI\n", pmlen); +} + +static void test_tagged_addr_abi(void) +{ + ksft_print_msg("Testing tagged address ABI\n"); + + test_tagged_addr_abi_pmlen(0); + test_tagged_addr_abi_pmlen(min_pmlen); + test_tagged_addr_abi_pmlen(max_pmlen); +} + +static struct test_info { + unsigned int nr_tests; + void (*test_fn)(void); +} tests[] =3D { + { .nr_tests =3D 17 * 3, test_pmlen }, + { .nr_tests =3D 3, test_dereference }, + { .nr_tests =3D 2, test_fork_exec }, + { .nr_tests =3D 2, test_tagged_addr_abi_sysctl }, + { .nr_tests =3D 3, test_tagged_addr_abi }, +}; + +int main(int argc, char **argv) +{ + unsigned int plan =3D 0; + int ret; + + /* Check if this is the child process after execve(). */ + if (!argv[0][0]) + return execve_child(); + + dev_zero =3D open("/dev/zero", O_RDWR); + if (dev_zero < 0) + return 1; + + /* Write to a pipe so the kernel must dereference the buffer pointer. */ + ret =3D pipe(pipefd); + if (ret) + return 1; + + ksft_print_header(); + + for (int i =3D 0; i < ARRAY_SIZE(tests); ++i) + plan +=3D tests[i].nr_tests; + + ksft_set_plan(plan); + + for (int i =3D 0; i < ARRAY_SIZE(tests); ++i) + tests[i].test_fn(); + + ksft_finished(); +} --=20 2.44.1