seccomp.c 24 KB

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