seccomp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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/ptrace.h>
  27. #include <linux/security.h>
  28. #include <linux/tracehook.h>
  29. #include <linux/uaccess.h>
  30. /**
  31. * struct seccomp_filter - container for seccomp BPF programs
  32. *
  33. * @usage: reference count to manage the object lifetime.
  34. * get/put helpers should be used when accessing an instance
  35. * outside of a lifetime-guarded section. In general, this
  36. * is only needed for handling filters shared across tasks.
  37. * @prev: points to a previously installed, or inherited, filter
  38. * @len: the number of instructions in the program
  39. * @insnsi: the BPF program instructions to evaluate
  40. *
  41. * seccomp_filter objects are organized in a tree linked via the @prev
  42. * pointer. For any task, it appears to be a singly-linked list starting
  43. * with current->seccomp.filter, the most recently attached or inherited filter.
  44. * However, multiple filters may share a @prev node, by way of fork(), which
  45. * results in a unidirectional tree existing in memory. This is similar to
  46. * how namespaces work.
  47. *
  48. * seccomp_filter objects should never be modified after being attached
  49. * to a task_struct (other than @usage).
  50. */
  51. struct seccomp_filter {
  52. atomic_t usage;
  53. struct seccomp_filter *prev;
  54. struct sk_filter *prog;
  55. };
  56. /* Limit any path through the tree to 256KB worth of instructions. */
  57. #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
  58. /*
  59. * Endianness is explicitly ignored and left for BPF program authors to manage
  60. * as per the specific architecture.
  61. */
  62. static void populate_seccomp_data(struct seccomp_data *sd)
  63. {
  64. struct task_struct *task = current;
  65. struct pt_regs *regs = task_pt_regs(task);
  66. unsigned long args[6];
  67. sd->nr = syscall_get_nr(task, regs);
  68. sd->arch = syscall_get_arch();
  69. syscall_get_arguments(task, regs, 0, 6, args);
  70. sd->args[0] = args[0];
  71. sd->args[1] = args[1];
  72. sd->args[2] = args[2];
  73. sd->args[3] = args[3];
  74. sd->args[4] = args[4];
  75. sd->args[5] = args[5];
  76. sd->instruction_pointer = KSTK_EIP(task);
  77. }
  78. /**
  79. * seccomp_check_filter - verify seccomp filter code
  80. * @filter: filter to verify
  81. * @flen: length of filter
  82. *
  83. * Takes a previously checked filter (by sk_chk_filter) and
  84. * redirects all filter code that loads struct sk_buff data
  85. * and related data through seccomp_bpf_load. It also
  86. * enforces length and alignment checking of those loads.
  87. *
  88. * Returns 0 if the rule set is legal or -EINVAL if not.
  89. */
  90. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  91. {
  92. int pc;
  93. for (pc = 0; pc < flen; pc++) {
  94. struct sock_filter *ftest = &filter[pc];
  95. u16 code = ftest->code;
  96. u32 k = ftest->k;
  97. switch (code) {
  98. case BPF_LD | BPF_W | BPF_ABS:
  99. ftest->code = BPF_LDX | BPF_W | BPF_ABS;
  100. /* 32-bit aligned and not out of bounds. */
  101. if (k >= sizeof(struct seccomp_data) || k & 3)
  102. return -EINVAL;
  103. continue;
  104. case BPF_LD | BPF_W | BPF_LEN:
  105. ftest->code = BPF_LD | BPF_IMM;
  106. ftest->k = sizeof(struct seccomp_data);
  107. continue;
  108. case BPF_LDX | BPF_W | BPF_LEN:
  109. ftest->code = BPF_LDX | BPF_IMM;
  110. ftest->k = sizeof(struct seccomp_data);
  111. continue;
  112. /* Explicitly include allowed calls. */
  113. case BPF_RET | BPF_K:
  114. case BPF_RET | BPF_A:
  115. case BPF_ALU | BPF_ADD | BPF_K:
  116. case BPF_ALU | BPF_ADD | BPF_X:
  117. case BPF_ALU | BPF_SUB | BPF_K:
  118. case BPF_ALU | BPF_SUB | BPF_X:
  119. case BPF_ALU | BPF_MUL | BPF_K:
  120. case BPF_ALU | BPF_MUL | BPF_X:
  121. case BPF_ALU | BPF_DIV | BPF_K:
  122. case BPF_ALU | BPF_DIV | BPF_X:
  123. case BPF_ALU | BPF_AND | BPF_K:
  124. case BPF_ALU | BPF_AND | BPF_X:
  125. case BPF_ALU | BPF_OR | BPF_K:
  126. case BPF_ALU | BPF_OR | BPF_X:
  127. case BPF_ALU | BPF_XOR | BPF_K:
  128. case BPF_ALU | BPF_XOR | BPF_X:
  129. case BPF_ALU | BPF_LSH | BPF_K:
  130. case BPF_ALU | BPF_LSH | BPF_X:
  131. case BPF_ALU | BPF_RSH | BPF_K:
  132. case BPF_ALU | BPF_RSH | BPF_X:
  133. case BPF_ALU | BPF_NEG:
  134. case BPF_LD | BPF_IMM:
  135. case BPF_LDX | BPF_IMM:
  136. case BPF_MISC | BPF_TAX:
  137. case BPF_MISC | BPF_TXA:
  138. case BPF_LD | BPF_MEM:
  139. case BPF_LDX | BPF_MEM:
  140. case BPF_ST:
  141. case BPF_STX:
  142. case BPF_JMP | BPF_JA:
  143. case BPF_JMP | BPF_JEQ | BPF_K:
  144. case BPF_JMP | BPF_JEQ | BPF_X:
  145. case BPF_JMP | BPF_JGE | BPF_K:
  146. case BPF_JMP | BPF_JGE | BPF_X:
  147. case BPF_JMP | BPF_JGT | BPF_K:
  148. case BPF_JMP | BPF_JGT | BPF_X:
  149. case BPF_JMP | BPF_JSET | BPF_K:
  150. case BPF_JMP | BPF_JSET | BPF_X:
  151. continue;
  152. default:
  153. return -EINVAL;
  154. }
  155. }
  156. return 0;
  157. }
  158. /**
  159. * seccomp_run_filters - evaluates all seccomp filters against @syscall
  160. * @syscall: number of the current system call
  161. *
  162. * Returns valid seccomp BPF response codes.
  163. */
  164. static u32 seccomp_run_filters(int syscall)
  165. {
  166. struct seccomp_filter *f;
  167. struct seccomp_data sd;
  168. u32 ret = SECCOMP_RET_ALLOW;
  169. /* Ensure unexpected behavior doesn't result in failing open. */
  170. if (WARN_ON(current->seccomp.filter == NULL))
  171. return SECCOMP_RET_KILL;
  172. populate_seccomp_data(&sd);
  173. /*
  174. * All filters in the list are evaluated and the lowest BPF return
  175. * value always takes priority (ignoring the DATA).
  176. */
  177. for (f = current->seccomp.filter; f; f = f->prev) {
  178. u32 cur_ret = SK_RUN_FILTER(f->prog, (void *)&sd);
  179. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  180. ret = cur_ret;
  181. }
  182. return ret;
  183. }
  184. #endif /* CONFIG_SECCOMP_FILTER */
  185. static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
  186. {
  187. if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
  188. return false;
  189. return true;
  190. }
  191. static inline void seccomp_assign_mode(unsigned long seccomp_mode)
  192. {
  193. current->seccomp.mode = seccomp_mode;
  194. set_tsk_thread_flag(current, TIF_SECCOMP);
  195. }
  196. #ifdef CONFIG_SECCOMP_FILTER
  197. /**
  198. * seccomp_prepare_filter: Prepares a seccomp filter for use.
  199. * @fprog: BPF program to install
  200. *
  201. * Returns filter on success or an ERR_PTR on failure.
  202. */
  203. static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
  204. {
  205. struct seccomp_filter *filter;
  206. unsigned long fp_size;
  207. struct sock_filter *fp;
  208. int new_len;
  209. long ret;
  210. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  211. return ERR_PTR(-EINVAL);
  212. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  213. fp_size = fprog->len * sizeof(struct sock_filter);
  214. /*
  215. * Installing a seccomp filter requires that the task has
  216. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  217. * This avoids scenarios where unprivileged tasks can affect the
  218. * behavior of privileged children.
  219. */
  220. if (!task_no_new_privs(current) &&
  221. security_capable_noaudit(current_cred(), current_user_ns(),
  222. CAP_SYS_ADMIN) != 0)
  223. return ERR_PTR(-EACCES);
  224. fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
  225. if (!fp)
  226. return ERR_PTR(-ENOMEM);
  227. /* Copy the instructions from fprog. */
  228. ret = -EFAULT;
  229. if (copy_from_user(fp, fprog->filter, fp_size))
  230. goto free_prog;
  231. /* Check and rewrite the fprog via the skb checker */
  232. ret = sk_chk_filter(fp, fprog->len);
  233. if (ret)
  234. goto free_prog;
  235. /* Check and rewrite the fprog for seccomp use */
  236. ret = seccomp_check_filter(fp, fprog->len);
  237. if (ret)
  238. goto free_prog;
  239. /* Convert 'sock_filter' insns to 'sock_filter_int' insns */
  240. ret = sk_convert_filter(fp, fprog->len, NULL, &new_len);
  241. if (ret)
  242. goto free_prog;
  243. /* Allocate a new seccomp_filter */
  244. ret = -ENOMEM;
  245. filter = kzalloc(sizeof(struct seccomp_filter),
  246. GFP_KERNEL|__GFP_NOWARN);
  247. if (!filter)
  248. goto free_prog;
  249. filter->prog = kzalloc(sk_filter_size(new_len),
  250. GFP_KERNEL|__GFP_NOWARN);
  251. if (!filter->prog)
  252. goto free_filter;
  253. ret = sk_convert_filter(fp, fprog->len, filter->prog->insnsi, &new_len);
  254. if (ret)
  255. goto free_filter_prog;
  256. kfree(fp);
  257. atomic_set(&filter->usage, 1);
  258. filter->prog->len = new_len;
  259. sk_filter_select_runtime(filter->prog);
  260. return filter;
  261. free_filter_prog:
  262. kfree(filter->prog);
  263. free_filter:
  264. kfree(filter);
  265. free_prog:
  266. kfree(fp);
  267. return ERR_PTR(ret);
  268. }
  269. /**
  270. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  271. * @user_filter: pointer to the user data containing a sock_fprog.
  272. *
  273. * Returns 0 on success and non-zero otherwise.
  274. */
  275. static struct seccomp_filter *
  276. seccomp_prepare_user_filter(const char __user *user_filter)
  277. {
  278. struct sock_fprog fprog;
  279. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  280. #ifdef CONFIG_COMPAT
  281. if (is_compat_task()) {
  282. struct compat_sock_fprog fprog32;
  283. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  284. goto out;
  285. fprog.len = fprog32.len;
  286. fprog.filter = compat_ptr(fprog32.filter);
  287. } else /* falls through to the if below. */
  288. #endif
  289. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  290. goto out;
  291. filter = seccomp_prepare_filter(&fprog);
  292. out:
  293. return filter;
  294. }
  295. /**
  296. * seccomp_attach_filter: validate and attach filter
  297. * @flags: flags to change filter behavior
  298. * @filter: seccomp filter to add to the current process
  299. *
  300. * Returns 0 on success, -ve on error.
  301. */
  302. static long seccomp_attach_filter(unsigned int flags,
  303. struct seccomp_filter *filter)
  304. {
  305. unsigned long total_insns;
  306. struct seccomp_filter *walker;
  307. /* Validate resulting filter length. */
  308. total_insns = filter->prog->len;
  309. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  310. total_insns += walker->prog->len + 4; /* 4 instr penalty */
  311. if (total_insns > MAX_INSNS_PER_PATH)
  312. return -ENOMEM;
  313. /*
  314. * If there is an existing filter, make it the prev and don't drop its
  315. * task reference.
  316. */
  317. filter->prev = current->seccomp.filter;
  318. current->seccomp.filter = filter;
  319. return 0;
  320. }
  321. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  322. void get_seccomp_filter(struct task_struct *tsk)
  323. {
  324. struct seccomp_filter *orig = tsk->seccomp.filter;
  325. if (!orig)
  326. return;
  327. /* Reference count is bounded by the number of total processes. */
  328. atomic_inc(&orig->usage);
  329. }
  330. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  331. {
  332. if (filter) {
  333. sk_filter_free(filter->prog);
  334. kfree(filter);
  335. }
  336. }
  337. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  338. void put_seccomp_filter(struct task_struct *tsk)
  339. {
  340. struct seccomp_filter *orig = tsk->seccomp.filter;
  341. /* Clean up single-reference branches iteratively. */
  342. while (orig && atomic_dec_and_test(&orig->usage)) {
  343. struct seccomp_filter *freeme = orig;
  344. orig = orig->prev;
  345. seccomp_filter_free(freeme);
  346. }
  347. }
  348. /**
  349. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  350. * @syscall: syscall number to send to userland
  351. * @reason: filter-supplied reason code to send to userland (via si_errno)
  352. *
  353. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  354. */
  355. static void seccomp_send_sigsys(int syscall, int reason)
  356. {
  357. struct siginfo info;
  358. memset(&info, 0, sizeof(info));
  359. info.si_signo = SIGSYS;
  360. info.si_code = SYS_SECCOMP;
  361. info.si_call_addr = (void __user *)KSTK_EIP(current);
  362. info.si_errno = reason;
  363. info.si_arch = syscall_get_arch();
  364. info.si_syscall = syscall;
  365. force_sig_info(SIGSYS, &info, current);
  366. }
  367. #endif /* CONFIG_SECCOMP_FILTER */
  368. /*
  369. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  370. * To be fully secure this must be combined with rlimit
  371. * to limit the stack allocations too.
  372. */
  373. static int mode1_syscalls[] = {
  374. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  375. 0, /* null terminated */
  376. };
  377. #ifdef CONFIG_COMPAT
  378. static int mode1_syscalls_32[] = {
  379. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  380. 0, /* null terminated */
  381. };
  382. #endif
  383. int __secure_computing(int this_syscall)
  384. {
  385. int mode = current->seccomp.mode;
  386. int exit_sig = 0;
  387. int *syscall;
  388. u32 ret;
  389. switch (mode) {
  390. case SECCOMP_MODE_STRICT:
  391. syscall = mode1_syscalls;
  392. #ifdef CONFIG_COMPAT
  393. if (is_compat_task())
  394. syscall = mode1_syscalls_32;
  395. #endif
  396. do {
  397. if (*syscall == this_syscall)
  398. return 0;
  399. } while (*++syscall);
  400. exit_sig = SIGKILL;
  401. ret = SECCOMP_RET_KILL;
  402. break;
  403. #ifdef CONFIG_SECCOMP_FILTER
  404. case SECCOMP_MODE_FILTER: {
  405. int data;
  406. struct pt_regs *regs = task_pt_regs(current);
  407. ret = seccomp_run_filters(this_syscall);
  408. data = ret & SECCOMP_RET_DATA;
  409. ret &= SECCOMP_RET_ACTION;
  410. switch (ret) {
  411. case SECCOMP_RET_ERRNO:
  412. /* Set the low-order 16-bits as a errno. */
  413. syscall_set_return_value(current, regs,
  414. -data, 0);
  415. goto skip;
  416. case SECCOMP_RET_TRAP:
  417. /* Show the handler the original registers. */
  418. syscall_rollback(current, regs);
  419. /* Let the filter pass back 16 bits of data. */
  420. seccomp_send_sigsys(this_syscall, data);
  421. goto skip;
  422. case SECCOMP_RET_TRACE:
  423. /* Skip these calls if there is no tracer. */
  424. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  425. syscall_set_return_value(current, regs,
  426. -ENOSYS, 0);
  427. goto skip;
  428. }
  429. /* Allow the BPF to provide the event message */
  430. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  431. /*
  432. * The delivery of a fatal signal during event
  433. * notification may silently skip tracer notification.
  434. * Terminating the task now avoids executing a system
  435. * call that may not be intended.
  436. */
  437. if (fatal_signal_pending(current))
  438. break;
  439. if (syscall_get_nr(current, regs) < 0)
  440. goto skip; /* Explicit request to skip. */
  441. return 0;
  442. case SECCOMP_RET_ALLOW:
  443. return 0;
  444. case SECCOMP_RET_KILL:
  445. default:
  446. break;
  447. }
  448. exit_sig = SIGSYS;
  449. break;
  450. }
  451. #endif
  452. default:
  453. BUG();
  454. }
  455. #ifdef SECCOMP_DEBUG
  456. dump_stack();
  457. #endif
  458. audit_seccomp(this_syscall, exit_sig, ret);
  459. do_exit(exit_sig);
  460. #ifdef CONFIG_SECCOMP_FILTER
  461. skip:
  462. audit_seccomp(this_syscall, exit_sig, ret);
  463. #endif
  464. return -1;
  465. }
  466. long prctl_get_seccomp(void)
  467. {
  468. return current->seccomp.mode;
  469. }
  470. /**
  471. * seccomp_set_mode_strict: internal function for setting strict seccomp
  472. *
  473. * Once current->seccomp.mode is non-zero, it may not be changed.
  474. *
  475. * Returns 0 on success or -EINVAL on failure.
  476. */
  477. static long seccomp_set_mode_strict(void)
  478. {
  479. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  480. long ret = -EINVAL;
  481. if (!seccomp_may_assign_mode(seccomp_mode))
  482. goto out;
  483. #ifdef TIF_NOTSC
  484. disable_TSC();
  485. #endif
  486. seccomp_assign_mode(seccomp_mode);
  487. ret = 0;
  488. out:
  489. return ret;
  490. }
  491. #ifdef CONFIG_SECCOMP_FILTER
  492. /**
  493. * seccomp_set_mode_filter: internal function for setting seccomp filter
  494. * @flags: flags to change filter behavior
  495. * @filter: struct sock_fprog containing filter
  496. *
  497. * This function may be called repeatedly to install additional filters.
  498. * Every filter successfully installed will be evaluated (in reverse order)
  499. * for each system call the task makes.
  500. *
  501. * Once current->seccomp.mode is non-zero, it may not be changed.
  502. *
  503. * Returns 0 on success or -EINVAL on failure.
  504. */
  505. static long seccomp_set_mode_filter(unsigned int flags,
  506. const char __user *filter)
  507. {
  508. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  509. struct seccomp_filter *prepared = NULL;
  510. long ret = -EINVAL;
  511. /* Validate flags. */
  512. if (flags != 0)
  513. goto out;
  514. /* Prepare the new filter before holding any locks. */
  515. prepared = seccomp_prepare_user_filter(filter);
  516. if (IS_ERR(prepared))
  517. return PTR_ERR(prepared);
  518. if (!seccomp_may_assign_mode(seccomp_mode))
  519. goto out;
  520. ret = seccomp_attach_filter(flags, prepared);
  521. if (ret)
  522. goto out;
  523. /* Do not free the successfully attached filter. */
  524. prepared = NULL;
  525. seccomp_assign_mode(seccomp_mode);
  526. out:
  527. seccomp_filter_free(prepared);
  528. return ret;
  529. }
  530. #else
  531. static inline long seccomp_set_mode_filter(unsigned int flags,
  532. const char __user *filter)
  533. {
  534. return -EINVAL;
  535. }
  536. #endif
  537. /* Common entry point for both prctl and syscall. */
  538. static long do_seccomp(unsigned int op, unsigned int flags,
  539. const char __user *uargs)
  540. {
  541. switch (op) {
  542. case SECCOMP_SET_MODE_STRICT:
  543. if (flags != 0 || uargs != NULL)
  544. return -EINVAL;
  545. return seccomp_set_mode_strict();
  546. case SECCOMP_SET_MODE_FILTER:
  547. return seccomp_set_mode_filter(flags, uargs);
  548. default:
  549. return -EINVAL;
  550. }
  551. }
  552. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  553. const char __user *, uargs)
  554. {
  555. return do_seccomp(op, flags, uargs);
  556. }
  557. /**
  558. * prctl_set_seccomp: configures current->seccomp.mode
  559. * @seccomp_mode: requested mode to use
  560. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  561. *
  562. * Returns 0 on success or -EINVAL on failure.
  563. */
  564. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  565. {
  566. unsigned int op;
  567. char __user *uargs;
  568. switch (seccomp_mode) {
  569. case SECCOMP_MODE_STRICT:
  570. op = SECCOMP_SET_MODE_STRICT;
  571. /*
  572. * Setting strict mode through prctl always ignored filter,
  573. * so make sure it is always NULL here to pass the internal
  574. * check in do_seccomp().
  575. */
  576. uargs = NULL;
  577. break;
  578. case SECCOMP_MODE_FILTER:
  579. op = SECCOMP_SET_MODE_FILTER;
  580. uargs = filter;
  581. break;
  582. default:
  583. return -EINVAL;
  584. }
  585. /* prctl interface doesn't have flags, so they are always zero. */
  586. return do_seccomp(op, 0, uargs);
  587. }