blob: 1d62fa2b6b9185a01ec6deee01d7c5a7ca5df928 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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
17#include <linux/refcount.h>
18#include <linux/audit.h>
19#include <linux/compat.h>
20#include <linux/coredump.h>
21#include <linux/kmemleak.h>
22#include <linux/nospec.h>
23#include <linux/prctl.h>
24#include <linux/sched.h>
25#include <linux/sched/task_stack.h>
26#include <linux/seccomp.h>
27#include <linux/slab.h>
28#include <linux/syscalls.h>
29#include <linux/sysctl.h>
30
31#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
32#include <asm/syscall.h>
33#endif
34
35#ifdef CONFIG_SECCOMP_FILTER
David Brazdil0f672f62019-12-10 10:32:29 +000036#include <linux/file.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037#include <linux/filter.h>
38#include <linux/pid.h>
39#include <linux/ptrace.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020040#include <linux/capability.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041#include <linux/tracehook.h>
42#include <linux/uaccess.h>
David Brazdil0f672f62019-12-10 10:32:29 +000043#include <linux/anon_inodes.h>
44
Olivier Deprez0e641232021-09-23 10:07:05 +020045/*
46 * When SECCOMP_IOCTL_NOTIF_ID_VALID was first introduced, it had the
47 * wrong direction flag in the ioctl number. This is the broken one,
48 * which the kernel needs to keep supporting until all userspaces stop
49 * using the wrong command number.
50 */
51#define SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR SECCOMP_IOR(2, __u64)
52
David Brazdil0f672f62019-12-10 10:32:29 +000053enum notify_state {
54 SECCOMP_NOTIFY_INIT,
55 SECCOMP_NOTIFY_SENT,
56 SECCOMP_NOTIFY_REPLIED,
57};
58
59struct seccomp_knotif {
60 /* The struct pid of the task whose filter triggered the notification */
61 struct task_struct *task;
62
63 /* The "cookie" for this request; this is unique for this filter. */
64 u64 id;
65
66 /*
67 * The seccomp data. This pointer is valid the entire time this
68 * notification is active, since it comes from __seccomp_filter which
69 * eclipses the entire lifecycle here.
70 */
71 const struct seccomp_data *data;
72
73 /*
74 * Notification states. When SECCOMP_RET_USER_NOTIF is returned, a
75 * struct seccomp_knotif is created and starts out in INIT. Once the
76 * handler reads the notification off of an FD, it transitions to SENT.
77 * If a signal is received the state transitions back to INIT and
78 * another message is sent. When the userspace handler replies, state
79 * transitions to REPLIED.
80 */
81 enum notify_state state;
82
83 /* The return values, only valid when in SECCOMP_NOTIFY_REPLIED */
84 int error;
85 long val;
86
87 /* Signals when this has entered SECCOMP_NOTIFY_REPLIED */
88 struct completion ready;
89
90 struct list_head list;
91};
92
93/**
94 * struct notification - container for seccomp userspace notifications. Since
95 * most seccomp filters will not have notification listeners attached and this
96 * structure is fairly large, we store the notification-specific stuff in a
97 * separate structure.
98 *
99 * @request: A semaphore that users of this notification can wait on for
100 * changes. Actual reads and writes are still controlled with
101 * filter->notify_lock.
102 * @next_id: The id of the next request.
103 * @notifications: A list of struct seccomp_knotif elements.
104 * @wqh: A wait queue for poll.
105 */
106struct notification {
107 struct semaphore request;
108 u64 next_id;
109 struct list_head notifications;
110 wait_queue_head_t wqh;
111};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000112
113/**
114 * struct seccomp_filter - container for seccomp BPF programs
115 *
116 * @usage: reference count to manage the object lifetime.
117 * get/put helpers should be used when accessing an instance
118 * outside of a lifetime-guarded section. In general, this
119 * is only needed for handling filters shared across tasks.
120 * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
121 * @prev: points to a previously installed, or inherited, filter
122 * @prog: the BPF program to evaluate
David Brazdil0f672f62019-12-10 10:32:29 +0000123 * @notif: the struct that holds all notification related information
124 * @notify_lock: A lock for all notification-related accesses.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125 *
126 * seccomp_filter objects are organized in a tree linked via the @prev
127 * pointer. For any task, it appears to be a singly-linked list starting
128 * with current->seccomp.filter, the most recently attached or inherited filter.
129 * However, multiple filters may share a @prev node, by way of fork(), which
130 * results in a unidirectional tree existing in memory. This is similar to
131 * how namespaces work.
132 *
133 * seccomp_filter objects should never be modified after being attached
134 * to a task_struct (other than @usage).
135 */
136struct seccomp_filter {
137 refcount_t usage;
138 bool log;
139 struct seccomp_filter *prev;
140 struct bpf_prog *prog;
David Brazdil0f672f62019-12-10 10:32:29 +0000141 struct notification *notif;
142 struct mutex notify_lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143};
144
145/* Limit any path through the tree to 256KB worth of instructions. */
146#define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
147
148/*
149 * Endianness is explicitly ignored and left for BPF program authors to manage
150 * as per the specific architecture.
151 */
152static void populate_seccomp_data(struct seccomp_data *sd)
153{
154 struct task_struct *task = current;
155 struct pt_regs *regs = task_pt_regs(task);
156 unsigned long args[6];
157
158 sd->nr = syscall_get_nr(task, regs);
David Brazdil0f672f62019-12-10 10:32:29 +0000159 sd->arch = syscall_get_arch(task);
160 syscall_get_arguments(task, regs, args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161 sd->args[0] = args[0];
162 sd->args[1] = args[1];
163 sd->args[2] = args[2];
164 sd->args[3] = args[3];
165 sd->args[4] = args[4];
166 sd->args[5] = args[5];
167 sd->instruction_pointer = KSTK_EIP(task);
168}
169
170/**
171 * seccomp_check_filter - verify seccomp filter code
172 * @filter: filter to verify
173 * @flen: length of filter
174 *
175 * Takes a previously checked filter (by bpf_check_classic) and
176 * redirects all filter code that loads struct sk_buff data
177 * and related data through seccomp_bpf_load. It also
178 * enforces length and alignment checking of those loads.
179 *
180 * Returns 0 if the rule set is legal or -EINVAL if not.
181 */
182static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
183{
184 int pc;
185 for (pc = 0; pc < flen; pc++) {
186 struct sock_filter *ftest = &filter[pc];
187 u16 code = ftest->code;
188 u32 k = ftest->k;
189
190 switch (code) {
191 case BPF_LD | BPF_W | BPF_ABS:
192 ftest->code = BPF_LDX | BPF_W | BPF_ABS;
193 /* 32-bit aligned and not out of bounds. */
194 if (k >= sizeof(struct seccomp_data) || k & 3)
195 return -EINVAL;
196 continue;
197 case BPF_LD | BPF_W | BPF_LEN:
198 ftest->code = BPF_LD | BPF_IMM;
199 ftest->k = sizeof(struct seccomp_data);
200 continue;
201 case BPF_LDX | BPF_W | BPF_LEN:
202 ftest->code = BPF_LDX | BPF_IMM;
203 ftest->k = sizeof(struct seccomp_data);
204 continue;
205 /* Explicitly include allowed calls. */
206 case BPF_RET | BPF_K:
207 case BPF_RET | BPF_A:
208 case BPF_ALU | BPF_ADD | BPF_K:
209 case BPF_ALU | BPF_ADD | BPF_X:
210 case BPF_ALU | BPF_SUB | BPF_K:
211 case BPF_ALU | BPF_SUB | BPF_X:
212 case BPF_ALU | BPF_MUL | BPF_K:
213 case BPF_ALU | BPF_MUL | BPF_X:
214 case BPF_ALU | BPF_DIV | BPF_K:
215 case BPF_ALU | BPF_DIV | BPF_X:
216 case BPF_ALU | BPF_AND | BPF_K:
217 case BPF_ALU | BPF_AND | BPF_X:
218 case BPF_ALU | BPF_OR | BPF_K:
219 case BPF_ALU | BPF_OR | BPF_X:
220 case BPF_ALU | BPF_XOR | BPF_K:
221 case BPF_ALU | BPF_XOR | BPF_X:
222 case BPF_ALU | BPF_LSH | BPF_K:
223 case BPF_ALU | BPF_LSH | BPF_X:
224 case BPF_ALU | BPF_RSH | BPF_K:
225 case BPF_ALU | BPF_RSH | BPF_X:
226 case BPF_ALU | BPF_NEG:
227 case BPF_LD | BPF_IMM:
228 case BPF_LDX | BPF_IMM:
229 case BPF_MISC | BPF_TAX:
230 case BPF_MISC | BPF_TXA:
231 case BPF_LD | BPF_MEM:
232 case BPF_LDX | BPF_MEM:
233 case BPF_ST:
234 case BPF_STX:
235 case BPF_JMP | BPF_JA:
236 case BPF_JMP | BPF_JEQ | BPF_K:
237 case BPF_JMP | BPF_JEQ | BPF_X:
238 case BPF_JMP | BPF_JGE | BPF_K:
239 case BPF_JMP | BPF_JGE | BPF_X:
240 case BPF_JMP | BPF_JGT | BPF_K:
241 case BPF_JMP | BPF_JGT | BPF_X:
242 case BPF_JMP | BPF_JSET | BPF_K:
243 case BPF_JMP | BPF_JSET | BPF_X:
244 continue;
245 default:
246 return -EINVAL;
247 }
248 }
249 return 0;
250}
251
252/**
253 * seccomp_run_filters - evaluates all seccomp filters against @sd
254 * @sd: optional seccomp data to be passed to filters
255 * @match: stores struct seccomp_filter that resulted in the return value,
256 * unless filter returned SECCOMP_RET_ALLOW, in which case it will
257 * be unchanged.
258 *
259 * Returns valid seccomp BPF response codes.
260 */
261#define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
262static u32 seccomp_run_filters(const struct seccomp_data *sd,
263 struct seccomp_filter **match)
264{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265 u32 ret = SECCOMP_RET_ALLOW;
266 /* Make sure cross-thread synced filter points somewhere sane. */
267 struct seccomp_filter *f =
268 READ_ONCE(current->seccomp.filter);
269
270 /* Ensure unexpected behavior doesn't result in failing open. */
David Brazdil0f672f62019-12-10 10:32:29 +0000271 if (WARN_ON(f == NULL))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000272 return SECCOMP_RET_KILL_PROCESS;
273
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000274 /*
275 * All filters in the list are evaluated and the lowest BPF return
276 * value always takes priority (ignoring the DATA).
277 */
David Brazdil0f672f62019-12-10 10:32:29 +0000278 preempt_disable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 for (; f; f = f->prev) {
280 u32 cur_ret = BPF_PROG_RUN(f->prog, sd);
281
282 if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
283 ret = cur_ret;
284 *match = f;
285 }
286 }
David Brazdil0f672f62019-12-10 10:32:29 +0000287 preempt_enable();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 return ret;
289}
290#endif /* CONFIG_SECCOMP_FILTER */
291
292static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
293{
294 assert_spin_locked(&current->sighand->siglock);
295
296 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
297 return false;
298
299 return true;
300}
301
302void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
303
304static inline void seccomp_assign_mode(struct task_struct *task,
305 unsigned long seccomp_mode,
306 unsigned long flags)
307{
308 assert_spin_locked(&task->sighand->siglock);
309
310 task->seccomp.mode = seccomp_mode;
311 /*
312 * Make sure TIF_SECCOMP cannot be set before the mode (and
313 * filter) is set.
314 */
315 smp_mb__before_atomic();
316 /* Assume default seccomp processes want spec flaw mitigation. */
317 if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
318 arch_seccomp_spec_mitigate(task);
319 set_tsk_thread_flag(task, TIF_SECCOMP);
320}
321
322#ifdef CONFIG_SECCOMP_FILTER
323/* Returns 1 if the parent is an ancestor of the child. */
324static int is_ancestor(struct seccomp_filter *parent,
325 struct seccomp_filter *child)
326{
327 /* NULL is the root ancestor. */
328 if (parent == NULL)
329 return 1;
330 for (; child; child = child->prev)
331 if (child == parent)
332 return 1;
333 return 0;
334}
335
336/**
337 * seccomp_can_sync_threads: checks if all threads can be synchronized
338 *
339 * Expects sighand and cred_guard_mutex locks to be held.
340 *
341 * Returns 0 on success, -ve on error, or the pid of a thread which was
David Brazdil0f672f62019-12-10 10:32:29 +0000342 * either not in the correct seccomp mode or did not have an ancestral
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000343 * seccomp filter.
344 */
345static inline pid_t seccomp_can_sync_threads(void)
346{
347 struct task_struct *thread, *caller;
348
349 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
350 assert_spin_locked(&current->sighand->siglock);
351
352 /* Validate all threads being eligible for synchronization. */
353 caller = current;
354 for_each_thread(caller, thread) {
355 pid_t failed;
356
357 /* Skip current, since it is initiating the sync. */
358 if (thread == caller)
359 continue;
360
361 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
362 (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
363 is_ancestor(thread->seccomp.filter,
364 caller->seccomp.filter)))
365 continue;
366
367 /* Return the first thread that cannot be synchronized. */
368 failed = task_pid_vnr(thread);
369 /* If the pid cannot be resolved, then return -ESRCH */
David Brazdil0f672f62019-12-10 10:32:29 +0000370 if (WARN_ON(failed == 0))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000371 failed = -ESRCH;
372 return failed;
373 }
374
375 return 0;
376}
377
378/**
379 * seccomp_sync_threads: sets all threads to use current's filter
380 *
381 * Expects sighand and cred_guard_mutex locks to be held, and for
382 * seccomp_can_sync_threads() to have returned success already
383 * without dropping the locks.
384 *
385 */
386static inline void seccomp_sync_threads(unsigned long flags)
387{
388 struct task_struct *thread, *caller;
389
390 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
391 assert_spin_locked(&current->sighand->siglock);
392
393 /* Synchronize all threads. */
394 caller = current;
395 for_each_thread(caller, thread) {
396 /* Skip current, since it needs no changes. */
397 if (thread == caller)
398 continue;
399
400 /* Get a task reference for the new leaf node. */
401 get_seccomp_filter(caller);
402 /*
403 * Drop the task reference to the shared ancestor since
404 * current's path will hold a reference. (This also
405 * allows a put before the assignment.)
406 */
407 put_seccomp_filter(thread);
408 smp_store_release(&thread->seccomp.filter,
409 caller->seccomp.filter);
410
411 /*
412 * Don't let an unprivileged task work around
413 * the no_new_privs restriction by creating
414 * a thread that sets it up, enters seccomp,
415 * then dies.
416 */
417 if (task_no_new_privs(caller))
418 task_set_no_new_privs(thread);
419
420 /*
421 * Opt the other thread into seccomp if needed.
422 * As threads are considered to be trust-realm
423 * equivalent (see ptrace_may_access), it is safe to
424 * allow one thread to transition the other.
425 */
426 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
427 seccomp_assign_mode(thread, SECCOMP_MODE_FILTER,
428 flags);
429 }
430}
431
432/**
433 * seccomp_prepare_filter: Prepares a seccomp filter for use.
434 * @fprog: BPF program to install
435 *
436 * Returns filter on success or an ERR_PTR on failure.
437 */
438static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
439{
440 struct seccomp_filter *sfilter;
441 int ret;
442 const bool save_orig = IS_ENABLED(CONFIG_CHECKPOINT_RESTORE);
443
444 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
445 return ERR_PTR(-EINVAL);
446
447 BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
448
449 /*
450 * Installing a seccomp filter requires that the task has
451 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
452 * This avoids scenarios where unprivileged tasks can affect the
453 * behavior of privileged children.
454 */
455 if (!task_no_new_privs(current) &&
Olivier Deprez0e641232021-09-23 10:07:05 +0200456 !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000457 return ERR_PTR(-EACCES);
458
459 /* Allocate a new seccomp_filter */
460 sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN);
461 if (!sfilter)
462 return ERR_PTR(-ENOMEM);
463
David Brazdil0f672f62019-12-10 10:32:29 +0000464 mutex_init(&sfilter->notify_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465 ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
466 seccomp_check_filter, save_orig);
467 if (ret < 0) {
468 kfree(sfilter);
469 return ERR_PTR(ret);
470 }
471
472 refcount_set(&sfilter->usage, 1);
473
474 return sfilter;
475}
476
477/**
478 * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
479 * @user_filter: pointer to the user data containing a sock_fprog.
480 *
481 * Returns 0 on success and non-zero otherwise.
482 */
483static struct seccomp_filter *
484seccomp_prepare_user_filter(const char __user *user_filter)
485{
486 struct sock_fprog fprog;
487 struct seccomp_filter *filter = ERR_PTR(-EFAULT);
488
489#ifdef CONFIG_COMPAT
490 if (in_compat_syscall()) {
491 struct compat_sock_fprog fprog32;
492 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
493 goto out;
494 fprog.len = fprog32.len;
495 fprog.filter = compat_ptr(fprog32.filter);
496 } else /* falls through to the if below. */
497#endif
498 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
499 goto out;
500 filter = seccomp_prepare_filter(&fprog);
501out:
502 return filter;
503}
504
505/**
506 * seccomp_attach_filter: validate and attach filter
507 * @flags: flags to change filter behavior
508 * @filter: seccomp filter to add to the current process
509 *
510 * Caller must be holding current->sighand->siglock lock.
511 *
David Brazdil0f672f62019-12-10 10:32:29 +0000512 * Returns 0 on success, -ve on error, or
513 * - in TSYNC mode: the pid of a thread which was either not in the correct
514 * seccomp mode or did not have an ancestral seccomp filter
515 * - in NEW_LISTENER mode: the fd of the new listener
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516 */
517static long seccomp_attach_filter(unsigned int flags,
518 struct seccomp_filter *filter)
519{
520 unsigned long total_insns;
521 struct seccomp_filter *walker;
522
523 assert_spin_locked(&current->sighand->siglock);
524
525 /* Validate resulting filter length. */
526 total_insns = filter->prog->len;
527 for (walker = current->seccomp.filter; walker; walker = walker->prev)
528 total_insns += walker->prog->len + 4; /* 4 instr penalty */
529 if (total_insns > MAX_INSNS_PER_PATH)
530 return -ENOMEM;
531
532 /* If thread sync has been requested, check that it is possible. */
533 if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
534 int ret;
535
536 ret = seccomp_can_sync_threads();
537 if (ret)
538 return ret;
539 }
540
541 /* Set log flag, if present. */
542 if (flags & SECCOMP_FILTER_FLAG_LOG)
543 filter->log = true;
544
545 /*
546 * If there is an existing filter, make it the prev and don't drop its
547 * task reference.
548 */
549 filter->prev = current->seccomp.filter;
550 current->seccomp.filter = filter;
551
552 /* Now that the new filter is in place, synchronize to all threads. */
553 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
554 seccomp_sync_threads(flags);
555
556 return 0;
557}
558
559static void __get_seccomp_filter(struct seccomp_filter *filter)
560{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000561 refcount_inc(&filter->usage);
562}
563
564/* get_seccomp_filter - increments the reference count of the filter on @tsk */
565void get_seccomp_filter(struct task_struct *tsk)
566{
567 struct seccomp_filter *orig = tsk->seccomp.filter;
568 if (!orig)
569 return;
570 __get_seccomp_filter(orig);
571}
572
573static inline void seccomp_filter_free(struct seccomp_filter *filter)
574{
575 if (filter) {
576 bpf_prog_destroy(filter->prog);
577 kfree(filter);
578 }
579}
580
581static void __put_seccomp_filter(struct seccomp_filter *orig)
582{
583 /* Clean up single-reference branches iteratively. */
584 while (orig && refcount_dec_and_test(&orig->usage)) {
585 struct seccomp_filter *freeme = orig;
586 orig = orig->prev;
587 seccomp_filter_free(freeme);
588 }
589}
590
591/* put_seccomp_filter - decrements the ref count of tsk->seccomp.filter */
592void put_seccomp_filter(struct task_struct *tsk)
593{
594 __put_seccomp_filter(tsk->seccomp.filter);
595}
596
David Brazdil0f672f62019-12-10 10:32:29 +0000597static void seccomp_init_siginfo(kernel_siginfo_t *info, int syscall, int reason)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598{
599 clear_siginfo(info);
600 info->si_signo = SIGSYS;
601 info->si_code = SYS_SECCOMP;
602 info->si_call_addr = (void __user *)KSTK_EIP(current);
603 info->si_errno = reason;
David Brazdil0f672f62019-12-10 10:32:29 +0000604 info->si_arch = syscall_get_arch(current);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000605 info->si_syscall = syscall;
606}
607
608/**
609 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
610 * @syscall: syscall number to send to userland
611 * @reason: filter-supplied reason code to send to userland (via si_errno)
612 *
613 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
614 */
615static void seccomp_send_sigsys(int syscall, int reason)
616{
David Brazdil0f672f62019-12-10 10:32:29 +0000617 struct kernel_siginfo info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000618 seccomp_init_siginfo(&info, syscall, reason);
David Brazdil0f672f62019-12-10 10:32:29 +0000619 force_sig_info(&info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000620}
621#endif /* CONFIG_SECCOMP_FILTER */
622
623/* For use with seccomp_actions_logged */
624#define SECCOMP_LOG_KILL_PROCESS (1 << 0)
625#define SECCOMP_LOG_KILL_THREAD (1 << 1)
626#define SECCOMP_LOG_TRAP (1 << 2)
627#define SECCOMP_LOG_ERRNO (1 << 3)
628#define SECCOMP_LOG_TRACE (1 << 4)
629#define SECCOMP_LOG_LOG (1 << 5)
630#define SECCOMP_LOG_ALLOW (1 << 6)
David Brazdil0f672f62019-12-10 10:32:29 +0000631#define SECCOMP_LOG_USER_NOTIF (1 << 7)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000632
633static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
634 SECCOMP_LOG_KILL_THREAD |
635 SECCOMP_LOG_TRAP |
636 SECCOMP_LOG_ERRNO |
David Brazdil0f672f62019-12-10 10:32:29 +0000637 SECCOMP_LOG_USER_NOTIF |
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000638 SECCOMP_LOG_TRACE |
639 SECCOMP_LOG_LOG;
640
641static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
642 bool requested)
643{
644 bool log = false;
645
646 switch (action) {
647 case SECCOMP_RET_ALLOW:
648 break;
649 case SECCOMP_RET_TRAP:
650 log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
651 break;
652 case SECCOMP_RET_ERRNO:
653 log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
654 break;
655 case SECCOMP_RET_TRACE:
656 log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
657 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000658 case SECCOMP_RET_USER_NOTIF:
659 log = requested && seccomp_actions_logged & SECCOMP_LOG_USER_NOTIF;
660 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661 case SECCOMP_RET_LOG:
662 log = seccomp_actions_logged & SECCOMP_LOG_LOG;
663 break;
664 case SECCOMP_RET_KILL_THREAD:
665 log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
666 break;
667 case SECCOMP_RET_KILL_PROCESS:
668 default:
669 log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
670 }
671
672 /*
673 * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the
674 * FILTER_FLAG_LOG bit was set. The admin has the ability to silence
675 * any action from being logged by removing the action name from the
676 * seccomp_actions_logged sysctl.
677 */
678 if (!log)
679 return;
680
681 audit_seccomp(syscall, signr, action);
682}
683
684/*
685 * Secure computing mode 1 allows only read/write/exit/sigreturn.
686 * To be fully secure this must be combined with rlimit
687 * to limit the stack allocations too.
688 */
689static const int mode1_syscalls[] = {
690 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
691 0, /* null terminated */
692};
693
694static void __secure_computing_strict(int this_syscall)
695{
696 const int *syscall_whitelist = mode1_syscalls;
697#ifdef CONFIG_COMPAT
698 if (in_compat_syscall())
699 syscall_whitelist = get_compat_mode1_syscalls();
700#endif
701 do {
702 if (*syscall_whitelist == this_syscall)
703 return;
704 } while (*++syscall_whitelist);
705
706#ifdef SECCOMP_DEBUG
707 dump_stack();
708#endif
709 seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
710 do_exit(SIGKILL);
711}
712
713#ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
714void secure_computing_strict(int this_syscall)
715{
716 int mode = current->seccomp.mode;
717
718 if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
719 unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
720 return;
721
722 if (mode == SECCOMP_MODE_DISABLED)
723 return;
724 else if (mode == SECCOMP_MODE_STRICT)
725 __secure_computing_strict(this_syscall);
726 else
727 BUG();
728}
729#else
730
731#ifdef CONFIG_SECCOMP_FILTER
David Brazdil0f672f62019-12-10 10:32:29 +0000732static u64 seccomp_next_notify_id(struct seccomp_filter *filter)
733{
734 /*
735 * Note: overflow is ok here, the id just needs to be unique per
736 * filter.
737 */
738 lockdep_assert_held(&filter->notify_lock);
739 return filter->notif->next_id++;
740}
741
742static void seccomp_do_user_notification(int this_syscall,
743 struct seccomp_filter *match,
744 const struct seccomp_data *sd)
745{
746 int err;
747 long ret = 0;
748 struct seccomp_knotif n = {};
749
750 mutex_lock(&match->notify_lock);
751 err = -ENOSYS;
752 if (!match->notif)
753 goto out;
754
755 n.task = current;
756 n.state = SECCOMP_NOTIFY_INIT;
757 n.data = sd;
758 n.id = seccomp_next_notify_id(match);
759 init_completion(&n.ready);
760 list_add(&n.list, &match->notif->notifications);
761
762 up(&match->notif->request);
763 wake_up_poll(&match->notif->wqh, EPOLLIN | EPOLLRDNORM);
764 mutex_unlock(&match->notify_lock);
765
766 /*
767 * This is where we wait for a reply from userspace.
768 */
769 err = wait_for_completion_interruptible(&n.ready);
770 mutex_lock(&match->notify_lock);
771 if (err == 0) {
772 ret = n.val;
773 err = n.error;
774 }
775
776 /*
777 * Note that it's possible the listener died in between the time when
778 * we were notified of a respons (or a signal) and when we were able to
779 * re-acquire the lock, so only delete from the list if the
780 * notification actually exists.
781 *
782 * Also note that this test is only valid because there's no way to
783 * *reattach* to a notifier right now. If one is added, we'll need to
784 * keep track of the notif itself and make sure they match here.
785 */
786 if (match->notif)
787 list_del(&n.list);
788out:
789 mutex_unlock(&match->notify_lock);
790 syscall_set_return_value(current, task_pt_regs(current),
791 err, ret);
792}
793
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
795 const bool recheck_after_trace)
796{
797 u32 filter_ret, action;
798 struct seccomp_filter *match = NULL;
799 int data;
David Brazdil0f672f62019-12-10 10:32:29 +0000800 struct seccomp_data sd_local;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801
802 /*
803 * Make sure that any changes to mode from another thread have
804 * been seen after TIF_SECCOMP was seen.
805 */
806 rmb();
807
David Brazdil0f672f62019-12-10 10:32:29 +0000808 if (!sd) {
809 populate_seccomp_data(&sd_local);
810 sd = &sd_local;
811 }
812
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 filter_ret = seccomp_run_filters(sd, &match);
814 data = filter_ret & SECCOMP_RET_DATA;
815 action = filter_ret & SECCOMP_RET_ACTION_FULL;
816
817 switch (action) {
818 case SECCOMP_RET_ERRNO:
819 /* Set low-order bits as an errno, capped at MAX_ERRNO. */
820 if (data > MAX_ERRNO)
821 data = MAX_ERRNO;
822 syscall_set_return_value(current, task_pt_regs(current),
823 -data, 0);
824 goto skip;
825
826 case SECCOMP_RET_TRAP:
827 /* Show the handler the original registers. */
828 syscall_rollback(current, task_pt_regs(current));
829 /* Let the filter pass back 16 bits of data. */
830 seccomp_send_sigsys(this_syscall, data);
831 goto skip;
832
833 case SECCOMP_RET_TRACE:
834 /* We've been put in this state by the ptracer already. */
835 if (recheck_after_trace)
836 return 0;
837
838 /* ENOSYS these calls if there is no tracer attached. */
839 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
840 syscall_set_return_value(current,
841 task_pt_regs(current),
842 -ENOSYS, 0);
843 goto skip;
844 }
845
846 /* Allow the BPF to provide the event message */
847 ptrace_event(PTRACE_EVENT_SECCOMP, data);
848 /*
849 * The delivery of a fatal signal during event
850 * notification may silently skip tracer notification,
851 * which could leave us with a potentially unmodified
852 * syscall that the tracer would have liked to have
853 * changed. Since the process is about to die, we just
854 * force the syscall to be skipped and let the signal
855 * kill the process and correctly handle any tracer exit
856 * notifications.
857 */
858 if (fatal_signal_pending(current))
859 goto skip;
860 /* Check if the tracer forced the syscall to be skipped. */
861 this_syscall = syscall_get_nr(current, task_pt_regs(current));
862 if (this_syscall < 0)
863 goto skip;
864
865 /*
866 * Recheck the syscall, since it may have changed. This
867 * intentionally uses a NULL struct seccomp_data to force
868 * a reload of all registers. This does not goto skip since
869 * a skip would have already been reported.
870 */
871 if (__seccomp_filter(this_syscall, NULL, true))
872 return -1;
873
874 return 0;
875
David Brazdil0f672f62019-12-10 10:32:29 +0000876 case SECCOMP_RET_USER_NOTIF:
877 seccomp_do_user_notification(this_syscall, match, sd);
878 goto skip;
879
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000880 case SECCOMP_RET_LOG:
881 seccomp_log(this_syscall, 0, action, true);
882 return 0;
883
884 case SECCOMP_RET_ALLOW:
885 /*
886 * Note that the "match" filter will always be NULL for
887 * this action since SECCOMP_RET_ALLOW is the starting
888 * state in seccomp_run_filters().
889 */
890 return 0;
891
892 case SECCOMP_RET_KILL_THREAD:
893 case SECCOMP_RET_KILL_PROCESS:
894 default:
895 seccomp_log(this_syscall, SIGSYS, action, true);
896 /* Dump core only if this is the last remaining thread. */
897 if (action == SECCOMP_RET_KILL_PROCESS ||
898 get_nr_threads(current) == 1) {
David Brazdil0f672f62019-12-10 10:32:29 +0000899 kernel_siginfo_t info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000900
901 /* Show the original registers in the dump. */
902 syscall_rollback(current, task_pt_regs(current));
903 /* Trigger a manual coredump since do_exit skips it. */
904 seccomp_init_siginfo(&info, this_syscall, data);
905 do_coredump(&info);
906 }
907 if (action == SECCOMP_RET_KILL_PROCESS)
908 do_group_exit(SIGSYS);
909 else
910 do_exit(SIGSYS);
911 }
912
913 unreachable();
914
915skip:
916 seccomp_log(this_syscall, 0, action, match ? match->log : false);
917 return -1;
918}
919#else
920static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
921 const bool recheck_after_trace)
922{
923 BUG();
Olivier Deprez0e641232021-09-23 10:07:05 +0200924
925 return -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000926}
927#endif
928
929int __secure_computing(const struct seccomp_data *sd)
930{
931 int mode = current->seccomp.mode;
932 int this_syscall;
933
934 if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
935 unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
936 return 0;
937
938 this_syscall = sd ? sd->nr :
939 syscall_get_nr(current, task_pt_regs(current));
940
941 switch (mode) {
942 case SECCOMP_MODE_STRICT:
943 __secure_computing_strict(this_syscall); /* may call do_exit */
944 return 0;
945 case SECCOMP_MODE_FILTER:
946 return __seccomp_filter(this_syscall, sd, false);
947 default:
948 BUG();
949 }
950}
951#endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
952
953long prctl_get_seccomp(void)
954{
955 return current->seccomp.mode;
956}
957
958/**
959 * seccomp_set_mode_strict: internal function for setting strict seccomp
960 *
961 * Once current->seccomp.mode is non-zero, it may not be changed.
962 *
963 * Returns 0 on success or -EINVAL on failure.
964 */
965static long seccomp_set_mode_strict(void)
966{
967 const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
968 long ret = -EINVAL;
969
970 spin_lock_irq(&current->sighand->siglock);
971
972 if (!seccomp_may_assign_mode(seccomp_mode))
973 goto out;
974
975#ifdef TIF_NOTSC
976 disable_TSC();
977#endif
978 seccomp_assign_mode(current, seccomp_mode, 0);
979 ret = 0;
980
981out:
982 spin_unlock_irq(&current->sighand->siglock);
983
984 return ret;
985}
986
987#ifdef CONFIG_SECCOMP_FILTER
David Brazdil0f672f62019-12-10 10:32:29 +0000988static int seccomp_notify_release(struct inode *inode, struct file *file)
989{
990 struct seccomp_filter *filter = file->private_data;
991 struct seccomp_knotif *knotif;
992
993 if (!filter)
994 return 0;
995
996 mutex_lock(&filter->notify_lock);
997
998 /*
999 * If this file is being closed because e.g. the task who owned it
1000 * died, let's wake everyone up who was waiting on us.
1001 */
1002 list_for_each_entry(knotif, &filter->notif->notifications, list) {
1003 if (knotif->state == SECCOMP_NOTIFY_REPLIED)
1004 continue;
1005
1006 knotif->state = SECCOMP_NOTIFY_REPLIED;
1007 knotif->error = -ENOSYS;
1008 knotif->val = 0;
1009
1010 complete(&knotif->ready);
1011 }
1012
1013 kfree(filter->notif);
1014 filter->notif = NULL;
1015 mutex_unlock(&filter->notify_lock);
1016 __put_seccomp_filter(filter);
1017 return 0;
1018}
1019
1020static long seccomp_notify_recv(struct seccomp_filter *filter,
1021 void __user *buf)
1022{
1023 struct seccomp_knotif *knotif = NULL, *cur;
1024 struct seccomp_notif unotif;
1025 ssize_t ret;
1026
Olivier Deprez0e641232021-09-23 10:07:05 +02001027 /* Verify that we're not given garbage to keep struct extensible. */
1028 ret = check_zeroed_user(buf, sizeof(unotif));
1029 if (ret < 0)
1030 return ret;
1031 if (!ret)
1032 return -EINVAL;
1033
David Brazdil0f672f62019-12-10 10:32:29 +00001034 memset(&unotif, 0, sizeof(unotif));
1035
1036 ret = down_interruptible(&filter->notif->request);
1037 if (ret < 0)
1038 return ret;
1039
1040 mutex_lock(&filter->notify_lock);
1041 list_for_each_entry(cur, &filter->notif->notifications, list) {
1042 if (cur->state == SECCOMP_NOTIFY_INIT) {
1043 knotif = cur;
1044 break;
1045 }
1046 }
1047
1048 /*
1049 * If we didn't find a notification, it could be that the task was
1050 * interrupted by a fatal signal between the time we were woken and
1051 * when we were able to acquire the rw lock.
1052 */
1053 if (!knotif) {
1054 ret = -ENOENT;
1055 goto out;
1056 }
1057
1058 unotif.id = knotif->id;
1059 unotif.pid = task_pid_vnr(knotif->task);
1060 unotif.data = *(knotif->data);
1061
1062 knotif->state = SECCOMP_NOTIFY_SENT;
1063 wake_up_poll(&filter->notif->wqh, EPOLLOUT | EPOLLWRNORM);
1064 ret = 0;
1065out:
1066 mutex_unlock(&filter->notify_lock);
1067
1068 if (ret == 0 && copy_to_user(buf, &unotif, sizeof(unotif))) {
1069 ret = -EFAULT;
1070
1071 /*
1072 * Userspace screwed up. To make sure that we keep this
1073 * notification alive, let's reset it back to INIT. It
1074 * may have died when we released the lock, so we need to make
1075 * sure it's still around.
1076 */
1077 knotif = NULL;
1078 mutex_lock(&filter->notify_lock);
1079 list_for_each_entry(cur, &filter->notif->notifications, list) {
1080 if (cur->id == unotif.id) {
1081 knotif = cur;
1082 break;
1083 }
1084 }
1085
1086 if (knotif) {
1087 knotif->state = SECCOMP_NOTIFY_INIT;
1088 up(&filter->notif->request);
1089 }
1090 mutex_unlock(&filter->notify_lock);
1091 }
1092
1093 return ret;
1094}
1095
1096static long seccomp_notify_send(struct seccomp_filter *filter,
1097 void __user *buf)
1098{
1099 struct seccomp_notif_resp resp = {};
1100 struct seccomp_knotif *knotif = NULL, *cur;
1101 long ret;
1102
1103 if (copy_from_user(&resp, buf, sizeof(resp)))
1104 return -EFAULT;
1105
1106 if (resp.flags)
1107 return -EINVAL;
1108
1109 ret = mutex_lock_interruptible(&filter->notify_lock);
1110 if (ret < 0)
1111 return ret;
1112
1113 list_for_each_entry(cur, &filter->notif->notifications, list) {
1114 if (cur->id == resp.id) {
1115 knotif = cur;
1116 break;
1117 }
1118 }
1119
1120 if (!knotif) {
1121 ret = -ENOENT;
1122 goto out;
1123 }
1124
1125 /* Allow exactly one reply. */
1126 if (knotif->state != SECCOMP_NOTIFY_SENT) {
1127 ret = -EINPROGRESS;
1128 goto out;
1129 }
1130
1131 ret = 0;
1132 knotif->state = SECCOMP_NOTIFY_REPLIED;
1133 knotif->error = resp.error;
1134 knotif->val = resp.val;
1135 complete(&knotif->ready);
1136out:
1137 mutex_unlock(&filter->notify_lock);
1138 return ret;
1139}
1140
1141static long seccomp_notify_id_valid(struct seccomp_filter *filter,
1142 void __user *buf)
1143{
1144 struct seccomp_knotif *knotif = NULL;
1145 u64 id;
1146 long ret;
1147
1148 if (copy_from_user(&id, buf, sizeof(id)))
1149 return -EFAULT;
1150
1151 ret = mutex_lock_interruptible(&filter->notify_lock);
1152 if (ret < 0)
1153 return ret;
1154
1155 ret = -ENOENT;
1156 list_for_each_entry(knotif, &filter->notif->notifications, list) {
1157 if (knotif->id == id) {
1158 if (knotif->state == SECCOMP_NOTIFY_SENT)
1159 ret = 0;
1160 goto out;
1161 }
1162 }
1163
1164out:
1165 mutex_unlock(&filter->notify_lock);
1166 return ret;
1167}
1168
1169static long seccomp_notify_ioctl(struct file *file, unsigned int cmd,
1170 unsigned long arg)
1171{
1172 struct seccomp_filter *filter = file->private_data;
1173 void __user *buf = (void __user *)arg;
1174
1175 switch (cmd) {
1176 case SECCOMP_IOCTL_NOTIF_RECV:
1177 return seccomp_notify_recv(filter, buf);
1178 case SECCOMP_IOCTL_NOTIF_SEND:
1179 return seccomp_notify_send(filter, buf);
Olivier Deprez0e641232021-09-23 10:07:05 +02001180 case SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR:
David Brazdil0f672f62019-12-10 10:32:29 +00001181 case SECCOMP_IOCTL_NOTIF_ID_VALID:
1182 return seccomp_notify_id_valid(filter, buf);
1183 default:
1184 return -EINVAL;
1185 }
1186}
1187
1188static __poll_t seccomp_notify_poll(struct file *file,
1189 struct poll_table_struct *poll_tab)
1190{
1191 struct seccomp_filter *filter = file->private_data;
1192 __poll_t ret = 0;
1193 struct seccomp_knotif *cur;
1194
1195 poll_wait(file, &filter->notif->wqh, poll_tab);
1196
1197 if (mutex_lock_interruptible(&filter->notify_lock) < 0)
1198 return EPOLLERR;
1199
1200 list_for_each_entry(cur, &filter->notif->notifications, list) {
1201 if (cur->state == SECCOMP_NOTIFY_INIT)
1202 ret |= EPOLLIN | EPOLLRDNORM;
1203 if (cur->state == SECCOMP_NOTIFY_SENT)
1204 ret |= EPOLLOUT | EPOLLWRNORM;
1205 if ((ret & EPOLLIN) && (ret & EPOLLOUT))
1206 break;
1207 }
1208
1209 mutex_unlock(&filter->notify_lock);
1210
1211 return ret;
1212}
1213
1214static const struct file_operations seccomp_notify_ops = {
1215 .poll = seccomp_notify_poll,
1216 .release = seccomp_notify_release,
1217 .unlocked_ioctl = seccomp_notify_ioctl,
Olivier Deprez0e641232021-09-23 10:07:05 +02001218 .compat_ioctl = seccomp_notify_ioctl,
David Brazdil0f672f62019-12-10 10:32:29 +00001219};
1220
1221static struct file *init_listener(struct seccomp_filter *filter)
1222{
Olivier Deprez0e641232021-09-23 10:07:05 +02001223 struct file *ret;
David Brazdil0f672f62019-12-10 10:32:29 +00001224
1225 ret = ERR_PTR(-ENOMEM);
1226 filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL);
1227 if (!filter->notif)
1228 goto out;
1229
1230 sema_init(&filter->notif->request, 0);
1231 filter->notif->next_id = get_random_u64();
1232 INIT_LIST_HEAD(&filter->notif->notifications);
1233 init_waitqueue_head(&filter->notif->wqh);
1234
1235 ret = anon_inode_getfile("seccomp notify", &seccomp_notify_ops,
1236 filter, O_RDWR);
1237 if (IS_ERR(ret))
1238 goto out_notif;
1239
1240 /* The file has a reference to it now */
1241 __get_seccomp_filter(filter);
1242
1243out_notif:
1244 if (IS_ERR(ret))
1245 kfree(filter->notif);
1246out:
1247 return ret;
1248}
1249
Olivier Deprez0e641232021-09-23 10:07:05 +02001250/*
1251 * Does @new_child have a listener while an ancestor also has a listener?
1252 * If so, we'll want to reject this filter.
1253 * This only has to be tested for the current process, even in the TSYNC case,
1254 * because TSYNC installs @child with the same parent on all threads.
1255 * Note that @new_child is not hooked up to its parent at this point yet, so
1256 * we use current->seccomp.filter.
1257 */
1258static bool has_duplicate_listener(struct seccomp_filter *new_child)
1259{
1260 struct seccomp_filter *cur;
1261
1262 /* must be protected against concurrent TSYNC */
1263 lockdep_assert_held(&current->sighand->siglock);
1264
1265 if (!new_child->notif)
1266 return false;
1267 for (cur = current->seccomp.filter; cur; cur = cur->prev) {
1268 if (cur->notif)
1269 return true;
1270 }
1271
1272 return false;
1273}
1274
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001275/**
1276 * seccomp_set_mode_filter: internal function for setting seccomp filter
1277 * @flags: flags to change filter behavior
1278 * @filter: struct sock_fprog containing filter
1279 *
1280 * This function may be called repeatedly to install additional filters.
1281 * Every filter successfully installed will be evaluated (in reverse order)
1282 * for each system call the task makes.
1283 *
1284 * Once current->seccomp.mode is non-zero, it may not be changed.
1285 *
1286 * Returns 0 on success or -EINVAL on failure.
1287 */
1288static long seccomp_set_mode_filter(unsigned int flags,
1289 const char __user *filter)
1290{
1291 const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
1292 struct seccomp_filter *prepared = NULL;
1293 long ret = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001294 int listener = -1;
1295 struct file *listener_f = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001296
1297 /* Validate flags. */
1298 if (flags & ~SECCOMP_FILTER_FLAG_MASK)
1299 return -EINVAL;
1300
David Brazdil0f672f62019-12-10 10:32:29 +00001301 /*
1302 * In the successful case, NEW_LISTENER returns the new listener fd.
1303 * But in the failure case, TSYNC returns the thread that died. If you
1304 * combine these two flags, there's no way to tell whether something
1305 * succeeded or failed. So, let's disallow this combination.
1306 */
1307 if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
1308 (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER))
1309 return -EINVAL;
1310
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001311 /* Prepare the new filter before holding any locks. */
1312 prepared = seccomp_prepare_user_filter(filter);
1313 if (IS_ERR(prepared))
1314 return PTR_ERR(prepared);
1315
David Brazdil0f672f62019-12-10 10:32:29 +00001316 if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
1317 listener = get_unused_fd_flags(O_CLOEXEC);
1318 if (listener < 0) {
1319 ret = listener;
1320 goto out_free;
1321 }
1322
1323 listener_f = init_listener(prepared);
1324 if (IS_ERR(listener_f)) {
1325 put_unused_fd(listener);
1326 ret = PTR_ERR(listener_f);
1327 goto out_free;
1328 }
1329 }
1330
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001331 /*
1332 * Make sure we cannot change seccomp or nnp state via TSYNC
1333 * while another thread is in the middle of calling exec.
1334 */
1335 if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
1336 mutex_lock_killable(&current->signal->cred_guard_mutex))
David Brazdil0f672f62019-12-10 10:32:29 +00001337 goto out_put_fd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001338
1339 spin_lock_irq(&current->sighand->siglock);
1340
1341 if (!seccomp_may_assign_mode(seccomp_mode))
1342 goto out;
1343
Olivier Deprez0e641232021-09-23 10:07:05 +02001344 if (has_duplicate_listener(prepared)) {
1345 ret = -EBUSY;
1346 goto out;
1347 }
1348
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349 ret = seccomp_attach_filter(flags, prepared);
1350 if (ret)
1351 goto out;
1352 /* Do not free the successfully attached filter. */
1353 prepared = NULL;
1354
1355 seccomp_assign_mode(current, seccomp_mode, flags);
1356out:
1357 spin_unlock_irq(&current->sighand->siglock);
1358 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
1359 mutex_unlock(&current->signal->cred_guard_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +00001360out_put_fd:
1361 if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
1362 if (ret) {
1363 listener_f->private_data = NULL;
1364 fput(listener_f);
1365 put_unused_fd(listener);
1366 } else {
1367 fd_install(listener, listener_f);
1368 ret = listener;
1369 }
1370 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001371out_free:
1372 seccomp_filter_free(prepared);
1373 return ret;
1374}
1375#else
1376static inline long seccomp_set_mode_filter(unsigned int flags,
1377 const char __user *filter)
1378{
1379 return -EINVAL;
1380}
1381#endif
1382
1383static long seccomp_get_action_avail(const char __user *uaction)
1384{
1385 u32 action;
1386
1387 if (copy_from_user(&action, uaction, sizeof(action)))
1388 return -EFAULT;
1389
1390 switch (action) {
1391 case SECCOMP_RET_KILL_PROCESS:
1392 case SECCOMP_RET_KILL_THREAD:
1393 case SECCOMP_RET_TRAP:
1394 case SECCOMP_RET_ERRNO:
David Brazdil0f672f62019-12-10 10:32:29 +00001395 case SECCOMP_RET_USER_NOTIF:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001396 case SECCOMP_RET_TRACE:
1397 case SECCOMP_RET_LOG:
1398 case SECCOMP_RET_ALLOW:
1399 break;
1400 default:
1401 return -EOPNOTSUPP;
1402 }
1403
1404 return 0;
1405}
1406
David Brazdil0f672f62019-12-10 10:32:29 +00001407static long seccomp_get_notif_sizes(void __user *usizes)
1408{
1409 struct seccomp_notif_sizes sizes = {
1410 .seccomp_notif = sizeof(struct seccomp_notif),
1411 .seccomp_notif_resp = sizeof(struct seccomp_notif_resp),
1412 .seccomp_data = sizeof(struct seccomp_data),
1413 };
1414
1415 if (copy_to_user(usizes, &sizes, sizeof(sizes)))
1416 return -EFAULT;
1417
1418 return 0;
1419}
1420
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001421/* Common entry point for both prctl and syscall. */
1422static long do_seccomp(unsigned int op, unsigned int flags,
David Brazdil0f672f62019-12-10 10:32:29 +00001423 void __user *uargs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001424{
1425 switch (op) {
1426 case SECCOMP_SET_MODE_STRICT:
1427 if (flags != 0 || uargs != NULL)
1428 return -EINVAL;
1429 return seccomp_set_mode_strict();
1430 case SECCOMP_SET_MODE_FILTER:
1431 return seccomp_set_mode_filter(flags, uargs);
1432 case SECCOMP_GET_ACTION_AVAIL:
1433 if (flags != 0)
1434 return -EINVAL;
1435
1436 return seccomp_get_action_avail(uargs);
David Brazdil0f672f62019-12-10 10:32:29 +00001437 case SECCOMP_GET_NOTIF_SIZES:
1438 if (flags != 0)
1439 return -EINVAL;
1440
1441 return seccomp_get_notif_sizes(uargs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001442 default:
1443 return -EINVAL;
1444 }
1445}
1446
1447SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
David Brazdil0f672f62019-12-10 10:32:29 +00001448 void __user *, uargs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001449{
1450 return do_seccomp(op, flags, uargs);
1451}
1452
1453/**
1454 * prctl_set_seccomp: configures current->seccomp.mode
1455 * @seccomp_mode: requested mode to use
1456 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
1457 *
1458 * Returns 0 on success or -EINVAL on failure.
1459 */
David Brazdil0f672f62019-12-10 10:32:29 +00001460long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001461{
1462 unsigned int op;
David Brazdil0f672f62019-12-10 10:32:29 +00001463 void __user *uargs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001464
1465 switch (seccomp_mode) {
1466 case SECCOMP_MODE_STRICT:
1467 op = SECCOMP_SET_MODE_STRICT;
1468 /*
1469 * Setting strict mode through prctl always ignored filter,
1470 * so make sure it is always NULL here to pass the internal
1471 * check in do_seccomp().
1472 */
1473 uargs = NULL;
1474 break;
1475 case SECCOMP_MODE_FILTER:
1476 op = SECCOMP_SET_MODE_FILTER;
1477 uargs = filter;
1478 break;
1479 default:
1480 return -EINVAL;
1481 }
1482
1483 /* prctl interface doesn't have flags, so they are always zero. */
1484 return do_seccomp(op, 0, uargs);
1485}
1486
1487#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
1488static struct seccomp_filter *get_nth_filter(struct task_struct *task,
1489 unsigned long filter_off)
1490{
1491 struct seccomp_filter *orig, *filter;
1492 unsigned long count;
1493
1494 /*
1495 * Note: this is only correct because the caller should be the (ptrace)
1496 * tracer of the task, otherwise lock_task_sighand is needed.
1497 */
1498 spin_lock_irq(&task->sighand->siglock);
1499
1500 if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
1501 spin_unlock_irq(&task->sighand->siglock);
1502 return ERR_PTR(-EINVAL);
1503 }
1504
1505 orig = task->seccomp.filter;
1506 __get_seccomp_filter(orig);
1507 spin_unlock_irq(&task->sighand->siglock);
1508
1509 count = 0;
1510 for (filter = orig; filter; filter = filter->prev)
1511 count++;
1512
1513 if (filter_off >= count) {
1514 filter = ERR_PTR(-ENOENT);
1515 goto out;
1516 }
1517
1518 count -= filter_off;
1519 for (filter = orig; filter && count > 1; filter = filter->prev)
1520 count--;
1521
1522 if (WARN_ON(count != 1 || !filter)) {
1523 filter = ERR_PTR(-ENOENT);
1524 goto out;
1525 }
1526
1527 __get_seccomp_filter(filter);
1528
1529out:
1530 __put_seccomp_filter(orig);
1531 return filter;
1532}
1533
1534long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
1535 void __user *data)
1536{
1537 struct seccomp_filter *filter;
1538 struct sock_fprog_kern *fprog;
1539 long ret;
1540
1541 if (!capable(CAP_SYS_ADMIN) ||
1542 current->seccomp.mode != SECCOMP_MODE_DISABLED) {
1543 return -EACCES;
1544 }
1545
1546 filter = get_nth_filter(task, filter_off);
1547 if (IS_ERR(filter))
1548 return PTR_ERR(filter);
1549
1550 fprog = filter->prog->orig_prog;
1551 if (!fprog) {
1552 /* This must be a new non-cBPF filter, since we save
1553 * every cBPF filter's orig_prog above when
1554 * CONFIG_CHECKPOINT_RESTORE is enabled.
1555 */
1556 ret = -EMEDIUMTYPE;
1557 goto out;
1558 }
1559
1560 ret = fprog->len;
1561 if (!data)
1562 goto out;
1563
1564 if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
1565 ret = -EFAULT;
1566
1567out:
1568 __put_seccomp_filter(filter);
1569 return ret;
1570}
1571
1572long seccomp_get_metadata(struct task_struct *task,
1573 unsigned long size, void __user *data)
1574{
1575 long ret;
1576 struct seccomp_filter *filter;
1577 struct seccomp_metadata kmd = {};
1578
1579 if (!capable(CAP_SYS_ADMIN) ||
1580 current->seccomp.mode != SECCOMP_MODE_DISABLED) {
1581 return -EACCES;
1582 }
1583
1584 size = min_t(unsigned long, size, sizeof(kmd));
1585
1586 if (size < sizeof(kmd.filter_off))
1587 return -EINVAL;
1588
1589 if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
1590 return -EFAULT;
1591
1592 filter = get_nth_filter(task, kmd.filter_off);
1593 if (IS_ERR(filter))
1594 return PTR_ERR(filter);
1595
1596 if (filter->log)
1597 kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
1598
1599 ret = size;
1600 if (copy_to_user(data, &kmd, size))
1601 ret = -EFAULT;
1602
1603 __put_seccomp_filter(filter);
1604 return ret;
1605}
1606#endif
1607
1608#ifdef CONFIG_SYSCTL
1609
1610/* Human readable action names for friendly sysctl interaction */
1611#define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
1612#define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
1613#define SECCOMP_RET_TRAP_NAME "trap"
1614#define SECCOMP_RET_ERRNO_NAME "errno"
David Brazdil0f672f62019-12-10 10:32:29 +00001615#define SECCOMP_RET_USER_NOTIF_NAME "user_notif"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001616#define SECCOMP_RET_TRACE_NAME "trace"
1617#define SECCOMP_RET_LOG_NAME "log"
1618#define SECCOMP_RET_ALLOW_NAME "allow"
1619
1620static const char seccomp_actions_avail[] =
1621 SECCOMP_RET_KILL_PROCESS_NAME " "
1622 SECCOMP_RET_KILL_THREAD_NAME " "
1623 SECCOMP_RET_TRAP_NAME " "
1624 SECCOMP_RET_ERRNO_NAME " "
David Brazdil0f672f62019-12-10 10:32:29 +00001625 SECCOMP_RET_USER_NOTIF_NAME " "
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001626 SECCOMP_RET_TRACE_NAME " "
1627 SECCOMP_RET_LOG_NAME " "
1628 SECCOMP_RET_ALLOW_NAME;
1629
1630struct seccomp_log_name {
1631 u32 log;
1632 const char *name;
1633};
1634
1635static const struct seccomp_log_name seccomp_log_names[] = {
1636 { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
1637 { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
1638 { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
1639 { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
David Brazdil0f672f62019-12-10 10:32:29 +00001640 { SECCOMP_LOG_USER_NOTIF, SECCOMP_RET_USER_NOTIF_NAME },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001641 { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
1642 { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
1643 { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
1644 { }
1645};
1646
1647static bool seccomp_names_from_actions_logged(char *names, size_t size,
1648 u32 actions_logged,
1649 const char *sep)
1650{
1651 const struct seccomp_log_name *cur;
1652 bool append_sep = false;
1653
1654 for (cur = seccomp_log_names; cur->name && size; cur++) {
1655 ssize_t ret;
1656
1657 if (!(actions_logged & cur->log))
1658 continue;
1659
1660 if (append_sep) {
1661 ret = strscpy(names, sep, size);
1662 if (ret < 0)
1663 return false;
1664
1665 names += ret;
1666 size -= ret;
1667 } else
1668 append_sep = true;
1669
1670 ret = strscpy(names, cur->name, size);
1671 if (ret < 0)
1672 return false;
1673
1674 names += ret;
1675 size -= ret;
1676 }
1677
1678 return true;
1679}
1680
1681static bool seccomp_action_logged_from_name(u32 *action_logged,
1682 const char *name)
1683{
1684 const struct seccomp_log_name *cur;
1685
1686 for (cur = seccomp_log_names; cur->name; cur++) {
1687 if (!strcmp(cur->name, name)) {
1688 *action_logged = cur->log;
1689 return true;
1690 }
1691 }
1692
1693 return false;
1694}
1695
1696static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
1697{
1698 char *name;
1699
1700 *actions_logged = 0;
1701 while ((name = strsep(&names, " ")) && *name) {
1702 u32 action_logged = 0;
1703
1704 if (!seccomp_action_logged_from_name(&action_logged, name))
1705 return false;
1706
1707 *actions_logged |= action_logged;
1708 }
1709
1710 return true;
1711}
1712
1713static int read_actions_logged(struct ctl_table *ro_table, void __user *buffer,
1714 size_t *lenp, loff_t *ppos)
1715{
1716 char names[sizeof(seccomp_actions_avail)];
1717 struct ctl_table table;
1718
1719 memset(names, 0, sizeof(names));
1720
1721 if (!seccomp_names_from_actions_logged(names, sizeof(names),
1722 seccomp_actions_logged, " "))
1723 return -EINVAL;
1724
1725 table = *ro_table;
1726 table.data = names;
1727 table.maxlen = sizeof(names);
1728 return proc_dostring(&table, 0, buffer, lenp, ppos);
1729}
1730
1731static int write_actions_logged(struct ctl_table *ro_table, void __user *buffer,
1732 size_t *lenp, loff_t *ppos, u32 *actions_logged)
1733{
1734 char names[sizeof(seccomp_actions_avail)];
1735 struct ctl_table table;
1736 int ret;
1737
1738 if (!capable(CAP_SYS_ADMIN))
1739 return -EPERM;
1740
1741 memset(names, 0, sizeof(names));
1742
1743 table = *ro_table;
1744 table.data = names;
1745 table.maxlen = sizeof(names);
1746 ret = proc_dostring(&table, 1, buffer, lenp, ppos);
1747 if (ret)
1748 return ret;
1749
1750 if (!seccomp_actions_logged_from_names(actions_logged, table.data))
1751 return -EINVAL;
1752
1753 if (*actions_logged & SECCOMP_LOG_ALLOW)
1754 return -EINVAL;
1755
1756 seccomp_actions_logged = *actions_logged;
1757 return 0;
1758}
1759
1760static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged,
1761 int ret)
1762{
1763 char names[sizeof(seccomp_actions_avail)];
1764 char old_names[sizeof(seccomp_actions_avail)];
1765 const char *new = names;
1766 const char *old = old_names;
1767
1768 if (!audit_enabled)
1769 return;
1770
1771 memset(names, 0, sizeof(names));
1772 memset(old_names, 0, sizeof(old_names));
1773
1774 if (ret)
1775 new = "?";
1776 else if (!actions_logged)
1777 new = "(none)";
1778 else if (!seccomp_names_from_actions_logged(names, sizeof(names),
1779 actions_logged, ","))
1780 new = "?";
1781
1782 if (!old_actions_logged)
1783 old = "(none)";
1784 else if (!seccomp_names_from_actions_logged(old_names,
1785 sizeof(old_names),
1786 old_actions_logged, ","))
1787 old = "?";
1788
1789 return audit_seccomp_actions_logged(new, old, !ret);
1790}
1791
1792static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
1793 void __user *buffer, size_t *lenp,
1794 loff_t *ppos)
1795{
1796 int ret;
1797
1798 if (write) {
1799 u32 actions_logged = 0;
1800 u32 old_actions_logged = seccomp_actions_logged;
1801
1802 ret = write_actions_logged(ro_table, buffer, lenp, ppos,
1803 &actions_logged);
1804 audit_actions_logged(actions_logged, old_actions_logged, ret);
1805 } else
1806 ret = read_actions_logged(ro_table, buffer, lenp, ppos);
1807
1808 return ret;
1809}
1810
1811static struct ctl_path seccomp_sysctl_path[] = {
1812 { .procname = "kernel", },
1813 { .procname = "seccomp", },
1814 { }
1815};
1816
1817static struct ctl_table seccomp_sysctl_table[] = {
1818 {
1819 .procname = "actions_avail",
1820 .data = (void *) &seccomp_actions_avail,
1821 .maxlen = sizeof(seccomp_actions_avail),
1822 .mode = 0444,
1823 .proc_handler = proc_dostring,
1824 },
1825 {
1826 .procname = "actions_logged",
1827 .mode = 0644,
1828 .proc_handler = seccomp_actions_logged_handler,
1829 },
1830 { }
1831};
1832
1833static int __init seccomp_sysctl_init(void)
1834{
1835 struct ctl_table_header *hdr;
1836
1837 hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
1838 if (!hdr)
1839 pr_warn("seccomp: sysctl registration failed\n");
1840 else
1841 kmemleak_not_leak(hdr);
1842
1843 return 0;
1844}
1845
1846device_initcall(seccomp_sysctl_init)
1847
1848#endif /* CONFIG_SYSCTL */