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 samefile(struct stat *sb1, struct stat *sb2) 123 { 124 125 ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device"); 126 ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode"); 127 } 128 129 static void 130 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen) 131 { 132 struct iovec iovec; 133 char message[CMSG_SPACE(sizeof(int))]; 134 struct cmsghdr *cmsghdr; 135 struct msghdr msghdr; 136 ssize_t len; 137 138 bzero(&msghdr, sizeof(msghdr)); 139 bzero(&message, sizeof(message)); 140 141 msghdr.msg_control = message; 142 msghdr.msg_controllen = sizeof(message); 143 144 iovec.iov_base = payload; 145 iovec.iov_len = paylen; 146 147 msghdr.msg_iov = &iovec; 148 msghdr.msg_iovlen = 1; 149 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)); 155 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, 160 strerror(errno)); 161 } 162 163 static void 164 sendfd(int sockfd, int send_fd) 165 { 166 char ch = 0; 167 168 sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)); 169 } 170 171 static void 172 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen) 173 { 174 struct cmsghdr *cmsghdr; 175 char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + 176 CMSG_SPACE(sizeof(int))]; 177 struct msghdr msghdr; 178 struct iovec iovec; 179 ssize_t len; 180 181 bzero(&msghdr, sizeof(msghdr)); 182 183 msghdr.msg_control = message; 184 msghdr.msg_controllen = sizeof(message); 185 186 iovec.iov_base = buf; 187 iovec.iov_len = buflen; 188 189 msghdr.msg_iov = &iovec; 190 msghdr.msg_iovlen = 1; 191 192 len = recvmsg(sockfd, &msghdr, 0); 193 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno)); 194 ATF_REQUIRE_MSG((size_t)len == buflen, 195 "recvmsg: %zd bytes received; expected %zd", len, buflen); 196 197 cmsghdr = CMSG_FIRSTHDR(&msghdr); 198 ATF_REQUIRE_MSG(cmsghdr != NULL, 199 "recvmsg: did not receive control message"); 200 *recv_fd = -1; 201 for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) { 202 if (cmsghdr->cmsg_level == SOL_SOCKET && 203 cmsghdr->cmsg_type == SCM_RIGHTS && 204 cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) { 205 memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int)); 206 ATF_REQUIRE(*recv_fd != -1); 207 } 208 } 209 ATF_REQUIRE_MSG(*recv_fd != -1, 210 "recvmsg: did not receive single-fd message"); 211 } 212 213 static void 214 recvfd(int sockfd, int *recv_fd) 215 { 216 char ch = 0; 217 218 recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch)); 219 } 220 221 /* 222 * Put a temporary file into a UNIX domain socket, then take it out and make 223 * sure it's the same file. First time around, don't close the reference 224 * after sending. 225 */ 226 ATF_TC_WITHOUT_HEAD(simple_send_fd); 227 ATF_TC_BODY(simple_send_fd, tc) 228 { 229 struct stat getfd_stat, putfd_stat; 230 int fd[2], getfd, putfd; 231 232 domainsocketpair(fd); 233 tempfile(&putfd); 234 dofstat(putfd, &putfd_stat); 235 sendfd(fd[0], putfd); 236 recvfd(fd[1], &getfd); 237 dofstat(getfd, &getfd_stat); 238 samefile(&putfd_stat, &getfd_stat); 239 close(putfd); 240 close(getfd); 241 closesocketpair(fd); 242 } 243 244 /* 245 * Same as simple_send_fd, only close the file reference after sending, so that 246 * the only reference is the descriptor in the UNIX domain socket buffer. 247 */ 248 ATF_TC_WITHOUT_HEAD(send_and_close); 249 ATF_TC_BODY(send_and_close, tc) 250 { 251 struct stat getfd_stat, putfd_stat; 252 int fd[2], getfd, putfd; 253 254 domainsocketpair(fd); 255 tempfile(&putfd); 256 dofstat(putfd, &putfd_stat); 257 sendfd(fd[0], putfd); 258 close(putfd); 259 recvfd(fd[1], &getfd); 260 dofstat(getfd, &getfd_stat); 261 samefile(&putfd_stat, &getfd_stat); 262 close(getfd); 263 closesocketpair(fd); 264 } 265 266 /* 267 * Put a temporary file into a UNIX domain socket, then close both endpoints 268 * causing garbage collection to kick off. 269 */ 270 ATF_TC_WITHOUT_HEAD(send_and_cancel); 271 ATF_TC_BODY(send_and_cancel, tc) 272 { 273 int fd[2], putfd; 274 275 domainsocketpair(fd); 276 tempfile(&putfd); 277 sendfd(fd[0], putfd); 278 close(putfd); 279 closesocketpair(fd); 280 } 281 282 /* 283 * Send two files. Then receive them. Make sure they are returned in the 284 * right order, and both get there. 285 */ 286 ATF_TC_WITHOUT_HEAD(two_files); 287 ATF_TC_BODY(two_files, tc) 288 { 289 struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat; 290 int fd[2], getfd_1, getfd_2, putfd_1, putfd_2; 291 292 domainsocketpair(fd); 293 tempfile(&putfd_1); 294 tempfile(&putfd_2); 295 dofstat(putfd_1, &putfd_1_stat); 296 dofstat(putfd_2, &putfd_2_stat); 297 sendfd(fd[0], putfd_1); 298 sendfd(fd[0], putfd_2); 299 close(putfd_1); 300 close(putfd_2); 301 recvfd(fd[1], &getfd_1); 302 recvfd(fd[1], &getfd_2); 303 dofstat(getfd_1, &getfd_1_stat); 304 dofstat(getfd_2, &getfd_2_stat); 305 samefile(&putfd_1_stat, &getfd_1_stat); 306 samefile(&putfd_2_stat, &getfd_2_stat); 307 close(getfd_1); 308 close(getfd_2); 309 closesocketpair(fd); 310 } 311 312 /* 313 * Big bundling test. Send an endpoint of the UNIX domain socket over itself, 314 * closing the door behind it. 315 */ 316 ATF_TC_WITHOUT_HEAD(bundle); 317 ATF_TC_BODY(bundle, tc) 318 { 319 int fd[2], getfd; 320 321 domainsocketpair(fd); 322 323 sendfd(fd[0], fd[0]); 324 close(fd[0]); 325 recvfd(fd[1], &getfd); 326 close(getfd); 327 close(fd[1]); 328 } 329 330 /* 331 * Big bundling test part two: Send an endpoint of the UNIX domain socket over 332 * itself, close the door behind it, and never remove it from the other end. 333 */ 334 ATF_TC_WITHOUT_HEAD(bundle_cancel); 335 ATF_TC_BODY(bundle_cancel, tc) 336 { 337 int fd[2]; 338 339 domainsocketpair(fd); 340 sendfd(fd[0], fd[0]); 341 sendfd(fd[1], fd[0]); 342 closesocketpair(fd); 343 } 344 345 /* 346 * Test for PR 151758: Send an character device over the UNIX domain socket 347 * and then close both sockets to orphan the device. 348 */ 349 ATF_TC_WITHOUT_HEAD(devfs_orphan); 350 ATF_TC_BODY(devfs_orphan, tc) 351 { 352 int fd[2], putfd; 353 354 domainsocketpair(fd); 355 devnull(&putfd); 356 sendfd(fd[0], putfd); 357 close(putfd); 358 closesocketpair(fd); 359 } 360 361 #define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace" 362 363 /* 364 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a 365 * control message to the data. Sender sends large payload. 366 * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver 367 * receives truncated data. 368 */ 369 ATF_TC_WITHOUT_HEAD(rights_creds_payload); 370 ATF_TC_BODY(rights_creds_payload, tc) 371 { 372 const int on = 1; 373 u_long sendspace; 374 size_t len; 375 void *buf; 376 int fd[2], getfd, putfd, rc; 377 378 atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages " 379 "are present with large data"); 380 381 len = sizeof(sendspace); 382 rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace, 383 &len, NULL, 0); 384 ATF_REQUIRE_MSG(rc != -1, 385 "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno)); 386 387 buf = calloc(1, sendspace); 388 ATF_REQUIRE(buf != NULL); 389 390 domainsocketpair(fd); 391 rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on)); 392 ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s", 393 strerror(errno)); 394 tempfile(&putfd); 395 sendfd_payload(fd[0], putfd, buf, sendspace); 396 recvfd_payload(fd[1], &getfd, buf, sendspace); 397 close(putfd); 398 close(getfd); 399 closesocketpair(fd); 400 } 401 402 /* 403 * Test for PR 131876. Receiver uses a control message buffer that is too 404 * small for the incoming SCM_RIGHTS message, so the message is truncated. 405 * The kernel must not leak the copied right into the receiver's namespace. 406 */ 407 ATF_TC_WITHOUT_HEAD(truncated_rights); 408 ATF_TC_BODY(truncated_rights, tc) 409 { 410 struct iovec iovec; 411 struct msghdr msghdr; 412 char buf[16], message[CMSG_SPACE(0)]; 413 ssize_t len; 414 int fd[2], nfds, putfd; 415 416 atf_tc_expect_fail("PR 131876: " 417 "FD leak when 'control' message is truncated"); 418 419 memset(buf, 42, sizeof(buf)); 420 domainsocketpair(fd); 421 devnull(&putfd); 422 nfds = getnfds(); 423 424 sendfd_payload(fd[0], putfd, buf, sizeof(buf)); 425 426 bzero(&msghdr, sizeof(msghdr)); 427 bzero(message, sizeof(message)); 428 429 iovec.iov_base = buf; 430 iovec.iov_len = sizeof(buf); 431 msghdr.msg_control = message; 432 msghdr.msg_controllen = sizeof(message); 433 msghdr.msg_iov = &iovec; 434 msghdr.msg_iovlen = 1; 435 436 len = recvmsg(fd[1], &msghdr, 0); 437 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno)); 438 ATF_REQUIRE_MSG((size_t)len == sizeof(buf), 439 "recvmsg: %zd bytes received; expected %zd", len, sizeof(buf)); 440 for (size_t i = 0; i < sizeof(buf); i++) 441 ATF_REQUIRE_MSG(buf[i] == 42, "unexpected buffer contents"); 442 443 ATF_REQUIRE_MSG((msghdr.msg_flags & MSG_CTRUNC) != 0, 444 "MSG_CTRUNC not set after truncation"); 445 ATF_REQUIRE(getnfds() == nfds); 446 447 close(putfd); 448 closesocketpair(fd); 449 } 450 451 ATF_TC_WITHOUT_HEAD(copyout_rights_error); 452 ATF_TC_BODY(copyout_rights_error, tc) 453 { 454 struct iovec iovec; 455 struct msghdr msghdr; 456 char buf[16]; 457 ssize_t len; 458 int fd[2], error, nfds, putfd; 459 460 atf_tc_expect_fail("PR 131876: " 461 "FD leak when copyout of rights returns an error"); 462 463 memset(buf, 0, sizeof(buf)); 464 domainsocketpair(fd); 465 devnull(&putfd); 466 nfds = getnfds(); 467 468 sendfd_payload(fd[0], putfd, buf, sizeof(buf)); 469 470 bzero(&msghdr, sizeof(msghdr)); 471 472 iovec.iov_base = buf; 473 iovec.iov_len = sizeof(buf); 474 msghdr.msg_control = (char *)-1; /* trigger EFAULT */ 475 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); 476 msghdr.msg_iov = &iovec; 477 msghdr.msg_iovlen = 1; 478 479 len = recvmsg(fd[1], &msghdr, 0); 480 error = errno; 481 ATF_REQUIRE_MSG(len == -1, "recvmsg succeeded: %zd", len); 482 ATF_REQUIRE_MSG(errno == EFAULT, "expected EFAULT, got %d (%s)", 483 error, strerror(errno)); 484 485 /* Verify that no FDs were leaked. */ 486 ATF_REQUIRE(getnfds() == nfds); 487 488 close(putfd); 489 closesocketpair(fd); 490 } 491 492 ATF_TP_ADD_TCS(tp) 493 { 494 495 ATF_TP_ADD_TC(tp, simple_send_fd); 496 ATF_TP_ADD_TC(tp, send_and_close); 497 ATF_TP_ADD_TC(tp, send_and_cancel); 498 ATF_TP_ADD_TC(tp, two_files); 499 ATF_TP_ADD_TC(tp, bundle); 500 ATF_TP_ADD_TC(tp, bundle_cancel); 501 ATF_TP_ADD_TC(tp, devfs_orphan); 502 ATF_TP_ADD_TC(tp, rights_creds_payload); 503 ATF_TP_ADD_TC(tp, truncated_rights); 504 ATF_TP_ADD_TC(tp, copyout_rights_error); 505 506 return (atf_no_error()); 507 } 508