seccomp.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927
  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(const 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 = IS_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 (in_compat_syscall()) {
  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 const int mode1_syscalls[] = {
  476. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  477. 0, /* null terminated */
  478. };
  479. static void __secure_computing_strict(int this_syscall)
  480. {
  481. const int *syscall_whitelist = mode1_syscalls;
  482. #ifdef CONFIG_COMPAT
  483. if (in_compat_syscall())
  484. syscall_whitelist = get_compat_mode1_syscalls();
  485. #endif
  486. do {
  487. if (*syscall_whitelist == this_syscall)
  488. return;
  489. } while (*++syscall_whitelist);
  490. #ifdef SECCOMP_DEBUG
  491. dump_stack();
  492. #endif
  493. audit_seccomp(this_syscall, SIGKILL, SECCOMP_RET_KILL);
  494. do_exit(SIGKILL);
  495. }
  496. #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  497. void secure_computing_strict(int this_syscall)
  498. {
  499. int mode = current->seccomp.mode;
  500. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  501. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  502. return;
  503. if (mode == SECCOMP_MODE_DISABLED)
  504. return;
  505. else if (mode == SECCOMP_MODE_STRICT)
  506. __secure_computing_strict(this_syscall);
  507. else
  508. BUG();
  509. }
  510. #else
  511. #ifdef CONFIG_SECCOMP_FILTER
  512. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  513. const bool recheck_after_trace)
  514. {
  515. u32 filter_ret, action;
  516. int data;
  517. /*
  518. * Make sure that any changes to mode from another thread have
  519. * been seen after TIF_SECCOMP was seen.
  520. */
  521. rmb();
  522. filter_ret = seccomp_run_filters(sd);
  523. data = filter_ret & SECCOMP_RET_DATA;
  524. action = filter_ret & SECCOMP_RET_ACTION;
  525. switch (action) {
  526. case SECCOMP_RET_ERRNO:
  527. /* Set low-order bits as an errno, capped at MAX_ERRNO. */
  528. if (data > MAX_ERRNO)
  529. data = MAX_ERRNO;
  530. syscall_set_return_value(current, task_pt_regs(current),
  531. -data, 0);
  532. goto skip;
  533. case SECCOMP_RET_TRAP:
  534. /* Show the handler the original registers. */
  535. syscall_rollback(current, task_pt_regs(current));
  536. /* Let the filter pass back 16 bits of data. */
  537. seccomp_send_sigsys(this_syscall, data);
  538. goto skip;
  539. case SECCOMP_RET_TRACE:
  540. /* We've been put in this state by the ptracer already. */
  541. if (recheck_after_trace)
  542. return 0;
  543. /* ENOSYS these calls if there is no tracer attached. */
  544. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  545. syscall_set_return_value(current,
  546. task_pt_regs(current),
  547. -ENOSYS, 0);
  548. goto skip;
  549. }
  550. /* Allow the BPF to provide the event message */
  551. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  552. /*
  553. * The delivery of a fatal signal during event
  554. * notification may silently skip tracer notification,
  555. * which could leave us with a potentially unmodified
  556. * syscall that the tracer would have liked to have
  557. * changed. Since the process is about to die, we just
  558. * force the syscall to be skipped and let the signal
  559. * kill the process and correctly handle any tracer exit
  560. * notifications.
  561. */
  562. if (fatal_signal_pending(current))
  563. goto skip;
  564. /* Check if the tracer forced the syscall to be skipped. */
  565. this_syscall = syscall_get_nr(current, task_pt_regs(current));
  566. if (this_syscall < 0)
  567. goto skip;
  568. /*
  569. * Recheck the syscall, since it may have changed. This
  570. * intentionally uses a NULL struct seccomp_data to force
  571. * a reload of all registers. This does not goto skip since
  572. * a skip would have already been reported.
  573. */
  574. if (__seccomp_filter(this_syscall, NULL, true))
  575. return -1;
  576. return 0;
  577. case SECCOMP_RET_ALLOW:
  578. return 0;
  579. case SECCOMP_RET_KILL:
  580. default:
  581. audit_seccomp(this_syscall, SIGSYS, action);
  582. do_exit(SIGSYS);
  583. }
  584. unreachable();
  585. skip:
  586. audit_seccomp(this_syscall, 0, action);
  587. return -1;
  588. }
  589. #else
  590. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  591. const bool recheck_after_trace)
  592. {
  593. BUG();
  594. }
  595. #endif
  596. int __secure_computing(const struct seccomp_data *sd)
  597. {
  598. int mode = current->seccomp.mode;
  599. int this_syscall;
  600. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  601. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  602. return 0;
  603. this_syscall = sd ? sd->nr :
  604. syscall_get_nr(current, task_pt_regs(current));
  605. switch (mode) {
  606. case SECCOMP_MODE_STRICT:
  607. __secure_computing_strict(this_syscall); /* may call do_exit */
  608. return 0;
  609. case SECCOMP_MODE_FILTER:
  610. return __seccomp_filter(this_syscall, sd, false);
  611. default:
  612. BUG();
  613. }
  614. }
  615. #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
  616. long prctl_get_seccomp(void)
  617. {
  618. return current->seccomp.mode;
  619. }
  620. /**
  621. * seccomp_set_mode_strict: internal function for setting strict seccomp
  622. *
  623. * Once current->seccomp.mode is non-zero, it may not be changed.
  624. *
  625. * Returns 0 on success or -EINVAL on failure.
  626. */
  627. static long seccomp_set_mode_strict(void)
  628. {
  629. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  630. long ret = -EINVAL;
  631. spin_lock_irq(&current->sighand->siglock);
  632. if (!seccomp_may_assign_mode(seccomp_mode))
  633. goto out;
  634. #ifdef TIF_NOTSC
  635. disable_TSC();
  636. #endif
  637. seccomp_assign_mode(current, seccomp_mode, 0);
  638. ret = 0;
  639. out:
  640. spin_unlock_irq(&current->sighand->siglock);
  641. return ret;
  642. }
  643. #ifdef CONFIG_SECCOMP_FILTER
  644. /**
  645. * seccomp_set_mode_filter: internal function for setting seccomp filter
  646. * @flags: flags to change filter behavior
  647. * @filter: struct sock_fprog containing filter
  648. *
  649. * This function may be called repeatedly to install additional filters.
  650. * Every filter successfully installed will be evaluated (in reverse order)
  651. * for each system call the task makes.
  652. *
  653. * Once current->seccomp.mode is non-zero, it may not be changed.
  654. *
  655. * Returns 0 on success or -EINVAL on failure.
  656. */
  657. static long seccomp_set_mode_filter(unsigned int flags,
  658. const char __user *filter)
  659. {
  660. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  661. struct seccomp_filter *prepared = NULL;
  662. long ret = -EINVAL;
  663. /* Validate flags. */
  664. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  665. return -EINVAL;
  666. /* Prepare the new filter before holding any locks. */
  667. prepared = seccomp_prepare_user_filter(filter);
  668. if (IS_ERR(prepared))
  669. return PTR_ERR(prepared);
  670. /*
  671. * Make sure we cannot change seccomp or nnp state via TSYNC
  672. * while another thread is in the middle of calling exec.
  673. */
  674. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  675. mutex_lock_killable(&current->signal->cred_guard_mutex))
  676. goto out_free;
  677. spin_lock_irq(&current->sighand->siglock);
  678. if (!seccomp_may_assign_mode(seccomp_mode))
  679. goto out;
  680. ret = seccomp_attach_filter(flags, prepared);
  681. if (ret)
  682. goto out;
  683. /* Do not free the successfully attached filter. */
  684. prepared = NULL;
  685. seccomp_assign_mode(current, seccomp_mode, flags);
  686. out:
  687. spin_unlock_irq(&current->sighand->siglock);
  688. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  689. mutex_unlock(&current->signal->cred_guard_mutex);
  690. out_free:
  691. seccomp_filter_free(prepared);
  692. return ret;
  693. }
  694. #else
  695. static inline long seccomp_set_mode_filter(unsigned int flags,
  696. const char __user *filter)
  697. {
  698. return -EINVAL;
  699. }
  700. #endif
  701. /* Common entry point for both prctl and syscall. */
  702. static long do_seccomp(unsigned int op, unsigned int flags,
  703. const char __user *uargs)
  704. {
  705. switch (op) {
  706. case SECCOMP_SET_MODE_STRICT:
  707. if (flags != 0 || uargs != NULL)
  708. return -EINVAL;
  709. return seccomp_set_mode_strict();
  710. case SECCOMP_SET_MODE_FILTER:
  711. return seccomp_set_mode_filter(flags, uargs);
  712. default:
  713. return -EINVAL;
  714. }
  715. }
  716. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  717. const char __user *, uargs)
  718. {
  719. return do_seccomp(op, flags, uargs);
  720. }
  721. /**
  722. * prctl_set_seccomp: configures current->seccomp.mode
  723. * @seccomp_mode: requested mode to use
  724. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  725. *
  726. * Returns 0 on success or -EINVAL on failure.
  727. */
  728. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  729. {
  730. unsigned int op;
  731. char __user *uargs;
  732. switch (seccomp_mode) {
  733. case SECCOMP_MODE_STRICT:
  734. op = SECCOMP_SET_MODE_STRICT;
  735. /*
  736. * Setting strict mode through prctl always ignored filter,
  737. * so make sure it is always NULL here to pass the internal
  738. * check in do_seccomp().
  739. */
  740. uargs = NULL;
  741. break;
  742. case SECCOMP_MODE_FILTER:
  743. op = SECCOMP_SET_MODE_FILTER;
  744. uargs = filter;
  745. break;
  746. default:
  747. return -EINVAL;
  748. }
  749. /* prctl interface doesn't have flags, so they are always zero. */
  750. return do_seccomp(op, 0, uargs);
  751. }
  752. #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
  753. long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
  754. void __user *data)
  755. {
  756. struct seccomp_filter *filter;
  757. struct sock_fprog_kern *fprog;
  758. long ret;
  759. unsigned long count = 0;
  760. if (!capable(CAP_SYS_ADMIN) ||
  761. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  762. return -EACCES;
  763. }
  764. spin_lock_irq(&task->sighand->siglock);
  765. if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
  766. ret = -EINVAL;
  767. goto out;
  768. }
  769. filter = task->seccomp.filter;
  770. while (filter) {
  771. filter = filter->prev;
  772. count++;
  773. }
  774. if (filter_off >= count) {
  775. ret = -ENOENT;
  776. goto out;
  777. }
  778. count -= filter_off;
  779. filter = task->seccomp.filter;
  780. while (filter && count > 1) {
  781. filter = filter->prev;
  782. count--;
  783. }
  784. if (WARN_ON(count != 1 || !filter)) {
  785. /* The filter tree shouldn't shrink while we're using it. */
  786. ret = -ENOENT;
  787. goto out;
  788. }
  789. fprog = filter->prog->orig_prog;
  790. if (!fprog) {
  791. /* This must be a new non-cBPF filter, since we save
  792. * every cBPF filter's orig_prog above when
  793. * CONFIG_CHECKPOINT_RESTORE is enabled.
  794. */
  795. ret = -EMEDIUMTYPE;
  796. goto out;
  797. }
  798. ret = fprog->len;
  799. if (!data)
  800. goto out;
  801. __get_seccomp_filter(filter);
  802. spin_unlock_irq(&task->sighand->siglock);
  803. if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
  804. ret = -EFAULT;
  805. __put_seccomp_filter(filter);
  806. return ret;
  807. out:
  808. spin_unlock_irq(&task->sighand->siglock);
  809. return ret;
  810. }
  811. #endif