Skip to content

Commit 8e3c9f9

Browse files
Terry Trittonkees
authored andcommitted
selftests/seccomp: user_notification_addfd check nextfd is available
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 <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 471dbc5 commit 8e3c9f9

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
@@ -4044,6 +4044,16 @@ TEST(user_notification_filter_empty_threaded)
40444044
EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0);
40454045
}
40464046

4047+
4048+
int get_next_fd(int prev_fd)
4049+
{
4050+
for (int i = prev_fd + 1; i < FD_SETSIZE; ++i) {
4051+
if (fcntl(i, F_GETFD) == -1)
4052+
return i;
4053+
}
4054+
_exit(EXIT_FAILURE);
4055+
}
4056+
40474057
TEST(user_notification_addfd)
40484058
{
40494059
pid_t pid;
@@ -4060,7 +4070,7 @@ TEST(user_notification_addfd)
40604070
/* There may be arbitrary already-open fds at test start. */
40614071
memfd = memfd_create("test", 0);
40624072
ASSERT_GE(memfd, 0);
4063-
nextfd = memfd + 1;
4073+
nextfd = get_next_fd(memfd);
40644074

40654075
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
40664076
ASSERT_EQ(0, ret) {
@@ -4071,7 +4081,8 @@ TEST(user_notification_addfd)
40714081
/* Check that the basic notification machinery works */
40724082
listener = user_notif_syscall(__NR_getppid,
40734083
SECCOMP_FILTER_FLAG_NEW_LISTENER);
4074-
ASSERT_EQ(listener, nextfd++);
4084+
ASSERT_EQ(listener, nextfd);
4085+
nextfd = get_next_fd(nextfd);
40754086

40764087
pid = fork();
40774088
ASSERT_GE(pid, 0);
@@ -4126,14 +4137,16 @@ TEST(user_notification_addfd)
41264137

41274138
/* Verify we can set an arbitrary remote fd */
41284139
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
4129-
EXPECT_EQ(fd, nextfd++);
4140+
EXPECT_EQ(fd, nextfd);
4141+
nextfd = get_next_fd(nextfd);
41304142
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
41314143

41324144
/* Verify we can set an arbitrary remote fd with large size */
41334145
memset(&big, 0x0, sizeof(big));
41344146
big.addfd = addfd;
41354147
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big);
4136-
EXPECT_EQ(fd, nextfd++);
4148+
EXPECT_EQ(fd, nextfd);
4149+
nextfd = get_next_fd(nextfd);
41374150

41384151
/* Verify we can set a specific remote fd */
41394152
addfd.newfd = 42;
@@ -4171,7 +4184,8 @@ TEST(user_notification_addfd)
41714184
* Child has earlier "low" fds and now 42, so we expect the next
41724185
* lowest available fd to be assigned here.
41734186
*/
4174-
EXPECT_EQ(fd, nextfd++);
4187+
EXPECT_EQ(fd, nextfd);
4188+
nextfd = get_next_fd(nextfd);
41754189
ASSERT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
41764190

41774191
/*

0 commit comments

Comments
 (0)