sud_test.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2020 Collabora Ltd.
  4. *
  5. * Test code for syscall user dispatch
  6. */
  7. #define _GNU_SOURCE
  8. #include <sys/prctl.h>
  9. #include <sys/sysinfo.h>
  10. #include <sys/syscall.h>
  11. #include <signal.h>
  12. #include <asm/unistd.h>
  13. #include "../kselftest_harness.h"
  14. #ifndef PR_SET_SYSCALL_USER_DISPATCH
  15. # define PR_SET_SYSCALL_USER_DISPATCH 59
  16. # define PR_SYS_DISPATCH_OFF 0
  17. # define PR_SYS_DISPATCH_ON 1
  18. # define SYSCALL_DISPATCH_FILTER_ALLOW 0
  19. # define SYSCALL_DISPATCH_FILTER_BLOCK 1
  20. #endif
  21. #ifndef SYS_USER_DISPATCH
  22. # define SYS_USER_DISPATCH 2
  23. #endif
  24. #ifdef __NR_syscalls
  25. # define MAGIC_SYSCALL_1 (__NR_syscalls + 1) /* Bad Linux syscall number */
  26. #else
  27. # define MAGIC_SYSCALL_1 (0xff00) /* Bad Linux syscall number */
  28. #endif
  29. #define SYSCALL_DISPATCH_ON(x) ((x) = SYSCALL_DISPATCH_FILTER_BLOCK)
  30. #define SYSCALL_DISPATCH_OFF(x) ((x) = SYSCALL_DISPATCH_FILTER_ALLOW)
  31. /* Test Summary:
  32. *
  33. * - dispatch_trigger_sigsys: Verify if PR_SET_SYSCALL_USER_DISPATCH is
  34. * able to trigger SIGSYS on a syscall.
  35. *
  36. * - bad_selector: Test that a bad selector value triggers SIGSYS with
  37. * si_errno EINVAL.
  38. *
  39. * - bad_prctl_param: Test that the API correctly rejects invalid
  40. * parameters on prctl
  41. *
  42. * - dispatch_and_return: Test that a syscall is selectively dispatched
  43. * to userspace depending on the value of selector.
  44. *
  45. * - disable_dispatch: Test that the PR_SYS_DISPATCH_OFF correctly
  46. * disables the dispatcher
  47. *
  48. * - direct_dispatch_range: Test that a syscall within the allowed range
  49. * can bypass the dispatcher.
  50. */
  51. TEST_SIGNAL(dispatch_trigger_sigsys, SIGSYS)
  52. {
  53. char sel = SYSCALL_DISPATCH_FILTER_ALLOW;
  54. struct sysinfo info;
  55. int ret;
  56. ret = sysinfo(&info);
  57. ASSERT_EQ(0, ret);
  58. ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &sel);
  59. ASSERT_EQ(0, ret) {
  60. TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
  61. }
  62. SYSCALL_DISPATCH_ON(sel);
  63. sysinfo(&info);
  64. EXPECT_FALSE(true) {
  65. TH_LOG("Unreachable!");
  66. }
  67. }
  68. static void prctl_valid(struct __test_metadata *_metadata,
  69. unsigned long op, unsigned long off,
  70. unsigned long size, void *sel)
  71. {
  72. EXPECT_EQ(0, prctl(PR_SET_SYSCALL_USER_DISPATCH, op, off, size, sel));
  73. }
  74. static void prctl_invalid(struct __test_metadata *_metadata,
  75. unsigned long op, unsigned long off,
  76. unsigned long size, void *sel, int err)
  77. {
  78. EXPECT_EQ(-1, prctl(PR_SET_SYSCALL_USER_DISPATCH, op, off, size, sel));
  79. EXPECT_EQ(err, errno);
  80. }
  81. TEST(bad_prctl_param)
  82. {
  83. char sel = SYSCALL_DISPATCH_FILTER_ALLOW;
  84. int op;
  85. /* Invalid op */
  86. op = -1;
  87. prctl_invalid(_metadata, op, 0, 0, &sel, EINVAL);
  88. /* PR_SYS_DISPATCH_OFF */
  89. op = PR_SYS_DISPATCH_OFF;
  90. /* offset != 0 */
  91. prctl_invalid(_metadata, op, 0x1, 0x0, 0, EINVAL);
  92. /* len != 0 */
  93. prctl_invalid(_metadata, op, 0x0, 0xff, 0, EINVAL);
  94. /* sel != NULL */
  95. prctl_invalid(_metadata, op, 0x0, 0x0, &sel, EINVAL);
  96. /* Valid parameter */
  97. prctl_valid(_metadata, op, 0x0, 0x0, 0x0);
  98. /* PR_SYS_DISPATCH_ON */
  99. op = PR_SYS_DISPATCH_ON;
  100. /* Dispatcher region is bad (offset > 0 && len == 0) */
  101. prctl_invalid(_metadata, op, 0x1, 0x0, &sel, EINVAL);
  102. prctl_invalid(_metadata, op, -1L, 0x0, &sel, EINVAL);
  103. /* Invalid selector */
  104. prctl_invalid(_metadata, op, 0x0, 0x1, (void *) -1, EFAULT);
  105. /*
  106. * Dispatcher range overflows unsigned long
  107. */
  108. prctl_invalid(_metadata, PR_SYS_DISPATCH_ON, 1, -1L, &sel, EINVAL);
  109. /*
  110. * Allowed range overflows usigned long
  111. */
  112. prctl_invalid(_metadata, PR_SYS_DISPATCH_ON, -1L, 0x1, &sel, EINVAL);
  113. }
  114. /*
  115. * Use global selector for handle_sigsys tests, to avoid passing
  116. * selector to signal handler
  117. */
  118. char glob_sel;
  119. int nr_syscalls_emulated;
  120. int si_code;
  121. int si_errno;
  122. static void handle_sigsys(int sig, siginfo_t *info, void *ucontext)
  123. {
  124. si_code = info->si_code;
  125. si_errno = info->si_errno;
  126. if (info->si_syscall == MAGIC_SYSCALL_1)
  127. nr_syscalls_emulated++;
  128. /* In preparation for sigreturn. */
  129. SYSCALL_DISPATCH_OFF(glob_sel);
  130. /*
  131. * The tests for argument handling assume that `syscall(x) == x`. This
  132. * is a NOP on x86 because the syscall number is passed in %rax, which
  133. * happens to also be the function ABI return register. Other
  134. * architectures may need to swizzle the arguments around.
  135. */
  136. #if defined(__riscv)
  137. /* REG_A7 is not defined in libc headers */
  138. # define REG_A7 (REG_A0 + 7)
  139. ((ucontext_t *)ucontext)->uc_mcontext.__gregs[REG_A0] =
  140. ((ucontext_t *)ucontext)->uc_mcontext.__gregs[REG_A7];
  141. #endif
  142. }
  143. TEST(dispatch_and_return)
  144. {
  145. long ret;
  146. struct sigaction act;
  147. sigset_t mask;
  148. glob_sel = 0;
  149. nr_syscalls_emulated = 0;
  150. si_code = 0;
  151. si_errno = 0;
  152. memset(&act, 0, sizeof(act));
  153. sigemptyset(&mask);
  154. act.sa_sigaction = handle_sigsys;
  155. act.sa_flags = SA_SIGINFO;
  156. act.sa_mask = mask;
  157. ret = sigaction(SIGSYS, &act, NULL);
  158. ASSERT_EQ(0, ret);
  159. /* Make sure selector is good prior to prctl. */
  160. SYSCALL_DISPATCH_OFF(glob_sel);
  161. ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &glob_sel);
  162. ASSERT_EQ(0, ret) {
  163. TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
  164. }
  165. /* MAGIC_SYSCALL_1 doesn't exist. */
  166. SYSCALL_DISPATCH_OFF(glob_sel);
  167. ret = syscall(MAGIC_SYSCALL_1);
  168. EXPECT_EQ(-1, ret) {
  169. TH_LOG("Dispatch triggered unexpectedly");
  170. }
  171. /* MAGIC_SYSCALL_1 should be emulated. */
  172. nr_syscalls_emulated = 0;
  173. SYSCALL_DISPATCH_ON(glob_sel);
  174. ret = syscall(MAGIC_SYSCALL_1);
  175. EXPECT_EQ(MAGIC_SYSCALL_1, ret) {
  176. TH_LOG("Failed to intercept syscall");
  177. }
  178. EXPECT_EQ(1, nr_syscalls_emulated) {
  179. TH_LOG("Failed to emulate syscall");
  180. }
  181. ASSERT_EQ(SYS_USER_DISPATCH, si_code) {
  182. TH_LOG("Bad si_code in SIGSYS");
  183. }
  184. ASSERT_EQ(0, si_errno) {
  185. TH_LOG("Bad si_errno in SIGSYS");
  186. }
  187. }
  188. TEST_SIGNAL(bad_selector, SIGSYS)
  189. {
  190. long ret;
  191. struct sigaction act;
  192. sigset_t mask;
  193. struct sysinfo info;
  194. glob_sel = SYSCALL_DISPATCH_FILTER_ALLOW;
  195. nr_syscalls_emulated = 0;
  196. si_code = 0;
  197. si_errno = 0;
  198. memset(&act, 0, sizeof(act));
  199. sigemptyset(&mask);
  200. act.sa_sigaction = handle_sigsys;
  201. act.sa_flags = SA_SIGINFO;
  202. act.sa_mask = mask;
  203. ret = sigaction(SIGSYS, &act, NULL);
  204. ASSERT_EQ(0, ret);
  205. /* Make sure selector is good prior to prctl. */
  206. SYSCALL_DISPATCH_OFF(glob_sel);
  207. ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &glob_sel);
  208. ASSERT_EQ(0, ret) {
  209. TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
  210. }
  211. glob_sel = -1;
  212. sysinfo(&info);
  213. /* Even though it is ready to catch SIGSYS, the signal is
  214. * supposed to be uncatchable.
  215. */
  216. EXPECT_FALSE(true) {
  217. TH_LOG("Unreachable!");
  218. }
  219. }
  220. TEST(disable_dispatch)
  221. {
  222. int ret;
  223. struct sysinfo info;
  224. char sel = 0;
  225. ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, 0, &sel);
  226. ASSERT_EQ(0, ret) {
  227. TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
  228. }
  229. /* MAGIC_SYSCALL_1 doesn't exist. */
  230. SYSCALL_DISPATCH_OFF(glob_sel);
  231. ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_OFF, 0, 0, 0);
  232. EXPECT_EQ(0, ret) {
  233. TH_LOG("Failed to unset syscall user dispatch");
  234. }
  235. /* Shouldn't have any effect... */
  236. SYSCALL_DISPATCH_ON(glob_sel);
  237. ret = syscall(__NR_sysinfo, &info);
  238. EXPECT_EQ(0, ret) {
  239. TH_LOG("Dispatch triggered unexpectedly");
  240. }
  241. }
  242. TEST(direct_dispatch_range)
  243. {
  244. int ret = 0;
  245. struct sysinfo info;
  246. char sel = SYSCALL_DISPATCH_FILTER_ALLOW;
  247. /*
  248. * Instead of calculating libc addresses; allow the entire
  249. * memory map and lock the selector.
  250. */
  251. ret = prctl(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, 0, -1L, &sel);
  252. ASSERT_EQ(0, ret) {
  253. TH_LOG("Kernel does not support CONFIG_SYSCALL_USER_DISPATCH");
  254. }
  255. SYSCALL_DISPATCH_ON(sel);
  256. ret = sysinfo(&info);
  257. ASSERT_EQ(0, ret) {
  258. TH_LOG("Dispatch triggered unexpectedly");
  259. }
  260. }
  261. TEST_HARNESS_MAIN