seccomp.c 25 KB

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