seccomp.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  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. memset(info, 0, sizeof(*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. long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
  865. void __user *data)
  866. {
  867. struct seccomp_filter *filter;
  868. struct sock_fprog_kern *fprog;
  869. long ret;
  870. unsigned long count = 0;
  871. if (!capable(CAP_SYS_ADMIN) ||
  872. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  873. return -EACCES;
  874. }
  875. spin_lock_irq(&task->sighand->siglock);
  876. if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
  877. ret = -EINVAL;
  878. goto out;
  879. }
  880. filter = task->seccomp.filter;
  881. while (filter) {
  882. filter = filter->prev;
  883. count++;
  884. }
  885. if (filter_off >= count) {
  886. ret = -ENOENT;
  887. goto out;
  888. }
  889. count -= filter_off;
  890. filter = task->seccomp.filter;
  891. while (filter && count > 1) {
  892. filter = filter->prev;
  893. count--;
  894. }
  895. if (WARN_ON(count != 1 || !filter)) {
  896. /* The filter tree shouldn't shrink while we're using it. */
  897. ret = -ENOENT;
  898. goto out;
  899. }
  900. fprog = filter->prog->orig_prog;
  901. if (!fprog) {
  902. /* This must be a new non-cBPF filter, since we save
  903. * every cBPF filter's orig_prog above when
  904. * CONFIG_CHECKPOINT_RESTORE is enabled.
  905. */
  906. ret = -EMEDIUMTYPE;
  907. goto out;
  908. }
  909. ret = fprog->len;
  910. if (!data)
  911. goto out;
  912. __get_seccomp_filter(filter);
  913. spin_unlock_irq(&task->sighand->siglock);
  914. if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
  915. ret = -EFAULT;
  916. __put_seccomp_filter(filter);
  917. return ret;
  918. out:
  919. spin_unlock_irq(&task->sighand->siglock);
  920. return ret;
  921. }
  922. #endif
  923. #ifdef CONFIG_SYSCTL
  924. /* Human readable action names for friendly sysctl interaction */
  925. #define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
  926. #define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
  927. #define SECCOMP_RET_TRAP_NAME "trap"
  928. #define SECCOMP_RET_ERRNO_NAME "errno"
  929. #define SECCOMP_RET_TRACE_NAME "trace"
  930. #define SECCOMP_RET_LOG_NAME "log"
  931. #define SECCOMP_RET_ALLOW_NAME "allow"
  932. static const char seccomp_actions_avail[] =
  933. SECCOMP_RET_KILL_PROCESS_NAME " "
  934. SECCOMP_RET_KILL_THREAD_NAME " "
  935. SECCOMP_RET_TRAP_NAME " "
  936. SECCOMP_RET_ERRNO_NAME " "
  937. SECCOMP_RET_TRACE_NAME " "
  938. SECCOMP_RET_LOG_NAME " "
  939. SECCOMP_RET_ALLOW_NAME;
  940. struct seccomp_log_name {
  941. u32 log;
  942. const char *name;
  943. };
  944. static const struct seccomp_log_name seccomp_log_names[] = {
  945. { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
  946. { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
  947. { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
  948. { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
  949. { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
  950. { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
  951. { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
  952. { }
  953. };
  954. static bool seccomp_names_from_actions_logged(char *names, size_t size,
  955. u32 actions_logged)
  956. {
  957. const struct seccomp_log_name *cur;
  958. bool append_space = false;
  959. for (cur = seccomp_log_names; cur->name && size; cur++) {
  960. ssize_t ret;
  961. if (!(actions_logged & cur->log))
  962. continue;
  963. if (append_space) {
  964. ret = strscpy(names, " ", size);
  965. if (ret < 0)
  966. return false;
  967. names += ret;
  968. size -= ret;
  969. } else
  970. append_space = true;
  971. ret = strscpy(names, cur->name, size);
  972. if (ret < 0)
  973. return false;
  974. names += ret;
  975. size -= ret;
  976. }
  977. return true;
  978. }
  979. static bool seccomp_action_logged_from_name(u32 *action_logged,
  980. const char *name)
  981. {
  982. const struct seccomp_log_name *cur;
  983. for (cur = seccomp_log_names; cur->name; cur++) {
  984. if (!strcmp(cur->name, name)) {
  985. *action_logged = cur->log;
  986. return true;
  987. }
  988. }
  989. return false;
  990. }
  991. static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
  992. {
  993. char *name;
  994. *actions_logged = 0;
  995. while ((name = strsep(&names, " ")) && *name) {
  996. u32 action_logged = 0;
  997. if (!seccomp_action_logged_from_name(&action_logged, name))
  998. return false;
  999. *actions_logged |= action_logged;
  1000. }
  1001. return true;
  1002. }
  1003. static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
  1004. void __user *buffer, size_t *lenp,
  1005. loff_t *ppos)
  1006. {
  1007. char names[sizeof(seccomp_actions_avail)];
  1008. struct ctl_table table;
  1009. int ret;
  1010. if (write && !capable(CAP_SYS_ADMIN))
  1011. return -EPERM;
  1012. memset(names, 0, sizeof(names));
  1013. if (!write) {
  1014. if (!seccomp_names_from_actions_logged(names, sizeof(names),
  1015. seccomp_actions_logged))
  1016. return -EINVAL;
  1017. }
  1018. table = *ro_table;
  1019. table.data = names;
  1020. table.maxlen = sizeof(names);
  1021. ret = proc_dostring(&table, write, buffer, lenp, ppos);
  1022. if (ret)
  1023. return ret;
  1024. if (write) {
  1025. u32 actions_logged;
  1026. if (!seccomp_actions_logged_from_names(&actions_logged,
  1027. table.data))
  1028. return -EINVAL;
  1029. if (actions_logged & SECCOMP_LOG_ALLOW)
  1030. return -EINVAL;
  1031. seccomp_actions_logged = actions_logged;
  1032. }
  1033. return 0;
  1034. }
  1035. static struct ctl_path seccomp_sysctl_path[] = {
  1036. { .procname = "kernel", },
  1037. { .procname = "seccomp", },
  1038. { }
  1039. };
  1040. static struct ctl_table seccomp_sysctl_table[] = {
  1041. {
  1042. .procname = "actions_avail",
  1043. .data = (void *) &seccomp_actions_avail,
  1044. .maxlen = sizeof(seccomp_actions_avail),
  1045. .mode = 0444,
  1046. .proc_handler = proc_dostring,
  1047. },
  1048. {
  1049. .procname = "actions_logged",
  1050. .mode = 0644,
  1051. .proc_handler = seccomp_actions_logged_handler,
  1052. },
  1053. { }
  1054. };
  1055. static int __init seccomp_sysctl_init(void)
  1056. {
  1057. struct ctl_table_header *hdr;
  1058. hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
  1059. if (!hdr)
  1060. pr_warn("seccomp: sysctl registration failed\n");
  1061. else
  1062. kmemleak_not_leak(hdr);
  1063. return 0;
  1064. }
  1065. device_initcall(seccomp_sysctl_init)
  1066. #endif /* CONFIG_SYSCTL */