seccomp.c 24 KB

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