Re: [PATCH 3/3] selftests: Extend syscall_user_dispatch test to check allowed range
From: Gregory Price
Date: Thu Feb 20 2025 - 10:20:17 EST
On Tue, Feb 18, 2025 at 05:04:36PM +0100, Dmitry Vyukov wrote:
> diff --git a/tools/testing/selftests/syscall_user_dispatch/sud_test.c b/tools/testing/selftests/syscall_user_dispatch/sud_test.c
> index b0969925ec64c..fa40e46e6d3e9 100644
> --- a/tools/testing/selftests/syscall_user_dispatch/sud_test.c
> +++ b/tools/testing/selftests/syscall_user_dispatch/sud_test.c
... snip ...
> @@ -110,31 +111,15 @@ TEST(bad_prctl_param)
> /* PR_SYS_DISPATCH_ON */
> op = PR_SYS_DISPATCH_ON;
>
> - /* Dispatcher region is bad (offset > 0 && len == 0) */
> - EXPECT_EQ(-1, prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x1, 0x0, &sel));
> - EXPECT_EQ(EINVAL, errno);
> - EXPECT_EQ(-1, prctl(PR_SET_SYSCALL_USER_DISPATCH, op, -1L, 0x0, &sel));
> - EXPECT_EQ(EINVAL, errno);
> + /* All ranges are allowed */
> + EXPECT_EQ(0, prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x1, 0x0, &sel));
> + EXPECT_EQ(0, prctl(PR_SET_SYSCALL_USER_DISPATCH, op, -1L, 0x0, &sel));
A 0 length is ambiguous and nonsensical in every other context, not sure
why you'd allow it here.
... snip ...
> +bool test_range(unsigned long offset, unsigned long length)
> +{
> + nr_syscalls_emulated = 0;
> + if (prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, offset, length, &glob_sel))
> + return false;
This creates an ambiguous failure state for your test. Is it failing
because the range is bad or because you didn't intercept a syscall?
Better to be more explicit here. It makes it difficult to understand
what each individual test is doing at a glance.
> + SYSCALL_DISPATCH_ON(glob_sel);
> + return syscall(MAGIC_SYSCALL_1) == MAGIC_SYSCALL_1 && nr_syscalls_emulated == 1;
> +}
> +
> +TEST(dispatch_range)
> +{
> + ASSERT_EQ(0, setup_sigsys_handler());
> + ASSERT_EQ(0, prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &glob_sel));
> + SYSCALL_DISPATCH_ON(glob_sel);
> + ASSERT_EQ(MAGIC_SYSCALL_1, syscall(MAGIC_SYSCALL_1));
> + TH_LOG("syscall_addr=0x%lx", syscall_addr);
> + EXPECT_FALSE(test_range(syscall_addr, 1));
> + EXPECT_FALSE(test_range(syscall_addr-100, 200));
> + EXPECT_TRUE(test_range(syscall_addr+1, 100));
> + EXPECT_TRUE(test_range(syscall_addr-100, 100));
> + /* Wrap-around tests for everything except for a single PC. */
> + EXPECT_TRUE(test_range(syscall_addr+1, -1));
> + EXPECT_FALSE(test_range(syscall_addr, -1));
> + EXPECT_FALSE(test_range(syscall_addr+2, -1));
If you are planning to include 0 as an allowed length, you need to
demonstrate what it does.
> + SYSCALL_DISPATCH_OFF(glob_sel);
> +}
> +
> TEST_HARNESS_MAIN
> --
> 2.48.1.601.g30ceb7b040-goog
>