selftests: Fix errno checking in syscall_user_dispatch test
Subsystems (3)
PATCH v3 1/3 selftests: Fix errno checking in syscall_user_dispatch test
In reply to a message not in the archiveSuccessful syscalls don't change errno, so checking errno is wrong to ensure that a syscall has failed. For example for the following sequence:
prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0xff, 0); EXPECT_EQ(EINVAL, errno); prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0x0, &sel); EXPECT_EQ(EINVAL, errno);
only the first syscall may fail and set errno, but the second may succeed and keep errno intact, and the check will falsely pass.
Or if errno happened to be EINVAL before, even the first check may falsely pass.
Also use EXPECT/ASSERT consistently. Currently there is an inconsistent mix without obvious reasons for usage of one or another.
---
Changes in v3:
- Added Fixes tag
- Moved this patch as the first in the series
- Added prctl_valid/invalid helpers
--- .../syscall_user_dispatch/sud_test.c | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-)
tools/testing/selftests/syscall_user_dispatch/sud_test.c +25 -25
@@ -79,6 +79,21 @@ TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS) } } +static void prctl_valid (struct __test_metadata *_metadata, +unsigned long op,unsigned long off, +unsigned long size,void *sel) +{ +EXPECT_EQ (0 ,prctl (PR_SET_SYSCALL_USER_DISPATCH , op, off, size, sel)); +} + +static void prctl_invalid (struct __test_metadata *_metadata, +unsigned long op,unsigned long off, +unsigned long size,void *sel,int err) +{ +EXPECT_EQ (-1 ,prctl (PR_SET_SYSCALL_USER_DISPATCH , op, off, size, sel)); +EXPECT_EQ (err, errno); +} +TEST (bad_prctl_param) {char sel =SYSCALL_DISPATCH_FILTER_ALLOW ;
@@ -86,57 +101,42 @@ TEST(bad_prctl_param)/* Invalid op */ op =-1 ; -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0 ,0 , &sel); -ASSERT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0 ,0 , &sel,EINVAL );/* PR_SYS_DISPATCH_OFF */ op =PR_SYS_DISPATCH_OFF ;/* offset != 0 */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x1 ,0x0 ,0 ); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x1 ,0x0 ,0 ,EINVAL );/* len != 0 */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0xff ,0 ); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x0 ,0xff ,0 ,EINVAL );/* sel != NULL */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x0 , &sel); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x0 ,0x0 , &sel,EINVAL );/* Valid parameter */ - errno =0 ; -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x0 ,0x0 ); -EXPECT_EQ (0 , errno); +prctl_valid (_metadata, op,0x0 ,0x0 ,0x0 );/* PR_SYS_DISPATCH_ON */ op =PR_SYS_DISPATCH_ON ;/* Dispatcher region is bad (offset > 0 && len == 0) */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x1 ,0x0 , &sel); -EXPECT_EQ (EINVAL , errno); -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,-1L ,0x0 , &sel); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x1 ,0x0 , &sel,EINVAL ); +prctl_invalid (_metadata, op,-1L ,0x0 , &sel,EINVAL );/* Invalid selector */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x1 , (void *)-1 ); -ASSERT_EQ (EFAULT , errno); +prctl_invalid (_metadata, op,0x0 ,0x1 , (void *)-1 ,EFAULT );/* * Dispatcher range overflows unsigned long */ -prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,1 ,-1L , &sel); - ASSERT_EQ(EINVAL, errno) { -TH_LOG ("Should reject bad syscall range" ); - } +prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,1 ,-1L , &sel,EINVAL );/* * Allowed range overflows usigned long */ -prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,-1L ,0x1 , &sel); - ASSERT_EQ(EINVAL, errno) { -TH_LOG ("Should reject bad syscall range" ); - } +prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,-1L ,0x1 , &sel,EINVAL ); }/*
Signature · 1 line
2.49.0.1143.g0be31eac6b-goog
PATCH v3 2/3 syscall_user_dispatch: Add PR_SYS_DISPATCH_INCLUSIVE_ON
In reply to a message not in the archiveThere are two possible scenarios for syscall filtering:
- having a trusted/allowed range of PCs, and intercepting everything else
- or the opposite: a single untrusted/intercepted range and allowing everything else (this is relevant for any kind of sandboxing scenario, or monitoring behavior of a single library)
The current API only allows the former use case due to allowed range wrap-around check. Add PR_SYS_DISPATCH_INCLUSIVE_ON that enables the second use case.
Add PR_SYS_DISPATCH_EXCLUSIVE_ON alias for PR_SYS_DISPATCH_ON to make it clear how it's different from the new PR_SYS_DISPATCH_INCLUSIVE_ON.
---
Changes in v3:
- Add new PR_SYS_DISPATCH_INCLUSIVE_ON mode
instead of extending behavior of the old mode
--- .../admin-guide/syscall-user-dispatch.rst | 23 +++++++----- include/uapi/linux/prctl.h | 7 +++- kernel/entry/syscall_user_dispatch.c | 36 ++++++++++++------- tools/include/uapi/linux/prctl.h | 7 +++- 4 files changed, 49 insertions(+), 24 deletions(-)
Documentation/admin-guide/syscall-user-dispatch.rst +14 -9
@@ -53,20 +53,25 @@ following prctl: prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <offset>, <length>, [selector]) -<op> is either PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF, to enable and -disable the mechanism globally for that thread. When -PR_SYS_DISPATCH_OFF is used, the other fields must be zero. - -[<offset>, <offset>+<length>) delimit a memory region interval -from which syscalls are always executed directly, regardless of the -userspace selector. This provides a fast path for the C library, which -includes the most common syscall dispatchers in the native code -applications, and also provides a way for the signal handler to return +<op> is either PR_SYS_DISPATCH_EXCLUSIVE_ON/PR_SYS_DISPATCH_INCLUSIVE_ON +or PR_SYS_DISPATCH_OFF, to enable and disable the mechanism globally for +that thread. When PR_SYS_DISPATCH_OFF is used, the other fields must be zero. + +For PR_SYS_DISPATCH_EXCLUSIVE_ON [<offset>, <offset>+<length>) delimit +a memory region interval from which syscalls are always executed directly, +regardless of the userspace selector. This provides a fast path for the +C library, which includes the most common syscall dispatchers in the native +code applications, and also provides a way for the signal handler to return without triggering a nested SIGSYS on (rt\_)sigreturn. Users of this interface should make sure that at least the signal trampoline code is included in this region. In addition, for syscalls that implement the trampoline code on the vDSO, that trampoline is never intercepted. +For PR_SYS_DISPATCH_INCLUSIVE_ON [<offset>, <offset>+<length>) delimit +a memory region interval from which syscalls are dispatched based on +the userspace selector. Syscalls from outside of the range are always +executed directly. + [selector] is a pointer to a char-sized region in the process memory region, that provides a quick way to enable disable syscall redirection thread-wide, without the need to invoke the kernel directly. selector
include/uapi/linux/prctl.h +6 -1
@@ -255,7 +255,12 @@ struct prctl_mm_map {/* Dispatch syscalls to a userspace handler */ #define PR_SET_SYSCALL_USER_DISPATCH 59# define PR_SYS_DISPATCH_OFF 0 -# define PR_SYS_DISPATCH_ON 1 +/* Enable dispatch except for the specified range */ +# define PR_SYS_DISPATCH_EXCLUSIVE_ON 1 +/* Enable dispatch for the specified range */ +# define PR_SYS_DISPATCH_INCLUSIVE_ON 2 +/* Legacy name for backwards compatibility */ +# define PR_SYS_DISPATCH_ON PR_SYS_DISPATCH_EXCLUSIVE_ON/* The control values for the user space selector when dispatch is enabled */ # define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1
kernel/entry/syscall_user_dispatch.c +23 -13
@@ -78,7 +78,7 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lonif (offset || len || selector)return -EINVAL ;break ; -case PR_SYS_DISPATCH_ON : +case PR_SYS_DISPATCH_EXCLUSIVE_ON :/* * Validate the direct dispatcher region just for basic * sanity against overflow and a 0-sized dispatcher
@@ -87,30 +87,40 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lon*/ if (offset && offset + len <= offset)return -EINVAL ; - +break ; +case PR_SYS_DISPATCH_INCLUSIVE_ON : +if (len ==0 || offset + len <= offset) +return -EINVAL ;/* -* access_ok() will clear memory tags for tagged addresses -* if current has memory tagging enabled. - -* To enable a tracer to set a tracees selector the -* selector address must be untagged for access_ok(), -* otherwise an untagged tracer will always fail to set a -* tagged tracees selector. +* Invert the range, the check in syscall_user_dispatch() +* supports wrap-around. */ -if (selector && !access_ok (untagged_addr (selector),sizeof (*selector))) -return -EFAULT ; - + offset = offset + len; + len = -len;break ;default :return -EINVAL ; } +/* +* access_ok() will clear memory tags for tagged addresses +* if current has memory tagging enabled. + +* To enable a tracer to set a tracees selector the +* selector address must be untagged for access_ok(), +* otherwise an untagged tracer will always fail to set a +* tagged tracees selector. +*/ +if (mode !=PR_SYS_DISPATCH_OFF && selector && + !access_ok (untagged_addr (selector),sizeof (*selector))) +return -EFAULT ; + task->syscall_dispatch.selector = selector; task->syscall_dispatch.offset = offset; task->syscall_dispatch.len = len; task->syscall_dispatch.on_dispatch = false; -if (mode ==PR_SYS_DISPATCH_ON ) +if (mode !=PR_SYS_DISPATCH_OFF )set_task_syscall_work (task,SYSCALL_USER_DISPATCH );else clear_task_syscall_work (task,SYSCALL_USER_DISPATCH );
tools/include/uapi/linux/prctl.h +6 -1
@@ -252,7 +252,12 @@ struct prctl_mm_map {/* Dispatch syscalls to a userspace handler */ #define PR_SET_SYSCALL_USER_DISPATCH 59# define PR_SYS_DISPATCH_OFF 0 -# define PR_SYS_DISPATCH_ON 1 +/* Enable dispatch except for the specified range */ +# define PR_SYS_DISPATCH_EXCLUSIVE_ON 1 +/* Enable dispatch for the specified range */ +# define PR_SYS_DISPATCH_INCLUSIVE_ON 2 +/* Legacy name for backwards compatibility */ +# define PR_SYS_DISPATCH_ON PR_SYS_DISPATCH_EXCLUSIVE_ON/* The control values for the user space selector when dispatch is enabled */ # define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1
Signature · 1 line
2.49.0.1143.g0be31eac6b-goog
PATCH v3 3/3 selftests: Add tests for PR_SYS_DISPATCH_INCLUSIVE_ON
In reply to a message not in the archiveAdd tests for PR_SYS_DISPATCH_INCLUSIVE_ON correct/incorrect args, and a test that ensures that the specified range is respected by both PR_SYS_DISPATCH_EXCLUSIVE_ON and PR_SYS_DISPATCH_INCLUSIVE_ON.
---
Changes in v3:
- rework tests for the new PR_SYS_DISPATCH_INCLUSIVE_ON
Changes in v2:
- add tests for 0-sized range
- change range setup in the test to be fatal
--- .../syscall_user_dispatch/sud_test.c | 94 +++++++++++++++---- 1 file changed, 74 insertions(+), 20 deletions(-)
tools/testing/selftests/syscall_user_dispatch/sud_test.c +74 -20
@@ -10,6 +10,8 @@#include <sys/sysinfo.h> #include <sys/syscall.h> #include <signal.h> +#include <stdbool.h> +#include <stdlib.h> #include <asm/unistd.h> #include "../kselftest_harness.h"
@@ -17,11 +19,15 @@#ifndef PR_SET_SYSCALL_USER_DISPATCH # define PR_SET_SYSCALL_USER_DISPATCH 59# define PR_SYS_DISPATCH_OFF 0 -# define PR_SYS_DISPATCH_ON 1# define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1#endif +#ifndef PR_SYS_DISPATCH_EXCLUSIVE_ON +# define PR_SYS_DISPATCH_EXCLUSIVE_ON 1 +# define PR_SYS_DISPATCH_INCLUSIVE_ON 2 +#endif +#ifndef SYS_USER_DISPATCH # define SYS_USER_DISPATCH 2#endif
@@ -65,7 +71,7 @@ TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS) ret =sysinfo (&info);ASSERT_EQ (0 , ret); - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -118,8 +124,8 @@ TEST(bad_prctl_param)/* Valid parameter */ prctl_valid (_metadata, op,0x0 ,0x0 ,0x0 ); -/* PR_SYS_DISPATCH_ON */ - op =PR_SYS_DISPATCH_ON ; +/* PR_SYS_DISPATCH_EXCLUSIVE_ON */ + op =PR_SYS_DISPATCH_EXCLUSIVE_ON ;/* Dispatcher region is bad (offset > 0 && len == 0) */ prctl_invalid (_metadata, op,0x1 ,0x0 , &sel,EINVAL );
@@ -131,12 +137,24 @@ TEST(bad_prctl_param)/* * Dispatcher range overflows unsigned long */ -prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,1 ,-1L , &sel,EINVAL ); +prctl_invalid (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON ,1 ,-1L , &sel,EINVAL );/* * Allowed range overflows usigned long */ -prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,-1L ,0x1 , &sel,EINVAL ); +prctl_invalid (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON ,-1L ,0x1 , &sel,EINVAL ); + +/* 0 len should fail for PR_SYS_DISPATCH_INCLUSIVE_ON */ +prctl_invalid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,1 ,0 ,0 ,EINVAL ); + +/* Range wrap-around should fail */ +prctl_invalid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,-1L ,2 ,0 ,EINVAL ); + +/* Normal range shouldn't fail */ +prctl_valid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,2 ,3 ,0 ); + +/* Invalid selector */ +prctl_invalid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,2 ,3 , (void *)-1 ,EFAULT ); }/*
@@ -147,11 +165,13 @@ char glob_sel;int nr_syscalls_emulated;int si_code;int si_errno; +unsigned long syscall_addr;static void handle_sigsys (int sig,siginfo_t *info,void *ucontext) { si_code = info->si_code; si_errno = info->si_errno; + syscall_addr = (unsigned long )info->si_call_addr;if (info->si_syscall ==MAGIC_SYSCALL_1 ) nr_syscalls_emulated++;
@@ -174,31 +194,34 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)#endif } -TEST (dispatch_and_return) +int setup_sigsys_handler (void ) { -long ret;struct sigaction act;sigset_t mask; - glob_sel =0 ; - nr_syscalls_emulated =0 ; - si_code =0 ; - si_errno =0 ; -memset (&act,0 ,sizeof (act));sigemptyset (&mask); - act.sa_sigaction = handle_sigsys; act.sa_flags =SA_SIGINFO ; act.sa_mask = mask; +return sigaction (SIGSYS , &act,NULL ); +} - ret =sigaction (SIGSYS , &act,NULL ); -ASSERT_EQ (0 , ret); +TEST (dispatch_and_return) +{ +long ret; + + glob_sel =0 ; + nr_syscalls_emulated =0 ; + si_code =0 ; + si_errno =0 ; + +ASSERT_EQ (0 ,setup_sigsys_handler ());/* Make sure selector is good prior to prctl. */ SYSCALL_DISPATCH_OFF (glob_sel); - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &glob_sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &glob_sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -254,7 +277,7 @@ TEST_SIGNAL(bad_selector, SIGSYS)/* Make sure selector is good prior to prctl. */ SYSCALL_DISPATCH_OFF (glob_sel); - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &glob_sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &glob_sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -278,7 +301,7 @@ TEST(disable_dispatch)struct sysinfo info;char sel =0 ; - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -310,7 +333,7 @@ TEST(direct_dispatch_range)* Instead of calculating libc addresses; allow the entire * memory map and lock the selector. */ - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,-1L , &sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,-1L , &sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -323,4 +346,35 @@ TEST(direct_dispatch_range) } } +static void test_range (struct __test_metadata *_metadata, +unsigned long op,unsigned long off, +unsigned long size,bool dispatch) +{ + nr_syscalls_emulated =0 ; +SYSCALL_DISPATCH_OFF (glob_sel); +EXPECT_EQ (0 ,prctl (PR_SET_SYSCALL_USER_DISPATCH , op, off, size, &glob_sel)); +SYSCALL_DISPATCH_ON (glob_sel); +if (dispatch) { +EXPECT_EQ (syscall (MAGIC_SYSCALL_1 ),MAGIC_SYSCALL_1 ); +EXPECT_EQ (nr_syscalls_emulated,1 ); + }else { +EXPECT_EQ (syscall (MAGIC_SYSCALL_1 ),-1 ); +EXPECT_EQ (nr_syscalls_emulated,0 ); + } +} + +TEST (dispatch_range) +{ +ASSERT_EQ (0 ,setup_sigsys_handler ()); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , true); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr,1 , false); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr-100 ,200 , false); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr+1 ,100 , true); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr-100 ,100 , true); +test_range (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON , syscall_addr,1 , true); +test_range (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON , syscall_addr-1 ,1 , false); +test_range (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON , syscall_addr+1 ,1 , false); +SYSCALL_DISPATCH_OFF (glob_sel); +} +TEST_HARNESS_MAIN
Signature · 1 line
2.49.0.1143.g0be31eac6b-goog
[tip: core/entry] selftests: Add tests for PR_SYS_DISPATCH_INCLUSIVE_ON
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: b6a5a16b8b59476156dc6d6f73bffaf3a1707adb Gitweb: https://git.kernel.org/tip/b6a5a16b8b59476156dc6d6f73bffaf3a1707adb Author: Dmitry Vyukov <[email protected]> AuthorDate: Wed, 21 May 2025 17:04:30 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 13 Jun 2025 18:36:39 +02:00
selftests: Add tests for PR_SYS_DISPATCH_INCLUSIVE_ON
Add tests for PR_SYS_DISPATCH_INCLUSIVE_ON correct/incorrect args, and a test that ensures that the specified range is respected by both PR_SYS_DISPATCH_EXCLUSIVE_ON and PR_SYS_DISPATCH_INCLUSIVE_ON.
--- tools/testing/selftests/syscall_user_dispatch/sud_test.c | 94 +++++-- 1 file changed, 74 insertions(+), 20 deletions(-)
tools/testing/selftests/syscall_user_dispatch/sud_test.c +74 -20
@@ -10,6 +10,8 @@#include <sys/sysinfo.h> #include <sys/syscall.h> #include <signal.h> +#include <stdbool.h> +#include <stdlib.h> #include <asm/unistd.h> #include "../kselftest_harness.h"
@@ -17,11 +19,15 @@#ifndef PR_SET_SYSCALL_USER_DISPATCH # define PR_SET_SYSCALL_USER_DISPATCH 59# define PR_SYS_DISPATCH_OFF 0 -# define PR_SYS_DISPATCH_ON 1# define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1#endif +#ifndef PR_SYS_DISPATCH_EXCLUSIVE_ON +# define PR_SYS_DISPATCH_EXCLUSIVE_ON 1 +# define PR_SYS_DISPATCH_INCLUSIVE_ON 2 +#endif +#ifndef SYS_USER_DISPATCH # define SYS_USER_DISPATCH 2#endif
@@ -65,7 +71,7 @@ TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS) ret =sysinfo (&info);ASSERT_EQ (0 , ret); - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -118,8 +124,8 @@ TEST(bad_prctl_param)/* Valid parameter */ prctl_valid (_metadata, op,0x0 ,0x0 ,0x0 ); -/* PR_SYS_DISPATCH_ON */ - op =PR_SYS_DISPATCH_ON ; +/* PR_SYS_DISPATCH_EXCLUSIVE_ON */ + op =PR_SYS_DISPATCH_EXCLUSIVE_ON ;/* Dispatcher region is bad (offset > 0 && len == 0) */ prctl_invalid (_metadata, op,0x1 ,0x0 , &sel,EINVAL );
@@ -131,12 +137,24 @@ TEST(bad_prctl_param)/* * Dispatcher range overflows unsigned long */ -prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,1 ,-1L , &sel,EINVAL ); +prctl_invalid (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON ,1 ,-1L , &sel,EINVAL );/* * Allowed range overflows usigned long */ -prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,-1L ,0x1 , &sel,EINVAL ); +prctl_invalid (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON ,-1L ,0x1 , &sel,EINVAL ); + +/* 0 len should fail for PR_SYS_DISPATCH_INCLUSIVE_ON */ +prctl_invalid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,1 ,0 ,0 ,EINVAL ); + +/* Range wrap-around should fail */ +prctl_invalid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,-1L ,2 ,0 ,EINVAL ); + +/* Normal range shouldn't fail */ +prctl_valid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,2 ,3 ,0 ); + +/* Invalid selector */ +prctl_invalid (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON ,2 ,3 , (void *)-1 ,EFAULT ); }/*
@@ -147,11 +165,13 @@ char glob_sel;int nr_syscalls_emulated;int si_code;int si_errno; +unsigned long syscall_addr;static void handle_sigsys (int sig,siginfo_t *info,void *ucontext) { si_code = info->si_code; si_errno = info->si_errno; + syscall_addr = (unsigned long )info->si_call_addr;if (info->si_syscall ==MAGIC_SYSCALL_1 ) nr_syscalls_emulated++;
@@ -174,31 +194,34 @@ static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)#endif } -TEST (dispatch_and_return) +int setup_sigsys_handler (void ) { -long ret;struct sigaction act;sigset_t mask; - glob_sel =0 ; - nr_syscalls_emulated =0 ; - si_code =0 ; - si_errno =0 ; -memset (&act,0 ,sizeof (act));sigemptyset (&mask); - act.sa_sigaction = handle_sigsys; act.sa_flags =SA_SIGINFO ; act.sa_mask = mask; +return sigaction (SIGSYS , &act,NULL ); +} - ret =sigaction (SIGSYS , &act,NULL ); -ASSERT_EQ (0 , ret); +TEST (dispatch_and_return) +{ +long ret; + + glob_sel =0 ; + nr_syscalls_emulated =0 ; + si_code =0 ; + si_errno =0 ; + +ASSERT_EQ (0 ,setup_sigsys_handler ());/* Make sure selector is good prior to prctl. */ SYSCALL_DISPATCH_OFF (glob_sel); - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &glob_sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &glob_sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -254,7 +277,7 @@ TEST_SIGNAL(bad_selector, SIGSYS)/* Make sure selector is good prior to prctl. */ SYSCALL_DISPATCH_OFF (glob_sel); - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &glob_sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &glob_sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -278,7 +301,7 @@ TEST(disable_dispatch)struct sysinfo info;char sel =0 ; - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,0 , &sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , &sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -310,7 +333,7 @@ TEST(direct_dispatch_range)* Instead of calculating libc addresses; allow the entire * memory map and lock the selector. */ - ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,0 ,-1L , &sel); + ret =prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,-1L , &sel); ASSERT_EQ(0, ret) {TH_LOG ("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH" ); }
@@ -323,4 +346,35 @@ TEST(direct_dispatch_range) } } +static void test_range (struct __test_metadata *_metadata, +unsigned long op,unsigned long off, +unsigned long size,bool dispatch) +{ + nr_syscalls_emulated =0 ; +SYSCALL_DISPATCH_OFF (glob_sel); +EXPECT_EQ (0 ,prctl (PR_SET_SYSCALL_USER_DISPATCH , op, off, size, &glob_sel)); +SYSCALL_DISPATCH_ON (glob_sel); +if (dispatch) { +EXPECT_EQ (syscall (MAGIC_SYSCALL_1 ),MAGIC_SYSCALL_1 ); +EXPECT_EQ (nr_syscalls_emulated,1 ); + }else { +EXPECT_EQ (syscall (MAGIC_SYSCALL_1 ),-1 ); +EXPECT_EQ (nr_syscalls_emulated,0 ); + } +} + +TEST (dispatch_range) +{ +ASSERT_EQ (0 ,setup_sigsys_handler ()); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON ,0 ,0 , true); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr,1 , false); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr-100 ,200 , false); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr+1 ,100 , true); +test_range (_metadata,PR_SYS_DISPATCH_EXCLUSIVE_ON , syscall_addr-100 ,100 , true); +test_range (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON , syscall_addr,1 , true); +test_range (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON , syscall_addr-1 ,1 , false); +test_range (_metadata,PR_SYS_DISPATCH_INCLUSIVE_ON , syscall_addr+1 ,1 , false); +SYSCALL_DISPATCH_OFF (glob_sel); +} +TEST_HARNESS_MAIN
[tip: core/entry] syscall_user_dispatch: Add PR_SYS_DISPATCH_INCLUSIVE_ON
In reply to Dmitry VyukovThe following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: a2fc422ed75748eef2985454e97847fb22f873c2 Gitweb: https://git.kernel.org/tip/a2fc422ed75748eef2985454e97847fb22f873c2 Author: Dmitry Vyukov <[email protected]> AuthorDate: Wed, 21 May 2025 17:04:29 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 13 Jun 2025 18:36:39 +02:00
syscall_user_dispatch: Add PR_SYS_DISPATCH_INCLUSIVE_ON
There are two possible scenarios for syscall filtering:
- having a trusted/allowed range of PCs, and intercepting everything else
- or the opposite: a single untrusted/intercepted range and allowing everything else (this is relevant for any kind of sandboxing scenario, or monitoring behavior of a single library)
The current API only allows the former use case due to allowed range wrap-around check. Add PR_SYS_DISPATCH_INCLUSIVE_ON that enables the second use case.
Add PR_SYS_DISPATCH_EXCLUSIVE_ON alias for PR_SYS_DISPATCH_ON to make it clear how it's different from the new PR_SYS_DISPATCH_INCLUSIVE_ON.
--- Documentation/admin-guide/syscall-user-dispatch.rst | 23 ++++---- include/uapi/linux/prctl.h | 7 +- kernel/entry/syscall_user_dispatch.c | 36 +++++++----- tools/include/uapi/linux/prctl.h | 7 +- 4 files changed, 49 insertions(+), 24 deletions(-)
Documentation/admin-guide/syscall-user-dispatch.rst +14 -9
@@ -53,20 +53,25 @@ following prctl: prctl(PR_SET_SYSCALL_USER_DISPATCH, <op>, <offset>, <length>, [selector]) -<op> is either PR_SYS_DISPATCH_ON or PR_SYS_DISPATCH_OFF, to enable and -disable the mechanism globally for that thread. When -PR_SYS_DISPATCH_OFF is used, the other fields must be zero. - -[<offset>, <offset>+<length>) delimit a memory region interval -from which syscalls are always executed directly, regardless of the -userspace selector. This provides a fast path for the C library, which -includes the most common syscall dispatchers in the native code -applications, and also provides a way for the signal handler to return +<op> is either PR_SYS_DISPATCH_EXCLUSIVE_ON/PR_SYS_DISPATCH_INCLUSIVE_ON +or PR_SYS_DISPATCH_OFF, to enable and disable the mechanism globally for +that thread. When PR_SYS_DISPATCH_OFF is used, the other fields must be zero. + +For PR_SYS_DISPATCH_EXCLUSIVE_ON [<offset>, <offset>+<length>) delimit +a memory region interval from which syscalls are always executed directly, +regardless of the userspace selector. This provides a fast path for the +C library, which includes the most common syscall dispatchers in the native +code applications, and also provides a way for the signal handler to return without triggering a nested SIGSYS on (rt\_)sigreturn. Users of this interface should make sure that at least the signal trampoline code is included in this region. In addition, for syscalls that implement the trampoline code on the vDSO, that trampoline is never intercepted. +For PR_SYS_DISPATCH_INCLUSIVE_ON [<offset>, <offset>+<length>) delimit +a memory region interval from which syscalls are dispatched based on +the userspace selector. Syscalls from outside of the range are always +executed directly. + [selector] is a pointer to a char-sized region in the process memory region, that provides a quick way to enable disable syscall redirection thread-wide, without the need to invoke the kernel directly. selector
include/uapi/linux/prctl.h +6 -1
@@ -255,7 +255,12 @@ struct prctl_mm_map {/* Dispatch syscalls to a userspace handler */ #define PR_SET_SYSCALL_USER_DISPATCH 59# define PR_SYS_DISPATCH_OFF 0 -# define PR_SYS_DISPATCH_ON 1 +/* Enable dispatch except for the specified range */ +# define PR_SYS_DISPATCH_EXCLUSIVE_ON 1 +/* Enable dispatch for the specified range */ +# define PR_SYS_DISPATCH_INCLUSIVE_ON 2 +/* Legacy name for backwards compatibility */ +# define PR_SYS_DISPATCH_ON PR_SYS_DISPATCH_EXCLUSIVE_ON/* The control values for the user space selector when dispatch is enabled */ # define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1
kernel/entry/syscall_user_dispatch.c +23 -13
@@ -78,7 +78,7 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lonif (offset || len || selector)return -EINVAL ;break ; -case PR_SYS_DISPATCH_ON : +case PR_SYS_DISPATCH_EXCLUSIVE_ON :/* * Validate the direct dispatcher region just for basic * sanity against overflow and a 0-sized dispatcher
@@ -87,30 +87,40 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lon*/ if (offset && offset + len <= offset)return -EINVAL ; - +break ; +case PR_SYS_DISPATCH_INCLUSIVE_ON : +if (len ==0 || offset + len <= offset) +return -EINVAL ;/* -* access_ok() will clear memory tags for tagged addresses -* if current has memory tagging enabled. - -* To enable a tracer to set a tracees selector the -* selector address must be untagged for access_ok(), -* otherwise an untagged tracer will always fail to set a -* tagged tracees selector. +* Invert the range, the check in syscall_user_dispatch() +* supports wrap-around. */ -if (selector && !access_ok (untagged_addr (selector),sizeof (*selector))) -return -EFAULT ; - + offset = offset + len; + len = -len;break ;default :return -EINVAL ; } +/* +* access_ok() will clear memory tags for tagged addresses +* if current has memory tagging enabled. +* +* To enable a tracer to set a tracees selector the +* selector address must be untagged for access_ok(), +* otherwise an untagged tracer will always fail to set a +* tagged tracees selector. +*/ +if (mode !=PR_SYS_DISPATCH_OFF && selector && + !access_ok (untagged_addr (selector),sizeof (*selector))) +return -EFAULT ; + task->syscall_dispatch.selector = selector; task->syscall_dispatch.offset = offset; task->syscall_dispatch.len = len; task->syscall_dispatch.on_dispatch = false; -if (mode ==PR_SYS_DISPATCH_ON ) +if (mode !=PR_SYS_DISPATCH_OFF )set_task_syscall_work (task,SYSCALL_USER_DISPATCH );else clear_task_syscall_work (task,SYSCALL_USER_DISPATCH );
tools/include/uapi/linux/prctl.h +6 -1
@@ -255,7 +255,12 @@ struct prctl_mm_map {/* Dispatch syscalls to a userspace handler */ #define PR_SET_SYSCALL_USER_DISPATCH 59# define PR_SYS_DISPATCH_OFF 0 -# define PR_SYS_DISPATCH_ON 1 +/* Enable dispatch except for the specified range */ +# define PR_SYS_DISPATCH_EXCLUSIVE_ON 1 +/* Enable dispatch for the specified range */ +# define PR_SYS_DISPATCH_INCLUSIVE_ON 2 +/* Legacy name for backwards compatibility */ +# define PR_SYS_DISPATCH_ON PR_SYS_DISPATCH_EXCLUSIVE_ON/* The control values for the user space selector when dispatch is enabled */ # define SYSCALL_DISPATCH_FILTER_ALLOW 0# define SYSCALL_DISPATCH_FILTER_BLOCK 1
The following commit has been merged into the core/entry branch of tip:
The following commit has been merged into the core/entry branch of tip:
Commit-ID: b89732c8c8357487185f260a723a060b3476144e Gitweb: https://git.kernel.org/tip/b89732c8c8357487185f260a723a060b3476144e Author: Dmitry Vyukov <[email protected]> AuthorDate: Wed, 21 May 2025 17:04:28 +02:00 Committer: Thomas Gleixner <[email protected]> CommitterDate: Fri, 13 Jun 2025 18:36:39 +02:00
selftests: Fix errno checking in syscall_user_dispatch test
Successful syscalls don't change errno, so checking errno is wrong to ensure that a syscall has failed. For example for the following sequence:
prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0xff, 0); EXPECT_EQ(EINVAL, errno); prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0x0, &sel); EXPECT_EQ(EINVAL, errno);
only the first syscall may fail and set errno, but the second may succeed and keep errno intact, and the check will falsely pass.
Or if errno happened to be EINVAL before, even the first check may falsely pass.
Also use EXPECT/ASSERT consistently. Currently there is an inconsistent mix without obvious reasons for usage of one or another.
--- tools/testing/selftests/syscall_user_dispatch/sud_test.c | 50 +++---- 1 file changed, 25 insertions(+), 25 deletions(-)
tools/testing/selftests/syscall_user_dispatch/sud_test.c +25 -25
@@ -79,6 +79,21 @@ TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS) } } +static void prctl_valid (struct __test_metadata *_metadata, +unsigned long op,unsigned long off, +unsigned long size,void *sel) +{ +EXPECT_EQ (0 ,prctl (PR_SET_SYSCALL_USER_DISPATCH , op, off, size, sel)); +} + +static void prctl_invalid (struct __test_metadata *_metadata, +unsigned long op,unsigned long off, +unsigned long size,void *sel,int err) +{ +EXPECT_EQ (-1 ,prctl (PR_SET_SYSCALL_USER_DISPATCH , op, off, size, sel)); +EXPECT_EQ (err, errno); +} +TEST (bad_prctl_param) {char sel =SYSCALL_DISPATCH_FILTER_ALLOW ;
@@ -86,57 +101,42 @@ TEST(bad_prctl_param)/* Invalid op */ op =-1 ; -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0 ,0 , &sel); -ASSERT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0 ,0 , &sel,EINVAL );/* PR_SYS_DISPATCH_OFF */ op =PR_SYS_DISPATCH_OFF ;/* offset != 0 */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x1 ,0x0 ,0 ); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x1 ,0x0 ,0 ,EINVAL );/* len != 0 */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0xff ,0 ); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x0 ,0xff ,0 ,EINVAL );/* sel != NULL */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x0 , &sel); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x0 ,0x0 , &sel,EINVAL );/* Valid parameter */ - errno =0 ; -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x0 ,0x0 ); -EXPECT_EQ (0 , errno); +prctl_valid (_metadata, op,0x0 ,0x0 ,0x0 );/* PR_SYS_DISPATCH_ON */ op =PR_SYS_DISPATCH_ON ;/* Dispatcher region is bad (offset > 0 && len == 0) */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x1 ,0x0 , &sel); -EXPECT_EQ (EINVAL , errno); -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,-1L ,0x0 , &sel); -EXPECT_EQ (EINVAL , errno); +prctl_invalid (_metadata, op,0x1 ,0x0 , &sel,EINVAL ); +prctl_invalid (_metadata, op,-1L ,0x0 , &sel,EINVAL );/* Invalid selector */ -prctl (PR_SET_SYSCALL_USER_DISPATCH , op,0x0 ,0x1 , (void *)-1 ); -ASSERT_EQ (EFAULT , errno); +prctl_invalid (_metadata, op,0x0 ,0x1 , (void *)-1 ,EFAULT );/* * Dispatcher range overflows unsigned long */ -prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,1 ,-1L , &sel); - ASSERT_EQ(EINVAL, errno) { -TH_LOG ("Should reject bad syscall range" ); - } +prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,1 ,-1L , &sel,EINVAL );/* * Allowed range overflows usigned long */ -prctl (PR_SET_SYSCALL_USER_DISPATCH ,PR_SYS_DISPATCH_ON ,-1L ,0x1 , &sel); - ASSERT_EQ(EINVAL, errno) { -TH_LOG ("Should reject bad syscall range" ); - } +prctl_invalid (_metadata,PR_SYS_DISPATCH_ON ,-1L ,0x1 , &sel,EINVAL ); }/*
PATCH 1/1 syscall_user_dispatch: Fix grammar in task_set_syscall_user_dispatch()
In reply to tip-bot2 for Dmitry VyukovReview
Quoted 9 lines · * tip-bot2 for Dmitry Vyukov <[email protected]> wrote:
(quoted diff) +9
-0+ /* + * access_ok() will clear memory tags for tagged addresses + * if current has memory tagging enabled. + * + * To enable a tracer to set a tracees selector the + * selector address must be untagged for access_ok(), + * otherwise an untagged tracer will always fail to set a + * tagged tracees selector. + */
Typo/grammar fixes below. (And feel free to squash this fixlet into the originator commit.)
Thanks,
Ingo
==================================>
kernel/entry/syscall_user_dispatch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/entry/syscall_user_dispatch.c +2 -2
@@ -106,10 +106,10 @@ static int task_set_syscall_user_dispatch(struct task_struct *task, unsigned lon* access_ok() will clear memory tags for tagged addresses * if current has memory tagging enabled. * -* To enable a tracer to set a tracees selector the +* To enable a tracer to set a tracee's selector, the * selector address must be untagged for access_ok(), * otherwise an untagged tracer will always fail to set a -* tagged tracees selector. +* tagged tracee's selector. */ if (mode !=PR_SYS_DISPATCH_OFF && selector && !access_ok (untagged_addr (selector),sizeof (*selector)))