Skip to content

Commit 5189149

Browse files
tych0kees
authored andcommitted
seccomp: allow TSYNC and USER_NOTIF together
The restriction introduced in 7a0df7f ("seccomp: Make NEW_LISTENER and TSYNC flags exclusive") is mostly artificial: there is enough information in a seccomp user notification to tell which thread triggered a notification. The reason it was introduced is because TSYNC makes the syscall return a thread-id on failure, and NEW_LISTENER returns an fd, and there's no way to distinguish between these two cases (well, I suppose the caller could check all fds it has, then do the syscall, and if the return value was an fd that already existed, then it must be a thread id, but bleh). Matthew would like to use these two flags together in the Chrome sandbox which wants to use TSYNC for video drivers and NEW_LISTENER to proxy syscalls. So, let's fix this ugliness by adding another flag, TSYNC_ESRCH, which tells the kernel to just return -ESRCH on a TSYNC error. This way, NEW_LISTENER (and any subsequent seccomp() commands that want to return positive values) don't conflict with each other. Suggested-by: Matthew Denton <mpdenton@google.com> Signed-off-by: Tycho Andersen <tycho@tycho.ws> Link: https://lore.kernel.org/r/20200304180517.23867-1-tycho@tycho.ws Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 11a48a5 commit 5189149

File tree

4 files changed

+86
-6
lines changed

4 files changed

+86
-6
lines changed

include/linux/seccomp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#define SECCOMP_FILTER_FLAG_MASK (SECCOMP_FILTER_FLAG_TSYNC | \
88
SECCOMP_FILTER_FLAG_LOG | \
99
SECCOMP_FILTER_FLAG_SPEC_ALLOW | \
10-
SECCOMP_FILTER_FLAG_NEW_LISTENER)
10+
SECCOMP_FILTER_FLAG_NEW_LISTENER | \
11+
SECCOMP_FILTER_FLAG_TSYNC_ESRCH)
1112

1213
#ifdef CONFIG_SECCOMP
1314

include/uapi/linux/seccomp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define SECCOMP_FILTER_FLAG_LOG (1UL << 1)
2323
#define SECCOMP_FILTER_FLAG_SPEC_ALLOW (1UL << 2)
2424
#define SECCOMP_FILTER_FLAG_NEW_LISTENER (1UL << 3)
25+
#define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4)
2526

2627
/*
2728
* All BPF programs must return a 32-bit value.

kernel/seccomp.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,12 @@ static long seccomp_attach_filter(unsigned int flags,
528528
int ret;
529529

530530
ret = seccomp_can_sync_threads();
531-
if (ret)
532-
return ret;
531+
if (ret) {
532+
if (flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH)
533+
return -ESRCH;
534+
else
535+
return ret;
536+
}
533537
}
534538

535539
/* Set log flag, if present. */
@@ -1288,10 +1292,12 @@ static long seccomp_set_mode_filter(unsigned int flags,
12881292
* In the successful case, NEW_LISTENER returns the new listener fd.
12891293
* But in the failure case, TSYNC returns the thread that died. If you
12901294
* combine these two flags, there's no way to tell whether something
1291-
* succeeded or failed. So, let's disallow this combination.
1295+
* succeeded or failed. So, let's disallow this combination if the user
1296+
* has not explicitly requested no errors from TSYNC.
12921297
*/
12931298
if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
1294-
(flags & SECCOMP_FILTER_FLAG_NEW_LISTENER))
1299+
(flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) &&
1300+
((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0))
12951301
return -EINVAL;
12961302

12971303
/* Prepare the new filter before holding any locks. */

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ struct seccomp_notif_sizes {
212212
#define SECCOMP_USER_NOTIF_FLAG_CONTINUE 0x00000001
213213
#endif
214214

215+
#ifndef SECCOMP_FILTER_FLAG_TSYNC_ESRCH
216+
#define SECCOMP_FILTER_FLAG_TSYNC_ESRCH (1UL << 4)
217+
#endif
218+
215219
#ifndef seccomp
216220
int seccomp(unsigned int op, unsigned int flags, void *args)
217221
{
@@ -2187,7 +2191,8 @@ TEST(detect_seccomp_filter_flags)
21872191
unsigned int flags[] = { SECCOMP_FILTER_FLAG_TSYNC,
21882192
SECCOMP_FILTER_FLAG_LOG,
21892193
SECCOMP_FILTER_FLAG_SPEC_ALLOW,
2190-
SECCOMP_FILTER_FLAG_NEW_LISTENER };
2194+
SECCOMP_FILTER_FLAG_NEW_LISTENER,
2195+
SECCOMP_FILTER_FLAG_TSYNC_ESRCH };
21912196
unsigned int exclusive[] = {
21922197
SECCOMP_FILTER_FLAG_TSYNC,
21932198
SECCOMP_FILTER_FLAG_NEW_LISTENER };
@@ -2645,6 +2650,55 @@ TEST_F(TSYNC, two_siblings_with_one_divergence)
26452650
EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
26462651
}
26472652

2653+
TEST_F(TSYNC, two_siblings_with_one_divergence_no_tid_in_err)
2654+
{
2655+
long ret, flags;
2656+
void *status;
2657+
2658+
ASSERT_EQ(0, prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
2659+
TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
2660+
}
2661+
2662+
ret = seccomp(SECCOMP_SET_MODE_FILTER, 0, &self->root_prog);
2663+
ASSERT_NE(ENOSYS, errno) {
2664+
TH_LOG("Kernel does not support seccomp syscall!");
2665+
}
2666+
ASSERT_EQ(0, ret) {
2667+
TH_LOG("Kernel does not support SECCOMP_SET_MODE_FILTER!");
2668+
}
2669+
self->sibling[0].diverge = 1;
2670+
tsync_start_sibling(&self->sibling[0]);
2671+
tsync_start_sibling(&self->sibling[1]);
2672+
2673+
while (self->sibling_count < TSYNC_SIBLINGS) {
2674+
sem_wait(&self->started);
2675+
self->sibling_count++;
2676+
}
2677+
2678+
flags = SECCOMP_FILTER_FLAG_TSYNC | \
2679+
SECCOMP_FILTER_FLAG_TSYNC_ESRCH;
2680+
ret = seccomp(SECCOMP_SET_MODE_FILTER, flags, &self->apply_prog);
2681+
ASSERT_EQ(ESRCH, errno) {
2682+
TH_LOG("Did not return ESRCH for diverged sibling.");
2683+
}
2684+
ASSERT_EQ(-1, ret) {
2685+
TH_LOG("Did not fail on diverged sibling.");
2686+
}
2687+
2688+
/* Wake the threads */
2689+
pthread_mutex_lock(&self->mutex);
2690+
ASSERT_EQ(0, pthread_cond_broadcast(&self->cond)) {
2691+
TH_LOG("cond broadcast non-zero");
2692+
}
2693+
pthread_mutex_unlock(&self->mutex);
2694+
2695+
/* Ensure they are both unkilled. */
2696+
PTHREAD_JOIN(self->sibling[0].tid, &status);
2697+
EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2698+
PTHREAD_JOIN(self->sibling[1].tid, &status);
2699+
EXPECT_EQ(SIBLING_EXIT_UNKILLED, (long)status);
2700+
}
2701+
26482702
TEST_F(TSYNC, two_siblings_not_under_filter)
26492703
{
26502704
long ret, sib;
@@ -3196,6 +3250,24 @@ TEST(user_notification_basic)
31963250
EXPECT_EQ(0, WEXITSTATUS(status));
31973251
}
31983252

3253+
TEST(user_notification_with_tsync)
3254+
{
3255+
int ret;
3256+
unsigned int flags;
3257+
3258+
/* these were exclusive */
3259+
flags = SECCOMP_FILTER_FLAG_NEW_LISTENER |
3260+
SECCOMP_FILTER_FLAG_TSYNC;
3261+
ASSERT_EQ(-1, user_trap_syscall(__NR_getppid, flags));
3262+
ASSERT_EQ(EINVAL, errno);
3263+
3264+
/* but now they're not */
3265+
flags |= SECCOMP_FILTER_FLAG_TSYNC_ESRCH;
3266+
ret = user_trap_syscall(__NR_getppid, flags);
3267+
close(ret);
3268+
ASSERT_LE(0, ret);
3269+
}
3270+
31993271
TEST(user_notification_kill_in_middle)
32003272
{
32013273
pid_t pid;

0 commit comments

Comments
 (0)