Skip to content

Commit 86ed652

Browse files
Terry TrittonMa Wupeng
authored andcommitted
selftests/seccomp: user_notification_addfd check nextfd is available
stable inclusion from stable-v6.6.30 commit 003af8c23fca24ec6ed5e6cd7a8176b1d27c7357 bugzilla: https://gitee.com/openeuler/kernel/issues/I9MPZ8 Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=003af8c23fca24ec6ed5e6cd7a8176b1d27c7357 -------------------------------- commit 8e3c9f9f3a0742cd12b682a1766674253b33fcf0 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> Signed-off-by: ZhangPeng <zhangpeng362@huawei.com>
1 parent f1f4d8b commit 86ed652

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
@@ -4050,6 +4050,16 @@ TEST(user_notification_filter_empty_threaded)
40504050
EXPECT_GT((pollfd.revents & POLLHUP) ?: 0, 0);
40514051
}
40524052

4053+
4054+
int get_next_fd(int prev_fd)
4055+
{
4056+
for (int i = prev_fd + 1; i < FD_SETSIZE; ++i) {
4057+
if (fcntl(i, F_GETFD) == -1)
4058+
return i;
4059+
}
4060+
_exit(EXIT_FAILURE);
4061+
}
4062+
40534063
TEST(user_notification_addfd)
40544064
{
40554065
pid_t pid;
@@ -4066,7 +4076,7 @@ TEST(user_notification_addfd)
40664076
/* There may be arbitrary already-open fds at test start. */
40674077
memfd = memfd_create("test", 0);
40684078
ASSERT_GE(memfd, 0);
4069-
nextfd = memfd + 1;
4079+
nextfd = get_next_fd(memfd);
40704080

40714081
ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
40724082
ASSERT_EQ(0, ret) {
@@ -4077,7 +4087,8 @@ TEST(user_notification_addfd)
40774087
/* Check that the basic notification machinery works */
40784088
listener = user_notif_syscall(__NR_getppid,
40794089
SECCOMP_FILTER_FLAG_NEW_LISTENER);
4080-
ASSERT_EQ(listener, nextfd++);
4090+
ASSERT_EQ(listener, nextfd);
4091+
nextfd = get_next_fd(nextfd);
40814092

40824093
pid = fork();
40834094
ASSERT_GE(pid, 0);
@@ -4132,14 +4143,16 @@ TEST(user_notification_addfd)
41324143

41334144
/* Verify we can set an arbitrary remote fd */
41344145
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
4135-
EXPECT_EQ(fd, nextfd++);
4146+
EXPECT_EQ(fd, nextfd);
4147+
nextfd = get_next_fd(nextfd);
41364148
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
41374149

41384150
/* Verify we can set an arbitrary remote fd with large size */
41394151
memset(&big, 0x0, sizeof(big));
41404152
big.addfd = addfd;
41414153
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD_BIG, &big);
4142-
EXPECT_EQ(fd, nextfd++);
4154+
EXPECT_EQ(fd, nextfd);
4155+
nextfd = get_next_fd(nextfd);
41434156

41444157
/* Verify we can set a specific remote fd */
41454158
addfd.newfd = 42;
@@ -4177,7 +4190,8 @@ TEST(user_notification_addfd)
41774190
* Child has earlier "low" fds and now 42, so we expect the next
41784191
* lowest available fd to be assigned here.
41794192
*/
4180-
EXPECT_EQ(fd, nextfd++);
4193+
EXPECT_EQ(fd, nextfd);
4194+
nextfd = get_next_fd(nextfd);
41814195
ASSERT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
41824196

41834197
/*

0 commit comments

Comments
 (0)