1===========================================
2Seccomp BPF (SECure COMPuting with filters)
3===========================================
4
5Introduction
6============
7
8A large number of system calls are exposed to every userland process
9with many of them going unused for the entire lifetime of the process.
10As system calls change and mature, bugs are found and eradicated.  A
11certain subset of userland applications benefit by having a reduced set
12of available system calls.  The resulting set reduces the total kernel
13surface exposed to the application.  System call filtering is meant for
14use with those applications.
15
16Seccomp filtering provides a means for a process to specify a filter for
17incoming system calls.  The filter is expressed as a Berkeley Packet
18Filter (BPF) program, as with socket filters, except that the data
19operated on is related to the system call being made: system call
20number and the system call arguments.  This allows for expressive
21filtering of system calls using a filter program language with a long
22history of being exposed to userland and a straightforward data set.
23
24Additionally, BPF makes it impossible for users of seccomp to fall prey
25to time-of-check-time-of-use (TOCTOU) attacks that are common in system
26call interposition frameworks.  BPF programs may not dereference
27pointers which constrains all filters to solely evaluating the system
28call arguments directly.
29
30What it isn't
31=============
32
33System call filtering isn't a sandbox.  It provides a clearly defined
34mechanism for minimizing the exposed kernel surface.  It is meant to be
35a tool for sandbox developers to use.  Beyond that, policy for logical
36behavior and information flow should be managed with a combination of
37other system hardening techniques and, potentially, an LSM of your
38choosing.  Expressive, dynamic filters provide further options down this
39path (avoiding pathological sizes or selecting which of the multiplexed
40system calls in socketcall() is allowed, for instance) which could be
41construed, incorrectly, as a more complete sandboxing solution.
42
43Usage
44=====
45
46An additional seccomp mode is added and is enabled using the same
47prctl(2) call as the strict seccomp.  If the architecture has
48``CONFIG_HAVE_ARCH_SECCOMP_FILTER``, then filters may be added as below:
49
50``PR_SET_SECCOMP``:
51	Now takes an additional argument which specifies a new filter
52	using a BPF program.
53	The BPF program will be executed over struct seccomp_data
54	reflecting the system call number, arguments, and other
55	metadata.  The BPF program must then return one of the
56	acceptable values to inform the kernel which action should be
57	taken.
58
59	Usage::
60
61		prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, prog);
62
63	The 'prog' argument is a pointer to a struct sock_fprog which
64	will contain the filter program.  If the program is invalid, the
65	call will return -1 and set errno to ``EINVAL``.
66
67	If ``fork``/``clone`` and ``execve`` are allowed by @prog, any child
68	processes will be constrained to the same filters and system
69	call ABI as the parent.
70
71	Prior to use, the task must call ``prctl(PR_SET_NO_NEW_PRIVS, 1)`` or
72	run with ``CAP_SYS_ADMIN`` privileges in its namespace.  If these are not
73	true, ``-EACCES`` will be returned.  This requirement ensures that filter
74	programs cannot be applied to child processes with greater privileges
75	than the task that installed them.
76
77	Additionally, if ``prctl(2)`` is allowed by the attached filter,
78	additional filters may be layered on which will increase evaluation
79	time, but allow for further decreasing the attack surface during
80	execution of a process.
81
82The above call returns 0 on success and non-zero on error.
83
84Return values
85=============
86
87A seccomp filter may return any of the following values. If multiple
88filters exist, the return value for the evaluation of a given system
89call will always use the highest precedent value. (For example,
90``SECCOMP_RET_KILL_PROCESS`` will always take precedence.)
91
92In precedence order, they are:
93
94``SECCOMP_RET_KILL_PROCESS``:
95	Results in the entire process exiting immediately without executing
96	the system call.  The exit status of the task (``status & 0x7f``)
97	will be ``SIGSYS``, not ``SIGKILL``.
98
99``SECCOMP_RET_KILL_THREAD``:
100	Results in the task exiting immediately without executing the
101	system call.  The exit status of the task (``status & 0x7f``) will
102	be ``SIGSYS``, not ``SIGKILL``.
103
104``SECCOMP_RET_TRAP``:
105	Results in the kernel sending a ``SIGSYS`` signal to the triggering
106	task without executing the system call. ``siginfo->si_call_addr``
107	will show the address of the system call instruction, and
108	``siginfo->si_syscall`` and ``siginfo->si_arch`` will indicate which
109	syscall was attempted.  The program counter will be as though
110	the syscall happened (i.e. it will not point to the syscall
111	instruction).  The return value register will contain an arch-
112	dependent value -- if resuming execution, set it to something
113	sensible.  (The architecture dependency is because replacing
114	it with ``-ENOSYS`` could overwrite some useful information.)
115
116	The ``SECCOMP_RET_DATA`` portion of the return value will be passed
117	as ``si_errno``.
118
119	``SIGSYS`` triggered by seccomp will have a si_code of ``SYS_SECCOMP``.
120
121``SECCOMP_RET_ERRNO``:
122	Results in the lower 16-bits of the return value being passed
123	to userland as the errno without executing the system call.
124
125``SECCOMP_RET_TRACE``:
126	When returned, this value will cause the kernel to attempt to
127	notify a ``ptrace()``-based tracer prior to executing the system
128	call.  If there is no tracer present, ``-ENOSYS`` is returned to
129	userland and the system call is not executed.
130
131	A tracer will be notified if it requests ``PTRACE_O_TRACESECCOM``P
132	using ``ptrace(PTRACE_SETOPTIONS)``.  The tracer will be notified
133	of a ``PTRACE_EVENT_SECCOMP`` and the ``SECCOMP_RET_DATA`` portion of
134	the BPF program return value will be available to the tracer
135	via ``PTRACE_GETEVENTMSG``.
136
137	The tracer can skip the system call by changing the syscall number
138	to -1.  Alternatively, the tracer can change the system call
139	requested by changing the system call to a valid syscall number.  If
140	the tracer asks to skip the system call, then the system call will
141	appear to return the value that the tracer puts in the return value
142	register.
143
144	The seccomp check will not be run again after the tracer is
145	notified.  (This means that seccomp-based sandboxes MUST NOT
146	allow use of ptrace, even of other sandboxed processes, without
147	extreme care; ptracers can use this mechanism to escape.)
148
149``SECCOMP_RET_LOG``:
150	Results in the system call being executed after it is logged. This
151	should be used by application developers to learn which syscalls their
152	application needs without having to iterate through multiple test and
153	development cycles to build the list.
154
155	This action will only be logged if "log" is present in the
156	actions_logged sysctl string.
157
158``SECCOMP_RET_ALLOW``:
159	Results in the system call being executed.
160
161If multiple filters exist, the return value for the evaluation of a
162given system call will always use the highest precedent value.
163
164Precedence is only determined using the ``SECCOMP_RET_ACTION`` mask.  When
165multiple filters return values of the same precedence, only the
166``SECCOMP_RET_DATA`` from the most recently installed filter will be
167returned.
168
169Pitfalls
170========
171
172The biggest pitfall to avoid during use is filtering on system call
173number without checking the architecture value.  Why?  On any
174architecture that supports multiple system call invocation conventions,
175the system call numbers may vary based on the specific invocation.  If
176the numbers in the different calling conventions overlap, then checks in
177the filters may be abused.  Always check the arch value!
178
179Example
180=======
181
182The ``samples/seccomp/`` directory contains both an x86-specific example
183and a more generic example of a higher level macro interface for BPF
184program generation.
185
186Sysctls
187=======
188
189Seccomp's sysctl files can be found in the ``/proc/sys/kernel/seccomp/``
190directory. Here's a description of each file in that directory:
191
192``actions_avail``:
193	A read-only ordered list of seccomp return values (refer to the
194	``SECCOMP_RET_*`` macros above) in string form. The ordering, from
195	left-to-right, is the least permissive return value to the most
196	permissive return value.
197
198	The list represents the set of seccomp return values supported
199	by the kernel. A userspace program may use this list to
200	determine if the actions found in the ``seccomp.h``, when the
201	program was built, differs from the set of actions actually
202	supported in the current running kernel.
203
204``actions_logged``:
205	A read-write ordered list of seccomp return values (refer to the
206	``SECCOMP_RET_*`` macros above) that are allowed to be logged. Writes
207	to the file do not need to be in ordered form but reads from the file
208	will be ordered in the same way as the actions_avail sysctl.
209
210	The ``allow`` string is not accepted in the ``actions_logged`` sysctl
211	as it is not possible to log ``SECCOMP_RET_ALLOW`` actions. Attempting
212	to write ``allow`` to the sysctl will result in an EINVAL being
213	returned.
214
215Adding architecture support
216===========================
217
218See ``arch/Kconfig`` for the authoritative requirements.  In general, if an
219architecture supports both ptrace_event and seccomp, it will be able to
220support seccomp filter with minor fixup: ``SIGSYS`` support and seccomp return
221value checking.  Then it must just add ``CONFIG_HAVE_ARCH_SECCOMP_FILTER``
222to its arch-specific Kconfig.
223
224
225
226Caveats
227=======
228
229The vDSO can cause some system calls to run entirely in userspace,
230leading to surprises when you run programs on different machines that
231fall back to real syscalls.  To minimize these surprises on x86, make
232sure you test with
233``/sys/devices/system/clocksource/clocksource0/current_clocksource`` set to
234something like ``acpi_pm``.
235
236On x86-64, vsyscall emulation is enabled by default.  (vsyscalls are
237legacy variants on vDSO calls.)  Currently, emulated vsyscalls will
238honor seccomp, with a few oddities:
239
240- A return value of ``SECCOMP_RET_TRAP`` will set a ``si_call_addr`` pointing to
241  the vsyscall entry for the given call and not the address after the
242  'syscall' instruction.  Any code which wants to restart the call
243  should be aware that (a) a ret instruction has been emulated and (b)
244  trying to resume the syscall will again trigger the standard vsyscall
245  emulation security checks, making resuming the syscall mostly
246  pointless.
247
248- A return value of ``SECCOMP_RET_TRACE`` will signal the tracer as usual,
249  but the syscall may not be changed to another system call using the
250  orig_rax register. It may only be changed to -1 order to skip the
251  currently emulated call. Any other change MAY terminate the process.
252  The rip value seen by the tracer will be the syscall entry address;
253  this is different from normal behavior.  The tracer MUST NOT modify
254  rip or rsp.  (Do not rely on other changes terminating the process.
255  They might work.  For example, on some kernels, choosing a syscall
256  that only exists in future kernels will be correctly emulated (by
257  returning ``-ENOSYS``).
258
259To detect this quirky behavior, check for ``addr & ~0x0C00 ==
2600xFFFFFFFFFF600000``.  (For ``SECCOMP_RET_TRACE``, use rip.  For
261``SECCOMP_RET_TRAP``, use ``siginfo->si_call_addr``.)  Do not check any other
262condition: future kernels may improve vsyscall emulation and current
263kernels in vsyscall=native mode will behave differently, but the
264instructions at ``0xF...F600{0,4,8,C}00`` will not be system calls in these
265cases.
266
267Note that modern systems are unlikely to use vsyscalls at all -- they
268are a legacy feature and they are considerably slower than standard
269syscalls.  New code will use the vDSO, and vDSO-issued system calls
270are indistinguishable from normal system calls.
271