seccomp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. * @insns: 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. unsigned short len; /* Instruction count */
  54. struct sock_filter_int insnsi[];
  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. sd->nr = syscall_get_nr(task, regs);
  67. sd->arch = syscall_get_arch();
  68. /* Unroll syscall_get_args to help gcc on arm. */
  69. syscall_get_arguments(task, regs, 0, 1, (unsigned long *) &sd->args[0]);
  70. syscall_get_arguments(task, regs, 1, 1, (unsigned long *) &sd->args[1]);
  71. syscall_get_arguments(task, regs, 2, 1, (unsigned long *) &sd->args[2]);
  72. syscall_get_arguments(task, regs, 3, 1, (unsigned long *) &sd->args[3]);
  73. syscall_get_arguments(task, regs, 4, 1, (unsigned long *) &sd->args[4]);
  74. syscall_get_arguments(task, regs, 5, 1, (unsigned long *) &sd->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_S_LD_W_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_S_LD_W_LEN:
  104. ftest->code = BPF_LD | BPF_IMM;
  105. ftest->k = sizeof(struct seccomp_data);
  106. continue;
  107. case BPF_S_LDX_W_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_S_RET_K:
  113. case BPF_S_RET_A:
  114. case BPF_S_ALU_ADD_K:
  115. case BPF_S_ALU_ADD_X:
  116. case BPF_S_ALU_SUB_K:
  117. case BPF_S_ALU_SUB_X:
  118. case BPF_S_ALU_MUL_K:
  119. case BPF_S_ALU_MUL_X:
  120. case BPF_S_ALU_DIV_X:
  121. case BPF_S_ALU_AND_K:
  122. case BPF_S_ALU_AND_X:
  123. case BPF_S_ALU_OR_K:
  124. case BPF_S_ALU_OR_X:
  125. case BPF_S_ALU_XOR_K:
  126. case BPF_S_ALU_XOR_X:
  127. case BPF_S_ALU_LSH_K:
  128. case BPF_S_ALU_LSH_X:
  129. case BPF_S_ALU_RSH_K:
  130. case BPF_S_ALU_RSH_X:
  131. case BPF_S_ALU_NEG:
  132. case BPF_S_LD_IMM:
  133. case BPF_S_LDX_IMM:
  134. case BPF_S_MISC_TAX:
  135. case BPF_S_MISC_TXA:
  136. case BPF_S_ALU_DIV_K:
  137. case BPF_S_LD_MEM:
  138. case BPF_S_LDX_MEM:
  139. case BPF_S_ST:
  140. case BPF_S_STX:
  141. case BPF_S_JMP_JA:
  142. case BPF_S_JMP_JEQ_K:
  143. case BPF_S_JMP_JEQ_X:
  144. case BPF_S_JMP_JGE_K:
  145. case BPF_S_JMP_JGE_X:
  146. case BPF_S_JMP_JGT_K:
  147. case BPF_S_JMP_JGT_X:
  148. case BPF_S_JMP_JSET_K:
  149. case BPF_S_JMP_JSET_X:
  150. sk_decode_filter(ftest, ftest);
  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_int_seccomp(&sd, f->insnsi);
  179. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  180. ret = cur_ret;
  181. }
  182. return ret;
  183. }
  184. /**
  185. * seccomp_attach_filter: Attaches a seccomp filter to current.
  186. * @fprog: BPF program to install
  187. *
  188. * Returns 0 on success or an errno on failure.
  189. */
  190. static long seccomp_attach_filter(struct sock_fprog *fprog)
  191. {
  192. struct seccomp_filter *filter;
  193. unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
  194. unsigned long total_insns = fprog->len;
  195. struct sock_filter *fp;
  196. int new_len;
  197. long ret;
  198. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  199. return -EINVAL;
  200. for (filter = current->seccomp.filter; filter; filter = filter->prev)
  201. total_insns += filter->len + 4; /* include a 4 instr penalty */
  202. if (total_insns > MAX_INSNS_PER_PATH)
  203. return -ENOMEM;
  204. /*
  205. * Installing a seccomp filter requires that the task have
  206. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  207. * This avoids scenarios where unprivileged tasks can affect the
  208. * behavior of privileged children.
  209. */
  210. if (!current->no_new_privs &&
  211. security_capable_noaudit(current_cred(), current_user_ns(),
  212. CAP_SYS_ADMIN) != 0)
  213. return -EACCES;
  214. fp = kzalloc(fp_size, GFP_KERNEL|__GFP_NOWARN);
  215. if (!fp)
  216. return -ENOMEM;
  217. /* Copy the instructions from fprog. */
  218. ret = -EFAULT;
  219. if (copy_from_user(fp, fprog->filter, fp_size))
  220. goto free_prog;
  221. /* Check and rewrite the fprog via the skb checker */
  222. ret = sk_chk_filter(fp, fprog->len);
  223. if (ret)
  224. goto free_prog;
  225. /* Check and rewrite the fprog for seccomp use */
  226. ret = seccomp_check_filter(fp, fprog->len);
  227. if (ret)
  228. goto free_prog;
  229. /* Convert 'sock_filter' insns to 'sock_filter_int' insns */
  230. ret = sk_convert_filter(fp, fprog->len, NULL, &new_len);
  231. if (ret)
  232. goto free_prog;
  233. /* Allocate a new seccomp_filter */
  234. filter = kzalloc(sizeof(struct seccomp_filter) +
  235. sizeof(struct sock_filter_int) * new_len,
  236. GFP_KERNEL|__GFP_NOWARN);
  237. if (!filter)
  238. goto free_prog;
  239. ret = sk_convert_filter(fp, fprog->len, filter->insnsi, &new_len);
  240. if (ret)
  241. goto free_filter;
  242. atomic_set(&filter->usage, 1);
  243. filter->len = new_len;
  244. /*
  245. * If there is an existing filter, make it the prev and don't drop its
  246. * task reference.
  247. */
  248. filter->prev = current->seccomp.filter;
  249. current->seccomp.filter = filter;
  250. return 0;
  251. free_filter:
  252. kfree(filter);
  253. free_prog:
  254. kfree(fp);
  255. return ret;
  256. }
  257. /**
  258. * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
  259. * @user_filter: pointer to the user data containing a sock_fprog.
  260. *
  261. * Returns 0 on success and non-zero otherwise.
  262. */
  263. static long seccomp_attach_user_filter(char __user *user_filter)
  264. {
  265. struct sock_fprog fprog;
  266. long ret = -EFAULT;
  267. #ifdef CONFIG_COMPAT
  268. if (is_compat_task()) {
  269. struct compat_sock_fprog fprog32;
  270. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  271. goto out;
  272. fprog.len = fprog32.len;
  273. fprog.filter = compat_ptr(fprog32.filter);
  274. } else /* falls through to the if below. */
  275. #endif
  276. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  277. goto out;
  278. ret = seccomp_attach_filter(&fprog);
  279. out:
  280. return ret;
  281. }
  282. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  283. void get_seccomp_filter(struct task_struct *tsk)
  284. {
  285. struct seccomp_filter *orig = tsk->seccomp.filter;
  286. if (!orig)
  287. return;
  288. /* Reference count is bounded by the number of total processes. */
  289. atomic_inc(&orig->usage);
  290. }
  291. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  292. void put_seccomp_filter(struct task_struct *tsk)
  293. {
  294. struct seccomp_filter *orig = tsk->seccomp.filter;
  295. /* Clean up single-reference branches iteratively. */
  296. while (orig && atomic_dec_and_test(&orig->usage)) {
  297. struct seccomp_filter *freeme = orig;
  298. orig = orig->prev;
  299. kfree(freeme);
  300. }
  301. }
  302. /**
  303. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  304. * @syscall: syscall number to send to userland
  305. * @reason: filter-supplied reason code to send to userland (via si_errno)
  306. *
  307. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  308. */
  309. static void seccomp_send_sigsys(int syscall, int reason)
  310. {
  311. struct siginfo info;
  312. memset(&info, 0, sizeof(info));
  313. info.si_signo = SIGSYS;
  314. info.si_code = SYS_SECCOMP;
  315. info.si_call_addr = (void __user *)KSTK_EIP(current);
  316. info.si_errno = reason;
  317. info.si_arch = syscall_get_arch();
  318. info.si_syscall = syscall;
  319. force_sig_info(SIGSYS, &info, current);
  320. }
  321. #endif /* CONFIG_SECCOMP_FILTER */
  322. /*
  323. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  324. * To be fully secure this must be combined with rlimit
  325. * to limit the stack allocations too.
  326. */
  327. static int mode1_syscalls[] = {
  328. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  329. 0, /* null terminated */
  330. };
  331. #ifdef CONFIG_COMPAT
  332. static int mode1_syscalls_32[] = {
  333. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  334. 0, /* null terminated */
  335. };
  336. #endif
  337. int __secure_computing(int this_syscall)
  338. {
  339. int mode = current->seccomp.mode;
  340. int exit_sig = 0;
  341. int *syscall;
  342. u32 ret;
  343. switch (mode) {
  344. case SECCOMP_MODE_STRICT:
  345. syscall = mode1_syscalls;
  346. #ifdef CONFIG_COMPAT
  347. if (is_compat_task())
  348. syscall = mode1_syscalls_32;
  349. #endif
  350. do {
  351. if (*syscall == this_syscall)
  352. return 0;
  353. } while (*++syscall);
  354. exit_sig = SIGKILL;
  355. ret = SECCOMP_RET_KILL;
  356. break;
  357. #ifdef CONFIG_SECCOMP_FILTER
  358. case SECCOMP_MODE_FILTER: {
  359. int data;
  360. struct pt_regs *regs = task_pt_regs(current);
  361. ret = seccomp_run_filters(this_syscall);
  362. data = ret & SECCOMP_RET_DATA;
  363. ret &= SECCOMP_RET_ACTION;
  364. switch (ret) {
  365. case SECCOMP_RET_ERRNO:
  366. /* Set the low-order 16-bits as a errno. */
  367. syscall_set_return_value(current, regs,
  368. -data, 0);
  369. goto skip;
  370. case SECCOMP_RET_TRAP:
  371. /* Show the handler the original registers. */
  372. syscall_rollback(current, regs);
  373. /* Let the filter pass back 16 bits of data. */
  374. seccomp_send_sigsys(this_syscall, data);
  375. goto skip;
  376. case SECCOMP_RET_TRACE:
  377. /* Skip these calls if there is no tracer. */
  378. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  379. syscall_set_return_value(current, regs,
  380. -ENOSYS, 0);
  381. goto skip;
  382. }
  383. /* Allow the BPF to provide the event message */
  384. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  385. /*
  386. * The delivery of a fatal signal during event
  387. * notification may silently skip tracer notification.
  388. * Terminating the task now avoids executing a system
  389. * call that may not be intended.
  390. */
  391. if (fatal_signal_pending(current))
  392. break;
  393. if (syscall_get_nr(current, regs) < 0)
  394. goto skip; /* Explicit request to skip. */
  395. return 0;
  396. case SECCOMP_RET_ALLOW:
  397. return 0;
  398. case SECCOMP_RET_KILL:
  399. default:
  400. break;
  401. }
  402. exit_sig = SIGSYS;
  403. break;
  404. }
  405. #endif
  406. default:
  407. BUG();
  408. }
  409. #ifdef SECCOMP_DEBUG
  410. dump_stack();
  411. #endif
  412. audit_seccomp(this_syscall, exit_sig, ret);
  413. do_exit(exit_sig);
  414. #ifdef CONFIG_SECCOMP_FILTER
  415. skip:
  416. audit_seccomp(this_syscall, exit_sig, ret);
  417. #endif
  418. return -1;
  419. }
  420. long prctl_get_seccomp(void)
  421. {
  422. return current->seccomp.mode;
  423. }
  424. /**
  425. * prctl_set_seccomp: configures current->seccomp.mode
  426. * @seccomp_mode: requested mode to use
  427. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  428. *
  429. * This function may be called repeatedly with a @seccomp_mode of
  430. * SECCOMP_MODE_FILTER to install additional filters. Every filter
  431. * successfully installed will be evaluated (in reverse order) for each system
  432. * call the task makes.
  433. *
  434. * Once current->seccomp.mode is non-zero, it may not be changed.
  435. *
  436. * Returns 0 on success or -EINVAL on failure.
  437. */
  438. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  439. {
  440. long ret = -EINVAL;
  441. if (current->seccomp.mode &&
  442. current->seccomp.mode != seccomp_mode)
  443. goto out;
  444. switch (seccomp_mode) {
  445. case SECCOMP_MODE_STRICT:
  446. ret = 0;
  447. #ifdef TIF_NOTSC
  448. disable_TSC();
  449. #endif
  450. break;
  451. #ifdef CONFIG_SECCOMP_FILTER
  452. case SECCOMP_MODE_FILTER:
  453. ret = seccomp_attach_user_filter(filter);
  454. if (ret)
  455. goto out;
  456. break;
  457. #endif
  458. default:
  459. goto out;
  460. }
  461. current->seccomp.mode = seccomp_mode;
  462. set_thread_flag(TIF_SECCOMP);
  463. out:
  464. return ret;
  465. }