xref: /openbmc/linux/kernel/seccomp.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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 <andrea@cpushare.com>
61da177e4SLinus Torvalds  *
7e2cfabdfSWill Drewry  * Copyright (C) 2012 Google, Inc.
8e2cfabdfSWill Drewry  * Will Drewry <wad@chromium.org>
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 
32495ac306SKees Cook /* Not exposed in headers: strictly internal use only. */
33495ac306SKees Cook #define SECCOMP_MODE_DEAD	(SECCOMP_MODE_FILTER + 1)
34495ac306SKees Cook 
35a4412fc9SAndy Lutomirski #ifdef CONFIG_HAVE_ARCH_SECCOMP_FILTER
36e2cfabdfSWill Drewry #include <asm/syscall.h>
37a4412fc9SAndy Lutomirski #endif
38e2cfabdfSWill Drewry 
39e2cfabdfSWill Drewry #ifdef CONFIG_SECCOMP_FILTER
406a21cc50STycho Andersen #include <linux/file.h>
41e2cfabdfSWill Drewry #include <linux/filter.h>
42c2e1f2e3SKees Cook #include <linux/pid.h>
43fb0fadf9SWill Drewry #include <linux/ptrace.h>
44fb14528eSMickaël Salaün #include <linux/capability.h>
45e2cfabdfSWill Drewry #include <linux/uaccess.h>
466a21cc50STycho Andersen #include <linux/anon_inodes.h>
479f87dcf1SSargun Dhillon #include <linux/lockdep.h>
486a21cc50STycho Andersen 
4947e33c05SKees Cook /*
5047e33c05SKees Cook  * When SECCOMP_IOCTL_NOTIF_ID_VALID was first introduced, it had the
5147e33c05SKees Cook  * wrong direction flag in the ioctl number. This is the broken one,
5247e33c05SKees Cook  * which the kernel needs to keep supporting until all userspaces stop
5347e33c05SKees Cook  * using the wrong command number.
5447e33c05SKees Cook  */
5547e33c05SKees Cook #define SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR	SECCOMP_IOR(2, __u64)
5647e33c05SKees Cook 
576a21cc50STycho Andersen enum notify_state {
586a21cc50STycho Andersen 	SECCOMP_NOTIFY_INIT,
596a21cc50STycho Andersen 	SECCOMP_NOTIFY_SENT,
606a21cc50STycho Andersen 	SECCOMP_NOTIFY_REPLIED,
616a21cc50STycho Andersen };
626a21cc50STycho Andersen 
636a21cc50STycho Andersen struct seccomp_knotif {
646a21cc50STycho Andersen 	/* The struct pid of the task whose filter triggered the notification */
656a21cc50STycho Andersen 	struct task_struct *task;
666a21cc50STycho Andersen 
676a21cc50STycho Andersen 	/* The "cookie" for this request; this is unique for this filter. */
686a21cc50STycho Andersen 	u64 id;
696a21cc50STycho Andersen 
706a21cc50STycho Andersen 	/*
716a21cc50STycho Andersen 	 * The seccomp data. This pointer is valid the entire time this
726a21cc50STycho Andersen 	 * notification is active, since it comes from __seccomp_filter which
736a21cc50STycho Andersen 	 * eclipses the entire lifecycle here.
746a21cc50STycho Andersen 	 */
756a21cc50STycho Andersen 	const struct seccomp_data *data;
766a21cc50STycho Andersen 
776a21cc50STycho Andersen 	/*
786a21cc50STycho Andersen 	 * Notification states. When SECCOMP_RET_USER_NOTIF is returned, a
796a21cc50STycho Andersen 	 * struct seccomp_knotif is created and starts out in INIT. Once the
806a21cc50STycho Andersen 	 * handler reads the notification off of an FD, it transitions to SENT.
816a21cc50STycho Andersen 	 * If a signal is received the state transitions back to INIT and
826a21cc50STycho Andersen 	 * another message is sent. When the userspace handler replies, state
836a21cc50STycho Andersen 	 * transitions to REPLIED.
846a21cc50STycho Andersen 	 */
856a21cc50STycho Andersen 	enum notify_state state;
866a21cc50STycho Andersen 
876a21cc50STycho Andersen 	/* The return values, only valid when in SECCOMP_NOTIFY_REPLIED */
886a21cc50STycho Andersen 	int error;
896a21cc50STycho Andersen 	long val;
90fb3c5386SChristian Brauner 	u32 flags;
916a21cc50STycho Andersen 
927cf97b12SSargun Dhillon 	/*
937cf97b12SSargun Dhillon 	 * Signals when this has changed states, such as the listener
947cf97b12SSargun Dhillon 	 * dying, a new seccomp addfd message, or changing to REPLIED
957cf97b12SSargun Dhillon 	 */
966a21cc50STycho Andersen 	struct completion ready;
976a21cc50STycho Andersen 
986a21cc50STycho Andersen 	struct list_head list;
997cf97b12SSargun Dhillon 
1007cf97b12SSargun Dhillon 	/* outstanding addfd requests */
1017cf97b12SSargun Dhillon 	struct list_head addfd;
1027cf97b12SSargun Dhillon };
1037cf97b12SSargun Dhillon 
1047cf97b12SSargun Dhillon /**
1057cf97b12SSargun Dhillon  * struct seccomp_kaddfd - container for seccomp_addfd ioctl messages
1067cf97b12SSargun Dhillon  *
1077cf97b12SSargun Dhillon  * @file: A reference to the file to install in the other task
1087cf97b12SSargun Dhillon  * @fd: The fd number to install it at. If the fd number is -1, it means the
1097cf97b12SSargun Dhillon  *      installing process should allocate the fd as normal.
1107cf97b12SSargun Dhillon  * @flags: The flags for the new file descriptor. At the moment, only O_CLOEXEC
1117cf97b12SSargun Dhillon  *         is allowed.
1120ae71c77SRodrigo Campos  * @ioctl_flags: The flags used for the seccomp_addfd ioctl.
113*46822860SKees Cook  * @setfd: whether or not SECCOMP_ADDFD_FLAG_SETFD was set during notify_addfd
1147cf97b12SSargun Dhillon  * @ret: The return value of the installing process. It is set to the fd num
1157cf97b12SSargun Dhillon  *       upon success (>= 0).
1167cf97b12SSargun Dhillon  * @completion: Indicates that the installing process has completed fd
1177cf97b12SSargun Dhillon  *              installation, or gone away (either due to successful
1187cf97b12SSargun Dhillon  *              reply, or signal)
119*46822860SKees Cook  * @list: list_head for chaining seccomp_kaddfd together.
1207cf97b12SSargun Dhillon  *
1217cf97b12SSargun Dhillon  */
1227cf97b12SSargun Dhillon struct seccomp_kaddfd {
1237cf97b12SSargun Dhillon 	struct file *file;
1247cf97b12SSargun Dhillon 	int fd;
1257cf97b12SSargun Dhillon 	unsigned int flags;
1260ae71c77SRodrigo Campos 	__u32 ioctl_flags;
1277cf97b12SSargun Dhillon 
12842eb0d54SChristoph Hellwig 	union {
12942eb0d54SChristoph Hellwig 		bool setfd;
1307cf97b12SSargun Dhillon 		/* To only be set on reply */
1317cf97b12SSargun Dhillon 		int ret;
13242eb0d54SChristoph Hellwig 	};
1337cf97b12SSargun Dhillon 	struct completion completion;
1347cf97b12SSargun Dhillon 	struct list_head list;
1356a21cc50STycho Andersen };
1366a21cc50STycho Andersen 
1376a21cc50STycho Andersen /**
1386a21cc50STycho Andersen  * struct notification - container for seccomp userspace notifications. Since
1396a21cc50STycho Andersen  * most seccomp filters will not have notification listeners attached and this
1406a21cc50STycho Andersen  * structure is fairly large, we store the notification-specific stuff in a
1416a21cc50STycho Andersen  * separate structure.
1426a21cc50STycho Andersen  *
143*46822860SKees Cook  * @requests: A semaphore that users of this notification can wait on for
1446a21cc50STycho Andersen  *            changes. Actual reads and writes are still controlled with
1456a21cc50STycho Andersen  *            filter->notify_lock.
146*46822860SKees Cook  * @flags: A set of SECCOMP_USER_NOTIF_FD_* flags.
1476a21cc50STycho Andersen  * @next_id: The id of the next request.
1486a21cc50STycho Andersen  * @notifications: A list of struct seccomp_knotif elements.
1496a21cc50STycho Andersen  */
15048a1084aSAndrei Vagin 
1516a21cc50STycho Andersen struct notification {
1524943b66dSAndrei Vagin 	atomic_t requests;
15348a1084aSAndrei Vagin 	u32 flags;
1546a21cc50STycho Andersen 	u64 next_id;
1556a21cc50STycho Andersen 	struct list_head notifications;
1566a21cc50STycho Andersen };
157e2cfabdfSWill Drewry 
158f9d480b6SYiFei Zhu #ifdef SECCOMP_ARCH_NATIVE
159f9d480b6SYiFei Zhu /**
160f9d480b6SYiFei Zhu  * struct action_cache - per-filter cache of seccomp actions per
161f9d480b6SYiFei Zhu  * arch/syscall pair
162f9d480b6SYiFei Zhu  *
163f9d480b6SYiFei Zhu  * @allow_native: A bitmap where each bit represents whether the
164f9d480b6SYiFei Zhu  *		  filter will always allow the syscall, for the
165f9d480b6SYiFei Zhu  *		  native architecture.
166f9d480b6SYiFei Zhu  * @allow_compat: A bitmap where each bit represents whether the
167f9d480b6SYiFei Zhu  *		  filter will always allow the syscall, for the
168f9d480b6SYiFei Zhu  *		  compat architecture.
169f9d480b6SYiFei Zhu  */
170f9d480b6SYiFei Zhu struct action_cache {
171f9d480b6SYiFei Zhu 	DECLARE_BITMAP(allow_native, SECCOMP_ARCH_NATIVE_NR);
172f9d480b6SYiFei Zhu #ifdef SECCOMP_ARCH_COMPAT
173f9d480b6SYiFei Zhu 	DECLARE_BITMAP(allow_compat, SECCOMP_ARCH_COMPAT_NR);
174f9d480b6SYiFei Zhu #endif
175f9d480b6SYiFei Zhu };
176f9d480b6SYiFei Zhu #else
177f9d480b6SYiFei Zhu struct action_cache { };
178f9d480b6SYiFei Zhu 
seccomp_cache_check_allow(const struct seccomp_filter * sfilter,const struct seccomp_data * sd)179f9d480b6SYiFei Zhu static inline bool seccomp_cache_check_allow(const struct seccomp_filter *sfilter,
180f9d480b6SYiFei Zhu 					     const struct seccomp_data *sd)
181f9d480b6SYiFei Zhu {
182f9d480b6SYiFei Zhu 	return false;
183f9d480b6SYiFei Zhu }
1848e01b51aSYiFei Zhu 
seccomp_cache_prepare(struct seccomp_filter * sfilter)1858e01b51aSYiFei Zhu static inline void seccomp_cache_prepare(struct seccomp_filter *sfilter)
1868e01b51aSYiFei Zhu {
1878e01b51aSYiFei Zhu }
188f9d480b6SYiFei Zhu #endif /* SECCOMP_ARCH_NATIVE */
189f9d480b6SYiFei Zhu 
190e2cfabdfSWill Drewry /**
191e2cfabdfSWill Drewry  * struct seccomp_filter - container for seccomp BPF programs
192e2cfabdfSWill Drewry  *
193b707ddeeSChristian Brauner  * @refs: Reference count to manage the object lifetime.
194b707ddeeSChristian Brauner  *	  A filter's reference count is incremented for each directly
195b707ddeeSChristian Brauner  *	  attached task, once for the dependent filter, and if
196b707ddeeSChristian Brauner  *	  requested for the user notifier. When @refs reaches zero,
197b707ddeeSChristian Brauner  *	  the filter can be freed.
19899cdb8b9SChristian Brauner  * @users: A filter's @users count is incremented for each directly
19999cdb8b9SChristian Brauner  *         attached task (filter installation, fork(), thread_sync),
20099cdb8b9SChristian Brauner  *	   and once for the dependent filter (tracked in filter->prev).
20199cdb8b9SChristian Brauner  *	   When it reaches zero it indicates that no direct or indirect
20299cdb8b9SChristian Brauner  *	   users of that filter exist. No new tasks can get associated with
20399cdb8b9SChristian Brauner  *	   this filter after reaching 0. The @users count is always smaller
20499cdb8b9SChristian Brauner  *	   or equal to @refs. Hence, reaching 0 for @users does not mean
20599cdb8b9SChristian Brauner  *	   the filter can be freed.
2068e01b51aSYiFei Zhu  * @cache: cache of arch/syscall mappings to actions
207e66a3997STyler Hicks  * @log: true if all actions except for SECCOMP_RET_ALLOW should be logged
208c2aa2dfeSSargun Dhillon  * @wait_killable_recv: Put notifying process in killable state once the
209c2aa2dfeSSargun Dhillon  *			notification is received by the userspace listener.
210e2cfabdfSWill Drewry  * @prev: points to a previously installed, or inherited, filter
211285fdfc5SMickaël Salaün  * @prog: the BPF program to evaluate
2126a21cc50STycho Andersen  * @notif: the struct that holds all notification related information
2136a21cc50STycho Andersen  * @notify_lock: A lock for all notification-related accesses.
21476194c4eSChristian Brauner  * @wqh: A wait queue for poll if a notifier is in use.
215e2cfabdfSWill Drewry  *
216e2cfabdfSWill Drewry  * seccomp_filter objects are organized in a tree linked via the @prev
217e2cfabdfSWill Drewry  * pointer.  For any task, it appears to be a singly-linked list starting
218e2cfabdfSWill Drewry  * with current->seccomp.filter, the most recently attached or inherited filter.
219e2cfabdfSWill Drewry  * However, multiple filters may share a @prev node, by way of fork(), which
220e2cfabdfSWill Drewry  * results in a unidirectional tree existing in memory.  This is similar to
221e2cfabdfSWill Drewry  * how namespaces work.
222e2cfabdfSWill Drewry  *
223e2cfabdfSWill Drewry  * seccomp_filter objects should never be modified after being attached
224b707ddeeSChristian Brauner  * to a task_struct (other than @refs).
225e2cfabdfSWill Drewry  */
226e2cfabdfSWill Drewry struct seccomp_filter {
227b707ddeeSChristian Brauner 	refcount_t refs;
22899cdb8b9SChristian Brauner 	refcount_t users;
229e66a3997STyler Hicks 	bool log;
230c2aa2dfeSSargun Dhillon 	bool wait_killable_recv;
2318e01b51aSYiFei Zhu 	struct action_cache cache;
232e2cfabdfSWill Drewry 	struct seccomp_filter *prev;
2337ae457c1SAlexei Starovoitov 	struct bpf_prog *prog;
2346a21cc50STycho Andersen 	struct notification *notif;
2356a21cc50STycho Andersen 	struct mutex notify_lock;
23676194c4eSChristian Brauner 	wait_queue_head_t wqh;
237e2cfabdfSWill Drewry };
238e2cfabdfSWill Drewry 
239e2cfabdfSWill Drewry /* Limit any path through the tree to 256KB worth of instructions. */
240e2cfabdfSWill Drewry #define MAX_INSNS_PER_PATH ((1 << 18) / sizeof(struct sock_filter))
241e2cfabdfSWill Drewry 
242bd4cf0edSAlexei Starovoitov /*
243e2cfabdfSWill Drewry  * Endianness is explicitly ignored and left for BPF program authors to manage
244e2cfabdfSWill Drewry  * as per the specific architecture.
245e2cfabdfSWill Drewry  */
populate_seccomp_data(struct seccomp_data * sd)246bd4cf0edSAlexei Starovoitov static void populate_seccomp_data(struct seccomp_data *sd)
247e2cfabdfSWill Drewry {
2482d9ca267SDenis Efremov 	/*
2492d9ca267SDenis Efremov 	 * Instead of using current_pt_reg(), we're already doing the work
2502d9ca267SDenis Efremov 	 * to safely fetch "current", so just use "task" everywhere below.
2512d9ca267SDenis Efremov 	 */
252bd4cf0edSAlexei Starovoitov 	struct task_struct *task = current;
253bd4cf0edSAlexei Starovoitov 	struct pt_regs *regs = task_pt_regs(task);
2542eac7648SDaniel Borkmann 	unsigned long args[6];
255e2cfabdfSWill Drewry 
256bd4cf0edSAlexei Starovoitov 	sd->nr = syscall_get_nr(task, regs);
25716add411SDmitry V. Levin 	sd->arch = syscall_get_arch(task);
258b35f549dSSteven Rostedt (Red Hat) 	syscall_get_arguments(task, regs, args);
2592eac7648SDaniel Borkmann 	sd->args[0] = args[0];
2602eac7648SDaniel Borkmann 	sd->args[1] = args[1];
2612eac7648SDaniel Borkmann 	sd->args[2] = args[2];
2622eac7648SDaniel Borkmann 	sd->args[3] = args[3];
2632eac7648SDaniel Borkmann 	sd->args[4] = args[4];
2642eac7648SDaniel Borkmann 	sd->args[5] = args[5];
265bd4cf0edSAlexei Starovoitov 	sd->instruction_pointer = KSTK_EIP(task);
266e2cfabdfSWill Drewry }
267e2cfabdfSWill Drewry 
268e2cfabdfSWill Drewry /**
269e2cfabdfSWill Drewry  *	seccomp_check_filter - verify seccomp filter code
270e2cfabdfSWill Drewry  *	@filter: filter to verify
271e2cfabdfSWill Drewry  *	@flen: length of filter
272e2cfabdfSWill Drewry  *
2734df95ff4SAlexei Starovoitov  * Takes a previously checked filter (by bpf_check_classic) and
274e2cfabdfSWill Drewry  * redirects all filter code that loads struct sk_buff data
275e2cfabdfSWill Drewry  * and related data through seccomp_bpf_load.  It also
276e2cfabdfSWill Drewry  * enforces length and alignment checking of those loads.
277e2cfabdfSWill Drewry  *
278e2cfabdfSWill Drewry  * Returns 0 if the rule set is legal or -EINVAL if not.
279e2cfabdfSWill Drewry  */
seccomp_check_filter(struct sock_filter * filter,unsigned int flen)280e2cfabdfSWill Drewry static int seccomp_check_filter(struct sock_filter *filter, unsigned int flen)
281e2cfabdfSWill Drewry {
282e2cfabdfSWill Drewry 	int pc;
283e2cfabdfSWill Drewry 	for (pc = 0; pc < flen; pc++) {
284e2cfabdfSWill Drewry 		struct sock_filter *ftest = &filter[pc];
285e2cfabdfSWill Drewry 		u16 code = ftest->code;
286e2cfabdfSWill Drewry 		u32 k = ftest->k;
287e2cfabdfSWill Drewry 
288e2cfabdfSWill Drewry 		switch (code) {
28934805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_ABS:
290bd4cf0edSAlexei Starovoitov 			ftest->code = BPF_LDX | BPF_W | BPF_ABS;
291e2cfabdfSWill Drewry 			/* 32-bit aligned and not out of bounds. */
292e2cfabdfSWill Drewry 			if (k >= sizeof(struct seccomp_data) || k & 3)
293e2cfabdfSWill Drewry 				return -EINVAL;
294e2cfabdfSWill Drewry 			continue;
29534805931SDaniel Borkmann 		case BPF_LD | BPF_W | BPF_LEN:
296bd4cf0edSAlexei Starovoitov 			ftest->code = BPF_LD | BPF_IMM;
297e2cfabdfSWill Drewry 			ftest->k = sizeof(struct seccomp_data);
298e2cfabdfSWill Drewry 			continue;
29934805931SDaniel Borkmann 		case BPF_LDX | BPF_W | BPF_LEN:
300bd4cf0edSAlexei Starovoitov 			ftest->code = BPF_LDX | BPF_IMM;
301e2cfabdfSWill Drewry 			ftest->k = sizeof(struct seccomp_data);
302e2cfabdfSWill Drewry 			continue;
303e2cfabdfSWill Drewry 		/* Explicitly include allowed calls. */
30434805931SDaniel Borkmann 		case BPF_RET | BPF_K:
30534805931SDaniel Borkmann 		case BPF_RET | BPF_A:
30634805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_K:
30734805931SDaniel Borkmann 		case BPF_ALU | BPF_ADD | BPF_X:
30834805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_K:
30934805931SDaniel Borkmann 		case BPF_ALU | BPF_SUB | BPF_X:
31034805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_K:
31134805931SDaniel Borkmann 		case BPF_ALU | BPF_MUL | BPF_X:
31234805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_K:
31334805931SDaniel Borkmann 		case BPF_ALU | BPF_DIV | BPF_X:
31434805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_K:
31534805931SDaniel Borkmann 		case BPF_ALU | BPF_AND | BPF_X:
31634805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_K:
31734805931SDaniel Borkmann 		case BPF_ALU | BPF_OR | BPF_X:
31834805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_K:
31934805931SDaniel Borkmann 		case BPF_ALU | BPF_XOR | BPF_X:
32034805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_K:
32134805931SDaniel Borkmann 		case BPF_ALU | BPF_LSH | BPF_X:
32234805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_K:
32334805931SDaniel Borkmann 		case BPF_ALU | BPF_RSH | BPF_X:
32434805931SDaniel Borkmann 		case BPF_ALU | BPF_NEG:
32534805931SDaniel Borkmann 		case BPF_LD | BPF_IMM:
32634805931SDaniel Borkmann 		case BPF_LDX | BPF_IMM:
32734805931SDaniel Borkmann 		case BPF_MISC | BPF_TAX:
32834805931SDaniel Borkmann 		case BPF_MISC | BPF_TXA:
32934805931SDaniel Borkmann 		case BPF_LD | BPF_MEM:
33034805931SDaniel Borkmann 		case BPF_LDX | BPF_MEM:
33134805931SDaniel Borkmann 		case BPF_ST:
33234805931SDaniel Borkmann 		case BPF_STX:
33334805931SDaniel Borkmann 		case BPF_JMP | BPF_JA:
33434805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_K:
33534805931SDaniel Borkmann 		case BPF_JMP | BPF_JEQ | BPF_X:
33634805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_K:
33734805931SDaniel Borkmann 		case BPF_JMP | BPF_JGE | BPF_X:
33834805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_K:
33934805931SDaniel Borkmann 		case BPF_JMP | BPF_JGT | BPF_X:
34034805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_K:
34134805931SDaniel Borkmann 		case BPF_JMP | BPF_JSET | BPF_X:
342e2cfabdfSWill Drewry 			continue;
343e2cfabdfSWill Drewry 		default:
344e2cfabdfSWill Drewry 			return -EINVAL;
345e2cfabdfSWill Drewry 		}
346e2cfabdfSWill Drewry 	}
347e2cfabdfSWill Drewry 	return 0;
348e2cfabdfSWill Drewry }
349e2cfabdfSWill Drewry 
350f9d480b6SYiFei Zhu #ifdef SECCOMP_ARCH_NATIVE
seccomp_cache_check_allow_bitmap(const void * bitmap,size_t bitmap_size,int syscall_nr)351f9d480b6SYiFei Zhu static inline bool seccomp_cache_check_allow_bitmap(const void *bitmap,
352f9d480b6SYiFei Zhu 						    size_t bitmap_size,
353f9d480b6SYiFei Zhu 						    int syscall_nr)
354f9d480b6SYiFei Zhu {
355f9d480b6SYiFei Zhu 	if (unlikely(syscall_nr < 0 || syscall_nr >= bitmap_size))
356f9d480b6SYiFei Zhu 		return false;
357f9d480b6SYiFei Zhu 	syscall_nr = array_index_nospec(syscall_nr, bitmap_size);
358f9d480b6SYiFei Zhu 
359f9d480b6SYiFei Zhu 	return test_bit(syscall_nr, bitmap);
360f9d480b6SYiFei Zhu }
361f9d480b6SYiFei Zhu 
362f9d480b6SYiFei Zhu /**
363f9d480b6SYiFei Zhu  * seccomp_cache_check_allow - lookup seccomp cache
364f9d480b6SYiFei Zhu  * @sfilter: The seccomp filter
365f9d480b6SYiFei Zhu  * @sd: The seccomp data to lookup the cache with
366f9d480b6SYiFei Zhu  *
367f9d480b6SYiFei Zhu  * Returns true if the seccomp_data is cached and allowed.
368f9d480b6SYiFei Zhu  */
seccomp_cache_check_allow(const struct seccomp_filter * sfilter,const struct seccomp_data * sd)369f9d480b6SYiFei Zhu static inline bool seccomp_cache_check_allow(const struct seccomp_filter *sfilter,
370f9d480b6SYiFei Zhu 					     const struct seccomp_data *sd)
371f9d480b6SYiFei Zhu {
372f9d480b6SYiFei Zhu 	int syscall_nr = sd->nr;
373f9d480b6SYiFei Zhu 	const struct action_cache *cache = &sfilter->cache;
374f9d480b6SYiFei Zhu 
375f9d480b6SYiFei Zhu #ifndef SECCOMP_ARCH_COMPAT
376f9d480b6SYiFei Zhu 	/* A native-only architecture doesn't need to check sd->arch. */
377f9d480b6SYiFei Zhu 	return seccomp_cache_check_allow_bitmap(cache->allow_native,
378f9d480b6SYiFei Zhu 						SECCOMP_ARCH_NATIVE_NR,
379f9d480b6SYiFei Zhu 						syscall_nr);
380f9d480b6SYiFei Zhu #else
381f9d480b6SYiFei Zhu 	if (likely(sd->arch == SECCOMP_ARCH_NATIVE))
382f9d480b6SYiFei Zhu 		return seccomp_cache_check_allow_bitmap(cache->allow_native,
383f9d480b6SYiFei Zhu 							SECCOMP_ARCH_NATIVE_NR,
384f9d480b6SYiFei Zhu 							syscall_nr);
385f9d480b6SYiFei Zhu 	if (likely(sd->arch == SECCOMP_ARCH_COMPAT))
386f9d480b6SYiFei Zhu 		return seccomp_cache_check_allow_bitmap(cache->allow_compat,
387f9d480b6SYiFei Zhu 							SECCOMP_ARCH_COMPAT_NR,
388f9d480b6SYiFei Zhu 							syscall_nr);
389f9d480b6SYiFei Zhu #endif /* SECCOMP_ARCH_COMPAT */
390f9d480b6SYiFei Zhu 
391f9d480b6SYiFei Zhu 	WARN_ON_ONCE(true);
392f9d480b6SYiFei Zhu 	return false;
393f9d480b6SYiFei Zhu }
394f9d480b6SYiFei Zhu #endif /* SECCOMP_ARCH_NATIVE */
395f9d480b6SYiFei Zhu 
3960fb0624bSRandy Dunlap #define ACTION_ONLY(ret) ((s32)((ret) & (SECCOMP_RET_ACTION_FULL)))
397e2cfabdfSWill Drewry /**
398285fdfc5SMickaël Salaün  * seccomp_run_filters - evaluates all seccomp filters against @sd
399285fdfc5SMickaël Salaün  * @sd: optional seccomp data to be passed to filters
400deb4de8bSKees Cook  * @match: stores struct seccomp_filter that resulted in the return value,
401deb4de8bSKees Cook  *         unless filter returned SECCOMP_RET_ALLOW, in which case it will
402deb4de8bSKees Cook  *         be unchanged.
403e2cfabdfSWill Drewry  *
404e2cfabdfSWill Drewry  * Returns valid seccomp BPF response codes.
405e2cfabdfSWill Drewry  */
seccomp_run_filters(const struct seccomp_data * sd,struct seccomp_filter ** match)406deb4de8bSKees Cook static u32 seccomp_run_filters(const struct seccomp_data *sd,
407deb4de8bSKees Cook 			       struct seccomp_filter **match)
408e2cfabdfSWill Drewry {
409acf3b2c7SWill Drewry 	u32 ret = SECCOMP_RET_ALLOW;
4108225d385SPranith Kumar 	/* Make sure cross-thread synced filter points somewhere sane. */
4118225d385SPranith Kumar 	struct seccomp_filter *f =
412506458efSWill Deacon 			READ_ONCE(current->seccomp.filter);
413acf3b2c7SWill Drewry 
414acf3b2c7SWill Drewry 	/* Ensure unexpected behavior doesn't result in failing open. */
4150d42d73aSIgor Stoppa 	if (WARN_ON(f == NULL))
4164d3b0b05SKees Cook 		return SECCOMP_RET_KILL_PROCESS;
417acf3b2c7SWill Drewry 
418f9d480b6SYiFei Zhu 	if (seccomp_cache_check_allow(f, sd))
419f9d480b6SYiFei Zhu 		return SECCOMP_RET_ALLOW;
420f9d480b6SYiFei Zhu 
421e2cfabdfSWill Drewry 	/*
422e2cfabdfSWill Drewry 	 * All filters in the list are evaluated and the lowest BPF return
423acf3b2c7SWill Drewry 	 * value always takes priority (ignoring the DATA).
424e2cfabdfSWill Drewry 	 */
4253ba2530cSKees Cook 	for (; f; f = f->prev) {
4263d9f773cSDavid Miller 		u32 cur_ret = bpf_prog_run_pin_on_cpu(f->prog, sd);
4278f577cadSAlexei Starovoitov 
4280466bdb9SKees Cook 		if (ACTION_ONLY(cur_ret) < ACTION_ONLY(ret)) {
429acf3b2c7SWill Drewry 			ret = cur_ret;
430deb4de8bSKees Cook 			*match = f;
431deb4de8bSKees Cook 		}
432e2cfabdfSWill Drewry 	}
433e2cfabdfSWill Drewry 	return ret;
434e2cfabdfSWill Drewry }
4351f41b450SKees Cook #endif /* CONFIG_SECCOMP_FILTER */
436e2cfabdfSWill Drewry 
seccomp_may_assign_mode(unsigned long seccomp_mode)4371f41b450SKees Cook static inline bool seccomp_may_assign_mode(unsigned long seccomp_mode)
4381f41b450SKees Cook {
43969f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
440dbd95212SKees Cook 
4411f41b450SKees Cook 	if (current->seccomp.mode && current->seccomp.mode != seccomp_mode)
4421f41b450SKees Cook 		return false;
4431f41b450SKees Cook 
4441f41b450SKees Cook 	return true;
4451f41b450SKees Cook }
4461f41b450SKees Cook 
arch_seccomp_spec_mitigate(struct task_struct * task)4478bf37d8cSThomas Gleixner void __weak arch_seccomp_spec_mitigate(struct task_struct *task) { }
4485c307089SKees Cook 
seccomp_assign_mode(struct task_struct * task,unsigned long seccomp_mode,unsigned long flags)4493ba2530cSKees Cook static inline void seccomp_assign_mode(struct task_struct *task,
45000a02d0cSKees Cook 				       unsigned long seccomp_mode,
45100a02d0cSKees Cook 				       unsigned long flags)
4521f41b450SKees Cook {
45369f6a34bSGuenter Roeck 	assert_spin_locked(&task->sighand->siglock);
454dbd95212SKees Cook 
4553ba2530cSKees Cook 	task->seccomp.mode = seccomp_mode;
4563ba2530cSKees Cook 	/*
45723d67a54SGabriel Krisman Bertazi 	 * Make sure SYSCALL_WORK_SECCOMP cannot be set before the mode (and
4583ba2530cSKees Cook 	 * filter) is set.
4593ba2530cSKees Cook 	 */
4603ba2530cSKees Cook 	smp_mb__before_atomic();
46100a02d0cSKees Cook 	/* Assume default seccomp processes want spec flaw mitigation. */
46200a02d0cSKees Cook 	if ((flags & SECCOMP_FILTER_FLAG_SPEC_ALLOW) == 0)
4638bf37d8cSThomas Gleixner 		arch_seccomp_spec_mitigate(task);
46423d67a54SGabriel Krisman Bertazi 	set_task_syscall_work(task, SECCOMP);
4651f41b450SKees Cook }
4661f41b450SKees Cook 
4671f41b450SKees Cook #ifdef CONFIG_SECCOMP_FILTER
468c2e1f2e3SKees Cook /* Returns 1 if the parent is an ancestor of the child. */
is_ancestor(struct seccomp_filter * parent,struct seccomp_filter * child)469c2e1f2e3SKees Cook static int is_ancestor(struct seccomp_filter *parent,
470c2e1f2e3SKees Cook 		       struct seccomp_filter *child)
471c2e1f2e3SKees Cook {
472c2e1f2e3SKees Cook 	/* NULL is the root ancestor. */
473c2e1f2e3SKees Cook 	if (parent == NULL)
474c2e1f2e3SKees Cook 		return 1;
475c2e1f2e3SKees Cook 	for (; child; child = child->prev)
476c2e1f2e3SKees Cook 		if (child == parent)
477c2e1f2e3SKees Cook 			return 1;
478c2e1f2e3SKees Cook 	return 0;
479c2e1f2e3SKees Cook }
480c2e1f2e3SKees Cook 
481c2e1f2e3SKees Cook /**
482c2e1f2e3SKees Cook  * seccomp_can_sync_threads: checks if all threads can be synchronized
483c2e1f2e3SKees Cook  *
484c2e1f2e3SKees Cook  * Expects sighand and cred_guard_mutex locks to be held.
485c2e1f2e3SKees Cook  *
486c2e1f2e3SKees Cook  * Returns 0 on success, -ve on error, or the pid of a thread which was
4876beff00bSTycho Andersen  * either not in the correct seccomp mode or did not have an ancestral
488c2e1f2e3SKees Cook  * seccomp filter.
489c2e1f2e3SKees Cook  */
seccomp_can_sync_threads(void)490c2e1f2e3SKees Cook static inline pid_t seccomp_can_sync_threads(void)
491c2e1f2e3SKees Cook {
492c2e1f2e3SKees Cook 	struct task_struct *thread, *caller;
493c2e1f2e3SKees Cook 
494c2e1f2e3SKees Cook 	BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
49569f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
496c2e1f2e3SKees Cook 
497c2e1f2e3SKees Cook 	/* Validate all threads being eligible for synchronization. */
498c2e1f2e3SKees Cook 	caller = current;
499c2e1f2e3SKees Cook 	for_each_thread(caller, thread) {
500c2e1f2e3SKees Cook 		pid_t failed;
501c2e1f2e3SKees Cook 
502c2e1f2e3SKees Cook 		/* Skip current, since it is initiating the sync. */
503c2e1f2e3SKees Cook 		if (thread == caller)
504c2e1f2e3SKees Cook 			continue;
505c2e1f2e3SKees Cook 
506c2e1f2e3SKees Cook 		if (thread->seccomp.mode == SECCOMP_MODE_DISABLED ||
507c2e1f2e3SKees Cook 		    (thread->seccomp.mode == SECCOMP_MODE_FILTER &&
508c2e1f2e3SKees Cook 		     is_ancestor(thread->seccomp.filter,
509c2e1f2e3SKees Cook 				 caller->seccomp.filter)))
510c2e1f2e3SKees Cook 			continue;
511c2e1f2e3SKees Cook 
512c2e1f2e3SKees Cook 		/* Return the first thread that cannot be synchronized. */
513c2e1f2e3SKees Cook 		failed = task_pid_vnr(thread);
514c2e1f2e3SKees Cook 		/* If the pid cannot be resolved, then return -ESRCH */
5150d42d73aSIgor Stoppa 		if (WARN_ON(failed == 0))
516c2e1f2e3SKees Cook 			failed = -ESRCH;
517c2e1f2e3SKees Cook 		return failed;
518c2e1f2e3SKees Cook 	}
519c2e1f2e3SKees Cook 
520c2e1f2e3SKees Cook 	return 0;
521c2e1f2e3SKees Cook }
522c2e1f2e3SKees Cook 
seccomp_filter_free(struct seccomp_filter * filter)5233a15fb6eSChristian Brauner static inline void seccomp_filter_free(struct seccomp_filter *filter)
5243a15fb6eSChristian Brauner {
5253a15fb6eSChristian Brauner 	if (filter) {
5263a15fb6eSChristian Brauner 		bpf_prog_destroy(filter->prog);
5273a15fb6eSChristian Brauner 		kfree(filter);
5283a15fb6eSChristian Brauner 	}
5293a15fb6eSChristian Brauner }
5303a15fb6eSChristian Brauner 
__seccomp_filter_orphan(struct seccomp_filter * orig)53199cdb8b9SChristian Brauner static void __seccomp_filter_orphan(struct seccomp_filter *orig)
53299cdb8b9SChristian Brauner {
53399cdb8b9SChristian Brauner 	while (orig && refcount_dec_and_test(&orig->users)) {
53499cdb8b9SChristian Brauner 		if (waitqueue_active(&orig->wqh))
53599cdb8b9SChristian Brauner 			wake_up_poll(&orig->wqh, EPOLLHUP);
53699cdb8b9SChristian Brauner 		orig = orig->prev;
53799cdb8b9SChristian Brauner 	}
53899cdb8b9SChristian Brauner }
53999cdb8b9SChristian Brauner 
__put_seccomp_filter(struct seccomp_filter * orig)5403a15fb6eSChristian Brauner static void __put_seccomp_filter(struct seccomp_filter *orig)
5413a15fb6eSChristian Brauner {
5423a15fb6eSChristian Brauner 	/* Clean up single-reference branches iteratively. */
5433a15fb6eSChristian Brauner 	while (orig && refcount_dec_and_test(&orig->refs)) {
5443a15fb6eSChristian Brauner 		struct seccomp_filter *freeme = orig;
5453a15fb6eSChristian Brauner 		orig = orig->prev;
5463a15fb6eSChristian Brauner 		seccomp_filter_free(freeme);
5473a15fb6eSChristian Brauner 	}
5483a15fb6eSChristian Brauner }
5493a15fb6eSChristian Brauner 
__seccomp_filter_release(struct seccomp_filter * orig)55099cdb8b9SChristian Brauner static void __seccomp_filter_release(struct seccomp_filter *orig)
55199cdb8b9SChristian Brauner {
55299cdb8b9SChristian Brauner 	/* Notify about any unused filters in the task's former filter tree. */
55399cdb8b9SChristian Brauner 	__seccomp_filter_orphan(orig);
55499cdb8b9SChristian Brauner 	/* Finally drop all references to the task's former tree. */
55599cdb8b9SChristian Brauner 	__put_seccomp_filter(orig);
55699cdb8b9SChristian Brauner }
55799cdb8b9SChristian Brauner 
5583a15fb6eSChristian Brauner /**
55999cdb8b9SChristian Brauner  * seccomp_filter_release - Detach the task from its filter tree,
56099cdb8b9SChristian Brauner  *			    drop its reference count, and notify
56199cdb8b9SChristian Brauner  *			    about unused filters
5623a15fb6eSChristian Brauner  *
563*46822860SKees Cook  * @tsk: task the filter should be released from.
564*46822860SKees Cook  *
5653a15fb6eSChristian Brauner  * This function should only be called when the task is exiting as
5663a15fb6eSChristian Brauner  * it detaches it from its filter tree. As such, READ_ONCE() and
5673a15fb6eSChristian Brauner  * barriers are not needed here, as would normally be needed.
5683a15fb6eSChristian Brauner  */
seccomp_filter_release(struct task_struct * tsk)5693a15fb6eSChristian Brauner void seccomp_filter_release(struct task_struct *tsk)
5703a15fb6eSChristian Brauner {
5713a15fb6eSChristian Brauner 	struct seccomp_filter *orig = tsk->seccomp.filter;
5723a15fb6eSChristian Brauner 
5730d8315ddSYiFei Zhu 	/* We are effectively holding the siglock by not having any sighand. */
5740d8315ddSYiFei Zhu 	WARN_ON(tsk->sighand != NULL);
5750d8315ddSYiFei Zhu 
5763a15fb6eSChristian Brauner 	/* Detach task from its filter tree. */
5773a15fb6eSChristian Brauner 	tsk->seccomp.filter = NULL;
57899cdb8b9SChristian Brauner 	__seccomp_filter_release(orig);
5793a15fb6eSChristian Brauner }
5803a15fb6eSChristian Brauner 
581c2e1f2e3SKees Cook /**
582c2e1f2e3SKees Cook  * seccomp_sync_threads: sets all threads to use current's filter
583c2e1f2e3SKees Cook  *
584*46822860SKees Cook  * @flags: SECCOMP_FILTER_FLAG_* flags to set during sync.
585*46822860SKees Cook  *
586c2e1f2e3SKees Cook  * Expects sighand and cred_guard_mutex locks to be held, and for
587c2e1f2e3SKees Cook  * seccomp_can_sync_threads() to have returned success already
588c2e1f2e3SKees Cook  * without dropping the locks.
589c2e1f2e3SKees Cook  *
590c2e1f2e3SKees Cook  */
seccomp_sync_threads(unsigned long flags)59100a02d0cSKees Cook static inline void seccomp_sync_threads(unsigned long flags)
592c2e1f2e3SKees Cook {
593c2e1f2e3SKees Cook 	struct task_struct *thread, *caller;
594c2e1f2e3SKees Cook 
595c2e1f2e3SKees Cook 	BUG_ON(!mutex_is_locked(&current->signal->cred_guard_mutex));
59669f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
597c2e1f2e3SKees Cook 
598c2e1f2e3SKees Cook 	/* Synchronize all threads. */
599c2e1f2e3SKees Cook 	caller = current;
600c2e1f2e3SKees Cook 	for_each_thread(caller, thread) {
601c2e1f2e3SKees Cook 		/* Skip current, since it needs no changes. */
602c2e1f2e3SKees Cook 		if (thread == caller)
603c2e1f2e3SKees Cook 			continue;
604c2e1f2e3SKees Cook 
605c2e1f2e3SKees Cook 		/* Get a task reference for the new leaf node. */
606c2e1f2e3SKees Cook 		get_seccomp_filter(caller);
60799cdb8b9SChristian Brauner 
608c2e1f2e3SKees Cook 		/*
609c2e1f2e3SKees Cook 		 * Drop the task reference to the shared ancestor since
610c2e1f2e3SKees Cook 		 * current's path will hold a reference.  (This also
611c2e1f2e3SKees Cook 		 * allows a put before the assignment.)
612c2e1f2e3SKees Cook 		 */
61399cdb8b9SChristian Brauner 		__seccomp_filter_release(thread->seccomp.filter);
61499cdb8b9SChristian Brauner 
61599cdb8b9SChristian Brauner 		/* Make our new filter tree visible. */
616c2e1f2e3SKees Cook 		smp_store_release(&thread->seccomp.filter,
617c2e1f2e3SKees Cook 				  caller->seccomp.filter);
618c818c03bSKees Cook 		atomic_set(&thread->seccomp.filter_count,
619b4d8a58fSHsuan-Chi Kuo 			   atomic_read(&caller->seccomp.filter_count));
620103502a3SJann Horn 
621c2e1f2e3SKees Cook 		/*
622c2e1f2e3SKees Cook 		 * Don't let an unprivileged task work around
623c2e1f2e3SKees Cook 		 * the no_new_privs restriction by creating
624c2e1f2e3SKees Cook 		 * a thread that sets it up, enters seccomp,
625c2e1f2e3SKees Cook 		 * then dies.
626c2e1f2e3SKees Cook 		 */
627c2e1f2e3SKees Cook 		if (task_no_new_privs(caller))
628c2e1f2e3SKees Cook 			task_set_no_new_privs(thread);
629c2e1f2e3SKees Cook 
630103502a3SJann Horn 		/*
631103502a3SJann Horn 		 * Opt the other thread into seccomp if needed.
632103502a3SJann Horn 		 * As threads are considered to be trust-realm
633103502a3SJann Horn 		 * equivalent (see ptrace_may_access), it is safe to
634103502a3SJann Horn 		 * allow one thread to transition the other.
635103502a3SJann Horn 		 */
636103502a3SJann Horn 		if (thread->seccomp.mode == SECCOMP_MODE_DISABLED)
63700a02d0cSKees Cook 			seccomp_assign_mode(thread, SECCOMP_MODE_FILTER,
63800a02d0cSKees Cook 					    flags);
639c2e1f2e3SKees Cook 	}
640c2e1f2e3SKees Cook }
641c2e1f2e3SKees Cook 
642e2cfabdfSWill Drewry /**
643c8bee430SKees Cook  * seccomp_prepare_filter: Prepares a seccomp filter for use.
644e2cfabdfSWill Drewry  * @fprog: BPF program to install
645e2cfabdfSWill Drewry  *
646c8bee430SKees Cook  * Returns filter on success or an ERR_PTR on failure.
647e2cfabdfSWill Drewry  */
seccomp_prepare_filter(struct sock_fprog * fprog)648c8bee430SKees Cook static struct seccomp_filter *seccomp_prepare_filter(struct sock_fprog *fprog)
649e2cfabdfSWill Drewry {
650ac67eb2cSDaniel Borkmann 	struct seccomp_filter *sfilter;
651ac67eb2cSDaniel Borkmann 	int ret;
6528e01b51aSYiFei Zhu 	const bool save_orig =
6538e01b51aSYiFei Zhu #if defined(CONFIG_CHECKPOINT_RESTORE) || defined(SECCOMP_ARCH_NATIVE)
6548e01b51aSYiFei Zhu 		true;
6558e01b51aSYiFei Zhu #else
6568e01b51aSYiFei Zhu 		false;
6578e01b51aSYiFei Zhu #endif
658e2cfabdfSWill Drewry 
659e2cfabdfSWill Drewry 	if (fprog->len == 0 || fprog->len > BPF_MAXINSNS)
660c8bee430SKees Cook 		return ERR_PTR(-EINVAL);
661d9e12f42SNicolas Schichan 
662c8bee430SKees Cook 	BUG_ON(INT_MAX / fprog->len < sizeof(struct sock_filter));
663e2cfabdfSWill Drewry 
664e2cfabdfSWill Drewry 	/*
665119ce5c8SFabian Frederick 	 * Installing a seccomp filter requires that the task has
666e2cfabdfSWill Drewry 	 * CAP_SYS_ADMIN in its namespace or be running with no_new_privs.
667e2cfabdfSWill Drewry 	 * This avoids scenarios where unprivileged tasks can affect the
668e2cfabdfSWill Drewry 	 * behavior of privileged children.
669e2cfabdfSWill Drewry 	 */
6701d4457f9SKees Cook 	if (!task_no_new_privs(current) &&
671fb14528eSMickaël Salaün 			!ns_capable_noaudit(current_user_ns(), CAP_SYS_ADMIN))
672c8bee430SKees Cook 		return ERR_PTR(-EACCES);
673e2cfabdfSWill Drewry 
674bd4cf0edSAlexei Starovoitov 	/* Allocate a new seccomp_filter */
675ac67eb2cSDaniel Borkmann 	sfilter = kzalloc(sizeof(*sfilter), GFP_KERNEL | __GFP_NOWARN);
676ac67eb2cSDaniel Borkmann 	if (!sfilter)
677d9e12f42SNicolas Schichan 		return ERR_PTR(-ENOMEM);
678ac67eb2cSDaniel Borkmann 
6796a21cc50STycho Andersen 	mutex_init(&sfilter->notify_lock);
680ac67eb2cSDaniel Borkmann 	ret = bpf_prog_create_from_user(&sfilter->prog, fprog,
681f8e529edSTycho Andersen 					seccomp_check_filter, save_orig);
682ac67eb2cSDaniel Borkmann 	if (ret < 0) {
683ac67eb2cSDaniel Borkmann 		kfree(sfilter);
684ac67eb2cSDaniel Borkmann 		return ERR_PTR(ret);
685d9e12f42SNicolas Schichan 	}
686bd4cf0edSAlexei Starovoitov 
687b707ddeeSChristian Brauner 	refcount_set(&sfilter->refs, 1);
68899cdb8b9SChristian Brauner 	refcount_set(&sfilter->users, 1);
68976194c4eSChristian Brauner 	init_waitqueue_head(&sfilter->wqh);
690e2cfabdfSWill Drewry 
691ac67eb2cSDaniel Borkmann 	return sfilter;
692e2cfabdfSWill Drewry }
693e2cfabdfSWill Drewry 
694e2cfabdfSWill Drewry /**
695c8bee430SKees Cook  * seccomp_prepare_user_filter - prepares a user-supplied sock_fprog
696e2cfabdfSWill Drewry  * @user_filter: pointer to the user data containing a sock_fprog.
697e2cfabdfSWill Drewry  *
698e2cfabdfSWill Drewry  * Returns 0 on success and non-zero otherwise.
699e2cfabdfSWill Drewry  */
700c8bee430SKees Cook static struct seccomp_filter *
seccomp_prepare_user_filter(const char __user * user_filter)701c8bee430SKees Cook seccomp_prepare_user_filter(const char __user *user_filter)
702e2cfabdfSWill Drewry {
703e2cfabdfSWill Drewry 	struct sock_fprog fprog;
704c8bee430SKees Cook 	struct seccomp_filter *filter = ERR_PTR(-EFAULT);
705e2cfabdfSWill Drewry 
706e2cfabdfSWill Drewry #ifdef CONFIG_COMPAT
7075c38065eSAndy Lutomirski 	if (in_compat_syscall()) {
708e2cfabdfSWill Drewry 		struct compat_sock_fprog fprog32;
709e2cfabdfSWill Drewry 		if (copy_from_user(&fprog32, user_filter, sizeof(fprog32)))
710e2cfabdfSWill Drewry 			goto out;
711e2cfabdfSWill Drewry 		fprog.len = fprog32.len;
712e2cfabdfSWill Drewry 		fprog.filter = compat_ptr(fprog32.filter);
713e2cfabdfSWill Drewry 	} else /* falls through to the if below. */
714e2cfabdfSWill Drewry #endif
715e2cfabdfSWill Drewry 	if (copy_from_user(&fprog, user_filter, sizeof(fprog)))
716e2cfabdfSWill Drewry 		goto out;
717c8bee430SKees Cook 	filter = seccomp_prepare_filter(&fprog);
718e2cfabdfSWill Drewry out:
719c8bee430SKees Cook 	return filter;
720c8bee430SKees Cook }
721c8bee430SKees Cook 
7228e01b51aSYiFei Zhu #ifdef SECCOMP_ARCH_NATIVE
7238e01b51aSYiFei Zhu /**
7248e01b51aSYiFei Zhu  * seccomp_is_const_allow - check if filter is constant allow with given data
7258e01b51aSYiFei Zhu  * @fprog: The BPF programs
7268e01b51aSYiFei Zhu  * @sd: The seccomp data to check against, only syscall number and arch
7278e01b51aSYiFei Zhu  *      number are considered constant.
7288e01b51aSYiFei Zhu  */
seccomp_is_const_allow(struct sock_fprog_kern * fprog,struct seccomp_data * sd)7298e01b51aSYiFei Zhu static bool seccomp_is_const_allow(struct sock_fprog_kern *fprog,
7308e01b51aSYiFei Zhu 				   struct seccomp_data *sd)
7318e01b51aSYiFei Zhu {
7328e01b51aSYiFei Zhu 	unsigned int reg_value = 0;
7338e01b51aSYiFei Zhu 	unsigned int pc;
7348e01b51aSYiFei Zhu 	bool op_res;
7358e01b51aSYiFei Zhu 
7368e01b51aSYiFei Zhu 	if (WARN_ON_ONCE(!fprog))
7378e01b51aSYiFei Zhu 		return false;
7388e01b51aSYiFei Zhu 
7398e01b51aSYiFei Zhu 	for (pc = 0; pc < fprog->len; pc++) {
7408e01b51aSYiFei Zhu 		struct sock_filter *insn = &fprog->filter[pc];
7418e01b51aSYiFei Zhu 		u16 code = insn->code;
7428e01b51aSYiFei Zhu 		u32 k = insn->k;
7438e01b51aSYiFei Zhu 
7448e01b51aSYiFei Zhu 		switch (code) {
7458e01b51aSYiFei Zhu 		case BPF_LD | BPF_W | BPF_ABS:
7468e01b51aSYiFei Zhu 			switch (k) {
7478e01b51aSYiFei Zhu 			case offsetof(struct seccomp_data, nr):
7488e01b51aSYiFei Zhu 				reg_value = sd->nr;
7498e01b51aSYiFei Zhu 				break;
7508e01b51aSYiFei Zhu 			case offsetof(struct seccomp_data, arch):
7518e01b51aSYiFei Zhu 				reg_value = sd->arch;
7528e01b51aSYiFei Zhu 				break;
7538e01b51aSYiFei Zhu 			default:
7548e01b51aSYiFei Zhu 				/* can't optimize (non-constant value load) */
7558e01b51aSYiFei Zhu 				return false;
7568e01b51aSYiFei Zhu 			}
7578e01b51aSYiFei Zhu 			break;
7588e01b51aSYiFei Zhu 		case BPF_RET | BPF_K:
7598e01b51aSYiFei Zhu 			/* reached return with constant values only, check allow */
7608e01b51aSYiFei Zhu 			return k == SECCOMP_RET_ALLOW;
7618e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JA:
7628e01b51aSYiFei Zhu 			pc += insn->k;
7638e01b51aSYiFei Zhu 			break;
7648e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JEQ | BPF_K:
7658e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JGE | BPF_K:
7668e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JGT | BPF_K:
7678e01b51aSYiFei Zhu 		case BPF_JMP | BPF_JSET | BPF_K:
7688e01b51aSYiFei Zhu 			switch (BPF_OP(code)) {
7698e01b51aSYiFei Zhu 			case BPF_JEQ:
7708e01b51aSYiFei Zhu 				op_res = reg_value == k;
7718e01b51aSYiFei Zhu 				break;
7728e01b51aSYiFei Zhu 			case BPF_JGE:
7738e01b51aSYiFei Zhu 				op_res = reg_value >= k;
7748e01b51aSYiFei Zhu 				break;
7758e01b51aSYiFei Zhu 			case BPF_JGT:
7768e01b51aSYiFei Zhu 				op_res = reg_value > k;
7778e01b51aSYiFei Zhu 				break;
7788e01b51aSYiFei Zhu 			case BPF_JSET:
7798e01b51aSYiFei Zhu 				op_res = !!(reg_value & k);
7808e01b51aSYiFei Zhu 				break;
7818e01b51aSYiFei Zhu 			default:
7828e01b51aSYiFei Zhu 				/* can't optimize (unknown jump) */
7838e01b51aSYiFei Zhu 				return false;
7848e01b51aSYiFei Zhu 			}
7858e01b51aSYiFei Zhu 
7868e01b51aSYiFei Zhu 			pc += op_res ? insn->jt : insn->jf;
7878e01b51aSYiFei Zhu 			break;
7888e01b51aSYiFei Zhu 		case BPF_ALU | BPF_AND | BPF_K:
7898e01b51aSYiFei Zhu 			reg_value &= k;
7908e01b51aSYiFei Zhu 			break;
7918e01b51aSYiFei Zhu 		default:
7928e01b51aSYiFei Zhu 			/* can't optimize (unknown insn) */
7938e01b51aSYiFei Zhu 			return false;
7948e01b51aSYiFei Zhu 		}
7958e01b51aSYiFei Zhu 	}
7968e01b51aSYiFei Zhu 
7978e01b51aSYiFei Zhu 	/* ran off the end of the filter?! */
7988e01b51aSYiFei Zhu 	WARN_ON(1);
7998e01b51aSYiFei Zhu 	return false;
8008e01b51aSYiFei Zhu }
8018e01b51aSYiFei Zhu 
seccomp_cache_prepare_bitmap(struct seccomp_filter * sfilter,void * bitmap,const void * bitmap_prev,size_t bitmap_size,int arch)8028e01b51aSYiFei Zhu static void seccomp_cache_prepare_bitmap(struct seccomp_filter *sfilter,
8038e01b51aSYiFei Zhu 					 void *bitmap, const void *bitmap_prev,
8048e01b51aSYiFei Zhu 					 size_t bitmap_size, int arch)
8058e01b51aSYiFei Zhu {
8068e01b51aSYiFei Zhu 	struct sock_fprog_kern *fprog = sfilter->prog->orig_prog;
8078e01b51aSYiFei Zhu 	struct seccomp_data sd;
8088e01b51aSYiFei Zhu 	int nr;
8098e01b51aSYiFei Zhu 
8108e01b51aSYiFei Zhu 	if (bitmap_prev) {
8118e01b51aSYiFei Zhu 		/* The new filter must be as restrictive as the last. */
8128e01b51aSYiFei Zhu 		bitmap_copy(bitmap, bitmap_prev, bitmap_size);
8138e01b51aSYiFei Zhu 	} else {
8148e01b51aSYiFei Zhu 		/* Before any filters, all syscalls are always allowed. */
8158e01b51aSYiFei Zhu 		bitmap_fill(bitmap, bitmap_size);
8168e01b51aSYiFei Zhu 	}
8178e01b51aSYiFei Zhu 
8188e01b51aSYiFei Zhu 	for (nr = 0; nr < bitmap_size; nr++) {
8198e01b51aSYiFei Zhu 		/* No bitmap change: not a cacheable action. */
8208e01b51aSYiFei Zhu 		if (!test_bit(nr, bitmap))
8218e01b51aSYiFei Zhu 			continue;
8228e01b51aSYiFei Zhu 
8238e01b51aSYiFei Zhu 		sd.nr = nr;
8248e01b51aSYiFei Zhu 		sd.arch = arch;
8258e01b51aSYiFei Zhu 
8268e01b51aSYiFei Zhu 		/* No bitmap change: continue to always allow. */
8278e01b51aSYiFei Zhu 		if (seccomp_is_const_allow(fprog, &sd))
8288e01b51aSYiFei Zhu 			continue;
8298e01b51aSYiFei Zhu 
8308e01b51aSYiFei Zhu 		/*
8318e01b51aSYiFei Zhu 		 * Not a cacheable action: always run filters.
8328e01b51aSYiFei Zhu 		 * atomic clear_bit() not needed, filter not visible yet.
8338e01b51aSYiFei Zhu 		 */
8348e01b51aSYiFei Zhu 		__clear_bit(nr, bitmap);
8358e01b51aSYiFei Zhu 	}
8368e01b51aSYiFei Zhu }
8378e01b51aSYiFei Zhu 
8388e01b51aSYiFei Zhu /**
839a3fc712cSCui GaoSheng  * seccomp_cache_prepare - emulate the filter to find cacheable syscalls
8408e01b51aSYiFei Zhu  * @sfilter: The seccomp filter
8418e01b51aSYiFei Zhu  *
8428e01b51aSYiFei Zhu  * Returns 0 if successful or -errno if error occurred.
8438e01b51aSYiFei Zhu  */
seccomp_cache_prepare(struct seccomp_filter * sfilter)8448e01b51aSYiFei Zhu static void seccomp_cache_prepare(struct seccomp_filter *sfilter)
8458e01b51aSYiFei Zhu {
8468e01b51aSYiFei Zhu 	struct action_cache *cache = &sfilter->cache;
8478e01b51aSYiFei Zhu 	const struct action_cache *cache_prev =
8488e01b51aSYiFei Zhu 		sfilter->prev ? &sfilter->prev->cache : NULL;
8498e01b51aSYiFei Zhu 
8508e01b51aSYiFei Zhu 	seccomp_cache_prepare_bitmap(sfilter, cache->allow_native,
8518e01b51aSYiFei Zhu 				     cache_prev ? cache_prev->allow_native : NULL,
8528e01b51aSYiFei Zhu 				     SECCOMP_ARCH_NATIVE_NR,
8538e01b51aSYiFei Zhu 				     SECCOMP_ARCH_NATIVE);
8548e01b51aSYiFei Zhu 
8558e01b51aSYiFei Zhu #ifdef SECCOMP_ARCH_COMPAT
8568e01b51aSYiFei Zhu 	seccomp_cache_prepare_bitmap(sfilter, cache->allow_compat,
8578e01b51aSYiFei Zhu 				     cache_prev ? cache_prev->allow_compat : NULL,
8588e01b51aSYiFei Zhu 				     SECCOMP_ARCH_COMPAT_NR,
8598e01b51aSYiFei Zhu 				     SECCOMP_ARCH_COMPAT);
8608e01b51aSYiFei Zhu #endif /* SECCOMP_ARCH_COMPAT */
8618e01b51aSYiFei Zhu }
8628e01b51aSYiFei Zhu #endif /* SECCOMP_ARCH_NATIVE */
8638e01b51aSYiFei Zhu 
864c8bee430SKees Cook /**
865c8bee430SKees Cook  * seccomp_attach_filter: validate and attach filter
866c8bee430SKees Cook  * @flags:  flags to change filter behavior
867c8bee430SKees Cook  * @filter: seccomp filter to add to the current process
868c8bee430SKees Cook  *
869dbd95212SKees Cook  * Caller must be holding current->sighand->siglock lock.
870dbd95212SKees Cook  *
8717a0df7fbSTycho Andersen  * Returns 0 on success, -ve on error, or
8727a0df7fbSTycho Andersen  *   - in TSYNC mode: the pid of a thread which was either not in the correct
8737a0df7fbSTycho Andersen  *     seccomp mode or did not have an ancestral seccomp filter
8747a0df7fbSTycho Andersen  *   - in NEW_LISTENER mode: the fd of the new listener
875c8bee430SKees Cook  */
seccomp_attach_filter(unsigned int flags,struct seccomp_filter * filter)876c8bee430SKees Cook static long seccomp_attach_filter(unsigned int flags,
877c8bee430SKees Cook 				  struct seccomp_filter *filter)
878c8bee430SKees Cook {
879c8bee430SKees Cook 	unsigned long total_insns;
880c8bee430SKees Cook 	struct seccomp_filter *walker;
881c8bee430SKees Cook 
88269f6a34bSGuenter Roeck 	assert_spin_locked(&current->sighand->siglock);
883dbd95212SKees Cook 
884c8bee430SKees Cook 	/* Validate resulting filter length. */
885c8bee430SKees Cook 	total_insns = filter->prog->len;
886c8bee430SKees Cook 	for (walker = current->seccomp.filter; walker; walker = walker->prev)
887c8bee430SKees Cook 		total_insns += walker->prog->len + 4;  /* 4 instr penalty */
888c8bee430SKees Cook 	if (total_insns > MAX_INSNS_PER_PATH)
889c8bee430SKees Cook 		return -ENOMEM;
890c8bee430SKees Cook 
891c2e1f2e3SKees Cook 	/* If thread sync has been requested, check that it is possible. */
892c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC) {
893c2e1f2e3SKees Cook 		int ret;
894c2e1f2e3SKees Cook 
895c2e1f2e3SKees Cook 		ret = seccomp_can_sync_threads();
89651891498STycho Andersen 		if (ret) {
89751891498STycho Andersen 			if (flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH)
89851891498STycho Andersen 				return -ESRCH;
89951891498STycho Andersen 			else
900c2e1f2e3SKees Cook 				return ret;
901c2e1f2e3SKees Cook 		}
90251891498STycho Andersen 	}
903c2e1f2e3SKees Cook 
904e66a3997STyler Hicks 	/* Set log flag, if present. */
905e66a3997STyler Hicks 	if (flags & SECCOMP_FILTER_FLAG_LOG)
906e66a3997STyler Hicks 		filter->log = true;
907e66a3997STyler Hicks 
908c2aa2dfeSSargun Dhillon 	/* Set wait killable flag, if present. */
909c2aa2dfeSSargun Dhillon 	if (flags & SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV)
910c2aa2dfeSSargun Dhillon 		filter->wait_killable_recv = true;
911c2aa2dfeSSargun Dhillon 
912c8bee430SKees Cook 	/*
913c8bee430SKees Cook 	 * If there is an existing filter, make it the prev and don't drop its
914c8bee430SKees Cook 	 * task reference.
915c8bee430SKees Cook 	 */
916c8bee430SKees Cook 	filter->prev = current->seccomp.filter;
9178e01b51aSYiFei Zhu 	seccomp_cache_prepare(filter);
918c8bee430SKees Cook 	current->seccomp.filter = filter;
919c818c03bSKees Cook 	atomic_inc(&current->seccomp.filter_count);
920c8bee430SKees Cook 
921c2e1f2e3SKees Cook 	/* Now that the new filter is in place, synchronize to all threads. */
922c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC)
92300a02d0cSKees Cook 		seccomp_sync_threads(flags);
924c2e1f2e3SKees Cook 
925c8bee430SKees Cook 	return 0;
926e2cfabdfSWill Drewry }
927e2cfabdfSWill Drewry 
__get_seccomp_filter(struct seccomp_filter * filter)928084f5601SColin Ian King static void __get_seccomp_filter(struct seccomp_filter *filter)
92966a733eaSOleg Nesterov {
930b707ddeeSChristian Brauner 	refcount_inc(&filter->refs);
93166a733eaSOleg Nesterov }
93266a733eaSOleg Nesterov 
933e2cfabdfSWill Drewry /* get_seccomp_filter - increments the reference count of the filter on @tsk */
get_seccomp_filter(struct task_struct * tsk)934e2cfabdfSWill Drewry void get_seccomp_filter(struct task_struct *tsk)
935e2cfabdfSWill Drewry {
936e2cfabdfSWill Drewry 	struct seccomp_filter *orig = tsk->seccomp.filter;
937e2cfabdfSWill Drewry 	if (!orig)
938e2cfabdfSWill Drewry 		return;
93966a733eaSOleg Nesterov 	__get_seccomp_filter(orig);
94099cdb8b9SChristian Brauner 	refcount_inc(&orig->users);
941e2cfabdfSWill Drewry }
942e2cfabdfSWill Drewry 
943e2cfabdfSWill Drewry #endif	/* CONFIG_SECCOMP_FILTER */
9441da177e4SLinus Torvalds 
9450ddec0fcSTyler Hicks /* For use with seccomp_actions_logged */
9464d3b0b05SKees Cook #define SECCOMP_LOG_KILL_PROCESS	(1 << 0)
9474d3b0b05SKees Cook #define SECCOMP_LOG_KILL_THREAD		(1 << 1)
9480ddec0fcSTyler Hicks #define SECCOMP_LOG_TRAP		(1 << 2)
9490ddec0fcSTyler Hicks #define SECCOMP_LOG_ERRNO		(1 << 3)
9500ddec0fcSTyler Hicks #define SECCOMP_LOG_TRACE		(1 << 4)
95159f5cf44STyler Hicks #define SECCOMP_LOG_LOG			(1 << 5)
95259f5cf44STyler Hicks #define SECCOMP_LOG_ALLOW		(1 << 6)
9536a21cc50STycho Andersen #define SECCOMP_LOG_USER_NOTIF		(1 << 7)
9540ddec0fcSTyler Hicks 
9554d3b0b05SKees Cook static u32 seccomp_actions_logged = SECCOMP_LOG_KILL_PROCESS |
9564d3b0b05SKees Cook 				    SECCOMP_LOG_KILL_THREAD  |
957fd76875cSKees Cook 				    SECCOMP_LOG_TRAP  |
958fd76875cSKees Cook 				    SECCOMP_LOG_ERRNO |
9596a21cc50STycho Andersen 				    SECCOMP_LOG_USER_NOTIF |
960fd76875cSKees Cook 				    SECCOMP_LOG_TRACE |
96159f5cf44STyler Hicks 				    SECCOMP_LOG_LOG;
9620ddec0fcSTyler Hicks 
seccomp_log(unsigned long syscall,long signr,u32 action,bool requested)963e66a3997STyler Hicks static inline void seccomp_log(unsigned long syscall, long signr, u32 action,
964e66a3997STyler Hicks 			       bool requested)
9650ddec0fcSTyler Hicks {
9660ddec0fcSTyler Hicks 	bool log = false;
9670ddec0fcSTyler Hicks 
9680ddec0fcSTyler Hicks 	switch (action) {
9690ddec0fcSTyler Hicks 	case SECCOMP_RET_ALLOW:
970e66a3997STyler Hicks 		break;
9710ddec0fcSTyler Hicks 	case SECCOMP_RET_TRAP:
972e66a3997STyler Hicks 		log = requested && seccomp_actions_logged & SECCOMP_LOG_TRAP;
973e66a3997STyler Hicks 		break;
9740ddec0fcSTyler Hicks 	case SECCOMP_RET_ERRNO:
975e66a3997STyler Hicks 		log = requested && seccomp_actions_logged & SECCOMP_LOG_ERRNO;
976e66a3997STyler Hicks 		break;
9770ddec0fcSTyler Hicks 	case SECCOMP_RET_TRACE:
978e66a3997STyler Hicks 		log = requested && seccomp_actions_logged & SECCOMP_LOG_TRACE;
9790ddec0fcSTyler Hicks 		break;
9806a21cc50STycho Andersen 	case SECCOMP_RET_USER_NOTIF:
9816a21cc50STycho Andersen 		log = requested && seccomp_actions_logged & SECCOMP_LOG_USER_NOTIF;
9826a21cc50STycho Andersen 		break;
98359f5cf44STyler Hicks 	case SECCOMP_RET_LOG:
98459f5cf44STyler Hicks 		log = seccomp_actions_logged & SECCOMP_LOG_LOG;
98559f5cf44STyler Hicks 		break;
986fd76875cSKees Cook 	case SECCOMP_RET_KILL_THREAD:
987fd76875cSKees Cook 		log = seccomp_actions_logged & SECCOMP_LOG_KILL_THREAD;
9884d3b0b05SKees Cook 		break;
9894d3b0b05SKees Cook 	case SECCOMP_RET_KILL_PROCESS:
9904d3b0b05SKees Cook 	default:
9914d3b0b05SKees Cook 		log = seccomp_actions_logged & SECCOMP_LOG_KILL_PROCESS;
9920ddec0fcSTyler Hicks 	}
9930ddec0fcSTyler Hicks 
9940ddec0fcSTyler Hicks 	/*
995326bee02STyler Hicks 	 * Emit an audit message when the action is RET_KILL_*, RET_LOG, or the
996326bee02STyler Hicks 	 * FILTER_FLAG_LOG bit was set. The admin has the ability to silence
997326bee02STyler Hicks 	 * any action from being logged by removing the action name from the
998326bee02STyler Hicks 	 * seccomp_actions_logged sysctl.
9990ddec0fcSTyler Hicks 	 */
1000326bee02STyler Hicks 	if (!log)
1001326bee02STyler Hicks 		return;
10020ddec0fcSTyler Hicks 
1003326bee02STyler Hicks 	audit_seccomp(syscall, signr, action);
10040ddec0fcSTyler Hicks }
10050ddec0fcSTyler Hicks 
10061da177e4SLinus Torvalds /*
10071da177e4SLinus Torvalds  * Secure computing mode 1 allows only read/write/exit/sigreturn.
10081da177e4SLinus Torvalds  * To be fully secure this must be combined with rlimit
10091da177e4SLinus Torvalds  * to limit the stack allocations too.
10101da177e4SLinus Torvalds  */
1011cb4253aaSMatt Redfearn static const int mode1_syscalls[] = {
10121da177e4SLinus Torvalds 	__NR_seccomp_read, __NR_seccomp_write, __NR_seccomp_exit, __NR_seccomp_sigreturn,
1013fe4bfff8SKees Cook 	-1, /* negative terminated */
10141da177e4SLinus Torvalds };
10151da177e4SLinus Torvalds 
__secure_computing_strict(int this_syscall)1016a4412fc9SAndy Lutomirski static void __secure_computing_strict(int this_syscall)
10171da177e4SLinus Torvalds {
1018fe4bfff8SKees Cook 	const int *allowed_syscalls = mode1_syscalls;
1019a4412fc9SAndy Lutomirski #ifdef CONFIG_COMPAT
10205c38065eSAndy Lutomirski 	if (in_compat_syscall())
1021fe4bfff8SKees Cook 		allowed_syscalls = get_compat_mode1_syscalls();
1022a4412fc9SAndy Lutomirski #endif
1023a4412fc9SAndy Lutomirski 	do {
1024fe4bfff8SKees Cook 		if (*allowed_syscalls == this_syscall)
1025a4412fc9SAndy Lutomirski 			return;
1026fe4bfff8SKees Cook 	} while (*++allowed_syscalls != -1);
1027a4412fc9SAndy Lutomirski 
1028a4412fc9SAndy Lutomirski #ifdef SECCOMP_DEBUG
1029a4412fc9SAndy Lutomirski 	dump_stack();
1030a4412fc9SAndy Lutomirski #endif
1031495ac306SKees Cook 	current->seccomp.mode = SECCOMP_MODE_DEAD;
1032fd76875cSKees Cook 	seccomp_log(this_syscall, SIGKILL, SECCOMP_RET_KILL_THREAD, true);
1033a4412fc9SAndy Lutomirski 	do_exit(SIGKILL);
1034a4412fc9SAndy Lutomirski }
1035a4412fc9SAndy Lutomirski 
1036a4412fc9SAndy Lutomirski #ifndef CONFIG_HAVE_ARCH_SECCOMP_FILTER
secure_computing_strict(int this_syscall)1037a4412fc9SAndy Lutomirski void secure_computing_strict(int this_syscall)
1038a4412fc9SAndy Lutomirski {
1039a4412fc9SAndy Lutomirski 	int mode = current->seccomp.mode;
1040a4412fc9SAndy Lutomirski 
104197f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
104213c4a901STycho Andersen 	    unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
104313c4a901STycho Andersen 		return;
104413c4a901STycho Andersen 
1045221272f9SKees Cook 	if (mode == SECCOMP_MODE_DISABLED)
1046a4412fc9SAndy Lutomirski 		return;
1047a4412fc9SAndy Lutomirski 	else if (mode == SECCOMP_MODE_STRICT)
1048a4412fc9SAndy Lutomirski 		__secure_computing_strict(this_syscall);
1049a4412fc9SAndy Lutomirski 	else
1050a4412fc9SAndy Lutomirski 		BUG();
1051a4412fc9SAndy Lutomirski }
1052a4412fc9SAndy Lutomirski #else
105313aa72f0SAndy Lutomirski 
105413aa72f0SAndy Lutomirski #ifdef CONFIG_SECCOMP_FILTER
seccomp_next_notify_id(struct seccomp_filter * filter)10556a21cc50STycho Andersen static u64 seccomp_next_notify_id(struct seccomp_filter *filter)
10566a21cc50STycho Andersen {
10576a21cc50STycho Andersen 	/*
10586a21cc50STycho Andersen 	 * Note: overflow is ok here, the id just needs to be unique per
10596a21cc50STycho Andersen 	 * filter.
10606a21cc50STycho Andersen 	 */
10616a21cc50STycho Andersen 	lockdep_assert_held(&filter->notify_lock);
10626a21cc50STycho Andersen 	return filter->notif->next_id++;
10636a21cc50STycho Andersen }
10646a21cc50STycho Andersen 
seccomp_handle_addfd(struct seccomp_kaddfd * addfd,struct seccomp_knotif * n)10650ae71c77SRodrigo Campos static void seccomp_handle_addfd(struct seccomp_kaddfd *addfd, struct seccomp_knotif *n)
10667cf97b12SSargun Dhillon {
10670ae71c77SRodrigo Campos 	int fd;
10680ae71c77SRodrigo Campos 
10697cf97b12SSargun Dhillon 	/*
10707cf97b12SSargun Dhillon 	 * Remove the notification, and reset the list pointers, indicating
10717cf97b12SSargun Dhillon 	 * that it has been handled.
10727cf97b12SSargun Dhillon 	 */
10737cf97b12SSargun Dhillon 	list_del_init(&addfd->list);
107442eb0d54SChristoph Hellwig 	if (!addfd->setfd)
10750ae71c77SRodrigo Campos 		fd = receive_fd(addfd->file, addfd->flags);
107642eb0d54SChristoph Hellwig 	else
10770ae71c77SRodrigo Campos 		fd = receive_fd_replace(addfd->fd, addfd->file, addfd->flags);
10780ae71c77SRodrigo Campos 	addfd->ret = fd;
10790ae71c77SRodrigo Campos 
10800ae71c77SRodrigo Campos 	if (addfd->ioctl_flags & SECCOMP_ADDFD_FLAG_SEND) {
10810ae71c77SRodrigo Campos 		/* If we fail reset and return an error to the notifier */
10820ae71c77SRodrigo Campos 		if (fd < 0) {
10830ae71c77SRodrigo Campos 			n->state = SECCOMP_NOTIFY_SENT;
10840ae71c77SRodrigo Campos 		} else {
10850ae71c77SRodrigo Campos 			/* Return the FD we just added */
10860ae71c77SRodrigo Campos 			n->flags = 0;
10870ae71c77SRodrigo Campos 			n->error = 0;
10880ae71c77SRodrigo Campos 			n->val = fd;
10890ae71c77SRodrigo Campos 		}
10900ae71c77SRodrigo Campos 	}
10910ae71c77SRodrigo Campos 
10920ae71c77SRodrigo Campos 	/*
10930ae71c77SRodrigo Campos 	 * Mark the notification as completed. From this point, addfd mem
10940ae71c77SRodrigo Campos 	 * might be invalidated and we can't safely read it anymore.
10950ae71c77SRodrigo Campos 	 */
10967cf97b12SSargun Dhillon 	complete(&addfd->completion);
10977cf97b12SSargun Dhillon }
10987cf97b12SSargun Dhillon 
should_sleep_killable(struct seccomp_filter * match,struct seccomp_knotif * n)1099c2aa2dfeSSargun Dhillon static bool should_sleep_killable(struct seccomp_filter *match,
1100c2aa2dfeSSargun Dhillon 				  struct seccomp_knotif *n)
1101c2aa2dfeSSargun Dhillon {
1102c2aa2dfeSSargun Dhillon 	return match->wait_killable_recv && n->state == SECCOMP_NOTIFY_SENT;
1103c2aa2dfeSSargun Dhillon }
1104c2aa2dfeSSargun Dhillon 
seccomp_do_user_notification(int this_syscall,struct seccomp_filter * match,const struct seccomp_data * sd)1105fb3c5386SChristian Brauner static int seccomp_do_user_notification(int this_syscall,
11066a21cc50STycho Andersen 					struct seccomp_filter *match,
11076a21cc50STycho Andersen 					const struct seccomp_data *sd)
11086a21cc50STycho Andersen {
11096a21cc50STycho Andersen 	int err;
1110fb3c5386SChristian Brauner 	u32 flags = 0;
11116a21cc50STycho Andersen 	long ret = 0;
11126a21cc50STycho Andersen 	struct seccomp_knotif n = {};
11137cf97b12SSargun Dhillon 	struct seccomp_kaddfd *addfd, *tmp;
11146a21cc50STycho Andersen 
11156a21cc50STycho Andersen 	mutex_lock(&match->notify_lock);
11166a21cc50STycho Andersen 	err = -ENOSYS;
11176a21cc50STycho Andersen 	if (!match->notif)
11186a21cc50STycho Andersen 		goto out;
11196a21cc50STycho Andersen 
11206a21cc50STycho Andersen 	n.task = current;
11216a21cc50STycho Andersen 	n.state = SECCOMP_NOTIFY_INIT;
11226a21cc50STycho Andersen 	n.data = sd;
11236a21cc50STycho Andersen 	n.id = seccomp_next_notify_id(match);
11246a21cc50STycho Andersen 	init_completion(&n.ready);
11254cbf6f62SSargun Dhillon 	list_add_tail(&n.list, &match->notif->notifications);
11267cf97b12SSargun Dhillon 	INIT_LIST_HEAD(&n.addfd);
11276a21cc50STycho Andersen 
11284943b66dSAndrei Vagin 	atomic_inc(&match->notif->requests);
112948a1084aSAndrei Vagin 	if (match->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
113048a1084aSAndrei Vagin 		wake_up_poll_on_current_cpu(&match->wqh, EPOLLIN | EPOLLRDNORM);
113148a1084aSAndrei Vagin 	else
113276194c4eSChristian Brauner 		wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
11336a21cc50STycho Andersen 
11346a21cc50STycho Andersen 	/*
11356a21cc50STycho Andersen 	 * This is where we wait for a reply from userspace.
11366a21cc50STycho Andersen 	 */
1137ddc47391SSargun Dhillon 	do {
1138c2aa2dfeSSargun Dhillon 		bool wait_killable = should_sleep_killable(match, &n);
1139c2aa2dfeSSargun Dhillon 
1140ddc47391SSargun Dhillon 		mutex_unlock(&match->notify_lock);
1141c2aa2dfeSSargun Dhillon 		if (wait_killable)
1142c2aa2dfeSSargun Dhillon 			err = wait_for_completion_killable(&n.ready);
1143c2aa2dfeSSargun Dhillon 		else
11446a21cc50STycho Andersen 			err = wait_for_completion_interruptible(&n.ready);
11456a21cc50STycho Andersen 		mutex_lock(&match->notify_lock);
1146c2aa2dfeSSargun Dhillon 
1147c2aa2dfeSSargun Dhillon 		if (err != 0) {
1148c2aa2dfeSSargun Dhillon 			/*
1149c2aa2dfeSSargun Dhillon 			 * Check to see if the notifcation got picked up and
1150c2aa2dfeSSargun Dhillon 			 * whether we should switch to wait killable.
1151c2aa2dfeSSargun Dhillon 			 */
1152c2aa2dfeSSargun Dhillon 			if (!wait_killable && should_sleep_killable(match, &n))
1153c2aa2dfeSSargun Dhillon 				continue;
1154c2aa2dfeSSargun Dhillon 
1155ddc47391SSargun Dhillon 			goto interrupted;
1156c2aa2dfeSSargun Dhillon 		}
1157ddc47391SSargun Dhillon 
11587cf97b12SSargun Dhillon 		addfd = list_first_entry_or_null(&n.addfd,
11597cf97b12SSargun Dhillon 						 struct seccomp_kaddfd, list);
1160ddc47391SSargun Dhillon 		/* Check if we were woken up by a addfd message */
1161ddc47391SSargun Dhillon 		if (addfd)
11620ae71c77SRodrigo Campos 			seccomp_handle_addfd(addfd, &n);
1163ddc47391SSargun Dhillon 
1164ddc47391SSargun Dhillon 	}  while (n.state != SECCOMP_NOTIFY_REPLIED);
1165ddc47391SSargun Dhillon 
11666a21cc50STycho Andersen 	ret = n.val;
11676a21cc50STycho Andersen 	err = n.error;
1168fb3c5386SChristian Brauner 	flags = n.flags;
11696a21cc50STycho Andersen 
1170ddc47391SSargun Dhillon interrupted:
11717cf97b12SSargun Dhillon 	/* If there were any pending addfd calls, clear them out */
11727cf97b12SSargun Dhillon 	list_for_each_entry_safe(addfd, tmp, &n.addfd, list) {
11737cf97b12SSargun Dhillon 		/* The process went away before we got a chance to handle it */
11747cf97b12SSargun Dhillon 		addfd->ret = -ESRCH;
11757cf97b12SSargun Dhillon 		list_del_init(&addfd->list);
11767cf97b12SSargun Dhillon 		complete(&addfd->completion);
11777cf97b12SSargun Dhillon 	}
11787cf97b12SSargun Dhillon 
11796a21cc50STycho Andersen 	/*
11806a21cc50STycho Andersen 	 * Note that it's possible the listener died in between the time when
11817cf97b12SSargun Dhillon 	 * we were notified of a response (or a signal) and when we were able to
11826a21cc50STycho Andersen 	 * re-acquire the lock, so only delete from the list if the
11836a21cc50STycho Andersen 	 * notification actually exists.
11846a21cc50STycho Andersen 	 *
11856a21cc50STycho Andersen 	 * Also note that this test is only valid because there's no way to
11866a21cc50STycho Andersen 	 * *reattach* to a notifier right now. If one is added, we'll need to
11876a21cc50STycho Andersen 	 * keep track of the notif itself and make sure they match here.
11886a21cc50STycho Andersen 	 */
11896a21cc50STycho Andersen 	if (match->notif)
11906a21cc50STycho Andersen 		list_del(&n.list);
11916a21cc50STycho Andersen out:
11926a21cc50STycho Andersen 	mutex_unlock(&match->notify_lock);
1193fb3c5386SChristian Brauner 
1194fb3c5386SChristian Brauner 	/* Userspace requests to continue the syscall. */
1195fb3c5386SChristian Brauner 	if (flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE)
1196fb3c5386SChristian Brauner 		return 0;
1197fb3c5386SChristian Brauner 
11982d9ca267SDenis Efremov 	syscall_set_return_value(current, current_pt_regs(),
11996a21cc50STycho Andersen 				 err, ret);
1200fb3c5386SChristian Brauner 	return -1;
12016a21cc50STycho Andersen }
12026a21cc50STycho Andersen 
__seccomp_filter(int this_syscall,const struct seccomp_data * sd,const bool recheck_after_trace)1203ce6526e8SKees Cook static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
1204ce6526e8SKees Cook 			    const bool recheck_after_trace)
120513aa72f0SAndy Lutomirski {
120613aa72f0SAndy Lutomirski 	u32 filter_ret, action;
1207deb4de8bSKees Cook 	struct seccomp_filter *match = NULL;
120813aa72f0SAndy Lutomirski 	int data;
1209db511391STycho Andersen 	struct seccomp_data sd_local;
12101da177e4SLinus Torvalds 
12113ba2530cSKees Cook 	/*
12123ba2530cSKees Cook 	 * Make sure that any changes to mode from another thread have
121323d67a54SGabriel Krisman Bertazi 	 * been seen after SYSCALL_WORK_SECCOMP was seen.
12143ba2530cSKees Cook 	 */
1215a381b70aSwanghongzhe 	smp_rmb();
12163ba2530cSKees Cook 
1217db511391STycho Andersen 	if (!sd) {
1218db511391STycho Andersen 		populate_seccomp_data(&sd_local);
1219db511391STycho Andersen 		sd = &sd_local;
1220db511391STycho Andersen 	}
1221db511391STycho Andersen 
1222deb4de8bSKees Cook 	filter_ret = seccomp_run_filters(sd, &match);
122313aa72f0SAndy Lutomirski 	data = filter_ret & SECCOMP_RET_DATA;
12240466bdb9SKees Cook 	action = filter_ret & SECCOMP_RET_ACTION_FULL;
122513aa72f0SAndy Lutomirski 
122613aa72f0SAndy Lutomirski 	switch (action) {
1227acf3b2c7SWill Drewry 	case SECCOMP_RET_ERRNO:
1228580c57f1SKees Cook 		/* Set low-order bits as an errno, capped at MAX_ERRNO. */
1229580c57f1SKees Cook 		if (data > MAX_ERRNO)
1230580c57f1SKees Cook 			data = MAX_ERRNO;
12312d9ca267SDenis Efremov 		syscall_set_return_value(current, current_pt_regs(),
1232acf3b2c7SWill Drewry 					 -data, 0);
1233acf3b2c7SWill Drewry 		goto skip;
123413aa72f0SAndy Lutomirski 
1235bb6ea430SWill Drewry 	case SECCOMP_RET_TRAP:
1236bb6ea430SWill Drewry 		/* Show the handler the original registers. */
12372d9ca267SDenis Efremov 		syscall_rollback(current, current_pt_regs());
1238bb6ea430SWill Drewry 		/* Let the filter pass back 16 bits of data. */
1239307d522fSEric W. Biederman 		force_sig_seccomp(this_syscall, data, false);
1240bb6ea430SWill Drewry 		goto skip;
124113aa72f0SAndy Lutomirski 
1242fb0fadf9SWill Drewry 	case SECCOMP_RET_TRACE:
1243ce6526e8SKees Cook 		/* We've been put in this state by the ptracer already. */
1244ce6526e8SKees Cook 		if (recheck_after_trace)
1245ce6526e8SKees Cook 			return 0;
1246ce6526e8SKees Cook 
12478112c4f1SKees Cook 		/* ENOSYS these calls if there is no tracer attached. */
124887b526d3SAndy Lutomirski 		if (!ptrace_event_enabled(current, PTRACE_EVENT_SECCOMP)) {
12498112c4f1SKees Cook 			syscall_set_return_value(current,
12502d9ca267SDenis Efremov 						 current_pt_regs(),
125187b526d3SAndy Lutomirski 						 -ENOSYS, 0);
12528112c4f1SKees Cook 			goto skip;
125387b526d3SAndy Lutomirski 		}
125413aa72f0SAndy Lutomirski 
1255fb0fadf9SWill Drewry 		/* Allow the BPF to provide the event message */
1256fb0fadf9SWill Drewry 		ptrace_event(PTRACE_EVENT_SECCOMP, data);
1257fb0fadf9SWill Drewry 		/*
1258fb0fadf9SWill Drewry 		 * The delivery of a fatal signal during event
1259485a252aSKees Cook 		 * notification may silently skip tracer notification,
1260485a252aSKees Cook 		 * which could leave us with a potentially unmodified
1261485a252aSKees Cook 		 * syscall that the tracer would have liked to have
1262485a252aSKees Cook 		 * changed. Since the process is about to die, we just
1263485a252aSKees Cook 		 * force the syscall to be skipped and let the signal
1264485a252aSKees Cook 		 * kill the process and correctly handle any tracer exit
1265485a252aSKees Cook 		 * notifications.
1266fb0fadf9SWill Drewry 		 */
1267fb0fadf9SWill Drewry 		if (fatal_signal_pending(current))
1268485a252aSKees Cook 			goto skip;
12698112c4f1SKees Cook 		/* Check if the tracer forced the syscall to be skipped. */
12702d9ca267SDenis Efremov 		this_syscall = syscall_get_nr(current, current_pt_regs());
12718112c4f1SKees Cook 		if (this_syscall < 0)
12728112c4f1SKees Cook 			goto skip;
127387b526d3SAndy Lutomirski 
1274ce6526e8SKees Cook 		/*
1275ce6526e8SKees Cook 		 * Recheck the syscall, since it may have changed. This
1276ce6526e8SKees Cook 		 * intentionally uses a NULL struct seccomp_data to force
1277ce6526e8SKees Cook 		 * a reload of all registers. This does not goto skip since
1278ce6526e8SKees Cook 		 * a skip would have already been reported.
1279ce6526e8SKees Cook 		 */
1280ce6526e8SKees Cook 		if (__seccomp_filter(this_syscall, NULL, true))
1281ce6526e8SKees Cook 			return -1;
1282ce6526e8SKees Cook 
1283fb0fadf9SWill Drewry 		return 0;
12848112c4f1SKees Cook 
12856a21cc50STycho Andersen 	case SECCOMP_RET_USER_NOTIF:
1286fb3c5386SChristian Brauner 		if (seccomp_do_user_notification(this_syscall, match, sd))
12876a21cc50STycho Andersen 			goto skip;
12886a21cc50STycho Andersen 
1289fb3c5386SChristian Brauner 		return 0;
1290fb3c5386SChristian Brauner 
129159f5cf44STyler Hicks 	case SECCOMP_RET_LOG:
129259f5cf44STyler Hicks 		seccomp_log(this_syscall, 0, action, true);
129359f5cf44STyler Hicks 		return 0;
129459f5cf44STyler Hicks 
12958112c4f1SKees Cook 	case SECCOMP_RET_ALLOW:
1296deb4de8bSKees Cook 		/*
1297deb4de8bSKees Cook 		 * Note that the "match" filter will always be NULL for
1298deb4de8bSKees Cook 		 * this action since SECCOMP_RET_ALLOW is the starting
1299deb4de8bSKees Cook 		 * state in seccomp_run_filters().
1300deb4de8bSKees Cook 		 */
13018112c4f1SKees Cook 		return 0;
13028112c4f1SKees Cook 
1303fd76875cSKees Cook 	case SECCOMP_RET_KILL_THREAD:
13044d3b0b05SKees Cook 	case SECCOMP_RET_KILL_PROCESS:
1305131b6351SKees Cook 	default:
1306495ac306SKees Cook 		current->seccomp.mode = SECCOMP_MODE_DEAD;
1307e66a3997STyler Hicks 		seccomp_log(this_syscall, SIGSYS, action, true);
1308d7276e32SKees Cook 		/* Dump core only if this is the last remaining thread. */
13094d671d92SRich Felker 		if (action != SECCOMP_RET_KILL_THREAD ||
1310d21918e5SEric W. Biederman 		    (atomic_read(&current->signal->live) == 1)) {
1311b25e6716SMike Frysinger 			/* Show the original registers in the dump. */
13122d9ca267SDenis Efremov 			syscall_rollback(current, current_pt_regs());
1313307d522fSEric W. Biederman 			/* Trigger a coredump with SIGSYS */
1314307d522fSEric W. Biederman 			force_sig_seccomp(this_syscall, data, true);
1315307d522fSEric W. Biederman 		} else {
13168112c4f1SKees Cook 			do_exit(SIGSYS);
1317307d522fSEric W. Biederman 		}
1318307d522fSEric W. Biederman 		return -1; /* skip the syscall go directly to signal handling */
13198112c4f1SKees Cook 	}
13208112c4f1SKees Cook 
13218112c4f1SKees Cook 	unreachable();
13228112c4f1SKees Cook 
13238112c4f1SKees Cook skip:
1324e66a3997STyler Hicks 	seccomp_log(this_syscall, 0, action, match ? match->log : false);
13258112c4f1SKees Cook 	return -1;
13268112c4f1SKees Cook }
13278112c4f1SKees Cook #else
__seccomp_filter(int this_syscall,const struct seccomp_data * sd,const bool recheck_after_trace)1328ce6526e8SKees Cook static int __seccomp_filter(int this_syscall, const struct seccomp_data *sd,
1329ce6526e8SKees Cook 			    const bool recheck_after_trace)
13308112c4f1SKees Cook {
13318112c4f1SKees Cook 	BUG();
133204b38d01SPaul Cercueil 
133304b38d01SPaul Cercueil 	return -1;
13348112c4f1SKees Cook }
13358112c4f1SKees Cook #endif
13368112c4f1SKees Cook 
__secure_computing(const struct seccomp_data * sd)13378112c4f1SKees Cook int __secure_computing(const struct seccomp_data *sd)
13388112c4f1SKees Cook {
13398112c4f1SKees Cook 	int mode = current->seccomp.mode;
13408112c4f1SKees Cook 	int this_syscall;
13418112c4f1SKees Cook 
134297f2645fSMasahiro Yamada 	if (IS_ENABLED(CONFIG_CHECKPOINT_RESTORE) &&
13438112c4f1SKees Cook 	    unlikely(current->ptrace & PT_SUSPEND_SECCOMP))
13448112c4f1SKees Cook 		return 0;
13458112c4f1SKees Cook 
13468112c4f1SKees Cook 	this_syscall = sd ? sd->nr :
13472d9ca267SDenis Efremov 		syscall_get_nr(current, current_pt_regs());
13488112c4f1SKees Cook 
13498112c4f1SKees Cook 	switch (mode) {
13508112c4f1SKees Cook 	case SECCOMP_MODE_STRICT:
13518112c4f1SKees Cook 		__secure_computing_strict(this_syscall);  /* may call do_exit */
13528112c4f1SKees Cook 		return 0;
13538112c4f1SKees Cook 	case SECCOMP_MODE_FILTER:
1354ce6526e8SKees Cook 		return __seccomp_filter(this_syscall, sd, false);
1355495ac306SKees Cook 	/* Surviving SECCOMP_RET_KILL_* must be proactively impossible. */
1356495ac306SKees Cook 	case SECCOMP_MODE_DEAD:
1357495ac306SKees Cook 		WARN_ON_ONCE(1);
1358495ac306SKees Cook 		do_exit(SIGKILL);
1359495ac306SKees Cook 		return -1;
13608112c4f1SKees Cook 	default:
13618112c4f1SKees Cook 		BUG();
13628112c4f1SKees Cook 	}
1363acf3b2c7SWill Drewry }
1364a4412fc9SAndy Lutomirski #endif /* CONFIG_HAVE_ARCH_SECCOMP_FILTER */
13651d9d02feSAndrea Arcangeli 
prctl_get_seccomp(void)13661d9d02feSAndrea Arcangeli long prctl_get_seccomp(void)
13671d9d02feSAndrea Arcangeli {
13681d9d02feSAndrea Arcangeli 	return current->seccomp.mode;
13691d9d02feSAndrea Arcangeli }
13701d9d02feSAndrea Arcangeli 
1371e2cfabdfSWill Drewry /**
13723b23dd12SKees Cook  * seccomp_set_mode_strict: internal function for setting strict seccomp
1373e2cfabdfSWill Drewry  *
1374e2cfabdfSWill Drewry  * Once current->seccomp.mode is non-zero, it may not be changed.
1375e2cfabdfSWill Drewry  *
1376e2cfabdfSWill Drewry  * Returns 0 on success or -EINVAL on failure.
1377e2cfabdfSWill Drewry  */
seccomp_set_mode_strict(void)13783b23dd12SKees Cook static long seccomp_set_mode_strict(void)
13791d9d02feSAndrea Arcangeli {
13803b23dd12SKees Cook 	const unsigned long seccomp_mode = SECCOMP_MODE_STRICT;
1381e2cfabdfSWill Drewry 	long ret = -EINVAL;
13821d9d02feSAndrea Arcangeli 
1383dbd95212SKees Cook 	spin_lock_irq(&current->sighand->siglock);
1384dbd95212SKees Cook 
13851f41b450SKees Cook 	if (!seccomp_may_assign_mode(seccomp_mode))
13861d9d02feSAndrea Arcangeli 		goto out;
13871d9d02feSAndrea Arcangeli 
1388cf99abacSAndrea Arcangeli #ifdef TIF_NOTSC
1389cf99abacSAndrea Arcangeli 	disable_TSC();
1390cf99abacSAndrea Arcangeli #endif
139100a02d0cSKees Cook 	seccomp_assign_mode(current, seccomp_mode, 0);
13923b23dd12SKees Cook 	ret = 0;
13933b23dd12SKees Cook 
13943b23dd12SKees Cook out:
1395dbd95212SKees Cook 	spin_unlock_irq(&current->sighand->siglock);
13963b23dd12SKees Cook 
13973b23dd12SKees Cook 	return ret;
13983b23dd12SKees Cook }
13993b23dd12SKees Cook 
1400e2cfabdfSWill Drewry #ifdef CONFIG_SECCOMP_FILTER
seccomp_notify_free(struct seccomp_filter * filter)1401e8393179STycho Andersen static void seccomp_notify_free(struct seccomp_filter *filter)
1402e8393179STycho Andersen {
1403e8393179STycho Andersen 	kfree(filter->notif);
1404e8393179STycho Andersen 	filter->notif = NULL;
1405e8393179STycho Andersen }
1406e8393179STycho Andersen 
seccomp_notify_detach(struct seccomp_filter * filter)1407a566a901STycho Andersen static void seccomp_notify_detach(struct seccomp_filter *filter)
14086a21cc50STycho Andersen {
14096a21cc50STycho Andersen 	struct seccomp_knotif *knotif;
14106a21cc50STycho Andersen 
1411a811dc61STycho Andersen 	if (!filter)
1412a566a901STycho Andersen 		return;
1413a811dc61STycho Andersen 
14146a21cc50STycho Andersen 	mutex_lock(&filter->notify_lock);
14156a21cc50STycho Andersen 
14166a21cc50STycho Andersen 	/*
14176a21cc50STycho Andersen 	 * If this file is being closed because e.g. the task who owned it
14186a21cc50STycho Andersen 	 * died, let's wake everyone up who was waiting on us.
14196a21cc50STycho Andersen 	 */
14206a21cc50STycho Andersen 	list_for_each_entry(knotif, &filter->notif->notifications, list) {
14216a21cc50STycho Andersen 		if (knotif->state == SECCOMP_NOTIFY_REPLIED)
14226a21cc50STycho Andersen 			continue;
14236a21cc50STycho Andersen 
14246a21cc50STycho Andersen 		knotif->state = SECCOMP_NOTIFY_REPLIED;
14256a21cc50STycho Andersen 		knotif->error = -ENOSYS;
14266a21cc50STycho Andersen 		knotif->val = 0;
14276a21cc50STycho Andersen 
14287cf97b12SSargun Dhillon 		/*
14297cf97b12SSargun Dhillon 		 * We do not need to wake up any pending addfd messages, as
14307cf97b12SSargun Dhillon 		 * the notifier will do that for us, as this just looks
14317cf97b12SSargun Dhillon 		 * like a standard reply.
14327cf97b12SSargun Dhillon 		 */
14336a21cc50STycho Andersen 		complete(&knotif->ready);
14346a21cc50STycho Andersen 	}
14356a21cc50STycho Andersen 
1436e8393179STycho Andersen 	seccomp_notify_free(filter);
14376a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
1438a566a901STycho Andersen }
1439a566a901STycho Andersen 
seccomp_notify_release(struct inode * inode,struct file * file)1440a566a901STycho Andersen static int seccomp_notify_release(struct inode *inode, struct file *file)
1441a566a901STycho Andersen {
1442a566a901STycho Andersen 	struct seccomp_filter *filter = file->private_data;
1443a566a901STycho Andersen 
1444a566a901STycho Andersen 	seccomp_notify_detach(filter);
14456a21cc50STycho Andersen 	__put_seccomp_filter(filter);
14466a21cc50STycho Andersen 	return 0;
14476a21cc50STycho Andersen }
14486a21cc50STycho Andersen 
14499f87dcf1SSargun Dhillon /* must be called with notif_lock held */
14509f87dcf1SSargun Dhillon static inline struct seccomp_knotif *
find_notification(struct seccomp_filter * filter,u64 id)14519f87dcf1SSargun Dhillon find_notification(struct seccomp_filter *filter, u64 id)
14529f87dcf1SSargun Dhillon {
14539f87dcf1SSargun Dhillon 	struct seccomp_knotif *cur;
14549f87dcf1SSargun Dhillon 
14559f87dcf1SSargun Dhillon 	lockdep_assert_held(&filter->notify_lock);
14569f87dcf1SSargun Dhillon 
14579f87dcf1SSargun Dhillon 	list_for_each_entry(cur, &filter->notif->notifications, list) {
14589f87dcf1SSargun Dhillon 		if (cur->id == id)
14599f87dcf1SSargun Dhillon 			return cur;
14609f87dcf1SSargun Dhillon 	}
14619f87dcf1SSargun Dhillon 
14629f87dcf1SSargun Dhillon 	return NULL;
14639f87dcf1SSargun Dhillon }
14649f87dcf1SSargun Dhillon 
recv_wake_function(wait_queue_entry_t * wait,unsigned int mode,int sync,void * key)14654943b66dSAndrei Vagin static int recv_wake_function(wait_queue_entry_t *wait, unsigned int mode, int sync,
14664943b66dSAndrei Vagin 				  void *key)
14674943b66dSAndrei Vagin {
14684943b66dSAndrei Vagin 	/* Avoid a wakeup if event not interesting for us. */
14694943b66dSAndrei Vagin 	if (key && !(key_to_poll(key) & (EPOLLIN | EPOLLERR)))
14704943b66dSAndrei Vagin 		return 0;
14714943b66dSAndrei Vagin 	return autoremove_wake_function(wait, mode, sync, key);
14724943b66dSAndrei Vagin }
14734943b66dSAndrei Vagin 
recv_wait_event(struct seccomp_filter * filter)14744943b66dSAndrei Vagin static int recv_wait_event(struct seccomp_filter *filter)
14754943b66dSAndrei Vagin {
14764943b66dSAndrei Vagin 	DEFINE_WAIT_FUNC(wait, recv_wake_function);
14774943b66dSAndrei Vagin 	int ret;
14784943b66dSAndrei Vagin 
14794943b66dSAndrei Vagin 	if (atomic_dec_if_positive(&filter->notif->requests) >= 0)
14804943b66dSAndrei Vagin 		return 0;
14814943b66dSAndrei Vagin 
14824943b66dSAndrei Vagin 	for (;;) {
14834943b66dSAndrei Vagin 		ret = prepare_to_wait_event(&filter->wqh, &wait, TASK_INTERRUPTIBLE);
14844943b66dSAndrei Vagin 
14854943b66dSAndrei Vagin 		if (atomic_dec_if_positive(&filter->notif->requests) >= 0)
14864943b66dSAndrei Vagin 			break;
14874943b66dSAndrei Vagin 
14884943b66dSAndrei Vagin 		if (ret)
14894943b66dSAndrei Vagin 			return ret;
14904943b66dSAndrei Vagin 
14914943b66dSAndrei Vagin 		schedule();
14924943b66dSAndrei Vagin 	}
14934943b66dSAndrei Vagin 	finish_wait(&filter->wqh, &wait);
14944943b66dSAndrei Vagin 	return 0;
14954943b66dSAndrei Vagin }
14969f87dcf1SSargun Dhillon 
seccomp_notify_recv(struct seccomp_filter * filter,void __user * buf)14976a21cc50STycho Andersen static long seccomp_notify_recv(struct seccomp_filter *filter,
14986a21cc50STycho Andersen 				void __user *buf)
14996a21cc50STycho Andersen {
15006a21cc50STycho Andersen 	struct seccomp_knotif *knotif = NULL, *cur;
15016a21cc50STycho Andersen 	struct seccomp_notif unotif;
15026a21cc50STycho Andersen 	ssize_t ret;
15036a21cc50STycho Andersen 
15042882d53cSSargun Dhillon 	/* Verify that we're not given garbage to keep struct extensible. */
15052882d53cSSargun Dhillon 	ret = check_zeroed_user(buf, sizeof(unotif));
15062882d53cSSargun Dhillon 	if (ret < 0)
15072882d53cSSargun Dhillon 		return ret;
15082882d53cSSargun Dhillon 	if (!ret)
15092882d53cSSargun Dhillon 		return -EINVAL;
15102882d53cSSargun Dhillon 
15116a21cc50STycho Andersen 	memset(&unotif, 0, sizeof(unotif));
15126a21cc50STycho Andersen 
15134943b66dSAndrei Vagin 	ret = recv_wait_event(filter);
15146a21cc50STycho Andersen 	if (ret < 0)
15156a21cc50STycho Andersen 		return ret;
15166a21cc50STycho Andersen 
15176a21cc50STycho Andersen 	mutex_lock(&filter->notify_lock);
15186a21cc50STycho Andersen 	list_for_each_entry(cur, &filter->notif->notifications, list) {
15196a21cc50STycho Andersen 		if (cur->state == SECCOMP_NOTIFY_INIT) {
15206a21cc50STycho Andersen 			knotif = cur;
15216a21cc50STycho Andersen 			break;
15226a21cc50STycho Andersen 		}
15236a21cc50STycho Andersen 	}
15246a21cc50STycho Andersen 
15256a21cc50STycho Andersen 	/*
15266a21cc50STycho Andersen 	 * If we didn't find a notification, it could be that the task was
15276a21cc50STycho Andersen 	 * interrupted by a fatal signal between the time we were woken and
15286a21cc50STycho Andersen 	 * when we were able to acquire the rw lock.
15296a21cc50STycho Andersen 	 */
15306a21cc50STycho Andersen 	if (!knotif) {
15316a21cc50STycho Andersen 		ret = -ENOENT;
15326a21cc50STycho Andersen 		goto out;
15336a21cc50STycho Andersen 	}
15346a21cc50STycho Andersen 
15356a21cc50STycho Andersen 	unotif.id = knotif->id;
15366a21cc50STycho Andersen 	unotif.pid = task_pid_vnr(knotif->task);
15376a21cc50STycho Andersen 	unotif.data = *(knotif->data);
15386a21cc50STycho Andersen 
15396a21cc50STycho Andersen 	knotif->state = SECCOMP_NOTIFY_SENT;
154076194c4eSChristian Brauner 	wake_up_poll(&filter->wqh, EPOLLOUT | EPOLLWRNORM);
15416a21cc50STycho Andersen 	ret = 0;
15426a21cc50STycho Andersen out:
15436a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
15446a21cc50STycho Andersen 
15456a21cc50STycho Andersen 	if (ret == 0 && copy_to_user(buf, &unotif, sizeof(unotif))) {
15466a21cc50STycho Andersen 		ret = -EFAULT;
15476a21cc50STycho Andersen 
15486a21cc50STycho Andersen 		/*
15496a21cc50STycho Andersen 		 * Userspace screwed up. To make sure that we keep this
15506a21cc50STycho Andersen 		 * notification alive, let's reset it back to INIT. It
15516a21cc50STycho Andersen 		 * may have died when we released the lock, so we need to make
15526a21cc50STycho Andersen 		 * sure it's still around.
15536a21cc50STycho Andersen 		 */
15546a21cc50STycho Andersen 		mutex_lock(&filter->notify_lock);
15559f87dcf1SSargun Dhillon 		knotif = find_notification(filter, unotif.id);
15566a21cc50STycho Andersen 		if (knotif) {
1557c2aa2dfeSSargun Dhillon 			/* Reset the process to make sure it's not stuck */
1558c2aa2dfeSSargun Dhillon 			if (should_sleep_killable(filter, knotif))
1559c2aa2dfeSSargun Dhillon 				complete(&knotif->ready);
15606a21cc50STycho Andersen 			knotif->state = SECCOMP_NOTIFY_INIT;
15614943b66dSAndrei Vagin 			atomic_inc(&filter->notif->requests);
15624943b66dSAndrei Vagin 			wake_up_poll(&filter->wqh, EPOLLIN | EPOLLRDNORM);
15636a21cc50STycho Andersen 		}
15646a21cc50STycho Andersen 		mutex_unlock(&filter->notify_lock);
15656a21cc50STycho Andersen 	}
15666a21cc50STycho Andersen 
15676a21cc50STycho Andersen 	return ret;
15686a21cc50STycho Andersen }
15696a21cc50STycho Andersen 
seccomp_notify_send(struct seccomp_filter * filter,void __user * buf)15706a21cc50STycho Andersen static long seccomp_notify_send(struct seccomp_filter *filter,
15716a21cc50STycho Andersen 				void __user *buf)
15726a21cc50STycho Andersen {
15736a21cc50STycho Andersen 	struct seccomp_notif_resp resp = {};
15749f87dcf1SSargun Dhillon 	struct seccomp_knotif *knotif;
15756a21cc50STycho Andersen 	long ret;
15766a21cc50STycho Andersen 
15776a21cc50STycho Andersen 	if (copy_from_user(&resp, buf, sizeof(resp)))
15786a21cc50STycho Andersen 		return -EFAULT;
15796a21cc50STycho Andersen 
1580fb3c5386SChristian Brauner 	if (resp.flags & ~SECCOMP_USER_NOTIF_FLAG_CONTINUE)
1581fb3c5386SChristian Brauner 		return -EINVAL;
1582fb3c5386SChristian Brauner 
1583fb3c5386SChristian Brauner 	if ((resp.flags & SECCOMP_USER_NOTIF_FLAG_CONTINUE) &&
1584fb3c5386SChristian Brauner 	    (resp.error || resp.val))
15856a21cc50STycho Andersen 		return -EINVAL;
15866a21cc50STycho Andersen 
15876a21cc50STycho Andersen 	ret = mutex_lock_interruptible(&filter->notify_lock);
15886a21cc50STycho Andersen 	if (ret < 0)
15896a21cc50STycho Andersen 		return ret;
15906a21cc50STycho Andersen 
15919f87dcf1SSargun Dhillon 	knotif = find_notification(filter, resp.id);
15926a21cc50STycho Andersen 	if (!knotif) {
15936a21cc50STycho Andersen 		ret = -ENOENT;
15946a21cc50STycho Andersen 		goto out;
15956a21cc50STycho Andersen 	}
15966a21cc50STycho Andersen 
15976a21cc50STycho Andersen 	/* Allow exactly one reply. */
15986a21cc50STycho Andersen 	if (knotif->state != SECCOMP_NOTIFY_SENT) {
15996a21cc50STycho Andersen 		ret = -EINPROGRESS;
16006a21cc50STycho Andersen 		goto out;
16016a21cc50STycho Andersen 	}
16026a21cc50STycho Andersen 
16036a21cc50STycho Andersen 	ret = 0;
16046a21cc50STycho Andersen 	knotif->state = SECCOMP_NOTIFY_REPLIED;
16056a21cc50STycho Andersen 	knotif->error = resp.error;
16066a21cc50STycho Andersen 	knotif->val = resp.val;
1607fb3c5386SChristian Brauner 	knotif->flags = resp.flags;
160848a1084aSAndrei Vagin 	if (filter->notif->flags & SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
160948a1084aSAndrei Vagin 		complete_on_current_cpu(&knotif->ready);
161048a1084aSAndrei Vagin 	else
16116a21cc50STycho Andersen 		complete(&knotif->ready);
16126a21cc50STycho Andersen out:
16136a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
16146a21cc50STycho Andersen 	return ret;
16156a21cc50STycho Andersen }
16166a21cc50STycho Andersen 
seccomp_notify_id_valid(struct seccomp_filter * filter,void __user * buf)16176a21cc50STycho Andersen static long seccomp_notify_id_valid(struct seccomp_filter *filter,
16186a21cc50STycho Andersen 				    void __user *buf)
16196a21cc50STycho Andersen {
16209f87dcf1SSargun Dhillon 	struct seccomp_knotif *knotif;
16216a21cc50STycho Andersen 	u64 id;
16226a21cc50STycho Andersen 	long ret;
16236a21cc50STycho Andersen 
16246a21cc50STycho Andersen 	if (copy_from_user(&id, buf, sizeof(id)))
16256a21cc50STycho Andersen 		return -EFAULT;
16266a21cc50STycho Andersen 
16276a21cc50STycho Andersen 	ret = mutex_lock_interruptible(&filter->notify_lock);
16286a21cc50STycho Andersen 	if (ret < 0)
16296a21cc50STycho Andersen 		return ret;
16306a21cc50STycho Andersen 
16319f87dcf1SSargun Dhillon 	knotif = find_notification(filter, id);
16329f87dcf1SSargun Dhillon 	if (knotif && knotif->state == SECCOMP_NOTIFY_SENT)
16336a21cc50STycho Andersen 		ret = 0;
16349f87dcf1SSargun Dhillon 	else
16359f87dcf1SSargun Dhillon 		ret = -ENOENT;
16366a21cc50STycho Andersen 
16376a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
16386a21cc50STycho Andersen 	return ret;
16396a21cc50STycho Andersen }
16406a21cc50STycho Andersen 
seccomp_notify_set_flags(struct seccomp_filter * filter,unsigned long flags)164148a1084aSAndrei Vagin static long seccomp_notify_set_flags(struct seccomp_filter *filter,
164248a1084aSAndrei Vagin 				    unsigned long flags)
164348a1084aSAndrei Vagin {
164448a1084aSAndrei Vagin 	long ret;
164548a1084aSAndrei Vagin 
164648a1084aSAndrei Vagin 	if (flags & ~SECCOMP_USER_NOTIF_FD_SYNC_WAKE_UP)
164748a1084aSAndrei Vagin 		return -EINVAL;
164848a1084aSAndrei Vagin 
164948a1084aSAndrei Vagin 	ret = mutex_lock_interruptible(&filter->notify_lock);
165048a1084aSAndrei Vagin 	if (ret < 0)
165148a1084aSAndrei Vagin 		return ret;
165248a1084aSAndrei Vagin 	filter->notif->flags = flags;
165348a1084aSAndrei Vagin 	mutex_unlock(&filter->notify_lock);
165448a1084aSAndrei Vagin 	return 0;
165548a1084aSAndrei Vagin }
165648a1084aSAndrei Vagin 
seccomp_notify_addfd(struct seccomp_filter * filter,struct seccomp_notif_addfd __user * uaddfd,unsigned int size)16577cf97b12SSargun Dhillon static long seccomp_notify_addfd(struct seccomp_filter *filter,
16587cf97b12SSargun Dhillon 				 struct seccomp_notif_addfd __user *uaddfd,
16597cf97b12SSargun Dhillon 				 unsigned int size)
16607cf97b12SSargun Dhillon {
16617cf97b12SSargun Dhillon 	struct seccomp_notif_addfd addfd;
16627cf97b12SSargun Dhillon 	struct seccomp_knotif *knotif;
16637cf97b12SSargun Dhillon 	struct seccomp_kaddfd kaddfd;
16647cf97b12SSargun Dhillon 	int ret;
16657cf97b12SSargun Dhillon 
16667cf97b12SSargun Dhillon 	BUILD_BUG_ON(sizeof(addfd) < SECCOMP_NOTIFY_ADDFD_SIZE_VER0);
16677cf97b12SSargun Dhillon 	BUILD_BUG_ON(sizeof(addfd) != SECCOMP_NOTIFY_ADDFD_SIZE_LATEST);
16687cf97b12SSargun Dhillon 
16697cf97b12SSargun Dhillon 	if (size < SECCOMP_NOTIFY_ADDFD_SIZE_VER0 || size >= PAGE_SIZE)
16707cf97b12SSargun Dhillon 		return -EINVAL;
16717cf97b12SSargun Dhillon 
16727cf97b12SSargun Dhillon 	ret = copy_struct_from_user(&addfd, sizeof(addfd), uaddfd, size);
16737cf97b12SSargun Dhillon 	if (ret)
16747cf97b12SSargun Dhillon 		return ret;
16757cf97b12SSargun Dhillon 
16767cf97b12SSargun Dhillon 	if (addfd.newfd_flags & ~O_CLOEXEC)
16777cf97b12SSargun Dhillon 		return -EINVAL;
16787cf97b12SSargun Dhillon 
16790ae71c77SRodrigo Campos 	if (addfd.flags & ~(SECCOMP_ADDFD_FLAG_SETFD | SECCOMP_ADDFD_FLAG_SEND))
16807cf97b12SSargun Dhillon 		return -EINVAL;
16817cf97b12SSargun Dhillon 
16827cf97b12SSargun Dhillon 	if (addfd.newfd && !(addfd.flags & SECCOMP_ADDFD_FLAG_SETFD))
16837cf97b12SSargun Dhillon 		return -EINVAL;
16847cf97b12SSargun Dhillon 
16857cf97b12SSargun Dhillon 	kaddfd.file = fget(addfd.srcfd);
16867cf97b12SSargun Dhillon 	if (!kaddfd.file)
16877cf97b12SSargun Dhillon 		return -EBADF;
16887cf97b12SSargun Dhillon 
16890ae71c77SRodrigo Campos 	kaddfd.ioctl_flags = addfd.flags;
16907cf97b12SSargun Dhillon 	kaddfd.flags = addfd.newfd_flags;
169142eb0d54SChristoph Hellwig 	kaddfd.setfd = addfd.flags & SECCOMP_ADDFD_FLAG_SETFD;
169242eb0d54SChristoph Hellwig 	kaddfd.fd = addfd.newfd;
16937cf97b12SSargun Dhillon 	init_completion(&kaddfd.completion);
16947cf97b12SSargun Dhillon 
16957cf97b12SSargun Dhillon 	ret = mutex_lock_interruptible(&filter->notify_lock);
16967cf97b12SSargun Dhillon 	if (ret < 0)
16977cf97b12SSargun Dhillon 		goto out;
16987cf97b12SSargun Dhillon 
16997cf97b12SSargun Dhillon 	knotif = find_notification(filter, addfd.id);
17007cf97b12SSargun Dhillon 	if (!knotif) {
17017cf97b12SSargun Dhillon 		ret = -ENOENT;
17027cf97b12SSargun Dhillon 		goto out_unlock;
17037cf97b12SSargun Dhillon 	}
17047cf97b12SSargun Dhillon 
17057cf97b12SSargun Dhillon 	/*
17067cf97b12SSargun Dhillon 	 * We do not want to allow for FD injection to occur before the
17077cf97b12SSargun Dhillon 	 * notification has been picked up by a userspace handler, or after
17087cf97b12SSargun Dhillon 	 * the notification has been replied to.
17097cf97b12SSargun Dhillon 	 */
17107cf97b12SSargun Dhillon 	if (knotif->state != SECCOMP_NOTIFY_SENT) {
17117cf97b12SSargun Dhillon 		ret = -EINPROGRESS;
17127cf97b12SSargun Dhillon 		goto out_unlock;
17137cf97b12SSargun Dhillon 	}
17147cf97b12SSargun Dhillon 
17150ae71c77SRodrigo Campos 	if (addfd.flags & SECCOMP_ADDFD_FLAG_SEND) {
17160ae71c77SRodrigo Campos 		/*
17170ae71c77SRodrigo Campos 		 * Disallow queuing an atomic addfd + send reply while there are
17180ae71c77SRodrigo Campos 		 * some addfd requests still to process.
17190ae71c77SRodrigo Campos 		 *
17200ae71c77SRodrigo Campos 		 * There is no clear reason to support it and allows us to keep
17210ae71c77SRodrigo Campos 		 * the loop on the other side straight-forward.
17220ae71c77SRodrigo Campos 		 */
17230ae71c77SRodrigo Campos 		if (!list_empty(&knotif->addfd)) {
17240ae71c77SRodrigo Campos 			ret = -EBUSY;
17250ae71c77SRodrigo Campos 			goto out_unlock;
17260ae71c77SRodrigo Campos 		}
17270ae71c77SRodrigo Campos 
17280ae71c77SRodrigo Campos 		/* Allow exactly only one reply */
17290ae71c77SRodrigo Campos 		knotif->state = SECCOMP_NOTIFY_REPLIED;
17300ae71c77SRodrigo Campos 	}
17310ae71c77SRodrigo Campos 
17327cf97b12SSargun Dhillon 	list_add(&kaddfd.list, &knotif->addfd);
17337cf97b12SSargun Dhillon 	complete(&knotif->ready);
17347cf97b12SSargun Dhillon 	mutex_unlock(&filter->notify_lock);
17357cf97b12SSargun Dhillon 
17367cf97b12SSargun Dhillon 	/* Now we wait for it to be processed or be interrupted */
17377cf97b12SSargun Dhillon 	ret = wait_for_completion_interruptible(&kaddfd.completion);
17387cf97b12SSargun Dhillon 	if (ret == 0) {
17397cf97b12SSargun Dhillon 		/*
17407cf97b12SSargun Dhillon 		 * We had a successful completion. The other side has already
17417cf97b12SSargun Dhillon 		 * removed us from the addfd queue, and
17427cf97b12SSargun Dhillon 		 * wait_for_completion_interruptible has a memory barrier upon
17437cf97b12SSargun Dhillon 		 * success that lets us read this value directly without
17447cf97b12SSargun Dhillon 		 * locking.
17457cf97b12SSargun Dhillon 		 */
17467cf97b12SSargun Dhillon 		ret = kaddfd.ret;
17477cf97b12SSargun Dhillon 		goto out;
17487cf97b12SSargun Dhillon 	}
17497cf97b12SSargun Dhillon 
17507cf97b12SSargun Dhillon 	mutex_lock(&filter->notify_lock);
17517cf97b12SSargun Dhillon 	/*
17527cf97b12SSargun Dhillon 	 * Even though we were woken up by a signal and not a successful
17537cf97b12SSargun Dhillon 	 * completion, a completion may have happened in the mean time.
17547cf97b12SSargun Dhillon 	 *
17557cf97b12SSargun Dhillon 	 * We need to check again if the addfd request has been handled,
17567cf97b12SSargun Dhillon 	 * and if not, we will remove it from the queue.
17577cf97b12SSargun Dhillon 	 */
17587cf97b12SSargun Dhillon 	if (list_empty(&kaddfd.list))
17597cf97b12SSargun Dhillon 		ret = kaddfd.ret;
17607cf97b12SSargun Dhillon 	else
17617cf97b12SSargun Dhillon 		list_del(&kaddfd.list);
17627cf97b12SSargun Dhillon 
17637cf97b12SSargun Dhillon out_unlock:
17647cf97b12SSargun Dhillon 	mutex_unlock(&filter->notify_lock);
17657cf97b12SSargun Dhillon out:
17667cf97b12SSargun Dhillon 	fput(kaddfd.file);
17677cf97b12SSargun Dhillon 
17687cf97b12SSargun Dhillon 	return ret;
17697cf97b12SSargun Dhillon }
17707cf97b12SSargun Dhillon 
seccomp_notify_ioctl(struct file * file,unsigned int cmd,unsigned long arg)17716a21cc50STycho Andersen static long seccomp_notify_ioctl(struct file *file, unsigned int cmd,
17726a21cc50STycho Andersen 				 unsigned long arg)
17736a21cc50STycho Andersen {
17746a21cc50STycho Andersen 	struct seccomp_filter *filter = file->private_data;
17756a21cc50STycho Andersen 	void __user *buf = (void __user *)arg;
17766a21cc50STycho Andersen 
17777cf97b12SSargun Dhillon 	/* Fixed-size ioctls */
17786a21cc50STycho Andersen 	switch (cmd) {
17796a21cc50STycho Andersen 	case SECCOMP_IOCTL_NOTIF_RECV:
17806a21cc50STycho Andersen 		return seccomp_notify_recv(filter, buf);
17816a21cc50STycho Andersen 	case SECCOMP_IOCTL_NOTIF_SEND:
17826a21cc50STycho Andersen 		return seccomp_notify_send(filter, buf);
178347e33c05SKees Cook 	case SECCOMP_IOCTL_NOTIF_ID_VALID_WRONG_DIR:
17846a21cc50STycho Andersen 	case SECCOMP_IOCTL_NOTIF_ID_VALID:
17856a21cc50STycho Andersen 		return seccomp_notify_id_valid(filter, buf);
178648a1084aSAndrei Vagin 	case SECCOMP_IOCTL_NOTIF_SET_FLAGS:
178748a1084aSAndrei Vagin 		return seccomp_notify_set_flags(filter, arg);
17887cf97b12SSargun Dhillon 	}
17897cf97b12SSargun Dhillon 
17907cf97b12SSargun Dhillon 	/* Extensible Argument ioctls */
17917cf97b12SSargun Dhillon #define EA_IOCTL(cmd)	((cmd) & ~(IOC_INOUT | IOCSIZE_MASK))
17927cf97b12SSargun Dhillon 	switch (EA_IOCTL(cmd)) {
17937cf97b12SSargun Dhillon 	case EA_IOCTL(SECCOMP_IOCTL_NOTIF_ADDFD):
17947cf97b12SSargun Dhillon 		return seccomp_notify_addfd(filter, buf, _IOC_SIZE(cmd));
17956a21cc50STycho Andersen 	default:
17966a21cc50STycho Andersen 		return -EINVAL;
17976a21cc50STycho Andersen 	}
17986a21cc50STycho Andersen }
17996a21cc50STycho Andersen 
seccomp_notify_poll(struct file * file,struct poll_table_struct * poll_tab)18006a21cc50STycho Andersen static __poll_t seccomp_notify_poll(struct file *file,
18016a21cc50STycho Andersen 				    struct poll_table_struct *poll_tab)
18026a21cc50STycho Andersen {
18036a21cc50STycho Andersen 	struct seccomp_filter *filter = file->private_data;
18046a21cc50STycho Andersen 	__poll_t ret = 0;
18056a21cc50STycho Andersen 	struct seccomp_knotif *cur;
18066a21cc50STycho Andersen 
180776194c4eSChristian Brauner 	poll_wait(file, &filter->wqh, poll_tab);
18086a21cc50STycho Andersen 
1809319deec7STycho Andersen 	if (mutex_lock_interruptible(&filter->notify_lock) < 0)
18106a21cc50STycho Andersen 		return EPOLLERR;
18116a21cc50STycho Andersen 
18126a21cc50STycho Andersen 	list_for_each_entry(cur, &filter->notif->notifications, list) {
18136a21cc50STycho Andersen 		if (cur->state == SECCOMP_NOTIFY_INIT)
18146a21cc50STycho Andersen 			ret |= EPOLLIN | EPOLLRDNORM;
18156a21cc50STycho Andersen 		if (cur->state == SECCOMP_NOTIFY_SENT)
18166a21cc50STycho Andersen 			ret |= EPOLLOUT | EPOLLWRNORM;
18176a21cc50STycho Andersen 		if ((ret & EPOLLIN) && (ret & EPOLLOUT))
18186a21cc50STycho Andersen 			break;
18196a21cc50STycho Andersen 	}
18206a21cc50STycho Andersen 
18216a21cc50STycho Andersen 	mutex_unlock(&filter->notify_lock);
18226a21cc50STycho Andersen 
182399cdb8b9SChristian Brauner 	if (refcount_read(&filter->users) == 0)
182499cdb8b9SChristian Brauner 		ret |= EPOLLHUP;
182599cdb8b9SChristian Brauner 
18266a21cc50STycho Andersen 	return ret;
18276a21cc50STycho Andersen }
18286a21cc50STycho Andersen 
18296a21cc50STycho Andersen static const struct file_operations seccomp_notify_ops = {
18306a21cc50STycho Andersen 	.poll = seccomp_notify_poll,
18316a21cc50STycho Andersen 	.release = seccomp_notify_release,
18326a21cc50STycho Andersen 	.unlocked_ioctl = seccomp_notify_ioctl,
18333db81afdSSven Schnelle 	.compat_ioctl = seccomp_notify_ioctl,
18346a21cc50STycho Andersen };
18356a21cc50STycho Andersen 
init_listener(struct seccomp_filter * filter)18366a21cc50STycho Andersen static struct file *init_listener(struct seccomp_filter *filter)
18376a21cc50STycho Andersen {
1838dfe719feSJann Horn 	struct file *ret;
18396a21cc50STycho Andersen 
18406a21cc50STycho Andersen 	ret = ERR_PTR(-ENOMEM);
18416a21cc50STycho Andersen 	filter->notif = kzalloc(sizeof(*(filter->notif)), GFP_KERNEL);
18426a21cc50STycho Andersen 	if (!filter->notif)
18436a21cc50STycho Andersen 		goto out;
18446a21cc50STycho Andersen 
18456a21cc50STycho Andersen 	filter->notif->next_id = get_random_u64();
18466a21cc50STycho Andersen 	INIT_LIST_HEAD(&filter->notif->notifications);
18476a21cc50STycho Andersen 
18486a21cc50STycho Andersen 	ret = anon_inode_getfile("seccomp notify", &seccomp_notify_ops,
18496a21cc50STycho Andersen 				 filter, O_RDWR);
18506a21cc50STycho Andersen 	if (IS_ERR(ret))
18516a21cc50STycho Andersen 		goto out_notif;
18526a21cc50STycho Andersen 
18536a21cc50STycho Andersen 	/* The file has a reference to it now */
18546a21cc50STycho Andersen 	__get_seccomp_filter(filter);
18556a21cc50STycho Andersen 
18566a21cc50STycho Andersen out_notif:
18576a21cc50STycho Andersen 	if (IS_ERR(ret))
1858e8393179STycho Andersen 		seccomp_notify_free(filter);
18596a21cc50STycho Andersen out:
18606a21cc50STycho Andersen 	return ret;
18616a21cc50STycho Andersen }
18626a21cc50STycho Andersen 
1863dfe719feSJann Horn /*
1864dfe719feSJann Horn  * Does @new_child have a listener while an ancestor also has a listener?
1865dfe719feSJann Horn  * If so, we'll want to reject this filter.
1866dfe719feSJann Horn  * This only has to be tested for the current process, even in the TSYNC case,
1867dfe719feSJann Horn  * because TSYNC installs @child with the same parent on all threads.
1868dfe719feSJann Horn  * Note that @new_child is not hooked up to its parent at this point yet, so
1869dfe719feSJann Horn  * we use current->seccomp.filter.
1870dfe719feSJann Horn  */
has_duplicate_listener(struct seccomp_filter * new_child)1871dfe719feSJann Horn static bool has_duplicate_listener(struct seccomp_filter *new_child)
1872dfe719feSJann Horn {
1873dfe719feSJann Horn 	struct seccomp_filter *cur;
1874dfe719feSJann Horn 
1875dfe719feSJann Horn 	/* must be protected against concurrent TSYNC */
1876dfe719feSJann Horn 	lockdep_assert_held(&current->sighand->siglock);
1877dfe719feSJann Horn 
1878dfe719feSJann Horn 	if (!new_child->notif)
1879dfe719feSJann Horn 		return false;
1880dfe719feSJann Horn 	for (cur = current->seccomp.filter; cur; cur = cur->prev) {
1881dfe719feSJann Horn 		if (cur->notif)
1882dfe719feSJann Horn 			return true;
1883dfe719feSJann Horn 	}
1884dfe719feSJann Horn 
1885dfe719feSJann Horn 	return false;
1886dfe719feSJann Horn }
1887dfe719feSJann Horn 
18883b23dd12SKees Cook /**
18893b23dd12SKees Cook  * seccomp_set_mode_filter: internal function for setting seccomp filter
189048dc92b9SKees Cook  * @flags:  flags to change filter behavior
18913b23dd12SKees Cook  * @filter: struct sock_fprog containing filter
18923b23dd12SKees Cook  *
18933b23dd12SKees Cook  * This function may be called repeatedly to install additional filters.
18943b23dd12SKees Cook  * Every filter successfully installed will be evaluated (in reverse order)
18953b23dd12SKees Cook  * for each system call the task makes.
18963b23dd12SKees Cook  *
18973b23dd12SKees Cook  * Once current->seccomp.mode is non-zero, it may not be changed.
18983b23dd12SKees Cook  *
18993b23dd12SKees Cook  * Returns 0 on success or -EINVAL on failure.
19003b23dd12SKees Cook  */
seccomp_set_mode_filter(unsigned int flags,const char __user * filter)190148dc92b9SKees Cook static long seccomp_set_mode_filter(unsigned int flags,
190248dc92b9SKees Cook 				    const char __user *filter)
19033b23dd12SKees Cook {
19043b23dd12SKees Cook 	const unsigned long seccomp_mode = SECCOMP_MODE_FILTER;
1905c8bee430SKees Cook 	struct seccomp_filter *prepared = NULL;
19063b23dd12SKees Cook 	long ret = -EINVAL;
19076a21cc50STycho Andersen 	int listener = -1;
19086a21cc50STycho Andersen 	struct file *listener_f = NULL;
19093b23dd12SKees Cook 
191048dc92b9SKees Cook 	/* Validate flags. */
1911c2e1f2e3SKees Cook 	if (flags & ~SECCOMP_FILTER_FLAG_MASK)
1912dbd95212SKees Cook 		return -EINVAL;
191348dc92b9SKees Cook 
19147a0df7fbSTycho Andersen 	/*
19157a0df7fbSTycho Andersen 	 * In the successful case, NEW_LISTENER returns the new listener fd.
19167a0df7fbSTycho Andersen 	 * But in the failure case, TSYNC returns the thread that died. If you
19177a0df7fbSTycho Andersen 	 * combine these two flags, there's no way to tell whether something
191851891498STycho Andersen 	 * succeeded or failed. So, let's disallow this combination if the user
191951891498STycho Andersen 	 * has not explicitly requested no errors from TSYNC.
19207a0df7fbSTycho Andersen 	 */
19217a0df7fbSTycho Andersen 	if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
192251891498STycho Andersen 	    (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) &&
192351891498STycho Andersen 	    ((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0))
19247a0df7fbSTycho Andersen 		return -EINVAL;
19257a0df7fbSTycho Andersen 
1926c2aa2dfeSSargun Dhillon 	/*
1927c2aa2dfeSSargun Dhillon 	 * The SECCOMP_FILTER_FLAG_WAIT_KILLABLE_SENT flag doesn't make sense
1928c2aa2dfeSSargun Dhillon 	 * without the SECCOMP_FILTER_FLAG_NEW_LISTENER flag.
1929c2aa2dfeSSargun Dhillon 	 */
1930c2aa2dfeSSargun Dhillon 	if ((flags & SECCOMP_FILTER_FLAG_WAIT_KILLABLE_RECV) &&
1931c2aa2dfeSSargun Dhillon 	    ((flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) == 0))
1932c2aa2dfeSSargun Dhillon 		return -EINVAL;
1933c2aa2dfeSSargun Dhillon 
1934c8bee430SKees Cook 	/* Prepare the new filter before holding any locks. */
1935c8bee430SKees Cook 	prepared = seccomp_prepare_user_filter(filter);
1936c8bee430SKees Cook 	if (IS_ERR(prepared))
1937c8bee430SKees Cook 		return PTR_ERR(prepared);
1938c8bee430SKees Cook 
19396a21cc50STycho Andersen 	if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
19406a21cc50STycho Andersen 		listener = get_unused_fd_flags(O_CLOEXEC);
19416a21cc50STycho Andersen 		if (listener < 0) {
19426a21cc50STycho Andersen 			ret = listener;
19436a21cc50STycho Andersen 			goto out_free;
19446a21cc50STycho Andersen 		}
19456a21cc50STycho Andersen 
19466a21cc50STycho Andersen 		listener_f = init_listener(prepared);
19476a21cc50STycho Andersen 		if (IS_ERR(listener_f)) {
19486a21cc50STycho Andersen 			put_unused_fd(listener);
19496a21cc50STycho Andersen 			ret = PTR_ERR(listener_f);
19506a21cc50STycho Andersen 			goto out_free;
19516a21cc50STycho Andersen 		}
19526a21cc50STycho Andersen 	}
19536a21cc50STycho Andersen 
1954c2e1f2e3SKees Cook 	/*
1955c2e1f2e3SKees Cook 	 * Make sure we cannot change seccomp or nnp state via TSYNC
1956c2e1f2e3SKees Cook 	 * while another thread is in the middle of calling exec.
1957c2e1f2e3SKees Cook 	 */
1958c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC &&
1959c2e1f2e3SKees Cook 	    mutex_lock_killable(&current->signal->cred_guard_mutex))
19606a21cc50STycho Andersen 		goto out_put_fd;
1961c2e1f2e3SKees Cook 
1962dbd95212SKees Cook 	spin_lock_irq(&current->sighand->siglock);
1963dbd95212SKees Cook 
19643b23dd12SKees Cook 	if (!seccomp_may_assign_mode(seccomp_mode))
19653b23dd12SKees Cook 		goto out;
19663b23dd12SKees Cook 
1967dfe719feSJann Horn 	if (has_duplicate_listener(prepared)) {
1968dfe719feSJann Horn 		ret = -EBUSY;
1969dfe719feSJann Horn 		goto out;
1970dfe719feSJann Horn 	}
1971dfe719feSJann Horn 
1972c8bee430SKees Cook 	ret = seccomp_attach_filter(flags, prepared);
1973e2cfabdfSWill Drewry 	if (ret)
1974e2cfabdfSWill Drewry 		goto out;
1975c8bee430SKees Cook 	/* Do not free the successfully attached filter. */
1976c8bee430SKees Cook 	prepared = NULL;
19771d9d02feSAndrea Arcangeli 
197800a02d0cSKees Cook 	seccomp_assign_mode(current, seccomp_mode, flags);
19791d9d02feSAndrea Arcangeli out:
1980dbd95212SKees Cook 	spin_unlock_irq(&current->sighand->siglock);
1981c2e1f2e3SKees Cook 	if (flags & SECCOMP_FILTER_FLAG_TSYNC)
1982c2e1f2e3SKees Cook 		mutex_unlock(&current->signal->cred_guard_mutex);
19836a21cc50STycho Andersen out_put_fd:
19846a21cc50STycho Andersen 	if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
19857a0df7fbSTycho Andersen 		if (ret) {
1986a811dc61STycho Andersen 			listener_f->private_data = NULL;
19876a21cc50STycho Andersen 			fput(listener_f);
19886a21cc50STycho Andersen 			put_unused_fd(listener);
1989a566a901STycho Andersen 			seccomp_notify_detach(prepared);
19906a21cc50STycho Andersen 		} else {
19916a21cc50STycho Andersen 			fd_install(listener, listener_f);
19926a21cc50STycho Andersen 			ret = listener;
19936a21cc50STycho Andersen 		}
19946a21cc50STycho Andersen 	}
1995c2e1f2e3SKees Cook out_free:
1996c8bee430SKees Cook 	seccomp_filter_free(prepared);
19971d9d02feSAndrea Arcangeli 	return ret;
19981d9d02feSAndrea Arcangeli }
19993b23dd12SKees Cook #else
seccomp_set_mode_filter(unsigned int flags,const char __user * filter)200048dc92b9SKees Cook static inline long seccomp_set_mode_filter(unsigned int flags,
200148dc92b9SKees Cook 					   const char __user *filter)
20023b23dd12SKees Cook {
20033b23dd12SKees Cook 	return -EINVAL;
20043b23dd12SKees Cook }
20053b23dd12SKees Cook #endif
2006d78ab02cSKees Cook 
seccomp_get_action_avail(const char __user * uaction)2007d612b1fdSTyler Hicks static long seccomp_get_action_avail(const char __user *uaction)
2008d612b1fdSTyler Hicks {
2009d612b1fdSTyler Hicks 	u32 action;
2010d612b1fdSTyler Hicks 
2011d612b1fdSTyler Hicks 	if (copy_from_user(&action, uaction, sizeof(action)))
2012d612b1fdSTyler Hicks 		return -EFAULT;
2013d612b1fdSTyler Hicks 
2014d612b1fdSTyler Hicks 	switch (action) {
20150466bdb9SKees Cook 	case SECCOMP_RET_KILL_PROCESS:
2016fd76875cSKees Cook 	case SECCOMP_RET_KILL_THREAD:
2017d612b1fdSTyler Hicks 	case SECCOMP_RET_TRAP:
2018d612b1fdSTyler Hicks 	case SECCOMP_RET_ERRNO:
20196a21cc50STycho Andersen 	case SECCOMP_RET_USER_NOTIF:
2020d612b1fdSTyler Hicks 	case SECCOMP_RET_TRACE:
202159f5cf44STyler Hicks 	case SECCOMP_RET_LOG:
2022d612b1fdSTyler Hicks 	case SECCOMP_RET_ALLOW:
2023d612b1fdSTyler Hicks 		break;
2024d612b1fdSTyler Hicks 	default:
2025d612b1fdSTyler Hicks 		return -EOPNOTSUPP;
2026d612b1fdSTyler Hicks 	}
2027d612b1fdSTyler Hicks 
2028d612b1fdSTyler Hicks 	return 0;
2029d612b1fdSTyler Hicks }
2030d612b1fdSTyler Hicks 
seccomp_get_notif_sizes(void __user * usizes)20316a21cc50STycho Andersen static long seccomp_get_notif_sizes(void __user *usizes)
20326a21cc50STycho Andersen {
20336a21cc50STycho Andersen 	struct seccomp_notif_sizes sizes = {
20346a21cc50STycho Andersen 		.seccomp_notif = sizeof(struct seccomp_notif),
20356a21cc50STycho Andersen 		.seccomp_notif_resp = sizeof(struct seccomp_notif_resp),
20366a21cc50STycho Andersen 		.seccomp_data = sizeof(struct seccomp_data),
20376a21cc50STycho Andersen 	};
20386a21cc50STycho Andersen 
20396a21cc50STycho Andersen 	if (copy_to_user(usizes, &sizes, sizeof(sizes)))
20406a21cc50STycho Andersen 		return -EFAULT;
20416a21cc50STycho Andersen 
20426a21cc50STycho Andersen 	return 0;
20436a21cc50STycho Andersen }
20446a21cc50STycho Andersen 
204548dc92b9SKees Cook /* Common entry point for both prctl and syscall. */
do_seccomp(unsigned int op,unsigned int flags,void __user * uargs)204648dc92b9SKees Cook static long do_seccomp(unsigned int op, unsigned int flags,
2047a5662e4dSTycho Andersen 		       void __user *uargs)
204848dc92b9SKees Cook {
204948dc92b9SKees Cook 	switch (op) {
205048dc92b9SKees Cook 	case SECCOMP_SET_MODE_STRICT:
205148dc92b9SKees Cook 		if (flags != 0 || uargs != NULL)
205248dc92b9SKees Cook 			return -EINVAL;
205348dc92b9SKees Cook 		return seccomp_set_mode_strict();
205448dc92b9SKees Cook 	case SECCOMP_SET_MODE_FILTER:
205548dc92b9SKees Cook 		return seccomp_set_mode_filter(flags, uargs);
2056d612b1fdSTyler Hicks 	case SECCOMP_GET_ACTION_AVAIL:
2057d612b1fdSTyler Hicks 		if (flags != 0)
2058d612b1fdSTyler Hicks 			return -EINVAL;
2059d612b1fdSTyler Hicks 
2060d612b1fdSTyler Hicks 		return seccomp_get_action_avail(uargs);
20616a21cc50STycho Andersen 	case SECCOMP_GET_NOTIF_SIZES:
20626a21cc50STycho Andersen 		if (flags != 0)
20636a21cc50STycho Andersen 			return -EINVAL;
20646a21cc50STycho Andersen 
20656a21cc50STycho Andersen 		return seccomp_get_notif_sizes(uargs);
206648dc92b9SKees Cook 	default:
206748dc92b9SKees Cook 		return -EINVAL;
206848dc92b9SKees Cook 	}
206948dc92b9SKees Cook }
207048dc92b9SKees Cook 
SYSCALL_DEFINE3(seccomp,unsigned int,op,unsigned int,flags,void __user *,uargs)207148dc92b9SKees Cook SYSCALL_DEFINE3(seccomp, unsigned int, op, unsigned int, flags,
2072a5662e4dSTycho Andersen 			 void __user *, uargs)
207348dc92b9SKees Cook {
207448dc92b9SKees Cook 	return do_seccomp(op, flags, uargs);
207548dc92b9SKees Cook }
207648dc92b9SKees Cook 
2077d78ab02cSKees Cook /**
2078d78ab02cSKees Cook  * prctl_set_seccomp: configures current->seccomp.mode
2079d78ab02cSKees Cook  * @seccomp_mode: requested mode to use
2080d78ab02cSKees Cook  * @filter: optional struct sock_fprog for use with SECCOMP_MODE_FILTER
2081d78ab02cSKees Cook  *
2082d78ab02cSKees Cook  * Returns 0 on success or -EINVAL on failure.
2083d78ab02cSKees Cook  */
prctl_set_seccomp(unsigned long seccomp_mode,void __user * filter)2084a5662e4dSTycho Andersen long prctl_set_seccomp(unsigned long seccomp_mode, void __user *filter)
2085d78ab02cSKees Cook {
208648dc92b9SKees Cook 	unsigned int op;
2087a5662e4dSTycho Andersen 	void __user *uargs;
208848dc92b9SKees Cook 
20893b23dd12SKees Cook 	switch (seccomp_mode) {
20903b23dd12SKees Cook 	case SECCOMP_MODE_STRICT:
209148dc92b9SKees Cook 		op = SECCOMP_SET_MODE_STRICT;
209248dc92b9SKees Cook 		/*
209348dc92b9SKees Cook 		 * Setting strict mode through prctl always ignored filter,
209448dc92b9SKees Cook 		 * so make sure it is always NULL here to pass the internal
209548dc92b9SKees Cook 		 * check in do_seccomp().
209648dc92b9SKees Cook 		 */
209748dc92b9SKees Cook 		uargs = NULL;
209848dc92b9SKees Cook 		break;
20993b23dd12SKees Cook 	case SECCOMP_MODE_FILTER:
210048dc92b9SKees Cook 		op = SECCOMP_SET_MODE_FILTER;
210148dc92b9SKees Cook 		uargs = filter;
210248dc92b9SKees Cook 		break;
21033b23dd12SKees Cook 	default:
21043b23dd12SKees Cook 		return -EINVAL;
21053b23dd12SKees Cook 	}
210648dc92b9SKees Cook 
210748dc92b9SKees Cook 	/* prctl interface doesn't have flags, so they are always zero. */
210848dc92b9SKees Cook 	return do_seccomp(op, 0, uargs);
2109d78ab02cSKees Cook }
2110f8e529edSTycho Andersen 
2111f8e529edSTycho Andersen #if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
get_nth_filter(struct task_struct * task,unsigned long filter_off)2112f06eae83STycho Andersen static struct seccomp_filter *get_nth_filter(struct task_struct *task,
2113f06eae83STycho Andersen 					     unsigned long filter_off)
2114f06eae83STycho Andersen {
2115f06eae83STycho Andersen 	struct seccomp_filter *orig, *filter;
2116f06eae83STycho Andersen 	unsigned long count;
2117f06eae83STycho Andersen 
2118f06eae83STycho Andersen 	/*
2119f06eae83STycho Andersen 	 * Note: this is only correct because the caller should be the (ptrace)
2120f06eae83STycho Andersen 	 * tracer of the task, otherwise lock_task_sighand is needed.
2121f06eae83STycho Andersen 	 */
2122f06eae83STycho Andersen 	spin_lock_irq(&task->sighand->siglock);
2123f06eae83STycho Andersen 
2124f06eae83STycho Andersen 	if (task->seccomp.mode != SECCOMP_MODE_FILTER) {
2125f06eae83STycho Andersen 		spin_unlock_irq(&task->sighand->siglock);
2126f06eae83STycho Andersen 		return ERR_PTR(-EINVAL);
2127f06eae83STycho Andersen 	}
2128f06eae83STycho Andersen 
2129f06eae83STycho Andersen 	orig = task->seccomp.filter;
2130f06eae83STycho Andersen 	__get_seccomp_filter(orig);
2131f06eae83STycho Andersen 	spin_unlock_irq(&task->sighand->siglock);
2132f06eae83STycho Andersen 
2133f06eae83STycho Andersen 	count = 0;
2134f06eae83STycho Andersen 	for (filter = orig; filter; filter = filter->prev)
2135f06eae83STycho Andersen 		count++;
2136f06eae83STycho Andersen 
2137f06eae83STycho Andersen 	if (filter_off >= count) {
2138f06eae83STycho Andersen 		filter = ERR_PTR(-ENOENT);
2139f06eae83STycho Andersen 		goto out;
2140f06eae83STycho Andersen 	}
2141f06eae83STycho Andersen 
2142f06eae83STycho Andersen 	count -= filter_off;
2143f06eae83STycho Andersen 	for (filter = orig; filter && count > 1; filter = filter->prev)
2144f06eae83STycho Andersen 		count--;
2145f06eae83STycho Andersen 
2146f06eae83STycho Andersen 	if (WARN_ON(count != 1 || !filter)) {
2147f06eae83STycho Andersen 		filter = ERR_PTR(-ENOENT);
2148f06eae83STycho Andersen 		goto out;
2149f06eae83STycho Andersen 	}
2150f06eae83STycho Andersen 
2151f06eae83STycho Andersen 	__get_seccomp_filter(filter);
2152f06eae83STycho Andersen 
2153f06eae83STycho Andersen out:
2154f06eae83STycho Andersen 	__put_seccomp_filter(orig);
2155f06eae83STycho Andersen 	return filter;
2156f06eae83STycho Andersen }
2157f06eae83STycho Andersen 
seccomp_get_filter(struct task_struct * task,unsigned long filter_off,void __user * data)2158f8e529edSTycho Andersen long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
2159f8e529edSTycho Andersen 			void __user *data)
2160f8e529edSTycho Andersen {
2161f8e529edSTycho Andersen 	struct seccomp_filter *filter;
2162f8e529edSTycho Andersen 	struct sock_fprog_kern *fprog;
2163f8e529edSTycho Andersen 	long ret;
2164f8e529edSTycho Andersen 
2165f8e529edSTycho Andersen 	if (!capable(CAP_SYS_ADMIN) ||
2166f8e529edSTycho Andersen 	    current->seccomp.mode != SECCOMP_MODE_DISABLED) {
2167f8e529edSTycho Andersen 		return -EACCES;
2168f8e529edSTycho Andersen 	}
2169f8e529edSTycho Andersen 
2170f06eae83STycho Andersen 	filter = get_nth_filter(task, filter_off);
2171f06eae83STycho Andersen 	if (IS_ERR(filter))
2172f06eae83STycho Andersen 		return PTR_ERR(filter);
2173f8e529edSTycho Andersen 
2174f8e529edSTycho Andersen 	fprog = filter->prog->orig_prog;
2175f8e529edSTycho Andersen 	if (!fprog) {
2176470bf1f2SMickaël Salaün 		/* This must be a new non-cBPF filter, since we save
2177f8e529edSTycho Andersen 		 * every cBPF filter's orig_prog above when
2178f8e529edSTycho Andersen 		 * CONFIG_CHECKPOINT_RESTORE is enabled.
2179f8e529edSTycho Andersen 		 */
2180f8e529edSTycho Andersen 		ret = -EMEDIUMTYPE;
2181f8e529edSTycho Andersen 		goto out;
2182f8e529edSTycho Andersen 	}
2183f8e529edSTycho Andersen 
2184f8e529edSTycho Andersen 	ret = fprog->len;
2185f8e529edSTycho Andersen 	if (!data)
2186f8e529edSTycho Andersen 		goto out;
2187f8e529edSTycho Andersen 
2188f8e529edSTycho Andersen 	if (copy_to_user(data, fprog->filter, bpf_classic_proglen(fprog)))
2189f8e529edSTycho Andersen 		ret = -EFAULT;
2190f8e529edSTycho Andersen 
2191f8e529edSTycho Andersen out:
21928e5f1ad1STyler Hicks 	__put_seccomp_filter(filter);
21938e5f1ad1STyler Hicks 	return ret;
2194f8e529edSTycho Andersen }
2195f8e529edSTycho Andersen 
seccomp_get_metadata(struct task_struct * task,unsigned long size,void __user * data)219626500475STycho Andersen long seccomp_get_metadata(struct task_struct *task,
219726500475STycho Andersen 			  unsigned long size, void __user *data)
219826500475STycho Andersen {
219926500475STycho Andersen 	long ret;
220026500475STycho Andersen 	struct seccomp_filter *filter;
220126500475STycho Andersen 	struct seccomp_metadata kmd = {};
220226500475STycho Andersen 
220326500475STycho Andersen 	if (!capable(CAP_SYS_ADMIN) ||
220426500475STycho Andersen 	    current->seccomp.mode != SECCOMP_MODE_DISABLED) {
220526500475STycho Andersen 		return -EACCES;
220626500475STycho Andersen 	}
220726500475STycho Andersen 
220826500475STycho Andersen 	size = min_t(unsigned long, size, sizeof(kmd));
220926500475STycho Andersen 
221063bb0045STycho Andersen 	if (size < sizeof(kmd.filter_off))
221163bb0045STycho Andersen 		return -EINVAL;
221263bb0045STycho Andersen 
221363bb0045STycho Andersen 	if (copy_from_user(&kmd.filter_off, data, sizeof(kmd.filter_off)))
221426500475STycho Andersen 		return -EFAULT;
221526500475STycho Andersen 
221626500475STycho Andersen 	filter = get_nth_filter(task, kmd.filter_off);
221726500475STycho Andersen 	if (IS_ERR(filter))
221826500475STycho Andersen 		return PTR_ERR(filter);
221926500475STycho Andersen 
222026500475STycho Andersen 	if (filter->log)
222126500475STycho Andersen 		kmd.flags |= SECCOMP_FILTER_FLAG_LOG;
222226500475STycho Andersen 
222326500475STycho Andersen 	ret = size;
222426500475STycho Andersen 	if (copy_to_user(data, &kmd, size))
222526500475STycho Andersen 		ret = -EFAULT;
222626500475STycho Andersen 
222726500475STycho Andersen 	__put_seccomp_filter(filter);
2228f8e529edSTycho Andersen 	return ret;
2229f8e529edSTycho Andersen }
2230f8e529edSTycho Andersen #endif
22318e5f1ad1STyler Hicks 
22328e5f1ad1STyler Hicks #ifdef CONFIG_SYSCTL
22338e5f1ad1STyler Hicks 
22348e5f1ad1STyler Hicks /* Human readable action names for friendly sysctl interaction */
22350466bdb9SKees Cook #define SECCOMP_RET_KILL_PROCESS_NAME	"kill_process"
2236fd76875cSKees Cook #define SECCOMP_RET_KILL_THREAD_NAME	"kill_thread"
22378e5f1ad1STyler Hicks #define SECCOMP_RET_TRAP_NAME		"trap"
22388e5f1ad1STyler Hicks #define SECCOMP_RET_ERRNO_NAME		"errno"
22396a21cc50STycho Andersen #define SECCOMP_RET_USER_NOTIF_NAME	"user_notif"
22408e5f1ad1STyler Hicks #define SECCOMP_RET_TRACE_NAME		"trace"
224159f5cf44STyler Hicks #define SECCOMP_RET_LOG_NAME		"log"
22428e5f1ad1STyler Hicks #define SECCOMP_RET_ALLOW_NAME		"allow"
22438e5f1ad1STyler Hicks 
2244fd76875cSKees Cook static const char seccomp_actions_avail[] =
22450466bdb9SKees Cook 				SECCOMP_RET_KILL_PROCESS_NAME	" "
2246fd76875cSKees Cook 				SECCOMP_RET_KILL_THREAD_NAME	" "
22478e5f1ad1STyler Hicks 				SECCOMP_RET_TRAP_NAME		" "
22488e5f1ad1STyler Hicks 				SECCOMP_RET_ERRNO_NAME		" "
22496a21cc50STycho Andersen 				SECCOMP_RET_USER_NOTIF_NAME     " "
22508e5f1ad1STyler Hicks 				SECCOMP_RET_TRACE_NAME		" "
225159f5cf44STyler Hicks 				SECCOMP_RET_LOG_NAME		" "
22528e5f1ad1STyler Hicks 				SECCOMP_RET_ALLOW_NAME;
22538e5f1ad1STyler Hicks 
22540ddec0fcSTyler Hicks struct seccomp_log_name {
22550ddec0fcSTyler Hicks 	u32		log;
22560ddec0fcSTyler Hicks 	const char	*name;
22570ddec0fcSTyler Hicks };
22580ddec0fcSTyler Hicks 
22590ddec0fcSTyler Hicks static const struct seccomp_log_name seccomp_log_names[] = {
22600466bdb9SKees Cook 	{ SECCOMP_LOG_KILL_PROCESS, SECCOMP_RET_KILL_PROCESS_NAME },
2261fd76875cSKees Cook 	{ SECCOMP_LOG_KILL_THREAD, SECCOMP_RET_KILL_THREAD_NAME },
22620ddec0fcSTyler Hicks 	{ SECCOMP_LOG_TRAP, SECCOMP_RET_TRAP_NAME },
22630ddec0fcSTyler Hicks 	{ SECCOMP_LOG_ERRNO, SECCOMP_RET_ERRNO_NAME },
22646a21cc50STycho Andersen 	{ SECCOMP_LOG_USER_NOTIF, SECCOMP_RET_USER_NOTIF_NAME },
22650ddec0fcSTyler Hicks 	{ SECCOMP_LOG_TRACE, SECCOMP_RET_TRACE_NAME },
226659f5cf44STyler Hicks 	{ SECCOMP_LOG_LOG, SECCOMP_RET_LOG_NAME },
22670ddec0fcSTyler Hicks 	{ SECCOMP_LOG_ALLOW, SECCOMP_RET_ALLOW_NAME },
22680ddec0fcSTyler Hicks 	{ }
22690ddec0fcSTyler Hicks };
22700ddec0fcSTyler Hicks 
seccomp_names_from_actions_logged(char * names,size_t size,u32 actions_logged,const char * sep)22710ddec0fcSTyler Hicks static bool seccomp_names_from_actions_logged(char *names, size_t size,
2272beb44acaSTyler Hicks 					      u32 actions_logged,
2273beb44acaSTyler Hicks 					      const char *sep)
22740ddec0fcSTyler Hicks {
22750ddec0fcSTyler Hicks 	const struct seccomp_log_name *cur;
2276beb44acaSTyler Hicks 	bool append_sep = false;
22770ddec0fcSTyler Hicks 
22780ddec0fcSTyler Hicks 	for (cur = seccomp_log_names; cur->name && size; cur++) {
22790ddec0fcSTyler Hicks 		ssize_t ret;
22800ddec0fcSTyler Hicks 
22810ddec0fcSTyler Hicks 		if (!(actions_logged & cur->log))
22820ddec0fcSTyler Hicks 			continue;
22830ddec0fcSTyler Hicks 
2284beb44acaSTyler Hicks 		if (append_sep) {
2285beb44acaSTyler Hicks 			ret = strscpy(names, sep, size);
22860ddec0fcSTyler Hicks 			if (ret < 0)
22870ddec0fcSTyler Hicks 				return false;
22880ddec0fcSTyler Hicks 
22890ddec0fcSTyler Hicks 			names += ret;
22900ddec0fcSTyler Hicks 			size -= ret;
22910ddec0fcSTyler Hicks 		} else
2292beb44acaSTyler Hicks 			append_sep = true;
22930ddec0fcSTyler Hicks 
22940ddec0fcSTyler Hicks 		ret = strscpy(names, cur->name, size);
22950ddec0fcSTyler Hicks 		if (ret < 0)
22960ddec0fcSTyler Hicks 			return false;
22970ddec0fcSTyler Hicks 
22980ddec0fcSTyler Hicks 		names += ret;
22990ddec0fcSTyler Hicks 		size -= ret;
23000ddec0fcSTyler Hicks 	}
23010ddec0fcSTyler Hicks 
23020ddec0fcSTyler Hicks 	return true;
23030ddec0fcSTyler Hicks }
23040ddec0fcSTyler Hicks 
seccomp_action_logged_from_name(u32 * action_logged,const char * name)23050ddec0fcSTyler Hicks static bool seccomp_action_logged_from_name(u32 *action_logged,
23060ddec0fcSTyler Hicks 					    const char *name)
23070ddec0fcSTyler Hicks {
23080ddec0fcSTyler Hicks 	const struct seccomp_log_name *cur;
23090ddec0fcSTyler Hicks 
23100ddec0fcSTyler Hicks 	for (cur = seccomp_log_names; cur->name; cur++) {
23110ddec0fcSTyler Hicks 		if (!strcmp(cur->name, name)) {
23120ddec0fcSTyler Hicks 			*action_logged = cur->log;
23130ddec0fcSTyler Hicks 			return true;
23140ddec0fcSTyler Hicks 		}
23150ddec0fcSTyler Hicks 	}
23160ddec0fcSTyler Hicks 
23170ddec0fcSTyler Hicks 	return false;
23180ddec0fcSTyler Hicks }
23190ddec0fcSTyler Hicks 
seccomp_actions_logged_from_names(u32 * actions_logged,char * names)23200ddec0fcSTyler Hicks static bool seccomp_actions_logged_from_names(u32 *actions_logged, char *names)
23210ddec0fcSTyler Hicks {
23220ddec0fcSTyler Hicks 	char *name;
23230ddec0fcSTyler Hicks 
23240ddec0fcSTyler Hicks 	*actions_logged = 0;
23250ddec0fcSTyler Hicks 	while ((name = strsep(&names, " ")) && *name) {
23260ddec0fcSTyler Hicks 		u32 action_logged = 0;
23270ddec0fcSTyler Hicks 
23280ddec0fcSTyler Hicks 		if (!seccomp_action_logged_from_name(&action_logged, name))
23290ddec0fcSTyler Hicks 			return false;
23300ddec0fcSTyler Hicks 
23310ddec0fcSTyler Hicks 		*actions_logged |= action_logged;
23320ddec0fcSTyler Hicks 	}
23330ddec0fcSTyler Hicks 
23340ddec0fcSTyler Hicks 	return true;
23350ddec0fcSTyler Hicks }
23360ddec0fcSTyler Hicks 
read_actions_logged(struct ctl_table * ro_table,void * buffer,size_t * lenp,loff_t * ppos)2337fab686ebSJann Horn static int read_actions_logged(struct ctl_table *ro_table, void *buffer,
2338d013db02STyler Hicks 			       size_t *lenp, loff_t *ppos)
23390ddec0fcSTyler Hicks {
23400ddec0fcSTyler Hicks 	char names[sizeof(seccomp_actions_avail)];
23410ddec0fcSTyler Hicks 	struct ctl_table table;
23420ddec0fcSTyler Hicks 
23430ddec0fcSTyler Hicks 	memset(names, 0, sizeof(names));
23440ddec0fcSTyler Hicks 
23450ddec0fcSTyler Hicks 	if (!seccomp_names_from_actions_logged(names, sizeof(names),
2346beb44acaSTyler Hicks 					       seccomp_actions_logged, " "))
23470ddec0fcSTyler Hicks 		return -EINVAL;
23480ddec0fcSTyler Hicks 
23490ddec0fcSTyler Hicks 	table = *ro_table;
23500ddec0fcSTyler Hicks 	table.data = names;
23510ddec0fcSTyler Hicks 	table.maxlen = sizeof(names);
2352d013db02STyler Hicks 	return proc_dostring(&table, 0, buffer, lenp, ppos);
2353d013db02STyler Hicks }
2354d013db02STyler Hicks 
write_actions_logged(struct ctl_table * ro_table,void * buffer,size_t * lenp,loff_t * ppos,u32 * actions_logged)2355fab686ebSJann Horn static int write_actions_logged(struct ctl_table *ro_table, void *buffer,
2356ea6eca77STyler Hicks 				size_t *lenp, loff_t *ppos, u32 *actions_logged)
23570ddec0fcSTyler Hicks {
23580ddec0fcSTyler Hicks 	char names[sizeof(seccomp_actions_avail)];
23590ddec0fcSTyler Hicks 	struct ctl_table table;
23600ddec0fcSTyler Hicks 	int ret;
23610ddec0fcSTyler Hicks 
2362d013db02STyler Hicks 	if (!capable(CAP_SYS_ADMIN))
23630ddec0fcSTyler Hicks 		return -EPERM;
23640ddec0fcSTyler Hicks 
23650ddec0fcSTyler Hicks 	memset(names, 0, sizeof(names));
23660ddec0fcSTyler Hicks 
23670ddec0fcSTyler Hicks 	table = *ro_table;
23680ddec0fcSTyler Hicks 	table.data = names;
23690ddec0fcSTyler Hicks 	table.maxlen = sizeof(names);
2370d013db02STyler Hicks 	ret = proc_dostring(&table, 1, buffer, lenp, ppos);
23710ddec0fcSTyler Hicks 	if (ret)
23720ddec0fcSTyler Hicks 		return ret;
23730ddec0fcSTyler Hicks 
2374ea6eca77STyler Hicks 	if (!seccomp_actions_logged_from_names(actions_logged, table.data))
23750ddec0fcSTyler Hicks 		return -EINVAL;
23760ddec0fcSTyler Hicks 
2377ea6eca77STyler Hicks 	if (*actions_logged & SECCOMP_LOG_ALLOW)
23780ddec0fcSTyler Hicks 		return -EINVAL;
23790ddec0fcSTyler Hicks 
2380ea6eca77STyler Hicks 	seccomp_actions_logged = *actions_logged;
2381d013db02STyler Hicks 	return 0;
23820ddec0fcSTyler Hicks }
23830ddec0fcSTyler Hicks 
audit_actions_logged(u32 actions_logged,u32 old_actions_logged,int ret)2384ea6eca77STyler Hicks static void audit_actions_logged(u32 actions_logged, u32 old_actions_logged,
2385ea6eca77STyler Hicks 				 int ret)
2386ea6eca77STyler Hicks {
2387ea6eca77STyler Hicks 	char names[sizeof(seccomp_actions_avail)];
2388ea6eca77STyler Hicks 	char old_names[sizeof(seccomp_actions_avail)];
2389ea6eca77STyler Hicks 	const char *new = names;
2390ea6eca77STyler Hicks 	const char *old = old_names;
2391ea6eca77STyler Hicks 
2392ea6eca77STyler Hicks 	if (!audit_enabled)
2393ea6eca77STyler Hicks 		return;
2394ea6eca77STyler Hicks 
2395ea6eca77STyler Hicks 	memset(names, 0, sizeof(names));
2396ea6eca77STyler Hicks 	memset(old_names, 0, sizeof(old_names));
2397ea6eca77STyler Hicks 
2398ea6eca77STyler Hicks 	if (ret)
2399ea6eca77STyler Hicks 		new = "?";
2400ea6eca77STyler Hicks 	else if (!actions_logged)
2401ea6eca77STyler Hicks 		new = "(none)";
2402ea6eca77STyler Hicks 	else if (!seccomp_names_from_actions_logged(names, sizeof(names),
2403ea6eca77STyler Hicks 						    actions_logged, ","))
2404ea6eca77STyler Hicks 		new = "?";
2405ea6eca77STyler Hicks 
2406ea6eca77STyler Hicks 	if (!old_actions_logged)
2407ea6eca77STyler Hicks 		old = "(none)";
2408ea6eca77STyler Hicks 	else if (!seccomp_names_from_actions_logged(old_names,
2409ea6eca77STyler Hicks 						    sizeof(old_names),
2410ea6eca77STyler Hicks 						    old_actions_logged, ","))
2411ea6eca77STyler Hicks 		old = "?";
2412ea6eca77STyler Hicks 
2413ea6eca77STyler Hicks 	return audit_seccomp_actions_logged(new, old, !ret);
2414ea6eca77STyler Hicks }
2415ea6eca77STyler Hicks 
seccomp_actions_logged_handler(struct ctl_table * ro_table,int write,void * buffer,size_t * lenp,loff_t * ppos)2416d013db02STyler Hicks static int seccomp_actions_logged_handler(struct ctl_table *ro_table, int write,
241732927393SChristoph Hellwig 					  void *buffer, size_t *lenp,
2418d013db02STyler Hicks 					  loff_t *ppos)
2419d013db02STyler Hicks {
2420ea6eca77STyler Hicks 	int ret;
2421ea6eca77STyler Hicks 
2422ea6eca77STyler Hicks 	if (write) {
2423ea6eca77STyler Hicks 		u32 actions_logged = 0;
2424ea6eca77STyler Hicks 		u32 old_actions_logged = seccomp_actions_logged;
2425ea6eca77STyler Hicks 
2426ea6eca77STyler Hicks 		ret = write_actions_logged(ro_table, buffer, lenp, ppos,
2427ea6eca77STyler Hicks 					   &actions_logged);
2428ea6eca77STyler Hicks 		audit_actions_logged(actions_logged, old_actions_logged, ret);
2429ea6eca77STyler Hicks 	} else
2430ea6eca77STyler Hicks 		ret = read_actions_logged(ro_table, buffer, lenp, ppos);
2431ea6eca77STyler Hicks 
2432ea6eca77STyler Hicks 	return ret;
24330ddec0fcSTyler Hicks }
24340ddec0fcSTyler Hicks 
24358e5f1ad1STyler Hicks static struct ctl_table seccomp_sysctl_table[] = {
24368e5f1ad1STyler Hicks 	{
24378e5f1ad1STyler Hicks 		.procname	= "actions_avail",
24388e5f1ad1STyler Hicks 		.data		= (void *) &seccomp_actions_avail,
24398e5f1ad1STyler Hicks 		.maxlen		= sizeof(seccomp_actions_avail),
24408e5f1ad1STyler Hicks 		.mode		= 0444,
24418e5f1ad1STyler Hicks 		.proc_handler	= proc_dostring,
24428e5f1ad1STyler Hicks 	},
24430ddec0fcSTyler Hicks 	{
24440ddec0fcSTyler Hicks 		.procname	= "actions_logged",
24450ddec0fcSTyler Hicks 		.mode		= 0644,
24460ddec0fcSTyler Hicks 		.proc_handler	= seccomp_actions_logged_handler,
24470ddec0fcSTyler Hicks 	},
24488e5f1ad1STyler Hicks 	{ }
24498e5f1ad1STyler Hicks };
24508e5f1ad1STyler Hicks 
seccomp_sysctl_init(void)24518e5f1ad1STyler Hicks static int __init seccomp_sysctl_init(void)
24528e5f1ad1STyler Hicks {
245302a6b455SLuis Chamberlain 	register_sysctl_init("kernel/seccomp", seccomp_sysctl_table);
24548e5f1ad1STyler Hicks 	return 0;
24558e5f1ad1STyler Hicks }
24568e5f1ad1STyler Hicks 
device_initcall(seccomp_sysctl_init)24578e5f1ad1STyler Hicks device_initcall(seccomp_sysctl_init)
24588e5f1ad1STyler Hicks 
24598e5f1ad1STyler Hicks #endif /* CONFIG_SYSCTL */
24600d8315ddSYiFei Zhu 
24610d8315ddSYiFei Zhu #ifdef CONFIG_SECCOMP_CACHE_DEBUG
24620d8315ddSYiFei Zhu /* Currently CONFIG_SECCOMP_CACHE_DEBUG implies SECCOMP_ARCH_NATIVE */
24630d8315ddSYiFei Zhu static void proc_pid_seccomp_cache_arch(struct seq_file *m, const char *name,
24640d8315ddSYiFei Zhu 					const void *bitmap, size_t bitmap_size)
24650d8315ddSYiFei Zhu {
24660d8315ddSYiFei Zhu 	int nr;
24670d8315ddSYiFei Zhu 
24680d8315ddSYiFei Zhu 	for (nr = 0; nr < bitmap_size; nr++) {
24690d8315ddSYiFei Zhu 		bool cached = test_bit(nr, bitmap);
24700d8315ddSYiFei Zhu 		char *status = cached ? "ALLOW" : "FILTER";
24710d8315ddSYiFei Zhu 
24720d8315ddSYiFei Zhu 		seq_printf(m, "%s %d %s\n", name, nr, status);
24730d8315ddSYiFei Zhu 	}
24740d8315ddSYiFei Zhu }
24750d8315ddSYiFei Zhu 
proc_pid_seccomp_cache(struct seq_file * m,struct pid_namespace * ns,struct pid * pid,struct task_struct * task)24760d8315ddSYiFei Zhu int proc_pid_seccomp_cache(struct seq_file *m, struct pid_namespace *ns,
24770d8315ddSYiFei Zhu 			   struct pid *pid, struct task_struct *task)
24780d8315ddSYiFei Zhu {
24790d8315ddSYiFei Zhu 	struct seccomp_filter *f;
24800d8315ddSYiFei Zhu 	unsigned long flags;
24810d8315ddSYiFei Zhu 
24820d8315ddSYiFei Zhu 	/*
24830d8315ddSYiFei Zhu 	 * We don't want some sandboxed process to know what their seccomp
24840d8315ddSYiFei Zhu 	 * filters consist of.
24850d8315ddSYiFei Zhu 	 */
24860d8315ddSYiFei Zhu 	if (!file_ns_capable(m->file, &init_user_ns, CAP_SYS_ADMIN))
24870d8315ddSYiFei Zhu 		return -EACCES;
24880d8315ddSYiFei Zhu 
24890d8315ddSYiFei Zhu 	if (!lock_task_sighand(task, &flags))
24900d8315ddSYiFei Zhu 		return -ESRCH;
24910d8315ddSYiFei Zhu 
24920d8315ddSYiFei Zhu 	f = READ_ONCE(task->seccomp.filter);
24930d8315ddSYiFei Zhu 	if (!f) {
24940d8315ddSYiFei Zhu 		unlock_task_sighand(task, &flags);
24950d8315ddSYiFei Zhu 		return 0;
24960d8315ddSYiFei Zhu 	}
24970d8315ddSYiFei Zhu 
24980d8315ddSYiFei Zhu 	/* prevent filter from being freed while we are printing it */
24990d8315ddSYiFei Zhu 	__get_seccomp_filter(f);
25000d8315ddSYiFei Zhu 	unlock_task_sighand(task, &flags);
25010d8315ddSYiFei Zhu 
25020d8315ddSYiFei Zhu 	proc_pid_seccomp_cache_arch(m, SECCOMP_ARCH_NATIVE_NAME,
25030d8315ddSYiFei Zhu 				    f->cache.allow_native,
25040d8315ddSYiFei Zhu 				    SECCOMP_ARCH_NATIVE_NR);
25050d8315ddSYiFei Zhu 
25060d8315ddSYiFei Zhu #ifdef SECCOMP_ARCH_COMPAT
25070d8315ddSYiFei Zhu 	proc_pid_seccomp_cache_arch(m, SECCOMP_ARCH_COMPAT_NAME,
25080d8315ddSYiFei Zhu 				    f->cache.allow_compat,
25090d8315ddSYiFei Zhu 				    SECCOMP_ARCH_COMPAT_NR);
25100d8315ddSYiFei Zhu #endif /* SECCOMP_ARCH_COMPAT */
25110d8315ddSYiFei Zhu 
25120d8315ddSYiFei Zhu 	__put_seccomp_filter(f);
25130d8315ddSYiFei Zhu 	return 0;
25140d8315ddSYiFei Zhu }
25150d8315ddSYiFei Zhu #endif /* CONFIG_SECCOMP_CACHE_DEBUG */
2516