seccomp.c 13 KB

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