1873dfd78SSargun Dhillon // SPDX-License-Identifier: GPL-2.0
2873dfd78SSargun Dhillon
3873dfd78SSargun Dhillon #define _GNU_SOURCE
4873dfd78SSargun Dhillon #include <errno.h>
5873dfd78SSargun Dhillon #include <fcntl.h>
6873dfd78SSargun Dhillon #include <limits.h>
7873dfd78SSargun Dhillon #include <linux/types.h>
8873dfd78SSargun Dhillon #include <sched.h>
9873dfd78SSargun Dhillon #include <signal.h>
10873dfd78SSargun Dhillon #include <stdio.h>
11873dfd78SSargun Dhillon #include <stdlib.h>
12873dfd78SSargun Dhillon #include <string.h>
13873dfd78SSargun Dhillon #include <syscall.h>
14873dfd78SSargun Dhillon #include <sys/prctl.h>
15873dfd78SSargun Dhillon #include <sys/wait.h>
16873dfd78SSargun Dhillon #include <unistd.h>
17873dfd78SSargun Dhillon #include <sys/socket.h>
18873dfd78SSargun Dhillon #include <linux/kcmp.h>
19873dfd78SSargun Dhillon
20873dfd78SSargun Dhillon #include "pidfd.h"
21873dfd78SSargun Dhillon #include "../kselftest_harness.h"
22873dfd78SSargun Dhillon
23873dfd78SSargun Dhillon /*
24873dfd78SSargun Dhillon * UNKNOWN_FD is an fd number that should never exist in the child, as it is
25873dfd78SSargun Dhillon * used to check the negative case.
26873dfd78SSargun Dhillon */
27873dfd78SSargun Dhillon #define UNKNOWN_FD 111
28873dfd78SSargun Dhillon #define UID_NOBODY 65535
29873dfd78SSargun Dhillon
sys_kcmp(pid_t pid1,pid_t pid2,int type,unsigned long idx1,unsigned long idx2)30873dfd78SSargun Dhillon static int sys_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1,
31873dfd78SSargun Dhillon unsigned long idx2)
32873dfd78SSargun Dhillon {
33873dfd78SSargun Dhillon return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
34873dfd78SSargun Dhillon }
35873dfd78SSargun Dhillon
__child(int sk,int memfd)36873dfd78SSargun Dhillon static int __child(int sk, int memfd)
37873dfd78SSargun Dhillon {
38873dfd78SSargun Dhillon int ret;
39873dfd78SSargun Dhillon char buf;
40873dfd78SSargun Dhillon
41873dfd78SSargun Dhillon /*
42873dfd78SSargun Dhillon * Ensure we don't leave around a bunch of orphaned children if our
43873dfd78SSargun Dhillon * tests fail.
44873dfd78SSargun Dhillon */
45873dfd78SSargun Dhillon ret = prctl(PR_SET_PDEATHSIG, SIGKILL);
46873dfd78SSargun Dhillon if (ret) {
47873dfd78SSargun Dhillon fprintf(stderr, "%s: Child could not set DEATHSIG\n",
48873dfd78SSargun Dhillon strerror(errno));
49873dfd78SSargun Dhillon return -1;
50873dfd78SSargun Dhillon }
51873dfd78SSargun Dhillon
52873dfd78SSargun Dhillon ret = send(sk, &memfd, sizeof(memfd), 0);
53873dfd78SSargun Dhillon if (ret != sizeof(memfd)) {
54873dfd78SSargun Dhillon fprintf(stderr, "%s: Child failed to send fd number\n",
55873dfd78SSargun Dhillon strerror(errno));
56873dfd78SSargun Dhillon return -1;
57873dfd78SSargun Dhillon }
58873dfd78SSargun Dhillon
59873dfd78SSargun Dhillon /*
60873dfd78SSargun Dhillon * The fixture setup is completed at this point. The tests will run.
61873dfd78SSargun Dhillon *
62873dfd78SSargun Dhillon * This blocking recv enables the parent to message the child.
63873dfd78SSargun Dhillon * Either we will read 'P' off of the sk, indicating that we need
64873dfd78SSargun Dhillon * to disable ptrace, or we will read a 0, indicating that the other
65873dfd78SSargun Dhillon * side has closed the sk. This occurs during fixture teardown time,
66873dfd78SSargun Dhillon * indicating that the child should exit.
67873dfd78SSargun Dhillon */
68873dfd78SSargun Dhillon while ((ret = recv(sk, &buf, sizeof(buf), 0)) > 0) {
69873dfd78SSargun Dhillon if (buf == 'P') {
70873dfd78SSargun Dhillon ret = prctl(PR_SET_DUMPABLE, 0);
71873dfd78SSargun Dhillon if (ret < 0) {
72873dfd78SSargun Dhillon fprintf(stderr,
73873dfd78SSargun Dhillon "%s: Child failed to disable ptrace\n",
74873dfd78SSargun Dhillon strerror(errno));
75873dfd78SSargun Dhillon return -1;
76873dfd78SSargun Dhillon }
77873dfd78SSargun Dhillon } else {
78873dfd78SSargun Dhillon fprintf(stderr, "Child received unknown command %c\n",
79873dfd78SSargun Dhillon buf);
80873dfd78SSargun Dhillon return -1;
81873dfd78SSargun Dhillon }
82873dfd78SSargun Dhillon ret = send(sk, &buf, sizeof(buf), 0);
83873dfd78SSargun Dhillon if (ret != 1) {
84873dfd78SSargun Dhillon fprintf(stderr, "%s: Child failed to ack\n",
85873dfd78SSargun Dhillon strerror(errno));
86873dfd78SSargun Dhillon return -1;
87873dfd78SSargun Dhillon }
88873dfd78SSargun Dhillon }
89873dfd78SSargun Dhillon if (ret < 0) {
90873dfd78SSargun Dhillon fprintf(stderr, "%s: Child failed to read from socket\n",
91873dfd78SSargun Dhillon strerror(errno));
92873dfd78SSargun Dhillon return -1;
93873dfd78SSargun Dhillon }
94873dfd78SSargun Dhillon
95873dfd78SSargun Dhillon return 0;
96873dfd78SSargun Dhillon }
97873dfd78SSargun Dhillon
child(int sk)98873dfd78SSargun Dhillon static int child(int sk)
99873dfd78SSargun Dhillon {
100873dfd78SSargun Dhillon int memfd, ret;
101873dfd78SSargun Dhillon
102873dfd78SSargun Dhillon memfd = sys_memfd_create("test", 0);
103873dfd78SSargun Dhillon if (memfd < 0) {
104873dfd78SSargun Dhillon fprintf(stderr, "%s: Child could not create memfd\n",
105873dfd78SSargun Dhillon strerror(errno));
106873dfd78SSargun Dhillon ret = -1;
107873dfd78SSargun Dhillon } else {
108873dfd78SSargun Dhillon ret = __child(sk, memfd);
109873dfd78SSargun Dhillon close(memfd);
110873dfd78SSargun Dhillon }
111873dfd78SSargun Dhillon
112873dfd78SSargun Dhillon close(sk);
113873dfd78SSargun Dhillon return ret;
114873dfd78SSargun Dhillon }
115873dfd78SSargun Dhillon
FIXTURE(child)116873dfd78SSargun Dhillon FIXTURE(child)
117873dfd78SSargun Dhillon {
118873dfd78SSargun Dhillon /*
119873dfd78SSargun Dhillon * remote_fd is the number of the FD which we are trying to retrieve
120873dfd78SSargun Dhillon * from the child.
121873dfd78SSargun Dhillon */
122873dfd78SSargun Dhillon int remote_fd;
123873dfd78SSargun Dhillon /* pid points to the child which we are fetching FDs from */
124873dfd78SSargun Dhillon pid_t pid;
125873dfd78SSargun Dhillon /* pidfd is the pidfd of the child */
126873dfd78SSargun Dhillon int pidfd;
127873dfd78SSargun Dhillon /*
128873dfd78SSargun Dhillon * sk is our side of the socketpair used to communicate with the child.
129873dfd78SSargun Dhillon * When it is closed, the child will exit.
130873dfd78SSargun Dhillon */
131873dfd78SSargun Dhillon int sk;
132873dfd78SSargun Dhillon };
133873dfd78SSargun Dhillon
FIXTURE_SETUP(child)134873dfd78SSargun Dhillon FIXTURE_SETUP(child)
135873dfd78SSargun Dhillon {
136873dfd78SSargun Dhillon int ret, sk_pair[2];
137873dfd78SSargun Dhillon
138873dfd78SSargun Dhillon ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) {
139873dfd78SSargun Dhillon TH_LOG("%s: failed to create socketpair", strerror(errno));
140873dfd78SSargun Dhillon }
141873dfd78SSargun Dhillon self->sk = sk_pair[0];
142873dfd78SSargun Dhillon
143873dfd78SSargun Dhillon self->pid = fork();
144873dfd78SSargun Dhillon ASSERT_GE(self->pid, 0);
145873dfd78SSargun Dhillon
146873dfd78SSargun Dhillon if (self->pid == 0) {
147873dfd78SSargun Dhillon close(sk_pair[0]);
148873dfd78SSargun Dhillon if (child(sk_pair[1]))
149873dfd78SSargun Dhillon _exit(EXIT_FAILURE);
150873dfd78SSargun Dhillon _exit(EXIT_SUCCESS);
151873dfd78SSargun Dhillon }
152873dfd78SSargun Dhillon
153873dfd78SSargun Dhillon close(sk_pair[1]);
154873dfd78SSargun Dhillon
155873dfd78SSargun Dhillon self->pidfd = sys_pidfd_open(self->pid, 0);
156873dfd78SSargun Dhillon ASSERT_GE(self->pidfd, 0);
157873dfd78SSargun Dhillon
158873dfd78SSargun Dhillon /*
159873dfd78SSargun Dhillon * Wait for the child to complete setup. It'll send the remote memfd's
160873dfd78SSargun Dhillon * number when ready.
161873dfd78SSargun Dhillon */
162873dfd78SSargun Dhillon ret = recv(sk_pair[0], &self->remote_fd, sizeof(self->remote_fd), 0);
163873dfd78SSargun Dhillon ASSERT_EQ(sizeof(self->remote_fd), ret);
164873dfd78SSargun Dhillon }
165873dfd78SSargun Dhillon
FIXTURE_TEARDOWN(child)166873dfd78SSargun Dhillon FIXTURE_TEARDOWN(child)
167873dfd78SSargun Dhillon {
168873dfd78SSargun Dhillon EXPECT_EQ(0, close(self->pidfd));
169873dfd78SSargun Dhillon EXPECT_EQ(0, close(self->sk));
170873dfd78SSargun Dhillon
171873dfd78SSargun Dhillon EXPECT_EQ(0, wait_for_pid(self->pid));
172873dfd78SSargun Dhillon }
173873dfd78SSargun Dhillon
TEST_F(child,disable_ptrace)174873dfd78SSargun Dhillon TEST_F(child, disable_ptrace)
175873dfd78SSargun Dhillon {
176873dfd78SSargun Dhillon int uid, fd;
177873dfd78SSargun Dhillon char c;
178873dfd78SSargun Dhillon
179873dfd78SSargun Dhillon /*
180873dfd78SSargun Dhillon * Turn into nobody if we're root, to avoid CAP_SYS_PTRACE
181873dfd78SSargun Dhillon *
182873dfd78SSargun Dhillon * The tests should run in their own process, so even this test fails,
183873dfd78SSargun Dhillon * it shouldn't result in subsequent tests failing.
184873dfd78SSargun Dhillon */
185873dfd78SSargun Dhillon uid = getuid();
186873dfd78SSargun Dhillon if (uid == 0)
187873dfd78SSargun Dhillon ASSERT_EQ(0, seteuid(UID_NOBODY));
188873dfd78SSargun Dhillon
189873dfd78SSargun Dhillon ASSERT_EQ(1, send(self->sk, "P", 1, 0));
190873dfd78SSargun Dhillon ASSERT_EQ(1, recv(self->sk, &c, 1, 0));
191873dfd78SSargun Dhillon
192873dfd78SSargun Dhillon fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
193873dfd78SSargun Dhillon EXPECT_EQ(-1, fd);
194873dfd78SSargun Dhillon EXPECT_EQ(EPERM, errno);
195873dfd78SSargun Dhillon
196873dfd78SSargun Dhillon if (uid == 0)
197873dfd78SSargun Dhillon ASSERT_EQ(0, seteuid(0));
198873dfd78SSargun Dhillon }
199873dfd78SSargun Dhillon
TEST_F(child,fetch_fd)200873dfd78SSargun Dhillon TEST_F(child, fetch_fd)
201873dfd78SSargun Dhillon {
202873dfd78SSargun Dhillon int fd, ret;
203873dfd78SSargun Dhillon
204873dfd78SSargun Dhillon fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
205873dfd78SSargun Dhillon ASSERT_GE(fd, 0);
206873dfd78SSargun Dhillon
207*b5ec9fe5STommi Rantala ret = sys_kcmp(getpid(), self->pid, KCMP_FILE, fd, self->remote_fd);
208*b5ec9fe5STommi Rantala if (ret < 0 && errno == ENOSYS)
209*b5ec9fe5STommi Rantala SKIP(return, "kcmp() syscall not supported");
210*b5ec9fe5STommi Rantala EXPECT_EQ(ret, 0);
211873dfd78SSargun Dhillon
212873dfd78SSargun Dhillon ret = fcntl(fd, F_GETFD);
213873dfd78SSargun Dhillon ASSERT_GE(ret, 0);
214873dfd78SSargun Dhillon EXPECT_GE(ret & FD_CLOEXEC, 0);
215873dfd78SSargun Dhillon
216873dfd78SSargun Dhillon close(fd);
217873dfd78SSargun Dhillon }
218873dfd78SSargun Dhillon
TEST_F(child,test_unknown_fd)219873dfd78SSargun Dhillon TEST_F(child, test_unknown_fd)
220873dfd78SSargun Dhillon {
221873dfd78SSargun Dhillon int fd;
222873dfd78SSargun Dhillon
223873dfd78SSargun Dhillon fd = sys_pidfd_getfd(self->pidfd, UNKNOWN_FD, 0);
224873dfd78SSargun Dhillon EXPECT_EQ(-1, fd) {
225873dfd78SSargun Dhillon TH_LOG("getfd succeeded while fetching unknown fd");
226873dfd78SSargun Dhillon };
227873dfd78SSargun Dhillon EXPECT_EQ(EBADF, errno) {
228873dfd78SSargun Dhillon TH_LOG("%s: getfd did not get EBADF", strerror(errno));
229873dfd78SSargun Dhillon }
230873dfd78SSargun Dhillon }
231873dfd78SSargun Dhillon
TEST(flags_set)232873dfd78SSargun Dhillon TEST(flags_set)
233873dfd78SSargun Dhillon {
234873dfd78SSargun Dhillon ASSERT_EQ(-1, sys_pidfd_getfd(0, 0, 1));
235873dfd78SSargun Dhillon EXPECT_EQ(errno, EINVAL);
236873dfd78SSargun Dhillon }
237873dfd78SSargun Dhillon
238873dfd78SSargun Dhillon #if __NR_pidfd_getfd == -1
main(void)239873dfd78SSargun Dhillon int main(void)
240873dfd78SSargun Dhillon {
241873dfd78SSargun Dhillon fprintf(stderr, "__NR_pidfd_getfd undefined. The pidfd_getfd syscall is unavailable. Test aborting\n");
242873dfd78SSargun Dhillon return KSFT_SKIP;
243873dfd78SSargun Dhillon }
244873dfd78SSargun Dhillon #else
245873dfd78SSargun Dhillon TEST_HARNESS_MAIN
246873dfd78SSargun Dhillon #endif
247