Skip to content

Commit

Permalink
selftests/seccomp: Add test for atomic addfd+send
Browse files Browse the repository at this point in the history
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 <rodrigo@sdfg.com.ar>
  • Loading branch information
rata committed Jan 14, 2021
1 parent 2abae15 commit bd9996f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tools/testing/selftests/seccomp/seccomp_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ struct seccomp_notif_addfd {
};
#endif

#ifndef SECCOMP_ADDFD_FLAG_SEND
#define SECCOMP_ADDFD_FLAG_SEND (1UL << 1) /* Addfd and return it, atomically */
#endif

struct seccomp_notif_addfd_small {
__u64 id;
char weird[4];
Expand Down Expand Up @@ -3976,8 +3980,14 @@ TEST(user_notification_addfd)
ASSERT_GE(pid, 0);

if (pid == 0) {
/* fds will be added and this value is expected */
if (syscall(__NR_getppid) != USER_NOTIF_MAGIC)
exit(1);

/* Atomic addfd+send is received here. Check it is a valid fd */
if (fcntl(syscall(__NR_getppid), F_GETFD) == -1)
exit(1);

exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC);
}

Expand Down Expand Up @@ -4060,6 +4070,27 @@ TEST(user_notification_addfd)
ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
ASSERT_EQ(addfd.id, req.id);

/* Verify we can do an atomic addfd and send */
addfd.newfd = 0;
addfd.flags = SECCOMP_ADDFD_FLAG_SEND;
fd = ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd);
EXPECT_EQ(filecmp(getpid(), pid, memfd, fd), 0);

/*
* This sets the ID of the ADD FD to the last request plus 1. The
* notification ID increments 1 per notification.
*/
addfd.id = req.id + 1;

/* This spins until the underlying notification is generated */
while (ioctl(listener, SECCOMP_IOCTL_NOTIF_ADDFD, &addfd) != -1 &&
errno != -EINPROGRESS)
nanosleep(&delay, NULL);

memset(&req, 0, sizeof(req));
ASSERT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
ASSERT_EQ(addfd.id, req.id);

resp.id = req.id;
resp.error = 0;
resp.val = USER_NOTIF_MAGIC;
Expand Down

0 comments on commit bd9996f

Please sign in to comment.