seccomp.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  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(current_cred(), current_user_ns(),
  349. CAP_SYS_ADMIN, CAP_OPT_NOAUDIT) != 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. clear_siginfo(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. * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the
  532. * FILTER_FLAG_LOG bit was set. The admin has the ability to silence
  533. * any action from being logged by removing the action name from the
  534. * seccomp_actions_logged sysctl.
  535. */
  536. if (!log)
  537. return;
  538. audit_seccomp(syscall, signr, action);
  539. }
  540. /*
  541. * Secure computing mode 1 allows only read/write/exit/sigreturn.
  542. * To be fully secure this must be combined with rlimit
  543. * to limit the stack allocations too.
  544. */
  545. static const int mode1_syscalls[] = {
  546. __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
  547. 0, /* null terminated */
  548. };
  549. static void __secure_computing_strict(int this_syscall)
  550. {
  551. const int *syscall_whitelist = mode1_syscalls;
  552. #ifdef CONFIG_COMPAT
  553. if (in_compat_syscall())
  554. syscall_whitelist = get_compat_mode1_syscalls();
  555. #endif
  556. do {
  557. if (*syscall_whitelist == this_syscall)
  558. return;
  559. } while (*++syscall_whitelist);
  560. #ifdef SECCOMP_DEBUG
  561. dump_stack();
  562. #endif
  563. seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
  564. do_exit(SIGKILL);
  565. }
  566. #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
  567. void secure_computing_strict(int this_syscall)
  568. {
  569. int mode = current->seccomp.mode;
  570. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  571. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  572. return;
  573. if (mode == SECCOMP_MODE_DISABLED)
  574. return;
  575. else if (mode == SECCOMP_MODE_STRICT)
  576. __secure_computing_strict(this_syscall);
  577. else
  578. BUG();
  579. }
  580. #else
  581. #ifdef CONFIG_SECCOMP_FILTER
  582. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  583. const bool recheck_after_trace)
  584. {
  585. u32 filter_ret, action;
  586. struct seccomp_filter *match = NULL;
  587. int data;
  588. /*
  589. * Make sure that any changes to mode from another thread have
  590. * been seen after TIF_SECCOMP was seen.
  591. */
  592. rmb();
  593. filter_ret = seccomp_run_filters(sd, &match);
  594. data = filter_ret & SECCOMP_RET_DATA;
  595. action = filter_ret & SECCOMP_RET_ACTION_FULL;
  596. switch (action) {
  597. case SECCOMP_RET_ERRNO:
  598. /* Set low-order bits as an errno, capped at MAX_ERRNO. */
  599. if (data > MAX_ERRNO)
  600. data = MAX_ERRNO;
  601. syscall_set_return_value(current, task_pt_regs(current),
  602. -data, 0);
  603. goto skip;
  604. case SECCOMP_RET_TRAP:
  605. /* Show the handler the original registers. */
  606. syscall_rollback(current, task_pt_regs(current));
  607. /* Let the filter pass back 16 bits of data. */
  608. seccomp_send_sigsys(this_syscall, data);
  609. goto skip;
  610. case SECCOMP_RET_TRACE:
  611. /* We've been put in this state by the ptracer already. */
  612. if (recheck_after_trace)
  613. return 0;
  614. /* ENOSYS these calls if there is no tracer attached. */
  615. if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
  616. syscall_set_return_value(current,
  617. task_pt_regs(current),
  618. -ENOSYS, 0);
  619. goto skip;
  620. }
  621. /* Allow the BPF to provide the event message */
  622. ptrace_event(PTRACE_EVENT_SECCOMP, data);
  623. /*
  624. * The delivery of a fatal signal during event
  625. * notification may silently skip tracer notification,
  626. * which could leave us with a potentially unmodified
  627. * syscall that the tracer would have liked to have
  628. * changed. Since the process is about to die, we just
  629. * force the syscall to be skipped and let the signal
  630. * kill the process and correctly handle any tracer exit
  631. * notifications.
  632. */
  633. if (fatal_signal_pending(current))
  634. goto skip;
  635. /* Check if the tracer forced the syscall to be skipped. */
  636. this_syscall = syscall_get_nr(current, task_pt_regs(current));
  637. if (this_syscall < 0)
  638. goto skip;
  639. /*
  640. * Recheck the syscall, since it may have changed. This
  641. * intentionally uses a NULL struct seccomp_data to force
  642. * a reload of all registers. This does not goto skip since
  643. * a skip would have already been reported.
  644. */
  645. if (__seccomp_filter(this_syscall, NULL, true))
  646. return -1;
  647. return 0;
  648. case SECCOMP_RET_LOG:
  649. seccomp_log(this_syscall, 0, action, true);
  650. return 0;
  651. case SECCOMP_RET_ALLOW:
  652. /*
  653. * Note that the "match" filter will always be NULL for
  654. * this action since SECCOMP_RET_ALLOW is the starting
  655. * state in seccomp_run_filters().
  656. */
  657. return 0;
  658. case SECCOMP_RET_KILL_THREAD:
  659. case SECCOMP_RET_KILL_PROCESS:
  660. default:
  661. seccomp_log(this_syscall, SIGSYS, action, true);
  662. /* Dump core only if this is the last remaining thread. */
  663. if (action == SECCOMP_RET_KILL_PROCESS ||
  664. get_nr_threads(current) == 1) {
  665. siginfo_t info;
  666. /* Show the original registers in the dump. */
  667. syscall_rollback(current, task_pt_regs(current));
  668. /* Trigger a manual coredump since do_exit skips it. */
  669. seccomp_init_siginfo(&info, this_syscall, data);
  670. do_coredump(&info);
  671. }
  672. if (action == SECCOMP_RET_KILL_PROCESS)
  673. do_group_exit(SIGSYS);
  674. else
  675. do_exit(SIGSYS);
  676. }
  677. unreachable();
  678. skip:
  679. seccomp_log(this_syscall, 0, action, match ? match->log : false);
  680. return -1;
  681. }
  682. #else
  683. static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
  684. const bool recheck_after_trace)
  685. {
  686. BUG();
  687. }
  688. #endif
  689. int __secure_computing(const struct seccomp_data *sd)
  690. {
  691. int mode = current->seccomp.mode;
  692. int this_syscall;
  693. if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
  694. unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
  695. return 0;
  696. this_syscall = sd ? sd->nr :
  697. syscall_get_nr(current, task_pt_regs(current));
  698. switch (mode) {
  699. case SECCOMP_MODE_STRICT:
  700. __secure_computing_strict(this_syscall); /* may call do_exit */
  701. return 0;
  702. case SECCOMP_MODE_FILTER:
  703. return __seccomp_filter(this_syscall, sd, false);
  704. default:
  705. BUG();
  706. }
  707. }
  708. #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
  709. long prctl_get_seccomp(void)
  710. {
  711. return current->seccomp.mode;
  712. }
  713. /**
  714. * seccomp_set_mode_strict: internal function for setting strict seccomp
  715. *
  716. * Once current->seccomp.mode is non-zero, it may not be changed.
  717. *
  718. * Returns 0 on success or -EINVAL on failure.
  719. */
  720. static long seccomp_set_mode_strict(void)
  721. {
  722. const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
  723. long ret = -EINVAL;
  724. spin_lock_irq(&current->sighand->siglock);
  725. if (!seccomp_may_assign_mode(seccomp_mode))
  726. goto out;
  727. #ifdef TIF_NOTSC
  728. disable_TSC();
  729. #endif
  730. seccomp_assign_mode(current, seccomp_mode, 0);
  731. ret = 0;
  732. out:
  733. spin_unlock_irq(&current->sighand->siglock);
  734. return ret;
  735. }
  736. #ifdef CONFIG_SECCOMP_FILTER
  737. /**
  738. * seccomp_set_mode_filter: internal function for setting seccomp filter
  739. * @flags: flags to change filter behavior
  740. * @filter: struct sock_fprog containing filter
  741. *
  742. * This function may be called repeatedly to install additional filters.
  743. * Every filter successfully installed will be evaluated (in reverse order)
  744. * for each system call the task makes.
  745. *
  746. * Once current->seccomp.mode is non-zero, it may not be changed.
  747. *
  748. * Returns 0 on success or -EINVAL on failure.
  749. */
  750. static long seccomp_set_mode_filter(unsigned int flags,
  751. const char __user *filter)
  752. {
  753. const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
  754. struct seccomp_filter *prepared = NULL;
  755. long ret = -EINVAL;
  756. /* Validate flags. */
  757. if (flags & ~SECCOMP_FILTER_FLAG_MASK)
  758. return -EINVAL;
  759. /* Prepare the new filter before holding any locks. */
  760. prepared = seccomp_prepare_user_filter(filter);
  761. if (IS_ERR(prepared))
  762. return PTR_ERR(prepared);
  763. /*
  764. * Make sure we cannot change seccomp or nnp state via TSYNC
  765. * while another thread is in the middle of calling exec.
  766. */
  767. if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
  768. mutex_lock_killable(&current->signal->cred_guard_mutex))
  769. goto out_free;
  770. spin_lock_irq(&current->sighand->siglock);
  771. if (!seccomp_may_assign_mode(seccomp_mode))
  772. goto out;
  773. ret = seccomp_attach_filter(flags, prepared);
  774. if (ret)
  775. goto out;
  776. /* Do not free the successfully attached filter. */
  777. prepared = NULL;
  778. seccomp_assign_mode(current, seccomp_mode, flags);
  779. out:
  780. spin_unlock_irq(&current->sighand->siglock);
  781. if (flags & SECCOMP_FILTER_FLAG_TSYNC)
  782. mutex_unlock(&current->signal->cred_guard_mutex);
  783. out_free:
  784. seccomp_filter_free(prepared);
  785. return ret;
  786. }
  787. #else
  788. static inline long seccomp_set_mode_filter(unsigned int flags,
  789. const char __user *filter)
  790. {
  791. return -EINVAL;
  792. }
  793. #endif
  794. static long seccomp_get_action_avail(const char __user *uaction)
  795. {
  796. u32 action;
  797. if (copy_from_user(&action, uaction, sizeof(action)))
  798. return -EFAULT;
  799. switch (action) {
  800. case SECCOMP_RET_KILL_PROCESS:
  801. case SECCOMP_RET_KILL_THREAD:
  802. case SECCOMP_RET_TRAP:
  803. case SECCOMP_RET_ERRNO:
  804. case SECCOMP_RET_TRACE:
  805. case SECCOMP_RET_LOG:
  806. case SECCOMP_RET_ALLOW:
  807. break;
  808. default:
  809. return -EOPNOTSUPP;
  810. }
  811. return 0;
  812. }
  813. /* Common entry point for both prctl and syscall. */
  814. static long do_seccomp(unsigned int op, unsigned int flags,
  815. const char __user *uargs)
  816. {
  817. switch (op) {
  818. case SECCOMP_SET_MODE_STRICT:
  819. if (flags != 0 || uargs != NULL)
  820. return -EINVAL;
  821. return seccomp_set_mode_strict();
  822. case SECCOMP_SET_MODE_FILTER:
  823. return seccomp_set_mode_filter(flags, uargs);
  824. case SECCOMP_GET_ACTION_AVAIL:
  825. if (flags != 0)
  826. return -EINVAL;
  827. return seccomp_get_action_avail(uargs);
  828. default:
  829. return -EINVAL;
  830. }
  831. }
  832. SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
  833. const char __user *, uargs)
  834. {
  835. return do_seccomp(op, flags, uargs);
  836. }
  837. /**
  838. * prctl_set_seccomp: configures current->seccomp.mode
  839. * @seccomp_mode: requested mode to use
  840. * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
  841. *
  842. * Returns 0 on success or -EINVAL on failure.
  843. */
  844. long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
  845. {
  846. unsigned int op;
  847. char __user *uargs;
  848. switch (seccomp_mode) {
  849. case SECCOMP_MODE_STRICT:
  850. op = SECCOMP_SET_MODE_STRICT;
  851. /*
  852. * Setting strict mode through prctl always ignored filter,
  853. * so make sure it is always NULL here to pass the internal
  854. * check in do_seccomp().
  855. */
  856. uargs = NULL;
  857. break;
  858. case SECCOMP_MODE_FILTER:
  859. op = SECCOMP_SET_MODE_FILTER;
  860. uargs = filter;
  861. break;
  862. default:
  863. return -EINVAL;
  864. }
  865. /* prctl interface doesn't have flags, so they are always zero. */
  866. return do_seccomp(op, 0, uargs);
  867. }
  868. #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
  869. static struct seccomp_filter *get_nth_filter(struct task_struct *task,
  870. unsigned long filter_off)
  871. {
  872. struct seccomp_filter *orig, *filter;
  873. unsigned long count;
  874. /*
  875. * Note: this is only correct because the caller should be the (ptrace)
  876. * tracer of the task, otherwise lock_task_sighand is needed.
  877. */
  878. spin_lock_irq(&task->sighand->siglock);
  879. if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
  880. spin_unlock_irq(&task->sighand->siglock);
  881. return ERR_PTR(-EINVAL);
  882. }
  883. orig = task->seccomp.filter;
  884. __get_seccomp_filter(orig);
  885. spin_unlock_irq(&task->sighand->siglock);
  886. count = 0;
  887. for (filter = orig; filter; filter = filter->prev)
  888. count++;
  889. if (filter_off >= count) {
  890. filter = ERR_PTR(-ENOENT);
  891. goto out;
  892. }
  893. count -= filter_off;
  894. for (filter = orig; filter && count > 1; filter = filter->prev)
  895. count--;
  896. if (WARN_ON(count != 1 || !filter)) {
  897. filter = ERR_PTR(-ENOENT);
  898. goto out;
  899. }
  900. __get_seccomp_filter(filter);
  901. out:
  902. __put_seccomp_filter(orig);
  903. return filter;
  904. }
  905. long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
  906. void __user *data)
  907. {
  908. struct seccomp_filter *filter;
  909. struct sock_fprog_kern *fprog;
  910. long ret;
  911. if (!capable(CAP_SYS_ADMIN) ||
  912. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  913. return -EACCES;
  914. }
  915. filter = get_nth_filter(task, filter_off);
  916. if (IS_ERR(filter))
  917. return PTR_ERR(filter);
  918. fprog = filter->prog->orig_prog;
  919. if (!fprog) {
  920. /* This must be a new non-cBPF filter, since we save
  921. * every cBPF filter's orig_prog above when
  922. * CONFIG_CHECKPOINT_RESTORE is enabled.
  923. */
  924. ret = -EMEDIUMTYPE;
  925. goto out;
  926. }
  927. ret = fprog->len;
  928. if (!data)
  929. goto out;
  930. if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
  931. ret = -EFAULT;
  932. out:
  933. __put_seccomp_filter(filter);
  934. return ret;
  935. }
  936. long seccomp_get_metadata(struct task_struct *task,
  937. unsigned long size, void __user *data)
  938. {
  939. long ret;
  940. struct seccomp_filter *filter;
  941. struct seccomp_metadata kmd = {};
  942. if (!capable(CAP_SYS_ADMIN) ||
  943. current->seccomp.mode != SECCOMP_MODE_DISABLED) {
  944. return -EACCES;
  945. }
  946. size = min_t(unsigned long, size, sizeof(kmd));
  947. if (size < sizeof(kmd.filter_off))
  948. return -EINVAL;
  949. if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
  950. return -EFAULT;
  951. filter = get_nth_filter(task, kmd.filter_off);
  952. if (IS_ERR(filter))
  953. return PTR_ERR(filter);
  954. if (filter->log)
  955. kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
  956. ret = size;
  957. if (copy_to_user(data, &kmd, size))
  958. ret = -EFAULT;
  959. __put_seccomp_filter(filter);
  960. return ret;
  961. }
  962. #endif
  963. #ifdef CONFIG_SYSCTL
  964. /* Human readable action names for friendly sysctl interaction */
  965. #define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
  966. #define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
  967. #define SECCOMP_RET_TRAP_NAME "trap"
  968. #define SECCOMP_RET_ERRNO_NAME "errno"
  969. #define SECCOMP_RET_TRACE_NAME "trace"
  970. #define SECCOMP_RET_LOG_NAME "log"
  971. #define SECCOMP_RET_ALLOW_NAME "allow"
  972. static const char seccomp_actions_avail[] =
  973. SECCOMP_RET_KILL_PROCESS_NAME " "
  974. SECCOMP_RET_KILL_THREAD_NAME " "
  975. SECCOMP_RET_TRAP_NAME " "
  976. SECCOMP_RET_ERRNO_NAME " "
  977. SECCOMP_RET_TRACE_NAME " "
  978. SECCOMP_RET_LOG_NAME " "
  979. SECCOMP_RET_ALLOW_NAME;
  980. struct seccomp_log_name {
  981. u32 log;
  982. const char *name;
  983. };
  984. static const struct seccomp_log_name seccomp_log_names[] = {
  985. { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
  986. { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
  987. { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
  988. { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
  989. { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
  990. { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
  991. { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
  992. { }
  993. };
  994. static bool seccomp_names_from_actions_logged(char *names, size_t size,
  995. u32 actions_logged,
  996. const char *sep)
  997. {
  998. const struct seccomp_log_name *cur;
  999. bool append_sep = false;
  1000. for (cur = seccomp_log_names; cur->name && size; cur++) {
  1001. ssize_t ret;
  1002. if (!(actions_logged & cur->log))
  1003. continue;
  1004. if (append_sep) {
  1005. ret = strscpy(names, sep, size);
  1006. if (ret < 0)
  1007. return false;
  1008. names += ret;
  1009. size -= ret;
  1010. } else
  1011. append_sep = true;
  1012. ret = strscpy(names, cur->name, size);
  1013. if (ret < 0)
  1014. return false;
  1015. names += ret;
  1016. size -= ret;
  1017. }
  1018. return true;
  1019. }
  1020. static bool seccomp_action_logged_from_name(u32 *action_logged,
  1021. const char *name)
  1022. {
  1023. const struct seccomp_log_name *cur;
  1024. for (cur = seccomp_log_names; cur->name; cur++) {
  1025. if (!strcmp(cur->name, name)) {
  1026. *action_logged = cur->log;
  1027. return true;
  1028. }
  1029. }
  1030. return false;
  1031. }
  1032. static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
  1033. {
  1034. char *name;
  1035. *actions_logged = 0;
  1036. while ((name = strsep(&names, " ")) && *name) {
  1037. u32 action_logged = 0;
  1038. if (!seccomp_action_logged_from_name(&action_logged, name))
  1039. return false;
  1040. *actions_logged |= action_logged;
  1041. }
  1042. return true;
  1043. }
  1044. static int read_actions_logged(struct ctl_table *ro_table, void __user *buffer,
  1045. size_t *lenp, loff_t *ppos)
  1046. {
  1047. char names[sizeof(seccomp_actions_avail)];
  1048. struct ctl_table table;
  1049. memset(names, 0, sizeof(names));
  1050. if (!seccomp_names_from_actions_logged(names, sizeof(names),
  1051. seccomp_actions_logged, " "))
  1052. return -EINVAL;
  1053. table = *ro_table;
  1054. table.data = names;
  1055. table.maxlen = sizeof(names);
  1056. return proc_dostring(&table, 0, buffer, lenp, ppos);
  1057. }
  1058. static int write_actions_logged(struct ctl_table *ro_table, void __user *buffer,
  1059. size_t *lenp, loff_t *ppos, u32 *actions_logged)
  1060. {
  1061. char names[sizeof(seccomp_actions_avail)];
  1062. struct ctl_table table;
  1063. int ret;
  1064. if (!capable(CAP_SYS_ADMIN))
  1065. return -EPERM;
  1066. memset(names, 0, sizeof(names));
  1067. table = *ro_table;
  1068. table.data = names;
  1069. table.maxlen = sizeof(names);
  1070. ret = proc_dostring(&table, 1, buffer, lenp, ppos);
  1071. if (ret)
  1072. return ret;
  1073. if (!seccomp_actions_logged_from_names(actions_logged, table.data))
  1074. return -EINVAL;
  1075. if (*actions_logged & SECCOMP_LOG_ALLOW)
  1076. return -EINVAL;
  1077. seccomp_actions_logged = *actions_logged;
  1078. return 0;
  1079. }
  1080. static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged,
  1081. int ret)
  1082. {
  1083. char names[sizeof(seccomp_actions_avail)];
  1084. char old_names[sizeof(seccomp_actions_avail)];
  1085. const char *new = names;
  1086. const char *old = old_names;
  1087. if (!audit_enabled)
  1088. return;
  1089. memset(names, 0, sizeof(names));
  1090. memset(old_names, 0, sizeof(old_names));
  1091. if (ret)
  1092. new = "?";
  1093. else if (!actions_logged)
  1094. new = "(none)";
  1095. else if (!seccomp_names_from_actions_logged(names, sizeof(names),
  1096. actions_logged, ","))
  1097. new = "?";
  1098. if (!old_actions_logged)
  1099. old = "(none)";
  1100. else if (!seccomp_names_from_actions_logged(old_names,
  1101. sizeof(old_names),
  1102. old_actions_logged, ","))
  1103. old = "?";
  1104. return audit_seccomp_actions_logged(new, old, !ret);
  1105. }
  1106. static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
  1107. void __user *buffer, size_t *lenp,
  1108. loff_t *ppos)
  1109. {
  1110. int ret;
  1111. if (write) {
  1112. u32 actions_logged = 0;
  1113. u32 old_actions_logged = seccomp_actions_logged;
  1114. ret = write_actions_logged(ro_table, buffer, lenp, ppos,
  1115. &actions_logged);
  1116. audit_actions_logged(actions_logged, old_actions_logged, ret);
  1117. } else
  1118. ret = read_actions_logged(ro_table, buffer, lenp, ppos);
  1119. return ret;
  1120. }
  1121. static struct ctl_path seccomp_sysctl_path[] = {
  1122. { .procname = "kernel", },
  1123. { .procname = "seccomp", },
  1124. { }
  1125. };
  1126. static struct ctl_table seccomp_sysctl_table[] = {
  1127. {
  1128. .procname = "actions_avail",
  1129. .data = (void *) &seccomp_actions_avail,
  1130. .maxlen = sizeof(seccomp_actions_avail),
  1131. .mode = 0444,
  1132. .proc_handler = proc_dostring,
  1133. },
  1134. {
  1135. .procname = "actions_logged",
  1136. .mode = 0644,
  1137. .proc_handler = seccomp_actions_logged_handler,
  1138. },
  1139. { }
  1140. };
  1141. static int __init seccomp_sysctl_init(void)
  1142. {
  1143. struct ctl_table_header *hdr;
  1144. hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
  1145. if (!hdr)
  1146. pr_warn("seccomp: sysctl registration failed\n");
  1147. else
  1148. kmemleak_not_leak(hdr);
  1149. return 0;
  1150. }
  1151. device_initcall(seccomp_sysctl_init)
  1152. #endif /* CONFIG_SYSCTL */