@@ -3612,6 +3618,110 @@ TEST(user_notification_continue)
}
}
+static int sys_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
+ unsigned int flags)
+{
+#ifdef __NR_pidfd_send_signal
+ return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
+#else
+ errno = ENOSYS;
+ return -1;
+#endif
+}
+
+TEST(user_notification_pidfd)
+{
+ struct seccomp_notif req = {
+ .flags = SECCOMP_USER_NOTIF_FLAG_PIDFD,
+ };
+ struct seccomp_notif_resp resp = {};
+ int ret, listener, status;
+ pid_t pid;
+
+
+ ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+ ASSERT_EQ(0, ret) {
+ TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
+ }
+
+ listener = user_trap_syscall(__NR_getppid,
+ SECCOMP_FILTER_FLAG_NEW_LISTENER);
+ ASSERT_GE(listener, 0);
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0) {
+ /* the process should be killed during this syscall */
+ syscall(__NR_getppid);
+ exit(0);
+ }
+
+ EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
+ ASSERT_GE(req.pidfd, 0);
+
+ ASSERT_EQ(sys_pidfd_send_signal(req.pidfd, SIGKILL, NULL, 0), 0) {
+ XFAIL(goto out,
+ "Kernel does not support pidfd_send_signal() syscall");
+ goto out;
+ }
+ EXPECT_EQ(req.pid, pid);
+
+ EXPECT_EQ(waitpid(pid, &status, 0), pid);
+ EXPECT_EQ(true, WIFSIGNALED(status));
+ EXPECT_EQ(SIGKILL, WTERMSIG(status));
+
+out:
+ close(req.pidfd);
+ close(listener);
+}
+
+TEST(user_notification_pidfd_fault)
+{
+ struct seccomp_notif req = {
+ .flags = SECCOMP_USER_NOTIF_FLAG_PIDFD,
+ };
+ struct seccomp_notif_resp resp = {};
+ int ret, listener, status;
+ pid_t pid;
+
+ ret = prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
+ ASSERT_EQ(0, ret) {
+ TH_LOG("Kernel does not support PR_SET_NO_NEW_PRIVS!");
+ }
+
+ listener = user_trap_syscall(__NR_getppid,
+ SECCOMP_FILTER_FLAG_NEW_LISTENER);
+ ASSERT_GE(listener, 0);
+
+ pid = fork();
+ ASSERT_GE(pid, 0);
+
+ if (pid == 0)
+ exit(syscall(__NR_getppid) != USER_NOTIF_MAGIC);
+
+ /* trigger an EFAULT */
+ EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, NULL), -1);
+ EXPECT_EQ(errno, EFAULT);
+
+ /* Check that we can still fetch it. */
+ EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_RECV, &req), 0);
+ EXPECT_EQ(req.pid, pid);
+
+ resp.id = req.id;
+ resp.error = 0;
+ resp.val = USER_NOTIF_MAGIC;
+
+ EXPECT_EQ(ioctl(listener, SECCOMP_IOCTL_NOTIF_SEND, &resp), 0);
+
+ EXPECT_EQ(waitpid(pid, &status, 0), pid);
+ EXPECT_EQ(true, WIFEXITED(status));
+ EXPECT_EQ(0, WEXITSTATUS(status));
+
+ close(req.pidfd);
+ close(listener);
+}
+
/*
* TODO:
* - add microbenchmarks