Skip to content

Commit 93e720d

Browse files
committed
selftests/seccomp: More closely track fds being assigned
Since the open fds might not always start at "4" (especially when running under kselftest, etc), start counting from the first assigned fd, rather than using the more permissive EXPECT_GE(fd, 0). Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/lkml/[email protected] Reviewed-by: Rodrigo Campos <[email protected]> Acked-by: Christian Brauner <[email protected]>
1 parent e540ad9 commit 93e720d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3954,7 +3954,7 @@ TEST(user_notification_addfd)
39543954
{
39553955
pid_t pid;
39563956
long ret;
3957-
int status, listener, memfd, fd;
3957+
int status, listener, memfd, fd, nextfd;
39583958
struct seccomp_notif_addfd addfd = {};
39593959
struct seccomp_notif_addfd_small small = {};
39603960
struct seccomp_notif_addfd_big big = {};
@@ -3963,18 +3963,21 @@ TEST(user_notification_addfd)
39633963
/* 100 ms */
39643964
struct timespec delay = { .tv_nsec = 100000000 };
39653965

3966+
/* There may be arbitrary already-open fds at test start. */
39663967
memfd = memfd_create("test", 0);
39673968
ASSERT_GE(memfd, 0);
3969+
nextfd = memfd + 1;
39683970

39693971
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
39703972
ASSERT_EQ(0, ret) {
39713973
TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
39723974
}
39733975

3976+
/* fd: 4 */
39743977
/* Check that the basic notification machinery works */
39753978
listener = user_notif_syscall(__NR_getppid,
39763979
SECCOMP_FILTER_FLAG_NEW_LISTENER);
3977-
ASSERT_GE(listener, 0);
3980+
ASSERT_EQ(listener, nextfd++);
39783981

39793982
pid = fork();
39803983
ASSERT_GE(pid, 0);
@@ -4029,14 +4032,14 @@ TEST(user_notification_addfd)
40294032

40304033
/* Verify we can set an arbitrary remote fd */
40314034
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
4032-
EXPECT_GE(fd, 0);
4035+
EXPECT_EQ(fd, nextfd++);
40334036
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
40344037

40354038
/* Verify we can set an arbitrary remote fd with large size */
40364039
memset(&big, 0x0, sizeof(big));
40374040
big.addfd = addfd;
40384041
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big);
4039-
EXPECT_GE(fd, 0);
4042+
EXPECT_EQ(fd, nextfd++);
40404043

40414044
/* Verify we can set a specific remote fd */
40424045
addfd.newfd = 42;
@@ -4070,9 +4073,11 @@ TEST(user_notification_addfd)
40704073
addfd.newfd = 0;
40714074
addfd.flags = SECCOMP_ADDFD_FLAG_SEND;
40724075
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
4073-
4074-
/* Child has fds 0-6 and 42 used, we expect the lower fd available: 7 */
4075-
EXPECT_EQ(fd, 7);
4076+
/*
4077+
* Child has earlier "low" fds and now 42, so we expect the next
4078+
* lowest available fd to be assigned here.
4079+
*/
4080+
EXPECT_EQ(fd, nextfd++);
40764081
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
40774082

40784083
/*

0 commit comments

Comments
 (0)