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 int 122 openfiles(void) 123 { 124 int files; 125 size_t len = sizeof(files); 126 127 ATF_REQUIRE(sysctlbyname("kern.openfiles", &files, &len, NULL, 0) == 0); 128 129 return (files); 130 } 131 132 static void 133 putfds(char *buf, int fd, int nfds) 134 { 135 struct cmsghdr *cm; 136 int *fdp, i; 137 138 cm = (struct cmsghdr *)buf; 139 cm->cmsg_len = CMSG_LEN(nfds * sizeof(int)); 140 cm->cmsg_level = SOL_SOCKET; 141 cm->cmsg_type = SCM_RIGHTS; 142 for (fdp = (int *)CMSG_DATA(cm), i = 0; i < nfds; i++) 143 *fdp++ = fd; 144 } 145 146 static void 147 samefile(struct stat *sb1, struct stat *sb2) 148 { 149 150 ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device"); 151 ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode"); 152 } 153 154 static size_t 155 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen) 156 { 157 struct iovec iovec; 158 char message[CMSG_SPACE(sizeof(int))]; 159 struct msghdr msghdr; 160 ssize_t len; 161 162 bzero(&msghdr, sizeof(msghdr)); 163 bzero(&message, sizeof(message)); 164 165 msghdr.msg_control = message; 166 msghdr.msg_controllen = sizeof(message); 167 168 iovec.iov_base = payload; 169 iovec.iov_len = paylen; 170 171 msghdr.msg_iov = &iovec; 172 msghdr.msg_iovlen = 1; 173 174 putfds(message, send_fd, 1); 175 len = sendmsg(sockfd, &msghdr, 0); 176 ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno)); 177 return ((size_t)len); 178 } 179 180 static void 181 sendfd(int sockfd, int send_fd) 182 { 183 size_t len; 184 char ch; 185 186 ch = 0; 187 len = sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)); 188 ATF_REQUIRE_MSG(len == sizeof(ch), 189 "sendmsg: %zu bytes sent; expected %zu; %s", len, sizeof(ch), 190 strerror(errno)); 191 } 192 193 static bool 194 localcreds(int sockfd) 195 { 196 socklen_t sz; 197 int rc, val; 198 199 sz = sizeof(val); 200 rc = getsockopt(sockfd, 0, LOCAL_CREDS, &val, &sz); 201 ATF_REQUIRE_MSG(rc != -1, "getsockopt(LOCAL_CREDS) failed: %s", 202 strerror(errno)); 203 return (val != 0); 204 } 205 206 static void 207 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen, 208 size_t cmsgsz, int recvmsg_flags) 209 { 210 struct cmsghdr *cmsghdr; 211 struct msghdr msghdr; 212 struct iovec iovec; 213 char *message; 214 ssize_t len; 215 bool foundcreds; 216 217 bzero(&msghdr, sizeof(msghdr)); 218 message = malloc(cmsgsz); 219 ATF_REQUIRE(message != NULL); 220 221 msghdr.msg_control = message; 222 msghdr.msg_controllen = cmsgsz; 223 224 iovec.iov_base = buf; 225 iovec.iov_len = buflen; 226 227 msghdr.msg_iov = &iovec; 228 msghdr.msg_iovlen = 1; 229 230 len = recvmsg(sockfd, &msghdr, recvmsg_flags); 231 ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno)); 232 ATF_REQUIRE_MSG((size_t)len == buflen, 233 "recvmsg: %zd bytes received; expected %zd", len, buflen); 234 235 cmsghdr = CMSG_FIRSTHDR(&msghdr); 236 ATF_REQUIRE_MSG(cmsghdr != NULL, 237 "recvmsg: did not receive control message"); 238 foundcreds = false; 239 *recv_fd = -1; 240 for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) { 241 if (cmsghdr->cmsg_level == SOL_SOCKET && 242 cmsghdr->cmsg_type == SCM_RIGHTS && 243 cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) { 244 memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int)); 245 ATF_REQUIRE(*recv_fd != -1); 246 } else if (cmsghdr->cmsg_level == SOL_SOCKET && 247 cmsghdr->cmsg_type == SCM_CREDS) 248 foundcreds = true; 249 } 250 ATF_REQUIRE_MSG(*recv_fd != -1, 251 "recvmsg: did not receive single-fd message"); 252 ATF_REQUIRE_MSG(!localcreds(sockfd) || foundcreds, 253 "recvmsg: expected credentials were not received"); 254 } 255 256 static void 257 recvfd(int sockfd, int *recv_fd, int flags) 258 { 259 char ch = 0; 260 261 recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch), 262 CMSG_SPACE(sizeof(int)), flags); 263 } 264 265 /* 266 * Put a temporary file into a UNIX domain socket, then take it out and make 267 * sure it's the same file. First time around, don't close the reference 268 * after sending. 269 */ 270 ATF_TC_WITHOUT_HEAD(simple_send_fd); 271 ATF_TC_BODY(simple_send_fd, tc) 272 { 273 struct stat getfd_stat, putfd_stat; 274 int fd[2], getfd, putfd; 275 276 domainsocketpair(fd); 277 tempfile(&putfd); 278 dofstat(putfd, &putfd_stat); 279 sendfd(fd[0], putfd); 280 recvfd(fd[1], &getfd, 0); 281 dofstat(getfd, &getfd_stat); 282 samefile(&putfd_stat, &getfd_stat); 283 close(putfd); 284 close(getfd); 285 closesocketpair(fd); 286 } 287 288 /* 289 * Like simple_send_fd but also sets MSG_CMSG_CLOEXEC and checks that the 290 * received file descriptor has the FD_CLOEXEC flag set. 291 */ 292 ATF_TC_WITHOUT_HEAD(simple_send_fd_msg_cmsg_cloexec); 293 ATF_TC_BODY(simple_send_fd_msg_cmsg_cloexec, tc) 294 { 295 struct stat getfd_stat, putfd_stat; 296 int fd[2], getfd, putfd; 297 298 domainsocketpair(fd); 299 tempfile(&putfd); 300 dofstat(putfd, &putfd_stat); 301 sendfd(fd[0], putfd); 302 recvfd(fd[1], &getfd, MSG_CMSG_CLOEXEC); 303 dofstat(getfd, &getfd_stat); 304 samefile(&putfd_stat, &getfd_stat); 305 ATF_REQUIRE_EQ_MSG(fcntl(getfd, F_GETFD) & FD_CLOEXEC, FD_CLOEXEC, 306 "FD_CLOEXEC not set on the received file descriptor"); 307 close(putfd); 308 close(getfd); 309 closesocketpair(fd); 310 } 311 312 /* 313 * Same as simple_send_fd, only close the file reference after sending, so that 314 * the only reference is the descriptor in the UNIX domain socket buffer. 315 */ 316 ATF_TC_WITHOUT_HEAD(send_and_close); 317 ATF_TC_BODY(send_and_close, tc) 318 { 319 struct stat getfd_stat, putfd_stat; 320 int fd[2], getfd, putfd; 321 322 domainsocketpair(fd); 323 tempfile(&putfd); 324 dofstat(putfd, &putfd_stat); 325 sendfd(fd[0], putfd); 326 close(putfd); 327 recvfd(fd[1], &getfd, 0); 328 dofstat(getfd, &getfd_stat); 329 samefile(&putfd_stat, &getfd_stat); 330 close(getfd); 331 closesocketpair(fd); 332 } 333 334 /* 335 * Put a temporary file into a UNIX domain socket, then close both endpoints 336 * causing garbage collection to kick off. 337 */ 338 ATF_TC_WITHOUT_HEAD(send_and_cancel); 339 ATF_TC_BODY(send_and_cancel, tc) 340 { 341 int fd[2], putfd; 342 343 domainsocketpair(fd); 344 tempfile(&putfd); 345 sendfd(fd[0], putfd); 346 close(putfd); 347 closesocketpair(fd); 348 } 349 350 /* 351 * Send file then shutdown receive side to exercise unp_dispose() call 352 * via soshutdown(). Check that shutdown(SHUT_RD) would gc the file 353 * reference sitting in the receive buffer. There is no good way of 354 * checking that except using global open file count. 355 */ 356 ATF_TC_WITHOUT_HEAD(send_and_shutdown); 357 ATF_TC_BODY(send_and_shutdown, tc) 358 { 359 int fd[2], putfd, nfiles; 360 361 domainsocketpair(fd); 362 tempfile(&putfd); 363 sendfd(fd[0], putfd); 364 nfiles = openfiles(); 365 close(putfd); 366 ATF_REQUIRE(openfiles() == nfiles); 367 shutdown(fd[1], SHUT_RD); 368 ATF_REQUIRE(openfiles() == nfiles - 1); 369 closesocketpair(fd); 370 } 371 372 /* 373 * Send two files. Then receive them. Make sure they are returned in the 374 * right order, and both get there. 375 */ 376 ATF_TC_WITHOUT_HEAD(two_files); 377 ATF_TC_BODY(two_files, tc) 378 { 379 struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat; 380 int fd[2], getfd_1, getfd_2, putfd_1, putfd_2; 381 382 domainsocketpair(fd); 383 tempfile(&putfd_1); 384 tempfile(&putfd_2); 385 dofstat(putfd_1, &putfd_1_stat); 386 dofstat(putfd_2, &putfd_2_stat); 387 sendfd(fd[0], putfd_1); 388 sendfd(fd[0], putfd_2); 389 close(putfd_1); 390 close(putfd_2); 391 recvfd(fd[1], &getfd_1, 0); 392 recvfd(fd[1], &getfd_2, 0); 393 dofstat(getfd_1, &getfd_1_stat); 394 dofstat(getfd_2, &getfd_2_stat); 395 samefile(&putfd_1_stat, &getfd_1_stat); 396 samefile(&putfd_2_stat, &getfd_2_stat); 397 close(getfd_1); 398 close(getfd_2); 399 closesocketpair(fd); 400 } 401 402 /* 403 * Big bundling test. Send an endpoint of the UNIX domain socket over itself, 404 * closing the door behind it. 405 */ 406 ATF_TC_WITHOUT_HEAD(bundle); 407 ATF_TC_BODY(bundle, tc) 408 { 409 int fd[2], getfd; 410 411 domainsocketpair(fd); 412 413 sendfd(fd[0], fd[0]); 414 close(fd[0]); 415 recvfd(fd[1], &getfd, 0); 416 close(getfd); 417 close(fd[1]); 418 } 419 420 /* 421 * Big bundling test part two: Send an endpoint of the UNIX domain socket over 422 * itself, close the door behind it, and never remove it from the other end. 423 */ 424 ATF_TC_WITHOUT_HEAD(bundle_cancel); 425 ATF_TC_BODY(bundle_cancel, tc) 426 { 427 int fd[2]; 428 429 domainsocketpair(fd); 430 sendfd(fd[0], fd[0]); 431 sendfd(fd[1], fd[0]); 432 closesocketpair(fd); 433 } 434 435 /* 436 * Test for PR 151758: Send an character device over the UNIX domain socket 437 * and then close both sockets to orphan the device. 438 */ 439 ATF_TC_WITHOUT_HEAD(devfs_orphan); 440 ATF_TC_BODY(devfs_orphan, tc) 441 { 442 int fd[2], putfd; 443 444 domainsocketpair(fd); 445 devnull(&putfd); 446 sendfd(fd[0], putfd); 447 close(putfd); 448 closesocketpair(fd); 449 } 450 451 #define LOCAL_SENDSPACE_SYSCTL "net.local.stream.sendspace" 452 453 /* 454 * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a 455 * control message to the data. Sender sends large payload using a non-blocking 456 * socket. Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and 457 * receiver receives truncated data. 458 */ 459 ATF_TC_WITHOUT_HEAD(rights_creds_payload); 460 ATF_TC_BODY(rights_creds_payload, tc) 461 { 462 const int on = 1; 463 u_long sendspace; 464 size_t len; 465 void *buf; 466 int fd[2], getfd, putfd, rc; 467 468 len = sizeof(sendspace); 469 rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace, 470 &len, NULL, 0); 471 ATF_REQUIRE_MSG(rc != -1, 472 "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno)); 473 474 buf = calloc(1, sendspace); 475 ATF_REQUIRE(buf != NULL); 476 477 domainsocketpair(fd); 478 tempfile(&putfd); 479 480 rc = fcntl(fd[0], F_SETFL, O_NONBLOCK); 481 ATF_REQUIRE_MSG(rc != -1, "fcntl(O_NONBLOCK) failed: %s", 482 strerror(errno)); 483 rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on)); 484 ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s", 485 strerror(errno)); 486 487 len = sendfd_payload(fd[0], putfd, buf, sendspace); 488 ATF_REQUIRE_MSG(len < sendspace, "sendmsg: %zu bytes sent", len); 489 recvfd_payload(fd[1], &getfd, buf, len, 490 CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + CMSG_SPACE(sizeof(int)), 0); 491 492 close(putfd); 493 close(getfd); 494 closesocketpair(fd); 495 } 496 497 static void 498 send_cmsg(int sockfd, void *cmsg, size_t cmsgsz) 499 { 500 struct iovec iov; 501 struct msghdr msghdr; 502 ssize_t len; 503 char ch; 504 505 ch = 0; 506 bzero(&msghdr, sizeof(msghdr)); 507 508 iov.iov_base = &ch; 509 iov.iov_len = sizeof(ch); 510 msghdr.msg_control = cmsg; 511 msghdr.msg_controllen = cmsgsz; 512 msghdr.msg_iov = &iov; 513 msghdr.msg_iovlen = 1; 514 515 len = sendmsg(sockfd, &msghdr, 0); 516 ATF_REQUIRE_MSG(len != -1, 517 "sendmsg failed: %s", strerror(errno)); 518 ATF_REQUIRE_MSG(len == sizeof(ch), 519 "sendmsg: %zd bytes sent; expected %zu", len, sizeof(ch)); 520 } 521 522 static void 523 recv_cmsg(int sockfd, char *cmsg, size_t cmsgsz, int flags) 524 { 525 struct iovec iov; 526 struct msghdr msghdr; 527 ssize_t len; 528 char ch; 529 530 ch = 0; 531 bzero(&msghdr, sizeof(msghdr)); 532 533 iov.iov_base = &ch; 534 iov.iov_len = sizeof(ch); 535 msghdr.msg_control = cmsg; 536 msghdr.msg_controllen = cmsgsz; 537 msghdr.msg_iov = &iov; 538 msghdr.msg_iovlen = 1; 539 540 len = recvmsg(sockfd, &msghdr, 0); 541 ATF_REQUIRE_MSG(len != -1, 542 "recvmsg failed: %s", strerror(errno)); 543 ATF_REQUIRE_MSG(len == sizeof(ch), 544 "recvmsg: %zd bytes received; expected %zu", len, sizeof(ch)); 545 ATF_REQUIRE_MSG((msghdr.msg_flags & flags) == flags, 546 "recvmsg: got flags %#x; expected %#x", msghdr.msg_flags, flags); 547 } 548 549 /* 550 * Test for PR 131876. Receiver uses a control message buffer that is too 551 * small for the incoming SCM_RIGHTS message, so the message is truncated. 552 * The kernel must not leak the copied right into the receiver's namespace. 553 */ 554 ATF_TC_WITHOUT_HEAD(truncated_rights); 555 ATF_TC_BODY(truncated_rights, tc) 556 { 557 char *message; 558 int fd[2], nfds, putfd, rc; 559 560 domainsocketpair(fd); 561 devnull(&putfd); 562 nfds = getnfds(); 563 564 /* 565 * Case 1: Send a single descriptor and truncate the message. 566 */ 567 message = malloc(CMSG_SPACE(sizeof(int))); 568 ATF_REQUIRE(message != NULL); 569 putfds(message, putfd, 1); 570 send_cmsg(fd[0], message, CMSG_LEN(sizeof(int))); 571 recv_cmsg(fd[1], message, CMSG_LEN(0), MSG_CTRUNC); 572 ATF_REQUIRE(getnfds() == nfds); 573 free(message); 574 575 /* 576 * Case 2a: Send two descriptors in separate messages, and truncate at 577 * the boundary between the two messages. We should still 578 * receive the first message. 579 */ 580 message = malloc(CMSG_SPACE(sizeof(int)) * 2); 581 ATF_REQUIRE(message != NULL); 582 putfds(message, putfd, 1); 583 putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1); 584 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2); 585 recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC); 586 rc = close(*(int *)CMSG_DATA(message)); 587 ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno)); 588 ATF_REQUIRE(getnfds() == nfds); 589 free(message); 590 591 /* 592 * Case 2b: Send two descriptors in separate messages, and truncate 593 * before the end of the first message. 594 */ 595 message = malloc(CMSG_SPACE(sizeof(int)) * 2); 596 ATF_REQUIRE(message != NULL); 597 putfds(message, putfd, 1); 598 putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1); 599 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2); 600 recv_cmsg(fd[1], message, CMSG_SPACE(0), MSG_CTRUNC); 601 ATF_REQUIRE(getnfds() == nfds); 602 free(message); 603 604 /* 605 * Case 2c: Send two descriptors in separate messages, and truncate 606 * after the end of the first message. We should still 607 * receive the first message. 608 */ 609 message = malloc(CMSG_SPACE(sizeof(int)) * 2); 610 ATF_REQUIRE(message != NULL); 611 putfds(message, putfd, 1); 612 putfds(message + CMSG_SPACE(sizeof(int)), putfd, 1); 613 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int)) * 2); 614 recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)) + CMSG_SPACE(0), 615 MSG_CTRUNC); 616 rc = close(*(int *)CMSG_DATA(message)); 617 ATF_REQUIRE_MSG(rc == 0, "close failed: %s", strerror(errno)); 618 ATF_REQUIRE(getnfds() == nfds); 619 free(message); 620 621 /* 622 * Case 3: Send three descriptors in the same message, and leave space 623 * only for the first when receiving the message. 624 */ 625 message = malloc(CMSG_SPACE(sizeof(int) * 3)); 626 ATF_REQUIRE(message != NULL); 627 putfds(message, putfd, 3); 628 send_cmsg(fd[0], message, CMSG_SPACE(sizeof(int) * 3)); 629 recv_cmsg(fd[1], message, CMSG_SPACE(sizeof(int)), MSG_CTRUNC); 630 ATF_REQUIRE(getnfds() == nfds); 631 free(message); 632 633 close(putfd); 634 closesocketpair(fd); 635 } 636 637 /* 638 * Ensure that an attempt to copy a SCM_RIGHTS message to the recipient 639 * fails. In this case the kernel must dispose of the externalized rights 640 * rather than leaking them into the recipient's file descriptor table. 641 */ 642 ATF_TC_WITHOUT_HEAD(copyout_rights_error); 643 ATF_TC_BODY(copyout_rights_error, tc) 644 { 645 struct iovec iovec; 646 struct msghdr msghdr; 647 char buf[16]; 648 ssize_t len; 649 int fd[2], error, nfds, putfd; 650 651 memset(buf, 0, sizeof(buf)); 652 domainsocketpair(fd); 653 devnull(&putfd); 654 nfds = getnfds(); 655 656 sendfd_payload(fd[0], putfd, buf, sizeof(buf)); 657 658 bzero(&msghdr, sizeof(msghdr)); 659 660 iovec.iov_base = buf; 661 iovec.iov_len = sizeof(buf); 662 msghdr.msg_control = (char *)-1; /* trigger EFAULT */ 663 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); 664 msghdr.msg_iov = &iovec; 665 msghdr.msg_iovlen = 1; 666 667 len = recvmsg(fd[1], &msghdr, 0); 668 error = errno; 669 ATF_REQUIRE_MSG(len == -1, "recvmsg succeeded: %zd", len); 670 ATF_REQUIRE_MSG(errno == EFAULT, "expected EFAULT, got %d (%s)", 671 error, strerror(errno)); 672 673 /* Verify that no FDs were leaked. */ 674 ATF_REQUIRE(getnfds() == nfds); 675 676 close(putfd); 677 closesocketpair(fd); 678 } 679 680 /* 681 * Verify that we can handle empty rights messages. Try sending two SCM_RIGHTS 682 * messages with a single call, one empty and one containing a single FD. 683 */ 684 ATF_TC_WITHOUT_HEAD(empty_rights_message); 685 ATF_TC_BODY(empty_rights_message, tc) 686 { 687 struct iovec iov; 688 struct msghdr msghdr; 689 char *cm, message[CMSG_SPACE(0) + CMSG_SPACE(sizeof(int))]; 690 ssize_t len; 691 int error, fd[2], putfd; 692 693 domainsocketpair(fd); 694 devnull(&putfd); 695 696 /* 697 * First, try sending an empty message followed by a non-empty message. 698 */ 699 cm = message; 700 putfds(cm, -1, 0); 701 cm += CMSG_SPACE(0); 702 putfds(cm, putfd, 1); 703 704 memset(&msghdr, 0, sizeof(msghdr)); 705 iov.iov_base = NULL; 706 iov.iov_len = 0; 707 msghdr.msg_control = message; 708 msghdr.msg_controllen = sizeof(message); 709 msghdr.msg_iov = &iov; 710 msghdr.msg_iovlen = 1; 711 712 len = sendmsg(fd[0], &msghdr, 0); 713 ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno)); 714 715 /* Only the non-empty message should be received. */ 716 len = recvmsg(fd[1], &msghdr, 0); 717 ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno)); 718 ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int))); 719 error = close(*(int *)CMSG_DATA(msghdr.msg_control)); 720 ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno)); 721 722 /* 723 * Now try sending with the non-empty message before the empty message. 724 */ 725 cm = message; 726 putfds(cm, putfd, 1); 727 cm += CMSG_SPACE(sizeof(int)); 728 putfds(cm, -1, 0); 729 730 memset(&msghdr, 0, sizeof(msghdr)); 731 iov.iov_base = NULL; 732 iov.iov_len = 0; 733 msghdr.msg_control = message; 734 msghdr.msg_controllen = CMSG_SPACE(sizeof(int)); 735 msghdr.msg_iov = &iov; 736 msghdr.msg_iovlen = 1; 737 738 len = sendmsg(fd[0], &msghdr, 0); 739 ATF_REQUIRE_MSG(len == 0, "sendmsg failed: %s", strerror(errno)); 740 741 /* Only the non-empty message should be received. */ 742 len = recvmsg(fd[1], &msghdr, 0); 743 ATF_REQUIRE_MSG(len == 0, "recvmsg failed: %s", strerror(errno)); 744 ATF_REQUIRE(msghdr.msg_controllen = CMSG_SPACE(sizeof(int))); 745 error = close(*(int *)CMSG_DATA(msghdr.msg_control)); 746 ATF_REQUIRE_MSG(error == 0, "close failed: %s", strerror(errno)); 747 748 (void)close(putfd); 749 } 750 751 ATF_TP_ADD_TCS(tp) 752 { 753 754 ATF_TP_ADD_TC(tp, simple_send_fd); 755 ATF_TP_ADD_TC(tp, simple_send_fd_msg_cmsg_cloexec); 756 ATF_TP_ADD_TC(tp, send_and_close); 757 ATF_TP_ADD_TC(tp, send_and_cancel); 758 ATF_TP_ADD_TC(tp, send_and_shutdown); 759 ATF_TP_ADD_TC(tp, two_files); 760 ATF_TP_ADD_TC(tp, bundle); 761 ATF_TP_ADD_TC(tp, bundle_cancel); 762 ATF_TP_ADD_TC(tp, devfs_orphan); 763 ATF_TP_ADD_TC(tp, rights_creds_payload); 764 ATF_TP_ADD_TC(tp, truncated_rights); 765 ATF_TP_ADD_TC(tp, copyout_rights_error); 766 ATF_TP_ADD_TC(tp, empty_rights_message); 767 768 return (atf_no_error()); 769 } 770