seccomp.c 23 KB

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