seccomp: Make duplicate listener detection non-racy
PATCH RESEND 1/1 seccomp: Make duplicate listener detection non-racy
Currently, init_listener() tries to prevent adding a filter with SECCOMP_FILTER_FLAG_NEW_LISTENER if one of the existing filters already has a listener. However, this check happens without holding any lock that would prevent another thread from concurrently installing a new filter (potentially with a listener) on top of the ones we already have.
Theoretically, this is also a data race: The plain load from current->seccomp.filter can race with concurrent writes to the same location.
Fix it by moving the check into the region that holds the siglock to guard against concurrent TSYNC.
(I am not marking this for stable backport because I believe that this does not have any implications beyond a theoretical data race and allowing userspace to create seccomp filters with weird semantics if userspace concurrently installs seccomp filters in a way no benign userspace workload would.)
(The "Fixes" tag points to the commit that introduced the theoretical data race; concurrent installation of another filter with TSYNC only became possible later, in commit 51891498f2da ("seccomp: allow TSYNC and USER_NOTIF together").)
---
NOTE: After Tycho gave his Reviewed-by, I've had to adjust the errno to -EBUSY (my original patch broke UAPI, good thing we have selftests), remove the unused "cur" from init_listener(), and remove the now useless initialization of "ret".
resending because the first time I mangled the diff... sorry
kernel/seccomp.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-)
kernel/seccomp.c +31 -7
@@ -1472,13 +1472,7 @@ static const struct file_operations seccomp_notify_ops = {static struct file *init_listener (struct seccomp_filter *filter) { -struct file *ret =ERR_PTR (-EBUSY ); -struct seccomp_filter *cur; - -for (cur = current->seccomp.filter; cur; cur = cur->prev) { -if (cur->notif) - goto out; - } +struct file *ret; ret =ERR_PTR (-ENOMEM ); filter->notif =kzalloc (sizeof (*(filter->notif)),GFP_KERNEL );
@@ -1504,6 +1498,31 @@ static struct file *init_listener(struct seccomp_filter *filter)return ret; } +/* +* Does @new_child have a listener while an ancestor also has a listener? +* If so, we'll want to reject this filter. +* This only has to be tested for the current process, even in the TSYNC case, +* because TSYNC installs @child with the same parent on all threads. +* Note that @new_child is not hooked up to its parent at this point yet, so +* we use current->seccomp.filter. +*/ +static bool has_duplicate_listener (struct seccomp_filter *new_child) +{ +struct seccomp_filter *cur; + +/* must be protected against concurrent TSYNC */ +lockdep_assert_held (¤t->sighand->siglock); + +if (!new_child->notif) +return false; +for (cur = current->seccomp.filter; cur; cur = cur->prev) { +if (cur->notif) +return true; + } + +return false; +} +/** * seccomp_set_mode_filter: internal function for setting seccomp filter * @flags: flags to change filter behavior
@@ -1575,6 +1594,11 @@ static long seccomp_set_mode_filter(unsigned int flags,if (!seccomp_may_assign_mode (seccomp_mode)) goto out; +if (has_duplicate_listener (prepared)) { + ret = -EBUSY ; + goto out; + } + ret =seccomp_attach_filter (flags, prepared);if (ret) goto out;
base-commit: fb0155a09b0224a7147cb07a4ce6034c8d29667f
Signature · 1 line
2.28.0.806.g8561365e88-goog
Kees Cook
AppliedQuoted 11 lines · Jann Horn
Currently,
init_listener()tries to prevent adding a filter withSECCOMP_FILTER_FLAG_NEW_LISTENERif one of the existing filters already has a listener. However, this check happens without holding any lock that would prevent another thread from concurrently installing a new filter (potentially with a listener) on top of the ones we already have.Theoretically, this is also a data race: The plain load from
current->seccomp.filtercan race with concurrent writes to the same location.[...]
Applied, thanks!
I added the stable CC, but I'd agree: it's not so urgent that I need to get this into Linus's tree ahead of the regular merge window. :)
[1/1] seccomp: Make duplicate listener detection non-racy
https://git.kernel.org/kees/c/ed2d479d3335
Signature · 1 line
Kees Cook