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