Skip to content

Commit 6a21cc5

Browse files
tych0kees
authored andcommitted
seccomp: add a return code to trap to userspace
This patch introduces a means for syscalls matched in seccomp to notify some other task that a particular filter has been triggered. The motivation for this is primarily for use with containers. For example, if a container does an init_module(), we obviously don't want to load this untrusted code, which may be compiled for the wrong version of the kernel anyway. Instead, we could parse the module image, figure out which module the container is trying to load and load it on the host. As another example, containers cannot mount() in general since various filesystems assume a trusted image. However, if an orchestrator knows that e.g. a particular block device has not been exposed to a container for writing, it want to allow the container to mount that block device (that is, handle the mount for it). This patch adds functionality that is already possible via at least two other means that I know about, both of which involve ptrace(): first, one could ptrace attach, and then iterate through syscalls via PTRACE_SYSCALL. Unfortunately this is slow, so a faster version would be to install a filter that does SECCOMP_RET_TRACE, which triggers a PTRACE_EVENT_SECCOMP. Since ptrace allows only one tracer, if the container runtime is that tracer, users inside the container (or outside) trying to debug it will not be able to use ptrace, which is annoying. It also means that older distributions based on Upstart cannot boot inside containers using ptrace, since upstart itself uses ptrace to monitor services while starting. The actual implementation of this is fairly small, although getting the synchronization right was/is slightly complex. Finally, it's worth noting that the classic seccomp TOCTOU of reading memory data from the task still applies here, but can be avoided with careful design of the userspace handler: if the userspace handler reads all of the task memory that is necessary before applying its security policy, the tracee's subsequent memory edits will not be read by the tracer. Signed-off-by: Tycho Andersen <tycho@tycho.ws> CC: Kees Cook <keescook@chromium.org> CC: Andy Lutomirski <luto@amacapital.net> CC: Oleg Nesterov <oleg@redhat.com> CC: Eric W. Biederman <ebiederm@xmission.com> CC: "Serge E. Hallyn" <serge@hallyn.com> Acked-by: Serge Hallyn <serge@hallyn.com> CC: Christian Brauner <christian@brauner.io> CC: Tyler Hicks <tyhicks@canonical.com> CC: Akihiro Suda <suda.akihiro@lab.ntt.co.jp> Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent a5662e4 commit 6a21cc5

6 files changed

Lines changed: 1017 additions & 10 deletions

File tree

‎Documentation/ioctl/ioctl-number.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Code Seq#(hex) Include File Comments
7979
0x1b all InfiniBand Subsystem <http://infiniband.sourceforge.net/>
8080
0x20 all drivers/cdrom/cm206.h
8181
0x22 all scsi/sg.h
82+
'!' 00-1F uapi/linux/seccomp.h
8283
'#' 00-3F IEEE 1394 Subsystem Block for the entire subsystem
8384
'$' 00-0F linux/perf_counter.h, linux/perf_event.h
8485
'%' 00-0F include/uapi/linux/stm.h

‎Documentation/userspace-api/seccomp_filter.rst‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ In precedence order, they are:
122122
Results in the lower 16-bits of the return value being passed
123123
to userland as the errno without executing the system call.
124124

125+
``SECCOMP_RET_USER_NOTIF``:
126+
Results in a ``struct seccomp_notif`` message sent on the userspace
127+
notification fd, if it is attached, or ``-ENOSYS`` if it is not. See below
128+
on discussion of how to handle user notifications.
129+
125130
``SECCOMP_RET_TRACE``:
126131
When returned, this value will cause the kernel to attempt to
127132
notify a ``ptrace()``-based tracer prior to executing the system
@@ -183,6 +188,85 @@ The ``samples/seccomp/`` directory contains both an x86-specific example
183188
and a more generic example of a higher level macro interface for BPF
184189
program generation.
185190

191+
Userspace Notification
192+
======================
193+
194+
The ``SECCOMP_RET_USER_NOTIF`` return code lets seccomp filters pass a
195+
particular syscall to userspace to be handled. This may be useful for
196+
applications like container managers, which wish to intercept particular
197+
syscalls (``mount()``, ``finit_module()``, etc.) and change their behavior.
198+
199+
To acquire a notification FD, use the ``SECCOMP_FILTER_FLAG_NEW_LISTENER``
200+
argument to the ``seccomp()`` syscall:
201+
202+
.. code-block:: c
203+
204+
fd = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog);
205+
206+
which (on success) will return a listener fd for the filter, which can then be
207+
passed around via ``SCM_RIGHTS`` or similar. Note that filter fds correspond to
208+
a particular filter, and not a particular task. So if this task then forks,
209+
notifications from both tasks will appear on the same filter fd. Reads and
210+
writes to/from a filter fd are also synchronized, so a filter fd can safely
211+
have many readers.
212+
213+
The interface for a seccomp notification fd consists of two structures:
214+
215+
.. code-block:: c
216+
217+
struct seccomp_notif_sizes {
218+
__u16 seccomp_notif;
219+
__u16 seccomp_notif_resp;
220+
__u16 seccomp_data;
221+
};
222+
223+
struct seccomp_notif {
224+
__u64 id;
225+
__u32 pid;
226+
__u32 flags;
227+
struct seccomp_data data;
228+
};
229+
230+
struct seccomp_notif_resp {
231+
__u64 id;
232+
__s64 val;
233+
__s32 error;
234+
__u32 flags;
235+
};
236+
237+
The ``struct seccomp_notif_sizes`` structure can be used to determine the size
238+
of the various structures used in seccomp notifications. The size of ``struct
239+
seccomp_data`` may change in the future, so code should use:
240+
241+
.. code-block:: c
242+
243+
struct seccomp_notif_sizes sizes;
244+
seccomp(SECCOMP_GET_NOTIF_SIZES, 0, &sizes);
245+
246+
to determine the size of the various structures to allocate. See
247+
samples/seccomp/user-trap.c for an example.
248+
249+
Users can read via ``ioctl(SECCOMP_IOCTL_NOTIF_RECV)`` (or ``poll()``) on a
250+
seccomp notification fd to receive a ``struct seccomp_notif``, which contains
251+
five members: the input length of the structure, a unique-per-filter ``id``,
252+
the ``pid`` of the task which triggered this request (which may be 0 if the
253+
task is in a pid ns not visible from the listener's pid namespace), a ``flags``
254+
member which for now only has ``SECCOMP_NOTIF_FLAG_SIGNALED``, representing
255+
whether or not the notification is a result of a non-fatal signal, and the
256+
``data`` passed to seccomp. Userspace can then make a decision based on this
257+
information about what to do, and ``ioctl(SECCOMP_IOCTL_NOTIF_SEND)`` a
258+
response, indicating what should be returned to userspace. The ``id`` member of
259+
``struct seccomp_notif_resp`` should be the same ``id`` as in ``struct
260+
seccomp_notif``.
261+
262+
It is worth noting that ``struct seccomp_data`` contains the values of register
263+
arguments to the syscall, but does not contain pointers to memory. The task's
264+
memory is accessible to suitably privileged traces via ``ptrace()`` or
265+
``/proc/pid/mem``. However, care should be taken to avoid the TOCTOU mentioned
266+
above in this document: all arguments being read from the tracee's memory
267+
should be read into the tracer's memory before any policy decisions are made.
268+
This allows for an atomic decision on syscall arguments.
269+
186270
Sysctls
187271
=======
188272

‎include/linux/seccomp.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#include <uapi/linux/seccomp.h>
66

7-
#define SECCOMP_FILTER_FLAG_MASK (SECCOMP_FILTER_FLAG_TSYNC | \
8-
SECCOMP_FILTER_FLAG_LOG | \
9-
SECCOMP_FILTER_FLAG_SPEC_ALLOW)
7+
#define SECCOMP_FILTER_FLAG_MASK (SECCOMP_FILTER_FLAG_TSYNC | \
8+
SECCOMP_FILTER_FLAG_LOG | \
9+
SECCOMP_FILTER_FLAG_SPEC_ALLOW | \
10+
SECCOMP_FILTER_FLAG_NEW_LISTENER)
1011

1112
#ifdef CONFIG_SECCOMP
1213

‎include/uapi/linux/seccomp.h‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
#define SECCOMP_SET_MODE_STRICT 0
1616
#define SECCOMP_SET_MODE_FILTER 1
1717
#define SECCOMP_GET_ACTION_AVAIL 2
18+
#define SECCOMP_GET_NOTIF_SIZES 3
1819

1920
/* Valid flags for SECCOMP_SET_MODE_FILTER */
20-
#define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
21-
#define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
22-
#define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
21+
#define SECCOMP_FILTER_FLAG_TSYNC (1UL << 0)
22+
#define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
23+
#define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
24+
#define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3)
2325

2426
/*
2527
* All BPF programs must return a 32-bit value.
@@ -35,6 +37,7 @@
3537
#define SECCOMP_RET_KILL SECCOMP_RET_KILL_THREAD
3638
#define SECCOMP_RET_TRAP 0x00030000U /* disallow and force a SIGSYS */
3739
#define SECCOMP_RET_ERRNO 0x00050000U /* returns an errno */
40+
#define SECCOMP_RET_USER_NOTIF 0x7fc00000U /* notifies userspace */
3841
#define SECCOMP_RET_TRACE 0x7ff00000U /* pass to a tracer or disallow */
3942
#define SECCOMP_RET_LOG 0x7ffc0000U /* allow after logging */
4043
#define SECCOMP_RET_ALLOW 0x7fff0000U /* allow */
@@ -60,4 +63,35 @@ struct seccomp_data {
6063
__u64 args[6];
6164
};
6265

66+
struct seccomp_notif_sizes {
67+
__u16 seccomp_notif;
68+
__u16 seccomp_notif_resp;
69+
__u16 seccomp_data;
70+
};
71+
72+
struct seccomp_notif {
73+
__u64 id;
74+
__u32 pid;
75+
__u32 flags;
76+
struct seccomp_data data;
77+
};
78+
79+
struct seccomp_notif_resp {
80+
__u64 id;
81+
__s64 val;
82+
__s32 error;
83+
__u32 flags;
84+
};
85+
86+
#define SECCOMP_IOC_MAGIC '!'
87+
#define SECCOMP_IO(nr) _IO(SECCOMP_IOC_MAGIC, nr)
88+
#define SECCOMP_IOR(nr, type) _IOR(SECCOMP_IOC_MAGIC, nr, type)
89+
#define SECCOMP_IOW(nr, type) _IOW(SECCOMP_IOC_MAGIC, nr, type)
90+
#define SECCOMP_IOWR(nr, type) _IOWR(SECCOMP_IOC_MAGIC, nr, type)
91+
92+
/* Flags for seccomp notification fd ioctl. */
93+
#define SECCOMP_IOCTL_NOTIF_RECV SECCOMP_IOWR(0, struct seccomp_notif)
94+
#define SECCOMP_IOCTL_NOTIF_SEND SECCOMP_IOWR(1, \
95+
struct seccomp_notif_resp)
96+
#define SECCOMP_IOCTL_NOTIF_ID_VALID SECCOMP_IOR(2, __u64)
6397
#endif /* _UAPI_LINUX_SECCOMP_H */

0 commit comments

Comments
 (0)