seccomp.c 32 KB

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