Skip to content

Commit 78b0ded

Browse files
committed
Merge tag 'seccomp-v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull seccomp updates from Kees Cook: "A couple of seccomp updates. They're both mostly bug fixes that I wanted to have sit in linux-next for a while: - allow TSYNC and USER_NOTIF together (Tycho Andersen) - add missing compat_ioctl for notify (Sven Schnelle)" * tag 'seccomp-v5.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: seccomp: Add missing compat_ioctl for notify seccomp: allow TSYNC and USER_NOTIF together
2 parents 377ad0c + 3db81af commit 78b0ded

File tree

4 files changed

+87
-6
lines changed

4 files changed

+87
-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: 11 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. */
@@ -1221,6 +1225,7 @@ static const struct file_operations seccomp_notify_ops = {
12211225
.poll = seccomp_notify_poll,
12221226
.release = seccomp_notify_release,
12231227
.unlocked_ioctl = seccomp_notify_ioctl,
1228+
.compat_ioctl = seccomp_notify_ioctl,
12241229
};
12251230

12261231
static struct file *init_listener(struct seccomp_filter *filter)
@@ -1288,10 +1293,12 @@ static long seccomp_set_mode_filter(unsigned int flags,
12881293
* In the successful case, NEW_LISTENER returns the new listener fd.
12891294
* But in the failure case, TSYNC returns the thread that died. If you
12901295
* combine these two flags, there's no way to tell whether something
1291-
* succeeded or failed. So, let's disallow this combination.
1296+
* succeeded or failed. So, let's disallow this combination if the user
1297+
* has not explicitly requested no errors from TSYNC.
12921298
*/
12931299
if ((flags & SECCOMP_FILTER_FLAG_TSYNC) &&
1294-
(flags & SECCOMP_FILTER_FLAG_NEW_LISTENER))
1300+
(flags & SECCOMP_FILTER_FLAG_NEW_LISTENER) &&
1301+
((flags & SECCOMP_FILTER_FLAG_TSYNC_ESRCH) == 0))
12951302
return -EINVAL;
12961303

12971304
/* 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)