seccomp.c 14 KB

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