1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define _GNU_SOURCE
4 #include <errno.h>
5 #include <fcntl.h>
6 #include <limits.h>
7 #include <linux/types.h>
8 #include <sched.h>
9 #include <signal.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <syscall.h>
14 #include <sys/prctl.h>
15 #include <sys/wait.h>
16 #include <unistd.h>
17 #include <sys/socket.h>
18 #include <linux/kcmp.h>
19 
20 #include "pidfd.h"
21 #include "../kselftest.h"
22 #include "../kselftest_harness.h"
23 
24 /*
25  * UNKNOWN_FD is an fd number that should never exist in the child, as it is
26  * used to check the negative case.
27  */
28 #define UNKNOWN_FD 111
29 #define UID_NOBODY 65535
30 
31 static int sys_kcmp(pid_t pid1, pid_t pid2, int type, unsigned long idx1,
32 		    unsigned long idx2)
33 {
34 	return syscall(__NR_kcmp, pid1, pid2, type, idx1, idx2);
35 }
36 
37 static int __child(int sk, int memfd)
38 {
39 	int ret;
40 	char buf;
41 
42 	/*
43 	 * Ensure we don't leave around a bunch of orphaned children if our
44 	 * tests fail.
45 	 */
46 	ret = prctl(PR_SET_PDEATHSIG, SIGKILL);
47 	if (ret) {
48 		fprintf(stderr, "%s: Child could not set DEATHSIG\n",
49 			strerror(errno));
50 		return -1;
51 	}
52 
53 	ret = send(sk, &memfd, sizeof(memfd), 0);
54 	if (ret != sizeof(memfd)) {
55 		fprintf(stderr, "%s: Child failed to send fd number\n",
56 			strerror(errno));
57 		return -1;
58 	}
59 
60 	/*
61 	 * The fixture setup is completed at this point. The tests will run.
62 	 *
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.
68 	 */
69 	while ((ret = recv(sk, &buf, sizeof(buf), 0)) > 0) {
70 		if (buf == 'P') {
71 			ret = prctl(PR_SET_DUMPABLE, 0);
72 			if (ret < 0) {
73 				fprintf(stderr,
74 					"%s: Child failed to disable ptrace\n",
75 					strerror(errno));
76 				return -1;
77 			}
78 		} else {
79 			fprintf(stderr, "Child received unknown command %c\n",
80 				buf);
81 			return -1;
82 		}
83 		ret = send(sk, &buf, sizeof(buf), 0);
84 		if (ret != 1) {
85 			fprintf(stderr, "%s: Child failed to ack\n",
86 				strerror(errno));
87 			return -1;
88 		}
89 	}
90 	if (ret < 0) {
91 		fprintf(stderr, "%s: Child failed to read from socket\n",
92 			strerror(errno));
93 		return -1;
94 	}
95 
96 	return 0;
97 }
98 
99 static int child(int sk)
100 {
101 	int memfd, ret;
102 
103 	memfd = sys_memfd_create("test", 0);
104 	if (memfd < 0) {
105 		fprintf(stderr, "%s: Child could not create memfd\n",
106 			strerror(errno));
107 		ret = -1;
108 	} else {
109 		ret = __child(sk, memfd);
110 		close(memfd);
111 	}
112 
113 	close(sk);
114 	return ret;
115 }
116 
117 FIXTURE(child)
118 {
119 	/*
120 	 * remote_fd is the number of the FD which we are trying to retrieve
121 	 * from the child.
122 	 */
123 	int remote_fd;
124 	/* pid points to the child which we are fetching FDs from */
125 	pid_t pid;
126 	/* pidfd is the pidfd of the child */
127 	int pidfd;
128 	/*
129 	 * sk is our side of the socketpair used to communicate with the child.
130 	 * When it is closed, the child will exit.
131 	 */
132 	int sk;
133 };
134 
135 FIXTURE_SETUP(child)
136 {
137 	int ret, sk_pair[2];
138 
139 	ASSERT_EQ(0, socketpair(PF_LOCAL, SOCK_SEQPACKET, 0, sk_pair)) {
140 		TH_LOG("%s: failed to create socketpair", strerror(errno));
141 	}
142 	self->sk = sk_pair[0];
143 
144 	self->pid = fork();
145 	ASSERT_GE(self->pid, 0);
146 
147 	if (self->pid == 0) {
148 		close(sk_pair[0]);
149 		if (child(sk_pair[1]))
150 			_exit(EXIT_FAILURE);
151 		_exit(EXIT_SUCCESS);
152 	}
153 
154 	close(sk_pair[1]);
155 
156 	self->pidfd = sys_pidfd_open(self->pid, 0);
157 	ASSERT_GE(self->pidfd, 0);
158 
159 	/*
160 	 * Wait for the child to complete setup. It'll send the remote memfd's
161 	 * number when ready.
162 	 */
163 	ret = recv(sk_pair[0], &self->remote_fd, sizeof(self->remote_fd), 0);
164 	ASSERT_EQ(sizeof(self->remote_fd), ret);
165 }
166 
167 FIXTURE_TEARDOWN(child)
168 {
169 	EXPECT_EQ(0, close(self->pidfd));
170 	EXPECT_EQ(0, close(self->sk));
171 
172 	EXPECT_EQ(0, wait_for_pid(self->pid));
173 }
174 
175 TEST_F(child, disable_ptrace)
176 {
177 	int uid, fd;
178 	char c;
179 
180 	/*
181 	 * Turn into nobody if we're root, to avoid CAP_SYS_PTRACE
182 	 *
183 	 * The tests should run in their own process, so even this test fails,
184 	 * it shouldn't result in subsequent tests failing.
185 	 */
186 	uid = getuid();
187 	if (uid == 0)
188 		ASSERT_EQ(0, seteuid(UID_NOBODY));
189 
190 	ASSERT_EQ(1, send(self->sk, "P", 1, 0));
191 	ASSERT_EQ(1, recv(self->sk, &c, 1, 0));
192 
193 	fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
194 	EXPECT_EQ(-1, fd);
195 	EXPECT_EQ(EPERM, errno);
196 
197 	if (uid == 0)
198 		ASSERT_EQ(0, seteuid(0));
199 }
200 
201 TEST_F(child, fetch_fd)
202 {
203 	int fd, ret;
204 
205 	fd = sys_pidfd_getfd(self->pidfd, self->remote_fd, 0);
206 	ASSERT_GE(fd, 0);
207 
208 	EXPECT_EQ(0, sys_kcmp(getpid(), self->pid, KCMP_FILE, fd, self->remote_fd));
209 
210 	ret = fcntl(fd, F_GETFD);
211 	ASSERT_GE(ret, 0);
212 	EXPECT_GE(ret & FD_CLOEXEC, 0);
213 
214 	close(fd);
215 }
216 
217 TEST_F(child, test_unknown_fd)
218 {
219 	int fd;
220 
221 	fd = sys_pidfd_getfd(self->pidfd, UNKNOWN_FD, 0);
222 	EXPECT_EQ(-1, fd) {
223 		TH_LOG("getfd succeeded while fetching unknown fd");
224 	};
225 	EXPECT_EQ(EBADF, errno) {
226 		TH_LOG("%s: getfd did not get EBADF", strerror(errno));
227 	}
228 }
229 
230 TEST(flags_set)
231 {
232 	ASSERT_EQ(-1, sys_pidfd_getfd(0, 0, 1));
233 	EXPECT_EQ(errno, EINVAL);
234 }
235 
236 #if __NR_pidfd_getfd == -1
237 int main(void)
238 {
239 	fprintf(stderr, "__NR_pidfd_getfd undefined. The pidfd_getfd syscall is unavailable. Test aborting\n");
240 	return KSFT_SKIP;
241 }
242 #else
243 TEST_HARNESS_MAIN
244 #endif
245