seccomp.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/kernel/seccomp.c
  4. *
  5. * Copyright 2004-2005 Andrea Arcangeli <andrea@cpushare.com>
  6. *
  7. * Copyright (C) 2012 Google, Inc.
  8. * Will Drewry <wad@chromium.org>
  9. *
  10. * This defines a simple but solid secure-computing facility.
  11. *
  12. * Mode 1 uses a fixed list of allowed system calls.
  13. * Mode 2 allows user-defined system call filters in the form
  14. * of Berkeley Packet Filters/Linux Socket Filters.
  15. */
  16. #include <linux/refcount.h>
  17. #include <linux/audit.h>
  18. #include <linux/compat.h>
  19. #include <linux/coredump.h>
  20. #include <linux/kmemleak.h>
  21. #include <linux/sched.h>
  22. #include <linux/sched/task_stack.h>
  23. #include <linux/seccomp.h>
  24. #include <linux/slab.h>
  25. #include <linux/syscalls.h>
  26. #include <linux/sysctl.h>
  27. #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  28. #include <asm/syscall.h>
  29. #endif
  30. #ifdef CONFIG_SECCOMP_FILTER
  31. #include <linux/filter.h>
  32. #include <linux/pid.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/security.h>
  35. #include <linux/tracehook.h>
  36. #include <linux/uaccess.h>
  37. /**
  38. * struct seccomp_filter - container for seccomp BPF programs
  39. *
  40. * @usage: reference count to manage the object lifetime.
  41. * get/put helpers should be used when accessing an instance
  42. * outside of a lifetime-guarded section. In general, this
  43. * is only needed for handling filters shared across tasks.
  44. * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
  45. * @prev: points to a previously installed, or inherited, filter
  46. * @prog: the BPF program to evaluate
  47. *
  48. * seccomp_filter objects are organized in a tree linked via the @prev
  49. * pointer. For any task, it appears to be a singly-linked list starting
  50. * with current->seccomp.filter, the most recently attached or inherited filter.
  51. * However, multiple filters may share a @prev node, by way of fork(), which
  52. * results in a unidirectional tree existing in memory. This is similar to
  53. * how namespaces work.
  54. *
  55. * seccomp_filter objects should never be modified after being attached
  56. * to a task_struct (other than @usage).
  57. */
  58. struct seccomp_filter {
  59. refcount_t usage;
  60. bool log;
  61. struct seccomp_filter *prev;
  62. struct bpf_prog *prog;
  63. };
  64. /* Limit any path through the tree to 256KB worth of instructions. */
  65. #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
  66. /*
  67. * Endianness is explicitly ignored and left for BPF program authors to manage
  68. * as per the specific architecture.
  69. */
  70. static void populate_seccomp_data(struct seccomp_data *sd)
  71. {
  72. struct task_struct *task = current;
  73. struct pt_regs *regs = task_pt_regs(task);
  74. unsigned long args[6];
  75. sd->nr = syscall_get_nr(task, regs);
  76. sd->arch = syscall_get_arch();
  77. syscall_get_arguments(task, regs, 0, 6, args);
  78. sd->args[0] = args[0];
  79. sd->args[1] = args[1];
  80. sd->args[2] = args[2];
  81. sd->args[3] = args[3];
  82. sd->args[4] = args[4];
  83. sd->args[5] = args[5];
  84. sd->instruction_pointer = KSTK_EIP(task);
  85. }
  86. /**
  87. * seccomp_check_filter - verify seccomp filter code
  88. * @filter: filter to verify
  89. * @flen: length of filter
  90. *
  91. * Takes a previously checked filter (by bpf_check_classic) and
  92. * redirects all filter code that loads struct sk_buff data
  93. * and related data through seccomp_bpf_load. It also
  94. * enforces length and alignment checking of those loads.
  95. *
  96. * Returns 0 if the rule set is legal or -EINVAL if not.
  97. */
  98. static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
  99. {
  100. int pc;
  101. for (pc = 0; pc < flen; pc++) {
  102. struct sock_filter *ftest = &filter[pc];
  103. u16 code = ftest->code;
  104. u32 k = ftest->k;
  105. switch (code) {
  106. case BPF_LD | BPF_W | BPF_ABS:
  107. ftest->code = BPF_LDX | BPF_W | BPF_ABS;
  108. /* 32-bit aligned and not out of bounds. */
  109. if (k >= sizeof(struct seccomp_data) || k & 3)
  110. return -EINVAL;
  111. continue;
  112. case BPF_LD | BPF_W | BPF_LEN:
  113. ftest->code = BPF_LD | BPF_IMM;
  114. ftest->k = sizeof(struct seccomp_data);
  115. continue;
  116. case BPF_LDX | BPF_W | BPF_LEN:
  117. ftest->code = BPF_LDX | BPF_IMM;
  118. ftest->k = sizeof(struct seccomp_data);
  119. continue;
  120. /* Explicitly include allowed calls. */
  121. case BPF_RET | BPF_K:
  122. case BPF_RET | BPF_A:
  123. case BPF_ALU | BPF_ADD | BPF_K:
  124. case BPF_ALU | BPF_ADD | BPF_X:
  125. case BPF_ALU | BPF_SUB | BPF_K:
  126. case BPF_ALU | BPF_SUB | BPF_X:
  127. case BPF_ALU | BPF_MUL | BPF_K:
  128. case BPF_ALU | BPF_MUL | BPF_X:
  129. case BPF_ALU | BPF_DIV | BPF_K:
  130. case BPF_ALU | BPF_DIV | BPF_X:
  131. case BPF_ALU | BPF_AND | BPF_K:
  132. case BPF_ALU | BPF_AND | BPF_X:
  133. case BPF_ALU | BPF_OR | BPF_K:
  134. case BPF_ALU | BPF_OR | BPF_X:
  135. case BPF_ALU | BPF_XOR | BPF_K:
  136. case BPF_ALU | BPF_XOR | BPF_X:
  137. case BPF_ALU | BPF_LSH | BPF_K:
  138. case BPF_ALU | BPF_LSH | BPF_X:
  139. case BPF_ALU | BPF_RSH | BPF_K:
  140. case BPF_ALU | BPF_RSH | BPF_X:
  141. case BPF_ALU | BPF_NEG:
  142. case BPF_LD | BPF_IMM:
  143. case BPF_LDX | BPF_IMM:
  144. case BPF_MISC | BPF_TAX:
  145. case BPF_MISC | BPF_TXA:
  146. case BPF_LD | BPF_MEM:
  147. case BPF_LDX | BPF_MEM:
  148. case BPF_ST:
  149. case BPF_STX:
  150. case BPF_JMP | BPF_JA:
  151. case BPF_JMP | BPF_JEQ | BPF_K:
  152. case BPF_JMP | BPF_JEQ | BPF_X:
  153. case BPF_JMP | BPF_JGE | BPF_K:
  154. case BPF_JMP | BPF_JGE | BPF_X:
  155. case BPF_JMP | BPF_JGT | BPF_K:
  156. case BPF_JMP | BPF_JGT | BPF_X:
  157. case BPF_JMP | BPF_JSET | BPF_K:
  158. case BPF_JMP | BPF_JSET | BPF_X:
  159. continue;
  160. default:
  161. return -EINVAL;
  162. }
  163. }
  164. return 0;
  165. }
  166. /**
  167. * seccomp_run_filters - evaluates all seccomp filters against @sd
  168. * @sd: optional seccomp data to be passed to filters
  169. * @match: stores struct seccomp_filter that resulted in the return value,
  170. * unless filter returned SECCOMP_RET_ALLOW, in which case it will
  171. * be unchanged.
  172. *
  173. * Returns valid seccomp BPF response codes.
  174. */
  175. #define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
  176. static u32 seccomp_run_filters(const struct seccomp_data *sd,
  177. struct seccomp_filter **match)
  178. {
  179. struct seccomp_data sd_local;
  180. u32 ret = SECCOMP_RET_ALLOW;
  181. /* Make sure cross-thread synced filter points somewhere sane. */
  182. struct seccomp_filter *f =
  183. READ_ONCE(current->seccomp.filter);
  184. /* Ensure unexpected behavior doesn't result in failing open. */
  185. if (unlikely(WARN_ON(f == NULL)))
  186. return SECCOMP_RET_KILL_PROCESS;
  187. if (!sd) {
  188. populate_seccomp_data(&sd_local);
  189. sd = &sd_local;
  190. }
  191. /*
  192. * All filters in the list are evaluated and the lowest BPF return
  193. * value always takes priority (ignoring the DATA).
  194. */
  195. for (; f; f = f->prev) {
  196. u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
  197. if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
  198. ret = cur_ret;
  199. *match = f;
  200. }
  201. }
  202. return ret;
  203. }
  204. #endif /* CONFIG_SECCOMP_FILTER */
  205. static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
  206. {
  207. assert_spin_locked(&current->sighand->siglock);
  208. if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
  209. return false;
  210. return true;
  211. }
  212. static inline void seccomp_assign_mode(struct task_struct *task,
  213. unsigned long seccomp_mode)
  214. {
  215. assert_spin_locked(&task->sighand->siglock);
  216. task->seccomp.mode = seccomp_mode;
  217. /*
  218. * Make sure TIF_SECCOMP cannot be set before the mode (and
  219. * filter) is set.
  220. */
  221. smp_mb__before_atomic();
  222. set_tsk_thread_flag(task, TIF_SECCOMP);
  223. }
  224. #ifdef CONFIG_SECCOMP_FILTER
  225. /* Returns 1 if the parent is an ancestor of the child. */
  226. static int is_ancestor(struct seccomp_filter *parent,
  227. struct seccomp_filter *child)
  228. {
  229. /* NULL is the root ancestor. */
  230. if (parent == NULL)
  231. return 1;
  232. for (; child; child = child->prev)
  233. if (child == parent)
  234. return 1;
  235. return 0;
  236. }
  237. /**
  238. * seccomp_can_sync_threads: checks if all threads can be synchronized
  239. *
  240. * Expects sighand and cred_guard_mutex locks to be held.
  241. *
  242. * Returns 0 on success, -ve on error, or the pid of a thread which was
  243. * either not in the correct seccomp mode or it did not have an ancestral
  244. * seccomp filter.
  245. */
  246. static inline pid_t seccomp_can_sync_threads(void)
  247. {
  248. struct task_struct *thread, *caller;
  249. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  250. assert_spin_locked(&current->sighand->siglock);
  251. /* Validate all threads being eligible for synchronization. */
  252. caller = current;
  253. for_each_thread(caller, thread) {
  254. pid_t failed;
  255. /* Skip current, since it is initiating the sync. */
  256. if (thread == caller)
  257. continue;
  258. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
  259. (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
  260. is_ancestor(thread->seccomp.filter,
  261. caller->seccomp.filter)))
  262. continue;
  263. /* Return the first thread that cannot be synchronized. */
  264. failed = task_pid_vnr(thread);
  265. /* If the pid cannot be resolved, then return -ESRCH */
  266. if (unlikely(WARN_ON(failed == 0)))
  267. failed = -ESRCH;
  268. return failed;
  269. }
  270. return 0;
  271. }
  272. /**
  273. * seccomp_sync_threads: sets all threads to use current's filter
  274. *
  275. * Expects sighand and cred_guard_mutex locks to be held, and for
  276. * seccomp_can_sync_threads() to have returned success already
  277. * without dropping the locks.
  278. *
  279. */
  280. static inline void seccomp_sync_threads(void)
  281. {
  282. struct task_struct *thread, *caller;
  283. BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
  284. assert_spin_locked(&current->sighand->siglock);
  285. /* Synchronize all threads. */
  286. caller = current;
  287. for_each_thread(caller, thread) {
  288. /* Skip current, since it needs no changes. */
  289. if (thread == caller)
  290. continue;
  291. /* Get a task reference for the new leaf node. */
  292. get_seccomp_filter(caller);
  293. /*
  294. * Drop the task reference to the shared ancestor since
  295. * current's path will hold a reference. (This also
  296. * allows a put before the assignment.)
  297. */
  298. put_seccomp_filter(thread);
  299. smp_store_release(&thread->seccomp.filter,
  300. caller->seccomp.filter);
  301. /*
  302. * Don't let an unprivileged task work around
  303. * the no_new_privs restriction by creating
  304. * a thread that sets it up, enters seccomp,
  305. * then dies.
  306. */
  307. if (task_no_new_privs(caller))
  308. task_set_no_new_privs(thread);
  309. /*
  310. * Opt the other thread into seccomp if needed.
  311. * As threads are considered to be trust-realm
  312. * equivalent (see ptrace_may_access), it is safe to
  313. * allow one thread to transition the other.
  314. */
  315. if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
  316. seccomp_assign_mode(thread, SECCOMP_MODE_FILTER);
  317. }
  318. }
  319. /**
  320. * seccomp_prepare_filter: Prepares a seccomp filter for use.
  321. * @fprog: BPF program to install
  322. *
  323. * Returns filter on success or an ERR_PTR on failure.
  324. */
  325. static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
  326. {
  327. struct seccomp_filter *sfilter;
  328. int ret;
  329. const bool save_orig = IS_ENABLED(CONFIG_CHECKPOINT_RESTORE);
  330. if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
  331. return ERR_PTR(-EINVAL);
  332. BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
  333. /*
  334. * Installing a seccomp filter requires that the task has
  335. * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
  336. * This avoids scenarios where unprivileged tasks can affect the
  337. * behavior of privileged children.
  338. */
  339. if (!task_no_new_privs(current) &&
  340. security_capable_noaudit(current_cred(), current_user_ns(),
  341. CAP_SYS_ADMIN) != 0)
  342. return ERR_PTR(-EACCES);
  343. /* Allocate a new seccomp_filter */
  344. sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN);
  345. if (!sfilter)
  346. return ERR_PTR(-ENOMEM);
  347. ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
  348. seccomp_check_filter, save_orig);
  349. if (ret < 0) {
  350. kfree(sfilter);
  351. return ERR_PTR(ret);
  352. }
  353. refcount_set(&sfilter->usage, 1);
  354. return sfilter;
  355. }
  356. /**
  357. * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
  358. * @user_filter: pointer to the user data containing a sock_fprog.
  359. *
  360. * Returns 0 on success and non-zero otherwise.
  361. */
  362. static struct seccomp_filter *
  363. seccomp_prepare_user_filter(const char __user *user_filter)
  364. {
  365. struct sock_fprog fprog;
  366. struct seccomp_filter *filter = ERR_PTR(-EFAULT);
  367. #ifdef CONFIG_COMPAT
  368. if (in_compat_syscall()) {
  369. struct compat_sock_fprog fprog32;
  370. if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
  371. goto out;
  372. fprog.len = fprog32.len;
  373. fprog.filter = compat_ptr(fprog32.filter);
  374. } else /* falls through to the if below. */
  375. #endif
  376. if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
  377. goto out;
  378. filter = seccomp_prepare_filter(&fprog);
  379. out:
  380. return filter;
  381. }
  382. /**
  383. * seccomp_attach_filter: validate and attach filter
  384. * @flags: flags to change filter behavior
  385. * @filter: seccomp filter to add to the current process
  386. *
  387. * Caller must be holding current->sighand->siglock lock.
  388. *
  389. * Returns 0 on success, -ve on error.
  390. */
  391. static long seccomp_attach_filter(unsigned int flags,
  392. struct seccomp_filter *filter)
  393. {
  394. unsigned long total_insns;
  395. struct seccomp_filter *walker;
  396. assert_spin_locked(&current->sighand->siglock);
  397. /* Validate resulting filter length. */
  398. total_insns = filter->prog->len;
  399. for (walker = current->seccomp.filter; walker; walker = walker->prev)
  400. total_insns += walker->prog->len + 4; /* 4 instr penalty */
  401. if (total_insns > MAX_INSNS_PER_PATH)
  402. return -ENOMEM;
  403. /* If thread sync has been requested, check that it is possible. */
  404. if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
  405. int ret;
  406. ret = seccomp_can_sync_threads();
  407. if (ret)
  408. return ret;
  409. }
  410. /* Set log flag, if present. */
  411. if (flags & SECCOMP_FILTER_FLAG_LOG)
  412. filter->log = true;
  413. /*
  414. * If there is an existing filter, make it the prev and don't drop its
  415. * task reference.
  416. */
  417. filter->prev = current->seccomp.filter;
  418. current->seccomp.filter = filter;
  419. /* Now that the new filter is in place, synchronize to all threads. */
  420. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  421. seccomp_sync_threads();
  422. return 0;
  423. }
  424. static void __get_seccomp_filter(struct seccomp_filter *filter)
  425. {
  426. /* Reference count is bounded by the number of total processes. */
  427. refcount_inc(&filter->usage);
  428. }
  429. /* get_seccomp_filter - increments the reference count of the filter on @tsk */
  430. void get_seccomp_filter(struct task_struct *tsk)
  431. {
  432. struct seccomp_filter *orig = tsk->seccomp.filter;
  433. if (!orig)
  434. return;
  435. __get_seccomp_filter(orig);
  436. }
  437. static inline void seccomp_filter_free(struct seccomp_filter *filter)
  438. {
  439. if (filter) {
  440. bpf_prog_destroy(filter->prog);
  441. kfree(filter);
  442. }
  443. }
  444. static void __put_seccomp_filter(struct seccomp_filter *orig)
  445. {
  446. /* Clean up single-reference branches iteratively. */
  447. while (orig && refcount_dec_and_test(&orig->usage)) {
  448. struct seccomp_filter *freeme = orig;
  449. orig = orig->prev;
  450. seccomp_filter_free(freeme);
  451. }
  452. }
  453. /* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
  454. void put_seccomp_filter(struct task_struct *tsk)
  455. {
  456. __put_seccomp_filter(tsk->seccomp.filter);
  457. }
  458. static void seccomp_init_siginfo(siginfo_t *info, int syscall, int reason)
  459. {
  460. clear_siginfo(info);
  461. info->si_signo = SIGSYS;
  462. info->si_code = SYS_SECCOMP;
  463. info->si_call_addr = (void __user *)KSTK_EIP(current);
  464. info->si_errno = reason;
  465. info->si_arch = syscall_get_arch();
  466. info->si_syscall = syscall;
  467. }
  468. /**
  469. * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
  470. * @syscall: syscall number to send to userland
  471. * @reason: filter-supplied reason code to send to userland (via si_errno)
  472. *
  473. * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
  474. */
  475. static void seccomp_send_sigsys(int syscall, int reason)
  476. {
  477. struct siginfo info;
  478. seccomp_init_siginfo(&info, syscall, reason);
  479. force_sig_info(SIGSYS, &info, current);
  480. }
  481. #endif /* CONFIG_SECCOMP_FILTER */
  482. /* For use with seccomp_actions_logged */
  483. #define SECCOMP_LOG_KILL_PROCESS (1 << 0)
  484. #define SECCOMP_LOG_KILL_THREAD (1 << 1)
  485. #define SECCOMP_LOG_TRAP (1 << 2)
  486. #define SECCOMP_LOG_ERRNO (1 << 3)
  487. #define SECCOMP_LOG_TRACE (1 << 4)
  488. #define SECCOMP_LOG_LOG (1 << 5)
  489. #define SECCOMP_LOG_ALLOW (1 << 6)
  490. static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
  491. SECCOMP_LOG_KILL_THREAD |
  492. SECCOMP_LOG_TRAP |
  493. SECCOMP_LOG_ERRNO |
  494. SECCOMP_LOG_TRACE |
  495. SECCOMP_LOG_LOG;
  496. static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
  497. bool requested)
  498. {
  499. bool log = false;
  500. switch (action) {
  501. case SECCOMP_RET_ALLOW:
  502. break;
  503. case SECCOMP_RET_TRAP:
  504. log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
  505. break;
  506. case SECCOMP_RET_ERRNO:
  507. log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
  508. break;
  509. case SECCOMP_RET_TRACE:
  510. log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
  511. break;
  512. case SECCOMP_RET_LOG:
  513. log = seccomp_actions_logged & SECCOMP_LOG_LOG;
  514. break;
  515. case SECCOMP_RET_KILL_THREAD:
  516. log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
  517. break;
  518. case SECCOMP_RET_KILL_PROCESS:
  519. default:
  520. log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
  521. }
  522. /*
  523. * Force an audit message to be emitted when the action is RET_KILL_*,
  524. * RET_LOG, or the FILTER_FLAG_LOG bit was set and the action is
  525. * allowed to be logged by the admin.
  526. */
  527. if (log)
  528. return __audit_seccomp(syscall, signr, action);
  529. /*
  530. * Let the audit subsystem decide if the action should be audited based
  531. * on whether the current task itself is being audited.
  532. */
  533. return audit_seccomp(syscall, signr, action);
  534. }
  535. /*
  536. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  537. * To be fully secure this must be combined with rlimit
  538. * to limit the stack allocations too.
  539. */
  540. static const int mode1_syscalls[] = {
  541. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  542. 0, /* null terminated */
  543. };
  544. static void __secure_computing_strict(int this_syscall)
  545. {
  546. const int *syscall_whitelist = mode1_syscalls;
  547. #ifdef CONFIG_COMPAT
  548. if (in_compat_syscall())
  549. syscall_whitelist = get_compat_mode1_syscalls();
  550. #endif
  551. do {
  552. if (*syscall_whitelist == this_syscall)
  553. return;
  554. } while (*++syscall_whitelist);
  555. #ifdef SECCOMP_DEBUG
  556. dump_stack();
  557. #endif
  558. seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
  559. do_exit(SIGKILL);
  560. }
  561. #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  562. void secure_computing_strict(int this_syscall)
  563. {
  564. int mode = current->seccomp.mode;
  565. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  566. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  567. return;
  568. if (mode == SECCOMP_MODE_DISABLED)
  569. return;
  570. else if (mode == SECCOMP_MODE_STRICT)
  571. __secure_computing_strict(this_syscall);
  572. else
  573. BUG();
  574. }
  575. #else
  576. #ifdef CONFIG_SECCOMP_FILTER
  577. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  578. const bool recheck_after_trace)
  579. {
  580. u32 filter_ret, action;
  581. struct seccomp_filter *match = NULL;
  582. int data;
  583. /*
  584. * Make sure that any changes to mode from another thread have
  585. * been seen after TIF_SECCOMP was seen.
  586. */
  587. rmb();
  588. filter_ret = seccomp_run_filters(sd, &match);
  589. data = filter_ret & SECCOMP_RET_DATA;
  590. action = filter_ret & SECCOMP_RET_ACTION_FULL;
  591. switch (action) {
  592. case SECCOMP_RET_ERRNO:
  593. /* Set low-order bits as an errno, capped at MAX_ERRNO. */
  594. if (data > MAX_ERRNO)
  595. data = MAX_ERRNO;
  596. syscall_set_return_value(current, task_pt_regs(current),
  597. -data, 0);
  598. goto skip;
  599. case SECCOMP_RET_TRAP:
  600. /* Show the handler the original registers. */
  601. syscall_rollback(current, task_pt_regs(current));
  602. /* Let the filter pass back 16 bits of data. */
  603. seccomp_send_sigsys(this_syscall, data);
  604. goto skip;
  605. case SECCOMP_RET_TRACE:
  606. /* We've been put in this state by the ptracer already. */
  607. if (recheck_after_trace)
  608. return 0;
  609. /* ENOSYS these calls if there is no tracer attached. */
  610. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  611. syscall_set_return_value(current,
  612. task_pt_regs(current),
  613. -ENOSYS, 0);
  614. goto skip;
  615. }
  616. /* Allow the BPF to provide the event message */
  617. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  618. /*
  619. * The delivery of a fatal signal during event
  620. * notification may silently skip tracer notification,
  621. * which could leave us with a potentially unmodified
  622. * syscall that the tracer would have liked to have
  623. * changed. Since the process is about to die, we just
  624. * force the syscall to be skipped and let the signal
  625. * kill the process and correctly handle any tracer exit
  626. * notifications.
  627. */
  628. if (fatal_signal_pending(current))
  629. goto skip;
  630. /* Check if the tracer forced the syscall to be skipped. */
  631. this_syscall = syscall_get_nr(current, task_pt_regs(current));
  632. if (this_syscall < 0)
  633. goto skip;
  634. /*
  635. * Recheck the syscall, since it may have changed. This
  636. * intentionally uses a NULL struct seccomp_data to force
  637. * a reload of all registers. This does not goto skip since
  638. * a skip would have already been reported.
  639. */
  640. if (__seccomp_filter(this_syscall, NULL, true))
  641. return -1;
  642. return 0;
  643. case SECCOMP_RET_LOG:
  644. seccomp_log(this_syscall, 0, action, true);
  645. return 0;
  646. case SECCOMP_RET_ALLOW:
  647. /*
  648. * Note that the "match" filter will always be NULL for
  649. * this action since SECCOMP_RET_ALLOW is the starting
  650. * state in seccomp_run_filters().
  651. */
  652. return 0;
  653. case SECCOMP_RET_KILL_THREAD:
  654. case SECCOMP_RET_KILL_PROCESS:
  655. default:
  656. seccomp_log(this_syscall, SIGSYS, action, true);
  657. /* Dump core only if this is the last remaining thread. */
  658. if (action == SECCOMP_RET_KILL_PROCESS ||
  659. get_nr_threads(current) == 1) {
  660. siginfo_t info;
  661. /* Show the original registers in the dump. */
  662. syscall_rollback(current, task_pt_regs(current));
  663. /* Trigger a manual coredump since do_exit skips it. */
  664. seccomp_init_siginfo(&info, this_syscall, data);
  665. do_coredump(&info);
  666. }
  667. if (action == SECCOMP_RET_KILL_PROCESS)
  668. do_group_exit(SIGSYS);
  669. else
  670. do_exit(SIGSYS);
  671. }
  672. unreachable();
  673. skip:
  674. seccomp_log(this_syscall, 0, action, match ? match->log : false);
  675. return -1;
  676. }
  677. #else
  678. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  679. const bool recheck_after_trace)
  680. {
  681. BUG();
  682. }
  683. #endif
  684. int __secure_computing(const struct seccomp_data *sd)
  685. {
  686. int mode = current->seccomp.mode;
  687. int this_syscall;
  688. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  689. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  690. return 0;
  691. this_syscall = sd ? sd->nr :
  692. syscall_get_nr(current, task_pt_regs(current));
  693. switch (mode) {
  694. case SECCOMP_MODE_STRICT:
  695. __secure_computing_strict(this_syscall); /* may call do_exit */
  696. return 0;
  697. case SECCOMP_MODE_FILTER:
  698. return __seccomp_filter(this_syscall, sd, false);
  699. default:
  700. BUG();
  701. }
  702. }
  703. #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
  704. long prctl_get_seccomp(void)
  705. {
  706. return current->seccomp.mode;
  707. }
  708. /**
  709. * seccomp_set_mode_strict: internal function for setting strict seccomp
  710. *
  711. * Once current->seccomp.mode is non-zero, it may not be changed.
  712. *
  713. * Returns 0 on success or -EINVAL on failure.
  714. */
  715. static long seccomp_set_mode_strict(void)
  716. {
  717. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  718. long ret = -EINVAL;
  719. spin_lock_irq(&current->sighand->siglock);
  720. if (!seccomp_may_assign_mode(seccomp_mode))
  721. goto out;
  722. #ifdef TIF_NOTSC
  723. disable_TSC();
  724. #endif
  725. seccomp_assign_mode(current, seccomp_mode);
  726. ret = 0;
  727. out:
  728. spin_unlock_irq(&current->sighand->siglock);
  729. return ret;
  730. }
  731. #ifdef CONFIG_SECCOMP_FILTER
  732. /**
  733. * seccomp_set_mode_filter: internal function for setting seccomp filter
  734. * @flags: flags to change filter behavior
  735. * @filter: struct sock_fprog containing filter
  736. *
  737. * This function may be called repeatedly to install additional filters.
  738. * Every filter successfully installed will be evaluated (in reverse order)
  739. * for each system call the task makes.
  740. *
  741. * Once current->seccomp.mode is non-zero, it may not be changed.
  742. *
  743. * Returns 0 on success or -EINVAL on failure.
  744. */
  745. static long seccomp_set_mode_filter(unsigned int flags,
  746. const char __user *filter)
  747. {
  748. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  749. struct seccomp_filter *prepared = NULL;
  750. long ret = -EINVAL;
  751. /* Validate flags. */
  752. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  753. return -EINVAL;
  754. /* Prepare the new filter before holding any locks. */
  755. prepared = seccomp_prepare_user_filter(filter);
  756. if (IS_ERR(prepared))
  757. return PTR_ERR(prepared);
  758. /*
  759. * Make sure we cannot change seccomp or nnp state via TSYNC
  760. * while another thread is in the middle of calling exec.
  761. */
  762. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  763. mutex_lock_killable(&current->signal->cred_guard_mutex))
  764. goto out_free;
  765. spin_lock_irq(&current->sighand->siglock);
  766. if (!seccomp_may_assign_mode(seccomp_mode))
  767. goto out;
  768. ret = seccomp_attach_filter(flags, prepared);
  769. if (ret)
  770. goto out;
  771. /* Do not free the successfully attached filter. */
  772. prepared = NULL;
  773. seccomp_assign_mode(current, seccomp_mode);
  774. out:
  775. spin_unlock_irq(&current->sighand->siglock);
  776. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  777. mutex_unlock(&current->signal->cred_guard_mutex);
  778. out_free:
  779. seccomp_filter_free(prepared);
  780. return ret;
  781. }
  782. #else
  783. static inline long seccomp_set_mode_filter(unsigned int flags,
  784. const char __user *filter)
  785. {
  786. return -EINVAL;
  787. }
  788. #endif
  789. static long seccomp_get_action_avail(const char __user *uaction)
  790. {
  791. u32 action;
  792. if (copy_from_user(&action, uaction, sizeof(action)))
  793. return -EFAULT;
  794. switch (action) {
  795. case SECCOMP_RET_KILL_PROCESS:
  796. case SECCOMP_RET_KILL_THREAD:
  797. case SECCOMP_RET_TRAP:
  798. case SECCOMP_RET_ERRNO:
  799. case SECCOMP_RET_TRACE:
  800. case SECCOMP_RET_LOG:
  801. case SECCOMP_RET_ALLOW:
  802. break;
  803. default:
  804. return -EOPNOTSUPP;
  805. }
  806. return 0;
  807. }
  808. /* Common entry point for both prctl and syscall. */
  809. static long do_seccomp(unsigned int op, unsigned int flags,
  810. const char __user *uargs)
  811. {
  812. switch (op) {
  813. case SECCOMP_SET_MODE_STRICT:
  814. if (flags != 0 || uargs != NULL)
  815. return -EINVAL;
  816. return seccomp_set_mode_strict();
  817. case SECCOMP_SET_MODE_FILTER:
  818. return seccomp_set_mode_filter(flags, uargs);
  819. case SECCOMP_GET_ACTION_AVAIL:
  820. if (flags != 0)
  821. return -EINVAL;
  822. return seccomp_get_action_avail(uargs);
  823. default:
  824. return -EINVAL;
  825. }
  826. }
  827. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  828. const char __user *, uargs)
  829. {
  830. return do_seccomp(op, flags, uargs);
  831. }
  832. /**
  833. * prctl_set_seccomp: configures current->seccomp.mode
  834. * @seccomp_mode: requested mode to use
  835. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  836. *
  837. * Returns 0 on success or -EINVAL on failure.
  838. */
  839. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  840. {
  841. unsigned int op;
  842. char __user *uargs;
  843. switch (seccomp_mode) {
  844. case SECCOMP_MODE_STRICT:
  845. op = SECCOMP_SET_MODE_STRICT;
  846. /*
  847. * Setting strict mode through prctl always ignored filter,
  848. * so make sure it is always NULL here to pass the internal
  849. * check in do_seccomp().
  850. */
  851. uargs = NULL;
  852. break;
  853. case SECCOMP_MODE_FILTER:
  854. op = SECCOMP_SET_MODE_FILTER;
  855. uargs = filter;
  856. break;
  857. default:
  858. return -EINVAL;
  859. }
  860. /* prctl interface doesn't have flags, so they are always zero. */
  861. return do_seccomp(op, 0, uargs);
  862. }
  863. #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
  864. static struct seccomp_filter *get_nth_filter(struct task_struct *task,
  865. unsigned long filter_off)
  866. {
  867. struct seccomp_filter *orig, *filter;
  868. unsigned long count;
  869. /*
  870. * Note: this is only correct because the caller should be the (ptrace)
  871. * tracer of the task, otherwise lock_task_sighand is needed.
  872. */
  873. spin_lock_irq(&task->sighand->siglock);
  874. if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
  875. spin_unlock_irq(&task->sighand->siglock);
  876. return ERR_PTR(-EINVAL);
  877. }
  878. orig = task->seccomp.filter;
  879. __get_seccomp_filter(orig);
  880. spin_unlock_irq(&task->sighand->siglock);
  881. count = 0;
  882. for (filter = orig; filter; filter = filter->prev)
  883. count++;
  884. if (filter_off >= count) {
  885. filter = ERR_PTR(-ENOENT);
  886. goto out;
  887. }
  888. count -= filter_off;
  889. for (filter = orig; filter && count > 1; filter = filter->prev)
  890. count--;
  891. if (WARN_ON(count != 1 || !filter)) {
  892. filter = ERR_PTR(-ENOENT);
  893. goto out;
  894. }
  895. __get_seccomp_filter(filter);
  896. out:
  897. __put_seccomp_filter(orig);
  898. return filter;
  899. }
  900. long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
  901. void __user *data)
  902. {
  903. struct seccomp_filter *filter;
  904. struct sock_fprog_kern *fprog;
  905. long ret;
  906. if (!capable(CAP_SYS_ADMIN) ||
  907. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  908. return -EACCES;
  909. }
  910. filter = get_nth_filter(task, filter_off);
  911. if (IS_ERR(filter))
  912. return PTR_ERR(filter);
  913. fprog = filter->prog->orig_prog;
  914. if (!fprog) {
  915. /* This must be a new non-cBPF filter, since we save
  916. * every cBPF filter's orig_prog above when
  917. * CONFIG_CHECKPOINT_RESTORE is enabled.
  918. */
  919. ret = -EMEDIUMTYPE;
  920. goto out;
  921. }
  922. ret = fprog->len;
  923. if (!data)
  924. goto out;
  925. if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
  926. ret = -EFAULT;
  927. out:
  928. __put_seccomp_filter(filter);
  929. return ret;
  930. }
  931. long seccomp_get_metadata(struct task_struct *task,
  932. unsigned long size, void __user *data)
  933. {
  934. long ret;
  935. struct seccomp_filter *filter;
  936. struct seccomp_metadata kmd = {};
  937. if (!capable(CAP_SYS_ADMIN) ||
  938. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  939. return -EACCES;
  940. }
  941. size = min_t(unsigned long, size, sizeof(kmd));
  942. if (copy_from_user(&kmd, data, size))
  943. return -EFAULT;
  944. filter = get_nth_filter(task, kmd.filter_off);
  945. if (IS_ERR(filter))
  946. return PTR_ERR(filter);
  947. memset(&kmd, 0, sizeof(kmd));
  948. if (filter->log)
  949. kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
  950. ret = size;
  951. if (copy_to_user(data, &kmd, size))
  952. ret = -EFAULT;
  953. __put_seccomp_filter(filter);
  954. return ret;
  955. }
  956. #endif
  957. #ifdef CONFIG_SYSCTL
  958. /* Human readable action names for friendly sysctl interaction */
  959. #define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
  960. #define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
  961. #define SECCOMP_RET_TRAP_NAME "trap"
  962. #define SECCOMP_RET_ERRNO_NAME "errno"
  963. #define SECCOMP_RET_TRACE_NAME "trace"
  964. #define SECCOMP_RET_LOG_NAME "log"
  965. #define SECCOMP_RET_ALLOW_NAME "allow"
  966. static const char seccomp_actions_avail[] =
  967. SECCOMP_RET_KILL_PROCESS_NAME " "
  968. SECCOMP_RET_KILL_THREAD_NAME " "
  969. SECCOMP_RET_TRAP_NAME " "
  970. SECCOMP_RET_ERRNO_NAME " "
  971. SECCOMP_RET_TRACE_NAME " "
  972. SECCOMP_RET_LOG_NAME " "
  973. SECCOMP_RET_ALLOW_NAME;
  974. struct seccomp_log_name {
  975. u32 log;
  976. const char *name;
  977. };
  978. static const struct seccomp_log_name seccomp_log_names[] = {
  979. { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
  980. { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
  981. { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
  982. { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
  983. { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
  984. { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
  985. { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
  986. { }
  987. };
  988. static bool seccomp_names_from_actions_logged(char *names, size_t size,
  989. u32 actions_logged)
  990. {
  991. const struct seccomp_log_name *cur;
  992. bool append_space = false;
  993. for (cur = seccomp_log_names; cur->name && size; cur++) {
  994. ssize_t ret;
  995. if (!(actions_logged & cur->log))
  996. continue;
  997. if (append_space) {
  998. ret = strscpy(names, " ", size);
  999. if (ret < 0)
  1000. return false;
  1001. names += ret;
  1002. size -= ret;
  1003. } else
  1004. append_space = true;
  1005. ret = strscpy(names, cur->name, size);
  1006. if (ret < 0)
  1007. return false;
  1008. names += ret;
  1009. size -= ret;
  1010. }
  1011. return true;
  1012. }
  1013. static bool seccomp_action_logged_from_name(u32 *action_logged,
  1014. const char *name)
  1015. {
  1016. const struct seccomp_log_name *cur;
  1017. for (cur = seccomp_log_names; cur->name; cur++) {
  1018. if (!strcmp(cur->name, name)) {
  1019. *action_logged = cur->log;
  1020. return true;
  1021. }
  1022. }
  1023. return false;
  1024. }
  1025. static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
  1026. {
  1027. char *name;
  1028. *actions_logged = 0;
  1029. while ((name = strsep(&names, " ")) && *name) {
  1030. u32 action_logged = 0;
  1031. if (!seccomp_action_logged_from_name(&action_logged, name))
  1032. return false;
  1033. *actions_logged |= action_logged;
  1034. }
  1035. return true;
  1036. }
  1037. static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
  1038. void __user *buffer, size_t *lenp,
  1039. loff_t *ppos)
  1040. {
  1041. char names[sizeof(seccomp_actions_avail)];
  1042. struct ctl_table table;
  1043. int ret;
  1044. if (write && !capable(CAP_SYS_ADMIN))
  1045. return -EPERM;
  1046. memset(names, 0, sizeof(names));
  1047. if (!write) {
  1048. if (!seccomp_names_from_actions_logged(names, sizeof(names),
  1049. seccomp_actions_logged))
  1050. return -EINVAL;
  1051. }
  1052. table = *ro_table;
  1053. table.data = names;
  1054. table.maxlen = sizeof(names);
  1055. ret = proc_dostring(&table, write, buffer, lenp, ppos);
  1056. if (ret)
  1057. return ret;
  1058. if (write) {
  1059. u32 actions_logged;
  1060. if (!seccomp_actions_logged_from_names(&actions_logged,
  1061. table.data))
  1062. return -EINVAL;
  1063. if (actions_logged & SECCOMP_LOG_ALLOW)
  1064. return -EINVAL;
  1065. seccomp_actions_logged = actions_logged;
  1066. }
  1067. return 0;
  1068. }
  1069. static struct ctl_path seccomp_sysctl_path[] = {
  1070. { .procname = "kernel", },
  1071. { .procname = "seccomp", },
  1072. { }
  1073. };
  1074. static struct ctl_table seccomp_sysctl_table[] = {
  1075. {
  1076. .procname = "actions_avail",
  1077. .data = (void *) &seccomp_actions_avail,
  1078. .maxlen = sizeof(seccomp_actions_avail),
  1079. .mode = 0444,
  1080. .proc_handler = proc_dostring,
  1081. },
  1082. {
  1083. .procname = "actions_logged",
  1084. .mode = 0644,
  1085. .proc_handler = seccomp_actions_logged_handler,
  1086. },
  1087. { }
  1088. };
  1089. static int __init seccomp_sysctl_init(void)
  1090. {
  1091. struct ctl_table_header *hdr;
  1092. hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
  1093. if (!hdr)
  1094. pr_warn("seccomp: sysctl registration failed\n");
  1095. else
  1096. kmemleak_not_leak(hdr);
  1097. return 0;
  1098. }
  1099. device_initcall(seccomp_sysctl_init)
  1100. #endif /* CONFIG_SYSCTL */