seccomp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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 *filter;
  316. unsigned long fp_size;
  317. struct sock_filter *fp;
  318. int new_len;
  319. long ret;
  320. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  321. return ERR_PTR(-EINVAL);
  322. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  323. fp_size = fprog->len * sizeof(struct sock_filter);
  324. /*
  325. * Installing a seccomp filter requires that the task has
  326. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  327. * This avoids scenarios where unprivileged tasks can affect the
  328. * behavior of privileged children.
  329. */
  330. if (!task_no_new_privs(current) &&
  331. security_capable_noaudit(current_cred(), current_user_ns(),
  332. CAP_SYS_ADMIN) != 0)
  333. return ERR_PTR(-EACCES);
  334. fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
  335. if (!fp)
  336. return ERR_PTR(-ENOMEM);
  337. /* Copy the instructions from fprog. */
  338. ret = -EFAULT;
  339. if (copy_from_user(fp, fprog->filter, fp_size))
  340. goto free_prog;
  341. /* Check and rewrite the fprog via the skb checker */
  342. ret = bpf_check_classic(fp, fprog->len);
  343. if (ret)
  344. goto free_prog;
  345. /* Check and rewrite the fprog for seccomp use */
  346. ret = seccomp_check_filter(fp, fprog->len);
  347. if (ret)
  348. goto free_prog;
  349. /* Convert 'sock_filter' insns to 'bpf_insn' insns */
  350. ret = bpf_convert_filter(fp, fprog->len, NULL, &new_len);
  351. if (ret)
  352. goto free_prog;
  353. /* Allocate a new seccomp_filter */
  354. ret = -ENOMEM;
  355. filter = kzalloc(sizeof(struct seccomp_filter),
  356. GFP_KERNEL|__GFP_NOWARN);
  357. if (!filter)
  358. goto free_prog;
  359. filter->prog = kzalloc(bpf_prog_size(new_len),
  360. GFP_KERNEL|__GFP_NOWARN);
  361. if (!filter->prog)
  362. goto free_filter;
  363. ret = bpf_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len);
  364. if (ret)
  365. goto free_filter_prog;
  366. kfree(fp);
  367. atomic_set(&filter->usage, 1);
  368. filter->prog->len = new_len;
  369. bpf_prog_select_runtime(filter->prog);
  370. return filter;
  371. free_filter_prog:
  372. kfree(filter->prog);
  373. free_filter:
  374. kfree(filter);
  375. free_prog:
  376. kfree(fp);
  377. return ERR_PTR(ret);
  378. }
  379. /**
  380. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  381. * @user_filter: pointer to the user data containing a sock_fprog.
  382. *
  383. * Returns 0 on success and non-zero otherwise.
  384. */
  385. static struct seccomp_filter *
  386. seccomp_prepare_user_filter(const char __user *user_filter)
  387. {
  388. struct sock_fprog fprog;
  389. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  390. #ifdef CONFIG_COMPAT
  391. if (is_compat_task()) {
  392. struct compat_sock_fprog fprog32;
  393. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  394. goto out;
  395. fprog.len = fprog32.len;
  396. fprog.filter = compat_ptr(fprog32.filter);
  397. } else /* falls through to the if below. */
  398. #endif
  399. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  400. goto out;
  401. filter = seccomp_prepare_filter(&fprog);
  402. out:
  403. return filter;
  404. }
  405. /**
  406. * seccomp_attach_filter: validate and attach filter
  407. * @flags: flags to change filter behavior
  408. * @filter: seccomp filter to add to the current process
  409. *
  410. * Caller must be holding current->sighand->siglock lock.
  411. *
  412. * Returns 0 on success, -ve on error.
  413. */
  414. static long seccomp_attach_filter(unsigned int flags,
  415. struct seccomp_filter *filter)
  416. {
  417. unsigned long total_insns;
  418. struct seccomp_filter *walker;
  419. assert_spin_locked(&current->sighand->siglock);
  420. /* Validate resulting filter length. */
  421. total_insns = filter->prog->len;
  422. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  423. total_insns += walker->prog->len + 4; /* 4 instr penalty */
  424. if (total_insns > MAX_INSNS_PER_PATH)
  425. return -ENOMEM;
  426. /* If thread sync has been requested, check that it is possible. */
  427. if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
  428. int ret;
  429. ret = seccomp_can_sync_threads();
  430. if (ret)
  431. return ret;
  432. }
  433. /*
  434. * If there is an existing filter, make it the prev and don't drop its
  435. * task reference.
  436. */
  437. filter->prev = current->seccomp.filter;
  438. current->seccomp.filter = filter;
  439. /* Now that the new filter is in place, synchronize to all threads. */
  440. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  441. seccomp_sync_threads();
  442. return 0;
  443. }
  444. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  445. void get_seccomp_filter(struct task_struct *tsk)
  446. {
  447. struct seccomp_filter *orig = tsk->seccomp.filter;
  448. if (!orig)
  449. return;
  450. /* Reference count is bounded by the number of total processes. */
  451. atomic_inc(&orig->usage);
  452. }
  453. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  454. {
  455. if (filter) {
  456. bpf_prog_free(filter->prog);
  457. kfree(filter);
  458. }
  459. }
  460. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  461. void put_seccomp_filter(struct task_struct *tsk)
  462. {
  463. struct seccomp_filter *orig = tsk->seccomp.filter;
  464. /* Clean up single-reference branches iteratively. */
  465. while (orig && atomic_dec_and_test(&orig->usage)) {
  466. struct seccomp_filter *freeme = orig;
  467. orig = orig->prev;
  468. seccomp_filter_free(freeme);
  469. }
  470. }
  471. /**
  472. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  473. * @syscall: syscall number to send to userland
  474. * @reason: filter-supplied reason code to send to userland (via si_errno)
  475. *
  476. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  477. */
  478. static void seccomp_send_sigsys(int syscall, int reason)
  479. {
  480. struct siginfo info;
  481. memset(&info, 0, sizeof(info));
  482. info.si_signo = SIGSYS;
  483. info.si_code = SYS_SECCOMP;
  484. info.si_call_addr = (void __user *)KSTK_EIP(current);
  485. info.si_errno = reason;
  486. info.si_arch = syscall_get_arch();
  487. info.si_syscall = syscall;
  488. force_sig_info(SIGSYS, &info, current);
  489. }
  490. #endif /* CONFIG_SECCOMP_FILTER */
  491. /*
  492. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  493. * To be fully secure this must be combined with rlimit
  494. * to limit the stack allocations too.
  495. */
  496. static int mode1_syscalls[] = {
  497. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  498. 0, /* null terminated */
  499. };
  500. #ifdef CONFIG_COMPAT
  501. static int mode1_syscalls_32[] = {
  502. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  503. 0, /* null terminated */
  504. };
  505. #endif
  506. static void __secure_computing_strict(int this_syscall)
  507. {
  508. int *syscall_whitelist = mode1_syscalls;
  509. #ifdef CONFIG_COMPAT
  510. if (is_compat_task())
  511. syscall_whitelist = mode1_syscalls_32;
  512. #endif
  513. do {
  514. if (*syscall_whitelist == this_syscall)
  515. return;
  516. } while (*++syscall_whitelist);
  517. #ifdef SECCOMP_DEBUG
  518. dump_stack();
  519. #endif
  520. audit_seccomp(this_syscall, SIGKILL, SECCOMP_RET_KILL);
  521. do_exit(SIGKILL);
  522. }
  523. #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  524. void secure_computing_strict(int this_syscall)
  525. {
  526. int mode = current->seccomp.mode;
  527. if (mode == 0)
  528. return;
  529. else if (mode == SECCOMP_MODE_STRICT)
  530. __secure_computing_strict(this_syscall);
  531. else
  532. BUG();
  533. }
  534. #else
  535. int __secure_computing(void)
  536. {
  537. u32 phase1_result = seccomp_phase1(NULL);
  538. if (likely(phase1_result == SECCOMP_PHASE1_OK))
  539. return 0;
  540. else if (likely(phase1_result == SECCOMP_PHASE1_SKIP))
  541. return -1;
  542. else
  543. return seccomp_phase2(phase1_result);
  544. }
  545. #ifdef CONFIG_SECCOMP_FILTER
  546. static u32 __seccomp_phase1_filter(int this_syscall, struct seccomp_data *sd)
  547. {
  548. u32 filter_ret, action;
  549. int data;
  550. /*
  551. * Make sure that any changes to mode from another thread have
  552. * been seen after TIF_SECCOMP was seen.
  553. */
  554. rmb();
  555. filter_ret = seccomp_run_filters(sd);
  556. data = filter_ret & SECCOMP_RET_DATA;
  557. action = filter_ret & SECCOMP_RET_ACTION;
  558. switch (action) {
  559. case SECCOMP_RET_ERRNO:
  560. /* Set the low-order 16-bits as a errno. */
  561. syscall_set_return_value(current, task_pt_regs(current),
  562. -data, 0);
  563. goto skip;
  564. case SECCOMP_RET_TRAP:
  565. /* Show the handler the original registers. */
  566. syscall_rollback(current, task_pt_regs(current));
  567. /* Let the filter pass back 16 bits of data. */
  568. seccomp_send_sigsys(this_syscall, data);
  569. goto skip;
  570. case SECCOMP_RET_TRACE:
  571. return filter_ret; /* Save the rest for phase 2. */
  572. case SECCOMP_RET_ALLOW:
  573. return SECCOMP_PHASE1_OK;
  574. case SECCOMP_RET_KILL:
  575. default:
  576. audit_seccomp(this_syscall, SIGSYS, action);
  577. do_exit(SIGSYS);
  578. }
  579. unreachable();
  580. skip:
  581. audit_seccomp(this_syscall, 0, action);
  582. return SECCOMP_PHASE1_SKIP;
  583. }
  584. #endif
  585. /**
  586. * seccomp_phase1() - run fast path seccomp checks on the current syscall
  587. * @arg sd: The seccomp_data or NULL
  588. *
  589. * This only reads pt_regs via the syscall_xyz helpers. The only change
  590. * it will make to pt_regs is via syscall_set_return_value, and it will
  591. * only do that if it returns SECCOMP_PHASE1_SKIP.
  592. *
  593. * If sd is provided, it will not read pt_regs at all.
  594. *
  595. * It may also call do_exit or force a signal; these actions must be
  596. * safe.
  597. *
  598. * If it returns SECCOMP_PHASE1_OK, the syscall passes checks and should
  599. * be processed normally.
  600. *
  601. * If it returns SECCOMP_PHASE1_SKIP, then the syscall should not be
  602. * invoked. In this case, seccomp_phase1 will have set the return value
  603. * using syscall_set_return_value.
  604. *
  605. * If it returns anything else, then the return value should be passed
  606. * to seccomp_phase2 from a context in which ptrace hooks are safe.
  607. */
  608. u32 seccomp_phase1(struct seccomp_data *sd)
  609. {
  610. int mode = current->seccomp.mode;
  611. int this_syscall = sd ? sd->nr :
  612. syscall_get_nr(current, task_pt_regs(current));
  613. switch (mode) {
  614. case SECCOMP_MODE_STRICT:
  615. __secure_computing_strict(this_syscall); /* may call do_exit */
  616. return SECCOMP_PHASE1_OK;
  617. #ifdef CONFIG_SECCOMP_FILTER
  618. case SECCOMP_MODE_FILTER:
  619. return __seccomp_phase1_filter(this_syscall, sd);
  620. #endif
  621. default:
  622. BUG();
  623. }
  624. }
  625. /**
  626. * seccomp_phase2() - finish slow path seccomp work for the current syscall
  627. * @phase1_result: The return value from seccomp_phase1()
  628. *
  629. * This must be called from a context in which ptrace hooks can be used.
  630. *
  631. * Returns 0 if the syscall should be processed or -1 to skip the syscall.
  632. */
  633. int seccomp_phase2(u32 phase1_result)
  634. {
  635. struct pt_regs *regs = task_pt_regs(current);
  636. u32 action = phase1_result & SECCOMP_RET_ACTION;
  637. int data = phase1_result & SECCOMP_RET_DATA;
  638. BUG_ON(action != SECCOMP_RET_TRACE);
  639. audit_seccomp(syscall_get_nr(current, regs), 0, action);
  640. /* Skip these calls if there is no tracer. */
  641. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  642. syscall_set_return_value(current, regs,
  643. -ENOSYS, 0);
  644. return -1;
  645. }
  646. /* Allow the BPF to provide the event message */
  647. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  648. /*
  649. * The delivery of a fatal signal during event
  650. * notification may silently skip tracer notification.
  651. * Terminating the task now avoids executing a system
  652. * call that may not be intended.
  653. */
  654. if (fatal_signal_pending(current))
  655. do_exit(SIGSYS);
  656. if (syscall_get_nr(current, regs) < 0)
  657. return -1; /* Explicit request to skip. */
  658. return 0;
  659. }
  660. #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
  661. long prctl_get_seccomp(void)
  662. {
  663. return current->seccomp.mode;
  664. }
  665. /**
  666. * seccomp_set_mode_strict: internal function for setting strict seccomp
  667. *
  668. * Once current->seccomp.mode is non-zero, it may not be changed.
  669. *
  670. * Returns 0 on success or -EINVAL on failure.
  671. */
  672. static long seccomp_set_mode_strict(void)
  673. {
  674. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  675. long ret = -EINVAL;
  676. spin_lock_irq(&current->sighand->siglock);
  677. if (!seccomp_may_assign_mode(seccomp_mode))
  678. goto out;
  679. #ifdef TIF_NOTSC
  680. disable_TSC();
  681. #endif
  682. seccomp_assign_mode(current, seccomp_mode);
  683. ret = 0;
  684. out:
  685. spin_unlock_irq(&current->sighand->siglock);
  686. return ret;
  687. }
  688. #ifdef CONFIG_SECCOMP_FILTER
  689. /**
  690. * seccomp_set_mode_filter: internal function for setting seccomp filter
  691. * @flags: flags to change filter behavior
  692. * @filter: struct sock_fprog containing filter
  693. *
  694. * This function may be called repeatedly to install additional filters.
  695. * Every filter successfully installed will be evaluated (in reverse order)
  696. * for each system call the task makes.
  697. *
  698. * Once current->seccomp.mode is non-zero, it may not be changed.
  699. *
  700. * Returns 0 on success or -EINVAL on failure.
  701. */
  702. static long seccomp_set_mode_filter(unsigned int flags,
  703. const char __user *filter)
  704. {
  705. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  706. struct seccomp_filter *prepared = NULL;
  707. long ret = -EINVAL;
  708. /* Validate flags. */
  709. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  710. return -EINVAL;
  711. /* Prepare the new filter before holding any locks. */
  712. prepared = seccomp_prepare_user_filter(filter);
  713. if (IS_ERR(prepared))
  714. return PTR_ERR(prepared);
  715. /*
  716. * Make sure we cannot change seccomp or nnp state via TSYNC
  717. * while another thread is in the middle of calling exec.
  718. */
  719. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  720. mutex_lock_killable(&current->signal->cred_guard_mutex))
  721. goto out_free;
  722. spin_lock_irq(&current->sighand->siglock);
  723. if (!seccomp_may_assign_mode(seccomp_mode))
  724. goto out;
  725. ret = seccomp_attach_filter(flags, prepared);
  726. if (ret)
  727. goto out;
  728. /* Do not free the successfully attached filter. */
  729. prepared = NULL;
  730. seccomp_assign_mode(current, seccomp_mode);
  731. out:
  732. spin_unlock_irq(&current->sighand->siglock);
  733. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  734. mutex_unlock(&current->signal->cred_guard_mutex);
  735. out_free:
  736. seccomp_filter_free(prepared);
  737. return ret;
  738. }
  739. #else
  740. static inline long seccomp_set_mode_filter(unsigned int flags,
  741. const char __user *filter)
  742. {
  743. return -EINVAL;
  744. }
  745. #endif
  746. /* Common entry point for both prctl and syscall. */
  747. static long do_seccomp(unsigned int op, unsigned int flags,
  748. const char __user *uargs)
  749. {
  750. switch (op) {
  751. case SECCOMP_SET_MODE_STRICT:
  752. if (flags != 0 || uargs != NULL)
  753. return -EINVAL;
  754. return seccomp_set_mode_strict();
  755. case SECCOMP_SET_MODE_FILTER:
  756. return seccomp_set_mode_filter(flags, uargs);
  757. default:
  758. return -EINVAL;
  759. }
  760. }
  761. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  762. const char __user *, uargs)
  763. {
  764. return do_seccomp(op, flags, uargs);
  765. }
  766. /**
  767. * prctl_set_seccomp: configures current->seccomp.mode
  768. * @seccomp_mode: requested mode to use
  769. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  770. *
  771. * Returns 0 on success or -EINVAL on failure.
  772. */
  773. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  774. {
  775. unsigned int op;
  776. char __user *uargs;
  777. switch (seccomp_mode) {
  778. case SECCOMP_MODE_STRICT:
  779. op = SECCOMP_SET_MODE_STRICT;
  780. /*
  781. * Setting strict mode through prctl always ignored filter,
  782. * so make sure it is always NULL here to pass the internal
  783. * check in do_seccomp().
  784. */
  785. uargs = NULL;
  786. break;
  787. case SECCOMP_MODE_FILTER:
  788. op = SECCOMP_SET_MODE_FILTER;
  789. uargs = filter;
  790. break;
  791. default:
  792. return -EINVAL;
  793. }
  794. /* prctl interface doesn't have flags, so they are always zero. */
  795. return do_seccomp(op, 0, uargs);
  796. }