Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | // SPDX-License-Identifier: GPL-2.0-only /* * Copyright (c) 2020 Collabora Ltd. * * Test code for syscall user dispatch */ #define _GNU_SOURCE #include <sys/prctl.h> #include <sys/sysinfo.h> #include <sys/syscall.h> #include <signal.h> #include <asm/unistd.h> #include "../kselftest_harness.h" #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 SYS_USER_DISPATCH # define SYS_USER_DISPATCH 2 #endif #ifdef __NR_syscalls # define MAGIC_SYSCALL_1 (__NR_syscalls + 1) /* Bad Linux syscall number */ #else # define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */ #endif #define SYSCALL_DISPATCH_ON(x) ((x) = SYSCALL_DISPATCH_FILTER_BLOCK) #define SYSCALL_DISPATCH_OFF(x) ((x) = SYSCALL_DISPATCH_FILTER_ALLOW) /* Test Summary: * * - dispatch_trigger_sigsys: Verify if PR_SET_SYSCALL_USER_DISPATCH is * able to trigger SIGSYS on a syscall. * * - bad_selector: Test that a bad selector value triggers SIGSYS with * si_errno EINVAL. * * - bad_prctl_param: Test that the API correctly rejects invalid * parameters on prctl * * - dispatch_and_return: Test that a syscall is selectively dispatched * to userspace depending on the value of selector. * * - disable_dispatch: Test that the PR_SYS_DISPATCH_OFF correctly * disables the dispatcher * * - direct_dispatch_range: Test that a syscall within the allowed range * can bypass the dispatcher. */ TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS) { char sel = SYSCALL_DISPATCH_FILTER_ALLOW; struct sysinfo info; int ret; ret = sysinfo(&info); ASSERT_EQ(0, ret); ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &sel); ASSERT_EQ(0, ret) { TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH"); } SYSCALL_DISPATCH_ON(sel); sysinfo(&info); EXPECT_FALSE(true) { TH_LOG("Unreachable!"); } } TEST(bad_prctl_param) { char sel = SYSCALL_DISPATCH_FILTER_ALLOW; int op; /* Invalid op */ op = -1; prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0, 0, &sel); ASSERT_EQ(EINVAL, errno); /* 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); /* len != 0 */ prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0xff, 0); EXPECT_EQ(EINVAL, errno); /* sel != NULL */ prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0x0, &sel); EXPECT_EQ(EINVAL, errno); /* Valid parameter */ errno = 0; prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0x0, 0x0); EXPECT_EQ(0, errno); /* 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); /* Invalid selector */ prctl(PR_SET_SYSCALL_USER_DISPATCH, op, 0x0, 0x1, (void *) -1); ASSERT_EQ(EFAULT, errno); /* * 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"); } /* * 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"); } } /* * Use global selector for handle_sigsys tests, to avoid passing * selector to signal handler */ char glob_sel; int nr_syscalls_emulated; int si_code; int si_errno; static void handle_sigsys(int sig, siginfo_t *info, void *ucontext) { si_code = info->si_code; si_errno = info->si_errno; if (info->si_syscall == MAGIC_SYSCALL_1) nr_syscalls_emulated++; /* In preparation for sigreturn. */ SYSCALL_DISPATCH_OFF(glob_sel); } TEST(dispatch_and_return) { 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; ret = sigaction(SIGSYS, &act, NULL); ASSERT_EQ(0, ret); /* 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); ASSERT_EQ(0, ret) { TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH"); } /* MAGIC_SYSCALL_1 doesn't exist. */ SYSCALL_DISPATCH_OFF(glob_sel); ret = syscall(MAGIC_SYSCALL_1); EXPECT_EQ(-1, ret) { TH_LOG("Dispatch triggered unexpectedly"); } /* MAGIC_SYSCALL_1 should be emulated. */ nr_syscalls_emulated = 0; SYSCALL_DISPATCH_ON(glob_sel); ret = syscall(MAGIC_SYSCALL_1); EXPECT_EQ(MAGIC_SYSCALL_1, ret) { TH_LOG("Failed to intercept syscall"); } EXPECT_EQ(1, nr_syscalls_emulated) { TH_LOG("Failed to emulate syscall"); } ASSERT_EQ(SYS_USER_DISPATCH, si_code) { TH_LOG("Bad si_code in SIGSYS"); } ASSERT_EQ(0, si_errno) { TH_LOG("Bad si_errno in SIGSYS"); } } TEST_SIGNAL(bad_selector, SIGSYS) { long ret; struct sigaction act; sigset_t mask; struct sysinfo info; glob_sel = SYSCALL_DISPATCH_FILTER_ALLOW; 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; ret = sigaction(SIGSYS, &act, NULL); ASSERT_EQ(0, ret); /* 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); ASSERT_EQ(0, ret) { TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH"); } glob_sel = -1; sysinfo(&info); /* Even though it is ready to catch SIGSYS, the signal is * supposed to be uncatchable. */ EXPECT_FALSE(true) { TH_LOG("Unreachable!"); } } TEST(disable_dispatch) { int ret; struct sysinfo info; char sel = 0; ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &sel); ASSERT_EQ(0, ret) { TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH"); } /* MAGIC_SYSCALL_1 doesn't exist. */ SYSCALL_DISPATCH_OFF(glob_sel); ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_OFF, 0, 0, 0); EXPECT_EQ(0, ret) { TH_LOG("Failed to unset syscall user dispatch"); } /* Shouldn't have any effect... */ SYSCALL_DISPATCH_ON(glob_sel); ret = syscall(__NR_sysinfo, &info); EXPECT_EQ(0, ret) { TH_LOG("Dispatch triggered unexpectedly"); } } TEST(direct_dispatch_range) { int ret = 0; struct sysinfo info; char sel = SYSCALL_DISPATCH_FILTER_ALLOW; /* * 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); ASSERT_EQ(0, ret) { TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH"); } SYSCALL_DISPATCH_ON(sel); ret = sysinfo(&info); ASSERT_EQ(0, ret) { TH_LOG("Dispatch triggered unexpectedly"); } } TEST_HARNESS_MAIN |