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