Skip to contentSkip to conversation

seccomp trap to userspace

PATCH Superseded Tycho Andersen 7 participants 31 messages Jun 21, 2018 – Aug 11, 2018 1 message not in the archive

Subsystems (6)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v4 cover seccomp trap to userspace

Hi all,

Here's v4 of the seccomp trap to userspace series. v3 is here:
https://lkml.org/lkml/2018/5/31/527

I believe we've addressed the two burning questions I had about v3: 1. it seems ok not to use netlink, since there's not a great way to re-use the API without a lot of unnecessary code and 2. only having return capability for fds seems fine with people. Or at least I haven't heard any strong objections.

I've re-worked a bunch of things in this version based on feedback from the last series. See patch notes for details. At this point I'm not aware of anything that needs to be addressed, but of course that is subject to change :)

Tycho

Tycho Andersen (4):
seccomp: add a return code to trap to userspace
seccomp: make get_nth_filter available outside of CHECKPOINT_RESTORE
seccomp: add a way to get a listener fd from ptrace
seccomp: add support for passing fds via USER_NOTIF

 .../userspace-api/seccomp_filter.rst          |  79 +++
 arch/Kconfig                                  |   7 +
 include/linux/seccomp.h                       |  18 +-
 include/uapi/linux/ptrace.h                   |   2 +
 include/uapi/linux/seccomp.h                  |  23 +-
 kernel/ptrace.c                               |   4 +
 kernel/seccomp.c                              | 491 ++++++++++++++-
 tools/testing/selftests/seccomp/seccomp_bpf.c | 560 +++++++++++++++++-
 8 files changed, 1172 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 1/4 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 mknod(), since this checks capable(CAP_SYS_ADMIN). However, harmless devices like /dev/null or /dev/zero should be ok for containers to mknod, but we'd like to avoid hard coding some whitelist in the kernel. Another example is mount(), which has many security restrictions for good reason, but configuration or runtime knowledge could potentially be used to relax these restrictions.

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 start services.

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.

v2: * make id a u64; the idea here being that it will never overflow,
because 64 is huge (one syscall every nanosecond => wrap every 584 years) (Andy)
* prevent nesting of user notifications: if someone is already attached the tree in one place, nobody else can attach to the tree (Andy)
* notify the listener of signals the tracee receives as well (Andy)
* implement poll
v3: * lockdep fix (Oleg)
* drop unnecessary WARN()s (Christian)
* rearrange error returns to be more rpetty (Christian)
* fix build in !CONFIG_SECCOMP_USER_NOTIFICATION case
v4: * fix implementation of poll to use poll_wait() (Jann)
* change listener's fd flags to be 0 (Jann)
* hoist filter initialization out of ifdefs to its own function init_user_notification()
* add some more testing around poll() and closing the listener while a syscall is in action
* s/GET_LISTENER/NEW_LISTENER, since you can't _get_ a listener, but it creates a new one (Matthew)
* correctly handle pid namespaces, add some testcases (Matthew)
* use EINPROGRESS instead of EINVAL when a notification response is written twice (Matthew)
* fix comment typo from older version (SEND vs READ) (Matthew)
* whitespace and logic simplification (Tobin)
* add some Documentation/ bits on userspace trapping

CC Kees Cook <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Eric W. Biederman <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Christian Brauner <[email protected]>
CC Tyler Hicks <[email protected]>
CC Akihiro Suda <[email protected]>
---
 .../userspace-api/seccomp_filter.rst          |  68 +++
 arch/Kconfig                                  |   7 +
 include/linux/seccomp.h                       |   7 +-
 include/uapi/linux/seccomp.h                  |  20 +-
 kernel/seccomp.c                              | 410 +++++++++++++++++-
 tools/testing/selftests/seccomp/seccomp_bpf.c | 380 +++++++++++++++-
 6 files changed, 882 insertions(+), 10 deletions(-)
Documentation/userspace-api/seccomp_filter.rst +68 -0
index 82a468bc7560..e51422559dd6 100644
@@ -122,6 +122,11 @@ In precedence order, they are:
 	Results in the lower 16-bits of the return value being passed
 	to userland as the errno without executing the system call.
 
+``SECCOMP_RET_USER_NOTIF``:
+    Results in a ``struct seccomp_notif`` message sent on the userspace
+    notification fd, if it is attached, or ``-ENOSYS`` if it is not. See below
+    on discussion of how to handle user notifications.
+
 ``SECCOMP_RET_TRACE``:
 	When returned, this value will cause the kernel to attempt to
 	notify a ``ptrace()``-based tracer prior to executing the system
@@ -183,6 +188,69 @@ The ``samples/seccomp/`` directory contains both an x86-specific example
 and a more generic example of a higher level macro interface for BPF
 program generation.
 
+Userspace Notification
+======================
+
+The ``SECCOMP_RET_USER_NOTIF`` return code lets seccomp filters pass a
+particular syscall to userspace to be handled. This may be useful for
+applications like container managers, which whish to intercept particular
+syscalls (``mount()``, ``finit_module()``, etc.) and change their behavior.
+
+There are currently two APIs to acquire a userspace notification fd for a
+particular filter. The first is when the filter is installed, the task
+installing the filter can ask the ``seccomp()`` syscall:
+
+.. code-block::
+
+    fd = seccomp(SECCOMP_SET_MODE_FILTER, SECCOMP_FILTER_FLAG_NEW_LISTENER, &prog);
+
+which (on success) will return a listener fd for the filter, which can them be
+passed around via ``SCM_RIGHTS`` or similar. Alternativley, a filter fd can be
+acquired via:
+
+.. code-block::
+
+    fd = ptrace(PTRACE_SECCOMP_NEW_LISTENER, pid, 0);
+
+which grabs the 0th filter for some task which the tracer has privilege over.
+Note that filter fds correspond to a particular filter, and not a particular
+task. So if this task then forks, notifications from both tasks will appear on
+the same filter fd. Reads and writes to/from a filter fd are also synchronized,
+so a filter fd can safely have many readers.
+
+The interface for a seccomp notification fd consists of two structures:
+
+.. code-block::
+
+    struct seccomp_notif {
+        __u64 id;
+        pid_t pid;
+        struct seccomp_data data;
+    };
+
+    struct seccomp_notif_resp {
+        __u64 id;
+        __s32 error;
+        __s64 val;
+    };
+
+Users can ``read()`` or ``poll()`` on a seccomp notification fd to receive a
+``struct seccomp_notif``, which contains three members: a globally unique
+``id``, the ``pid`` of the task which triggered this request (which may be 0 if
+the task is in a pid ns not visible from the listener's pid namespace), and the
+``data`` passed to seccomp. Userspace can then make a decision based on this
+information about what to do, and ``write()`` a response, indicating what
+should be returned to userspace. The ``id`` member of ``struct
+seccomp_notif_resp`` should be the same ``id`` as in ``struct seccomp_notif``.
+
+It is worth noting that ``struct seccomp_data`` contains the values of register
+arguments to the syscall, but does not contain pointers to memory. The task's
+memory is accessiable to suitably privileged traces via via ``ptrace()`` or
+``/proc/pid/map_files/``. However, care should be taken to avoid the TOCTOU
+mentioned above in this document: all arguments being read from the tracee's
+memory should be read into the tracer's memory before any policy decisions are
+made. This allows for an atomic decision on syscall arguments.
+
 Sysctls
 =======
 
arch/Kconfig +7 -0
index 1aa59063f1fd..2f0c97bf4bc8 100644
@@ -414,6 +414,13 @@ config PLUGIN_HOSTCC
 	  Host compiler used to build GCC plugins.  This can be $(HOSTCXX),
 	  $(HOSTCC), or a null string if GCC plugin is unsupported.
 
+config SECCOMP_USER_NOTIFICATION
+	bool "Enable the SECCOMP_RET_USER_NOTIF seccomp action"
+	depends on SECCOMP_FILTER
+	help
+	  Enable SECCOMP_RET_USER_NOTIF, a return code which can be used by seccomp
+	  programs to notify a userspace listener that a particular event happened.
+
 config HAVE_GCC_PLUGINS
 	bool
 	help
include/linux/seccomp.h +4 -3
index e5320f6c8654..017444b5efed 100644
@@ -4,9 +4,10 @@
 
 #include <uapi/linux/seccomp.h>
 
-#define SECCOMP_FILTER_FLAG_MASK	(SECCOMP_FILTER_FLAG_TSYNC	| \
-					 SECCOMP_FILTER_FLAG_LOG	| \
-					 SECCOMP_FILTER_FLAG_SPEC_ALLOW)
+#define SECCOMP_FILTER_FLAG_MASK	(SECCOMP_FILTER_FLAG_TSYNC | \
+					 SECCOMP_FILTER_FLAG_LOG | \
+					 SECCOMP_FILTER_FLAG_SPEC_ALLOW | \
+					 SECCOMP_FILTER_FLAG_NEW_LISTENER)
 
 #ifdef CONFIG_SECCOMP
 
include/uapi/linux/seccomp.h +17 -3
index 9efc0e73d50b..8836a3b25500 100644
@@ -17,9 +17,10 @@
 #define SECCOMP_GET_ACTION_AVAIL	2
 
 /* Valid flags for SECCOMP_SET_MODE_FILTER */
-#define SECCOMP_FILTER_FLAG_TSYNC	(1UL << 0)
-#define SECCOMP_FILTER_FLAG_LOG		(1UL << 1)
-#define SECCOMP_FILTER_FLAG_SPEC_ALLOW	(1UL << 2)
+#define SECCOMP_FILTER_FLAG_TSYNC		(1UL << 0)
+#define SECCOMP_FILTER_FLAG_LOG			(1UL << 1)
+#define SECCOMP_FILTER_FLAG_SPEC_ALLOW		(1UL << 2)
+#define SECCOMP_FILTER_FLAG_NEW_LISTENER	(1UL << 3)
 
 /*
  * All BPF programs must return a 32-bit value.
@@ -35,6 +36,7 @@
 #define SECCOMP_RET_KILL	 SECCOMP_RET_KILL_THREAD
 #define SECCOMP_RET_TRAP	 0x00030000U /* disallow and force a SIGSYS */
 #define SECCOMP_RET_ERRNO	 0x00050000U /* returns an errno */
+#define SECCOMP_RET_USER_NOTIF   0x7fc00000U /* notifies userspace */
 #define SECCOMP_RET_TRACE	 0x7ff00000U /* pass to a tracer or disallow */
 #define SECCOMP_RET_LOG		 0x7ffc0000U /* allow after logging */
 #define SECCOMP_RET_ALLOW	 0x7fff0000U /* allow */
@@ -60,4 +62,16 @@ struct seccomp_data {
 	__u64 args[6];
 };
 
+struct seccomp_notif {
+	__u64 id;
+	pid_t pid;
+	struct seccomp_data data;
+};
+
+struct seccomp_notif_resp {
+	__u64 id;
+	__s32 error;
+	__s64 val;
+};
+
 #endif /* _UAPI_LINUX_SECCOMP_H */
kernel/seccomp.c +408 -2

527 lines not on this page · raw message

tools/testing/selftests/seccomp/seccomp_bpf.c +378 -2

413 lines not on this page · raw message

Signature · 1 line
2.17.1
#2 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 3 lines · Tycho Andersen

This patch introduces a means for syscalls matched in seccomp to notify some other task that a particular filter has been triggered.

[...]

Documentation/userspace-api/seccomp_filter.rst The ``samples/seccomp/`` directory contains both an x86-specific example · line 196

Quoted 6 lines
Documentation/userspace-api/seccomp_filter.rst +6 -0
+Userspace Notification
+======================
+
+The ``SECCOMP_RET_USER_NOTIF`` return code lets seccomp filters pass a
+particular syscall to userspace to be handled. This may be useful for
+applications like container managers, which whish to intercept particular

typo: "wish"

[...]

Quoted 1 line

+passed around via ``SCM_RIGHTS`` or similar. Alternativley, a filter fd can be

typo: "Alternatively"

[...]

Documentation/userspace-api/seccomp_filter.rst The ``samples/seccomp/`` directory contains both an x86-specific example · line 248

Quoted 3 lines
Documentation/userspace-api/seccomp_filter.rst +3 -0
+It is worth noting that ``struct seccomp_data`` contains the values of register
+arguments to the syscall, but does not contain pointers to memory. The task's
+memory is accessiable to suitably privileged traces via via ``ptrace()`` or

Typo: "accessible"

[...]

Quoted 35 lines
kernel/seccomp.c +35 -0
+
+static void seccomp_do_user_notification(int this_syscall,
+                                        struct seccomp_filter *match,
+                                        const struct seccomp_data *sd)
+{
+       int err;
+       long ret = 0;
+       struct seccomp_knotif n = {};
+
+       mutex_lock(&match->notify_lock);
+       err = -ENOSYS;
+       if (!match->has_listener)
+               goto out;
+
+       n.pid = task_pid(current);
+       n.state = SECCOMP_NOTIFY_INIT;
+       n.data = sd;
+       n.id = seccomp_next_notify_id(match);
+       init_completion(&n.ready);
+
+       list_add(&n.list, &match->notifications);
+       wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
+
+       mutex_unlock(&match->notify_lock);
+       up(&match->request);
+
+       err = wait_for_completion_interruptible(&n.ready);
+       mutex_lock(&match->notify_lock);
+
+       /*
+        * Here it's possible we got a signal and then had to wait on the mutex
+        * while the reply was sent, so let's be sure there wasn't a response
+        * in the meantime.
+        */
+       if (err < 0 && n.state != SECCOMP_NOTIFY_REPLIED) {

kernel/seccomp.c void secure_computing_strict(int this_syscall) · line 793

Quoted 10 lines
kernel/seccomp.c +10 -0
+               /*
+                * We got a signal. Let's tell userspace about it (potentially
+                * again, if we had already notified them about the first one).
+                */
+               if (n.state == SECCOMP_NOTIFY_SENT) {
+                       n.state = SECCOMP_NOTIFY_INIT;
+                       up(&match->request);
+               }
+               mutex_unlock(&match->notify_lock);
+               err = wait_for_completion_killable(&n.ready);

Does this mean that when you get a signal that isn't SIGKILL, wait_for_completion_interruptible() will bail out with -ERESTARTSYS, but then you hang on this wait_for_completion_killable()? I don't understand what's going on here. What's the point of using wait_for_completion_interruptible() when you'll just hang on another wait on the same "struct completion"?

Quoted 15 lines
kernel/seccomp.c +15 -0
+               mutex_lock(&match->notify_lock);
+               if (err < 0)
+                       goto remove_list;
+       }
+
+       ret = n.val;
+       err = n.error;
+
+remove_list:
+       list_del(&n.list);
+out:
+       mutex_unlock(&match->notify_lock);
+       syscall_set_return_value(current, task_pt_regs(current),
+                                err, ret);
+}
#3 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 17 lines · Jann Horn
Quoted 3 lines · Tycho Andersen

This patch introduces a means for syscalls matched in seccomp to notify some other task that a particular filter has been triggered.

[...]

Documentation/userspace-api/seccomp_filter.rst The ``samples/seccomp/`` directory contains both an x86-specific example · line 196

Quoted 6 lines
Documentation/userspace-api/seccomp_filter.rst +6 -0
+Userspace Notification
+======================
+
+The ``SECCOMP_RET_USER_NOTIF`` return code lets seccomp filters pass a
+particular syscall to userspace to be handled. This may be useful for
+applications like container managers, which whish to intercept particular

typo: "wish"

[...]

Quoted 1 line

+passed around via ``SCM_RIGHTS`` or similar. Alternativley, a filter fd can be

Quoted 8 lines

typo: "Alternatively"

[...]

Documentation/userspace-api/seccomp_filter.rst The ``samples/seccomp/`` directory contains both an x86-specific example · line 248

Quoted 3 lines
Documentation/userspace-api/seccomp_filter.rst +3 -0
+It is worth noting that ``struct seccomp_data`` contains the values of register
+arguments to the syscall, but does not contain pointers to memory. The task's
+memory is accessiable to suitably privileged traces via via ``ptrace()`` or

Typo: "accessible"

Thanks!

Quoted 45 lines

[...]

Quoted 44 lines
kernel/seccomp.c +44 -0
+
+static void seccomp_do_user_notification(int this_syscall,
+                                        struct seccomp_filter *match,
+                                        const struct seccomp_data *sd)
+{
+       int err;
+       long ret = 0;
+       struct seccomp_knotif n = {};
+
+       mutex_lock(&match->notify_lock);
+       err = -ENOSYS;
+       if (!match->has_listener)
+               goto out;
+
+       n.pid = task_pid(current);
+       n.state = SECCOMP_NOTIFY_INIT;
+       n.data = sd;
+       n.id = seccomp_next_notify_id(match);
+       init_completion(&n.ready);
+
+       list_add(&n.list, &match->notifications);
+       wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
+
+       mutex_unlock(&match->notify_lock);
+       up(&match->request);
+
+       err = wait_for_completion_interruptible(&n.ready);
+       mutex_lock(&match->notify_lock);
+
+       /*
+        * Here it's possible we got a signal and then had to wait on the mutex
+        * while the reply was sent, so let's be sure there wasn't a response
+        * in the meantime.
+        */
+       if (err < 0 && n.state != SECCOMP_NOTIFY_REPLIED) {
+               /*
+                * We got a signal. Let's tell userspace about it (potentially
+                * again, if we had already notified them about the first one).
+                */
+               if (n.state == SECCOMP_NOTIFY_SENT) {
+                       n.state = SECCOMP_NOTIFY_INIT;
+                       up(&match->request);
+               }
+               mutex_unlock(&match->notify_lock);
Quoted 8 lines
Quoted 1 line

+ err = wait_for_completion_killable(&n.ready);

Does this mean that when you get a signal that isn't SIGKILL, wait_for_completion_interruptible() will bail out with -ERESTARTSYS, but then you hang on this wait_for_completion_killable()? I don't understand what's going on here. What's the point of using wait_for_completion_interruptible() when you'll just hang on another wait on the same "struct completion"?

This is the implementation of this suggestion by Andy:
https://lkml.org/lkml/2018/3/15/1122

The idea is to alert the listener that there was a signal exactly once, in case it's in the middle of processing a request it could bail out and do something else. So the killable wait is intended to ignore other (non-fatal) signals after the first one and wait for whatever the handler decides to do with the signal it received.

Tycho

#4 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 84 lines · Tycho Andersen
Quoted 17 lines · Jann Horn
Quoted 3 lines · Tycho Andersen

This patch introduces a means for syscalls matched in seccomp to notify some other task that a particular filter has been triggered.

[...]

Documentation/userspace-api/seccomp_filter.rst The ``samples/seccomp/`` directory contains both an x86-specific example · line 196

Quoted 6 lines
Documentation/userspace-api/seccomp_filter.rst +6 -0
+Userspace Notification
+======================
+
+The ``SECCOMP_RET_USER_NOTIF`` return code lets seccomp filters pass a
+particular syscall to userspace to be handled. This may be useful for
+applications like container managers, which whish to intercept particular

typo: "wish"

[...]

Quoted 1 line

+passed around via ``SCM_RIGHTS`` or similar. Alternativley, a filter fd can be

Quoted 8 lines

typo: "Alternatively"

[...]

Documentation/userspace-api/seccomp_filter.rst The ``samples/seccomp/`` directory contains both an x86-specific example · line 248

Quoted 3 lines
Documentation/userspace-api/seccomp_filter.rst +3 -0
+It is worth noting that ``struct seccomp_data`` contains the values of register
+arguments to the syscall, but does not contain pointers to memory. The task's
+memory is accessiable to suitably privileged traces via via ``ptrace()`` or

Typo: "accessible"

Thanks!

Quoted 45 lines

[...]

Quoted 44 lines
kernel/seccomp.c +44 -0
+
+static void seccomp_do_user_notification(int this_syscall,
+                                        struct seccomp_filter *match,
+                                        const struct seccomp_data *sd)
+{
+       int err;
+       long ret = 0;
+       struct seccomp_knotif n = {};
+
+       mutex_lock(&match->notify_lock);
+       err = -ENOSYS;
+       if (!match->has_listener)
+               goto out;
+
+       n.pid = task_pid(current);
+       n.state = SECCOMP_NOTIFY_INIT;
+       n.data = sd;
+       n.id = seccomp_next_notify_id(match);
+       init_completion(&n.ready);
+
+       list_add(&n.list, &match->notifications);
+       wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
+
+       mutex_unlock(&match->notify_lock);
+       up(&match->request);
+
+       err = wait_for_completion_interruptible(&n.ready);
+       mutex_lock(&match->notify_lock);
+
+       /*
+        * Here it's possible we got a signal and then had to wait on the mutex
+        * while the reply was sent, so let's be sure there wasn't a response
+        * in the meantime.
+        */
+       if (err < 0 && n.state != SECCOMP_NOTIFY_REPLIED) {
+               /*
+                * We got a signal. Let's tell userspace about it (potentially
+                * again, if we had already notified them about the first one).
+                */
+               if (n.state == SECCOMP_NOTIFY_SENT) {
+                       n.state = SECCOMP_NOTIFY_INIT;
+                       up(&match->request);
+               }
+               mutex_unlock(&match->notify_lock);
Quoted 8 lines
Quoted 1 line

+ err = wait_for_completion_killable(&n.ready);

Does this mean that when you get a signal that isn't SIGKILL, wait_for_completion_interruptible() will bail out with -ERESTARTSYS, but then you hang on this wait_for_completion_killable()? I don't understand what's going on here. What's the point of using wait_for_completion_interruptible() when you'll just hang on another wait on the same "struct completion"?

Quoted 8 lines

This is the implementation of this suggestion by Andy:
https://lkml.org/lkml/2018/3/15/1122

The idea is to alert the listener that there was a signal exactly once, in case it's in the middle of processing a request it could bail out and do something else. So the killable wait is intended to ignore other (non-fatal) signals after the first one and wait for whatever the handler decides to do with the signal it received.

How can the listener tell that a signal arrived? When the first non-fatal signal comes in, you just set the state to SECCOMP_NOTIFY_INIT if it was SECCOMP_NOTIFY_SENT, right? So the listener will potentially see the request twice, but with no additional indicator that a signal arrived? And in particular, if the listener doesn't read the request before the signal arrives, it will only see the request once, just as if it was a normal request with no signals involved?

Would it perhaps make sense to add a field to struct seccomp_notif that indicates whether the notification is for a normal syscall or a canceled syscall?

#5 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 67 lines · Jann Horn
Quoted 57 lines · Tycho Andersen
Quoted 46 lines · Jann Horn

On Fri, Jun 22, 2018 at 12:05 AM Tycho Andersen <[email protected]> wrote:
[...]

Quoted 44 lines
kernel/seccomp.c +44 -0
+
+static void seccomp_do_user_notification(int this_syscall,
+                                        struct seccomp_filter *match,
+                                        const struct seccomp_data *sd)
+{
+       int err;
+       long ret = 0;
+       struct seccomp_knotif n = {};
+
+       mutex_lock(&match->notify_lock);
+       err = -ENOSYS;
+       if (!match->has_listener)
+               goto out;
+
+       n.pid = task_pid(current);
+       n.state = SECCOMP_NOTIFY_INIT;
+       n.data = sd;
+       n.id = seccomp_next_notify_id(match);
+       init_completion(&n.ready);
+
+       list_add(&n.list, &match->notifications);
+       wake_up_poll(&match->wqh, EPOLLIN | EPOLLRDNORM);
+
+       mutex_unlock(&match->notify_lock);
+       up(&match->request);
+
+       err = wait_for_completion_interruptible(&n.ready);
+       mutex_lock(&match->notify_lock);
+
+       /*
+        * Here it's possible we got a signal and then had to wait on the mutex
+        * while the reply was sent, so let's be sure there wasn't a response
+        * in the meantime.
+        */
+       if (err < 0 && n.state != SECCOMP_NOTIFY_REPLIED) {
+               /*
+                * We got a signal. Let's tell userspace about it (potentially
+                * again, if we had already notified them about the first one).
+                */
+               if (n.state == SECCOMP_NOTIFY_SENT) {
+                       n.state = SECCOMP_NOTIFY_INIT;
+                       up(&match->request);
+               }
+               mutex_unlock(&match->notify_lock);
Quoted 8 lines
Quoted 1 line

+ err = wait_for_completion_killable(&n.ready);

Does this mean that when you get a signal that isn't SIGKILL, wait_for_completion_interruptible() will bail out with -ERESTARTSYS, but then you hang on this wait_for_completion_killable()? I don't understand what's going on here. What's the point of using wait_for_completion_interruptible() when you'll just hang on another wait on the same "struct completion"?

Quoted 8 lines

This is the implementation of this suggestion by Andy:
https://lkml.org/lkml/2018/3/15/1122

The idea is to alert the listener that there was a signal exactly once, in case it's in the middle of processing a request it could bail out and do something else. So the killable wait is intended to ignore other (non-fatal) signals after the first one and wait for whatever the handler decides to do with the signal it received.

Quoted 8 lines

How can the listener tell that a signal arrived? When the first non-fatal signal comes in, you just set the state to SECCOMP_NOTIFY_INIT if it was SECCOMP_NOTIFY_SENT, right? So the listener will potentially see the request twice, but with no additional indicator that a signal arrived? And in particular, if the listener doesn't read the request before the signal arrives, it will only see the request once, just as if it was a normal request with no signals involved?

I was thinking just parsing /proc/pid/status (given that people are already going to be mapping things in /proc/pid/map_files to read arguments and stuff, I didn't think too much of it),

Quoted 3 lines

Would it perhaps make sense to add a field to struct seccomp_notif that indicates whether the notification is for a normal syscall or a canceled syscall?

Sure, I'll add a __u32 signal and set it to the value of the signal if we got one.

Thanks!

Tycho

#6 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

In reply to Tycho Andersen
Quoted 27 lines · Tycho Andersen

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 mknod(), since this checks capable(CAP_SYS_ADMIN). However, harmless devices like /dev/null or /dev/zero should be ok for containers to mknod, but we'd like to avoid hard coding some whitelist in the kernel. Another example is mount(), which has many security restrictions for good reason, but configuration or runtime knowledge could potentially be used to relax these restrictions.

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 start services.

Quoted 8 lines

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.

I've been thinking about how one would actually write userspace code that uses this API, and whether PID reuse is an issue here. As far as I can tell, the following situation can happen:

- seccomped process tries to perform a syscall that gets trapped
- notification is sent to the supervisor
- supervisor reads the notification
- seccomped process gets SIGKILLed
- new process appears with the PID that the seccomped process had
- supervisor tries to access memory of the seccomped process via
process_vm_{read,write}v or /proc/$pid/mem
- supervisor unintentionally accesses memory of the new process instead

This could have particularly nasty consequences if the supervisor has to write to memory of the seccomped process for some reason. It might make sense to explicitly document how the API has to be used to avoid such a scenario from occuring. AFAICS, process_vm_{read,write}v are fundamentally unsafe for this; /proc/$pid/mem might be safe if you do the following dance in the supervisor to validate that you have a reference to the right struct mm before starting to actually access memory:

- supervisor reads a syscall notification for the seccomped process with PID $A
- supervisor opens /proc/$A/mem [taking a reference on the mm of the
process that currently has PID $A]
- supervisor reads all pending events from the notification FD; if
one of them says that PID $A was signalled, send back -ERESTARTSYS (or -ERESTARTNOINTR?) and bail out
- [at this point, the open FD to /proc/$A/mem is known to actually
refer to the mm struct of the seccomped process]
- read and write on the open FD to /proc/$A/mem as necessary
- send back the syscall result

It might be nice if the kernel was able to directly give the supervisor an FD to /proc/$A/mem that is guaranteed to point to the right struct mm, but trying to implement that would probably make this patch set significantly larger?

#7 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Hi Jann,

Quoted 59 lines · Jann Horn
Quoted 27 lines · Tycho Andersen

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 mknod(), since this checks capable(CAP_SYS_ADMIN). However, harmless devices like /dev/null or /dev/zero should be ok for containers to mknod, but we'd like to avoid hard coding some whitelist in the kernel. Another example is mount(), which has many security restrictions for good reason, but configuration or runtime knowledge could potentially be used to relax these restrictions.

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 start services.

Quoted 8 lines

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.

I've been thinking about how one would actually write userspace code that uses this API, and whether PID reuse is an issue here. As far as I can tell, the following situation can happen:

- seccomped process tries to perform a syscall that gets trapped
- notification is sent to the supervisor
- supervisor reads the notification
- seccomped process gets SIGKILLed
- new process appears with the PID that the seccomped process had
- supervisor tries to access memory of the seccomped process via
process_vm_{read,write}v or /proc/$pid/mem
- supervisor unintentionally accesses memory of the new process instead

This could have particularly nasty consequences if the supervisor has to write to memory of the seccomped process for some reason. It might make sense to explicitly document how the API has to be used to avoid such a scenario from occuring. AFAICS, process_vm_{read,write}v are fundamentally unsafe for this; /proc/$pid/mem might be safe if you do the following dance in the supervisor to validate that you have a reference to the right struct mm before starting to actually access memory:

Quoted 10 lines

- supervisor reads a syscall notification for the seccomped process with PID $A
- supervisor opens /proc/$A/mem [taking a reference on the mm of the
process that currently has PID $A]
- supervisor reads all pending events from the notification FD; if
one of them says that PID $A was signalled, send back -ERESTARTSYS (or -ERESTARTNOINTR?) and bail out
- [at this point, the open FD to /proc/$A/mem is known to actually
refer to the mm struct of the seccomped process]
- read and write on the open FD to /proc/$A/mem as necessary
- send back the syscall result

Yes, this is a nasty problem :(. We have the id in the request/response structs to avoid this race, so perhaps we can re-use that? So it would look like:

- supervisor gets syscall notification for $A
- supervisor opens /proc/$A/mem or /proc/$A/map_files/... or a dir fd to the container's root or whatever
- supervisor calls seccomp(SECCOMP_NOTIFICATION_IS_VALID, req->id, listener_fd)
- supervisor knows that the fds it has open are safe

That way it doesn't have to flush the whole queue? Of course this makes things a lot slower, but it does enable safety for more than just memory accesses, and also isn't required for things which wouldn't read memory.

Quoted 4 lines

It might be nice if the kernel was able to directly give the supervisor an FD to /proc/$A/mem that is guaranteed to point to the right struct mm, but trying to implement that would probably make this patch set significantly larger?

I'll take a look and see how big it is, it doesn't *seem* like it should be that hard. Famous last words :)

Tycho

#8 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 74 lines · Tycho Andersen

Hi Jann,

Quoted 59 lines · Jann Horn
Quoted 27 lines · Tycho Andersen

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 mknod(), since this checks capable(CAP_SYS_ADMIN). However, harmless devices like /dev/null or /dev/zero should be ok for containers to mknod, but we'd like to avoid hard coding some whitelist in the kernel. Another example is mount(), which has many security restrictions for good reason, but configuration or runtime knowledge could potentially be used to relax these restrictions.

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 start services.

Quoted 8 lines

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.

I've been thinking about how one would actually write userspace code that uses this API, and whether PID reuse is an issue here. As far as I can tell, the following situation can happen:

- seccomped process tries to perform a syscall that gets trapped
- notification is sent to the supervisor
- supervisor reads the notification
- seccomped process gets SIGKILLed
- new process appears with the PID that the seccomped process had
- supervisor tries to access memory of the seccomped process via
process_vm_{read,write}v or /proc/$pid/mem
- supervisor unintentionally accesses memory of the new process instead

This could have particularly nasty consequences if the supervisor has to write to memory of the seccomped process for some reason. It might make sense to explicitly document how the API has to be used to avoid such a scenario from occuring. AFAICS, process_vm_{read,write}v are fundamentally unsafe for this; /proc/$pid/mem might be safe if you do the following dance in the supervisor to validate that you have a reference to the right struct mm before starting to actually access memory:

Quoted 10 lines

- supervisor reads a syscall notification for the seccomped process with PID $A
- supervisor opens /proc/$A/mem [taking a reference on the mm of the
process that currently has PID $A]
- supervisor reads all pending events from the notification FD; if
one of them says that PID $A was signalled, send back -ERESTARTSYS (or -ERESTARTNOINTR?) and bail out
- [at this point, the open FD to /proc/$A/mem is known to actually
refer to the mm struct of the seccomped process]
- read and write on the open FD to /proc/$A/mem as necessary
- send back the syscall result

Quoted 7 lines

Yes, this is a nasty problem :(. We have the id in the request/response structs to avoid this race, so perhaps we can re-use that? So it would look like:

- supervisor gets syscall notification for $A
- supervisor opens /proc/$A/mem or /proc/$A/map_files/... or a dir fd to the container's root or whatever

(or open a dir fd to /proc/$A; then later, you can use openat() relative to that to open whatever you need)

Quoted 7 lines

- supervisor calls seccomp(SECCOMP_NOTIFICATION_IS_VALID, req->id, listener_fd)
- supervisor knows that the fds it has open are safe

That way it doesn't have to flush the whole queue? Of course this makes things a lot slower, but it does enable safety for more than just memory accesses, and also isn't required for things which wouldn't read memory.

That sounds good to me. :)

Quoted 7 lines
Quoted 4 lines

It might be nice if the kernel was able to directly give the supervisor an FD to /proc/$A/mem that is guaranteed to point to the right struct mm, but trying to implement that would probably make this patch set significantly larger?

I'll take a look and see how big it is, it doesn't *seem* like it should be that hard. Famous last words :)

Good luck. :D

If you do manage to implement this, it might actually make sense to hand out an O_PATH FD to /proc/$A (or perhaps more accurately, /proc/$A/task/$A?) instead of an FD to /proc/*/mem. Then you could safely open whatever files you need from the process' procfs directory in a race-free manner.

I think you'd have to add some way to tell the kernel in which procfs instance you want the lookup to happen; so I think you'd need to supply an FD to the root of a procfs when opening a notification fd, and then in the read handler, you'd have to perform a lookup in procfs.

#9 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

In reply to Tycho Andersen
Quoted 92 lines · Tycho Andersen

Hi Jann,

Quoted 60 lines · Jann Horn
Quoted 28 lines · Tycho Andersen

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 mknod(), since this checks capable(CAP_SYS_ADMIN). However, harmless devices like /dev/null or /dev/zero should be ok for containers to mknod, but we'd like to avoid hard coding some whitelist in the kernel. Another example is mount(), which has many security restrictions for good reason, but configuration or runtime knowledge could potentially be used to relax these restrictions.

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 start services.

Quoted 8 lines

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.

I've been thinking about how one would actually write userspace code that uses this API, and whether PID reuse is an issue here. As far as I can tell, the following situation can happen:

- seccomped process tries to perform a syscall that gets trapped
- notification is sent to the supervisor
- supervisor reads the notification
- seccomped process gets SIGKILLed
- new process appears with the PID that the seccomped process had
- supervisor tries to access memory of the seccomped process via process_vm_{read,write}v or /proc/$pid/mem
- supervisor unintentionally accesses memory of the new process instead

This could have particularly nasty consequences if the supervisor has to write to memory of the seccomped process for some reason. It might make sense to explicitly document how the API has to be used to avoid such a scenario from occuring. AFAICS, process_vm_{read,write}v are fundamentally unsafe for this; /proc/$pid/mem might be safe if you do the following dance in the supervisor to validate that you have a reference to the right struct mm before starting to actually access memory:

Quoted 10 lines

- supervisor reads a syscall notification for the seccomped process with PID $A
- supervisor opens /proc/$A/mem [taking a reference on the mm of the process that currently has PID $A]
- supervisor reads all pending events from the notification FD; if one of them says that PID $A was signalled, send back -ERESTARTSYS (or -ERESTARTNOINTR?) and bail out
- [at this point, the open FD to /proc/$A/mem is known to actually refer to the mm struct of the seccomped process]
- read and write on the open FD to /proc/$A/mem as necessary
- send back the syscall result

Yes, this is a nasty problem :(. We have the id in the request/response structs to avoid this race, so perhaps we can re-use that? So it would look like:

- supervisor gets syscall notification for $A
- supervisor opens /proc/$A/mem or /proc/$A/map_files/... or a dir fd to the container's root or whatever
- supervisor calls seccomp(SECCOMP_NOTIFICATION_IS_VALID, req->id, listener_fd)
- supervisor knows that the fds it has open are safe

That way it doesn't have to flush the whole queue? Of course this makes things a lot slower, but it does enable safety for more than just memory accesses, and also isn't required for things which wouldn't read memory.

Quoted 7 lines
Quoted 4 lines

It might be nice if the kernel was able to directly give the supervisor an FD to /proc/$A/mem that is guaranteed to point to the right struct mm, but trying to implement that would probably make this patch set significantly larger?

I'll take a look and see how big it is, it doesn't *seem* like it should be that hard. Famous last words :)

One possible extra issue: IIRC /proc/.../mem uses FOLL_FORCE, which is not what we want here.

How about just adding an explicit “read/write the seccomp-trapped task’s memory” primitive? That should be easier than a “open mem fd” primitive.

#10 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 3 lines · Andy Lutomirski

One possible extra issue: IIRC /proc/.../mem uses FOLL_FORCE, which is not what we want here.

How about just adding an explicit “read/write the seccomp-trapped task’s memory” primitive? That should be easier than a “open mem fd” primitive.

Uuugh. Can we avoid adding another "read/write remote process memory" interface? The point of this series was to provide a lightweight approach to what should normally be possible via the existing seccomp+ptrace interface. I do like Jann's context idea, but I agree with Andy: it can't be a handle to /proc/$pid/mem, since it's FOLL_FORCE. Is there any other kind of process context id we can use for this instead of pid? There was once an idea of pid-fd but it never landed... This would let us get rid of the "id" in the structure too. And if that existed, we could make process_vm_*v() safer too (taking a pid-fd instead of a pid).

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#11 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 3 lines · Kees Cook
Quoted 1 line · Andy Lutomirski

One possible extra issue: IIRC /proc/.../mem uses FOLL_FORCE, which is not what we want here.

Uuugh, I forgot about that.

Quoted 12 lines
Quoted 1 line

How about just adding an explicit “read/write the seccomp-trapped task’s memory” primitive? That should be easier than a “open mem fd” primitive.

Uuugh. Can we avoid adding another "read/write remote process memory" interface? The point of this series was to provide a lightweight approach to what should normally be possible via the existing seccomp+ptrace interface. I do like Jann's context idea, but I agree with Andy: it can't be a handle to /proc/$pid/mem, since it's FOLL_FORCE. Is there any other kind of process context id we can use for this instead of pid? There was once an idea of pid-fd but it never landed... This would let us get rid of the "id" in the structure too. And if that existed, we could make process_vm_*v() safer too (taking a pid-fd instead of a pid).

Or make a duplicate of /proc/$pid/mem that only differs in whether it sets FOLL_FORCE? The code is basically already there... something like this:

fs/proc/base.c +16 -1
index 80aa42506b8b..e8a6a63046da 100644
@@ -762,6 +762,8 @@ static int mem_open(struct inode *inode, struct file *file)
        return ret;
 }
 
+static const struct file_operations proc_mem_operations;
+
 static ssize_t mem_rw(struct file *file, char __user *buf,
                        size_t count, loff_t *ppos, int write)
 {
@@ -782,7 +784,10 @@ static ssize_t mem_rw(struct file *file, char __user *buf,
        if (!mmget_not_zero(mm))
                goto free;
 
-       flags = FOLL_FORCE | (write ? FOLL_WRITE : 0);
+       flags = (write ? FOLL_WRITE : 0);
+       if (file->f_op == &proc_mem_operations) {
+               flags |= FOLL_FORCE;
+       }
 
        while (count > 0) {
                int this_len = min_t(int, count, PAGE_SIZE);
@@ -861,6 +866,14 @@ static const struct file_operations proc_mem_operations = {
        .release        = mem_release,
 };
 
+static const struct file_operations proc_mem_noforce_operations = {
+       .llseek         = mem_lseek,
+       .read           = mem_read,
+       .write          = mem_write,
+       .open           = mem_open,
+       .release        = mem_release,
+};
+
 static int environ_open(struct inode *inode, struct file *file)
 {
        return __mem_open(inode, file, PTRACE_MODE_READ);
@@ -2916,6 +2929,7 @@ static const struct pid_entry tgid_base_stuff[] = {
        REG("numa_maps",  S_IRUGO, proc_pid_numa_maps_operations),
 #endif
        REG("mem",        S_IRUSR|S_IWUSR, proc_mem_operations),
+       REG("mem_noforce", S_IRUSR|S_IWUSR, proc_mem_noforce_operations),
        LNK("cwd",        proc_cwd_link),
        LNK("root",       proc_root_link),
        LNK("exe",        proc_exe_link),
@@ -3302,6 +3316,7 @@ static const struct pid_entry tid_base_stuff[] = {
        REG("numa_maps", S_IRUGO, proc_tid_numa_maps_operations),
 #endif
        REG("mem",       S_IRUSR|S_IWUSR, proc_mem_operations),
+       REG("mem_noforce",S_IRUSR|S_IWUSR, proc_mem_noforce_operations),
        LNK("cwd",       proc_cwd_link),
        LNK("root",      proc_root_link),
        LNK("exe",       proc_exe_link),
#12 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 20 lines · Jann Horn
Quoted 3 lines · Kees Cook
Quoted 1 line · Andy Lutomirski

One possible extra issue: IIRC /proc/.../mem uses FOLL_FORCE, which is not what we want here.

Uuugh, I forgot about that.

Quoted 12 lines
Quoted 1 line

How about just adding an explicit “read/write the seccomp-trapped task’s memory” primitive? That should be easier than a “open mem fd” primitive.

Uuugh. Can we avoid adding another "read/write remote process memory" interface? The point of this series was to provide a lightweight approach to what should normally be possible via the existing seccomp+ptrace interface. I do like Jann's context idea, but I agree with Andy: it can't be a handle to /proc/$pid/mem, since it's FOLL_FORCE. Is there any other kind of process context id we can use for this instead of pid? There was once an idea of pid-fd but it never landed... This would let us get rid of the "id" in the structure too. And if that existed, we could make process_vm_*v() safer too (taking a pid-fd instead of a pid).

Quoted 3 lines

Or make a duplicate of /proc/$pid/mem that only differs in whether it sets FOLL_FORCE? The code is basically already there... something like this:

But we want more than just memory access, I think. rootfs access, ns fds, etc. all seem like they might be useful, and racy to open.

I guess I see two options: use the existing id and add something to seccomp() to ask if it's still valid or independent of this patchset add some kind of pid id :\

Tycho

#13 lore raw

Re: [PATCH v4 1/4] seccomp: add a return code to trap to userspace

Quoted 29 lines · Tycho Andersen
Quoted 22 lines · Jann Horn
Quoted 4 lines · Kees Cook
Quoted 3 lines · Andy Lutomirski

One possible extra issue: IIRC /proc/.../mem uses FOLL_FORCE, which is not what we want here.

Uuugh, I forgot about that.

Quoted 12 lines
Quoted 1 line

How about just adding an explicit “read/write the seccomp-trapped task’s memory” primitive? That should be easier than a “open mem fd” primitive.

Uuugh. Can we avoid adding another "read/write remote process memory" interface? The point of this series was to provide a lightweight approach to what should normally be possible via the existing seccomp+ptrace interface. I do like Jann's context idea, but I agree with Andy: it can't be a handle to /proc/$pid/mem, since it's FOLL_FORCE. Is there any other kind of process context id we can use for this instead of pid? There was once an idea of pid-fd but it never landed... This would let us get rid of the "id" in the structure too. And if that existed, we could make process_vm_*v() safer too (taking a pid-fd instead of a pid).

Quoted 3 lines

Or make a duplicate of /proc/$pid/mem that only differs in whether it sets FOLL_FORCE? The code is basically already there... something like this:

Quoted 7 lines

But we want more than just memory access, I think. rootfs access, ns fds, etc. all seem like they might be useful, and racy to open.

I guess I see two options: use the existing id and add something to seccomp() to ask if it's still valid or independent of this patchset add some kind of pid id :\

I think we use the existing id / cookie / whatever and ask seccomp, or new syscalls, to do the requested operation. This is because we know the target task is in a very special stopping point. As a result, a seccomp-specific mechanism can do RCU-less fd modifications against a single-threaded target, can muck with things like struct cred, etc, while a more general interface can’t.

It might be nice to add a syscall with flags such that it could be used on ptrace-stopped targets later on. Something like:

access_remote_task(int fd, u64 id, u32 type, ...)

Where type is 16 bits of “id and fd is from seccomp” and 16 bits of “write memory” or such.

#14 lore raw

PATCH v4 2/4 seccomp: make get_nth_filter available outside of CHECKPOINT_RESTORE

In reply to Tycho Andersen

In the next commit we'll use this same mnemonic to get a listener for the nth filter, so we need it available outside of CHECKPOINT_RESTORE. This is slightly looser than necessary, because it really could be CHECKPOINT_RESTORE || USER_NOTIFICATION, but it's declared static and this complicates the code less, so hopefully it's ok.

v2: new in v2
v3: no changes

CC Kees Cook <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Eric W. Biederman <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Christian Brauner <[email protected]>
CC Tyler Hicks <[email protected]>
CC Akihiro Suda <[email protected]>
---
 kernel/seccomp.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/seccomp.c +4 -2
index 24949478a812..bbc24938c51d 100644
@@ -1184,7 +1184,7 @@ long prctl_set_seccomp(unsigned long seccomp_mode, char __user *filter)
 	return do_seccomp(op, 0, uargs);
 }
 
-#if defined(CONFIG_SECCOMP_FILTER) && defined(CONFIG_CHECKPOINT_RESTORE)
+#if defined(CONFIG_SECCOMP_FILTER)
 static struct seccomp_filter *get_nth_filter(struct task_struct *task,
 					     unsigned long filter_off)
 {
@@ -1231,6 +1231,7 @@ static struct seccomp_filter *get_nth_filter(struct task_struct *task,
 	return filter;
 }
 
+#if defined(CONFIG_CHECKPOINT_RESTORE)
 long seccomp_get_filter(struct task_struct *task, unsigned long filter_off,
 			void __user *data)
 {
@@ -1303,7 +1304,8 @@ long seccomp_get_metadata(struct task_struct *task,
 	__put_seccomp_filter(filter);
 	return ret;
 }
-#endif
+#endif /* CONFIG_CHECKPOINT_RESTORE */
+#endif /* CONFIG_SECCOMP_FILTER */
 
 #ifdef CONFIG_SYSCTL
 
Signature · 1 line
2.17.1
#15 lore raw

PATCH v4 3/4 seccomp: add a way to get a listener fd from ptrace

In reply to Tycho Andersen

As an alternative to SECCOMP_FILTER_FLAG_GET_LISTENER, perhaps a ptrace() version which can acquire filters is useful. There are at least two reasons this is preferable, even though it uses ptrace:

1. You can control tasks that aren't cooperating with you
2. You can control tasks whose filters block sendmsg() and socket(); if the task installs a filter which blocks these calls, there's no way with SECCOMP_FILTER_FLAG_GET_LISTENER to get the fd out to the privileged task.

v2: fix a bug where listener mode was not unset when an unused fd was not
available
v3: fix refcounting bug (Oleg)
v4: * change the listener's fd flags to be 0
* rename GET_LISTENER to NEW_LISTENER (Matthew)

CC Kees Cook <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Eric W. Biederman <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Christian Brauner <[email protected]>
CC Tyler Hicks <[email protected]>
CC Akihiro Suda <[email protected]>
---
 include/linux/seccomp.h                       | 11 ++++
 include/uapi/linux/ptrace.h                   |  2 +
 kernel/ptrace.c                               |  4 ++
 kernel/seccomp.c                              | 28 ++++++++
 tools/testing/selftests/seccomp/seccomp_bpf.c | 66 +++++++++++++++++++
 5 files changed, 111 insertions(+)
include/linux/seccomp.h +11 -0
index 017444b5efed..c17c7d051af0 100644
@@ -112,4 +112,15 @@ static inline long seccomp_get_metadata(struct task_struct *task,
 	return -EINVAL;
 }
 #endif /* CONFIG_SECCOMP_FILTER && CONFIG_CHECKPOINT_RESTORE */
+
+#ifdef CONFIG_SECCOMP_USER_NOTIFICATION
+extern long seccomp_new_listener(struct task_struct *task,
+				 unsigned long filter_off);
+#else
+static inline long seccomp_new_listener(struct task_struct *task,
+					unsigned long filter_off)
+{
+	return -EINVAL;
+}
+#endif/* CONFIG_SECCOMP_USER_NOTIFICATION */
 #endif /* _LINUX_SECCOMP_H */
include/uapi/linux/ptrace.h +2 -0
index d5a1b8a492b9..e80ecb1bd427 100644
@@ -73,6 +73,8 @@ struct seccomp_metadata {
 	__u64 flags;		/* Output: filter's flags */
 };
 
+#define PTRACE_SECCOMP_NEW_LISTENER	0x420e
+
 /* Read signals from a shared (process wide) queue */
 #define PTRACE_PEEKSIGINFO_SHARED	(1 << 0)
 
kernel/ptrace.c +4 -0
index 21fec73d45d4..289960ac181b 100644
@@ -1096,6 +1096,10 @@ int ptrace_request(struct task_struct *child, long request,
 		ret = seccomp_get_metadata(child, addr, datavp);
 		break;
 
+	case PTRACE_SECCOMP_NEW_LISTENER:
+		ret = seccomp_new_listener(child, addr);
+		break;
+
 	default:
 		break;
 	}
kernel/seccomp.c +28 -0
index bbc24938c51d..b68a5d4a15cd 100644
@@ -1743,6 +1743,34 @@ static struct file *init_listener(struct task_struct *task,
 
 	return ret;
 }
+
+long seccomp_new_listener(struct task_struct *task,
+			  unsigned long filter_off)
+{
+	struct seccomp_filter *filter;
+	struct file *listener;
+	int fd;
+
+	filter = get_nth_filter(task, filter_off);
+	if (IS_ERR(filter))
+		return PTR_ERR(filter);
+
+	fd = get_unused_fd_flags(0);
+	if (fd < 0) {
+		__put_seccomp_filter(filter);
+		return fd;
+	}
+
+	listener = init_listener(task, task->seccomp.filter);
+	__put_seccomp_filter(filter);
+	if (IS_ERR(listener)) {
+		put_unused_fd(fd);
+		return PTR_ERR(listener);
+	}
+
+	fd_install(fd, listener);
+	return fd;
+}
 #else
 static struct file *init_listener(struct task_struct *task,
 				  struct seccomp_filter *filter)
tools/testing/selftests/seccomp/seccomp_bpf.c +66 -0

78 lines not on this page · raw message

Signature · 1 line
2.17.1
#16 lore raw

Re: [PATCH v4 3/4] seccomp: add a way to get a listener fd from ptrace

Quoted 8 lines · Tycho Andersen

As an alternative to SECCOMP_FILTER_FLAG_GET_LISTENER, perhaps a ptrace() version which can acquire filters is useful. There are at least two reasons this is preferable, even though it uses ptrace:

1. You can control tasks that aren't cooperating with you
2. You can control tasks whose filters block sendmsg() and socket(); if the task installs a filter which blocks these calls, there's no way with SECCOMP_FILTER_FLAG_GET_LISTENER to get the fd out to the privileged task.

[...]

Quoted 26 lines
kernel/seccomp.c +18 -0
index bbc24938c51d..b68a5d4a15cd 100644
@@ -1743,6 +1743,34 @@ static struct file *init_listener(struct task_struct *task,
 
        return ret;
 }
+
+long seccomp_new_listener(struct task_struct *task,
+                         unsigned long filter_off)
+{
+       struct seccomp_filter *filter;
+       struct file *listener;
+       int fd;
+
+       filter = get_nth_filter(task, filter_off);
+       if (IS_ERR(filter))
+               return PTR_ERR(filter);
+
+       fd = get_unused_fd_flags(0);
+       if (fd < 0) {
+               __put_seccomp_filter(filter);
+               return fd;
+       }
+

kernel/seccomp.c static struct file *init_listener(struct task_struct *task, · line 1773

Quoted 10 lines
kernel/seccomp.c +10 -0
+       listener = init_listener(task, task->seccomp.filter);
+       __put_seccomp_filter(filter);
+       if (IS_ERR(listener)) {
+               put_unused_fd(fd);
+               return PTR_ERR(listener);
+       }
+
+       fd_install(fd, listener);
+       return fd;
+}

I think there's a security problem here. Imagine the following scenario:

1. task A (uid==0) sets up a seccomp filter that uses SECCOMP_RET_USER_NOTIF
2. task A forks off a child B
3. task B uses setuid(1) to drop its privileges
4. task B becomes dumpable again, either via prctl(PR_SET_DUMPABLE, 1) or via execve()
5. task C (the attacker, uid==1) attaches to task B via ptrace
6. task C uses PTRACE_SECCOMP_NEW_LISTENER on task B
7. because the seccomp filter is shared by task A and task B, task C is now able to influence syscall results for syscalls performed by task A

Unless I'm missing something, you might have to add some extra security check here: Either a check to ensure that no other task is using the same seccomp filter, or (as a last resort) a check for capable(CAP_SYS_ADMIN).

#17 lore raw

Re: [PATCH v4 3/4] seccomp: add a way to get a listener fd from ptrace

Hi Jann,

Quoted 60 lines · Jann Horn
Quoted 8 lines · Tycho Andersen

As an alternative to SECCOMP_FILTER_FLAG_GET_LISTENER, perhaps a ptrace() version which can acquire filters is useful. There are at least two reasons this is preferable, even though it uses ptrace:

1. You can control tasks that aren't cooperating with you
2. You can control tasks whose filters block sendmsg() and socket(); if the task installs a filter which blocks these calls, there's no way with SECCOMP_FILTER_FLAG_GET_LISTENER to get the fd out to the privileged task.

[...]

Quoted 26 lines
kernel/seccomp.c +18 -0
index bbc24938c51d..b68a5d4a15cd 100644
@@ -1743,6 +1743,34 @@ static struct file *init_listener(struct task_struct *task,
 
        return ret;
 }
+
+long seccomp_new_listener(struct task_struct *task,
+                         unsigned long filter_off)
+{
+       struct seccomp_filter *filter;
+       struct file *listener;
+       int fd;
+
+       filter = get_nth_filter(task, filter_off);
+       if (IS_ERR(filter))
+               return PTR_ERR(filter);
+
+       fd = get_unused_fd_flags(0);
+       if (fd < 0) {
+               __put_seccomp_filter(filter);
+               return fd;
+       }
+

kernel/seccomp.c static struct file *init_listener(struct task_struct *task, · line 1773

Quoted 10 lines
kernel/seccomp.c +10 -0
+       listener = init_listener(task, task->seccomp.filter);
+       __put_seccomp_filter(filter);
+       if (IS_ERR(listener)) {
+               put_unused_fd(fd);
+               return PTR_ERR(listener);
+       }
+
+       fd_install(fd, listener);
+       return fd;
+}

I think there's a security problem here. Imagine the following scenario:

1. task A (uid==0) sets up a seccomp filter that uses SECCOMP_RET_USER_NOTIF
2. task A forks off a child B
3. task B uses setuid(1) to drop its privileges
4. task B becomes dumpable again, either via prctl(PR_SET_DUMPABLE, 1) or via execve()
5. task C (the attacker, uid==1) attaches to task B via ptrace
6. task C uses PTRACE_SECCOMP_NEW_LISTENER on task B
7. because the seccomp filter is shared by task A and task B, task C is now able to influence syscall results for syscalls performed by task A

Quoted 4 lines

Unless I'm missing something, you might have to add some extra security check here: Either a check to ensure that no other task is using the same seccomp filter, or (as a last resort) a check for capable(CAP_SYS_ADMIN).

I guess my first thought is "don't do that". But I am also not opposed to adding a check for capable(CAP_SYS_ADMIN) to prevent the footgun, so I can do that for v5. I think checking whether other tasks are using a filter would be hard without adding some additional counter logic or something, and at least for the use cases I know of, capable(CAP_SYS_ADMIN) is fine.

Tycho

#18 lore raw

PATCH v4 4/4 seccomp: add support for passing fds via USER_NOTIF

In reply to Tycho Andersen

The idea here is that the userspace handler should be able to pass an fd back to the trapped task, for example so it can be returned from socket().

I've proposed one API here, but I'm open to other options. In particular, this only lets you return an fd from a syscall, which may not be enough in all cases. For example, if an fd is written to an output parameter instead of returned, the current API can't handle this. Another case is that netlink takes as input fds sometimes (IFLA_NET_NS_FD, e.g.). If netlink ever decides to install an fd and output it, we wouldn't be able to handle this either.

Still, the vast majority of interesting cases are covered by this API, so perhaps it is Enough.

I've left it as a separate commit for two reasons:
* It illustrates the way in which we would grow struct seccomp_notif and struct seccomp_notif_resp without using netlink
* It shows just how little code is needed to accomplish this :)

v2: new in v2
v3: no changes
v4: * pass fd flags back from userspace as well (Jann)
* update same cgroup data on fd pass as SCM_RIGHTS (Alban)
* only set the REPLIED state /after/ successful fdget (Alban)
* reflect GET_LISTENER -> NEW_LISTENER changes
* add to the new Documentation/ on user notifications about fd replies

CC Kees Cook <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Eric W. Biederman <[email protected]>
CC "Serge E. Hallyn" <[email protected]>
CC Christian Brauner <[email protected]>
CC Tyler Hicks <[email protected]>
CC Akihiro Suda <[email protected]>
---
 .../userspace-api/seccomp_filter.rst          |  11 ++
 include/uapi/linux/seccomp.h                  |   3 +
 kernel/seccomp.c                              |  51 +++++++-
 tools/testing/selftests/seccomp/seccomp_bpf.c | 114 ++++++++++++++++++
 4 files changed, 177 insertions(+), 2 deletions(-)
Documentation/userspace-api/seccomp_filter.rst +11 -0
index e51422559dd6..3db93df254fb 100644
@@ -232,6 +232,9 @@ The interface for a seccomp notification fd consists of two structures:
         __u64 id;
         __s32 error;
         __s64 val;
+        __u8 return_fd;
+        __u32 fd;
+        __u32 fd_flags;
     };
 
 Users can ``read()`` or ``poll()`` on a seccomp notification fd to receive a
@@ -251,6 +254,14 @@ mentioned above in this document: all arguments being read from the tracee's
 memory should be read into the tracer's memory before any policy decisions are
 made. This allows for an atomic decision on syscall arguments.
 
+Userspace can also return file descriptors. For example, one may decide to
+intercept ``socket()`` syscalls, and return some file descriptor from those
+based on some policy. To return a file descriptor, the ``return_fd`` member
+should be non-zero, the ``fd`` argument should be the fd in the listener's
+table to send to the tracee (similar to how ``SCM_RIGHTS`` works), and
+``fd_flags`` should be the flags that the fd in the tracee's table is opened
+with (e.g. ``O_EXCL`` or similar).
+
 Sysctls
 =======
 
include/uapi/linux/seccomp.h +3 -0
index 8836a3b25500..ed2a475e0fe6 100644
@@ -72,6 +72,9 @@ struct seccomp_notif_resp {
 	__u64 id;
 	__s32 error;
 	__s64 val;
+	__u8 return_fd;
+	__u32 fd;
+	__u32 fd_flags;
 };
 
 #endif /* _UAPI_LINUX_SECCOMP_H */
kernel/seccomp.c +49 -2

81 lines not on this page · raw message

tools/testing/selftests/seccomp/seccomp_bpf.c +114 -0

126 lines not on this page · raw message

Signature · 1 line
2.17.1
#19 lore raw

Re: [PATCH v4 4/4] seccomp: add support for passing fds via USER_NOTIF

Quoted 3 lines · Tycho Andersen

The idea here is that the userspace handler should be able to pass an fd back to the trapped task, for example so it can be returned from socket().

[...]

Documentation/userspace-api/seccomp_filter.rst mentioned above in this document: all arguments being read from the tracee's · line 263

Quoted 7 lines
Documentation/userspace-api/seccomp_filter.rst +7 -0
+Userspace can also return file descriptors. For example, one may decide to
+intercept ``socket()`` syscalls, and return some file descriptor from those
+based on some policy. To return a file descriptor, the ``return_fd`` member
+should be non-zero, the ``fd`` argument should be the fd in the listener's
+table to send to the tracee (similar to how ``SCM_RIGHTS`` works), and
+``fd_flags`` should be the flags that the fd in the tracee's table is opened
+with (e.g. ``O_EXCL`` or similar).

fd_flags only contains file descriptor flags (meaning only O_CLOEXEC). O_EXCL is a file creation flag, so setting it here wouldn't make sense. Setting file status flags like O_APPEND does make sense, but those are stored in the struct file and don't need to be passed separately; the caller can e.g. set them via fcntl(fd, F_SETFD, flags) or on open().
(The fcntl.2 manpage explains these.)

#20 lore raw

Re: [PATCH v4 4/4] seccomp: add support for passing fds via USER_NOTIF

Quoted 5 lines · Jann Horn
Quoted 3 lines · Tycho Andersen

The idea here is that the userspace handler should be able to pass an fd back to the trapped task, for example so it can be returned from socket().

[...]

Quoted 15 lines

Documentation/userspace-api/seccomp_filter.rst mentioned above in this document: all arguments being read from the tracee's · line 263

Quoted 7 lines
Documentation/userspace-api/seccomp_filter.rst +7 -0
+Userspace can also return file descriptors. For example, one may decide to
+intercept ``socket()`` syscalls, and return some file descriptor from those
+based on some policy. To return a file descriptor, the ``return_fd`` member
+should be non-zero, the ``fd`` argument should be the fd in the listener's
+table to send to the tracee (similar to how ``SCM_RIGHTS`` works), and
+``fd_flags`` should be the flags that the fd in the tracee's table is opened
+with (e.g. ``O_EXCL`` or similar).

fd_flags only contains file descriptor flags (meaning only O_CLOEXEC). O_EXCL is a file creation flag, so setting it here wouldn't make sense. Setting file status flags like O_APPEND does make sense, but those are stored in the struct file and don't need to be passed separately; the caller can e.g. set them via fcntl(fd, F_SETFD, flags) or on open().
(The fcntl.2 manpage explains these.)

Ugh, yes, O_CLOEXEC is what I meant. Thanks, I'll clarify.

Tycho

#21 lore raw

Re: [PATCH v4 4/4] seccomp: add support for passing fds via USER_NOTIF

In reply to Tycho Andersen
Quoted 20 lines · Tycho Andersen

The idea here is that the userspace handler should be able to pass an fd back to the trapped task, for example so it can be returned from socket().

I've proposed one API here, but I'm open to other options. In particular, this only lets you return an fd from a syscall, which may not be enough in all cases. For example, if an fd is written to an output parameter instead of returned, the current API can't handle this. Another case is that netlink takes as input fds sometimes (IFLA_NET_NS_FD, e.g.). If netlink ever decides to install an fd and output it, we wouldn't be able to handle this either.

Still, the vast majority of interesting cases are covered by this API, so perhaps it is Enough.

I've left it as a separate commit for two reasons:
* It illustrates the way in which we would grow struct seccomp_notif and struct seccomp_notif_resp without using netlink
* It shows just how little code is needed to accomplish this :)

[...]

kernel/seccomp.c static ssize_t seccomp_notify_write(struct file *file, const char __user *buf, · line 1717

Quoted 13 lines
kernel/seccomp.c +9 -0
@@ -1669,10 +1706,20 @@ static ssize_t seccomp_notify_write(struct file *file, const char __user *buf,
                goto out;
        }
 
+       if (resp.return_fd) {
+               knotif->flags = resp.fd_flags;
+               knotif->file = fget(resp.fd);
+               if (!knotif->file) {
+                       ret = -EBADF;
+                       goto out;
+               }
+       }
+

I think this is a security bug. Imagine the following scenario:

- attacker creates processes A and B
- process A installs a seccomp filter and sends the notification fd
to process B
- process A starts a syscall for which the filter returns
SECCOMP_RET_USER_NOTIF
- process B reads the notification from the notification fd
- process B uses dup2() to copy the notification fd to file
descriptor 1 (stdout)
- process B executes a setuid root binary
- the setuid root binary opens some privileged file descriptor
(something like open("/etc/shadow", O_RDWR))
- the setuid root binary tries to write some attacker-controlled data to stdout
- seccomp_notify_write() interprets the start of the written data as
a struct seccomp_notif_resp
- seccomp_notify_write() grabs the privileged file descriptor and
installs a copy in process A
- process A now has access to the privileged file (e.g. /etc/shadow)

It isn't clear whether it would actually be exploitable - you'd need a setuid binary that performs the right actions - but it's still bad.

Unless I'm missing something, can you please turn the ->read and ->write handlers into an ->unlocked_ioctl handler? Something like this:

struct seccomp_user_notif_args {
        u64 buf;
        u64 size;
};

static long unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

{
        struct seccomp_user_notif_args args;
        struct seccomp_user_notif_args __user *uargs;

        if (cmd != SECCOMP_USER_NOTIF_READ && cmd != SECCOMP_USER_NOTIF_WRITE)
                return -EINVAL;

        if (copy_from_user(&args, uargs, sizeof(args)))
                return -EFAULT;

        switch (cmd) {
        case SECCOMP_USER_NOTIF_READ:
                return seccomp_notify_read(file, (char __user
*)args.buf, (size_t)args.size);
        case SECCOMP_USER_NOTIF_WRITE:
                return seccomp_notify_write(file, (char __user
*)args.buf, (size_t)args.size);
        default:
                return -EINVAL;
        }
}
#22 lore raw

Re: [PATCH v4 4/4] seccomp: add support for passing fds via USER_NOTIF

Quoted 60 lines · Jann Horn
Quoted 22 lines · Tycho Andersen

The idea here is that the userspace handler should be able to pass an fd back to the trapped task, for example so it can be returned from socket().

I've proposed one API here, but I'm open to other options. In particular, this only lets you return an fd from a syscall, which may not be enough in all cases. For example, if an fd is written to an output parameter instead of returned, the current API can't handle this. Another case is that netlink takes as input fds sometimes (IFLA_NET_NS_FD, e.g.). If netlink ever decides to install an fd and output it, we wouldn't be able to handle this either.

Still, the vast majority of interesting cases are covered by this API, so perhaps it is Enough.

I've left it as a separate commit for two reasons:
* It illustrates the way in which we would grow struct seccomp_notif and struct seccomp_notif_resp without using netlink
* It shows just how little code is needed to accomplish this :)

[...]

kernel/seccomp.c static ssize_t seccomp_notify_write(struct file *file, const char __user *buf, · line 1717

Quoted 13 lines
kernel/seccomp.c +9 -0
@@ -1669,10 +1706,20 @@ static ssize_t seccomp_notify_write(struct file *file, const char __user *buf,
               goto out;
       }
 
+       if (resp.return_fd) {
+               knotif->flags = resp.fd_flags;
+               knotif->file = fget(resp.fd);
+               if (!knotif->file) {
+                       ret = -EBADF;
+                       goto out;
+               }
+       }
+

I think this is a security bug. Imagine the following scenario:

- attacker creates processes A and B
- process A installs a seccomp filter and sends the notification fd to process B
- process A starts a syscall for which the filter returns SECCOMP_RET_USER_NOTIF
- process B reads the notification from the notification fd
- process B uses dup2() to copy the notification fd to file descriptor 1 (stdout)
- process B executes a setuid root binary
- the setuid root binary opens some privileged file descriptor (something like open("/etc/shadow", O_RDWR))
- the setuid root binary tries to write some attacker-controlled data to stdout
- seccomp_notify_write() interprets the start of the written data as a struct seccomp_notif_resp
- seccomp_notify_write() grabs the privileged file descriptor and installs a copy in process A
- process A now has access to the privileged file (e.g. /etc/shadow)

Quoted 2 lines

It isn't clear whether it would actually be exploitable - you'd need a setuid binary that performs the right actions - but it's still bad.

Jann is right. ->read and ->write must not reference any of the calling task’s state except the literal memory passed in.

Quoted 33 lines

Unless I'm missing something, can you please turn the ->read and ->write handlers into an ->unlocked_ioctl handler? Something like this:

struct seccomp_user_notif_args {
       u64 buf;
       u64 size;
};

static long unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)

{
       struct seccomp_user_notif_args args;
       struct seccomp_user_notif_args __user *uargs;

       if (cmd != SECCOMP_USER_NOTIF_READ && cmd != SECCOMP_USER_NOTIF_WRITE)
               return -EINVAL;

       if (copy_from_user(&args, uargs, sizeof(args)))
               return -EFAULT;

       switch (cmd) {
       case SECCOMP_USER_NOTIF_READ:
               return seccomp_notify_read(file, (char __user
*)args.buf, (size_t)args.size);
       case SECCOMP_USER_NOTIF_WRITE:
               return seccomp_notify_write(file, (char __user
*)args.buf, (size_t)args.size);
       default:
               return -EINVAL;
       }
}
#23 lore raw
In reply to Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

Tycho

Quoted 37 lines · Tycho Andersen

Hi all,

Here's v4 of the seccomp trap to userspace series. v3 is here:
https://lkml.org/lkml/2018/5/31/527

I believe we've addressed the two burning questions I had about v3: 1. it seems ok not to use netlink, since there's not a great way to re-use the API without a lot of unnecessary code and 2. only having return capability for fds seems fine with people. Or at least I haven't heard any strong objections.

I've re-worked a bunch of things in this version based on feedback from the last series. See patch notes for details. At this point I'm not aware of anything that needs to be addressed, but of course that is subject to change :)

Tycho

Tycho Andersen (4):
seccomp: add a return code to trap to userspace
seccomp: make get_nth_filter available outside of CHECKPOINT_RESTORE
seccomp: add a way to get a listener fd from ptrace
seccomp: add support for passing fds via USER_NOTIF

 .../userspace-api/seccomp_filter.rst          |  79 +++
 arch/Kconfig                                  |   7 +
 include/linux/seccomp.h                       |  18 +-
 include/uapi/linux/ptrace.h                   |   2 +
 include/uapi/linux/seccomp.h                  |  23 +-
 kernel/ptrace.c                               |   4 +
 kernel/seccomp.c                              | 491 ++++++++++++++-
 tools/testing/selftests/seccomp/seccomp_bpf.c | 560 +++++++++++++++++-
 8 files changed, 1172 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#24 lore raw
Quoted 8 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

That... is utterly ridiculous. Does LF have a mechanism to figure out wtf and deal with it by, for example, filing for ex parte review.

Every microkernel ever should strongly resemble prior art.

Quoted 41 lines

Tycho

Quoted 38 lines · Tycho Andersen

Hi all,

Here's v4 of the seccomp trap to userspace series. v3 is here:
https://lkml.org/lkml/2018/5/31/527

I believe we've addressed the two burning questions I had about v3: 1. it seems ok not to use netlink, since there's not a great way to re-use the API without a lot of unnecessary code and 2. only having return capability for fds seems fine with people. Or at least I haven't heard any strong objections.

I've re-worked a bunch of things in this version based on feedback from the last series. See patch notes for details. At this point I'm not aware of anything that needs to be addressed, but of course that is subject to change :)

Tycho

Tycho Andersen (4):
seccomp: add a return code to trap to userspace
seccomp: make get_nth_filter available outside of CHECKPOINT_RESTORE
seccomp: add a way to get a listener fd from ptrace
seccomp: add support for passing fds via USER_NOTIF

.../userspace-api/seccomp_filter.rst          |  79 +++
arch/Kconfig                                  |   7 +
include/linux/seccomp.h                       |  18 +-
include/uapi/linux/ptrace.h                   |   2 +
include/uapi/linux/seccomp.h                  |  23 +-
kernel/ptrace.c                               |   4 +
kernel/seccomp.c                              | 491 ++++++++++++++-
tools/testing/selftests/seccomp/seccomp_bpf.c | 560 +++++++++++++++++-
8 files changed, 1172 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#25 lore raw
In reply to Tycho Andersen
Quoted 5 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

Hey man,

Sorry to hear that your faced with such nonsense, Tycho. This is utter bullsh*t of course. If you have more details at some point and feel comfortable doing so it would probably be good to share them here.

Christian

Quoted 45 lines

Tycho

Quoted 24 lines · Tycho Andersen

Hi all,

Here's v4 of the seccomp trap to userspace series. v3 is here:
https://lkml.org/lkml/2018/5/31/527

I believe we've addressed the two burning questions I had about v3: 1. it seems ok not to use netlink, since there's not a great way to re-use the API without a lot of unnecessary code and 2. only having return capability for fds seems fine with people. Or at least I haven't heard any strong objections.

I've re-worked a bunch of things in this version based on feedback from the last series. See patch notes for details. At this point I'm not aware of anything that needs to be addressed, but of course that is subject to change :)

Tycho

Tycho Andersen (4):
seccomp: add a return code to trap to userspace
seccomp: make get_nth_filter available outside of CHECKPOINT_RESTORE
seccomp: add a way to get a listener fd from ptrace
seccomp: add support for passing fds via USER_NOTIF

Quoted 13 lines
 .../userspace-api/seccomp_filter.rst          |  79 +++
 arch/Kconfig                                  |   7 +
 include/linux/seccomp.h                       |  18 +-
 include/uapi/linux/ptrace.h                   |   2 +
 include/uapi/linux/seccomp.h                  |  23 +-
 kernel/ptrace.c                               |   4 +
 kernel/seccomp.c                              | 491 ++++++++++++++-
 tools/testing/selftests/seccomp/seccomp_bpf.c | 560 +++++++++++++++++-
 8 files changed, 1172 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1

_______________________________________________
Containers mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/containers

#26 lore raw
Quoted 7 lines · Christian Brauner
Quoted 5 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

Quoted 5 lines

Hey man,

Sorry to hear that your faced with such nonsense, Tycho. This is utter bullsh*t of course. If you have more details at some point and feel comfortable doing so it would probably be good to share them here.

IANAL, but I think it would probably *not* be good to share them here. Patent law is seriously fucked up like that.

--Andy

#27 lore raw
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines · Christian Brauner
Quoted 5 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

Quoted 8 lines
Quoted 5 lines

Hey man,

Sorry to hear that your faced with such nonsense, Tycho. This is utter bullsh*t of course. If you have more details at some point and feel comfortable doing so it would probably be good to share them here.

IANAL, but I think it would probably *not* be good to share them here. Patent law is seriously fucked up like that.

**ugh**. Yeah, you're probably right. My thought was that in the face of publicity this nonesense might go away quickly.

Christian

#28 lore raw
In reply to Tycho Andersen
Quoted 5 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

Just on a general policy for handling things like this.

Firstly, never discuss specifics on a mailing list. Patents are a really tricky area where advice in confidence is needed and lack of confidentiality usually only benefits the patent holder.

Secondly, vague threats (like this) can be safely ignored. Thanks to the terms of the GPL and the specific actions of OIN, we legitimately have licences to a vast pool of patents, so we should assume for every vague threat of the existence of a patent that we do legitimately possess a licence until the threat is made specific.

Finally, if the vague threat becomes specific (which means citation of patent by number and claim) you need to engage the resources we have at our disposal to investigate. Likely it will be OIN resources that do this investigation, but the way to begin is to start with your corporate counsel for contributions on behalf of a company. If they're on behalf of you as an individual, you can begin with Counsel which the Linux Foundation will provide.

James

#29 lore raw
In reply to Tycho Andersen
Quoted 6 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

For the sake of everyone's clarity, there is no patent or intellectual property issue here and we'd like to see this feature in the kernel. I did indicate that there is a patent related to the mechanism underlying this patch set but that is completely incidental to the issue here. We have filed that patent only for the purpose of defense and have no interest or resources to go after infringements. As such, I spoke about the approach, it's value for networking and a few possible ways of implementing it in the kernel at Linux Plumbers Conference 2017.

As a contractor and a member of our team at AppSwitch (FKA Fermat), Tycho Andersen helped implement a fully user space version of system call trap mechanism based on seccomp / fd-passing and participated in our team discussions about upstreaming a kernel version of the feature. Given that context, we were taken aback that he posted the v1 patch set without letting us know and without any mention of AppSwitch in the post even though he was still under contract at that time.

In any case, please note that we will communicate with Tycho directly regarding this matter going forward.

--
Dinesh Subhraveti

A message that is not in the archive (sent off-list or not yet received): CAP4sa4+rODVahad2hW-L3h7k6fkfGBsoCfDfBVuMwp3Aaie2KA@mail.gmail.com

#31 lore raw
Quoted 16 lines · Dinesh Subhraveti
Quoted 5 lines · Tycho Andersen

Hi all,

Dinesh Subhraveti has claimed that some part of this series might be patented. While he has not furnished me with anything to confirm this claim, I'll put this series on hold.

For the sake of everyone's clarity, there is no patent or intellectual property issue here and we'd like to see this feature in the kernel. I did indicate that there is a patent related to the mechanism underlying this patch set but that is completely incidental to the issue here. We have filed that patent only for the purpose of defense and have no interest or resources to go after infringements. As such, I spoke about the approach, it's value for networking and a few possible ways of implementing it in the kernel at Linux Plumbers Conference 2017.

Quoted 7 lines

As a contractor and a member of our team at AppSwitch (FKA Fermat), Tycho Andersen helped implement a fully user space version of system call trap mechanism based on seccomp / fd-passing and participated in our team discussions about upstreaming a kernel version of the feature. Given that context, we were taken aback that he posted the v1 patch set without letting us know and without any mention of AppSwitch in the post even though he was still under contract at that time.

It seems we disagree on the series of events, but agree that this patchset should move forward. I'll work on preparing a v5.

Thanks!

Tycho