seccomp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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_LSH_K:
  155. case BPF_S_ALU_LSH_X:
  156. case BPF_S_ALU_RSH_K:
  157. case BPF_S_ALU_RSH_X:
  158. case BPF_S_ALU_NEG:
  159. case BPF_S_LD_IMM:
  160. case BPF_S_LDX_IMM:
  161. case BPF_S_MISC_TAX:
  162. case BPF_S_MISC_TXA:
  163. case BPF_S_ALU_DIV_K:
  164. case BPF_S_LD_MEM:
  165. case BPF_S_LDX_MEM:
  166. case BPF_S_ST:
  167. case BPF_S_STX:
  168. case BPF_S_JMP_JA:
  169. case BPF_S_JMP_JEQ_K:
  170. case BPF_S_JMP_JEQ_X:
  171. case BPF_S_JMP_JGE_K:
  172. case BPF_S_JMP_JGE_X:
  173. case BPF_S_JMP_JGT_K:
  174. case BPF_S_JMP_JGT_X:
  175. case BPF_S_JMP_JSET_K:
  176. case BPF_S_JMP_JSET_X:
  177. continue;
  178. default:
  179. return -EINVAL;
  180. }
  181. }
  182. return 0;
  183. }
  184. /**
  185. * seccomp_run_filters - evaluates all seccomp filters against @syscall
  186. * @syscall: number of the current system call
  187. *
  188. * Returns valid seccomp BPF response codes.
  189. */
  190. static u32 seccomp_run_filters(int syscall)
  191. {
  192. struct seccomp_filter *f;
  193. u32 ret = SECCOMP_RET_ALLOW;
  194. /* Ensure unexpected behavior doesn't result in failing open. */
  195. if (WARN_ON(current->seccomp.filter == NULL))
  196. return SECCOMP_RET_KILL;
  197. /*
  198. * All filters in the list are evaluated and the lowest BPF return
  199. * value always takes priority (ignoring the DATA).
  200. */
  201. for (f = current->seccomp.filter; f; f = f->prev) {
  202. u32 cur_ret = sk_run_filter(NULL, f->insns);
  203. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  204. ret = cur_ret;
  205. }
  206. return ret;
  207. }
  208. /**
  209. * seccomp_attach_filter: Attaches a seccomp filter to current.
  210. * @fprog: BPF program to install
  211. *
  212. * Returns 0 on success or an errno on failure.
  213. */
  214. static long seccomp_attach_filter(struct sock_fprog *fprog)
  215. {
  216. struct seccomp_filter *filter;
  217. unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
  218. unsigned long total_insns = fprog->len;
  219. long ret;
  220. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  221. return -EINVAL;
  222. for (filter = current->seccomp.filter; filter; filter = filter->prev)
  223. total_insns += filter->len + 4; /* include a 4 instr penalty */
  224. if (total_insns > MAX_INSNS_PER_PATH)
  225. return -ENOMEM;
  226. /*
  227. * Installing a seccomp filter requires that the task have
  228. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  229. * This avoids scenarios where unprivileged tasks can affect the
  230. * behavior of privileged children.
  231. */
  232. if (!current->no_new_privs &&
  233. security_capable_noaudit(current_cred(), current_user_ns(),
  234. CAP_SYS_ADMIN) != 0)
  235. return -EACCES;
  236. /* Allocate a new seccomp_filter */
  237. filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
  238. GFP_KERNEL|__GFP_NOWARN);
  239. if (!filter)
  240. return -ENOMEM;
  241. atomic_set(&filter->usage, 1);
  242. filter->len = fprog->len;
  243. /* Copy the instructions from fprog. */
  244. ret = -EFAULT;
  245. if (copy_from_user(filter->insns, fprog->filter, fp_size))
  246. goto fail;
  247. /* Check and rewrite the fprog via the skb checker */
  248. ret = sk_chk_filter(filter->insns, filter->len);
  249. if (ret)
  250. goto fail;
  251. /* Check and rewrite the fprog for seccomp use */
  252. ret = seccomp_check_filter(filter->insns, filter->len);
  253. if (ret)
  254. goto fail;
  255. /*
  256. * If there is an existing filter, make it the prev and don't drop its
  257. * task reference.
  258. */
  259. filter->prev = current->seccomp.filter;
  260. current->seccomp.filter = filter;
  261. return 0;
  262. fail:
  263. kfree(filter);
  264. return ret;
  265. }
  266. /**
  267. * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
  268. * @user_filter: pointer to the user data containing a sock_fprog.
  269. *
  270. * Returns 0 on success and non-zero otherwise.
  271. */
  272. long seccomp_attach_user_filter(char __user *user_filter)
  273. {
  274. struct sock_fprog fprog;
  275. long ret = -EFAULT;
  276. #ifdef CONFIG_COMPAT
  277. if (is_compat_task()) {
  278. struct compat_sock_fprog fprog32;
  279. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  280. goto out;
  281. fprog.len = fprog32.len;
  282. fprog.filter = compat_ptr(fprog32.filter);
  283. } else /* falls through to the if below. */
  284. #endif
  285. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  286. goto out;
  287. ret = seccomp_attach_filter(&fprog);
  288. out:
  289. return ret;
  290. }
  291. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  292. void get_seccomp_filter(struct task_struct *tsk)
  293. {
  294. struct seccomp_filter *orig = tsk->seccomp.filter;
  295. if (!orig)
  296. return;
  297. /* Reference count is bounded by the number of total processes. */
  298. atomic_inc(&orig->usage);
  299. }
  300. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  301. void put_seccomp_filter(struct task_struct *tsk)
  302. {
  303. struct seccomp_filter *orig = tsk->seccomp.filter;
  304. /* Clean up single-reference branches iteratively. */
  305. while (orig && atomic_dec_and_test(&orig->usage)) {
  306. struct seccomp_filter *freeme = orig;
  307. orig = orig->prev;
  308. kfree(freeme);
  309. }
  310. }
  311. /**
  312. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  313. * @syscall: syscall number to send to userland
  314. * @reason: filter-supplied reason code to send to userland (via si_errno)
  315. *
  316. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  317. */
  318. static void seccomp_send_sigsys(int syscall, int reason)
  319. {
  320. struct siginfo info;
  321. memset(&info, 0, sizeof(info));
  322. info.si_signo = SIGSYS;
  323. info.si_code = SYS_SECCOMP;
  324. info.si_call_addr = (void __user *)KSTK_EIP(current);
  325. info.si_errno = reason;
  326. info.si_arch = syscall_get_arch(current, task_pt_regs(current));
  327. info.si_syscall = syscall;
  328. force_sig_info(SIGSYS, &info, current);
  329. }
  330. #endif /* CONFIG_SECCOMP_FILTER */
  331. /*
  332. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  333. * To be fully secure this must be combined with rlimit
  334. * to limit the stack allocations too.
  335. */
  336. static int mode1_syscalls[] = {
  337. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  338. 0, /* null terminated */
  339. };
  340. #ifdef CONFIG_COMPAT
  341. static int mode1_syscalls_32[] = {
  342. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  343. 0, /* null terminated */
  344. };
  345. #endif
  346. int __secure_computing(int this_syscall)
  347. {
  348. int mode = current->seccomp.mode;
  349. int exit_sig = 0;
  350. int *syscall;
  351. u32 ret;
  352. switch (mode) {
  353. case SECCOMP_MODE_STRICT:
  354. syscall = mode1_syscalls;
  355. #ifdef CONFIG_COMPAT
  356. if (is_compat_task())
  357. syscall = mode1_syscalls_32;
  358. #endif
  359. do {
  360. if (*syscall == this_syscall)
  361. return 0;
  362. } while (*++syscall);
  363. exit_sig = SIGKILL;
  364. ret = SECCOMP_RET_KILL;
  365. break;
  366. #ifdef CONFIG_SECCOMP_FILTER
  367. case SECCOMP_MODE_FILTER: {
  368. int data;
  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, task_pt_regs(current),
  376. -data, 0);
  377. goto skip;
  378. case SECCOMP_RET_TRAP:
  379. /* Show the handler the original registers. */
  380. syscall_rollback(current, task_pt_regs(current));
  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. goto skip;
  388. /* Allow the BPF to provide the event message */
  389. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  390. /*
  391. * The delivery of a fatal signal during event
  392. * notification may silently skip tracer notification.
  393. * Terminating the task now avoids executing a system
  394. * call that may not be intended.
  395. */
  396. if (fatal_signal_pending(current))
  397. break;
  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. }