blob: 305f0eca163ed34ced41456514a6e0e80c522466 [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 */
Olivier Deprez157378f2022-04-04 15:47:50 +020016#define pr_fmt(fmt) "seccomp: " fmt
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000017
18#include <linux/refcount.h>
19#include <linux/audit.h>
20#include <linux/compat.h>
21#include <linux/coredump.h>
22#include <linux/kmemleak.h>
23#include <linux/nospec.h>
24#include <linux/prctl.h>
25#include <linux/sched.h>
26#include <linux/sched/task_stack.h>
27#include <linux/seccomp.h>
28#include <linux/slab.h>
29#include <linux/syscalls.h>
30#include <linux/sysctl.h>
31
32#ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
33#include <asm/syscall.h>
34#endif
35
36#ifdef CONFIG_SECCOMP_FILTER
David Brazdil0f672f62019-12-10 10:32:29 +000037#include <linux/file.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038#include <linux/filter.h>
39#include <linux/pid.h>
40#include <linux/ptrace.h>
Olivier Deprez0e641232021-09-23 10:07:05 +020041#include <linux/capability.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042#include <linux/tracehook.h>
43#include <linux/uaccess.h>
David Brazdil0f672f62019-12-10 10:32:29 +000044#include <linux/anon_inodes.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020045#include <linux/lockdep.h>
David Brazdil0f672f62019-12-10 10:32:29 +000046
Olivier Deprez0e641232021-09-23 10:07:05 +020047/*
48 * When SECCOMP_IOCTL_NOTIF_ID_VALID was first introduced, it had the
49 * wrong direction flag in the ioctl number. This is the broken one,
50 * which the kernel needs to keep supporting until all userspaces stop
51 * using the wrong command number.
52 */
53#define SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR SECCOMP_IOR(2, __u64)
54
David Brazdil0f672f62019-12-10 10:32:29 +000055enum notify_state {
56 SECCOMP_NOTIFY_INIT,
57 SECCOMP_NOTIFY_SENT,
58 SECCOMP_NOTIFY_REPLIED,
59};
60
61struct seccomp_knotif {
62 /* The struct pid of the task whose filter triggered the notification */
63 struct task_struct *task;
64
65 /* The "cookie" for this request; this is unique for this filter. */
66 u64 id;
67
68 /*
69 * The seccomp data. This pointer is valid the entire time this
70 * notification is active, since it comes from __seccomp_filter which
71 * eclipses the entire lifecycle here.
72 */
73 const struct seccomp_data *data;
74
75 /*
76 * Notification states. When SECCOMP_RET_USER_NOTIF is returned, a
77 * struct seccomp_knotif is created and starts out in INIT. Once the
78 * handler reads the notification off of an FD, it transitions to SENT.
79 * If a signal is received the state transitions back to INIT and
80 * another message is sent. When the userspace handler replies, state
81 * transitions to REPLIED.
82 */
83 enum notify_state state;
84
85 /* The return values, only valid when in SECCOMP_NOTIFY_REPLIED */
86 int error;
87 long val;
Olivier Deprez157378f2022-04-04 15:47:50 +020088 u32 flags;
David Brazdil0f672f62019-12-10 10:32:29 +000089
Olivier Deprez157378f2022-04-04 15:47:50 +020090 /*
91 * Signals when this has changed states, such as the listener
92 * dying, a new seccomp addfd message, or changing to REPLIED
93 */
David Brazdil0f672f62019-12-10 10:32:29 +000094 struct completion ready;
95
96 struct list_head list;
Olivier Deprez157378f2022-04-04 15:47:50 +020097
98 /* outstanding addfd requests */
99 struct list_head addfd;
100};
101
102/**
103 * struct seccomp_kaddfd - container for seccomp_addfd ioctl messages
104 *
105 * @file: A reference to the file to install in the other task
106 * @fd: The fd number to install it at. If the fd number is -1, it means the
107 * installing process should allocate the fd as normal.
108 * @flags: The flags for the new file descriptor. At the moment, only O_CLOEXEC
109 * is allowed.
110 * @ret: The return value of the installing process. It is set to the fd num
111 * upon success (>= 0).
112 * @completion: Indicates that the installing process has completed fd
113 * installation, or gone away (either due to successful
114 * reply, or signal)
115 *
116 */
117struct seccomp_kaddfd {
118 struct file *file;
119 int fd;
120 unsigned int flags;
121
122 /* To only be set on reply */
123 int ret;
124 struct completion completion;
125 struct list_head list;
David Brazdil0f672f62019-12-10 10:32:29 +0000126};
127
128/**
129 * struct notification - container for seccomp userspace notifications. Since
130 * most seccomp filters will not have notification listeners attached and this
131 * structure is fairly large, we store the notification-specific stuff in a
132 * separate structure.
133 *
134 * @request: A semaphore that users of this notification can wait on for
135 * changes. Actual reads and writes are still controlled with
136 * filter->notify_lock.
137 * @next_id: The id of the next request.
138 * @notifications: A list of struct seccomp_knotif elements.
David Brazdil0f672f62019-12-10 10:32:29 +0000139 */
140struct notification {
141 struct semaphore request;
142 u64 next_id;
143 struct list_head notifications;
David Brazdil0f672f62019-12-10 10:32:29 +0000144};
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145
146/**
147 * struct seccomp_filter - container for seccomp BPF programs
148 *
Olivier Deprez157378f2022-04-04 15:47:50 +0200149 * @refs: Reference count to manage the object lifetime.
150 * A filter's reference count is incremented for each directly
151 * attached task, once for the dependent filter, and if
152 * requested for the user notifier. When @refs reaches zero,
153 * the filter can be freed.
154 * @users: A filter's @users count is incremented for each directly
155 * attached task (filter installation, fork(), thread_sync),
156 * and once for the dependent filter (tracked in filter->prev).
157 * When it reaches zero it indicates that no direct or indirect
158 * users of that filter exist. No new tasks can get associated with
159 * this filter after reaching 0. The @users count is always smaller
160 * or equal to @refs. Hence, reaching 0 for @users does not mean
161 * the filter can be freed.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162 * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
163 * @prev: points to a previously installed, or inherited, filter
164 * @prog: the BPF program to evaluate
David Brazdil0f672f62019-12-10 10:32:29 +0000165 * @notif: the struct that holds all notification related information
166 * @notify_lock: A lock for all notification-related accesses.
Olivier Deprez157378f2022-04-04 15:47:50 +0200167 * @wqh: A wait queue for poll if a notifier is in use.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168 *
169 * seccomp_filter objects are organized in a tree linked via the @prev
170 * pointer. For any task, it appears to be a singly-linked list starting
171 * with current->seccomp.filter, the most recently attached or inherited filter.
172 * However, multiple filters may share a @prev node, by way of fork(), which
173 * results in a unidirectional tree existing in memory. This is similar to
174 * how namespaces work.
175 *
176 * seccomp_filter objects should never be modified after being attached
Olivier Deprez157378f2022-04-04 15:47:50 +0200177 * to a task_struct (other than @refs).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 */
179struct seccomp_filter {
Olivier Deprez157378f2022-04-04 15:47:50 +0200180 refcount_t refs;
181 refcount_t users;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000182 bool log;
183 struct seccomp_filter *prev;
184 struct bpf_prog *prog;
David Brazdil0f672f62019-12-10 10:32:29 +0000185 struct notification *notif;
186 struct mutex notify_lock;
Olivier Deprez157378f2022-04-04 15:47:50 +0200187 wait_queue_head_t wqh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000188};
189
190/* Limit any path through the tree to 256KB worth of instructions. */
191#define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
192
193/*
194 * Endianness is explicitly ignored and left for BPF program authors to manage
195 * as per the specific architecture.
196 */
197static void populate_seccomp_data(struct seccomp_data *sd)
198{
Olivier Deprez157378f2022-04-04 15:47:50 +0200199 /*
200 * Instead of using current_pt_reg(), we're already doing the work
201 * to safely fetch "current", so just use "task" everywhere below.
202 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203 struct task_struct *task = current;
204 struct pt_regs *regs = task_pt_regs(task);
205 unsigned long args[6];
206
207 sd->nr = syscall_get_nr(task, regs);
David Brazdil0f672f62019-12-10 10:32:29 +0000208 sd->arch = syscall_get_arch(task);
209 syscall_get_arguments(task, regs, args);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210 sd->args[0] = args[0];
211 sd->args[1] = args[1];
212 sd->args[2] = args[2];
213 sd->args[3] = args[3];
214 sd->args[4] = args[4];
215 sd->args[5] = args[5];
216 sd->instruction_pointer = KSTK_EIP(task);
217}
218
219/**
220 * seccomp_check_filter - verify seccomp filter code
221 * @filter: filter to verify
222 * @flen: length of filter
223 *
224 * Takes a previously checked filter (by bpf_check_classic) and
225 * redirects all filter code that loads struct sk_buff data
226 * and related data through seccomp_bpf_load. It also
227 * enforces length and alignment checking of those loads.
228 *
229 * Returns 0 if the rule set is legal or -EINVAL if not.
230 */
231static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
232{
233 int pc;
234 for (pc = 0; pc < flen; pc++) {
235 struct sock_filter *ftest = &filter[pc];
236 u16 code = ftest->code;
237 u32 k = ftest->k;
238
239 switch (code) {
240 case BPF_LD | BPF_W | BPF_ABS:
241 ftest->code = BPF_LDX | BPF_W | BPF_ABS;
242 /* 32-bit aligned and not out of bounds. */
243 if (k >= sizeof(struct seccomp_data) || k & 3)
244 return -EINVAL;
245 continue;
246 case BPF_LD | BPF_W | BPF_LEN:
247 ftest->code = BPF_LD | BPF_IMM;
248 ftest->k = sizeof(struct seccomp_data);
249 continue;
250 case BPF_LDX | BPF_W | BPF_LEN:
251 ftest->code = BPF_LDX | BPF_IMM;
252 ftest->k = sizeof(struct seccomp_data);
253 continue;
254 /* Explicitly include allowed calls. */
255 case BPF_RET | BPF_K:
256 case BPF_RET | BPF_A:
257 case BPF_ALU | BPF_ADD | BPF_K:
258 case BPF_ALU | BPF_ADD | BPF_X:
259 case BPF_ALU | BPF_SUB | BPF_K:
260 case BPF_ALU | BPF_SUB | BPF_X:
261 case BPF_ALU | BPF_MUL | BPF_K:
262 case BPF_ALU | BPF_MUL | BPF_X:
263 case BPF_ALU | BPF_DIV | BPF_K:
264 case BPF_ALU | BPF_DIV | BPF_X:
265 case BPF_ALU | BPF_AND | BPF_K:
266 case BPF_ALU | BPF_AND | BPF_X:
267 case BPF_ALU | BPF_OR | BPF_K:
268 case BPF_ALU | BPF_OR | BPF_X:
269 case BPF_ALU | BPF_XOR | BPF_K:
270 case BPF_ALU | BPF_XOR | BPF_X:
271 case BPF_ALU | BPF_LSH | BPF_K:
272 case BPF_ALU | BPF_LSH | BPF_X:
273 case BPF_ALU | BPF_RSH | BPF_K:
274 case BPF_ALU | BPF_RSH | BPF_X:
275 case BPF_ALU | BPF_NEG:
276 case BPF_LD | BPF_IMM:
277 case BPF_LDX | BPF_IMM:
278 case BPF_MISC | BPF_TAX:
279 case BPF_MISC | BPF_TXA:
280 case BPF_LD | BPF_MEM:
281 case BPF_LDX | BPF_MEM:
282 case BPF_ST:
283 case BPF_STX:
284 case BPF_JMP | BPF_JA:
285 case BPF_JMP | BPF_JEQ | BPF_K:
286 case BPF_JMP | BPF_JEQ | BPF_X:
287 case BPF_JMP | BPF_JGE | BPF_K:
288 case BPF_JMP | BPF_JGE | BPF_X:
289 case BPF_JMP | BPF_JGT | BPF_K:
290 case BPF_JMP | BPF_JGT | BPF_X:
291 case BPF_JMP | BPF_JSET | BPF_K:
292 case BPF_JMP | BPF_JSET | BPF_X:
293 continue;
294 default:
295 return -EINVAL;
296 }
297 }
298 return 0;
299}
300
301/**
302 * seccomp_run_filters - evaluates all seccomp filters against @sd
303 * @sd: optional seccomp data to be passed to filters
304 * @match: stores struct seccomp_filter that resulted in the return value,
305 * unless filter returned SECCOMP_RET_ALLOW, in which case it will
306 * be unchanged.
307 *
308 * Returns valid seccomp BPF response codes.
309 */
310#define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
311static u32 seccomp_run_filters(const struct seccomp_data *sd,
312 struct seccomp_filter **match)
313{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000314 u32 ret = SECCOMP_RET_ALLOW;
315 /* Make sure cross-thread synced filter points somewhere sane. */
316 struct seccomp_filter *f =
317 READ_ONCE(current->seccomp.filter);
318
319 /* Ensure unexpected behavior doesn't result in failing open. */
David Brazdil0f672f62019-12-10 10:32:29 +0000320 if (WARN_ON(f == NULL))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321 return SECCOMP_RET_KILL_PROCESS;
322
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000323 /*
324 * All filters in the list are evaluated and the lowest BPF return
325 * value always takes priority (ignoring the DATA).
326 */
327 for (; f; f = f->prev) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200328 u32 cur_ret = bpf_prog_run_pin_on_cpu(f->prog, sd);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000329
330 if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
331 ret = cur_ret;
332 *match = f;
333 }
334 }
335 return ret;
336}
337#endif /* CONFIG_SECCOMP_FILTER */
338
339static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
340{
341 assert_spin_locked(&current->sighand->siglock);
342
343 if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
344 return false;
345
346 return true;
347}
348
349void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
350
351static inline void seccomp_assign_mode(struct task_struct *task,
352 unsigned long seccomp_mode,
353 unsigned long flags)
354{
355 assert_spin_locked(&task->sighand->siglock);
356
357 task->seccomp.mode = seccomp_mode;
358 /*
359 * Make sure TIF_SECCOMP cannot be set before the mode (and
360 * filter) is set.
361 */
362 smp_mb__before_atomic();
363 /* Assume default seccomp processes want spec flaw mitigation. */
364 if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
365 arch_seccomp_spec_mitigate(task);
366 set_tsk_thread_flag(task, TIF_SECCOMP);
367}
368
369#ifdef CONFIG_SECCOMP_FILTER
370/* Returns 1 if the parent is an ancestor of the child. */
371static int is_ancestor(struct seccomp_filter *parent,
372 struct seccomp_filter *child)
373{
374 /* NULL is the root ancestor. */
375 if (parent == NULL)
376 return 1;
377 for (; child; child = child->prev)
378 if (child == parent)
379 return 1;
380 return 0;
381}
382
383/**
384 * seccomp_can_sync_threads: checks if all threads can be synchronized
385 *
386 * Expects sighand and cred_guard_mutex locks to be held.
387 *
388 * Returns 0 on success, -ve on error, or the pid of a thread which was
David Brazdil0f672f62019-12-10 10:32:29 +0000389 * either not in the correct seccomp mode or did not have an ancestral
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000390 * seccomp filter.
391 */
392static inline pid_t seccomp_can_sync_threads(void)
393{
394 struct task_struct *thread, *caller;
395
396 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
397 assert_spin_locked(&current->sighand->siglock);
398
399 /* Validate all threads being eligible for synchronization. */
400 caller = current;
401 for_each_thread(caller, thread) {
402 pid_t failed;
403
404 /* Skip current, since it is initiating the sync. */
405 if (thread == caller)
406 continue;
407
408 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
409 (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
410 is_ancestor(thread->seccomp.filter,
411 caller->seccomp.filter)))
412 continue;
413
414 /* Return the first thread that cannot be synchronized. */
415 failed = task_pid_vnr(thread);
416 /* If the pid cannot be resolved, then return -ESRCH */
David Brazdil0f672f62019-12-10 10:32:29 +0000417 if (WARN_ON(failed == 0))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000418 failed = -ESRCH;
419 return failed;
420 }
421
422 return 0;
423}
424
Olivier Deprez157378f2022-04-04 15:47:50 +0200425static inline void seccomp_filter_free(struct seccomp_filter *filter)
426{
427 if (filter) {
428 bpf_prog_destroy(filter->prog);
429 kfree(filter);
430 }
431}
432
433static void __seccomp_filter_orphan(struct seccomp_filter *orig)
434{
435 while (orig && refcount_dec_and_test(&orig->users)) {
436 if (waitqueue_active(&orig->wqh))
437 wake_up_poll(&orig->wqh, EPOLLHUP);
438 orig = orig->prev;
439 }
440}
441
442static void __put_seccomp_filter(struct seccomp_filter *orig)
443{
444 /* Clean up single-reference branches iteratively. */
445 while (orig && refcount_dec_and_test(&orig->refs)) {
446 struct seccomp_filter *freeme = orig;
447 orig = orig->prev;
448 seccomp_filter_free(freeme);
449 }
450}
451
452static void __seccomp_filter_release(struct seccomp_filter *orig)
453{
454 /* Notify about any unused filters in the task's former filter tree. */
455 __seccomp_filter_orphan(orig);
456 /* Finally drop all references to the task's former tree. */
457 __put_seccomp_filter(orig);
458}
459
460/**
461 * seccomp_filter_release - Detach the task from its filter tree,
462 * drop its reference count, and notify
463 * about unused filters
464 *
465 * This function should only be called when the task is exiting as
466 * it detaches it from its filter tree. As such, READ_ONCE() and
467 * barriers are not needed here, as would normally be needed.
468 */
469void seccomp_filter_release(struct task_struct *tsk)
470{
471 struct seccomp_filter *orig = tsk->seccomp.filter;
472
473 /* Detach task from its filter tree. */
474 tsk->seccomp.filter = NULL;
475 __seccomp_filter_release(orig);
476}
477
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000478/**
479 * seccomp_sync_threads: sets all threads to use current's filter
480 *
481 * Expects sighand and cred_guard_mutex locks to be held, and for
482 * seccomp_can_sync_threads() to have returned success already
483 * without dropping the locks.
484 *
485 */
486static inline void seccomp_sync_threads(unsigned long flags)
487{
488 struct task_struct *thread, *caller;
489
490 BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
491 assert_spin_locked(&current->sighand->siglock);
492
493 /* Synchronize all threads. */
494 caller = current;
495 for_each_thread(caller, thread) {
496 /* Skip current, since it needs no changes. */
497 if (thread == caller)
498 continue;
499
500 /* Get a task reference for the new leaf node. */
501 get_seccomp_filter(caller);
Olivier Deprez157378f2022-04-04 15:47:50 +0200502
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503 /*
504 * Drop the task reference to the shared ancestor since
505 * current's path will hold a reference. (This also
506 * allows a put before the assignment.)
507 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200508 __seccomp_filter_release(thread->seccomp.filter);
509
510 /* Make our new filter tree visible. */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000511 smp_store_release(&thread->seccomp.filter,
512 caller->seccomp.filter);
Olivier Deprez157378f2022-04-04 15:47:50 +0200513 atomic_set(&thread->seccomp.filter_count,
514 atomic_read(&caller->seccomp.filter_count));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000515
516 /*
517 * Don't let an unprivileged task work around
518 * the no_new_privs restriction by creating
519 * a thread that sets it up, enters seccomp,
520 * then dies.
521 */
522 if (task_no_new_privs(caller))
523 task_set_no_new_privs(thread);
524
525 /*
526 * Opt the other thread into seccomp if needed.
527 * As threads are considered to be trust-realm
528 * equivalent (see ptrace_may_access), it is safe to
529 * allow one thread to transition the other.
530 */
531 if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
532 seccomp_assign_mode(thread, SECCOMP_MODE_FILTER,
533 flags);
534 }
535}
536
537/**
538 * seccomp_prepare_filter: Prepares a seccomp filter for use.
539 * @fprog: BPF program to install
540 *
541 * Returns filter on success or an ERR_PTR on failure.
542 */
543static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
544{
545 struct seccomp_filter *sfilter;
546 int ret;
547 const bool save_orig = IS_ENABLED(CONFIG_CHECKPOINT_RESTORE);
548
549 if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
550 return ERR_PTR(-EINVAL);
551
552 BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
553
554 /*
555 * Installing a seccomp filter requires that the task has
556 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
557 * This avoids scenarios where unprivileged tasks can affect the
558 * behavior of privileged children.
559 */
560 if (!task_no_new_privs(current) &&
Olivier Deprez0e641232021-09-23 10:07:05 +0200561 !ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000562 return ERR_PTR(-EACCES);
563
564 /* Allocate a new seccomp_filter */
565 sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN);
566 if (!sfilter)
567 return ERR_PTR(-ENOMEM);
568
David Brazdil0f672f62019-12-10 10:32:29 +0000569 mutex_init(&sfilter->notify_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570 ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
571 seccomp_check_filter, save_orig);
572 if (ret < 0) {
573 kfree(sfilter);
574 return ERR_PTR(ret);
575 }
576
Olivier Deprez157378f2022-04-04 15:47:50 +0200577 refcount_set(&sfilter->refs, 1);
578 refcount_set(&sfilter->users, 1);
579 init_waitqueue_head(&sfilter->wqh);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580
581 return sfilter;
582}
583
584/**
585 * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
586 * @user_filter: pointer to the user data containing a sock_fprog.
587 *
588 * Returns 0 on success and non-zero otherwise.
589 */
590static struct seccomp_filter *
591seccomp_prepare_user_filter(const char __user *user_filter)
592{
593 struct sock_fprog fprog;
594 struct seccomp_filter *filter = ERR_PTR(-EFAULT);
595
596#ifdef CONFIG_COMPAT
597 if (in_compat_syscall()) {
598 struct compat_sock_fprog fprog32;
599 if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
600 goto out;
601 fprog.len = fprog32.len;
602 fprog.filter = compat_ptr(fprog32.filter);
603 } else /* falls through to the if below. */
604#endif
605 if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
606 goto out;
607 filter = seccomp_prepare_filter(&fprog);
608out:
609 return filter;
610}
611
612/**
613 * seccomp_attach_filter: validate and attach filter
614 * @flags: flags to change filter behavior
615 * @filter: seccomp filter to add to the current process
616 *
617 * Caller must be holding current->sighand->siglock lock.
618 *
David Brazdil0f672f62019-12-10 10:32:29 +0000619 * Returns 0 on success, -ve on error, or
620 * - in TSYNC mode: the pid of a thread which was either not in the correct
621 * seccomp mode or did not have an ancestral seccomp filter
622 * - in NEW_LISTENER mode: the fd of the new listener
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000623 */
624static long seccomp_attach_filter(unsigned int flags,
625 struct seccomp_filter *filter)
626{
627 unsigned long total_insns;
628 struct seccomp_filter *walker;
629
630 assert_spin_locked(&current->sighand->siglock);
631
632 /* Validate resulting filter length. */
633 total_insns = filter->prog->len;
634 for (walker = current->seccomp.filter; walker; walker = walker->prev)
635 total_insns += walker->prog->len + 4; /* 4 instr penalty */
636 if (total_insns > MAX_INSNS_PER_PATH)
637 return -ENOMEM;
638
639 /* If thread sync has been requested, check that it is possible. */
640 if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
641 int ret;
642
643 ret = seccomp_can_sync_threads();
Olivier Deprez157378f2022-04-04 15:47:50 +0200644 if (ret) {
645 if (flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH)
646 return -ESRCH;
647 else
648 return ret;
649 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000650 }
651
652 /* Set log flag, if present. */
653 if (flags & SECCOMP_FILTER_FLAG_LOG)
654 filter->log = true;
655
656 /*
657 * If there is an existing filter, make it the prev and don't drop its
658 * task reference.
659 */
660 filter->prev = current->seccomp.filter;
661 current->seccomp.filter = filter;
Olivier Deprez157378f2022-04-04 15:47:50 +0200662 atomic_inc(&current->seccomp.filter_count);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000663
664 /* Now that the new filter is in place, synchronize to all threads. */
665 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
666 seccomp_sync_threads(flags);
667
668 return 0;
669}
670
671static void __get_seccomp_filter(struct seccomp_filter *filter)
672{
Olivier Deprez157378f2022-04-04 15:47:50 +0200673 refcount_inc(&filter->refs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000674}
675
676/* get_seccomp_filter - increments the reference count of the filter on @tsk */
677void get_seccomp_filter(struct task_struct *tsk)
678{
679 struct seccomp_filter *orig = tsk->seccomp.filter;
680 if (!orig)
681 return;
682 __get_seccomp_filter(orig);
Olivier Deprez157378f2022-04-04 15:47:50 +0200683 refcount_inc(&orig->users);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000684}
685
David Brazdil0f672f62019-12-10 10:32:29 +0000686static void seccomp_init_siginfo(kernel_siginfo_t *info, int syscall, int reason)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000687{
688 clear_siginfo(info);
689 info->si_signo = SIGSYS;
690 info->si_code = SYS_SECCOMP;
691 info->si_call_addr = (void __user *)KSTK_EIP(current);
692 info->si_errno = reason;
David Brazdil0f672f62019-12-10 10:32:29 +0000693 info->si_arch = syscall_get_arch(current);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000694 info->si_syscall = syscall;
695}
696
697/**
698 * seccomp_send_sigsys - signals the task to allow in-process syscall emulation
699 * @syscall: syscall number to send to userland
700 * @reason: filter-supplied reason code to send to userland (via si_errno)
701 *
702 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
703 */
704static void seccomp_send_sigsys(int syscall, int reason)
705{
David Brazdil0f672f62019-12-10 10:32:29 +0000706 struct kernel_siginfo info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000707 seccomp_init_siginfo(&info, syscall, reason);
David Brazdil0f672f62019-12-10 10:32:29 +0000708 force_sig_info(&info);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000709}
710#endif /* CONFIG_SECCOMP_FILTER */
711
712/* For use with seccomp_actions_logged */
713#define SECCOMP_LOG_KILL_PROCESS (1 << 0)
714#define SECCOMP_LOG_KILL_THREAD (1 << 1)
715#define SECCOMP_LOG_TRAP (1 << 2)
716#define SECCOMP_LOG_ERRNO (1 << 3)
717#define SECCOMP_LOG_TRACE (1 << 4)
718#define SECCOMP_LOG_LOG (1 << 5)
719#define SECCOMP_LOG_ALLOW (1 << 6)
David Brazdil0f672f62019-12-10 10:32:29 +0000720#define SECCOMP_LOG_USER_NOTIF (1 << 7)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721
722static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
723 SECCOMP_LOG_KILL_THREAD |
724 SECCOMP_LOG_TRAP |
725 SECCOMP_LOG_ERRNO |
David Brazdil0f672f62019-12-10 10:32:29 +0000726 SECCOMP_LOG_USER_NOTIF |
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727 SECCOMP_LOG_TRACE |
728 SECCOMP_LOG_LOG;
729
730static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
731 bool requested)
732{
733 bool log = false;
734
735 switch (action) {
736 case SECCOMP_RET_ALLOW:
737 break;
738 case SECCOMP_RET_TRAP:
739 log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
740 break;
741 case SECCOMP_RET_ERRNO:
742 log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
743 break;
744 case SECCOMP_RET_TRACE:
745 log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
746 break;
David Brazdil0f672f62019-12-10 10:32:29 +0000747 case SECCOMP_RET_USER_NOTIF:
748 log = requested && seccomp_actions_logged & SECCOMP_LOG_USER_NOTIF;
749 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000750 case SECCOMP_RET_LOG:
751 log = seccomp_actions_logged & SECCOMP_LOG_LOG;
752 break;
753 case SECCOMP_RET_KILL_THREAD:
754 log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
755 break;
756 case SECCOMP_RET_KILL_PROCESS:
757 default:
758 log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
759 }
760
761 /*
762 * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the
763 * FILTER_FLAG_LOG bit was set. The admin has the ability to silence
764 * any action from being logged by removing the action name from the
765 * seccomp_actions_logged sysctl.
766 */
767 if (!log)
768 return;
769
770 audit_seccomp(syscall, signr, action);
771}
772
773/*
774 * Secure computing mode 1 allows only read/write/exit/sigreturn.
775 * To be fully secure this must be combined with rlimit
776 * to limit the stack allocations too.
777 */
778static const int mode1_syscalls[] = {
779 __NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
Olivier Deprez157378f2022-04-04 15:47:50 +0200780 -1, /* negative terminated */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000781};
782
783static void __secure_computing_strict(int this_syscall)
784{
Olivier Deprez157378f2022-04-04 15:47:50 +0200785 const int *allowed_syscalls = mode1_syscalls;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000786#ifdef CONFIG_COMPAT
787 if (in_compat_syscall())
Olivier Deprez157378f2022-04-04 15:47:50 +0200788 allowed_syscalls = get_compat_mode1_syscalls();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000789#endif
790 do {
Olivier Deprez157378f2022-04-04 15:47:50 +0200791 if (*allowed_syscalls == this_syscall)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000792 return;
Olivier Deprez157378f2022-04-04 15:47:50 +0200793 } while (*++allowed_syscalls != -1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000794
795#ifdef SECCOMP_DEBUG
796 dump_stack();
797#endif
798 seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
799 do_exit(SIGKILL);
800}
801
802#ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
803void secure_computing_strict(int this_syscall)
804{
805 int mode = current->seccomp.mode;
806
807 if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
808 unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
809 return;
810
811 if (mode == SECCOMP_MODE_DISABLED)
812 return;
813 else if (mode == SECCOMP_MODE_STRICT)
814 __secure_computing_strict(this_syscall);
815 else
816 BUG();
817}
818#else
819
820#ifdef CONFIG_SECCOMP_FILTER
David Brazdil0f672f62019-12-10 10:32:29 +0000821static u64 seccomp_next_notify_id(struct seccomp_filter *filter)
822{
823 /*
824 * Note: overflow is ok here, the id just needs to be unique per
825 * filter.
826 */
827 lockdep_assert_held(&filter->notify_lock);
828 return filter->notif->next_id++;
829}
830
Olivier Deprez157378f2022-04-04 15:47:50 +0200831static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd)
832{
833 /*
834 * Remove the notification, and reset the list pointers, indicating
835 * that it has been handled.
836 */
837 list_del_init(&addfd->list);
838 addfd->ret = receive_fd_replace(addfd->fd, addfd->file, addfd->flags);
839 complete(&addfd->completion);
840}
841
842static int seccomp_do_user_notification(int this_syscall,
843 struct seccomp_filter *match,
844 const struct seccomp_data *sd)
David Brazdil0f672f62019-12-10 10:32:29 +0000845{
846 int err;
Olivier Deprez157378f2022-04-04 15:47:50 +0200847 u32 flags = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000848 long ret = 0;
849 struct seccomp_knotif n = {};
Olivier Deprez157378f2022-04-04 15:47:50 +0200850 struct seccomp_kaddfd *addfd, *tmp;
David Brazdil0f672f62019-12-10 10:32:29 +0000851
852 mutex_lock(&match->notify_lock);
853 err = -ENOSYS;
854 if (!match->notif)
855 goto out;
856
857 n.task = current;
858 n.state = SECCOMP_NOTIFY_INIT;
859 n.data = sd;
860 n.id = seccomp_next_notify_id(match);
861 init_completion(&n.ready);
862 list_add(&n.list, &match->notif->notifications);
Olivier Deprez157378f2022-04-04 15:47:50 +0200863 INIT_LIST_HEAD(&n.addfd);
David Brazdil0f672f62019-12-10 10:32:29 +0000864
865 up(&match->notif->request);
Olivier Deprez157378f2022-04-04 15:47:50 +0200866 wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
David Brazdil0f672f62019-12-10 10:32:29 +0000867
868 /*
869 * This is where we wait for a reply from userspace.
870 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200871 do {
872 mutex_unlock(&match->notify_lock);
873 err = wait_for_completion_interruptible(&n.ready);
874 mutex_lock(&match->notify_lock);
875 if (err != 0)
876 goto interrupted;
877
878 addfd = list_first_entry_or_null(&n.addfd,
879 struct seccomp_kaddfd, list);
880 /* Check if we were woken up by a addfd message */
881 if (addfd)
882 seccomp_handle_addfd(addfd);
883
884 } while (n.state != SECCOMP_NOTIFY_REPLIED);
885
886 ret = n.val;
887 err = n.error;
888 flags = n.flags;
889
890interrupted:
891 /* If there were any pending addfd calls, clear them out */
892 list_for_each_entry_safe(addfd, tmp, &n.addfd, list) {
893 /* The process went away before we got a chance to handle it */
894 addfd->ret = -ESRCH;
895 list_del_init(&addfd->list);
896 complete(&addfd->completion);
David Brazdil0f672f62019-12-10 10:32:29 +0000897 }
898
899 /*
900 * Note that it's possible the listener died in between the time when
Olivier Deprez157378f2022-04-04 15:47:50 +0200901 * we were notified of a response (or a signal) and when we were able to
David Brazdil0f672f62019-12-10 10:32:29 +0000902 * re-acquire the lock, so only delete from the list if the
903 * notification actually exists.
904 *
905 * Also note that this test is only valid because there's no way to
906 * *reattach* to a notifier right now. If one is added, we'll need to
907 * keep track of the notif itself and make sure they match here.
908 */
909 if (match->notif)
910 list_del(&n.list);
911out:
912 mutex_unlock(&match->notify_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +0200913
914 /* Userspace requests to continue the syscall. */
915 if (flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE)
916 return 0;
917
918 syscall_set_return_value(current, current_pt_regs(),
David Brazdil0f672f62019-12-10 10:32:29 +0000919 err, ret);
Olivier Deprez157378f2022-04-04 15:47:50 +0200920 return -1;
David Brazdil0f672f62019-12-10 10:32:29 +0000921}
922
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000923static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
924 const bool recheck_after_trace)
925{
926 u32 filter_ret, action;
927 struct seccomp_filter *match = NULL;
928 int data;
David Brazdil0f672f62019-12-10 10:32:29 +0000929 struct seccomp_data sd_local;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000930
931 /*
932 * Make sure that any changes to mode from another thread have
933 * been seen after TIF_SECCOMP was seen.
934 */
935 rmb();
936
David Brazdil0f672f62019-12-10 10:32:29 +0000937 if (!sd) {
938 populate_seccomp_data(&sd_local);
939 sd = &sd_local;
940 }
941
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000942 filter_ret = seccomp_run_filters(sd, &match);
943 data = filter_ret & SECCOMP_RET_DATA;
944 action = filter_ret & SECCOMP_RET_ACTION_FULL;
945
946 switch (action) {
947 case SECCOMP_RET_ERRNO:
948 /* Set low-order bits as an errno, capped at MAX_ERRNO. */
949 if (data > MAX_ERRNO)
950 data = MAX_ERRNO;
Olivier Deprez157378f2022-04-04 15:47:50 +0200951 syscall_set_return_value(current, current_pt_regs(),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000952 -data, 0);
953 goto skip;
954
955 case SECCOMP_RET_TRAP:
956 /* Show the handler the original registers. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200957 syscall_rollback(current, current_pt_regs());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000958 /* Let the filter pass back 16 bits of data. */
959 seccomp_send_sigsys(this_syscall, data);
960 goto skip;
961
962 case SECCOMP_RET_TRACE:
963 /* We've been put in this state by the ptracer already. */
964 if (recheck_after_trace)
965 return 0;
966
967 /* ENOSYS these calls if there is no tracer attached. */
968 if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
969 syscall_set_return_value(current,
Olivier Deprez157378f2022-04-04 15:47:50 +0200970 current_pt_regs(),
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000971 -ENOSYS, 0);
972 goto skip;
973 }
974
975 /* Allow the BPF to provide the event message */
976 ptrace_event(PTRACE_EVENT_SECCOMP, data);
977 /*
978 * The delivery of a fatal signal during event
979 * notification may silently skip tracer notification,
980 * which could leave us with a potentially unmodified
981 * syscall that the tracer would have liked to have
982 * changed. Since the process is about to die, we just
983 * force the syscall to be skipped and let the signal
984 * kill the process and correctly handle any tracer exit
985 * notifications.
986 */
987 if (fatal_signal_pending(current))
988 goto skip;
989 /* Check if the tracer forced the syscall to be skipped. */
Olivier Deprez157378f2022-04-04 15:47:50 +0200990 this_syscall = syscall_get_nr(current, current_pt_regs());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000991 if (this_syscall < 0)
992 goto skip;
993
994 /*
995 * Recheck the syscall, since it may have changed. This
996 * intentionally uses a NULL struct seccomp_data to force
997 * a reload of all registers. This does not goto skip since
998 * a skip would have already been reported.
999 */
1000 if (__seccomp_filter(this_syscall, NULL, true))
1001 return -1;
1002
1003 return 0;
1004
David Brazdil0f672f62019-12-10 10:32:29 +00001005 case SECCOMP_RET_USER_NOTIF:
Olivier Deprez157378f2022-04-04 15:47:50 +02001006 if (seccomp_do_user_notification(this_syscall, match, sd))
1007 goto skip;
1008
1009 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001010
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001011 case SECCOMP_RET_LOG:
1012 seccomp_log(this_syscall, 0, action, true);
1013 return 0;
1014
1015 case SECCOMP_RET_ALLOW:
1016 /*
1017 * Note that the "match" filter will always be NULL for
1018 * this action since SECCOMP_RET_ALLOW is the starting
1019 * state in seccomp_run_filters().
1020 */
1021 return 0;
1022
1023 case SECCOMP_RET_KILL_THREAD:
1024 case SECCOMP_RET_KILL_PROCESS:
1025 default:
1026 seccomp_log(this_syscall, SIGSYS, action, true);
1027 /* Dump core only if this is the last remaining thread. */
Olivier Deprez157378f2022-04-04 15:47:50 +02001028 if (action != SECCOMP_RET_KILL_THREAD ||
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001029 get_nr_threads(current) == 1) {
David Brazdil0f672f62019-12-10 10:32:29 +00001030 kernel_siginfo_t info;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001031
1032 /* Show the original registers in the dump. */
Olivier Deprez157378f2022-04-04 15:47:50 +02001033 syscall_rollback(current, current_pt_regs());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001034 /* Trigger a manual coredump since do_exit skips it. */
1035 seccomp_init_siginfo(&info, this_syscall, data);
1036 do_coredump(&info);
1037 }
Olivier Deprez157378f2022-04-04 15:47:50 +02001038 if (action == SECCOMP_RET_KILL_THREAD)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001039 do_exit(SIGSYS);
Olivier Deprez157378f2022-04-04 15:47:50 +02001040 else
1041 do_group_exit(SIGSYS);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001042 }
1043
1044 unreachable();
1045
1046skip:
1047 seccomp_log(this_syscall, 0, action, match ? match->log : false);
1048 return -1;
1049}
1050#else
1051static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
1052 const bool recheck_after_trace)
1053{
1054 BUG();
Olivier Deprez0e641232021-09-23 10:07:05 +02001055
1056 return -1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001057}
1058#endif
1059
1060int __secure_computing(const struct seccomp_data *sd)
1061{
1062 int mode = current->seccomp.mode;
1063 int this_syscall;
1064
1065 if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
1066 unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
1067 return 0;
1068
1069 this_syscall = sd ? sd->nr :
Olivier Deprez157378f2022-04-04 15:47:50 +02001070 syscall_get_nr(current, current_pt_regs());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001071
1072 switch (mode) {
1073 case SECCOMP_MODE_STRICT:
1074 __secure_computing_strict(this_syscall); /* may call do_exit */
1075 return 0;
1076 case SECCOMP_MODE_FILTER:
1077 return __seccomp_filter(this_syscall, sd, false);
1078 default:
1079 BUG();
1080 }
1081}
1082#endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
1083
1084long prctl_get_seccomp(void)
1085{
1086 return current->seccomp.mode;
1087}
1088
1089/**
1090 * seccomp_set_mode_strict: internal function for setting strict seccomp
1091 *
1092 * Once current->seccomp.mode is non-zero, it may not be changed.
1093 *
1094 * Returns 0 on success or -EINVAL on failure.
1095 */
1096static long seccomp_set_mode_strict(void)
1097{
1098 const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
1099 long ret = -EINVAL;
1100
1101 spin_lock_irq(&current->sighand->siglock);
1102
1103 if (!seccomp_may_assign_mode(seccomp_mode))
1104 goto out;
1105
1106#ifdef TIF_NOTSC
1107 disable_TSC();
1108#endif
1109 seccomp_assign_mode(current, seccomp_mode, 0);
1110 ret = 0;
1111
1112out:
1113 spin_unlock_irq(&current->sighand->siglock);
1114
1115 return ret;
1116}
1117
1118#ifdef CONFIG_SECCOMP_FILTER
Olivier Deprez157378f2022-04-04 15:47:50 +02001119static void seccomp_notify_free(struct seccomp_filter *filter)
David Brazdil0f672f62019-12-10 10:32:29 +00001120{
Olivier Deprez157378f2022-04-04 15:47:50 +02001121 kfree(filter->notif);
1122 filter->notif = NULL;
1123}
1124
1125static void seccomp_notify_detach(struct seccomp_filter *filter)
1126{
David Brazdil0f672f62019-12-10 10:32:29 +00001127 struct seccomp_knotif *knotif;
1128
1129 if (!filter)
Olivier Deprez157378f2022-04-04 15:47:50 +02001130 return;
David Brazdil0f672f62019-12-10 10:32:29 +00001131
1132 mutex_lock(&filter->notify_lock);
1133
1134 /*
1135 * If this file is being closed because e.g. the task who owned it
1136 * died, let's wake everyone up who was waiting on us.
1137 */
1138 list_for_each_entry(knotif, &filter->notif->notifications, list) {
1139 if (knotif->state == SECCOMP_NOTIFY_REPLIED)
1140 continue;
1141
1142 knotif->state = SECCOMP_NOTIFY_REPLIED;
1143 knotif->error = -ENOSYS;
1144 knotif->val = 0;
1145
Olivier Deprez157378f2022-04-04 15:47:50 +02001146 /*
1147 * We do not need to wake up any pending addfd messages, as
1148 * the notifier will do that for us, as this just looks
1149 * like a standard reply.
1150 */
David Brazdil0f672f62019-12-10 10:32:29 +00001151 complete(&knotif->ready);
1152 }
1153
Olivier Deprez157378f2022-04-04 15:47:50 +02001154 seccomp_notify_free(filter);
David Brazdil0f672f62019-12-10 10:32:29 +00001155 mutex_unlock(&filter->notify_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001156}
1157
1158static int seccomp_notify_release(struct inode *inode, struct file *file)
1159{
1160 struct seccomp_filter *filter = file->private_data;
1161
1162 seccomp_notify_detach(filter);
David Brazdil0f672f62019-12-10 10:32:29 +00001163 __put_seccomp_filter(filter);
1164 return 0;
1165}
1166
Olivier Deprez157378f2022-04-04 15:47:50 +02001167/* must be called with notif_lock held */
1168static inline struct seccomp_knotif *
1169find_notification(struct seccomp_filter *filter, u64 id)
1170{
1171 struct seccomp_knotif *cur;
1172
1173 lockdep_assert_held(&filter->notify_lock);
1174
1175 list_for_each_entry(cur, &filter->notif->notifications, list) {
1176 if (cur->id == id)
1177 return cur;
1178 }
1179
1180 return NULL;
1181}
1182
1183
David Brazdil0f672f62019-12-10 10:32:29 +00001184static long seccomp_notify_recv(struct seccomp_filter *filter,
1185 void __user *buf)
1186{
1187 struct seccomp_knotif *knotif = NULL, *cur;
1188 struct seccomp_notif unotif;
1189 ssize_t ret;
1190
Olivier Deprez0e641232021-09-23 10:07:05 +02001191 /* Verify that we're not given garbage to keep struct extensible. */
1192 ret = check_zeroed_user(buf, sizeof(unotif));
1193 if (ret < 0)
1194 return ret;
1195 if (!ret)
1196 return -EINVAL;
1197
David Brazdil0f672f62019-12-10 10:32:29 +00001198 memset(&unotif, 0, sizeof(unotif));
1199
1200 ret = down_interruptible(&filter->notif->request);
1201 if (ret < 0)
1202 return ret;
1203
1204 mutex_lock(&filter->notify_lock);
1205 list_for_each_entry(cur, &filter->notif->notifications, list) {
1206 if (cur->state == SECCOMP_NOTIFY_INIT) {
1207 knotif = cur;
1208 break;
1209 }
1210 }
1211
1212 /*
1213 * If we didn't find a notification, it could be that the task was
1214 * interrupted by a fatal signal between the time we were woken and
1215 * when we were able to acquire the rw lock.
1216 */
1217 if (!knotif) {
1218 ret = -ENOENT;
1219 goto out;
1220 }
1221
1222 unotif.id = knotif->id;
1223 unotif.pid = task_pid_vnr(knotif->task);
1224 unotif.data = *(knotif->data);
1225
1226 knotif->state = SECCOMP_NOTIFY_SENT;
Olivier Deprez157378f2022-04-04 15:47:50 +02001227 wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM);
David Brazdil0f672f62019-12-10 10:32:29 +00001228 ret = 0;
1229out:
1230 mutex_unlock(&filter->notify_lock);
1231
1232 if (ret == 0 && copy_to_user(buf, &unotif, sizeof(unotif))) {
1233 ret = -EFAULT;
1234
1235 /*
1236 * Userspace screwed up. To make sure that we keep this
1237 * notification alive, let's reset it back to INIT. It
1238 * may have died when we released the lock, so we need to make
1239 * sure it's still around.
1240 */
David Brazdil0f672f62019-12-10 10:32:29 +00001241 mutex_lock(&filter->notify_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001242 knotif = find_notification(filter, unotif.id);
David Brazdil0f672f62019-12-10 10:32:29 +00001243 if (knotif) {
1244 knotif->state = SECCOMP_NOTIFY_INIT;
1245 up(&filter->notif->request);
1246 }
1247 mutex_unlock(&filter->notify_lock);
1248 }
1249
1250 return ret;
1251}
1252
1253static long seccomp_notify_send(struct seccomp_filter *filter,
1254 void __user *buf)
1255{
1256 struct seccomp_notif_resp resp = {};
Olivier Deprez157378f2022-04-04 15:47:50 +02001257 struct seccomp_knotif *knotif;
David Brazdil0f672f62019-12-10 10:32:29 +00001258 long ret;
1259
1260 if (copy_from_user(&resp, buf, sizeof(resp)))
1261 return -EFAULT;
1262
Olivier Deprez157378f2022-04-04 15:47:50 +02001263 if (resp.flags & ~SECCOMP_USER_NOTIF_FLAG_CONTINUE)
1264 return -EINVAL;
1265
1266 if ((resp.flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE) &&
1267 (resp.error || resp.val))
David Brazdil0f672f62019-12-10 10:32:29 +00001268 return -EINVAL;
1269
1270 ret = mutex_lock_interruptible(&filter->notify_lock);
1271 if (ret < 0)
1272 return ret;
1273
Olivier Deprez157378f2022-04-04 15:47:50 +02001274 knotif = find_notification(filter, resp.id);
David Brazdil0f672f62019-12-10 10:32:29 +00001275 if (!knotif) {
1276 ret = -ENOENT;
1277 goto out;
1278 }
1279
1280 /* Allow exactly one reply. */
1281 if (knotif->state != SECCOMP_NOTIFY_SENT) {
1282 ret = -EINPROGRESS;
1283 goto out;
1284 }
1285
1286 ret = 0;
1287 knotif->state = SECCOMP_NOTIFY_REPLIED;
1288 knotif->error = resp.error;
1289 knotif->val = resp.val;
Olivier Deprez157378f2022-04-04 15:47:50 +02001290 knotif->flags = resp.flags;
David Brazdil0f672f62019-12-10 10:32:29 +00001291 complete(&knotif->ready);
1292out:
1293 mutex_unlock(&filter->notify_lock);
1294 return ret;
1295}
1296
1297static long seccomp_notify_id_valid(struct seccomp_filter *filter,
1298 void __user *buf)
1299{
Olivier Deprez157378f2022-04-04 15:47:50 +02001300 struct seccomp_knotif *knotif;
David Brazdil0f672f62019-12-10 10:32:29 +00001301 u64 id;
1302 long ret;
1303
1304 if (copy_from_user(&id, buf, sizeof(id)))
1305 return -EFAULT;
1306
1307 ret = mutex_lock_interruptible(&filter->notify_lock);
1308 if (ret < 0)
1309 return ret;
1310
Olivier Deprez157378f2022-04-04 15:47:50 +02001311 knotif = find_notification(filter, id);
1312 if (knotif && knotif->state == SECCOMP_NOTIFY_SENT)
1313 ret = 0;
1314 else
1315 ret = -ENOENT;
1316
1317 mutex_unlock(&filter->notify_lock);
1318 return ret;
1319}
1320
1321static long seccomp_notify_addfd(struct seccomp_filter *filter,
1322 struct seccomp_notif_addfd __user *uaddfd,
1323 unsigned int size)
1324{
1325 struct seccomp_notif_addfd addfd;
1326 struct seccomp_knotif *knotif;
1327 struct seccomp_kaddfd kaddfd;
1328 int ret;
1329
1330 BUILD_BUG_ON(sizeof(addfd) < SECCOMP_NOTIFY_ADDFD_SIZE_VER0);
1331 BUILD_BUG_ON(sizeof(addfd) != SECCOMP_NOTIFY_ADDFD_SIZE_LATEST);
1332
1333 if (size < SECCOMP_NOTIFY_ADDFD_SIZE_VER0 || size >= PAGE_SIZE)
1334 return -EINVAL;
1335
1336 ret = copy_struct_from_user(&addfd, sizeof(addfd), uaddfd, size);
1337 if (ret)
1338 return ret;
1339
1340 if (addfd.newfd_flags & ~O_CLOEXEC)
1341 return -EINVAL;
1342
1343 if (addfd.flags & ~SECCOMP_ADDFD_FLAG_SETFD)
1344 return -EINVAL;
1345
1346 if (addfd.newfd && !(addfd.flags & SECCOMP_ADDFD_FLAG_SETFD))
1347 return -EINVAL;
1348
1349 kaddfd.file = fget(addfd.srcfd);
1350 if (!kaddfd.file)
1351 return -EBADF;
1352
1353 kaddfd.flags = addfd.newfd_flags;
1354 kaddfd.fd = (addfd.flags & SECCOMP_ADDFD_FLAG_SETFD) ?
1355 addfd.newfd : -1;
1356 init_completion(&kaddfd.completion);
1357
1358 ret = mutex_lock_interruptible(&filter->notify_lock);
1359 if (ret < 0)
1360 goto out;
1361
1362 knotif = find_notification(filter, addfd.id);
1363 if (!knotif) {
1364 ret = -ENOENT;
1365 goto out_unlock;
David Brazdil0f672f62019-12-10 10:32:29 +00001366 }
1367
Olivier Deprez157378f2022-04-04 15:47:50 +02001368 /*
1369 * We do not want to allow for FD injection to occur before the
1370 * notification has been picked up by a userspace handler, or after
1371 * the notification has been replied to.
1372 */
1373 if (knotif->state != SECCOMP_NOTIFY_SENT) {
1374 ret = -EINPROGRESS;
1375 goto out_unlock;
1376 }
1377
1378 list_add(&kaddfd.list, &knotif->addfd);
1379 complete(&knotif->ready);
David Brazdil0f672f62019-12-10 10:32:29 +00001380 mutex_unlock(&filter->notify_lock);
Olivier Deprez157378f2022-04-04 15:47:50 +02001381
1382 /* Now we wait for it to be processed or be interrupted */
1383 ret = wait_for_completion_interruptible(&kaddfd.completion);
1384 if (ret == 0) {
1385 /*
1386 * We had a successful completion. The other side has already
1387 * removed us from the addfd queue, and
1388 * wait_for_completion_interruptible has a memory barrier upon
1389 * success that lets us read this value directly without
1390 * locking.
1391 */
1392 ret = kaddfd.ret;
1393 goto out;
1394 }
1395
1396 mutex_lock(&filter->notify_lock);
1397 /*
1398 * Even though we were woken up by a signal and not a successful
1399 * completion, a completion may have happened in the mean time.
1400 *
1401 * We need to check again if the addfd request has been handled,
1402 * and if not, we will remove it from the queue.
1403 */
1404 if (list_empty(&kaddfd.list))
1405 ret = kaddfd.ret;
1406 else
1407 list_del(&kaddfd.list);
1408
1409out_unlock:
1410 mutex_unlock(&filter->notify_lock);
1411out:
1412 fput(kaddfd.file);
1413
David Brazdil0f672f62019-12-10 10:32:29 +00001414 return ret;
1415}
1416
1417static long seccomp_notify_ioctl(struct file *file, unsigned int cmd,
1418 unsigned long arg)
1419{
1420 struct seccomp_filter *filter = file->private_data;
1421 void __user *buf = (void __user *)arg;
1422
Olivier Deprez157378f2022-04-04 15:47:50 +02001423 /* Fixed-size ioctls */
David Brazdil0f672f62019-12-10 10:32:29 +00001424 switch (cmd) {
1425 case SECCOMP_IOCTL_NOTIF_RECV:
1426 return seccomp_notify_recv(filter, buf);
1427 case SECCOMP_IOCTL_NOTIF_SEND:
1428 return seccomp_notify_send(filter, buf);
Olivier Deprez0e641232021-09-23 10:07:05 +02001429 case SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR:
David Brazdil0f672f62019-12-10 10:32:29 +00001430 case SECCOMP_IOCTL_NOTIF_ID_VALID:
1431 return seccomp_notify_id_valid(filter, buf);
Olivier Deprez157378f2022-04-04 15:47:50 +02001432 }
1433
1434 /* Extensible Argument ioctls */
1435#define EA_IOCTL(cmd) ((cmd) & ~(IOC_INOUT | IOCSIZE_MASK))
1436 switch (EA_IOCTL(cmd)) {
1437 case EA_IOCTL(SECCOMP_IOCTL_NOTIF_ADDFD):
1438 return seccomp_notify_addfd(filter, buf, _IOC_SIZE(cmd));
David Brazdil0f672f62019-12-10 10:32:29 +00001439 default:
1440 return -EINVAL;
1441 }
1442}
1443
1444static __poll_t seccomp_notify_poll(struct file *file,
1445 struct poll_table_struct *poll_tab)
1446{
1447 struct seccomp_filter *filter = file->private_data;
1448 __poll_t ret = 0;
1449 struct seccomp_knotif *cur;
1450
Olivier Deprez157378f2022-04-04 15:47:50 +02001451 poll_wait(file, &filter->wqh, poll_tab);
David Brazdil0f672f62019-12-10 10:32:29 +00001452
1453 if (mutex_lock_interruptible(&filter->notify_lock) < 0)
1454 return EPOLLERR;
1455
1456 list_for_each_entry(cur, &filter->notif->notifications, list) {
1457 if (cur->state == SECCOMP_NOTIFY_INIT)
1458 ret |= EPOLLIN | EPOLLRDNORM;
1459 if (cur->state == SECCOMP_NOTIFY_SENT)
1460 ret |= EPOLLOUT | EPOLLWRNORM;
1461 if ((ret & EPOLLIN) && (ret & EPOLLOUT))
1462 break;
1463 }
1464
1465 mutex_unlock(&filter->notify_lock);
1466
Olivier Deprez157378f2022-04-04 15:47:50 +02001467 if (refcount_read(&filter->users) == 0)
1468 ret |= EPOLLHUP;
1469
David Brazdil0f672f62019-12-10 10:32:29 +00001470 return ret;
1471}
1472
1473static const struct file_operations seccomp_notify_ops = {
1474 .poll = seccomp_notify_poll,
1475 .release = seccomp_notify_release,
1476 .unlocked_ioctl = seccomp_notify_ioctl,
Olivier Deprez0e641232021-09-23 10:07:05 +02001477 .compat_ioctl = seccomp_notify_ioctl,
David Brazdil0f672f62019-12-10 10:32:29 +00001478};
1479
1480static struct file *init_listener(struct seccomp_filter *filter)
1481{
Olivier Deprez0e641232021-09-23 10:07:05 +02001482 struct file *ret;
David Brazdil0f672f62019-12-10 10:32:29 +00001483
1484 ret = ERR_PTR(-ENOMEM);
1485 filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL);
1486 if (!filter->notif)
1487 goto out;
1488
1489 sema_init(&filter->notif->request, 0);
1490 filter->notif->next_id = get_random_u64();
1491 INIT_LIST_HEAD(&filter->notif->notifications);
David Brazdil0f672f62019-12-10 10:32:29 +00001492
1493 ret = anon_inode_getfile("seccomp notify", &seccomp_notify_ops,
1494 filter, O_RDWR);
1495 if (IS_ERR(ret))
1496 goto out_notif;
1497
1498 /* The file has a reference to it now */
1499 __get_seccomp_filter(filter);
1500
1501out_notif:
1502 if (IS_ERR(ret))
Olivier Deprez157378f2022-04-04 15:47:50 +02001503 seccomp_notify_free(filter);
David Brazdil0f672f62019-12-10 10:32:29 +00001504out:
1505 return ret;
1506}
1507
Olivier Deprez0e641232021-09-23 10:07:05 +02001508/*
1509 * Does @new_child have a listener while an ancestor also has a listener?
1510 * If so, we'll want to reject this filter.
1511 * This only has to be tested for the current process, even in the TSYNC case,
1512 * because TSYNC installs @child with the same parent on all threads.
1513 * Note that @new_child is not hooked up to its parent at this point yet, so
1514 * we use current->seccomp.filter.
1515 */
1516static bool has_duplicate_listener(struct seccomp_filter *new_child)
1517{
1518 struct seccomp_filter *cur;
1519
1520 /* must be protected against concurrent TSYNC */
1521 lockdep_assert_held(&current->sighand->siglock);
1522
1523 if (!new_child->notif)
1524 return false;
1525 for (cur = current->seccomp.filter; cur; cur = cur->prev) {
1526 if (cur->notif)
1527 return true;
1528 }
1529
1530 return false;
1531}
1532
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001533/**
1534 * seccomp_set_mode_filter: internal function for setting seccomp filter
1535 * @flags: flags to change filter behavior
1536 * @filter: struct sock_fprog containing filter
1537 *
1538 * This function may be called repeatedly to install additional filters.
1539 * Every filter successfully installed will be evaluated (in reverse order)
1540 * for each system call the task makes.
1541 *
1542 * Once current->seccomp.mode is non-zero, it may not be changed.
1543 *
1544 * Returns 0 on success or -EINVAL on failure.
1545 */
1546static long seccomp_set_mode_filter(unsigned int flags,
1547 const char __user *filter)
1548{
1549 const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
1550 struct seccomp_filter *prepared = NULL;
1551 long ret = -EINVAL;
David Brazdil0f672f62019-12-10 10:32:29 +00001552 int listener = -1;
1553 struct file *listener_f = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001554
1555 /* Validate flags. */
1556 if (flags & ~SECCOMP_FILTER_FLAG_MASK)
1557 return -EINVAL;
1558
David Brazdil0f672f62019-12-10 10:32:29 +00001559 /*
1560 * In the successful case, NEW_LISTENER returns the new listener fd.
1561 * But in the failure case, TSYNC returns the thread that died. If you
1562 * combine these two flags, there's no way to tell whether something
Olivier Deprez157378f2022-04-04 15:47:50 +02001563 * succeeded or failed. So, let's disallow this combination if the user
1564 * has not explicitly requested no errors from TSYNC.
David Brazdil0f672f62019-12-10 10:32:29 +00001565 */
1566 if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
Olivier Deprez157378f2022-04-04 15:47:50 +02001567 (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) &&
1568 ((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0))
David Brazdil0f672f62019-12-10 10:32:29 +00001569 return -EINVAL;
1570
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001571 /* Prepare the new filter before holding any locks. */
1572 prepared = seccomp_prepare_user_filter(filter);
1573 if (IS_ERR(prepared))
1574 return PTR_ERR(prepared);
1575
David Brazdil0f672f62019-12-10 10:32:29 +00001576 if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
1577 listener = get_unused_fd_flags(O_CLOEXEC);
1578 if (listener < 0) {
1579 ret = listener;
1580 goto out_free;
1581 }
1582
1583 listener_f = init_listener(prepared);
1584 if (IS_ERR(listener_f)) {
1585 put_unused_fd(listener);
1586 ret = PTR_ERR(listener_f);
1587 goto out_free;
1588 }
1589 }
1590
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001591 /*
1592 * Make sure we cannot change seccomp or nnp state via TSYNC
1593 * while another thread is in the middle of calling exec.
1594 */
1595 if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
1596 mutex_lock_killable(&current->signal->cred_guard_mutex))
David Brazdil0f672f62019-12-10 10:32:29 +00001597 goto out_put_fd;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001598
1599 spin_lock_irq(&current->sighand->siglock);
1600
1601 if (!seccomp_may_assign_mode(seccomp_mode))
1602 goto out;
1603
Olivier Deprez0e641232021-09-23 10:07:05 +02001604 if (has_duplicate_listener(prepared)) {
1605 ret = -EBUSY;
1606 goto out;
1607 }
1608
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001609 ret = seccomp_attach_filter(flags, prepared);
1610 if (ret)
1611 goto out;
1612 /* Do not free the successfully attached filter. */
1613 prepared = NULL;
1614
1615 seccomp_assign_mode(current, seccomp_mode, flags);
1616out:
1617 spin_unlock_irq(&current->sighand->siglock);
1618 if (flags & SECCOMP_FILTER_FLAG_TSYNC)
1619 mutex_unlock(&current->signal->cred_guard_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +00001620out_put_fd:
1621 if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
1622 if (ret) {
1623 listener_f->private_data = NULL;
1624 fput(listener_f);
1625 put_unused_fd(listener);
Olivier Deprez157378f2022-04-04 15:47:50 +02001626 seccomp_notify_detach(prepared);
David Brazdil0f672f62019-12-10 10:32:29 +00001627 } else {
1628 fd_install(listener, listener_f);
1629 ret = listener;
1630 }
1631 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001632out_free:
1633 seccomp_filter_free(prepared);
1634 return ret;
1635}
1636#else
1637static inline long seccomp_set_mode_filter(unsigned int flags,
1638 const char __user *filter)
1639{
1640 return -EINVAL;
1641}
1642#endif
1643
1644static long seccomp_get_action_avail(const char __user *uaction)
1645{
1646 u32 action;
1647
1648 if (copy_from_user(&action, uaction, sizeof(action)))
1649 return -EFAULT;
1650
1651 switch (action) {
1652 case SECCOMP_RET_KILL_PROCESS:
1653 case SECCOMP_RET_KILL_THREAD:
1654 case SECCOMP_RET_TRAP:
1655 case SECCOMP_RET_ERRNO:
David Brazdil0f672f62019-12-10 10:32:29 +00001656 case SECCOMP_RET_USER_NOTIF:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001657 case SECCOMP_RET_TRACE:
1658 case SECCOMP_RET_LOG:
1659 case SECCOMP_RET_ALLOW:
1660 break;
1661 default:
1662 return -EOPNOTSUPP;
1663 }
1664
1665 return 0;
1666}
1667
David Brazdil0f672f62019-12-10 10:32:29 +00001668static long seccomp_get_notif_sizes(void __user *usizes)
1669{
1670 struct seccomp_notif_sizes sizes = {
1671 .seccomp_notif = sizeof(struct seccomp_notif),
1672 .seccomp_notif_resp = sizeof(struct seccomp_notif_resp),
1673 .seccomp_data = sizeof(struct seccomp_data),
1674 };
1675
1676 if (copy_to_user(usizes, &sizes, sizeof(sizes)))
1677 return -EFAULT;
1678
1679 return 0;
1680}
1681
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001682/* Common entry point for both prctl and syscall. */
1683static long do_seccomp(unsigned int op, unsigned int flags,
David Brazdil0f672f62019-12-10 10:32:29 +00001684 void __user *uargs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001685{
1686 switch (op) {
1687 case SECCOMP_SET_MODE_STRICT:
1688 if (flags != 0 || uargs != NULL)
1689 return -EINVAL;
1690 return seccomp_set_mode_strict();
1691 case SECCOMP_SET_MODE_FILTER:
1692 return seccomp_set_mode_filter(flags, uargs);
1693 case SECCOMP_GET_ACTION_AVAIL:
1694 if (flags != 0)
1695 return -EINVAL;
1696
1697 return seccomp_get_action_avail(uargs);
David Brazdil0f672f62019-12-10 10:32:29 +00001698 case SECCOMP_GET_NOTIF_SIZES:
1699 if (flags != 0)
1700 return -EINVAL;
1701
1702 return seccomp_get_notif_sizes(uargs);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001703 default:
1704 return -EINVAL;
1705 }
1706}
1707
1708SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
David Brazdil0f672f62019-12-10 10:32:29 +00001709 void __user *, uargs)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001710{
1711 return do_seccomp(op, flags, uargs);
1712}
1713
1714/**
1715 * prctl_set_seccomp: configures current->seccomp.mode
1716 * @seccomp_mode: requested mode to use
1717 * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
1718 *
1719 * Returns 0 on success or -EINVAL on failure.
1720 */
David Brazdil0f672f62019-12-10 10:32:29 +00001721long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001722{
1723 unsigned int op;
David Brazdil0f672f62019-12-10 10:32:29 +00001724 void __user *uargs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001725
1726 switch (seccomp_mode) {
1727 case SECCOMP_MODE_STRICT:
1728 op = SECCOMP_SET_MODE_STRICT;
1729 /*
1730 * Setting strict mode through prctl always ignored filter,
1731 * so make sure it is always NULL here to pass the internal
1732 * check in do_seccomp().
1733 */
1734 uargs = NULL;
1735 break;
1736 case SECCOMP_MODE_FILTER:
1737 op = SECCOMP_SET_MODE_FILTER;
1738 uargs = filter;
1739 break;
1740 default:
1741 return -EINVAL;
1742 }
1743
1744 /* prctl interface doesn't have flags, so they are always zero. */
1745 return do_seccomp(op, 0, uargs);
1746}
1747
1748#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
1749static struct seccomp_filter *get_nth_filter(struct task_struct *task,
1750 unsigned long filter_off)
1751{
1752 struct seccomp_filter *orig, *filter;
1753 unsigned long count;
1754
1755 /*
1756 * Note: this is only correct because the caller should be the (ptrace)
1757 * tracer of the task, otherwise lock_task_sighand is needed.
1758 */
1759 spin_lock_irq(&task->sighand->siglock);
1760
1761 if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
1762 spin_unlock_irq(&task->sighand->siglock);
1763 return ERR_PTR(-EINVAL);
1764 }
1765
1766 orig = task->seccomp.filter;
1767 __get_seccomp_filter(orig);
1768 spin_unlock_irq(&task->sighand->siglock);
1769
1770 count = 0;
1771 for (filter = orig; filter; filter = filter->prev)
1772 count++;
1773
1774 if (filter_off >= count) {
1775 filter = ERR_PTR(-ENOENT);
1776 goto out;
1777 }
1778
1779 count -= filter_off;
1780 for (filter = orig; filter && count > 1; filter = filter->prev)
1781 count--;
1782
1783 if (WARN_ON(count != 1 || !filter)) {
1784 filter = ERR_PTR(-ENOENT);
1785 goto out;
1786 }
1787
1788 __get_seccomp_filter(filter);
1789
1790out:
1791 __put_seccomp_filter(orig);
1792 return filter;
1793}
1794
1795long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
1796 void __user *data)
1797{
1798 struct seccomp_filter *filter;
1799 struct sock_fprog_kern *fprog;
1800 long ret;
1801
1802 if (!capable(CAP_SYS_ADMIN) ||
1803 current->seccomp.mode != SECCOMP_MODE_DISABLED) {
1804 return -EACCES;
1805 }
1806
1807 filter = get_nth_filter(task, filter_off);
1808 if (IS_ERR(filter))
1809 return PTR_ERR(filter);
1810
1811 fprog = filter->prog->orig_prog;
1812 if (!fprog) {
1813 /* This must be a new non-cBPF filter, since we save
1814 * every cBPF filter's orig_prog above when
1815 * CONFIG_CHECKPOINT_RESTORE is enabled.
1816 */
1817 ret = -EMEDIUMTYPE;
1818 goto out;
1819 }
1820
1821 ret = fprog->len;
1822 if (!data)
1823 goto out;
1824
1825 if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
1826 ret = -EFAULT;
1827
1828out:
1829 __put_seccomp_filter(filter);
1830 return ret;
1831}
1832
1833long seccomp_get_metadata(struct task_struct *task,
1834 unsigned long size, void __user *data)
1835{
1836 long ret;
1837 struct seccomp_filter *filter;
1838 struct seccomp_metadata kmd = {};
1839
1840 if (!capable(CAP_SYS_ADMIN) ||
1841 current->seccomp.mode != SECCOMP_MODE_DISABLED) {
1842 return -EACCES;
1843 }
1844
1845 size = min_t(unsigned long, size, sizeof(kmd));
1846
1847 if (size < sizeof(kmd.filter_off))
1848 return -EINVAL;
1849
1850 if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
1851 return -EFAULT;
1852
1853 filter = get_nth_filter(task, kmd.filter_off);
1854 if (IS_ERR(filter))
1855 return PTR_ERR(filter);
1856
1857 if (filter->log)
1858 kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
1859
1860 ret = size;
1861 if (copy_to_user(data, &kmd, size))
1862 ret = -EFAULT;
1863
1864 __put_seccomp_filter(filter);
1865 return ret;
1866}
1867#endif
1868
1869#ifdef CONFIG_SYSCTL
1870
1871/* Human readable action names for friendly sysctl interaction */
1872#define SECCOMP_RET_KILL_PROCESS_NAME "kill_process"
1873#define SECCOMP_RET_KILL_THREAD_NAME "kill_thread"
1874#define SECCOMP_RET_TRAP_NAME "trap"
1875#define SECCOMP_RET_ERRNO_NAME "errno"
David Brazdil0f672f62019-12-10 10:32:29 +00001876#define SECCOMP_RET_USER_NOTIF_NAME "user_notif"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001877#define SECCOMP_RET_TRACE_NAME "trace"
1878#define SECCOMP_RET_LOG_NAME "log"
1879#define SECCOMP_RET_ALLOW_NAME "allow"
1880
1881static const char seccomp_actions_avail[] =
1882 SECCOMP_RET_KILL_PROCESS_NAME " "
1883 SECCOMP_RET_KILL_THREAD_NAME " "
1884 SECCOMP_RET_TRAP_NAME " "
1885 SECCOMP_RET_ERRNO_NAME " "
David Brazdil0f672f62019-12-10 10:32:29 +00001886 SECCOMP_RET_USER_NOTIF_NAME " "
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001887 SECCOMP_RET_TRACE_NAME " "
1888 SECCOMP_RET_LOG_NAME " "
1889 SECCOMP_RET_ALLOW_NAME;
1890
1891struct seccomp_log_name {
1892 u32 log;
1893 const char *name;
1894};
1895
1896static const struct seccomp_log_name seccomp_log_names[] = {
1897 { SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
1898 { SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
1899 { SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
1900 { SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
David Brazdil0f672f62019-12-10 10:32:29 +00001901 { SECCOMP_LOG_USER_NOTIF, SECCOMP_RET_USER_NOTIF_NAME },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001902 { SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
1903 { SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
1904 { SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
1905 { }
1906};
1907
1908static bool seccomp_names_from_actions_logged(char *names, size_t size,
1909 u32 actions_logged,
1910 const char *sep)
1911{
1912 const struct seccomp_log_name *cur;
1913 bool append_sep = false;
1914
1915 for (cur = seccomp_log_names; cur->name && size; cur++) {
1916 ssize_t ret;
1917
1918 if (!(actions_logged & cur->log))
1919 continue;
1920
1921 if (append_sep) {
1922 ret = strscpy(names, sep, size);
1923 if (ret < 0)
1924 return false;
1925
1926 names += ret;
1927 size -= ret;
1928 } else
1929 append_sep = true;
1930
1931 ret = strscpy(names, cur->name, size);
1932 if (ret < 0)
1933 return false;
1934
1935 names += ret;
1936 size -= ret;
1937 }
1938
1939 return true;
1940}
1941
1942static bool seccomp_action_logged_from_name(u32 *action_logged,
1943 const char *name)
1944{
1945 const struct seccomp_log_name *cur;
1946
1947 for (cur = seccomp_log_names; cur->name; cur++) {
1948 if (!strcmp(cur->name, name)) {
1949 *action_logged = cur->log;
1950 return true;
1951 }
1952 }
1953
1954 return false;
1955}
1956
1957static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
1958{
1959 char *name;
1960
1961 *actions_logged = 0;
1962 while ((name = strsep(&names, " ")) && *name) {
1963 u32 action_logged = 0;
1964
1965 if (!seccomp_action_logged_from_name(&action_logged, name))
1966 return false;
1967
1968 *actions_logged |= action_logged;
1969 }
1970
1971 return true;
1972}
1973
1974static int read_actions_logged(struct ctl_table *ro_table, void __user *buffer,
1975 size_t *lenp, loff_t *ppos)
1976{
1977 char names[sizeof(seccomp_actions_avail)];
1978 struct ctl_table table;
1979
1980 memset(names, 0, sizeof(names));
1981
1982 if (!seccomp_names_from_actions_logged(names, sizeof(names),
1983 seccomp_actions_logged, " "))
1984 return -EINVAL;
1985
1986 table = *ro_table;
1987 table.data = names;
1988 table.maxlen = sizeof(names);
1989 return proc_dostring(&table, 0, buffer, lenp, ppos);
1990}
1991
1992static int write_actions_logged(struct ctl_table *ro_table, void __user *buffer,
1993 size_t *lenp, loff_t *ppos, u32 *actions_logged)
1994{
1995 char names[sizeof(seccomp_actions_avail)];
1996 struct ctl_table table;
1997 int ret;
1998
1999 if (!capable(CAP_SYS_ADMIN))
2000 return -EPERM;
2001
2002 memset(names, 0, sizeof(names));
2003
2004 table = *ro_table;
2005 table.data = names;
2006 table.maxlen = sizeof(names);
2007 ret = proc_dostring(&table, 1, buffer, lenp, ppos);
2008 if (ret)
2009 return ret;
2010
2011 if (!seccomp_actions_logged_from_names(actions_logged, table.data))
2012 return -EINVAL;
2013
2014 if (*actions_logged & SECCOMP_LOG_ALLOW)
2015 return -EINVAL;
2016
2017 seccomp_actions_logged = *actions_logged;
2018 return 0;
2019}
2020
2021static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged,
2022 int ret)
2023{
2024 char names[sizeof(seccomp_actions_avail)];
2025 char old_names[sizeof(seccomp_actions_avail)];
2026 const char *new = names;
2027 const char *old = old_names;
2028
2029 if (!audit_enabled)
2030 return;
2031
2032 memset(names, 0, sizeof(names));
2033 memset(old_names, 0, sizeof(old_names));
2034
2035 if (ret)
2036 new = "?";
2037 else if (!actions_logged)
2038 new = "(none)";
2039 else if (!seccomp_names_from_actions_logged(names, sizeof(names),
2040 actions_logged, ","))
2041 new = "?";
2042
2043 if (!old_actions_logged)
2044 old = "(none)";
2045 else if (!seccomp_names_from_actions_logged(old_names,
2046 sizeof(old_names),
2047 old_actions_logged, ","))
2048 old = "?";
2049
2050 return audit_seccomp_actions_logged(new, old, !ret);
2051}
2052
2053static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
Olivier Deprez157378f2022-04-04 15:47:50 +02002054 void *buffer, size_t *lenp,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002055 loff_t *ppos)
2056{
2057 int ret;
2058
2059 if (write) {
2060 u32 actions_logged = 0;
2061 u32 old_actions_logged = seccomp_actions_logged;
2062
2063 ret = write_actions_logged(ro_table, buffer, lenp, ppos,
2064 &actions_logged);
2065 audit_actions_logged(actions_logged, old_actions_logged, ret);
2066 } else
2067 ret = read_actions_logged(ro_table, buffer, lenp, ppos);
2068
2069 return ret;
2070}
2071
2072static struct ctl_path seccomp_sysctl_path[] = {
2073 { .procname = "kernel", },
2074 { .procname = "seccomp", },
2075 { }
2076};
2077
2078static struct ctl_table seccomp_sysctl_table[] = {
2079 {
2080 .procname = "actions_avail",
2081 .data = (void *) &seccomp_actions_avail,
2082 .maxlen = sizeof(seccomp_actions_avail),
2083 .mode = 0444,
2084 .proc_handler = proc_dostring,
2085 },
2086 {
2087 .procname = "actions_logged",
2088 .mode = 0644,
2089 .proc_handler = seccomp_actions_logged_handler,
2090 },
2091 { }
2092};
2093
2094static int __init seccomp_sysctl_init(void)
2095{
2096 struct ctl_table_header *hdr;
2097
2098 hdr = register_sysctl_paths(seccomp_sysctl_path, seccomp_sysctl_table);
2099 if (!hdr)
Olivier Deprez157378f2022-04-04 15:47:50 +02002100 pr_warn("sysctl registration failed\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002101 else
2102 kmemleak_not_leak(hdr);
2103
2104 return 0;
2105}
2106
2107device_initcall(seccomp_sysctl_init)
2108
2109#endif /* CONFIG_SYSCTL */