xref: /freebsd/tests/sys/kern/unix_passfd_test.c (revision d904ce8a52e2f73bdcb1b8fba8016eff12128317)
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * Copyright (c) 2015 Mark Johnston
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35 #include <sys/un.h>
36 
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include <atf-c.h>
46 
47 /*
48  * UNIX domain sockets allow file descriptors to be passed via "ancillary
49  * data", or control messages.  This regression test is intended to exercise
50  * this facility, both performing some basic tests that it operates, and also
51  * causing some kernel edge cases to execute, such as garbage collection when
52  * there are cyclic file descriptor references.  Right now we test only with
53  * stream sockets, but ideally we'd also test with datagram sockets.
54  */
55 
56 static void
57 domainsocketpair(int *fdp)
58 {
59 
60 	ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
61 	    "socketpair(PF_UNIX, SOCK_STREAM) failed: %s", strerror(errno));
62 }
63 
64 static void
65 closesocketpair(int *fdp)
66 {
67 
68 	close(fdp[0]);
69 	close(fdp[1]);
70 }
71 
72 static void
73 devnull(int *fdp)
74 {
75 	int fd;
76 
77 	fd = open("/dev/null", O_RDONLY);
78 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
79 	*fdp = fd;
80 }
81 
82 static void
83 tempfile(int *fdp)
84 {
85 	char path[PATH_MAX];
86 	int fd;
87 
88 	snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
89 	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
90 	fd = mkstemp(path);
91 	ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
92 	(void)unlink(path);
93 	*fdp = fd;
94 }
95 
96 static void
97 dofstat(int fd, struct stat *sb)
98 {
99 
100 	ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
101 	    "fstat failed: %s", strerror(errno));
102 }
103 
104 static int
105 getnfds(void)
106 {
107 	size_t len;
108 	int mib[4], n, rc;
109 
110 	len = sizeof(n);
111 	mib[0] = CTL_KERN;
112 	mib[1] = KERN_PROC;
113 	mib[2] = KERN_PROC_NFDS;
114 	mib[3] = 0;
115 
116 	rc = sysctl(mib, 4, &n, &len, NULL, 0);
117 	ATF_REQUIRE_MSG(rc != -1, "sysctl(KERN_PROC_NFDS) failed");
118 	return (n);
119 }
120 
121 static void
122 samefile(struct stat *sb1, struct stat *sb2)
123 {
124 
125 	ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
126 	ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
127 }
128 
129 static void
130 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
131 {
132 	struct iovec iovec;
133 	char message[CMSG_SPACE(sizeof(int))];
134 	struct cmsghdr *cmsghdr;
135 	struct msghdr msghdr;
136 	ssize_t len;
137 
138 	bzero(&msghdr, sizeof(msghdr));
139 	bzero(&message, sizeof(message));
140 
141 	msghdr.msg_control = message;
142 	msghdr.msg_controllen = sizeof(message);
143 
144 	iovec.iov_base = payload;
145 	iovec.iov_len = paylen;
146 
147 	msghdr.msg_iov = &iovec;
148 	msghdr.msg_iovlen = 1;
149 
150 	cmsghdr = (struct cmsghdr *)(void *)message;
151 	cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
152 	cmsghdr->cmsg_level = SOL_SOCKET;
153 	cmsghdr->cmsg_type = SCM_RIGHTS;
154 	memcpy(CMSG_DATA(cmsghdr), &send_fd, sizeof(int));
155 
156 	len = sendmsg(sockfd, &msghdr, 0);
157 	ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
158 	ATF_REQUIRE_MSG((size_t)len == paylen,
159 	    "sendmsg: %zd messages sent; expected: %zu; %s", len, paylen,
160 	    strerror(errno));
161 }
162 
163 static void
164 sendfd(int sockfd, int send_fd)
165 {
166 	char ch = 0;
167 
168 	return (sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)));
169 }
170 
171 static void
172 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen)
173 {
174 	struct cmsghdr *cmsghdr;
175 	char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
176 	struct msghdr msghdr;
177 	struct iovec iovec;
178 	ssize_t len;
179 
180 	bzero(&msghdr, sizeof(msghdr));
181 
182 	msghdr.msg_control = message;
183 	msghdr.msg_controllen = sizeof(message);
184 
185 	iovec.iov_base = buf;
186 	iovec.iov_len = buflen;
187 
188 	msghdr.msg_iov = &iovec;
189 	msghdr.msg_iovlen = 1;
190 
191 	len = recvmsg(sockfd, &msghdr, 0);
192 	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
193 	ATF_REQUIRE_MSG((size_t)len == buflen,
194 	    "recvmsg: %zd bytes received; expected %zd", len, buflen);
195 
196 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
197 	ATF_REQUIRE_MSG(cmsghdr != NULL,
198 	    "recvmsg: did not receive control message");
199 	*recv_fd = -1;
200 	for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
201 		if (cmsghdr->cmsg_level == SOL_SOCKET &&
202 		    cmsghdr->cmsg_type == SCM_RIGHTS &&
203 		    cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
204 			memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
205 			ATF_REQUIRE(*recv_fd != -1);
206 		}
207 	}
208 	ATF_REQUIRE_MSG(*recv_fd != -1,
209 	    "recvmsg: did not receive single-fd message");
210 }
211 
212 static void
213 recvfd(int sockfd, int *recv_fd)
214 {
215 	char ch = 0;
216 
217 	return (recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch)));
218 }
219 
220 /*
221  * Put a temporary file into a UNIX domain socket, then take it out and make
222  * sure it's the same file.  First time around, don't close the reference
223  * after sending.
224  */
225 ATF_TC_WITHOUT_HEAD(simple_send_fd);
226 ATF_TC_BODY(simple_send_fd, tc)
227 {
228 	struct stat getfd_stat, putfd_stat;
229 	int fd[2], getfd, putfd;
230 
231 	domainsocketpair(fd);
232 	tempfile(&putfd);
233 	dofstat(putfd, &putfd_stat);
234 	sendfd(fd[0], putfd);
235 	recvfd(fd[1], &getfd);
236 	dofstat(getfd, &getfd_stat);
237 	samefile(&putfd_stat, &getfd_stat);
238 	close(putfd);
239 	close(getfd);
240 	closesocketpair(fd);
241 }
242 
243 /*
244  * Same as simple_send_fd, only close the file reference after sending, so that
245  * the only reference is the descriptor in the UNIX domain socket buffer.
246  */
247 ATF_TC_WITHOUT_HEAD(send_and_close);
248 ATF_TC_BODY(send_and_close, tc)
249 {
250 	struct stat getfd_stat, putfd_stat;
251 	int fd[2], getfd, putfd;
252 
253 	domainsocketpair(fd);
254 	tempfile(&putfd);
255 	dofstat(putfd, &putfd_stat);
256 	sendfd(fd[0], putfd);
257 	close(putfd);
258 	recvfd(fd[1], &getfd);
259 	dofstat(getfd, &getfd_stat);
260 	samefile(&putfd_stat, &getfd_stat);
261 	close(getfd);
262 	closesocketpair(fd);
263 }
264 
265 /*
266  * Put a temporary file into a UNIX domain socket, then close both endpoints
267  * causing garbage collection to kick off.
268  */
269 ATF_TC_WITHOUT_HEAD(send_and_cancel);
270 ATF_TC_BODY(send_and_cancel, tc)
271 {
272 	int fd[2], putfd;
273 
274 	domainsocketpair(fd);
275 	tempfile(&putfd);
276 	sendfd(fd[0], putfd);
277 	close(putfd);
278 	closesocketpair(fd);
279 }
280 
281 /*
282  * Send two files.  Then receive them.  Make sure they are returned in the
283  * right order, and both get there.
284  */
285 ATF_TC_WITHOUT_HEAD(two_files);
286 ATF_TC_BODY(two_files, tc)
287 {
288 	struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
289 	int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
290 
291 	domainsocketpair(fd);
292 	tempfile(&putfd_1);
293 	tempfile(&putfd_2);
294 	dofstat(putfd_1, &putfd_1_stat);
295 	dofstat(putfd_2, &putfd_2_stat);
296 	sendfd(fd[0], putfd_1);
297 	sendfd(fd[0], putfd_2);
298 	close(putfd_1);
299 	close(putfd_2);
300 	recvfd(fd[1], &getfd_1);
301 	recvfd(fd[1], &getfd_2);
302 	dofstat(getfd_1, &getfd_1_stat);
303 	dofstat(getfd_2, &getfd_2_stat);
304 	samefile(&putfd_1_stat, &getfd_1_stat);
305 	samefile(&putfd_2_stat, &getfd_2_stat);
306 	close(getfd_1);
307 	close(getfd_2);
308 	closesocketpair(fd);
309 }
310 
311 /*
312  * Big bundling test.  Send an endpoint of the UNIX domain socket over itself,
313  * closing the door behind it.
314  */
315 ATF_TC_WITHOUT_HEAD(bundle);
316 ATF_TC_BODY(bundle, tc)
317 {
318 	int fd[2], getfd;
319 
320 	domainsocketpair(fd);
321 
322 	sendfd(fd[0], fd[0]);
323 	close(fd[0]);
324 	recvfd(fd[1], &getfd);
325 	close(getfd);
326 	close(fd[1]);
327 }
328 
329 /*
330  * Big bundling test part two: Send an endpoint of the UNIX domain socket over
331  * itself, close the door behind it, and never remove it from the other end.
332  */
333 ATF_TC_WITHOUT_HEAD(bundle_cancel);
334 ATF_TC_BODY(bundle_cancel, tc)
335 {
336 	int fd[2];
337 
338 	domainsocketpair(fd);
339 	sendfd(fd[0], fd[0]);
340 	sendfd(fd[1], fd[0]);
341 	closesocketpair(fd);
342 }
343 
344 /*
345  * Test for PR 151758: Send an character device over the UNIX domain socket
346  * and then close both sockets to orphan the device.
347  */
348 ATF_TC_WITHOUT_HEAD(devfs_orphan);
349 ATF_TC_BODY(devfs_orphan, tc)
350 {
351 	int fd[2], putfd;
352 
353 	domainsocketpair(fd);
354 	devnull(&putfd);
355 	sendfd(fd[0], putfd);
356 	close(putfd);
357 	closesocketpair(fd);
358 }
359 
360 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
361 
362 /*
363  * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
364  * control message to the data. Sender sends large payload.
365  * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver
366  * receives truncated data.
367  */
368 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
369 ATF_TC_BODY(rights_creds_payload, tc)
370 {
371 	const int on = 1;
372 	u_long sendspace;
373 	size_t len;
374 	void *buf;
375 	int fd[2], getfd, putfd, rc;
376 
377 	atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages "
378 	    "are present with large data");
379 
380 	len = sizeof(sendspace);
381 	rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
382 	    &len, NULL, 0);
383 	ATF_REQUIRE_MSG(rc != -1,
384 	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
385 
386 	buf = calloc(1, sendspace);
387 	ATF_REQUIRE(buf != NULL);
388 
389 	domainsocketpair(fd);
390 	rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
391 	ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
392 	    strerror(errno));
393 	tempfile(&putfd);
394 	sendfd_payload(fd[0], putfd, buf, sendspace);
395 	recvfd_payload(fd[1], &getfd, buf, sendspace);
396 	close(putfd);
397 	close(getfd);
398 	closesocketpair(fd);
399 }
400 
401 /*
402  * Test for PR 131876. Receiver uses a control message buffer that is too
403  * small for the incoming SCM_RIGHTS message, so the message is truncated.
404  * The kernel must not leak the copied right into the receiver's namespace.
405  */
406 ATF_TC_WITHOUT_HEAD(truncated_rights);
407 ATF_TC_BODY(truncated_rights, tc)
408 {
409 	struct iovec iovec;
410 	struct msghdr msghdr;
411 	char buf[16], message[CMSG_SPACE(0)];
412 	ssize_t len;
413 	int fd[2], nfds, putfd;
414 
415 	atf_tc_expect_fail("PR 131876: "
416 	    "FD leak when 'control' message is truncated");
417 
418 	memset(buf, 42, sizeof(buf));
419 	domainsocketpair(fd);
420 	devnull(&putfd);
421 	nfds = getnfds();
422 
423 	sendfd_payload(fd[0], putfd, buf, sizeof(buf));
424 
425 	bzero(&msghdr, sizeof(msghdr));
426 	bzero(message, sizeof(message));
427 
428 	iovec.iov_base = buf;
429 	iovec.iov_len = sizeof(buf);
430 	msghdr.msg_control = message;
431 	msghdr.msg_controllen = sizeof(message);
432 	msghdr.msg_iov = &iovec;
433 	msghdr.msg_iovlen = 1;
434 
435 	len = recvmsg(fd[1], &msghdr, 0);
436 	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
437 	ATF_REQUIRE_MSG((size_t)len == sizeof(buf),
438 	    "recvmsg: %zd bytes received; expected %zd", len, sizeof(buf));
439 	for (size_t i = 0; i < sizeof(buf); i++)
440 		ATF_REQUIRE_MSG(buf[i] == 42, "unexpected buffer contents");
441 
442 	ATF_REQUIRE_MSG((msghdr.msg_flags & MSG_CTRUNC) != 0,
443 	    "MSG_CTRUNC not set after truncation");
444 	ATF_REQUIRE(getnfds() == nfds);
445 
446 	close(putfd);
447 	closesocketpair(fd);
448 }
449 
450 ATF_TP_ADD_TCS(tp)
451 {
452 
453 	ATF_TP_ADD_TC(tp, simple_send_fd);
454 	ATF_TP_ADD_TC(tp, send_and_close);
455 	ATF_TP_ADD_TC(tp, send_and_cancel);
456 	ATF_TP_ADD_TC(tp, two_files);
457 	ATF_TP_ADD_TC(tp, bundle);
458 	ATF_TP_ADD_TC(tp, bundle_cancel);
459 	ATF_TP_ADD_TC(tp, devfs_orphan);
460 	ATF_TP_ADD_TC(tp, rights_creds_payload);
461 	ATF_TP_ADD_TC(tp, truncated_rights);
462 
463 	return (atf_no_error());
464 }
465