seccomp.c 24 KB

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