1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2020 Collabora Ltd.
5 * Test code for syscall user dispatch
10 #include <sys/sysinfo.h>
11 #include <sys/syscall.h>
14 #include <asm/unistd.h>
15 #include "../kselftest_harness.h"
17 #ifndef PR_SET_SYSCALL_USER_DISPATCH
18 # define PR_SET_SYSCALL_USER_DISPATCH 59
19 # define PR_SYS_DISPATCH_OFF 0
20 # define PR_SYS_DISPATCH_ON 1
21 # define SYSCALL_DISPATCH_FILTER_ALLOW 0
22 # define SYSCALL_DISPATCH_FILTER_BLOCK 1
25 #ifndef SYS_USER_DISPATCH
26 # define SYS_USER_DISPATCH 2
30 # define MAGIC_SYSCALL_1 (__NR_syscalls + 1) /* Bad Linux syscall number */
32 # define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */
35 #define SYSCALL_DISPATCH_ON(x) ((x) = SYSCALL_DISPATCH_FILTER_BLOCK)
36 #define SYSCALL_DISPATCH_OFF(x) ((x) = SYSCALL_DISPATCH_FILTER_ALLOW)
40 * - dispatch_trigger_sigsys: Verify if PR_SET_SYSCALL_USER_DISPATCH is
41 * able to trigger SIGSYS on a syscall.
43 * - bad_selector: Test that a bad selector value triggers SIGSYS with
46 * - bad_prctl_param: Test that the API correctly rejects invalid
49 * - dispatch_and_return: Test that a syscall is selectively dispatched
50 * to userspace depending on the value of selector.
52 * - disable_dispatch: Test that the PR_SYS_DISPATCH_OFF correctly
53 * disables the dispatcher
55 * - direct_dispatch_range: Test that a syscall within the allowed range
56 * can bypass the dispatcher.
59 TEST_SIGNAL(dispatch_trigger_sigsys
, SIGSYS
)
61 char sel
= SYSCALL_DISPATCH_FILTER_ALLOW
;
68 ret
= prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, 0, 0, &sel
);
70 TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
73 SYSCALL_DISPATCH_ON(sel
);
78 TH_LOG("Unreachable!");
84 char sel
= SYSCALL_DISPATCH_FILTER_ALLOW
;
89 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0, 0, &sel
);
90 ASSERT_EQ(EINVAL
, errno
);
92 /* PR_SYS_DISPATCH_OFF */
93 op
= PR_SYS_DISPATCH_OFF
;
96 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0x1, 0x0, 0);
97 EXPECT_EQ(EINVAL
, errno
);
100 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0x0, 0xff, 0);
101 EXPECT_EQ(EINVAL
, errno
);
104 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0x0, 0x0, &sel
);
105 EXPECT_EQ(EINVAL
, errno
);
107 /* Valid parameter */
109 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0x0, 0x0, 0x0);
112 /* PR_SYS_DISPATCH_ON */
113 op
= PR_SYS_DISPATCH_ON
;
115 /* Dispatcher region is bad (offset > 0 && len == 0) */
116 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0x1, 0x0, &sel
);
117 EXPECT_EQ(EINVAL
, errno
);
118 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, -1L, 0x0, &sel
);
119 EXPECT_EQ(EINVAL
, errno
);
121 /* Invalid selector */
122 prctl(PR_SET_SYSCALL_USER_DISPATCH
, op
, 0x0, 0x1, (void *) -1);
123 ASSERT_EQ(EFAULT
, errno
);
126 * Dispatcher range overflows unsigned long
128 prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, 1, -1L, &sel
);
129 ASSERT_EQ(EINVAL
, errno
) {
130 TH_LOG("Should reject bad syscall range");
134 * Allowed range overflows usigned long
136 prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, -1L, 0x1, &sel
);
137 ASSERT_EQ(EINVAL
, errno
) {
138 TH_LOG("Should reject bad syscall range");
143 * Use global selector for handle_sigsys tests, to avoid passing
144 * selector to signal handler
147 int nr_syscalls_emulated
;
151 static void handle_sigsys(int sig
, siginfo_t
*info
, void *ucontext
)
153 si_code
= info
->si_code
;
154 si_errno
= info
->si_errno
;
156 if (info
->si_syscall
== MAGIC_SYSCALL_1
)
157 nr_syscalls_emulated
++;
159 /* In preparation for sigreturn. */
160 SYSCALL_DISPATCH_OFF(glob_sel
);
163 * The tests for argument handling assume that `syscall(x) == x`. This
164 * is a NOP on x86 because the syscall number is passed in %rax, which
165 * happens to also be the function ABI return register. Other
166 * architectures may need to swizzle the arguments around.
169 /* REG_A7 is not defined in libc headers */
170 # define REG_A7 (REG_A0 + 7)
172 ((ucontext_t
*)ucontext
)->uc_mcontext
.__gregs
[REG_A0
] =
173 ((ucontext_t
*)ucontext
)->uc_mcontext
.__gregs
[REG_A7
];
177 TEST(dispatch_and_return
)
180 struct sigaction act
;
184 nr_syscalls_emulated
= 0;
188 memset(&act
, 0, sizeof(act
));
191 act
.sa_sigaction
= handle_sigsys
;
192 act
.sa_flags
= SA_SIGINFO
;
195 ret
= sigaction(SIGSYS
, &act
, NULL
);
198 /* Make sure selector is good prior to prctl. */
199 SYSCALL_DISPATCH_OFF(glob_sel
);
201 ret
= prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, 0, 0, &glob_sel
);
203 TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
206 /* MAGIC_SYSCALL_1 doesn't exist. */
207 SYSCALL_DISPATCH_OFF(glob_sel
);
208 ret
= syscall(MAGIC_SYSCALL_1
);
210 TH_LOG("Dispatch triggered unexpectedly");
213 /* MAGIC_SYSCALL_1 should be emulated. */
214 nr_syscalls_emulated
= 0;
215 SYSCALL_DISPATCH_ON(glob_sel
);
217 ret
= syscall(MAGIC_SYSCALL_1
);
218 EXPECT_EQ(MAGIC_SYSCALL_1
, ret
) {
219 TH_LOG("Failed to intercept syscall");
221 EXPECT_EQ(1, nr_syscalls_emulated
) {
222 TH_LOG("Failed to emulate syscall");
224 ASSERT_EQ(SYS_USER_DISPATCH
, si_code
) {
225 TH_LOG("Bad si_code in SIGSYS");
227 ASSERT_EQ(0, si_errno
) {
228 TH_LOG("Bad si_errno in SIGSYS");
232 TEST_SIGNAL(bad_selector
, SIGSYS
)
235 struct sigaction act
;
239 glob_sel
= SYSCALL_DISPATCH_FILTER_ALLOW
;
240 nr_syscalls_emulated
= 0;
244 memset(&act
, 0, sizeof(act
));
247 act
.sa_sigaction
= handle_sigsys
;
248 act
.sa_flags
= SA_SIGINFO
;
251 ret
= sigaction(SIGSYS
, &act
, NULL
);
254 /* Make sure selector is good prior to prctl. */
255 SYSCALL_DISPATCH_OFF(glob_sel
);
257 ret
= prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, 0, 0, &glob_sel
);
259 TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
266 /* Even though it is ready to catch SIGSYS, the signal is
267 * supposed to be uncatchable.
271 TH_LOG("Unreachable!");
275 TEST(disable_dispatch
)
281 ret
= prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, 0, 0, &sel
);
283 TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
286 /* MAGIC_SYSCALL_1 doesn't exist. */
287 SYSCALL_DISPATCH_OFF(glob_sel
);
289 ret
= prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_OFF
, 0, 0, 0);
291 TH_LOG("Failed to unset syscall user dispatch");
294 /* Shouldn't have any effect... */
295 SYSCALL_DISPATCH_ON(glob_sel
);
297 ret
= syscall(__NR_sysinfo
, &info
);
299 TH_LOG("Dispatch triggered unexpectedly");
303 TEST(direct_dispatch_range
)
307 char sel
= SYSCALL_DISPATCH_FILTER_ALLOW
;
310 * Instead of calculating libc addresses; allow the entire
311 * memory map and lock the selector.
313 ret
= prctl(PR_SET_SYSCALL_USER_DISPATCH
, PR_SYS_DISPATCH_ON
, 0, -1L, &sel
);
315 TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
318 SYSCALL_DISPATCH_ON(sel
);
320 ret
= sysinfo(&info
);
322 TH_LOG("Dispatch triggered unexpectedly");