Skip to content

Commit e540ad9

Browse files
ratakees
authored andcommitted
selftests/seccomp: Add test for atomic addfd+send
This just adds a test to verify that when using the new introduced flag to ADDFD, a valid fd is added and returned as the syscall result. Signed-off-by: Rodrigo Campos <[email protected]> Signed-off-by: Sargun Dhillon <[email protected]> Acked-by: Tycho Andersen <[email protected]> Acked-by: Christian Brauner <[email protected]> Signed-off-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0ae71c7 commit e540ad9

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tools/testing/selftests/seccomp/seccomp_bpf.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ struct seccomp_notif_addfd {
235235
};
236236
#endif
237237

238+
#ifndef SECCOMP_ADDFD_FLAG_SEND
239+
#define SECCOMP_ADDFD_FLAG_SEND (1UL << 1) /* Addfd and return it, atomically */
240+
#endif
241+
238242
struct seccomp_notif_addfd_small {
239243
__u64 id;
240244
char weird[4];
@@ -3976,8 +3980,14 @@ TEST(user_notification_addfd)
39763980
ASSERT_GE(pid, 0);
39773981

39783982
if (pid == 0) {
3983+
/* fds will be added and this value is expected */
39793984
if (syscall(__NR_getppid) != USER_NOTIF_MAGIC)
39803985
exit(1);
3986+
3987+
/* Atomic addfd+send is received here. Check it is a valid fd */
3988+
if (fcntl(syscall(__NR_getppid), F_GETFD) == -1)
3989+
exit(1);
3990+
39813991
exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC);
39823992
}
39833993

@@ -4056,6 +4066,30 @@ TEST(user_notification_addfd)
40564066
ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
40574067
ASSERT_EQ(addfd.id, req.id);
40584068

4069+
/* Verify we can do an atomic addfd and send */
4070+
addfd.newfd = 0;
4071+
addfd.flags = SECCOMP_ADDFD_FLAG_SEND;
4072+
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+
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);
4077+
4078+
/*
4079+
* This sets the ID of the ADD FD to the last request plus 1. The
4080+
* notification ID increments 1 per notification.
4081+
*/
4082+
addfd.id = req.id + 1;
4083+
4084+
/* This spins until the underlying notification is generated */
4085+
while (ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd) != -1 &&
4086+
errno != -EINPROGRESS)
4087+
nanosleep(&delay, NULL);
4088+
4089+
memset(&req, 0, sizeof(req));
4090+
ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
4091+
ASSERT_EQ(addfd.id, req.id);
4092+
40594093
resp.id = req.id;
40604094
resp.error = 0;
40614095
resp.val = USER_NOTIF_MAGIC;
@@ -4116,6 +4150,10 @@ TEST(user_notification_addfd_rlimit)
41164150
EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1);
41174151
EXPECT_EQ(errno, EMFILE);
41184152

4153+
addfd.flags = SECCOMP_ADDFD_FLAG_SEND;
4154+
EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1);
4155+
EXPECT_EQ(errno, EMFILE);
4156+
41194157
addfd.newfd = 100;
41204158
addfd.flags = SECCOMP_ADDFD_FLAG_SETFD;
41214159
EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd), -1);

0 commit comments

Comments
 (0)