seccomp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * linux/kernel/seccomp.c
  3. *
  4. * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
  5. *
  6. * Copyright (C) 2012 Google, Inc.
  7. * Will Drewry <wad@chromium.org>
  8. *
  9. * This defines a simple but solid secure-computing facility.
  10. *
  11. * Mode 1 uses a fixed list of allowed system calls.
  12. * Mode 2 allows user-defined system call filters in the form
  13. * of Berkeley Packet Filters/Linux Socket Filters.
  14. */
  15. #include <linux/atomic.h>
  16. #include <linux/audit.h>
  17. #include <linux/compat.h>
  18. #include <linux/sched.h>
  19. #include <linux/seccomp.h>
  20. #include <linux/slab.h>
  21. #include <linux/syscalls.h>
  22. /* #define SECCOMP_DEBUG 1 */
  23. #ifdef CONFIG_SECCOMP_FILTER
  24. #include <asm/syscall.h>
  25. #include <linux/filter.h>
  26. #include <linux/pid.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/security.h>
  29. #include <linux/tracehook.h>
  30. #include <linux/uaccess.h>
  31. /**
  32. * struct seccomp_filter - container for seccomp BPF programs
  33. *
  34. * @usage: reference count to manage the object lifetime.
  35. * get/put helpers should be used when accessing an instance
  36. * outside of a lifetime-guarded section. In general, this
  37. * is only needed for handling filters shared across tasks.
  38. * @prev: points to a previously installed, or inherited, filter
  39. * @len: the number of instructions in the program
  40. * @insnsi: the BPF program instructions to evaluate
  41. *
  42. * seccomp_filter objects are organized in a tree linked via the @prev
  43. * pointer. For any task, it appears to be a singly-linked list starting
  44. * with current->seccomp.filter, the most recently attached or inherited filter.
  45. * However, multiple filters may share a @prev node, by way of fork(), which
  46. * results in a unidirectional tree existing in memory. This is similar to
  47. * how namespaces work.
  48. *
  49. * seccomp_filter objects should never be modified after being attached
  50. * to a task_struct (other than @usage).
  51. */
  52. struct seccomp_filter {
  53. atomic_t usage;
  54. struct seccomp_filter *prev;
  55. struct bpf_prog *prog;
  56. };
  57. /* Limit any path through the tree to 256KB worth of instructions. */
  58. #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
  59. /*
  60. * Endianness is explicitly ignored and left for BPF program authors to manage
  61. * as per the specific architecture.
  62. */
  63. static void populate_seccomp_data(struct seccomp_data *sd)
  64. {
  65. struct task_struct *task = current;
  66. struct pt_regs *regs = task_pt_regs(task);
  67. unsigned long args[6];
  68. sd->nr = syscall_get_nr(task, regs);
  69. sd->arch = syscall_get_arch();
  70. syscall_get_arguments(task, regs, 0, 6, args);
  71. sd->args[0] = args[0];
  72. sd->args[1] = args[1];
  73. sd->args[2] = args[2];
  74. sd->args[3] = args[3];
  75. sd->args[4] = args[4];
  76. sd->args[5] = args[5];
  77. sd->instruction_pointer = KSTK_EIP(task);
  78. }
  79. /**
  80. * seccomp_check_filter - verify seccomp filter code
  81. * @filter: filter to verify
  82. * @flen: length of filter
  83. *
  84. * Takes a previously checked filter (by bpf_check_classic) and
  85. * redirects all filter code that loads struct sk_buff data
  86. * and related data through seccomp_bpf_load. It also
  87. * enforces length and alignment checking of those loads.
  88. *
  89. * Returns 0 if the rule set is legal or -EINVAL if not.
  90. */
  91. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  92. {
  93. int pc;
  94. for (pc = 0; pc < flen; pc++) {
  95. struct sock_filter *ftest = &filter[pc];
  96. u16 code = ftest->code;
  97. u32 k = ftest->k;
  98. switch (code) {
  99. case BPF_LD | BPF_W | BPF_ABS:
  100. ftest->code = BPF_LDX | BPF_W | BPF_ABS;
  101. /* 32-bit aligned and not out of bounds. */
  102. if (k >= sizeof(struct seccomp_data) || k & 3)
  103. return -EINVAL;
  104. continue;
  105. case BPF_LD | BPF_W | BPF_LEN:
  106. ftest->code = BPF_LD | BPF_IMM;
  107. ftest->k = sizeof(struct seccomp_data);
  108. continue;
  109. case BPF_LDX | BPF_W | BPF_LEN:
  110. ftest->code = BPF_LDX | BPF_IMM;
  111. ftest->k = sizeof(struct seccomp_data);
  112. continue;
  113. /* Explicitly include allowed calls. */
  114. case BPF_RET | BPF_K:
  115. case BPF_RET | BPF_A:
  116. case BPF_ALU | BPF_ADD | BPF_K:
  117. case BPF_ALU | BPF_ADD | BPF_X:
  118. case BPF_ALU | BPF_SUB | BPF_K:
  119. case BPF_ALU | BPF_SUB | BPF_X:
  120. case BPF_ALU | BPF_MUL | BPF_K:
  121. case BPF_ALU | BPF_MUL | BPF_X:
  122. case BPF_ALU | BPF_DIV | BPF_K:
  123. case BPF_ALU | BPF_DIV | BPF_X:
  124. case BPF_ALU | BPF_AND | BPF_K:
  125. case BPF_ALU | BPF_AND | BPF_X:
  126. case BPF_ALU | BPF_OR | BPF_K:
  127. case BPF_ALU | BPF_OR | BPF_X:
  128. case BPF_ALU | BPF_XOR | BPF_K:
  129. case BPF_ALU | BPF_XOR | BPF_X:
  130. case BPF_ALU | BPF_LSH | BPF_K:
  131. case BPF_ALU | BPF_LSH | BPF_X:
  132. case BPF_ALU | BPF_RSH | BPF_K:
  133. case BPF_ALU | BPF_RSH | BPF_X:
  134. case BPF_ALU | BPF_NEG:
  135. case BPF_LD | BPF_IMM:
  136. case BPF_LDX | BPF_IMM:
  137. case BPF_MISC | BPF_TAX:
  138. case BPF_MISC | BPF_TXA:
  139. case BPF_LD | BPF_MEM:
  140. case BPF_LDX | BPF_MEM:
  141. case BPF_ST:
  142. case BPF_STX:
  143. case BPF_JMP | BPF_JA:
  144. case BPF_JMP | BPF_JEQ | BPF_K:
  145. case BPF_JMP | BPF_JEQ | BPF_X:
  146. case BPF_JMP | BPF_JGE | BPF_K:
  147. case BPF_JMP | BPF_JGE | BPF_X:
  148. case BPF_JMP | BPF_JGT | BPF_K:
  149. case BPF_JMP | BPF_JGT | BPF_X:
  150. case BPF_JMP | BPF_JSET | BPF_K:
  151. case BPF_JMP | BPF_JSET | BPF_X:
  152. continue;
  153. default:
  154. return -EINVAL;
  155. }
  156. }
  157. return 0;
  158. }
  159. /**
  160. * seccomp_run_filters - evaluates all seccomp filters against @syscall
  161. * @syscall: number of the current system call
  162. *
  163. * Returns valid seccomp BPF response codes.
  164. */
  165. static u32 seccomp_run_filters(int syscall)
  166. {
  167. struct seccomp_filter *f = ACCESS_ONCE(current->seccomp.filter);
  168. struct seccomp_data sd;
  169. u32 ret = SECCOMP_RET_ALLOW;
  170. /* Ensure unexpected behavior doesn't result in failing open. */
  171. if (unlikely(WARN_ON(f == NULL)))
  172. return SECCOMP_RET_KILL;
  173. /* Make sure cross-thread synced filter points somewhere sane. */
  174. smp_read_barrier_depends();
  175. populate_seccomp_data(&sd);
  176. /*
  177. * All filters in the list are evaluated and the lowest BPF return
  178. * value always takes priority (ignoring the DATA).
  179. */
  180. for (; f; f = f->prev) {
  181. u32 cur_ret = BPF_PROG_RUN(f->prog, (void *)&sd);
  182. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  183. ret = cur_ret;
  184. }
  185. return ret;
  186. }
  187. #endif /* CONFIG_SECCOMP_FILTER */
  188. static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
  189. {
  190. assert_spin_locked(&current->sighand->siglock);
  191. if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
  192. return false;
  193. return true;
  194. }
  195. static inline void seccomp_assign_mode(struct task_struct *task,
  196. unsigned long seccomp_mode)
  197. {
  198. assert_spin_locked(&task->sighand->siglock);
  199. task->seccomp.mode = seccomp_mode;
  200. /*
  201. * Make sure TIF_SECCOMP cannot be set before the mode (and
  202. * filter) is set.
  203. */
  204. smp_mb__before_atomic();
  205. set_tsk_thread_flag(task, TIF_SECCOMP);
  206. }
  207. #ifdef CONFIG_SECCOMP_FILTER
  208. /* Returns 1 if the parent is an ancestor of the child. */
  209. static int is_ancestor(struct seccomp_filter *parent,
  210. struct seccomp_filter *child)
  211. {
  212. /* NULL is the root ancestor. */
  213. if (parent == NULL)
  214. return 1;
  215. for (; child; child = child->prev)
  216. if (child == parent)
  217. return 1;
  218. return 0;
  219. }
  220. /**
  221. * seccomp_can_sync_threads: checks if all threads can be synchronized
  222. *
  223. * Expects sighand and cred_guard_mutex locks to be held.
  224. *
  225. * Returns 0 on success, -ve on error, or the pid of a thread which was
  226. * either not in the correct seccomp mode or it did not have an ancestral
  227. * seccomp filter.
  228. */
  229. static inline pid_t seccomp_can_sync_threads(void)
  230. {
  231. struct task_struct *thread, *caller;
  232. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  233. assert_spin_locked(&current->sighand->siglock);
  234. /* Validate all threads being eligible for synchronization. */
  235. caller = current;
  236. for_each_thread(caller, thread) {
  237. pid_t failed;
  238. /* Skip current, since it is initiating the sync. */
  239. if (thread == caller)
  240. continue;
  241. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
  242. (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
  243. is_ancestor(thread->seccomp.filter,
  244. caller->seccomp.filter)))
  245. continue;
  246. /* Return the first thread that cannot be synchronized. */
  247. failed = task_pid_vnr(thread);
  248. /* If the pid cannot be resolved, then return -ESRCH */
  249. if (unlikely(WARN_ON(failed == 0)))
  250. failed = -ESRCH;
  251. return failed;
  252. }
  253. return 0;
  254. }
  255. /**
  256. * seccomp_sync_threads: sets all threads to use current's filter
  257. *
  258. * Expects sighand and cred_guard_mutex locks to be held, and for
  259. * seccomp_can_sync_threads() to have returned success already
  260. * without dropping the locks.
  261. *
  262. */
  263. static inline void seccomp_sync_threads(void)
  264. {
  265. struct task_struct *thread, *caller;
  266. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  267. assert_spin_locked(&current->sighand->siglock);
  268. /* Synchronize all threads. */
  269. caller = current;
  270. for_each_thread(caller, thread) {
  271. /* Skip current, since it needs no changes. */
  272. if (thread == caller)
  273. continue;
  274. /* Get a task reference for the new leaf node. */
  275. get_seccomp_filter(caller);
  276. /*
  277. * Drop the task reference to the shared ancestor since
  278. * current's path will hold a reference. (This also
  279. * allows a put before the assignment.)
  280. */
  281. put_seccomp_filter(thread);
  282. smp_store_release(&thread->seccomp.filter,
  283. caller->seccomp.filter);
  284. /*
  285. * Opt the other thread into seccomp if needed.
  286. * As threads are considered to be trust-realm
  287. * equivalent (see ptrace_may_access), it is safe to
  288. * allow one thread to transition the other.
  289. */
  290. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED) {
  291. /*
  292. * Don't let an unprivileged task work around
  293. * the no_new_privs restriction by creating
  294. * a thread that sets it up, enters seccomp,
  295. * then dies.
  296. */
  297. if (task_no_new_privs(caller))
  298. task_set_no_new_privs(thread);
  299. seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
  300. }
  301. }
  302. }
  303. /**
  304. * seccomp_prepare_filter: Prepares a seccomp filter for use.
  305. * @fprog: BPF program to install
  306. *
  307. * Returns filter on success or an ERR_PTR on failure.
  308. */
  309. static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
  310. {
  311. struct seccomp_filter *filter;
  312. unsigned long fp_size;
  313. struct sock_filter *fp;
  314. int new_len;
  315. long ret;
  316. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  317. return ERR_PTR(-EINVAL);
  318. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  319. fp_size = fprog->len * sizeof(struct sock_filter);
  320. /*
  321. * Installing a seccomp filter requires that the task has
  322. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  323. * This avoids scenarios where unprivileged tasks can affect the
  324. * behavior of privileged children.
  325. */
  326. if (!task_no_new_privs(current) &&
  327. security_capable_noaudit(current_cred(), current_user_ns(),
  328. CAP_SYS_ADMIN) != 0)
  329. return ERR_PTR(-EACCES);
  330. fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
  331. if (!fp)
  332. return ERR_PTR(-ENOMEM);
  333. /* Copy the instructions from fprog. */
  334. ret = -EFAULT;
  335. if (copy_from_user(fp, fprog->filter, fp_size))
  336. goto free_prog;
  337. /* Check and rewrite the fprog via the skb checker */
  338. ret = bpf_check_classic(fp, fprog->len);
  339. if (ret)
  340. goto free_prog;
  341. /* Check and rewrite the fprog for seccomp use */
  342. ret = seccomp_check_filter(fp, fprog->len);
  343. if (ret)
  344. goto free_prog;
  345. /* Convert 'sock_filter' insns to 'bpf_insn' insns */
  346. ret = bpf_convert_filter(fp, fprog->len, NULL, &new_len);
  347. if (ret)
  348. goto free_prog;
  349. /* Allocate a new seccomp_filter */
  350. ret = -ENOMEM;
  351. filter = kzalloc(sizeof(struct seccomp_filter),
  352. GFP_KERNEL|__GFP_NOWARN);
  353. if (!filter)
  354. goto free_prog;
  355. filter->prog = kzalloc(bpf_prog_size(new_len),
  356. GFP_KERNEL|__GFP_NOWARN);
  357. if (!filter->prog)
  358. goto free_filter;
  359. ret = bpf_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len);
  360. if (ret)
  361. goto free_filter_prog;
  362. kfree(fp);
  363. atomic_set(&filter->usage, 1);
  364. filter->prog->len = new_len;
  365. bpf_prog_select_runtime(filter->prog);
  366. return filter;
  367. free_filter_prog:
  368. kfree(filter->prog);
  369. free_filter:
  370. kfree(filter);
  371. free_prog:
  372. kfree(fp);
  373. return ERR_PTR(ret);
  374. }
  375. /**
  376. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  377. * @user_filter: pointer to the user data containing a sock_fprog.
  378. *
  379. * Returns 0 on success and non-zero otherwise.
  380. */
  381. static struct seccomp_filter *
  382. seccomp_prepare_user_filter(const char __user *user_filter)
  383. {
  384. struct sock_fprog fprog;
  385. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  386. #ifdef CONFIG_COMPAT
  387. if (is_compat_task()) {
  388. struct compat_sock_fprog fprog32;
  389. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  390. goto out;
  391. fprog.len = fprog32.len;
  392. fprog.filter = compat_ptr(fprog32.filter);
  393. } else /* falls through to the if below. */
  394. #endif
  395. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  396. goto out;
  397. filter = seccomp_prepare_filter(&fprog);
  398. out:
  399. return filter;
  400. }
  401. /**
  402. * seccomp_attach_filter: validate and attach filter
  403. * @flags: flags to change filter behavior
  404. * @filter: seccomp filter to add to the current process
  405. *
  406. * Caller must be holding current->sighand->siglock lock.
  407. *
  408. * Returns 0 on success, -ve on error.
  409. */
  410. static long seccomp_attach_filter(unsigned int flags,
  411. struct seccomp_filter *filter)
  412. {
  413. unsigned long total_insns;
  414. struct seccomp_filter *walker;
  415. assert_spin_locked(&current->sighand->siglock);
  416. /* Validate resulting filter length. */
  417. total_insns = filter->prog->len;
  418. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  419. total_insns += walker->prog->len + 4; /* 4 instr penalty */
  420. if (total_insns > MAX_INSNS_PER_PATH)
  421. return -ENOMEM;
  422. /* If thread sync has been requested, check that it is possible. */
  423. if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
  424. int ret;
  425. ret = seccomp_can_sync_threads();
  426. if (ret)
  427. return ret;
  428. }
  429. /*
  430. * If there is an existing filter, make it the prev and don't drop its
  431. * task reference.
  432. */
  433. filter->prev = current->seccomp.filter;
  434. current->seccomp.filter = filter;
  435. /* Now that the new filter is in place, synchronize to all threads. */
  436. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  437. seccomp_sync_threads();
  438. return 0;
  439. }
  440. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  441. void get_seccomp_filter(struct task_struct *tsk)
  442. {
  443. struct seccomp_filter *orig = tsk->seccomp.filter;
  444. if (!orig)
  445. return;
  446. /* Reference count is bounded by the number of total processes. */
  447. atomic_inc(&orig->usage);
  448. }
  449. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  450. {
  451. if (filter) {
  452. bpf_prog_free(filter->prog);
  453. kfree(filter);
  454. }
  455. }
  456. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  457. void put_seccomp_filter(struct task_struct *tsk)
  458. {
  459. struct seccomp_filter *orig = tsk->seccomp.filter;
  460. /* Clean up single-reference branches iteratively. */
  461. while (orig && atomic_dec_and_test(&orig->usage)) {
  462. struct seccomp_filter *freeme = orig;
  463. orig = orig->prev;
  464. seccomp_filter_free(freeme);
  465. }
  466. }
  467. /**
  468. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  469. * @syscall: syscall number to send to userland
  470. * @reason: filter-supplied reason code to send to userland (via si_errno)
  471. *
  472. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  473. */
  474. static void seccomp_send_sigsys(int syscall, int reason)
  475. {
  476. struct siginfo info;
  477. memset(&info, 0, sizeof(info));
  478. info.si_signo = SIGSYS;
  479. info.si_code = SYS_SECCOMP;
  480. info.si_call_addr = (void __user *)KSTK_EIP(current);
  481. info.si_errno = reason;
  482. info.si_arch = syscall_get_arch();
  483. info.si_syscall = syscall;
  484. force_sig_info(SIGSYS, &info, current);
  485. }
  486. #endif /* CONFIG_SECCOMP_FILTER */
  487. /*
  488. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  489. * To be fully secure this must be combined with rlimit
  490. * to limit the stack allocations too.
  491. */
  492. static int mode1_syscalls[] = {
  493. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  494. 0, /* null terminated */
  495. };
  496. #ifdef CONFIG_COMPAT
  497. static int mode1_syscalls_32[] = {
  498. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  499. 0, /* null terminated */
  500. };
  501. #endif
  502. int __secure_computing(int this_syscall)
  503. {
  504. int exit_sig = 0;
  505. int *syscall;
  506. u32 ret;
  507. /*
  508. * Make sure that any changes to mode from another thread have
  509. * been seen after TIF_SECCOMP was seen.
  510. */
  511. rmb();
  512. switch (current->seccomp.mode) {
  513. case SECCOMP_MODE_STRICT:
  514. syscall = mode1_syscalls;
  515. #ifdef CONFIG_COMPAT
  516. if (is_compat_task())
  517. syscall = mode1_syscalls_32;
  518. #endif
  519. do {
  520. if (*syscall == this_syscall)
  521. return 0;
  522. } while (*++syscall);
  523. exit_sig = SIGKILL;
  524. ret = SECCOMP_RET_KILL;
  525. break;
  526. #ifdef CONFIG_SECCOMP_FILTER
  527. case SECCOMP_MODE_FILTER: {
  528. int data;
  529. struct pt_regs *regs = task_pt_regs(current);
  530. ret = seccomp_run_filters(this_syscall);
  531. data = ret & SECCOMP_RET_DATA;
  532. ret &= SECCOMP_RET_ACTION;
  533. switch (ret) {
  534. case SECCOMP_RET_ERRNO:
  535. /* Set the low-order 16-bits as a errno. */
  536. syscall_set_return_value(current, regs,
  537. -data, 0);
  538. goto skip;
  539. case SECCOMP_RET_TRAP:
  540. /* Show the handler the original registers. */
  541. syscall_rollback(current, regs);
  542. /* Let the filter pass back 16 bits of data. */
  543. seccomp_send_sigsys(this_syscall, data);
  544. goto skip;
  545. case SECCOMP_RET_TRACE:
  546. /* Skip these calls if there is no tracer. */
  547. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  548. syscall_set_return_value(current, regs,
  549. -ENOSYS, 0);
  550. goto skip;
  551. }
  552. /* Allow the BPF to provide the event message */
  553. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  554. /*
  555. * The delivery of a fatal signal during event
  556. * notification may silently skip tracer notification.
  557. * Terminating the task now avoids executing a system
  558. * call that may not be intended.
  559. */
  560. if (fatal_signal_pending(current))
  561. break;
  562. if (syscall_get_nr(current, regs) < 0)
  563. goto skip; /* Explicit request to skip. */
  564. return 0;
  565. case SECCOMP_RET_ALLOW:
  566. return 0;
  567. case SECCOMP_RET_KILL:
  568. default:
  569. break;
  570. }
  571. exit_sig = SIGSYS;
  572. break;
  573. }
  574. #endif
  575. default:
  576. BUG();
  577. }
  578. #ifdef SECCOMP_DEBUG
  579. dump_stack();
  580. #endif
  581. audit_seccomp(this_syscall, exit_sig, ret);
  582. do_exit(exit_sig);
  583. #ifdef CONFIG_SECCOMP_FILTER
  584. skip:
  585. audit_seccomp(this_syscall, exit_sig, ret);
  586. #endif
  587. return -1;
  588. }
  589. long prctl_get_seccomp(void)
  590. {
  591. return current->seccomp.mode;
  592. }
  593. /**
  594. * seccomp_set_mode_strict: internal function for setting strict seccomp
  595. *
  596. * Once current->seccomp.mode is non-zero, it may not be changed.
  597. *
  598. * Returns 0 on success or -EINVAL on failure.
  599. */
  600. static long seccomp_set_mode_strict(void)
  601. {
  602. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  603. long ret = -EINVAL;
  604. spin_lock_irq(&current->sighand->siglock);
  605. if (!seccomp_may_assign_mode(seccomp_mode))
  606. goto out;
  607. #ifdef TIF_NOTSC
  608. disable_TSC();
  609. #endif
  610. seccomp_assign_mode(current, seccomp_mode);
  611. ret = 0;
  612. out:
  613. spin_unlock_irq(&current->sighand->siglock);
  614. return ret;
  615. }
  616. #ifdef CONFIG_SECCOMP_FILTER
  617. /**
  618. * seccomp_set_mode_filter: internal function for setting seccomp filter
  619. * @flags: flags to change filter behavior
  620. * @filter: struct sock_fprog containing filter
  621. *
  622. * This function may be called repeatedly to install additional filters.
  623. * Every filter successfully installed will be evaluated (in reverse order)
  624. * for each system call the task makes.
  625. *
  626. * Once current->seccomp.mode is non-zero, it may not be changed.
  627. *
  628. * Returns 0 on success or -EINVAL on failure.
  629. */
  630. static long seccomp_set_mode_filter(unsigned int flags,
  631. const char __user *filter)
  632. {
  633. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  634. struct seccomp_filter *prepared = NULL;
  635. long ret = -EINVAL;
  636. /* Validate flags. */
  637. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  638. return -EINVAL;
  639. /* Prepare the new filter before holding any locks. */
  640. prepared = seccomp_prepare_user_filter(filter);
  641. if (IS_ERR(prepared))
  642. return PTR_ERR(prepared);
  643. /*
  644. * Make sure we cannot change seccomp or nnp state via TSYNC
  645. * while another thread is in the middle of calling exec.
  646. */
  647. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  648. mutex_lock_killable(&current->signal->cred_guard_mutex))
  649. goto out_free;
  650. spin_lock_irq(&current->sighand->siglock);
  651. if (!seccomp_may_assign_mode(seccomp_mode))
  652. goto out;
  653. ret = seccomp_attach_filter(flags, prepared);
  654. if (ret)
  655. goto out;
  656. /* Do not free the successfully attached filter. */
  657. prepared = NULL;
  658. seccomp_assign_mode(current, seccomp_mode);
  659. out:
  660. spin_unlock_irq(&current->sighand->siglock);
  661. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  662. mutex_unlock(&current->signal->cred_guard_mutex);
  663. out_free:
  664. seccomp_filter_free(prepared);
  665. return ret;
  666. }
  667. #else
  668. static inline long seccomp_set_mode_filter(unsigned int flags,
  669. const char __user *filter)
  670. {
  671. return -EINVAL;
  672. }
  673. #endif
  674. /* Common entry point for both prctl and syscall. */
  675. static long do_seccomp(unsigned int op, unsigned int flags,
  676. const char __user *uargs)
  677. {
  678. switch (op) {
  679. case SECCOMP_SET_MODE_STRICT:
  680. if (flags != 0 || uargs != NULL)
  681. return -EINVAL;
  682. return seccomp_set_mode_strict();
  683. case SECCOMP_SET_MODE_FILTER:
  684. return seccomp_set_mode_filter(flags, uargs);
  685. default:
  686. return -EINVAL;
  687. }
  688. }
  689. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  690. const char __user *, uargs)
  691. {
  692. return do_seccomp(op, flags, uargs);
  693. }
  694. /**
  695. * prctl_set_seccomp: configures current->seccomp.mode
  696. * @seccomp_mode: requested mode to use
  697. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  698. *
  699. * Returns 0 on success or -EINVAL on failure.
  700. */
  701. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  702. {
  703. unsigned int op;
  704. char __user *uargs;
  705. switch (seccomp_mode) {
  706. case SECCOMP_MODE_STRICT:
  707. op = SECCOMP_SET_MODE_STRICT;
  708. /*
  709. * Setting strict mode through prctl always ignored filter,
  710. * so make sure it is always NULL here to pass the internal
  711. * check in do_seccomp().
  712. */
  713. uargs = NULL;
  714. break;
  715. case SECCOMP_MODE_FILTER:
  716. op = SECCOMP_SET_MODE_FILTER;
  717. uargs = filter;
  718. break;
  719. default:
  720. return -EINVAL;
  721. }
  722. /* prctl interface doesn't have flags, so they are always zero. */
  723. return do_seccomp(op, 0, uargs);
  724. }