seccomp.c 24 KB

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