xref: /freebsd/tests/sys/kern/unix_passfd_test.c (revision a0b956f5ac5e0941f9e74e24c1c53e05ad061a38)
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * Copyright (c) 2015 Mark Johnston
4  * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/stat.h>
35 #include <sys/sysctl.h>
36 #include <sys/time.h>
37 #include <sys/resource.h>
38 #include <sys/un.h>
39 
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <limits.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include <atf-c.h>
49 
50 #if !defined(TEST_PROTO)
51 #error Need TEST_PROTO defined to SOCK_STREAM or SOCK_DGRAM
52 #endif
53 
54 /*
55  * UNIX domain sockets allow file descriptors to be passed via "ancillary
56  * data", or control messages.  This regression test is intended to exercise
57  * this facility, both performing some basic tests that it operates, and also
58  * causing some kernel edge cases to execute, such as garbage collection when
59  * there are cyclic file descriptor references.  Right now we test only with
60  * stream sockets, but ideally we'd also test with datagram sockets.
61  */
62 
63 static void
64 domainsocketpair(int *fdp)
65 {
66 
67 	ATF_REQUIRE_MSG(socketpair(PF_UNIX, TEST_PROTO, 0, fdp) != -1,
68 	    "socketpair(PF_UNIX, %u) failed: %s", TEST_PROTO, strerror(errno));
69 }
70 
71 static void
72 closesocketpair(int *fdp)
73 {
74 
75 	close(fdp[0]);
76 	close(fdp[1]);
77 }
78 
79 static void
80 devnull(int *fdp)
81 {
82 	int fd;
83 
84 	fd = open("/dev/null", O_RDONLY);
85 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
86 	*fdp = fd;
87 }
88 
89 static void
90 tempfile(int *fdp)
91 {
92 	char path[PATH_MAX];
93 	int fd;
94 
95 	snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
96 	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
97 	fd = mkstemp(path);
98 	ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
99 	(void)unlink(path);
100 	*fdp = fd;
101 }
102 
103 static void
104 dofstat(int fd, struct stat *sb)
105 {
106 
107 	ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
108 	    "fstat failed: %s", strerror(errno));
109 }
110 
111 static int
112 getnfds(void)
113 {
114 	size_t len;
115 	int mib[4], n, rc;
116 
117 	len = sizeof(n);
118 	mib[0] = CTL_KERN;
119 	mib[1] = KERN_PROC;
120 	mib[2] = KERN_PROC_NFDS;
121 	mib[3] = 0;
122 
123 	rc = sysctl(mib, 4, &n, &len, NULL, 0);
124 	ATF_REQUIRE_MSG(rc != -1, "sysctl(KERN_PROC_NFDS) failed");
125 	return (n);
126 }
127 
128 static int
129 openfiles(void)
130 {
131 	int files;
132 	size_t len = sizeof(files);
133 
134 	ATF_REQUIRE(sysctlbyname("kern.openfiles", &files, &len, NULL, 0) == 0);
135 
136 	return (files);
137 }
138 
139 static void
140 putfds(char *buf, int fd, int nfds)
141 {
142 	struct cmsghdr *cm;
143 	int *fdp, i;
144 
145 	cm = (struct cmsghdr *)buf;
146 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
147 	cm->cmsg_level = SOL_SOCKET;
148 	cm->cmsg_type = SCM_RIGHTS;
149 	for (fdp = (int *)CMSG_DATA(cm), i = 0; i < nfds; i++)
150 		*fdp++ = fd;
151 }
152 
153 static void
154 samefile(struct stat *sb1, struct stat *sb2)
155 {
156 
157 	ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
158 	ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
159 }
160 
161 static ssize_t
162 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
163 {
164 	struct iovec iovec;
165 	char message[CMSG_SPACE(sizeof(int))];
166 	struct msghdr msghdr;
167 
168 	bzero(&msghdr, sizeof(msghdr));
169 	bzero(&message, sizeof(message));
170 
171 	msghdr.msg_control = message;
172 	msghdr.msg_controllen = sizeof(message);
173 
174 	iovec.iov_base = payload;
175 	iovec.iov_len = paylen;
176 
177 	msghdr.msg_iov = &iovec;
178 	msghdr.msg_iovlen = 1;
179 
180 	putfds(message, send_fd, 1);
181 	return (sendmsg(sockfd, &msghdr, 0));
182 }
183 
184 static void
185 sendfd(int sockfd, int send_fd)
186 {
187 	size_t len;
188 	char ch;
189 
190 	ch = 0;
191 	len = sendfd_payload(sockfd, send_fd, &ch, sizeof(ch));
192 	ATF_REQUIRE_MSG(len == sizeof(ch),
193 	    "sendmsg: %zu bytes sent; expected %zu; %s", len, sizeof(ch),
194 	    strerror(errno));
195 }
196 
197 static bool
198 localcreds(int sockfd)
199 {
200 	socklen_t sz;
201 	int rc, val;
202 
203 	sz = sizeof(val);
204 	rc = getsockopt(sockfd, 0, LOCAL_CREDS, &val, &sz);
205 	ATF_REQUIRE_MSG(rc != -1, "getsockopt(LOCAL_CREDS) failed: %s",
206 	    strerror(errno));
207 	return (val != 0);
208 }
209 
210 static void
211 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen,
212     size_t cmsgsz, int recvmsg_flags)
213 {
214 	struct cmsghdr *cmsghdr;
215 	struct msghdr msghdr;
216 	struct iovec iovec;
217 	char *message;
218 	ssize_t len;
219 	bool foundcreds;
220 
221 	bzero(&msghdr, sizeof(msghdr));
222 	message = malloc(cmsgsz);
223 	ATF_REQUIRE(message != NULL);
224 
225 	msghdr.msg_control = message;
226 	msghdr.msg_controllen = cmsgsz;
227 
228 	iovec.iov_base = buf;
229 	iovec.iov_len = buflen;
230 
231 	msghdr.msg_iov = &iovec;
232 	msghdr.msg_iovlen = 1;
233 
234 	len = recvmsg(sockfd, &msghdr, recvmsg_flags);
235 	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
236 	ATF_REQUIRE_MSG((size_t)len == buflen,
237 	    "recvmsg: %zd bytes received; expected %zd", len, buflen);
238 
239 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
240 	ATF_REQUIRE_MSG(cmsghdr != NULL,
241 	    "recvmsg: did not receive control message");
242 	foundcreds = false;
243 	*recv_fd = -1;
244 	for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
245 		if (cmsghdr->cmsg_level == SOL_SOCKET &&
246 		    cmsghdr->cmsg_type == SCM_RIGHTS &&
247 		    cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
248 			memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
249 			ATF_REQUIRE(*recv_fd != -1);
250 		} else if (cmsghdr->cmsg_level == SOL_SOCKET &&
251 		    cmsghdr->cmsg_type == SCM_CREDS)
252 			foundcreds = true;
253 	}
254 	ATF_REQUIRE_MSG(*recv_fd != -1,
255 	    "recvmsg: did not receive single-fd message");
256 	ATF_REQUIRE_MSG(!localcreds(sockfd) || foundcreds,
257 	    "recvmsg: expected credentials were not received");
258 	if (recvmsg_flags & MSG_TRUNC)
259 		ATF_REQUIRE_MSG(msghdr.msg_flags & MSG_TRUNC,
260 		    "recvmsg: expected MSG_TRUNC missing");
261 }
262 
263 static void
264 recvfd(int sockfd, int *recv_fd, int flags)
265 {
266 	char ch = 0;
267 
268 	recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch),
269 	    CMSG_SPACE(sizeof(int)), flags);
270 }
271 
272 /*
273  * Put a temporary file into a UNIX domain socket, then take it out and make
274  * sure it's the same file.  First time around, don't close the reference
275  * after sending.
276  */
277 ATF_TC_WITHOUT_HEAD(simple_send_fd);
278 ATF_TC_BODY(simple_send_fd, tc)
279 {
280 	struct stat getfd_stat, putfd_stat;
281 	int fd[2], getfd, putfd;
282 
283 	domainsocketpair(fd);
284 	tempfile(&putfd);
285 	dofstat(putfd, &putfd_stat);
286 	sendfd(fd[0], putfd);
287 	recvfd(fd[1], &getfd, 0);
288 	dofstat(getfd, &getfd_stat);
289 	samefile(&putfd_stat, &getfd_stat);
290 	close(putfd);
291 	close(getfd);
292 	closesocketpair(fd);
293 }
294 
295 /*
296  * Like simple_send_fd but also sets MSG_CMSG_CLOEXEC and checks that the
297  * received file descriptor has the FD_CLOEXEC flag set.
298  */
299 ATF_TC_WITHOUT_HEAD(simple_send_fd_msg_cmsg_cloexec);
300 ATF_TC_BODY(simple_send_fd_msg_cmsg_cloexec, tc)
301 {
302 	struct stat getfd_stat, putfd_stat;
303 	int fd[2], getfd, putfd;
304 
305 	domainsocketpair(fd);
306 	tempfile(&putfd);
307 	dofstat(putfd, &putfd_stat);
308 	sendfd(fd[0], putfd);
309 	recvfd(fd[1], &getfd, MSG_CMSG_CLOEXEC);
310 	dofstat(getfd, &getfd_stat);
311 	samefile(&putfd_stat, &getfd_stat);
312 	ATF_REQUIRE_EQ_MSG(fcntl(getfd, F_GETFD) & FD_CLOEXEC, FD_CLOEXEC,
313 	    "FD_CLOEXEC not set on the received file descriptor");
314 	close(putfd);
315 	close(getfd);
316 	closesocketpair(fd);
317 }
318 
319 /*
320  * Same as simple_send_fd, only close the file reference after sending, so that
321  * the only reference is the descriptor in the UNIX domain socket buffer.
322  */
323 ATF_TC_WITHOUT_HEAD(send_and_close);
324 ATF_TC_BODY(send_and_close, tc)
325 {
326 	struct stat getfd_stat, putfd_stat;
327 	int fd[2], getfd, putfd;
328 
329 	domainsocketpair(fd);
330 	tempfile(&putfd);
331 	dofstat(putfd, &putfd_stat);
332 	sendfd(fd[0], putfd);
333 	close(putfd);
334 	recvfd(fd[1], &getfd, 0);
335 	dofstat(getfd, &getfd_stat);
336 	samefile(&putfd_stat, &getfd_stat);
337 	close(getfd);
338 	closesocketpair(fd);
339 }
340 
341 /*
342  * Put a temporary file into a UNIX domain socket, then close both endpoints
343  * causing garbage collection to kick off.
344  */
345 ATF_TC_WITHOUT_HEAD(send_and_cancel);
346 ATF_TC_BODY(send_and_cancel, tc)
347 {
348 	int fd[2], putfd;
349 
350 	domainsocketpair(fd);
351 	tempfile(&putfd);
352 	sendfd(fd[0], putfd);
353 	close(putfd);
354 	closesocketpair(fd);
355 }
356 
357 /*
358  * Send file then shutdown receive side to exercise unp_dispose() call
359  * via soshutdown().  Check that shutdown(SHUT_RD) would gc the file
360  * reference sitting in the receive buffer.  There is no good way of
361  * checking that except using global open file count.
362  */
363 ATF_TC_WITHOUT_HEAD(send_and_shutdown);
364 ATF_TC_BODY(send_and_shutdown, tc)
365 {
366 	int fd[2], putfd, nfiles;
367 
368 	domainsocketpair(fd);
369 	tempfile(&putfd);
370 	sendfd(fd[0], putfd);
371 	nfiles = openfiles();
372 	close(putfd);
373 	ATF_REQUIRE(openfiles() == nfiles);
374 	shutdown(fd[1], SHUT_RD);
375 	ATF_REQUIRE(openfiles() == nfiles - 1);
376 	closesocketpair(fd);
377 }
378 
379 /*
380  * Send maximum possible SCM_RIGHTS message.
381  * Internally the file descriptors are converted from integers to pointers
382  * and stored in a single mbuf cluster.  Check that we can not send too much
383  * and that we can successfully send maximum possible amount.  Check that we
384  * can not exploit getrlimit(3).
385  */
386 #define	MAXFDS	((MCLBYTES - _ALIGN(sizeof(struct cmsghdr)))/sizeof(void *))
387 ATF_TC_WITHOUT_HEAD(send_a_lot);
388 ATF_TC_BODY(send_a_lot, tc)
389 {
390 	struct msghdr msghdr;
391 	struct iovec iov;
392 	struct rlimit rlim;
393 	int fd[2], nfds;
394 	char *cmsg, ch;
395 
396 	domainsocketpair(fd);
397 	cmsg = malloc(CMSG_SPACE((MAXFDS + 1) * sizeof(int)));
398 	ATF_REQUIRE(cmsg != NULL);
399 	iov.iov_base = &ch;
400 	iov.iov_len = sizeof(ch);
401 	msghdr = (struct msghdr ){
402 		.msg_control = cmsg,
403 		.msg_controllen = CMSG_LEN((MAXFDS + 1) * sizeof(int)),
404 		.msg_iov = &iov,
405 		.msg_iovlen = 1,
406 	};
407 
408 	/* Sending too much fails. */
409 	putfds(cmsg, fd[0], MAXFDS + 1);
410 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == -1);
411 	ATF_REQUIRE(errno == EMSGSIZE);
412 
413 	/* Sending just the right amount works and everything is received. */
414 	putfds(cmsg, fd[0], MAXFDS);
415 	msghdr.msg_controllen = CMSG_LEN(MAXFDS * sizeof(int));
416 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1);
417 	nfds = getnfds();
418 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1);
419 	ATF_REQUIRE(getnfds() == (int)(nfds + MAXFDS));
420 
421 	/* Limit our process open files... */
422 	ATF_REQUIRE(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
423 	nfds = rlim.rlim_cur = getnfds();
424 	ATF_REQUIRE(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
425 
426 	/* ... and try to receive a single descriptor. */
427 	putfds(cmsg, fd[0], 1);
428 	msghdr.msg_controllen = CMSG_LEN(sizeof(int));
429 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1);
430 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == -1);
431 	/* Such attempt shall fail with EMFILE. */
432 	ATF_REQUIRE(errno == EMFILE);
433 	ATF_REQUIRE(getnfds() == nfds);
434 #if TEST_PROTO == SOCK_STREAM
435 	/*
436 	 * For the SOCK_STREAM the above attempt shall free the control in
437 	 * the kernel, so that socket isn't left in a stuck state.  Next read
438 	 * shall bring us the normal data only.  The stream data shall not
439 	 * miss a byte.
440 	 */
441 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1);
442 	ATF_REQUIRE(msghdr.msg_controllen == 0);
443 #elif TEST_PROTO == SOCK_DGRAM
444 	/*
445 	 * For SOCK_DGRAM there are two options for the previously failed
446 	 * syscall: strip the control leaving datagram in the socket or
447 	 * drop the whole datagram.  Our implementation drops the whole
448 	 * datagram.
449 	 */
450 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, MSG_DONTWAIT) == -1);
451 	ATF_REQUIRE(errno == EAGAIN);
452 #endif
453 }
454 
455 /*
456  * Send two files.  Then receive them.  Make sure they are returned in the
457  * right order, and both get there.
458  */
459 ATF_TC_WITHOUT_HEAD(two_files);
460 ATF_TC_BODY(two_files, tc)
461 {
462 	struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
463 	int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
464 
465 	domainsocketpair(fd);
466 	tempfile(&putfd_1);
467 	tempfile(&putfd_2);
468 	dofstat(putfd_1, &putfd_1_stat);
469 	dofstat(putfd_2, &putfd_2_stat);
470 	sendfd(fd[0], putfd_1);
471 	sendfd(fd[0], putfd_2);
472 	close(putfd_1);
473 	close(putfd_2);
474 	recvfd(fd[1], &getfd_1, 0);
475 	recvfd(fd[1], &getfd_2, 0);
476 	dofstat(getfd_1, &getfd_1_stat);
477 	dofstat(getfd_2, &getfd_2_stat);
478 	samefile(&putfd_1_stat, &getfd_1_stat);
479 	samefile(&putfd_2_stat, &getfd_2_stat);
480 	close(getfd_1);
481 	close(getfd_2);
482 	closesocketpair(fd);
483 }
484 
485 /*
486  * Big bundling test.  Send an endpoint of the UNIX domain socket over itself,
487  * closing the door behind it.
488  */
489 ATF_TC_WITHOUT_HEAD(bundle);
490 ATF_TC_BODY(bundle, tc)
491 {
492 	int fd[2], getfd;
493 
494 	domainsocketpair(fd);
495 
496 	sendfd(fd[0], fd[0]);
497 	close(fd[0]);
498 	recvfd(fd[1], &getfd, 0);
499 	close(getfd);
500 	close(fd[1]);
501 }
502 
503 /*
504  * Big bundling test part two: Send an endpoint of the UNIX domain socket over
505  * itself, close the door behind it, and never remove it from the other end.
506  */
507 ATF_TC_WITHOUT_HEAD(bundle_cancel);
508 ATF_TC_BODY(bundle_cancel, tc)
509 {
510 	int fd[2];
511 
512 	domainsocketpair(fd);
513 	sendfd(fd[0], fd[0]);
514 	sendfd(fd[1], fd[0]);
515 	closesocketpair(fd);
516 }
517 
518 /*
519  * Test for PR 151758: Send an character device over the UNIX domain socket
520  * and then close both sockets to orphan the device.
521  */
522 ATF_TC_WITHOUT_HEAD(devfs_orphan);
523 ATF_TC_BODY(devfs_orphan, tc)
524 {
525 	int fd[2], putfd;
526 
527 	domainsocketpair(fd);
528 	devnull(&putfd);
529 	sendfd(fd[0], putfd);
530 	close(putfd);
531 	closesocketpair(fd);
532 }
533 
534 #if TEST_PROTO == SOCK_STREAM
535 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
536 #elif TEST_PROTO == SOCK_DGRAM
537 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.dgram.maxdgram"
538 #endif
539 
540 /*
541  * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
542  * control message to the data. Sender sends large payload using a non-blocking
543  * socket. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and
544  * receiver receives truncated data.
545  */
546 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
547 ATF_TC_BODY(rights_creds_payload, tc)
548 {
549 	const int on = 1;
550 	u_long sendspace;
551 	ssize_t len;
552 	void *buf;
553 	int fd[2], getfd, putfd, rc;
554 
555 	len = sizeof(sendspace);
556 	rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
557 	    &len, NULL, 0);
558 	ATF_REQUIRE_MSG(rc != -1,
559 	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
560 
561 	buf = calloc(1, sendspace);
562 	ATF_REQUIRE(buf != NULL);
563 
564 	domainsocketpair(fd);
565 	tempfile(&putfd);
566 
567 	rc = fcntl(fd[0], F_SETFL, O_NONBLOCK);
568 	ATF_REQUIRE_MSG(rc != -1, "fcntl(O_NONBLOCK) failed: %s",
569 	    strerror(errno));
570 	rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
571 	ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
572 	    strerror(errno));
573 
574 	len = sendfd_payload(fd[0], putfd, buf, sendspace);
575 #if TEST_PROTO == SOCK_STREAM
576 	ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno));
577 	ATF_REQUIRE_MSG((size_t)len < sendspace,
578 	    "sendmsg: %zu bytes sent", len);
579 	recvfd_payload(fd[1], &getfd, buf, len,
580 	    CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0);
581 #endif
582 #if TEST_PROTO == SOCK_DGRAM
583 	ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno));
584 	ATF_REQUIRE_MSG((size_t)len == sendspace,
585 	    "sendmsg: %zu bytes sent", len);
586 	recvfd_payload(fd[1], &getfd, buf, len,
587 	    CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)),
588 	    MSG_TRUNC);
589 #endif
590 
591 	close(putfd);
592 	close(getfd);
593 	closesocketpair(fd);
594 }
595 
596 static void
597 send_cmsg(int sockfd, void *cmsg, size_t cmsgsz)
598 {
599 	struct iovec iov;
600 	struct msghdr msghdr;
601 	ssize_t len;
602 	char ch;
603 
604 	ch = 0;
605 	bzero(&msghdr, sizeof(msghdr));
606 
607 	iov.iov_base = &ch;
608 	iov.iov_len = sizeof(ch);
609 	msghdr.msg_control = cmsg;
610 	msghdr.msg_controllen = cmsgsz;
611 	msghdr.msg_iov = &iov;
612 	msghdr.msg_iovlen = 1;
613 
614 	len = sendmsg(sockfd, &msghdr, 0);
615 	ATF_REQUIRE_MSG(len != -1,
616 	    "sendmsg failed: %s", strerror(errno));
617 	ATF_REQUIRE_MSG(len == sizeof(ch),
618 	    "sendmsg: %zd bytes sent; expected %zu", len, sizeof(ch));
619 }
620 
621 static void
622 recv_cmsg(int sockfd, char *cmsg, size_t cmsgsz, int flags)
623 {
624 	struct iovec iov;
625 	struct msghdr msghdr;
626 	ssize_t len;
627 	char ch;
628 
629 	ch = 0;
630 	bzero(&msghdr, sizeof(msghdr));
631 
632 	iov.iov_base = &ch;
633 	iov.iov_len = sizeof(ch);
634 	msghdr.msg_control = cmsg;
635 	msghdr.msg_controllen = cmsgsz;
636 	msghdr.msg_iov = &iov;
637 	msghdr.msg_iovlen = 1;
638 
639 	len = recvmsg(sockfd, &msghdr, 0);
640 	ATF_REQUIRE_MSG(len != -1,
641 	    "recvmsg failed: %s", strerror(errno));
642 	ATF_REQUIRE_MSG(len == sizeof(ch),
643 	    "recvmsg: %zd bytes received; expected %zu", len, sizeof(ch));
644 	ATF_REQUIRE_MSG((msghdr.msg_flags & flags) == flags,
645 	    "recvmsg: got flags %#x; expected %#x", msghdr.msg_flags, flags);
646 }
647 
648 /*
649  * Test for PR 131876.  Receiver uses a control message buffer that is too
650  * small for the incoming SCM_RIGHTS message, so the message is truncated.
651  * The kernel must not leak the copied right into the receiver's namespace.
652  */
653 ATF_TC_WITHOUT_HEAD(truncated_rights);
654 ATF_TC_BODY(truncated_rights, tc)
655 {
656 	char *message;
657 	int fd[2], nfds, putfd, rc;
658 
659 	domainsocketpair(fd);
660 	devnull(&putfd);
661 	nfds = getnfds();
662 
663 	/*
664 	 * Case 1: Send a single descriptor and truncate the message.
665 	 */
666 	message = malloc(CMSG_SPACE(sizeof(int)));
667 	ATF_REQUIRE(message != NULL);
668 	putfds(message, putfd, 1);
669 	send_cmsg(fd[0], message, CMSG_LEN(sizeof(int)));
670 	recv_cmsg(fd[1], message, CMSG_LEN(0), MSG_CTRUNC);
671 	ATF_REQUIRE(getnfds() == nfds);
672 	free(message);
673 
674 	/*
675 	 * Case 2a: Send two descriptors in separate messages, and truncate at
676 	 *          the boundary between the two messages.  We should still
677 	 *          receive the first message.
678 	 */
679 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
680 	ATF_REQUIRE(message != NULL);
681 	putfds(message, putfd, 1);
682 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
683 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
684 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
685 	rc = close(*(int *)CMSG_DATA(message));
686 	ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
687 	ATF_REQUIRE(getnfds() == nfds);
688 	free(message);
689 
690 	/*
691 	 * Case 2b: Send two descriptors in separate messages, and truncate
692 	 *          before the end of the first message.
693 	 */
694 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
695 	ATF_REQUIRE(message != NULL);
696 	putfds(message, putfd, 1);
697 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
698 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
699 	recv_cmsg(fd[1], message, CMSG_SPACE(0), MSG_CTRUNC);
700 	ATF_REQUIRE(getnfds() == nfds);
701 	free(message);
702 
703 	/*
704 	 * Case 2c: Send two descriptors in separate messages, and truncate
705 	 *          after the end of the first message.  We should still
706 	 *          receive the first message.
707 	 */
708 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
709 	ATF_REQUIRE(message != NULL);
710 	putfds(message, putfd, 1);
711 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
712 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
713 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)) + CMSG_SPACE(0),
714 	    MSG_CTRUNC);
715 	rc = close(*(int *)CMSG_DATA(message));
716 	ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
717 	ATF_REQUIRE(getnfds() == nfds);
718 	free(message);
719 
720 	/*
721 	 * Case 3: Send three descriptors in the same message, and leave space
722 	 *         only for the first when receiving the message.
723 	 */
724 	message = malloc(CMSG_SPACE(sizeof(int) * 3));
725 	ATF_REQUIRE(message != NULL);
726 	putfds(message, putfd, 3);
727 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int) * 3));
728 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
729 	ATF_REQUIRE(getnfds() == nfds);
730 	free(message);
731 
732 	close(putfd);
733 	closesocketpair(fd);
734 }
735 
736 /*
737  * Ensure that an attempt to copy a SCM_RIGHTS message to the recipient
738  * fails.  In this case the kernel must dispose of the externalized rights
739  * rather than leaking them into the recipient's file descriptor table.
740  */
741 ATF_TC_WITHOUT_HEAD(copyout_rights_error);
742 ATF_TC_BODY(copyout_rights_error, tc)
743 {
744 	struct iovec iovec;
745 	struct msghdr msghdr;
746 	char buf[16];
747 	ssize_t len;
748 	int fd[2], error, nfds, putfd;
749 
750 	memset(buf, 0, sizeof(buf));
751 	domainsocketpair(fd);
752 	devnull(&putfd);
753 	nfds = getnfds();
754 
755 	len = sendfd_payload(fd[0], putfd, buf, sizeof(buf));
756 	ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
757 
758 	bzero(&msghdr, sizeof(msghdr));
759 
760 	iovec.iov_base = buf;
761 	iovec.iov_len = sizeof(buf);
762 	msghdr.msg_control = (char *)-1; /* trigger EFAULT */
763 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
764 	msghdr.msg_iov = &iovec;
765 	msghdr.msg_iovlen = 1;
766 
767 	len = recvmsg(fd[1], &msghdr, 0);
768 	error = errno;
769 	ATF_REQUIRE_MSG(len == -1, "recvmsg succeeded: %zd", len);
770 	ATF_REQUIRE_MSG(errno == EFAULT, "expected EFAULT, got %d (%s)",
771 	    error, strerror(errno));
772 
773 	/* Verify that no FDs were leaked. */
774 	ATF_REQUIRE(getnfds() == nfds);
775 
776 	close(putfd);
777 	closesocketpair(fd);
778 }
779 
780 /*
781  * Verify that we can handle empty rights messages.
782  */
783 ATF_TC_WITHOUT_HEAD(empty_rights_message);
784 ATF_TC_BODY(empty_rights_message, tc)
785 {
786 	struct iovec iov;
787 	struct msghdr msghdr;
788 	struct cmsghdr cmsg;
789 	char *cm, message[CMSG_SPACE(0) + CMSG_SPACE(sizeof(int))];
790 	ssize_t len;
791 	int error, fd[2], putfd;
792 
793 	domainsocketpair(fd);
794 	devnull(&putfd);
795 
796 	memset(&msghdr, 0, sizeof(msghdr));
797 	iov.iov_base = NULL;
798 	iov.iov_len = 0;
799 	msghdr.msg_iov = &iov;
800 	msghdr.msg_iovlen = 1;
801 
802 	/*
803 	 * Try sending incorrect empty message.  On 64-bit platforms, where
804 	 * CMSG_SPACE(0) > sizeof(struct cmsghdr), this will exercise
805 	 * an edge case.
806 	 */
807 	cmsg = (struct cmsghdr ){
808 	    .cmsg_len = sizeof(struct cmsghdr),	/* not CMSG_LEN(0)! */
809 	    .cmsg_level = SOL_SOCKET,
810 	    .cmsg_type = SCM_RIGHTS,
811 	};
812 	msghdr.msg_control = &cmsg;
813 	msghdr.msg_controllen = CMSG_SPACE(0);
814 
815 	len = sendmsg(fd[0], &msghdr, 0);
816 	if (CMSG_LEN(0) != sizeof(struct cmsghdr))
817 		ATF_REQUIRE(len == -1 && errno == EINVAL);
818 	else
819 		ATF_REQUIRE(len == 0);
820 
821 	/*
822 	 * Try sending an empty message followed by a non-empty message.
823 	 */
824 	cm = message;
825 	putfds(cm, -1, 0);
826 	cm += CMSG_SPACE(0);
827 	putfds(cm, putfd, 1);
828 	msghdr.msg_control = message;
829 	msghdr.msg_controllen = sizeof(message);
830 
831 	len = sendmsg(fd[0], &msghdr, 0);
832 	ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
833 
834 	/* Only the non-empty message should be received. */
835 	len = recvmsg(fd[1], &msghdr, 0);
836 	ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
837 	ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int)));
838 	error = close(*(int *)CMSG_DATA(msghdr.msg_control));
839 	ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
840 
841 	/*
842 	 * Now try sending with the non-empty message before the empty message.
843 	 */
844 	cm = message;
845 	putfds(cm, putfd, 1);
846 	cm += CMSG_SPACE(sizeof(int));
847 	putfds(cm, -1, 0);
848 
849 	memset(&msghdr, 0, sizeof(msghdr));
850 	iov.iov_base = NULL;
851 	iov.iov_len = 0;
852 	msghdr.msg_control = message;
853 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
854 	msghdr.msg_iov = &iov;
855 	msghdr.msg_iovlen = 1;
856 
857 	len = sendmsg(fd[0], &msghdr, 0);
858 	ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
859 
860 	/* Only the non-empty message should be received. */
861 	len = recvmsg(fd[1], &msghdr, 0);
862 	ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
863 	ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int)));
864 	error = close(*(int *)CMSG_DATA(msghdr.msg_control));
865 	ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
866 
867 	(void)close(putfd);
868 }
869 
870 ATF_TP_ADD_TCS(tp)
871 {
872 
873 	ATF_TP_ADD_TC(tp, simple_send_fd);
874 	ATF_TP_ADD_TC(tp, simple_send_fd_msg_cmsg_cloexec);
875 	ATF_TP_ADD_TC(tp, send_and_close);
876 	ATF_TP_ADD_TC(tp, send_and_cancel);
877 	ATF_TP_ADD_TC(tp, send_and_shutdown);
878 	ATF_TP_ADD_TC(tp, send_a_lot);
879 	ATF_TP_ADD_TC(tp, two_files);
880 	ATF_TP_ADD_TC(tp, bundle);
881 	ATF_TP_ADD_TC(tp, bundle_cancel);
882 	ATF_TP_ADD_TC(tp, devfs_orphan);
883 	ATF_TP_ADD_TC(tp, rights_creds_payload);
884 	ATF_TP_ADD_TC(tp, truncated_rights);
885 	ATF_TP_ADD_TC(tp, copyout_rights_error);
886 	ATF_TP_ADD_TC(tp, empty_rights_message);
887 
888 	return (atf_no_error());
889 }
890