1 /* $NetBSD: uipc_usrreq.c,v 1.184 2018/03/19 16:26:26 roy Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000, 2004, 2008, 2009 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center, and by Andrew Doran. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1989, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95 62 */ 63 64 /* 65 * Copyright (c) 1997 Christopher G. Demetriou. All rights reserved. 66 * 67 * Redistribution and use in source and binary forms, with or without 68 * modification, are permitted provided that the following conditions 69 * are met: 70 * 1. Redistributions of source code must retain the above copyright 71 * notice, this list of conditions and the following disclaimer. 72 * 2. Redistributions in binary form must reproduce the above copyright 73 * notice, this list of conditions and the following disclaimer in the 74 * documentation and/or other materials provided with the distribution. 75 * 3. All advertising materials mentioning features or use of this software 76 * must display the following acknowledgement: 77 * This product includes software developed by the University of 78 * California, Berkeley and its contributors. 79 * 4. Neither the name of the University nor the names of its contributors 80 * may be used to endorse or promote products derived from this software 81 * without specific prior written permission. 82 * 83 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 84 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 85 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 86 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 87 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 88 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 89 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 90 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 91 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 92 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 93 * SUCH DAMAGE. 94 * 95 * @(#)uipc_usrreq.c 8.9 (Berkeley) 5/14/95 96 */ 97 98 #include <sys/cdefs.h> 99 __KERNEL_RCSID(0, "$NetBSD: uipc_usrreq.c,v 1.184 2018/03/19 16:26:26 roy Exp $"); 100 101 #ifdef _KERNEL_OPT 102 #include "opt_compat_netbsd.h" 103 #endif 104 105 #include <sys/param.h> 106 #include <sys/systm.h> 107 #include <sys/proc.h> 108 #include <sys/filedesc.h> 109 #include <sys/domain.h> 110 #include <sys/protosw.h> 111 #include <sys/socket.h> 112 #include <sys/socketvar.h> 113 #include <sys/unpcb.h> 114 #include <sys/un.h> 115 #include <sys/namei.h> 116 #include <sys/vnode.h> 117 #include <sys/file.h> 118 #include <sys/stat.h> 119 #include <sys/mbuf.h> 120 #include <sys/kauth.h> 121 #include <sys/kmem.h> 122 #include <sys/atomic.h> 123 #include <sys/uidinfo.h> 124 #include <sys/kernel.h> 125 #include <sys/kthread.h> 126 127 #ifdef COMPAT_70 128 #include <compat/sys/socket.h> 129 #endif 130 131 /* 132 * Unix communications domain. 133 * 134 * TODO: 135 * RDM 136 * rethink name space problems 137 * need a proper out-of-band 138 * 139 * Notes on locking: 140 * 141 * The generic rules noted in uipc_socket2.c apply. In addition: 142 * 143 * o We have a global lock, uipc_lock. 144 * 145 * o All datagram sockets are locked by uipc_lock. 146 * 147 * o For stream socketpairs, the two endpoints are created sharing the same 148 * independent lock. Sockets presented to PRU_CONNECT2 must already have 149 * matching locks. 150 * 151 * o Stream sockets created via socket() start life with their own 152 * independent lock. 153 * 154 * o Stream connections to a named endpoint are slightly more complicated. 155 * Sockets that have called listen() have their lock pointer mutated to 156 * the global uipc_lock. When establishing a connection, the connecting 157 * socket also has its lock mutated to uipc_lock, which matches the head 158 * (listening socket). We create a new socket for accept() to return, and 159 * that also shares the head's lock. Until the connection is completely 160 * done on both ends, all three sockets are locked by uipc_lock. Once the 161 * connection is complete, the association with the head's lock is broken. 162 * The connecting socket and the socket returned from accept() have their 163 * lock pointers mutated away from uipc_lock, and back to the connecting 164 * socket's original, independent lock. The head continues to be locked 165 * by uipc_lock. 166 * 167 * o If uipc_lock is determined to be a significant source of contention, 168 * it could easily be hashed out. It is difficult to simply make it an 169 * independent lock because of visibility / garbage collection issues: 170 * if a socket has been associated with a lock at any point, that lock 171 * must remain valid until the socket is no longer visible in the system. 172 * The lock must not be freed or otherwise destroyed until any sockets 173 * that had referenced it have also been destroyed. 174 */ 175 const struct sockaddr_un sun_noname = { 176 .sun_len = offsetof(struct sockaddr_un, sun_path), 177 .sun_family = AF_LOCAL, 178 }; 179 ino_t unp_ino; /* prototype for fake inode numbers */ 180 181 static struct mbuf * unp_addsockcred(struct lwp *, struct mbuf *); 182 static void unp_discard_later(file_t *); 183 static void unp_discard_now(file_t *); 184 static void unp_disconnect1(struct unpcb *); 185 static bool unp_drop(struct unpcb *, int); 186 static int unp_internalize(struct mbuf **); 187 static void unp_mark(file_t *); 188 static void unp_scan(struct mbuf *, void (*)(file_t *), int); 189 static void unp_shutdown1(struct unpcb *); 190 static void unp_thread(void *); 191 static void unp_thread_kick(void); 192 193 static kmutex_t *uipc_lock; 194 195 static kcondvar_t unp_thread_cv; 196 static lwp_t *unp_thread_lwp; 197 static SLIST_HEAD(,file) unp_thread_discard; 198 static int unp_defer; 199 200 /* 201 * Initialize Unix protocols. 202 */ 203 void 204 uipc_init(void) 205 { 206 int error; 207 208 uipc_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 209 cv_init(&unp_thread_cv, "unpgc"); 210 211 error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, unp_thread, 212 NULL, &unp_thread_lwp, "unpgc"); 213 if (error != 0) 214 panic("uipc_init %d", error); 215 } 216 217 static void 218 unp_connid(struct lwp *l, struct unpcb *unp, int flags) 219 { 220 unp->unp_connid.unp_pid = l->l_proc->p_pid; 221 unp->unp_connid.unp_euid = kauth_cred_geteuid(l->l_cred); 222 unp->unp_connid.unp_egid = kauth_cred_getegid(l->l_cred); 223 unp->unp_flags |= flags; 224 } 225 226 /* 227 * A connection succeeded: disassociate both endpoints from the head's 228 * lock, and make them share their own lock. There is a race here: for 229 * a very brief time one endpoint will be locked by a different lock 230 * than the other end. However, since the current thread holds the old 231 * lock (the listening socket's lock, the head) access can still only be 232 * made to one side of the connection. 233 */ 234 static void 235 unp_setpeerlocks(struct socket *so, struct socket *so2) 236 { 237 struct unpcb *unp; 238 kmutex_t *lock; 239 240 KASSERT(solocked2(so, so2)); 241 242 /* 243 * Bail out if either end of the socket is not yet fully 244 * connected or accepted. We only break the lock association 245 * with the head when the pair of sockets stand completely 246 * on their own. 247 */ 248 KASSERT(so->so_head == NULL); 249 if (so2->so_head != NULL) 250 return; 251 252 /* 253 * Drop references to old lock. A third reference (from the 254 * queue head) must be held as we still hold its lock. Bonus: 255 * we don't need to worry about garbage collecting the lock. 256 */ 257 lock = so->so_lock; 258 KASSERT(lock == uipc_lock); 259 mutex_obj_free(lock); 260 mutex_obj_free(lock); 261 262 /* 263 * Grab stream lock from the initiator and share between the two 264 * endpoints. Issue memory barrier to ensure all modifications 265 * become globally visible before the lock change. so2 is 266 * assumed not to have a stream lock, because it was created 267 * purely for the server side to accept this connection and 268 * started out life using the domain-wide lock. 269 */ 270 unp = sotounpcb(so); 271 KASSERT(unp->unp_streamlock != NULL); 272 KASSERT(sotounpcb(so2)->unp_streamlock == NULL); 273 lock = unp->unp_streamlock; 274 unp->unp_streamlock = NULL; 275 mutex_obj_hold(lock); 276 membar_exit(); 277 /* 278 * possible race if lock is not held - see comment in 279 * uipc_usrreq(PRU_ACCEPT). 280 */ 281 KASSERT(mutex_owned(lock)); 282 solockreset(so, lock); 283 solockreset(so2, lock); 284 } 285 286 /* 287 * Reset a socket's lock back to the domain-wide lock. 288 */ 289 static void 290 unp_resetlock(struct socket *so) 291 { 292 kmutex_t *olock, *nlock; 293 struct unpcb *unp; 294 295 KASSERT(solocked(so)); 296 297 olock = so->so_lock; 298 nlock = uipc_lock; 299 if (olock == nlock) 300 return; 301 unp = sotounpcb(so); 302 KASSERT(unp->unp_streamlock == NULL); 303 unp->unp_streamlock = olock; 304 mutex_obj_hold(nlock); 305 mutex_enter(nlock); 306 solockreset(so, nlock); 307 mutex_exit(olock); 308 } 309 310 static void 311 unp_free(struct unpcb *unp) 312 { 313 if (unp->unp_addr) 314 free(unp->unp_addr, M_SONAME); 315 if (unp->unp_streamlock != NULL) 316 mutex_obj_free(unp->unp_streamlock); 317 kmem_free(unp, sizeof(*unp)); 318 } 319 320 static int 321 unp_output(struct mbuf *m, struct mbuf *control, struct unpcb *unp) 322 { 323 struct socket *so2; 324 const struct sockaddr_un *sun; 325 326 /* XXX: server side closed the socket */ 327 if (unp->unp_conn == NULL) 328 return ECONNREFUSED; 329 so2 = unp->unp_conn->unp_socket; 330 331 KASSERT(solocked(so2)); 332 333 if (unp->unp_addr) 334 sun = unp->unp_addr; 335 else 336 sun = &sun_noname; 337 if (unp->unp_conn->unp_flags & UNP_WANTCRED) 338 control = unp_addsockcred(curlwp, control); 339 #ifdef COMPAT_SOCKCRED70 340 if (unp->unp_conn->unp_flags & UNP_OWANTCRED) 341 control = compat_70_unp_addsockcred(curlwp, control); 342 #endif 343 if (sbappendaddr(&so2->so_rcv, (const struct sockaddr *)sun, m, 344 control) == 0) { 345 unp_dispose(control); 346 m_freem(control); 347 m_freem(m); 348 soroverflow(so2); 349 return (ENOBUFS); 350 } else { 351 sorwakeup(so2); 352 return (0); 353 } 354 } 355 356 static void 357 unp_setaddr(struct socket *so, struct sockaddr *nam, bool peeraddr) 358 { 359 const struct sockaddr_un *sun = NULL; 360 struct unpcb *unp; 361 362 KASSERT(solocked(so)); 363 unp = sotounpcb(so); 364 365 if (peeraddr) { 366 if (unp->unp_conn && unp->unp_conn->unp_addr) 367 sun = unp->unp_conn->unp_addr; 368 } else { 369 if (unp->unp_addr) 370 sun = unp->unp_addr; 371 } 372 if (sun == NULL) 373 sun = &sun_noname; 374 375 memcpy(nam, sun, sun->sun_len); 376 } 377 378 static int 379 unp_rcvd(struct socket *so, int flags, struct lwp *l) 380 { 381 struct unpcb *unp = sotounpcb(so); 382 struct socket *so2; 383 u_int newhiwat; 384 385 KASSERT(solocked(so)); 386 KASSERT(unp != NULL); 387 388 switch (so->so_type) { 389 390 case SOCK_DGRAM: 391 panic("uipc 1"); 392 /*NOTREACHED*/ 393 394 case SOCK_SEQPACKET: /* FALLTHROUGH */ 395 case SOCK_STREAM: 396 #define rcv (&so->so_rcv) 397 #define snd (&so2->so_snd) 398 if (unp->unp_conn == 0) 399 break; 400 so2 = unp->unp_conn->unp_socket; 401 KASSERT(solocked2(so, so2)); 402 /* 403 * Adjust backpressure on sender 404 * and wakeup any waiting to write. 405 */ 406 snd->sb_mbmax += unp->unp_mbcnt - rcv->sb_mbcnt; 407 unp->unp_mbcnt = rcv->sb_mbcnt; 408 newhiwat = snd->sb_hiwat + unp->unp_cc - rcv->sb_cc; 409 (void)chgsbsize(so2->so_uidinfo, 410 &snd->sb_hiwat, newhiwat, RLIM_INFINITY); 411 unp->unp_cc = rcv->sb_cc; 412 sowwakeup(so2); 413 #undef snd 414 #undef rcv 415 break; 416 417 default: 418 panic("uipc 2"); 419 } 420 421 return 0; 422 } 423 424 static int 425 unp_recvoob(struct socket *so, struct mbuf *m, int flags) 426 { 427 KASSERT(solocked(so)); 428 429 return EOPNOTSUPP; 430 } 431 432 static int 433 unp_send(struct socket *so, struct mbuf *m, struct sockaddr *nam, 434 struct mbuf *control, struct lwp *l) 435 { 436 struct unpcb *unp = sotounpcb(so); 437 int error = 0; 438 u_int newhiwat; 439 struct socket *so2; 440 441 KASSERT(solocked(so)); 442 KASSERT(unp != NULL); 443 KASSERT(m != NULL); 444 445 /* 446 * Note: unp_internalize() rejects any control message 447 * other than SCM_RIGHTS, and only allows one. This 448 * has the side-effect of preventing a caller from 449 * forging SCM_CREDS. 450 */ 451 if (control) { 452 sounlock(so); 453 error = unp_internalize(&control); 454 solock(so); 455 if (error != 0) { 456 m_freem(control); 457 m_freem(m); 458 return error; 459 } 460 } 461 462 switch (so->so_type) { 463 464 case SOCK_DGRAM: { 465 KASSERT(so->so_lock == uipc_lock); 466 if (nam) { 467 if ((so->so_state & SS_ISCONNECTED) != 0) 468 error = EISCONN; 469 else { 470 /* 471 * Note: once connected, the 472 * socket's lock must not be 473 * dropped until we have sent 474 * the message and disconnected. 475 * This is necessary to prevent 476 * intervening control ops, like 477 * another connection. 478 */ 479 error = unp_connect(so, nam, l); 480 } 481 } else { 482 if ((so->so_state & SS_ISCONNECTED) == 0) 483 error = ENOTCONN; 484 } 485 if (error) { 486 unp_dispose(control); 487 m_freem(control); 488 m_freem(m); 489 return error; 490 } 491 error = unp_output(m, control, unp); 492 if (nam) 493 unp_disconnect1(unp); 494 break; 495 } 496 497 case SOCK_SEQPACKET: /* FALLTHROUGH */ 498 case SOCK_STREAM: 499 #define rcv (&so2->so_rcv) 500 #define snd (&so->so_snd) 501 if (unp->unp_conn == NULL) { 502 error = ENOTCONN; 503 break; 504 } 505 so2 = unp->unp_conn->unp_socket; 506 KASSERT(solocked2(so, so2)); 507 if (unp->unp_conn->unp_flags & UNP_WANTCRED) { 508 /* 509 * Credentials are passed only once on 510 * SOCK_STREAM and SOCK_SEQPACKET. 511 */ 512 unp->unp_conn->unp_flags &= ~UNP_WANTCRED; 513 control = unp_addsockcred(l, control); 514 } 515 #ifdef COMPAT_SOCKCRED70 516 if (unp->unp_conn->unp_flags & UNP_OWANTCRED) { 517 /* 518 * Credentials are passed only once on 519 * SOCK_STREAM and SOCK_SEQPACKET. 520 */ 521 unp->unp_conn->unp_flags &= ~UNP_OWANTCRED; 522 control = compat_70_unp_addsockcred(l, control); 523 } 524 #endif 525 /* 526 * Send to paired receive port, and then reduce 527 * send buffer hiwater marks to maintain backpressure. 528 * Wake up readers. 529 */ 530 if (control) { 531 if (sbappendcontrol(rcv, m, control) != 0) 532 control = NULL; 533 } else { 534 switch(so->so_type) { 535 case SOCK_SEQPACKET: 536 sbappendrecord(rcv, m); 537 break; 538 case SOCK_STREAM: 539 sbappend(rcv, m); 540 break; 541 default: 542 panic("uipc_usrreq"); 543 break; 544 } 545 } 546 snd->sb_mbmax -= 547 rcv->sb_mbcnt - unp->unp_conn->unp_mbcnt; 548 unp->unp_conn->unp_mbcnt = rcv->sb_mbcnt; 549 newhiwat = snd->sb_hiwat - 550 (rcv->sb_cc - unp->unp_conn->unp_cc); 551 (void)chgsbsize(so->so_uidinfo, 552 &snd->sb_hiwat, newhiwat, RLIM_INFINITY); 553 unp->unp_conn->unp_cc = rcv->sb_cc; 554 sorwakeup(so2); 555 #undef snd 556 #undef rcv 557 if (control != NULL) { 558 unp_dispose(control); 559 m_freem(control); 560 } 561 break; 562 563 default: 564 panic("uipc 4"); 565 } 566 567 return error; 568 } 569 570 static int 571 unp_sendoob(struct socket *so, struct mbuf *m, struct mbuf * control) 572 { 573 KASSERT(solocked(so)); 574 575 m_freem(m); 576 m_freem(control); 577 578 return EOPNOTSUPP; 579 } 580 581 /* 582 * Unix domain socket option processing. 583 */ 584 int 585 uipc_ctloutput(int op, struct socket *so, struct sockopt *sopt) 586 { 587 struct unpcb *unp = sotounpcb(so); 588 int optval = 0, error = 0; 589 590 KASSERT(solocked(so)); 591 592 if (sopt->sopt_level != 0) { 593 error = ENOPROTOOPT; 594 } else switch (op) { 595 596 case PRCO_SETOPT: 597 switch (sopt->sopt_name) { 598 case LOCAL_CREDS: 599 case LOCAL_CONNWAIT: 600 #ifdef COMPAT_SOCKCRED70 601 case LOCAL_OCREDS: 602 #endif 603 error = sockopt_getint(sopt, &optval); 604 if (error) 605 break; 606 switch (sopt->sopt_name) { 607 #define OPTSET(bit) \ 608 if (optval) \ 609 unp->unp_flags |= (bit); \ 610 else \ 611 unp->unp_flags &= ~(bit); 612 613 case LOCAL_CREDS: 614 OPTSET(UNP_WANTCRED); 615 break; 616 case LOCAL_CONNWAIT: 617 OPTSET(UNP_CONNWAIT); 618 break; 619 #ifdef COMPAT_SOCKCRED70 620 case LOCAL_OCREDS: 621 OPTSET(UNP_OWANTCRED); 622 break; 623 #endif 624 } 625 break; 626 #undef OPTSET 627 628 default: 629 error = ENOPROTOOPT; 630 break; 631 } 632 break; 633 634 case PRCO_GETOPT: 635 sounlock(so); 636 switch (sopt->sopt_name) { 637 case LOCAL_PEEREID: 638 if (unp->unp_flags & UNP_EIDSVALID) { 639 error = sockopt_set(sopt, &unp->unp_connid, 640 sizeof(unp->unp_connid)); 641 } else { 642 error = EINVAL; 643 } 644 break; 645 case LOCAL_CREDS: 646 #define OPTBIT(bit) (unp->unp_flags & (bit) ? 1 : 0) 647 648 optval = OPTBIT(UNP_WANTCRED); 649 error = sockopt_setint(sopt, optval); 650 break; 651 #ifdef COMPAT_SOCKCRED70 652 case LOCAL_OCREDS: 653 optval = OPTBIT(UNP_OWANTCRED); 654 error = sockopt_setint(sopt, optval); 655 break; 656 #endif 657 #undef OPTBIT 658 659 default: 660 error = ENOPROTOOPT; 661 break; 662 } 663 solock(so); 664 break; 665 } 666 return (error); 667 } 668 669 /* 670 * Both send and receive buffers are allocated PIPSIZ bytes of buffering 671 * for stream sockets, although the total for sender and receiver is 672 * actually only PIPSIZ. 673 * Datagram sockets really use the sendspace as the maximum datagram size, 674 * and don't really want to reserve the sendspace. Their recvspace should 675 * be large enough for at least one max-size datagram plus address. 676 */ 677 #define PIPSIZ 4096 678 u_long unpst_sendspace = PIPSIZ; 679 u_long unpst_recvspace = PIPSIZ; 680 u_long unpdg_sendspace = 2*1024; /* really max datagram size */ 681 u_long unpdg_recvspace = 4*1024; 682 683 u_int unp_rights; /* files in flight */ 684 u_int unp_rights_ratio = 2; /* limit, fraction of maxfiles */ 685 686 static int 687 unp_attach(struct socket *so, int proto) 688 { 689 struct unpcb *unp = sotounpcb(so); 690 u_long sndspc, rcvspc; 691 int error; 692 693 KASSERT(unp == NULL); 694 695 switch (so->so_type) { 696 case SOCK_SEQPACKET: 697 /* FALLTHROUGH */ 698 case SOCK_STREAM: 699 if (so->so_lock == NULL) { 700 so->so_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE); 701 solock(so); 702 } 703 sndspc = unpst_sendspace; 704 rcvspc = unpst_recvspace; 705 break; 706 707 case SOCK_DGRAM: 708 if (so->so_lock == NULL) { 709 mutex_obj_hold(uipc_lock); 710 so->so_lock = uipc_lock; 711 solock(so); 712 } 713 sndspc = unpdg_sendspace; 714 rcvspc = unpdg_recvspace; 715 break; 716 717 default: 718 panic("unp_attach"); 719 } 720 721 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 722 error = soreserve(so, sndspc, rcvspc); 723 if (error) { 724 return error; 725 } 726 } 727 728 unp = kmem_zalloc(sizeof(*unp), KM_SLEEP); 729 nanotime(&unp->unp_ctime); 730 unp->unp_socket = so; 731 so->so_pcb = unp; 732 733 KASSERT(solocked(so)); 734 return 0; 735 } 736 737 static void 738 unp_detach(struct socket *so) 739 { 740 struct unpcb *unp; 741 vnode_t *vp; 742 743 unp = sotounpcb(so); 744 KASSERT(unp != NULL); 745 KASSERT(solocked(so)); 746 retry: 747 if ((vp = unp->unp_vnode) != NULL) { 748 sounlock(so); 749 /* Acquire v_interlock to protect against unp_connect(). */ 750 /* XXXAD racy */ 751 mutex_enter(vp->v_interlock); 752 vp->v_socket = NULL; 753 mutex_exit(vp->v_interlock); 754 vrele(vp); 755 solock(so); 756 unp->unp_vnode = NULL; 757 } 758 if (unp->unp_conn) 759 unp_disconnect1(unp); 760 while (unp->unp_refs) { 761 KASSERT(solocked2(so, unp->unp_refs->unp_socket)); 762 if (unp_drop(unp->unp_refs, ECONNRESET)) { 763 solock(so); 764 goto retry; 765 } 766 } 767 soisdisconnected(so); 768 so->so_pcb = NULL; 769 if (unp_rights) { 770 /* 771 * Normally the receive buffer is flushed later, in sofree, 772 * but if our receive buffer holds references to files that 773 * are now garbage, we will enqueue those file references to 774 * the garbage collector and kick it into action. 775 */ 776 sorflush(so); 777 unp_free(unp); 778 unp_thread_kick(); 779 } else 780 unp_free(unp); 781 } 782 783 static int 784 unp_accept(struct socket *so, struct sockaddr *nam) 785 { 786 struct unpcb *unp = sotounpcb(so); 787 struct socket *so2; 788 789 KASSERT(solocked(so)); 790 KASSERT(nam != NULL); 791 792 /* XXX code review required to determine if unp can ever be NULL */ 793 if (unp == NULL) 794 return EINVAL; 795 796 KASSERT(so->so_lock == uipc_lock); 797 /* 798 * Mark the initiating STREAM socket as connected *ONLY* 799 * after it's been accepted. This prevents a client from 800 * overrunning a server and receiving ECONNREFUSED. 801 */ 802 if (unp->unp_conn == NULL) { 803 /* 804 * This will use the empty socket and will not 805 * allocate. 806 */ 807 unp_setaddr(so, nam, true); 808 return 0; 809 } 810 so2 = unp->unp_conn->unp_socket; 811 if (so2->so_state & SS_ISCONNECTING) { 812 KASSERT(solocked2(so, so->so_head)); 813 KASSERT(solocked2(so2, so->so_head)); 814 soisconnected(so2); 815 } 816 /* 817 * If the connection is fully established, break the 818 * association with uipc_lock and give the connected 819 * pair a separate lock to share. 820 * There is a race here: sotounpcb(so2)->unp_streamlock 821 * is not locked, so when changing so2->so_lock 822 * another thread can grab it while so->so_lock is still 823 * pointing to the (locked) uipc_lock. 824 * this should be harmless, except that this makes 825 * solocked2() and solocked() unreliable. 826 * Another problem is that unp_setaddr() expects the 827 * the socket locked. Grabing sotounpcb(so2)->unp_streamlock 828 * fixes both issues. 829 */ 830 mutex_enter(sotounpcb(so2)->unp_streamlock); 831 unp_setpeerlocks(so2, so); 832 /* 833 * Only now return peer's address, as we may need to 834 * block in order to allocate memory. 835 * 836 * XXX Minor race: connection can be broken while 837 * lock is dropped in unp_setaddr(). We will return 838 * error == 0 and sun_noname as the peer address. 839 */ 840 unp_setaddr(so, nam, true); 841 /* so_lock now points to unp_streamlock */ 842 mutex_exit(so2->so_lock); 843 return 0; 844 } 845 846 static int 847 unp_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp) 848 { 849 return EOPNOTSUPP; 850 } 851 852 static int 853 unp_stat(struct socket *so, struct stat *ub) 854 { 855 struct unpcb *unp; 856 struct socket *so2; 857 858 KASSERT(solocked(so)); 859 860 unp = sotounpcb(so); 861 if (unp == NULL) 862 return EINVAL; 863 864 ub->st_blksize = so->so_snd.sb_hiwat; 865 switch (so->so_type) { 866 case SOCK_SEQPACKET: /* FALLTHROUGH */ 867 case SOCK_STREAM: 868 if (unp->unp_conn == 0) 869 break; 870 871 so2 = unp->unp_conn->unp_socket; 872 KASSERT(solocked2(so, so2)); 873 ub->st_blksize += so2->so_rcv.sb_cc; 874 break; 875 default: 876 break; 877 } 878 ub->st_dev = NODEV; 879 if (unp->unp_ino == 0) 880 unp->unp_ino = unp_ino++; 881 ub->st_atimespec = ub->st_mtimespec = ub->st_ctimespec = unp->unp_ctime; 882 ub->st_ino = unp->unp_ino; 883 return (0); 884 } 885 886 static int 887 unp_peeraddr(struct socket *so, struct sockaddr *nam) 888 { 889 KASSERT(solocked(so)); 890 KASSERT(sotounpcb(so) != NULL); 891 KASSERT(nam != NULL); 892 893 unp_setaddr(so, nam, true); 894 return 0; 895 } 896 897 static int 898 unp_sockaddr(struct socket *so, struct sockaddr *nam) 899 { 900 KASSERT(solocked(so)); 901 KASSERT(sotounpcb(so) != NULL); 902 KASSERT(nam != NULL); 903 904 unp_setaddr(so, nam, false); 905 return 0; 906 } 907 908 /* 909 * we only need to perform this allocation until syscalls other than 910 * bind are adjusted to use sockaddr_big. 911 */ 912 static struct sockaddr_un * 913 makeun_sb(struct sockaddr *nam, size_t *addrlen) 914 { 915 struct sockaddr_un *sun; 916 917 *addrlen = nam->sa_len + 1; 918 sun = malloc(*addrlen, M_SONAME, M_WAITOK); 919 memcpy(sun, nam, nam->sa_len); 920 *(((char *)sun) + nam->sa_len) = '\0'; 921 return sun; 922 } 923 924 static int 925 unp_bind(struct socket *so, struct sockaddr *nam, struct lwp *l) 926 { 927 struct sockaddr_un *sun; 928 struct unpcb *unp; 929 vnode_t *vp; 930 struct vattr vattr; 931 size_t addrlen; 932 int error; 933 struct pathbuf *pb; 934 struct nameidata nd; 935 proc_t *p; 936 937 unp = sotounpcb(so); 938 939 KASSERT(solocked(so)); 940 KASSERT(unp != NULL); 941 KASSERT(nam != NULL); 942 943 if (unp->unp_vnode != NULL) 944 return (EINVAL); 945 if ((unp->unp_flags & UNP_BUSY) != 0) { 946 /* 947 * EALREADY may not be strictly accurate, but since this 948 * is a major application error it's hardly a big deal. 949 */ 950 return (EALREADY); 951 } 952 unp->unp_flags |= UNP_BUSY; 953 sounlock(so); 954 955 p = l->l_proc; 956 sun = makeun_sb(nam, &addrlen); 957 958 pb = pathbuf_create(sun->sun_path); 959 if (pb == NULL) { 960 error = ENOMEM; 961 goto bad; 962 } 963 NDINIT(&nd, CREATE, FOLLOW | LOCKPARENT | TRYEMULROOT, pb); 964 965 /* SHOULD BE ABLE TO ADOPT EXISTING AND wakeup() ALA FIFO's */ 966 if ((error = namei(&nd)) != 0) { 967 pathbuf_destroy(pb); 968 goto bad; 969 } 970 vp = nd.ni_vp; 971 if (vp != NULL) { 972 VOP_ABORTOP(nd.ni_dvp, &nd.ni_cnd); 973 if (nd.ni_dvp == vp) 974 vrele(nd.ni_dvp); 975 else 976 vput(nd.ni_dvp); 977 vrele(vp); 978 pathbuf_destroy(pb); 979 error = EADDRINUSE; 980 goto bad; 981 } 982 vattr_null(&vattr); 983 vattr.va_type = VSOCK; 984 vattr.va_mode = ACCESSPERMS & ~(p->p_cwdi->cwdi_cmask); 985 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 986 if (error) { 987 vput(nd.ni_dvp); 988 pathbuf_destroy(pb); 989 goto bad; 990 } 991 vp = nd.ni_vp; 992 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 993 solock(so); 994 vp->v_socket = unp->unp_socket; 995 unp->unp_vnode = vp; 996 unp->unp_addrlen = addrlen; 997 unp->unp_addr = sun; 998 VOP_UNLOCK(vp); 999 vput(nd.ni_dvp); 1000 unp->unp_flags &= ~UNP_BUSY; 1001 pathbuf_destroy(pb); 1002 return (0); 1003 1004 bad: 1005 free(sun, M_SONAME); 1006 solock(so); 1007 unp->unp_flags &= ~UNP_BUSY; 1008 return (error); 1009 } 1010 1011 static int 1012 unp_listen(struct socket *so, struct lwp *l) 1013 { 1014 struct unpcb *unp = sotounpcb(so); 1015 1016 KASSERT(solocked(so)); 1017 KASSERT(unp != NULL); 1018 1019 /* 1020 * If the socket can accept a connection, it must be 1021 * locked by uipc_lock. 1022 */ 1023 unp_resetlock(so); 1024 if (unp->unp_vnode == NULL) 1025 return EINVAL; 1026 1027 unp_connid(l, unp, UNP_EIDSBIND); 1028 return 0; 1029 } 1030 1031 static int 1032 unp_disconnect(struct socket *so) 1033 { 1034 KASSERT(solocked(so)); 1035 KASSERT(sotounpcb(so) != NULL); 1036 1037 unp_disconnect1(sotounpcb(so)); 1038 return 0; 1039 } 1040 1041 static int 1042 unp_shutdown(struct socket *so) 1043 { 1044 KASSERT(solocked(so)); 1045 KASSERT(sotounpcb(so) != NULL); 1046 1047 socantsendmore(so); 1048 unp_shutdown1(sotounpcb(so)); 1049 return 0; 1050 } 1051 1052 static int 1053 unp_abort(struct socket *so) 1054 { 1055 KASSERT(solocked(so)); 1056 KASSERT(sotounpcb(so) != NULL); 1057 1058 (void)unp_drop(sotounpcb(so), ECONNABORTED); 1059 KASSERT(so->so_head == NULL); 1060 KASSERT(so->so_pcb != NULL); 1061 unp_detach(so); 1062 return 0; 1063 } 1064 1065 static int 1066 unp_connect1(struct socket *so, struct socket *so2, struct lwp *l) 1067 { 1068 struct unpcb *unp = sotounpcb(so); 1069 struct unpcb *unp2; 1070 1071 if (so2->so_type != so->so_type) 1072 return EPROTOTYPE; 1073 1074 /* 1075 * All three sockets involved must be locked by same lock: 1076 * 1077 * local endpoint (so) 1078 * remote endpoint (so2) 1079 * queue head (so2->so_head, only if PR_CONNREQUIRED) 1080 */ 1081 KASSERT(solocked2(so, so2)); 1082 KASSERT(so->so_head == NULL); 1083 if (so2->so_head != NULL) { 1084 KASSERT(so2->so_lock == uipc_lock); 1085 KASSERT(solocked2(so2, so2->so_head)); 1086 } 1087 1088 unp2 = sotounpcb(so2); 1089 unp->unp_conn = unp2; 1090 1091 switch (so->so_type) { 1092 1093 case SOCK_DGRAM: 1094 unp->unp_nextref = unp2->unp_refs; 1095 unp2->unp_refs = unp; 1096 soisconnected(so); 1097 break; 1098 1099 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1100 case SOCK_STREAM: 1101 1102 /* 1103 * SOCK_SEQPACKET and SOCK_STREAM cases are handled by callers 1104 * which are unp_connect() or unp_connect2(). 1105 */ 1106 1107 break; 1108 1109 default: 1110 panic("unp_connect1"); 1111 } 1112 1113 return 0; 1114 } 1115 1116 int 1117 unp_connect(struct socket *so, struct sockaddr *nam, struct lwp *l) 1118 { 1119 struct sockaddr_un *sun; 1120 vnode_t *vp; 1121 struct socket *so2, *so3; 1122 struct unpcb *unp, *unp2, *unp3; 1123 size_t addrlen; 1124 int error; 1125 struct pathbuf *pb; 1126 struct nameidata nd; 1127 1128 unp = sotounpcb(so); 1129 if ((unp->unp_flags & UNP_BUSY) != 0) { 1130 /* 1131 * EALREADY may not be strictly accurate, but since this 1132 * is a major application error it's hardly a big deal. 1133 */ 1134 return (EALREADY); 1135 } 1136 unp->unp_flags |= UNP_BUSY; 1137 sounlock(so); 1138 1139 sun = makeun_sb(nam, &addrlen); 1140 pb = pathbuf_create(sun->sun_path); 1141 if (pb == NULL) { 1142 error = ENOMEM; 1143 goto bad2; 1144 } 1145 1146 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | TRYEMULROOT, pb); 1147 1148 if ((error = namei(&nd)) != 0) { 1149 pathbuf_destroy(pb); 1150 goto bad2; 1151 } 1152 vp = nd.ni_vp; 1153 pathbuf_destroy(pb); 1154 if (vp->v_type != VSOCK) { 1155 error = ENOTSOCK; 1156 goto bad; 1157 } 1158 if ((error = VOP_ACCESS(vp, VWRITE, l->l_cred)) != 0) 1159 goto bad; 1160 /* Acquire v_interlock to protect against unp_detach(). */ 1161 mutex_enter(vp->v_interlock); 1162 so2 = vp->v_socket; 1163 if (so2 == NULL) { 1164 mutex_exit(vp->v_interlock); 1165 error = ECONNREFUSED; 1166 goto bad; 1167 } 1168 if (so->so_type != so2->so_type) { 1169 mutex_exit(vp->v_interlock); 1170 error = EPROTOTYPE; 1171 goto bad; 1172 } 1173 solock(so); 1174 unp_resetlock(so); 1175 mutex_exit(vp->v_interlock); 1176 if ((so->so_proto->pr_flags & PR_CONNREQUIRED) != 0) { 1177 /* 1178 * This may seem somewhat fragile but is OK: if we can 1179 * see SO_ACCEPTCONN set on the endpoint, then it must 1180 * be locked by the domain-wide uipc_lock. 1181 */ 1182 KASSERT((so2->so_options & SO_ACCEPTCONN) == 0 || 1183 so2->so_lock == uipc_lock); 1184 if ((so2->so_options & SO_ACCEPTCONN) == 0 || 1185 (so3 = sonewconn(so2, false)) == NULL) { 1186 error = ECONNREFUSED; 1187 sounlock(so); 1188 goto bad; 1189 } 1190 unp2 = sotounpcb(so2); 1191 unp3 = sotounpcb(so3); 1192 if (unp2->unp_addr) { 1193 unp3->unp_addr = malloc(unp2->unp_addrlen, 1194 M_SONAME, M_WAITOK); 1195 memcpy(unp3->unp_addr, unp2->unp_addr, 1196 unp2->unp_addrlen); 1197 unp3->unp_addrlen = unp2->unp_addrlen; 1198 } 1199 unp3->unp_flags = unp2->unp_flags; 1200 so2 = so3; 1201 /* 1202 * The connector's (client's) credentials are copied from its 1203 * process structure at the time of connect() (which is now). 1204 */ 1205 unp_connid(l, unp3, UNP_EIDSVALID); 1206 /* 1207 * The receiver's (server's) credentials are copied from the 1208 * unp_peercred member of socket on which the former called 1209 * listen(); unp_listen() cached that process's credentials 1210 * at that time so we can use them now. 1211 */ 1212 if (unp2->unp_flags & UNP_EIDSBIND) { 1213 memcpy(&unp->unp_connid, &unp2->unp_connid, 1214 sizeof(unp->unp_connid)); 1215 unp->unp_flags |= UNP_EIDSVALID; 1216 } 1217 } 1218 error = unp_connect1(so, so2, l); 1219 if (error) { 1220 sounlock(so); 1221 goto bad; 1222 } 1223 unp2 = sotounpcb(so2); 1224 switch (so->so_type) { 1225 1226 /* 1227 * SOCK_DGRAM and default cases are handled in prior call to 1228 * unp_connect1(), do not add a default case without fixing 1229 * unp_connect1(). 1230 */ 1231 1232 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1233 case SOCK_STREAM: 1234 unp2->unp_conn = unp; 1235 if ((unp->unp_flags | unp2->unp_flags) & UNP_CONNWAIT) 1236 soisconnecting(so); 1237 else 1238 soisconnected(so); 1239 soisconnected(so2); 1240 /* 1241 * If the connection is fully established, break the 1242 * association with uipc_lock and give the connected 1243 * pair a seperate lock to share. 1244 */ 1245 KASSERT(so2->so_head != NULL); 1246 unp_setpeerlocks(so, so2); 1247 break; 1248 1249 } 1250 sounlock(so); 1251 bad: 1252 vput(vp); 1253 bad2: 1254 free(sun, M_SONAME); 1255 solock(so); 1256 unp->unp_flags &= ~UNP_BUSY; 1257 return (error); 1258 } 1259 1260 int 1261 unp_connect2(struct socket *so, struct socket *so2) 1262 { 1263 struct unpcb *unp = sotounpcb(so); 1264 struct unpcb *unp2; 1265 int error = 0; 1266 1267 KASSERT(solocked2(so, so2)); 1268 1269 error = unp_connect1(so, so2, curlwp); 1270 if (error) 1271 return error; 1272 1273 unp2 = sotounpcb(so2); 1274 switch (so->so_type) { 1275 1276 /* 1277 * SOCK_DGRAM and default cases are handled in prior call to 1278 * unp_connect1(), do not add a default case without fixing 1279 * unp_connect1(). 1280 */ 1281 1282 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1283 case SOCK_STREAM: 1284 unp2->unp_conn = unp; 1285 soisconnected(so); 1286 soisconnected(so2); 1287 break; 1288 1289 } 1290 return error; 1291 } 1292 1293 static void 1294 unp_disconnect1(struct unpcb *unp) 1295 { 1296 struct unpcb *unp2 = unp->unp_conn; 1297 struct socket *so; 1298 1299 if (unp2 == 0) 1300 return; 1301 unp->unp_conn = 0; 1302 so = unp->unp_socket; 1303 switch (so->so_type) { 1304 case SOCK_DGRAM: 1305 if (unp2->unp_refs == unp) 1306 unp2->unp_refs = unp->unp_nextref; 1307 else { 1308 unp2 = unp2->unp_refs; 1309 for (;;) { 1310 KASSERT(solocked2(so, unp2->unp_socket)); 1311 if (unp2 == 0) 1312 panic("unp_disconnect1"); 1313 if (unp2->unp_nextref == unp) 1314 break; 1315 unp2 = unp2->unp_nextref; 1316 } 1317 unp2->unp_nextref = unp->unp_nextref; 1318 } 1319 unp->unp_nextref = 0; 1320 so->so_state &= ~SS_ISCONNECTED; 1321 break; 1322 1323 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1324 case SOCK_STREAM: 1325 KASSERT(solocked2(so, unp2->unp_socket)); 1326 soisdisconnected(so); 1327 unp2->unp_conn = 0; 1328 soisdisconnected(unp2->unp_socket); 1329 break; 1330 } 1331 } 1332 1333 static void 1334 unp_shutdown1(struct unpcb *unp) 1335 { 1336 struct socket *so; 1337 1338 switch(unp->unp_socket->so_type) { 1339 case SOCK_SEQPACKET: /* FALLTHROUGH */ 1340 case SOCK_STREAM: 1341 if (unp->unp_conn && (so = unp->unp_conn->unp_socket)) 1342 socantrcvmore(so); 1343 break; 1344 default: 1345 break; 1346 } 1347 } 1348 1349 static bool 1350 unp_drop(struct unpcb *unp, int errno) 1351 { 1352 struct socket *so = unp->unp_socket; 1353 1354 KASSERT(solocked(so)); 1355 1356 so->so_error = errno; 1357 unp_disconnect1(unp); 1358 if (so->so_head) { 1359 so->so_pcb = NULL; 1360 /* sofree() drops the socket lock */ 1361 sofree(so); 1362 unp_free(unp); 1363 return true; 1364 } 1365 return false; 1366 } 1367 1368 #ifdef notdef 1369 unp_drain(void) 1370 { 1371 1372 } 1373 #endif 1374 1375 int 1376 unp_externalize(struct mbuf *rights, struct lwp *l, int flags) 1377 { 1378 struct cmsghdr * const cm = mtod(rights, struct cmsghdr *); 1379 struct proc * const p = l->l_proc; 1380 file_t **rp; 1381 int error = 0; 1382 1383 const size_t nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / 1384 sizeof(file_t *); 1385 if (nfds == 0) 1386 goto noop; 1387 1388 int * const fdp = kmem_alloc(nfds * sizeof(int), KM_SLEEP); 1389 rw_enter(&p->p_cwdi->cwdi_lock, RW_READER); 1390 1391 /* Make sure the recipient should be able to see the files.. */ 1392 rp = (file_t **)CMSG_DATA(cm); 1393 for (size_t i = 0; i < nfds; i++) { 1394 file_t * const fp = *rp++; 1395 if (fp == NULL) { 1396 error = EINVAL; 1397 goto out; 1398 } 1399 /* 1400 * If we are in a chroot'ed directory, and 1401 * someone wants to pass us a directory, make 1402 * sure it's inside the subtree we're allowed 1403 * to access. 1404 */ 1405 if (p->p_cwdi->cwdi_rdir != NULL && fp->f_type == DTYPE_VNODE) { 1406 vnode_t *vp = fp->f_vnode; 1407 if ((vp->v_type == VDIR) && 1408 !vn_isunder(vp, p->p_cwdi->cwdi_rdir, l)) { 1409 error = EPERM; 1410 goto out; 1411 } 1412 } 1413 } 1414 1415 restart: 1416 /* 1417 * First loop -- allocate file descriptor table slots for the 1418 * new files. 1419 */ 1420 for (size_t i = 0; i < nfds; i++) { 1421 if ((error = fd_alloc(p, 0, &fdp[i])) != 0) { 1422 /* 1423 * Back out what we've done so far. 1424 */ 1425 while (i-- > 0) { 1426 fd_abort(p, NULL, fdp[i]); 1427 } 1428 if (error == ENOSPC) { 1429 fd_tryexpand(p); 1430 error = 0; 1431 goto restart; 1432 } 1433 /* 1434 * This is the error that has historically 1435 * been returned, and some callers may 1436 * expect it. 1437 */ 1438 error = EMSGSIZE; 1439 goto out; 1440 } 1441 } 1442 1443 /* 1444 * Now that adding them has succeeded, update all of the 1445 * file passing state and affix the descriptors. 1446 */ 1447 rp = (file_t **)CMSG_DATA(cm); 1448 int *ofdp = (int *)CMSG_DATA(cm); 1449 for (size_t i = 0; i < nfds; i++) { 1450 file_t * const fp = *rp++; 1451 const int fd = fdp[i]; 1452 atomic_dec_uint(&unp_rights); 1453 fd_set_exclose(l, fd, (flags & O_CLOEXEC) != 0); 1454 fd_affix(p, fp, fd); 1455 /* 1456 * Done with this file pointer, replace it with a fd; 1457 */ 1458 *ofdp++ = fd; 1459 mutex_enter(&fp->f_lock); 1460 fp->f_msgcount--; 1461 mutex_exit(&fp->f_lock); 1462 /* 1463 * Note that fd_affix() adds a reference to the file. 1464 * The file may already have been closed by another 1465 * LWP in the process, so we must drop the reference 1466 * added by unp_internalize() with closef(). 1467 */ 1468 closef(fp); 1469 } 1470 1471 /* 1472 * Adjust length, in case of transition from large file_t 1473 * pointers to ints. 1474 */ 1475 if (sizeof(file_t *) != sizeof(int)) { 1476 cm->cmsg_len = CMSG_LEN(nfds * sizeof(int)); 1477 rights->m_len = CMSG_SPACE(nfds * sizeof(int)); 1478 } 1479 out: 1480 if (__predict_false(error != 0)) { 1481 file_t **const fpp = (file_t **)CMSG_DATA(cm); 1482 for (size_t i = 0; i < nfds; i++) 1483 unp_discard_now(fpp[i]); 1484 /* 1485 * Truncate the array so that nobody will try to interpret 1486 * what is now garbage in it. 1487 */ 1488 cm->cmsg_len = CMSG_LEN(0); 1489 rights->m_len = CMSG_SPACE(0); 1490 } 1491 rw_exit(&p->p_cwdi->cwdi_lock); 1492 kmem_free(fdp, nfds * sizeof(int)); 1493 1494 noop: 1495 /* 1496 * Don't disclose kernel memory in the alignment space. 1497 */ 1498 KASSERT(cm->cmsg_len <= rights->m_len); 1499 memset(&mtod(rights, char *)[cm->cmsg_len], 0, rights->m_len - 1500 cm->cmsg_len); 1501 return error; 1502 } 1503 1504 static int 1505 unp_internalize(struct mbuf **controlp) 1506 { 1507 filedesc_t *fdescp = curlwp->l_fd; 1508 struct mbuf *control = *controlp; 1509 struct cmsghdr *newcm, *cm = mtod(control, struct cmsghdr *); 1510 file_t **rp, **files; 1511 file_t *fp; 1512 int i, fd, *fdp; 1513 int nfds, error; 1514 u_int maxmsg; 1515 1516 error = 0; 1517 newcm = NULL; 1518 1519 /* Sanity check the control message header. */ 1520 if (cm->cmsg_type != SCM_RIGHTS || cm->cmsg_level != SOL_SOCKET || 1521 cm->cmsg_len > control->m_len || 1522 cm->cmsg_len < CMSG_ALIGN(sizeof(*cm))) 1523 return (EINVAL); 1524 1525 /* 1526 * Verify that the file descriptors are valid, and acquire 1527 * a reference to each. 1528 */ 1529 nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int); 1530 fdp = (int *)CMSG_DATA(cm); 1531 maxmsg = maxfiles / unp_rights_ratio; 1532 for (i = 0; i < nfds; i++) { 1533 fd = *fdp++; 1534 if (atomic_inc_uint_nv(&unp_rights) > maxmsg) { 1535 atomic_dec_uint(&unp_rights); 1536 nfds = i; 1537 error = EAGAIN; 1538 goto out; 1539 } 1540 if ((fp = fd_getfile(fd)) == NULL 1541 || fp->f_type == DTYPE_KQUEUE) { 1542 if (fp) 1543 fd_putfile(fd); 1544 atomic_dec_uint(&unp_rights); 1545 nfds = i; 1546 error = EBADF; 1547 goto out; 1548 } 1549 } 1550 1551 /* Allocate new space and copy header into it. */ 1552 newcm = malloc(CMSG_SPACE(nfds * sizeof(file_t *)), M_MBUF, M_WAITOK); 1553 if (newcm == NULL) { 1554 error = E2BIG; 1555 goto out; 1556 } 1557 memcpy(newcm, cm, sizeof(struct cmsghdr)); 1558 files = (file_t **)CMSG_DATA(newcm); 1559 1560 /* 1561 * Transform the file descriptors into file_t pointers, in 1562 * reverse order so that if pointers are bigger than ints, the 1563 * int won't get until we're done. No need to lock, as we have 1564 * already validated the descriptors with fd_getfile(). 1565 */ 1566 fdp = (int *)CMSG_DATA(cm) + nfds; 1567 rp = files + nfds; 1568 for (i = 0; i < nfds; i++) { 1569 fp = fdescp->fd_dt->dt_ff[*--fdp]->ff_file; 1570 KASSERT(fp != NULL); 1571 mutex_enter(&fp->f_lock); 1572 *--rp = fp; 1573 fp->f_count++; 1574 fp->f_msgcount++; 1575 mutex_exit(&fp->f_lock); 1576 } 1577 1578 out: 1579 /* Release descriptor references. */ 1580 fdp = (int *)CMSG_DATA(cm); 1581 for (i = 0; i < nfds; i++) { 1582 fd_putfile(*fdp++); 1583 if (error != 0) { 1584 atomic_dec_uint(&unp_rights); 1585 } 1586 } 1587 1588 if (error == 0) { 1589 if (control->m_flags & M_EXT) { 1590 m_freem(control); 1591 *controlp = control = m_get(M_WAIT, MT_CONTROL); 1592 } 1593 MEXTADD(control, newcm, CMSG_SPACE(nfds * sizeof(file_t *)), 1594 M_MBUF, NULL, NULL); 1595 cm = newcm; 1596 /* 1597 * Adjust message & mbuf to note amount of space 1598 * actually used. 1599 */ 1600 cm->cmsg_len = CMSG_LEN(nfds * sizeof(file_t *)); 1601 control->m_len = CMSG_SPACE(nfds * sizeof(file_t *)); 1602 } 1603 1604 return error; 1605 } 1606 1607 struct mbuf * 1608 unp_addsockcred(struct lwp *l, struct mbuf *control) 1609 { 1610 struct sockcred *sc; 1611 struct mbuf *m; 1612 void *p; 1613 1614 m = sbcreatecontrol1(&p, SOCKCREDSIZE(kauth_cred_ngroups(l->l_cred)), 1615 SCM_CREDS, SOL_SOCKET, M_WAITOK); 1616 if (m == NULL) 1617 return control; 1618 1619 sc = p; 1620 sc->sc_pid = l->l_proc->p_pid; 1621 sc->sc_uid = kauth_cred_getuid(l->l_cred); 1622 sc->sc_euid = kauth_cred_geteuid(l->l_cred); 1623 sc->sc_gid = kauth_cred_getgid(l->l_cred); 1624 sc->sc_egid = kauth_cred_getegid(l->l_cred); 1625 sc->sc_ngroups = kauth_cred_ngroups(l->l_cred); 1626 1627 for (int i = 0; i < sc->sc_ngroups; i++) 1628 sc->sc_groups[i] = kauth_cred_group(l->l_cred, i); 1629 1630 return m_add(control, m); 1631 } 1632 1633 /* 1634 * Do a mark-sweep GC of files in the system, to free up any which are 1635 * caught in flight to an about-to-be-closed socket. Additionally, 1636 * process deferred file closures. 1637 */ 1638 static void 1639 unp_gc(file_t *dp) 1640 { 1641 extern struct domain unixdomain; 1642 file_t *fp, *np; 1643 struct socket *so, *so1; 1644 u_int i, oflags, rflags; 1645 bool didwork; 1646 1647 KASSERT(curlwp == unp_thread_lwp); 1648 KASSERT(mutex_owned(&filelist_lock)); 1649 1650 /* 1651 * First, process deferred file closures. 1652 */ 1653 while (!SLIST_EMPTY(&unp_thread_discard)) { 1654 fp = SLIST_FIRST(&unp_thread_discard); 1655 KASSERT(fp->f_unpcount > 0); 1656 KASSERT(fp->f_count > 0); 1657 KASSERT(fp->f_msgcount > 0); 1658 KASSERT(fp->f_count >= fp->f_unpcount); 1659 KASSERT(fp->f_count >= fp->f_msgcount); 1660 KASSERT(fp->f_msgcount >= fp->f_unpcount); 1661 SLIST_REMOVE_HEAD(&unp_thread_discard, f_unplist); 1662 i = fp->f_unpcount; 1663 fp->f_unpcount = 0; 1664 mutex_exit(&filelist_lock); 1665 for (; i != 0; i--) { 1666 unp_discard_now(fp); 1667 } 1668 mutex_enter(&filelist_lock); 1669 } 1670 1671 /* 1672 * Clear mark bits. Ensure that we don't consider new files 1673 * entering the file table during this loop (they will not have 1674 * FSCAN set). 1675 */ 1676 unp_defer = 0; 1677 LIST_FOREACH(fp, &filehead, f_list) { 1678 for (oflags = fp->f_flag;; oflags = rflags) { 1679 rflags = atomic_cas_uint(&fp->f_flag, oflags, 1680 (oflags | FSCAN) & ~(FMARK|FDEFER)); 1681 if (__predict_true(oflags == rflags)) { 1682 break; 1683 } 1684 } 1685 } 1686 1687 /* 1688 * Iterate over the set of sockets, marking ones believed (based on 1689 * refcount) to be referenced from a process, and marking for rescan 1690 * sockets which are queued on a socket. Recan continues descending 1691 * and searching for sockets referenced by sockets (FDEFER), until 1692 * there are no more socket->socket references to be discovered. 1693 */ 1694 do { 1695 didwork = false; 1696 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) { 1697 KASSERT(mutex_owned(&filelist_lock)); 1698 np = LIST_NEXT(fp, f_list); 1699 mutex_enter(&fp->f_lock); 1700 if ((fp->f_flag & FDEFER) != 0) { 1701 atomic_and_uint(&fp->f_flag, ~FDEFER); 1702 unp_defer--; 1703 if (fp->f_count == 0) { 1704 /* 1705 * XXX: closef() doesn't pay attention 1706 * to FDEFER 1707 */ 1708 mutex_exit(&fp->f_lock); 1709 continue; 1710 } 1711 } else { 1712 if (fp->f_count == 0 || 1713 (fp->f_flag & FMARK) != 0 || 1714 fp->f_count == fp->f_msgcount || 1715 fp->f_unpcount != 0) { 1716 mutex_exit(&fp->f_lock); 1717 continue; 1718 } 1719 } 1720 atomic_or_uint(&fp->f_flag, FMARK); 1721 1722 if (fp->f_type != DTYPE_SOCKET || 1723 (so = fp->f_socket) == NULL || 1724 so->so_proto->pr_domain != &unixdomain || 1725 (so->so_proto->pr_flags & PR_RIGHTS) == 0) { 1726 mutex_exit(&fp->f_lock); 1727 continue; 1728 } 1729 1730 /* Gain file ref, mark our position, and unlock. */ 1731 didwork = true; 1732 LIST_INSERT_AFTER(fp, dp, f_list); 1733 fp->f_count++; 1734 mutex_exit(&fp->f_lock); 1735 mutex_exit(&filelist_lock); 1736 1737 /* 1738 * Mark files referenced from sockets queued on the 1739 * accept queue as well. 1740 */ 1741 solock(so); 1742 unp_scan(so->so_rcv.sb_mb, unp_mark, 0); 1743 if ((so->so_options & SO_ACCEPTCONN) != 0) { 1744 TAILQ_FOREACH(so1, &so->so_q0, so_qe) { 1745 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0); 1746 } 1747 TAILQ_FOREACH(so1, &so->so_q, so_qe) { 1748 unp_scan(so1->so_rcv.sb_mb, unp_mark, 0); 1749 } 1750 } 1751 sounlock(so); 1752 1753 /* Re-lock and restart from where we left off. */ 1754 closef(fp); 1755 mutex_enter(&filelist_lock); 1756 np = LIST_NEXT(dp, f_list); 1757 LIST_REMOVE(dp, f_list); 1758 } 1759 /* 1760 * Bail early if we did nothing in the loop above. Could 1761 * happen because of concurrent activity causing unp_defer 1762 * to get out of sync. 1763 */ 1764 } while (unp_defer != 0 && didwork); 1765 1766 /* 1767 * Sweep pass. 1768 * 1769 * We grab an extra reference to each of the files that are 1770 * not otherwise accessible and then free the rights that are 1771 * stored in messages on them. 1772 */ 1773 for (fp = LIST_FIRST(&filehead); fp != NULL; fp = np) { 1774 KASSERT(mutex_owned(&filelist_lock)); 1775 np = LIST_NEXT(fp, f_list); 1776 mutex_enter(&fp->f_lock); 1777 1778 /* 1779 * Ignore non-sockets. 1780 * Ignore dead sockets, or sockets with pending close. 1781 * Ignore sockets obviously referenced elsewhere. 1782 * Ignore sockets marked as referenced by our scan. 1783 * Ignore new sockets that did not exist during the scan. 1784 */ 1785 if (fp->f_type != DTYPE_SOCKET || 1786 fp->f_count == 0 || fp->f_unpcount != 0 || 1787 fp->f_count != fp->f_msgcount || 1788 (fp->f_flag & (FMARK | FSCAN)) != FSCAN) { 1789 mutex_exit(&fp->f_lock); 1790 continue; 1791 } 1792 1793 /* Gain file ref, mark our position, and unlock. */ 1794 LIST_INSERT_AFTER(fp, dp, f_list); 1795 fp->f_count++; 1796 mutex_exit(&fp->f_lock); 1797 mutex_exit(&filelist_lock); 1798 1799 /* 1800 * Flush all data from the socket's receive buffer. 1801 * This will cause files referenced only by the 1802 * socket to be queued for close. 1803 */ 1804 so = fp->f_socket; 1805 solock(so); 1806 sorflush(so); 1807 sounlock(so); 1808 1809 /* Re-lock and restart from where we left off. */ 1810 closef(fp); 1811 mutex_enter(&filelist_lock); 1812 np = LIST_NEXT(dp, f_list); 1813 LIST_REMOVE(dp, f_list); 1814 } 1815 } 1816 1817 /* 1818 * Garbage collector thread. While SCM_RIGHTS messages are in transit, 1819 * wake once per second to garbage collect. Run continually while we 1820 * have deferred closes to process. 1821 */ 1822 static void 1823 unp_thread(void *cookie) 1824 { 1825 file_t *dp; 1826 1827 /* Allocate a dummy file for our scans. */ 1828 if ((dp = fgetdummy()) == NULL) { 1829 panic("unp_thread"); 1830 } 1831 1832 mutex_enter(&filelist_lock); 1833 for (;;) { 1834 KASSERT(mutex_owned(&filelist_lock)); 1835 if (SLIST_EMPTY(&unp_thread_discard)) { 1836 if (unp_rights != 0) { 1837 (void)cv_timedwait(&unp_thread_cv, 1838 &filelist_lock, hz); 1839 } else { 1840 cv_wait(&unp_thread_cv, &filelist_lock); 1841 } 1842 } 1843 unp_gc(dp); 1844 } 1845 /* NOTREACHED */ 1846 } 1847 1848 /* 1849 * Kick the garbage collector into action if there is something for 1850 * it to process. 1851 */ 1852 static void 1853 unp_thread_kick(void) 1854 { 1855 1856 if (!SLIST_EMPTY(&unp_thread_discard) || unp_rights != 0) { 1857 mutex_enter(&filelist_lock); 1858 cv_signal(&unp_thread_cv); 1859 mutex_exit(&filelist_lock); 1860 } 1861 } 1862 1863 void 1864 unp_dispose(struct mbuf *m) 1865 { 1866 1867 if (m) 1868 unp_scan(m, unp_discard_later, 1); 1869 } 1870 1871 void 1872 unp_scan(struct mbuf *m0, void (*op)(file_t *), int discard) 1873 { 1874 struct mbuf *m; 1875 file_t **rp, *fp; 1876 struct cmsghdr *cm; 1877 int i, qfds; 1878 1879 while (m0) { 1880 for (m = m0; m; m = m->m_next) { 1881 if (m->m_type != MT_CONTROL || 1882 m->m_len < sizeof(*cm)) { 1883 continue; 1884 } 1885 cm = mtod(m, struct cmsghdr *); 1886 if (cm->cmsg_level != SOL_SOCKET || 1887 cm->cmsg_type != SCM_RIGHTS) 1888 continue; 1889 qfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) 1890 / sizeof(file_t *); 1891 rp = (file_t **)CMSG_DATA(cm); 1892 for (i = 0; i < qfds; i++) { 1893 fp = *rp; 1894 if (discard) { 1895 *rp = 0; 1896 } 1897 (*op)(fp); 1898 rp++; 1899 } 1900 } 1901 m0 = m0->m_nextpkt; 1902 } 1903 } 1904 1905 void 1906 unp_mark(file_t *fp) 1907 { 1908 1909 if (fp == NULL) 1910 return; 1911 1912 /* If we're already deferred, don't screw up the defer count */ 1913 mutex_enter(&fp->f_lock); 1914 if (fp->f_flag & (FMARK | FDEFER)) { 1915 mutex_exit(&fp->f_lock); 1916 return; 1917 } 1918 1919 /* 1920 * Minimize the number of deferrals... Sockets are the only type of 1921 * file which can hold references to another file, so just mark 1922 * other files, and defer unmarked sockets for the next pass. 1923 */ 1924 if (fp->f_type == DTYPE_SOCKET) { 1925 unp_defer++; 1926 KASSERT(fp->f_count != 0); 1927 atomic_or_uint(&fp->f_flag, FDEFER); 1928 } else { 1929 atomic_or_uint(&fp->f_flag, FMARK); 1930 } 1931 mutex_exit(&fp->f_lock); 1932 } 1933 1934 static void 1935 unp_discard_now(file_t *fp) 1936 { 1937 1938 if (fp == NULL) 1939 return; 1940 1941 KASSERT(fp->f_count > 0); 1942 KASSERT(fp->f_msgcount > 0); 1943 1944 mutex_enter(&fp->f_lock); 1945 fp->f_msgcount--; 1946 mutex_exit(&fp->f_lock); 1947 atomic_dec_uint(&unp_rights); 1948 (void)closef(fp); 1949 } 1950 1951 static void 1952 unp_discard_later(file_t *fp) 1953 { 1954 1955 if (fp == NULL) 1956 return; 1957 1958 KASSERT(fp->f_count > 0); 1959 KASSERT(fp->f_msgcount > 0); 1960 1961 mutex_enter(&filelist_lock); 1962 if (fp->f_unpcount++ == 0) { 1963 SLIST_INSERT_HEAD(&unp_thread_discard, fp, f_unplist); 1964 } 1965 mutex_exit(&filelist_lock); 1966 } 1967 1968 const struct pr_usrreqs unp_usrreqs = { 1969 .pr_attach = unp_attach, 1970 .pr_detach = unp_detach, 1971 .pr_accept = unp_accept, 1972 .pr_bind = unp_bind, 1973 .pr_listen = unp_listen, 1974 .pr_connect = unp_connect, 1975 .pr_connect2 = unp_connect2, 1976 .pr_disconnect = unp_disconnect, 1977 .pr_shutdown = unp_shutdown, 1978 .pr_abort = unp_abort, 1979 .pr_ioctl = unp_ioctl, 1980 .pr_stat = unp_stat, 1981 .pr_peeraddr = unp_peeraddr, 1982 .pr_sockaddr = unp_sockaddr, 1983 .pr_rcvd = unp_rcvd, 1984 .pr_recvoob = unp_recvoob, 1985 .pr_send = unp_send, 1986 .pr_sendoob = unp_sendoob, 1987 }; 1988