1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1989, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include "opt_capsicum.h" 38 #include "opt_ddb.h" 39 #include "opt_ktrace.h" 40 41 #define EXTERR_CATEGORY EXTERR_CAT_FILEDESC 42 #include <sys/systm.h> 43 #include <sys/capsicum.h> 44 #include <sys/conf.h> 45 #include <sys/exterrvar.h> 46 #include <sys/fcntl.h> 47 #include <sys/file.h> 48 #include <sys/filedesc.h> 49 #include <sys/filio.h> 50 #include <sys/jail.h> 51 #include <sys/kernel.h> 52 #include <sys/limits.h> 53 #include <sys/lock.h> 54 #include <sys/malloc.h> 55 #include <sys/mount.h> 56 #include <sys/mutex.h> 57 #include <sys/namei.h> 58 #include <sys/selinfo.h> 59 #include <sys/poll.h> 60 #include <sys/priv.h> 61 #include <sys/proc.h> 62 #include <sys/protosw.h> 63 #include <sys/racct.h> 64 #include <sys/resourcevar.h> 65 #include <sys/sbuf.h> 66 #include <sys/signalvar.h> 67 #include <sys/kdb.h> 68 #include <sys/smr.h> 69 #include <sys/stat.h> 70 #include <sys/sx.h> 71 #include <sys/syscallsubr.h> 72 #include <sys/sysctl.h> 73 #include <sys/sysproto.h> 74 #include <sys/unistd.h> 75 #include <sys/user.h> 76 #include <sys/vnode.h> 77 #include <sys/ktrace.h> 78 79 #include <net/vnet.h> 80 81 #include <security/audit/audit.h> 82 83 #include <vm/uma.h> 84 #include <vm/vm.h> 85 86 #include <ddb/ddb.h> 87 88 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table"); 89 static MALLOC_DEFINE(M_PWD, "pwd", "Descriptor table vnodes"); 90 static MALLOC_DEFINE(M_PWDDESC, "pwddesc", "Pwd descriptors"); 91 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader", 92 "file desc to leader structures"); 93 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures"); 94 MALLOC_DEFINE(M_FILECAPS, "filecaps", "descriptor capabilities"); 95 96 MALLOC_DECLARE(M_FADVISE); 97 98 static __read_mostly uma_zone_t file_zone; 99 static __read_mostly uma_zone_t filedesc0_zone; 100 __read_mostly uma_zone_t pwd_zone; 101 VFS_SMR_DECLARE; 102 103 static int closefp(struct filedesc *fdp, int fd, struct file *fp, 104 struct thread *td, bool holdleaders, bool audit); 105 static void export_file_to_kinfo(struct file *fp, int fd, 106 cap_rights_t *rightsp, struct kinfo_file *kif, 107 struct filedesc *fdp, int flags); 108 static int fd_first_free(struct filedesc *fdp, int low, int size); 109 static void fdgrowtable(struct filedesc *fdp, int nfd); 110 static void fdgrowtable_exp(struct filedesc *fdp, int nfd); 111 static void fdunused(struct filedesc *fdp, int fd); 112 static void fdused(struct filedesc *fdp, int fd); 113 static int fget_unlocked_seq(struct thread *td, int fd, 114 const cap_rights_t *needrightsp, uint8_t *flagsp, 115 struct file **fpp, seqc_t *seqp); 116 static int getmaxfd(struct thread *td); 117 static u_long *filecaps_copy_prep(const struct filecaps *src); 118 static void filecaps_copy_finish(const struct filecaps *src, 119 struct filecaps *dst, u_long *ioctls); 120 static u_long *filecaps_free_prep(struct filecaps *fcaps); 121 static void filecaps_free_finish(u_long *ioctls); 122 123 static struct pwd *pwd_alloc(void); 124 125 /* 126 * Each process has: 127 * 128 * - An array of open file descriptors (fd_ofiles) 129 * - An array of file flags (fd_ofileflags) 130 * - A bitmap recording which descriptors are in use (fd_map) 131 * 132 * A process starts out with NDFILE descriptors. The value of NDFILE has 133 * been selected based the historical limit of 20 open files, and an 134 * assumption that the majority of processes, especially short-lived 135 * processes like shells, will never need more. 136 * 137 * If this initial allocation is exhausted, a larger descriptor table and 138 * map are allocated dynamically, and the pointers in the process's struct 139 * filedesc are updated to point to those. This is repeated every time 140 * the process runs out of file descriptors (provided it hasn't hit its 141 * resource limit). 142 * 143 * Since threads may hold references to individual descriptor table 144 * entries, the tables are never freed. Instead, they are placed on a 145 * linked list and freed only when the struct filedesc is released. 146 */ 147 #define NDFILE 20 148 #define NDSLOTSIZE sizeof(NDSLOTTYPE) 149 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT) 150 #define NDSLOT(x) ((x) / NDENTRIES) 151 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES)) 152 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES) 153 154 #define FILEDESC_FOREACH_FDE(fdp, _iterator, _fde) \ 155 struct filedesc *_fdp = (fdp); \ 156 int _lastfile = fdlastfile_single(_fdp); \ 157 for (_iterator = 0; _iterator <= _lastfile; _iterator++) \ 158 if ((_fde = &_fdp->fd_ofiles[_iterator])->fde_file != NULL) 159 160 #define FILEDESC_FOREACH_FP(fdp, _iterator, _fp) \ 161 struct filedesc *_fdp = (fdp); \ 162 int _lastfile = fdlastfile_single(_fdp); \ 163 for (_iterator = 0; _iterator <= _lastfile; _iterator++) \ 164 if ((_fp = _fdp->fd_ofiles[_iterator].fde_file) != NULL) 165 166 /* 167 * SLIST entry used to keep track of ofiles which must be reclaimed when 168 * the process exits. 169 */ 170 struct freetable { 171 struct fdescenttbl *ft_table; 172 SLIST_ENTRY(freetable) ft_next; 173 }; 174 175 /* 176 * Initial allocation: a filedesc structure + the head of SLIST used to 177 * keep track of old ofiles + enough space for NDFILE descriptors. 178 */ 179 180 struct fdescenttbl0 { 181 int fdt_nfiles; 182 struct filedescent fdt_ofiles[NDFILE]; 183 }; 184 185 struct filedesc0 { 186 struct filedesc fd_fd; 187 SLIST_HEAD(, freetable) fd_free; 188 struct fdescenttbl0 fd_dfiles; 189 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)]; 190 }; 191 192 /* 193 * Descriptor management. 194 */ 195 static int __exclusive_cache_line openfiles; /* actual number of open files */ 196 struct mtx sigio_lock; /* mtx to protect pointers to sigio */ 197 void __read_mostly (*mq_fdclose)(struct thread *td, int fd, struct file *fp); 198 199 /* 200 * If low >= size, just return low. Otherwise find the first zero bit in the 201 * given bitmap, starting at low and not exceeding size - 1. Return size if 202 * not found. 203 */ 204 static int 205 fd_first_free(struct filedesc *fdp, int low, int size) 206 { 207 NDSLOTTYPE *map = fdp->fd_map; 208 NDSLOTTYPE mask; 209 int off, maxoff; 210 211 if (low >= size) 212 return (low); 213 214 off = NDSLOT(low); 215 if (low % NDENTRIES) { 216 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES))); 217 if ((mask &= ~map[off]) != 0UL) 218 return (off * NDENTRIES + ffsl(mask) - 1); 219 ++off; 220 } 221 for (maxoff = NDSLOTS(size); off < maxoff; ++off) 222 if (map[off] != ~0UL) 223 return (off * NDENTRIES + ffsl(~map[off]) - 1); 224 return (size); 225 } 226 227 /* 228 * Find the last used fd. 229 * 230 * Call this variant if fdp can't be modified by anyone else (e.g, during exec). 231 * Otherwise use fdlastfile. 232 */ 233 int 234 fdlastfile_single(struct filedesc *fdp) 235 { 236 NDSLOTTYPE *map = fdp->fd_map; 237 int off, minoff; 238 239 off = NDSLOT(fdp->fd_nfiles - 1); 240 for (minoff = NDSLOT(0); off >= minoff; --off) 241 if (map[off] != 0) 242 return (off * NDENTRIES + flsl(map[off]) - 1); 243 return (-1); 244 } 245 246 int 247 fdlastfile(struct filedesc *fdp) 248 { 249 250 FILEDESC_LOCK_ASSERT(fdp); 251 return (fdlastfile_single(fdp)); 252 } 253 254 static int 255 fdisused(struct filedesc *fdp, int fd) 256 { 257 258 KASSERT(fd >= 0 && fd < fdp->fd_nfiles, 259 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles)); 260 261 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0); 262 } 263 264 /* 265 * Mark a file descriptor as used. 266 */ 267 static void 268 fdused_init(struct filedesc *fdp, int fd) 269 { 270 271 KASSERT(!fdisused(fdp, fd), ("fd=%d is already used", fd)); 272 273 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd); 274 } 275 276 static void 277 fdused(struct filedesc *fdp, int fd) 278 { 279 280 FILEDESC_XLOCK_ASSERT(fdp); 281 282 fdused_init(fdp, fd); 283 if (fd == fdp->fd_freefile) 284 fdp->fd_freefile++; 285 } 286 287 /* 288 * Mark a file descriptor as unused. 289 */ 290 static void 291 fdunused(struct filedesc *fdp, int fd) 292 { 293 294 FILEDESC_XLOCK_ASSERT(fdp); 295 296 KASSERT(fdisused(fdp, fd), ("fd=%d is already unused", fd)); 297 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL, 298 ("fd=%d is still in use", fd)); 299 300 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd); 301 if (fd < fdp->fd_freefile) 302 fdp->fd_freefile = fd; 303 } 304 305 /* 306 * Free a file descriptor. 307 * 308 * Avoid some work if fdp is about to be destroyed. 309 */ 310 static inline void 311 fdefree_last(struct filedescent *fde) 312 { 313 314 filecaps_free(&fde->fde_caps); 315 } 316 317 static inline void 318 fdfree(struct filedesc *fdp, int fd) 319 { 320 struct filedescent *fde; 321 322 FILEDESC_XLOCK_ASSERT(fdp); 323 fde = &fdp->fd_ofiles[fd]; 324 #ifdef CAPABILITIES 325 seqc_write_begin(&fde->fde_seqc); 326 #endif 327 fde->fde_file = NULL; 328 #ifdef CAPABILITIES 329 seqc_write_end(&fde->fde_seqc); 330 #endif 331 fdefree_last(fde); 332 fdunused(fdp, fd); 333 } 334 335 /* 336 * System calls on descriptors. 337 */ 338 #ifndef _SYS_SYSPROTO_H_ 339 struct getdtablesize_args { 340 int dummy; 341 }; 342 #endif 343 /* ARGSUSED */ 344 int 345 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap) 346 { 347 #ifdef RACCT 348 uint64_t lim; 349 #endif 350 351 td->td_retval[0] = getmaxfd(td); 352 #ifdef RACCT 353 PROC_LOCK(td->td_proc); 354 lim = racct_get_limit(td->td_proc, RACCT_NOFILE); 355 PROC_UNLOCK(td->td_proc); 356 if (lim < td->td_retval[0]) 357 td->td_retval[0] = lim; 358 #endif 359 return (0); 360 } 361 362 /* 363 * Duplicate a file descriptor to a particular value. 364 * 365 * Note: keep in mind that a potential race condition exists when closing 366 * descriptors from a shared descriptor table (via rfork). 367 */ 368 #ifndef _SYS_SYSPROTO_H_ 369 struct dup2_args { 370 u_int from; 371 u_int to; 372 }; 373 #endif 374 /* ARGSUSED */ 375 int 376 sys_dup2(struct thread *td, struct dup2_args *uap) 377 { 378 379 return (kern_dup(td, FDDUP_FIXED, 0, (int)uap->from, (int)uap->to)); 380 } 381 382 /* 383 * Duplicate a file descriptor. 384 */ 385 #ifndef _SYS_SYSPROTO_H_ 386 struct dup_args { 387 u_int fd; 388 }; 389 #endif 390 /* ARGSUSED */ 391 int 392 sys_dup(struct thread *td, struct dup_args *uap) 393 { 394 395 return (kern_dup(td, FDDUP_NORMAL, 0, (int)uap->fd, 0)); 396 } 397 398 /* 399 * The file control system call. 400 */ 401 #ifndef _SYS_SYSPROTO_H_ 402 struct fcntl_args { 403 int fd; 404 int cmd; 405 long arg; 406 }; 407 #endif 408 /* ARGSUSED */ 409 int 410 sys_fcntl(struct thread *td, struct fcntl_args *uap) 411 { 412 413 return (kern_fcntl_freebsd(td, uap->fd, uap->cmd, uap->arg)); 414 } 415 416 int 417 kern_fcntl_freebsd(struct thread *td, int fd, int cmd, intptr_t arg) 418 { 419 struct flock fl; 420 struct __oflock ofl; 421 intptr_t arg1; 422 int error, newcmd; 423 424 error = 0; 425 newcmd = cmd; 426 switch (cmd) { 427 case F_OGETLK: 428 case F_OSETLK: 429 case F_OSETLKW: 430 /* 431 * Convert old flock structure to new. 432 */ 433 error = copyin((void *)arg, &ofl, sizeof(ofl)); 434 fl.l_start = ofl.l_start; 435 fl.l_len = ofl.l_len; 436 fl.l_pid = ofl.l_pid; 437 fl.l_type = ofl.l_type; 438 fl.l_whence = ofl.l_whence; 439 fl.l_sysid = 0; 440 441 switch (cmd) { 442 case F_OGETLK: 443 newcmd = F_GETLK; 444 break; 445 case F_OSETLK: 446 newcmd = F_SETLK; 447 break; 448 case F_OSETLKW: 449 newcmd = F_SETLKW; 450 break; 451 } 452 arg1 = (intptr_t)&fl; 453 break; 454 case F_GETLK: 455 case F_SETLK: 456 case F_SETLKW: 457 case F_SETLK_REMOTE: 458 error = copyin((void *)arg, &fl, sizeof(fl)); 459 arg1 = (intptr_t)&fl; 460 break; 461 default: 462 arg1 = arg; 463 break; 464 } 465 if (error) 466 return (error); 467 error = kern_fcntl(td, fd, newcmd, arg1); 468 if (error) 469 return (error); 470 if (cmd == F_OGETLK) { 471 ofl.l_start = fl.l_start; 472 ofl.l_len = fl.l_len; 473 ofl.l_pid = fl.l_pid; 474 ofl.l_type = fl.l_type; 475 ofl.l_whence = fl.l_whence; 476 error = copyout(&ofl, (void *)arg, sizeof(ofl)); 477 } else if (cmd == F_GETLK) { 478 error = copyout(&fl, (void *)arg, sizeof(fl)); 479 } 480 return (error); 481 } 482 483 int 484 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg) 485 { 486 struct filedesc *fdp; 487 struct flock *flp; 488 struct file *fp, *fp2; 489 struct filedescent *fde; 490 struct proc *p; 491 struct vnode *vp; 492 struct mount *mp; 493 struct kinfo_file *kif; 494 int error, flg, kif_sz, seals, tmp, got_set, got_cleared; 495 uint64_t bsize; 496 off_t foffset; 497 int flags; 498 499 error = 0; 500 flg = F_POSIX; 501 p = td->td_proc; 502 fdp = p->p_fd; 503 504 AUDIT_ARG_FD(cmd); 505 AUDIT_ARG_CMD(cmd); 506 switch (cmd) { 507 case F_DUPFD: 508 tmp = arg; 509 error = kern_dup(td, FDDUP_FCNTL, 0, fd, tmp); 510 break; 511 512 case F_DUPFD_CLOEXEC: 513 tmp = arg; 514 error = kern_dup(td, FDDUP_FCNTL, FDDUP_FLAG_CLOEXEC, fd, tmp); 515 break; 516 517 case F_DUPFD_CLOFORK: 518 tmp = arg; 519 error = kern_dup(td, FDDUP_FCNTL, FDDUP_FLAG_CLOFORK, fd, tmp); 520 break; 521 522 case F_DUP2FD: 523 tmp = arg; 524 error = kern_dup(td, FDDUP_FIXED, 0, fd, tmp); 525 break; 526 527 case F_DUP2FD_CLOEXEC: 528 tmp = arg; 529 error = kern_dup(td, FDDUP_FIXED, FDDUP_FLAG_CLOEXEC, fd, tmp); 530 break; 531 532 case F_GETFD: 533 error = EBADF; 534 FILEDESC_SLOCK(fdp); 535 fde = fdeget_noref(fdp, fd); 536 if (fde != NULL) { 537 td->td_retval[0] = 538 ((fde->fde_flags & UF_EXCLOSE) ? FD_CLOEXEC : 0) | 539 ((fde->fde_flags & UF_FOCLOSE) ? FD_CLOFORK : 0) | 540 ((fde->fde_flags & UF_RESOLVE_BENEATH) ? 541 FD_RESOLVE_BENEATH : 0); 542 error = 0; 543 } 544 FILEDESC_SUNLOCK(fdp); 545 break; 546 547 case F_SETFD: 548 error = EBADF; 549 FILEDESC_XLOCK(fdp); 550 fde = fdeget_noref(fdp, fd); 551 if (fde != NULL) { 552 /* 553 * UF_RESOLVE_BENEATH is sticky and cannot be cleared. 554 */ 555 fde->fde_flags = (fde->fde_flags & ~UF_EXCLOSE) | 556 ((arg & FD_CLOEXEC) != 0 ? UF_EXCLOSE : 0) | 557 ((arg & FD_CLOFORK) != 0 ? UF_FOCLOSE : 0) | 558 ((arg & FD_RESOLVE_BENEATH) != 0 ? 559 UF_RESOLVE_BENEATH : 0); 560 error = 0; 561 } 562 FILEDESC_XUNLOCK(fdp); 563 break; 564 565 case F_GETFL: 566 error = fget_fcntl(td, fd, &cap_fcntl_rights, F_GETFL, &fp); 567 if (error != 0) 568 break; 569 td->td_retval[0] = OFLAGS(fp->f_flag); 570 fdrop(fp, td); 571 break; 572 573 case F_SETFL: 574 error = fget_fcntl(td, fd, &cap_fcntl_rights, F_SETFL, &fp); 575 if (error != 0) 576 break; 577 if (fp->f_ops == &path_fileops) { 578 fdrop(fp, td); 579 error = EBADF; 580 break; 581 } 582 do { 583 tmp = flg = fp->f_flag; 584 tmp &= ~FCNTLFLAGS; 585 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS; 586 } while (atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0); 587 got_set = tmp & ~flg; 588 got_cleared = flg & ~tmp; 589 tmp = fp->f_flag & FNONBLOCK; 590 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); 591 if (error != 0) 592 goto revert_f_setfl; 593 tmp = fp->f_flag & FASYNC; 594 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td); 595 if (error == 0) { 596 fdrop(fp, td); 597 break; 598 } 599 atomic_clear_int(&fp->f_flag, FNONBLOCK); 600 tmp = 0; 601 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td); 602 revert_f_setfl: 603 do { 604 tmp = flg = fp->f_flag; 605 tmp &= ~FCNTLFLAGS; 606 tmp |= got_cleared; 607 tmp &= ~got_set; 608 } while (atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0); 609 fdrop(fp, td); 610 break; 611 612 case F_GETOWN: 613 error = fget_fcntl(td, fd, &cap_fcntl_rights, F_GETOWN, &fp); 614 if (error != 0) 615 break; 616 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td); 617 if (error == 0) 618 td->td_retval[0] = tmp; 619 fdrop(fp, td); 620 break; 621 622 case F_SETOWN: 623 error = fget_fcntl(td, fd, &cap_fcntl_rights, F_SETOWN, &fp); 624 if (error != 0) 625 break; 626 tmp = arg; 627 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td); 628 fdrop(fp, td); 629 break; 630 631 case F_SETLK_REMOTE: 632 error = priv_check(td, PRIV_NFS_LOCKD); 633 if (error != 0) 634 return (error); 635 flg = F_REMOTE; 636 goto do_setlk; 637 638 case F_SETLKW: 639 flg |= F_WAIT; 640 /* FALLTHROUGH F_SETLK */ 641 642 case F_SETLK: 643 do_setlk: 644 flp = (struct flock *)arg; 645 if ((flg & F_REMOTE) != 0 && flp->l_sysid == 0) { 646 error = EINVAL; 647 break; 648 } 649 650 error = fget_unlocked(td, fd, &cap_flock_rights, &fp); 651 if (error != 0) 652 break; 653 if (fp->f_type != DTYPE_VNODE || fp->f_ops == &path_fileops) { 654 error = EBADF; 655 fdrop(fp, td); 656 break; 657 } 658 659 if (flp->l_whence == SEEK_CUR) { 660 foffset = foffset_get(fp); 661 if (foffset < 0 || 662 (flp->l_start > 0 && 663 foffset > OFF_MAX - flp->l_start)) { 664 error = EOVERFLOW; 665 fdrop(fp, td); 666 break; 667 } 668 flp->l_start += foffset; 669 } 670 671 vp = fp->f_vnode; 672 switch (flp->l_type) { 673 case F_RDLCK: 674 if ((fp->f_flag & FREAD) == 0) { 675 error = EBADF; 676 break; 677 } 678 if ((p->p_leader->p_flag & P_ADVLOCK) == 0) { 679 PROC_LOCK(p->p_leader); 680 p->p_leader->p_flag |= P_ADVLOCK; 681 PROC_UNLOCK(p->p_leader); 682 } 683 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, 684 flp, flg); 685 break; 686 case F_WRLCK: 687 if ((fp->f_flag & FWRITE) == 0) { 688 error = EBADF; 689 break; 690 } 691 if ((p->p_leader->p_flag & P_ADVLOCK) == 0) { 692 PROC_LOCK(p->p_leader); 693 p->p_leader->p_flag |= P_ADVLOCK; 694 PROC_UNLOCK(p->p_leader); 695 } 696 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK, 697 flp, flg); 698 break; 699 case F_UNLCK: 700 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, 701 flp, flg); 702 break; 703 case F_UNLCKSYS: 704 if (flg != F_REMOTE) { 705 error = EINVAL; 706 break; 707 } 708 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, 709 F_UNLCKSYS, flp, flg); 710 break; 711 default: 712 error = EINVAL; 713 break; 714 } 715 if (error != 0 || flp->l_type == F_UNLCK || 716 flp->l_type == F_UNLCKSYS) { 717 fdrop(fp, td); 718 break; 719 } 720 721 /* 722 * Check for a race with close. 723 * 724 * The vnode is now advisory locked (or unlocked, but this case 725 * is not really important) as the caller requested. 726 * We had to drop the filedesc lock, so we need to recheck if 727 * the descriptor is still valid, because if it was closed 728 * in the meantime we need to remove advisory lock from the 729 * vnode - close on any descriptor leading to an advisory 730 * locked vnode, removes that lock. 731 * We will return 0 on purpose in that case, as the result of 732 * successful advisory lock might have been externally visible 733 * already. This is fine - effectively we pretend to the caller 734 * that the closing thread was a bit slower and that the 735 * advisory lock succeeded before the close. 736 */ 737 error = fget_unlocked(td, fd, &cap_no_rights, &fp2); 738 if (error != 0) { 739 fdrop(fp, td); 740 break; 741 } 742 if (fp != fp2) { 743 flp->l_whence = SEEK_SET; 744 flp->l_start = 0; 745 flp->l_len = 0; 746 flp->l_type = F_UNLCK; 747 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, 748 F_UNLCK, flp, F_POSIX); 749 } 750 fdrop(fp, td); 751 fdrop(fp2, td); 752 break; 753 754 case F_GETLK: 755 error = fget_unlocked(td, fd, &cap_flock_rights, &fp); 756 if (error != 0) 757 break; 758 if (fp->f_type != DTYPE_VNODE || fp->f_ops == &path_fileops) { 759 error = EBADF; 760 fdrop(fp, td); 761 break; 762 } 763 flp = (struct flock *)arg; 764 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK && 765 flp->l_type != F_UNLCK) { 766 error = EINVAL; 767 fdrop(fp, td); 768 break; 769 } 770 if (flp->l_whence == SEEK_CUR) { 771 foffset = foffset_get(fp); 772 if ((flp->l_start > 0 && 773 foffset > OFF_MAX - flp->l_start) || 774 (flp->l_start < 0 && 775 foffset < OFF_MIN - flp->l_start)) { 776 error = EOVERFLOW; 777 fdrop(fp, td); 778 break; 779 } 780 flp->l_start += foffset; 781 } 782 vp = fp->f_vnode; 783 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp, 784 F_POSIX); 785 fdrop(fp, td); 786 break; 787 788 case F_ADD_SEALS: 789 error = fget_unlocked(td, fd, &cap_no_rights, &fp); 790 if (error != 0) 791 break; 792 error = fo_add_seals(fp, arg); 793 fdrop(fp, td); 794 break; 795 796 case F_GET_SEALS: 797 error = fget_unlocked(td, fd, &cap_no_rights, &fp); 798 if (error != 0) 799 break; 800 if (fo_get_seals(fp, &seals) == 0) 801 td->td_retval[0] = seals; 802 else 803 error = EINVAL; 804 fdrop(fp, td); 805 break; 806 807 case F_RDAHEAD: 808 arg = arg ? 128 * 1024: 0; 809 /* FALLTHROUGH */ 810 case F_READAHEAD: 811 error = fget_unlocked(td, fd, &cap_no_rights, &fp); 812 if (error != 0) 813 break; 814 if (fp->f_type != DTYPE_VNODE || fp->f_ops == &path_fileops) { 815 fdrop(fp, td); 816 error = EBADF; 817 break; 818 } 819 vp = fp->f_vnode; 820 if (vp->v_type != VREG) { 821 fdrop(fp, td); 822 error = ENOTTY; 823 break; 824 } 825 826 /* 827 * Exclusive lock synchronizes against f_seqcount reads and 828 * writes in sequential_heuristic(). 829 */ 830 error = vn_lock(vp, LK_EXCLUSIVE); 831 if (error != 0) { 832 fdrop(fp, td); 833 break; 834 } 835 if (arg >= 0) { 836 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize; 837 arg = MIN(arg, INT_MAX - bsize + 1); 838 fp->f_seqcount[UIO_READ] = MIN(IO_SEQMAX, 839 (arg + bsize - 1) / bsize); 840 atomic_set_int(&fp->f_flag, FRDAHEAD); 841 } else { 842 atomic_clear_int(&fp->f_flag, FRDAHEAD); 843 } 844 VOP_UNLOCK(vp); 845 fdrop(fp, td); 846 break; 847 848 case F_ISUNIONSTACK: 849 /* 850 * Check if the vnode is part of a union stack (either the 851 * "union" flag from mount(2) or unionfs). 852 * 853 * Prior to introduction of this op libc's readdir would call 854 * fstatfs(2), in effect unnecessarily copying kilobytes of 855 * data just to check fs name and a mount flag. 856 * 857 * Fixing the code to handle everything in the kernel instead 858 * is a non-trivial endeavor and has low priority, thus this 859 * horrible kludge facilitates the current behavior in a much 860 * cheaper manner until someone(tm) sorts this out. 861 */ 862 error = fget_unlocked(td, fd, &cap_no_rights, &fp); 863 if (error != 0) 864 break; 865 if (fp->f_type != DTYPE_VNODE) { 866 fdrop(fp, td); 867 error = EBADF; 868 break; 869 } 870 vp = fp->f_vnode; 871 /* 872 * Since we don't prevent dooming the vnode even non-null mp 873 * found can become immediately stale. This is tolerable since 874 * mount points are type-stable (providing safe memory access) 875 * and any vfs op on this vnode going forward will return an 876 * error (meaning return value in this case is meaningless). 877 */ 878 mp = atomic_load_ptr(&vp->v_mount); 879 if (__predict_false(mp == NULL)) { 880 fdrop(fp, td); 881 error = EBADF; 882 break; 883 } 884 td->td_retval[0] = 0; 885 if (mp->mnt_kern_flag & MNTK_UNIONFS || 886 mp->mnt_flag & MNT_UNION) 887 td->td_retval[0] = 1; 888 fdrop(fp, td); 889 break; 890 891 case F_KINFO: 892 #ifdef CAPABILITY_MODE 893 if (CAP_TRACING(td)) 894 ktrcapfail(CAPFAIL_SYSCALL, &cmd); 895 if (IN_CAPABILITY_MODE(td)) { 896 error = ECAPMODE; 897 break; 898 } 899 #endif 900 error = copyin((void *)arg, &kif_sz, sizeof(kif_sz)); 901 if (error != 0) 902 break; 903 if (kif_sz != sizeof(*kif)) { 904 error = EINVAL; 905 break; 906 } 907 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK | M_ZERO); 908 FILEDESC_SLOCK(fdp); 909 error = fget_cap_noref(fdp, fd, &cap_fcntl_rights, &fp, NULL); 910 if (error == 0 && fhold(fp)) { 911 export_file_to_kinfo(fp, fd, NULL, kif, fdp, 0); 912 FILEDESC_SUNLOCK(fdp); 913 fdrop(fp, td); 914 if ((kif->kf_status & KF_ATTR_VALID) != 0) { 915 kif->kf_structsize = sizeof(*kif); 916 error = copyout(kif, (void *)arg, sizeof(*kif)); 917 } else { 918 error = EBADF; 919 } 920 } else { 921 FILEDESC_SUNLOCK(fdp); 922 if (error == 0) 923 error = EBADF; 924 } 925 free(kif, M_TEMP); 926 break; 927 928 default: 929 if ((cmd & ((1u << F_DUP3FD_SHIFT) - 1)) != F_DUP3FD) 930 return (EXTERROR(EINVAL, "invalid fcntl cmd")); 931 /* Handle F_DUP3FD */ 932 flags = (cmd >> F_DUP3FD_SHIFT); 933 if ((flags & ~(FD_CLOEXEC | FD_CLOFORK)) != 0) 934 return (EXTERROR(EINVAL, "invalid flags for F_DUP3FD")); 935 tmp = arg; 936 error = kern_dup(td, FDDUP_FIXED, 937 ((flags & FD_CLOEXEC) != 0 ? FDDUP_FLAG_CLOEXEC : 0) | 938 ((flags & FD_CLOFORK) != 0 ? FDDUP_FLAG_CLOFORK : 0), 939 fd, tmp); 940 break; 941 } 942 return (error); 943 } 944 945 static int 946 getmaxfd(struct thread *td) 947 { 948 949 return (min((int)lim_cur(td, RLIMIT_NOFILE), maxfilesperproc)); 950 } 951 952 /* 953 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD). 954 */ 955 int 956 kern_dup(struct thread *td, u_int mode, int flags, int old, int new) 957 { 958 struct filedesc *fdp; 959 struct filedescent *oldfde, *newfde; 960 struct proc *p; 961 struct file *delfp, *oldfp; 962 u_long *oioctls, *nioctls; 963 int error, maxfd; 964 965 p = td->td_proc; 966 fdp = p->p_fd; 967 oioctls = NULL; 968 969 MPASS((flags & ~(FDDUP_FLAG_CLOEXEC | FDDUP_FLAG_CLOFORK)) == 0); 970 MPASS(mode < FDDUP_LASTMODE); 971 972 AUDIT_ARG_FD(old); 973 /* XXXRW: if (flags & FDDUP_FIXED) AUDIT_ARG_FD2(new); */ 974 975 /* 976 * Verify we have a valid descriptor to dup from and possibly to 977 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should 978 * return EINVAL when the new descriptor is out of bounds. 979 */ 980 if (old < 0) 981 return (EBADF); 982 if (new < 0) 983 return (mode == FDDUP_FCNTL ? EINVAL : EBADF); 984 maxfd = getmaxfd(td); 985 if (new >= maxfd) 986 return (mode == FDDUP_FCNTL ? EINVAL : EBADF); 987 988 error = EBADF; 989 FILEDESC_XLOCK(fdp); 990 if (fget_noref(fdp, old) == NULL) 991 goto unlock; 992 if (mode == FDDUP_FIXED && old == new) { 993 td->td_retval[0] = new; 994 if ((flags & FDDUP_FLAG_CLOEXEC) != 0) 995 fdp->fd_ofiles[new].fde_flags |= UF_EXCLOSE; 996 if ((flags & FDDUP_FLAG_CLOFORK) != 0) 997 fdp->fd_ofiles[new].fde_flags |= UF_FOCLOSE; 998 error = 0; 999 goto unlock; 1000 } 1001 1002 oldfde = &fdp->fd_ofiles[old]; 1003 oldfp = oldfde->fde_file; 1004 if (!fhold(oldfp)) 1005 goto unlock; 1006 1007 /* 1008 * If the caller specified a file descriptor, make sure the file 1009 * table is large enough to hold it, and grab it. Otherwise, just 1010 * allocate a new descriptor the usual way. 1011 */ 1012 switch (mode) { 1013 case FDDUP_NORMAL: 1014 case FDDUP_FCNTL: 1015 if ((error = fdalloc(td, new, &new)) != 0) { 1016 fdrop(oldfp, td); 1017 goto unlock; 1018 } 1019 break; 1020 case FDDUP_FIXED: 1021 if (new >= fdp->fd_nfiles) { 1022 /* 1023 * The resource limits are here instead of e.g. 1024 * fdalloc(), because the file descriptor table may be 1025 * shared between processes, so we can't really use 1026 * racct_add()/racct_sub(). Instead of counting the 1027 * number of actually allocated descriptors, just put 1028 * the limit on the size of the file descriptor table. 1029 */ 1030 #ifdef RACCT 1031 if (RACCT_ENABLED()) { 1032 error = racct_set_unlocked(p, RACCT_NOFILE, new + 1); 1033 if (error != 0) { 1034 error = EMFILE; 1035 fdrop(oldfp, td); 1036 goto unlock; 1037 } 1038 } 1039 #endif 1040 fdgrowtable_exp(fdp, new + 1); 1041 } 1042 if (!fdisused(fdp, new)) 1043 fdused(fdp, new); 1044 break; 1045 default: 1046 KASSERT(0, ("%s unsupported mode %d", __func__, mode)); 1047 } 1048 1049 KASSERT(old != new, ("new fd is same as old")); 1050 1051 /* Refetch oldfde because the table may have grown and old one freed. */ 1052 oldfde = &fdp->fd_ofiles[old]; 1053 KASSERT(oldfp == oldfde->fde_file, 1054 ("fdt_ofiles shift from growth observed at fd %d", 1055 old)); 1056 1057 newfde = &fdp->fd_ofiles[new]; 1058 delfp = newfde->fde_file; 1059 1060 nioctls = filecaps_copy_prep(&oldfde->fde_caps); 1061 1062 /* 1063 * Duplicate the source descriptor. 1064 */ 1065 #ifdef CAPABILITIES 1066 seqc_write_begin(&newfde->fde_seqc); 1067 #endif 1068 oioctls = filecaps_free_prep(&newfde->fde_caps); 1069 fde_copy(oldfde, newfde); 1070 filecaps_copy_finish(&oldfde->fde_caps, &newfde->fde_caps, 1071 nioctls); 1072 newfde->fde_flags = (oldfde->fde_flags & ~(UF_EXCLOSE | UF_FOCLOSE)) | 1073 ((flags & FDDUP_FLAG_CLOEXEC) != 0 ? UF_EXCLOSE : 0) | 1074 ((flags & FDDUP_FLAG_CLOFORK) != 0 ? UF_FOCLOSE : 0); 1075 #ifdef CAPABILITIES 1076 seqc_write_end(&newfde->fde_seqc); 1077 #endif 1078 td->td_retval[0] = new; 1079 1080 error = 0; 1081 1082 if (delfp != NULL) { 1083 (void) closefp(fdp, new, delfp, td, true, false); 1084 FILEDESC_UNLOCK_ASSERT(fdp); 1085 } else { 1086 unlock: 1087 FILEDESC_XUNLOCK(fdp); 1088 } 1089 1090 filecaps_free_finish(oioctls); 1091 return (error); 1092 } 1093 1094 static void 1095 sigiofree(struct sigio *sigio) 1096 { 1097 crfree(sigio->sio_ucred); 1098 free(sigio, M_SIGIO); 1099 } 1100 1101 static struct sigio * 1102 funsetown_locked(struct sigio *sigio) 1103 { 1104 struct proc *p; 1105 struct pgrp *pg; 1106 1107 SIGIO_ASSERT_LOCKED(); 1108 1109 if (sigio == NULL) 1110 return (NULL); 1111 *sigio->sio_myref = NULL; 1112 if (sigio->sio_pgid < 0) { 1113 pg = sigio->sio_pgrp; 1114 PGRP_LOCK(pg); 1115 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio, sio_pgsigio); 1116 PGRP_UNLOCK(pg); 1117 } else { 1118 p = sigio->sio_proc; 1119 PROC_LOCK(p); 1120 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio, sio_pgsigio); 1121 PROC_UNLOCK(p); 1122 } 1123 return (sigio); 1124 } 1125 1126 /* 1127 * If sigio is on the list associated with a process or process group, 1128 * disable signalling from the device, remove sigio from the list and 1129 * free sigio. 1130 */ 1131 void 1132 funsetown(struct sigio **sigiop) 1133 { 1134 struct sigio *sigio; 1135 1136 /* Racy check, consumers must provide synchronization. */ 1137 if (*sigiop == NULL) 1138 return; 1139 1140 SIGIO_LOCK(); 1141 sigio = funsetown_locked(*sigiop); 1142 SIGIO_UNLOCK(); 1143 if (sigio != NULL) 1144 sigiofree(sigio); 1145 } 1146 1147 /* 1148 * Free a list of sigio structures. The caller must ensure that new sigio 1149 * structures cannot be added after this point. For process groups this is 1150 * guaranteed using the proctree lock; for processes, the P_WEXIT flag serves 1151 * as an interlock. 1152 */ 1153 void 1154 funsetownlst(struct sigiolst *sigiolst) 1155 { 1156 struct proc *p; 1157 struct pgrp *pg; 1158 struct sigio *sigio, *tmp; 1159 1160 /* Racy check. */ 1161 sigio = SLIST_FIRST(sigiolst); 1162 if (sigio == NULL) 1163 return; 1164 1165 p = NULL; 1166 pg = NULL; 1167 1168 SIGIO_LOCK(); 1169 sigio = SLIST_FIRST(sigiolst); 1170 if (sigio == NULL) { 1171 SIGIO_UNLOCK(); 1172 return; 1173 } 1174 1175 /* 1176 * Every entry of the list should belong to a single proc or pgrp. 1177 */ 1178 if (sigio->sio_pgid < 0) { 1179 pg = sigio->sio_pgrp; 1180 sx_assert(&proctree_lock, SX_XLOCKED); 1181 PGRP_LOCK(pg); 1182 } else /* if (sigio->sio_pgid > 0) */ { 1183 p = sigio->sio_proc; 1184 PROC_LOCK(p); 1185 KASSERT((p->p_flag & P_WEXIT) != 0, 1186 ("%s: process %p is not exiting", __func__, p)); 1187 } 1188 1189 SLIST_FOREACH(sigio, sigiolst, sio_pgsigio) { 1190 *sigio->sio_myref = NULL; 1191 if (pg != NULL) { 1192 KASSERT(sigio->sio_pgid < 0, 1193 ("Proc sigio in pgrp sigio list")); 1194 KASSERT(sigio->sio_pgrp == pg, 1195 ("Bogus pgrp in sigio list")); 1196 } else /* if (p != NULL) */ { 1197 KASSERT(sigio->sio_pgid > 0, 1198 ("Pgrp sigio in proc sigio list")); 1199 KASSERT(sigio->sio_proc == p, 1200 ("Bogus proc in sigio list")); 1201 } 1202 } 1203 1204 if (pg != NULL) 1205 PGRP_UNLOCK(pg); 1206 else 1207 PROC_UNLOCK(p); 1208 SIGIO_UNLOCK(); 1209 1210 SLIST_FOREACH_SAFE(sigio, sigiolst, sio_pgsigio, tmp) 1211 sigiofree(sigio); 1212 } 1213 1214 /* 1215 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg). 1216 * 1217 * After permission checking, add a sigio structure to the sigio list for 1218 * the process or process group. 1219 */ 1220 int 1221 fsetown(pid_t pgid, struct sigio **sigiop) 1222 { 1223 struct proc *proc; 1224 struct pgrp *pgrp; 1225 struct sigio *osigio, *sigio; 1226 int ret; 1227 1228 if (pgid == 0) { 1229 funsetown(sigiop); 1230 return (0); 1231 } 1232 1233 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK); 1234 sigio->sio_pgid = pgid; 1235 sigio->sio_ucred = crhold(curthread->td_ucred); 1236 sigio->sio_myref = sigiop; 1237 1238 ret = 0; 1239 if (pgid > 0) { 1240 ret = pget(pgid, PGET_NOTWEXIT | PGET_NOTID | PGET_HOLD, &proc); 1241 SIGIO_LOCK(); 1242 osigio = funsetown_locked(*sigiop); 1243 if (ret == 0) { 1244 PROC_LOCK(proc); 1245 _PRELE(proc); 1246 if ((proc->p_flag & P_WEXIT) != 0) { 1247 ret = ESRCH; 1248 } else if (proc->p_session != 1249 curthread->td_proc->p_session) { 1250 /* 1251 * Policy - Don't allow a process to FSETOWN a 1252 * process in another session. 1253 * 1254 * Remove this test to allow maximum flexibility 1255 * or restrict FSETOWN to the current process or 1256 * process group for maximum safety. 1257 */ 1258 ret = EPERM; 1259 } else { 1260 sigio->sio_proc = proc; 1261 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, 1262 sio_pgsigio); 1263 } 1264 PROC_UNLOCK(proc); 1265 } 1266 } else /* if (pgid < 0) */ { 1267 sx_slock(&proctree_lock); 1268 SIGIO_LOCK(); 1269 osigio = funsetown_locked(*sigiop); 1270 pgrp = pgfind(-pgid); 1271 if (pgrp == NULL) { 1272 ret = ESRCH; 1273 } else { 1274 if (pgrp->pg_session != curthread->td_proc->p_session) { 1275 /* 1276 * Policy - Don't allow a process to FSETOWN a 1277 * process in another session. 1278 * 1279 * Remove this test to allow maximum flexibility 1280 * or restrict FSETOWN to the current process or 1281 * process group for maximum safety. 1282 */ 1283 ret = EPERM; 1284 } else { 1285 sigio->sio_pgrp = pgrp; 1286 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, 1287 sio_pgsigio); 1288 } 1289 PGRP_UNLOCK(pgrp); 1290 } 1291 sx_sunlock(&proctree_lock); 1292 } 1293 if (ret == 0) 1294 *sigiop = sigio; 1295 SIGIO_UNLOCK(); 1296 if (osigio != NULL) 1297 sigiofree(osigio); 1298 return (ret); 1299 } 1300 1301 /* 1302 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg). 1303 */ 1304 pid_t 1305 fgetown(struct sigio **sigiop) 1306 { 1307 pid_t pgid; 1308 1309 SIGIO_LOCK(); 1310 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0; 1311 SIGIO_UNLOCK(); 1312 return (pgid); 1313 } 1314 1315 static int 1316 closefp_impl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, 1317 bool audit) 1318 { 1319 int error; 1320 1321 FILEDESC_XLOCK_ASSERT(fdp); 1322 1323 /* 1324 * We now hold the fp reference that used to be owned by the 1325 * descriptor array. We have to unlock the FILEDESC *AFTER* 1326 * knote_fdclose to prevent a race of the fd getting opened, a knote 1327 * added, and deleteing a knote for the new fd. 1328 */ 1329 if (__predict_false(!TAILQ_EMPTY(&fdp->fd_kqlist))) 1330 knote_fdclose(td, fd); 1331 1332 /* 1333 * We need to notify mqueue if the object is of type mqueue. 1334 */ 1335 if (__predict_false(fp->f_type == DTYPE_MQUEUE)) 1336 mq_fdclose(td, fd, fp); 1337 FILEDESC_XUNLOCK(fdp); 1338 1339 #ifdef AUDIT 1340 if (AUDITING_TD(td) && audit) 1341 audit_sysclose(td, fd, fp); 1342 #endif 1343 error = closef(fp, td); 1344 1345 /* 1346 * All paths leading up to closefp() will have already removed or 1347 * replaced the fd in the filedesc table, so a restart would not 1348 * operate on the same file. 1349 */ 1350 if (error == ERESTART) 1351 error = EINTR; 1352 1353 return (error); 1354 } 1355 1356 static int 1357 closefp_hl(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, 1358 bool holdleaders, bool audit) 1359 { 1360 int error; 1361 1362 FILEDESC_XLOCK_ASSERT(fdp); 1363 1364 if (holdleaders) { 1365 if (td->td_proc->p_fdtol != NULL) { 1366 /* 1367 * Ask fdfree() to sleep to ensure that all relevant 1368 * process leaders can be traversed in closef(). 1369 */ 1370 fdp->fd_holdleaderscount++; 1371 } else { 1372 holdleaders = false; 1373 } 1374 } 1375 1376 error = closefp_impl(fdp, fd, fp, td, audit); 1377 if (holdleaders) { 1378 FILEDESC_XLOCK(fdp); 1379 fdp->fd_holdleaderscount--; 1380 if (fdp->fd_holdleaderscount == 0 && 1381 fdp->fd_holdleaderswakeup != 0) { 1382 fdp->fd_holdleaderswakeup = 0; 1383 wakeup(&fdp->fd_holdleaderscount); 1384 } 1385 FILEDESC_XUNLOCK(fdp); 1386 } 1387 return (error); 1388 } 1389 1390 static int 1391 closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td, 1392 bool holdleaders, bool audit) 1393 { 1394 1395 FILEDESC_XLOCK_ASSERT(fdp); 1396 1397 if (__predict_false(td->td_proc->p_fdtol != NULL)) { 1398 return (closefp_hl(fdp, fd, fp, td, holdleaders, audit)); 1399 } else { 1400 return (closefp_impl(fdp, fd, fp, td, audit)); 1401 } 1402 } 1403 1404 /* 1405 * Close a file descriptor. 1406 */ 1407 #ifndef _SYS_SYSPROTO_H_ 1408 struct close_args { 1409 int fd; 1410 }; 1411 #endif 1412 /* ARGSUSED */ 1413 int 1414 sys_close(struct thread *td, struct close_args *uap) 1415 { 1416 1417 return (kern_close(td, uap->fd)); 1418 } 1419 1420 int 1421 kern_close(struct thread *td, int fd) 1422 { 1423 struct filedesc *fdp; 1424 struct file *fp; 1425 1426 fdp = td->td_proc->p_fd; 1427 1428 FILEDESC_XLOCK(fdp); 1429 if ((fp = fget_noref(fdp, fd)) == NULL) { 1430 FILEDESC_XUNLOCK(fdp); 1431 return (EBADF); 1432 } 1433 fdfree(fdp, fd); 1434 1435 /* closefp() drops the FILEDESC lock for us. */ 1436 return (closefp(fdp, fd, fp, td, true, true)); 1437 } 1438 1439 static int 1440 close_range_flags(struct thread *td, u_int lowfd, u_int highfd, int flags) 1441 { 1442 struct filedesc *fdp; 1443 struct fdescenttbl *fdt; 1444 struct filedescent *fde; 1445 int fd, fde_flags; 1446 1447 fde_flags = ((flags & CLOSE_RANGE_CLOEXEC) != 0 ? UF_EXCLOSE : 0) | 1448 ((flags & CLOSE_RANGE_CLOFORK) != 0 ? UF_FOCLOSE : 0); 1449 fdp = td->td_proc->p_fd; 1450 FILEDESC_XLOCK(fdp); 1451 fdt = atomic_load_ptr(&fdp->fd_files); 1452 highfd = MIN(highfd, fdt->fdt_nfiles - 1); 1453 fd = lowfd; 1454 if (__predict_false(fd > highfd)) { 1455 goto out_locked; 1456 } 1457 for (; fd <= highfd; fd++) { 1458 fde = &fdt->fdt_ofiles[fd]; 1459 if (fde->fde_file != NULL) 1460 fde->fde_flags |= fde_flags; 1461 } 1462 out_locked: 1463 FILEDESC_XUNLOCK(fdp); 1464 return (0); 1465 } 1466 1467 static int 1468 close_range_impl(struct thread *td, u_int lowfd, u_int highfd) 1469 { 1470 struct filedesc *fdp; 1471 const struct fdescenttbl *fdt; 1472 struct file *fp; 1473 int fd; 1474 1475 fdp = td->td_proc->p_fd; 1476 FILEDESC_XLOCK(fdp); 1477 fdt = atomic_load_ptr(&fdp->fd_files); 1478 highfd = MIN(highfd, fdt->fdt_nfiles - 1); 1479 fd = lowfd; 1480 if (__predict_false(fd > highfd)) { 1481 goto out_locked; 1482 } 1483 for (;;) { 1484 fp = fdt->fdt_ofiles[fd].fde_file; 1485 if (fp == NULL) { 1486 if (fd == highfd) 1487 goto out_locked; 1488 } else { 1489 fdfree(fdp, fd); 1490 (void) closefp(fdp, fd, fp, td, true, true); 1491 if (fd == highfd) 1492 goto out_unlocked; 1493 FILEDESC_XLOCK(fdp); 1494 fdt = atomic_load_ptr(&fdp->fd_files); 1495 } 1496 fd++; 1497 } 1498 out_locked: 1499 FILEDESC_XUNLOCK(fdp); 1500 out_unlocked: 1501 return (0); 1502 } 1503 1504 int 1505 kern_close_range(struct thread *td, int flags, u_int lowfd, u_int highfd) 1506 { 1507 1508 /* 1509 * Check this prior to clamping; closefrom(3) with only fd 0, 1, and 2 1510 * open should not be a usage error. From a close_range() perspective, 1511 * close_range(3, ~0U, 0) in the same scenario should also likely not 1512 * be a usage error as all fd above 3 are in-fact already closed. 1513 */ 1514 if (highfd < lowfd) { 1515 return (EINVAL); 1516 } 1517 1518 if ((flags & (CLOSE_RANGE_CLOEXEC | CLOSE_RANGE_CLOFORK)) != 0) 1519 return (close_range_flags(td, lowfd, highfd, flags)); 1520 1521 return (close_range_impl(td, lowfd, highfd)); 1522 } 1523 1524 #ifndef _SYS_SYSPROTO_H_ 1525 struct close_range_args { 1526 u_int lowfd; 1527 u_int highfd; 1528 int flags; 1529 }; 1530 #endif 1531 int 1532 sys_close_range(struct thread *td, struct close_range_args *uap) 1533 { 1534 1535 AUDIT_ARG_FD(uap->lowfd); 1536 AUDIT_ARG_CMD(uap->highfd); 1537 AUDIT_ARG_FFLAGS(uap->flags); 1538 1539 if ((uap->flags & ~(CLOSE_RANGE_CLOEXEC | CLOSE_RANGE_CLOFORK)) != 0) 1540 return (EINVAL); 1541 return (kern_close_range(td, uap->flags, uap->lowfd, uap->highfd)); 1542 } 1543 1544 #ifdef COMPAT_FREEBSD12 1545 /* 1546 * Close open file descriptors. 1547 */ 1548 #ifndef _SYS_SYSPROTO_H_ 1549 struct freebsd12_closefrom_args { 1550 int lowfd; 1551 }; 1552 #endif 1553 /* ARGSUSED */ 1554 int 1555 freebsd12_closefrom(struct thread *td, struct freebsd12_closefrom_args *uap) 1556 { 1557 u_int lowfd; 1558 1559 AUDIT_ARG_FD(uap->lowfd); 1560 1561 /* 1562 * Treat negative starting file descriptor values identical to 1563 * closefrom(0) which closes all files. 1564 */ 1565 lowfd = MAX(0, uap->lowfd); 1566 return (kern_close_range(td, 0, lowfd, ~0U)); 1567 } 1568 #endif /* COMPAT_FREEBSD12 */ 1569 1570 #if defined(COMPAT_43) 1571 /* 1572 * Return status information about a file descriptor. 1573 */ 1574 #ifndef _SYS_SYSPROTO_H_ 1575 struct ofstat_args { 1576 int fd; 1577 struct ostat *sb; 1578 }; 1579 #endif 1580 /* ARGSUSED */ 1581 int 1582 ofstat(struct thread *td, struct ofstat_args *uap) 1583 { 1584 struct ostat oub; 1585 struct stat ub; 1586 int error; 1587 1588 error = kern_fstat(td, uap->fd, &ub); 1589 if (error == 0) { 1590 cvtstat(&ub, &oub); 1591 error = copyout(&oub, uap->sb, sizeof(oub)); 1592 } 1593 return (error); 1594 } 1595 #endif /* COMPAT_43 */ 1596 1597 #if defined(COMPAT_FREEBSD11) 1598 int 1599 freebsd11_fstat(struct thread *td, struct freebsd11_fstat_args *uap) 1600 { 1601 struct stat sb; 1602 struct freebsd11_stat osb; 1603 int error; 1604 1605 error = kern_fstat(td, uap->fd, &sb); 1606 if (error != 0) 1607 return (error); 1608 error = freebsd11_cvtstat(&sb, &osb); 1609 if (error == 0) 1610 error = copyout(&osb, uap->sb, sizeof(osb)); 1611 return (error); 1612 } 1613 #endif /* COMPAT_FREEBSD11 */ 1614 1615 /* 1616 * Return status information about a file descriptor. 1617 */ 1618 #ifndef _SYS_SYSPROTO_H_ 1619 struct fstat_args { 1620 int fd; 1621 struct stat *sb; 1622 }; 1623 #endif 1624 /* ARGSUSED */ 1625 int 1626 sys_fstat(struct thread *td, struct fstat_args *uap) 1627 { 1628 struct stat ub; 1629 int error; 1630 1631 error = kern_fstat(td, uap->fd, &ub); 1632 if (error == 0) 1633 error = copyout(&ub, uap->sb, sizeof(ub)); 1634 return (error); 1635 } 1636 1637 int 1638 kern_fstat(struct thread *td, int fd, struct stat *sbp) 1639 { 1640 struct file *fp; 1641 int error; 1642 1643 AUDIT_ARG_FD(fd); 1644 1645 error = fget(td, fd, &cap_fstat_rights, &fp); 1646 if (__predict_false(error != 0)) 1647 return (error); 1648 1649 AUDIT_ARG_FILE(td->td_proc, fp); 1650 1651 sbp->st_filerev = 0; 1652 sbp->st_bsdflags = 0; 1653 error = fo_stat(fp, sbp, td->td_ucred); 1654 fdrop(fp, td); 1655 #ifdef __STAT_TIME_T_EXT 1656 sbp->st_atim_ext = 0; 1657 sbp->st_mtim_ext = 0; 1658 sbp->st_ctim_ext = 0; 1659 sbp->st_btim_ext = 0; 1660 #endif 1661 #ifdef KTRACE 1662 if (KTRPOINT(td, KTR_STRUCT)) 1663 ktrstat_error(sbp, error); 1664 #endif 1665 return (error); 1666 } 1667 1668 #if defined(COMPAT_FREEBSD11) 1669 /* 1670 * Return status information about a file descriptor. 1671 */ 1672 #ifndef _SYS_SYSPROTO_H_ 1673 struct freebsd11_nfstat_args { 1674 int fd; 1675 struct nstat *sb; 1676 }; 1677 #endif 1678 /* ARGSUSED */ 1679 int 1680 freebsd11_nfstat(struct thread *td, struct freebsd11_nfstat_args *uap) 1681 { 1682 struct nstat nub; 1683 struct stat ub; 1684 int error; 1685 1686 error = kern_fstat(td, uap->fd, &ub); 1687 if (error != 0) 1688 return (error); 1689 error = freebsd11_cvtnstat(&ub, &nub); 1690 if (error != 0) 1691 error = copyout(&nub, uap->sb, sizeof(nub)); 1692 return (error); 1693 } 1694 #endif /* COMPAT_FREEBSD11 */ 1695 1696 /* 1697 * Return pathconf information about a file descriptor. 1698 */ 1699 #ifndef _SYS_SYSPROTO_H_ 1700 struct fpathconf_args { 1701 int fd; 1702 int name; 1703 }; 1704 #endif 1705 /* ARGSUSED */ 1706 int 1707 sys_fpathconf(struct thread *td, struct fpathconf_args *uap) 1708 { 1709 long value; 1710 int error; 1711 1712 error = kern_fpathconf(td, uap->fd, uap->name, &value); 1713 if (error == 0) 1714 td->td_retval[0] = value; 1715 return (error); 1716 } 1717 1718 int 1719 kern_fpathconf(struct thread *td, int fd, int name, long *valuep) 1720 { 1721 struct file *fp; 1722 struct vnode *vp; 1723 int error; 1724 1725 error = fget(td, fd, &cap_fpathconf_rights, &fp); 1726 if (error != 0) 1727 return (error); 1728 1729 if (name == _PC_ASYNC_IO) { 1730 *valuep = _POSIX_ASYNCHRONOUS_IO; 1731 goto out; 1732 } 1733 vp = fp->f_vnode; 1734 if (vp != NULL) { 1735 vn_lock(vp, LK_SHARED | LK_RETRY); 1736 error = VOP_PATHCONF(vp, name, valuep); 1737 VOP_UNLOCK(vp); 1738 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) { 1739 if (name != _PC_PIPE_BUF) { 1740 error = EINVAL; 1741 } else { 1742 *valuep = PIPE_BUF; 1743 error = 0; 1744 } 1745 } else { 1746 error = EOPNOTSUPP; 1747 } 1748 out: 1749 fdrop(fp, td); 1750 return (error); 1751 } 1752 1753 /* 1754 * Copy filecaps structure allocating memory for ioctls array if needed. 1755 * 1756 * The last parameter indicates whether the fdtable is locked. If it is not and 1757 * ioctls are encountered, copying fails and the caller must lock the table. 1758 * 1759 * Note that if the table was not locked, the caller has to check the relevant 1760 * sequence counter to determine whether the operation was successful. 1761 */ 1762 bool 1763 filecaps_copy(const struct filecaps *src, struct filecaps *dst, bool locked) 1764 { 1765 size_t size; 1766 1767 if (src->fc_ioctls != NULL && !locked) 1768 return (false); 1769 memcpy(dst, src, sizeof(*src)); 1770 if (src->fc_ioctls == NULL) 1771 return (true); 1772 1773 KASSERT(src->fc_nioctls > 0, 1774 ("fc_ioctls != NULL, but fc_nioctls=%hd", src->fc_nioctls)); 1775 1776 size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls; 1777 dst->fc_ioctls = malloc(size, M_FILECAPS, M_WAITOK); 1778 memcpy(dst->fc_ioctls, src->fc_ioctls, size); 1779 return (true); 1780 } 1781 1782 static u_long * 1783 filecaps_copy_prep(const struct filecaps *src) 1784 { 1785 u_long *ioctls; 1786 size_t size; 1787 1788 if (__predict_true(src->fc_ioctls == NULL)) 1789 return (NULL); 1790 1791 KASSERT(src->fc_nioctls > 0, 1792 ("fc_ioctls != NULL, but fc_nioctls=%hd", src->fc_nioctls)); 1793 1794 size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls; 1795 ioctls = malloc(size, M_FILECAPS, M_WAITOK); 1796 return (ioctls); 1797 } 1798 1799 static void 1800 filecaps_copy_finish(const struct filecaps *src, struct filecaps *dst, 1801 u_long *ioctls) 1802 { 1803 size_t size; 1804 1805 *dst = *src; 1806 if (__predict_true(src->fc_ioctls == NULL)) { 1807 MPASS(ioctls == NULL); 1808 return; 1809 } 1810 1811 size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls; 1812 dst->fc_ioctls = ioctls; 1813 bcopy(src->fc_ioctls, dst->fc_ioctls, size); 1814 } 1815 1816 /* 1817 * Move filecaps structure to the new place and clear the old place. 1818 */ 1819 void 1820 filecaps_move(struct filecaps *src, struct filecaps *dst) 1821 { 1822 1823 *dst = *src; 1824 bzero(src, sizeof(*src)); 1825 } 1826 1827 /* 1828 * Fill the given filecaps structure with full rights. 1829 */ 1830 static void 1831 filecaps_fill(struct filecaps *fcaps) 1832 { 1833 1834 CAP_ALL(&fcaps->fc_rights); 1835 fcaps->fc_ioctls = NULL; 1836 fcaps->fc_nioctls = -1; 1837 fcaps->fc_fcntls = CAP_FCNTL_ALL; 1838 } 1839 1840 /* 1841 * Free memory allocated within filecaps structure. 1842 */ 1843 static void 1844 filecaps_free_ioctl(struct filecaps *fcaps) 1845 { 1846 1847 free(fcaps->fc_ioctls, M_FILECAPS); 1848 fcaps->fc_ioctls = NULL; 1849 } 1850 1851 void 1852 filecaps_free(struct filecaps *fcaps) 1853 { 1854 1855 filecaps_free_ioctl(fcaps); 1856 bzero(fcaps, sizeof(*fcaps)); 1857 } 1858 1859 static u_long * 1860 filecaps_free_prep(struct filecaps *fcaps) 1861 { 1862 u_long *ioctls; 1863 1864 ioctls = fcaps->fc_ioctls; 1865 bzero(fcaps, sizeof(*fcaps)); 1866 return (ioctls); 1867 } 1868 1869 static void 1870 filecaps_free_finish(u_long *ioctls) 1871 { 1872 1873 free(ioctls, M_FILECAPS); 1874 } 1875 1876 /* 1877 * Validate the given filecaps structure. 1878 */ 1879 static void 1880 filecaps_validate(const struct filecaps *fcaps, const char *func) 1881 { 1882 1883 KASSERT(cap_rights_is_valid(&fcaps->fc_rights), 1884 ("%s: invalid rights", func)); 1885 KASSERT((fcaps->fc_fcntls & ~CAP_FCNTL_ALL) == 0, 1886 ("%s: invalid fcntls", func)); 1887 KASSERT(fcaps->fc_fcntls == 0 || 1888 cap_rights_is_set(&fcaps->fc_rights, CAP_FCNTL), 1889 ("%s: fcntls without CAP_FCNTL", func)); 1890 /* 1891 * open calls without WANTIOCTLCAPS free caps but leave the counter 1892 */ 1893 #if 0 1894 KASSERT(fcaps->fc_ioctls != NULL ? fcaps->fc_nioctls > 0 : 1895 (fcaps->fc_nioctls == -1 || fcaps->fc_nioctls == 0), 1896 ("%s: invalid ioctls", func)); 1897 #endif 1898 KASSERT(fcaps->fc_nioctls == 0 || 1899 cap_rights_is_set(&fcaps->fc_rights, CAP_IOCTL), 1900 ("%s: ioctls without CAP_IOCTL", func)); 1901 } 1902 1903 static void 1904 fdgrowtable_exp(struct filedesc *fdp, int nfd) 1905 { 1906 int nfd1; 1907 1908 FILEDESC_XLOCK_ASSERT(fdp); 1909 1910 nfd1 = fdp->fd_nfiles * 2; 1911 if (nfd1 < nfd) 1912 nfd1 = nfd; 1913 fdgrowtable(fdp, nfd1); 1914 } 1915 1916 /* 1917 * Grow the file table to accommodate (at least) nfd descriptors. 1918 */ 1919 static void 1920 fdgrowtable(struct filedesc *fdp, int nfd) 1921 { 1922 struct filedesc0 *fdp0; 1923 struct freetable *ft; 1924 struct fdescenttbl *ntable; 1925 struct fdescenttbl *otable; 1926 int nnfiles, onfiles; 1927 NDSLOTTYPE *nmap, *omap; 1928 1929 KASSERT(fdp->fd_nfiles > 0, ("zero-length file table")); 1930 1931 /* save old values */ 1932 onfiles = fdp->fd_nfiles; 1933 otable = fdp->fd_files; 1934 omap = fdp->fd_map; 1935 1936 /* compute the size of the new table */ 1937 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */ 1938 if (nnfiles <= onfiles) 1939 /* the table is already large enough */ 1940 return; 1941 1942 /* 1943 * Allocate a new table. We need enough space for the number of 1944 * entries, file entries themselves and the struct freetable we will use 1945 * when we decommission the table and place it on the freelist. 1946 * We place the struct freetable in the middle so we don't have 1947 * to worry about padding. 1948 */ 1949 ntable = malloc(offsetof(struct fdescenttbl, fdt_ofiles) + 1950 nnfiles * sizeof(ntable->fdt_ofiles[0]) + 1951 sizeof(struct freetable), 1952 M_FILEDESC, M_ZERO | M_WAITOK); 1953 /* copy the old data */ 1954 ntable->fdt_nfiles = nnfiles; 1955 memcpy(ntable->fdt_ofiles, otable->fdt_ofiles, 1956 onfiles * sizeof(ntable->fdt_ofiles[0])); 1957 1958 /* 1959 * Allocate a new map only if the old is not large enough. It will 1960 * grow at a slower rate than the table as it can map more 1961 * entries than the table can hold. 1962 */ 1963 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) { 1964 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC, 1965 M_ZERO | M_WAITOK); 1966 /* copy over the old data and update the pointer */ 1967 memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap)); 1968 fdp->fd_map = nmap; 1969 } 1970 1971 /* 1972 * Make sure that ntable is correctly initialized before we replace 1973 * fd_files poiner. Otherwise fget_unlocked() may see inconsistent 1974 * data. 1975 */ 1976 atomic_store_rel_ptr((volatile void *)&fdp->fd_files, (uintptr_t)ntable); 1977 1978 /* 1979 * Free the old file table when not shared by other threads or processes. 1980 * The old file table is considered to be shared when either are true: 1981 * - The process has more than one thread. 1982 * - The file descriptor table has been shared via fdshare(). 1983 * 1984 * When shared, the old file table will be placed on a freelist 1985 * which will be processed when the struct filedesc is released. 1986 * 1987 * Note that if onfiles == NDFILE, we're dealing with the original 1988 * static allocation contained within (struct filedesc0 *)fdp, 1989 * which must not be freed. 1990 */ 1991 if (onfiles > NDFILE) { 1992 /* 1993 * Note we may be called here from fdinit while allocating a 1994 * table for a new process in which case ->p_fd points 1995 * elsewhere. 1996 */ 1997 if (curproc->p_fd != fdp || FILEDESC_IS_ONLY_USER(fdp)) { 1998 free(otable, M_FILEDESC); 1999 } else { 2000 ft = (struct freetable *)&otable->fdt_ofiles[onfiles]; 2001 fdp0 = (struct filedesc0 *)fdp; 2002 ft->ft_table = otable; 2003 SLIST_INSERT_HEAD(&fdp0->fd_free, ft, ft_next); 2004 } 2005 } 2006 /* 2007 * The map does not have the same possibility of threads still 2008 * holding references to it. So always free it as long as it 2009 * does not reference the original static allocation. 2010 */ 2011 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE)) 2012 free(omap, M_FILEDESC); 2013 } 2014 2015 /* 2016 * Allocate a file descriptor for the process. 2017 */ 2018 int 2019 fdalloc(struct thread *td, int minfd, int *result) 2020 { 2021 struct proc *p = td->td_proc; 2022 struct filedesc *fdp = p->p_fd; 2023 int fd, maxfd, allocfd; 2024 #ifdef RACCT 2025 int error; 2026 #endif 2027 2028 FILEDESC_XLOCK_ASSERT(fdp); 2029 2030 if (fdp->fd_freefile > minfd) 2031 minfd = fdp->fd_freefile; 2032 2033 maxfd = getmaxfd(td); 2034 2035 /* 2036 * Search the bitmap for a free descriptor starting at minfd. 2037 * If none is found, grow the file table. 2038 */ 2039 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles); 2040 if (__predict_false(fd >= maxfd)) 2041 return (EMFILE); 2042 if (__predict_false(fd >= fdp->fd_nfiles)) { 2043 allocfd = min(fd * 2, maxfd); 2044 #ifdef RACCT 2045 if (RACCT_ENABLED()) { 2046 error = racct_set_unlocked(p, RACCT_NOFILE, allocfd); 2047 if (error != 0) 2048 return (EMFILE); 2049 } 2050 #endif 2051 /* 2052 * fd is already equal to first free descriptor >= minfd, so 2053 * we only need to grow the table and we are done. 2054 */ 2055 fdgrowtable_exp(fdp, allocfd); 2056 } 2057 2058 /* 2059 * Perform some sanity checks, then mark the file descriptor as 2060 * used and return it to the caller. 2061 */ 2062 KASSERT(fd >= 0 && fd < min(maxfd, fdp->fd_nfiles), 2063 ("invalid descriptor %d", fd)); 2064 KASSERT(!fdisused(fdp, fd), 2065 ("fd_first_free() returned non-free descriptor")); 2066 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL, 2067 ("file descriptor isn't free")); 2068 fdused(fdp, fd); 2069 *result = fd; 2070 return (0); 2071 } 2072 2073 /* 2074 * Allocate n file descriptors for the process. 2075 */ 2076 int 2077 fdallocn(struct thread *td, int minfd, int *fds, int n) 2078 { 2079 struct proc *p = td->td_proc; 2080 struct filedesc *fdp = p->p_fd; 2081 int i; 2082 2083 FILEDESC_XLOCK_ASSERT(fdp); 2084 2085 for (i = 0; i < n; i++) 2086 if (fdalloc(td, 0, &fds[i]) != 0) 2087 break; 2088 2089 if (i < n) { 2090 for (i--; i >= 0; i--) 2091 fdunused(fdp, fds[i]); 2092 return (EMFILE); 2093 } 2094 2095 return (0); 2096 } 2097 2098 /* 2099 * Create a new open file structure and allocate a file descriptor for the 2100 * process that refers to it. We add one reference to the file for the 2101 * descriptor table and one reference for resultfp. This is to prevent us 2102 * being preempted and the entry in the descriptor table closed after we 2103 * release the FILEDESC lock. 2104 */ 2105 int 2106 falloc_caps(struct thread *td, struct file **resultfp, int *resultfd, int flags, 2107 struct filecaps *fcaps) 2108 { 2109 struct file *fp; 2110 int error, fd; 2111 2112 MPASS(resultfp != NULL); 2113 MPASS(resultfd != NULL); 2114 2115 error = _falloc_noinstall(td, &fp, 2); 2116 if (__predict_false(error != 0)) { 2117 return (error); 2118 } 2119 2120 error = finstall_refed(td, fp, &fd, flags, fcaps); 2121 if (__predict_false(error != 0)) { 2122 falloc_abort(td, fp); 2123 return (error); 2124 } 2125 2126 *resultfp = fp; 2127 *resultfd = fd; 2128 2129 return (0); 2130 } 2131 2132 /* 2133 * Create a new open file structure without allocating a file descriptor. 2134 */ 2135 int 2136 _falloc_noinstall(struct thread *td, struct file **resultfp, u_int n) 2137 { 2138 struct file *fp; 2139 int maxuserfiles = maxfiles - (maxfiles / 20); 2140 int openfiles_new; 2141 static struct timeval lastfail; 2142 static int curfail; 2143 2144 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__)); 2145 MPASS(n > 0); 2146 2147 openfiles_new = atomic_fetchadd_int(&openfiles, 1) + 1; 2148 if ((openfiles_new >= maxuserfiles && 2149 priv_check(td, PRIV_MAXFILES) != 0) || 2150 openfiles_new >= maxfiles) { 2151 atomic_subtract_int(&openfiles, 1); 2152 if (ppsratecheck(&lastfail, &curfail, 1)) { 2153 printf("kern.maxfiles limit exceeded by uid %i, (%s) " 2154 "please see tuning(7).\n", td->td_ucred->cr_ruid, td->td_proc->p_comm); 2155 } 2156 return (ENFILE); 2157 } 2158 fp = uma_zalloc(file_zone, M_WAITOK); 2159 bzero(fp, sizeof(*fp)); 2160 refcount_init(&fp->f_count, n); 2161 fp->f_cred = crhold(td->td_ucred); 2162 fp->f_ops = &badfileops; 2163 *resultfp = fp; 2164 return (0); 2165 } 2166 2167 void 2168 falloc_abort(struct thread *td, struct file *fp) 2169 { 2170 2171 /* 2172 * For assertion purposes. 2173 */ 2174 refcount_init(&fp->f_count, 0); 2175 _fdrop(fp, td); 2176 } 2177 2178 /* 2179 * Install a file in a file descriptor table. 2180 */ 2181 void 2182 _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags, 2183 struct filecaps *fcaps) 2184 { 2185 struct filedescent *fde; 2186 2187 MPASS(fp != NULL); 2188 if (fcaps != NULL) 2189 filecaps_validate(fcaps, __func__); 2190 FILEDESC_XLOCK_ASSERT(fdp); 2191 2192 fde = &fdp->fd_ofiles[fd]; 2193 #ifdef CAPABILITIES 2194 seqc_write_begin(&fde->fde_seqc); 2195 #endif 2196 fde->fde_file = fp; 2197 fde->fde_flags = ((flags & O_CLOEXEC) != 0 ? UF_EXCLOSE : 0) | 2198 ((flags & O_CLOFORK) != 0 ? UF_FOCLOSE : 0) | 2199 ((flags & O_RESOLVE_BENEATH) != 0 ? UF_RESOLVE_BENEATH : 0); 2200 if (fcaps != NULL) 2201 filecaps_move(fcaps, &fde->fde_caps); 2202 else 2203 filecaps_fill(&fde->fde_caps); 2204 #ifdef CAPABILITIES 2205 seqc_write_end(&fde->fde_seqc); 2206 #endif 2207 } 2208 2209 int 2210 finstall_refed(struct thread *td, struct file *fp, int *fd, int flags, 2211 struct filecaps *fcaps) 2212 { 2213 struct filedesc *fdp = td->td_proc->p_fd; 2214 int error; 2215 2216 MPASS(fd != NULL); 2217 2218 FILEDESC_XLOCK(fdp); 2219 error = fdalloc(td, 0, fd); 2220 if (__predict_true(error == 0)) { 2221 _finstall(fdp, fp, *fd, flags, fcaps); 2222 } 2223 FILEDESC_XUNLOCK(fdp); 2224 return (error); 2225 } 2226 2227 int 2228 finstall(struct thread *td, struct file *fp, int *fd, int flags, 2229 struct filecaps *fcaps) 2230 { 2231 int error; 2232 2233 MPASS(fd != NULL); 2234 2235 if (!fhold(fp)) 2236 return (EBADF); 2237 error = finstall_refed(td, fp, fd, flags, fcaps); 2238 if (__predict_false(error != 0)) { 2239 fdrop(fp, td); 2240 } 2241 return (error); 2242 } 2243 2244 /* 2245 * Build a new filedesc structure from another. 2246 * 2247 * If fdp is not NULL, return with it shared locked. 2248 */ 2249 struct filedesc * 2250 fdinit(void) 2251 { 2252 struct filedesc0 *newfdp0; 2253 struct filedesc *newfdp; 2254 2255 newfdp0 = uma_zalloc(filedesc0_zone, M_WAITOK | M_ZERO); 2256 newfdp = &newfdp0->fd_fd; 2257 2258 /* Create the file descriptor table. */ 2259 FILEDESC_LOCK_INIT(newfdp); 2260 refcount_init(&newfdp->fd_refcnt, 1); 2261 refcount_init(&newfdp->fd_holdcnt, 1); 2262 newfdp->fd_map = newfdp0->fd_dmap; 2263 newfdp->fd_files = (struct fdescenttbl *)&newfdp0->fd_dfiles; 2264 newfdp->fd_files->fdt_nfiles = NDFILE; 2265 2266 return (newfdp); 2267 } 2268 2269 /* 2270 * Build a pwddesc structure from another. 2271 * Copy the current, root, and jail root vnode references. 2272 * 2273 * If pdp is not NULL and keeplock is true, return with it (exclusively) locked. 2274 */ 2275 struct pwddesc * 2276 pdinit(struct pwddesc *pdp, bool keeplock) 2277 { 2278 struct pwddesc *newpdp; 2279 struct pwd *newpwd; 2280 2281 newpdp = malloc(sizeof(*newpdp), M_PWDDESC, M_WAITOK | M_ZERO); 2282 2283 PWDDESC_LOCK_INIT(newpdp); 2284 refcount_init(&newpdp->pd_refcount, 1); 2285 newpdp->pd_cmask = CMASK; 2286 2287 if (pdp == NULL) { 2288 newpwd = pwd_alloc(); 2289 smr_serialized_store(&newpdp->pd_pwd, newpwd, true); 2290 return (newpdp); 2291 } 2292 2293 PWDDESC_XLOCK(pdp); 2294 newpwd = pwd_hold_pwddesc(pdp); 2295 smr_serialized_store(&newpdp->pd_pwd, newpwd, true); 2296 if (!keeplock) 2297 PWDDESC_XUNLOCK(pdp); 2298 return (newpdp); 2299 } 2300 2301 /* 2302 * Hold either filedesc or pwddesc of the passed process. 2303 * 2304 * The process lock is used to synchronize against the target exiting and 2305 * freeing the data. 2306 * 2307 * Clearing can be ilustrated in 3 steps: 2308 * 1. set the pointer to NULL. Either routine can race against it, hence 2309 * atomic_load_ptr. 2310 * 2. observe the process lock as not taken. Until then fdhold/pdhold can 2311 * race to either still see the pointer or find NULL. It is still safe to 2312 * grab a reference as clearing is stalled. 2313 * 3. after the lock is observed as not taken, any fdhold/pdhold calls are 2314 * guaranteed to see NULL, making it safe to finish clearing 2315 */ 2316 static struct filedesc * 2317 fdhold(struct proc *p) 2318 { 2319 struct filedesc *fdp; 2320 2321 PROC_LOCK_ASSERT(p, MA_OWNED); 2322 fdp = atomic_load_ptr(&p->p_fd); 2323 if (fdp != NULL) 2324 refcount_acquire(&fdp->fd_holdcnt); 2325 return (fdp); 2326 } 2327 2328 static struct pwddesc * 2329 pdhold(struct proc *p) 2330 { 2331 struct pwddesc *pdp; 2332 2333 PROC_LOCK_ASSERT(p, MA_OWNED); 2334 pdp = atomic_load_ptr(&p->p_pd); 2335 if (pdp != NULL) 2336 refcount_acquire(&pdp->pd_refcount); 2337 return (pdp); 2338 } 2339 2340 static void 2341 fddrop(struct filedesc *fdp) 2342 { 2343 2344 if (refcount_load(&fdp->fd_holdcnt) > 1) { 2345 if (refcount_release(&fdp->fd_holdcnt) == 0) 2346 return; 2347 } 2348 2349 FILEDESC_LOCK_DESTROY(fdp); 2350 uma_zfree(filedesc0_zone, fdp); 2351 } 2352 2353 static void 2354 pddrop(struct pwddesc *pdp) 2355 { 2356 struct pwd *pwd; 2357 2358 if (refcount_release_if_not_last(&pdp->pd_refcount)) 2359 return; 2360 2361 PWDDESC_XLOCK(pdp); 2362 if (refcount_release(&pdp->pd_refcount) == 0) { 2363 PWDDESC_XUNLOCK(pdp); 2364 return; 2365 } 2366 pwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 2367 pwd_set(pdp, NULL); 2368 PWDDESC_XUNLOCK(pdp); 2369 pwd_drop(pwd); 2370 2371 PWDDESC_LOCK_DESTROY(pdp); 2372 free(pdp, M_PWDDESC); 2373 } 2374 2375 /* 2376 * Share a filedesc structure. 2377 */ 2378 struct filedesc * 2379 fdshare(struct filedesc *fdp) 2380 { 2381 2382 refcount_acquire(&fdp->fd_refcnt); 2383 return (fdp); 2384 } 2385 2386 /* 2387 * Share a pwddesc structure. 2388 */ 2389 struct pwddesc * 2390 pdshare(struct pwddesc *pdp) 2391 { 2392 refcount_acquire(&pdp->pd_refcount); 2393 return (pdp); 2394 } 2395 2396 /* 2397 * Unshare a filedesc structure, if necessary by making a copy 2398 */ 2399 void 2400 fdunshare(struct thread *td) 2401 { 2402 struct filedesc *tmp; 2403 struct proc *p = td->td_proc; 2404 2405 if (refcount_load(&p->p_fd->fd_refcnt) == 1) 2406 return; 2407 2408 tmp = fdcopy(p->p_fd); 2409 fdescfree(td); 2410 p->p_fd = tmp; 2411 } 2412 2413 /* 2414 * Unshare a pwddesc structure. 2415 */ 2416 void 2417 pdunshare(struct thread *td) 2418 { 2419 struct pwddesc *pdp; 2420 struct proc *p; 2421 2422 p = td->td_proc; 2423 /* Not shared. */ 2424 if (refcount_load(&p->p_pd->pd_refcount) == 1) 2425 return; 2426 2427 pdp = pdcopy(p->p_pd); 2428 pdescfree(td); 2429 p->p_pd = pdp; 2430 } 2431 2432 /* 2433 * Copy a filedesc structure. A NULL pointer in returns a NULL reference, 2434 * this is to ease callers, not catch errors. 2435 */ 2436 struct filedesc * 2437 fdcopy(struct filedesc *fdp) 2438 { 2439 struct filedesc *newfdp; 2440 struct filedescent *nfde, *ofde; 2441 int i, lastfile; 2442 2443 MPASS(fdp != NULL); 2444 2445 newfdp = fdinit(); 2446 FILEDESC_SLOCK(fdp); 2447 for (;;) { 2448 lastfile = fdlastfile(fdp); 2449 if (lastfile < newfdp->fd_nfiles) 2450 break; 2451 FILEDESC_SUNLOCK(fdp); 2452 fdgrowtable(newfdp, lastfile + 1); 2453 FILEDESC_SLOCK(fdp); 2454 } 2455 /* copy all passable descriptors (i.e. not kqueue) */ 2456 newfdp->fd_freefile = fdp->fd_freefile; 2457 FILEDESC_FOREACH_FDE(fdp, i, ofde) { 2458 if ((ofde->fde_file->f_ops->fo_flags & DFLAG_PASSABLE) == 0 || 2459 (ofde->fde_flags & UF_FOCLOSE) != 0 || 2460 !fhold(ofde->fde_file)) { 2461 if (newfdp->fd_freefile == fdp->fd_freefile) 2462 newfdp->fd_freefile = i; 2463 continue; 2464 } 2465 nfde = &newfdp->fd_ofiles[i]; 2466 *nfde = *ofde; 2467 filecaps_copy(&ofde->fde_caps, &nfde->fde_caps, true); 2468 fdused_init(newfdp, i); 2469 } 2470 MPASS(newfdp->fd_freefile != -1); 2471 FILEDESC_SUNLOCK(fdp); 2472 return (newfdp); 2473 } 2474 2475 /* 2476 * Copy a pwddesc structure. 2477 */ 2478 struct pwddesc * 2479 pdcopy(struct pwddesc *pdp) 2480 { 2481 struct pwddesc *newpdp; 2482 2483 MPASS(pdp != NULL); 2484 2485 newpdp = pdinit(pdp, true); 2486 newpdp->pd_cmask = pdp->pd_cmask; 2487 PWDDESC_XUNLOCK(pdp); 2488 return (newpdp); 2489 } 2490 2491 /* 2492 * Clear POSIX style locks. This is only used when fdp looses a reference (i.e. 2493 * one of processes using it exits) and the table used to be shared. 2494 */ 2495 static void 2496 fdclearlocks(struct thread *td) 2497 { 2498 struct filedesc *fdp; 2499 struct filedesc_to_leader *fdtol; 2500 struct flock lf; 2501 struct file *fp; 2502 struct proc *p; 2503 struct vnode *vp; 2504 int i; 2505 2506 p = td->td_proc; 2507 fdp = p->p_fd; 2508 fdtol = p->p_fdtol; 2509 MPASS(fdtol != NULL); 2510 2511 FILEDESC_XLOCK(fdp); 2512 KASSERT(fdtol->fdl_refcount > 0, 2513 ("filedesc_to_refcount botch: fdl_refcount=%d", 2514 fdtol->fdl_refcount)); 2515 if (fdtol->fdl_refcount == 1 && 2516 (p->p_leader->p_flag & P_ADVLOCK) != 0) { 2517 FILEDESC_FOREACH_FP(fdp, i, fp) { 2518 if (fp->f_type != DTYPE_VNODE || 2519 !fhold(fp)) 2520 continue; 2521 FILEDESC_XUNLOCK(fdp); 2522 lf.l_whence = SEEK_SET; 2523 lf.l_start = 0; 2524 lf.l_len = 0; 2525 lf.l_type = F_UNLCK; 2526 vp = fp->f_vnode; 2527 (void) VOP_ADVLOCK(vp, 2528 (caddr_t)p->p_leader, F_UNLCK, 2529 &lf, F_POSIX); 2530 FILEDESC_XLOCK(fdp); 2531 fdrop(fp, td); 2532 } 2533 } 2534 retry: 2535 if (fdtol->fdl_refcount == 1) { 2536 if (fdp->fd_holdleaderscount > 0 && 2537 (p->p_leader->p_flag & P_ADVLOCK) != 0) { 2538 /* 2539 * close() or kern_dup() has cleared a reference 2540 * in a shared file descriptor table. 2541 */ 2542 fdp->fd_holdleaderswakeup = 1; 2543 sx_sleep(&fdp->fd_holdleaderscount, 2544 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0); 2545 goto retry; 2546 } 2547 if (fdtol->fdl_holdcount > 0) { 2548 /* 2549 * Ensure that fdtol->fdl_leader remains 2550 * valid in closef(). 2551 */ 2552 fdtol->fdl_wakeup = 1; 2553 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK, 2554 "fdlhold", 0); 2555 goto retry; 2556 } 2557 } 2558 fdtol->fdl_refcount--; 2559 if (fdtol->fdl_refcount == 0 && 2560 fdtol->fdl_holdcount == 0) { 2561 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev; 2562 fdtol->fdl_prev->fdl_next = fdtol->fdl_next; 2563 } else 2564 fdtol = NULL; 2565 p->p_fdtol = NULL; 2566 FILEDESC_XUNLOCK(fdp); 2567 if (fdtol != NULL) 2568 free(fdtol, M_FILEDESC_TO_LEADER); 2569 } 2570 2571 /* 2572 * Release a filedesc structure. 2573 */ 2574 static void 2575 fdescfree_fds(struct thread *td, struct filedesc *fdp) 2576 { 2577 struct filedesc0 *fdp0; 2578 struct freetable *ft, *tft; 2579 struct filedescent *fde; 2580 struct file *fp; 2581 int i; 2582 2583 KASSERT(refcount_load(&fdp->fd_refcnt) == 0, 2584 ("%s: fd table %p carries references", __func__, fdp)); 2585 2586 /* 2587 * Serialize with threads iterating over the table, if any. 2588 */ 2589 if (refcount_load(&fdp->fd_holdcnt) > 1) { 2590 FILEDESC_XLOCK(fdp); 2591 FILEDESC_XUNLOCK(fdp); 2592 } 2593 2594 FILEDESC_FOREACH_FDE(fdp, i, fde) { 2595 fp = fde->fde_file; 2596 fdefree_last(fde); 2597 (void) closef(fp, td); 2598 } 2599 2600 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE)) 2601 free(fdp->fd_map, M_FILEDESC); 2602 if (fdp->fd_nfiles > NDFILE) 2603 free(fdp->fd_files, M_FILEDESC); 2604 2605 fdp0 = (struct filedesc0 *)fdp; 2606 SLIST_FOREACH_SAFE(ft, &fdp0->fd_free, ft_next, tft) 2607 free(ft->ft_table, M_FILEDESC); 2608 2609 fddrop(fdp); 2610 } 2611 2612 void 2613 fdescfree(struct thread *td) 2614 { 2615 struct proc *p; 2616 struct filedesc *fdp; 2617 2618 p = td->td_proc; 2619 fdp = p->p_fd; 2620 MPASS(fdp != NULL); 2621 2622 #ifdef RACCT 2623 if (RACCT_ENABLED()) 2624 racct_set_unlocked(p, RACCT_NOFILE, 0); 2625 #endif 2626 2627 if (p->p_fdtol != NULL) 2628 fdclearlocks(td); 2629 2630 /* 2631 * Check fdhold for an explanation. 2632 */ 2633 atomic_store_ptr(&p->p_fd, NULL); 2634 atomic_thread_fence_seq_cst(); 2635 PROC_WAIT_UNLOCKED(p); 2636 2637 if (refcount_release(&fdp->fd_refcnt) == 0) 2638 return; 2639 2640 fdescfree_fds(td, fdp); 2641 } 2642 2643 void 2644 pdescfree(struct thread *td) 2645 { 2646 struct proc *p; 2647 struct pwddesc *pdp; 2648 2649 p = td->td_proc; 2650 pdp = p->p_pd; 2651 MPASS(pdp != NULL); 2652 2653 /* 2654 * Check pdhold for an explanation. 2655 */ 2656 atomic_store_ptr(&p->p_pd, NULL); 2657 atomic_thread_fence_seq_cst(); 2658 PROC_WAIT_UNLOCKED(p); 2659 2660 pddrop(pdp); 2661 } 2662 2663 /* 2664 * For setugid programs, we don't want to people to use that setugidness 2665 * to generate error messages which write to a file which otherwise would 2666 * otherwise be off-limits to the process. We check for filesystems where 2667 * the vnode can change out from under us after execve (like [lin]procfs). 2668 * 2669 * Since fdsetugidsafety calls this only for fd 0, 1 and 2, this check is 2670 * sufficient. We also don't check for setugidness since we know we are. 2671 */ 2672 static bool 2673 is_unsafe(struct file *fp) 2674 { 2675 struct vnode *vp; 2676 2677 if (fp->f_type != DTYPE_VNODE) 2678 return (false); 2679 2680 vp = fp->f_vnode; 2681 return ((vp->v_vflag & VV_PROCDEP) != 0); 2682 } 2683 2684 /* 2685 * Make this setguid thing safe, if at all possible. 2686 */ 2687 void 2688 fdsetugidsafety(struct thread *td) 2689 { 2690 struct filedesc *fdp; 2691 struct file *fp; 2692 int i; 2693 2694 fdp = td->td_proc->p_fd; 2695 KASSERT(refcount_load(&fdp->fd_refcnt) == 1, 2696 ("the fdtable should not be shared")); 2697 MPASS(fdp->fd_nfiles >= 3); 2698 for (i = 0; i <= 2; i++) { 2699 fp = fdp->fd_ofiles[i].fde_file; 2700 if (fp != NULL && is_unsafe(fp)) { 2701 FILEDESC_XLOCK(fdp); 2702 knote_fdclose(td, i); 2703 /* 2704 * NULL-out descriptor prior to close to avoid 2705 * a race while close blocks. 2706 */ 2707 fdfree(fdp, i); 2708 FILEDESC_XUNLOCK(fdp); 2709 (void) closef(fp, td); 2710 } 2711 } 2712 } 2713 2714 /* 2715 * If a specific file object occupies a specific file descriptor, close the 2716 * file descriptor entry and drop a reference on the file object. This is a 2717 * convenience function to handle a subsequent error in a function that calls 2718 * falloc() that handles the race that another thread might have closed the 2719 * file descriptor out from under the thread creating the file object. 2720 */ 2721 void 2722 fdclose(struct thread *td, struct file *fp, int idx) 2723 { 2724 struct filedesc *fdp = td->td_proc->p_fd; 2725 2726 FILEDESC_XLOCK(fdp); 2727 if (fdp->fd_ofiles[idx].fde_file == fp) { 2728 fdfree(fdp, idx); 2729 FILEDESC_XUNLOCK(fdp); 2730 fdrop(fp, td); 2731 } else 2732 FILEDESC_XUNLOCK(fdp); 2733 } 2734 2735 /* 2736 * Close any files on exec? 2737 */ 2738 void 2739 fdcloseexec(struct thread *td) 2740 { 2741 struct filedesc *fdp; 2742 struct filedescent *fde; 2743 struct file *fp; 2744 int i; 2745 2746 fdp = td->td_proc->p_fd; 2747 KASSERT(refcount_load(&fdp->fd_refcnt) == 1, 2748 ("the fdtable should not be shared")); 2749 FILEDESC_FOREACH_FDE(fdp, i, fde) { 2750 fp = fde->fde_file; 2751 if (fp->f_type == DTYPE_MQUEUE || 2752 (fde->fde_flags & UF_EXCLOSE)) { 2753 FILEDESC_XLOCK(fdp); 2754 fdfree(fdp, i); 2755 (void) closefp(fdp, i, fp, td, false, false); 2756 FILEDESC_UNLOCK_ASSERT(fdp); 2757 } else if (fde->fde_flags & UF_FOCLOSE) { 2758 /* 2759 * https://austingroupbugs.net/view.php?id=1851 2760 * FD_CLOFORK should not be preserved across exec 2761 */ 2762 fde->fde_flags &= ~UF_FOCLOSE; 2763 } 2764 } 2765 } 2766 2767 /* 2768 * It is unsafe for set[ug]id processes to be started with file 2769 * descriptors 0..2 closed, as these descriptors are given implicit 2770 * significance in the Standard C library. fdcheckstd() will create a 2771 * descriptor referencing /dev/null for each of stdin, stdout, and 2772 * stderr that is not already open. 2773 */ 2774 int 2775 fdcheckstd(struct thread *td) 2776 { 2777 struct filedesc *fdp; 2778 register_t save; 2779 int i, error, devnull; 2780 2781 fdp = td->td_proc->p_fd; 2782 KASSERT(refcount_load(&fdp->fd_refcnt) == 1, 2783 ("the fdtable should not be shared")); 2784 MPASS(fdp->fd_nfiles >= 3); 2785 devnull = -1; 2786 for (i = 0; i <= 2; i++) { 2787 if (fdp->fd_ofiles[i].fde_file != NULL) 2788 continue; 2789 2790 save = td->td_retval[0]; 2791 if (devnull != -1) { 2792 error = kern_dup(td, FDDUP_FIXED, 0, devnull, i); 2793 } else { 2794 error = kern_openat(td, AT_FDCWD, "/dev/null", 2795 UIO_SYSSPACE, O_RDWR, 0); 2796 if (error == 0) { 2797 devnull = td->td_retval[0]; 2798 KASSERT(devnull == i, ("we didn't get our fd")); 2799 } 2800 } 2801 td->td_retval[0] = save; 2802 if (error != 0) 2803 return (error); 2804 } 2805 return (0); 2806 } 2807 2808 /* 2809 * Internal form of close. Decrement reference count on file structure. 2810 * Note: td may be NULL when closing a file that was being passed in a 2811 * message. 2812 */ 2813 int 2814 closef(struct file *fp, struct thread *td) 2815 { 2816 struct vnode *vp; 2817 struct flock lf; 2818 struct filedesc_to_leader *fdtol; 2819 struct filedesc *fdp; 2820 2821 MPASS(td != NULL); 2822 2823 /* 2824 * POSIX record locking dictates that any close releases ALL 2825 * locks owned by this process. This is handled by setting 2826 * a flag in the unlock to free ONLY locks obeying POSIX 2827 * semantics, and not to free BSD-style file locks. 2828 * If the descriptor was in a message, POSIX-style locks 2829 * aren't passed with the descriptor, and the thread pointer 2830 * will be NULL. Callers should be careful only to pass a 2831 * NULL thread pointer when there really is no owning 2832 * context that might have locks, or the locks will be 2833 * leaked. 2834 */ 2835 if (fp->f_type == DTYPE_VNODE) { 2836 vp = fp->f_vnode; 2837 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) { 2838 lf.l_whence = SEEK_SET; 2839 lf.l_start = 0; 2840 lf.l_len = 0; 2841 lf.l_type = F_UNLCK; 2842 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader, 2843 F_UNLCK, &lf, F_POSIX); 2844 } 2845 fdtol = td->td_proc->p_fdtol; 2846 if (fdtol != NULL) { 2847 /* 2848 * Handle special case where file descriptor table is 2849 * shared between multiple process leaders. 2850 */ 2851 fdp = td->td_proc->p_fd; 2852 FILEDESC_XLOCK(fdp); 2853 for (fdtol = fdtol->fdl_next; 2854 fdtol != td->td_proc->p_fdtol; 2855 fdtol = fdtol->fdl_next) { 2856 if ((fdtol->fdl_leader->p_flag & 2857 P_ADVLOCK) == 0) 2858 continue; 2859 fdtol->fdl_holdcount++; 2860 FILEDESC_XUNLOCK(fdp); 2861 lf.l_whence = SEEK_SET; 2862 lf.l_start = 0; 2863 lf.l_len = 0; 2864 lf.l_type = F_UNLCK; 2865 vp = fp->f_vnode; 2866 (void) VOP_ADVLOCK(vp, 2867 (caddr_t)fdtol->fdl_leader, F_UNLCK, &lf, 2868 F_POSIX); 2869 FILEDESC_XLOCK(fdp); 2870 fdtol->fdl_holdcount--; 2871 if (fdtol->fdl_holdcount == 0 && 2872 fdtol->fdl_wakeup != 0) { 2873 fdtol->fdl_wakeup = 0; 2874 wakeup(fdtol); 2875 } 2876 } 2877 FILEDESC_XUNLOCK(fdp); 2878 } 2879 } 2880 return (fdrop_close(fp, td)); 2881 } 2882 2883 /* 2884 * Hack for file descriptor passing code. 2885 */ 2886 void 2887 closef_nothread(struct file *fp) 2888 { 2889 2890 fdrop(fp, NULL); 2891 } 2892 2893 /* 2894 * Initialize the file pointer with the specified properties. 2895 * 2896 * The ops are set with release semantics to be certain that the flags, type, 2897 * and data are visible when ops is. This is to prevent ops methods from being 2898 * called with bad data. 2899 */ 2900 void 2901 finit(struct file *fp, u_int flag, short type, void *data, 2902 const struct fileops *ops) 2903 { 2904 fp->f_data = data; 2905 fp->f_flag = flag; 2906 fp->f_type = type; 2907 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops); 2908 } 2909 2910 void 2911 finit_vnode(struct file *fp, u_int flag, void *data, const struct fileops *ops) 2912 { 2913 fp->f_seqcount[UIO_READ] = 1; 2914 fp->f_seqcount[UIO_WRITE] = 1; 2915 finit(fp, (flag & FMASK) | (fp->f_flag & FHASLOCK), DTYPE_VNODE, 2916 data, ops); 2917 } 2918 2919 int 2920 fget_cap_noref(struct filedesc *fdp, int fd, const cap_rights_t *needrightsp, 2921 struct file **fpp, struct filecaps *havecapsp) 2922 { 2923 struct filedescent *fde; 2924 int error; 2925 2926 FILEDESC_LOCK_ASSERT(fdp); 2927 2928 *fpp = NULL; 2929 fde = fdeget_noref(fdp, fd); 2930 if (fde == NULL) { 2931 error = EBADF; 2932 goto out; 2933 } 2934 2935 #ifdef CAPABILITIES 2936 error = cap_check(cap_rights_fde_inline(fde), needrightsp); 2937 if (error != 0) 2938 goto out; 2939 #endif 2940 2941 if (havecapsp != NULL) 2942 filecaps_copy(&fde->fde_caps, havecapsp, true); 2943 2944 *fpp = fde->fde_file; 2945 2946 error = 0; 2947 out: 2948 return (error); 2949 } 2950 2951 #ifdef CAPABILITIES 2952 int 2953 fget_cap(struct thread *td, int fd, const cap_rights_t *needrightsp, 2954 uint8_t *flagsp, struct file **fpp, struct filecaps *havecapsp) 2955 { 2956 struct filedesc *fdp = td->td_proc->p_fd; 2957 int error; 2958 struct file *fp; 2959 seqc_t seq; 2960 2961 *fpp = NULL; 2962 for (;;) { 2963 error = fget_unlocked_seq(td, fd, needrightsp, flagsp, &fp, 2964 &seq); 2965 if (error != 0) 2966 return (error); 2967 2968 if (havecapsp != NULL) { 2969 if (!filecaps_copy(&fdp->fd_ofiles[fd].fde_caps, 2970 havecapsp, false)) { 2971 fdrop(fp, td); 2972 goto get_locked; 2973 } 2974 } 2975 2976 if (!fd_modified(fdp, fd, seq)) 2977 break; 2978 fdrop(fp, td); 2979 } 2980 2981 *fpp = fp; 2982 return (0); 2983 2984 get_locked: 2985 FILEDESC_SLOCK(fdp); 2986 error = fget_cap_noref(fdp, fd, needrightsp, fpp, havecapsp); 2987 if (error == 0 && !fhold(*fpp)) 2988 error = EBADF; 2989 FILEDESC_SUNLOCK(fdp); 2990 return (error); 2991 } 2992 #else 2993 int 2994 fget_cap(struct thread *td, int fd, const cap_rights_t *needrightsp, 2995 uint8_t *flagsp, struct file **fpp, struct filecaps *havecapsp) 2996 { 2997 int error; 2998 error = fget_unlocked_flags(td, fd, needrightsp, flagsp, fpp); 2999 if (havecapsp != NULL && error == 0) 3000 filecaps_fill(havecapsp); 3001 3002 return (error); 3003 } 3004 #endif 3005 3006 int 3007 fget_remote(struct thread *td, struct proc *p, int fd, struct file **fpp) 3008 { 3009 struct filedesc *fdp; 3010 struct file *fp; 3011 int error; 3012 3013 if (p == td->td_proc) /* curproc */ 3014 return (fget_unlocked(td, fd, &cap_no_rights, fpp)); 3015 3016 PROC_LOCK(p); 3017 fdp = fdhold(p); 3018 PROC_UNLOCK(p); 3019 if (fdp == NULL) 3020 return (ENOENT); 3021 FILEDESC_SLOCK(fdp); 3022 if (refcount_load(&fdp->fd_refcnt) != 0) { 3023 fp = fget_noref(fdp, fd); 3024 if (fp != NULL && fhold(fp)) { 3025 *fpp = fp; 3026 error = 0; 3027 } else { 3028 error = EBADF; 3029 } 3030 } else { 3031 error = ENOENT; 3032 } 3033 FILEDESC_SUNLOCK(fdp); 3034 fddrop(fdp); 3035 return (error); 3036 } 3037 3038 int 3039 fget_remote_foreach(struct thread *td, struct proc *p, 3040 int (*fn)(struct proc *, int, struct file *, void *), void *arg) 3041 { 3042 struct filedesc *fdp; 3043 struct fdescenttbl *fdt; 3044 struct file *fp; 3045 int error, error1, fd, highfd; 3046 3047 error = 0; 3048 PROC_LOCK(p); 3049 fdp = fdhold(p); 3050 PROC_UNLOCK(p); 3051 if (fdp == NULL) 3052 return (ENOENT); 3053 3054 FILEDESC_SLOCK(fdp); 3055 if (refcount_load(&fdp->fd_refcnt) != 0) { 3056 fdt = atomic_load_ptr(&fdp->fd_files); 3057 highfd = fdt->fdt_nfiles - 1; 3058 FILEDESC_SUNLOCK(fdp); 3059 } else { 3060 error = ENOENT; 3061 FILEDESC_SUNLOCK(fdp); 3062 goto out; 3063 } 3064 3065 for (fd = 0; fd <= highfd; fd++) { 3066 error1 = fget_remote(td, p, fd, &fp); 3067 if (error1 != 0) 3068 continue; 3069 error = fn(p, fd, fp, arg); 3070 fdrop(fp, td); 3071 if (error != 0) 3072 break; 3073 } 3074 out: 3075 fddrop(fdp); 3076 return (error); 3077 } 3078 3079 #ifdef CAPABILITIES 3080 int 3081 fgetvp_lookup_smr(struct nameidata *ndp, struct vnode **vpp, int *flagsp) 3082 { 3083 const struct filedescent *fde; 3084 const struct fdescenttbl *fdt; 3085 struct filedesc *fdp; 3086 struct file *fp; 3087 struct vnode *vp; 3088 const cap_rights_t *haverights; 3089 cap_rights_t rights; 3090 seqc_t seq; 3091 int fd, flags; 3092 3093 VFS_SMR_ASSERT_ENTERED(); 3094 3095 fd = ndp->ni_dirfd; 3096 rights = *ndp->ni_rightsneeded; 3097 cap_rights_set_one(&rights, CAP_LOOKUP); 3098 3099 fdp = curproc->p_fd; 3100 fdt = fdp->fd_files; 3101 if (__predict_false((u_int)fd >= fdt->fdt_nfiles)) 3102 return (EBADF); 3103 seq = seqc_read_notmodify(fd_seqc(fdt, fd)); 3104 fde = &fdt->fdt_ofiles[fd]; 3105 haverights = cap_rights_fde_inline(fde); 3106 fp = fde->fde_file; 3107 if (__predict_false(fp == NULL)) 3108 return (EAGAIN); 3109 if (__predict_false(cap_check_inline_transient(haverights, &rights))) 3110 return (EAGAIN); 3111 flags = fp->f_flag & FSEARCH; 3112 flags |= (fde->fde_flags & UF_RESOLVE_BENEATH) != 0 ? 3113 O_RESOLVE_BENEATH : 0; 3114 vp = fp->f_vnode; 3115 if (__predict_false(vp == NULL)) { 3116 return (EAGAIN); 3117 } 3118 if (!filecaps_copy(&fde->fde_caps, &ndp->ni_filecaps, false)) { 3119 return (EAGAIN); 3120 } 3121 /* 3122 * Use an acquire barrier to force re-reading of fdt so it is 3123 * refreshed for verification. 3124 */ 3125 atomic_thread_fence_acq(); 3126 fdt = fdp->fd_files; 3127 if (__predict_false(!seqc_consistent_no_fence(fd_seqc(fdt, fd), seq))) 3128 return (EAGAIN); 3129 /* 3130 * If file descriptor doesn't have all rights, 3131 * all lookups relative to it must also be 3132 * strictly relative. 3133 * 3134 * Not yet supported by fast path. 3135 */ 3136 CAP_ALL(&rights); 3137 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights, &rights) || 3138 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL || 3139 ndp->ni_filecaps.fc_nioctls != -1) { 3140 #ifdef notyet 3141 ndp->ni_lcf |= NI_LCF_STRICTREL; 3142 #else 3143 return (EAGAIN); 3144 #endif 3145 } 3146 *vpp = vp; 3147 *flagsp = flags; 3148 return (0); 3149 } 3150 #else 3151 int 3152 fgetvp_lookup_smr(struct nameidata *ndp, struct vnode **vpp, int *flagsp) 3153 { 3154 const struct filedescent *fde; 3155 const struct fdescenttbl *fdt; 3156 struct filedesc *fdp; 3157 struct file *fp; 3158 struct vnode *vp; 3159 int fd, flags; 3160 3161 VFS_SMR_ASSERT_ENTERED(); 3162 3163 fd = ndp->ni_dirfd; 3164 fdp = curproc->p_fd; 3165 fdt = fdp->fd_files; 3166 if (__predict_false((u_int)fd >= fdt->fdt_nfiles)) 3167 return (EBADF); 3168 fde = &fdt->fdt_ofiles[fd]; 3169 fp = fde->fde_file; 3170 if (__predict_false(fp == NULL)) 3171 return (EAGAIN); 3172 flags = fp->f_flag & FSEARCH; 3173 flags |= (fde->fde_flags & UF_RESOLVE_BENEATH) != 0 ? 3174 O_RESOLVE_BENEATH : 0; 3175 vp = fp->f_vnode; 3176 if (__predict_false(vp == NULL || vp->v_type != VDIR)) { 3177 return (EAGAIN); 3178 } 3179 /* 3180 * Use an acquire barrier to force re-reading of fdt so it is 3181 * refreshed for verification. 3182 */ 3183 atomic_thread_fence_acq(); 3184 fdt = fdp->fd_files; 3185 if (__predict_false(fp != fdt->fdt_ofiles[fd].fde_file)) 3186 return (EAGAIN); 3187 filecaps_fill(&ndp->ni_filecaps); 3188 *vpp = vp; 3189 *flagsp = flags; 3190 return (0); 3191 } 3192 #endif 3193 3194 int 3195 fgetvp_lookup(struct nameidata *ndp, struct vnode **vpp) 3196 { 3197 struct thread *td; 3198 struct file *fp; 3199 struct vnode *vp; 3200 struct componentname *cnp; 3201 cap_rights_t rights; 3202 int error; 3203 uint8_t flags; 3204 3205 td = curthread; 3206 rights = *ndp->ni_rightsneeded; 3207 cap_rights_set_one(&rights, CAP_LOOKUP); 3208 cnp = &ndp->ni_cnd; 3209 3210 error = fget_cap(td, ndp->ni_dirfd, &rights, &flags, &fp, 3211 &ndp->ni_filecaps); 3212 if (__predict_false(error != 0)) 3213 return (error); 3214 if (__predict_false(fp->f_ops == &badfileops)) { 3215 error = EBADF; 3216 goto out_free; 3217 } 3218 vp = fp->f_vnode; 3219 if (__predict_false(vp == NULL)) { 3220 error = ENOTDIR; 3221 goto out_free; 3222 } 3223 vrefact(vp); 3224 /* 3225 * XXX does not check for VDIR, handled by namei_setup 3226 */ 3227 if ((fp->f_flag & FSEARCH) != 0) 3228 cnp->cn_flags |= NOEXECCHECK; 3229 if ((flags & UF_RESOLVE_BENEATH) != 0) { 3230 cnp->cn_flags |= RBENEATH; 3231 ndp->ni_resflags |= NIRES_BENEATH; 3232 } 3233 fdrop(fp, td); 3234 3235 #ifdef CAPABILITIES 3236 /* 3237 * If file descriptor doesn't have all rights, 3238 * all lookups relative to it must also be 3239 * strictly relative. 3240 */ 3241 CAP_ALL(&rights); 3242 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights, &rights) || 3243 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL || 3244 ndp->ni_filecaps.fc_nioctls != -1) { 3245 ndp->ni_lcf |= NI_LCF_STRICTREL; 3246 ndp->ni_resflags |= NIRES_STRICTREL; 3247 } 3248 #endif 3249 3250 /* 3251 * TODO: avoid copying ioctl caps if it can be helped to begin with 3252 */ 3253 if ((cnp->cn_flags & WANTIOCTLCAPS) == 0) 3254 filecaps_free_ioctl(&ndp->ni_filecaps); 3255 3256 *vpp = vp; 3257 return (0); 3258 3259 out_free: 3260 filecaps_free(&ndp->ni_filecaps); 3261 fdrop(fp, td); 3262 return (error); 3263 } 3264 3265 /* 3266 * Fetch the descriptor locklessly. 3267 * 3268 * We avoid fdrop() races by never raising a refcount above 0. To accomplish 3269 * this we have to use a cmpset loop rather than an atomic_add. The descriptor 3270 * must be re-verified once we acquire a reference to be certain that the 3271 * identity is still correct and we did not lose a race due to preemption. 3272 * 3273 * Force a reload of fdt when looping. Another thread could reallocate 3274 * the table before this fd was closed, so it is possible that there is 3275 * a stale fp pointer in cached version. 3276 */ 3277 #ifdef CAPABILITIES 3278 static int 3279 fget_unlocked_seq(struct thread *td, int fd, const cap_rights_t *needrightsp, 3280 uint8_t *flagsp, struct file **fpp, seqc_t *seqp) 3281 { 3282 struct filedesc *fdp; 3283 const struct filedescent *fde; 3284 const struct fdescenttbl *fdt; 3285 struct file *fp; 3286 seqc_t seq; 3287 cap_rights_t haverights; 3288 int error; 3289 uint8_t flags; 3290 3291 fdp = td->td_proc->p_fd; 3292 fdt = fdp->fd_files; 3293 if (__predict_false((u_int)fd >= fdt->fdt_nfiles)) 3294 return (EBADF); 3295 3296 for (;;) { 3297 seq = seqc_read_notmodify(fd_seqc(fdt, fd)); 3298 fde = &fdt->fdt_ofiles[fd]; 3299 haverights = *cap_rights_fde_inline(fde); 3300 fp = fde->fde_file; 3301 flags = fde->fde_flags; 3302 if (__predict_false(fp == NULL)) { 3303 if (seqc_consistent(fd_seqc(fdt, fd), seq)) 3304 return (EBADF); 3305 fdt = atomic_load_ptr(&fdp->fd_files); 3306 continue; 3307 } 3308 error = cap_check_inline(&haverights, needrightsp); 3309 if (__predict_false(error != 0)) { 3310 if (seqc_consistent(fd_seqc(fdt, fd), seq)) 3311 return (error); 3312 fdt = atomic_load_ptr(&fdp->fd_files); 3313 continue; 3314 } 3315 if (__predict_false(!refcount_acquire_if_not_zero(&fp->f_count))) { 3316 fdt = atomic_load_ptr(&fdp->fd_files); 3317 continue; 3318 } 3319 /* 3320 * Use an acquire barrier to force re-reading of fdt so it is 3321 * refreshed for verification. 3322 */ 3323 atomic_thread_fence_acq(); 3324 fdt = fdp->fd_files; 3325 if (seqc_consistent_no_fence(fd_seqc(fdt, fd), seq)) 3326 break; 3327 fdrop(fp, td); 3328 } 3329 *fpp = fp; 3330 if (flagsp != NULL) 3331 *flagsp = flags; 3332 if (seqp != NULL) 3333 *seqp = seq; 3334 return (0); 3335 } 3336 #else 3337 static int 3338 fget_unlocked_seq(struct thread *td, int fd, const cap_rights_t *needrightsp, 3339 uint8_t *flagsp, struct file **fpp, seqc_t *seqp __unused) 3340 { 3341 struct filedesc *fdp; 3342 const struct fdescenttbl *fdt; 3343 struct file *fp; 3344 uint8_t flags; 3345 3346 fdp = td->td_proc->p_fd; 3347 fdt = fdp->fd_files; 3348 if (__predict_false((u_int)fd >= fdt->fdt_nfiles)) 3349 return (EBADF); 3350 3351 for (;;) { 3352 fp = fdt->fdt_ofiles[fd].fde_file; 3353 flags = fdt->fdt_ofiles[fd].fde_flags; 3354 if (__predict_false(fp == NULL)) 3355 return (EBADF); 3356 if (__predict_false(!refcount_acquire_if_not_zero(&fp->f_count))) { 3357 fdt = atomic_load_ptr(&fdp->fd_files); 3358 continue; 3359 } 3360 /* 3361 * Use an acquire barrier to force re-reading of fdt so it is 3362 * refreshed for verification. 3363 */ 3364 atomic_thread_fence_acq(); 3365 fdt = fdp->fd_files; 3366 if (__predict_true(fp == fdt->fdt_ofiles[fd].fde_file)) 3367 break; 3368 fdrop(fp, td); 3369 } 3370 if (flagsp != NULL) 3371 *flagsp = flags; 3372 *fpp = fp; 3373 return (0); 3374 } 3375 #endif 3376 3377 /* 3378 * See the comments in fget_unlocked_seq for an explanation of how this works. 3379 * 3380 * This is a simplified variant which bails out to the aforementioned routine 3381 * if anything goes wrong. In practice this only happens when userspace is 3382 * racing with itself. 3383 */ 3384 int 3385 fget_unlocked_flags(struct thread *td, int fd, const cap_rights_t *needrightsp, 3386 uint8_t *flagsp, struct file **fpp) 3387 { 3388 struct filedesc *fdp; 3389 #ifdef CAPABILITIES 3390 const struct filedescent *fde; 3391 #endif 3392 const struct fdescenttbl *fdt; 3393 struct file *fp; 3394 #ifdef CAPABILITIES 3395 seqc_t seq; 3396 const cap_rights_t *haverights; 3397 #endif 3398 uint8_t flags; 3399 3400 fdp = td->td_proc->p_fd; 3401 fdt = fdp->fd_files; 3402 if (__predict_false((u_int)fd >= fdt->fdt_nfiles)) { 3403 *fpp = NULL; 3404 return (EBADF); 3405 } 3406 #ifdef CAPABILITIES 3407 seq = seqc_read_notmodify(fd_seqc(fdt, fd)); 3408 fde = &fdt->fdt_ofiles[fd]; 3409 haverights = cap_rights_fde_inline(fde); 3410 fp = fde->fde_file; 3411 flags = fde->fde_flags; 3412 #else 3413 fp = fdt->fdt_ofiles[fd].fde_file; 3414 flags = fdt->fdt_ofiles[fd].fde_flags; 3415 #endif 3416 if (__predict_false(fp == NULL)) 3417 goto out_fallback; 3418 #ifdef CAPABILITIES 3419 if (__predict_false(cap_check_inline_transient(haverights, needrightsp))) 3420 goto out_fallback; 3421 #endif 3422 if (__predict_false(!refcount_acquire_if_not_zero(&fp->f_count))) 3423 goto out_fallback; 3424 3425 /* 3426 * Use an acquire barrier to force re-reading of fdt so it is 3427 * refreshed for verification. 3428 */ 3429 atomic_thread_fence_acq(); 3430 fdt = fdp->fd_files; 3431 #ifdef CAPABILITIES 3432 if (__predict_false(!seqc_consistent_no_fence(fd_seqc(fdt, fd), seq))) 3433 #else 3434 if (__predict_false(fp != fdt->fdt_ofiles[fd].fde_file)) 3435 #endif 3436 goto out_fdrop; 3437 *fpp = fp; 3438 if (flagsp != NULL) 3439 *flagsp = flags; 3440 return (0); 3441 out_fdrop: 3442 fdrop(fp, td); 3443 out_fallback: 3444 *fpp = NULL; 3445 return (fget_unlocked_seq(td, fd, needrightsp, flagsp, fpp, NULL)); 3446 } 3447 3448 int 3449 fget_unlocked(struct thread *td, int fd, const cap_rights_t *needrightsp, 3450 struct file **fpp) 3451 { 3452 return (fget_unlocked_flags(td, fd, needrightsp, NULL, fpp)); 3453 } 3454 3455 /* 3456 * Translate fd -> file when the caller guarantees the file descriptor table 3457 * can't be changed by others. 3458 * 3459 * Note this does not mean the file object itself is only visible to the caller, 3460 * merely that it wont disappear without having to be referenced. 3461 * 3462 * Must be paired with fput_only_user. 3463 */ 3464 #ifdef CAPABILITIES 3465 int 3466 fget_only_user(struct filedesc *fdp, int fd, const cap_rights_t *needrightsp, 3467 struct file **fpp) 3468 { 3469 const struct filedescent *fde; 3470 const struct fdescenttbl *fdt; 3471 const cap_rights_t *haverights; 3472 struct file *fp; 3473 int error; 3474 3475 MPASS(FILEDESC_IS_ONLY_USER(fdp)); 3476 3477 *fpp = NULL; 3478 if (__predict_false(fd >= fdp->fd_nfiles)) 3479 return (EBADF); 3480 3481 fdt = fdp->fd_files; 3482 fde = &fdt->fdt_ofiles[fd]; 3483 fp = fde->fde_file; 3484 if (__predict_false(fp == NULL)) 3485 return (EBADF); 3486 MPASS(refcount_load(&fp->f_count) > 0); 3487 haverights = cap_rights_fde_inline(fde); 3488 error = cap_check_inline(haverights, needrightsp); 3489 if (__predict_false(error != 0)) 3490 return (error); 3491 *fpp = fp; 3492 return (0); 3493 } 3494 #else 3495 int 3496 fget_only_user(struct filedesc *fdp, int fd, const cap_rights_t *needrightsp, 3497 struct file **fpp) 3498 { 3499 struct file *fp; 3500 3501 MPASS(FILEDESC_IS_ONLY_USER(fdp)); 3502 3503 *fpp = NULL; 3504 if (__predict_false(fd >= fdp->fd_nfiles)) 3505 return (EBADF); 3506 3507 fp = fdp->fd_ofiles[fd].fde_file; 3508 if (__predict_false(fp == NULL)) 3509 return (EBADF); 3510 3511 MPASS(refcount_load(&fp->f_count) > 0); 3512 *fpp = fp; 3513 return (0); 3514 } 3515 #endif 3516 3517 /* 3518 * Extract the file pointer associated with the specified descriptor for the 3519 * current user process. 3520 * 3521 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is 3522 * returned. 3523 * 3524 * File's rights will be checked against the capability rights mask. 3525 * 3526 * If an error occurred the non-zero error is returned and *fpp is set to 3527 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is 3528 * responsible for fdrop(). 3529 */ 3530 static __inline int 3531 _fget(struct thread *td, int fd, struct file **fpp, int flags, 3532 const cap_rights_t *needrightsp) 3533 { 3534 struct file *fp; 3535 int error; 3536 3537 *fpp = NULL; 3538 error = fget_unlocked(td, fd, needrightsp, &fp); 3539 if (__predict_false(error != 0)) 3540 return (error); 3541 if (__predict_false(fp->f_ops == &badfileops)) { 3542 fdrop(fp, td); 3543 return (EBADF); 3544 } 3545 3546 /* 3547 * FREAD and FWRITE failure return EBADF as per POSIX. 3548 */ 3549 error = 0; 3550 switch (flags) { 3551 case FREAD: 3552 case FWRITE: 3553 if ((fp->f_flag & flags) == 0) 3554 error = EBADF; 3555 break; 3556 case FEXEC: 3557 if (fp->f_ops != &path_fileops && 3558 ((fp->f_flag & (FREAD | FEXEC)) == 0 || 3559 (fp->f_flag & FWRITE) != 0)) 3560 error = EBADF; 3561 break; 3562 case 0: 3563 break; 3564 default: 3565 KASSERT(0, ("wrong flags")); 3566 } 3567 3568 if (error != 0) { 3569 fdrop(fp, td); 3570 return (error); 3571 } 3572 3573 *fpp = fp; 3574 return (0); 3575 } 3576 3577 int 3578 fget(struct thread *td, int fd, const cap_rights_t *rightsp, struct file **fpp) 3579 { 3580 3581 return (_fget(td, fd, fpp, 0, rightsp)); 3582 } 3583 3584 int 3585 fget_mmap(struct thread *td, int fd, const cap_rights_t *rightsp, 3586 vm_prot_t *maxprotp, struct file **fpp) 3587 { 3588 int error; 3589 #ifndef CAPABILITIES 3590 error = _fget(td, fd, fpp, 0, rightsp); 3591 if (maxprotp != NULL) 3592 *maxprotp = VM_PROT_ALL; 3593 return (error); 3594 #else 3595 cap_rights_t fdrights; 3596 struct filedesc *fdp; 3597 struct file *fp; 3598 seqc_t seq; 3599 3600 *fpp = NULL; 3601 fdp = td->td_proc->p_fd; 3602 MPASS(cap_rights_is_set(rightsp, CAP_MMAP)); 3603 for (;;) { 3604 error = fget_unlocked_seq(td, fd, rightsp, NULL, &fp, &seq); 3605 if (__predict_false(error != 0)) 3606 return (error); 3607 if (__predict_false(fp->f_ops == &badfileops)) { 3608 fdrop(fp, td); 3609 return (EBADF); 3610 } 3611 if (maxprotp != NULL) 3612 fdrights = *cap_rights(fdp, fd); 3613 if (!fd_modified(fdp, fd, seq)) 3614 break; 3615 fdrop(fp, td); 3616 } 3617 3618 /* 3619 * If requested, convert capability rights to access flags. 3620 */ 3621 if (maxprotp != NULL) 3622 *maxprotp = cap_rights_to_vmprot(&fdrights); 3623 *fpp = fp; 3624 return (0); 3625 #endif 3626 } 3627 3628 int 3629 fget_read(struct thread *td, int fd, const cap_rights_t *rightsp, 3630 struct file **fpp) 3631 { 3632 3633 return (_fget(td, fd, fpp, FREAD, rightsp)); 3634 } 3635 3636 int 3637 fget_write(struct thread *td, int fd, const cap_rights_t *rightsp, 3638 struct file **fpp) 3639 { 3640 3641 return (_fget(td, fd, fpp, FWRITE, rightsp)); 3642 } 3643 3644 int 3645 fget_fcntl(struct thread *td, int fd, const cap_rights_t *rightsp, 3646 int needfcntl, struct file **fpp) 3647 { 3648 #ifndef CAPABILITIES 3649 return (fget_unlocked(td, fd, rightsp, fpp)); 3650 #else 3651 struct filedesc *fdp = td->td_proc->p_fd; 3652 struct file *fp; 3653 int error; 3654 seqc_t seq; 3655 3656 *fpp = NULL; 3657 MPASS(cap_rights_is_set(rightsp, CAP_FCNTL)); 3658 for (;;) { 3659 error = fget_unlocked_seq(td, fd, rightsp, NULL, &fp, &seq); 3660 if (error != 0) 3661 return (error); 3662 error = cap_fcntl_check(fdp, fd, needfcntl); 3663 if (!fd_modified(fdp, fd, seq)) 3664 break; 3665 fdrop(fp, td); 3666 } 3667 if (error != 0) { 3668 fdrop(fp, td); 3669 return (error); 3670 } 3671 *fpp = fp; 3672 return (0); 3673 #endif 3674 } 3675 3676 /* 3677 * Like fget() but loads the underlying vnode, or returns an error if the 3678 * descriptor does not represent a vnode. Note that pipes use vnodes but 3679 * never have VM objects. The returned vnode will be vref()'d. 3680 * 3681 * XXX: what about the unused flags ? 3682 */ 3683 static __inline int 3684 _fgetvp(struct thread *td, int fd, int flags, const cap_rights_t *needrightsp, 3685 struct vnode **vpp) 3686 { 3687 struct file *fp; 3688 int error; 3689 3690 *vpp = NULL; 3691 error = _fget(td, fd, &fp, flags, needrightsp); 3692 if (error != 0) 3693 return (error); 3694 if (fp->f_vnode == NULL) { 3695 error = EINVAL; 3696 } else { 3697 *vpp = fp->f_vnode; 3698 vrefact(*vpp); 3699 } 3700 fdrop(fp, td); 3701 3702 return (error); 3703 } 3704 3705 int 3706 fgetvp(struct thread *td, int fd, const cap_rights_t *rightsp, 3707 struct vnode **vpp) 3708 { 3709 3710 return (_fgetvp(td, fd, 0, rightsp, vpp)); 3711 } 3712 3713 int 3714 fgetvp_rights(struct thread *td, int fd, const cap_rights_t *needrightsp, 3715 struct filecaps *havecaps, struct vnode **vpp) 3716 { 3717 struct filecaps caps; 3718 struct file *fp; 3719 int error; 3720 3721 error = fget_cap(td, fd, needrightsp, NULL, &fp, &caps); 3722 if (error != 0) 3723 return (error); 3724 if (fp->f_ops == &badfileops) { 3725 error = EBADF; 3726 goto out; 3727 } 3728 if (fp->f_vnode == NULL) { 3729 error = EINVAL; 3730 goto out; 3731 } 3732 3733 *havecaps = caps; 3734 *vpp = fp->f_vnode; 3735 vrefact(*vpp); 3736 fdrop(fp, td); 3737 3738 return (0); 3739 out: 3740 filecaps_free(&caps); 3741 fdrop(fp, td); 3742 return (error); 3743 } 3744 3745 int 3746 fgetvp_read(struct thread *td, int fd, const cap_rights_t *rightsp, 3747 struct vnode **vpp) 3748 { 3749 3750 return (_fgetvp(td, fd, FREAD, rightsp, vpp)); 3751 } 3752 3753 int 3754 fgetvp_exec(struct thread *td, int fd, const cap_rights_t *rightsp, 3755 struct vnode **vpp) 3756 { 3757 3758 return (_fgetvp(td, fd, FEXEC, rightsp, vpp)); 3759 } 3760 3761 #ifdef notyet 3762 int 3763 fgetvp_write(struct thread *td, int fd, const cap_rights_t *rightsp, 3764 struct vnode **vpp) 3765 { 3766 3767 return (_fgetvp(td, fd, FWRITE, rightsp, vpp)); 3768 } 3769 #endif 3770 3771 /* 3772 * Handle the last reference to a file being closed. 3773 * 3774 * Without the noinline attribute clang keeps inlining the func thorough this 3775 * file when fdrop is used. 3776 */ 3777 int __noinline 3778 _fdrop(struct file *fp, struct thread *td) 3779 { 3780 int error; 3781 3782 KASSERT(refcount_load(&fp->f_count) == 0, 3783 ("fdrop: fp %p count %d", fp, refcount_load(&fp->f_count))); 3784 3785 error = fo_close(fp, td); 3786 atomic_subtract_int(&openfiles, 1); 3787 crfree(fp->f_cred); 3788 free(fp->f_advice, M_FADVISE); 3789 uma_zfree(file_zone, fp); 3790 3791 return (error); 3792 } 3793 3794 /* 3795 * Apply an advisory lock on a file descriptor. 3796 * 3797 * Just attempt to get a record lock of the requested type on the entire file 3798 * (l_whence = SEEK_SET, l_start = 0, l_len = 0). 3799 */ 3800 #ifndef _SYS_SYSPROTO_H_ 3801 struct flock_args { 3802 int fd; 3803 int how; 3804 }; 3805 #endif 3806 /* ARGSUSED */ 3807 int 3808 sys_flock(struct thread *td, struct flock_args *uap) 3809 { 3810 struct file *fp; 3811 struct vnode *vp; 3812 struct flock lf; 3813 int error; 3814 3815 error = fget(td, uap->fd, &cap_flock_rights, &fp); 3816 if (error != 0) 3817 return (error); 3818 error = EOPNOTSUPP; 3819 if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) { 3820 goto done; 3821 } 3822 if (fp->f_ops == &path_fileops) { 3823 goto done; 3824 } 3825 3826 error = 0; 3827 vp = fp->f_vnode; 3828 lf.l_whence = SEEK_SET; 3829 lf.l_start = 0; 3830 lf.l_len = 0; 3831 if (uap->how & LOCK_UN) { 3832 lf.l_type = F_UNLCK; 3833 atomic_clear_int(&fp->f_flag, FHASLOCK); 3834 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK); 3835 goto done; 3836 } 3837 if (uap->how & LOCK_EX) 3838 lf.l_type = F_WRLCK; 3839 else if (uap->how & LOCK_SH) 3840 lf.l_type = F_RDLCK; 3841 else { 3842 error = EBADF; 3843 goto done; 3844 } 3845 atomic_set_int(&fp->f_flag, FHASLOCK); 3846 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, 3847 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT); 3848 done: 3849 fdrop(fp, td); 3850 return (error); 3851 } 3852 /* 3853 * Duplicate the specified descriptor to a free descriptor. 3854 */ 3855 int 3856 dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode, 3857 int openerror, int *indxp) 3858 { 3859 struct filedescent *newfde, *oldfde; 3860 struct file *fp; 3861 u_long *ioctls; 3862 int error, indx; 3863 3864 KASSERT(openerror == ENODEV || openerror == ENXIO, 3865 ("unexpected error %d in %s", openerror, __func__)); 3866 3867 /* 3868 * If the to-be-dup'd fd number is greater than the allowed number 3869 * of file descriptors, or the fd to be dup'd has already been 3870 * closed, then reject. 3871 */ 3872 FILEDESC_XLOCK(fdp); 3873 if ((fp = fget_noref(fdp, dfd)) == NULL) { 3874 FILEDESC_XUNLOCK(fdp); 3875 return (EBADF); 3876 } 3877 3878 error = fdalloc(td, 0, &indx); 3879 if (error != 0) { 3880 FILEDESC_XUNLOCK(fdp); 3881 return (error); 3882 } 3883 3884 /* 3885 * There are two cases of interest here. 3886 * 3887 * For ENODEV simply dup (dfd) to file descriptor (indx) and return. 3888 * 3889 * For ENXIO steal away the file structure from (dfd) and store it in 3890 * (indx). (dfd) is effectively closed by this operation. 3891 */ 3892 switch (openerror) { 3893 case ENODEV: 3894 /* 3895 * Check that the mode the file is being opened for is a 3896 * subset of the mode of the existing descriptor. 3897 */ 3898 if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) { 3899 fdunused(fdp, indx); 3900 FILEDESC_XUNLOCK(fdp); 3901 return (EACCES); 3902 } 3903 if (!fhold(fp)) { 3904 fdunused(fdp, indx); 3905 FILEDESC_XUNLOCK(fdp); 3906 return (EBADF); 3907 } 3908 newfde = &fdp->fd_ofiles[indx]; 3909 oldfde = &fdp->fd_ofiles[dfd]; 3910 ioctls = filecaps_copy_prep(&oldfde->fde_caps); 3911 #ifdef CAPABILITIES 3912 seqc_write_begin(&newfde->fde_seqc); 3913 #endif 3914 fde_copy(oldfde, newfde); 3915 filecaps_copy_finish(&oldfde->fde_caps, &newfde->fde_caps, 3916 ioctls); 3917 #ifdef CAPABILITIES 3918 seqc_write_end(&newfde->fde_seqc); 3919 #endif 3920 break; 3921 case ENXIO: 3922 /* 3923 * Steal away the file pointer from dfd and stuff it into indx. 3924 */ 3925 newfde = &fdp->fd_ofiles[indx]; 3926 oldfde = &fdp->fd_ofiles[dfd]; 3927 #ifdef CAPABILITIES 3928 seqc_write_begin(&oldfde->fde_seqc); 3929 seqc_write_begin(&newfde->fde_seqc); 3930 #endif 3931 fde_copy(oldfde, newfde); 3932 oldfde->fde_file = NULL; 3933 fdunused(fdp, dfd); 3934 #ifdef CAPABILITIES 3935 seqc_write_end(&newfde->fde_seqc); 3936 seqc_write_end(&oldfde->fde_seqc); 3937 #endif 3938 break; 3939 } 3940 FILEDESC_XUNLOCK(fdp); 3941 *indxp = indx; 3942 return (0); 3943 } 3944 3945 /* 3946 * This sysctl determines if we will allow a process to chroot(2) if it 3947 * has a directory open: 3948 * 0: disallowed for all processes. 3949 * 1: allowed for processes that were not already chroot(2)'ed. 3950 * 2: allowed for all processes. 3951 */ 3952 3953 static int chroot_allow_open_directories = 1; 3954 3955 SYSCTL_INT(_kern, OID_AUTO, chroot_allow_open_directories, CTLFLAG_RW, 3956 &chroot_allow_open_directories, 0, 3957 "Allow a process to chroot(2) if it has a directory open"); 3958 3959 /* 3960 * Helper function for raised chroot(2) security function: Refuse if 3961 * any filedescriptors are open directories. 3962 */ 3963 static int 3964 chroot_refuse_vdir_fds(struct filedesc *fdp) 3965 { 3966 struct vnode *vp; 3967 struct file *fp; 3968 int i; 3969 3970 FILEDESC_LOCK_ASSERT(fdp); 3971 3972 FILEDESC_FOREACH_FP(fdp, i, fp) { 3973 if (fp->f_type == DTYPE_VNODE) { 3974 vp = fp->f_vnode; 3975 if (vp->v_type == VDIR) 3976 return (EPERM); 3977 } 3978 } 3979 return (0); 3980 } 3981 3982 static void 3983 pwd_fill(struct pwd *oldpwd, struct pwd *newpwd) 3984 { 3985 3986 if (newpwd->pwd_cdir == NULL && oldpwd->pwd_cdir != NULL) { 3987 vrefact(oldpwd->pwd_cdir); 3988 newpwd->pwd_cdir = oldpwd->pwd_cdir; 3989 } 3990 3991 if (newpwd->pwd_rdir == NULL && oldpwd->pwd_rdir != NULL) { 3992 vrefact(oldpwd->pwd_rdir); 3993 newpwd->pwd_rdir = oldpwd->pwd_rdir; 3994 } 3995 3996 if (newpwd->pwd_jdir == NULL && oldpwd->pwd_jdir != NULL) { 3997 vrefact(oldpwd->pwd_jdir); 3998 newpwd->pwd_jdir = oldpwd->pwd_jdir; 3999 } 4000 4001 if (newpwd->pwd_adir == NULL && oldpwd->pwd_adir != NULL) { 4002 vrefact(oldpwd->pwd_adir); 4003 newpwd->pwd_adir = oldpwd->pwd_adir; 4004 } 4005 } 4006 4007 struct pwd * 4008 pwd_hold_pwddesc(struct pwddesc *pdp) 4009 { 4010 struct pwd *pwd; 4011 4012 PWDDESC_ASSERT_XLOCKED(pdp); 4013 pwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4014 if (pwd != NULL) 4015 refcount_acquire(&pwd->pwd_refcount); 4016 return (pwd); 4017 } 4018 4019 bool 4020 pwd_hold_smr(struct pwd *pwd) 4021 { 4022 4023 MPASS(pwd != NULL); 4024 if (__predict_true(refcount_acquire_if_not_zero(&pwd->pwd_refcount))) { 4025 return (true); 4026 } 4027 return (false); 4028 } 4029 4030 struct pwd * 4031 pwd_hold(struct thread *td) 4032 { 4033 struct pwddesc *pdp; 4034 struct pwd *pwd; 4035 4036 pdp = td->td_proc->p_pd; 4037 4038 vfs_smr_enter(); 4039 pwd = vfs_smr_entered_load(&pdp->pd_pwd); 4040 if (pwd_hold_smr(pwd)) { 4041 vfs_smr_exit(); 4042 return (pwd); 4043 } 4044 vfs_smr_exit(); 4045 PWDDESC_XLOCK(pdp); 4046 pwd = pwd_hold_pwddesc(pdp); 4047 MPASS(pwd != NULL); 4048 PWDDESC_XUNLOCK(pdp); 4049 return (pwd); 4050 } 4051 4052 struct pwd * 4053 pwd_hold_proc(struct proc *p) 4054 { 4055 struct pwddesc *pdp; 4056 struct pwd *pwd; 4057 4058 PROC_ASSERT_HELD(p); 4059 PROC_LOCK(p); 4060 pdp = pdhold(p); 4061 MPASS(pdp != NULL); 4062 PROC_UNLOCK(p); 4063 4064 PWDDESC_XLOCK(pdp); 4065 pwd = pwd_hold_pwddesc(pdp); 4066 MPASS(pwd != NULL); 4067 PWDDESC_XUNLOCK(pdp); 4068 pddrop(pdp); 4069 return (pwd); 4070 } 4071 4072 static struct pwd * 4073 pwd_alloc(void) 4074 { 4075 struct pwd *pwd; 4076 4077 pwd = uma_zalloc_smr(pwd_zone, M_WAITOK); 4078 bzero(pwd, sizeof(*pwd)); 4079 refcount_init(&pwd->pwd_refcount, 1); 4080 return (pwd); 4081 } 4082 4083 void 4084 pwd_drop(struct pwd *pwd) 4085 { 4086 4087 if (!refcount_release(&pwd->pwd_refcount)) 4088 return; 4089 4090 if (pwd->pwd_cdir != NULL) 4091 vrele(pwd->pwd_cdir); 4092 if (pwd->pwd_rdir != NULL) 4093 vrele(pwd->pwd_rdir); 4094 if (pwd->pwd_jdir != NULL) 4095 vrele(pwd->pwd_jdir); 4096 if (pwd->pwd_adir != NULL) 4097 vrele(pwd->pwd_adir); 4098 uma_zfree_smr(pwd_zone, pwd); 4099 } 4100 4101 /* 4102 * The caller is responsible for invoking priv_check() and 4103 * mac_vnode_check_chroot() to authorize this operation. 4104 */ 4105 int 4106 pwd_chroot(struct thread *td, struct vnode *vp) 4107 { 4108 struct pwddesc *pdp; 4109 struct filedesc *fdp; 4110 struct pwd *newpwd, *oldpwd; 4111 int error; 4112 4113 fdp = td->td_proc->p_fd; 4114 pdp = td->td_proc->p_pd; 4115 newpwd = pwd_alloc(); 4116 FILEDESC_SLOCK(fdp); 4117 PWDDESC_XLOCK(pdp); 4118 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4119 if (chroot_allow_open_directories == 0 || 4120 (chroot_allow_open_directories == 1 && 4121 oldpwd->pwd_rdir != rootvnode)) { 4122 error = chroot_refuse_vdir_fds(fdp); 4123 FILEDESC_SUNLOCK(fdp); 4124 if (error != 0) { 4125 PWDDESC_XUNLOCK(pdp); 4126 pwd_drop(newpwd); 4127 return (error); 4128 } 4129 } else { 4130 FILEDESC_SUNLOCK(fdp); 4131 } 4132 4133 vrefact(vp); 4134 newpwd->pwd_rdir = vp; 4135 vrefact(vp); 4136 newpwd->pwd_adir = vp; 4137 if (oldpwd->pwd_jdir == NULL) { 4138 vrefact(vp); 4139 newpwd->pwd_jdir = vp; 4140 } 4141 pwd_fill(oldpwd, newpwd); 4142 pwd_set(pdp, newpwd); 4143 PWDDESC_XUNLOCK(pdp); 4144 pwd_drop(oldpwd); 4145 return (0); 4146 } 4147 4148 void 4149 pwd_chdir(struct thread *td, struct vnode *vp) 4150 { 4151 struct pwddesc *pdp; 4152 struct pwd *newpwd, *oldpwd; 4153 4154 VNPASS(vp->v_usecount > 0, vp); 4155 4156 newpwd = pwd_alloc(); 4157 pdp = td->td_proc->p_pd; 4158 PWDDESC_XLOCK(pdp); 4159 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4160 newpwd->pwd_cdir = vp; 4161 pwd_fill(oldpwd, newpwd); 4162 pwd_set(pdp, newpwd); 4163 PWDDESC_XUNLOCK(pdp); 4164 pwd_drop(oldpwd); 4165 } 4166 4167 /* 4168 * Process is transitioning to/from a non-native ABI. 4169 */ 4170 void 4171 pwd_altroot(struct thread *td, struct vnode *altroot_vp) 4172 { 4173 struct pwddesc *pdp; 4174 struct pwd *newpwd, *oldpwd; 4175 4176 newpwd = pwd_alloc(); 4177 pdp = td->td_proc->p_pd; 4178 PWDDESC_XLOCK(pdp); 4179 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4180 if (altroot_vp != NULL) { 4181 /* 4182 * Native process to a non-native ABI. 4183 */ 4184 4185 vrefact(altroot_vp); 4186 newpwd->pwd_adir = altroot_vp; 4187 } else { 4188 /* 4189 * Non-native process to the native ABI. 4190 */ 4191 4192 vrefact(oldpwd->pwd_rdir); 4193 newpwd->pwd_adir = oldpwd->pwd_rdir; 4194 } 4195 pwd_fill(oldpwd, newpwd); 4196 pwd_set(pdp, newpwd); 4197 PWDDESC_XUNLOCK(pdp); 4198 pwd_drop(oldpwd); 4199 } 4200 4201 /* 4202 * jail_attach(2) changes both root and working directories. 4203 */ 4204 int 4205 pwd_chroot_chdir(struct thread *td, struct vnode *vp) 4206 { 4207 struct pwddesc *pdp; 4208 struct filedesc *fdp; 4209 struct pwd *newpwd, *oldpwd; 4210 int error; 4211 4212 fdp = td->td_proc->p_fd; 4213 pdp = td->td_proc->p_pd; 4214 newpwd = pwd_alloc(); 4215 FILEDESC_SLOCK(fdp); 4216 PWDDESC_XLOCK(pdp); 4217 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4218 error = chroot_refuse_vdir_fds(fdp); 4219 FILEDESC_SUNLOCK(fdp); 4220 if (error != 0) { 4221 PWDDESC_XUNLOCK(pdp); 4222 pwd_drop(newpwd); 4223 return (error); 4224 } 4225 4226 vrefact(vp); 4227 newpwd->pwd_rdir = vp; 4228 vrefact(vp); 4229 newpwd->pwd_cdir = vp; 4230 if (oldpwd->pwd_jdir == NULL) { 4231 vrefact(vp); 4232 newpwd->pwd_jdir = vp; 4233 } 4234 vrefact(vp); 4235 newpwd->pwd_adir = vp; 4236 pwd_fill(oldpwd, newpwd); 4237 pwd_set(pdp, newpwd); 4238 PWDDESC_XUNLOCK(pdp); 4239 pwd_drop(oldpwd); 4240 return (0); 4241 } 4242 4243 void 4244 pwd_ensure_dirs(void) 4245 { 4246 struct pwddesc *pdp; 4247 struct pwd *oldpwd, *newpwd; 4248 4249 pdp = curproc->p_pd; 4250 PWDDESC_XLOCK(pdp); 4251 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4252 if (oldpwd->pwd_cdir != NULL && oldpwd->pwd_rdir != NULL && 4253 oldpwd->pwd_adir != NULL) { 4254 PWDDESC_XUNLOCK(pdp); 4255 return; 4256 } 4257 PWDDESC_XUNLOCK(pdp); 4258 4259 newpwd = pwd_alloc(); 4260 PWDDESC_XLOCK(pdp); 4261 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4262 pwd_fill(oldpwd, newpwd); 4263 if (newpwd->pwd_cdir == NULL) { 4264 vrefact(rootvnode); 4265 newpwd->pwd_cdir = rootvnode; 4266 } 4267 if (newpwd->pwd_rdir == NULL) { 4268 vrefact(rootvnode); 4269 newpwd->pwd_rdir = rootvnode; 4270 } 4271 if (newpwd->pwd_adir == NULL) { 4272 vrefact(rootvnode); 4273 newpwd->pwd_adir = rootvnode; 4274 } 4275 pwd_set(pdp, newpwd); 4276 PWDDESC_XUNLOCK(pdp); 4277 pwd_drop(oldpwd); 4278 } 4279 4280 void 4281 pwd_set_rootvnode(void) 4282 { 4283 struct pwddesc *pdp; 4284 struct pwd *oldpwd, *newpwd; 4285 4286 pdp = curproc->p_pd; 4287 4288 newpwd = pwd_alloc(); 4289 PWDDESC_XLOCK(pdp); 4290 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4291 vrefact(rootvnode); 4292 newpwd->pwd_cdir = rootvnode; 4293 vrefact(rootvnode); 4294 newpwd->pwd_rdir = rootvnode; 4295 vrefact(rootvnode); 4296 newpwd->pwd_adir = rootvnode; 4297 pwd_fill(oldpwd, newpwd); 4298 pwd_set(pdp, newpwd); 4299 PWDDESC_XUNLOCK(pdp); 4300 pwd_drop(oldpwd); 4301 } 4302 4303 /* 4304 * Scan all active processes and prisons to see if any of them have a current 4305 * or root directory of `olddp'. If so, replace them with the new mount point. 4306 */ 4307 void 4308 mountcheckdirs(struct vnode *olddp, struct vnode *newdp) 4309 { 4310 struct pwddesc *pdp; 4311 struct pwd *newpwd, *oldpwd; 4312 struct prison *pr; 4313 struct proc *p; 4314 int nrele; 4315 4316 if (vrefcnt(olddp) == 1) 4317 return; 4318 nrele = 0; 4319 newpwd = pwd_alloc(); 4320 sx_slock(&allproc_lock); 4321 FOREACH_PROC_IN_SYSTEM(p) { 4322 PROC_LOCK(p); 4323 pdp = pdhold(p); 4324 PROC_UNLOCK(p); 4325 if (pdp == NULL) 4326 continue; 4327 PWDDESC_XLOCK(pdp); 4328 oldpwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 4329 if (oldpwd == NULL || 4330 (oldpwd->pwd_cdir != olddp && 4331 oldpwd->pwd_rdir != olddp && 4332 oldpwd->pwd_jdir != olddp && 4333 oldpwd->pwd_adir != olddp)) { 4334 PWDDESC_XUNLOCK(pdp); 4335 pddrop(pdp); 4336 continue; 4337 } 4338 if (oldpwd->pwd_cdir == olddp) { 4339 vrefact(newdp); 4340 newpwd->pwd_cdir = newdp; 4341 } 4342 if (oldpwd->pwd_rdir == olddp) { 4343 vrefact(newdp); 4344 newpwd->pwd_rdir = newdp; 4345 } 4346 if (oldpwd->pwd_jdir == olddp) { 4347 vrefact(newdp); 4348 newpwd->pwd_jdir = newdp; 4349 } 4350 if (oldpwd->pwd_adir == olddp) { 4351 vrefact(newdp); 4352 newpwd->pwd_adir = newdp; 4353 } 4354 pwd_fill(oldpwd, newpwd); 4355 pwd_set(pdp, newpwd); 4356 PWDDESC_XUNLOCK(pdp); 4357 pwd_drop(oldpwd); 4358 pddrop(pdp); 4359 newpwd = pwd_alloc(); 4360 } 4361 sx_sunlock(&allproc_lock); 4362 pwd_drop(newpwd); 4363 if (rootvnode == olddp) { 4364 vrefact(newdp); 4365 rootvnode = newdp; 4366 nrele++; 4367 } 4368 mtx_lock(&prison0.pr_mtx); 4369 if (prison0.pr_root == olddp) { 4370 vrefact(newdp); 4371 prison0.pr_root = newdp; 4372 nrele++; 4373 } 4374 mtx_unlock(&prison0.pr_mtx); 4375 sx_slock(&allprison_lock); 4376 TAILQ_FOREACH(pr, &allprison, pr_list) { 4377 mtx_lock(&pr->pr_mtx); 4378 if (pr->pr_root == olddp) { 4379 vrefact(newdp); 4380 pr->pr_root = newdp; 4381 nrele++; 4382 } 4383 mtx_unlock(&pr->pr_mtx); 4384 } 4385 sx_sunlock(&allprison_lock); 4386 while (nrele--) 4387 vrele(olddp); 4388 } 4389 4390 int 4391 descrip_check_write_mp(struct filedesc *fdp, struct mount *mp) 4392 { 4393 struct file *fp; 4394 struct vnode *vp; 4395 int error, i; 4396 4397 error = 0; 4398 FILEDESC_SLOCK(fdp); 4399 FILEDESC_FOREACH_FP(fdp, i, fp) { 4400 if (fp->f_type != DTYPE_VNODE || 4401 (atomic_load_int(&fp->f_flag) & FWRITE) == 0) 4402 continue; 4403 vp = fp->f_vnode; 4404 if (vp->v_mount == mp) { 4405 error = EDEADLK; 4406 break; 4407 } 4408 } 4409 FILEDESC_SUNLOCK(fdp); 4410 return (error); 4411 } 4412 4413 struct filedesc_to_leader * 4414 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, 4415 struct proc *leader) 4416 { 4417 struct filedesc_to_leader *fdtol; 4418 4419 fdtol = malloc(sizeof(struct filedesc_to_leader), 4420 M_FILEDESC_TO_LEADER, M_WAITOK); 4421 fdtol->fdl_refcount = 1; 4422 fdtol->fdl_holdcount = 0; 4423 fdtol->fdl_wakeup = 0; 4424 fdtol->fdl_leader = leader; 4425 if (old != NULL) { 4426 FILEDESC_XLOCK(fdp); 4427 fdtol->fdl_next = old->fdl_next; 4428 fdtol->fdl_prev = old; 4429 old->fdl_next = fdtol; 4430 fdtol->fdl_next->fdl_prev = fdtol; 4431 FILEDESC_XUNLOCK(fdp); 4432 } else { 4433 fdtol->fdl_next = fdtol; 4434 fdtol->fdl_prev = fdtol; 4435 } 4436 return (fdtol); 4437 } 4438 4439 struct filedesc_to_leader * 4440 filedesc_to_leader_share(struct filedesc_to_leader *fdtol, struct filedesc *fdp) 4441 { 4442 FILEDESC_XLOCK(fdp); 4443 fdtol->fdl_refcount++; 4444 FILEDESC_XUNLOCK(fdp); 4445 return (fdtol); 4446 } 4447 4448 static int 4449 filedesc_nfiles(struct filedesc *fdp) 4450 { 4451 NDSLOTTYPE *map; 4452 int count, off, minoff; 4453 4454 if (fdp == NULL) 4455 return (0); 4456 count = 0; 4457 FILEDESC_SLOCK(fdp); 4458 map = fdp->fd_map; 4459 off = NDSLOT(fdp->fd_nfiles - 1); 4460 for (minoff = NDSLOT(0); off >= minoff; --off) 4461 count += bitcountl(map[off]); 4462 FILEDESC_SUNLOCK(fdp); 4463 return (count); 4464 } 4465 4466 int 4467 proc_nfiles(struct proc *p) 4468 { 4469 struct filedesc *fdp; 4470 int res; 4471 4472 PROC_LOCK(p); 4473 fdp = fdhold(p); 4474 PROC_UNLOCK(p); 4475 res = filedesc_nfiles(fdp); 4476 fddrop(fdp); 4477 return (res); 4478 } 4479 4480 static int 4481 sysctl_kern_proc_nfds(SYSCTL_HANDLER_ARGS) 4482 { 4483 u_int namelen; 4484 int count; 4485 4486 namelen = arg2; 4487 if (namelen != 1) 4488 return (EINVAL); 4489 4490 if (*(int *)arg1 != 0) 4491 return (EINVAL); 4492 4493 count = filedesc_nfiles(curproc->p_fd); 4494 return (SYSCTL_OUT(req, &count, sizeof(count))); 4495 } 4496 4497 static SYSCTL_NODE(_kern_proc, KERN_PROC_NFDS, nfds, 4498 CTLFLAG_RD|CTLFLAG_CAPRD|CTLFLAG_MPSAFE, sysctl_kern_proc_nfds, 4499 "Number of open file descriptors"); 4500 4501 /* 4502 * Get file structures globally. 4503 */ 4504 static int 4505 sysctl_kern_file(SYSCTL_HANDLER_ARGS) 4506 { 4507 struct xfile xf; 4508 struct filedesc *fdp; 4509 struct file *fp; 4510 struct proc *p; 4511 int error, n; 4512 4513 error = sysctl_wire_old_buffer(req, 0); 4514 if (error != 0) 4515 return (error); 4516 if (req->oldptr == NULL) { 4517 n = 0; 4518 sx_slock(&allproc_lock); 4519 FOREACH_PROC_IN_SYSTEM(p) { 4520 PROC_LOCK(p); 4521 if (p->p_state == PRS_NEW) { 4522 PROC_UNLOCK(p); 4523 continue; 4524 } 4525 fdp = fdhold(p); 4526 PROC_UNLOCK(p); 4527 if (fdp == NULL) 4528 continue; 4529 /* overestimates sparse tables. */ 4530 n += fdp->fd_nfiles; 4531 fddrop(fdp); 4532 } 4533 sx_sunlock(&allproc_lock); 4534 return (SYSCTL_OUT(req, 0, n * sizeof(xf))); 4535 } 4536 error = 0; 4537 bzero(&xf, sizeof(xf)); 4538 xf.xf_size = sizeof(xf); 4539 sx_slock(&allproc_lock); 4540 FOREACH_PROC_IN_SYSTEM(p) { 4541 PROC_LOCK(p); 4542 if (p->p_state == PRS_NEW) { 4543 PROC_UNLOCK(p); 4544 continue; 4545 } 4546 if (p_cansee(req->td, p) != 0) { 4547 PROC_UNLOCK(p); 4548 continue; 4549 } 4550 xf.xf_pid = p->p_pid; 4551 xf.xf_uid = p->p_ucred->cr_uid; 4552 fdp = fdhold(p); 4553 PROC_UNLOCK(p); 4554 if (fdp == NULL) 4555 continue; 4556 FILEDESC_SLOCK(fdp); 4557 if (refcount_load(&fdp->fd_refcnt) == 0) 4558 goto nextproc; 4559 FILEDESC_FOREACH_FP(fdp, n, fp) { 4560 xf.xf_fd = n; 4561 xf.xf_file = (uintptr_t)fp; 4562 xf.xf_data = (uintptr_t)fp->f_data; 4563 xf.xf_vnode = (uintptr_t)fp->f_vnode; 4564 xf.xf_type = (uintptr_t)fp->f_type; 4565 xf.xf_count = refcount_load(&fp->f_count); 4566 xf.xf_msgcount = 0; 4567 xf.xf_offset = foffset_get(fp); 4568 xf.xf_flag = fp->f_flag; 4569 error = SYSCTL_OUT(req, &xf, sizeof(xf)); 4570 4571 /* 4572 * There is no need to re-check the fdtable refcount 4573 * here since the filedesc lock is not dropped in the 4574 * loop body. 4575 */ 4576 if (error != 0) 4577 break; 4578 } 4579 nextproc: 4580 FILEDESC_SUNLOCK(fdp); 4581 fddrop(fdp); 4582 if (error) 4583 break; 4584 } 4585 sx_sunlock(&allproc_lock); 4586 return (error); 4587 } 4588 4589 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE, 4590 0, 0, sysctl_kern_file, "S,xfile", "Entire file table"); 4591 4592 #ifdef KINFO_FILE_SIZE 4593 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE); 4594 #endif 4595 4596 static int 4597 xlate_fflags(int fflags) 4598 { 4599 static const struct { 4600 int fflag; 4601 int kf_fflag; 4602 } fflags_table[] = { 4603 { FAPPEND, KF_FLAG_APPEND }, 4604 { FASYNC, KF_FLAG_ASYNC }, 4605 { FFSYNC, KF_FLAG_FSYNC }, 4606 { FHASLOCK, KF_FLAG_HASLOCK }, 4607 { FNONBLOCK, KF_FLAG_NONBLOCK }, 4608 { FREAD, KF_FLAG_READ }, 4609 { FWRITE, KF_FLAG_WRITE }, 4610 { O_CREAT, KF_FLAG_CREAT }, 4611 { O_DIRECT, KF_FLAG_DIRECT }, 4612 { O_EXCL, KF_FLAG_EXCL }, 4613 { O_EXEC, KF_FLAG_EXEC }, 4614 { O_EXLOCK, KF_FLAG_EXLOCK }, 4615 { O_NOFOLLOW, KF_FLAG_NOFOLLOW }, 4616 { O_SHLOCK, KF_FLAG_SHLOCK }, 4617 { O_TRUNC, KF_FLAG_TRUNC } 4618 }; 4619 unsigned int i; 4620 int kflags; 4621 4622 kflags = 0; 4623 for (i = 0; i < nitems(fflags_table); i++) 4624 if (fflags & fflags_table[i].fflag) 4625 kflags |= fflags_table[i].kf_fflag; 4626 return (kflags); 4627 } 4628 4629 /* Trim unused data from kf_path by truncating the structure size. */ 4630 void 4631 pack_kinfo(struct kinfo_file *kif) 4632 { 4633 4634 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) + 4635 strlen(kif->kf_path) + 1; 4636 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t)); 4637 } 4638 4639 static void 4640 export_file_to_kinfo(struct file *fp, int fd, cap_rights_t *rightsp, 4641 struct kinfo_file *kif, struct filedesc *fdp, int flags) 4642 { 4643 int error; 4644 4645 bzero(kif, sizeof(*kif)); 4646 4647 /* Set a default type to allow for empty fill_kinfo() methods. */ 4648 kif->kf_type = KF_TYPE_UNKNOWN; 4649 kif->kf_flags = xlate_fflags(fp->f_flag); 4650 if (rightsp != NULL) 4651 kif->kf_cap_rights = *rightsp; 4652 else 4653 cap_rights_init_zero(&kif->kf_cap_rights); 4654 kif->kf_fd = fd; 4655 kif->kf_ref_count = refcount_load(&fp->f_count); 4656 kif->kf_offset = foffset_get(fp); 4657 4658 /* 4659 * This may drop the filedesc lock, so the 'fp' cannot be 4660 * accessed after this call. 4661 */ 4662 error = fo_fill_kinfo(fp, kif, fdp); 4663 if (error == 0) 4664 kif->kf_status |= KF_ATTR_VALID; 4665 if ((flags & KERN_FILEDESC_PACK_KINFO) != 0) 4666 pack_kinfo(kif); 4667 else 4668 kif->kf_structsize = roundup2(sizeof(*kif), sizeof(uint64_t)); 4669 } 4670 4671 static void 4672 export_vnode_to_kinfo(struct vnode *vp, int fd, int fflags, 4673 struct kinfo_file *kif, int flags) 4674 { 4675 int error; 4676 4677 bzero(kif, sizeof(*kif)); 4678 4679 kif->kf_type = KF_TYPE_VNODE; 4680 error = vn_fill_kinfo_vnode(vp, kif); 4681 if (error == 0) 4682 kif->kf_status |= KF_ATTR_VALID; 4683 kif->kf_flags = xlate_fflags(fflags); 4684 cap_rights_init_zero(&kif->kf_cap_rights); 4685 kif->kf_fd = fd; 4686 kif->kf_ref_count = -1; 4687 kif->kf_offset = -1; 4688 if ((flags & KERN_FILEDESC_PACK_KINFO) != 0) 4689 pack_kinfo(kif); 4690 else 4691 kif->kf_structsize = roundup2(sizeof(*kif), sizeof(uint64_t)); 4692 vrele(vp); 4693 } 4694 4695 struct export_fd_buf { 4696 struct filedesc *fdp; 4697 struct pwddesc *pdp; 4698 struct sbuf *sb; 4699 ssize_t remainder; 4700 struct kinfo_file kif; 4701 int flags; 4702 }; 4703 4704 static int 4705 export_kinfo_to_sb(struct export_fd_buf *efbuf) 4706 { 4707 struct kinfo_file *kif; 4708 4709 kif = &efbuf->kif; 4710 if (efbuf->remainder != -1) { 4711 if (efbuf->remainder < kif->kf_structsize) 4712 return (ENOMEM); 4713 efbuf->remainder -= kif->kf_structsize; 4714 } 4715 if (sbuf_bcat(efbuf->sb, kif, kif->kf_structsize) != 0) 4716 return (sbuf_error(efbuf->sb)); 4717 return (0); 4718 } 4719 4720 static int 4721 export_file_to_sb(struct file *fp, int fd, cap_rights_t *rightsp, 4722 struct export_fd_buf *efbuf) 4723 { 4724 int error; 4725 4726 if (efbuf->remainder == 0) 4727 return (ENOMEM); 4728 export_file_to_kinfo(fp, fd, rightsp, &efbuf->kif, efbuf->fdp, 4729 efbuf->flags); 4730 FILEDESC_SUNLOCK(efbuf->fdp); 4731 error = export_kinfo_to_sb(efbuf); 4732 FILEDESC_SLOCK(efbuf->fdp); 4733 return (error); 4734 } 4735 4736 static int 4737 export_vnode_to_sb(struct vnode *vp, int fd, int fflags, 4738 struct export_fd_buf *efbuf) 4739 { 4740 int error; 4741 4742 if (efbuf->remainder == 0) 4743 return (ENOMEM); 4744 if (efbuf->pdp != NULL) 4745 PWDDESC_XUNLOCK(efbuf->pdp); 4746 export_vnode_to_kinfo(vp, fd, fflags, &efbuf->kif, efbuf->flags); 4747 error = export_kinfo_to_sb(efbuf); 4748 if (efbuf->pdp != NULL) 4749 PWDDESC_XLOCK(efbuf->pdp); 4750 return (error); 4751 } 4752 4753 /* 4754 * Store a process file descriptor information to sbuf. 4755 * 4756 * Takes a locked proc as argument, and returns with the proc unlocked. 4757 */ 4758 int 4759 kern_proc_filedesc_out(struct proc *p, struct sbuf *sb, ssize_t maxlen, 4760 int flags) 4761 { 4762 struct file *fp; 4763 struct filedesc *fdp; 4764 struct pwddesc *pdp; 4765 struct export_fd_buf *efbuf; 4766 struct vnode *cttyvp, *textvp, *tracevp; 4767 struct pwd *pwd; 4768 int error, i; 4769 cap_rights_t rights; 4770 4771 PROC_LOCK_ASSERT(p, MA_OWNED); 4772 4773 /* ktrace vnode */ 4774 tracevp = ktr_get_tracevp(p, true); 4775 /* text vnode */ 4776 textvp = p->p_textvp; 4777 if (textvp != NULL) 4778 vrefact(textvp); 4779 /* Controlling tty. */ 4780 cttyvp = NULL; 4781 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) { 4782 cttyvp = p->p_pgrp->pg_session->s_ttyvp; 4783 if (cttyvp != NULL) 4784 vrefact(cttyvp); 4785 } 4786 fdp = fdhold(p); 4787 pdp = pdhold(p); 4788 PROC_UNLOCK(p); 4789 4790 efbuf = malloc(sizeof(*efbuf), M_TEMP, M_WAITOK); 4791 efbuf->fdp = NULL; 4792 efbuf->pdp = NULL; 4793 efbuf->sb = sb; 4794 efbuf->remainder = maxlen; 4795 efbuf->flags = flags; 4796 4797 error = 0; 4798 if (tracevp != NULL) 4799 error = export_vnode_to_sb(tracevp, KF_FD_TYPE_TRACE, 4800 FREAD | FWRITE, efbuf); 4801 if (error == 0 && textvp != NULL) 4802 error = export_vnode_to_sb(textvp, KF_FD_TYPE_TEXT, FREAD, 4803 efbuf); 4804 if (error == 0 && cttyvp != NULL) 4805 error = export_vnode_to_sb(cttyvp, KF_FD_TYPE_CTTY, 4806 FREAD | FWRITE, efbuf); 4807 if (error != 0 || pdp == NULL || fdp == NULL) 4808 goto fail; 4809 efbuf->fdp = fdp; 4810 efbuf->pdp = pdp; 4811 PWDDESC_XLOCK(pdp); 4812 pwd = pwd_hold_pwddesc(pdp); 4813 if (pwd != NULL) { 4814 /* working directory */ 4815 if (pwd->pwd_cdir != NULL) { 4816 vrefact(pwd->pwd_cdir); 4817 error = export_vnode_to_sb(pwd->pwd_cdir, 4818 KF_FD_TYPE_CWD, FREAD, efbuf); 4819 } 4820 /* root directory */ 4821 if (error == 0 && pwd->pwd_rdir != NULL) { 4822 vrefact(pwd->pwd_rdir); 4823 error = export_vnode_to_sb(pwd->pwd_rdir, 4824 KF_FD_TYPE_ROOT, FREAD, efbuf); 4825 } 4826 /* jail directory */ 4827 if (error == 0 && pwd->pwd_jdir != NULL) { 4828 vrefact(pwd->pwd_jdir); 4829 error = export_vnode_to_sb(pwd->pwd_jdir, 4830 KF_FD_TYPE_JAIL, FREAD, efbuf); 4831 } 4832 } 4833 PWDDESC_XUNLOCK(pdp); 4834 if (error != 0) 4835 goto fail; 4836 if (pwd != NULL) 4837 pwd_drop(pwd); 4838 FILEDESC_SLOCK(fdp); 4839 if (refcount_load(&fdp->fd_refcnt) == 0) 4840 goto skip; 4841 FILEDESC_FOREACH_FP(fdp, i, fp) { 4842 #ifdef CAPABILITIES 4843 rights = *cap_rights(fdp, i); 4844 #else /* !CAPABILITIES */ 4845 rights = cap_no_rights; 4846 #endif 4847 /* 4848 * Create sysctl entry. It is OK to drop the filedesc 4849 * lock inside of export_file_to_sb() as we will 4850 * re-validate and re-evaluate its properties when the 4851 * loop continues. 4852 */ 4853 error = export_file_to_sb(fp, i, &rights, efbuf); 4854 if (error != 0 || refcount_load(&fdp->fd_refcnt) == 0) 4855 break; 4856 } 4857 skip: 4858 FILEDESC_SUNLOCK(fdp); 4859 fail: 4860 if (fdp != NULL) 4861 fddrop(fdp); 4862 if (pdp != NULL) 4863 pddrop(pdp); 4864 free(efbuf, M_TEMP); 4865 return (error); 4866 } 4867 4868 #define FILEDESC_SBUF_SIZE (sizeof(struct kinfo_file) * 5) 4869 4870 /* 4871 * Get per-process file descriptors for use by procstat(1), et al. 4872 */ 4873 static int 4874 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS) 4875 { 4876 struct sbuf sb; 4877 struct proc *p; 4878 ssize_t maxlen; 4879 u_int namelen; 4880 int error, error2, *name; 4881 4882 namelen = arg2; 4883 if (namelen != 1) 4884 return (EINVAL); 4885 4886 name = (int *)arg1; 4887 4888 sbuf_new_for_sysctl(&sb, NULL, FILEDESC_SBUF_SIZE, req); 4889 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 4890 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p); 4891 if (error != 0) { 4892 sbuf_delete(&sb); 4893 return (error); 4894 } 4895 maxlen = req->oldptr != NULL ? req->oldlen : -1; 4896 error = kern_proc_filedesc_out(p, &sb, maxlen, 4897 KERN_FILEDESC_PACK_KINFO); 4898 error2 = sbuf_finish(&sb); 4899 sbuf_delete(&sb); 4900 return (error != 0 ? error : error2); 4901 } 4902 4903 #ifdef COMPAT_FREEBSD7 4904 #ifdef KINFO_OFILE_SIZE 4905 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE); 4906 #endif 4907 4908 static void 4909 kinfo_to_okinfo(struct kinfo_file *kif, struct kinfo_ofile *okif) 4910 { 4911 4912 okif->kf_structsize = sizeof(*okif); 4913 okif->kf_type = kif->kf_type; 4914 okif->kf_fd = kif->kf_fd; 4915 okif->kf_ref_count = kif->kf_ref_count; 4916 okif->kf_flags = kif->kf_flags & (KF_FLAG_READ | KF_FLAG_WRITE | 4917 KF_FLAG_APPEND | KF_FLAG_ASYNC | KF_FLAG_FSYNC | KF_FLAG_NONBLOCK | 4918 KF_FLAG_DIRECT | KF_FLAG_HASLOCK); 4919 okif->kf_offset = kif->kf_offset; 4920 if (kif->kf_type == KF_TYPE_VNODE) 4921 okif->kf_vnode_type = kif->kf_un.kf_file.kf_file_type; 4922 else 4923 okif->kf_vnode_type = KF_VTYPE_VNON; 4924 strlcpy(okif->kf_path, kif->kf_path, sizeof(okif->kf_path)); 4925 if (kif->kf_type == KF_TYPE_SOCKET) { 4926 okif->kf_sock_domain = kif->kf_un.kf_sock.kf_sock_domain0; 4927 okif->kf_sock_type = kif->kf_un.kf_sock.kf_sock_type0; 4928 okif->kf_sock_protocol = kif->kf_un.kf_sock.kf_sock_protocol0; 4929 okif->kf_sa_local = kif->kf_un.kf_sock.kf_sa_local; 4930 okif->kf_sa_peer = kif->kf_un.kf_sock.kf_sa_peer; 4931 } else { 4932 okif->kf_sa_local.ss_family = AF_UNSPEC; 4933 okif->kf_sa_peer.ss_family = AF_UNSPEC; 4934 } 4935 } 4936 4937 static int 4938 export_vnode_for_osysctl(struct vnode *vp, int type, struct kinfo_file *kif, 4939 struct kinfo_ofile *okif, struct pwddesc *pdp, struct sysctl_req *req) 4940 { 4941 int error; 4942 4943 vrefact(vp); 4944 PWDDESC_XUNLOCK(pdp); 4945 export_vnode_to_kinfo(vp, type, 0, kif, KERN_FILEDESC_PACK_KINFO); 4946 kinfo_to_okinfo(kif, okif); 4947 error = SYSCTL_OUT(req, okif, sizeof(*okif)); 4948 PWDDESC_XLOCK(pdp); 4949 return (error); 4950 } 4951 4952 /* 4953 * Get per-process file descriptors for use by procstat(1), et al. 4954 */ 4955 static int 4956 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS) 4957 { 4958 struct kinfo_ofile *okif; 4959 struct kinfo_file *kif; 4960 struct filedesc *fdp; 4961 struct pwddesc *pdp; 4962 struct pwd *pwd; 4963 u_int namelen; 4964 int error, i, *name; 4965 struct file *fp; 4966 struct proc *p; 4967 4968 namelen = arg2; 4969 if (namelen != 1) 4970 return (EINVAL); 4971 4972 name = (int *)arg1; 4973 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p); 4974 if (error != 0) 4975 return (error); 4976 fdp = fdhold(p); 4977 if (fdp != NULL) 4978 pdp = pdhold(p); 4979 PROC_UNLOCK(p); 4980 if (fdp == NULL || pdp == NULL) { 4981 if (fdp != NULL) 4982 fddrop(fdp); 4983 return (ENOENT); 4984 } 4985 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK); 4986 okif = malloc(sizeof(*okif), M_TEMP, M_WAITOK); 4987 PWDDESC_XLOCK(pdp); 4988 pwd = pwd_hold_pwddesc(pdp); 4989 if (pwd != NULL) { 4990 if (pwd->pwd_cdir != NULL) 4991 export_vnode_for_osysctl(pwd->pwd_cdir, KF_FD_TYPE_CWD, kif, 4992 okif, pdp, req); 4993 if (pwd->pwd_rdir != NULL) 4994 export_vnode_for_osysctl(pwd->pwd_rdir, KF_FD_TYPE_ROOT, kif, 4995 okif, pdp, req); 4996 if (pwd->pwd_jdir != NULL) 4997 export_vnode_for_osysctl(pwd->pwd_jdir, KF_FD_TYPE_JAIL, kif, 4998 okif, pdp, req); 4999 } 5000 PWDDESC_XUNLOCK(pdp); 5001 if (pwd != NULL) 5002 pwd_drop(pwd); 5003 FILEDESC_SLOCK(fdp); 5004 if (refcount_load(&fdp->fd_refcnt) == 0) 5005 goto skip; 5006 FILEDESC_FOREACH_FP(fdp, i, fp) { 5007 export_file_to_kinfo(fp, i, NULL, kif, fdp, 5008 KERN_FILEDESC_PACK_KINFO); 5009 FILEDESC_SUNLOCK(fdp); 5010 kinfo_to_okinfo(kif, okif); 5011 error = SYSCTL_OUT(req, okif, sizeof(*okif)); 5012 FILEDESC_SLOCK(fdp); 5013 if (error != 0 || refcount_load(&fdp->fd_refcnt) == 0) 5014 break; 5015 } 5016 skip: 5017 FILEDESC_SUNLOCK(fdp); 5018 fddrop(fdp); 5019 pddrop(pdp); 5020 free(kif, M_TEMP); 5021 free(okif, M_TEMP); 5022 return (0); 5023 } 5024 5025 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, 5026 CTLFLAG_RD|CTLFLAG_MPSAFE, sysctl_kern_proc_ofiledesc, 5027 "Process ofiledesc entries"); 5028 #endif /* COMPAT_FREEBSD7 */ 5029 5030 int 5031 vntype_to_kinfo(int vtype) 5032 { 5033 struct { 5034 int vtype; 5035 int kf_vtype; 5036 } vtypes_table[] = { 5037 { VBAD, KF_VTYPE_VBAD }, 5038 { VBLK, KF_VTYPE_VBLK }, 5039 { VCHR, KF_VTYPE_VCHR }, 5040 { VDIR, KF_VTYPE_VDIR }, 5041 { VFIFO, KF_VTYPE_VFIFO }, 5042 { VLNK, KF_VTYPE_VLNK }, 5043 { VNON, KF_VTYPE_VNON }, 5044 { VREG, KF_VTYPE_VREG }, 5045 { VSOCK, KF_VTYPE_VSOCK } 5046 }; 5047 unsigned int i; 5048 5049 /* 5050 * Perform vtype translation. 5051 */ 5052 for (i = 0; i < nitems(vtypes_table); i++) 5053 if (vtypes_table[i].vtype == vtype) 5054 return (vtypes_table[i].kf_vtype); 5055 5056 return (KF_VTYPE_UNKNOWN); 5057 } 5058 5059 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, 5060 CTLFLAG_RD|CTLFLAG_MPSAFE, sysctl_kern_proc_filedesc, 5061 "Process filedesc entries"); 5062 5063 /* 5064 * Store a process current working directory information to sbuf. 5065 * 5066 * Takes a locked proc as argument, and returns with the proc unlocked. 5067 */ 5068 int 5069 kern_proc_cwd_out(struct proc *p, struct sbuf *sb, ssize_t maxlen) 5070 { 5071 struct pwddesc *pdp; 5072 struct pwd *pwd; 5073 struct export_fd_buf *efbuf; 5074 struct vnode *cdir; 5075 int error; 5076 5077 PROC_LOCK_ASSERT(p, MA_OWNED); 5078 5079 pdp = pdhold(p); 5080 PROC_UNLOCK(p); 5081 if (pdp == NULL) 5082 return (EINVAL); 5083 5084 efbuf = malloc(sizeof(*efbuf), M_TEMP, M_WAITOK); 5085 efbuf->fdp = NULL; 5086 efbuf->pdp = pdp; 5087 efbuf->sb = sb; 5088 efbuf->remainder = maxlen; 5089 efbuf->flags = 0; 5090 5091 PWDDESC_XLOCK(pdp); 5092 pwd = PWDDESC_XLOCKED_LOAD_PWD(pdp); 5093 cdir = pwd->pwd_cdir; 5094 if (cdir == NULL) { 5095 error = EINVAL; 5096 } else { 5097 vrefact(cdir); 5098 error = export_vnode_to_sb(cdir, KF_FD_TYPE_CWD, FREAD, efbuf); 5099 } 5100 PWDDESC_XUNLOCK(pdp); 5101 pddrop(pdp); 5102 free(efbuf, M_TEMP); 5103 return (error); 5104 } 5105 5106 /* 5107 * Get per-process current working directory. 5108 */ 5109 static int 5110 sysctl_kern_proc_cwd(SYSCTL_HANDLER_ARGS) 5111 { 5112 struct sbuf sb; 5113 struct proc *p; 5114 ssize_t maxlen; 5115 u_int namelen; 5116 int error, error2, *name; 5117 5118 namelen = arg2; 5119 if (namelen != 1) 5120 return (EINVAL); 5121 5122 name = (int *)arg1; 5123 5124 sbuf_new_for_sysctl(&sb, NULL, sizeof(struct kinfo_file), req); 5125 sbuf_clear_flags(&sb, SBUF_INCLUDENUL); 5126 error = pget((pid_t)name[0], PGET_CANDEBUG | PGET_NOTWEXIT, &p); 5127 if (error != 0) { 5128 sbuf_delete(&sb); 5129 return (error); 5130 } 5131 maxlen = req->oldptr != NULL ? req->oldlen : -1; 5132 error = kern_proc_cwd_out(p, &sb, maxlen); 5133 error2 = sbuf_finish(&sb); 5134 sbuf_delete(&sb); 5135 return (error != 0 ? error : error2); 5136 } 5137 5138 static SYSCTL_NODE(_kern_proc, KERN_PROC_CWD, cwd, CTLFLAG_RD|CTLFLAG_MPSAFE, 5139 sysctl_kern_proc_cwd, "Process current working directory"); 5140 5141 #ifdef DDB 5142 /* 5143 * For the purposes of debugging, generate a human-readable string for the 5144 * file type. 5145 */ 5146 static const char * 5147 file_type_to_name(short type) 5148 { 5149 5150 switch (type) { 5151 case 0: 5152 return ("zero"); 5153 case DTYPE_VNODE: 5154 return ("vnode"); 5155 case DTYPE_SOCKET: 5156 return ("socket"); 5157 case DTYPE_PIPE: 5158 return ("pipe"); 5159 case DTYPE_FIFO: 5160 return ("fifo"); 5161 case DTYPE_KQUEUE: 5162 return ("kqueue"); 5163 case DTYPE_CRYPTO: 5164 return ("crypto"); 5165 case DTYPE_MQUEUE: 5166 return ("mqueue"); 5167 case DTYPE_SHM: 5168 return ("shm"); 5169 case DTYPE_SEM: 5170 return ("ksem"); 5171 case DTYPE_PTS: 5172 return ("pts"); 5173 case DTYPE_DEV: 5174 return ("dev"); 5175 case DTYPE_PROCDESC: 5176 return ("proc"); 5177 case DTYPE_EVENTFD: 5178 return ("eventfd"); 5179 case DTYPE_TIMERFD: 5180 return ("timerfd"); 5181 default: 5182 return ("unkn"); 5183 } 5184 } 5185 5186 /* 5187 * For the purposes of debugging, identify a process (if any, perhaps one of 5188 * many) that references the passed file in its file descriptor array. Return 5189 * NULL if none. 5190 */ 5191 static struct proc * 5192 file_to_first_proc(struct file *fp) 5193 { 5194 struct filedesc *fdp; 5195 struct proc *p; 5196 int n; 5197 5198 FOREACH_PROC_IN_SYSTEM(p) { 5199 if (p->p_state == PRS_NEW) 5200 continue; 5201 fdp = p->p_fd; 5202 if (fdp == NULL) 5203 continue; 5204 for (n = 0; n < fdp->fd_nfiles; n++) { 5205 if (fp == fdp->fd_ofiles[n].fde_file) 5206 return (p); 5207 } 5208 } 5209 return (NULL); 5210 } 5211 5212 static void 5213 db_print_file(struct file *fp, int header) 5214 { 5215 #define XPTRWIDTH ((int)howmany(sizeof(void *) * NBBY, 4)) 5216 struct proc *p; 5217 5218 if (header) 5219 db_printf("%*s %6s %*s %8s %4s %5s %6s %*s %5s %s\n", 5220 XPTRWIDTH, "File", "Type", XPTRWIDTH, "Data", "Flag", 5221 "GCFl", "Count", "MCount", XPTRWIDTH, "Vnode", "FPID", 5222 "FCmd"); 5223 p = file_to_first_proc(fp); 5224 db_printf("%*p %6s %*p %08x %04x %5d %6d %*p %5d %s\n", XPTRWIDTH, 5225 fp, file_type_to_name(fp->f_type), XPTRWIDTH, fp->f_data, 5226 fp->f_flag, 0, refcount_load(&fp->f_count), 0, XPTRWIDTH, fp->f_vnode, 5227 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-"); 5228 5229 #undef XPTRWIDTH 5230 } 5231 5232 DB_SHOW_COMMAND(file, db_show_file) 5233 { 5234 struct file *fp; 5235 5236 if (!have_addr) { 5237 db_printf("usage: show file <addr>\n"); 5238 return; 5239 } 5240 fp = (struct file *)addr; 5241 db_print_file(fp, 1); 5242 } 5243 5244 DB_SHOW_COMMAND_FLAGS(files, db_show_files, DB_CMD_MEMSAFE) 5245 { 5246 struct filedesc *fdp; 5247 struct file *fp; 5248 struct proc *p; 5249 int header; 5250 int n; 5251 5252 header = 1; 5253 FOREACH_PROC_IN_SYSTEM(p) { 5254 if (p->p_state == PRS_NEW) 5255 continue; 5256 if ((fdp = p->p_fd) == NULL) 5257 continue; 5258 for (n = 0; n < fdp->fd_nfiles; ++n) { 5259 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL) 5260 continue; 5261 db_print_file(fp, header); 5262 header = 0; 5263 } 5264 } 5265 } 5266 #endif 5267 5268 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, 5269 CTLFLAG_RWTUN | CTLFLAG_NOFETCH, 5270 &maxfilesperproc, 0, "Maximum files allowed open per process"); 5271 5272 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RWTUN | CTLFLAG_NOFETCH, 5273 &maxfiles, 0, "Maximum number of files"); 5274 5275 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD, 5276 &openfiles, 0, "System-wide number of open files"); 5277 5278 /* ARGSUSED*/ 5279 static void 5280 filelistinit(void *dummy) 5281 { 5282 5283 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL, 5284 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 5285 filedesc0_zone = uma_zcreate("filedesc0", sizeof(struct filedesc0), 5286 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 5287 pwd_zone = uma_zcreate("PWD", sizeof(struct pwd), NULL, NULL, 5288 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_SMR); 5289 /* 5290 * XXXMJG this is a temporary hack due to boot ordering issues against 5291 * the vnode zone. 5292 */ 5293 vfs_smr = uma_zone_get_smr(pwd_zone); 5294 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF); 5295 } 5296 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL); 5297 5298 /*-------------------------------------------------------------------*/ 5299 5300 static int 5301 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, 5302 int flags, struct thread *td) 5303 { 5304 5305 return (EBADF); 5306 } 5307 5308 static int 5309 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, 5310 struct thread *td) 5311 { 5312 5313 return (EINVAL); 5314 } 5315 5316 static int 5317 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, 5318 struct thread *td) 5319 { 5320 5321 return (EBADF); 5322 } 5323 5324 static int 5325 badfo_poll(struct file *fp, int events, struct ucred *active_cred, 5326 struct thread *td) 5327 { 5328 5329 return (0); 5330 } 5331 5332 static int 5333 badfo_kqfilter(struct file *fp, struct knote *kn) 5334 { 5335 5336 return (EBADF); 5337 } 5338 5339 static int 5340 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred) 5341 { 5342 5343 return (EBADF); 5344 } 5345 5346 static int 5347 badfo_close(struct file *fp, struct thread *td) 5348 { 5349 5350 return (0); 5351 } 5352 5353 static int 5354 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 5355 struct thread *td) 5356 { 5357 5358 return (EBADF); 5359 } 5360 5361 static int 5362 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 5363 struct thread *td) 5364 { 5365 5366 return (EBADF); 5367 } 5368 5369 static int 5370 badfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, 5371 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, 5372 struct thread *td) 5373 { 5374 5375 return (EBADF); 5376 } 5377 5378 static int 5379 badfo_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp) 5380 { 5381 5382 return (0); 5383 } 5384 5385 const struct fileops badfileops = { 5386 .fo_read = badfo_readwrite, 5387 .fo_write = badfo_readwrite, 5388 .fo_truncate = badfo_truncate, 5389 .fo_ioctl = badfo_ioctl, 5390 .fo_poll = badfo_poll, 5391 .fo_kqfilter = badfo_kqfilter, 5392 .fo_stat = badfo_stat, 5393 .fo_close = badfo_close, 5394 .fo_chmod = badfo_chmod, 5395 .fo_chown = badfo_chown, 5396 .fo_sendfile = badfo_sendfile, 5397 .fo_fill_kinfo = badfo_fill_kinfo, 5398 }; 5399 5400 static int 5401 path_poll(struct file *fp, int events, struct ucred *active_cred, 5402 struct thread *td) 5403 { 5404 return (POLLNVAL); 5405 } 5406 5407 static int 5408 path_close(struct file *fp, struct thread *td) 5409 { 5410 MPASS(fp->f_type == DTYPE_VNODE); 5411 fp->f_ops = &badfileops; 5412 vrele(fp->f_vnode); 5413 return (0); 5414 } 5415 5416 const struct fileops path_fileops = { 5417 .fo_read = badfo_readwrite, 5418 .fo_write = badfo_readwrite, 5419 .fo_truncate = badfo_truncate, 5420 .fo_ioctl = badfo_ioctl, 5421 .fo_poll = path_poll, 5422 .fo_kqfilter = vn_kqfilter_opath, 5423 .fo_stat = vn_statfile, 5424 .fo_close = path_close, 5425 .fo_chmod = badfo_chmod, 5426 .fo_chown = badfo_chown, 5427 .fo_sendfile = badfo_sendfile, 5428 .fo_fill_kinfo = vn_fill_kinfo, 5429 .fo_cmp = vn_cmp, 5430 .fo_flags = DFLAG_PASSABLE, 5431 }; 5432 5433 int 5434 invfo_rdwr(struct file *fp, struct uio *uio, struct ucred *active_cred, 5435 int flags, struct thread *td) 5436 { 5437 5438 return (EOPNOTSUPP); 5439 } 5440 5441 int 5442 invfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, 5443 struct thread *td) 5444 { 5445 5446 return (EINVAL); 5447 } 5448 5449 int 5450 invfo_ioctl(struct file *fp, u_long com, void *data, 5451 struct ucred *active_cred, struct thread *td) 5452 { 5453 5454 return (ENOTTY); 5455 } 5456 5457 int 5458 invfo_poll(struct file *fp, int events, struct ucred *active_cred, 5459 struct thread *td) 5460 { 5461 5462 return (poll_no_poll(events)); 5463 } 5464 5465 int 5466 invfo_kqfilter(struct file *fp, struct knote *kn) 5467 { 5468 5469 return (EINVAL); 5470 } 5471 5472 int 5473 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred, 5474 struct thread *td) 5475 { 5476 5477 return (EINVAL); 5478 } 5479 5480 int 5481 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred, 5482 struct thread *td) 5483 { 5484 5485 return (EINVAL); 5486 } 5487 5488 int 5489 invfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio, 5490 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags, 5491 struct thread *td) 5492 { 5493 5494 return (EINVAL); 5495 } 5496 5497 /*-------------------------------------------------------------------*/ 5498 5499 /* 5500 * File Descriptor pseudo-device driver (/dev/fd/). 5501 * 5502 * Opening minor device N dup()s the file (if any) connected to file 5503 * descriptor N belonging to the calling process. Note that this driver 5504 * consists of only the ``open()'' routine, because all subsequent 5505 * references to this file will be direct to the other driver. 5506 * 5507 * XXX: we could give this one a cloning event handler if necessary. 5508 */ 5509 5510 /* ARGSUSED */ 5511 static int 5512 fdopen(struct cdev *dev, int mode, int type, struct thread *td) 5513 { 5514 5515 /* 5516 * XXX Kludge: set curthread->td_dupfd to contain the value of the 5517 * the file descriptor being sought for duplication. The error 5518 * return ensures that the vnode for this device will be released 5519 * by vn_open. Open will detect this special error and take the 5520 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN 5521 * will simply report the error. 5522 */ 5523 td->td_dupfd = dev2unit(dev); 5524 return (ENODEV); 5525 } 5526 5527 static struct cdevsw fildesc_cdevsw = { 5528 .d_version = D_VERSION, 5529 .d_open = fdopen, 5530 .d_name = "FD", 5531 }; 5532 5533 static void 5534 fildesc_drvinit(void *unused) 5535 { 5536 struct cdev *dev; 5537 5538 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL, 5539 UID_ROOT, GID_WHEEL, 0666, "fd/0"); 5540 make_dev_alias(dev, "stdin"); 5541 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL, 5542 UID_ROOT, GID_WHEEL, 0666, "fd/1"); 5543 make_dev_alias(dev, "stdout"); 5544 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL, 5545 UID_ROOT, GID_WHEEL, 0666, "fd/2"); 5546 make_dev_alias(dev, "stderr"); 5547 } 5548 5549 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL); 5550