xref: /linux-6.15/kernel/seccomp.c (revision 8f193313)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/kernel/seccomp.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright 2004-2005  Andrea Arcangeli <[email protected]>
61da177e4SLinus Torvalds  *
7e2cfabdfSWill Drewry  * Copyright (C) 2012 Google, Inc.
8e2cfabdfSWill Drewry  * Will Drewry <[email protected]>
9e2cfabdfSWill Drewry  *
10e2cfabdfSWill Drewry  * This defines a simple but solid secure-computing facility.
11e2cfabdfSWill Drewry  *
12e2cfabdfSWill Drewry  * Mode 1 uses a fixed list of allowed system calls.
13e2cfabdfSWill Drewry  * Mode 2 allows user-defined system call filters in the form
14e2cfabdfSWill Drewry  *        of Berkeley Packet Filters/Linux Socket Filters.
151da177e4SLinus Torvalds  */
16e68f9d49SKees Cook #define pr_fmt(fmt) "seccomp: " fmt
171da177e4SLinus Torvalds 
180b5fa229SKees Cook #include <linux/refcount.h>
1985e7bac3SEric Paris #include <linux/audit.h>
205b101740SRoland McGrath #include <linux/compat.h>
21b25e6716SMike Frysinger #include <linux/coredump.h>
228e5f1ad1STyler Hicks #include <linux/kmemleak.h>
235c307089SKees Cook #include <linux/nospec.h>
245c307089SKees Cook #include <linux/prctl.h>
25e2cfabdfSWill Drewry #include <linux/sched.h>
2668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
27e2cfabdfSWill Drewry #include <linux/seccomp.h>
28c8bee430SKees Cook #include <linux/slab.h>
2948dc92b9SKees Cook #include <linux/syscalls.h>
308e5f1ad1STyler Hicks #include <linux/sysctl.h>
311da177e4SLinus Torvalds 
32b37778beSOleg Nesterov #include <asm/syscall.h>
33b37778beSOleg Nesterov 
34495ac306SKees Cook /* Not exposed in headers: strictly internal use only. */
35495ac306SKees Cook #define SECCOMP_MODE_DEAD	(SECCOMP_MODE_FILTER + 1)
36495ac306SKees Cook 
37e2cfabdfSWill Drewry #ifdef CONFIG_SECCOMP_FILTER
386a21cc50STycho Andersen #include <linux/file.h>
39e2cfabdfSWill Drewry #include <linux/filter.h>
40c2e1f2e3SKees Cook #include <linux/pid.h>
41fb0fadf9SWill Drewry #include <linux/ptrace.h>
42fb14528eSMickaël Salaün #include <linux/capability.h>
43e2cfabdfSWill Drewry #include <linux/uaccess.h>
446a21cc50STycho Andersen #include <linux/anon_inodes.h>
459f87dcf1SSargun Dhillon #include <linux/lockdep.h>
466a21cc50STycho Andersen 
4747e33c05SKees Cook /*
4847e33c05SKees Cook  * When SECCOMP_IOCTL_NOTIF_ID_VALID was first introduced, it had the
4947e33c05SKees Cook  * wrong direction flag in the ioctl number. This is the broken one,
5047e33c05SKees Cook  * which the kernel needs to keep supporting until all userspaces stop
5147e33c05SKees Cook  * using the wrong command number.
5247e33c05SKees Cook  */
5347e33c05SKees Cook #define SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR	SECCOMP_IOR(2, __u64)
5447e33c05SKees Cook 
556a21cc50STycho Andersen enum notify_state {
566a21cc50STycho Andersen 	SECCOMP_NOTIFY_INIT,
576a21cc50STycho Andersen 	SECCOMP_NOTIFY_SENT,
586a21cc50STycho Andersen 	SECCOMP_NOTIFY_REPLIED,
596a21cc50STycho Andersen };
606a21cc50STycho Andersen 
616a21cc50STycho Andersen struct seccomp_knotif {
626a21cc50STycho Andersen 	/* The struct pid of the task whose filter triggered the notification */
636a21cc50STycho Andersen 	struct task_struct *task;
646a21cc50STycho Andersen 
656a21cc50STycho Andersen 	/* The "cookie" for this request; this is unique for this filter. */
666a21cc50STycho Andersen 	u64 id;
676a21cc50STycho Andersen 
686a21cc50STycho Andersen 	/*
696a21cc50STycho Andersen 	 * The seccomp data. This pointer is valid the entire time this
706a21cc50STycho Andersen 	 * notification is active, since it comes from __seccomp_filter which
716a21cc50STycho Andersen 	 * eclipses the entire lifecycle here.
726a21cc50STycho Andersen 	 */
736a21cc50STycho Andersen 	const struct seccomp_data *data;
746a21cc50STycho Andersen 
756a21cc50STycho Andersen 	/*
766a21cc50STycho Andersen 	 * Notification states. When SECCOMP_RET_USER_NOTIF is returned, a
776a21cc50STycho Andersen 	 * struct seccomp_knotif is created and starts out in INIT. Once the
786a21cc50STycho Andersen 	 * handler reads the notification off of an FD, it transitions to SENT.
796a21cc50STycho Andersen 	 * If a signal is received the state transitions back to INIT and
806a21cc50STycho Andersen 	 * another message is sent. When the userspace handler replies, state
816a21cc50STycho Andersen 	 * transitions to REPLIED.
826a21cc50STycho Andersen 	 */
836a21cc50STycho Andersen 	enum notify_state state;
846a21cc50STycho Andersen 
856a21cc50STycho Andersen 	/* The return values, only valid when in SECCOMP_NOTIFY_REPLIED */
866a21cc50STycho Andersen 	int error;
876a21cc50STycho Andersen 	long val;
88fb3c5386SChristian Brauner 	u32 flags;
896a21cc50STycho Andersen 
907cf97b12SSargun Dhillon 	/*
917cf97b12SSargun Dhillon 	 * Signals when this has changed states, such as the listener
927cf97b12SSargun Dhillon 	 * dying, a new seccomp addfd message, or changing to REPLIED
937cf97b12SSargun Dhillon 	 */
946a21cc50STycho Andersen 	struct completion ready;
956a21cc50STycho Andersen 
966a21cc50STycho Andersen 	struct list_head list;
977cf97b12SSargun Dhillon 
987cf97b12SSargun Dhillon 	/* outstanding addfd requests */
997cf97b12SSargun Dhillon 	struct list_head addfd;
1007cf97b12SSargun Dhillon };
1017cf97b12SSargun Dhillon 
1027cf97b12SSargun Dhillon /**
1037cf97b12SSargun Dhillon  * struct seccomp_kaddfd - container for seccomp_addfd ioctl messages
1047cf97b12SSargun Dhillon  *
1057cf97b12SSargun Dhillon  * @file: A reference to the file to install in the other task
1067cf97b12SSargun Dhillon  * @fd: The fd number to install it at. If the fd number is -1, it means the
1077cf97b12SSargun Dhillon  *      installing process should allocate the fd as normal.
1087cf97b12SSargun Dhillon  * @flags: The flags for the new file descriptor. At the moment, only O_CLOEXEC
1097cf97b12SSargun Dhillon  *         is allowed.
1100ae71c77SRodrigo Campos  * @ioctl_flags: The flags used for the seccomp_addfd ioctl.
11146822860SKees Cook  * @setfd: whether or not SECCOMP_ADDFD_FLAG_SETFD was set during notify_addfd
1127cf97b12SSargun Dhillon  * @ret: The return value of the installing process. It is set to the fd num
1137cf97b12SSargun Dhillon  *       upon success (>= 0).
1147cf97b12SSargun Dhillon  * @completion: Indicates that the installing process has completed fd
1157cf97b12SSargun Dhillon  *              installation, or gone away (either due to successful
1167cf97b12SSargun Dhillon  *              reply, or signal)
11746822860SKees Cook  * @list: list_head for chaining seccomp_kaddfd together.
1187cf97b12SSargun Dhillon  *
1197cf97b12SSargun Dhillon  */
1207cf97b12SSargun Dhillon struct seccomp_kaddfd {
1217cf97b12SSargun Dhillon 	struct file *file;
1227cf97b12SSargun Dhillon 	int fd;
1237cf97b12SSargun Dhillon 	unsigned int flags;
1240ae71c77SRodrigo Campos 	__u32 ioctl_flags;
1257cf97b12SSargun Dhillon 
12642eb0d54SChristoph Hellwig 	union {
12742eb0d54SChristoph Hellwig 		bool setfd;
1287cf97b12SSargun Dhillon 		/* To only be set on reply */
1297cf97b12SSargun Dhillon 		int ret;
13042eb0d54SChristoph Hellwig 	};
1317cf97b12SSargun Dhillon 	struct completion completion;
1327cf97b12SSargun Dhillon 	struct list_head list;
1336a21cc50STycho Andersen };
1346a21cc50STycho Andersen 
1356a21cc50STycho Andersen /**
1366a21cc50STycho Andersen  * struct notification - container for seccomp userspace notifications. Since
1376a21cc50STycho Andersen  * most seccomp filters will not have notification listeners attached and this
1386a21cc50STycho Andersen  * structure is fairly large, we store the notification-specific stuff in a
1396a21cc50STycho Andersen  * separate structure.
1406a21cc50STycho Andersen  *
14146822860SKees Cook  * @requests: A semaphore that users of this notification can wait on for
1426a21cc50STycho Andersen  *            changes. Actual reads and writes are still controlled with
1436a21cc50STycho Andersen  *            filter->notify_lock.
14446822860SKees Cook  * @flags: A set of SECCOMP_USER_NOTIF_FD_* flags.
1456a21cc50STycho Andersen  * @next_id: The id of the next request.
1466a21cc50STycho Andersen  * @notifications: A list of struct seccomp_knotif elements.
1476a21cc50STycho Andersen  */
14848a1084aSAndrei Vagin 
1496a21cc50STycho Andersen struct notification {
1504943b66dSAndrei Vagin 	atomic_t requests;
15148a1084aSAndrei Vagin 	u32 flags;
1526a21cc50STycho Andersen 	u64 next_id;
1536a21cc50STycho Andersen 	struct list_head notifications;
1546a21cc50STycho Andersen };
155e2cfabdfSWill Drewry 
156f9d480b6SYiFei Zhu #ifdef SECCOMP_ARCH_NATIVE
157f9d480b6SYiFei Zhu /**
158f9d480b6SYiFei Zhu  * struct action_cache - per-filter cache of seccomp actions per
159f9d480b6SYiFei Zhu  * arch/syscall pair
160f9d480b6SYiFei Zhu  *
161f9d480b6SYiFei Zhu  * @allow_native: A bitmap where each bit represents whether the
162f9d480b6SYiFei Zhu  *		  filter will always allow the syscall, for the
163f9d480b6SYiFei Zhu  *		  native architecture.
164f9d480b6SYiFei Zhu  * @allow_compat: A bitmap where each bit represents whether the
165f9d480b6SYiFei Zhu  *		  filter will always allow the syscall, for the
166f9d480b6SYiFei Zhu  *		  compat architecture.
167f9d480b6SYiFei Zhu  */
168f9d480b6SYiFei Zhu struct action_cache {
169f9d480b6SYiFei Zhu 	DECLARE_BITMAP(allow_native, SECCOMP_ARCH_NATIVE_NR);
170f9d480b6SYiFei Zhu #ifdef SECCOMP_ARCH_COMPAT
171f9d480b6SYiFei Zhu 	DECLARE_BITMAP(allow_compat, SECCOMP_ARCH_COMPAT_NR);
172f9d480b6SYiFei Zhu #endif
173f9d480b6SYiFei Zhu };
174f9d480b6SYiFei Zhu #else
175f9d480b6SYiFei Zhu struct action_cache { };
176f9d480b6SYiFei Zhu 
seccomp_cache_check_allow(const struct seccomp_filter * sfilter,const struct seccomp_data * sd)177f9d480b6SYiFei Zhu static inline bool seccomp_cache_check_allow(const struct seccomp_filter *sfilter,
178f9d480b6SYiFei Zhu 					     const struct seccomp_data *sd)
179f9d480b6SYiFei Zhu {
180f9d480b6SYiFei Zhu 	return false;
181f9d480b6SYiFei Zhu }
1828e01b51aSYiFei Zhu 
seccomp_cache_prepare(struct seccomp_filter * sfilter)1838e01b51aSYiFei Zhu static inline void seccomp_cache_prepare(struct seccomp_filter *sfilter)
1848e01b51aSYiFei Zhu {
1858e01b51aSYiFei Zhu }
186f9d480b6SYiFei Zhu #endif /* SECCOMP_ARCH_NATIVE */
187f9d480b6SYiFei Zhu 
188e2cfabdfSWill Drewry /**
189e2cfabdfSWill Drewry  * struct seccomp_filter - container for seccomp BPF programs
190e2cfabdfSWill Drewry  *
191b707ddeeSChristian Brauner  * @refs: Reference count to manage the object lifetime.
192b707ddeeSChristian Brauner  *	  A filter's reference count is incremented for each directly
193b707ddeeSChristian Brauner  *	  attached task, once for the dependent filter, and if
194b707ddeeSChristian Brauner  *	  requested for the user notifier. When @refs reaches zero,
195b707ddeeSChristian Brauner  *	  the filter can be freed.
19699cdb8b9SChristian Brauner  * @users: A filter's @users count is incremented for each directly
19799cdb8b9SChristian Brauner  *         attached task (filter installation, fork(), thread_sync),
19899cdb8b9SChristian Brauner  *	   and once for the dependent filter (tracked in filter->prev).
19999cdb8b9SChristian Brauner  *	   When it reaches zero it indicates that no direct or indirect
20099cdb8b9SChristian Brauner  *	   users of that filter exist. No new tasks can get associated with
20199cdb8b9SChristian Brauner  *	   this filter after reaching 0. The @users count is always smaller
20299cdb8b9SChristian Brauner  *	   or equal to @refs. Hence, reaching 0 for @users does not mean
20399cdb8b9SChristian Brauner  *	   the filter can be freed.
2048e01b51aSYiFei Zhu  * @cache: cache of arch/syscall mappings to actions
205e66a3997STyler Hicks  * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
206c2aa2dfeSSargun Dhillon  * @wait_killable_recv: Put notifying process in killable state once the
207c2aa2dfeSSargun Dhillon  *			notification is received by the userspace listener.
208e2cfabdfSWill Drewry  * @prev: points to a previously installed, or inherited, filter
209285fdfc5SMickaël Salaün  * @prog: the BPF program to evaluate
2106a21cc50STycho Andersen  * @notif: the struct that holds all notification related information
2116a21cc50STycho Andersen  * @notify_lock: A lock for all notification-related accesses.
21276194c4eSChristian Brauner  * @wqh: A wait queue for poll if a notifier is in use.
213e2cfabdfSWill Drewry  *
214e2cfabdfSWill Drewry  * seccomp_filter objects are organized in a tree linked via the @prev
215e2cfabdfSWill Drewry  * pointer.  For any task, it appears to be a singly-linked list starting
216e2cfabdfSWill Drewry  * with current->seccomp.filter, the most recently attached or inherited filter.
217e2cfabdfSWill Drewry  * However, multiple filters may share a @prev node, by way of fork(), which
218e2cfabdfSWill Drewry  * results in a unidirectional tree existing in memory.  This is similar to
219e2cfabdfSWill Drewry  * how namespaces work.
220e2cfabdfSWill Drewry  *
221e2cfabdfSWill Drewry  * seccomp_filter objects should never be modified after being attached
222b707ddeeSChristian Brauner  * to a task_struct (other than @refs).
223e2cfabdfSWill Drewry  */
224e2cfabdfSWill Drewry struct seccomp_filter {
225b707ddeeSChristian Brauner 	refcount_t refs;
22699cdb8b9SChristian Brauner 	refcount_t users;
227e66a3997STyler Hicks 	bool log;
228c2aa2dfeSSargun Dhillon 	bool wait_killable_recv;
2298e01b51aSYiFei Zhu 	struct action_cache cache;
230e2cfabdfSWill Drewry 	struct seccomp_filter *prev;
2317ae457c1SAlexei Starovoitov 	struct bpf_prog *prog;
2326a21cc50STycho Andersen 	struct notification *notif;
2336a21cc50STycho Andersen 	struct mutex notify_lock;
23476194c4eSChristian Brauner 	wait_queue_head_t wqh;
235e2cfabdfSWill Drewry };
236e2cfabdfSWill Drewry 
237e2cfabdfSWill Drewry /* Limit any path through the tree to 256KB worth of instructions. */
238e2cfabdfSWill Drewry #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
239e2cfabdfSWill Drewry 
240bd4cf0edSAlexei Starovoitov /*
241e2cfabdfSWill Drewry  * Endianness is explicitly ignored and left for BPF program authors to manage
242e2cfabdfSWill Drewry  * as per the specific architecture.
243e2cfabdfSWill Drewry  */
populate_seccomp_data(struct seccomp_data * sd)244bd4cf0edSAlexei Starovoitov static void populate_seccomp_data(struct seccomp_data *sd)
245e2cfabdfSWill Drewry {
2462d9ca267SDenis Efremov 	/*
2472d9ca267SDenis Efremov 	 * Instead of using current_pt_reg(), we're already doing the work
2482d9ca267SDenis Efremov 	 * to safely fetch "current", so just use "task" everywhere below.
2492d9ca267SDenis Efremov 	 */
250bd4cf0edSAlexei Starovoitov 	struct task_struct *task = current;
251bd4cf0edSAlexei Starovoitov 	struct pt_regs *regs = task_pt_regs(task);
2522eac7648SDaniel Borkmann 	unsigned long args[6];
253e2cfabdfSWill Drewry 
254bd4cf0edSAlexei Starovoitov 	sd->nr = syscall_get_nr(task, regs);
25516add411SDmitry V. Levin 	sd->arch = syscall_get_arch(task);
256b35f549dSSteven Rostedt (Red Hat) 	syscall_get_arguments(task, regs, args);
2572eac7648SDaniel Borkmann 	sd->args[0] = args[0];
2582eac7648SDaniel Borkmann 	sd->args[1] = args[1];
2592eac7648SDaniel Borkmann 	sd->args[2] = args[2];
2602eac7648SDaniel Borkmann 	sd->args[3] = args[3];
2612eac7648SDaniel Borkmann 	sd->args[4] = args[4];
2622eac7648SDaniel Borkmann 	sd->args[5] = args[5];
263bd4cf0edSAlexei Starovoitov 	sd->instruction_pointer = KSTK_EIP(task);
264e2cfabdfSWill Drewry }
265e2cfabdfSWill Drewry 
266e2cfabdfSWill Drewry /**
267e2cfabdfSWill Drewry  *	seccomp_check_filter - verify seccomp filter code
268e2cfabdfSWill Drewry  *	@filter: filter to verify
269e2cfabdfSWill Drewry  *	@flen: length of filter
270e2cfabdfSWill Drewry  *
2714df95ff4SAlexei Starovoitov  * Takes a previously checked filter (by bpf_check_classic) and
272e2cfabdfSWill Drewry  * redirects all filter code that loads struct sk_buff data
273e2cfabdfSWill Drewry  * and related data through seccomp_bpf_load.  It also
274e2cfabdfSWill Drewry  * enforces length and alignment checking of those loads.
275e2cfabdfSWill Drewry  *
276e2cfabdfSWill Drewry  * Returns 0 if the rule set is legal or -EINVAL if not.
277e2cfabdfSWill Drewry  */
seccomp_check_filter(struct sock_filter * filter,unsigned int flen)278e2cfabdfSWill Drewry static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
279e2cfabdfSWill Drewry {
280e2cfabdfSWill Drewry 	int pc;
281e2cfabdfSWill Drewry 	for (pc = 0; pc < flen; pc++) {
282e2cfabdfSWill Drewry 		struct sock_filter *ftest = &filter[pc];
283e2cfabdfSWill Drewry 		u16 code = ftest->code;
284e2cfabdfSWill Drewry 		u32 k = ftest->k;
285e2cfabdfSWill Drewry 
286e2cfabdfSWill Drewry 		switch (code) {
28734805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_ABS:
288bd4cf0edSAlexei Starovoitov 			ftest->code = BPF_LDX | BPF_W | BPF_ABS;
289e2cfabdfSWill Drewry 			/* 32-bit aligned and not out of bounds. */
290e2cfabdfSWill Drewry 			if (k >= sizeof(struct seccomp_data) || k & 3)
291e2cfabdfSWill Drewry 				return -EINVAL;
292e2cfabdfSWill Drewry 			continue;
29334805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_LEN:
294bd4cf0edSAlexei Starovoitov 			ftest->code = BPF_LD | BPF_IMM;
295e2cfabdfSWill Drewry 			ftest->k = sizeof(struct seccomp_data);
296e2cfabdfSWill Drewry 			continue;
29734805931SDaniel Borkmann 		case BPF_LDX | BPF_W | BPF_LEN:
298bd4cf0edSAlexei Starovoitov 			ftest->code = BPF_LDX | BPF_IMM;
299e2cfabdfSWill Drewry 			ftest->k = sizeof(struct seccomp_data);
300e2cfabdfSWill Drewry 			continue;
301e2cfabdfSWill Drewry 		/* Explicitly include allowed calls. */
30234805931SDaniel Borkmann 		case BPF_RET | BPF_K:
30334805931SDaniel Borkmann 		case BPF_RET | BPF_A:
30434805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_K:
30534805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_X:
30634805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_K:
30734805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_X:
30834805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_K:
30934805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_X:
31034805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_K:
31134805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_X:
31234805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_K:
31334805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_X:
31434805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_K:
31534805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_X:
31634805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_K:
31734805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_X:
31834805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_K:
31934805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_X:
32034805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_K:
32134805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_X:
32234805931SDaniel Borkmann 		case BPF_ALU | BPF_NEG:
32334805931SDaniel Borkmann 		case BPF_LD | BPF_IMM:
32434805931SDaniel Borkmann 		case BPF_LDX | BPF_IMM:
32534805931SDaniel Borkmann 		case BPF_MISC | BPF_TAX:
32634805931SDaniel Borkmann 		case BPF_MISC | BPF_TXA:
32734805931SDaniel Borkmann 		case BPF_LD | BPF_MEM:
32834805931SDaniel Borkmann 		case BPF_LDX | BPF_MEM:
32934805931SDaniel Borkmann 		case BPF_ST:
33034805931SDaniel Borkmann 		case BPF_STX:
33134805931SDaniel Borkmann 		case BPF_JMP | BPF_JA:
33234805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_K:
33334805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_X:
33434805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_K:
33534805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_X:
33634805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_K:
33734805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_X:
33834805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_K:
33934805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_X:
340e2cfabdfSWill Drewry 			continue;
341e2cfabdfSWill Drewry 		default:
342e2cfabdfSWill Drewry 			return -EINVAL;
343e2cfabdfSWill Drewry 		}
344e2cfabdfSWill Drewry 	}
345e2cfabdfSWill Drewry 	return 0;
346e2cfabdfSWill Drewry }
347e2cfabdfSWill Drewry 
348f9d480b6SYiFei Zhu #ifdef SECCOMP_ARCH_NATIVE
seccomp_cache_check_allow_bitmap(const void * bitmap,size_t bitmap_size,int syscall_nr)349f9d480b6SYiFei Zhu static inline bool seccomp_cache_check_allow_bitmap(const void *bitmap,
350f9d480b6SYiFei Zhu 						    size_t bitmap_size,
351f9d480b6SYiFei Zhu 						    int syscall_nr)
352f9d480b6SYiFei Zhu {
353f9d480b6SYiFei Zhu 	if (unlikely(syscall_nr < 0 || syscall_nr >= bitmap_size))
354f9d480b6SYiFei Zhu 		return false;
355f9d480b6SYiFei Zhu 	syscall_nr = array_index_nospec(syscall_nr, bitmap_size);
356f9d480b6SYiFei Zhu 
357f9d480b6SYiFei Zhu 	return test_bit(syscall_nr, bitmap);
358f9d480b6SYiFei Zhu }
359f9d480b6SYiFei Zhu 
360f9d480b6SYiFei Zhu /**
361f9d480b6SYiFei Zhu  * seccomp_cache_check_allow - lookup seccomp cache
362f9d480b6SYiFei Zhu  * @sfilter: The seccomp filter
363f9d480b6SYiFei Zhu  * @sd: The seccomp data to lookup the cache with
364f9d480b6SYiFei Zhu  *
365f9d480b6SYiFei Zhu  * Returns true if the seccomp_data is cached and allowed.
366f9d480b6SYiFei Zhu  */
seccomp_cache_check_allow(const struct seccomp_filter * sfilter,const struct seccomp_data * sd)367f9d480b6SYiFei Zhu static inline bool seccomp_cache_check_allow(const struct seccomp_filter *sfilter,
368f9d480b6SYiFei Zhu 					     const struct seccomp_data *sd)
369f9d480b6SYiFei Zhu {
370f9d480b6SYiFei Zhu 	int syscall_nr = sd->nr;
371f9d480b6SYiFei Zhu 	const struct action_cache *cache = &sfilter->cache;
372f9d480b6SYiFei Zhu 
373f9d480b6SYiFei Zhu #ifndef SECCOMP_ARCH_COMPAT
374f9d480b6SYiFei Zhu 	/* A native-only architecture doesn't need to check sd->arch. */
375f9d480b6SYiFei Zhu 	return seccomp_cache_check_allow_bitmap(cache->allow_native,
376f9d480b6SYiFei Zhu 						SECCOMP_ARCH_NATIVE_NR,
377f9d480b6SYiFei Zhu 						syscall_nr);
378f9d480b6SYiFei Zhu #else
379f9d480b6SYiFei Zhu 	if (likely(sd->arch == SECCOMP_ARCH_NATIVE))
380f9d480b6SYiFei Zhu 		return seccomp_cache_check_allow_bitmap(cache->allow_native,
381f9d480b6SYiFei Zhu 							SECCOMP_ARCH_NATIVE_NR,
382f9d480b6SYiFei Zhu 							syscall_nr);
383f9d480b6SYiFei Zhu 	if (likely(sd->arch == SECCOMP_ARCH_COMPAT))
384f9d480b6SYiFei Zhu 		return seccomp_cache_check_allow_bitmap(cache->allow_compat,
385f9d480b6SYiFei Zhu 							SECCOMP_ARCH_COMPAT_NR,
386f9d480b6SYiFei Zhu 							syscall_nr);
387f9d480b6SYiFei Zhu #endif /* SECCOMP_ARCH_COMPAT */
388f9d480b6SYiFei Zhu 
389f9d480b6SYiFei Zhu 	WARN_ON_ONCE(true);
390f9d480b6SYiFei Zhu 	return false;
391f9d480b6SYiFei Zhu }
392f9d480b6SYiFei Zhu #endif /* SECCOMP_ARCH_NATIVE */
393f9d480b6SYiFei Zhu 
3940fb0624bSRandy Dunlap #define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
395e2cfabdfSWill Drewry /**
396285fdfc5SMickaël Salaün  * seccomp_run_filters - evaluates all seccomp filters against @sd
397285fdfc5SMickaël Salaün  * @sd: optional seccomp data to be passed to filters
398deb4de8bSKees Cook  * @match: stores struct seccomp_filter that resulted in the return value,
399deb4de8bSKees Cook  *         unless filter returned SECCOMP_RET_ALLOW, in which case it will
400deb4de8bSKees Cook  *         be unchanged.
401e2cfabdfSWill Drewry  *
402e2cfabdfSWill Drewry  * Returns valid seccomp BPF response codes.
403e2cfabdfSWill Drewry  */
seccomp_run_filters(const struct seccomp_data * sd,struct seccomp_filter ** match)404deb4de8bSKees Cook static u32 seccomp_run_filters(const struct seccomp_data *sd,
405deb4de8bSKees Cook 			       struct seccomp_filter **match)
406e2cfabdfSWill Drewry {
407acf3b2c7SWill Drewry 	u32 ret = SECCOMP_RET_ALLOW;
4088225d385SPranith Kumar 	/* Make sure cross-thread synced filter points somewhere sane. */
4098225d385SPranith Kumar 	struct seccomp_filter *f =
410506458efSWill Deacon 			READ_ONCE(current->seccomp.filter);
411acf3b2c7SWill Drewry 
412acf3b2c7SWill Drewry 	/* Ensure unexpected behavior doesn't result in failing open. */
4130d42d73aSIgor Stoppa 	if (WARN_ON(f == NULL))
4144d3b0b05SKees Cook 		return SECCOMP_RET_KILL_PROCESS;
415acf3b2c7SWill Drewry 
416f9d480b6SYiFei Zhu 	if (seccomp_cache_check_allow(f, sd))
417f9d480b6SYiFei Zhu 		return SECCOMP_RET_ALLOW;
418f9d480b6SYiFei Zhu 
419e2cfabdfSWill Drewry 	/*
420e2cfabdfSWill Drewry 	 * All filters in the list are evaluated and the lowest BPF return
421acf3b2c7SWill Drewry 	 * value always takes priority (ignoring the DATA).
422e2cfabdfSWill Drewry 	 */
4233ba2530cSKees Cook 	for (; f; f = f->prev) {
4243d9f773cSDavid Miller 		u32 cur_ret = bpf_prog_run_pin_on_cpu(f->prog, sd);
4258f577cadSAlexei Starovoitov 
4260466bdb9SKees Cook 		if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
427acf3b2c7SWill Drewry 			ret = cur_ret;
428deb4de8bSKees Cook 			*match = f;
429deb4de8bSKees Cook 		}
430e2cfabdfSWill Drewry 	}
431e2cfabdfSWill Drewry 	return ret;
432e2cfabdfSWill Drewry }
4331f41b450SKees Cook #endif /* CONFIG_SECCOMP_FILTER */
434e2cfabdfSWill Drewry 
seccomp_may_assign_mode(unsigned long seccomp_mode)4351f41b450SKees Cook static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
4361f41b450SKees Cook {
43769f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
438dbd95212SKees Cook 
4391f41b450SKees Cook 	if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
4401f41b450SKees Cook 		return false;
4411f41b450SKees Cook 
4421f41b450SKees Cook 	return true;
4431f41b450SKees Cook }
4441f41b450SKees Cook 
arch_seccomp_spec_mitigate(struct task_struct * task)4458bf37d8cSThomas Gleixner void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
4465c307089SKees Cook 
seccomp_assign_mode(struct task_struct * task,unsigned long seccomp_mode,unsigned long flags)4473ba2530cSKees Cook static inline void seccomp_assign_mode(struct task_struct *task,
44800a02d0cSKees Cook 				       unsigned long seccomp_mode,
44900a02d0cSKees Cook 				       unsigned long flags)
4501f41b450SKees Cook {
45169f6a34bSGuenter Roeck 	assert_spin_locked(&task->sighand->siglock);
452dbd95212SKees Cook 
4533ba2530cSKees Cook 	task->seccomp.mode = seccomp_mode;
4543ba2530cSKees Cook 	/*
45523d67a54SGabriel Krisman Bertazi 	 * Make sure SYSCALL_WORK_SECCOMP cannot be set before the mode (and
4563ba2530cSKees Cook 	 * filter) is set.
4573ba2530cSKees Cook 	 */
4583ba2530cSKees Cook 	smp_mb__before_atomic();
45900a02d0cSKees Cook 	/* Assume default seccomp processes want spec flaw mitigation. */
46000a02d0cSKees Cook 	if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
4618bf37d8cSThomas Gleixner 		arch_seccomp_spec_mitigate(task);
46223d67a54SGabriel Krisman Bertazi 	set_task_syscall_work(task, SECCOMP);
4631f41b450SKees Cook }
4641f41b450SKees Cook 
4651f41b450SKees Cook #ifdef CONFIG_SECCOMP_FILTER
466c2e1f2e3SKees Cook /* Returns 1 if the parent is an ancestor of the child. */
is_ancestor(struct seccomp_filter * parent,struct seccomp_filter * child)467c2e1f2e3SKees Cook static int is_ancestor(struct seccomp_filter *parent,
468c2e1f2e3SKees Cook 		       struct seccomp_filter *child)
469c2e1f2e3SKees Cook {
470c2e1f2e3SKees Cook 	/* NULL is the root ancestor. */
471c2e1f2e3SKees Cook 	if (parent == NULL)
472c2e1f2e3SKees Cook 		return 1;
473c2e1f2e3SKees Cook 	for (; child; child = child->prev)
474c2e1f2e3SKees Cook 		if (child == parent)
475c2e1f2e3SKees Cook 			return 1;
476c2e1f2e3SKees Cook 	return 0;
477c2e1f2e3SKees Cook }
478c2e1f2e3SKees Cook 
479c2e1f2e3SKees Cook /**
480c2e1f2e3SKees Cook  * seccomp_can_sync_threads: checks if all threads can be synchronized
481c2e1f2e3SKees Cook  *
482c2e1f2e3SKees Cook  * Expects sighand and cred_guard_mutex locks to be held.
483c2e1f2e3SKees Cook  *
484c2e1f2e3SKees Cook  * Returns 0 on success, -ve on error, or the pid of a thread which was
4856beff00bSTycho Andersen  * either not in the correct seccomp mode or did not have an ancestral
486c2e1f2e3SKees Cook  * seccomp filter.
487c2e1f2e3SKees Cook  */
seccomp_can_sync_threads(void)488c2e1f2e3SKees Cook static inline pid_t seccomp_can_sync_threads(void)
489c2e1f2e3SKees Cook {
490c2e1f2e3SKees Cook 	struct task_struct *thread, *caller;
491c2e1f2e3SKees Cook 
492c2e1f2e3SKees Cook 	BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
49369f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
494c2e1f2e3SKees Cook 
495c2e1f2e3SKees Cook 	/* Validate all threads being eligible for synchronization. */
496c2e1f2e3SKees Cook 	caller = current;
497c2e1f2e3SKees Cook 	for_each_thread(caller, thread) {
498c2e1f2e3SKees Cook 		pid_t failed;
499c2e1f2e3SKees Cook 
500c2e1f2e3SKees Cook 		/* Skip current, since it is initiating the sync. */
501c2e1f2e3SKees Cook 		if (thread == caller)
502c2e1f2e3SKees Cook 			continue;
503bfafe5efSAndrei Vagin 		/* Skip exited threads. */
504bfafe5efSAndrei Vagin 		if (thread->flags & PF_EXITING)
505bfafe5efSAndrei Vagin 			continue;
506c2e1f2e3SKees Cook 
507c2e1f2e3SKees Cook 		if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
508c2e1f2e3SKees Cook 		    (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
509c2e1f2e3SKees Cook 		     is_ancestor(thread->seccomp.filter,
510c2e1f2e3SKees Cook 				 caller->seccomp.filter)))
511c2e1f2e3SKees Cook 			continue;
512c2e1f2e3SKees Cook 
513c2e1f2e3SKees Cook 		/* Return the first thread that cannot be synchronized. */
514c2e1f2e3SKees Cook 		failed = task_pid_vnr(thread);
515c2e1f2e3SKees Cook 		/* If the pid cannot be resolved, then return -ESRCH */
5160d42d73aSIgor Stoppa 		if (WARN_ON(failed == 0))
517c2e1f2e3SKees Cook 			failed = -ESRCH;
518c2e1f2e3SKees Cook 		return failed;
519c2e1f2e3SKees Cook 	}
520c2e1f2e3SKees Cook 
521c2e1f2e3SKees Cook 	return 0;
522c2e1f2e3SKees Cook }
523c2e1f2e3SKees Cook 
seccomp_filter_free(struct seccomp_filter * filter)5243a15fb6eSChristian Brauner static inline void seccomp_filter_free(struct seccomp_filter *filter)
5253a15fb6eSChristian Brauner {
5263a15fb6eSChristian Brauner 	if (filter) {
5273a15fb6eSChristian Brauner 		bpf_prog_destroy(filter->prog);
5283a15fb6eSChristian Brauner 		kfree(filter);
5293a15fb6eSChristian Brauner 	}
5303a15fb6eSChristian Brauner }
5313a15fb6eSChristian Brauner 
__seccomp_filter_orphan(struct seccomp_filter * orig)53299cdb8b9SChristian Brauner static void __seccomp_filter_orphan(struct seccomp_filter *orig)
53399cdb8b9SChristian Brauner {
53499cdb8b9SChristian Brauner 	while (orig && refcount_dec_and_test(&orig->users)) {
53599cdb8b9SChristian Brauner 		if (waitqueue_active(&orig->wqh))
53699cdb8b9SChristian Brauner 			wake_up_poll(&orig->wqh, EPOLLHUP);
53799cdb8b9SChristian Brauner 		orig = orig->prev;
53899cdb8b9SChristian Brauner 	}
53999cdb8b9SChristian Brauner }
54099cdb8b9SChristian Brauner 
__put_seccomp_filter(struct seccomp_filter * orig)5413a15fb6eSChristian Brauner static void __put_seccomp_filter(struct seccomp_filter *orig)
5423a15fb6eSChristian Brauner {
5433a15fb6eSChristian Brauner 	/* Clean up single-reference branches iteratively. */
5443a15fb6eSChristian Brauner 	while (orig && refcount_dec_and_test(&orig->refs)) {
5453a15fb6eSChristian Brauner 		struct seccomp_filter *freeme = orig;
5463a15fb6eSChristian Brauner 		orig = orig->prev;
5473a15fb6eSChristian Brauner 		seccomp_filter_free(freeme);
5483a15fb6eSChristian Brauner 	}
5493a15fb6eSChristian Brauner }
5503a15fb6eSChristian Brauner 
__seccomp_filter_release(struct seccomp_filter * orig)55199cdb8b9SChristian Brauner static void __seccomp_filter_release(struct seccomp_filter *orig)
55299cdb8b9SChristian Brauner {
55399cdb8b9SChristian Brauner 	/* Notify about any unused filters in the task's former filter tree. */
55499cdb8b9SChristian Brauner 	__seccomp_filter_orphan(orig);
55599cdb8b9SChristian Brauner 	/* Finally drop all references to the task's former tree. */
55699cdb8b9SChristian Brauner 	__put_seccomp_filter(orig);
55799cdb8b9SChristian Brauner }
55899cdb8b9SChristian Brauner 
5593a15fb6eSChristian Brauner /**
56099cdb8b9SChristian Brauner  * seccomp_filter_release - Detach the task from its filter tree,
56199cdb8b9SChristian Brauner  *			    drop its reference count, and notify
56299cdb8b9SChristian Brauner  *			    about unused filters
5633a15fb6eSChristian Brauner  *
56446822860SKees Cook  * @tsk: task the filter should be released from.
56546822860SKees Cook  *
5663a15fb6eSChristian Brauner  * This function should only be called when the task is exiting as
567bfafe5efSAndrei Vagin  * it detaches it from its filter tree. PF_EXITING has to be set
568bfafe5efSAndrei Vagin  * for the task.
5693a15fb6eSChristian Brauner  */
seccomp_filter_release(struct task_struct * tsk)5703a15fb6eSChristian Brauner void seccomp_filter_release(struct task_struct *tsk)
5713a15fb6eSChristian Brauner {
572bfafe5efSAndrei Vagin 	struct seccomp_filter *orig;
5733a15fb6eSChristian Brauner 
574bfafe5efSAndrei Vagin 	if (WARN_ON((tsk->flags & PF_EXITING) == 0))
575bfafe5efSAndrei Vagin 		return;
5760d8315ddSYiFei Zhu 
577*8f193313SMateusz Guzik 	if (READ_ONCE(tsk->seccomp.filter) == NULL)
578*8f193313SMateusz Guzik 		return;
579*8f193313SMateusz Guzik 
580bfafe5efSAndrei Vagin 	spin_lock_irq(&tsk->sighand->siglock);
581bfafe5efSAndrei Vagin 	orig = tsk->seccomp.filter;
5823a15fb6eSChristian Brauner 	/* Detach task from its filter tree. */
5833a15fb6eSChristian Brauner 	tsk->seccomp.filter = NULL;
584bfafe5efSAndrei Vagin 	spin_unlock_irq(&tsk->sighand->siglock);
58599cdb8b9SChristian Brauner 	__seccomp_filter_release(orig);
5863a15fb6eSChristian Brauner }
5873a15fb6eSChristian Brauner 
588c2e1f2e3SKees Cook /**
589c2e1f2e3SKees Cook  * seccomp_sync_threads: sets all threads to use current's filter
590c2e1f2e3SKees Cook  *
59146822860SKees Cook  * @flags: SECCOMP_FILTER_FLAG_* flags to set during sync.
59246822860SKees Cook  *
593c2e1f2e3SKees Cook  * Expects sighand and cred_guard_mutex locks to be held, and for
594c2e1f2e3SKees Cook  * seccomp_can_sync_threads() to have returned success already
595c2e1f2e3SKees Cook  * without dropping the locks.
596c2e1f2e3SKees Cook  *
597c2e1f2e3SKees Cook  */
seccomp_sync_threads(unsigned long flags)59800a02d0cSKees Cook static inline void seccomp_sync_threads(unsigned long flags)
599c2e1f2e3SKees Cook {
600c2e1f2e3SKees Cook 	struct task_struct *thread, *caller;
601c2e1f2e3SKees Cook 
602c2e1f2e3SKees Cook 	BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
60369f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
604c2e1f2e3SKees Cook 
605*8f193313SMateusz Guzik 	/*
606*8f193313SMateusz Guzik 	 * Don't touch any of the threads if the process is being killed.
607*8f193313SMateusz Guzik 	 * This allows for a lockless check in seccomp_filter_release.
608*8f193313SMateusz Guzik 	 */
609*8f193313SMateusz Guzik 	if (current->signal->flags & SIGNAL_GROUP_EXIT)
610*8f193313SMateusz Guzik 		return;
611*8f193313SMateusz Guzik 
612c2e1f2e3SKees Cook 	/* Synchronize all threads. */
613c2e1f2e3SKees Cook 	caller = current;
614c2e1f2e3SKees Cook 	for_each_thread(caller, thread) {
615c2e1f2e3SKees Cook 		/* Skip current, since it needs no changes. */
616c2e1f2e3SKees Cook 		if (thread == caller)
617c2e1f2e3SKees Cook 			continue;
618c2e1f2e3SKees Cook 
619bfafe5efSAndrei Vagin 		/*
620bfafe5efSAndrei Vagin 		 * Skip exited threads. seccomp_filter_release could have
621bfafe5efSAndrei Vagin 		 * been already called for this task.
622bfafe5efSAndrei Vagin 		 */
623bfafe5efSAndrei Vagin 		if (thread->flags & PF_EXITING)
624bfafe5efSAndrei Vagin 			continue;
625bfafe5efSAndrei Vagin 
626c2e1f2e3SKees Cook 		/* Get a task reference for the new leaf node. */
627c2e1f2e3SKees Cook 		get_seccomp_filter(caller);
62899cdb8b9SChristian Brauner 
629c2e1f2e3SKees Cook 		/*
630c2e1f2e3SKees Cook 		 * Drop the task reference to the shared ancestor since
631c2e1f2e3SKees Cook 		 * current's path will hold a reference.  (This also
632c2e1f2e3SKees Cook 		 * allows a put before the assignment.)
633c2e1f2e3SKees Cook 		 */
63499cdb8b9SChristian Brauner 		__seccomp_filter_release(thread->seccomp.filter);
63599cdb8b9SChristian Brauner 
63699cdb8b9SChristian Brauner 		/* Make our new filter tree visible. */
637c2e1f2e3SKees Cook 		smp_store_release(&thread->seccomp.filter,
638c2e1f2e3SKees Cook 				  caller->seccomp.filter);
639c818c03bSKees Cook 		atomic_set(&thread->seccomp.filter_count,
640b4d8a58fSHsuan-Chi Kuo 			   atomic_read(&caller->seccomp.filter_count));
641103502a3SJann Horn 
642c2e1f2e3SKees Cook 		/*
643c2e1f2e3SKees Cook 		 * Don't let an unprivileged task work around
644c2e1f2e3SKees Cook 		 * the no_new_privs restriction by creating
645c2e1f2e3SKees Cook 		 * a thread that sets it up, enters seccomp,
646c2e1f2e3SKees Cook 		 * then dies.
647c2e1f2e3SKees Cook 		 */
648c2e1f2e3SKees Cook 		if (task_no_new_privs(caller))
649c2e1f2e3SKees Cook 			task_set_no_new_privs(thread);
650c2e1f2e3SKees Cook 
651103502a3SJann Horn 		/*
652103502a3SJann Horn 		 * Opt the other thread into seccomp if needed.
653103502a3SJann Horn 		 * As threads are considered to be trust-realm
654103502a3SJann Horn 		 * equivalent (see ptrace_may_access), it is safe to
655103502a3SJann Horn 		 * allow one thread to transition the other.
656103502a3SJann Horn 		 */
657103502a3SJann Horn 		if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
65800a02d0cSKees Cook 			seccomp_assign_mode(thread, SECCOMP_MODE_FILTER,
65900a02d0cSKees Cook 					    flags);
660c2e1f2e3SKees Cook 	}
661c2e1f2e3SKees Cook }
662c2e1f2e3SKees Cook 
663e2cfabdfSWill Drewry /**
664c8bee430SKees Cook  * seccomp_prepare_filter: Prepares a seccomp filter for use.
665e2cfabdfSWill Drewry  * @fprog: BPF program to install
666e2cfabdfSWill Drewry  *
667c8bee430SKees Cook  * Returns filter on success or an ERR_PTR on failure.
668e2cfabdfSWill Drewry  */
seccomp_prepare_filter(struct sock_fprog * fprog)669c8bee430SKees Cook static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
670e2cfabdfSWill Drewry {
671ac67eb2cSDaniel Borkmann 	struct seccomp_filter *sfilter;
672ac67eb2cSDaniel Borkmann 	int ret;
6738e01b51aSYiFei Zhu 	const bool save_orig =
6748e01b51aSYiFei Zhu #if defined(CONFIG_CHECKPOINT_RESTORE) || defined(SECCOMP_ARCH_NATIVE)
6758e01b51aSYiFei Zhu 		true;
6768e01b51aSYiFei Zhu #else
6778e01b51aSYiFei Zhu 		false;
6788e01b51aSYiFei Zhu #endif
679e2cfabdfSWill Drewry 
680e2cfabdfSWill Drewry 	if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
681c8bee430SKees Cook 		return ERR_PTR(-EINVAL);
682d9e12f42SNicolas Schichan 
683c8bee430SKees Cook 	BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
684e2cfabdfSWill Drewry 
685e2cfabdfSWill Drewry 	/*
686119ce5c8SFabian Frederick 	 * Installing a seccomp filter requires that the task has
687e2cfabdfSWill Drewry 	 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
688e2cfabdfSWill Drewry 	 * This avoids scenarios where unprivileged tasks can affect the
689e2cfabdfSWill Drewry 	 * behavior of privileged children.
690e2cfabdfSWill Drewry 	 */
6911d4457f9SKees Cook 	if (!task_no_new_privs(current) &&
692fb14528eSMickaël Salaün 			!ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
693c8bee430SKees Cook 		return ERR_PTR(-EACCES);
694e2cfabdfSWill Drewry 
695bd4cf0edSAlexei Starovoitov 	/* Allocate a new seccomp_filter */
696ac67eb2cSDaniel Borkmann 	sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN);
697ac67eb2cSDaniel Borkmann 	if (!sfilter)
698d9e12f42SNicolas Schichan 		return ERR_PTR(-ENOMEM);
699ac67eb2cSDaniel Borkmann 
7006a21cc50STycho Andersen 	mutex_init(&sfilter->notify_lock);
701ac67eb2cSDaniel Borkmann 	ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
702f8e529edSTycho Andersen 					seccomp_check_filter, save_orig);
703ac67eb2cSDaniel Borkmann 	if (ret < 0) {
704ac67eb2cSDaniel Borkmann 		kfree(sfilter);
705ac67eb2cSDaniel Borkmann 		return ERR_PTR(ret);
706d9e12f42SNicolas Schichan 	}
707bd4cf0edSAlexei Starovoitov 
708b707ddeeSChristian Brauner 	refcount_set(&sfilter->refs, 1);
70999cdb8b9SChristian Brauner 	refcount_set(&sfilter->users, 1);
71076194c4eSChristian Brauner 	init_waitqueue_head(&sfilter->wqh);
711e2cfabdfSWill Drewry 
712ac67eb2cSDaniel Borkmann 	return sfilter;
713e2cfabdfSWill Drewry }
714e2cfabdfSWill Drewry 
715e2cfabdfSWill Drewry /**
716c8bee430SKees Cook  * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
717e2cfabdfSWill Drewry  * @user_filter: pointer to the user data containing a sock_fprog.
718e2cfabdfSWill Drewry  *
719e2cfabdfSWill Drewry  * Returns 0 on success and non-zero otherwise.
720e2cfabdfSWill Drewry  */
721c8bee430SKees Cook static struct seccomp_filter *
seccomp_prepare_user_filter(const char __user * user_filter)722c8bee430SKees Cook seccomp_prepare_user_filter(const char __user *user_filter)
723e2cfabdfSWill Drewry {
724e2cfabdfSWill Drewry 	struct sock_fprog fprog;
725c8bee430SKees Cook 	struct seccomp_filter *filter = ERR_PTR(-EFAULT);
726e2cfabdfSWill Drewry 
727e2cfabdfSWill Drewry #ifdef CONFIG_COMPAT
7285c38065eSAndy Lutomirski 	if (in_compat_syscall()) {
729e2cfabdfSWill Drewry 		struct compat_sock_fprog fprog32;
730e2cfabdfSWill Drewry 		if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
731e2cfabdfSWill Drewry 			goto out;
732e2cfabdfSWill Drewry 		fprog.len = fprog32.len;
733e2cfabdfSWill Drewry 		fprog.filter = compat_ptr(fprog32.filter);
734e2cfabdfSWill Drewry 	} else /* falls through to the if below. */
735e2cfabdfSWill Drewry #endif
736e2cfabdfSWill Drewry 	if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
737e2cfabdfSWill Drewry 		goto out;
738c8bee430SKees Cook 	filter = seccomp_prepare_filter(&fprog);
739e2cfabdfSWill Drewry out:
740c8bee430SKees Cook 	return filter;
741c8bee430SKees Cook }
742c8bee430SKees Cook 
7438e01b51aSYiFei Zhu #ifdef SECCOMP_ARCH_NATIVE
7448e01b51aSYiFei Zhu /**
7458e01b51aSYiFei Zhu  * seccomp_is_const_allow - check if filter is constant allow with given data
7468e01b51aSYiFei Zhu  * @fprog: The BPF programs
7478e01b51aSYiFei Zhu  * @sd: The seccomp data to check against, only syscall number and arch
7488e01b51aSYiFei Zhu  *      number are considered constant.
7498e01b51aSYiFei Zhu  */
seccomp_is_const_allow(struct sock_fprog_kern * fprog,struct seccomp_data * sd)7508e01b51aSYiFei Zhu static bool seccomp_is_const_allow(struct sock_fprog_kern *fprog,
7518e01b51aSYiFei Zhu 				   struct seccomp_data *sd)
7528e01b51aSYiFei Zhu {
7538e01b51aSYiFei Zhu 	unsigned int reg_value = 0;
7548e01b51aSYiFei Zhu 	unsigned int pc;
7558e01b51aSYiFei Zhu 	bool op_res;
7568e01b51aSYiFei Zhu 
7578e01b51aSYiFei Zhu 	if (WARN_ON_ONCE(!fprog))
7588e01b51aSYiFei Zhu 		return false;
7598e01b51aSYiFei Zhu 
760cf6cb56eSEyal Birger 	/* Our single exception to filtering. */
761cf6cb56eSEyal Birger #ifdef __NR_uretprobe
762cf6cb56eSEyal Birger #ifdef SECCOMP_ARCH_COMPAT
763cf6cb56eSEyal Birger 	if (sd->arch == SECCOMP_ARCH_NATIVE)
764cf6cb56eSEyal Birger #endif
765cf6cb56eSEyal Birger 		if (sd->nr == __NR_uretprobe)
766cf6cb56eSEyal Birger 			return true;
767cf6cb56eSEyal Birger #endif
768cf6cb56eSEyal Birger 
7698e01b51aSYiFei Zhu 	for (pc = 0; pc < fprog->len; pc++) {
7708e01b51aSYiFei Zhu 		struct sock_filter *insn = &fprog->filter[pc];
7718e01b51aSYiFei Zhu 		u16 code = insn->code;
7728e01b51aSYiFei Zhu 		u32 k = insn->k;
7738e01b51aSYiFei Zhu 
7748e01b51aSYiFei Zhu 		switch (code) {
7758e01b51aSYiFei Zhu 		case BPF_LD | BPF_W | BPF_ABS:
7768e01b51aSYiFei Zhu 			switch (k) {
7778e01b51aSYiFei Zhu 			case offsetof(struct seccomp_data, nr):
7788e01b51aSYiFei Zhu 				reg_value = sd->nr;
7798e01b51aSYiFei Zhu 				break;
7808e01b51aSYiFei Zhu 			case offsetof(struct seccomp_data, arch):
7818e01b51aSYiFei Zhu 				reg_value = sd->arch;
7828e01b51aSYiFei Zhu 				break;
7838e01b51aSYiFei Zhu 			default:
7848e01b51aSYiFei Zhu 				/* can't optimize (non-constant value load) */
7858e01b51aSYiFei Zhu 				return false;
7868e01b51aSYiFei Zhu 			}
7878e01b51aSYiFei Zhu 			break;
7888e01b51aSYiFei Zhu 		case BPF_RET | BPF_K:
7898e01b51aSYiFei Zhu 			/* reached return with constant values only, check allow */
7908e01b51aSYiFei Zhu 			return k == SECCOMP_RET_ALLOW;
7918e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JA:
7928e01b51aSYiFei Zhu 			pc += insn->k;
7938e01b51aSYiFei Zhu 			break;
7948e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JEQ | BPF_K:
7958e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JGE | BPF_K:
7968e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JGT | BPF_K:
7978e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JSET | BPF_K:
7988e01b51aSYiFei Zhu 			switch (BPF_OP(code)) {
7998e01b51aSYiFei Zhu 			case BPF_JEQ:
8008e01b51aSYiFei Zhu 				op_res = reg_value == k;
8018e01b51aSYiFei Zhu 				break;
8028e01b51aSYiFei Zhu 			case BPF_JGE:
8038e01b51aSYiFei Zhu 				op_res = reg_value >= k;
8048e01b51aSYiFei Zhu 				break;
8058e01b51aSYiFei Zhu 			case BPF_JGT:
8068e01b51aSYiFei Zhu 				op_res = reg_value > k;
8078e01b51aSYiFei Zhu 				break;
8088e01b51aSYiFei Zhu 			case BPF_JSET:
8098e01b51aSYiFei Zhu 				op_res = !!(reg_value & k);
8108e01b51aSYiFei Zhu 				break;
8118e01b51aSYiFei Zhu 			default:
8128e01b51aSYiFei Zhu 				/* can't optimize (unknown jump) */
8138e01b51aSYiFei Zhu 				return false;
8148e01b51aSYiFei Zhu 			}
8158e01b51aSYiFei Zhu 
8168e01b51aSYiFei Zhu 			pc += op_res ? insn->jt : insn->jf;
8178e01b51aSYiFei Zhu 			break;
8188e01b51aSYiFei Zhu 		case BPF_ALU | BPF_AND | BPF_K:
8198e01b51aSYiFei Zhu 			reg_value &= k;
8208e01b51aSYiFei Zhu 			break;
8218e01b51aSYiFei Zhu 		default:
8228e01b51aSYiFei Zhu 			/* can't optimize (unknown insn) */
8238e01b51aSYiFei Zhu 			return false;
8248e01b51aSYiFei Zhu 		}
8258e01b51aSYiFei Zhu 	}
8268e01b51aSYiFei Zhu 
8278e01b51aSYiFei Zhu 	/* ran off the end of the filter?! */
8288e01b51aSYiFei Zhu 	WARN_ON(1);
8298e01b51aSYiFei Zhu 	return false;
8308e01b51aSYiFei Zhu }
8318e01b51aSYiFei Zhu 
seccomp_cache_prepare_bitmap(struct seccomp_filter * sfilter,void * bitmap,const void * bitmap_prev,size_t bitmap_size,int arch)8328e01b51aSYiFei Zhu static void seccomp_cache_prepare_bitmap(struct seccomp_filter *sfilter,
8338e01b51aSYiFei Zhu 					 void *bitmap, const void *bitmap_prev,
8348e01b51aSYiFei Zhu 					 size_t bitmap_size, int arch)
8358e01b51aSYiFei Zhu {
8368e01b51aSYiFei Zhu 	struct sock_fprog_kern *fprog = sfilter->prog->orig_prog;
8378e01b51aSYiFei Zhu 	struct seccomp_data sd;
8388e01b51aSYiFei Zhu 	int nr;
8398e01b51aSYiFei Zhu 
8408e01b51aSYiFei Zhu 	if (bitmap_prev) {
8418e01b51aSYiFei Zhu 		/* The new filter must be as restrictive as the last. */
8428e01b51aSYiFei Zhu 		bitmap_copy(bitmap, bitmap_prev, bitmap_size);
8438e01b51aSYiFei Zhu 	} else {
8448e01b51aSYiFei Zhu 		/* Before any filters, all syscalls are always allowed. */
8458e01b51aSYiFei Zhu 		bitmap_fill(bitmap, bitmap_size);
8468e01b51aSYiFei Zhu 	}
8478e01b51aSYiFei Zhu 
8488e01b51aSYiFei Zhu 	for (nr = 0; nr < bitmap_size; nr++) {
8498e01b51aSYiFei Zhu 		/* No bitmap change: not a cacheable action. */
8508e01b51aSYiFei Zhu 		if (!test_bit(nr, bitmap))
8518e01b51aSYiFei Zhu 			continue;
8528e01b51aSYiFei Zhu 
8538e01b51aSYiFei Zhu 		sd.nr = nr;
8548e01b51aSYiFei Zhu 		sd.arch = arch;
8558e01b51aSYiFei Zhu 
8568e01b51aSYiFei Zhu 		/* No bitmap change: continue to always allow. */
8578e01b51aSYiFei Zhu 		if (seccomp_is_const_allow(fprog, &sd))
8588e01b51aSYiFei Zhu 			continue;
8598e01b51aSYiFei Zhu 
8608e01b51aSYiFei Zhu 		/*
8618e01b51aSYiFei Zhu 		 * Not a cacheable action: always run filters.
8628e01b51aSYiFei Zhu 		 * atomic clear_bit() not needed, filter not visible yet.
8638e01b51aSYiFei Zhu 		 */
8648e01b51aSYiFei Zhu 		__clear_bit(nr, bitmap);
8658e01b51aSYiFei Zhu 	}
8668e01b51aSYiFei Zhu }
8678e01b51aSYiFei Zhu 
8688e01b51aSYiFei Zhu /**
869a3fc712cSCui GaoSheng  * seccomp_cache_prepare - emulate the filter to find cacheable syscalls
8708e01b51aSYiFei Zhu  * @sfilter: The seccomp filter
8718e01b51aSYiFei Zhu  *
8728e01b51aSYiFei Zhu  * Returns 0 if successful or -errno if error occurred.
8738e01b51aSYiFei Zhu  */
seccomp_cache_prepare(struct seccomp_filter * sfilter)8748e01b51aSYiFei Zhu static void seccomp_cache_prepare(struct seccomp_filter *sfilter)
8758e01b51aSYiFei Zhu {
8768e01b51aSYiFei Zhu 	struct action_cache *cache = &sfilter->cache;
8778e01b51aSYiFei Zhu 	const struct action_cache *cache_prev =
8788e01b51aSYiFei Zhu 		sfilter->prev ? &sfilter->prev->cache : NULL;
8798e01b51aSYiFei Zhu 
8808e01b51aSYiFei Zhu 	seccomp_cache_prepare_bitmap(sfilter, cache->allow_native,
8818e01b51aSYiFei Zhu 				     cache_prev ? cache_prev->allow_native : NULL,
8828e01b51aSYiFei Zhu 				     SECCOMP_ARCH_NATIVE_NR,
8838e01b51aSYiFei Zhu 				     SECCOMP_ARCH_NATIVE);
8848e01b51aSYiFei Zhu 
8858e01b51aSYiFei Zhu #ifdef SECCOMP_ARCH_COMPAT
8868e01b51aSYiFei Zhu 	seccomp_cache_prepare_bitmap(sfilter, cache->allow_compat,
8878e01b51aSYiFei Zhu 				     cache_prev ? cache_prev->allow_compat : NULL,
8888e01b51aSYiFei Zhu 				     SECCOMP_ARCH_COMPAT_NR,
8898e01b51aSYiFei Zhu 				     SECCOMP_ARCH_COMPAT);
8908e01b51aSYiFei Zhu #endif /* SECCOMP_ARCH_COMPAT */
8918e01b51aSYiFei Zhu }
8928e01b51aSYiFei Zhu #endif /* SECCOMP_ARCH_NATIVE */
8938e01b51aSYiFei Zhu 
894c8bee430SKees Cook /**
895c8bee430SKees Cook  * seccomp_attach_filter: validate and attach filter
896c8bee430SKees Cook  * @flags:  flags to change filter behavior
897c8bee430SKees Cook  * @filter: seccomp filter to add to the current process
898c8bee430SKees Cook  *
899dbd95212SKees Cook  * Caller must be holding current->sighand->siglock lock.
900dbd95212SKees Cook  *
9017a0df7fbSTycho Andersen  * Returns 0 on success, -ve on error, or
9027a0df7fbSTycho Andersen  *   - in TSYNC mode: the pid of a thread which was either not in the correct
9037a0df7fbSTycho Andersen  *     seccomp mode or did not have an ancestral seccomp filter
9047a0df7fbSTycho Andersen  *   - in NEW_LISTENER mode: the fd of the new listener
905c8bee430SKees Cook  */
seccomp_attach_filter(unsigned int flags,struct seccomp_filter * filter)906c8bee430SKees Cook static long seccomp_attach_filter(unsigned int flags,
907c8bee430SKees Cook 				  struct seccomp_filter *filter)
908c8bee430SKees Cook {
909c8bee430SKees Cook 	unsigned long total_insns;
910c8bee430SKees Cook 	struct seccomp_filter *walker;
911c8bee430SKees Cook 
91269f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
913dbd95212SKees Cook 
914c8bee430SKees Cook 	/* Validate resulting filter length. */
915c8bee430SKees Cook 	total_insns = filter->prog->len;
916c8bee430SKees Cook 	for (walker = current->seccomp.filter; walker; walker = walker->prev)
917c8bee430SKees Cook 		total_insns += walker->prog->len + 4;  /* 4 instr penalty */
918c8bee430SKees Cook 	if (total_insns > MAX_INSNS_PER_PATH)
919c8bee430SKees Cook 		return -ENOMEM;
920c8bee430SKees Cook 
921c2e1f2e3SKees Cook 	/* If thread sync has been requested, check that it is possible. */
922c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
923c2e1f2e3SKees Cook 		int ret;
924c2e1f2e3SKees Cook 
925c2e1f2e3SKees Cook 		ret = seccomp_can_sync_threads();
92651891498STycho Andersen 		if (ret) {
92751891498STycho Andersen 			if (flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH)
92851891498STycho Andersen 				return -ESRCH;
92951891498STycho Andersen 			else
930c2e1f2e3SKees Cook 				return ret;
931c2e1f2e3SKees Cook 		}
93251891498STycho Andersen 	}
933c2e1f2e3SKees Cook 
934e66a3997STyler Hicks 	/* Set log flag, if present. */
935e66a3997STyler Hicks 	if (flags & SECCOMP_FILTER_FLAG_LOG)
936e66a3997STyler Hicks 		filter->log = true;
937e66a3997STyler Hicks 
938c2aa2dfeSSargun Dhillon 	/* Set wait killable flag, if present. */
939c2aa2dfeSSargun Dhillon 	if (flags & SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV)
940c2aa2dfeSSargun Dhillon 		filter->wait_killable_recv = true;
941c2aa2dfeSSargun Dhillon 
942c8bee430SKees Cook 	/*
943c8bee430SKees Cook 	 * If there is an existing filter, make it the prev and don't drop its
944c8bee430SKees Cook 	 * task reference.
945c8bee430SKees Cook 	 */
946c8bee430SKees Cook 	filter->prev = current->seccomp.filter;
9478e01b51aSYiFei Zhu 	seccomp_cache_prepare(filter);
948c8bee430SKees Cook 	current->seccomp.filter = filter;
949c818c03bSKees Cook 	atomic_inc(&current->seccomp.filter_count);
950c8bee430SKees Cook 
951c2e1f2e3SKees Cook 	/* Now that the new filter is in place, synchronize to all threads. */
952c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC)
95300a02d0cSKees Cook 		seccomp_sync_threads(flags);
954c2e1f2e3SKees Cook 
955c8bee430SKees Cook 	return 0;
956e2cfabdfSWill Drewry }
957e2cfabdfSWill Drewry 
__get_seccomp_filter(struct seccomp_filter * filter)958084f5601SColin Ian King static void __get_seccomp_filter(struct seccomp_filter *filter)
95966a733eaSOleg Nesterov {
960b707ddeeSChristian Brauner 	refcount_inc(&filter->refs);
96166a733eaSOleg Nesterov }
96266a733eaSOleg Nesterov 
963e2cfabdfSWill Drewry /* get_seccomp_filter - increments the reference count of the filter on @tsk */
get_seccomp_filter(struct task_struct * tsk)964e2cfabdfSWill Drewry void get_seccomp_filter(struct task_struct *tsk)
965e2cfabdfSWill Drewry {
966e2cfabdfSWill Drewry 	struct seccomp_filter *orig = tsk->seccomp.filter;
967e2cfabdfSWill Drewry 	if (!orig)
968e2cfabdfSWill Drewry 		return;
96966a733eaSOleg Nesterov 	__get_seccomp_filter(orig);
97099cdb8b9SChristian Brauner 	refcount_inc(&orig->users);
971e2cfabdfSWill Drewry }
972e2cfabdfSWill Drewry 
973e2cfabdfSWill Drewry #endif	/* CONFIG_SECCOMP_FILTER */
9741da177e4SLinus Torvalds 
9750ddec0fcSTyler Hicks /* For use with seccomp_actions_logged */
9764d3b0b05SKees Cook #define SECCOMP_LOG_KILL_PROCESS	(1 << 0)
9774d3b0b05SKees Cook #define SECCOMP_LOG_KILL_THREAD		(1 << 1)
9780ddec0fcSTyler Hicks #define SECCOMP_LOG_TRAP		(1 << 2)
9790ddec0fcSTyler Hicks #define SECCOMP_LOG_ERRNO		(1 << 3)
9800ddec0fcSTyler Hicks #define SECCOMP_LOG_TRACE		(1 << 4)
98159f5cf44STyler Hicks #define SECCOMP_LOG_LOG			(1 << 5)
98259f5cf44STyler Hicks #define SECCOMP_LOG_ALLOW		(1 << 6)
9836a21cc50STycho Andersen #define SECCOMP_LOG_USER_NOTIF		(1 << 7)
9840ddec0fcSTyler Hicks 
9854d3b0b05SKees Cook static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
9864d3b0b05SKees Cook 				    SECCOMP_LOG_KILL_THREAD  |
987fd76875cSKees Cook 				    SECCOMP_LOG_TRAP  |
988fd76875cSKees Cook 				    SECCOMP_LOG_ERRNO |
9896a21cc50STycho Andersen 				    SECCOMP_LOG_USER_NOTIF |
990fd76875cSKees Cook 				    SECCOMP_LOG_TRACE |
99159f5cf44STyler Hicks 				    SECCOMP_LOG_LOG;
9920ddec0fcSTyler Hicks 
seccomp_log(unsigned long syscall,long signr,u32 action,bool requested)993e66a3997STyler Hicks static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
994e66a3997STyler Hicks 			       bool requested)
9950ddec0fcSTyler Hicks {
9960ddec0fcSTyler Hicks 	bool log = false;
9970ddec0fcSTyler Hicks 
9980ddec0fcSTyler Hicks 	switch (action) {
9990ddec0fcSTyler Hicks 	case SECCOMP_RET_ALLOW:
1000e66a3997STyler Hicks 		break;
10010ddec0fcSTyler Hicks 	case SECCOMP_RET_TRAP:
1002e66a3997STyler Hicks 		log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
1003e66a3997STyler Hicks 		break;
10040ddec0fcSTyler Hicks 	case SECCOMP_RET_ERRNO:
1005e66a3997STyler Hicks 		log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
1006e66a3997STyler Hicks 		break;
10070ddec0fcSTyler Hicks 	case SECCOMP_RET_TRACE:
1008e66a3997STyler Hicks 		log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
10090ddec0fcSTyler Hicks 		break;
10106a21cc50STycho Andersen 	case SECCOMP_RET_USER_NOTIF:
10116a21cc50STycho Andersen 		log = requested && seccomp_actions_logged & SECCOMP_LOG_USER_NOTIF;
10126a21cc50STycho Andersen 		break;
101359f5cf44STyler Hicks 	case SECCOMP_RET_LOG:
101459f5cf44STyler Hicks 		log = seccomp_actions_logged & SECCOMP_LOG_LOG;
101559f5cf44STyler Hicks 		break;
1016fd76875cSKees Cook 	case SECCOMP_RET_KILL_THREAD:
1017fd76875cSKees Cook 		log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
10184d3b0b05SKees Cook 		break;
10194d3b0b05SKees Cook 	case SECCOMP_RET_KILL_PROCESS:
10204d3b0b05SKees Cook 	default:
10214d3b0b05SKees Cook 		log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
10220ddec0fcSTyler Hicks 	}
10230ddec0fcSTyler Hicks 
10240ddec0fcSTyler Hicks 	/*
1025326bee02STyler Hicks 	 * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the
1026326bee02STyler Hicks 	 * FILTER_FLAG_LOG bit was set. The admin has the ability to silence
1027326bee02STyler Hicks 	 * any action from being logged by removing the action name from the
1028326bee02STyler Hicks 	 * seccomp_actions_logged sysctl.
10290ddec0fcSTyler Hicks 	 */
1030326bee02STyler Hicks 	if (!log)
1031326bee02STyler Hicks 		return;
10320ddec0fcSTyler Hicks 
1033326bee02STyler Hicks 	audit_seccomp(syscall, signr, action);
10340ddec0fcSTyler Hicks }
10350ddec0fcSTyler Hicks 
10361da177e4SLinus Torvalds /*
10371da177e4SLinus Torvalds  * Secure computing mode 1 allows only read/write/exit/sigreturn.
10381da177e4SLinus Torvalds  * To be fully secure this must be combined with rlimit
10391da177e4SLinus Torvalds  * to limit the stack allocations too.
10401da177e4SLinus Torvalds  */
1041cb4253aaSMatt Redfearn static const int mode1_syscalls[] = {
10421da177e4SLinus Torvalds 	__NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
1043cf6cb56eSEyal Birger #ifdef __NR_uretprobe
1044cf6cb56eSEyal Birger 	__NR_uretprobe,
1045cf6cb56eSEyal Birger #endif
1046fe4bfff8SKees Cook 	-1, /* negative terminated */
10471da177e4SLinus Torvalds };
10481da177e4SLinus Torvalds 
__secure_computing_strict(int this_syscall)1049a4412fc9SAndy Lutomirski static void __secure_computing_strict(int this_syscall)
10501da177e4SLinus Torvalds {
1051fe4bfff8SKees Cook 	const int *allowed_syscalls = mode1_syscalls;
1052a4412fc9SAndy Lutomirski #ifdef CONFIG_COMPAT
10535c38065eSAndy Lutomirski 	if (in_compat_syscall())
1054fe4bfff8SKees Cook 		allowed_syscalls = get_compat_mode1_syscalls();
1055a4412fc9SAndy Lutomirski #endif
1056a4412fc9SAndy Lutomirski 	do {
1057fe4bfff8SKees Cook 		if (*allowed_syscalls == this_syscall)
1058a4412fc9SAndy Lutomirski 			return;
1059fe4bfff8SKees Cook 	} while (*++allowed_syscalls != -1);
1060a4412fc9SAndy Lutomirski 
1061a4412fc9SAndy Lutomirski #ifdef SECCOMP_DEBUG
1062a4412fc9SAndy Lutomirski 	dump_stack();
1063a4412fc9SAndy Lutomirski #endif
1064495ac306SKees Cook 	current->seccomp.mode = SECCOMP_MODE_DEAD;
1065fd76875cSKees Cook 	seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
1066a4412fc9SAndy Lutomirski 	do_exit(SIGKILL);
1067a4412fc9SAndy Lutomirski }
1068a4412fc9SAndy Lutomirski 
1069a4412fc9SAndy Lutomirski #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
secure_computing_strict(int this_syscall)1070a4412fc9SAndy Lutomirski void secure_computing_strict(int this_syscall)
1071a4412fc9SAndy Lutomirski {
1072a4412fc9SAndy Lutomirski 	int mode = current->seccomp.mode;
1073a4412fc9SAndy Lutomirski 
107497f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
107513c4a901STycho Andersen 	    unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
107613c4a901STycho Andersen 		return;
107713c4a901STycho Andersen 
1078221272f9SKees Cook 	if (mode == SECCOMP_MODE_DISABLED)
1079a4412fc9SAndy Lutomirski 		return;
1080a4412fc9SAndy Lutomirski 	else if (mode == SECCOMP_MODE_STRICT)
1081a4412fc9SAndy Lutomirski 		__secure_computing_strict(this_syscall);
1082a4412fc9SAndy Lutomirski 	else
1083a4412fc9SAndy Lutomirski 		BUG();
1084a4412fc9SAndy Lutomirski }
__secure_computing(void)10851027cd80SOleg Nesterov int __secure_computing(void)
1086b37778beSOleg Nesterov {
10871027cd80SOleg Nesterov 	int this_syscall = syscall_get_nr(current, current_pt_regs());
1088b37778beSOleg Nesterov 
1089b37778beSOleg Nesterov 	secure_computing_strict(this_syscall);
1090b37778beSOleg Nesterov 	return 0;
1091b37778beSOleg Nesterov }
1092a4412fc9SAndy Lutomirski #else
109313aa72f0SAndy Lutomirski 
109413aa72f0SAndy Lutomirski #ifdef CONFIG_SECCOMP_FILTER
seccomp_next_notify_id(struct seccomp_filter * filter)10956a21cc50STycho Andersen static u64 seccomp_next_notify_id(struct seccomp_filter *filter)
10966a21cc50STycho Andersen {
10976a21cc50STycho Andersen 	/*
10986a21cc50STycho Andersen 	 * Note: overflow is ok here, the id just needs to be unique per
10996a21cc50STycho Andersen 	 * filter.
11006a21cc50STycho Andersen 	 */
11016a21cc50STycho Andersen 	lockdep_assert_held(&filter->notify_lock);
11026a21cc50STycho Andersen 	return filter->notif->next_id++;
11036a21cc50STycho Andersen }
11046a21cc50STycho Andersen 
seccomp_handle_addfd(struct seccomp_kaddfd * addfd,struct seccomp_knotif * n)11050ae71c77SRodrigo Campos static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd, struct seccomp_knotif *n)
11067cf97b12SSargun Dhillon {
11070ae71c77SRodrigo Campos 	int fd;
11080ae71c77SRodrigo Campos 
11097cf97b12SSargun Dhillon 	/*
11107cf97b12SSargun Dhillon 	 * Remove the notification, and reset the list pointers, indicating
11117cf97b12SSargun Dhillon 	 * that it has been handled.
11127cf97b12SSargun Dhillon 	 */
11137cf97b12SSargun Dhillon 	list_del_init(&addfd->list);
111442eb0d54SChristoph Hellwig 	if (!addfd->setfd)
11154e94ddfeSChristian Brauner 		fd = receive_fd(addfd->file, NULL, addfd->flags);
111642eb0d54SChristoph Hellwig 	else
11170ae71c77SRodrigo Campos 		fd = receive_fd_replace(addfd->fd, addfd->file, addfd->flags);
11180ae71c77SRodrigo Campos 	addfd->ret = fd;
11190ae71c77SRodrigo Campos 
11200ae71c77SRodrigo Campos 	if (addfd->ioctl_flags & SECCOMP_ADDFD_FLAG_SEND) {
11210ae71c77SRodrigo Campos 		/* If we fail reset and return an error to the notifier */
11220ae71c77SRodrigo Campos 		if (fd < 0) {
11230ae71c77SRodrigo Campos 			n->state = SECCOMP_NOTIFY_SENT;
11240ae71c77SRodrigo Campos 		} else {
11250ae71c77SRodrigo Campos 			/* Return the FD we just added */
11260ae71c77SRodrigo Campos 			n->flags = 0;
11270ae71c77SRodrigo Campos 			n->error = 0;
11280ae71c77SRodrigo Campos 			n->val = fd;
11290ae71c77SRodrigo Campos 		}
11300ae71c77SRodrigo Campos 	}
11310ae71c77SRodrigo Campos 
11320ae71c77SRodrigo Campos 	/*
11330ae71c77SRodrigo Campos 	 * Mark the notification as completed. From this point, addfd mem
11340ae71c77SRodrigo Campos 	 * might be invalidated and we can't safely read it anymore.
11350ae71c77SRodrigo Campos 	 */
11367cf97b12SSargun Dhillon 	complete(&addfd->completion);
11377cf97b12SSargun Dhillon }
11387cf97b12SSargun Dhillon 
should_sleep_killable(struct seccomp_filter * match,struct seccomp_knotif * n)1139c2aa2dfeSSargun Dhillon static bool should_sleep_killable(struct seccomp_filter *match,
1140c2aa2dfeSSargun Dhillon 				  struct seccomp_knotif *n)
1141c2aa2dfeSSargun Dhillon {
1142c2aa2dfeSSargun Dhillon 	return match->wait_killable_recv && n->state == SECCOMP_NOTIFY_SENT;
1143c2aa2dfeSSargun Dhillon }
1144c2aa2dfeSSargun Dhillon 
seccomp_do_user_notification(int this_syscall,struct seccomp_filter * match,const struct seccomp_data * sd)1145fb3c5386SChristian Brauner static int seccomp_do_user_notification(int this_syscall,
11466a21cc50STycho Andersen 					struct seccomp_filter *match,
11476a21cc50STycho Andersen 					const struct seccomp_data *sd)
11486a21cc50STycho Andersen {
11496a21cc50STycho Andersen 	int err;
1150fb3c5386SChristian Brauner 	u32 flags = 0;
11516a21cc50STycho Andersen 	long ret = 0;
11526a21cc50STycho Andersen 	struct seccomp_knotif n = {};
11537cf97b12SSargun Dhillon 	struct seccomp_kaddfd *addfd, *tmp;
11546a21cc50STycho Andersen 
11556a21cc50STycho Andersen 	mutex_lock(&match->notify_lock);
11566a21cc50STycho Andersen 	err = -ENOSYS;
11576a21cc50STycho Andersen 	if (!match->notif)
11586a21cc50STycho Andersen 		goto out;
11596a21cc50STycho Andersen 
11606a21cc50STycho Andersen 	n.task = current;
11616a21cc50STycho Andersen 	n.state = SECCOMP_NOTIFY_INIT;
11626a21cc50STycho Andersen 	n.data = sd;
11636a21cc50STycho Andersen 	n.id = seccomp_next_notify_id(match);
11646a21cc50STycho Andersen 	init_completion(&n.ready);
11654cbf6f62SSargun Dhillon 	list_add_tail(&n.list, &match->notif->notifications);
11667cf97b12SSargun Dhillon 	INIT_LIST_HEAD(&n.addfd);
11676a21cc50STycho Andersen 
11684943b66dSAndrei Vagin 	atomic_inc(&match->notif->requests);
116948a1084aSAndrei Vagin 	if (match->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
117048a1084aSAndrei Vagin 		wake_up_poll_on_current_cpu(&match->wqh, EPOLLIN | EPOLLRDNORM);
117148a1084aSAndrei Vagin 	else
117276194c4eSChristian Brauner 		wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
11736a21cc50STycho Andersen 
11746a21cc50STycho Andersen 	/*
11756a21cc50STycho Andersen 	 * This is where we wait for a reply from userspace.
11766a21cc50STycho Andersen 	 */
1177ddc47391SSargun Dhillon 	do {
1178c2aa2dfeSSargun Dhillon 		bool wait_killable = should_sleep_killable(match, &n);
1179c2aa2dfeSSargun Dhillon 
1180ddc47391SSargun Dhillon 		mutex_unlock(&match->notify_lock);
1181c2aa2dfeSSargun Dhillon 		if (wait_killable)
1182c2aa2dfeSSargun Dhillon 			err = wait_for_completion_killable(&n.ready);
1183c2aa2dfeSSargun Dhillon 		else
11846a21cc50STycho Andersen 			err = wait_for_completion_interruptible(&n.ready);
11856a21cc50STycho Andersen 		mutex_lock(&match->notify_lock);
1186c2aa2dfeSSargun Dhillon 
1187c2aa2dfeSSargun Dhillon 		if (err != 0) {
1188c2aa2dfeSSargun Dhillon 			/*
1189c2aa2dfeSSargun Dhillon 			 * Check to see if the notifcation got picked up and
1190c2aa2dfeSSargun Dhillon 			 * whether we should switch to wait killable.
1191c2aa2dfeSSargun Dhillon 			 */
1192c2aa2dfeSSargun Dhillon 			if (!wait_killable && should_sleep_killable(match, &n))
1193c2aa2dfeSSargun Dhillon 				continue;
1194c2aa2dfeSSargun Dhillon 
1195ddc47391SSargun Dhillon 			goto interrupted;
1196c2aa2dfeSSargun Dhillon 		}
1197ddc47391SSargun Dhillon 
11987cf97b12SSargun Dhillon 		addfd = list_first_entry_or_null(&n.addfd,
11997cf97b12SSargun Dhillon 						 struct seccomp_kaddfd, list);
1200ddc47391SSargun Dhillon 		/* Check if we were woken up by a addfd message */
1201ddc47391SSargun Dhillon 		if (addfd)
12020ae71c77SRodrigo Campos 			seccomp_handle_addfd(addfd, &n);
1203ddc47391SSargun Dhillon 
1204ddc47391SSargun Dhillon 	}  while (n.state != SECCOMP_NOTIFY_REPLIED);
1205ddc47391SSargun Dhillon 
12066a21cc50STycho Andersen 	ret = n.val;
12076a21cc50STycho Andersen 	err = n.error;
1208fb3c5386SChristian Brauner 	flags = n.flags;
12096a21cc50STycho Andersen 
1210ddc47391SSargun Dhillon interrupted:
12117cf97b12SSargun Dhillon 	/* If there were any pending addfd calls, clear them out */
12127cf97b12SSargun Dhillon 	list_for_each_entry_safe(addfd, tmp, &n.addfd, list) {
12137cf97b12SSargun Dhillon 		/* The process went away before we got a chance to handle it */
12147cf97b12SSargun Dhillon 		addfd->ret = -ESRCH;
12157cf97b12SSargun Dhillon 		list_del_init(&addfd->list);
12167cf97b12SSargun Dhillon 		complete(&addfd->completion);
12177cf97b12SSargun Dhillon 	}
12187cf97b12SSargun Dhillon 
12196a21cc50STycho Andersen 	/*
12206a21cc50STycho Andersen 	 * Note that it's possible the listener died in between the time when
12217cf97b12SSargun Dhillon 	 * we were notified of a response (or a signal) and when we were able to
12226a21cc50STycho Andersen 	 * re-acquire the lock, so only delete from the list if the
12236a21cc50STycho Andersen 	 * notification actually exists.
12246a21cc50STycho Andersen 	 *
12256a21cc50STycho Andersen 	 * Also note that this test is only valid because there's no way to
12266a21cc50STycho Andersen 	 * *reattach* to a notifier right now. If one is added, we'll need to
12276a21cc50STycho Andersen 	 * keep track of the notif itself and make sure they match here.
12286a21cc50STycho Andersen 	 */
12296a21cc50STycho Andersen 	if (match->notif)
12306a21cc50STycho Andersen 		list_del(&n.list);
12316a21cc50STycho Andersen out:
12326a21cc50STycho Andersen 	mutex_unlock(&match->notify_lock);
1233fb3c5386SChristian Brauner 
1234fb3c5386SChristian Brauner 	/* Userspace requests to continue the syscall. */
1235fb3c5386SChristian Brauner 	if (flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE)
1236fb3c5386SChristian Brauner 		return 0;
1237fb3c5386SChristian Brauner 
12382d9ca267SDenis Efremov 	syscall_set_return_value(current, current_pt_regs(),
12396a21cc50STycho Andersen 				 err, ret);
1240fb3c5386SChristian Brauner 	return -1;
12416a21cc50STycho Andersen }
12426a21cc50STycho Andersen 
__seccomp_filter(int this_syscall,const bool recheck_after_trace)1243e1cec510SOleg Nesterov static int __seccomp_filter(int this_syscall, const bool recheck_after_trace)
124413aa72f0SAndy Lutomirski {
124513aa72f0SAndy Lutomirski 	u32 filter_ret, action;
1246e1cec510SOleg Nesterov 	struct seccomp_data sd;
1247deb4de8bSKees Cook 	struct seccomp_filter *match = NULL;
124813aa72f0SAndy Lutomirski 	int data;
12491da177e4SLinus Torvalds 
12503ba2530cSKees Cook 	/*
12513ba2530cSKees Cook 	 * Make sure that any changes to mode from another thread have
125223d67a54SGabriel Krisman Bertazi 	 * been seen after SYSCALL_WORK_SECCOMP was seen.
12533ba2530cSKees Cook 	 */
1254a381b70aSwanghongzhe 	smp_rmb();
12553ba2530cSKees Cook 
1256e1cec510SOleg Nesterov 	populate_seccomp_data(&sd);
1257db511391STycho Andersen 
1258e1cec510SOleg Nesterov 	filter_ret = seccomp_run_filters(&sd, &match);
125913aa72f0SAndy Lutomirski 	data = filter_ret & SECCOMP_RET_DATA;
12600466bdb9SKees Cook 	action = filter_ret & SECCOMP_RET_ACTION_FULL;
126113aa72f0SAndy Lutomirski 
126213aa72f0SAndy Lutomirski 	switch (action) {
1263acf3b2c7SWill Drewry 	case SECCOMP_RET_ERRNO:
1264580c57f1SKees Cook 		/* Set low-order bits as an errno, capped at MAX_ERRNO. */
1265580c57f1SKees Cook 		if (data > MAX_ERRNO)
1266580c57f1SKees Cook 			data = MAX_ERRNO;
12672d9ca267SDenis Efremov 		syscall_set_return_value(current, current_pt_regs(),
1268acf3b2c7SWill Drewry 					 -data, 0);
1269acf3b2c7SWill Drewry 		goto skip;
127013aa72f0SAndy Lutomirski 
1271bb6ea430SWill Drewry 	case SECCOMP_RET_TRAP:
1272bb6ea430SWill Drewry 		/* Show the handler the original registers. */
12732d9ca267SDenis Efremov 		syscall_rollback(current, current_pt_regs());
1274bb6ea430SWill Drewry 		/* Let the filter pass back 16 bits of data. */
1275307d522fSEric W. Biederman 		force_sig_seccomp(this_syscall, data, false);
1276bb6ea430SWill Drewry 		goto skip;
127713aa72f0SAndy Lutomirski 
1278fb0fadf9SWill Drewry 	case SECCOMP_RET_TRACE:
1279ce6526e8SKees Cook 		/* We've been put in this state by the ptracer already. */
1280ce6526e8SKees Cook 		if (recheck_after_trace)
1281ce6526e8SKees Cook 			return 0;
1282ce6526e8SKees Cook 
12838112c4f1SKees Cook 		/* ENOSYS these calls if there is no tracer attached. */
128487b526d3SAndy Lutomirski 		if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
12858112c4f1SKees Cook 			syscall_set_return_value(current,
12862d9ca267SDenis Efremov 						 current_pt_regs(),
128787b526d3SAndy Lutomirski 						 -ENOSYS, 0);
12888112c4f1SKees Cook 			goto skip;
128987b526d3SAndy Lutomirski 		}
129013aa72f0SAndy Lutomirski 
1291fb0fadf9SWill Drewry 		/* Allow the BPF to provide the event message */
1292fb0fadf9SWill Drewry 		ptrace_event(PTRACE_EVENT_SECCOMP, data);
1293fb0fadf9SWill Drewry 		/*
1294fb0fadf9SWill Drewry 		 * The delivery of a fatal signal during event
1295485a252aSKees Cook 		 * notification may silently skip tracer notification,
1296485a252aSKees Cook 		 * which could leave us with a potentially unmodified
1297485a252aSKees Cook 		 * syscall that the tracer would have liked to have
1298485a252aSKees Cook 		 * changed. Since the process is about to die, we just
1299485a252aSKees Cook 		 * force the syscall to be skipped and let the signal
1300485a252aSKees Cook 		 * kill the process and correctly handle any tracer exit
1301485a252aSKees Cook 		 * notifications.
1302fb0fadf9SWill Drewry 		 */
1303fb0fadf9SWill Drewry 		if (fatal_signal_pending(current))
1304485a252aSKees Cook 			goto skip;
13058112c4f1SKees Cook 		/* Check if the tracer forced the syscall to be skipped. */
13062d9ca267SDenis Efremov 		this_syscall = syscall_get_nr(current, current_pt_regs());
13078112c4f1SKees Cook 		if (this_syscall < 0)
13088112c4f1SKees Cook 			goto skip;
130987b526d3SAndy Lutomirski 
1310ce6526e8SKees Cook 		/*
1311ce6526e8SKees Cook 		 * Recheck the syscall, since it may have changed. This
1312ce6526e8SKees Cook 		 * intentionally uses a NULL struct seccomp_data to force
1313ce6526e8SKees Cook 		 * a reload of all registers. This does not goto skip since
1314ce6526e8SKees Cook 		 * a skip would have already been reported.
1315ce6526e8SKees Cook 		 */
1316e1cec510SOleg Nesterov 		if (__seccomp_filter(this_syscall, true))
1317ce6526e8SKees Cook 			return -1;
1318ce6526e8SKees Cook 
1319fb0fadf9SWill Drewry 		return 0;
13208112c4f1SKees Cook 
13216a21cc50STycho Andersen 	case SECCOMP_RET_USER_NOTIF:
1322e1cec510SOleg Nesterov 		if (seccomp_do_user_notification(this_syscall, match, &sd))
13236a21cc50STycho Andersen 			goto skip;
13246a21cc50STycho Andersen 
1325fb3c5386SChristian Brauner 		return 0;
1326fb3c5386SChristian Brauner 
132759f5cf44STyler Hicks 	case SECCOMP_RET_LOG:
132859f5cf44STyler Hicks 		seccomp_log(this_syscall, 0, action, true);
132959f5cf44STyler Hicks 		return 0;
133059f5cf44STyler Hicks 
13318112c4f1SKees Cook 	case SECCOMP_RET_ALLOW:
1332deb4de8bSKees Cook 		/*
1333deb4de8bSKees Cook 		 * Note that the "match" filter will always be NULL for
1334deb4de8bSKees Cook 		 * this action since SECCOMP_RET_ALLOW is the starting
1335deb4de8bSKees Cook 		 * state in seccomp_run_filters().
1336deb4de8bSKees Cook 		 */
13378112c4f1SKees Cook 		return 0;
13388112c4f1SKees Cook 
1339fd76875cSKees Cook 	case SECCOMP_RET_KILL_THREAD:
13404d3b0b05SKees Cook 	case SECCOMP_RET_KILL_PROCESS:
1341131b6351SKees Cook 	default:
1342495ac306SKees Cook 		current->seccomp.mode = SECCOMP_MODE_DEAD;
1343e66a3997STyler Hicks 		seccomp_log(this_syscall, SIGSYS, action, true);
1344d7276e32SKees Cook 		/* Dump core only if this is the last remaining thread. */
13454d671d92SRich Felker 		if (action != SECCOMP_RET_KILL_THREAD ||
1346d21918e5SEric W. Biederman 		    (atomic_read(&current->signal->live) == 1)) {
1347b25e6716SMike Frysinger 			/* Show the original registers in the dump. */
13482d9ca267SDenis Efremov 			syscall_rollback(current, current_pt_regs());
1349307d522fSEric W. Biederman 			/* Trigger a coredump with SIGSYS */
1350307d522fSEric W. Biederman 			force_sig_seccomp(this_syscall, data, true);
1351307d522fSEric W. Biederman 		} else {
13528112c4f1SKees Cook 			do_exit(SIGSYS);
1353307d522fSEric W. Biederman 		}
1354307d522fSEric W. Biederman 		return -1; /* skip the syscall go directly to signal handling */
13558112c4f1SKees Cook 	}
13568112c4f1SKees Cook 
13578112c4f1SKees Cook 	unreachable();
13588112c4f1SKees Cook 
13598112c4f1SKees Cook skip:
1360e66a3997STyler Hicks 	seccomp_log(this_syscall, 0, action, match ? match->log : false);
13618112c4f1SKees Cook 	return -1;
13628112c4f1SKees Cook }
13638112c4f1SKees Cook #else
__seccomp_filter(int this_syscall,const bool recheck_after_trace)1364e1cec510SOleg Nesterov static int __seccomp_filter(int this_syscall, const bool recheck_after_trace)
13658112c4f1SKees Cook {
13668112c4f1SKees Cook 	BUG();
136704b38d01SPaul Cercueil 
136804b38d01SPaul Cercueil 	return -1;
13698112c4f1SKees Cook }
13708112c4f1SKees Cook #endif
13718112c4f1SKees Cook 
__secure_computing(void)13721027cd80SOleg Nesterov int __secure_computing(void)
13738112c4f1SKees Cook {
13748112c4f1SKees Cook 	int mode = current->seccomp.mode;
13758112c4f1SKees Cook 	int this_syscall;
13768112c4f1SKees Cook 
137797f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
13788112c4f1SKees Cook 	    unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
13798112c4f1SKees Cook 		return 0;
13808112c4f1SKees Cook 
13811027cd80SOleg Nesterov 	this_syscall = syscall_get_nr(current, current_pt_regs());
13828112c4f1SKees Cook 
13838112c4f1SKees Cook 	switch (mode) {
13848112c4f1SKees Cook 	case SECCOMP_MODE_STRICT:
13858112c4f1SKees Cook 		__secure_computing_strict(this_syscall);  /* may call do_exit */
13868112c4f1SKees Cook 		return 0;
13878112c4f1SKees Cook 	case SECCOMP_MODE_FILTER:
1388e1cec510SOleg Nesterov 		return __seccomp_filter(this_syscall, false);
1389495ac306SKees Cook 	/* Surviving SECCOMP_RET_KILL_* must be proactively impossible. */
1390495ac306SKees Cook 	case SECCOMP_MODE_DEAD:
1391495ac306SKees Cook 		WARN_ON_ONCE(1);
1392495ac306SKees Cook 		do_exit(SIGKILL);
1393495ac306SKees Cook 		return -1;
13948112c4f1SKees Cook 	default:
13958112c4f1SKees Cook 		BUG();
13968112c4f1SKees Cook 	}
1397acf3b2c7SWill Drewry }
1398a4412fc9SAndy Lutomirski #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
13991d9d02feSAndrea Arcangeli 
prctl_get_seccomp(void)14001d9d02feSAndrea Arcangeli long prctl_get_seccomp(void)
14011d9d02feSAndrea Arcangeli {
14021d9d02feSAndrea Arcangeli 	return current->seccomp.mode;
14031d9d02feSAndrea Arcangeli }
14041d9d02feSAndrea Arcangeli 
1405e2cfabdfSWill Drewry /**
14063b23dd12SKees Cook  * seccomp_set_mode_strict: internal function for setting strict seccomp
1407e2cfabdfSWill Drewry  *
1408e2cfabdfSWill Drewry  * Once current->seccomp.mode is non-zero, it may not be changed.
1409e2cfabdfSWill Drewry  *
1410e2cfabdfSWill Drewry  * Returns 0 on success or -EINVAL on failure.
1411e2cfabdfSWill Drewry  */
seccomp_set_mode_strict(void)14123b23dd12SKees Cook static long seccomp_set_mode_strict(void)
14131d9d02feSAndrea Arcangeli {
14143b23dd12SKees Cook 	const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
1415e2cfabdfSWill Drewry 	long ret = -EINVAL;
14161d9d02feSAndrea Arcangeli 
1417dbd95212SKees Cook 	spin_lock_irq(&current->sighand->siglock);
1418dbd95212SKees Cook 
14191f41b450SKees Cook 	if (!seccomp_may_assign_mode(seccomp_mode))
14201d9d02feSAndrea Arcangeli 		goto out;
14211d9d02feSAndrea Arcangeli 
1422cf99abacSAndrea Arcangeli #ifdef TIF_NOTSC
1423cf99abacSAndrea Arcangeli 	disable_TSC();
1424cf99abacSAndrea Arcangeli #endif
142500a02d0cSKees Cook 	seccomp_assign_mode(current, seccomp_mode, 0);
14263b23dd12SKees Cook 	ret = 0;
14273b23dd12SKees Cook 
14283b23dd12SKees Cook out:
1429dbd95212SKees Cook 	spin_unlock_irq(&current->sighand->siglock);
14303b23dd12SKees Cook 
14313b23dd12SKees Cook 	return ret;
14323b23dd12SKees Cook }
14333b23dd12SKees Cook 
1434e2cfabdfSWill Drewry #ifdef CONFIG_SECCOMP_FILTER
seccomp_notify_free(struct seccomp_filter * filter)1435e8393179STycho Andersen static void seccomp_notify_free(struct seccomp_filter *filter)
1436e8393179STycho Andersen {
1437e8393179STycho Andersen 	kfree(filter->notif);
1438e8393179STycho Andersen 	filter->notif = NULL;
1439e8393179STycho Andersen }
1440e8393179STycho Andersen 
seccomp_notify_detach(struct seccomp_filter * filter)1441a566a901STycho Andersen static void seccomp_notify_detach(struct seccomp_filter *filter)
14426a21cc50STycho Andersen {
14436a21cc50STycho Andersen 	struct seccomp_knotif *knotif;
14446a21cc50STycho Andersen 
1445a811dc61STycho Andersen 	if (!filter)
1446a566a901STycho Andersen 		return;
1447a811dc61STycho Andersen 
14486a21cc50STycho Andersen 	mutex_lock(&filter->notify_lock);
14496a21cc50STycho Andersen 
14506a21cc50STycho Andersen 	/*
14516a21cc50STycho Andersen 	 * If this file is being closed because e.g. the task who owned it
14526a21cc50STycho Andersen 	 * died, let's wake everyone up who was waiting on us.
14536a21cc50STycho Andersen 	 */
14546a21cc50STycho Andersen 	list_for_each_entry(knotif, &filter->notif->notifications, list) {
14556a21cc50STycho Andersen 		if (knotif->state == SECCOMP_NOTIFY_REPLIED)
14566a21cc50STycho Andersen 			continue;
14576a21cc50STycho Andersen 
14586a21cc50STycho Andersen 		knotif->state = SECCOMP_NOTIFY_REPLIED;
14596a21cc50STycho Andersen 		knotif->error = -ENOSYS;
14606a21cc50STycho Andersen 		knotif->val = 0;
14616a21cc50STycho Andersen 
14627cf97b12SSargun Dhillon 		/*
14637cf97b12SSargun Dhillon 		 * We do not need to wake up any pending addfd messages, as
14647cf97b12SSargun Dhillon 		 * the notifier will do that for us, as this just looks
14657cf97b12SSargun Dhillon 		 * like a standard reply.
14667cf97b12SSargun Dhillon 		 */
14676a21cc50STycho Andersen 		complete(&knotif->ready);
14686a21cc50STycho Andersen 	}
14696a21cc50STycho Andersen 
1470e8393179STycho Andersen 	seccomp_notify_free(filter);
14716a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
1472a566a901STycho Andersen }
1473a566a901STycho Andersen 
seccomp_notify_release(struct inode * inode,struct file * file)1474a566a901STycho Andersen static int seccomp_notify_release(struct inode *inode, struct file *file)
1475a566a901STycho Andersen {
1476a566a901STycho Andersen 	struct seccomp_filter *filter = file->private_data;
1477a566a901STycho Andersen 
1478a566a901STycho Andersen 	seccomp_notify_detach(filter);
14796a21cc50STycho Andersen 	__put_seccomp_filter(filter);
14806a21cc50STycho Andersen 	return 0;
14816a21cc50STycho Andersen }
14826a21cc50STycho Andersen 
14839f87dcf1SSargun Dhillon /* must be called with notif_lock held */
14849f87dcf1SSargun Dhillon static inline struct seccomp_knotif *
find_notification(struct seccomp_filter * filter,u64 id)14859f87dcf1SSargun Dhillon find_notification(struct seccomp_filter *filter, u64 id)
14869f87dcf1SSargun Dhillon {
14879f87dcf1SSargun Dhillon 	struct seccomp_knotif *cur;
14889f87dcf1SSargun Dhillon 
14899f87dcf1SSargun Dhillon 	lockdep_assert_held(&filter->notify_lock);
14909f87dcf1SSargun Dhillon 
14919f87dcf1SSargun Dhillon 	list_for_each_entry(cur, &filter->notif->notifications, list) {
14929f87dcf1SSargun Dhillon 		if (cur->id == id)
14939f87dcf1SSargun Dhillon 			return cur;
14949f87dcf1SSargun Dhillon 	}
14959f87dcf1SSargun Dhillon 
14969f87dcf1SSargun Dhillon 	return NULL;
14979f87dcf1SSargun Dhillon }
14989f87dcf1SSargun Dhillon 
recv_wake_function(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)14994943b66dSAndrei Vagin static int recv_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync,
15004943b66dSAndrei Vagin 				  void *key)
15014943b66dSAndrei Vagin {
15024943b66dSAndrei Vagin 	/* Avoid a wakeup if event not interesting for us. */
150395036a79SAndrei Vagin 	if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR | EPOLLHUP)))
15044943b66dSAndrei Vagin 		return 0;
15054943b66dSAndrei Vagin 	return autoremove_wake_function(wait, mode, sync, key);
15064943b66dSAndrei Vagin }
15074943b66dSAndrei Vagin 
recv_wait_event(struct seccomp_filter * filter)15084943b66dSAndrei Vagin static int recv_wait_event(struct seccomp_filter *filter)
15094943b66dSAndrei Vagin {
15104943b66dSAndrei Vagin 	DEFINE_WAIT_FUNC(wait, recv_wake_function);
15114943b66dSAndrei Vagin 	int ret;
15124943b66dSAndrei Vagin 
151395036a79SAndrei Vagin 	if (refcount_read(&filter->users) == 0)
151495036a79SAndrei Vagin 		return 0;
151595036a79SAndrei Vagin 
15164943b66dSAndrei Vagin 	if (atomic_dec_if_positive(&filter->notif->requests) >= 0)
15174943b66dSAndrei Vagin 		return 0;
15184943b66dSAndrei Vagin 
15194943b66dSAndrei Vagin 	for (;;) {
15204943b66dSAndrei Vagin 		ret = prepare_to_wait_event(&filter->wqh, &wait, TASK_INTERRUPTIBLE);
15214943b66dSAndrei Vagin 
15224943b66dSAndrei Vagin 		if (atomic_dec_if_positive(&filter->notif->requests) >= 0)
15234943b66dSAndrei Vagin 			break;
152495036a79SAndrei Vagin 		if (refcount_read(&filter->users) == 0)
152595036a79SAndrei Vagin 			break;
15264943b66dSAndrei Vagin 
15274943b66dSAndrei Vagin 		if (ret)
15284943b66dSAndrei Vagin 			return ret;
15294943b66dSAndrei Vagin 
15304943b66dSAndrei Vagin 		schedule();
15314943b66dSAndrei Vagin 	}
15324943b66dSAndrei Vagin 	finish_wait(&filter->wqh, &wait);
15334943b66dSAndrei Vagin 	return 0;
15344943b66dSAndrei Vagin }
15359f87dcf1SSargun Dhillon 
seccomp_notify_recv(struct seccomp_filter * filter,void __user * buf)15366a21cc50STycho Andersen static long seccomp_notify_recv(struct seccomp_filter *filter,
15376a21cc50STycho Andersen 				void __user *buf)
15386a21cc50STycho Andersen {
15396a21cc50STycho Andersen 	struct seccomp_knotif *knotif = NULL, *cur;
15406a21cc50STycho Andersen 	struct seccomp_notif unotif;
15416a21cc50STycho Andersen 	ssize_t ret;
15426a21cc50STycho Andersen 
15432882d53cSSargun Dhillon 	/* Verify that we're not given garbage to keep struct extensible. */
15442882d53cSSargun Dhillon 	ret = check_zeroed_user(buf, sizeof(unotif));
15452882d53cSSargun Dhillon 	if (ret < 0)
15462882d53cSSargun Dhillon 		return ret;
15472882d53cSSargun Dhillon 	if (!ret)
15482882d53cSSargun Dhillon 		return -EINVAL;
15492882d53cSSargun Dhillon 
15506a21cc50STycho Andersen 	memset(&unotif, 0, sizeof(unotif));
15516a21cc50STycho Andersen 
15524943b66dSAndrei Vagin 	ret = recv_wait_event(filter);
15536a21cc50STycho Andersen 	if (ret < 0)
15546a21cc50STycho Andersen 		return ret;
15556a21cc50STycho Andersen 
15566a21cc50STycho Andersen 	mutex_lock(&filter->notify_lock);
15576a21cc50STycho Andersen 	list_for_each_entry(cur, &filter->notif->notifications, list) {
15586a21cc50STycho Andersen 		if (cur->state == SECCOMP_NOTIFY_INIT) {
15596a21cc50STycho Andersen 			knotif = cur;
15606a21cc50STycho Andersen 			break;
15616a21cc50STycho Andersen 		}
15626a21cc50STycho Andersen 	}
15636a21cc50STycho Andersen 
15646a21cc50STycho Andersen 	/*
15656a21cc50STycho Andersen 	 * If we didn't find a notification, it could be that the task was
15666a21cc50STycho Andersen 	 * interrupted by a fatal signal between the time we were woken and
15676a21cc50STycho Andersen 	 * when we were able to acquire the rw lock.
15686a21cc50STycho Andersen 	 */
15696a21cc50STycho Andersen 	if (!knotif) {
15706a21cc50STycho Andersen 		ret = -ENOENT;
15716a21cc50STycho Andersen 		goto out;
15726a21cc50STycho Andersen 	}
15736a21cc50STycho Andersen 
15746a21cc50STycho Andersen 	unotif.id = knotif->id;
15756a21cc50STycho Andersen 	unotif.pid = task_pid_vnr(knotif->task);
15766a21cc50STycho Andersen 	unotif.data = *(knotif->data);
15776a21cc50STycho Andersen 
15786a21cc50STycho Andersen 	knotif->state = SECCOMP_NOTIFY_SENT;
157976194c4eSChristian Brauner 	wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM);
15806a21cc50STycho Andersen 	ret = 0;
15816a21cc50STycho Andersen out:
15826a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
15836a21cc50STycho Andersen 
15846a21cc50STycho Andersen 	if (ret == 0 && copy_to_user(buf, &unotif, sizeof(unotif))) {
15856a21cc50STycho Andersen 		ret = -EFAULT;
15866a21cc50STycho Andersen 
15876a21cc50STycho Andersen 		/*
15886a21cc50STycho Andersen 		 * Userspace screwed up. To make sure that we keep this
15896a21cc50STycho Andersen 		 * notification alive, let's reset it back to INIT. It
15906a21cc50STycho Andersen 		 * may have died when we released the lock, so we need to make
15916a21cc50STycho Andersen 		 * sure it's still around.
15926a21cc50STycho Andersen 		 */
15936a21cc50STycho Andersen 		mutex_lock(&filter->notify_lock);
15949f87dcf1SSargun Dhillon 		knotif = find_notification(filter, unotif.id);
15956a21cc50STycho Andersen 		if (knotif) {
1596c2aa2dfeSSargun Dhillon 			/* Reset the process to make sure it's not stuck */
1597c2aa2dfeSSargun Dhillon 			if (should_sleep_killable(filter, knotif))
1598c2aa2dfeSSargun Dhillon 				complete(&knotif->ready);
15996a21cc50STycho Andersen 			knotif->state = SECCOMP_NOTIFY_INIT;
16004943b66dSAndrei Vagin 			atomic_inc(&filter->notif->requests);
16014943b66dSAndrei Vagin 			wake_up_poll(&filter->wqh, EPOLLIN | EPOLLRDNORM);
16026a21cc50STycho Andersen 		}
16036a21cc50STycho Andersen 		mutex_unlock(&filter->notify_lock);
16046a21cc50STycho Andersen 	}
16056a21cc50STycho Andersen 
16066a21cc50STycho Andersen 	return ret;
16076a21cc50STycho Andersen }
16086a21cc50STycho Andersen 
seccomp_notify_send(struct seccomp_filter * filter,void __user * buf)16096a21cc50STycho Andersen static long seccomp_notify_send(struct seccomp_filter *filter,
16106a21cc50STycho Andersen 				void __user *buf)
16116a21cc50STycho Andersen {
16126a21cc50STycho Andersen 	struct seccomp_notif_resp resp = {};
16139f87dcf1SSargun Dhillon 	struct seccomp_knotif *knotif;
16146a21cc50STycho Andersen 	long ret;
16156a21cc50STycho Andersen 
16166a21cc50STycho Andersen 	if (copy_from_user(&resp, buf, sizeof(resp)))
16176a21cc50STycho Andersen 		return -EFAULT;
16186a21cc50STycho Andersen 
1619fb3c5386SChristian Brauner 	if (resp.flags & ~SECCOMP_USER_NOTIF_FLAG_CONTINUE)
1620fb3c5386SChristian Brauner 		return -EINVAL;
1621fb3c5386SChristian Brauner 
1622fb3c5386SChristian Brauner 	if ((resp.flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE) &&
1623fb3c5386SChristian Brauner 	    (resp.error || resp.val))
16246a21cc50STycho Andersen 		return -EINVAL;
16256a21cc50STycho Andersen 
16266a21cc50STycho Andersen 	ret = mutex_lock_interruptible(&filter->notify_lock);
16276a21cc50STycho Andersen 	if (ret < 0)
16286a21cc50STycho Andersen 		return ret;
16296a21cc50STycho Andersen 
16309f87dcf1SSargun Dhillon 	knotif = find_notification(filter, resp.id);
16316a21cc50STycho Andersen 	if (!knotif) {
16326a21cc50STycho Andersen 		ret = -ENOENT;
16336a21cc50STycho Andersen 		goto out;
16346a21cc50STycho Andersen 	}
16356a21cc50STycho Andersen 
16366a21cc50STycho Andersen 	/* Allow exactly one reply. */
16376a21cc50STycho Andersen 	if (knotif->state != SECCOMP_NOTIFY_SENT) {
16386a21cc50STycho Andersen 		ret = -EINPROGRESS;
16396a21cc50STycho Andersen 		goto out;
16406a21cc50STycho Andersen 	}
16416a21cc50STycho Andersen 
16426a21cc50STycho Andersen 	ret = 0;
16436a21cc50STycho Andersen 	knotif->state = SECCOMP_NOTIFY_REPLIED;
16446a21cc50STycho Andersen 	knotif->error = resp.error;
16456a21cc50STycho Andersen 	knotif->val = resp.val;
1646fb3c5386SChristian Brauner 	knotif->flags = resp.flags;
164748a1084aSAndrei Vagin 	if (filter->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
164848a1084aSAndrei Vagin 		complete_on_current_cpu(&knotif->ready);
164948a1084aSAndrei Vagin 	else
16506a21cc50STycho Andersen 		complete(&knotif->ready);
16516a21cc50STycho Andersen out:
16526a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
16536a21cc50STycho Andersen 	return ret;
16546a21cc50STycho Andersen }
16556a21cc50STycho Andersen 
seccomp_notify_id_valid(struct seccomp_filter * filter,void __user * buf)16566a21cc50STycho Andersen static long seccomp_notify_id_valid(struct seccomp_filter *filter,
16576a21cc50STycho Andersen 				    void __user *buf)
16586a21cc50STycho Andersen {
16599f87dcf1SSargun Dhillon 	struct seccomp_knotif *knotif;
16606a21cc50STycho Andersen 	u64 id;
16616a21cc50STycho Andersen 	long ret;
16626a21cc50STycho Andersen 
16636a21cc50STycho Andersen 	if (copy_from_user(&id, buf, sizeof(id)))
16646a21cc50STycho Andersen 		return -EFAULT;
16656a21cc50STycho Andersen 
16666a21cc50STycho Andersen 	ret = mutex_lock_interruptible(&filter->notify_lock);
16676a21cc50STycho Andersen 	if (ret < 0)
16686a21cc50STycho Andersen 		return ret;
16696a21cc50STycho Andersen 
16709f87dcf1SSargun Dhillon 	knotif = find_notification(filter, id);
16719f87dcf1SSargun Dhillon 	if (knotif && knotif->state == SECCOMP_NOTIFY_SENT)
16726a21cc50STycho Andersen 		ret = 0;
16739f87dcf1SSargun Dhillon 	else
16749f87dcf1SSargun Dhillon 		ret = -ENOENT;
16756a21cc50STycho Andersen 
16766a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
16776a21cc50STycho Andersen 	return ret;
16786a21cc50STycho Andersen }
16796a21cc50STycho Andersen 
seccomp_notify_set_flags(struct seccomp_filter * filter,unsigned long flags)168048a1084aSAndrei Vagin static long seccomp_notify_set_flags(struct seccomp_filter *filter,
168148a1084aSAndrei Vagin 				    unsigned long flags)
168248a1084aSAndrei Vagin {
168348a1084aSAndrei Vagin 	long ret;
168448a1084aSAndrei Vagin 
168548a1084aSAndrei Vagin 	if (flags & ~SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
168648a1084aSAndrei Vagin 		return -EINVAL;
168748a1084aSAndrei Vagin 
168848a1084aSAndrei Vagin 	ret = mutex_lock_interruptible(&filter->notify_lock);
168948a1084aSAndrei Vagin 	if (ret < 0)
169048a1084aSAndrei Vagin 		return ret;
169148a1084aSAndrei Vagin 	filter->notif->flags = flags;
169248a1084aSAndrei Vagin 	mutex_unlock(&filter->notify_lock);
169348a1084aSAndrei Vagin 	return 0;
169448a1084aSAndrei Vagin }
169548a1084aSAndrei Vagin 
seccomp_notify_addfd(struct seccomp_filter * filter,struct seccomp_notif_addfd __user * uaddfd,unsigned int size)16967cf97b12SSargun Dhillon static long seccomp_notify_addfd(struct seccomp_filter *filter,
16977cf97b12SSargun Dhillon 				 struct seccomp_notif_addfd __user *uaddfd,
16987cf97b12SSargun Dhillon 				 unsigned int size)
16997cf97b12SSargun Dhillon {
17007cf97b12SSargun Dhillon 	struct seccomp_notif_addfd addfd;
17017cf97b12SSargun Dhillon 	struct seccomp_knotif *knotif;
17027cf97b12SSargun Dhillon 	struct seccomp_kaddfd kaddfd;
17037cf97b12SSargun Dhillon 	int ret;
17047cf97b12SSargun Dhillon 
17057cf97b12SSargun Dhillon 	BUILD_BUG_ON(sizeof(addfd) < SECCOMP_NOTIFY_ADDFD_SIZE_VER0);
17067cf97b12SSargun Dhillon 	BUILD_BUG_ON(sizeof(addfd) != SECCOMP_NOTIFY_ADDFD_SIZE_LATEST);
17077cf97b12SSargun Dhillon 
17087cf97b12SSargun Dhillon 	if (size < SECCOMP_NOTIFY_ADDFD_SIZE_VER0 || size >= PAGE_SIZE)
17097cf97b12SSargun Dhillon 		return -EINVAL;
17107cf97b12SSargun Dhillon 
17117cf97b12SSargun Dhillon 	ret = copy_struct_from_user(&addfd, sizeof(addfd), uaddfd, size);
17127cf97b12SSargun Dhillon 	if (ret)
17137cf97b12SSargun Dhillon 		return ret;
17147cf97b12SSargun Dhillon 
17157cf97b12SSargun Dhillon 	if (addfd.newfd_flags & ~O_CLOEXEC)
17167cf97b12SSargun Dhillon 		return -EINVAL;
17177cf97b12SSargun Dhillon 
17180ae71c77SRodrigo Campos 	if (addfd.flags & ~(SECCOMP_ADDFD_FLAG_SETFD | SECCOMP_ADDFD_FLAG_SEND))
17197cf97b12SSargun Dhillon 		return -EINVAL;
17207cf97b12SSargun Dhillon 
17217cf97b12SSargun Dhillon 	if (addfd.newfd && !(addfd.flags & SECCOMP_ADDFD_FLAG_SETFD))
17227cf97b12SSargun Dhillon 		return -EINVAL;
17237cf97b12SSargun Dhillon 
17247cf97b12SSargun Dhillon 	kaddfd.file = fget(addfd.srcfd);
17257cf97b12SSargun Dhillon 	if (!kaddfd.file)
17267cf97b12SSargun Dhillon 		return -EBADF;
17277cf97b12SSargun Dhillon 
17280ae71c77SRodrigo Campos 	kaddfd.ioctl_flags = addfd.flags;
17297cf97b12SSargun Dhillon 	kaddfd.flags = addfd.newfd_flags;
173042eb0d54SChristoph Hellwig 	kaddfd.setfd = addfd.flags & SECCOMP_ADDFD_FLAG_SETFD;
173142eb0d54SChristoph Hellwig 	kaddfd.fd = addfd.newfd;
17327cf97b12SSargun Dhillon 	init_completion(&kaddfd.completion);
17337cf97b12SSargun Dhillon 
17347cf97b12SSargun Dhillon 	ret = mutex_lock_interruptible(&filter->notify_lock);
17357cf97b12SSargun Dhillon 	if (ret < 0)
17367cf97b12SSargun Dhillon 		goto out;
17377cf97b12SSargun Dhillon 
17387cf97b12SSargun Dhillon 	knotif = find_notification(filter, addfd.id);
17397cf97b12SSargun Dhillon 	if (!knotif) {
17407cf97b12SSargun Dhillon 		ret = -ENOENT;
17417cf97b12SSargun Dhillon 		goto out_unlock;
17427cf97b12SSargun Dhillon 	}
17437cf97b12SSargun Dhillon 
17447cf97b12SSargun Dhillon 	/*
17457cf97b12SSargun Dhillon 	 * We do not want to allow for FD injection to occur before the
17467cf97b12SSargun Dhillon 	 * notification has been picked up by a userspace handler, or after
17477cf97b12SSargun Dhillon 	 * the notification has been replied to.
17487cf97b12SSargun Dhillon 	 */
17497cf97b12SSargun Dhillon 	if (knotif->state != SECCOMP_NOTIFY_SENT) {
17507cf97b12SSargun Dhillon 		ret = -EINPROGRESS;
17517cf97b12SSargun Dhillon 		goto out_unlock;
17527cf97b12SSargun Dhillon 	}
17537cf97b12SSargun Dhillon 
17540ae71c77SRodrigo Campos 	if (addfd.flags & SECCOMP_ADDFD_FLAG_SEND) {
17550ae71c77SRodrigo Campos 		/*
17560ae71c77SRodrigo Campos 		 * Disallow queuing an atomic addfd + send reply while there are
17570ae71c77SRodrigo Campos 		 * some addfd requests still to process.
17580ae71c77SRodrigo Campos 		 *
17590ae71c77SRodrigo Campos 		 * There is no clear reason to support it and allows us to keep
17600ae71c77SRodrigo Campos 		 * the loop on the other side straight-forward.
17610ae71c77SRodrigo Campos 		 */
17620ae71c77SRodrigo Campos 		if (!list_empty(&knotif->addfd)) {
17630ae71c77SRodrigo Campos 			ret = -EBUSY;
17640ae71c77SRodrigo Campos 			goto out_unlock;
17650ae71c77SRodrigo Campos 		}
17660ae71c77SRodrigo Campos 
17670ae71c77SRodrigo Campos 		/* Allow exactly only one reply */
17680ae71c77SRodrigo Campos 		knotif->state = SECCOMP_NOTIFY_REPLIED;
17690ae71c77SRodrigo Campos 	}
17700ae71c77SRodrigo Campos 
17717cf97b12SSargun Dhillon 	list_add(&kaddfd.list, &knotif->addfd);
17727cf97b12SSargun Dhillon 	complete(&knotif->ready);
17737cf97b12SSargun Dhillon 	mutex_unlock(&filter->notify_lock);
17747cf97b12SSargun Dhillon 
17757cf97b12SSargun Dhillon 	/* Now we wait for it to be processed or be interrupted */
17767cf97b12SSargun Dhillon 	ret = wait_for_completion_interruptible(&kaddfd.completion);
17777cf97b12SSargun Dhillon 	if (ret == 0) {
17787cf97b12SSargun Dhillon 		/*
17797cf97b12SSargun Dhillon 		 * We had a successful completion. The other side has already
17807cf97b12SSargun Dhillon 		 * removed us from the addfd queue, and
17817cf97b12SSargun Dhillon 		 * wait_for_completion_interruptible has a memory barrier upon
17827cf97b12SSargun Dhillon 		 * success that lets us read this value directly without
17837cf97b12SSargun Dhillon 		 * locking.
17847cf97b12SSargun Dhillon 		 */
17857cf97b12SSargun Dhillon 		ret = kaddfd.ret;
17867cf97b12SSargun Dhillon 		goto out;
17877cf97b12SSargun Dhillon 	}
17887cf97b12SSargun Dhillon 
17897cf97b12SSargun Dhillon 	mutex_lock(&filter->notify_lock);
17907cf97b12SSargun Dhillon 	/*
17917cf97b12SSargun Dhillon 	 * Even though we were woken up by a signal and not a successful
17927cf97b12SSargun Dhillon 	 * completion, a completion may have happened in the mean time.
17937cf97b12SSargun Dhillon 	 *
17947cf97b12SSargun Dhillon 	 * We need to check again if the addfd request has been handled,
17957cf97b12SSargun Dhillon 	 * and if not, we will remove it from the queue.
17967cf97b12SSargun Dhillon 	 */
17977cf97b12SSargun Dhillon 	if (list_empty(&kaddfd.list))
17987cf97b12SSargun Dhillon 		ret = kaddfd.ret;
17997cf97b12SSargun Dhillon 	else
18007cf97b12SSargun Dhillon 		list_del(&kaddfd.list);
18017cf97b12SSargun Dhillon 
18027cf97b12SSargun Dhillon out_unlock:
18037cf97b12SSargun Dhillon 	mutex_unlock(&filter->notify_lock);
18047cf97b12SSargun Dhillon out:
18057cf97b12SSargun Dhillon 	fput(kaddfd.file);
18067cf97b12SSargun Dhillon 
18077cf97b12SSargun Dhillon 	return ret;
18087cf97b12SSargun Dhillon }
18097cf97b12SSargun Dhillon 
seccomp_notify_ioctl(struct file * file,unsigned int cmd,unsigned long arg)18106a21cc50STycho Andersen static long seccomp_notify_ioctl(struct file *file, unsigned int cmd,
18116a21cc50STycho Andersen 				 unsigned long arg)
18126a21cc50STycho Andersen {
18136a21cc50STycho Andersen 	struct seccomp_filter *filter = file->private_data;
18146a21cc50STycho Andersen 	void __user *buf = (void __user *)arg;
18156a21cc50STycho Andersen 
18167cf97b12SSargun Dhillon 	/* Fixed-size ioctls */
18176a21cc50STycho Andersen 	switch (cmd) {
18186a21cc50STycho Andersen 	case SECCOMP_IOCTL_NOTIF_RECV:
18196a21cc50STycho Andersen 		return seccomp_notify_recv(filter, buf);
18206a21cc50STycho Andersen 	case SECCOMP_IOCTL_NOTIF_SEND:
18216a21cc50STycho Andersen 		return seccomp_notify_send(filter, buf);
182247e33c05SKees Cook 	case SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR:
18236a21cc50STycho Andersen 	case SECCOMP_IOCTL_NOTIF_ID_VALID:
18246a21cc50STycho Andersen 		return seccomp_notify_id_valid(filter, buf);
182548a1084aSAndrei Vagin 	case SECCOMP_IOCTL_NOTIF_SET_FLAGS:
182648a1084aSAndrei Vagin 		return seccomp_notify_set_flags(filter, arg);
18277cf97b12SSargun Dhillon 	}
18287cf97b12SSargun Dhillon 
18297cf97b12SSargun Dhillon 	/* Extensible Argument ioctls */
18307cf97b12SSargun Dhillon #define EA_IOCTL(cmd)	((cmd) & ~(IOC_INOUT | IOCSIZE_MASK))
18317cf97b12SSargun Dhillon 	switch (EA_IOCTL(cmd)) {
18327cf97b12SSargun Dhillon 	case EA_IOCTL(SECCOMP_IOCTL_NOTIF_ADDFD):
18337cf97b12SSargun Dhillon 		return seccomp_notify_addfd(filter, buf, _IOC_SIZE(cmd));
18346a21cc50STycho Andersen 	default:
18356a21cc50STycho Andersen 		return -EINVAL;
18366a21cc50STycho Andersen 	}
18376a21cc50STycho Andersen }
18386a21cc50STycho Andersen 
seccomp_notify_poll(struct file * file,struct poll_table_struct * poll_tab)18396a21cc50STycho Andersen static __poll_t seccomp_notify_poll(struct file *file,
18406a21cc50STycho Andersen 				    struct poll_table_struct *poll_tab)
18416a21cc50STycho Andersen {
18426a21cc50STycho Andersen 	struct seccomp_filter *filter = file->private_data;
18436a21cc50STycho Andersen 	__poll_t ret = 0;
18446a21cc50STycho Andersen 	struct seccomp_knotif *cur;
18456a21cc50STycho Andersen 
184676194c4eSChristian Brauner 	poll_wait(file, &filter->wqh, poll_tab);
18476a21cc50STycho Andersen 
1848319deec7STycho Andersen 	if (mutex_lock_interruptible(&filter->notify_lock) < 0)
18496a21cc50STycho Andersen 		return EPOLLERR;
18506a21cc50STycho Andersen 
18516a21cc50STycho Andersen 	list_for_each_entry(cur, &filter->notif->notifications, list) {
18526a21cc50STycho Andersen 		if (cur->state == SECCOMP_NOTIFY_INIT)
18536a21cc50STycho Andersen 			ret |= EPOLLIN | EPOLLRDNORM;
18546a21cc50STycho Andersen 		if (cur->state == SECCOMP_NOTIFY_SENT)
18556a21cc50STycho Andersen 			ret |= EPOLLOUT | EPOLLWRNORM;
18566a21cc50STycho Andersen 		if ((ret & EPOLLIN) && (ret & EPOLLOUT))
18576a21cc50STycho Andersen 			break;
18586a21cc50STycho Andersen 	}
18596a21cc50STycho Andersen 
18606a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
18616a21cc50STycho Andersen 
186299cdb8b9SChristian Brauner 	if (refcount_read(&filter->users) == 0)
186399cdb8b9SChristian Brauner 		ret |= EPOLLHUP;
186499cdb8b9SChristian Brauner 
18656a21cc50STycho Andersen 	return ret;
18666a21cc50STycho Andersen }
18676a21cc50STycho Andersen 
18686a21cc50STycho Andersen static const struct file_operations seccomp_notify_ops = {
18696a21cc50STycho Andersen 	.poll = seccomp_notify_poll,
18706a21cc50STycho Andersen 	.release = seccomp_notify_release,
18716a21cc50STycho Andersen 	.unlocked_ioctl = seccomp_notify_ioctl,
18723db81afdSSven Schnelle 	.compat_ioctl = seccomp_notify_ioctl,
18736a21cc50STycho Andersen };
18746a21cc50STycho Andersen 
init_listener(struct seccomp_filter * filter)18756a21cc50STycho Andersen static struct file *init_listener(struct seccomp_filter *filter)
18766a21cc50STycho Andersen {
1877dfe719feSJann Horn 	struct file *ret;
18786a21cc50STycho Andersen 
18796a21cc50STycho Andersen 	ret = ERR_PTR(-ENOMEM);
18806a21cc50STycho Andersen 	filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL);
18816a21cc50STycho Andersen 	if (!filter->notif)
18826a21cc50STycho Andersen 		goto out;
18836a21cc50STycho Andersen 
18846a21cc50STycho Andersen 	filter->notif->next_id = get_random_u64();
18856a21cc50STycho Andersen 	INIT_LIST_HEAD(&filter->notif->notifications);
18866a21cc50STycho Andersen 
18876a21cc50STycho Andersen 	ret = anon_inode_getfile("seccomp notify", &seccomp_notify_ops,
18886a21cc50STycho Andersen 				 filter, O_RDWR);
18896a21cc50STycho Andersen 	if (IS_ERR(ret))
18906a21cc50STycho Andersen 		goto out_notif;
18916a21cc50STycho Andersen 
18926a21cc50STycho Andersen 	/* The file has a reference to it now */
18936a21cc50STycho Andersen 	__get_seccomp_filter(filter);
18946a21cc50STycho Andersen 
18956a21cc50STycho Andersen out_notif:
18966a21cc50STycho Andersen 	if (IS_ERR(ret))
1897e8393179STycho Andersen 		seccomp_notify_free(filter);
18986a21cc50STycho Andersen out:
18996a21cc50STycho Andersen 	return ret;
19006a21cc50STycho Andersen }
19016a21cc50STycho Andersen 
1902dfe719feSJann Horn /*
1903dfe719feSJann Horn  * Does @new_child have a listener while an ancestor also has a listener?
1904dfe719feSJann Horn  * If so, we'll want to reject this filter.
1905dfe719feSJann Horn  * This only has to be tested for the current process, even in the TSYNC case,
1906dfe719feSJann Horn  * because TSYNC installs @child with the same parent on all threads.
1907dfe719feSJann Horn  * Note that @new_child is not hooked up to its parent at this point yet, so
1908dfe719feSJann Horn  * we use current->seccomp.filter.
1909dfe719feSJann Horn  */
has_duplicate_listener(struct seccomp_filter * new_child)1910dfe719feSJann Horn static bool has_duplicate_listener(struct seccomp_filter *new_child)
1911dfe719feSJann Horn {
1912dfe719feSJann Horn 	struct seccomp_filter *cur;
1913dfe719feSJann Horn 
1914dfe719feSJann Horn 	/* must be protected against concurrent TSYNC */
1915dfe719feSJann Horn 	lockdep_assert_held(&current->sighand->siglock);
1916dfe719feSJann Horn 
1917dfe719feSJann Horn 	if (!new_child->notif)
1918dfe719feSJann Horn 		return false;
1919dfe719feSJann Horn 	for (cur = current->seccomp.filter; cur; cur = cur->prev) {
1920dfe719feSJann Horn 		if (cur->notif)
1921dfe719feSJann Horn 			return true;
1922dfe719feSJann Horn 	}
1923dfe719feSJann Horn 
1924dfe719feSJann Horn 	return false;
1925dfe719feSJann Horn }
1926dfe719feSJann Horn 
19273b23dd12SKees Cook /**
19283b23dd12SKees Cook  * seccomp_set_mode_filter: internal function for setting seccomp filter
192948dc92b9SKees Cook  * @flags:  flags to change filter behavior
19303b23dd12SKees Cook  * @filter: struct sock_fprog containing filter
19313b23dd12SKees Cook  *
19323b23dd12SKees Cook  * This function may be called repeatedly to install additional filters.
19333b23dd12SKees Cook  * Every filter successfully installed will be evaluated (in reverse order)
19343b23dd12SKees Cook  * for each system call the task makes.
19353b23dd12SKees Cook  *
19363b23dd12SKees Cook  * Once current->seccomp.mode is non-zero, it may not be changed.
19373b23dd12SKees Cook  *
19383b23dd12SKees Cook  * Returns 0 on success or -EINVAL on failure.
19393b23dd12SKees Cook  */
seccomp_set_mode_filter(unsigned int flags,const char __user * filter)194048dc92b9SKees Cook static long seccomp_set_mode_filter(unsigned int flags,
194148dc92b9SKees Cook 				    const char __user *filter)
19423b23dd12SKees Cook {
19433b23dd12SKees Cook 	const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
1944c8bee430SKees Cook 	struct seccomp_filter *prepared = NULL;
19453b23dd12SKees Cook 	long ret = -EINVAL;
19466a21cc50STycho Andersen 	int listener = -1;
19476a21cc50STycho Andersen 	struct file *listener_f = NULL;
19483b23dd12SKees Cook 
194948dc92b9SKees Cook 	/* Validate flags. */
1950c2e1f2e3SKees Cook 	if (flags & ~SECCOMP_FILTER_FLAG_MASK)
1951dbd95212SKees Cook 		return -EINVAL;
195248dc92b9SKees Cook 
19537a0df7fbSTycho Andersen 	/*
19547a0df7fbSTycho Andersen 	 * In the successful case, NEW_LISTENER returns the new listener fd.
19557a0df7fbSTycho Andersen 	 * But in the failure case, TSYNC returns the thread that died. If you
19567a0df7fbSTycho Andersen 	 * combine these two flags, there's no way to tell whether something
195751891498STycho Andersen 	 * succeeded or failed. So, let's disallow this combination if the user
195851891498STycho Andersen 	 * has not explicitly requested no errors from TSYNC.
19597a0df7fbSTycho Andersen 	 */
19607a0df7fbSTycho Andersen 	if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
196151891498STycho Andersen 	    (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) &&
196251891498STycho Andersen 	    ((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0))
19637a0df7fbSTycho Andersen 		return -EINVAL;
19647a0df7fbSTycho Andersen 
1965c2aa2dfeSSargun Dhillon 	/*
1966c2aa2dfeSSargun Dhillon 	 * The SECCOMP_FILTER_FLAG_WAIT_KILLABLE_SENT flag doesn't make sense
1967c2aa2dfeSSargun Dhillon 	 * without the SECCOMP_FILTER_FLAG_NEW_LISTENER flag.
1968c2aa2dfeSSargun Dhillon 	 */
1969c2aa2dfeSSargun Dhillon 	if ((flags & SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV) &&
1970c2aa2dfeSSargun Dhillon 	    ((flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) == 0))
1971c2aa2dfeSSargun Dhillon 		return -EINVAL;
1972c2aa2dfeSSargun Dhillon 
1973c8bee430SKees Cook 	/* Prepare the new filter before holding any locks. */
1974c8bee430SKees Cook 	prepared = seccomp_prepare_user_filter(filter);
1975c8bee430SKees Cook 	if (IS_ERR(prepared))
1976c8bee430SKees Cook 		return PTR_ERR(prepared);
1977c8bee430SKees Cook 
19786a21cc50STycho Andersen 	if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
19796a21cc50STycho Andersen 		listener = get_unused_fd_flags(O_CLOEXEC);
19806a21cc50STycho Andersen 		if (listener < 0) {
19816a21cc50STycho Andersen 			ret = listener;
19826a21cc50STycho Andersen 			goto out_free;
19836a21cc50STycho Andersen 		}
19846a21cc50STycho Andersen 
19856a21cc50STycho Andersen 		listener_f = init_listener(prepared);
19866a21cc50STycho Andersen 		if (IS_ERR(listener_f)) {
19876a21cc50STycho Andersen 			put_unused_fd(listener);
19886a21cc50STycho Andersen 			ret = PTR_ERR(listener_f);
19896a21cc50STycho Andersen 			goto out_free;
19906a21cc50STycho Andersen 		}
19916a21cc50STycho Andersen 	}
19926a21cc50STycho Andersen 
1993c2e1f2e3SKees Cook 	/*
1994c2e1f2e3SKees Cook 	 * Make sure we cannot change seccomp or nnp state via TSYNC
1995c2e1f2e3SKees Cook 	 * while another thread is in the middle of calling exec.
1996c2e1f2e3SKees Cook 	 */
1997c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
1998c2e1f2e3SKees Cook 	    mutex_lock_killable(&current->signal->cred_guard_mutex))
19996a21cc50STycho Andersen 		goto out_put_fd;
2000c2e1f2e3SKees Cook 
2001dbd95212SKees Cook 	spin_lock_irq(&current->sighand->siglock);
2002dbd95212SKees Cook 
20033b23dd12SKees Cook 	if (!seccomp_may_assign_mode(seccomp_mode))
20043b23dd12SKees Cook 		goto out;
20053b23dd12SKees Cook 
2006dfe719feSJann Horn 	if (has_duplicate_listener(prepared)) {
2007dfe719feSJann Horn 		ret = -EBUSY;
2008dfe719feSJann Horn 		goto out;
2009dfe719feSJann Horn 	}
2010dfe719feSJann Horn 
2011c8bee430SKees Cook 	ret = seccomp_attach_filter(flags, prepared);
2012e2cfabdfSWill Drewry 	if (ret)
2013e2cfabdfSWill Drewry 		goto out;
2014c8bee430SKees Cook 	/* Do not free the successfully attached filter. */
2015c8bee430SKees Cook 	prepared = NULL;
20161d9d02feSAndrea Arcangeli 
201700a02d0cSKees Cook 	seccomp_assign_mode(current, seccomp_mode, flags);
20181d9d02feSAndrea Arcangeli out:
2019dbd95212SKees Cook 	spin_unlock_irq(&current->sighand->siglock);
2020c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC)
2021c2e1f2e3SKees Cook 		mutex_unlock(&current->signal->cred_guard_mutex);
20226a21cc50STycho Andersen out_put_fd:
20236a21cc50STycho Andersen 	if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
20247a0df7fbSTycho Andersen 		if (ret) {
2025a811dc61STycho Andersen 			listener_f->private_data = NULL;
20266a21cc50STycho Andersen 			fput(listener_f);
20276a21cc50STycho Andersen 			put_unused_fd(listener);
2028a566a901STycho Andersen 			seccomp_notify_detach(prepared);
20296a21cc50STycho Andersen 		} else {
20306a21cc50STycho Andersen 			fd_install(listener, listener_f);
20316a21cc50STycho Andersen 			ret = listener;
20326a21cc50STycho Andersen 		}
20336a21cc50STycho Andersen 	}
2034c2e1f2e3SKees Cook out_free:
2035c8bee430SKees Cook 	seccomp_filter_free(prepared);
20361d9d02feSAndrea Arcangeli 	return ret;
20371d9d02feSAndrea Arcangeli }
20383b23dd12SKees Cook #else
seccomp_set_mode_filter(unsigned int flags,const char __user * filter)203948dc92b9SKees Cook static inline long seccomp_set_mode_filter(unsigned int flags,
204048dc92b9SKees Cook 					   const char __user *filter)
20413b23dd12SKees Cook {
20423b23dd12SKees Cook 	return -EINVAL;
20433b23dd12SKees Cook }
20443b23dd12SKees Cook #endif
2045d78ab02cSKees Cook 
seccomp_get_action_avail(const char __user * uaction)2046d612b1fdSTyler Hicks static long seccomp_get_action_avail(const char __user *uaction)
2047d612b1fdSTyler Hicks {
2048d612b1fdSTyler Hicks 	u32 action;
2049d612b1fdSTyler Hicks 
2050d612b1fdSTyler Hicks 	if (copy_from_user(&action, uaction, sizeof(action)))
2051d612b1fdSTyler Hicks 		return -EFAULT;
2052d612b1fdSTyler Hicks 
2053d612b1fdSTyler Hicks 	switch (action) {
20540466bdb9SKees Cook 	case SECCOMP_RET_KILL_PROCESS:
2055fd76875cSKees Cook 	case SECCOMP_RET_KILL_THREAD:
2056d612b1fdSTyler Hicks 	case SECCOMP_RET_TRAP:
2057d612b1fdSTyler Hicks 	case SECCOMP_RET_ERRNO:
20586a21cc50STycho Andersen 	case SECCOMP_RET_USER_NOTIF:
2059d612b1fdSTyler Hicks 	case SECCOMP_RET_TRACE:
206059f5cf44STyler Hicks 	case SECCOMP_RET_LOG:
2061d612b1fdSTyler Hicks 	case SECCOMP_RET_ALLOW:
2062d612b1fdSTyler Hicks 		break;
2063d612b1fdSTyler Hicks 	default:
2064d612b1fdSTyler Hicks 		return -EOPNOTSUPP;
2065d612b1fdSTyler Hicks 	}
2066d612b1fdSTyler Hicks 
2067d612b1fdSTyler Hicks 	return 0;
2068d612b1fdSTyler Hicks }
2069d612b1fdSTyler Hicks 
seccomp_get_notif_sizes(void __user * usizes)20706a21cc50STycho Andersen static long seccomp_get_notif_sizes(void __user *usizes)
20716a21cc50STycho Andersen {
20726a21cc50STycho Andersen 	struct seccomp_notif_sizes sizes = {
20736a21cc50STycho Andersen 		.seccomp_notif = sizeof(struct seccomp_notif),
20746a21cc50STycho Andersen 		.seccomp_notif_resp = sizeof(struct seccomp_notif_resp),
20756a21cc50STycho Andersen 		.seccomp_data = sizeof(struct seccomp_data),
20766a21cc50STycho Andersen 	};
20776a21cc50STycho Andersen 
20786a21cc50STycho Andersen 	if (copy_to_user(usizes, &sizes, sizeof(sizes)))
20796a21cc50STycho Andersen 		return -EFAULT;
20806a21cc50STycho Andersen 
20816a21cc50STycho Andersen 	return 0;
20826a21cc50STycho Andersen }
20836a21cc50STycho Andersen 
208448dc92b9SKees Cook /* Common entry point for both prctl and syscall. */
do_seccomp(unsigned int op,unsigned int flags,void __user * uargs)208548dc92b9SKees Cook static long do_seccomp(unsigned int op, unsigned int flags,
2086a5662e4dSTycho Andersen 		       void __user *uargs)
208748dc92b9SKees Cook {
208848dc92b9SKees Cook 	switch (op) {
208948dc92b9SKees Cook 	case SECCOMP_SET_MODE_STRICT:
209048dc92b9SKees Cook 		if (flags != 0 || uargs != NULL)
209148dc92b9SKees Cook 			return -EINVAL;
209248dc92b9SKees Cook 		return seccomp_set_mode_strict();
209348dc92b9SKees Cook 	case SECCOMP_SET_MODE_FILTER:
209448dc92b9SKees Cook 		return seccomp_set_mode_filter(flags, uargs);
2095d612b1fdSTyler Hicks 	case SECCOMP_GET_ACTION_AVAIL:
2096d612b1fdSTyler Hicks 		if (flags != 0)
2097d612b1fdSTyler Hicks 			return -EINVAL;
2098d612b1fdSTyler Hicks 
2099d612b1fdSTyler Hicks 		return seccomp_get_action_avail(uargs);
21006a21cc50STycho Andersen 	case SECCOMP_GET_NOTIF_SIZES:
21016a21cc50STycho Andersen 		if (flags != 0)
21026a21cc50STycho Andersen 			return -EINVAL;
21036a21cc50STycho Andersen 
21046a21cc50STycho Andersen 		return seccomp_get_notif_sizes(uargs);
210548dc92b9SKees Cook 	default:
210648dc92b9SKees Cook 		return -EINVAL;
210748dc92b9SKees Cook 	}
210848dc92b9SKees Cook }
210948dc92b9SKees Cook 
SYSCALL_DEFINE3(seccomp,unsigned int,op,unsigned int,flags,void __user *,uargs)211048dc92b9SKees Cook SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
2111a5662e4dSTycho Andersen 			 void __user *, uargs)
211248dc92b9SKees Cook {
211348dc92b9SKees Cook 	return do_seccomp(op, flags, uargs);
211448dc92b9SKees Cook }
211548dc92b9SKees Cook 
2116d78ab02cSKees Cook /**
2117d78ab02cSKees Cook  * prctl_set_seccomp: configures current->seccomp.mode
2118d78ab02cSKees Cook  * @seccomp_mode: requested mode to use
2119d78ab02cSKees Cook  * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
2120d78ab02cSKees Cook  *
2121d78ab02cSKees Cook  * Returns 0 on success or -EINVAL on failure.
2122d78ab02cSKees Cook  */
prctl_set_seccomp(unsigned long seccomp_mode,void __user * filter)2123a5662e4dSTycho Andersen long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter)
2124d78ab02cSKees Cook {
212548dc92b9SKees Cook 	unsigned int op;
2126a5662e4dSTycho Andersen 	void __user *uargs;
212748dc92b9SKees Cook 
21283b23dd12SKees Cook 	switch (seccomp_mode) {
21293b23dd12SKees Cook 	case SECCOMP_MODE_STRICT:
213048dc92b9SKees Cook 		op = SECCOMP_SET_MODE_STRICT;
213148dc92b9SKees Cook 		/*
213248dc92b9SKees Cook 		 * Setting strict mode through prctl always ignored filter,
213348dc92b9SKees Cook 		 * so make sure it is always NULL here to pass the internal
213448dc92b9SKees Cook 		 * check in do_seccomp().
213548dc92b9SKees Cook 		 */
213648dc92b9SKees Cook 		uargs = NULL;
213748dc92b9SKees Cook 		break;
21383b23dd12SKees Cook 	case SECCOMP_MODE_FILTER:
213948dc92b9SKees Cook 		op = SECCOMP_SET_MODE_FILTER;
214048dc92b9SKees Cook 		uargs = filter;
214148dc92b9SKees Cook 		break;
21423b23dd12SKees Cook 	default:
21433b23dd12SKees Cook 		return -EINVAL;
21443b23dd12SKees Cook 	}
214548dc92b9SKees Cook 
214648dc92b9SKees Cook 	/* prctl interface doesn't have flags, so they are always zero. */
214748dc92b9SKees Cook 	return do_seccomp(op, 0, uargs);
2148d78ab02cSKees Cook }
2149f8e529edSTycho Andersen 
2150f8e529edSTycho Andersen #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
get_nth_filter(struct task_struct * task,unsigned long filter_off)2151f06eae83STycho Andersen static struct seccomp_filter *get_nth_filter(struct task_struct *task,
2152f06eae83STycho Andersen 					     unsigned long filter_off)
2153f06eae83STycho Andersen {
2154f06eae83STycho Andersen 	struct seccomp_filter *orig, *filter;
2155f06eae83STycho Andersen 	unsigned long count;
2156f06eae83STycho Andersen 
2157f06eae83STycho Andersen 	/*
2158f06eae83STycho Andersen 	 * Note: this is only correct because the caller should be the (ptrace)
2159f06eae83STycho Andersen 	 * tracer of the task, otherwise lock_task_sighand is needed.
2160f06eae83STycho Andersen 	 */
2161f06eae83STycho Andersen 	spin_lock_irq(&task->sighand->siglock);
2162f06eae83STycho Andersen 
2163f06eae83STycho Andersen 	if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
2164f06eae83STycho Andersen 		spin_unlock_irq(&task->sighand->siglock);
2165f06eae83STycho Andersen 		return ERR_PTR(-EINVAL);
2166f06eae83STycho Andersen 	}
2167f06eae83STycho Andersen 
2168f06eae83STycho Andersen 	orig = task->seccomp.filter;
2169f06eae83STycho Andersen 	__get_seccomp_filter(orig);
2170f06eae83STycho Andersen 	spin_unlock_irq(&task->sighand->siglock);
2171f06eae83STycho Andersen 
2172f06eae83STycho Andersen 	count = 0;
2173f06eae83STycho Andersen 	for (filter = orig; filter; filter = filter->prev)
2174f06eae83STycho Andersen 		count++;
2175f06eae83STycho Andersen 
2176f06eae83STycho Andersen 	if (filter_off >= count) {
2177f06eae83STycho Andersen 		filter = ERR_PTR(-ENOENT);
2178f06eae83STycho Andersen 		goto out;
2179f06eae83STycho Andersen 	}
2180f06eae83STycho Andersen 
2181f06eae83STycho Andersen 	count -= filter_off;
2182f06eae83STycho Andersen 	for (filter = orig; filter && count > 1; filter = filter->prev)
2183f06eae83STycho Andersen 		count--;
2184f06eae83STycho Andersen 
2185f06eae83STycho Andersen 	if (WARN_ON(count != 1 || !filter)) {
2186f06eae83STycho Andersen 		filter = ERR_PTR(-ENOENT);
2187f06eae83STycho Andersen 		goto out;
2188f06eae83STycho Andersen 	}
2189f06eae83STycho Andersen 
2190f06eae83STycho Andersen 	__get_seccomp_filter(filter);
2191f06eae83STycho Andersen 
2192f06eae83STycho Andersen out:
2193f06eae83STycho Andersen 	__put_seccomp_filter(orig);
2194f06eae83STycho Andersen 	return filter;
2195f06eae83STycho Andersen }
2196f06eae83STycho Andersen 
seccomp_get_filter(struct task_struct * task,unsigned long filter_off,void __user * data)2197f8e529edSTycho Andersen long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
2198f8e529edSTycho Andersen 			void __user *data)
2199f8e529edSTycho Andersen {
2200f8e529edSTycho Andersen 	struct seccomp_filter *filter;
2201f8e529edSTycho Andersen 	struct sock_fprog_kern *fprog;
2202f8e529edSTycho Andersen 	long ret;
2203f8e529edSTycho Andersen 
2204f8e529edSTycho Andersen 	if (!capable(CAP_SYS_ADMIN) ||
2205f8e529edSTycho Andersen 	    current->seccomp.mode != SECCOMP_MODE_DISABLED) {
2206f8e529edSTycho Andersen 		return -EACCES;
2207f8e529edSTycho Andersen 	}
2208f8e529edSTycho Andersen 
2209f06eae83STycho Andersen 	filter = get_nth_filter(task, filter_off);
2210f06eae83STycho Andersen 	if (IS_ERR(filter))
2211f06eae83STycho Andersen 		return PTR_ERR(filter);
2212f8e529edSTycho Andersen 
2213f8e529edSTycho Andersen 	fprog = filter->prog->orig_prog;
2214f8e529edSTycho Andersen 	if (!fprog) {
2215470bf1f2SMickaël Salaün 		/* This must be a new non-cBPF filter, since we save
2216f8e529edSTycho Andersen 		 * every cBPF filter's orig_prog above when
2217f8e529edSTycho Andersen 		 * CONFIG_CHECKPOINT_RESTORE is enabled.
2218f8e529edSTycho Andersen 		 */
2219f8e529edSTycho Andersen 		ret = -EMEDIUMTYPE;
2220f8e529edSTycho Andersen 		goto out;
2221f8e529edSTycho Andersen 	}
2222f8e529edSTycho Andersen 
2223f8e529edSTycho Andersen 	ret = fprog->len;
2224f8e529edSTycho Andersen 	if (!data)
2225f8e529edSTycho Andersen 		goto out;
2226f8e529edSTycho Andersen 
2227f8e529edSTycho Andersen 	if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
2228f8e529edSTycho Andersen 		ret = -EFAULT;
2229f8e529edSTycho Andersen 
2230f8e529edSTycho Andersen out:
22318e5f1ad1STyler Hicks 	__put_seccomp_filter(filter);
22328e5f1ad1STyler Hicks 	return ret;
2233f8e529edSTycho Andersen }
2234f8e529edSTycho Andersen 
seccomp_get_metadata(struct task_struct * task,unsigned long size,void __user * data)223526500475STycho Andersen long seccomp_get_metadata(struct task_struct *task,
223626500475STycho Andersen 			  unsigned long size, void __user *data)
223726500475STycho Andersen {
223826500475STycho Andersen 	long ret;
223926500475STycho Andersen 	struct seccomp_filter *filter;
224026500475STycho Andersen 	struct seccomp_metadata kmd = {};
224126500475STycho Andersen 
224226500475STycho Andersen 	if (!capable(CAP_SYS_ADMIN) ||
224326500475STycho Andersen 	    current->seccomp.mode != SECCOMP_MODE_DISABLED) {
224426500475STycho Andersen 		return -EACCES;
224526500475STycho Andersen 	}
224626500475STycho Andersen 
224726500475STycho Andersen 	size = min_t(unsigned long, size, sizeof(kmd));
224826500475STycho Andersen 
224963bb0045STycho Andersen 	if (size < sizeof(kmd.filter_off))
225063bb0045STycho Andersen 		return -EINVAL;
225163bb0045STycho Andersen 
225263bb0045STycho Andersen 	if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
225326500475STycho Andersen 		return -EFAULT;
225426500475STycho Andersen 
225526500475STycho Andersen 	filter = get_nth_filter(task, kmd.filter_off);
225626500475STycho Andersen 	if (IS_ERR(filter))
225726500475STycho Andersen 		return PTR_ERR(filter);
225826500475STycho Andersen 
225926500475STycho Andersen 	if (filter->log)
226026500475STycho Andersen 		kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
226126500475STycho Andersen 
226226500475STycho Andersen 	ret = size;
226326500475STycho Andersen 	if (copy_to_user(data, &kmd, size))
226426500475STycho Andersen 		ret = -EFAULT;
226526500475STycho Andersen 
226626500475STycho Andersen 	__put_seccomp_filter(filter);
2267f8e529edSTycho Andersen 	return ret;
2268f8e529edSTycho Andersen }
2269f8e529edSTycho Andersen #endif
22708e5f1ad1STyler Hicks 
22718e5f1ad1STyler Hicks #ifdef CONFIG_SYSCTL
22728e5f1ad1STyler Hicks 
22738e5f1ad1STyler Hicks /* Human readable action names for friendly sysctl interaction */
22740466bdb9SKees Cook #define SECCOMP_RET_KILL_PROCESS_NAME	"kill_process"
2275fd76875cSKees Cook #define SECCOMP_RET_KILL_THREAD_NAME	"kill_thread"
22768e5f1ad1STyler Hicks #define SECCOMP_RET_TRAP_NAME		"trap"
22778e5f1ad1STyler Hicks #define SECCOMP_RET_ERRNO_NAME		"errno"
22786a21cc50STycho Andersen #define SECCOMP_RET_USER_NOTIF_NAME	"user_notif"
22798e5f1ad1STyler Hicks #define SECCOMP_RET_TRACE_NAME		"trace"
228059f5cf44STyler Hicks #define SECCOMP_RET_LOG_NAME		"log"
22818e5f1ad1STyler Hicks #define SECCOMP_RET_ALLOW_NAME		"allow"
22828e5f1ad1STyler Hicks 
2283fd76875cSKees Cook static const char seccomp_actions_avail[] =
22840466bdb9SKees Cook 				SECCOMP_RET_KILL_PROCESS_NAME	" "
2285fd76875cSKees Cook 				SECCOMP_RET_KILL_THREAD_NAME	" "
22868e5f1ad1STyler Hicks 				SECCOMP_RET_TRAP_NAME		" "
22878e5f1ad1STyler Hicks 				SECCOMP_RET_ERRNO_NAME		" "
22886a21cc50STycho Andersen 				SECCOMP_RET_USER_NOTIF_NAME     " "
22898e5f1ad1STyler Hicks 				SECCOMP_RET_TRACE_NAME		" "
229059f5cf44STyler Hicks 				SECCOMP_RET_LOG_NAME		" "
22918e5f1ad1STyler Hicks 				SECCOMP_RET_ALLOW_NAME;
22928e5f1ad1STyler Hicks 
22930ddec0fcSTyler Hicks struct seccomp_log_name {
22940ddec0fcSTyler Hicks 	u32		log;
22950ddec0fcSTyler Hicks 	const char	*name;
22960ddec0fcSTyler Hicks };
22970ddec0fcSTyler Hicks 
22980ddec0fcSTyler Hicks static const struct seccomp_log_name seccomp_log_names[] = {
22990466bdb9SKees Cook 	{ SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
2300fd76875cSKees Cook 	{ SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
23010ddec0fcSTyler Hicks 	{ SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
23020ddec0fcSTyler Hicks 	{ SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
23036a21cc50STycho Andersen 	{ SECCOMP_LOG_USER_NOTIF, SECCOMP_RET_USER_NOTIF_NAME },
23040ddec0fcSTyler Hicks 	{ SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
230559f5cf44STyler Hicks 	{ SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
23060ddec0fcSTyler Hicks 	{ SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
23070ddec0fcSTyler Hicks 	{ }
23080ddec0fcSTyler Hicks };
23090ddec0fcSTyler Hicks 
seccomp_names_from_actions_logged(char * names,size_t size,u32 actions_logged,const char * sep)23100ddec0fcSTyler Hicks static bool seccomp_names_from_actions_logged(char *names, size_t size,
2311beb44acaSTyler Hicks 					      u32 actions_logged,
2312beb44acaSTyler Hicks 					      const char *sep)
23130ddec0fcSTyler Hicks {
23140ddec0fcSTyler Hicks 	const struct seccomp_log_name *cur;
2315beb44acaSTyler Hicks 	bool append_sep = false;
23160ddec0fcSTyler Hicks 
23170ddec0fcSTyler Hicks 	for (cur = seccomp_log_names; cur->name && size; cur++) {
23180ddec0fcSTyler Hicks 		ssize_t ret;
23190ddec0fcSTyler Hicks 
23200ddec0fcSTyler Hicks 		if (!(actions_logged & cur->log))
23210ddec0fcSTyler Hicks 			continue;
23220ddec0fcSTyler Hicks 
2323beb44acaSTyler Hicks 		if (append_sep) {
2324beb44acaSTyler Hicks 			ret = strscpy(names, sep, size);
23250ddec0fcSTyler Hicks 			if (ret < 0)
23260ddec0fcSTyler Hicks 				return false;
23270ddec0fcSTyler Hicks 
23280ddec0fcSTyler Hicks 			names += ret;
23290ddec0fcSTyler Hicks 			size -= ret;
23300ddec0fcSTyler Hicks 		} else
2331beb44acaSTyler Hicks 			append_sep = true;
23320ddec0fcSTyler Hicks 
23330ddec0fcSTyler Hicks 		ret = strscpy(names, cur->name, size);
23340ddec0fcSTyler Hicks 		if (ret < 0)
23350ddec0fcSTyler Hicks 			return false;
23360ddec0fcSTyler Hicks 
23370ddec0fcSTyler Hicks 		names += ret;
23380ddec0fcSTyler Hicks 		size -= ret;
23390ddec0fcSTyler Hicks 	}
23400ddec0fcSTyler Hicks 
23410ddec0fcSTyler Hicks 	return true;
23420ddec0fcSTyler Hicks }
23430ddec0fcSTyler Hicks 
seccomp_action_logged_from_name(u32 * action_logged,const char * name)23440ddec0fcSTyler Hicks static bool seccomp_action_logged_from_name(u32 *action_logged,
23450ddec0fcSTyler Hicks 					    const char *name)
23460ddec0fcSTyler Hicks {
23470ddec0fcSTyler Hicks 	const struct seccomp_log_name *cur;
23480ddec0fcSTyler Hicks 
23490ddec0fcSTyler Hicks 	for (cur = seccomp_log_names; cur->name; cur++) {
23500ddec0fcSTyler Hicks 		if (!strcmp(cur->name, name)) {
23510ddec0fcSTyler Hicks 			*action_logged = cur->log;
23520ddec0fcSTyler Hicks 			return true;
23530ddec0fcSTyler Hicks 		}
23540ddec0fcSTyler Hicks 	}
23550ddec0fcSTyler Hicks 
23560ddec0fcSTyler Hicks 	return false;
23570ddec0fcSTyler Hicks }
23580ddec0fcSTyler Hicks 
seccomp_actions_logged_from_names(u32 * actions_logged,char * names)23590ddec0fcSTyler Hicks static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
23600ddec0fcSTyler Hicks {
23610ddec0fcSTyler Hicks 	char *name;
23620ddec0fcSTyler Hicks 
23630ddec0fcSTyler Hicks 	*actions_logged = 0;
23640ddec0fcSTyler Hicks 	while ((name = strsep(&names, " ")) && *name) {
23650ddec0fcSTyler Hicks 		u32 action_logged = 0;
23660ddec0fcSTyler Hicks 
23670ddec0fcSTyler Hicks 		if (!seccomp_action_logged_from_name(&action_logged, name))
23680ddec0fcSTyler Hicks 			return false;
23690ddec0fcSTyler Hicks 
23700ddec0fcSTyler Hicks 		*actions_logged |= action_logged;
23710ddec0fcSTyler Hicks 	}
23720ddec0fcSTyler Hicks 
23730ddec0fcSTyler Hicks 	return true;
23740ddec0fcSTyler Hicks }
23750ddec0fcSTyler Hicks 
read_actions_logged(const struct ctl_table * ro_table,void * buffer,size_t * lenp,loff_t * ppos)2376e406737bSKees Cook static int read_actions_logged(const struct ctl_table *ro_table, void *buffer,
2377d013db02STyler Hicks 			       size_t *lenp, loff_t *ppos)
23780ddec0fcSTyler Hicks {
23790ddec0fcSTyler Hicks 	char names[sizeof(seccomp_actions_avail)];
23800ddec0fcSTyler Hicks 	struct ctl_table table;
23810ddec0fcSTyler Hicks 
23820ddec0fcSTyler Hicks 	memset(names, 0, sizeof(names));
23830ddec0fcSTyler Hicks 
23840ddec0fcSTyler Hicks 	if (!seccomp_names_from_actions_logged(names, sizeof(names),
2385beb44acaSTyler Hicks 					       seccomp_actions_logged, " "))
23860ddec0fcSTyler Hicks 		return -EINVAL;
23870ddec0fcSTyler Hicks 
23880ddec0fcSTyler Hicks 	table = *ro_table;
23890ddec0fcSTyler Hicks 	table.data = names;
23900ddec0fcSTyler Hicks 	table.maxlen = sizeof(names);
2391d013db02STyler Hicks 	return proc_dostring(&table, 0, buffer, lenp, ppos);
2392d013db02STyler Hicks }
2393d013db02STyler Hicks 
write_actions_logged(const struct ctl_table * ro_table,void * buffer,size_t * lenp,loff_t * ppos,u32 * actions_logged)2394e406737bSKees Cook static int write_actions_logged(const struct ctl_table *ro_table, void *buffer,
2395ea6eca77STyler Hicks 				size_t *lenp, loff_t *ppos, u32 *actions_logged)
23960ddec0fcSTyler Hicks {
23970ddec0fcSTyler Hicks 	char names[sizeof(seccomp_actions_avail)];
23980ddec0fcSTyler Hicks 	struct ctl_table table;
23990ddec0fcSTyler Hicks 	int ret;
24000ddec0fcSTyler Hicks 
2401d013db02STyler Hicks 	if (!capable(CAP_SYS_ADMIN))
24020ddec0fcSTyler Hicks 		return -EPERM;
24030ddec0fcSTyler Hicks 
24040ddec0fcSTyler Hicks 	memset(names, 0, sizeof(names));
24050ddec0fcSTyler Hicks 
24060ddec0fcSTyler Hicks 	table = *ro_table;
24070ddec0fcSTyler Hicks 	table.data = names;
24080ddec0fcSTyler Hicks 	table.maxlen = sizeof(names);
2409d013db02STyler Hicks 	ret = proc_dostring(&table, 1, buffer, lenp, ppos);
24100ddec0fcSTyler Hicks 	if (ret)
24110ddec0fcSTyler Hicks 		return ret;
24120ddec0fcSTyler Hicks 
2413ea6eca77STyler Hicks 	if (!seccomp_actions_logged_from_names(actions_logged, table.data))
24140ddec0fcSTyler Hicks 		return -EINVAL;
24150ddec0fcSTyler Hicks 
2416ea6eca77STyler Hicks 	if (*actions_logged & SECCOMP_LOG_ALLOW)
24170ddec0fcSTyler Hicks 		return -EINVAL;
24180ddec0fcSTyler Hicks 
2419ea6eca77STyler Hicks 	seccomp_actions_logged = *actions_logged;
2420d013db02STyler Hicks 	return 0;
24210ddec0fcSTyler Hicks }
24220ddec0fcSTyler Hicks 
audit_actions_logged(u32 actions_logged,u32 old_actions_logged,int ret)2423ea6eca77STyler Hicks static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged,
2424ea6eca77STyler Hicks 				 int ret)
2425ea6eca77STyler Hicks {
2426ea6eca77STyler Hicks 	char names[sizeof(seccomp_actions_avail)];
2427ea6eca77STyler Hicks 	char old_names[sizeof(seccomp_actions_avail)];
2428ea6eca77STyler Hicks 	const char *new = names;
2429ea6eca77STyler Hicks 	const char *old = old_names;
2430ea6eca77STyler Hicks 
2431ea6eca77STyler Hicks 	if (!audit_enabled)
2432ea6eca77STyler Hicks 		return;
2433ea6eca77STyler Hicks 
2434ea6eca77STyler Hicks 	memset(names, 0, sizeof(names));
2435ea6eca77STyler Hicks 	memset(old_names, 0, sizeof(old_names));
2436ea6eca77STyler Hicks 
2437ea6eca77STyler Hicks 	if (ret)
2438ea6eca77STyler Hicks 		new = "?";
2439ea6eca77STyler Hicks 	else if (!actions_logged)
2440ea6eca77STyler Hicks 		new = "(none)";
2441ea6eca77STyler Hicks 	else if (!seccomp_names_from_actions_logged(names, sizeof(names),
2442ea6eca77STyler Hicks 						    actions_logged, ","))
2443ea6eca77STyler Hicks 		new = "?";
2444ea6eca77STyler Hicks 
2445ea6eca77STyler Hicks 	if (!old_actions_logged)
2446ea6eca77STyler Hicks 		old = "(none)";
2447ea6eca77STyler Hicks 	else if (!seccomp_names_from_actions_logged(old_names,
2448ea6eca77STyler Hicks 						    sizeof(old_names),
2449ea6eca77STyler Hicks 						    old_actions_logged, ","))
2450ea6eca77STyler Hicks 		old = "?";
2451ea6eca77STyler Hicks 
2452ea6eca77STyler Hicks 	return audit_seccomp_actions_logged(new, old, !ret);
2453ea6eca77STyler Hicks }
2454ea6eca77STyler Hicks 
seccomp_actions_logged_handler(const struct ctl_table * ro_table,int write,void * buffer,size_t * lenp,loff_t * ppos)245578eb4ea2SJoel Granados static int seccomp_actions_logged_handler(const struct ctl_table *ro_table, int write,
245632927393SChristoph Hellwig 					  void *buffer, size_t *lenp,
2457d013db02STyler Hicks 					  loff_t *ppos)
2458d013db02STyler Hicks {
2459ea6eca77STyler Hicks 	int ret;
2460ea6eca77STyler Hicks 
2461ea6eca77STyler Hicks 	if (write) {
2462ea6eca77STyler Hicks 		u32 actions_logged = 0;
2463ea6eca77STyler Hicks 		u32 old_actions_logged = seccomp_actions_logged;
2464ea6eca77STyler Hicks 
2465ea6eca77STyler Hicks 		ret = write_actions_logged(ro_table, buffer, lenp, ppos,
2466ea6eca77STyler Hicks 					   &actions_logged);
2467ea6eca77STyler Hicks 		audit_actions_logged(actions_logged, old_actions_logged, ret);
2468ea6eca77STyler Hicks 	} else
2469ea6eca77STyler Hicks 		ret = read_actions_logged(ro_table, buffer, lenp, ppos);
2470ea6eca77STyler Hicks 
2471ea6eca77STyler Hicks 	return ret;
24720ddec0fcSTyler Hicks }
24730ddec0fcSTyler Hicks 
24741751f872SJoel Granados static const struct ctl_table seccomp_sysctl_table[] = {
24758e5f1ad1STyler Hicks 	{
24768e5f1ad1STyler Hicks 		.procname	= "actions_avail",
24778e5f1ad1STyler Hicks 		.data		= (void *) &seccomp_actions_avail,
24788e5f1ad1STyler Hicks 		.maxlen		= sizeof(seccomp_actions_avail),
24798e5f1ad1STyler Hicks 		.mode		= 0444,
24808e5f1ad1STyler Hicks 		.proc_handler	= proc_dostring,
24818e5f1ad1STyler Hicks 	},
24820ddec0fcSTyler Hicks 	{
24830ddec0fcSTyler Hicks 		.procname	= "actions_logged",
24840ddec0fcSTyler Hicks 		.mode		= 0644,
24850ddec0fcSTyler Hicks 		.proc_handler	= seccomp_actions_logged_handler,
24860ddec0fcSTyler Hicks 	},
24878e5f1ad1STyler Hicks };
24888e5f1ad1STyler Hicks 
seccomp_sysctl_init(void)24898e5f1ad1STyler Hicks static int __init seccomp_sysctl_init(void)
24908e5f1ad1STyler Hicks {
249102a6b455SLuis Chamberlain 	register_sysctl_init("kernel/seccomp", seccomp_sysctl_table);
24928e5f1ad1STyler Hicks 	return 0;
24938e5f1ad1STyler Hicks }
24948e5f1ad1STyler Hicks 
device_initcall(seccomp_sysctl_init)24958e5f1ad1STyler Hicks device_initcall(seccomp_sysctl_init)
24968e5f1ad1STyler Hicks 
24978e5f1ad1STyler Hicks #endif /* CONFIG_SYSCTL */
24980d8315ddSYiFei Zhu 
24990d8315ddSYiFei Zhu #ifdef CONFIG_SECCOMP_CACHE_DEBUG
25000d8315ddSYiFei Zhu /* Currently CONFIG_SECCOMP_CACHE_DEBUG implies SECCOMP_ARCH_NATIVE */
25010d8315ddSYiFei Zhu static void proc_pid_seccomp_cache_arch(struct seq_file *m, const char *name,
25020d8315ddSYiFei Zhu 					const void *bitmap, size_t bitmap_size)
25030d8315ddSYiFei Zhu {
25040d8315ddSYiFei Zhu 	int nr;
25050d8315ddSYiFei Zhu 
25060d8315ddSYiFei Zhu 	for (nr = 0; nr < bitmap_size; nr++) {
25070d8315ddSYiFei Zhu 		bool cached = test_bit(nr, bitmap);
25080d8315ddSYiFei Zhu 		char *status = cached ? "ALLOW" : "FILTER";
25090d8315ddSYiFei Zhu 
25100d8315ddSYiFei Zhu 		seq_printf(m, "%s %d %s\n", name, nr, status);
25110d8315ddSYiFei Zhu 	}
25120d8315ddSYiFei Zhu }
25130d8315ddSYiFei Zhu 
proc_pid_seccomp_cache(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)25140d8315ddSYiFei Zhu int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns,
25150d8315ddSYiFei Zhu 			   struct pid *pid, struct task_struct *task)
25160d8315ddSYiFei Zhu {
25170d8315ddSYiFei Zhu 	struct seccomp_filter *f;
25180d8315ddSYiFei Zhu 	unsigned long flags;
25190d8315ddSYiFei Zhu 
25200d8315ddSYiFei Zhu 	/*
25210d8315ddSYiFei Zhu 	 * We don't want some sandboxed process to know what their seccomp
25220d8315ddSYiFei Zhu 	 * filters consist of.
25230d8315ddSYiFei Zhu 	 */
25240d8315ddSYiFei Zhu 	if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
25250d8315ddSYiFei Zhu 		return -EACCES;
25260d8315ddSYiFei Zhu 
25270d8315ddSYiFei Zhu 	if (!lock_task_sighand(task, &flags))
25280d8315ddSYiFei Zhu 		return -ESRCH;
25290d8315ddSYiFei Zhu 
25300d8315ddSYiFei Zhu 	f = READ_ONCE(task->seccomp.filter);
25310d8315ddSYiFei Zhu 	if (!f) {
25320d8315ddSYiFei Zhu 		unlock_task_sighand(task, &flags);
25330d8315ddSYiFei Zhu 		return 0;
25340d8315ddSYiFei Zhu 	}
25350d8315ddSYiFei Zhu 
25360d8315ddSYiFei Zhu 	/* prevent filter from being freed while we are printing it */
25370d8315ddSYiFei Zhu 	__get_seccomp_filter(f);
25380d8315ddSYiFei Zhu 	unlock_task_sighand(task, &flags);
25390d8315ddSYiFei Zhu 
25400d8315ddSYiFei Zhu 	proc_pid_seccomp_cache_arch(m, SECCOMP_ARCH_NATIVE_NAME,
25410d8315ddSYiFei Zhu 				    f->cache.allow_native,
25420d8315ddSYiFei Zhu 				    SECCOMP_ARCH_NATIVE_NR);
25430d8315ddSYiFei Zhu 
25440d8315ddSYiFei Zhu #ifdef SECCOMP_ARCH_COMPAT
25450d8315ddSYiFei Zhu 	proc_pid_seccomp_cache_arch(m, SECCOMP_ARCH_COMPAT_NAME,
25460d8315ddSYiFei Zhu 				    f->cache.allow_compat,
25470d8315ddSYiFei Zhu 				    SECCOMP_ARCH_COMPAT_NR);
25480d8315ddSYiFei Zhu #endif /* SECCOMP_ARCH_COMPAT */
25490d8315ddSYiFei Zhu 
25500d8315ddSYiFei Zhu 	__put_seccomp_filter(f);
25510d8315ddSYiFei Zhu 	return 0;
25520d8315ddSYiFei Zhu }
25530d8315ddSYiFei Zhu #endif /* CONFIG_SECCOMP_CACHE_DEBUG */
2554