2 * Copyright (c) 2005 Robert N. M. Watson
3 * Copyright (c) 2015 Mark Johnston
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/socket.h>
34 #include <sys/sysctl.h>
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.
57 domainsocketpair(int *fdp)
60 ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
61 "socketpair(PF_UNIX, SOCK_STREAM) failed: %s", strerror(errno));
65 closesocketpair(int *fdp)
77 fd = open("/dev/null", O_RDONLY);
78 ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
88 snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
89 getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
91 ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
97 dofstat(int fd, struct stat *sb)
100 ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
101 "fstat failed: %s", strerror(errno));
113 mib[2] = KERN_PROC_NFDS;
116 rc = sysctl(mib, 4, &n, &len, NULL, 0);
117 ATF_REQUIRE_MSG(rc != -1, "sysctl(KERN_PROC_NFDS) failed");
122 putfds(char *buf, int fd, int nfds)
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++)
136 samefile(struct stat *sb1, struct stat *sb2)
139 ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
140 ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
144 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
147 char message[CMSG_SPACE(sizeof(int))];
148 struct msghdr msghdr;
151 bzero(&msghdr, sizeof(msghdr));
152 bzero(&message, sizeof(message));
154 msghdr.msg_control = message;
155 msghdr.msg_controllen = sizeof(message);
157 iovec.iov_base = payload;
158 iovec.iov_len = paylen;
160 msghdr.msg_iov = &iovec;
161 msghdr.msg_iovlen = 1;
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);
170 sendfd(int sockfd, int send_fd)
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),
183 localcreds(int sockfd)
189 rc = getsockopt(sockfd, 0, LOCAL_CREDS, &val, &sz);
190 ATF_REQUIRE_MSG(rc != -1, "getsockopt(LOCAL_CREDS) failed: %s",
196 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen,
199 struct cmsghdr *cmsghdr;
200 struct msghdr msghdr;
206 bzero(&msghdr, sizeof(msghdr));
207 message = malloc(cmsgsz);
208 ATF_REQUIRE(message != NULL);
210 msghdr.msg_control = message;
211 msghdr.msg_controllen = cmsgsz;
213 iovec.iov_base = buf;
214 iovec.iov_len = buflen;
216 msghdr.msg_iov = &iovec;
217 msghdr.msg_iovlen = 1;
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);
224 cmsghdr = CMSG_FIRSTHDR(&msghdr);
225 ATF_REQUIRE_MSG(cmsghdr != NULL,
226 "recvmsg: did not receive control message");
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)
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");
246 recvfd(int sockfd, int *recv_fd)
250 recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch),
251 CMSG_SPACE(sizeof(int)));
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
259 ATF_TC_WITHOUT_HEAD(simple_send_fd);
260 ATF_TC_BODY(simple_send_fd, tc)
262 struct stat getfd_stat, putfd_stat;
263 int fd[2], getfd, putfd;
265 domainsocketpair(fd);
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);
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.
281 ATF_TC_WITHOUT_HEAD(send_and_close);
282 ATF_TC_BODY(send_and_close, tc)
284 struct stat getfd_stat, putfd_stat;
285 int fd[2], getfd, putfd;
287 domainsocketpair(fd);
289 dofstat(putfd, &putfd_stat);
290 sendfd(fd[0], putfd);
292 recvfd(fd[1], &getfd);
293 dofstat(getfd, &getfd_stat);
294 samefile(&putfd_stat, &getfd_stat);
300 * Put a temporary file into a UNIX domain socket, then close both endpoints
301 * causing garbage collection to kick off.
303 ATF_TC_WITHOUT_HEAD(send_and_cancel);
304 ATF_TC_BODY(send_and_cancel, tc)
308 domainsocketpair(fd);
310 sendfd(fd[0], putfd);
316 * Send two files. Then receive them. Make sure they are returned in the
317 * right order, and both get there.
319 ATF_TC_WITHOUT_HEAD(two_files);
320 ATF_TC_BODY(two_files, tc)
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;
325 domainsocketpair(fd);
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);
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);
346 * Big bundling test. Send an endpoint of the UNIX domain socket over itself,
347 * closing the door behind it.
349 ATF_TC_WITHOUT_HEAD(bundle);
350 ATF_TC_BODY(bundle, tc)
354 domainsocketpair(fd);
356 sendfd(fd[0], fd[0]);
358 recvfd(fd[1], &getfd);
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.
367 ATF_TC_WITHOUT_HEAD(bundle_cancel);
368 ATF_TC_BODY(bundle_cancel, tc)
372 domainsocketpair(fd);
373 sendfd(fd[0], fd[0]);
374 sendfd(fd[1], fd[0]);
379 * Test for PR 151758: Send an character device over the UNIX domain socket
380 * and then close both sockets to orphan the device.
382 ATF_TC_WITHOUT_HEAD(devfs_orphan);
383 ATF_TC_BODY(devfs_orphan, tc)
387 domainsocketpair(fd);
389 sendfd(fd[0], putfd);
394 #define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace"
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.
402 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
403 ATF_TC_BODY(rights_creds_payload, tc)
409 int fd[2], getfd, putfd, rc;
411 len = sizeof(sendspace);
412 rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
414 ATF_REQUIRE_MSG(rc != -1,
415 "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
417 buf = calloc(1, sendspace);
418 ATF_REQUIRE(buf != NULL);
420 domainsocketpair(fd);
423 rc = fcntl(fd[0], F_SETFL, O_NONBLOCK);
424 ATF_REQUIRE_MSG(rc != -1, "fcntl(O_NONBLOCK) failed: %s",
426 rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
427 ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
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)));
441 send_cmsg(int sockfd, void *cmsg, size_t cmsgsz)
444 struct msghdr msghdr;
449 bzero(&msghdr, sizeof(msghdr));
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;
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));
466 recv_cmsg(int sockfd, char *cmsg, size_t cmsgsz, int flags)
469 struct msghdr msghdr;
474 bzero(&msghdr, sizeof(msghdr));
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;
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);
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.
497 ATF_TC_WITHOUT_HEAD(truncated_rights);
498 ATF_TC_BODY(truncated_rights, tc)
501 int fd[2], nfds, putfd, rc;
503 domainsocketpair(fd);
508 * Case 1: Send a single descriptor and truncate the message.
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);
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.
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);
535 * Case 2b: Send two descriptors in separate messages, and truncate
536 * before the end of the first message.
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);
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.
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),
559 rc = close(*(int *)CMSG_DATA(message));
560 ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno));
561 ATF_REQUIRE(getnfds() == nfds);
565 * Case 3: Send three descriptors in the same message, and leave space
566 * only for the first when receiving the message.
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);
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.
585 ATF_TC_WITHOUT_HEAD(copyout_rights_error);
586 ATF_TC_BODY(copyout_rights_error, tc)
589 struct msghdr msghdr;
592 int fd[2], error, nfds, putfd;
594 memset(buf, 0, sizeof(buf));
595 domainsocketpair(fd);
599 sendfd_payload(fd[0], putfd, buf, sizeof(buf));
601 bzero(&msghdr, sizeof(msghdr));
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;
610 len = recvmsg(fd[1], &msghdr, 0);
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));
616 /* Verify that no FDs were leaked. */
617 ATF_REQUIRE(getnfds() == nfds);
626 ATF_TP_ADD_TC(tp, simple_send_fd);
627 ATF_TP_ADD_TC(tp, send_and_close);
628 ATF_TP_ADD_TC(tp, send_and_cancel);
629 ATF_TP_ADD_TC(tp, two_files);
630 ATF_TP_ADD_TC(tp, bundle);
631 ATF_TP_ADD_TC(tp, bundle_cancel);
632 ATF_TP_ADD_TC(tp, devfs_orphan);
633 ATF_TP_ADD_TC(tp, rights_creds_payload);
634 ATF_TP_ADD_TC(tp, truncated_rights);
635 ATF_TP_ADD_TC(tp, copyout_rights_error);
637 return (atf_no_error());