seccomp: don't leak memory when filter install races
authorTycho Andersen <tycho@tycho.pizza>
Wed, 2 Sep 2020 01:40:16 +0000 (19:40 -0600)
committerKees Cook <keescook@chromium.org>
Tue, 8 Sep 2020 18:19:50 +0000 (11:19 -0700)
commita566a9012acd7c9a4be7e30dc7acb7a811ec2260
treeeaf1faa6cece4d4ba2d24192d6bf9cf633f47a44
parentd012a7190fc1fd72ed48911e77ca97ba4521bccd
seccomp: don't leak memory when filter install races

In seccomp_set_mode_filter() with TSYNC | NEW_LISTENER, we first initialize
the listener fd, then check to see if we can actually use it later in
seccomp_may_assign_mode(), which can fail if anyone else in our thread
group has installed a filter and caused some divergence. If we can't, we
partially clean up the newly allocated file: we put the fd, put the file,
but don't actually clean up the *memory* that was allocated at
filter->notif. Let's clean that up too.

To accomplish this, let's hoist the actual "detach a notifier from a
filter" code to its own helper out of seccomp_notify_release(), so that in
case anyone adds stuff to init_listener(), they only have to add the
cleanup code in one spot. This does a bit of extra locking and such on the
failure path when the filter is not attached, but it's a slow failure path
anyway.

Fixes: 51891498f2da ("seccomp: allow TSYNC and USER_NOTIF together")
Reported-by: syzbot+3ad9614a12f80994c32e@syzkaller.appspotmail.com
Signed-off-by: Tycho Andersen <tycho@tycho.pizza>
Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
Link: https://lore.kernel.org/r/20200902014017.934315-1-tycho@tycho.pizza
Signed-off-by: Kees Cook <keescook@chromium.org>
kernel/seccomp.c