1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/types.h>
15 #include <sys/prctl.h>
18 #include <sys/socket.h>
19 #include <linux/kcmp.h>
22 #include "../kselftest_harness.h"
25 * UNKNOWN_FD is an fd number that should never exist in the child, as it is
26 * used to check the negative case.
28 #define UNKNOWN_FD 111
29 #define UID_NOBODY 65535
31 static int sys_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1,
34 return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
37 static int __child(int sk, int memfd)
43 * Ensure we don't leave around a bunch of orphaned children if our
46 ret = prctl(PR_SET_PDEATHSIG, SIGKILL);
48 fprintf(stderr, "%s: Child could not set DEATHSIG\n",
53 ret = send(sk, &memfd, sizeof(memfd), 0);
54 if (ret != sizeof(memfd)) {
55 fprintf(stderr, "%s: Child failed to send fd number\n",
61 * The fixture setup is completed at this point. The tests will run.
63 * This blocking recv enables the parent to message the child.
64 * Either we will read 'P' off of the sk, indicating that we need
65 * to disable ptrace, or we will read a 0, indicating that the other
66 * side has closed the sk. This occurs during fixture teardown time,
67 * indicating that the child should exit.
69 while ((ret = recv(sk, &buf, sizeof(buf), 0)) > 0) {
71 ret = prctl(PR_SET_DUMPABLE, 0);
74 "%s: Child failed to disable ptrace\n",
79 fprintf(stderr, "Child received unknown command %c\n",
83 ret = send(sk, &buf, sizeof(buf), 0);
85 fprintf(stderr, "%s: Child failed to ack\n",
91 fprintf(stderr, "%s: Child failed to read from socket\n",
99 static int child(int sk)
103 memfd = sys_memfd_create("test", 0);
105 fprintf(stderr, "%s: Child could not create memfd\n",
109 ret = __child(sk, memfd);
120 * remote_fd is the number of the FD which we are trying to retrieve
124 /* pid points to the child which we are fetching FDs from */
126 /* pidfd is the pidfd of the child */
129 * sk is our side of the socketpair used to communicate with the child.
130 * When it is closed, the child will exit.
133 bool ignore_child_result;
140 ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) {
141 TH_LOG("%s: failed to create socketpair", strerror(errno));
143 self->sk = sk_pair[0];
146 ASSERT_GE(self->pid, 0);
148 if (self->pid == 0) {
150 if (child(sk_pair[1]))
157 self->pidfd = sys_pidfd_open(self->pid, 0);
158 ASSERT_GE(self->pidfd, 0);
161 * Wait for the child to complete setup. It'll send the remote memfd's
164 ret = recv(sk_pair[0], &self->remote_fd, sizeof(self->remote_fd), 0);
165 ASSERT_EQ(sizeof(self->remote_fd), ret);
168 FIXTURE_TEARDOWN(child)
172 EXPECT_EQ(0, close(self->pidfd));
173 EXPECT_EQ(0, close(self->sk));
175 ret = wait_for_pid(self->pid);
176 if (!self->ignore_child_result)
180 TEST_F(child, disable_ptrace)
186 * Turn into nobody if we're root, to avoid CAP_SYS_PTRACE
188 * The tests should run in their own process, so even this test fails,
189 * it shouldn't result in subsequent tests failing.
193 ASSERT_EQ(0, seteuid(UID_NOBODY));
195 ASSERT_EQ(1, send(self->sk, "P", 1, 0));
196 ASSERT_EQ(1, recv(self->sk, &c, 1, 0));
198 fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
200 EXPECT_EQ(EPERM, errno);
203 ASSERT_EQ(0, seteuid(0));
206 TEST_F(child, fetch_fd)
210 fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
213 ret = sys_kcmp(getpid(), self->pid, KCMP_FILE, fd, self->remote_fd);
214 if (ret < 0 && errno == ENOSYS)
215 SKIP(return, "kcmp() syscall not supported");
218 ret = fcntl(fd, F_GETFD);
220 EXPECT_GE(ret & FD_CLOEXEC, 0);
225 TEST_F(child, test_unknown_fd)
229 fd = sys_pidfd_getfd(self->pidfd, UNKNOWN_FD, 0);
231 TH_LOG("getfd succeeded while fetching unknown fd");
233 EXPECT_EQ(EBADF, errno) {
234 TH_LOG("%s: getfd did not get EBADF", strerror(errno));
240 ASSERT_EQ(-1, sys_pidfd_getfd(0, 0, 1));
241 EXPECT_EQ(errno, EINVAL);
244 TEST_F(child, no_strange_EBADF)
248 self->ignore_child_result = true;
250 fds.fd = self->pidfd;
253 ASSERT_EQ(kill(self->pid, SIGKILL), 0);
254 ASSERT_EQ(poll(&fds, 1, 5000), 1);
257 * It used to be that pidfd_getfd() could race with the exiting thread
258 * between exit_files() and release_task(), and get a non-null task
259 * with a NULL files struct, and you'd get EBADF, which was slightly
263 EXPECT_EQ(sys_pidfd_getfd(self->pidfd, self->remote_fd, 0), -1);
264 EXPECT_EQ(errno, ESRCH);
267 #if __NR_pidfd_getfd == -1
270 fprintf(stderr, "__NR_pidfd_getfd undefined. The pidfd_getfd syscall is unavailable. Test aborting\n");