seccomp.c 34 KB

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