[PATCH RFC 2/4] Introduce seccomp-assisted syscall filtering

Paul Chaignon paul.chaignon at gmail.com
Sat Jul 13 10:24:11 UTC 2019


From: Chen Jingpiao <chenjingpiao at gmail.com>

With this patch, strace can rely on seccomp to only be stopped at syscalls
of interest, instead of stopping at all syscalls.  The seccomp filtering
of syscalls is opt-in only; it must be enabled with the -n option.  Kernel
support is first checked with check_seccomp_filter(), which also ensures
the BPF program derived from the syscalls to filter is not too larger than
the kernel's limit.

When using -n, -f is also required.  Since a task's children inherit its
seccomp filters, we want to ensure all children are also traced to avoid
their syscalls failing with ENOSYS (cf. SECCOMP_RET_TRACE in seccomp man
page).

The current BPF program implements a simple linear match of the syscall
numbers.  It can be improved in the future without impacting user-observed
behavior.

The behavior of SECCOMP_RET_TRACE changed between Linux 4.7 and 4.8 (cf.
PTRACE_EVENT_SECCOMP in ptrace man page).  This patch supports both
behavior by checking the kernel's actual behavior before installing the
seccomp filter.

* filter_seccomp.c: New file.
* filter_seccomp.h: New file.
* Makefile.am (strace_SOURCES): Add filter_seccomp.c and filter_seccomp.h.
* strace.c (exec_or_die): Initialize seccomp filtering if requested.
(init): Handle -n option and check that seccomp can be enabled.
(print_debug_info): Handle PTRACE_EVENT_SECCOMP.
(next_event): Capture PTRACE_EVENT_SECCOMP event.
(dispatch_event): Handle PTRACE_EVENT_SECCOMP event.
* trace_event.h (trace_event): New enumeration entity.
* strace.1.in: Document new -n option.
* NEWS: Mention this change.

Co-authored-by: Paul Chaignon <paul.chaignon at gmail.com>
---
 Makefile.am      |   2 +
 NEWS             |   1 +
 filter_seccomp.c | 446 +++++++++++++++++++++++++++++++++++++++++++++++
 filter_seccomp.h |  57 ++++++
 strace.1.in      |  10 ++
 strace.c         |  38 +++-
 trace_event.h    |   5 +
 7 files changed, 556 insertions(+), 3 deletions(-)
 create mode 100644 filter_seccomp.c
 create mode 100644 filter_seccomp.h

diff --git a/Makefile.am b/Makefile.am
index 0fa7f25d..eb923439 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -129,6 +129,8 @@ strace_SOURCES =	\
 	file_ioctl.c	\
 	filter.h	\
 	filter_qualify.c \
+	filter_seccomp.c \
+	filter_seccomp.h \
 	flock.c		\
 	flock.h		\
 	fs_x_ioctl.c	\
diff --git a/NEWS b/NEWS
index 8241dc40..7d01d073 100644
--- a/NEWS
+++ b/NEWS
@@ -15,6 +15,7 @@ Noteworthy changes in release ?.? (????-??-??)
   * Updated lists of AT_*, AUDIT_*, BPF_*, CLONE_*, ETH_*, KEY_*, KVM_*, MPOL_*,
     TIPC_*, and V4L2_* constants.
   * Updated lists of ioctl commands from Linux 5.2.
+  * Implemented seccomp filtering of system calls.  Use -n option to enable.
 
 Noteworthy changes in release 5.1 (2019-05-22)
 ==============================================
diff --git a/filter_seccomp.c b/filter_seccomp.c
new file mode 100644
index 00000000..58e5836a
--- /dev/null
+++ b/filter_seccomp.c
@@ -0,0 +1,446 @@
+/*
+ * Copyright (c) 2018 Chen Jingpiao <chenjingpiao at gmail.com>
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "defs.h"
+
+#include "ptrace.h"
+#include <sys/prctl.h>
+#include <sys/wait.h>
+#include <linux/audit.h>
+#include <linux/filter.h>
+#include <linux/seccomp.h>
+#include <asm/unistd.h>
+#include <signal.h>
+
+#include "filter_seccomp.h"
+#include "number_set.h"
+
+bool seccomp_filtering = false;
+bool seccomp_before_sysentry;
+
+static void
+check_seccomp_order_do_child(void)
+{
+	struct sock_filter filter[] = {
+		BPF_STMT(BPF_LD + BPF_W + BPF_ABS,
+			 offsetof(struct seccomp_data, nr)),
+		BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, __NR_getuid, 0, 1),
+		BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_TRACE),
+		BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW)
+	};
+	struct sock_fprog prog = {
+		.len = ARRAY_SIZE(filter),
+		.filter = filter
+	};
+
+	if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) < 0) {
+		/* Check if strace is already being traced. */
+		if (errno == EPERM)
+			_exit(0);
+		else
+			perror_func_msg_and_die("ptrace(PTRACE_TRACEME, ...");
+	}
+	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0)
+		perror_func_msg_and_die("prctl(PR_SET_NO_NEW_PRIVS, 1, ...");
+	if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0)
+		perror_func_msg_and_die("prctl(PR_SET_SECCOMP)");
+	kill(getpid(), SIGSTOP);
+	syscall(__NR_getuid);
+	pause();
+	_exit(0);
+}
+
+static int
+check_seccomp_order_tracer(int pid)
+{
+	int status, tracee_pid, flags = 0;
+
+	while (1) {
+		errno = 0;
+		tracee_pid = waitpid(pid, &status, 0);
+		if (tracee_pid <= 0) {
+			if (errno == EINTR)
+				continue;
+			perror_func_msg("unexpected wait result %d",
+					tracee_pid);
+			return -1;
+		}
+		if (flags == 0) {
+			if (ptrace(PTRACE_SETOPTIONS, pid, 0,
+				   PTRACE_O_TRACESECCOMP) < 0) {
+				/*
+				 * Check if child exited early because strace
+				 * is already being traced.
+				 */
+				if (errno != ESRCH)
+					perror_func_msg("ptrace(PTRACE_SETOPTIONS, ...");
+				return -1;
+			}
+			if (ptrace(PTRACE_SYSCALL, pid, NULL, NULL) < 0) {
+				perror_func_msg("ptrace(PTRACE_SYSCALL, ...");
+				return -1;
+			}
+		} else if (flags == 1) {
+			const unsigned int event = (unsigned int) status >> 16;
+			seccomp_before_sysentry = event == PTRACE_EVENT_SECCOMP;
+			kill(pid, SIGKILL);
+		} else {
+			if (WIFSIGNALED(status))
+				break;
+
+			error_func_msg("unexpected wait status %#x",
+					       status);
+			return -1;
+		}
+		flags++;
+	}
+	return 0;
+}
+
+static int
+check_seccomp_order(void)
+{
+	int pid;
+
+	pid = fork();
+	if (pid < 0) {
+		perror_func_msg("fork");
+		return -1;
+	}
+
+	if (pid == 0)
+		check_seccomp_order_do_child();
+
+	return check_seccomp_order_tracer(pid);
+}
+
+static bool
+traced_by_seccomp(unsigned int scno, unsigned int p)
+{
+	return !sysent_vec[p][scno].sys_func
+	       || sysent_vec[p][scno].sys_flags & TRACE_INDIRECT_SUBCALL
+	       || is_number_in_set_array(scno, trace_set, p)
+	       || strcmp("execve", sysent_vec[p][scno].sys_name) == 0
+	       || strcmp("execveat", sysent_vec[p][scno].sys_name) == 0
+#if defined SPARC || defined SPARC64
+	       || strcmp("execv", sysent_vec[p][scno].sys_name) == 0
+#endif
+	       || strcmp("socketcall", sysent_vec[p][scno].sys_name) == 0
+	       || strcmp("ipc", sysent_vec[p][scno].sys_name) == 0
+#ifdef LINUX_MIPSO32
+	       || strcmp("syscall", sysent_vec[p][scno].sys_name) == 0
+#endif
+	       ;
+}
+
+static void
+check_bpf_instruction_number(void)
+{
+	for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
+		unsigned int lower = UINT_MAX, count = 0;
+
+		for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
+			if (traced_by_seccomp(i, p)) {
+				if (lower == UINT_MAX)
+					lower = i;
+				continue;
+			}
+			if (lower == UINT_MAX)
+				continue;
+			if (lower + 1 == i)
+				count++;
+			else
+				count += 2;
+			lower = UINT_MAX;
+		}
+		if (lower != UINT_MAX)
+			count += 2;
+		if (count > SECCOMP_TRACE_SYSCALL_MAX) {
+			seccomp_filtering = false;
+			break;
+		}
+	}
+}
+
+void
+check_seccomp_filter(void)
+{
+	if (!seccomp_filtering)
+		return;
+#ifdef SECCOMP_MODE_FILTER
+	int rc;
+
+	if (NOMMU_SYSTEM) {
+		seccomp_filtering = false;
+		goto end;
+	}
+
+	rc = prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL, 0, 0);
+	seccomp_filtering = rc >= 0 || errno != EINVAL;
+	if (seccomp_filtering)
+		check_bpf_instruction_number();
+	if (seccomp_filtering && check_seccomp_order() < 0)
+		seccomp_filtering = false;
+#else
+	seccomp_filtering = false;
+#endif
+end:
+	error_msg("seccomp-filter %s",
+		  seccomp_filtering ? "enabled" : "disabled");
+}
+
+static unsigned short
+bpf_add_syscalls(struct sock_filter *filter,
+		       unsigned int lower, unsigned int upper)
+{
+	if (lower + 1 == upper) {
+		/* filter[X].jt will set when return instruction added */
+		SET_BPF_JUMP(filter, BPF_JMP + BPF_JEQ + BPF_K, lower, 0, 0);
+		return 1;
+	} else {
+		SET_BPF_JUMP(filter, BPF_JMP + BPF_JGE + BPF_K, lower, 0, 1);
+		++filter;
+		/* filter[X].jf will set when return instruction added */
+		SET_BPF_JUMP(filter, BPF_JMP + BPF_JGE + BPF_K, upper, 0, 0);
+		return 2;
+	}
+}
+
+static void
+dump_seccomp_bpf(const struct sock_filter *filter, unsigned short len)
+{
+	for (unsigned int i = 0; i < len; ++i) {
+		switch (filter[i].code) {
+		case BPF_LD + BPF_W + BPF_ABS:
+			switch (filter[i].k) {
+			case offsetof(struct seccomp_data, arch):
+				error_msg("STMT(BPF_LDWABS, data->arch)");
+				break;
+			case offsetof(struct seccomp_data, nr):
+				error_msg("STMT(BPF_LDWABS, data->nr)");
+				break;
+			default:
+				error_msg("STMT(BPF_LDWABS, 0x%x)",
+					  filter[i].k);
+			}
+			break;
+		case BPF_RET + BPF_K:
+			switch (filter[i].k) {
+			case SECCOMP_RET_TRACE:
+				error_msg("STMT(BPF_RET, SECCOMP_RET_TRACE)");
+				break;
+			case SECCOMP_RET_ALLOW:
+				error_msg("STMT(BPF_RET, SECCOMP_RET_ALLOW)");
+				break;
+			default:
+				error_msg("STMT(BPF_RET, 0x%x)", filter[i].k);
+			}
+			break;
+		case BPF_JMP + BPF_JEQ + BPF_K:
+			switch (filter[i].k) {
+			case AUDIT_ARCH_X86_64:
+				error_msg("JUMP(BPF_JEQ, %u, %u, AUDIT_ARCH_X86_64)",
+					  filter[i].jt, filter[i].jf);
+				break;
+			case AUDIT_ARCH_I386:
+				error_msg("JUMP(BPF_JEQ, %u, %u, AUDIT_ARCH_I386)",
+					  filter[i].jt, filter[i].jf);
+				break;
+			case __X32_SYSCALL_BIT:
+				error_msg("JUMP(BPF_JEQ, %u, %u, __X32_SYSCALL_BIT)",
+					  filter[i].jt, filter[i].jf);
+				break;
+			default:
+				error_msg("JUMP(BPF_JEQ, %u, %u, %u)",
+					  filter[i].jt, filter[i].jf,
+					  filter[i].k);
+			}
+			break;
+		case BPF_JMP + BPF_JGE + BPF_K:
+			switch (filter[i].k) {
+			case AUDIT_ARCH_X86_64:
+				error_msg("JUMP(BPF_JGE, %u, %u, AUDIT_ARCH_X86_64)",
+					  filter[i].jt, filter[i].jf);
+				break;
+			case AUDIT_ARCH_I386:
+				error_msg("JUMP(BPF_JGE, %u, %u, AUDIT_ARCH_I386)",
+					  filter[i].jt, filter[i].jf);
+				break;
+			case __X32_SYSCALL_BIT:
+				error_msg("JUMP(BPF_JGE, %u, %u, __X32_SYSCALL_BIT)",
+					  filter[i].jt, filter[i].jf);
+				break;
+			default:
+				error_msg("JUMP(BPF_JGE, %u, %u, %u)",
+					  filter[i].jt, filter[i].jf,
+					  filter[i].k);
+			}
+			break;
+		default:
+			error_msg("STMT(0x%x, %u, %u, 0x%x)", filter[i].code,
+				  filter[i].jt, filter[i].jf, filter[i].k);
+		}
+	}
+}
+
+static unsigned short
+init_sock_filter(struct sock_filter *filter)
+{
+	unsigned short pos = 0;
+#if SUPPORTED_PERSONALITIES > 1
+	unsigned int audit_arch_vec[] = {
+# if defined X86_64
+		AUDIT_ARCH_X86_64,
+		AUDIT_ARCH_I386,
+		AUDIT_ARCH_X86_64
+# elif SUPPORTED_PERSONALITIES == 2
+		AUDIT_ARCH_X86_64,
+		AUDIT_ARCH_I386
+# endif
+	};
+#endif
+	unsigned int syscall_bit_vec[] = {
+#if defined X86_64
+		0, 0, __X32_SYSCALL_BIT
+#elif defined X32
+		__X32_SYSCALL_BIT, 0
+#elif SUPPORTED_PERSONALITIES == 2
+		0, 0
+#else
+		0
+#endif
+	};
+
+#if SUPPORTED_PERSONALITIES > 1
+	SET_BPF_STMT(&filter[pos++], BPF_LD + BPF_W + BPF_ABS,
+		     offsetof(struct seccomp_data, arch));
+#endif
+	for (unsigned int p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
+		unsigned int lower = UINT_MAX;
+		unsigned short previous = pos, start, end;
+
+#if SUPPORTED_PERSONALITIES > 1
+		/* Jump offset is set once the return instruction is added. */
+		SET_BPF_JUMP(&filter[pos++], BPF_JMP + BPF_JEQ + BPF_K,
+			     audit_arch_vec[p], 0, 0);
+#endif
+		SET_BPF_STMT(&filter[pos++], BPF_LD + BPF_W + BPF_ABS,
+			     offsetof(struct seccomp_data, nr));
+
+		start = pos;
+		for (unsigned int i = 0; i < nsyscall_vec[p]; ++i) {
+			if (traced_by_seccomp(i, p)) {
+				if (lower == UINT_MAX)
+					lower = i;
+				continue;
+			}
+			if (lower == UINT_MAX)
+				continue;
+			pos += bpf_add_syscalls(filter + pos,
+						lower + syscall_bit_vec[p],
+						i + syscall_bit_vec[p]);
+			lower = UINT_MAX;
+		}
+		if (lower != UINT_MAX)
+			pos += bpf_add_syscalls(filter + pos,
+						lower + syscall_bit_vec[p],
+						nsyscall_vec[p] + syscall_bit_vec[p]);
+		end = pos;
+
+#ifdef X86_64
+		if (p == 0) {
+			SET_BPF_JUMP(&filter[pos++], BPF_JMP + BPF_JGE + BPF_K,
+				     __X32_SYSCALL_BIT, 0, 2);
+			SET_BPF_STMT(&filter[pos++], BPF_LD + BPF_W + BPF_ABS,
+				     offsetof(struct seccomp_data, arch));
+			SET_BPF_JUMP(&filter[pos++], BPF_JMP + BPF_JEQ + BPF_K,
+				     AUDIT_ARCH_X86_64, 3, 0);
+
+			SET_BPF_STMT(&filter[pos++], BPF_LD + BPF_W + BPF_ABS,
+				     offsetof(struct seccomp_data, nr));
+		}
+#endif
+		SET_BPF_JUMP(&filter[pos++], BPF_JMP + BPF_JGE + BPF_K,
+			     nsyscall_vec[p] + syscall_bit_vec[p], 1, 0);
+
+		SET_BPF_STMT(&filter[pos++], BPF_RET + BPF_K,
+			     SECCOMP_RET_ALLOW);
+		SET_BPF_STMT(&filter[pos++], BPF_RET + BPF_K,
+			     SECCOMP_RET_TRACE);
+		filter[previous].jf = pos - previous - 1;
+		for (unsigned int i = start; i < end; ++i) {
+			if (BPF_CLASS(filter[i].code) != BPF_JMP)
+				continue;
+			if (BPF_OP(filter[i].code) == BPF_JEQ)
+				filter[i].jt = pos - i - 2;
+			else if (BPF_OP(filter[i].code) == BPF_JGE
+				 && filter[i].jf == 0)
+				filter[i].jf = pos - i - 2;
+		}
+	}
+#if SUPPORTED_PERSONALITIES > 1
+	SET_BPF_STMT(&filter[pos++], BPF_RET + BPF_K, SECCOMP_RET_TRACE);
+#endif
+
+	if (debug_flag)
+		dump_seccomp_bpf(filter, pos);
+
+	return pos;
+}
+
+void
+init_seccomp_filter(void)
+{
+	struct sock_filter filter[SECCOMP_BPF_MAXINSNS];
+	unsigned short len;
+
+	len = init_sock_filter(filter);
+
+	struct sock_fprog prog = {
+		.len = len,
+		.filter = filter
+	};
+
+	if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) < 0) {
+		perror_msg("prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)");
+		return;
+	}
+
+	if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0)
+		perror_msg("prctl");
+}
+
+int
+seccomp_filter_restart_operator(const struct tcb *tcp)
+{
+	if (tcp && exiting(tcp)
+	    && tcp->scno < nsyscall_vec[current_personality]
+	    && traced_by_seccomp(tcp->scno, current_personality))
+		return PTRACE_SYSCALL;
+	return PTRACE_CONT;
+}
diff --git a/filter_seccomp.h b/filter_seccomp.h
new file mode 100644
index 00000000..a5a2ffa6
--- /dev/null
+++ b/filter_seccomp.h
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2018 Chen Jingpiao <chenjingpiao at gmail.com>
+ * Copyright (c) 2018 The strace developers.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef STRACE_SECCOMP_FILTER_H
+#define STRACE_SECCOMP_FILTER_H
+
+#include "defs.h"
+
+#ifdef HAVE_LINUX_SECCOMP_H
+# include <linux/seccomp.h>
+#endif
+
+#define SECCOMP_TRACE_SYSCALL_MAX	(SUPPORTED_PERSONALITIES * 150)
+#define SECCOMP_BPF_MAXINSNS		(SECCOMP_TRACE_SYSCALL_MAX + 200)
+
+extern bool seccomp_filtering;
+extern bool seccomp_before_sysentry;
+
+extern void check_seccomp_filter(void);
+extern void init_seccomp_filter(void);
+extern int seccomp_filter_restart_operator(const struct tcb *);
+
+#define SET_BPF(filter, code, jt, jf, k) \
+	(*(filter) = (struct sock_filter) { code, jt, jf, k })
+
+#define SET_BPF_STMT(filter, code, k) \
+	SET_BPF(filter, code, 0, 0, k)
+
+#define SET_BPF_JUMP(filter, code, k, jt, jf) \
+	SET_BPF(filter, code, jt, jf, k)
+
+#endif /* !STRACE_SECCOMP_FILTER_H */
diff --git a/strace.1.in b/strace.1.in
index 56728371..292cf04d 100644
--- a/strace.1.in
+++ b/strace.1.in
@@ -951,6 +951,16 @@ Show some debugging output of
 .B strace
 itself on the standard error.
 .TP
+.B \-n
+Enable use of seccomp-bpf to interrupt only system calls that are being traced.
+Requires the
+.B \-f
+option.
+The attempt to rely on seccomp-bpf to filter system calls may fail for diverse
+reasons: too many system calls to filter, seccomp API unavailable, or strace is
+itself being traced.  In those cases, strace proceeds as usual and interrupts
+all system calls.
+.TP
 .B \-F
 This option is deprecated.  It is retained for backward compatibility only
 and may be removed in future releases.
diff --git a/strace.c b/strace.c
index 4f03a4c9..272f00e4 100644
--- a/strace.c
+++ b/strace.c
@@ -31,6 +31,7 @@
 #include <asm/unistd.h>
 
 #include "kill_save_errno.h"
+#include "filter_seccomp.h"
 #include "largefile_wrappers.h"
 #include "mmap_cache.h"
 #include "number_set.h"
@@ -294,6 +295,7 @@ Startup:\n\
 \n\
 Miscellaneous:\n\
   -d             enable debug output to stderr\n\
+  -n             enable seccomp-bpf filtering\n\
   -v             verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
   -h             print help message\n\
   -V             print version\n\
@@ -1208,6 +1210,8 @@ exec_or_die(void)
 	if (params_for_tracee.child_sa.sa_handler != SIG_DFL)
 		sigaction(SIGCHLD, &params_for_tracee.child_sa, NULL);
 
+	if (seccomp_filtering)
+		init_seccomp_filter();
 	execv(params->pathname, params->argv);
 	perror_msg_and_die("exec");
 }
@@ -1583,7 +1587,7 @@ init(int argc, char *argv[])
 #ifdef ENABLE_STACKTRACE
 	    "k"
 #endif
-	    "a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yzZ")) != EOF) {
+	    "a:Ab:cCdDe:E:fFhiI:no:O:p:P:qrs:S:tTu:vVwxX:yzZ")) != EOF) {
 		switch (c) {
 		case 'a':
 			acolumn = string_to_uint(optarg);
@@ -1685,6 +1689,9 @@ init(int argc, char *argv[])
 		case 'u':
 			username = optarg;
 			break;
+		case 'n':
+			seccomp_filtering = true;
+			break;
 		case 'v':
 			qualify("abbrev=none");
 			break;
@@ -1738,6 +1745,10 @@ init(int argc, char *argv[])
 		error_msg_and_help("PROG [ARGS] must be specified with -D");
 	}
 
+	if (seccomp_filtering && !followfork) {
+		error_msg_and_help("-n requires -f");
+	}
+
 	if (optF) {
 		if (followfork) {
 			error_msg("deprecated option -F ignored");
@@ -1809,6 +1820,10 @@ init(int argc, char *argv[])
 		run_gid = getgid();
 	}
 
+	check_seccomp_filter();
+	if (seccomp_filtering)
+		ptrace_setoptions |= PTRACE_O_TRACESECCOMP;
+
 	if (followfork)
 		ptrace_setoptions |= PTRACE_O_TRACECLONE |
 				     PTRACE_O_TRACEFORK |
@@ -2000,6 +2015,7 @@ print_debug_info(const int pid, int status)
 			[PTRACE_EVENT_VFORK_DONE] = "VFORK_DONE",
 			[PTRACE_EVENT_EXEC]  = "EXEC",
 			[PTRACE_EVENT_EXIT]  = "EXIT",
+			[PTRACE_EVENT_SECCOMP]  = "SECCOMP",
 			/* [PTRACE_EVENT_STOP (=128)] would make biggish array */
 		};
 		const char *e = "??";
@@ -2530,6 +2546,9 @@ next_event(void)
 			case PTRACE_EVENT_EXIT:
 				wd->te = TE_STOP_BEFORE_EXIT;
 				break;
+			case PTRACE_EVENT_SECCOMP:
+				wd->te = TE_SECCOMP;
+				break;
 			default:
 				wd->te = TE_RESTART;
 			}
@@ -2615,8 +2634,7 @@ trace_syscall(struct tcb *tcp, unsigned int *sig)
 static bool
 dispatch_event(const struct tcb_wait_data *wd)
 {
-	unsigned int restart_op = PTRACE_SYSCALL;
-	unsigned int restart_sig = 0;
+	unsigned int restart_sig = 0, restart_op;
 	enum trace_event te = wd ? wd->te : TE_BREAK;
 	/*
 	 * Copy wd->status to a non-const variable to workaround glibc bugs
@@ -2624,6 +2642,11 @@ dispatch_event(const struct tcb_wait_data *wd)
 	 */
 	int status = wd ? wd->status : 0;
 
+	if (seccomp_filtering)
+		restart_op = seccomp_filter_restart_operator(current_tcp);
+	else
+		restart_op = PTRACE_SYSCALL;
+
 	switch (te) {
 	case TE_BREAK:
 		return false;
@@ -2634,6 +2657,13 @@ dispatch_event(const struct tcb_wait_data *wd)
 	case TE_RESTART:
 		break;
 
+	case TE_SECCOMP:
+		if (seccomp_before_sysentry) {
+			restart_op = PTRACE_SYSCALL;
+			break;
+		}
+		ATTRIBUTE_FALLTHROUGH;
+
 	case TE_SYSCALL_STOP:
 		if (trace_syscall(current_tcp, &restart_sig) < 0) {
 			/*
@@ -2649,6 +2679,8 @@ dispatch_event(const struct tcb_wait_data *wd)
 			 */
 			return true;
 		}
+		if (seccomp_filtering)
+			restart_op = exiting(current_tcp)? PTRACE_SYSCALL : PTRACE_CONT;
 		break;
 
 	case TE_SIGNAL_DELIVERY_STOP:
diff --git a/trace_event.h b/trace_event.h
index 53a711b8..9021fc55 100644
--- a/trace_event.h
+++ b/trace_event.h
@@ -66,6 +66,11 @@ enum trace_event {
 	 * Restart the tracee with signal 0.
 	 */
 	TE_STOP_BEFORE_EXIT,
+
+	/*
+	 * SECCOMP_RET_TRACE rule is triggered.
+	 */
+	TE_SECCOMP,
 };
 
 #endif /* !STRACE_TRACE_EVENT_H */
-- 
2.17.1



More information about the Strace-devel mailing list