Skip to content

Commit 7a0df7f

Browse files
tych0kees
authored andcommitted
seccomp: Make NEW_LISTENER and TSYNC flags exclusive
As the comment notes, the return codes for TSYNC and NEW_LISTENER conflict, because they both return positive values, one in the case of success and one in the case of error. So, let's disallow both of these flags together. While this is technically a userspace break, all the users I know of are still waiting on me to land this feature in libseccomp, so I think it'll be safe. Also, at present my use case doesn't require TSYNC at all, so this isn't a big deal to disallow. If someone wanted to support this, a path forward would be to add a new flag like TSYNC_AND_LISTENER_YES_I_UNDERSTAND_THAT_TSYNC_WILL_JUST_RETURN_EAGAIN, but the use cases are so different I don't see it really happening. Finally, it's worth noting that this does actually fix a UAF issue: at the end of seccomp_set_mode_filter(), we have: if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) { if (ret < 0) { listener_f->private_data = NULL; fput(listener_f); put_unused_fd(listener); } else { fd_install(listener, listener_f); ret = listener; } } out_free: seccomp_filter_free(prepared); But if ret > 0 because TSYNC raced, we'll install the listener fd and then free the filter out from underneath it, causing a UAF when the task closes it or dies. This patch also switches the condition to be simply if (ret), so that if someone does add the flag mentioned above, they won't have to remember to fix this too. Reported-by: syzbot+b562969adb2e04af3442@syzkaller.appspotmail.com Fixes: 6a21cc5 ("seccomp: add a return code to trap to userspace") CC: stable@vger.kernel.org # v5.0+ Signed-off-by: Tycho Andersen <tycho@tycho.ws> Signed-off-by: Kees Cook <keescook@chromium.org> Acked-by: James Morris <jamorris@linux.microsoft.com>
1 parent 4ee0776 commit 7a0df7f

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

kernel/seccomp.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,10 @@ seccomp_prepare_user_filter(const char __user *user_filter)
502502
*
503503
* Caller must be holding current->sighand->siglock lock.
504504
*
505-
* Returns 0 on success, -ve on error.
505+
* Returns 0 on success, -ve on error, or
506+
* - in TSYNC mode: the pid of a thread which was either not in the correct
507+
* seccomp mode or did not have an ancestral seccomp filter
508+
* - in NEW_LISTENER mode: the fd of the new listener
506509
*/
507510
static long seccomp_attach_filter(unsigned int flags,
508511
struct seccomp_filter *filter)
@@ -1258,6 +1261,16 @@ static long seccomp_set_mode_filter(unsigned int flags,
12581261
if (flags & ~SECCOMP_FILTER_FLAG_MASK)
12591262
return -EINVAL;
12601263

1264+
/*
1265+
* In the successful case, NEW_LISTENER returns the new listener fd.
1266+
* But in the failure case, TSYNC returns the thread that died. If you
1267+
* combine these two flags, there's no way to tell whether something
1268+
* succeeded or failed. So, let's disallow this combination.
1269+
*/
1270+
if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
1271+
(flags & SECCOMP_FILTER_FLAG_NEW_LISTENER))
1272+
return -EINVAL;
1273+
12611274
/* Prepare the new filter before holding any locks. */
12621275
prepared = seccomp_prepare_user_filter(filter);
12631276
if (IS_ERR(prepared))
@@ -1304,7 +1317,7 @@ static long seccomp_set_mode_filter(unsigned int flags,
13041317
mutex_unlock(&current->signal->cred_guard_mutex);
13051318
out_put_fd:
13061319
if (flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) {
1307-
if (ret < 0) {
1320+
if (ret) {
13081321
listener_f->private_data = NULL;
13091322
fput(listener_f);
13101323
put_unused_fd(listener);

0 commit comments

Comments
 (0)