Skip to content

Commit 8b7423a

Browse files
Terry TrittonSasha Levin
authored andcommitted
selftests/seccomp: user_notification_addfd check nextfd is available
commit 8e3c9f9 upstream. Currently the user_notification_addfd test checks what the next expected file descriptor will be by incrementing a variable nextfd. This does not account for file descriptors that may already be open before the test is started and will cause the test to fail if any exist. Replace nextfd++ with a function get_next_fd which will check and return the next available file descriptor. Signed-off-by: Terry Tritton <terry.tritton@linaro.org> Link: https://lore.kernel.org/r/20240124141357.1243457-4-terry.tritton@linaro.org Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f34d6b7 commit 8b7423a

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4037,6 +4037,16 @@ TEST(user_notification_filter_empty_threaded)
40374037
EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0);
40384038
}
40394039

4040+
4041+
int get_next_fd(int prev_fd)
4042+
{
4043+
for (int i = prev_fd + 1; i < FD_SETSIZE; ++i) {
4044+
if (fcntl(i, F_GETFD) == -1)
4045+
return i;
4046+
}
4047+
_exit(EXIT_FAILURE);
4048+
}
4049+
40404050
TEST(user_notification_addfd)
40414051
{
40424052
pid_t pid;
@@ -4053,7 +4063,7 @@ TEST(user_notification_addfd)
40534063
/* There may be arbitrary already-open fds at test start. */
40544064
memfd = memfd_create("test", 0);
40554065
ASSERT_GE(memfd, 0);
4056-
nextfd = memfd + 1;
4066+
nextfd = get_next_fd(memfd);
40574067

40584068
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
40594069
ASSERT_EQ(0, ret) {
@@ -4064,7 +4074,8 @@ TEST(user_notification_addfd)
40644074
/* Check that the basic notification machinery works */
40654075
listener = user_notif_syscall(__NR_getppid,
40664076
SECCOMP_FILTER_FLAG_NEW_LISTENER);
4067-
ASSERT_EQ(listener, nextfd++);
4077+
ASSERT_EQ(listener, nextfd);
4078+
nextfd = get_next_fd(nextfd);
40684079

40694080
pid = fork();
40704081
ASSERT_GE(pid, 0);
@@ -4119,14 +4130,16 @@ TEST(user_notification_addfd)
41194130

41204131
/* Verify we can set an arbitrary remote fd */
41214132
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
4122-
EXPECT_EQ(fd, nextfd++);
4133+
EXPECT_EQ(fd, nextfd);
4134+
nextfd = get_next_fd(nextfd);
41234135
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
41244136

41254137
/* Verify we can set an arbitrary remote fd with large size */
41264138
memset(&big, 0x0, sizeof(big));
41274139
big.addfd = addfd;
41284140
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big);
4129-
EXPECT_EQ(fd, nextfd++);
4141+
EXPECT_EQ(fd, nextfd);
4142+
nextfd = get_next_fd(nextfd);
41304143

41314144
/* Verify we can set a specific remote fd */
41324145
addfd.newfd = 42;
@@ -4164,7 +4177,8 @@ TEST(user_notification_addfd)
41644177
* Child has earlier "low" fds and now 42, so we expect the next
41654178
* lowest available fd to be assigned here.
41664179
*/
4167-
EXPECT_EQ(fd, nextfd++);
4180+
EXPECT_EQ(fd, nextfd);
4181+
nextfd = get_next_fd(nextfd);
41684182
ASSERT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
41694183

41704184
/*

0 commit comments

Comments
 (0)