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
30 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <sys/sysctl.h>
47 * UNIX domain sockets allow file descriptors to be passed via "ancillary
48 * data", or control messages. This regression test is intended to exercise
49 * this facility, both performing some basic tests that it operates, and also
50 * causing some kernel edge cases to execute, such as garbage collection when
51 * there are cyclic file descriptor references. Right now we test only with
52 * stream sockets, but ideally we'd also test with datagram sockets.
56 domainsocketpair(int *fdp)
59 ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
60 "socketpair(PF_UNIX, SOCK_STREAM) failed: %s", strerror(errno));
64 closesocketpair(int *fdp)
76 fd = open("/dev/null", O_RDONLY);
77 ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
87 snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
88 getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
90 ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
96 dofstat(int fd, struct stat *sb)
99 ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
100 "fstat failed: %s", strerror(errno));
104 samefile(struct stat *sb1, struct stat *sb2)
107 ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
108 ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
112 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
115 char message[CMSG_SPACE(sizeof(int))];
116 struct cmsghdr *cmsghdr;
117 struct msghdr msghdr;
120 bzero(&msghdr, sizeof(msghdr));
121 bzero(&message, sizeof(message));
123 msghdr.msg_control = message;
124 msghdr.msg_controllen = sizeof(message);
126 iovec.iov_base = payload;
127 iovec.iov_len = paylen;
129 msghdr.msg_iov = &iovec;
130 msghdr.msg_iovlen = 1;
132 cmsghdr = (struct cmsghdr *)(void*)message;
133 cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
134 cmsghdr->cmsg_level = SOL_SOCKET;
135 cmsghdr->cmsg_type = SCM_RIGHTS;
136 memcpy(CMSG_DATA(cmsghdr), &send_fd, sizeof(int));
138 len = sendmsg(sockfd, &msghdr, 0);
139 ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
140 ATF_REQUIRE_MSG((size_t)len == paylen,
141 "sendmsg: %zd messages sent; expected: %zu; %s", len, paylen,
146 sendfd(int sockfd, int send_fd)
150 return (sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)));
154 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen)
156 struct cmsghdr *cmsghdr;
157 char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
158 struct msghdr msghdr;
162 bzero(&msghdr, sizeof(msghdr));
164 msghdr.msg_control = message;
165 msghdr.msg_controllen = sizeof(message);
167 iovec.iov_base = buf;
168 iovec.iov_len = buflen;
170 msghdr.msg_iov = &iovec;
171 msghdr.msg_iovlen = 1;
173 len = recvmsg(sockfd, &msghdr, 0);
174 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
175 ATF_REQUIRE_MSG((size_t)len == buflen,
176 "recvmsg: %zd bytes received; expected %zd", len, buflen);
178 cmsghdr = CMSG_FIRSTHDR(&msghdr);
179 ATF_REQUIRE_MSG(cmsghdr != NULL,
180 "recvmsg: did not receive control message");
182 for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
183 if (cmsghdr->cmsg_level == SOL_SOCKET &&
184 cmsghdr->cmsg_type == SCM_RIGHTS &&
185 cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
186 memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
187 ATF_REQUIRE(*recv_fd != -1);
190 ATF_REQUIRE_MSG(*recv_fd != -1,
191 "recvmsg: did not receive single-fd message");
195 recvfd(int sockfd, int *recv_fd)
199 return (recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch)));
203 * Put a temporary file into a UNIX domain socket, then take it out and make
204 * sure it's the same file. First time around, don't close the reference
207 ATF_TC_WITHOUT_HEAD(simple_send_fd);
208 ATF_TC_BODY(simple_send_fd, tc)
210 struct stat getfd_stat, putfd_stat;
211 int fd[2], getfd, putfd;
213 domainsocketpair(fd);
215 dofstat(putfd, &putfd_stat);
216 sendfd(fd[0], putfd);
217 recvfd(fd[1], &getfd);
218 dofstat(getfd, &getfd_stat);
219 samefile(&putfd_stat, &getfd_stat);
226 * Same as simple_send_fd, only close the file reference after sending, so that
227 * the only reference is the descriptor in the UNIX domain socket buffer.
229 ATF_TC_WITHOUT_HEAD(send_and_close);
230 ATF_TC_BODY(send_and_close, tc)
232 struct stat getfd_stat, putfd_stat;
233 int fd[2], getfd, putfd;
235 domainsocketpair(fd);
237 dofstat(putfd, &putfd_stat);
238 sendfd(fd[0], putfd);
240 recvfd(fd[1], &getfd);
241 dofstat(getfd, &getfd_stat);
242 samefile(&putfd_stat, &getfd_stat);
248 * Put a temporary file into a UNIX domain socket, then close both endpoints
249 * causing garbage collection to kick off.
251 ATF_TC_WITHOUT_HEAD(send_and_cancel);
252 ATF_TC_BODY(send_and_cancel, tc)
256 domainsocketpair(fd);
258 sendfd(fd[0], putfd);
264 * Send two files. Then receive them. Make sure they are returned in the
265 * right order, and both get there.
267 ATF_TC_WITHOUT_HEAD(two_files);
268 ATF_TC_BODY(two_files, tc)
270 struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
271 int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
273 domainsocketpair(fd);
276 dofstat(putfd_1, &putfd_1_stat);
277 dofstat(putfd_2, &putfd_2_stat);
278 sendfd(fd[0], putfd_1);
279 sendfd(fd[0], putfd_2);
282 recvfd(fd[1], &getfd_1);
283 recvfd(fd[1], &getfd_2);
284 dofstat(getfd_1, &getfd_1_stat);
285 dofstat(getfd_2, &getfd_2_stat);
286 samefile(&putfd_1_stat, &getfd_1_stat);
287 samefile(&putfd_2_stat, &getfd_2_stat);
294 * Big bundling test. Send an endpoint of the UNIX domain socket over itself,
295 * closing the door behind it.
297 ATF_TC_WITHOUT_HEAD(bundle);
298 ATF_TC_BODY(bundle, tc)
302 domainsocketpair(fd);
304 sendfd(fd[0], fd[0]);
306 recvfd(fd[1], &getfd);
312 * Big bundling test part two: Send an endpoint of the UNIX domain socket over
313 * itself, close the door behind it, and never remove it from the other end.
315 ATF_TC_WITHOUT_HEAD(bundle_cancel);
316 ATF_TC_BODY(bundle_cancel, tc)
320 domainsocketpair(fd);
321 sendfd(fd[0], fd[0]);
322 sendfd(fd[1], fd[0]);
327 * Test for PR 151758: Send an character device over the UNIX domain socket
328 * and then close both sockets to orphan the device.
330 ATF_TC_WITHOUT_HEAD(devfs_orphan);
331 ATF_TC_BODY(devfs_orphan, tc)
335 domainsocketpair(fd);
337 sendfd(fd[0], putfd);
342 #define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace"
345 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
346 * control message to the data. Sender sends large payload.
347 * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver
348 * receives truncated data.
350 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
351 ATF_TC_BODY(rights_creds_payload, tc)
357 int fd[2], getfd, putfd, rc;
359 atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages "
360 "are present with large data");
362 len = sizeof(sendspace);
363 rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
365 ATF_REQUIRE_MSG(rc != -1,
366 "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
368 buf = calloc(1, sendspace);
369 ATF_REQUIRE(buf != NULL);
371 domainsocketpair(fd);
372 rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
373 ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
376 sendfd_payload(fd[0], putfd, buf, sendspace);
377 recvfd_payload(fd[1], &getfd, buf, sendspace);
386 ATF_TP_ADD_TC(tp, simple_send_fd);
387 ATF_TP_ADD_TC(tp, send_and_close);
388 ATF_TP_ADD_TC(tp, send_and_cancel);
389 ATF_TP_ADD_TC(tp, two_files);
390 ATF_TP_ADD_TC(tp, bundle);
391 ATF_TP_ADD_TC(tp, bundle_cancel);
392 ATF_TP_ADD_TC(tp, devfs_orphan);
393 ATF_TP_ADD_TC(tp, rights_creds_payload);
395 return (atf_no_error());