seccomp.c 24 KB

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