seccomp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. /* #define SECCOMP_DEBUG 1 */
  23. #ifdef CONFIG_SECCOMP_FILTER
  24. #include <asm/syscall.h>
  25. #include <linux/filter.h>
  26. #include <linux/pid.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/security.h>
  29. #include <linux/tracehook.h>
  30. #include <linux/uaccess.h>
  31. /**
  32. * struct seccomp_filter - container for seccomp BPF programs
  33. *
  34. * @usage: reference count to manage the object lifetime.
  35. * get/put helpers should be used when accessing an instance
  36. * outside of a lifetime-guarded section. In general, this
  37. * is only needed for handling filters shared across tasks.
  38. * @prev: points to a previously installed, or inherited, filter
  39. * @len: the number of instructions in the program
  40. * @insns: the BPF program instructions 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. unsigned short len; /* Instruction count */
  56. struct sock_filter insns[];
  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. * get_u32 - returns a u32 offset into data
  62. * @data: a unsigned 64 bit value
  63. * @index: 0 or 1 to return the first or second 32-bits
  64. *
  65. * This inline exists to hide the length of unsigned long. If a 32-bit
  66. * unsigned long is passed in, it will be extended and the top 32-bits will be
  67. * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
  68. * properly returned.
  69. *
  70. * Endianness is explicitly ignored and left for BPF program authors to manage
  71. * as per the specific architecture.
  72. */
  73. static inline u32 get_u32(u64 data, int index)
  74. {
  75. return ((u32 *)&data)[index];
  76. }
  77. /* Helper for bpf_load below. */
  78. #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
  79. /**
  80. * bpf_load: checks and returns a pointer to the requested offset
  81. * @off: offset into struct seccomp_data to load from
  82. *
  83. * Returns the requested 32-bits of data.
  84. * seccomp_check_filter() should assure that @off is 32-bit aligned
  85. * and not out of bounds. Failure to do so is a BUG.
  86. */
  87. u32 seccomp_bpf_load(int off)
  88. {
  89. struct pt_regs *regs = task_pt_regs(current);
  90. if (off == BPF_DATA(nr))
  91. return syscall_get_nr(current, regs);
  92. if (off == BPF_DATA(arch))
  93. return syscall_get_arch();
  94. if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
  95. unsigned long value;
  96. int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
  97. int index = !!(off % sizeof(u64));
  98. syscall_get_arguments(current, regs, arg, 1, &value);
  99. return get_u32(value, index);
  100. }
  101. if (off == BPF_DATA(instruction_pointer))
  102. return get_u32(KSTK_EIP(current), 0);
  103. if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
  104. return get_u32(KSTK_EIP(current), 1);
  105. /* seccomp_check_filter should make this impossible. */
  106. BUG();
  107. }
  108. /**
  109. * seccomp_check_filter - verify seccomp filter code
  110. * @filter: filter to verify
  111. * @flen: length of filter
  112. *
  113. * Takes a previously checked filter (by sk_chk_filter) and
  114. * redirects all filter code that loads struct sk_buff data
  115. * and related data through seccomp_bpf_load. It also
  116. * enforces length and alignment checking of those loads.
  117. *
  118. * Returns 0 if the rule set is legal or -EINVAL if not.
  119. */
  120. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  121. {
  122. int pc;
  123. for (pc = 0; pc < flen; pc++) {
  124. struct sock_filter *ftest = &filter[pc];
  125. u16 code = ftest->code;
  126. u32 k = ftest->k;
  127. switch (code) {
  128. case BPF_S_LD_W_ABS:
  129. ftest->code = BPF_S_ANC_SECCOMP_LD_W;
  130. /* 32-bit aligned and not out of bounds. */
  131. if (k >= sizeof(struct seccomp_data) || k & 3)
  132. return -EINVAL;
  133. continue;
  134. case BPF_S_LD_W_LEN:
  135. ftest->code = BPF_S_LD_IMM;
  136. ftest->k = sizeof(struct seccomp_data);
  137. continue;
  138. case BPF_S_LDX_W_LEN:
  139. ftest->code = BPF_S_LDX_IMM;
  140. ftest->k = sizeof(struct seccomp_data);
  141. continue;
  142. /* Explicitly include allowed calls. */
  143. case BPF_S_RET_K:
  144. case BPF_S_RET_A:
  145. case BPF_S_ALU_ADD_K:
  146. case BPF_S_ALU_ADD_X:
  147. case BPF_S_ALU_SUB_K:
  148. case BPF_S_ALU_SUB_X:
  149. case BPF_S_ALU_MUL_K:
  150. case BPF_S_ALU_MUL_X:
  151. case BPF_S_ALU_DIV_X:
  152. case BPF_S_ALU_AND_K:
  153. case BPF_S_ALU_AND_X:
  154. case BPF_S_ALU_OR_K:
  155. case BPF_S_ALU_OR_X:
  156. case BPF_S_ALU_LSH_K:
  157. case BPF_S_ALU_LSH_X:
  158. case BPF_S_ALU_RSH_K:
  159. case BPF_S_ALU_RSH_X:
  160. case BPF_S_ALU_NEG:
  161. case BPF_S_LD_IMM:
  162. case BPF_S_LDX_IMM:
  163. case BPF_S_MISC_TAX:
  164. case BPF_S_MISC_TXA:
  165. case BPF_S_ALU_DIV_K:
  166. case BPF_S_LD_MEM:
  167. case BPF_S_LDX_MEM:
  168. case BPF_S_ST:
  169. case BPF_S_STX:
  170. case BPF_S_JMP_JA:
  171. case BPF_S_JMP_JEQ_K:
  172. case BPF_S_JMP_JEQ_X:
  173. case BPF_S_JMP_JGE_K:
  174. case BPF_S_JMP_JGE_X:
  175. case BPF_S_JMP_JGT_K:
  176. case BPF_S_JMP_JGT_X:
  177. case BPF_S_JMP_JSET_K:
  178. case BPF_S_JMP_JSET_X:
  179. continue;
  180. default:
  181. return -EINVAL;
  182. }
  183. }
  184. return 0;
  185. }
  186. /**
  187. * seccomp_run_filters - evaluates all seccomp filters against @syscall
  188. * @syscall: number of the current system call
  189. *
  190. * Returns valid seccomp BPF response codes.
  191. */
  192. static u32 seccomp_run_filters(int syscall)
  193. {
  194. struct seccomp_filter *f = ACCESS_ONCE(current->seccomp.filter);
  195. u32 ret = SECCOMP_RET_ALLOW;
  196. /* Ensure unexpected behavior doesn't result in failing open. */
  197. if (unlikely(WARN_ON(f == NULL)))
  198. return SECCOMP_RET_KILL;
  199. /* Make sure cross-thread synced filter points somewhere sane. */
  200. smp_read_barrier_depends();
  201. /*
  202. * All filters in the list are evaluated and the lowest BPF return
  203. * value always takes priority (ignoring the DATA).
  204. */
  205. for (; f; f = f->prev) {
  206. u32 cur_ret = sk_run_filter(NULL, f->insns);
  207. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  208. ret = cur_ret;
  209. }
  210. return ret;
  211. }
  212. #endif /* CONFIG_SECCOMP_FILTER */
  213. static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
  214. {
  215. assert_spin_locked(&current->sighand->siglock);
  216. if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
  217. return false;
  218. return true;
  219. }
  220. static inline void seccomp_assign_mode(struct task_struct *task,
  221. unsigned long seccomp_mode)
  222. {
  223. assert_spin_locked(&task->sighand->siglock);
  224. task->seccomp.mode = seccomp_mode;
  225. /*
  226. * Make sure TIF_SECCOMP cannot be set before the mode (and
  227. * filter) is set.
  228. */
  229. smp_mb();
  230. set_tsk_thread_flag(task, TIF_SECCOMP);
  231. }
  232. #ifdef CONFIG_SECCOMP_FILTER
  233. /* Returns 1 if the parent is an ancestor of the child. */
  234. static int is_ancestor(struct seccomp_filter *parent,
  235. struct seccomp_filter *child)
  236. {
  237. /* NULL is the root ancestor. */
  238. if (parent == NULL)
  239. return 1;
  240. for (; child; child = child->prev)
  241. if (child == parent)
  242. return 1;
  243. return 0;
  244. }
  245. /**
  246. * seccomp_can_sync_threads: checks if all threads can be synchronized
  247. *
  248. * Expects sighand and cred_guard_mutex locks to be held.
  249. *
  250. * Returns 0 on success, -ve on error, or the pid of a thread which was
  251. * either not in the correct seccomp mode or it did not have an ancestral
  252. * seccomp filter.
  253. */
  254. static inline pid_t seccomp_can_sync_threads(void)
  255. {
  256. struct task_struct *thread, *caller;
  257. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  258. assert_spin_locked(&current->sighand->siglock);
  259. /* Validate all threads being eligible for synchronization. */
  260. caller = current;
  261. for_each_thread(caller, thread) {
  262. pid_t failed;
  263. /* Skip current, since it is initiating the sync. */
  264. if (thread == caller)
  265. continue;
  266. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
  267. (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
  268. is_ancestor(thread->seccomp.filter,
  269. caller->seccomp.filter)))
  270. continue;
  271. /* Return the first thread that cannot be synchronized. */
  272. failed = task_pid_vnr(thread);
  273. /* If the pid cannot be resolved, then return -ESRCH */
  274. if (unlikely(WARN_ON(failed == 0)))
  275. failed = -ESRCH;
  276. return failed;
  277. }
  278. return 0;
  279. }
  280. /**
  281. * seccomp_sync_threads: sets all threads to use current's filter
  282. *
  283. * Expects sighand and cred_guard_mutex locks to be held, and for
  284. * seccomp_can_sync_threads() to have returned success already
  285. * without dropping the locks.
  286. *
  287. */
  288. static inline void seccomp_sync_threads(void)
  289. {
  290. struct task_struct *thread, *caller;
  291. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  292. assert_spin_locked(&current->sighand->siglock);
  293. /* Synchronize all threads. */
  294. caller = current;
  295. for_each_thread(caller, thread) {
  296. /* Skip current, since it needs no changes. */
  297. if (thread == caller)
  298. continue;
  299. /* Get a task reference for the new leaf node. */
  300. get_seccomp_filter(caller);
  301. /*
  302. * Drop the task reference to the shared ancestor since
  303. * current's path will hold a reference. (This also
  304. * allows a put before the assignment.)
  305. */
  306. put_seccomp_filter(thread);
  307. smp_mb();
  308. ACCESS_ONCE(thread->seccomp.filter) = caller->seccomp.filter;
  309. /*
  310. * Don't let an unprivileged task work around
  311. * the no_new_privs restriction by creating
  312. * a thread that sets it up, enters seccomp,
  313. * then dies.
  314. */
  315. if (task_no_new_privs(caller))
  316. task_set_no_new_privs(thread);
  317. /*
  318. * Opt the other thread into seccomp if needed.
  319. * As threads are considered to be trust-realm
  320. * equivalent (see ptrace_may_access), it is safe to
  321. * allow one thread to transition the other.
  322. */
  323. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
  324. seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
  325. }
  326. }
  327. /**
  328. * seccomp_prepare_filter: Prepares a seccomp filter for use.
  329. * @fprog: BPF program to install
  330. *
  331. * Returns filter on success or an ERR_PTR on failure.
  332. */
  333. static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
  334. {
  335. struct seccomp_filter *filter;
  336. unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
  337. unsigned long total_insns = fprog->len;
  338. long ret;
  339. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  340. return ERR_PTR(-EINVAL);
  341. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  342. for (filter = current->seccomp.filter; filter; filter = filter->prev)
  343. total_insns += filter->len + 4; /* include a 4 instr penalty */
  344. if (total_insns > MAX_INSNS_PER_PATH)
  345. return ERR_PTR(-ENOMEM);
  346. /*
  347. * Installing a seccomp filter requires that the task have
  348. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  349. * This avoids scenarios where unprivileged tasks can affect the
  350. * behavior of privileged children.
  351. */
  352. if (!task_no_new_privs(current) &&
  353. security_capable_noaudit(current_cred(), current_user_ns(),
  354. CAP_SYS_ADMIN) != 0)
  355. return ERR_PTR(-EACCES);
  356. /* Allocate a new seccomp_filter */
  357. filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
  358. GFP_KERNEL|__GFP_NOWARN);
  359. if (!filter)
  360. return ERR_PTR(-ENOMEM);
  361. atomic_set(&filter->usage, 1);
  362. filter->len = fprog->len;
  363. /* Copy the instructions from fprog. */
  364. ret = -EFAULT;
  365. if (copy_from_user(filter->insns, fprog->filter, fp_size))
  366. goto fail;
  367. /* Check and rewrite the fprog via the skb checker */
  368. ret = sk_chk_filter(filter->insns, filter->len);
  369. if (ret)
  370. goto fail;
  371. /* Check and rewrite the fprog for seccomp use */
  372. ret = seccomp_check_filter(filter->insns, filter->len);
  373. if (ret)
  374. goto fail;
  375. return filter;
  376. fail:
  377. kfree(filter);
  378. return ERR_PTR(ret);
  379. }
  380. /**
  381. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  382. * @user_filter: pointer to the user data containing a sock_fprog.
  383. *
  384. * Returns 0 on success and non-zero otherwise.
  385. */
  386. static struct seccomp_filter *
  387. seccomp_prepare_user_filter(const char __user *user_filter)
  388. {
  389. struct sock_fprog fprog;
  390. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  391. #ifdef CONFIG_COMPAT
  392. if (is_compat_task()) {
  393. struct compat_sock_fprog fprog32;
  394. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  395. goto out;
  396. fprog.len = fprog32.len;
  397. fprog.filter = compat_ptr(fprog32.filter);
  398. } else /* falls through to the if below. */
  399. #endif
  400. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  401. goto out;
  402. filter = seccomp_prepare_filter(&fprog);
  403. out:
  404. return filter;
  405. }
  406. /**
  407. * seccomp_attach_filter: validate and attach filter
  408. * @flags: flags to change filter behavior
  409. * @filter: seccomp filter to add to the current process
  410. *
  411. * Caller must be holding current->sighand->siglock lock.
  412. *
  413. * Returns 0 on success, -ve on error.
  414. */
  415. static long seccomp_attach_filter(unsigned int flags,
  416. struct seccomp_filter *filter)
  417. {
  418. unsigned long total_insns;
  419. struct seccomp_filter *walker;
  420. assert_spin_locked(&current->sighand->siglock);
  421. /* Validate resulting filter length. */
  422. total_insns = filter->len;
  423. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  424. total_insns += walker->len + 4; /* 4 instr penalty */
  425. if (total_insns > MAX_INSNS_PER_PATH)
  426. return -ENOMEM;
  427. /* If thread sync has been requested, check that it is possible. */
  428. if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
  429. int ret;
  430. ret = seccomp_can_sync_threads();
  431. if (ret)
  432. return ret;
  433. }
  434. /*
  435. * If there is an existing filter, make it the prev and don't drop its
  436. * task reference.
  437. */
  438. filter->prev = current->seccomp.filter;
  439. current->seccomp.filter = filter;
  440. /* Now that the new filter is in place, synchronize to all threads. */
  441. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  442. seccomp_sync_threads();
  443. return 0;
  444. }
  445. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  446. void get_seccomp_filter(struct task_struct *tsk)
  447. {
  448. struct seccomp_filter *orig = tsk->seccomp.filter;
  449. if (!orig)
  450. return;
  451. /* Reference count is bounded by the number of total processes. */
  452. atomic_inc(&orig->usage);
  453. }
  454. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  455. {
  456. if (filter) {
  457. kfree(filter);
  458. }
  459. }
  460. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  461. void put_seccomp_filter(struct task_struct *tsk)
  462. {
  463. struct seccomp_filter *orig = tsk->seccomp.filter;
  464. /* Clean up single-reference branches iteratively. */
  465. while (orig && atomic_dec_and_test(&orig->usage)) {
  466. struct seccomp_filter *freeme = orig;
  467. orig = orig->prev;
  468. seccomp_filter_free(freeme);
  469. }
  470. }
  471. /**
  472. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  473. * @syscall: syscall number to send to userland
  474. * @reason: filter-supplied reason code to send to userland (via si_errno)
  475. *
  476. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  477. */
  478. static void seccomp_send_sigsys(int syscall, int reason)
  479. {
  480. struct siginfo info;
  481. memset(&info, 0, sizeof(info));
  482. info.si_signo = SIGSYS;
  483. info.si_code = SYS_SECCOMP;
  484. info.si_call_addr = (void __user *)KSTK_EIP(current);
  485. info.si_errno = reason;
  486. info.si_arch = syscall_get_arch();
  487. info.si_syscall = syscall;
  488. force_sig_info(SIGSYS, &info, current);
  489. }
  490. #endif /* CONFIG_SECCOMP_FILTER */
  491. /*
  492. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  493. * To be fully secure this must be combined with rlimit
  494. * to limit the stack allocations too.
  495. */
  496. static int mode1_syscalls[] = {
  497. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  498. 0, /* null terminated */
  499. };
  500. #ifdef CONFIG_COMPAT
  501. static int mode1_syscalls_32[] = {
  502. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  503. 0, /* null terminated */
  504. };
  505. #endif
  506. int __secure_computing(int this_syscall)
  507. {
  508. int exit_sig = 0;
  509. int *syscall;
  510. u32 ret;
  511. /*
  512. * Make sure that any changes to mode from another thread have
  513. * been seen after TIF_SECCOMP was seen.
  514. */
  515. rmb();
  516. switch (current->seccomp.mode) {
  517. case SECCOMP_MODE_STRICT:
  518. syscall = mode1_syscalls;
  519. #ifdef CONFIG_COMPAT
  520. if (is_compat_task())
  521. syscall = mode1_syscalls_32;
  522. #endif
  523. do {
  524. if (*syscall == this_syscall)
  525. return 0;
  526. } while (*++syscall);
  527. exit_sig = SIGKILL;
  528. ret = SECCOMP_RET_KILL;
  529. break;
  530. #ifdef CONFIG_SECCOMP_FILTER
  531. case SECCOMP_MODE_FILTER: {
  532. int data;
  533. ret = seccomp_run_filters(this_syscall);
  534. data = ret & SECCOMP_RET_DATA;
  535. ret &= SECCOMP_RET_ACTION;
  536. switch (ret) {
  537. case SECCOMP_RET_ERRNO:
  538. /* Set the low-order 16-bits as a errno. */
  539. syscall_set_return_value(current, task_pt_regs(current),
  540. -data, 0);
  541. goto skip;
  542. case SECCOMP_RET_TRAP:
  543. /* Show the handler the original registers. */
  544. syscall_rollback(current, task_pt_regs(current));
  545. /* Let the filter pass back 16 bits of data. */
  546. seccomp_send_sigsys(this_syscall, data);
  547. goto skip;
  548. case SECCOMP_RET_TRACE:
  549. /* Skip these calls if there is no tracer. */
  550. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  551. /* Make sure userspace sees an ENOSYS. */
  552. syscall_set_return_value(current,
  553. task_pt_regs(current), -ENOSYS, 0);
  554. goto skip;
  555. }
  556. /* Allow the BPF to provide the event message */
  557. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  558. /*
  559. * The delivery of a fatal signal during event
  560. * notification may silently skip tracer notification.
  561. * Terminating the task now avoids executing a system
  562. * call that may not be intended.
  563. */
  564. if (fatal_signal_pending(current))
  565. break;
  566. return 0;
  567. case SECCOMP_RET_ALLOW:
  568. return 0;
  569. case SECCOMP_RET_KILL:
  570. default:
  571. break;
  572. }
  573. exit_sig = SIGSYS;
  574. break;
  575. }
  576. #endif
  577. default:
  578. BUG();
  579. }
  580. #ifdef SECCOMP_DEBUG
  581. dump_stack();
  582. #endif
  583. audit_seccomp(this_syscall, exit_sig, ret);
  584. do_exit(exit_sig);
  585. #ifdef CONFIG_SECCOMP_FILTER
  586. skip:
  587. audit_seccomp(this_syscall, exit_sig, ret);
  588. #endif
  589. return -1;
  590. }
  591. long prctl_get_seccomp(void)
  592. {
  593. return current->seccomp.mode;
  594. }
  595. /**
  596. * seccomp_set_mode_strict: internal function for setting strict seccomp
  597. *
  598. * Once current->seccomp.mode is non-zero, it may not be changed.
  599. *
  600. * Returns 0 on success or -EINVAL on failure.
  601. */
  602. static long seccomp_set_mode_strict(void)
  603. {
  604. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  605. long ret = -EINVAL;
  606. spin_lock_irq(&current->sighand->siglock);
  607. if (!seccomp_may_assign_mode(seccomp_mode))
  608. goto out;
  609. #ifdef TIF_NOTSC
  610. disable_TSC();
  611. #endif
  612. seccomp_assign_mode(current, seccomp_mode);
  613. ret = 0;
  614. out:
  615. spin_unlock_irq(&current->sighand->siglock);
  616. return ret;
  617. }
  618. #ifdef CONFIG_SECCOMP_FILTER
  619. /**
  620. * seccomp_set_mode_filter: internal function for setting seccomp filter
  621. * @flags: flags to change filter behavior
  622. * @filter: struct sock_fprog containing filter
  623. *
  624. * This function may be called repeatedly to install additional filters.
  625. * Every filter successfully installed will be evaluated (in reverse order)
  626. * for each system call the task makes.
  627. *
  628. * Once current->seccomp.mode is non-zero, it may not be changed.
  629. *
  630. * Returns 0 on success or -EINVAL on failure.
  631. */
  632. static long seccomp_set_mode_filter(unsigned int flags,
  633. const char __user *filter)
  634. {
  635. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  636. struct seccomp_filter *prepared = NULL;
  637. long ret = -EINVAL;
  638. /* Validate flags. */
  639. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  640. return -EINVAL;
  641. /* Prepare the new filter before holding any locks. */
  642. prepared = seccomp_prepare_user_filter(filter);
  643. if (IS_ERR(prepared))
  644. return PTR_ERR(prepared);
  645. /*
  646. * Make sure we cannot change seccomp or nnp state via TSYNC
  647. * while another thread is in the middle of calling exec.
  648. */
  649. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  650. mutex_lock_killable(&current->signal->cred_guard_mutex))
  651. goto out_free;
  652. spin_lock_irq(&current->sighand->siglock);
  653. if (!seccomp_may_assign_mode(seccomp_mode))
  654. goto out;
  655. ret = seccomp_attach_filter(flags, prepared);
  656. if (ret)
  657. goto out;
  658. /* Do not free the successfully attached filter. */
  659. prepared = NULL;
  660. seccomp_assign_mode(current, seccomp_mode);
  661. out:
  662. spin_unlock_irq(&current->sighand->siglock);
  663. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  664. mutex_unlock(&current->signal->cred_guard_mutex);
  665. out_free:
  666. seccomp_filter_free(prepared);
  667. return ret;
  668. }
  669. #else
  670. static inline long seccomp_set_mode_filter(unsigned int flags,
  671. const char __user *filter)
  672. {
  673. return -EINVAL;
  674. }
  675. #endif
  676. /* Common entry point for both prctl and syscall. */
  677. static long do_seccomp(unsigned int op, unsigned int flags,
  678. const char __user *uargs)
  679. {
  680. switch (op) {
  681. case SECCOMP_SET_MODE_STRICT:
  682. if (flags != 0 || uargs != NULL)
  683. return -EINVAL;
  684. return seccomp_set_mode_strict();
  685. case SECCOMP_SET_MODE_FILTER:
  686. return seccomp_set_mode_filter(flags, uargs);
  687. default:
  688. return -EINVAL;
  689. }
  690. }
  691. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  692. const char __user *, uargs)
  693. {
  694. return do_seccomp(op, flags, uargs);
  695. }
  696. /**
  697. * prctl_set_seccomp: configures current->seccomp.mode
  698. * @seccomp_mode: requested mode to use
  699. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  700. *
  701. * Returns 0 on success or -EINVAL on failure.
  702. */
  703. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  704. {
  705. unsigned int op;
  706. char __user *uargs;
  707. switch (seccomp_mode) {
  708. case SECCOMP_MODE_STRICT:
  709. op = SECCOMP_SET_MODE_STRICT;
  710. /*
  711. * Setting strict mode through prctl always ignored filter,
  712. * so make sure it is always NULL here to pass the internal
  713. * check in do_seccomp().
  714. */
  715. uargs = NULL;
  716. break;
  717. case SECCOMP_MODE_FILTER:
  718. op = SECCOMP_SET_MODE_FILTER;
  719. uargs = filter;
  720. break;
  721. default:
  722. return -EINVAL;
  723. }
  724. /* prctl interface doesn't have flags, so they are always zero. */
  725. return do_seccomp(op, 0, uargs);
  726. }