Skip to content

Commit 83a5084

Browse files
committed
Merge tag 'seccomp-v5.1-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull seccomp fixes from Kees Cook: "Syzbot found a use-after-free bug in seccomp due to flags that should not be allowed to be used together. Tycho fixed this, I updated the self-tests, and the syzkaller PoC has been running for several days without triggering KASan (before this fix, it would reproduce). These patches have also been in -next for almost a week, just to be sure. - Add logic for making some seccomp flags exclusive (Tycho) - Update selftests for exclusivity testing (Kees)" * tag 'seccomp-v5.1-rc8' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: seccomp: Make NEW_LISTENER and TSYNC flags exclusive selftests/seccomp: Prepare for exclusive seccomp flags
2 parents 8087148 + 7a0df7f commit 83a5084

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

‎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);

‎tools/testing/selftests/seccomp/seccomp_bpf.c‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,11 +2166,14 @@ TEST(detect_seccomp_filter_flags)
21662166
SECCOMP_FILTER_FLAG_LOG,
21672167
SECCOMP_FILTER_FLAG_SPEC_ALLOW,
21682168
SECCOMP_FILTER_FLAG_NEW_LISTENER };
2169-
unsigned int flag, all_flags;
2169+
unsigned int exclusive[] = {
2170+
SECCOMP_FILTER_FLAG_TSYNC,
2171+
SECCOMP_FILTER_FLAG_NEW_LISTENER };
2172+
unsigned int flag, all_flags, exclusive_mask;
21702173
int i;
21712174
long ret;
21722175

2173-
/* Test detection of known-good filter flags */
2176+
/* Test detection of individual known-good filter flags */
21742177
for (i = 0, all_flags = 0; i < ARRAY_SIZE(flags); i++) {
21752178
int bits = 0;
21762179

@@ -2197,16 +2200,29 @@ TEST(detect_seccomp_filter_flags)
21972200
all_flags |= flag;
21982201
}
21992202

2200-
/* Test detection of all known-good filter flags */
2201-
ret = seccomp(SECCOMP_SET_MODE_FILTER, all_flags, NULL);
2202-
EXPECT_EQ(-1, ret);
2203-
EXPECT_EQ(EFAULT, errno) {
2204-
TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2205-
all_flags);
2203+
/*
2204+
* Test detection of all known-good filter flags combined. But
2205+
* for the exclusive flags we need to mask them out and try them
2206+
* individually for the "all flags" testing.
2207+
*/
2208+
exclusive_mask = 0;
2209+
for (i = 0; i < ARRAY_SIZE(exclusive); i++)
2210+
exclusive_mask |= exclusive[i];
2211+
for (i = 0; i < ARRAY_SIZE(exclusive); i++) {
2212+
flag = all_flags & ~exclusive_mask;
2213+
flag |= exclusive[i];
2214+
2215+
ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL);
2216+
EXPECT_EQ(-1, ret);
2217+
EXPECT_EQ(EFAULT, errno) {
2218+
TH_LOG("Failed to detect that all known-good filter flags (0x%X) are supported!",
2219+
flag);
2220+
}
22062221
}
22072222

2208-
/* Test detection of an unknown filter flag */
2223+
/* Test detection of an unknown filter flags, without exclusives. */
22092224
flag = -1;
2225+
flag &= ~exclusive_mask;
22102226
ret = seccomp(SECCOMP_SET_MODE_FILTER, flag, NULL);
22112227
EXPECT_EQ(-1, ret);
22122228
EXPECT_EQ(EINVAL, errno) {

0 commit comments

Comments
 (0)