xref: /freebsd/tests/sys/kern/unix_passfd_test.c (revision c27f7d6b9cf6d4ab01cb3d0972726c14e0aca146)
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/param.h>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/sysctl.h>
33 #include <sys/time.h>
34 #include <sys/resource.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 #if !defined(TEST_PROTO)
48 #error Need TEST_PROTO defined to SOCK_STREAM or SOCK_DGRAM
49 #endif
50 
51 /*
52  * UNIX domain sockets allow file descriptors to be passed via "ancillary
53  * data", or control messages.  This regression test is intended to exercise
54  * this facility, both performing some basic tests that it operates, and also
55  * causing some kernel edge cases to execute, such as garbage collection when
56  * there are cyclic file descriptor references.  Right now we test only with
57  * stream sockets, but ideally we'd also test with datagram sockets.
58  */
59 
60 static void
61 domainsocketpair(int *fdp)
62 {
63 
64 	ATF_REQUIRE_MSG(socketpair(PF_UNIX, TEST_PROTO, 0, fdp) != -1,
65 	    "socketpair(PF_UNIX, %u) failed: %s", TEST_PROTO, strerror(errno));
66 }
67 
68 static void
69 closesocketpair(int *fdp)
70 {
71 
72 	close(fdp[0]);
73 	close(fdp[1]);
74 }
75 
76 static void
77 devnull(int *fdp)
78 {
79 	int fd;
80 
81 	fd = open("/dev/null", O_RDONLY);
82 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
83 	*fdp = fd;
84 }
85 
86 static void
87 tempfile(int *fdp)
88 {
89 	char path[PATH_MAX];
90 	int fd;
91 
92 	snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
93 	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
94 	fd = mkstemp(path);
95 	ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
96 	(void)unlink(path);
97 	*fdp = fd;
98 }
99 
100 static void
101 dofstat(int fd, struct stat *sb)
102 {
103 
104 	ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
105 	    "fstat failed: %s", strerror(errno));
106 }
107 
108 static int
109 getnfds(void)
110 {
111 	size_t len;
112 	int mib[4], n, rc;
113 
114 	len = sizeof(n);
115 	mib[0] = CTL_KERN;
116 	mib[1] = KERN_PROC;
117 	mib[2] = KERN_PROC_NFDS;
118 	mib[3] = 0;
119 
120 	rc = sysctl(mib, 4, &n, &len, NULL, 0);
121 	ATF_REQUIRE_MSG(rc != -1, "sysctl(KERN_PROC_NFDS) failed");
122 	return (n);
123 }
124 
125 static int
126 openfiles(void)
127 {
128 	int files;
129 	size_t len = sizeof(files);
130 
131 	ATF_REQUIRE(sysctlbyname("kern.openfiles", &files, &len, NULL, 0) == 0);
132 
133 	return (files);
134 }
135 
136 static void
137 putfds(char *buf, int fd, int nfds)
138 {
139 	struct cmsghdr *cm;
140 	int *fdp, i;
141 
142 	cm = (struct cmsghdr *)buf;
143 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
144 	cm->cmsg_level = SOL_SOCKET;
145 	cm->cmsg_type = SCM_RIGHTS;
146 	for (fdp = (int *)CMSG_DATA(cm), i = 0; i < nfds; i++)
147 		*fdp++ = fd;
148 }
149 
150 static void
151 samefile(struct stat *sb1, struct stat *sb2)
152 {
153 
154 	ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
155 	ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
156 }
157 
158 static ssize_t
159 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
160 {
161 	struct iovec iovec;
162 	char message[CMSG_SPACE(sizeof(int))];
163 	struct msghdr msghdr;
164 
165 	bzero(&msghdr, sizeof(msghdr));
166 	bzero(&message, sizeof(message));
167 
168 	msghdr.msg_control = message;
169 	msghdr.msg_controllen = sizeof(message);
170 
171 	iovec.iov_base = payload;
172 	iovec.iov_len = paylen;
173 
174 	msghdr.msg_iov = &iovec;
175 	msghdr.msg_iovlen = 1;
176 
177 	putfds(message, send_fd, 1);
178 	return (sendmsg(sockfd, &msghdr, 0));
179 }
180 
181 static void
182 sendfd(int sockfd, int send_fd)
183 {
184 	ssize_t len;
185 	char ch;
186 
187 	ch = 0;
188 	len = sendfd_payload(sockfd, send_fd, &ch, sizeof(ch));
189 	ATF_REQUIRE_MSG(len == sizeof(ch),
190 	    "sendmsg: %zd bytes sent; expected %zu; %s", len, sizeof(ch),
191 	    strerror(errno));
192 }
193 
194 static bool
195 localcreds(int sockfd)
196 {
197 	socklen_t sz;
198 	int rc, val;
199 
200 	sz = sizeof(val);
201 	rc = getsockopt(sockfd, 0, LOCAL_CREDS, &val, &sz);
202 	ATF_REQUIRE_MSG(rc != -1, "getsockopt(LOCAL_CREDS) failed: %s",
203 	    strerror(errno));
204 	return (val != 0);
205 }
206 
207 static ssize_t
208 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen,
209     size_t cmsgsz, int recvmsg_flags)
210 {
211 	struct cmsghdr *cmsghdr;
212 	struct msghdr msghdr;
213 	struct iovec iovec;
214 	char *message;
215 	ssize_t len;
216 	bool foundcreds;
217 
218 	bzero(&msghdr, sizeof(msghdr));
219 	message = malloc(cmsgsz);
220 	ATF_REQUIRE(message != NULL);
221 
222 	msghdr.msg_control = message;
223 	msghdr.msg_controllen = cmsgsz;
224 
225 	iovec.iov_base = buf;
226 	iovec.iov_len = buflen;
227 
228 	msghdr.msg_iov = &iovec;
229 	msghdr.msg_iovlen = 1;
230 
231 	len = recvmsg(sockfd, &msghdr, recvmsg_flags);
232 	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
233 
234 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
235 	ATF_REQUIRE_MSG(cmsghdr != NULL,
236 	    "recvmsg: did not receive control message");
237 	foundcreds = false;
238 	*recv_fd = -1;
239 	for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
240 		if (cmsghdr->cmsg_level == SOL_SOCKET &&
241 		    cmsghdr->cmsg_type == SCM_RIGHTS &&
242 		    cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
243 			memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
244 			ATF_REQUIRE(*recv_fd != -1);
245 		} else if (cmsghdr->cmsg_level == SOL_SOCKET &&
246 		    cmsghdr->cmsg_type == SCM_CREDS)
247 			foundcreds = true;
248 	}
249 	ATF_REQUIRE_MSG(*recv_fd != -1,
250 	    "recvmsg: did not receive single-fd message");
251 	ATF_REQUIRE_MSG(!localcreds(sockfd) || foundcreds,
252 	    "recvmsg: expected credentials were not received");
253 	ATF_REQUIRE_MSG((msghdr.msg_flags & MSG_TRUNC) == 0,
254 	    "recvmsg: MSG_TRUNC is set while buffer is sufficient");
255 
256 	return (len);
257 }
258 
259 static void
260 recvfd(int sockfd, int *recv_fd, int flags)
261 {
262 	ssize_t len;
263 	char ch = 0;
264 
265 	len = recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch),
266 	    CMSG_SPACE(sizeof(int)), flags);
267 	ATF_REQUIRE_MSG((size_t)len == sizeof(ch),
268 	    "recvmsg: %zd bytes received; expected %zd", len, sizeof(ch));
269 }
270 
271 #if TEST_PROTO == SOCK_STREAM
272 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
273 #define	LOCAL_RECVSPACE_SYSCTL	"net.local.stream.recvspace"
274 #elif TEST_PROTO == SOCK_DGRAM
275 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.dgram.maxdgram"
276 #define	LOCAL_RECVSPACE_SYSCTL	"net.local.dgram.recvspace"
277 #endif
278 
279 static u_long
280 getsendspace(void)
281 {
282 	u_long sendspace;
283 
284 	ATF_REQUIRE_MSG(sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
285             &(size_t){sizeof(u_long)}, NULL, 0) != -1,
286 	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
287 
288 	return (sendspace);
289 }
290 
291 static u_long
292 getrecvspace(void)
293 {
294 	u_long recvspace;
295 
296 	ATF_REQUIRE_MSG(sysctlbyname(LOCAL_RECVSPACE_SYSCTL, &recvspace,
297             &(size_t){sizeof(u_long)}, NULL, 0) != -1,
298 	    "sysctl %s failed: %s", LOCAL_RECVSPACE_SYSCTL, strerror(errno));
299 
300 	return (recvspace);
301 }
302 
303 /*
304  * Fill socket to a state when next max sized send would fail with EAGAIN.
305  */
306 static void
307 fill(int fd)
308 {
309 	u_long sendspace;
310 	void *buf;
311 
312 	sendspace = getsendspace();
313 	ATF_REQUIRE((buf = malloc(sendspace)) != NULL);
314 
315 	ATF_REQUIRE_MSG(fcntl(fd, F_SETFL, O_NONBLOCK) != -1,
316 	    "fcntl(O_NONBLOCK) failed: %s", strerror(errno));
317 
318 #if TEST_PROTO == SOCK_STREAM
319 	do {} while (send(fd, buf, sendspace, 0) == (ssize_t)sendspace);
320 #elif TEST_PROTO == SOCK_DGRAM
321 	u_long recvspace = getrecvspace();
322 
323 	for (ssize_t sent = 0;
324 	    sent + sendspace + sizeof(struct sockaddr) < recvspace;
325 	    sent += sendspace + sizeof(struct sockaddr))
326 		ATF_REQUIRE(send(fd, buf, sendspace, 0) == (ssize_t)sendspace);
327 #endif
328 	free(buf);
329 }
330 
331 /*
332  * Put a temporary file into a UNIX domain socket, then take it out and make
333  * sure it's the same file.  First time around, don't close the reference
334  * after sending.
335  */
336 ATF_TC_WITHOUT_HEAD(simple_send_fd);
337 ATF_TC_BODY(simple_send_fd, tc)
338 {
339 	struct stat getfd_stat, putfd_stat;
340 	int fd[2], getfd, putfd;
341 
342 	domainsocketpair(fd);
343 	tempfile(&putfd);
344 	dofstat(putfd, &putfd_stat);
345 	sendfd(fd[0], putfd);
346 	recvfd(fd[1], &getfd, 0);
347 	dofstat(getfd, &getfd_stat);
348 	samefile(&putfd_stat, &getfd_stat);
349 	close(putfd);
350 	close(getfd);
351 	closesocketpair(fd);
352 }
353 
354 /*
355  * Like simple_send_fd but also sets MSG_CMSG_CLOEXEC and checks that the
356  * received file descriptor has the FD_CLOEXEC flag set.
357  */
358 ATF_TC_WITHOUT_HEAD(simple_send_fd_msg_cmsg_cloexec);
359 ATF_TC_BODY(simple_send_fd_msg_cmsg_cloexec, tc)
360 {
361 	struct stat getfd_stat, putfd_stat;
362 	int fd[2], getfd, putfd;
363 
364 	domainsocketpair(fd);
365 	tempfile(&putfd);
366 	dofstat(putfd, &putfd_stat);
367 	sendfd(fd[0], putfd);
368 	recvfd(fd[1], &getfd, MSG_CMSG_CLOEXEC);
369 	dofstat(getfd, &getfd_stat);
370 	samefile(&putfd_stat, &getfd_stat);
371 	ATF_REQUIRE_EQ_MSG(fcntl(getfd, F_GETFD) & FD_CLOEXEC, FD_CLOEXEC,
372 	    "FD_CLOEXEC not set on the received file descriptor");
373 	close(putfd);
374 	close(getfd);
375 	closesocketpair(fd);
376 }
377 
378 /*
379  * Same as simple_send_fd, only close the file reference after sending, so that
380  * the only reference is the descriptor in the UNIX domain socket buffer.
381  */
382 ATF_TC_WITHOUT_HEAD(send_and_close);
383 ATF_TC_BODY(send_and_close, tc)
384 {
385 	struct stat getfd_stat, putfd_stat;
386 	int fd[2], getfd, putfd;
387 
388 	domainsocketpair(fd);
389 	tempfile(&putfd);
390 	dofstat(putfd, &putfd_stat);
391 	sendfd(fd[0], putfd);
392 	close(putfd);
393 	recvfd(fd[1], &getfd, 0);
394 	dofstat(getfd, &getfd_stat);
395 	samefile(&putfd_stat, &getfd_stat);
396 	close(getfd);
397 	closesocketpair(fd);
398 }
399 
400 /*
401  * Put a temporary file into a UNIX domain socket, then close both endpoints
402  * causing garbage collection to kick off.
403  */
404 ATF_TC_WITHOUT_HEAD(send_and_cancel);
405 ATF_TC_BODY(send_and_cancel, tc)
406 {
407 	int fd[2], putfd;
408 
409 	domainsocketpair(fd);
410 	tempfile(&putfd);
411 	sendfd(fd[0], putfd);
412 	close(putfd);
413 	closesocketpair(fd);
414 }
415 
416 /*
417  * Send file then shutdown receive side to exercise unp_dispose() call
418  * via soshutdown().  Check that shutdown(SHUT_RD) would gc the file
419  * reference sitting in the receive buffer.  There is no good way of
420  * checking that except using global open file count.
421  */
422 ATF_TC_WITHOUT_HEAD(send_and_shutdown);
423 ATF_TC_BODY(send_and_shutdown, tc)
424 {
425 	int fd[2], putfd, nfiles;
426 
427 	domainsocketpair(fd);
428 	tempfile(&putfd);
429 	sendfd(fd[0], putfd);
430 	nfiles = openfiles();
431 	close(putfd);
432 	ATF_REQUIRE(openfiles() == nfiles);
433 	shutdown(fd[1], SHUT_RD);
434 	ATF_REQUIRE(openfiles() == nfiles - 1);
435 	closesocketpair(fd);
436 }
437 
438 /*
439  * Send maximum possible SCM_RIGHTS message.
440  * Internally the file descriptors are converted from integers to pointers
441  * and stored in a single mbuf cluster.  Check that we can not send too much
442  * and that we can successfully send maximum possible amount.  Check that we
443  * can not exploit getrlimit(3).
444  */
445 #define	MAXFDS	((MCLBYTES - _ALIGN(sizeof(struct cmsghdr)))/sizeof(void *))
446 ATF_TC_WITHOUT_HEAD(send_a_lot);
447 ATF_TC_BODY(send_a_lot, tc)
448 {
449 	struct msghdr msghdr;
450 	struct iovec iov;
451 	struct rlimit rlim;
452 	int fd[2], nfds;
453 	char *cmsg, ch;
454 
455 	domainsocketpair(fd);
456 	cmsg = malloc(CMSG_SPACE((MAXFDS + 1) * sizeof(int)));
457 	ATF_REQUIRE(cmsg != NULL);
458 	iov.iov_base = &ch;
459 	iov.iov_len = sizeof(ch);
460 	msghdr = (struct msghdr ){
461 		.msg_control = cmsg,
462 		.msg_controllen = CMSG_LEN((MAXFDS + 1) * sizeof(int)),
463 		.msg_iov = &iov,
464 		.msg_iovlen = 1,
465 	};
466 
467 	/* Sending too much fails. */
468 	putfds(cmsg, fd[0], MAXFDS + 1);
469 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == -1);
470 	ATF_REQUIRE(errno == EMSGSIZE);
471 
472 	/* Sending just the right amount works and everything is received. */
473 	putfds(cmsg, fd[0], MAXFDS);
474 	msghdr.msg_controllen = CMSG_LEN(MAXFDS * sizeof(int));
475 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1);
476 	nfds = getnfds();
477 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1);
478 	ATF_REQUIRE(getnfds() == (int)(nfds + MAXFDS));
479 
480 	/* Limit our process open files... */
481 	ATF_REQUIRE(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
482 	nfds = rlim.rlim_cur = getnfds();
483 	ATF_REQUIRE(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
484 
485 	/* ... and try to receive a single descriptor. */
486 	putfds(cmsg, fd[0], 1);
487 	msghdr.msg_controllen = CMSG_LEN(sizeof(int));
488 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1);
489 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == -1);
490 	/* Such attempt shall fail with EMFILE. */
491 	ATF_REQUIRE(errno == EMFILE);
492 	ATF_REQUIRE(getnfds() == nfds);
493 #if TEST_PROTO == SOCK_STREAM
494 	/*
495 	 * For the SOCK_STREAM the above attempt shall free the control in
496 	 * the kernel, so that socket isn't left in a stuck state.  Next read
497 	 * shall bring us the normal data only.  The stream data shall not
498 	 * miss a byte.
499 	 */
500 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1);
501 	ATF_REQUIRE(msghdr.msg_controllen == 0);
502 #elif TEST_PROTO == SOCK_DGRAM
503 	/*
504 	 * For SOCK_DGRAM there are two options for the previously failed
505 	 * syscall: strip the control leaving datagram in the socket or
506 	 * drop the whole datagram.  Our implementation drops the whole
507 	 * datagram.
508 	 */
509 	ATF_REQUIRE(recvmsg(fd[1], &msghdr, MSG_DONTWAIT) == -1);
510 	ATF_REQUIRE(errno == EAGAIN);
511 #endif
512 }
513 
514 /*
515  * Exersize condition when SCM_RIGHTS is successfully internalized, but
516  * message delivery fails due to receive buffer overflow.  Check that no
517  * file descriptors are leaked.
518  */
519 ATF_TC_WITHOUT_HEAD(send_overflow);
520 ATF_TC_BODY(send_overflow, tc)
521 {
522 	void *buf;
523 	ssize_t len;
524 	int fd[2], putfd, nfiles;
525 	int sendspace;
526 
527 	sendspace = (int)getsendspace();
528 	ATF_REQUIRE((buf = malloc(sendspace)) != NULL);
529 
530 	domainsocketpair(fd);
531 	fill(fd[0]);
532 	nfiles = openfiles();
533 	tempfile(&putfd);
534 	len = sendfd_payload(fd[0], putfd, buf, sendspace);
535 #if TEST_PROTO == SOCK_STREAM
536 	ATF_REQUIRE_MSG(len == -1 && errno == EAGAIN,
537 	    "sendmsg: %zd bytes sent, errno %d", len, errno);
538 #elif TEST_PROTO == SOCK_DGRAM
539 	ATF_REQUIRE_MSG(len == -1 && errno == ENOBUFS,
540 	    "sendmsg: %zd bytes sent, errno %d", len, errno);
541 #endif
542 	close(putfd);
543 	ATF_REQUIRE(nfiles == openfiles());
544 	closesocketpair(fd);
545 }
546 
547 /*
548  * Make sure that we do not receive descriptors with MSG_PEEK.
549  */
550 ATF_TC_WITHOUT_HEAD(peek);
551 ATF_TC_BODY(peek, tc)
552 {
553 	int fd[2], getfd, putfd, nfds;
554 
555 	domainsocketpair(fd);
556 	tempfile(&putfd);
557 	nfds = getnfds();
558 	sendfd(fd[0], putfd);
559 	ATF_REQUIRE(getnfds() == nfds);
560 
561 	/* First make MSG_PEEK recvmsg(2)... */
562 	char cbuf[CMSG_SPACE(sizeof(int))];
563 	char buf[1];
564 	struct iovec iov = {
565 		.iov_base = buf,
566 		.iov_len = sizeof(buf)
567 	};
568 	struct msghdr msghdr = {
569 		.msg_iov = &iov,
570 		.msg_iovlen = 1,
571 		.msg_control = cbuf,
572 		.msg_controllen = sizeof(cbuf),
573 	};
574 	ATF_REQUIRE(1 == recvmsg(fd[1], &msghdr, MSG_PEEK));
575 	for (struct cmsghdr *cmsghdr = CMSG_FIRSTHDR(&msghdr);
576 	     cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
577 		/* Usually this is some garbage. */
578 		printf("level %d type %d len %u\n",
579 		    cmsghdr->cmsg_level, cmsghdr->cmsg_type, cmsghdr->cmsg_len);
580 	}
581 
582 	/* ... and make sure we did not receive any descriptors! */
583 	ATF_REQUIRE(getnfds() == nfds);
584 
585 	/* Now really receive a descriptor. */
586 	recvfd(fd[1], &getfd, 0);
587 	ATF_REQUIRE(getnfds() == nfds + 1);
588 	close(putfd);
589 	close(getfd);
590 	closesocketpair(fd);
591 }
592 
593 /*
594  * Send two files.  Then receive them.  Make sure they are returned in the
595  * right order, and both get there.
596  */
597 ATF_TC_WITHOUT_HEAD(two_files);
598 ATF_TC_BODY(two_files, tc)
599 {
600 	struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
601 	int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
602 
603 	domainsocketpair(fd);
604 	tempfile(&putfd_1);
605 	tempfile(&putfd_2);
606 	dofstat(putfd_1, &putfd_1_stat);
607 	dofstat(putfd_2, &putfd_2_stat);
608 	sendfd(fd[0], putfd_1);
609 	sendfd(fd[0], putfd_2);
610 	close(putfd_1);
611 	close(putfd_2);
612 	recvfd(fd[1], &getfd_1, 0);
613 	recvfd(fd[1], &getfd_2, 0);
614 	dofstat(getfd_1, &getfd_1_stat);
615 	dofstat(getfd_2, &getfd_2_stat);
616 	samefile(&putfd_1_stat, &getfd_1_stat);
617 	samefile(&putfd_2_stat, &getfd_2_stat);
618 	close(getfd_1);
619 	close(getfd_2);
620 	closesocketpair(fd);
621 }
622 
623 /*
624  * Big bundling test.  Send an endpoint of the UNIX domain socket over itself,
625  * closing the door behind it.
626  */
627 ATF_TC_WITHOUT_HEAD(bundle);
628 ATF_TC_BODY(bundle, tc)
629 {
630 	int fd[2], getfd;
631 
632 	domainsocketpair(fd);
633 
634 	sendfd(fd[0], fd[0]);
635 	close(fd[0]);
636 	recvfd(fd[1], &getfd, 0);
637 	close(getfd);
638 	close(fd[1]);
639 }
640 
641 /*
642  * Big bundling test part two: Send an endpoint of the UNIX domain socket over
643  * itself, close the door behind it, and never remove it from the other end.
644  */
645 ATF_TC_WITHOUT_HEAD(bundle_cancel);
646 ATF_TC_BODY(bundle_cancel, tc)
647 {
648 	int fd[2];
649 
650 	domainsocketpair(fd);
651 	sendfd(fd[0], fd[0]);
652 	sendfd(fd[1], fd[0]);
653 	closesocketpair(fd);
654 }
655 
656 /*
657  * Test for PR 151758: Send an character device over the UNIX domain socket
658  * and then close both sockets to orphan the device.
659  */
660 ATF_TC_WITHOUT_HEAD(devfs_orphan);
661 ATF_TC_BODY(devfs_orphan, tc)
662 {
663 	int fd[2], putfd;
664 
665 	domainsocketpair(fd);
666 	devnull(&putfd);
667 	sendfd(fd[0], putfd);
668 	close(putfd);
669 	closesocketpair(fd);
670 }
671 
672 /*
673  * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
674  * control message to the data. Sender sends large payload using a non-blocking
675  * socket. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and
676  * receiver receives truncated data.
677  */
678 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
679 ATF_TC_BODY(rights_creds_payload, tc)
680 {
681 	const int on = 1;
682 	u_long sendspace;
683 	ssize_t len, rlen;
684 	void *buf;
685 	int fd[2], getfd, putfd, rc;
686 
687 	sendspace = getsendspace();
688 	buf = calloc(1, sendspace);
689 	ATF_REQUIRE(buf != NULL);
690 
691 	domainsocketpair(fd);
692 	tempfile(&putfd);
693 
694 	rc = fcntl(fd[0], F_SETFL, O_NONBLOCK);
695 	ATF_REQUIRE_MSG(rc != -1, "fcntl(O_NONBLOCK) failed: %s",
696 	    strerror(errno));
697 	rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
698 	ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
699 	    strerror(errno));
700 
701 	len = sendfd_payload(fd[0], putfd, buf, sendspace);
702 	ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno));
703 #if TEST_PROTO == SOCK_STREAM
704 	ATF_REQUIRE_MSG((size_t)len < sendspace,
705 	    "sendmsg: %zd bytes sent, expected < %lu", len, sendspace);
706 #endif
707 #if TEST_PROTO == SOCK_DGRAM
708 	/*
709 	 * sendmsg(2) can't truncate datagrams, only recvmsg(2) can.  There are
710 	 * two options for the kernel here: either accept the datagram with
711 	 * slight overcommit of the socket buffer space or return ENOBUFS for a
712 	 * datagram that is smaller or equal to the socket buffer space.  Our
713 	 * implementation does overcommit.  Explanation is simple: from our
714 	 * side we see space available, we have no idea that remote side has
715 	 * LOCAL_CREDS set.  From our side we expect sendmsg(2) to succeed.
716 	 */
717 	ATF_REQUIRE_MSG((size_t)len == sendspace,
718 	    "sendmsg: %zd bytes sent, expected %lu", len, sendspace);
719 #endif
720 	rlen = recvfd_payload(fd[1], &getfd, buf, len,
721 	    CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0);
722 	ATF_REQUIRE_MSG(rlen == len,
723 	    "recvmsg: %zd bytes received; expected %zd", rlen, len);
724 
725 	close(putfd);
726 	close(getfd);
727 	closesocketpair(fd);
728 }
729 
730 static void
731 send_cmsg(int sockfd, void *cmsg, size_t cmsgsz)
732 {
733 	struct iovec iov;
734 	struct msghdr msghdr;
735 	ssize_t len;
736 	char ch;
737 
738 	ch = 0;
739 	bzero(&msghdr, sizeof(msghdr));
740 
741 	iov.iov_base = &ch;
742 	iov.iov_len = sizeof(ch);
743 	msghdr.msg_control = cmsg;
744 	msghdr.msg_controllen = cmsgsz;
745 	msghdr.msg_iov = &iov;
746 	msghdr.msg_iovlen = 1;
747 
748 	len = sendmsg(sockfd, &msghdr, 0);
749 	ATF_REQUIRE_MSG(len != -1,
750 	    "sendmsg failed: %s", strerror(errno));
751 	ATF_REQUIRE_MSG(len == sizeof(ch),
752 	    "sendmsg: %zd bytes sent; expected %zu", len, sizeof(ch));
753 }
754 
755 static void
756 recv_cmsg(int sockfd, char *cmsg, size_t cmsgsz, int flags)
757 {
758 	struct iovec iov;
759 	struct msghdr msghdr;
760 	ssize_t len;
761 	char ch;
762 
763 	ch = 0;
764 	bzero(&msghdr, sizeof(msghdr));
765 
766 	iov.iov_base = &ch;
767 	iov.iov_len = sizeof(ch);
768 	msghdr.msg_control = cmsg;
769 	msghdr.msg_controllen = cmsgsz;
770 	msghdr.msg_iov = &iov;
771 	msghdr.msg_iovlen = 1;
772 
773 	len = recvmsg(sockfd, &msghdr, 0);
774 	ATF_REQUIRE_MSG(len != -1,
775 	    "recvmsg failed: %s", strerror(errno));
776 	ATF_REQUIRE_MSG(len == sizeof(ch),
777 	    "recvmsg: %zd bytes received; expected %zu", len, sizeof(ch));
778 	ATF_REQUIRE_MSG((msghdr.msg_flags & flags) == flags,
779 	    "recvmsg: got flags %#x; expected %#x", msghdr.msg_flags, flags);
780 }
781 
782 /*
783  * Test for PR 131876.  Receiver uses a control message buffer that is too
784  * small for the incoming SCM_RIGHTS message, so the message is truncated.
785  * The kernel must not leak the copied right into the receiver's namespace.
786  */
787 ATF_TC_WITHOUT_HEAD(truncated_rights);
788 ATF_TC_BODY(truncated_rights, tc)
789 {
790 	char *message;
791 	int fd[2], nfds, putfd, rc;
792 
793 	domainsocketpair(fd);
794 	devnull(&putfd);
795 	nfds = getnfds();
796 
797 	/*
798 	 * Case 1: Send a single descriptor and truncate the message.
799 	 */
800 	message = malloc(CMSG_SPACE(sizeof(int)));
801 	ATF_REQUIRE(message != NULL);
802 	putfds(message, putfd, 1);
803 	send_cmsg(fd[0], message, CMSG_LEN(sizeof(int)));
804 	recv_cmsg(fd[1], message, CMSG_LEN(0), MSG_CTRUNC);
805 	ATF_REQUIRE(getnfds() == nfds);
806 	free(message);
807 
808 	/*
809 	 * Case 2a: Send two descriptors in separate messages, and truncate at
810 	 *          the boundary between the two messages.  We should still
811 	 *          receive the first message.
812 	 */
813 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
814 	ATF_REQUIRE(message != NULL);
815 	putfds(message, putfd, 1);
816 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
817 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
818 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
819 	rc = close(*(int *)CMSG_DATA(message));
820 	ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
821 	ATF_REQUIRE(getnfds() == nfds);
822 	free(message);
823 
824 	/*
825 	 * Case 2b: Send two descriptors in separate messages, and truncate
826 	 *          before the end of the first message.
827 	 */
828 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
829 	ATF_REQUIRE(message != NULL);
830 	putfds(message, putfd, 1);
831 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
832 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
833 	recv_cmsg(fd[1], message, CMSG_SPACE(0), MSG_CTRUNC);
834 	ATF_REQUIRE(getnfds() == nfds);
835 	free(message);
836 
837 	/*
838 	 * Case 2c: Send two descriptors in separate messages, and truncate
839 	 *          after the end of the first message.  We should still
840 	 *          receive the first message.
841 	 */
842 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
843 	ATF_REQUIRE(message != NULL);
844 	putfds(message, putfd, 1);
845 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
846 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
847 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)) + CMSG_SPACE(0),
848 	    MSG_CTRUNC);
849 	rc = close(*(int *)CMSG_DATA(message));
850 	ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
851 	ATF_REQUIRE(getnfds() == nfds);
852 	free(message);
853 
854 	/*
855 	 * Case 3: Send three descriptors in the same message, and leave space
856 	 *         only for the first when receiving the message.
857 	 */
858 	message = malloc(CMSG_SPACE(sizeof(int) * 3));
859 	ATF_REQUIRE(message != NULL);
860 	putfds(message, putfd, 3);
861 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int) * 3));
862 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
863 	ATF_REQUIRE(getnfds() == nfds);
864 	free(message);
865 
866 	close(putfd);
867 	closesocketpair(fd);
868 }
869 
870 /*
871  * Ensure that an attempt to copy a SCM_RIGHTS message to the recipient
872  * fails.  In this case the kernel must dispose of the externalized rights
873  * rather than leaking them into the recipient's file descriptor table.
874  */
875 ATF_TC_WITHOUT_HEAD(copyout_rights_error);
876 ATF_TC_BODY(copyout_rights_error, tc)
877 {
878 	struct iovec iovec;
879 	struct msghdr msghdr;
880 	char buf[16];
881 	ssize_t len;
882 	int fd[2], error, nfds, putfd;
883 
884 	memset(buf, 0, sizeof(buf));
885 	domainsocketpair(fd);
886 	devnull(&putfd);
887 	nfds = getnfds();
888 
889 	len = sendfd_payload(fd[0], putfd, buf, sizeof(buf));
890 	ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
891 
892 	bzero(&msghdr, sizeof(msghdr));
893 
894 	iovec.iov_base = buf;
895 	iovec.iov_len = sizeof(buf);
896 	msghdr.msg_control = (char *)-1; /* trigger EFAULT */
897 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
898 	msghdr.msg_iov = &iovec;
899 	msghdr.msg_iovlen = 1;
900 
901 	len = recvmsg(fd[1], &msghdr, 0);
902 	error = errno;
903 	ATF_REQUIRE_MSG(len == -1, "recvmsg succeeded: %zd", len);
904 	ATF_REQUIRE_MSG(errno == EFAULT, "expected EFAULT, got %d (%s)",
905 	    error, strerror(errno));
906 
907 	/* Verify that no FDs were leaked. */
908 	ATF_REQUIRE(getnfds() == nfds);
909 
910 	close(putfd);
911 	closesocketpair(fd);
912 }
913 
914 /*
915  * Verify that we can handle empty rights messages.
916  */
917 ATF_TC_WITHOUT_HEAD(empty_rights_message);
918 ATF_TC_BODY(empty_rights_message, tc)
919 {
920 	struct iovec iov;
921 	struct msghdr msghdr;
922 	struct cmsghdr cmsg;
923 	char *cm, message[CMSG_SPACE(0) + CMSG_SPACE(sizeof(int))];
924 	ssize_t len;
925 	int error, fd[2], putfd;
926 
927 	domainsocketpair(fd);
928 	devnull(&putfd);
929 
930 	memset(&msghdr, 0, sizeof(msghdr));
931 	iov.iov_base = NULL;
932 	iov.iov_len = 0;
933 	msghdr.msg_iov = &iov;
934 	msghdr.msg_iovlen = 1;
935 
936 	/*
937 	 * Try sending incorrect empty message.  On 64-bit platforms, where
938 	 * CMSG_SPACE(0) > sizeof(struct cmsghdr), this will exercise
939 	 * an edge case.
940 	 */
941 	cmsg = (struct cmsghdr ){
942 	    .cmsg_len = sizeof(struct cmsghdr),	/* not CMSG_LEN(0)! */
943 	    .cmsg_level = SOL_SOCKET,
944 	    .cmsg_type = SCM_RIGHTS,
945 	};
946 	msghdr.msg_control = &cmsg;
947 	msghdr.msg_controllen = CMSG_SPACE(0);
948 
949 	len = sendmsg(fd[0], &msghdr, 0);
950 	if (CMSG_LEN(0) != sizeof(struct cmsghdr))
951 		ATF_REQUIRE(len == -1 && errno == EINVAL);
952 	else
953 		ATF_REQUIRE(len == 0);
954 
955 	/*
956 	 * Try sending an empty message followed by a non-empty message.
957 	 */
958 	cm = message;
959 	putfds(cm, -1, 0);
960 	cm += CMSG_SPACE(0);
961 	putfds(cm, putfd, 1);
962 	msghdr.msg_control = message;
963 	msghdr.msg_controllen = sizeof(message);
964 
965 	len = sendmsg(fd[0], &msghdr, 0);
966 	ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
967 
968 	/* Only the non-empty message should be received. */
969 	len = recvmsg(fd[1], &msghdr, 0);
970 	ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
971 	ATF_REQUIRE(msghdr.msg_controllen == CMSG_SPACE(sizeof(int)));
972 	error = close(*(int *)CMSG_DATA(msghdr.msg_control));
973 	ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
974 
975 	/*
976 	 * Now try sending with the non-empty message before the empty message.
977 	 */
978 	cm = message;
979 	putfds(cm, putfd, 1);
980 	cm += CMSG_SPACE(sizeof(int));
981 	putfds(cm, -1, 0);
982 
983 	memset(&msghdr, 0, sizeof(msghdr));
984 	iov.iov_base = NULL;
985 	iov.iov_len = 0;
986 	msghdr.msg_control = message;
987 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
988 	msghdr.msg_iov = &iov;
989 	msghdr.msg_iovlen = 1;
990 
991 	len = sendmsg(fd[0], &msghdr, 0);
992 	ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
993 
994 	/* Only the non-empty message should be received. */
995 	len = recvmsg(fd[1], &msghdr, 0);
996 	ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
997 	ATF_REQUIRE(msghdr.msg_controllen == CMSG_SPACE(sizeof(int)));
998 	error = close(*(int *)CMSG_DATA(msghdr.msg_control));
999 	ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
1000 
1001 	(void)close(putfd);
1002 }
1003 
1004 /*
1005  * Check that sending control creates records in a stream socket, making it
1006  * behave like a seqpacket socket.  If we stack several control+data writes
1007  * on a stream socket, we won't be able to read them all at once, even if we
1008  * provide a buffer large enough to receive all at once.
1009  *
1010  * XXXGL: adding MSG_WAITALL to the recvmsg() flags will make this test stuck.
1011  */
1012 ATF_TC_WITHOUT_HEAD(control_creates_records);
1013 ATF_TC_BODY(control_creates_records, tc)
1014 {
1015 	int fd[2], putfd, getfd;
1016 	char buf[2];
1017 	ssize_t rlen;
1018 
1019 	domainsocketpair(fd);
1020 	tempfile(&putfd);
1021 
1022 	for (int i = 1; i <= 2; i++)
1023 		ATF_REQUIRE(sendfd_payload(fd[0], putfd, buf, 1) == 1);
1024 	ATF_REQUIRE(close(putfd) == 0);
1025 	for (int i = 1; i <= 2; i++) {
1026 		rlen = recvfd_payload(fd[1], &getfd, buf, 2,
1027 		    CMSG_SPACE(sizeof(int)) * 2, 0);
1028 		ATF_REQUIRE_MSG(rlen == 1,
1029 		    "recvmsg: %zd bytes received; expected 1", rlen);
1030 		ATF_REQUIRE(close(getfd) == 0);
1031 	}
1032 	closesocketpair(fd);
1033 }
1034 
1035 ATF_TP_ADD_TCS(tp)
1036 {
1037 
1038 	ATF_TP_ADD_TC(tp, simple_send_fd);
1039 	ATF_TP_ADD_TC(tp, simple_send_fd_msg_cmsg_cloexec);
1040 	ATF_TP_ADD_TC(tp, send_and_close);
1041 	ATF_TP_ADD_TC(tp, send_and_cancel);
1042 	ATF_TP_ADD_TC(tp, send_and_shutdown);
1043 	ATF_TP_ADD_TC(tp, send_a_lot);
1044 	ATF_TP_ADD_TC(tp, send_overflow);
1045 	ATF_TP_ADD_TC(tp, peek);
1046 	ATF_TP_ADD_TC(tp, two_files);
1047 	ATF_TP_ADD_TC(tp, bundle);
1048 	ATF_TP_ADD_TC(tp, bundle_cancel);
1049 	ATF_TP_ADD_TC(tp, devfs_orphan);
1050 	ATF_TP_ADD_TC(tp, rights_creds_payload);
1051 	ATF_TP_ADD_TC(tp, truncated_rights);
1052 	ATF_TP_ADD_TC(tp, copyout_rights_error);
1053 	ATF_TP_ADD_TC(tp, empty_rights_message);
1054 	ATF_TP_ADD_TC(tp, control_creates_records);
1055 
1056 	return (atf_no_error());
1057 }
1058