xref: /freebsd/tests/sys/kern/unix_passfd_test.c (revision ce6a89e27cd190313be39bb479880aeda4778436)
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 putfds(char *buf, int fd, int nfds)
123 {
124 	struct cmsghdr *cm;
125 	int *fdp, i;
126 
127 	cm = (struct cmsghdr *)buf;
128 	cm->cmsg_len = CMSG_LEN(nfds * sizeof(int));
129 	cm->cmsg_level = SOL_SOCKET;
130 	cm->cmsg_type = SCM_RIGHTS;
131 	for (fdp = (int *)CMSG_DATA(cm), i = 0; i < nfds; i++)
132 		*fdp++ = fd;
133 }
134 
135 static void
136 samefile(struct stat *sb1, struct stat *sb2)
137 {
138 
139 	ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
140 	ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
141 }
142 
143 static size_t
144 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
145 {
146 	struct iovec iovec;
147 	char message[CMSG_SPACE(sizeof(int))];
148 	struct msghdr msghdr;
149 	ssize_t len;
150 
151 	bzero(&msghdr, sizeof(msghdr));
152 	bzero(&message, sizeof(message));
153 
154 	msghdr.msg_control = message;
155 	msghdr.msg_controllen = sizeof(message);
156 
157 	iovec.iov_base = payload;
158 	iovec.iov_len = paylen;
159 
160 	msghdr.msg_iov = &iovec;
161 	msghdr.msg_iovlen = 1;
162 
163 	putfds(message, send_fd, 1);
164 	len = sendmsg(sockfd, &msghdr, 0);
165 	ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
166 	return ((size_t)len);
167 }
168 
169 static void
170 sendfd(int sockfd, int send_fd)
171 {
172 	size_t len;
173 	char ch;
174 
175 	ch = 0;
176 	len = sendfd_payload(sockfd, send_fd, &ch, sizeof(ch));
177 	ATF_REQUIRE_MSG(len == sizeof(ch),
178 	    "sendmsg: %zu bytes sent; expected %zu; %s", len, sizeof(ch),
179 	    strerror(errno));
180 }
181 
182 static bool
183 localcreds(int sockfd)
184 {
185 	socklen_t sz;
186 	int rc, val;
187 
188 	sz = sizeof(val);
189 	rc = getsockopt(sockfd, 0, LOCAL_CREDS, &val, &sz);
190 	ATF_REQUIRE_MSG(rc != -1, "getsockopt(LOCAL_CREDS) failed: %s",
191 	    strerror(errno));
192 	return (val != 0);
193 }
194 
195 static void
196 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen,
197     size_t cmsgsz)
198 {
199 	struct cmsghdr *cmsghdr;
200 	struct msghdr msghdr;
201 	struct iovec iovec;
202 	char *message;
203 	ssize_t len;
204 	bool foundcreds;
205 
206 	bzero(&msghdr, sizeof(msghdr));
207 	message = malloc(cmsgsz);
208 	ATF_REQUIRE(message != NULL);
209 
210 	msghdr.msg_control = message;
211 	msghdr.msg_controllen = cmsgsz;
212 
213 	iovec.iov_base = buf;
214 	iovec.iov_len = buflen;
215 
216 	msghdr.msg_iov = &iovec;
217 	msghdr.msg_iovlen = 1;
218 
219 	len = recvmsg(sockfd, &msghdr, 0);
220 	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
221 	ATF_REQUIRE_MSG((size_t)len == buflen,
222 	    "recvmsg: %zd bytes received; expected %zd", len, buflen);
223 
224 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
225 	ATF_REQUIRE_MSG(cmsghdr != NULL,
226 	    "recvmsg: did not receive control message");
227 	foundcreds = false;
228 	*recv_fd = -1;
229 	for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
230 		if (cmsghdr->cmsg_level == SOL_SOCKET &&
231 		    cmsghdr->cmsg_type == SCM_RIGHTS &&
232 		    cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
233 			memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
234 			ATF_REQUIRE(*recv_fd != -1);
235 		} else if (cmsghdr->cmsg_level == SOL_SOCKET &&
236 		    cmsghdr->cmsg_type == SCM_CREDS)
237 			foundcreds = true;
238 	}
239 	ATF_REQUIRE_MSG(*recv_fd != -1,
240 	    "recvmsg: did not receive single-fd message");
241 	ATF_REQUIRE_MSG(!localcreds(sockfd) || foundcreds,
242 	    "recvmsg: expected credentials were not received");
243 }
244 
245 static void
246 recvfd(int sockfd, int *recv_fd)
247 {
248 	char ch = 0;
249 
250 	recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch),
251 	    CMSG_SPACE(sizeof(int)));
252 }
253 
254 /*
255  * Put a temporary file into a UNIX domain socket, then take it out and make
256  * sure it's the same file.  First time around, don't close the reference
257  * after sending.
258  */
259 ATF_TC_WITHOUT_HEAD(simple_send_fd);
260 ATF_TC_BODY(simple_send_fd, tc)
261 {
262 	struct stat getfd_stat, putfd_stat;
263 	int fd[2], getfd, putfd;
264 
265 	domainsocketpair(fd);
266 	tempfile(&putfd);
267 	dofstat(putfd, &putfd_stat);
268 	sendfd(fd[0], putfd);
269 	recvfd(fd[1], &getfd);
270 	dofstat(getfd, &getfd_stat);
271 	samefile(&putfd_stat, &getfd_stat);
272 	close(putfd);
273 	close(getfd);
274 	closesocketpair(fd);
275 }
276 
277 /*
278  * Same as simple_send_fd, only close the file reference after sending, so that
279  * the only reference is the descriptor in the UNIX domain socket buffer.
280  */
281 ATF_TC_WITHOUT_HEAD(send_and_close);
282 ATF_TC_BODY(send_and_close, tc)
283 {
284 	struct stat getfd_stat, putfd_stat;
285 	int fd[2], getfd, putfd;
286 
287 	domainsocketpair(fd);
288 	tempfile(&putfd);
289 	dofstat(putfd, &putfd_stat);
290 	sendfd(fd[0], putfd);
291 	close(putfd);
292 	recvfd(fd[1], &getfd);
293 	dofstat(getfd, &getfd_stat);
294 	samefile(&putfd_stat, &getfd_stat);
295 	close(getfd);
296 	closesocketpair(fd);
297 }
298 
299 /*
300  * Put a temporary file into a UNIX domain socket, then close both endpoints
301  * causing garbage collection to kick off.
302  */
303 ATF_TC_WITHOUT_HEAD(send_and_cancel);
304 ATF_TC_BODY(send_and_cancel, tc)
305 {
306 	int fd[2], putfd;
307 
308 	domainsocketpair(fd);
309 	tempfile(&putfd);
310 	sendfd(fd[0], putfd);
311 	close(putfd);
312 	closesocketpair(fd);
313 }
314 
315 /*
316  * Send two files.  Then receive them.  Make sure they are returned in the
317  * right order, and both get there.
318  */
319 ATF_TC_WITHOUT_HEAD(two_files);
320 ATF_TC_BODY(two_files, tc)
321 {
322 	struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
323 	int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
324 
325 	domainsocketpair(fd);
326 	tempfile(&putfd_1);
327 	tempfile(&putfd_2);
328 	dofstat(putfd_1, &putfd_1_stat);
329 	dofstat(putfd_2, &putfd_2_stat);
330 	sendfd(fd[0], putfd_1);
331 	sendfd(fd[0], putfd_2);
332 	close(putfd_1);
333 	close(putfd_2);
334 	recvfd(fd[1], &getfd_1);
335 	recvfd(fd[1], &getfd_2);
336 	dofstat(getfd_1, &getfd_1_stat);
337 	dofstat(getfd_2, &getfd_2_stat);
338 	samefile(&putfd_1_stat, &getfd_1_stat);
339 	samefile(&putfd_2_stat, &getfd_2_stat);
340 	close(getfd_1);
341 	close(getfd_2);
342 	closesocketpair(fd);
343 }
344 
345 /*
346  * Big bundling test.  Send an endpoint of the UNIX domain socket over itself,
347  * closing the door behind it.
348  */
349 ATF_TC_WITHOUT_HEAD(bundle);
350 ATF_TC_BODY(bundle, tc)
351 {
352 	int fd[2], getfd;
353 
354 	domainsocketpair(fd);
355 
356 	sendfd(fd[0], fd[0]);
357 	close(fd[0]);
358 	recvfd(fd[1], &getfd);
359 	close(getfd);
360 	close(fd[1]);
361 }
362 
363 /*
364  * Big bundling test part two: Send an endpoint of the UNIX domain socket over
365  * itself, close the door behind it, and never remove it from the other end.
366  */
367 ATF_TC_WITHOUT_HEAD(bundle_cancel);
368 ATF_TC_BODY(bundle_cancel, tc)
369 {
370 	int fd[2];
371 
372 	domainsocketpair(fd);
373 	sendfd(fd[0], fd[0]);
374 	sendfd(fd[1], fd[0]);
375 	closesocketpair(fd);
376 }
377 
378 /*
379  * Test for PR 151758: Send an character device over the UNIX domain socket
380  * and then close both sockets to orphan the device.
381  */
382 ATF_TC_WITHOUT_HEAD(devfs_orphan);
383 ATF_TC_BODY(devfs_orphan, tc)
384 {
385 	int fd[2], putfd;
386 
387 	domainsocketpair(fd);
388 	devnull(&putfd);
389 	sendfd(fd[0], putfd);
390 	close(putfd);
391 	closesocketpair(fd);
392 }
393 
394 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
395 
396 /*
397  * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
398  * control message to the data. Sender sends large payload using a non-blocking
399  * socket. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and
400  * receiver receives truncated data.
401  */
402 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
403 ATF_TC_BODY(rights_creds_payload, tc)
404 {
405 	const int on = 1;
406 	u_long sendspace;
407 	size_t len;
408 	void *buf;
409 	int fd[2], getfd, putfd, rc;
410 
411 	len = sizeof(sendspace);
412 	rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
413 	    &len, NULL, 0);
414 	ATF_REQUIRE_MSG(rc != -1,
415 	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
416 
417 	buf = calloc(1, sendspace);
418 	ATF_REQUIRE(buf != NULL);
419 
420 	domainsocketpair(fd);
421 	tempfile(&putfd);
422 
423 	rc = fcntl(fd[0], F_SETFL, O_NONBLOCK);
424 	ATF_REQUIRE_MSG(rc != -1, "fcntl(O_NONBLOCK) failed: %s",
425 	    strerror(errno));
426 	rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
427 	ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
428 	    strerror(errno));
429 
430 	len = sendfd_payload(fd[0], putfd, buf, sendspace);
431 	ATF_REQUIRE_MSG(len < sendspace, "sendmsg: %zu bytes sent", len);
432 	recvfd_payload(fd[1], &getfd, buf, len,
433 	    CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)));
434 
435 	close(putfd);
436 	close(getfd);
437 	closesocketpair(fd);
438 }
439 
440 static void
441 send_cmsg(int sockfd, void *cmsg, size_t cmsgsz)
442 {
443 	struct iovec iov;
444 	struct msghdr msghdr;
445 	ssize_t len;
446 	char ch;
447 
448 	ch = 0;
449 	bzero(&msghdr, sizeof(msghdr));
450 
451 	iov.iov_base = &ch;
452 	iov.iov_len = sizeof(ch);
453 	msghdr.msg_control = cmsg;
454 	msghdr.msg_controllen = cmsgsz;
455 	msghdr.msg_iov = &iov;
456 	msghdr.msg_iovlen = 1;
457 
458 	len = sendmsg(sockfd, &msghdr, 0);
459 	ATF_REQUIRE_MSG(len != -1,
460 	    "sendmsg failed: %s", strerror(errno));
461 	ATF_REQUIRE_MSG(len == sizeof(ch),
462 	    "sendmsg: %zd bytes sent; expected %zu", len, sizeof(ch));
463 }
464 
465 static void
466 recv_cmsg(int sockfd, char *cmsg, size_t cmsgsz, int flags)
467 {
468 	struct iovec iov;
469 	struct msghdr msghdr;
470 	ssize_t len;
471 	char ch;
472 
473 	ch = 0;
474 	bzero(&msghdr, sizeof(msghdr));
475 
476 	iov.iov_base = &ch;
477 	iov.iov_len = sizeof(ch);
478 	msghdr.msg_control = cmsg;
479 	msghdr.msg_controllen = cmsgsz;
480 	msghdr.msg_iov = &iov;
481 	msghdr.msg_iovlen = 1;
482 
483 	len = recvmsg(sockfd, &msghdr, 0);
484 	ATF_REQUIRE_MSG(len != -1,
485 	    "recvmsg failed: %s", strerror(errno));
486 	ATF_REQUIRE_MSG(len == sizeof(ch),
487 	    "recvmsg: %zd bytes received; expected %zu", len, sizeof(ch));
488 	ATF_REQUIRE_MSG((msghdr.msg_flags & flags) == flags,
489 	    "recvmsg: got flags %#x; expected %#x", msghdr.msg_flags, flags);
490 }
491 
492 /*
493  * Test for PR 131876.  Receiver uses a control message buffer that is too
494  * small for the incoming SCM_RIGHTS message, so the message is truncated.
495  * The kernel must not leak the copied right into the receiver's namespace.
496  */
497 ATF_TC_WITHOUT_HEAD(truncated_rights);
498 ATF_TC_BODY(truncated_rights, tc)
499 {
500 	char *message;
501 	int fd[2], nfds, putfd, rc;
502 
503 	domainsocketpair(fd);
504 	devnull(&putfd);
505 	nfds = getnfds();
506 
507 	/*
508 	 * Case 1: Send a single descriptor and truncate the message.
509 	 */
510 	message = malloc(CMSG_SPACE(sizeof(int)));
511 	ATF_REQUIRE(message != NULL);
512 	putfds(message, putfd, 1);
513 	send_cmsg(fd[0], message, CMSG_LEN(sizeof(int)));
514 	recv_cmsg(fd[1], message, CMSG_LEN(0), MSG_CTRUNC);
515 	ATF_REQUIRE(getnfds() == nfds);
516 	free(message);
517 
518 	/*
519 	 * Case 2a: Send two descriptors in separate messages, and truncate at
520 	 *          the boundary between the two messages.  We should still
521 	 *          receive the first message.
522 	 */
523 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
524 	ATF_REQUIRE(message != NULL);
525 	putfds(message, putfd, 1);
526 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
527 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
528 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
529 	rc = close(*(int *)CMSG_DATA(message));
530 	ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
531 	ATF_REQUIRE(getnfds() == nfds);
532 	free(message);
533 
534 	/*
535 	 * Case 2b: Send two descriptors in separate messages, and truncate
536 	 *          before the end of the first message.
537 	 */
538 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
539 	ATF_REQUIRE(message != NULL);
540 	putfds(message, putfd, 1);
541 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
542 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
543 	recv_cmsg(fd[1], message, CMSG_SPACE(0), MSG_CTRUNC);
544 	ATF_REQUIRE(getnfds() == nfds);
545 	free(message);
546 
547 	/*
548 	 * Case 2c: Send two descriptors in separate messages, and truncate
549 	 *          after the end of the first message.  We should still
550 	 *          receive the first message.
551 	 */
552 	message = malloc(CMSG_SPACE(sizeof(int)) * 2);
553 	ATF_REQUIRE(message != NULL);
554 	putfds(message, putfd, 1);
555 	putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
556 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
557 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)) + CMSG_SPACE(0),
558 	    MSG_CTRUNC);
559 	rc = close(*(int *)CMSG_DATA(message));
560 	ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
561 	ATF_REQUIRE(getnfds() == nfds);
562 	free(message);
563 
564 	/*
565 	 * Case 3: Send three descriptors in the same message, and leave space
566 	 *         only for the first when receiving the message.
567 	 */
568 	message = malloc(CMSG_SPACE(sizeof(int) * 3));
569 	ATF_REQUIRE(message != NULL);
570 	putfds(message, putfd, 3);
571 	send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int) * 3));
572 	recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
573 	ATF_REQUIRE(getnfds() == nfds);
574 	free(message);
575 
576 	close(putfd);
577 	closesocketpair(fd);
578 }
579 
580 /*
581  * Ensure that an attempt to copy a SCM_RIGHTS message to the recipient
582  * fails.  In this case the kernel must dispose of the externalized rights
583  * rather than leaking them into the recipient's file descriptor table.
584  */
585 ATF_TC_WITHOUT_HEAD(copyout_rights_error);
586 ATF_TC_BODY(copyout_rights_error, tc)
587 {
588 	struct iovec iovec;
589 	struct msghdr msghdr;
590 	char buf[16];
591 	ssize_t len;
592 	int fd[2], error, nfds, putfd;
593 
594 	memset(buf, 0, sizeof(buf));
595 	domainsocketpair(fd);
596 	devnull(&putfd);
597 	nfds = getnfds();
598 
599 	sendfd_payload(fd[0], putfd, buf, sizeof(buf));
600 
601 	bzero(&msghdr, sizeof(msghdr));
602 
603 	iovec.iov_base = buf;
604 	iovec.iov_len = sizeof(buf);
605 	msghdr.msg_control = (char *)-1; /* trigger EFAULT */
606 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
607 	msghdr.msg_iov = &iovec;
608 	msghdr.msg_iovlen = 1;
609 
610 	len = recvmsg(fd[1], &msghdr, 0);
611 	error = errno;
612 	ATF_REQUIRE_MSG(len == -1, "recvmsg succeeded: %zd", len);
613 	ATF_REQUIRE_MSG(errno == EFAULT, "expected EFAULT, got %d (%s)",
614 	    error, strerror(errno));
615 
616 	/* Verify that no FDs were leaked. */
617 	ATF_REQUIRE(getnfds() == nfds);
618 
619 	close(putfd);
620 	closesocketpair(fd);
621 }
622 
623 /*
624  * Verify that we can handle empty rights messages.  Try sending two SCM_RIGHTS
625  * messages with a single call, one empty and one containing a single FD.
626  */
627 ATF_TC_WITHOUT_HEAD(empty_rights_message);
628 ATF_TC_BODY(empty_rights_message, tc)
629 {
630 	struct iovec iov;
631 	struct msghdr msghdr;
632 	char *cm, message[CMSG_SPACE(0) + CMSG_SPACE(sizeof(int))];
633 	ssize_t len;
634 	int error, fd[2], putfd;
635 
636 	domainsocketpair(fd);
637 	devnull(&putfd);
638 
639 	/*
640 	 * First, try sending an empty message followed by a non-empty message.
641 	 */
642 	cm = message;
643 	putfds(cm, -1, 0);
644 	cm += CMSG_SPACE(0);
645 	putfds(cm, putfd, 1);
646 
647 	memset(&msghdr, 0, sizeof(msghdr));
648 	iov.iov_base = NULL;
649 	iov.iov_len = 0;
650 	msghdr.msg_control = message;
651 	msghdr.msg_controllen = sizeof(message);
652 	msghdr.msg_iov = &iov;
653 	msghdr.msg_iovlen = 1;
654 
655 	len = sendmsg(fd[0], &msghdr, 0);
656 	ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
657 
658 	/* Only the non-empty message should be received. */
659 	len = recvmsg(fd[1], &msghdr, 0);
660 	ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
661 	ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int)));
662 	error = close(*(int *)CMSG_DATA(msghdr.msg_control));
663 	ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
664 
665 	/*
666 	 * Now try sending with the non-empty message before the empty message.
667 	 */
668 	cm = message;
669 	putfds(cm, putfd, 1);
670 	cm += CMSG_SPACE(sizeof(int));
671 	putfds(cm, -1, 0);
672 
673 	memset(&msghdr, 0, sizeof(msghdr));
674 	iov.iov_base = NULL;
675 	iov.iov_len = 0;
676 	msghdr.msg_control = message;
677 	msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
678 	msghdr.msg_iov = &iov;
679 	msghdr.msg_iovlen = 1;
680 
681 	len = sendmsg(fd[0], &msghdr, 0);
682 	ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
683 
684 	/* Only the non-empty message should be received. */
685 	len = recvmsg(fd[1], &msghdr, 0);
686 	ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
687 	ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int)));
688 	error = close(*(int *)CMSG_DATA(msghdr.msg_control));
689 	ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
690 
691 	(void)close(putfd);
692 }
693 
694 ATF_TP_ADD_TCS(tp)
695 {
696 
697 	ATF_TP_ADD_TC(tp, simple_send_fd);
698 	ATF_TP_ADD_TC(tp, send_and_close);
699 	ATF_TP_ADD_TC(tp, send_and_cancel);
700 	ATF_TP_ADD_TC(tp, two_files);
701 	ATF_TP_ADD_TC(tp, bundle);
702 	ATF_TP_ADD_TC(tp, bundle_cancel);
703 	ATF_TP_ADD_TC(tp, devfs_orphan);
704 	ATF_TP_ADD_TC(tp, rights_creds_payload);
705 	ATF_TP_ADD_TC(tp, truncated_rights);
706 	ATF_TP_ADD_TC(tp, copyout_rights_error);
707 	ATF_TP_ADD_TC(tp, empty_rights_message);
708 
709 	return (atf_no_error());
710 }
711