seccomp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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/security.h>
  25. #include <linux/slab.h>
  26. #include <linux/tracehook.h>
  27. #include <linux/uaccess.h>
  28. /**
  29. * struct seccomp_filter - container for seccomp BPF programs
  30. *
  31. * @usage: reference count to manage the object lifetime.
  32. * get/put helpers should be used when accessing an instance
  33. * outside of a lifetime-guarded section. In general, this
  34. * is only needed for handling filters shared across tasks.
  35. * @prev: points to a previously installed, or inherited, filter
  36. * @len: the number of instructions in the program
  37. * @insns: the BPF program instructions to evaluate
  38. *
  39. * seccomp_filter objects are organized in a tree linked via the @prev
  40. * pointer. For any task, it appears to be a singly-linked list starting
  41. * with current->seccomp.filter, the most recently attached or inherited filter.
  42. * However, multiple filters may share a @prev node, by way of fork(), which
  43. * results in a unidirectional tree existing in memory. This is similar to
  44. * how namespaces work.
  45. *
  46. * seccomp_filter objects should never be modified after being attached
  47. * to a task_struct (other than @usage).
  48. */
  49. struct seccomp_filter {
  50. atomic_t usage;
  51. struct seccomp_filter *prev;
  52. unsigned short len; /* Instruction count */
  53. struct sock_filter insns[];
  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. * get_u32 - returns a u32 offset into data
  59. * @data: a unsigned 64 bit value
  60. * @index: 0 or 1 to return the first or second 32-bits
  61. *
  62. * This inline exists to hide the length of unsigned long. If a 32-bit
  63. * unsigned long is passed in, it will be extended and the top 32-bits will be
  64. * 0. If it is a 64-bit unsigned long, then whatever data is resident will be
  65. * properly returned.
  66. *
  67. * Endianness is explicitly ignored and left for BPF program authors to manage
  68. * as per the specific architecture.
  69. */
  70. static inline u32 get_u32(u64 data, int index)
  71. {
  72. return ((u32 *)&data)[index];
  73. }
  74. /* Helper for bpf_load below. */
  75. #define BPF_DATA(_name) offsetof(struct seccomp_data, _name)
  76. /**
  77. * bpf_load: checks and returns a pointer to the requested offset
  78. * @off: offset into struct seccomp_data to load from
  79. *
  80. * Returns the requested 32-bits of data.
  81. * seccomp_check_filter() should assure that @off is 32-bit aligned
  82. * and not out of bounds. Failure to do so is a BUG.
  83. */
  84. u32 seccomp_bpf_load(int off)
  85. {
  86. struct pt_regs *regs = task_pt_regs(current);
  87. if (off == BPF_DATA(nr))
  88. return syscall_get_nr(current, regs);
  89. if (off == BPF_DATA(arch))
  90. return syscall_get_arch(current, regs);
  91. if (off >= BPF_DATA(args[0]) && off < BPF_DATA(args[6])) {
  92. unsigned long value;
  93. int arg = (off - BPF_DATA(args[0])) / sizeof(u64);
  94. int index = !!(off % sizeof(u64));
  95. syscall_get_arguments(current, regs, arg, 1, &value);
  96. return get_u32(value, index);
  97. }
  98. if (off == BPF_DATA(instruction_pointer))
  99. return get_u32(KSTK_EIP(current), 0);
  100. if (off == BPF_DATA(instruction_pointer) + sizeof(u32))
  101. return get_u32(KSTK_EIP(current), 1);
  102. /* seccomp_check_filter should make this impossible. */
  103. BUG();
  104. }
  105. /**
  106. * seccomp_check_filter - verify seccomp filter code
  107. * @filter: filter to verify
  108. * @flen: length of filter
  109. *
  110. * Takes a previously checked filter (by sk_chk_filter) and
  111. * redirects all filter code that loads struct sk_buff data
  112. * and related data through seccomp_bpf_load. It also
  113. * enforces length and alignment checking of those loads.
  114. *
  115. * Returns 0 if the rule set is legal or -EINVAL if not.
  116. */
  117. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  118. {
  119. int pc;
  120. for (pc = 0; pc < flen; pc++) {
  121. struct sock_filter *ftest = &filter[pc];
  122. u16 code = ftest->code;
  123. u32 k = ftest->k;
  124. switch (code) {
  125. case BPF_S_LD_W_ABS:
  126. ftest->code = BPF_S_ANC_SECCOMP_LD_W;
  127. /* 32-bit aligned and not out of bounds. */
  128. if (k >= sizeof(struct seccomp_data) || k & 3)
  129. return -EINVAL;
  130. continue;
  131. case BPF_S_LD_W_LEN:
  132. ftest->code = BPF_S_LD_IMM;
  133. ftest->k = sizeof(struct seccomp_data);
  134. continue;
  135. case BPF_S_LDX_W_LEN:
  136. ftest->code = BPF_S_LDX_IMM;
  137. ftest->k = sizeof(struct seccomp_data);
  138. continue;
  139. /* Explicitly include allowed calls. */
  140. case BPF_S_RET_K:
  141. case BPF_S_RET_A:
  142. case BPF_S_ALU_ADD_K:
  143. case BPF_S_ALU_ADD_X:
  144. case BPF_S_ALU_SUB_K:
  145. case BPF_S_ALU_SUB_X:
  146. case BPF_S_ALU_MUL_K:
  147. case BPF_S_ALU_MUL_X:
  148. case BPF_S_ALU_DIV_X:
  149. case BPF_S_ALU_AND_K:
  150. case BPF_S_ALU_AND_X:
  151. case BPF_S_ALU_OR_K:
  152. case BPF_S_ALU_OR_X:
  153. case BPF_S_ALU_LSH_K:
  154. case BPF_S_ALU_LSH_X:
  155. case BPF_S_ALU_RSH_K:
  156. case BPF_S_ALU_RSH_X:
  157. case BPF_S_ALU_NEG:
  158. case BPF_S_LD_IMM:
  159. case BPF_S_LDX_IMM:
  160. case BPF_S_MISC_TAX:
  161. case BPF_S_MISC_TXA:
  162. case BPF_S_ALU_DIV_K:
  163. case BPF_S_LD_MEM:
  164. case BPF_S_LDX_MEM:
  165. case BPF_S_ST:
  166. case BPF_S_STX:
  167. case BPF_S_JMP_JA:
  168. case BPF_S_JMP_JEQ_K:
  169. case BPF_S_JMP_JEQ_X:
  170. case BPF_S_JMP_JGE_K:
  171. case BPF_S_JMP_JGE_X:
  172. case BPF_S_JMP_JGT_K:
  173. case BPF_S_JMP_JGT_X:
  174. case BPF_S_JMP_JSET_K:
  175. case BPF_S_JMP_JSET_X:
  176. continue;
  177. default:
  178. return -EINVAL;
  179. }
  180. }
  181. return 0;
  182. }
  183. /**
  184. * seccomp_run_filters - evaluates all seccomp filters against @syscall
  185. * @syscall: number of the current system call
  186. *
  187. * Returns valid seccomp BPF response codes.
  188. */
  189. static u32 seccomp_run_filters(int syscall)
  190. {
  191. struct seccomp_filter *f;
  192. u32 ret = SECCOMP_RET_ALLOW;
  193. /* Ensure unexpected behavior doesn't result in failing open. */
  194. if (WARN_ON(current->seccomp.filter == NULL))
  195. return SECCOMP_RET_KILL;
  196. /*
  197. * All filters in the list are evaluated and the lowest BPF return
  198. * value always takes priority (ignoring the DATA).
  199. */
  200. for (f = current->seccomp.filter; f; f = f->prev) {
  201. u32 cur_ret = sk_run_filter(NULL, f->insns);
  202. if ((cur_ret & SECCOMP_RET_ACTION) < (ret & SECCOMP_RET_ACTION))
  203. ret = cur_ret;
  204. }
  205. return ret;
  206. }
  207. /**
  208. * seccomp_attach_filter: Attaches a seccomp filter to current.
  209. * @fprog: BPF program to install
  210. *
  211. * Returns 0 on success or an errno on failure.
  212. */
  213. static long seccomp_attach_filter(struct sock_fprog *fprog)
  214. {
  215. struct seccomp_filter *filter;
  216. unsigned long fp_size = fprog->len * sizeof(struct sock_filter);
  217. unsigned long total_insns = fprog->len;
  218. long ret;
  219. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  220. return -EINVAL;
  221. for (filter = current->seccomp.filter; filter; filter = filter->prev)
  222. total_insns += filter->len + 4; /* include a 4 instr penalty */
  223. if (total_insns > MAX_INSNS_PER_PATH)
  224. return -ENOMEM;
  225. /*
  226. * Installing a seccomp filter requires that the task have
  227. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  228. * This avoids scenarios where unprivileged tasks can affect the
  229. * behavior of privileged children.
  230. */
  231. if (!current->no_new_privs &&
  232. security_capable_noaudit(current_cred(), current_user_ns(),
  233. CAP_SYS_ADMIN) != 0)
  234. return -EACCES;
  235. /* Allocate a new seccomp_filter */
  236. filter = kzalloc(sizeof(struct seccomp_filter) + fp_size,
  237. GFP_KERNEL|__GFP_NOWARN);
  238. if (!filter)
  239. return -ENOMEM;
  240. atomic_set(&filter->usage, 1);
  241. filter->len = fprog->len;
  242. /* Copy the instructions from fprog. */
  243. ret = -EFAULT;
  244. if (copy_from_user(filter->insns, fprog->filter, fp_size))
  245. goto fail;
  246. /* Check and rewrite the fprog via the skb checker */
  247. ret = sk_chk_filter(filter->insns, filter->len);
  248. if (ret)
  249. goto fail;
  250. /* Check and rewrite the fprog for seccomp use */
  251. ret = seccomp_check_filter(filter->insns, filter->len);
  252. if (ret)
  253. goto fail;
  254. /*
  255. * If there is an existing filter, make it the prev and don't drop its
  256. * task reference.
  257. */
  258. filter->prev = current->seccomp.filter;
  259. current->seccomp.filter = filter;
  260. return 0;
  261. fail:
  262. kfree(filter);
  263. return ret;
  264. }
  265. /**
  266. * seccomp_attach_user_filter - attaches a user-supplied sock_fprog
  267. * @user_filter: pointer to the user data containing a sock_fprog.
  268. *
  269. * Returns 0 on success and non-zero otherwise.
  270. */
  271. long seccomp_attach_user_filter(char __user *user_filter)
  272. {
  273. struct sock_fprog fprog;
  274. long ret = -EFAULT;
  275. #ifdef CONFIG_COMPAT
  276. if (is_compat_task()) {
  277. struct compat_sock_fprog fprog32;
  278. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  279. goto out;
  280. fprog.len = fprog32.len;
  281. fprog.filter = compat_ptr(fprog32.filter);
  282. } else /* falls through to the if below. */
  283. #endif
  284. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  285. goto out;
  286. ret = seccomp_attach_filter(&fprog);
  287. out:
  288. return ret;
  289. }
  290. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  291. void get_seccomp_filter(struct task_struct *tsk)
  292. {
  293. struct seccomp_filter *orig = tsk->seccomp.filter;
  294. if (!orig)
  295. return;
  296. /* Reference count is bounded by the number of total processes. */
  297. atomic_inc(&orig->usage);
  298. }
  299. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  300. void put_seccomp_filter(struct task_struct *tsk)
  301. {
  302. struct seccomp_filter *orig = tsk->seccomp.filter;
  303. /* Clean up single-reference branches iteratively. */
  304. while (orig && atomic_dec_and_test(&orig->usage)) {
  305. struct seccomp_filter *freeme = orig;
  306. orig = orig->prev;
  307. kfree(freeme);
  308. }
  309. }
  310. #endif /* CONFIG_SECCOMP_FILTER */
  311. /*
  312. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  313. * To be fully secure this must be combined with rlimit
  314. * to limit the stack allocations too.
  315. */
  316. static int mode1_syscalls[] = {
  317. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  318. 0, /* null terminated */
  319. };
  320. #ifdef CONFIG_COMPAT
  321. static int mode1_syscalls_32[] = {
  322. __NR_seccomp_read_32, __NR_seccomp_write_32, __NR_seccomp_exit_32, __NR_seccomp_sigreturn_32,
  323. 0, /* null terminated */
  324. };
  325. #endif
  326. int __secure_computing(int this_syscall)
  327. {
  328. int mode = current->seccomp.mode;
  329. int exit_sig = 0;
  330. int *syscall;
  331. u32 ret = SECCOMP_RET_KILL;
  332. int data;
  333. switch (mode) {
  334. case SECCOMP_MODE_STRICT:
  335. syscall = mode1_syscalls;
  336. #ifdef CONFIG_COMPAT
  337. if (is_compat_task())
  338. syscall = mode1_syscalls_32;
  339. #endif
  340. do {
  341. if (*syscall == this_syscall)
  342. return 0;
  343. } while (*++syscall);
  344. exit_sig = SIGKILL;
  345. break;
  346. #ifdef CONFIG_SECCOMP_FILTER
  347. case SECCOMP_MODE_FILTER:
  348. ret = seccomp_run_filters(this_syscall);
  349. data = ret & SECCOMP_RET_DATA;
  350. switch (ret & SECCOMP_RET_ACTION) {
  351. case SECCOMP_RET_ERRNO:
  352. /* Set the low-order 16-bits as a errno. */
  353. syscall_set_return_value(current, task_pt_regs(current),
  354. -data, 0);
  355. goto skip;
  356. case SECCOMP_RET_ALLOW:
  357. return 0;
  358. case SECCOMP_RET_KILL:
  359. default:
  360. break;
  361. }
  362. exit_sig = SIGSYS;
  363. break;
  364. #endif
  365. default:
  366. BUG();
  367. }
  368. #ifdef SECCOMP_DEBUG
  369. dump_stack();
  370. #endif
  371. audit_seccomp(this_syscall, exit_sig, ret);
  372. do_exit(exit_sig);
  373. skip:
  374. audit_seccomp(this_syscall, exit_sig, ret);
  375. return -1;
  376. }
  377. long prctl_get_seccomp(void)
  378. {
  379. return current->seccomp.mode;
  380. }
  381. /**
  382. * prctl_set_seccomp: configures current->seccomp.mode
  383. * @seccomp_mode: requested mode to use
  384. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  385. *
  386. * This function may be called repeatedly with a @seccomp_mode of
  387. * SECCOMP_MODE_FILTER to install additional filters. Every filter
  388. * successfully installed will be evaluated (in reverse order) for each system
  389. * call the task makes.
  390. *
  391. * Once current->seccomp.mode is non-zero, it may not be changed.
  392. *
  393. * Returns 0 on success or -EINVAL on failure.
  394. */
  395. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  396. {
  397. long ret = -EINVAL;
  398. if (current->seccomp.mode &&
  399. current->seccomp.mode != seccomp_mode)
  400. goto out;
  401. switch (seccomp_mode) {
  402. case SECCOMP_MODE_STRICT:
  403. ret = 0;
  404. #ifdef TIF_NOTSC
  405. disable_TSC();
  406. #endif
  407. break;
  408. #ifdef CONFIG_SECCOMP_FILTER
  409. case SECCOMP_MODE_FILTER:
  410. ret = seccomp_attach_user_filter(filter);
  411. if (ret)
  412. goto out;
  413. break;
  414. #endif
  415. default:
  416. goto out;
  417. }
  418. current->seccomp.mode = seccomp_mode;
  419. set_thread_flag(TIF_SECCOMP);
  420. out:
  421. return ret;
  422. }