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 samefile(struct stat *sb1, struct stat *sb2)
125 ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
126 ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
130 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
133 char message[CMSG_SPACE(sizeof(int))];
134 struct cmsghdr *cmsghdr;
135 struct msghdr msghdr;
138 bzero(&msghdr, sizeof(msghdr));
139 bzero(&message, sizeof(message));
141 msghdr.msg_control = message;
142 msghdr.msg_controllen = sizeof(message);
144 iovec.iov_base = payload;
145 iovec.iov_len = paylen;
147 msghdr.msg_iov = &iovec;
148 msghdr.msg_iovlen = 1;
150 cmsghdr = (struct cmsghdr *)(void *)message;
151 cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
152 cmsghdr->cmsg_level = SOL_SOCKET;
153 cmsghdr->cmsg_type = SCM_RIGHTS;
154 memcpy(CMSG_DATA(cmsghdr), &send_fd, sizeof(int));
156 len = sendmsg(sockfd, &msghdr, 0);
157 ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
158 ATF_REQUIRE_MSG((size_t)len == paylen,
159 "sendmsg: %zd messages sent; expected: %zu; %s", len, paylen,
164 sendfd(int sockfd, int send_fd)
168 return (sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)));
172 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen)
174 struct cmsghdr *cmsghdr;
175 char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
176 struct msghdr msghdr;
180 bzero(&msghdr, sizeof(msghdr));
182 msghdr.msg_control = message;
183 msghdr.msg_controllen = sizeof(message);
185 iovec.iov_base = buf;
186 iovec.iov_len = buflen;
188 msghdr.msg_iov = &iovec;
189 msghdr.msg_iovlen = 1;
191 len = recvmsg(sockfd, &msghdr, 0);
192 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
193 ATF_REQUIRE_MSG((size_t)len == buflen,
194 "recvmsg: %zd bytes received; expected %zd", len, buflen);
196 cmsghdr = CMSG_FIRSTHDR(&msghdr);
197 ATF_REQUIRE_MSG(cmsghdr != NULL,
198 "recvmsg: did not receive control message");
200 for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
201 if (cmsghdr->cmsg_level == SOL_SOCKET &&
202 cmsghdr->cmsg_type == SCM_RIGHTS &&
203 cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
204 memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
205 ATF_REQUIRE(*recv_fd != -1);
208 ATF_REQUIRE_MSG(*recv_fd != -1,
209 "recvmsg: did not receive single-fd message");
213 recvfd(int sockfd, int *recv_fd)
217 return (recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch)));
221 * Put a temporary file into a UNIX domain socket, then take it out and make
222 * sure it's the same file. First time around, don't close the reference
225 ATF_TC_WITHOUT_HEAD(simple_send_fd);
226 ATF_TC_BODY(simple_send_fd, tc)
228 struct stat getfd_stat, putfd_stat;
229 int fd[2], getfd, putfd;
231 domainsocketpair(fd);
233 dofstat(putfd, &putfd_stat);
234 sendfd(fd[0], putfd);
235 recvfd(fd[1], &getfd);
236 dofstat(getfd, &getfd_stat);
237 samefile(&putfd_stat, &getfd_stat);
244 * Same as simple_send_fd, only close the file reference after sending, so that
245 * the only reference is the descriptor in the UNIX domain socket buffer.
247 ATF_TC_WITHOUT_HEAD(send_and_close);
248 ATF_TC_BODY(send_and_close, tc)
250 struct stat getfd_stat, putfd_stat;
251 int fd[2], getfd, putfd;
253 domainsocketpair(fd);
255 dofstat(putfd, &putfd_stat);
256 sendfd(fd[0], putfd);
258 recvfd(fd[1], &getfd);
259 dofstat(getfd, &getfd_stat);
260 samefile(&putfd_stat, &getfd_stat);
266 * Put a temporary file into a UNIX domain socket, then close both endpoints
267 * causing garbage collection to kick off.
269 ATF_TC_WITHOUT_HEAD(send_and_cancel);
270 ATF_TC_BODY(send_and_cancel, tc)
274 domainsocketpair(fd);
276 sendfd(fd[0], putfd);
282 * Send two files. Then receive them. Make sure they are returned in the
283 * right order, and both get there.
285 ATF_TC_WITHOUT_HEAD(two_files);
286 ATF_TC_BODY(two_files, tc)
288 struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
289 int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
291 domainsocketpair(fd);
294 dofstat(putfd_1, &putfd_1_stat);
295 dofstat(putfd_2, &putfd_2_stat);
296 sendfd(fd[0], putfd_1);
297 sendfd(fd[0], putfd_2);
300 recvfd(fd[1], &getfd_1);
301 recvfd(fd[1], &getfd_2);
302 dofstat(getfd_1, &getfd_1_stat);
303 dofstat(getfd_2, &getfd_2_stat);
304 samefile(&putfd_1_stat, &getfd_1_stat);
305 samefile(&putfd_2_stat, &getfd_2_stat);
312 * Big bundling test. Send an endpoint of the UNIX domain socket over itself,
313 * closing the door behind it.
315 ATF_TC_WITHOUT_HEAD(bundle);
316 ATF_TC_BODY(bundle, tc)
320 domainsocketpair(fd);
322 sendfd(fd[0], fd[0]);
324 recvfd(fd[1], &getfd);
330 * Big bundling test part two: Send an endpoint of the UNIX domain socket over
331 * itself, close the door behind it, and never remove it from the other end.
333 ATF_TC_WITHOUT_HEAD(bundle_cancel);
334 ATF_TC_BODY(bundle_cancel, tc)
338 domainsocketpair(fd);
339 sendfd(fd[0], fd[0]);
340 sendfd(fd[1], fd[0]);
345 * Test for PR 151758: Send an character device over the UNIX domain socket
346 * and then close both sockets to orphan the device.
348 ATF_TC_WITHOUT_HEAD(devfs_orphan);
349 ATF_TC_BODY(devfs_orphan, tc)
353 domainsocketpair(fd);
355 sendfd(fd[0], putfd);
360 #define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace"
363 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
364 * control message to the data. Sender sends large payload.
365 * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver
366 * receives truncated data.
368 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
369 ATF_TC_BODY(rights_creds_payload, tc)
375 int fd[2], getfd, putfd, rc;
377 atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages "
378 "are present with large data");
380 len = sizeof(sendspace);
381 rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
383 ATF_REQUIRE_MSG(rc != -1,
384 "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
386 buf = calloc(1, sendspace);
387 ATF_REQUIRE(buf != NULL);
389 domainsocketpair(fd);
390 rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
391 ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
394 sendfd_payload(fd[0], putfd, buf, sendspace);
395 recvfd_payload(fd[1], &getfd, buf, sendspace);
402 * Test for PR 131876. Receiver uses a control message buffer that is too
403 * small for the incoming SCM_RIGHTS message, so the message is truncated.
404 * The kernel must not leak the copied right into the receiver's namespace.
406 ATF_TC_WITHOUT_HEAD(truncated_rights);
407 ATF_TC_BODY(truncated_rights, tc)
410 struct msghdr msghdr;
411 char buf[16], message[CMSG_SPACE(0)];
413 int fd[2], nfds, putfd;
415 atf_tc_expect_fail("PR 131876: "
416 "FD leak when 'control' message is truncated");
418 memset(buf, 42, sizeof(buf));
419 domainsocketpair(fd);
423 sendfd_payload(fd[0], putfd, buf, sizeof(buf));
425 bzero(&msghdr, sizeof(msghdr));
426 bzero(message, sizeof(message));
428 iovec.iov_base = buf;
429 iovec.iov_len = sizeof(buf);
430 msghdr.msg_control = message;
431 msghdr.msg_controllen = sizeof(message);
432 msghdr.msg_iov = &iovec;
433 msghdr.msg_iovlen = 1;
435 len = recvmsg(fd[1], &msghdr, 0);
436 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
437 ATF_REQUIRE_MSG((size_t)len == sizeof(buf),
438 "recvmsg: %zd bytes received; expected %zd", len, sizeof(buf));
439 for (size_t i = 0; i < sizeof(buf); i++)
440 ATF_REQUIRE_MSG(buf[i] == 42, "unexpected buffer contents");
442 ATF_REQUIRE_MSG((msghdr.msg_flags & MSG_CTRUNC) != 0,
443 "MSG_CTRUNC not set after truncation");
444 ATF_REQUIRE(getnfds() == nfds);
453 ATF_TP_ADD_TC(tp, simple_send_fd);
454 ATF_TP_ADD_TC(tp, send_and_close);
455 ATF_TP_ADD_TC(tp, send_and_cancel);
456 ATF_TP_ADD_TC(tp, two_files);
457 ATF_TP_ADD_TC(tp, bundle);
458 ATF_TP_ADD_TC(tp, bundle_cancel);
459 ATF_TP_ADD_TC(tp, devfs_orphan);
460 ATF_TP_ADD_TC(tp, rights_creds_payload);
461 ATF_TP_ADD_TC(tp, truncated_rights);
463 return (atf_no_error());