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(current, regs);
  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. * Opt the other thread into seccomp if needed.
  311. * As threads are considered to be trust-realm
  312. * equivalent (see ptrace_may_access), it is safe to
  313. * allow one thread to transition the other.
  314. */
  315. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED) {
  316. /*
  317. * Don't let an unprivileged task work around
  318. * the no_new_privs restriction by creating
  319. * a thread that sets it up, enters seccomp,
  320. * then dies.
  321. */
  322. if (task_no_new_privs(caller))
  323. task_set_no_new_privs(thread);
  324. seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
  325. }
  326. }
  327. }
  328. /**
  329. * seccomp_prepare_filter: Prepares a seccomp filter for use.
  330. * @fprog: BPF program to install
  331. *
  332. * Returns filter on success or an ERR_PTR on failure.
  333. */
  334. static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
  335. {
  336. struct seccomp_filter *filter;
  337. unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
  338. unsigned long total_insns = fprog->len;
  339. long ret;
  340. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  341. return ERR_PTR(-EINVAL);
  342. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  343. for (filter = current->seccomp.filter; filter; filter = filter->prev)
  344. total_insns += filter->len + 4; /* include a 4 instr penalty */
  345. if (total_insns > MAX_INSNS_PER_PATH)
  346. return ERR_PTR(-ENOMEM);
  347. /*
  348. * Installing a seccomp filter requires that the task have
  349. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  350. * This avoids scenarios where unprivileged tasks can affect the
  351. * behavior of privileged children.
  352. */
  353. if (!task_no_new_privs(current) &&
  354. security_capable_noaudit(current_cred(), current_user_ns(),
  355. CAP_SYS_ADMIN) != 0)
  356. return ERR_PTR(-EACCES);
  357. /* Allocate a new seccomp_filter */
  358. filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
  359. GFP_KERNEL|__GFP_NOWARN);
  360. if (!filter)
  361. return ERR_PTR(-ENOMEM);
  362. atomic_set(&filter->usage, 1);
  363. filter->len = fprog->len;
  364. /* Copy the instructions from fprog. */
  365. ret = -EFAULT;
  366. if (copy_from_user(filter->insns, fprog->filter, fp_size))
  367. goto fail;
  368. /* Check and rewrite the fprog via the skb checker */
  369. ret = sk_chk_filter(filter->insns, filter->len);
  370. if (ret)
  371. goto fail;
  372. /* Check and rewrite the fprog for seccomp use */
  373. ret = seccomp_check_filter(filter->insns, filter->len);
  374. if (ret)
  375. goto fail;
  376. return filter;
  377. fail:
  378. kfree(filter);
  379. return ERR_PTR(ret);
  380. }
  381. /**
  382. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  383. * @user_filter: pointer to the user data containing a sock_fprog.
  384. *
  385. * Returns 0 on success and non-zero otherwise.
  386. */
  387. static struct seccomp_filter *
  388. seccomp_prepare_user_filter(const char __user *user_filter)
  389. {
  390. struct sock_fprog fprog;
  391. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  392. #ifdef CONFIG_COMPAT
  393. if (is_compat_task()) {
  394. struct compat_sock_fprog fprog32;
  395. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  396. goto out;
  397. fprog.len = fprog32.len;
  398. fprog.filter = compat_ptr(fprog32.filter);
  399. } else /* falls through to the if below. */
  400. #endif
  401. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  402. goto out;
  403. filter = seccomp_prepare_filter(&fprog);
  404. out:
  405. return filter;
  406. }
  407. /**
  408. * seccomp_attach_filter: validate and attach filter
  409. * @flags: flags to change filter behavior
  410. * @filter: seccomp filter to add to the current process
  411. *
  412. * Caller must be holding current->sighand->siglock lock.
  413. *
  414. * Returns 0 on success, -ve on error.
  415. */
  416. static long seccomp_attach_filter(unsigned int flags,
  417. struct seccomp_filter *filter)
  418. {
  419. unsigned long total_insns;
  420. struct seccomp_filter *walker;
  421. assert_spin_locked(&current->sighand->siglock);
  422. /* Validate resulting filter length. */
  423. total_insns = filter->len;
  424. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  425. total_insns += walker->len + 4; /* 4 instr penalty */
  426. if (total_insns > MAX_INSNS_PER_PATH)
  427. return -ENOMEM;
  428. /* If thread sync has been requested, check that it is possible. */
  429. if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
  430. int ret;
  431. ret = seccomp_can_sync_threads();
  432. if (ret)
  433. return ret;
  434. }
  435. /*
  436. * If there is an existing filter, make it the prev and don't drop its
  437. * task reference.
  438. */
  439. filter->prev = current->seccomp.filter;
  440. current->seccomp.filter = filter;
  441. /* Now that the new filter is in place, synchronize to all threads. */
  442. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  443. seccomp_sync_threads();
  444. return 0;
  445. }
  446. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  447. void get_seccomp_filter(struct task_struct *tsk)
  448. {
  449. struct seccomp_filter *orig = tsk->seccomp.filter;
  450. if (!orig)
  451. return;
  452. /* Reference count is bounded by the number of total processes. */
  453. atomic_inc(&orig->usage);
  454. }
  455. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  456. {
  457. if (filter) {
  458. kfree(filter);
  459. }
  460. }
  461. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  462. void put_seccomp_filter(struct task_struct *tsk)
  463. {
  464. struct seccomp_filter *orig = tsk->seccomp.filter;
  465. /* Clean up single-reference branches iteratively. */
  466. while (orig && atomic_dec_and_test(&orig->usage)) {
  467. struct seccomp_filter *freeme = orig;
  468. orig = orig->prev;
  469. seccomp_filter_free(freeme);
  470. }
  471. }
  472. /**
  473. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  474. * @syscall: syscall number to send to userland
  475. * @reason: filter-supplied reason code to send to userland (via si_errno)
  476. *
  477. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  478. */
  479. static void seccomp_send_sigsys(int syscall, int reason)
  480. {
  481. struct siginfo info;
  482. memset(&info, 0, sizeof(info));
  483. info.si_signo = SIGSYS;
  484. info.si_code = SYS_SECCOMP;
  485. info.si_call_addr = (void __user *)KSTK_EIP(current);
  486. info.si_errno = reason;
  487. info.si_arch = syscall_get_arch(current, task_pt_regs(current));
  488. info.si_syscall = syscall;
  489. force_sig_info(SIGSYS, &info, current);
  490. }
  491. #endif /* CONFIG_SECCOMP_FILTER */
  492. /*
  493. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  494. * To be fully secure this must be combined with rlimit
  495. * to limit the stack allocations too.
  496. */
  497. static int mode1_syscalls[] = {
  498. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  499. 0, /* null terminated */
  500. };
  501. #ifdef CONFIG_COMPAT
  502. static int mode1_syscalls_32[] = {
  503. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  504. 0, /* null terminated */
  505. };
  506. #endif
  507. int __secure_computing(int this_syscall)
  508. {
  509. int exit_sig = 0;
  510. int *syscall;
  511. u32 ret;
  512. /*
  513. * Make sure that any changes to mode from another thread have
  514. * been seen after TIF_SECCOMP was seen.
  515. */
  516. rmb();
  517. switch (current->seccomp.mode) {
  518. case SECCOMP_MODE_STRICT:
  519. syscall = mode1_syscalls;
  520. #ifdef CONFIG_COMPAT
  521. if (is_compat_task())
  522. syscall = mode1_syscalls_32;
  523. #endif
  524. do {
  525. if (*syscall == this_syscall)
  526. return 0;
  527. } while (*++syscall);
  528. exit_sig = SIGKILL;
  529. ret = SECCOMP_RET_KILL;
  530. break;
  531. #ifdef CONFIG_SECCOMP_FILTER
  532. case SECCOMP_MODE_FILTER: {
  533. int data;
  534. ret = seccomp_run_filters(this_syscall);
  535. data = ret & SECCOMP_RET_DATA;
  536. ret &= SECCOMP_RET_ACTION;
  537. switch (ret) {
  538. case SECCOMP_RET_ERRNO:
  539. /* Set the low-order 16-bits as a errno. */
  540. syscall_set_return_value(current, task_pt_regs(current),
  541. -data, 0);
  542. goto skip;
  543. case SECCOMP_RET_TRAP:
  544. /* Show the handler the original registers. */
  545. syscall_rollback(current, task_pt_regs(current));
  546. /* Let the filter pass back 16 bits of data. */
  547. seccomp_send_sigsys(this_syscall, data);
  548. goto skip;
  549. case SECCOMP_RET_TRACE:
  550. /* Skip these calls if there is no tracer. */
  551. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  552. /* Make sure userspace sees an ENOSYS. */
  553. syscall_set_return_value(current,
  554. task_pt_regs(current), -ENOSYS, 0);
  555. goto skip;
  556. }
  557. /* Allow the BPF to provide the event message */
  558. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  559. /*
  560. * The delivery of a fatal signal during event
  561. * notification may silently skip tracer notification.
  562. * Terminating the task now avoids executing a system
  563. * call that may not be intended.
  564. */
  565. if (fatal_signal_pending(current))
  566. break;
  567. return 0;
  568. case SECCOMP_RET_ALLOW:
  569. return 0;
  570. case SECCOMP_RET_KILL:
  571. default:
  572. break;
  573. }
  574. exit_sig = SIGSYS;
  575. break;
  576. }
  577. #endif
  578. default:
  579. BUG();
  580. }
  581. #ifdef SECCOMP_DEBUG
  582. dump_stack();
  583. #endif
  584. audit_seccomp(this_syscall, exit_sig, ret);
  585. do_exit(exit_sig);
  586. #ifdef CONFIG_SECCOMP_FILTER
  587. skip:
  588. audit_seccomp(this_syscall, exit_sig, ret);
  589. #endif
  590. return -1;
  591. }
  592. long prctl_get_seccomp(void)
  593. {
  594. return current->seccomp.mode;
  595. }
  596. /**
  597. * seccomp_set_mode_strict: internal function for setting strict seccomp
  598. *
  599. * Once current->seccomp.mode is non-zero, it may not be changed.
  600. *
  601. * Returns 0 on success or -EINVAL on failure.
  602. */
  603. static long seccomp_set_mode_strict(void)
  604. {
  605. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  606. long ret = -EINVAL;
  607. spin_lock_irq(&current->sighand->siglock);
  608. if (!seccomp_may_assign_mode(seccomp_mode))
  609. goto out;
  610. #ifdef TIF_NOTSC
  611. disable_TSC();
  612. #endif
  613. seccomp_assign_mode(current, seccomp_mode);
  614. ret = 0;
  615. out:
  616. spin_unlock_irq(&current->sighand->siglock);
  617. return ret;
  618. }
  619. #ifdef CONFIG_SECCOMP_FILTER
  620. /**
  621. * seccomp_set_mode_filter: internal function for setting seccomp filter
  622. * @flags: flags to change filter behavior
  623. * @filter: struct sock_fprog containing filter
  624. *
  625. * This function may be called repeatedly to install additional filters.
  626. * Every filter successfully installed will be evaluated (in reverse order)
  627. * for each system call the task makes.
  628. *
  629. * Once current->seccomp.mode is non-zero, it may not be changed.
  630. *
  631. * Returns 0 on success or -EINVAL on failure.
  632. */
  633. static long seccomp_set_mode_filter(unsigned int flags,
  634. const char __user *filter)
  635. {
  636. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  637. struct seccomp_filter *prepared = NULL;
  638. long ret = -EINVAL;
  639. /* Validate flags. */
  640. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  641. return -EINVAL;
  642. /* Prepare the new filter before holding any locks. */
  643. prepared = seccomp_prepare_user_filter(filter);
  644. if (IS_ERR(prepared))
  645. return PTR_ERR(prepared);
  646. /*
  647. * Make sure we cannot change seccomp or nnp state via TSYNC
  648. * while another thread is in the middle of calling exec.
  649. */
  650. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  651. mutex_lock_killable(&current->signal->cred_guard_mutex))
  652. goto out_free;
  653. spin_lock_irq(&current->sighand->siglock);
  654. if (!seccomp_may_assign_mode(seccomp_mode))
  655. goto out;
  656. ret = seccomp_attach_filter(flags, prepared);
  657. if (ret)
  658. goto out;
  659. /* Do not free the successfully attached filter. */
  660. prepared = NULL;
  661. seccomp_assign_mode(current, seccomp_mode);
  662. out:
  663. spin_unlock_irq(&current->sighand->siglock);
  664. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  665. mutex_unlock(&current->signal->cred_guard_mutex);
  666. out_free:
  667. seccomp_filter_free(prepared);
  668. return ret;
  669. }
  670. #else
  671. static inline long seccomp_set_mode_filter(unsigned int flags,
  672. const char __user *filter)
  673. {
  674. return -EINVAL;
  675. }
  676. #endif
  677. /* Common entry point for both prctl and syscall. */
  678. static long do_seccomp(unsigned int op, unsigned int flags,
  679. const char __user *uargs)
  680. {
  681. switch (op) {
  682. case SECCOMP_SET_MODE_STRICT:
  683. if (flags != 0 || uargs != NULL)
  684. return -EINVAL;
  685. return seccomp_set_mode_strict();
  686. case SECCOMP_SET_MODE_FILTER:
  687. return seccomp_set_mode_filter(flags, uargs);
  688. default:
  689. return -EINVAL;
  690. }
  691. }
  692. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  693. const char __user *, uargs)
  694. {
  695. return do_seccomp(op, flags, uargs);
  696. }
  697. /**
  698. * prctl_set_seccomp: configures current->seccomp.mode
  699. * @seccomp_mode: requested mode to use
  700. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  701. *
  702. * Returns 0 on success or -EINVAL on failure.
  703. */
  704. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  705. {
  706. unsigned int op;
  707. char __user *uargs;
  708. switch (seccomp_mode) {
  709. case SECCOMP_MODE_STRICT:
  710. op = SECCOMP_SET_MODE_STRICT;
  711. /*
  712. * Setting strict mode through prctl always ignored filter,
  713. * so make sure it is always NULL here to pass the internal
  714. * check in do_seccomp().
  715. */
  716. uargs = NULL;
  717. break;
  718. case SECCOMP_MODE_FILTER:
  719. op = SECCOMP_SET_MODE_FILTER;
  720. uargs = filter;
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. /* prctl interface doesn't have flags, so they are always zero. */
  726. return do_seccomp(op, 0, uargs);
  727. }