seccomp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 insns[];
  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. * get_u32 - returns a u32 offset into data
  60. * @data: a unsigned 64 bit value
  61. * @index: 0 or 1 to return the first or second 32-bits
  62. *
  63. * This inline exists to hide the length of unsigned long. If a 32-bit
  64. * unsigned long is passed in, it will be extended and the top 32-bits will be
  65. * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
  66. * properly returned.
  67. *
  68. * Endianness is explicitly ignored and left for BPF program authors to manage
  69. * as per the specific architecture.
  70. */
  71. static inline u32 get_u32(u64 data, int index)
  72. {
  73. return ((u32 *)&data)[index];
  74. }
  75. /* Helper for bpf_load below. */
  76. #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
  77. /**
  78. * bpf_load: checks and returns a pointer to the requested offset
  79. * @off: offset into struct seccomp_data to load from
  80. *
  81. * Returns the requested 32-bits of data.
  82. * seccomp_check_filter() should assure that @off is 32-bit aligned
  83. * and not out of bounds. Failure to do so is a BUG.
  84. */
  85. u32 seccomp_bpf_load(int off)
  86. {
  87. struct pt_regs *regs = task_pt_regs(current);
  88. if (off == BPF_DATA(nr))
  89. return syscall_get_nr(current, regs);
  90. if (off == BPF_DATA(arch))
  91. return syscall_get_arch(current, regs);
  92. if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
  93. unsigned long value;
  94. int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
  95. int index = !!(off % sizeof(u64));
  96. syscall_get_arguments(current, regs, arg, 1, &value);
  97. return get_u32(value, index);
  98. }
  99. if (off == BPF_DATA(instruction_pointer))
  100. return get_u32(KSTK_EIP(current), 0);
  101. if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
  102. return get_u32(KSTK_EIP(current), 1);
  103. /* seccomp_check_filter should make this impossible. */
  104. BUG();
  105. }
  106. /**
  107. * seccomp_check_filter - verify seccomp filter code
  108. * @filter: filter to verify
  109. * @flen: length of filter
  110. *
  111. * Takes a previously checked filter (by sk_chk_filter) and
  112. * redirects all filter code that loads struct sk_buff data
  113. * and related data through seccomp_bpf_load. It also
  114. * enforces length and alignment checking of those loads.
  115. *
  116. * Returns 0 if the rule set is legal or -EINVAL if not.
  117. */
  118. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  119. {
  120. int pc;
  121. for (pc = 0; pc < flen; pc++) {
  122. struct sock_filter *ftest = &filter[pc];
  123. u16 code = ftest->code;
  124. u32 k = ftest->k;
  125. switch (code) {
  126. case BPF_S_LD_W_ABS:
  127. ftest->code = BPF_S_ANC_SECCOMP_LD_W;
  128. /* 32-bit aligned and not out of bounds. */
  129. if (k >= sizeof(struct seccomp_data) || k & 3)
  130. return -EINVAL;
  131. continue;
  132. case BPF_S_LD_W_LEN:
  133. ftest->code = BPF_S_LD_IMM;
  134. ftest->k = sizeof(struct seccomp_data);
  135. continue;
  136. case BPF_S_LDX_W_LEN:
  137. ftest->code = BPF_S_LDX_IMM;
  138. ftest->k = sizeof(struct seccomp_data);
  139. continue;
  140. /* Explicitly include allowed calls. */
  141. case BPF_S_RET_K:
  142. case BPF_S_RET_A:
  143. case BPF_S_ALU_ADD_K:
  144. case BPF_S_ALU_ADD_X:
  145. case BPF_S_ALU_SUB_K:
  146. case BPF_S_ALU_SUB_X:
  147. case BPF_S_ALU_MUL_K:
  148. case BPF_S_ALU_MUL_X:
  149. case BPF_S_ALU_DIV_X:
  150. case BPF_S_ALU_AND_K:
  151. case BPF_S_ALU_AND_X:
  152. case BPF_S_ALU_OR_K:
  153. case BPF_S_ALU_OR_X:
  154. case BPF_S_ALU_XOR_K:
  155. case BPF_S_ALU_XOR_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;
  195. u32 ret = SECCOMP_RET_ALLOW;
  196. /* Ensure unexpected behavior doesn't result in failing open. */
  197. if (WARN_ON(current->seccomp.filter == NULL))
  198. return SECCOMP_RET_KILL;
  199. /*
  200. * All filters in the list are evaluated and the lowest BPF return
  201. * value always takes priority (ignoring the DATA).
  202. */
  203. for (f = current->seccomp.filter; f; f = f->prev) {
  204. u32 cur_ret = sk_run_filter(NULL, f->insns);
  205. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  206. ret = cur_ret;
  207. }
  208. return ret;
  209. }
  210. /**
  211. * seccomp_attach_filter: Attaches a seccomp filter to current.
  212. * @fprog: BPF program to install
  213. *
  214. * Returns 0 on success or an errno on failure.
  215. */
  216. static long seccomp_attach_filter(struct sock_fprog *fprog)
  217. {
  218. struct seccomp_filter *filter;
  219. unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
  220. unsigned long total_insns = fprog->len;
  221. long ret;
  222. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  223. return -EINVAL;
  224. for (filter = current->seccomp.filter; filter; filter = filter->prev)
  225. total_insns += filter->len + 4; /* include a 4 instr penalty */
  226. if (total_insns > MAX_INSNS_PER_PATH)
  227. return -ENOMEM;
  228. /*
  229. * Installing a seccomp filter requires that the task have
  230. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  231. * This avoids scenarios where unprivileged tasks can affect the
  232. * behavior of privileged children.
  233. */
  234. if (!current->no_new_privs &&
  235. security_capable_noaudit(current_cred(), current_user_ns(),
  236. CAP_SYS_ADMIN) != 0)
  237. return -EACCES;
  238. /* Allocate a new seccomp_filter */
  239. filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
  240. GFP_KERNEL|__GFP_NOWARN);
  241. if (!filter)
  242. return -ENOMEM;
  243. atomic_set(&filter->usage, 1);
  244. filter->len = fprog->len;
  245. /* Copy the instructions from fprog. */
  246. ret = -EFAULT;
  247. if (copy_from_user(filter->insns, fprog->filter, fp_size))
  248. goto fail;
  249. /* Check and rewrite the fprog via the skb checker */
  250. ret = sk_chk_filter(filter->insns, filter->len);
  251. if (ret)
  252. goto fail;
  253. /* Check and rewrite the fprog for seccomp use */
  254. ret = seccomp_check_filter(filter->insns, filter->len);
  255. if (ret)
  256. goto fail;
  257. /*
  258. * If there is an existing filter, make it the prev and don't drop its
  259. * task reference.
  260. */
  261. filter->prev = current->seccomp.filter;
  262. current->seccomp.filter = filter;
  263. return 0;
  264. fail:
  265. kfree(filter);
  266. return ret;
  267. }
  268. /**
  269. * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
  270. * @user_filter: pointer to the user data containing a sock_fprog.
  271. *
  272. * Returns 0 on success and non-zero otherwise.
  273. */
  274. long seccomp_attach_user_filter(char __user *user_filter)
  275. {
  276. struct sock_fprog fprog;
  277. long ret = -EFAULT;
  278. #ifdef CONFIG_COMPAT
  279. if (is_compat_task()) {
  280. struct compat_sock_fprog fprog32;
  281. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  282. goto out;
  283. fprog.len = fprog32.len;
  284. fprog.filter = compat_ptr(fprog32.filter);
  285. } else /* falls through to the if below. */
  286. #endif
  287. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  288. goto out;
  289. ret = seccomp_attach_filter(&fprog);
  290. out:
  291. return ret;
  292. }
  293. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  294. void get_seccomp_filter(struct task_struct *tsk)
  295. {
  296. struct seccomp_filter *orig = tsk->seccomp.filter;
  297. if (!orig)
  298. return;
  299. /* Reference count is bounded by the number of total processes. */
  300. atomic_inc(&orig->usage);
  301. }
  302. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  303. void put_seccomp_filter(struct task_struct *tsk)
  304. {
  305. struct seccomp_filter *orig = tsk->seccomp.filter;
  306. /* Clean up single-reference branches iteratively. */
  307. while (orig && atomic_dec_and_test(&orig->usage)) {
  308. struct seccomp_filter *freeme = orig;
  309. orig = orig->prev;
  310. kfree(freeme);
  311. }
  312. }
  313. /**
  314. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  315. * @syscall: syscall number to send to userland
  316. * @reason: filter-supplied reason code to send to userland (via si_errno)
  317. *
  318. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  319. */
  320. static void seccomp_send_sigsys(int syscall, int reason)
  321. {
  322. struct siginfo info;
  323. memset(&info, 0, sizeof(info));
  324. info.si_signo = SIGSYS;
  325. info.si_code = SYS_SECCOMP;
  326. info.si_call_addr = (void __user *)KSTK_EIP(current);
  327. info.si_errno = reason;
  328. info.si_arch = syscall_get_arch(current, task_pt_regs(current));
  329. info.si_syscall = syscall;
  330. force_sig_info(SIGSYS, &info, current);
  331. }
  332. #endif /* CONFIG_SECCOMP_FILTER */
  333. /*
  334. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  335. * To be fully secure this must be combined with rlimit
  336. * to limit the stack allocations too.
  337. */
  338. static int mode1_syscalls[] = {
  339. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  340. 0, /* null terminated */
  341. };
  342. #ifdef CONFIG_COMPAT
  343. static int mode1_syscalls_32[] = {
  344. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  345. 0, /* null terminated */
  346. };
  347. #endif
  348. int __secure_computing(int this_syscall)
  349. {
  350. int mode = current->seccomp.mode;
  351. int exit_sig = 0;
  352. int *syscall;
  353. u32 ret;
  354. switch (mode) {
  355. case SECCOMP_MODE_STRICT:
  356. syscall = mode1_syscalls;
  357. #ifdef CONFIG_COMPAT
  358. if (is_compat_task())
  359. syscall = mode1_syscalls_32;
  360. #endif
  361. do {
  362. if (*syscall == this_syscall)
  363. return 0;
  364. } while (*++syscall);
  365. exit_sig = SIGKILL;
  366. ret = SECCOMP_RET_KILL;
  367. break;
  368. #ifdef CONFIG_SECCOMP_FILTER
  369. case SECCOMP_MODE_FILTER: {
  370. int data;
  371. struct pt_regs *regs = task_pt_regs(current);
  372. ret = seccomp_run_filters(this_syscall);
  373. data = ret & SECCOMP_RET_DATA;
  374. ret &= SECCOMP_RET_ACTION;
  375. switch (ret) {
  376. case SECCOMP_RET_ERRNO:
  377. /* Set the low-order 16-bits as a errno. */
  378. syscall_set_return_value(current, regs,
  379. -data, 0);
  380. goto skip;
  381. case SECCOMP_RET_TRAP:
  382. /* Show the handler the original registers. */
  383. syscall_rollback(current, regs);
  384. /* Let the filter pass back 16 bits of data. */
  385. seccomp_send_sigsys(this_syscall, data);
  386. goto skip;
  387. case SECCOMP_RET_TRACE:
  388. /* Skip these calls if there is no tracer. */
  389. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  390. syscall_set_return_value(current, regs,
  391. -ENOSYS, 0);
  392. goto skip;
  393. }
  394. /* Allow the BPF to provide the event message */
  395. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  396. /*
  397. * The delivery of a fatal signal during event
  398. * notification may silently skip tracer notification.
  399. * Terminating the task now avoids executing a system
  400. * call that may not be intended.
  401. */
  402. if (fatal_signal_pending(current))
  403. break;
  404. if (syscall_get_nr(current, regs) < 0)
  405. goto skip; /* Explicit request to skip. */
  406. return 0;
  407. case SECCOMP_RET_ALLOW:
  408. return 0;
  409. case SECCOMP_RET_KILL:
  410. default:
  411. break;
  412. }
  413. exit_sig = SIGSYS;
  414. break;
  415. }
  416. #endif
  417. default:
  418. BUG();
  419. }
  420. #ifdef SECCOMP_DEBUG
  421. dump_stack();
  422. #endif
  423. audit_seccomp(this_syscall, exit_sig, ret);
  424. do_exit(exit_sig);
  425. #ifdef CONFIG_SECCOMP_FILTER
  426. skip:
  427. audit_seccomp(this_syscall, exit_sig, ret);
  428. #endif
  429. return -1;
  430. }
  431. long prctl_get_seccomp(void)
  432. {
  433. return current->seccomp.mode;
  434. }
  435. /**
  436. * prctl_set_seccomp: configures current->seccomp.mode
  437. * @seccomp_mode: requested mode to use
  438. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  439. *
  440. * This function may be called repeatedly with a @seccomp_mode of
  441. * SECCOMP_MODE_FILTER to install additional filters. Every filter
  442. * successfully installed will be evaluated (in reverse order) for each system
  443. * call the task makes.
  444. *
  445. * Once current->seccomp.mode is non-zero, it may not be changed.
  446. *
  447. * Returns 0 on success or -EINVAL on failure.
  448. */
  449. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  450. {
  451. long ret = -EINVAL;
  452. if (current->seccomp.mode &&
  453. current->seccomp.mode != seccomp_mode)
  454. goto out;
  455. switch (seccomp_mode) {
  456. case SECCOMP_MODE_STRICT:
  457. ret = 0;
  458. #ifdef TIF_NOTSC
  459. disable_TSC();
  460. #endif
  461. break;
  462. #ifdef CONFIG_SECCOMP_FILTER
  463. case SECCOMP_MODE_FILTER:
  464. ret = seccomp_attach_user_filter(filter);
  465. if (ret)
  466. goto out;
  467. break;
  468. #endif
  469. default:
  470. goto out;
  471. }
  472. current->seccomp.mode = seccomp_mode;
  473. set_thread_flag(TIF_SECCOMP);
  474. out:
  475. return ret;
  476. }