2 * Copyright (c) 2005 Robert N. M. Watson
3 * Copyright (c) 2015 Mark Johnston
4 * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/socket.h>
32 #include <sys/sysctl.h>
34 #include <sys/resource.h>
47 #if !defined(TEST_PROTO)
48 #error Need TEST_PROTO defined to SOCK_STREAM or SOCK_DGRAM
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.
61 domainsocketpair(int *fdp)
64 ATF_REQUIRE_MSG(socketpair(PF_UNIX, TEST_PROTO, 0, fdp) != -1,
65 "socketpair(PF_UNIX, %u) failed: %s", TEST_PROTO, strerror(errno));
69 closesocketpair(int *fdp)
81 fd = open("/dev/null", O_RDONLY);
82 ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
92 snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
93 getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
95 ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
101 dofstat(int fd, struct stat *sb)
104 ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
105 "fstat failed: %s", strerror(errno));
117 mib[2] = KERN_PROC_NFDS;
120 rc = sysctl(mib, 4, &n, &len, NULL, 0);
121 ATF_REQUIRE_MSG(rc != -1, "sysctl(KERN_PROC_NFDS) failed");
129 size_t len = sizeof(files);
131 ATF_REQUIRE(sysctlbyname("kern.openfiles", &files, &len, NULL, 0) == 0);
137 putfds(char *buf, int fd, int nfds)
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++)
151 samefile(struct stat *sb1, struct stat *sb2)
154 ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
155 ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
159 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
162 char message[CMSG_SPACE(sizeof(int))];
163 struct msghdr msghdr;
165 bzero(&msghdr, sizeof(msghdr));
166 bzero(&message, sizeof(message));
168 msghdr.msg_control = message;
169 msghdr.msg_controllen = sizeof(message);
171 iovec.iov_base = payload;
172 iovec.iov_len = paylen;
174 msghdr.msg_iov = &iovec;
175 msghdr.msg_iovlen = 1;
177 putfds(message, send_fd, 1);
178 return (sendmsg(sockfd, &msghdr, 0));
182 sendfd(int sockfd, int send_fd)
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),
195 localcreds(int sockfd)
201 rc = getsockopt(sockfd, 0, LOCAL_CREDS, &val, &sz);
202 ATF_REQUIRE_MSG(rc != -1, "getsockopt(LOCAL_CREDS) failed: %s",
208 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen,
209 size_t cmsgsz, int recvmsg_flags)
211 struct cmsghdr *cmsghdr;
212 struct msghdr msghdr;
218 bzero(&msghdr, sizeof(msghdr));
219 message = malloc(cmsgsz);
220 ATF_REQUIRE(message != NULL);
222 msghdr.msg_control = message;
223 msghdr.msg_controllen = cmsgsz;
225 iovec.iov_base = buf;
226 iovec.iov_len = buflen;
228 msghdr.msg_iov = &iovec;
229 msghdr.msg_iovlen = 1;
231 len = recvmsg(sockfd, &msghdr, recvmsg_flags);
232 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
233 ATF_REQUIRE_MSG((size_t)len == buflen,
234 "recvmsg: %zd bytes received; expected %zd", len, buflen);
236 cmsghdr = CMSG_FIRSTHDR(&msghdr);
237 ATF_REQUIRE_MSG(cmsghdr != NULL,
238 "recvmsg: did not receive control message");
241 for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
242 if (cmsghdr->cmsg_level == SOL_SOCKET &&
243 cmsghdr->cmsg_type == SCM_RIGHTS &&
244 cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
245 memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
246 ATF_REQUIRE(*recv_fd != -1);
247 } else if (cmsghdr->cmsg_level == SOL_SOCKET &&
248 cmsghdr->cmsg_type == SCM_CREDS)
251 ATF_REQUIRE_MSG(*recv_fd != -1,
252 "recvmsg: did not receive single-fd message");
253 ATF_REQUIRE_MSG(!localcreds(sockfd) || foundcreds,
254 "recvmsg: expected credentials were not received");
255 ATF_REQUIRE_MSG((msghdr.msg_flags & MSG_TRUNC) == 0,
256 "recvmsg: MSG_TRUNC is set while buffer is sufficient");
260 recvfd(int sockfd, int *recv_fd, int flags)
264 recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch),
265 CMSG_SPACE(sizeof(int)), flags);
268 #if TEST_PROTO == SOCK_STREAM
269 #define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace"
270 #define LOCAL_RECVSPACE_SYSCTL "net.local.stream.recvspace"
271 #elif TEST_PROTO == SOCK_DGRAM
272 #define LOCAL_SENDSPACE_SYSCTL "net.local.dgram.maxdgram"
273 #define LOCAL_RECVSPACE_SYSCTL "net.local.dgram.recvspace"
281 ATF_REQUIRE_MSG(sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
282 &(size_t){sizeof(u_long)}, NULL, 0) != -1,
283 "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
293 ATF_REQUIRE_MSG(sysctlbyname(LOCAL_RECVSPACE_SYSCTL, &recvspace,
294 &(size_t){sizeof(u_long)}, NULL, 0) != -1,
295 "sysctl %s failed: %s", LOCAL_RECVSPACE_SYSCTL, strerror(errno));
301 * Fill socket to a state when next max sized send would fail with EAGAIN.
309 sendspace = getsendspace();
310 ATF_REQUIRE((buf = malloc(sendspace)) != NULL);
312 ATF_REQUIRE_MSG(fcntl(fd, F_SETFL, O_NONBLOCK) != -1,
313 "fcntl(O_NONBLOCK) failed: %s", strerror(errno));
315 #if TEST_PROTO == SOCK_STREAM
316 do {} while (send(fd, buf, sendspace, 0) == (ssize_t)sendspace);
317 #elif TEST_PROTO == SOCK_DGRAM
318 u_long recvspace = getrecvspace();
320 for (ssize_t sent = 0;
321 sent + sendspace + sizeof(struct sockaddr) < recvspace;
322 sent += sendspace + sizeof(struct sockaddr))
323 ATF_REQUIRE(send(fd, buf, sendspace, 0) == (ssize_t)sendspace);
329 * Put a temporary file into a UNIX domain socket, then take it out and make
330 * sure it's the same file. First time around, don't close the reference
333 ATF_TC_WITHOUT_HEAD(simple_send_fd);
334 ATF_TC_BODY(simple_send_fd, tc)
336 struct stat getfd_stat, putfd_stat;
337 int fd[2], getfd, putfd;
339 domainsocketpair(fd);
341 dofstat(putfd, &putfd_stat);
342 sendfd(fd[0], putfd);
343 recvfd(fd[1], &getfd, 0);
344 dofstat(getfd, &getfd_stat);
345 samefile(&putfd_stat, &getfd_stat);
352 * Like simple_send_fd but also sets MSG_CMSG_CLOEXEC and checks that the
353 * received file descriptor has the FD_CLOEXEC flag set.
355 ATF_TC_WITHOUT_HEAD(simple_send_fd_msg_cmsg_cloexec);
356 ATF_TC_BODY(simple_send_fd_msg_cmsg_cloexec, tc)
358 struct stat getfd_stat, putfd_stat;
359 int fd[2], getfd, putfd;
361 domainsocketpair(fd);
363 dofstat(putfd, &putfd_stat);
364 sendfd(fd[0], putfd);
365 recvfd(fd[1], &getfd, MSG_CMSG_CLOEXEC);
366 dofstat(getfd, &getfd_stat);
367 samefile(&putfd_stat, &getfd_stat);
368 ATF_REQUIRE_EQ_MSG(fcntl(getfd, F_GETFD) & FD_CLOEXEC, FD_CLOEXEC,
369 "FD_CLOEXEC not set on the received file descriptor");
376 * Same as simple_send_fd, only close the file reference after sending, so that
377 * the only reference is the descriptor in the UNIX domain socket buffer.
379 ATF_TC_WITHOUT_HEAD(send_and_close);
380 ATF_TC_BODY(send_and_close, tc)
382 struct stat getfd_stat, putfd_stat;
383 int fd[2], getfd, putfd;
385 domainsocketpair(fd);
387 dofstat(putfd, &putfd_stat);
388 sendfd(fd[0], putfd);
390 recvfd(fd[1], &getfd, 0);
391 dofstat(getfd, &getfd_stat);
392 samefile(&putfd_stat, &getfd_stat);
398 * Put a temporary file into a UNIX domain socket, then close both endpoints
399 * causing garbage collection to kick off.
401 ATF_TC_WITHOUT_HEAD(send_and_cancel);
402 ATF_TC_BODY(send_and_cancel, tc)
406 domainsocketpair(fd);
408 sendfd(fd[0], putfd);
414 * Send file then shutdown receive side to exercise unp_dispose() call
415 * via soshutdown(). Check that shutdown(SHUT_RD) would gc the file
416 * reference sitting in the receive buffer. There is no good way of
417 * checking that except using global open file count.
419 ATF_TC_WITHOUT_HEAD(send_and_shutdown);
420 ATF_TC_BODY(send_and_shutdown, tc)
422 int fd[2], putfd, nfiles;
424 domainsocketpair(fd);
426 sendfd(fd[0], putfd);
427 nfiles = openfiles();
429 ATF_REQUIRE(openfiles() == nfiles);
430 shutdown(fd[1], SHUT_RD);
431 ATF_REQUIRE(openfiles() == nfiles - 1);
436 * Send maximum possible SCM_RIGHTS message.
437 * Internally the file descriptors are converted from integers to pointers
438 * and stored in a single mbuf cluster. Check that we can not send too much
439 * and that we can successfully send maximum possible amount. Check that we
440 * can not exploit getrlimit(3).
442 #define MAXFDS ((MCLBYTES - _ALIGN(sizeof(struct cmsghdr)))/sizeof(void *))
443 ATF_TC_WITHOUT_HEAD(send_a_lot);
444 ATF_TC_BODY(send_a_lot, tc)
446 struct msghdr msghdr;
452 domainsocketpair(fd);
453 cmsg = malloc(CMSG_SPACE((MAXFDS + 1) * sizeof(int)));
454 ATF_REQUIRE(cmsg != NULL);
456 iov.iov_len = sizeof(ch);
457 msghdr = (struct msghdr ){
459 .msg_controllen = CMSG_LEN((MAXFDS + 1) * sizeof(int)),
464 /* Sending too much fails. */
465 putfds(cmsg, fd[0], MAXFDS + 1);
466 ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == -1);
467 ATF_REQUIRE(errno == EMSGSIZE);
469 /* Sending just the right amount works and everything is received. */
470 putfds(cmsg, fd[0], MAXFDS);
471 msghdr.msg_controllen = CMSG_LEN(MAXFDS * sizeof(int));
472 ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1);
474 ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1);
475 ATF_REQUIRE(getnfds() == (int)(nfds + MAXFDS));
477 /* Limit our process open files... */
478 ATF_REQUIRE(getrlimit(RLIMIT_NOFILE, &rlim) == 0);
479 nfds = rlim.rlim_cur = getnfds();
480 ATF_REQUIRE(setrlimit(RLIMIT_NOFILE, &rlim) == 0);
482 /* ... and try to receive a single descriptor. */
483 putfds(cmsg, fd[0], 1);
484 msghdr.msg_controllen = CMSG_LEN(sizeof(int));
485 ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 1);
486 ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == -1);
487 /* Such attempt shall fail with EMFILE. */
488 ATF_REQUIRE(errno == EMFILE);
489 ATF_REQUIRE(getnfds() == nfds);
490 #if TEST_PROTO == SOCK_STREAM
492 * For the SOCK_STREAM the above attempt shall free the control in
493 * the kernel, so that socket isn't left in a stuck state. Next read
494 * shall bring us the normal data only. The stream data shall not
497 ATF_REQUIRE(recvmsg(fd[1], &msghdr, 0) == 1);
498 ATF_REQUIRE(msghdr.msg_controllen == 0);
499 #elif TEST_PROTO == SOCK_DGRAM
501 * For SOCK_DGRAM there are two options for the previously failed
502 * syscall: strip the control leaving datagram in the socket or
503 * drop the whole datagram. Our implementation drops the whole
506 ATF_REQUIRE(recvmsg(fd[1], &msghdr, MSG_DONTWAIT) == -1);
507 ATF_REQUIRE(errno == EAGAIN);
512 * Exersize condition when SCM_RIGHTS is successfully internalized, but
513 * message delivery fails due to receive buffer overflow. Check that no
514 * file descriptors are leaked.
516 ATF_TC_WITHOUT_HEAD(send_overflow);
517 ATF_TC_BODY(send_overflow, tc)
521 int fd[2], putfd, nfiles;
524 sendspace = (int)getsendspace();
525 ATF_REQUIRE((buf = malloc(sendspace)) != NULL);
527 domainsocketpair(fd);
529 nfiles = openfiles();
531 len = sendfd_payload(fd[0], putfd, buf, sendspace);
532 #if TEST_PROTO == SOCK_STREAM
533 ATF_REQUIRE_MSG(len == -1 && errno == EAGAIN,
534 "sendmsg: %zd bytes sent, errno %d", len, errno);
535 #elif TEST_PROTO == SOCK_DGRAM
536 ATF_REQUIRE_MSG(len == -1 && errno == ENOBUFS,
537 "sendmsg: %zd bytes sent, errno %d", len, errno);
540 ATF_REQUIRE(nfiles == openfiles());
546 * Send two files. Then receive them. Make sure they are returned in the
547 * right order, and both get there.
549 ATF_TC_WITHOUT_HEAD(two_files);
550 ATF_TC_BODY(two_files, tc)
552 struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
553 int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
555 domainsocketpair(fd);
558 dofstat(putfd_1, &putfd_1_stat);
559 dofstat(putfd_2, &putfd_2_stat);
560 sendfd(fd[0], putfd_1);
561 sendfd(fd[0], putfd_2);
564 recvfd(fd[1], &getfd_1, 0);
565 recvfd(fd[1], &getfd_2, 0);
566 dofstat(getfd_1, &getfd_1_stat);
567 dofstat(getfd_2, &getfd_2_stat);
568 samefile(&putfd_1_stat, &getfd_1_stat);
569 samefile(&putfd_2_stat, &getfd_2_stat);
576 * Big bundling test. Send an endpoint of the UNIX domain socket over itself,
577 * closing the door behind it.
579 ATF_TC_WITHOUT_HEAD(bundle);
580 ATF_TC_BODY(bundle, tc)
584 domainsocketpair(fd);
586 sendfd(fd[0], fd[0]);
588 recvfd(fd[1], &getfd, 0);
594 * Big bundling test part two: Send an endpoint of the UNIX domain socket over
595 * itself, close the door behind it, and never remove it from the other end.
597 ATF_TC_WITHOUT_HEAD(bundle_cancel);
598 ATF_TC_BODY(bundle_cancel, tc)
602 domainsocketpair(fd);
603 sendfd(fd[0], fd[0]);
604 sendfd(fd[1], fd[0]);
609 * Test for PR 151758: Send an character device over the UNIX domain socket
610 * and then close both sockets to orphan the device.
612 ATF_TC_WITHOUT_HEAD(devfs_orphan);
613 ATF_TC_BODY(devfs_orphan, tc)
617 domainsocketpair(fd);
619 sendfd(fd[0], putfd);
625 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
626 * control message to the data. Sender sends large payload using a non-blocking
627 * socket. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and
628 * receiver receives truncated data.
630 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
631 ATF_TC_BODY(rights_creds_payload, tc)
637 int fd[2], getfd, putfd, rc;
639 sendspace = getsendspace();
640 buf = calloc(1, sendspace);
641 ATF_REQUIRE(buf != NULL);
643 domainsocketpair(fd);
646 rc = fcntl(fd[0], F_SETFL, O_NONBLOCK);
647 ATF_REQUIRE_MSG(rc != -1, "fcntl(O_NONBLOCK) failed: %s",
649 rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
650 ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
653 len = sendfd_payload(fd[0], putfd, buf, sendspace);
654 #if TEST_PROTO == SOCK_STREAM
655 ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno));
656 ATF_REQUIRE_MSG((size_t)len < sendspace,
657 "sendmsg: %zd bytes sent", len);
658 recvfd_payload(fd[1], &getfd, buf, len,
659 CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0);
661 #if TEST_PROTO == SOCK_DGRAM
662 ATF_REQUIRE_MSG(len != -1 , "sendmsg failed: %s", strerror(errno));
663 ATF_REQUIRE_MSG((size_t)len == sendspace,
664 "sendmsg: %zd bytes sent", len);
665 recvfd_payload(fd[1], &getfd, buf, len,
666 CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0);
675 send_cmsg(int sockfd, void *cmsg, size_t cmsgsz)
678 struct msghdr msghdr;
683 bzero(&msghdr, sizeof(msghdr));
686 iov.iov_len = sizeof(ch);
687 msghdr.msg_control = cmsg;
688 msghdr.msg_controllen = cmsgsz;
689 msghdr.msg_iov = &iov;
690 msghdr.msg_iovlen = 1;
692 len = sendmsg(sockfd, &msghdr, 0);
693 ATF_REQUIRE_MSG(len != -1,
694 "sendmsg failed: %s", strerror(errno));
695 ATF_REQUIRE_MSG(len == sizeof(ch),
696 "sendmsg: %zd bytes sent; expected %zu", len, sizeof(ch));
700 recv_cmsg(int sockfd, char *cmsg, size_t cmsgsz, int flags)
703 struct msghdr msghdr;
708 bzero(&msghdr, sizeof(msghdr));
711 iov.iov_len = sizeof(ch);
712 msghdr.msg_control = cmsg;
713 msghdr.msg_controllen = cmsgsz;
714 msghdr.msg_iov = &iov;
715 msghdr.msg_iovlen = 1;
717 len = recvmsg(sockfd, &msghdr, 0);
718 ATF_REQUIRE_MSG(len != -1,
719 "recvmsg failed: %s", strerror(errno));
720 ATF_REQUIRE_MSG(len == sizeof(ch),
721 "recvmsg: %zd bytes received; expected %zu", len, sizeof(ch));
722 ATF_REQUIRE_MSG((msghdr.msg_flags & flags) == flags,
723 "recvmsg: got flags %#x; expected %#x", msghdr.msg_flags, flags);
727 * Test for PR 131876. Receiver uses a control message buffer that is too
728 * small for the incoming SCM_RIGHTS message, so the message is truncated.
729 * The kernel must not leak the copied right into the receiver's namespace.
731 ATF_TC_WITHOUT_HEAD(truncated_rights);
732 ATF_TC_BODY(truncated_rights, tc)
735 int fd[2], nfds, putfd, rc;
737 domainsocketpair(fd);
742 * Case 1: Send a single descriptor and truncate the message.
744 message = malloc(CMSG_SPACE(sizeof(int)));
745 ATF_REQUIRE(message != NULL);
746 putfds(message, putfd, 1);
747 send_cmsg(fd[0], message, CMSG_LEN(sizeof(int)));
748 recv_cmsg(fd[1], message, CMSG_LEN(0), MSG_CTRUNC);
749 ATF_REQUIRE(getnfds() == nfds);
753 * Case 2a: Send two descriptors in separate messages, and truncate at
754 * the boundary between the two messages. We should still
755 * receive the first message.
757 message = malloc(CMSG_SPACE(sizeof(int)) * 2);
758 ATF_REQUIRE(message != NULL);
759 putfds(message, putfd, 1);
760 putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
761 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
762 recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
763 rc = close(*(int *)CMSG_DATA(message));
764 ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
765 ATF_REQUIRE(getnfds() == nfds);
769 * Case 2b: Send two descriptors in separate messages, and truncate
770 * before the end of the first message.
772 message = malloc(CMSG_SPACE(sizeof(int)) * 2);
773 ATF_REQUIRE(message != NULL);
774 putfds(message, putfd, 1);
775 putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
776 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
777 recv_cmsg(fd[1], message, CMSG_SPACE(0), MSG_CTRUNC);
778 ATF_REQUIRE(getnfds() == nfds);
782 * Case 2c: Send two descriptors in separate messages, and truncate
783 * after the end of the first message. We should still
784 * receive the first message.
786 message = malloc(CMSG_SPACE(sizeof(int)) * 2);
787 ATF_REQUIRE(message != NULL);
788 putfds(message, putfd, 1);
789 putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1);
790 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2);
791 recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)) + CMSG_SPACE(0),
793 rc = close(*(int *)CMSG_DATA(message));
794 ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
795 ATF_REQUIRE(getnfds() == nfds);
799 * Case 3: Send three descriptors in the same message, and leave space
800 * only for the first when receiving the message.
802 message = malloc(CMSG_SPACE(sizeof(int) * 3));
803 ATF_REQUIRE(message != NULL);
804 putfds(message, putfd, 3);
805 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int) * 3));
806 recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC);
807 ATF_REQUIRE(getnfds() == nfds);
815 * Ensure that an attempt to copy a SCM_RIGHTS message to the recipient
816 * fails. In this case the kernel must dispose of the externalized rights
817 * rather than leaking them into the recipient's file descriptor table.
819 ATF_TC_WITHOUT_HEAD(copyout_rights_error);
820 ATF_TC_BODY(copyout_rights_error, tc)
823 struct msghdr msghdr;
826 int fd[2], error, nfds, putfd;
828 memset(buf, 0, sizeof(buf));
829 domainsocketpair(fd);
833 len = sendfd_payload(fd[0], putfd, buf, sizeof(buf));
834 ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
836 bzero(&msghdr, sizeof(msghdr));
838 iovec.iov_base = buf;
839 iovec.iov_len = sizeof(buf);
840 msghdr.msg_control = (char *)-1; /* trigger EFAULT */
841 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
842 msghdr.msg_iov = &iovec;
843 msghdr.msg_iovlen = 1;
845 len = recvmsg(fd[1], &msghdr, 0);
847 ATF_REQUIRE_MSG(len == -1, "recvmsg succeeded: %zd", len);
848 ATF_REQUIRE_MSG(errno == EFAULT, "expected EFAULT, got %d (%s)",
849 error, strerror(errno));
851 /* Verify that no FDs were leaked. */
852 ATF_REQUIRE(getnfds() == nfds);
859 * Verify that we can handle empty rights messages.
861 ATF_TC_WITHOUT_HEAD(empty_rights_message);
862 ATF_TC_BODY(empty_rights_message, tc)
865 struct msghdr msghdr;
867 char *cm, message[CMSG_SPACE(0) + CMSG_SPACE(sizeof(int))];
869 int error, fd[2], putfd;
871 domainsocketpair(fd);
874 memset(&msghdr, 0, sizeof(msghdr));
877 msghdr.msg_iov = &iov;
878 msghdr.msg_iovlen = 1;
881 * Try sending incorrect empty message. On 64-bit platforms, where
882 * CMSG_SPACE(0) > sizeof(struct cmsghdr), this will exercise
885 cmsg = (struct cmsghdr ){
886 .cmsg_len = sizeof(struct cmsghdr), /* not CMSG_LEN(0)! */
887 .cmsg_level = SOL_SOCKET,
888 .cmsg_type = SCM_RIGHTS,
890 msghdr.msg_control = &cmsg;
891 msghdr.msg_controllen = CMSG_SPACE(0);
893 len = sendmsg(fd[0], &msghdr, 0);
894 if (CMSG_LEN(0) != sizeof(struct cmsghdr))
895 ATF_REQUIRE(len == -1 && errno == EINVAL);
897 ATF_REQUIRE(len == 0);
900 * Try sending an empty message followed by a non-empty message.
905 putfds(cm, putfd, 1);
906 msghdr.msg_control = message;
907 msghdr.msg_controllen = sizeof(message);
909 len = sendmsg(fd[0], &msghdr, 0);
910 ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
912 /* Only the non-empty message should be received. */
913 len = recvmsg(fd[1], &msghdr, 0);
914 ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
915 ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int)));
916 error = close(*(int *)CMSG_DATA(msghdr.msg_control));
917 ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
920 * Now try sending with the non-empty message before the empty message.
923 putfds(cm, putfd, 1);
924 cm += CMSG_SPACE(sizeof(int));
927 memset(&msghdr, 0, sizeof(msghdr));
930 msghdr.msg_control = message;
931 msghdr.msg_controllen = CMSG_SPACE(sizeof(int));
932 msghdr.msg_iov = &iov;
933 msghdr.msg_iovlen = 1;
935 len = sendmsg(fd[0], &msghdr, 0);
936 ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno));
938 /* Only the non-empty message should be received. */
939 len = recvmsg(fd[1], &msghdr, 0);
940 ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno));
941 ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int)));
942 error = close(*(int *)CMSG_DATA(msghdr.msg_control));
943 ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno));
951 ATF_TP_ADD_TC(tp, simple_send_fd);
952 ATF_TP_ADD_TC(tp, simple_send_fd_msg_cmsg_cloexec);
953 ATF_TP_ADD_TC(tp, send_and_close);
954 ATF_TP_ADD_TC(tp, send_and_cancel);
955 ATF_TP_ADD_TC(tp, send_and_shutdown);
956 ATF_TP_ADD_TC(tp, send_a_lot);
957 ATF_TP_ADD_TC(tp, send_overflow);
958 ATF_TP_ADD_TC(tp, two_files);
959 ATF_TP_ADD_TC(tp, bundle);
960 ATF_TP_ADD_TC(tp, bundle_cancel);
961 ATF_TP_ADD_TC(tp, devfs_orphan);
962 ATF_TP_ADD_TC(tp, rights_creds_payload);
963 ATF_TP_ADD_TC(tp, truncated_rights);
964 ATF_TP_ADD_TC(tp, copyout_rights_error);
965 ATF_TP_ADD_TC(tp, empty_rights_message);
967 return (atf_no_error());