1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1989, 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_ktrace.h" 39 40 #define EXTERR_CATEGORY EXTERR_CAT_VFSSYSCALL 41 #include <sys/systm.h> 42 #ifdef COMPAT_FREEBSD11 43 #include <sys/abi_compat.h> 44 #endif 45 #include <sys/bio.h> 46 #include <sys/buf.h> 47 #include <sys/capsicum.h> 48 #include <sys/disk.h> 49 #include <sys/dirent.h> 50 #include <sys/exterrvar.h> 51 #include <sys/fcntl.h> 52 #include <sys/file.h> 53 #include <sys/filedesc.h> 54 #include <sys/filio.h> 55 #include <sys/jail.h> 56 #include <sys/kernel.h> 57 #ifdef KTRACE 58 #include <sys/ktrace.h> 59 #endif 60 #include <sys/limits.h> 61 #include <sys/linker.h> 62 #include <sys/malloc.h> 63 #include <sys/mount.h> 64 #include <sys/mutex.h> 65 #include <sys/namei.h> 66 #include <sys/priv.h> 67 #include <sys/proc.h> 68 #include <sys/rwlock.h> 69 #include <sys/sdt.h> 70 #include <sys/stat.h> 71 #include <sys/stdarg.h> 72 #include <sys/sx.h> 73 #include <sys/syscallsubr.h> 74 #include <sys/sysctl.h> 75 #include <sys/sysproto.h> 76 #include <sys/unistd.h> 77 #include <sys/vnode.h> 78 79 #include <security/audit/audit.h> 80 #include <security/mac/mac_framework.h> 81 82 #include <vm/vm.h> 83 #include <vm/vm_object.h> 84 #include <vm/vm_page.h> 85 #include <vm/vnode_pager.h> 86 #include <vm/uma.h> 87 88 #include <fs/devfs/devfs.h> 89 90 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information"); 91 92 static int kern_chflagsat(struct thread *td, int fd, const char *path, 93 enum uio_seg pathseg, u_long flags, int atflag); 94 static int setfflags(struct thread *td, struct vnode *, u_long); 95 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *); 96 static int getutimens(const struct timespec *, enum uio_seg, 97 struct timespec *, int *); 98 static int setutimes(struct thread *td, struct vnode *, 99 const struct timespec *, int, int); 100 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred, 101 struct thread *td); 102 static int kern_fhlinkat(struct thread *td, int fd, const char *path, 103 enum uio_seg pathseg, fhandle_t *fhp); 104 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, 105 size_t count, struct thread *td); 106 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, 107 const char *path, enum uio_seg segflag); 108 109 uint64_t 110 at2cnpflags(u_int at_flags, u_int mask) 111 { 112 uint64_t res; 113 114 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) != 115 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)); 116 117 res = 0; 118 at_flags &= mask; 119 if ((at_flags & AT_RESOLVE_BENEATH) != 0) 120 res |= RBENEATH; 121 if ((at_flags & AT_SYMLINK_FOLLOW) != 0) 122 res |= FOLLOW; 123 /* NOFOLLOW is pseudo flag */ 124 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) { 125 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW : 126 FOLLOW; 127 } 128 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0) 129 res |= EMPTYPATH; 130 return (res); 131 } 132 133 int 134 kern_sync(struct thread *td) 135 { 136 struct mount *mp, *nmp; 137 int save; 138 139 mtx_lock(&mountlist_mtx); 140 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 141 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) { 142 nmp = TAILQ_NEXT(mp, mnt_list); 143 continue; 144 } 145 if ((mp->mnt_flag & MNT_RDONLY) == 0 && 146 vn_start_write(NULL, &mp, V_NOWAIT) == 0) { 147 save = curthread_pflags_set(TDP_SYNCIO); 148 vfs_periodic(mp, MNT_NOWAIT); 149 VFS_SYNC(mp, MNT_NOWAIT); 150 curthread_pflags_restore(save); 151 vn_finished_write(mp); 152 } 153 mtx_lock(&mountlist_mtx); 154 nmp = TAILQ_NEXT(mp, mnt_list); 155 vfs_unbusy(mp); 156 } 157 mtx_unlock(&mountlist_mtx); 158 return (0); 159 } 160 161 /* 162 * Sync each mounted filesystem. 163 */ 164 #ifndef _SYS_SYSPROTO_H_ 165 struct sync_args { 166 int dummy; 167 }; 168 #endif 169 /* ARGSUSED */ 170 int 171 sys_sync(struct thread *td, struct sync_args *uap) 172 { 173 174 return (kern_sync(td)); 175 } 176 177 /* 178 * Change filesystem quotas. 179 */ 180 #ifndef _SYS_SYSPROTO_H_ 181 struct quotactl_args { 182 char *path; 183 int cmd; 184 int uid; 185 caddr_t arg; 186 }; 187 #endif 188 int 189 sys_quotactl(struct thread *td, struct quotactl_args *uap) 190 { 191 struct mount *mp; 192 struct nameidata nd; 193 int error; 194 bool mp_busy; 195 196 AUDIT_ARG_CMD(uap->cmd); 197 AUDIT_ARG_UID(uap->uid); 198 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS)) 199 return (EPERM); 200 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE, 201 uap->path); 202 if ((error = namei(&nd)) != 0) 203 return (error); 204 NDFREE_PNBUF(&nd); 205 mp = nd.ni_vp->v_mount; 206 vfs_ref(mp); 207 vput(nd.ni_vp); 208 error = vfs_busy(mp, 0); 209 if (error != 0) { 210 vfs_rel(mp); 211 return (error); 212 } 213 mp_busy = true; 214 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy); 215 216 /* 217 * Since quota on/off operations typically need to open quota 218 * files, the implementation may need to unbusy the mount point 219 * before calling into namei. Otherwise, unmount might be 220 * started between two vfs_busy() invocations (first is ours, 221 * second is from mount point cross-walk code in lookup()), 222 * causing deadlock. 223 * 224 * Avoid unbusying mp if the implementation indicates it has 225 * already done so. 226 */ 227 if (mp_busy) 228 vfs_unbusy(mp); 229 vfs_rel(mp); 230 return (error); 231 } 232 233 /* 234 * Used by statfs conversion routines to scale the block size up if 235 * necessary so that all of the block counts are <= 'max_size'. Note 236 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero 237 * value of 'n'. 238 */ 239 void 240 statfs_scale_blocks(struct statfs *sf, long max_size) 241 { 242 uint64_t count; 243 int shift; 244 245 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__)); 246 247 /* 248 * Attempt to scale the block counts to give a more accurate 249 * overview to userland of the ratio of free space to used 250 * space. To do this, find the largest block count and compute 251 * a divisor that lets it fit into a signed integer <= max_size. 252 */ 253 if (sf->f_bavail < 0) 254 count = -sf->f_bavail; 255 else 256 count = sf->f_bavail; 257 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count)); 258 if (count <= max_size) 259 return; 260 261 count >>= flsl(max_size); 262 shift = 0; 263 while (count > 0) { 264 shift++; 265 count >>=1; 266 } 267 268 sf->f_bsize <<= shift; 269 sf->f_blocks >>= shift; 270 sf->f_bfree >>= shift; 271 sf->f_bavail >>= shift; 272 } 273 274 static int 275 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf) 276 { 277 int error; 278 279 if (mp == NULL) 280 return (EBADF); 281 error = vfs_busy(mp, MBF_PCATCH); 282 vfs_rel(mp); 283 if (error != 0) 284 return (error); 285 #ifdef MAC 286 error = mac_mount_check_stat(td->td_ucred, mp); 287 if (error != 0) 288 goto out; 289 #endif 290 error = VFS_STATFS(mp, buf); 291 if (error != 0) 292 goto out; 293 if (priv_check_cred_vfs_generation(td->td_ucred)) 294 prison_enforce_statfs(td->td_ucred, mp, buf); 295 out: 296 vfs_unbusy(mp); 297 return (error); 298 } 299 300 /* 301 * Get filesystem statistics. 302 */ 303 #ifndef _SYS_SYSPROTO_H_ 304 struct statfs_args { 305 char *path; 306 struct statfs *buf; 307 }; 308 #endif 309 int 310 sys_statfs(struct thread *td, struct statfs_args *uap) 311 { 312 struct statfs *sfp; 313 int error; 314 315 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 316 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp); 317 if (error == 0) 318 error = copyout(sfp, uap->buf, sizeof(struct statfs)); 319 free(sfp, M_STATFS); 320 return (error); 321 } 322 323 int 324 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg, 325 struct statfs *buf) 326 { 327 struct mount *mp; 328 struct nameidata nd; 329 int error; 330 331 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path); 332 error = namei(&nd); 333 if (error != 0) 334 return (error); 335 NDFREE_PNBUF(&nd); 336 mp = vfs_ref_from_vp(nd.ni_vp); 337 vrele(nd.ni_vp); 338 return (kern_do_statfs(td, mp, buf)); 339 } 340 341 /* 342 * Get filesystem statistics. 343 */ 344 #ifndef _SYS_SYSPROTO_H_ 345 struct fstatfs_args { 346 int fd; 347 struct statfs *buf; 348 }; 349 #endif 350 int 351 sys_fstatfs(struct thread *td, struct fstatfs_args *uap) 352 { 353 struct statfs *sfp; 354 int error; 355 356 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 357 error = kern_fstatfs(td, uap->fd, sfp); 358 if (error == 0) 359 error = copyout(sfp, uap->buf, sizeof(struct statfs)); 360 free(sfp, M_STATFS); 361 return (error); 362 } 363 364 int 365 kern_fstatfs(struct thread *td, int fd, struct statfs *buf) 366 { 367 struct file *fp; 368 struct mount *mp; 369 struct vnode *vp; 370 int error; 371 372 AUDIT_ARG_FD(fd); 373 error = getvnode_path(td, fd, &cap_fstatfs_rights, NULL, &fp); 374 if (error != 0) 375 return (error); 376 vp = fp->f_vnode; 377 #ifdef AUDIT 378 if (AUDITING_TD(td)) { 379 vn_lock(vp, LK_SHARED | LK_RETRY); 380 AUDIT_ARG_VNODE1(vp); 381 VOP_UNLOCK(vp); 382 } 383 #endif 384 mp = vfs_ref_from_vp(vp); 385 fdrop(fp, td); 386 return (kern_do_statfs(td, mp, buf)); 387 } 388 389 /* 390 * Get statistics on all filesystems. 391 */ 392 #ifndef _SYS_SYSPROTO_H_ 393 struct getfsstat_args { 394 struct statfs *buf; 395 long bufsize; 396 int mode; 397 }; 398 #endif 399 int 400 sys_getfsstat(struct thread *td, struct getfsstat_args *uap) 401 { 402 size_t count; 403 int error; 404 405 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX) 406 return (EINVAL); 407 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count, 408 UIO_USERSPACE, uap->mode); 409 if (error == 0) 410 td->td_retval[0] = count; 411 return (error); 412 } 413 414 /* 415 * If (bufsize > 0 && bufseg == UIO_SYSSPACE) 416 * The caller is responsible for freeing memory which will be allocated 417 * in '*buf'. 418 */ 419 int 420 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize, 421 size_t *countp, enum uio_seg bufseg, int mode) 422 { 423 struct mount *mp, *nmp; 424 struct statfs *sfsp, *sp, *sptmp, *tofree; 425 size_t count, maxcount; 426 int error; 427 428 switch (mode) { 429 case MNT_WAIT: 430 case MNT_NOWAIT: 431 break; 432 default: 433 if (bufseg == UIO_SYSSPACE) 434 *buf = NULL; 435 return (EINVAL); 436 } 437 restart: 438 maxcount = bufsize / sizeof(struct statfs); 439 if (bufsize == 0) { 440 sfsp = NULL; 441 tofree = NULL; 442 } else if (bufseg == UIO_USERSPACE) { 443 sfsp = *buf; 444 tofree = NULL; 445 } else /* if (bufseg == UIO_SYSSPACE) */ { 446 count = 0; 447 mtx_lock(&mountlist_mtx); 448 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 449 count++; 450 } 451 mtx_unlock(&mountlist_mtx); 452 if (maxcount > count) 453 maxcount = count; 454 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs), 455 M_STATFS, M_WAITOK); 456 } 457 458 count = 0; 459 460 /* 461 * If there is no target buffer they only want the count. 462 * 463 * This could be TAILQ_FOREACH but it is open-coded to match the original 464 * code below. 465 */ 466 if (sfsp == NULL) { 467 mtx_lock(&mountlist_mtx); 468 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 469 if (prison_canseemount(td->td_ucred, mp) != 0) { 470 nmp = TAILQ_NEXT(mp, mnt_list); 471 continue; 472 } 473 #ifdef MAC 474 if (mac_mount_check_stat(td->td_ucred, mp) != 0) { 475 nmp = TAILQ_NEXT(mp, mnt_list); 476 continue; 477 } 478 #endif 479 count++; 480 nmp = TAILQ_NEXT(mp, mnt_list); 481 } 482 mtx_unlock(&mountlist_mtx); 483 *countp = count; 484 return (0); 485 } 486 487 /* 488 * They want the entire thing. 489 * 490 * Short-circuit the corner case of no room for anything, avoids 491 * relocking below. 492 */ 493 if (maxcount < 1) { 494 goto out; 495 } 496 497 mtx_lock(&mountlist_mtx); 498 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 499 if (prison_canseemount(td->td_ucred, mp) != 0) { 500 nmp = TAILQ_NEXT(mp, mnt_list); 501 continue; 502 } 503 #ifdef MAC 504 if (mac_mount_check_stat(td->td_ucred, mp) != 0) { 505 nmp = TAILQ_NEXT(mp, mnt_list); 506 continue; 507 } 508 #endif 509 if (mode == MNT_WAIT) { 510 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) { 511 /* 512 * If vfs_busy() failed, and MBF_NOWAIT 513 * wasn't passed, then the mp is gone. 514 * Furthermore, because of MBF_MNTLSTLOCK, 515 * the mountlist_mtx was dropped. We have 516 * no other choice than to start over. 517 */ 518 mtx_unlock(&mountlist_mtx); 519 free(tofree, M_STATFS); 520 goto restart; 521 } 522 } else { 523 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) { 524 nmp = TAILQ_NEXT(mp, mnt_list); 525 continue; 526 } 527 } 528 sp = &mp->mnt_stat; 529 /* 530 * If MNT_NOWAIT is specified, do not refresh 531 * the fsstat cache. 532 */ 533 if (mode != MNT_NOWAIT) { 534 error = VFS_STATFS(mp, sp); 535 if (error != 0) { 536 mtx_lock(&mountlist_mtx); 537 nmp = TAILQ_NEXT(mp, mnt_list); 538 vfs_unbusy(mp); 539 continue; 540 } 541 } 542 if (priv_check_cred_vfs_generation(td->td_ucred)) { 543 sptmp = malloc(sizeof(struct statfs), M_STATFS, 544 M_WAITOK); 545 *sptmp = *sp; 546 prison_enforce_statfs(td->td_ucred, mp, sptmp); 547 sp = sptmp; 548 } else 549 sptmp = NULL; 550 if (bufseg == UIO_SYSSPACE) { 551 bcopy(sp, sfsp, sizeof(*sp)); 552 free(sptmp, M_STATFS); 553 } else /* if (bufseg == UIO_USERSPACE) */ { 554 error = copyout(sp, sfsp, sizeof(*sp)); 555 free(sptmp, M_STATFS); 556 if (error != 0) { 557 vfs_unbusy(mp); 558 return (error); 559 } 560 } 561 sfsp++; 562 count++; 563 564 if (count == maxcount) { 565 vfs_unbusy(mp); 566 goto out; 567 } 568 569 mtx_lock(&mountlist_mtx); 570 nmp = TAILQ_NEXT(mp, mnt_list); 571 vfs_unbusy(mp); 572 } 573 mtx_unlock(&mountlist_mtx); 574 out: 575 *countp = count; 576 return (0); 577 } 578 579 #ifdef COMPAT_FREEBSD4 580 /* 581 * Get old format filesystem statistics. 582 */ 583 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *); 584 585 #ifndef _SYS_SYSPROTO_H_ 586 struct freebsd4_statfs_args { 587 char *path; 588 struct ostatfs *buf; 589 }; 590 #endif 591 int 592 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap) 593 { 594 struct ostatfs osb; 595 struct statfs *sfp; 596 int error; 597 598 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 599 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp); 600 if (error == 0) { 601 freebsd4_cvtstatfs(sfp, &osb); 602 error = copyout(&osb, uap->buf, sizeof(osb)); 603 } 604 free(sfp, M_STATFS); 605 return (error); 606 } 607 608 /* 609 * Get filesystem statistics. 610 */ 611 #ifndef _SYS_SYSPROTO_H_ 612 struct freebsd4_fstatfs_args { 613 int fd; 614 struct ostatfs *buf; 615 }; 616 #endif 617 int 618 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap) 619 { 620 struct ostatfs osb; 621 struct statfs *sfp; 622 int error; 623 624 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 625 error = kern_fstatfs(td, uap->fd, sfp); 626 if (error == 0) { 627 freebsd4_cvtstatfs(sfp, &osb); 628 error = copyout(&osb, uap->buf, sizeof(osb)); 629 } 630 free(sfp, M_STATFS); 631 return (error); 632 } 633 634 /* 635 * Get statistics on all filesystems. 636 */ 637 #ifndef _SYS_SYSPROTO_H_ 638 struct freebsd4_getfsstat_args { 639 struct ostatfs *buf; 640 long bufsize; 641 int mode; 642 }; 643 #endif 644 int 645 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap) 646 { 647 struct statfs *buf, *sp; 648 struct ostatfs osb; 649 size_t count, size; 650 int error; 651 652 if (uap->bufsize < 0) 653 return (EINVAL); 654 count = uap->bufsize / sizeof(struct ostatfs); 655 if (count > SIZE_MAX / sizeof(struct statfs)) 656 return (EINVAL); 657 size = count * sizeof(struct statfs); 658 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, 659 uap->mode); 660 if (error == 0) 661 td->td_retval[0] = count; 662 if (size != 0) { 663 sp = buf; 664 while (count != 0 && error == 0) { 665 freebsd4_cvtstatfs(sp, &osb); 666 error = copyout(&osb, uap->buf, sizeof(osb)); 667 sp++; 668 uap->buf++; 669 count--; 670 } 671 free(buf, M_STATFS); 672 } 673 return (error); 674 } 675 676 /* 677 * Implement fstatfs() for (NFS) file handles. 678 */ 679 #ifndef _SYS_SYSPROTO_H_ 680 struct freebsd4_fhstatfs_args { 681 struct fhandle *u_fhp; 682 struct ostatfs *buf; 683 }; 684 #endif 685 int 686 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap) 687 { 688 struct ostatfs osb; 689 struct statfs *sfp; 690 fhandle_t fh; 691 int error; 692 693 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 694 if (error != 0) 695 return (error); 696 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 697 error = kern_fhstatfs(td, fh, sfp); 698 if (error == 0) { 699 freebsd4_cvtstatfs(sfp, &osb); 700 error = copyout(&osb, uap->buf, sizeof(osb)); 701 } 702 free(sfp, M_STATFS); 703 return (error); 704 } 705 706 /* 707 * Convert a new format statfs structure to an old format statfs structure. 708 */ 709 static void 710 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp) 711 { 712 713 statfs_scale_blocks(nsp, LONG_MAX); 714 bzero(osp, sizeof(*osp)); 715 osp->f_bsize = nsp->f_bsize; 716 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX); 717 osp->f_blocks = nsp->f_blocks; 718 osp->f_bfree = nsp->f_bfree; 719 osp->f_bavail = nsp->f_bavail; 720 osp->f_files = MIN(nsp->f_files, LONG_MAX); 721 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX); 722 osp->f_owner = nsp->f_owner; 723 osp->f_type = nsp->f_type; 724 osp->f_flags = nsp->f_flags; 725 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX); 726 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX); 727 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX); 728 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX); 729 strlcpy(osp->f_fstypename, nsp->f_fstypename, 730 MIN(MFSNAMELEN, OMFSNAMELEN)); 731 strlcpy(osp->f_mntonname, nsp->f_mntonname, 732 MIN(MNAMELEN, OMNAMELEN)); 733 strlcpy(osp->f_mntfromname, nsp->f_mntfromname, 734 MIN(MNAMELEN, OMNAMELEN)); 735 osp->f_fsid = nsp->f_fsid; 736 } 737 #endif /* COMPAT_FREEBSD4 */ 738 739 #if defined(COMPAT_FREEBSD11) 740 /* 741 * Get old format filesystem statistics. 742 */ 743 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *); 744 745 int 746 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap) 747 { 748 struct freebsd11_statfs osb; 749 struct statfs *sfp; 750 int error; 751 752 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 753 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp); 754 if (error == 0) { 755 freebsd11_cvtstatfs(sfp, &osb); 756 error = copyout(&osb, uap->buf, sizeof(osb)); 757 } 758 free(sfp, M_STATFS); 759 return (error); 760 } 761 762 /* 763 * Get filesystem statistics. 764 */ 765 int 766 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap) 767 { 768 struct freebsd11_statfs osb; 769 struct statfs *sfp; 770 int error; 771 772 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 773 error = kern_fstatfs(td, uap->fd, sfp); 774 if (error == 0) { 775 freebsd11_cvtstatfs(sfp, &osb); 776 error = copyout(&osb, uap->buf, sizeof(osb)); 777 } 778 free(sfp, M_STATFS); 779 return (error); 780 } 781 782 /* 783 * Get statistics on all filesystems. 784 */ 785 int 786 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap) 787 { 788 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode)); 789 } 790 791 int 792 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf, 793 long bufsize, int mode) 794 { 795 struct freebsd11_statfs osb; 796 struct statfs *buf, *sp; 797 size_t count, size; 798 int error; 799 800 if (bufsize < 0) 801 return (EINVAL); 802 803 count = bufsize / sizeof(struct ostatfs); 804 size = count * sizeof(struct statfs); 805 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode); 806 if (error == 0) 807 td->td_retval[0] = count; 808 if (size > 0) { 809 sp = buf; 810 while (count > 0 && error == 0) { 811 freebsd11_cvtstatfs(sp, &osb); 812 error = copyout(&osb, ubuf, sizeof(osb)); 813 sp++; 814 ubuf++; 815 count--; 816 } 817 free(buf, M_STATFS); 818 } 819 return (error); 820 } 821 822 /* 823 * Implement fstatfs() for (NFS) file handles. 824 */ 825 int 826 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap) 827 { 828 struct freebsd11_statfs osb; 829 struct statfs *sfp; 830 fhandle_t fh; 831 int error; 832 833 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 834 if (error) 835 return (error); 836 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 837 error = kern_fhstatfs(td, fh, sfp); 838 if (error == 0) { 839 freebsd11_cvtstatfs(sfp, &osb); 840 error = copyout(&osb, uap->buf, sizeof(osb)); 841 } 842 free(sfp, M_STATFS); 843 return (error); 844 } 845 846 /* 847 * Convert a new format statfs structure to an old format statfs structure. 848 */ 849 static void 850 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp) 851 { 852 853 bzero(osp, sizeof(*osp)); 854 osp->f_version = FREEBSD11_STATFS_VERSION; 855 osp->f_type = nsp->f_type; 856 osp->f_flags = nsp->f_flags; 857 osp->f_bsize = nsp->f_bsize; 858 osp->f_iosize = nsp->f_iosize; 859 osp->f_blocks = nsp->f_blocks; 860 osp->f_bfree = nsp->f_bfree; 861 osp->f_bavail = nsp->f_bavail; 862 osp->f_files = nsp->f_files; 863 osp->f_ffree = nsp->f_ffree; 864 osp->f_syncwrites = nsp->f_syncwrites; 865 osp->f_asyncwrites = nsp->f_asyncwrites; 866 osp->f_syncreads = nsp->f_syncreads; 867 osp->f_asyncreads = nsp->f_asyncreads; 868 osp->f_namemax = nsp->f_namemax; 869 osp->f_owner = nsp->f_owner; 870 osp->f_fsid = nsp->f_fsid; 871 strlcpy(osp->f_fstypename, nsp->f_fstypename, 872 MIN(MFSNAMELEN, sizeof(osp->f_fstypename))); 873 strlcpy(osp->f_mntonname, nsp->f_mntonname, 874 MIN(MNAMELEN, sizeof(osp->f_mntonname))); 875 strlcpy(osp->f_mntfromname, nsp->f_mntfromname, 876 MIN(MNAMELEN, sizeof(osp->f_mntfromname))); 877 } 878 #endif /* COMPAT_FREEBSD11 */ 879 880 /* 881 * Change current working directory to a given file descriptor. 882 */ 883 #ifndef _SYS_SYSPROTO_H_ 884 struct fchdir_args { 885 int fd; 886 }; 887 #endif 888 int 889 sys_fchdir(struct thread *td, struct fchdir_args *uap) 890 { 891 struct vnode *vp, *tdp; 892 struct mount *mp; 893 struct file *fp; 894 int error; 895 uint8_t fdflags; 896 897 AUDIT_ARG_FD(uap->fd); 898 error = getvnode_path(td, uap->fd, &cap_fchdir_rights, &fdflags, 899 &fp); 900 if (error != 0) 901 return (error); 902 if ((fdflags & UF_RESOLVE_BENEATH) != 0) { 903 fdrop(fp, td); 904 return (ENOTCAPABLE); 905 } 906 vp = fp->f_vnode; 907 vrefact(vp); 908 fdrop(fp, td); 909 vn_lock(vp, LK_SHARED | LK_RETRY); 910 AUDIT_ARG_VNODE1(vp); 911 error = change_dir(vp, td); 912 while (!error && (mp = vp->v_mountedhere) != NULL) { 913 if (vfs_busy(mp, 0)) 914 continue; 915 error = VFS_ROOT(mp, LK_SHARED, &tdp); 916 vfs_unbusy(mp); 917 if (error != 0) 918 break; 919 vput(vp); 920 vp = tdp; 921 } 922 if (error != 0) { 923 vput(vp); 924 return (error); 925 } 926 VOP_UNLOCK(vp); 927 pwd_chdir(td, vp); 928 return (0); 929 } 930 931 /* 932 * Change current working directory (``.''). 933 */ 934 #ifndef _SYS_SYSPROTO_H_ 935 struct chdir_args { 936 char *path; 937 }; 938 #endif 939 int 940 sys_chdir(struct thread *td, struct chdir_args *uap) 941 { 942 943 return (kern_chdir(td, uap->path, UIO_USERSPACE)); 944 } 945 946 int 947 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg) 948 { 949 struct nameidata nd; 950 int error; 951 952 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1, 953 pathseg, path); 954 if ((error = namei(&nd)) != 0) 955 return (error); 956 if ((error = change_dir(nd.ni_vp, td)) != 0) { 957 vput(nd.ni_vp); 958 NDFREE_PNBUF(&nd); 959 return (error); 960 } 961 VOP_UNLOCK(nd.ni_vp); 962 NDFREE_PNBUF(&nd); 963 pwd_chdir(td, nd.ni_vp); 964 return (0); 965 } 966 967 static int unprivileged_chroot = 0; 968 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW, 969 &unprivileged_chroot, 0, 970 "Unprivileged processes can use chroot(2)"); 971 972 /* 973 * Takes locked vnode, unlocks it before returning. 974 */ 975 static int 976 kern_chroot(struct thread *td, struct vnode *vp) 977 { 978 struct proc *p; 979 int error; 980 981 error = priv_check(td, PRIV_VFS_CHROOT); 982 if (error != 0) { 983 p = td->td_proc; 984 if (unprivileged_chroot == 0) { 985 error = EXTERROR(EPERM, 986 "security.bsd.unprivileged_chroot sysctl not enabled"); 987 goto e_vunlock; 988 } 989 if ((p->p_flag2 & P2_NO_NEW_PRIVS) == 0) { 990 error = EXTERROR(EPERM, 991 "PROC_NO_NEW_PRIVS not enabled"); 992 goto e_vunlock; 993 } 994 } 995 996 error = change_dir(vp, td); 997 if (error != 0) 998 goto e_vunlock; 999 #ifdef MAC 1000 error = mac_vnode_check_chroot(td->td_ucred, vp); 1001 if (error != 0) 1002 goto e_vunlock; 1003 #endif 1004 VOP_UNLOCK(vp); 1005 error = pwd_chroot(td, vp); 1006 vrele(vp); 1007 return (error); 1008 e_vunlock: 1009 vput(vp); 1010 return (error); 1011 } 1012 1013 /* 1014 * Change notion of root (``/'') directory. 1015 */ 1016 #ifndef _SYS_SYSPROTO_H_ 1017 struct chroot_args { 1018 char *path; 1019 }; 1020 #endif 1021 int 1022 sys_chroot(struct thread *td, struct chroot_args *uap) 1023 { 1024 struct nameidata nd; 1025 int error; 1026 1027 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1, 1028 UIO_USERSPACE, uap->path); 1029 error = namei(&nd); 1030 if (error != 0) 1031 return (error); 1032 NDFREE_PNBUF(&nd); 1033 error = kern_chroot(td, nd.ni_vp); 1034 return (error); 1035 } 1036 1037 /* 1038 * Change notion of root directory to a given file descriptor. 1039 */ 1040 #ifndef _SYS_SYSPROTO_H_ 1041 struct fchroot_args { 1042 int fd; 1043 }; 1044 #endif 1045 int 1046 sys_fchroot(struct thread *td, struct fchroot_args *uap) 1047 { 1048 struct vnode *vp; 1049 struct file *fp; 1050 int error; 1051 uint8_t fdflags; 1052 1053 error = getvnode_path(td, uap->fd, &cap_fchroot_rights, &fdflags, &fp); 1054 if (error != 0) 1055 return (error); 1056 if ((fdflags & UF_RESOLVE_BENEATH) != 0) { 1057 fdrop(fp, td); 1058 return (ENOTCAPABLE); 1059 } 1060 vp = fp->f_vnode; 1061 vrefact(vp); 1062 fdrop(fp, td); 1063 vn_lock(vp, LK_SHARED | LK_RETRY); 1064 error = kern_chroot(td, vp); 1065 return (error); 1066 } 1067 1068 /* 1069 * Common routine for chroot and chdir. Callers must provide a locked vnode 1070 * instance. 1071 */ 1072 int 1073 change_dir(struct vnode *vp, struct thread *td) 1074 { 1075 #ifdef MAC 1076 int error; 1077 #endif 1078 1079 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked"); 1080 if (vp->v_type != VDIR) 1081 return (ENOTDIR); 1082 #ifdef MAC 1083 error = mac_vnode_check_chdir(td->td_ucred, vp); 1084 if (error != 0) 1085 return (error); 1086 #endif 1087 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td)); 1088 } 1089 1090 static __inline void 1091 flags_to_rights(int flags, cap_rights_t *rightsp) 1092 { 1093 if (flags & O_EXEC) { 1094 cap_rights_set_one(rightsp, CAP_FEXECVE); 1095 if (flags & O_PATH) 1096 return; 1097 } else { 1098 switch ((flags & O_ACCMODE)) { 1099 case O_RDONLY: 1100 cap_rights_set_one(rightsp, CAP_READ); 1101 break; 1102 case O_RDWR: 1103 cap_rights_set_one(rightsp, CAP_READ); 1104 /* FALLTHROUGH */ 1105 case O_WRONLY: 1106 cap_rights_set_one(rightsp, CAP_WRITE); 1107 if (!(flags & (O_APPEND | O_TRUNC))) 1108 cap_rights_set_one(rightsp, CAP_SEEK); 1109 break; 1110 } 1111 } 1112 1113 if (flags & O_CREAT) 1114 cap_rights_set_one(rightsp, CAP_CREATE); 1115 1116 if (flags & O_TRUNC) 1117 cap_rights_set_one(rightsp, CAP_FTRUNCATE); 1118 1119 if (flags & (O_SYNC | O_FSYNC | O_DSYNC)) 1120 cap_rights_set_one(rightsp, CAP_FSYNC); 1121 1122 if (flags & (O_EXLOCK | O_SHLOCK)) 1123 cap_rights_set_one(rightsp, CAP_FLOCK); 1124 } 1125 1126 /* 1127 * Check permissions, allocate an open file structure, and call the device 1128 * open routine if any. 1129 */ 1130 #ifndef _SYS_SYSPROTO_H_ 1131 struct open_args { 1132 char *path; 1133 int flags; 1134 int mode; 1135 }; 1136 #endif 1137 int 1138 sys_open(struct thread *td, struct open_args *uap) 1139 { 1140 1141 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1142 uap->flags, uap->mode)); 1143 } 1144 1145 #ifndef _SYS_SYSPROTO_H_ 1146 struct openat_args { 1147 int fd; 1148 char *path; 1149 int flag; 1150 int mode; 1151 }; 1152 #endif 1153 int 1154 sys_openat(struct thread *td, struct openat_args *uap) 1155 { 1156 1157 AUDIT_ARG_FD(uap->fd); 1158 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag, 1159 uap->mode)); 1160 } 1161 1162 /* 1163 * Validate open(2) flags and convert access mode flags (O_RDONLY etc.) to their 1164 * in-kernel representations (FREAD etc.). 1165 */ 1166 static int 1167 openflags(int *flagsp) 1168 { 1169 int flags; 1170 1171 flags = *flagsp; 1172 if ((flags & ~FUSERALLOWED) != 0) 1173 return (EINVAL); 1174 1175 /* 1176 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags 1177 * may be specified. On the other hand, for O_PATH any mode 1178 * except O_EXEC is ignored. 1179 */ 1180 if ((flags & O_PATH) != 0) { 1181 flags &= ~O_ACCMODE; 1182 } else if ((flags & O_EXEC) != 0) { 1183 if ((flags & O_ACCMODE) != 0) 1184 return (EINVAL); 1185 } else if ((flags & O_ACCMODE) == O_ACCMODE) { 1186 return (EINVAL); 1187 } else { 1188 flags = FFLAGS(flags); 1189 } 1190 *flagsp = flags; 1191 return (0); 1192 } 1193 1194 static void 1195 finit_open(struct file *fp, struct vnode *vp, int flags) 1196 { 1197 /* 1198 * Store the vnode, for any f_type. Typically, the vnode use count is 1199 * decremented by a direct call to vnops.fo_close() for files that 1200 * switched type. 1201 */ 1202 fp->f_vnode = vp; 1203 1204 /* 1205 * If the file wasn't claimed by devfs or fifofs, bind it to the normal 1206 * vnode operations here. 1207 */ 1208 if (fp->f_ops == &badfileops) { 1209 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0, 1210 ("Unexpected fifo fp %p vp %p", fp, vp)); 1211 if ((flags & O_PATH) != 0) { 1212 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED), 1213 DTYPE_VNODE, NULL, &path_fileops); 1214 } else { 1215 finit_vnode(fp, flags, NULL, &vnops); 1216 } 1217 } 1218 } 1219 1220 /* 1221 * If fpp != NULL, opened file is not installed into the file 1222 * descriptor table, instead it is returned in *fpp. This is 1223 * incompatible with fdopen(), in which case we return EINVAL. 1224 */ 1225 static int 1226 openatfp(struct thread *td, int dirfd, const char *path, 1227 enum uio_seg pathseg, int flags, int mode, struct file **fpp) 1228 { 1229 struct proc *p; 1230 struct filedesc *fdp; 1231 struct pwddesc *pdp; 1232 struct file *fp; 1233 struct vnode *vp; 1234 struct filecaps *fcaps; 1235 struct nameidata nd; 1236 cap_rights_t rights; 1237 int cmode, error, indx; 1238 1239 indx = -1; 1240 p = td->td_proc; 1241 fdp = p->p_fd; 1242 pdp = p->p_pd; 1243 1244 AUDIT_ARG_FFLAGS(flags); 1245 AUDIT_ARG_MODE(mode); 1246 cap_rights_init_one(&rights, CAP_LOOKUP); 1247 flags_to_rights(flags, &rights); 1248 1249 error = openflags(&flags); 1250 if (error != 0) 1251 return (error); 1252 1253 /* 1254 * Allocate a file structure. The descriptor to reference it 1255 * is allocated and used by finstall_refed() below. 1256 */ 1257 error = falloc_noinstall(td, &fp); 1258 if (error != 0) 1259 return (error); 1260 /* Set the flags early so the finit in devfs can pick them up. */ 1261 fp->f_flag = flags & FMASK; 1262 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT; 1263 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS, 1264 pathseg, path, dirfd, &rights); 1265 td->td_dupfd = -1; /* XXX check for fdopen */ 1266 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS, 1267 td->td_ucred, fp); 1268 if (error != 0) { 1269 /* 1270 * If the vn_open replaced the method vector, something 1271 * wonderous happened deep below and we just pass it up 1272 * pretending we know what we do. 1273 */ 1274 if (error == ENXIO && fp->f_ops != &badfileops) { 1275 MPASS((flags & O_PATH) == 0); 1276 goto success; 1277 } 1278 1279 /* 1280 * Handle special fdopen() case. bleh. 1281 * 1282 * Don't do this for relative (capability) lookups; we don't 1283 * understand exactly what would happen, and we don't think 1284 * that it ever should. 1285 */ 1286 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 && 1287 (error == ENODEV || error == ENXIO) && 1288 td->td_dupfd >= 0) { 1289 MPASS(fpp == NULL); 1290 error = dupfdopen(td, fdp, td->td_dupfd, flags, error, 1291 &indx); 1292 if (error == 0) 1293 goto success; 1294 } 1295 1296 goto bad; 1297 } 1298 td->td_dupfd = 0; 1299 NDFREE_PNBUF(&nd); 1300 vp = nd.ni_vp; 1301 1302 finit_open(fp, vp, flags); 1303 VOP_UNLOCK(vp); 1304 if (flags & O_TRUNC) { 1305 error = fo_truncate(fp, 0, td->td_ucred, td); 1306 if (error != 0) 1307 goto bad; 1308 } 1309 success: 1310 if (fpp != NULL) { 1311 MPASS(error == 0); 1312 NDFREE_IOCTLCAPS(&nd); 1313 *fpp = fp; 1314 return (0); 1315 } 1316 1317 /* 1318 * If we haven't already installed the FD (for dupfdopen), do so now. 1319 */ 1320 if (indx == -1) { 1321 #ifdef CAPABILITIES 1322 if ((nd.ni_resflags & NIRES_STRICTREL) != 0) 1323 fcaps = &nd.ni_filecaps; 1324 else 1325 #endif 1326 fcaps = NULL; 1327 if ((nd.ni_resflags & NIRES_BENEATH) != 0) 1328 flags |= O_RESOLVE_BENEATH; 1329 else 1330 flags &= ~O_RESOLVE_BENEATH; 1331 error = finstall_refed(td, fp, &indx, flags, fcaps); 1332 /* On success finstall_refed() consumes fcaps. */ 1333 if (error != 0) { 1334 goto bad; 1335 } 1336 } else { 1337 NDFREE_IOCTLCAPS(&nd); 1338 falloc_abort(td, fp); 1339 } 1340 1341 td->td_retval[0] = indx; 1342 return (0); 1343 bad: 1344 KASSERT(indx == -1, ("indx=%d, should be -1", indx)); 1345 NDFREE_IOCTLCAPS(&nd); 1346 falloc_abort(td, fp); 1347 return (error); 1348 } 1349 1350 int 1351 kern_openat(struct thread *td, int dirfd, const char *path, 1352 enum uio_seg pathseg, int flags, int mode) 1353 { 1354 return (openatfp(td, dirfd, path, pathseg, flags, mode, NULL)); 1355 } 1356 1357 int 1358 kern_openatfp(struct thread *td, int dirfd, const char *path, 1359 enum uio_seg pathseg, int flags, int mode, struct file **fpp) 1360 { 1361 int error, old_dupfd; 1362 1363 old_dupfd = td->td_dupfd; 1364 td->td_dupfd = -1; 1365 error = openatfp(td, dirfd, path, pathseg, flags, mode, fpp); 1366 td->td_dupfd = old_dupfd; 1367 return (error); 1368 } 1369 1370 #ifdef COMPAT_43 1371 /* 1372 * Create a file. 1373 */ 1374 #ifndef _SYS_SYSPROTO_H_ 1375 struct ocreat_args { 1376 char *path; 1377 int mode; 1378 }; 1379 #endif 1380 int 1381 ocreat(struct thread *td, struct ocreat_args *uap) 1382 { 1383 1384 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1385 O_WRONLY | O_CREAT | O_TRUNC, uap->mode)); 1386 } 1387 #endif /* COMPAT_43 */ 1388 1389 /* 1390 * Create a special file. 1391 */ 1392 #ifndef _SYS_SYSPROTO_H_ 1393 struct mknodat_args { 1394 int fd; 1395 char *path; 1396 mode_t mode; 1397 dev_t dev; 1398 }; 1399 #endif 1400 int 1401 sys_mknodat(struct thread *td, struct mknodat_args *uap) 1402 { 1403 1404 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode, 1405 uap->dev)); 1406 } 1407 1408 #if defined(COMPAT_FREEBSD11) 1409 int 1410 freebsd11_mknod(struct thread *td, 1411 struct freebsd11_mknod_args *uap) 1412 { 1413 1414 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1415 uap->mode, uap->dev)); 1416 } 1417 1418 int 1419 freebsd11_mknodat(struct thread *td, 1420 struct freebsd11_mknodat_args *uap) 1421 { 1422 1423 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode, 1424 uap->dev)); 1425 } 1426 #endif /* COMPAT_FREEBSD11 */ 1427 1428 int 1429 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg, 1430 int mode, dev_t dev) 1431 { 1432 struct vnode *vp; 1433 struct mount *mp; 1434 struct vattr vattr; 1435 struct nameidata nd; 1436 int error, whiteout = 0; 1437 1438 AUDIT_ARG_MODE(mode); 1439 AUDIT_ARG_DEV(dev); 1440 switch (mode & S_IFMT) { 1441 case S_IFCHR: 1442 case S_IFBLK: 1443 error = priv_check(td, PRIV_VFS_MKNOD_DEV); 1444 if (error == 0 && dev == VNOVAL) 1445 error = EINVAL; 1446 break; 1447 case S_IFWHT: 1448 error = priv_check(td, PRIV_VFS_MKNOD_WHT); 1449 break; 1450 case S_IFIFO: 1451 if (dev == 0) 1452 return (kern_mkfifoat(td, fd, path, pathseg, mode)); 1453 /* FALLTHROUGH */ 1454 default: 1455 error = EINVAL; 1456 break; 1457 } 1458 if (error != 0) 1459 return (error); 1460 NDPREINIT(&nd); 1461 restart: 1462 bwillwrite(); 1463 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, 1464 pathseg, path, fd, &cap_mknodat_rights); 1465 if ((error = namei(&nd)) != 0) 1466 return (error); 1467 vp = nd.ni_vp; 1468 if (vp != NULL) { 1469 NDFREE_PNBUF(&nd); 1470 if (vp == nd.ni_dvp) 1471 vrele(nd.ni_dvp); 1472 else 1473 vput(nd.ni_dvp); 1474 vrele(vp); 1475 return (EEXIST); 1476 } else if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) { 1477 NDFREE_PNBUF(&nd); 1478 vput(nd.ni_dvp); 1479 return (EINVAL); 1480 } else { 1481 VATTR_NULL(&vattr); 1482 vattr.va_mode = (mode & ALLPERMS) & 1483 ~td->td_proc->p_pd->pd_cmask; 1484 vattr.va_rdev = dev; 1485 whiteout = 0; 1486 1487 switch (mode & S_IFMT) { 1488 case S_IFCHR: 1489 vattr.va_type = VCHR; 1490 break; 1491 case S_IFBLK: 1492 vattr.va_type = VBLK; 1493 break; 1494 case S_IFWHT: 1495 whiteout = 1; 1496 break; 1497 default: 1498 panic("kern_mknod: invalid mode"); 1499 } 1500 } 1501 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1502 NDFREE_PNBUF(&nd); 1503 vput(nd.ni_dvp); 1504 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0) 1505 return (error); 1506 goto restart; 1507 } 1508 #ifdef MAC 1509 if (error == 0 && !whiteout) 1510 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, 1511 &nd.ni_cnd, &vattr); 1512 #endif 1513 if (error == 0) { 1514 if (whiteout) 1515 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE); 1516 else { 1517 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, 1518 &nd.ni_cnd, &vattr); 1519 } 1520 } 1521 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL, 1522 true); 1523 vn_finished_write(mp); 1524 NDFREE_PNBUF(&nd); 1525 if (error == ERELOOKUP) 1526 goto restart; 1527 return (error); 1528 } 1529 1530 /* 1531 * Create a named pipe. 1532 */ 1533 #ifndef _SYS_SYSPROTO_H_ 1534 struct mkfifo_args { 1535 char *path; 1536 int mode; 1537 }; 1538 #endif 1539 int 1540 sys_mkfifo(struct thread *td, struct mkfifo_args *uap) 1541 { 1542 1543 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 1544 uap->mode)); 1545 } 1546 1547 #ifndef _SYS_SYSPROTO_H_ 1548 struct mkfifoat_args { 1549 int fd; 1550 char *path; 1551 mode_t mode; 1552 }; 1553 #endif 1554 int 1555 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap) 1556 { 1557 1558 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE, 1559 uap->mode)); 1560 } 1561 1562 int 1563 kern_mkfifoat(struct thread *td, int fd, const char *path, 1564 enum uio_seg pathseg, int mode) 1565 { 1566 struct mount *mp; 1567 struct vattr vattr; 1568 struct nameidata nd; 1569 int error; 1570 1571 AUDIT_ARG_MODE(mode); 1572 NDPREINIT(&nd); 1573 restart: 1574 bwillwrite(); 1575 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, 1576 pathseg, path, fd, &cap_mkfifoat_rights); 1577 if ((error = namei(&nd)) != 0) 1578 return (error); 1579 if (nd.ni_vp != NULL) { 1580 NDFREE_PNBUF(&nd); 1581 if (nd.ni_vp == nd.ni_dvp) 1582 vrele(nd.ni_dvp); 1583 else 1584 vput(nd.ni_dvp); 1585 vrele(nd.ni_vp); 1586 return (EEXIST); 1587 } 1588 if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) { 1589 NDFREE_PNBUF(&nd); 1590 vput(nd.ni_dvp); 1591 return (EINVAL); 1592 } 1593 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1594 NDFREE_PNBUF(&nd); 1595 vput(nd.ni_dvp); 1596 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0) 1597 return (error); 1598 goto restart; 1599 } 1600 VATTR_NULL(&vattr); 1601 vattr.va_type = VFIFO; 1602 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask; 1603 #ifdef MAC 1604 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 1605 &vattr); 1606 if (error != 0) 1607 goto out; 1608 #endif 1609 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 1610 #ifdef MAC 1611 out: 1612 #endif 1613 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true); 1614 vn_finished_write(mp); 1615 NDFREE_PNBUF(&nd); 1616 if (error == ERELOOKUP) 1617 goto restart; 1618 return (error); 1619 } 1620 1621 /* 1622 * Make a hard file link. 1623 */ 1624 #ifndef _SYS_SYSPROTO_H_ 1625 struct link_args { 1626 char *path; 1627 char *link; 1628 }; 1629 #endif 1630 int 1631 sys_link(struct thread *td, struct link_args *uap) 1632 { 1633 1634 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link, 1635 UIO_USERSPACE, AT_SYMLINK_FOLLOW)); 1636 } 1637 1638 #ifndef _SYS_SYSPROTO_H_ 1639 struct linkat_args { 1640 int fd1; 1641 char *path1; 1642 int fd2; 1643 char *path2; 1644 int flag; 1645 }; 1646 #endif 1647 int 1648 sys_linkat(struct thread *td, struct linkat_args *uap) 1649 { 1650 1651 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2, 1652 UIO_USERSPACE, uap->flag)); 1653 } 1654 1655 int hardlink_check_uid = 0; 1656 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW, 1657 &hardlink_check_uid, 0, 1658 "Unprivileged processes cannot create hard links to files owned by other " 1659 "users"); 1660 static int hardlink_check_gid = 0; 1661 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW, 1662 &hardlink_check_gid, 0, 1663 "Unprivileged processes cannot create hard links to files owned by other " 1664 "groups"); 1665 1666 static int 1667 can_hardlink(struct vnode *vp, struct ucred *cred) 1668 { 1669 struct vattr va; 1670 int error; 1671 1672 if (!hardlink_check_uid && !hardlink_check_gid) 1673 return (0); 1674 1675 error = VOP_GETATTR(vp, &va, cred); 1676 if (error != 0) 1677 return (error); 1678 1679 if (hardlink_check_uid && cred->cr_uid != va.va_uid) { 1680 error = priv_check_cred(cred, PRIV_VFS_LINK); 1681 if (error != 0) 1682 return (error); 1683 } 1684 1685 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) { 1686 error = priv_check_cred(cred, PRIV_VFS_LINK); 1687 if (error != 0) 1688 return (error); 1689 } 1690 1691 return (0); 1692 } 1693 1694 int 1695 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1, 1696 const char *path2, enum uio_seg segflag, int flag) 1697 { 1698 struct nameidata nd; 1699 int error; 1700 1701 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | 1702 AT_EMPTY_PATH)) != 0) 1703 return (EINVAL); 1704 1705 NDPREINIT(&nd); 1706 do { 1707 bwillwrite(); 1708 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag, 1709 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH), 1710 segflag, path1, fd1, &cap_linkat_source_rights); 1711 if ((error = namei(&nd)) != 0) 1712 return (error); 1713 NDFREE_PNBUF(&nd); 1714 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) { 1715 error = priv_check(td, PRIV_VFS_FHOPEN); 1716 if (error != 0) { 1717 vrele(nd.ni_vp); 1718 return (error); 1719 } 1720 } 1721 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag); 1722 } while (error == EAGAIN || error == ERELOOKUP); 1723 return (error); 1724 } 1725 1726 static int 1727 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path, 1728 enum uio_seg segflag) 1729 { 1730 struct nameidata nd; 1731 struct mount *mp; 1732 int error; 1733 1734 if (vp->v_type == VDIR) { 1735 vrele(vp); 1736 return (EPERM); /* POSIX */ 1737 } 1738 if ((vn_irflag_read(vp) & (VIRF_NAMEDDIR | VIRF_NAMEDATTR)) != 0) { 1739 vrele(vp); 1740 return (EINVAL); 1741 } 1742 NDINIT_ATRIGHTS(&nd, CREATE, 1743 LOCKPARENT | AUDITVNODE2 | NOCACHE, segflag, path, fd, 1744 &cap_linkat_target_rights); 1745 if ((error = namei(&nd)) == 0) { 1746 if (nd.ni_vp != NULL) { 1747 NDFREE_PNBUF(&nd); 1748 if (nd.ni_dvp == nd.ni_vp) 1749 vrele(nd.ni_dvp); 1750 else 1751 vput(nd.ni_dvp); 1752 vrele(nd.ni_vp); 1753 vrele(vp); 1754 return (EEXIST); 1755 } else if (nd.ni_dvp->v_mount != vp->v_mount) { 1756 /* 1757 * Cross-device link. No need to recheck 1758 * vp->v_type, since it cannot change, except 1759 * to VBAD. 1760 */ 1761 NDFREE_PNBUF(&nd); 1762 vput(nd.ni_dvp); 1763 vrele(vp); 1764 return (EXDEV); 1765 } else if (vn_lock(vp, LK_EXCLUSIVE) == 0) { 1766 error = can_hardlink(vp, td->td_ucred); 1767 #ifdef MAC 1768 if (error == 0) 1769 error = mac_vnode_check_link(td->td_ucred, 1770 nd.ni_dvp, vp, &nd.ni_cnd); 1771 #endif 1772 if (error != 0) { 1773 vput(vp); 1774 vput(nd.ni_dvp); 1775 NDFREE_PNBUF(&nd); 1776 return (error); 1777 } 1778 error = vn_start_write(vp, &mp, V_NOWAIT); 1779 if (error != 0) { 1780 vput(vp); 1781 vput(nd.ni_dvp); 1782 NDFREE_PNBUF(&nd); 1783 error = vn_start_write(NULL, &mp, 1784 V_XSLEEP | V_PCATCH); 1785 if (error != 0) 1786 return (error); 1787 return (EAGAIN); 1788 } 1789 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd); 1790 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true); 1791 vn_finished_write(mp); 1792 NDFREE_PNBUF(&nd); 1793 vp = NULL; 1794 } else { 1795 vput(nd.ni_dvp); 1796 NDFREE_PNBUF(&nd); 1797 vrele(vp); 1798 return (EAGAIN); 1799 } 1800 } 1801 if (vp != NULL) 1802 vrele(vp); 1803 return (error); 1804 } 1805 1806 /* 1807 * Make a symbolic link. 1808 */ 1809 #ifndef _SYS_SYSPROTO_H_ 1810 struct symlink_args { 1811 char *path; 1812 char *link; 1813 }; 1814 #endif 1815 int 1816 sys_symlink(struct thread *td, struct symlink_args *uap) 1817 { 1818 1819 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link, 1820 UIO_USERSPACE)); 1821 } 1822 1823 #ifndef _SYS_SYSPROTO_H_ 1824 struct symlinkat_args { 1825 char *path; 1826 int fd; 1827 char *path2; 1828 }; 1829 #endif 1830 int 1831 sys_symlinkat(struct thread *td, struct symlinkat_args *uap) 1832 { 1833 1834 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2, 1835 UIO_USERSPACE)); 1836 } 1837 1838 int 1839 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2, 1840 enum uio_seg segflg) 1841 { 1842 struct mount *mp; 1843 struct vattr vattr; 1844 const char *syspath; 1845 char *tmppath; 1846 struct nameidata nd; 1847 int error; 1848 1849 if (segflg == UIO_SYSSPACE) { 1850 syspath = path1; 1851 } else { 1852 tmppath = uma_zalloc(namei_zone, M_WAITOK); 1853 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0) 1854 goto out; 1855 syspath = tmppath; 1856 } 1857 AUDIT_ARG_TEXT(syspath); 1858 NDPREINIT(&nd); 1859 restart: 1860 bwillwrite(); 1861 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, segflg, 1862 path2, fd, &cap_symlinkat_rights); 1863 if ((error = namei(&nd)) != 0) 1864 goto out; 1865 if (nd.ni_vp) { 1866 NDFREE_PNBUF(&nd); 1867 if (nd.ni_vp == nd.ni_dvp) 1868 vrele(nd.ni_dvp); 1869 else 1870 vput(nd.ni_dvp); 1871 vrele(nd.ni_vp); 1872 nd.ni_vp = NULL; 1873 error = EEXIST; 1874 goto out; 1875 } 1876 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1877 NDFREE_PNBUF(&nd); 1878 vput(nd.ni_dvp); 1879 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0) 1880 goto out; 1881 goto restart; 1882 } 1883 if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) { 1884 error = EINVAL; 1885 goto out2; 1886 } 1887 VATTR_NULL(&vattr); 1888 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask; 1889 #ifdef MAC 1890 vattr.va_type = VLNK; 1891 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 1892 &vattr); 1893 if (error != 0) 1894 goto out2; 1895 #endif 1896 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath); 1897 out2: 1898 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true); 1899 vn_finished_write(mp); 1900 NDFREE_PNBUF(&nd); 1901 if (error == ERELOOKUP) 1902 goto restart; 1903 out: 1904 if (segflg != UIO_SYSSPACE) 1905 uma_zfree(namei_zone, tmppath); 1906 return (error); 1907 } 1908 1909 /* 1910 * Delete a whiteout from the filesystem. 1911 */ 1912 #ifndef _SYS_SYSPROTO_H_ 1913 struct undelete_args { 1914 char *path; 1915 }; 1916 #endif 1917 int 1918 sys_undelete(struct thread *td, struct undelete_args *uap) 1919 { 1920 struct mount *mp; 1921 struct nameidata nd; 1922 int error; 1923 1924 NDPREINIT(&nd); 1925 restart: 1926 bwillwrite(); 1927 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1, 1928 UIO_USERSPACE, uap->path); 1929 error = namei(&nd); 1930 if (error != 0) 1931 return (error); 1932 1933 if (nd.ni_vp != NULL || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) { 1934 NDFREE_PNBUF(&nd); 1935 if (nd.ni_vp == nd.ni_dvp) 1936 vrele(nd.ni_dvp); 1937 else 1938 vput(nd.ni_dvp); 1939 if (nd.ni_vp) 1940 vrele(nd.ni_vp); 1941 return (EEXIST); 1942 } 1943 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 1944 NDFREE_PNBUF(&nd); 1945 vput(nd.ni_dvp); 1946 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0) 1947 return (error); 1948 goto restart; 1949 } 1950 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE); 1951 NDFREE_PNBUF(&nd); 1952 vput(nd.ni_dvp); 1953 vn_finished_write(mp); 1954 if (error == ERELOOKUP) 1955 goto restart; 1956 return (error); 1957 } 1958 1959 /* 1960 * Delete a name from the filesystem. 1961 */ 1962 #ifndef _SYS_SYSPROTO_H_ 1963 struct unlink_args { 1964 char *path; 1965 }; 1966 #endif 1967 int 1968 sys_unlink(struct thread *td, struct unlink_args *uap) 1969 { 1970 1971 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE, 1972 0, 0)); 1973 } 1974 1975 static int 1976 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd, 1977 int flags, enum uio_seg pathseg, ino_t oldinum) 1978 { 1979 1980 if ((flags & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0) 1981 return (EINVAL); 1982 1983 if ((flags & AT_REMOVEDIR) != 0) 1984 return (kern_frmdirat(td, dfd, path, fd, pathseg, 1985 flags & ~AT_REMOVEDIR)); 1986 1987 return (kern_funlinkat(td, dfd, path, fd, pathseg, flags, 0)); 1988 } 1989 1990 #ifndef _SYS_SYSPROTO_H_ 1991 struct unlinkat_args { 1992 int fd; 1993 char *path; 1994 int flag; 1995 }; 1996 #endif 1997 int 1998 sys_unlinkat(struct thread *td, struct unlinkat_args *uap) 1999 { 2000 2001 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag, 2002 UIO_USERSPACE, 0)); 2003 } 2004 2005 #ifndef _SYS_SYSPROTO_H_ 2006 struct funlinkat_args { 2007 int dfd; 2008 const char *path; 2009 int fd; 2010 int flag; 2011 }; 2012 #endif 2013 int 2014 sys_funlinkat(struct thread *td, struct funlinkat_args *uap) 2015 { 2016 2017 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag, 2018 UIO_USERSPACE, 0)); 2019 } 2020 2021 int 2022 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd, 2023 enum uio_seg pathseg, int flag, ino_t oldinum) 2024 { 2025 struct mount *mp; 2026 struct file *fp; 2027 struct vnode *vp; 2028 struct nameidata nd; 2029 struct stat sb; 2030 int error; 2031 2032 fp = NULL; 2033 if (fd != FD_NONE) { 2034 error = getvnode_path(td, fd, &cap_no_rights, NULL, &fp); 2035 if (error != 0) 2036 return (error); 2037 } 2038 2039 NDPREINIT(&nd); 2040 restart: 2041 bwillwrite(); 2042 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 | 2043 at2cnpflags(flag, AT_RESOLVE_BENEATH), 2044 pathseg, path, dfd, &cap_unlinkat_rights); 2045 if ((error = namei(&nd)) != 0) { 2046 if (error == EINVAL) 2047 error = EPERM; 2048 goto fdout; 2049 } 2050 vp = nd.ni_vp; 2051 if (vp->v_type == VDIR && oldinum == 0) { 2052 error = EPERM; /* POSIX */ 2053 } else if (oldinum != 0 && 2054 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) && 2055 sb.st_ino != oldinum) { 2056 error = EIDRM; /* Identifier removed */ 2057 } else if (fp != NULL && fp->f_vnode != vp) { 2058 if (VN_IS_DOOMED(fp->f_vnode)) 2059 error = EBADF; 2060 else 2061 error = EDEADLK; 2062 } else { 2063 /* 2064 * The root of a mounted filesystem cannot be deleted. 2065 * 2066 * XXX: can this only be a VDIR case? 2067 */ 2068 if (vp->v_vflag & VV_ROOT) 2069 error = EBUSY; 2070 } 2071 if (error == 0) { 2072 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 2073 NDFREE_PNBUF(&nd); 2074 vput(nd.ni_dvp); 2075 if (vp == nd.ni_dvp) 2076 vrele(vp); 2077 else 2078 vput(vp); 2079 if ((error = vn_start_write(NULL, &mp, 2080 V_XSLEEP | V_PCATCH)) != 0) { 2081 goto fdout; 2082 } 2083 goto restart; 2084 } 2085 #ifdef MAC 2086 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp, 2087 &nd.ni_cnd); 2088 if (error != 0) 2089 goto out; 2090 #endif 2091 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd); 2092 #ifdef MAC 2093 out: 2094 #endif 2095 vn_finished_write(mp); 2096 } 2097 NDFREE_PNBUF(&nd); 2098 vput(nd.ni_dvp); 2099 if (vp == nd.ni_dvp) 2100 vrele(vp); 2101 else 2102 vput(vp); 2103 if (error == ERELOOKUP) 2104 goto restart; 2105 fdout: 2106 if (fp != NULL) 2107 fdrop(fp, td); 2108 return (error); 2109 } 2110 2111 /* 2112 * Reposition read/write file offset. 2113 */ 2114 #ifndef _SYS_SYSPROTO_H_ 2115 struct lseek_args { 2116 int fd; 2117 int pad; 2118 off_t offset; 2119 int whence; 2120 }; 2121 #endif 2122 int 2123 sys_lseek(struct thread *td, struct lseek_args *uap) 2124 { 2125 2126 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 2127 } 2128 2129 int 2130 kern_lseek(struct thread *td, int fd, off_t offset, int whence) 2131 { 2132 struct file *fp; 2133 int error; 2134 2135 AUDIT_ARG_FD(fd); 2136 error = fget(td, fd, &cap_seek_rights, &fp); 2137 if (error != 0) 2138 return (error); 2139 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ? 2140 fo_seek(fp, offset, whence, td) : ESPIPE; 2141 fdrop(fp, td); 2142 return (error); 2143 } 2144 2145 #if defined(COMPAT_43) 2146 /* 2147 * Reposition read/write file offset. 2148 */ 2149 #ifndef _SYS_SYSPROTO_H_ 2150 struct olseek_args { 2151 int fd; 2152 long offset; 2153 int whence; 2154 }; 2155 #endif 2156 int 2157 olseek(struct thread *td, struct olseek_args *uap) 2158 { 2159 2160 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 2161 } 2162 #endif /* COMPAT_43 */ 2163 2164 #if defined(COMPAT_FREEBSD6) 2165 /* Version with the 'pad' argument */ 2166 int 2167 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap) 2168 { 2169 2170 return (kern_lseek(td, uap->fd, uap->offset, uap->whence)); 2171 } 2172 #endif 2173 2174 /* 2175 * Check access permissions using passed credentials. 2176 */ 2177 static int 2178 vn_access(struct vnode *vp, int user_flags, struct ucred *cred, 2179 struct thread *td) 2180 { 2181 accmode_t accmode; 2182 int error; 2183 2184 /* Flags == 0 means only check for existence. */ 2185 if (user_flags == 0) 2186 return (0); 2187 2188 accmode = 0; 2189 if (user_flags & R_OK) 2190 accmode |= VREAD; 2191 if (user_flags & W_OK) 2192 accmode |= VWRITE; 2193 if (user_flags & X_OK) 2194 accmode |= VEXEC; 2195 #ifdef MAC 2196 error = mac_vnode_check_access(cred, vp, accmode); 2197 if (error != 0) 2198 return (error); 2199 #endif 2200 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0) 2201 error = VOP_ACCESS(vp, accmode, cred, td); 2202 return (error); 2203 } 2204 2205 /* 2206 * Check access permissions using "real" credentials. 2207 */ 2208 #ifndef _SYS_SYSPROTO_H_ 2209 struct access_args { 2210 char *path; 2211 int amode; 2212 }; 2213 #endif 2214 int 2215 sys_access(struct thread *td, struct access_args *uap) 2216 { 2217 2218 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2219 0, uap->amode)); 2220 } 2221 2222 #ifndef _SYS_SYSPROTO_H_ 2223 struct faccessat_args { 2224 int dirfd; 2225 char *path; 2226 int amode; 2227 int flag; 2228 } 2229 #endif 2230 int 2231 sys_faccessat(struct thread *td, struct faccessat_args *uap) 2232 { 2233 2234 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag, 2235 uap->amode)); 2236 } 2237 2238 int 2239 kern_accessat(struct thread *td, int fd, const char *path, 2240 enum uio_seg pathseg, int flag, int amode) 2241 { 2242 struct ucred *cred, *usecred; 2243 struct vnode *vp; 2244 struct nameidata nd; 2245 int error; 2246 2247 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH | 2248 AT_SYMLINK_NOFOLLOW)) != 0) 2249 return (EINVAL); 2250 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0) 2251 return (EINVAL); 2252 2253 /* 2254 * Create and modify a temporary credential instead of one that 2255 * is potentially shared (if we need one). 2256 */ 2257 cred = td->td_ucred; 2258 if ((flag & AT_EACCESS) == 0 && 2259 ((cred->cr_uid != cred->cr_ruid || 2260 cred->cr_rgid != cred->cr_gid))) { 2261 usecred = crdup(cred); 2262 usecred->cr_uid = cred->cr_ruid; 2263 usecred->cr_gid = cred->cr_rgid; 2264 td->td_ucred = usecred; 2265 } else 2266 usecred = cred; 2267 AUDIT_ARG_VALUE(amode); 2268 NDINIT_ATRIGHTS(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | 2269 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH | AT_SYMLINK_NOFOLLOW | 2270 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights); 2271 if ((error = namei(&nd)) != 0) 2272 goto out; 2273 vp = nd.ni_vp; 2274 2275 error = vn_access(vp, amode, usecred, td); 2276 NDFREE_PNBUF(&nd); 2277 vput(vp); 2278 out: 2279 if (usecred != cred) { 2280 td->td_ucred = cred; 2281 crfree(usecred); 2282 } 2283 return (error); 2284 } 2285 2286 /* 2287 * Check access permissions using "effective" credentials. 2288 */ 2289 #ifndef _SYS_SYSPROTO_H_ 2290 struct eaccess_args { 2291 char *path; 2292 int amode; 2293 }; 2294 #endif 2295 int 2296 sys_eaccess(struct thread *td, struct eaccess_args *uap) 2297 { 2298 2299 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2300 AT_EACCESS, uap->amode)); 2301 } 2302 2303 #if defined(COMPAT_43) 2304 /* 2305 * Get file status; this version follows links. 2306 */ 2307 #ifndef _SYS_SYSPROTO_H_ 2308 struct ostat_args { 2309 char *path; 2310 struct ostat *ub; 2311 }; 2312 #endif 2313 int 2314 ostat(struct thread *td, struct ostat_args *uap) 2315 { 2316 struct stat sb; 2317 struct ostat osb; 2318 int error; 2319 2320 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb); 2321 if (error != 0) 2322 return (error); 2323 cvtstat(&sb, &osb); 2324 return (copyout(&osb, uap->ub, sizeof (osb))); 2325 } 2326 2327 /* 2328 * Get file status; this version does not follow links. 2329 */ 2330 #ifndef _SYS_SYSPROTO_H_ 2331 struct olstat_args { 2332 char *path; 2333 struct ostat *ub; 2334 }; 2335 #endif 2336 int 2337 olstat(struct thread *td, struct olstat_args *uap) 2338 { 2339 struct stat sb; 2340 struct ostat osb; 2341 int error; 2342 2343 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2344 UIO_USERSPACE, &sb); 2345 if (error != 0) 2346 return (error); 2347 cvtstat(&sb, &osb); 2348 return (copyout(&osb, uap->ub, sizeof (osb))); 2349 } 2350 2351 /* 2352 * Convert from an old to a new stat structure. 2353 * XXX: many values are blindly truncated. 2354 */ 2355 void 2356 cvtstat(struct stat *st, struct ostat *ost) 2357 { 2358 2359 bzero(ost, sizeof(*ost)); 2360 ost->st_dev = st->st_dev; 2361 ost->st_ino = st->st_ino; 2362 ost->st_mode = st->st_mode; 2363 ost->st_nlink = st->st_nlink; 2364 ost->st_uid = st->st_uid; 2365 ost->st_gid = st->st_gid; 2366 ost->st_rdev = st->st_rdev; 2367 ost->st_size = MIN(st->st_size, INT32_MAX); 2368 ost->st_atim = st->st_atim; 2369 ost->st_mtim = st->st_mtim; 2370 ost->st_ctim = st->st_ctim; 2371 ost->st_blksize = st->st_blksize; 2372 ost->st_blocks = st->st_blocks; 2373 ost->st_flags = st->st_flags; 2374 ost->st_gen = st->st_gen; 2375 } 2376 #endif /* COMPAT_43 */ 2377 2378 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11) 2379 int ino64_trunc_error; 2380 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW, 2381 &ino64_trunc_error, 0, 2382 "Error on truncation of device, file or inode number, or link count"); 2383 2384 int 2385 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost) 2386 { 2387 2388 ost->st_dev = st->st_dev; 2389 if (ost->st_dev != st->st_dev) { 2390 switch (ino64_trunc_error) { 2391 default: 2392 /* 2393 * Since dev_t is almost raw, don't clamp to the 2394 * maximum for case 2, but ignore the error. 2395 */ 2396 break; 2397 case 1: 2398 return (EOVERFLOW); 2399 } 2400 } 2401 ost->st_ino = st->st_ino; 2402 if (ost->st_ino != st->st_ino) { 2403 switch (ino64_trunc_error) { 2404 default: 2405 case 0: 2406 break; 2407 case 1: 2408 return (EOVERFLOW); 2409 case 2: 2410 ost->st_ino = UINT32_MAX; 2411 break; 2412 } 2413 } 2414 ost->st_mode = st->st_mode; 2415 ost->st_nlink = st->st_nlink; 2416 if (ost->st_nlink != st->st_nlink) { 2417 switch (ino64_trunc_error) { 2418 default: 2419 case 0: 2420 break; 2421 case 1: 2422 return (EOVERFLOW); 2423 case 2: 2424 ost->st_nlink = UINT16_MAX; 2425 break; 2426 } 2427 } 2428 ost->st_uid = st->st_uid; 2429 ost->st_gid = st->st_gid; 2430 ost->st_rdev = st->st_rdev; 2431 if (ost->st_rdev != st->st_rdev) { 2432 switch (ino64_trunc_error) { 2433 default: 2434 break; 2435 case 1: 2436 return (EOVERFLOW); 2437 } 2438 } 2439 ost->st_atim = st->st_atim; 2440 ost->st_mtim = st->st_mtim; 2441 ost->st_ctim = st->st_ctim; 2442 ost->st_size = st->st_size; 2443 ost->st_blocks = st->st_blocks; 2444 ost->st_blksize = st->st_blksize; 2445 ost->st_flags = st->st_flags; 2446 ost->st_gen = st->st_gen; 2447 ost->st_lspare = 0; 2448 ost->st_birthtim = st->st_birthtim; 2449 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim), 2450 sizeof(*ost) - offsetof(struct freebsd11_stat, 2451 st_birthtim) - sizeof(ost->st_birthtim)); 2452 return (0); 2453 } 2454 2455 int 2456 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap) 2457 { 2458 struct stat sb; 2459 struct freebsd11_stat osb; 2460 int error; 2461 2462 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb); 2463 if (error != 0) 2464 return (error); 2465 error = freebsd11_cvtstat(&sb, &osb); 2466 if (error == 0) 2467 error = copyout(&osb, uap->ub, sizeof(osb)); 2468 return (error); 2469 } 2470 2471 int 2472 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap) 2473 { 2474 struct stat sb; 2475 struct freebsd11_stat osb; 2476 int error; 2477 2478 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2479 UIO_USERSPACE, &sb); 2480 if (error != 0) 2481 return (error); 2482 error = freebsd11_cvtstat(&sb, &osb); 2483 if (error == 0) 2484 error = copyout(&osb, uap->ub, sizeof(osb)); 2485 return (error); 2486 } 2487 2488 int 2489 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap) 2490 { 2491 struct fhandle fh; 2492 struct stat sb; 2493 struct freebsd11_stat osb; 2494 int error; 2495 2496 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 2497 if (error != 0) 2498 return (error); 2499 error = kern_fhstat(td, fh, &sb); 2500 if (error != 0) 2501 return (error); 2502 error = freebsd11_cvtstat(&sb, &osb); 2503 if (error == 0) 2504 error = copyout(&osb, uap->sb, sizeof(osb)); 2505 return (error); 2506 } 2507 2508 int 2509 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap) 2510 { 2511 struct stat sb; 2512 struct freebsd11_stat osb; 2513 int error; 2514 2515 error = kern_statat(td, uap->flag, uap->fd, uap->path, 2516 UIO_USERSPACE, &sb); 2517 if (error != 0) 2518 return (error); 2519 error = freebsd11_cvtstat(&sb, &osb); 2520 if (error == 0) 2521 error = copyout(&osb, uap->buf, sizeof(osb)); 2522 return (error); 2523 } 2524 #endif /* COMPAT_FREEBSD11 */ 2525 2526 /* 2527 * Get file status 2528 */ 2529 #ifndef _SYS_SYSPROTO_H_ 2530 struct fstatat_args { 2531 int fd; 2532 char *path; 2533 struct stat *buf; 2534 int flag; 2535 } 2536 #endif 2537 int 2538 sys_fstatat(struct thread *td, struct fstatat_args *uap) 2539 { 2540 struct stat sb; 2541 int error; 2542 2543 error = kern_statat(td, uap->flag, uap->fd, uap->path, 2544 UIO_USERSPACE, &sb); 2545 if (error == 0) 2546 error = copyout(&sb, uap->buf, sizeof (sb)); 2547 return (error); 2548 } 2549 2550 int 2551 kern_statat(struct thread *td, int flag, int fd, const char *path, 2552 enum uio_seg pathseg, struct stat *sbp) 2553 { 2554 struct nameidata nd; 2555 int error; 2556 2557 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH | 2558 AT_EMPTY_PATH)) != 0) 2559 return (EINVAL); 2560 2561 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH | 2562 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF | 2563 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights); 2564 2565 if ((error = namei(&nd)) != 0) { 2566 if (error == ENOTDIR && 2567 (nd.ni_resflags & NIRES_EMPTYPATH) != 0) 2568 error = kern_fstat(td, fd, sbp); 2569 return (error); 2570 } 2571 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED); 2572 NDFREE_PNBUF(&nd); 2573 if (error == 0 && (nd.ni_vp->v_vflag & VV_ROOT) != 0) 2574 sbp->st_bsdflags |= SFBSD_MNTPOINT; 2575 vput(nd.ni_vp); 2576 #ifdef __STAT_TIME_T_EXT 2577 sbp->st_atim_ext = 0; 2578 sbp->st_mtim_ext = 0; 2579 sbp->st_ctim_ext = 0; 2580 sbp->st_btim_ext = 0; 2581 #endif 2582 #ifdef KTRACE 2583 if (KTRPOINT(td, KTR_STRUCT)) 2584 ktrstat_error(sbp, error); 2585 #endif 2586 return (error); 2587 } 2588 2589 #if defined(COMPAT_FREEBSD11) 2590 /* 2591 * Implementation of the NetBSD [l]stat() functions. 2592 */ 2593 int 2594 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb) 2595 { 2596 struct freebsd11_stat sb11; 2597 int error; 2598 2599 error = freebsd11_cvtstat(sb, &sb11); 2600 if (error != 0) 2601 return (error); 2602 2603 bzero(nsb, sizeof(*nsb)); 2604 CP(sb11, *nsb, st_dev); 2605 CP(sb11, *nsb, st_ino); 2606 CP(sb11, *nsb, st_mode); 2607 CP(sb11, *nsb, st_nlink); 2608 CP(sb11, *nsb, st_uid); 2609 CP(sb11, *nsb, st_gid); 2610 CP(sb11, *nsb, st_rdev); 2611 CP(sb11, *nsb, st_atim); 2612 CP(sb11, *nsb, st_mtim); 2613 CP(sb11, *nsb, st_ctim); 2614 CP(sb11, *nsb, st_size); 2615 CP(sb11, *nsb, st_blocks); 2616 CP(sb11, *nsb, st_blksize); 2617 CP(sb11, *nsb, st_flags); 2618 CP(sb11, *nsb, st_gen); 2619 CP(sb11, *nsb, st_birthtim); 2620 return (0); 2621 } 2622 2623 #ifndef _SYS_SYSPROTO_H_ 2624 struct freebsd11_nstat_args { 2625 char *path; 2626 struct nstat *ub; 2627 }; 2628 #endif 2629 int 2630 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap) 2631 { 2632 struct stat sb; 2633 struct nstat nsb; 2634 int error; 2635 2636 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb); 2637 if (error != 0) 2638 return (error); 2639 error = freebsd11_cvtnstat(&sb, &nsb); 2640 if (error == 0) 2641 error = copyout(&nsb, uap->ub, sizeof (nsb)); 2642 return (error); 2643 } 2644 2645 /* 2646 * NetBSD lstat. Get file status; this version does not follow links. 2647 */ 2648 #ifndef _SYS_SYSPROTO_H_ 2649 struct freebsd11_nlstat_args { 2650 char *path; 2651 struct nstat *ub; 2652 }; 2653 #endif 2654 int 2655 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap) 2656 { 2657 struct stat sb; 2658 struct nstat nsb; 2659 int error; 2660 2661 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path, 2662 UIO_USERSPACE, &sb); 2663 if (error != 0) 2664 return (error); 2665 error = freebsd11_cvtnstat(&sb, &nsb); 2666 if (error == 0) 2667 error = copyout(&nsb, uap->ub, sizeof (nsb)); 2668 return (error); 2669 } 2670 #endif /* COMPAT_FREEBSD11 */ 2671 2672 /* 2673 * Get configurable pathname variables. 2674 */ 2675 #ifndef _SYS_SYSPROTO_H_ 2676 struct pathconf_args { 2677 char *path; 2678 int name; 2679 }; 2680 #endif 2681 int 2682 sys_pathconf(struct thread *td, struct pathconf_args *uap) 2683 { 2684 long value; 2685 int error; 2686 2687 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW, 2688 &value); 2689 if (error == 0) 2690 td->td_retval[0] = value; 2691 return (error); 2692 } 2693 2694 #ifndef _SYS_SYSPROTO_H_ 2695 struct lpathconf_args { 2696 char *path; 2697 int name; 2698 }; 2699 #endif 2700 int 2701 sys_lpathconf(struct thread *td, struct lpathconf_args *uap) 2702 { 2703 long value; 2704 int error; 2705 2706 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, 2707 NOFOLLOW, &value); 2708 if (error == 0) 2709 td->td_retval[0] = value; 2710 return (error); 2711 } 2712 2713 int 2714 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg, 2715 int name, u_long flags, long *valuep) 2716 { 2717 struct nameidata nd; 2718 int error; 2719 2720 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags, 2721 pathseg, path); 2722 if ((error = namei(&nd)) != 0) 2723 return (error); 2724 NDFREE_PNBUF(&nd); 2725 2726 error = VOP_PATHCONF(nd.ni_vp, name, valuep); 2727 vput(nd.ni_vp); 2728 return (error); 2729 } 2730 2731 /* 2732 * Return target name of a symbolic link. 2733 */ 2734 #ifndef _SYS_SYSPROTO_H_ 2735 struct readlink_args { 2736 char *path; 2737 char *buf; 2738 size_t count; 2739 }; 2740 #endif 2741 int 2742 sys_readlink(struct thread *td, struct readlink_args *uap) 2743 { 2744 2745 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2746 uap->buf, UIO_USERSPACE, uap->count)); 2747 } 2748 #ifndef _SYS_SYSPROTO_H_ 2749 struct readlinkat_args { 2750 int fd; 2751 char *path; 2752 char *buf; 2753 size_t bufsize; 2754 }; 2755 #endif 2756 int 2757 sys_readlinkat(struct thread *td, struct readlinkat_args *uap) 2758 { 2759 2760 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE, 2761 uap->buf, UIO_USERSPACE, uap->bufsize)); 2762 } 2763 2764 int 2765 kern_readlinkat(struct thread *td, int fd, const char *path, 2766 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count) 2767 { 2768 struct vnode *vp; 2769 struct nameidata nd; 2770 int error; 2771 2772 if (count > IOSIZE_MAX) 2773 return (EINVAL); 2774 2775 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 | 2776 EMPTYPATH, pathseg, path, fd); 2777 2778 if ((error = namei(&nd)) != 0) 2779 return (error); 2780 NDFREE_PNBUF(&nd); 2781 vp = nd.ni_vp; 2782 2783 error = kern_readlink_vp(vp, buf, bufseg, count, td); 2784 vput(vp); 2785 2786 return (error); 2787 } 2788 2789 /* 2790 * Helper function to readlink from a vnode 2791 */ 2792 static int 2793 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count, 2794 struct thread *td) 2795 { 2796 struct iovec aiov; 2797 struct uio auio; 2798 int error; 2799 2800 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked"); 2801 #ifdef MAC 2802 error = mac_vnode_check_readlink(td->td_ucred, vp); 2803 if (error != 0) 2804 return (error); 2805 #endif 2806 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0) 2807 return (EINVAL); 2808 2809 aiov.iov_base = buf; 2810 aiov.iov_len = count; 2811 auio.uio_iov = &aiov; 2812 auio.uio_iovcnt = 1; 2813 auio.uio_offset = 0; 2814 auio.uio_rw = UIO_READ; 2815 auio.uio_segflg = bufseg; 2816 auio.uio_td = td; 2817 auio.uio_resid = count; 2818 error = VOP_READLINK(vp, &auio, td->td_ucred); 2819 td->td_retval[0] = count - auio.uio_resid; 2820 return (error); 2821 } 2822 2823 /* 2824 * Common implementation code for chflags() and fchflags(). 2825 */ 2826 static int 2827 setfflags(struct thread *td, struct vnode *vp, u_long flags) 2828 { 2829 struct mount *mp; 2830 struct vattr vattr; 2831 int error; 2832 2833 /* We can't support the value matching VNOVAL. */ 2834 if (flags == VNOVAL) 2835 return (EOPNOTSUPP); 2836 2837 /* 2838 * Prevent non-root users from setting flags on devices. When 2839 * a device is reused, users can retain ownership of the device 2840 * if they are allowed to set flags and programs assume that 2841 * chown can't fail when done as root. 2842 */ 2843 if (VN_ISDEV(vp)) { 2844 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV); 2845 if (error != 0) 2846 return (error); 2847 } 2848 2849 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 2850 return (error); 2851 VATTR_NULL(&vattr); 2852 vattr.va_flags = flags; 2853 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2854 #ifdef MAC 2855 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags); 2856 if (error == 0) 2857 #endif 2858 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 2859 VOP_UNLOCK(vp); 2860 vn_finished_write(mp); 2861 return (error); 2862 } 2863 2864 /* 2865 * Change flags of a file given a path name. 2866 */ 2867 #ifndef _SYS_SYSPROTO_H_ 2868 struct chflags_args { 2869 const char *path; 2870 u_long flags; 2871 }; 2872 #endif 2873 int 2874 sys_chflags(struct thread *td, struct chflags_args *uap) 2875 { 2876 2877 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2878 uap->flags, 0)); 2879 } 2880 2881 #ifndef _SYS_SYSPROTO_H_ 2882 struct chflagsat_args { 2883 int fd; 2884 const char *path; 2885 u_long flags; 2886 int atflag; 2887 } 2888 #endif 2889 int 2890 sys_chflagsat(struct thread *td, struct chflagsat_args *uap) 2891 { 2892 2893 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE, 2894 uap->flags, uap->atflag)); 2895 } 2896 2897 /* 2898 * Same as chflags() but doesn't follow symlinks. 2899 */ 2900 #ifndef _SYS_SYSPROTO_H_ 2901 struct lchflags_args { 2902 const char *path; 2903 u_long flags; 2904 }; 2905 #endif 2906 int 2907 sys_lchflags(struct thread *td, struct lchflags_args *uap) 2908 { 2909 2910 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 2911 uap->flags, AT_SYMLINK_NOFOLLOW)); 2912 } 2913 2914 static int 2915 kern_chflagsat(struct thread *td, int fd, const char *path, 2916 enum uio_seg pathseg, u_long flags, int atflag) 2917 { 2918 struct nameidata nd; 2919 int error; 2920 2921 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH | 2922 AT_EMPTY_PATH)) != 0) 2923 return (EINVAL); 2924 2925 AUDIT_ARG_FFLAGS(flags); 2926 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW | 2927 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path, 2928 fd, &cap_fchflags_rights); 2929 if ((error = namei(&nd)) != 0) 2930 return (error); 2931 NDFREE_PNBUF(&nd); 2932 error = setfflags(td, nd.ni_vp, flags); 2933 vrele(nd.ni_vp); 2934 return (error); 2935 } 2936 2937 /* 2938 * Change flags of a file given a file descriptor. 2939 */ 2940 #ifndef _SYS_SYSPROTO_H_ 2941 struct fchflags_args { 2942 int fd; 2943 u_long flags; 2944 }; 2945 #endif 2946 int 2947 sys_fchflags(struct thread *td, struct fchflags_args *uap) 2948 { 2949 struct file *fp; 2950 int error; 2951 2952 AUDIT_ARG_FD(uap->fd); 2953 AUDIT_ARG_FFLAGS(uap->flags); 2954 error = getvnode(td, uap->fd, &cap_fchflags_rights, 2955 &fp); 2956 if (error != 0) 2957 return (error); 2958 #ifdef AUDIT 2959 if (AUDITING_TD(td)) { 2960 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); 2961 AUDIT_ARG_VNODE1(fp->f_vnode); 2962 VOP_UNLOCK(fp->f_vnode); 2963 } 2964 #endif 2965 error = setfflags(td, fp->f_vnode, uap->flags); 2966 fdrop(fp, td); 2967 return (error); 2968 } 2969 2970 /* 2971 * Common implementation code for chmod(), lchmod() and fchmod(). 2972 */ 2973 int 2974 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode) 2975 { 2976 struct mount *mp; 2977 struct vattr vattr; 2978 int error; 2979 2980 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 2981 return (error); 2982 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 2983 VATTR_NULL(&vattr); 2984 vattr.va_mode = mode & ALLPERMS; 2985 #ifdef MAC 2986 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode); 2987 if (error == 0) 2988 #endif 2989 error = VOP_SETATTR(vp, &vattr, cred); 2990 VOP_UNLOCK(vp); 2991 vn_finished_write(mp); 2992 return (error); 2993 } 2994 2995 /* 2996 * Change mode of a file given path name. 2997 */ 2998 #ifndef _SYS_SYSPROTO_H_ 2999 struct chmod_args { 3000 char *path; 3001 int mode; 3002 }; 3003 #endif 3004 int 3005 sys_chmod(struct thread *td, struct chmod_args *uap) 3006 { 3007 3008 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 3009 uap->mode, 0)); 3010 } 3011 3012 #ifndef _SYS_SYSPROTO_H_ 3013 struct fchmodat_args { 3014 int dirfd; 3015 char *path; 3016 mode_t mode; 3017 int flag; 3018 } 3019 #endif 3020 int 3021 sys_fchmodat(struct thread *td, struct fchmodat_args *uap) 3022 { 3023 3024 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE, 3025 uap->mode, uap->flag)); 3026 } 3027 3028 /* 3029 * Change mode of a file given path name (don't follow links.) 3030 */ 3031 #ifndef _SYS_SYSPROTO_H_ 3032 struct lchmod_args { 3033 char *path; 3034 int mode; 3035 }; 3036 #endif 3037 int 3038 sys_lchmod(struct thread *td, struct lchmod_args *uap) 3039 { 3040 3041 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 3042 uap->mode, AT_SYMLINK_NOFOLLOW)); 3043 } 3044 3045 int 3046 kern_fchmodat(struct thread *td, int fd, const char *path, 3047 enum uio_seg pathseg, mode_t mode, int flag) 3048 { 3049 struct nameidata nd; 3050 int error; 3051 3052 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH | 3053 AT_EMPTY_PATH)) != 0) 3054 return (EINVAL); 3055 3056 AUDIT_ARG_MODE(mode); 3057 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW | 3058 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path, 3059 fd, &cap_fchmod_rights); 3060 if ((error = namei(&nd)) != 0) 3061 return (error); 3062 NDFREE_PNBUF(&nd); 3063 error = setfmode(td, td->td_ucred, nd.ni_vp, mode); 3064 vrele(nd.ni_vp); 3065 return (error); 3066 } 3067 3068 /* 3069 * Change mode of a file given a file descriptor. 3070 */ 3071 #ifndef _SYS_SYSPROTO_H_ 3072 struct fchmod_args { 3073 int fd; 3074 int mode; 3075 }; 3076 #endif 3077 int 3078 sys_fchmod(struct thread *td, struct fchmod_args *uap) 3079 { 3080 struct file *fp; 3081 int error; 3082 3083 AUDIT_ARG_FD(uap->fd); 3084 AUDIT_ARG_MODE(uap->mode); 3085 3086 error = fget(td, uap->fd, &cap_fchmod_rights, &fp); 3087 if (error != 0) 3088 return (error); 3089 error = fo_chmod(fp, uap->mode, td->td_ucred, td); 3090 fdrop(fp, td); 3091 return (error); 3092 } 3093 3094 /* 3095 * Common implementation for chown(), lchown(), and fchown() 3096 */ 3097 int 3098 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid, 3099 gid_t gid) 3100 { 3101 struct mount *mp; 3102 struct vattr vattr; 3103 int error; 3104 3105 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 3106 return (error); 3107 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3108 VATTR_NULL(&vattr); 3109 vattr.va_uid = uid; 3110 vattr.va_gid = gid; 3111 #ifdef MAC 3112 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid, 3113 vattr.va_gid); 3114 if (error == 0) 3115 #endif 3116 error = VOP_SETATTR(vp, &vattr, cred); 3117 VOP_UNLOCK(vp); 3118 vn_finished_write(mp); 3119 return (error); 3120 } 3121 3122 /* 3123 * Set ownership given a path name. 3124 */ 3125 #ifndef _SYS_SYSPROTO_H_ 3126 struct chown_args { 3127 char *path; 3128 int uid; 3129 int gid; 3130 }; 3131 #endif 3132 int 3133 sys_chown(struct thread *td, struct chown_args *uap) 3134 { 3135 3136 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid, 3137 uap->gid, 0)); 3138 } 3139 3140 #ifndef _SYS_SYSPROTO_H_ 3141 struct fchownat_args { 3142 int fd; 3143 const char * path; 3144 uid_t uid; 3145 gid_t gid; 3146 int flag; 3147 }; 3148 #endif 3149 int 3150 sys_fchownat(struct thread *td, struct fchownat_args *uap) 3151 { 3152 3153 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid, 3154 uap->gid, uap->flag)); 3155 } 3156 3157 int 3158 kern_fchownat(struct thread *td, int fd, const char *path, 3159 enum uio_seg pathseg, int uid, int gid, int flag) 3160 { 3161 struct nameidata nd; 3162 int error; 3163 3164 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH | 3165 AT_EMPTY_PATH)) != 0) 3166 return (EINVAL); 3167 3168 AUDIT_ARG_OWNER(uid, gid); 3169 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW | 3170 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path, 3171 fd, &cap_fchown_rights); 3172 3173 if ((error = namei(&nd)) != 0) 3174 return (error); 3175 NDFREE_PNBUF(&nd); 3176 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid); 3177 vrele(nd.ni_vp); 3178 return (error); 3179 } 3180 3181 /* 3182 * Set ownership given a path name, do not cross symlinks. 3183 */ 3184 #ifndef _SYS_SYSPROTO_H_ 3185 struct lchown_args { 3186 char *path; 3187 int uid; 3188 int gid; 3189 }; 3190 #endif 3191 int 3192 sys_lchown(struct thread *td, struct lchown_args *uap) 3193 { 3194 3195 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 3196 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW)); 3197 } 3198 3199 /* 3200 * Set ownership given a file descriptor. 3201 */ 3202 #ifndef _SYS_SYSPROTO_H_ 3203 struct fchown_args { 3204 int fd; 3205 int uid; 3206 int gid; 3207 }; 3208 #endif 3209 int 3210 sys_fchown(struct thread *td, struct fchown_args *uap) 3211 { 3212 struct file *fp; 3213 int error; 3214 3215 AUDIT_ARG_FD(uap->fd); 3216 AUDIT_ARG_OWNER(uap->uid, uap->gid); 3217 error = fget(td, uap->fd, &cap_fchown_rights, &fp); 3218 if (error != 0) 3219 return (error); 3220 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td); 3221 fdrop(fp, td); 3222 return (error); 3223 } 3224 3225 /* 3226 * Common implementation code for utimes(), lutimes(), and futimes(). 3227 */ 3228 static int 3229 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg, 3230 struct timespec *tsp) 3231 { 3232 struct timeval tv[2]; 3233 const struct timeval *tvp; 3234 int error; 3235 3236 if (usrtvp == NULL) { 3237 vfs_timestamp(&tsp[0]); 3238 tsp[1] = tsp[0]; 3239 } else { 3240 if (tvpseg == UIO_SYSSPACE) { 3241 tvp = usrtvp; 3242 } else { 3243 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0) 3244 return (error); 3245 tvp = tv; 3246 } 3247 3248 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 || 3249 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000) 3250 return (EINVAL); 3251 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]); 3252 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]); 3253 } 3254 return (0); 3255 } 3256 3257 /* 3258 * Common implementation code for futimens(), utimensat(). 3259 */ 3260 #define UTIMENS_NULL 0x1 3261 #define UTIMENS_EXIT 0x2 3262 static int 3263 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg, 3264 struct timespec *tsp, int *retflags) 3265 { 3266 struct timespec tsnow; 3267 int error; 3268 3269 vfs_timestamp(&tsnow); 3270 *retflags = 0; 3271 if (usrtsp == NULL) { 3272 tsp[0] = tsnow; 3273 tsp[1] = tsnow; 3274 *retflags |= UTIMENS_NULL; 3275 return (0); 3276 } 3277 if (tspseg == UIO_SYSSPACE) { 3278 tsp[0] = usrtsp[0]; 3279 tsp[1] = usrtsp[1]; 3280 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0) 3281 return (error); 3282 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT) 3283 *retflags |= UTIMENS_EXIT; 3284 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW) 3285 *retflags |= UTIMENS_NULL; 3286 if (tsp[0].tv_nsec == UTIME_OMIT) 3287 tsp[0].tv_sec = VNOVAL; 3288 else if (tsp[0].tv_nsec == UTIME_NOW) 3289 tsp[0] = tsnow; 3290 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L) 3291 return (EINVAL); 3292 if (tsp[1].tv_nsec == UTIME_OMIT) 3293 tsp[1].tv_sec = VNOVAL; 3294 else if (tsp[1].tv_nsec == UTIME_NOW) 3295 tsp[1] = tsnow; 3296 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L) 3297 return (EINVAL); 3298 3299 return (0); 3300 } 3301 3302 /* 3303 * Common implementation code for utimes(), lutimes(), futimes(), futimens(), 3304 * and utimensat(). 3305 */ 3306 static int 3307 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts, 3308 int numtimes, int nullflag) 3309 { 3310 struct mount *mp; 3311 struct vattr vattr; 3312 int error; 3313 bool setbirthtime; 3314 3315 setbirthtime = false; 3316 vattr.va_birthtime.tv_sec = VNOVAL; 3317 vattr.va_birthtime.tv_nsec = 0; 3318 3319 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) 3320 return (error); 3321 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3322 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 && 3323 timespeccmp(&ts[1], &vattr.va_birthtime, < )) 3324 setbirthtime = true; 3325 VATTR_NULL(&vattr); 3326 vattr.va_atime = ts[0]; 3327 vattr.va_mtime = ts[1]; 3328 if (setbirthtime) 3329 vattr.va_birthtime = ts[1]; 3330 if (numtimes > 2) 3331 vattr.va_birthtime = ts[2]; 3332 if (nullflag) 3333 vattr.va_vaflags |= VA_UTIMES_NULL; 3334 #ifdef MAC 3335 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime, 3336 vattr.va_mtime); 3337 #endif 3338 if (error == 0) 3339 error = VOP_SETATTR(vp, &vattr, td->td_ucred); 3340 VOP_UNLOCK(vp); 3341 vn_finished_write(mp); 3342 return (error); 3343 } 3344 3345 /* 3346 * Set the access and modification times of a file. 3347 */ 3348 #ifndef _SYS_SYSPROTO_H_ 3349 struct utimes_args { 3350 char *path; 3351 struct timeval *tptr; 3352 }; 3353 #endif 3354 int 3355 sys_utimes(struct thread *td, struct utimes_args *uap) 3356 { 3357 3358 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 3359 uap->tptr, UIO_USERSPACE)); 3360 } 3361 3362 #ifndef _SYS_SYSPROTO_H_ 3363 struct futimesat_args { 3364 int fd; 3365 const char * path; 3366 const struct timeval * times; 3367 }; 3368 #endif 3369 int 3370 sys_futimesat(struct thread *td, struct futimesat_args *uap) 3371 { 3372 3373 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE, 3374 uap->times, UIO_USERSPACE)); 3375 } 3376 3377 int 3378 kern_utimesat(struct thread *td, int fd, const char *path, 3379 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg) 3380 { 3381 struct nameidata nd; 3382 struct timespec ts[2]; 3383 int error; 3384 3385 if ((error = getutimes(tptr, tptrseg, ts)) != 0) 3386 return (error); 3387 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd, 3388 &cap_futimes_rights); 3389 3390 if ((error = namei(&nd)) != 0) 3391 return (error); 3392 NDFREE_PNBUF(&nd); 3393 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL); 3394 vrele(nd.ni_vp); 3395 return (error); 3396 } 3397 3398 /* 3399 * Set the access and modification times of a file. 3400 */ 3401 #ifndef _SYS_SYSPROTO_H_ 3402 struct lutimes_args { 3403 char *path; 3404 struct timeval *tptr; 3405 }; 3406 #endif 3407 int 3408 sys_lutimes(struct thread *td, struct lutimes_args *uap) 3409 { 3410 3411 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr, 3412 UIO_USERSPACE)); 3413 } 3414 3415 int 3416 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg, 3417 const struct timeval *tptr, enum uio_seg tptrseg) 3418 { 3419 struct timespec ts[2]; 3420 struct nameidata nd; 3421 int error; 3422 3423 if ((error = getutimes(tptr, tptrseg, ts)) != 0) 3424 return (error); 3425 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path); 3426 if ((error = namei(&nd)) != 0) 3427 return (error); 3428 NDFREE_PNBUF(&nd); 3429 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL); 3430 vrele(nd.ni_vp); 3431 return (error); 3432 } 3433 3434 /* 3435 * Set the access and modification times of a file. 3436 */ 3437 #ifndef _SYS_SYSPROTO_H_ 3438 struct futimes_args { 3439 int fd; 3440 struct timeval *tptr; 3441 }; 3442 #endif 3443 int 3444 sys_futimes(struct thread *td, struct futimes_args *uap) 3445 { 3446 3447 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE)); 3448 } 3449 3450 int 3451 kern_futimes(struct thread *td, int fd, const struct timeval *tptr, 3452 enum uio_seg tptrseg) 3453 { 3454 struct timespec ts[2]; 3455 struct file *fp; 3456 int error; 3457 3458 AUDIT_ARG_FD(fd); 3459 error = getutimes(tptr, tptrseg, ts); 3460 if (error != 0) 3461 return (error); 3462 error = getvnode(td, fd, &cap_futimes_rights, &fp); 3463 if (error != 0) 3464 return (error); 3465 #ifdef AUDIT 3466 if (AUDITING_TD(td)) { 3467 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); 3468 AUDIT_ARG_VNODE1(fp->f_vnode); 3469 VOP_UNLOCK(fp->f_vnode); 3470 } 3471 #endif 3472 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL); 3473 fdrop(fp, td); 3474 return (error); 3475 } 3476 3477 int 3478 sys_futimens(struct thread *td, struct futimens_args *uap) 3479 { 3480 3481 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE)); 3482 } 3483 3484 int 3485 kern_futimens(struct thread *td, int fd, const struct timespec *tptr, 3486 enum uio_seg tptrseg) 3487 { 3488 struct timespec ts[2]; 3489 struct file *fp; 3490 int error, flags; 3491 3492 AUDIT_ARG_FD(fd); 3493 error = getutimens(tptr, tptrseg, ts, &flags); 3494 if (error != 0) 3495 return (error); 3496 if (flags & UTIMENS_EXIT) 3497 return (0); 3498 error = getvnode(td, fd, &cap_futimes_rights, &fp); 3499 if (error != 0) 3500 return (error); 3501 #ifdef AUDIT 3502 if (AUDITING_TD(td)) { 3503 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY); 3504 AUDIT_ARG_VNODE1(fp->f_vnode); 3505 VOP_UNLOCK(fp->f_vnode); 3506 } 3507 #endif 3508 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL); 3509 fdrop(fp, td); 3510 return (error); 3511 } 3512 3513 int 3514 sys_utimensat(struct thread *td, struct utimensat_args *uap) 3515 { 3516 3517 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE, 3518 uap->times, UIO_USERSPACE, uap->flag)); 3519 } 3520 3521 int 3522 kern_utimensat(struct thread *td, int fd, const char *path, 3523 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg, 3524 int flag) 3525 { 3526 struct nameidata nd; 3527 struct timespec ts[2]; 3528 int error, flags; 3529 3530 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH | 3531 AT_EMPTY_PATH)) != 0) 3532 return (EINVAL); 3533 3534 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0) 3535 return (error); 3536 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW | 3537 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, 3538 pathseg, path, fd, &cap_futimes_rights); 3539 if ((error = namei(&nd)) != 0) 3540 return (error); 3541 /* 3542 * We are allowed to call namei() regardless of 2xUTIME_OMIT. 3543 * POSIX states: 3544 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected." 3545 * "Search permission is denied by a component of the path prefix." 3546 */ 3547 NDFREE_PNBUF(&nd); 3548 if ((flags & UTIMENS_EXIT) == 0) 3549 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL); 3550 vrele(nd.ni_vp); 3551 return (error); 3552 } 3553 3554 /* 3555 * Truncate a file given its path name. 3556 */ 3557 #ifndef _SYS_SYSPROTO_H_ 3558 struct truncate_args { 3559 char *path; 3560 int pad; 3561 off_t length; 3562 }; 3563 #endif 3564 int 3565 sys_truncate(struct thread *td, struct truncate_args *uap) 3566 { 3567 3568 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 3569 } 3570 3571 int 3572 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg, 3573 off_t length) 3574 { 3575 struct mount *mp; 3576 struct vnode *vp; 3577 void *rl_cookie; 3578 struct nameidata nd; 3579 int error; 3580 3581 if (length < 0) 3582 return (EINVAL); 3583 NDPREINIT(&nd); 3584 retry: 3585 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path); 3586 if ((error = namei(&nd)) != 0) 3587 return (error); 3588 vp = nd.ni_vp; 3589 NDFREE_PNBUF(&nd); 3590 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX); 3591 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) { 3592 vn_rangelock_unlock(vp, rl_cookie); 3593 vrele(vp); 3594 return (error); 3595 } 3596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3597 if (vp->v_type == VDIR) { 3598 error = EISDIR; 3599 goto out; 3600 } 3601 #ifdef MAC 3602 error = mac_vnode_check_write(td->td_ucred, NOCRED, vp); 3603 if (error != 0) 3604 goto out; 3605 #endif 3606 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td); 3607 if (error != 0) 3608 goto out; 3609 3610 error = vn_truncate_locked(vp, length, false, td->td_ucred); 3611 out: 3612 VOP_UNLOCK(vp); 3613 vn_finished_write(mp); 3614 vn_rangelock_unlock(vp, rl_cookie); 3615 vrele(vp); 3616 if (error == ERELOOKUP) 3617 goto retry; 3618 return (error); 3619 } 3620 3621 #if defined(COMPAT_43) 3622 /* 3623 * Truncate a file given its path name. 3624 */ 3625 #ifndef _SYS_SYSPROTO_H_ 3626 struct otruncate_args { 3627 char *path; 3628 long length; 3629 }; 3630 #endif 3631 int 3632 otruncate(struct thread *td, struct otruncate_args *uap) 3633 { 3634 3635 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 3636 } 3637 #endif /* COMPAT_43 */ 3638 3639 #if defined(COMPAT_FREEBSD6) 3640 /* Versions with the pad argument */ 3641 int 3642 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap) 3643 { 3644 3645 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length)); 3646 } 3647 3648 int 3649 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap) 3650 { 3651 3652 return (kern_ftruncate(td, uap->fd, uap->length)); 3653 } 3654 #endif 3655 3656 int 3657 kern_fsync(struct thread *td, int fd, bool fullsync) 3658 { 3659 struct vnode *vp; 3660 struct mount *mp; 3661 struct file *fp; 3662 int error; 3663 3664 AUDIT_ARG_FD(fd); 3665 error = getvnode(td, fd, &cap_fsync_rights, &fp); 3666 if (error != 0) 3667 return (error); 3668 vp = fp->f_vnode; 3669 #if 0 3670 if (!fullsync) 3671 /* XXXKIB: compete outstanding aio writes */; 3672 #endif 3673 retry: 3674 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH); 3675 if (error != 0) 3676 goto drop; 3677 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY); 3678 AUDIT_ARG_VNODE1(vp); 3679 vnode_pager_clean_async(vp); 3680 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td); 3681 VOP_UNLOCK(vp); 3682 vn_finished_write(mp); 3683 if (error == ERELOOKUP) 3684 goto retry; 3685 drop: 3686 fdrop(fp, td); 3687 return (error); 3688 } 3689 3690 /* 3691 * Sync an open file. 3692 */ 3693 #ifndef _SYS_SYSPROTO_H_ 3694 struct fsync_args { 3695 int fd; 3696 }; 3697 #endif 3698 int 3699 sys_fsync(struct thread *td, struct fsync_args *uap) 3700 { 3701 3702 return (kern_fsync(td, uap->fd, true)); 3703 } 3704 3705 int 3706 sys_fdatasync(struct thread *td, struct fdatasync_args *uap) 3707 { 3708 3709 return (kern_fsync(td, uap->fd, false)); 3710 } 3711 3712 /* 3713 * Rename files. Source and destination must either both be directories, or 3714 * both not be directories. If target is a directory, it must be empty. 3715 */ 3716 #ifndef _SYS_SYSPROTO_H_ 3717 struct rename_args { 3718 char *from; 3719 char *to; 3720 }; 3721 #endif 3722 int 3723 sys_rename(struct thread *td, struct rename_args *uap) 3724 { 3725 3726 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD, 3727 uap->to, UIO_USERSPACE, 0)); 3728 } 3729 3730 #ifndef _SYS_SYSPROTO_H_ 3731 struct renameat_args { 3732 int oldfd; 3733 char *old; 3734 int newfd; 3735 char *new; 3736 }; 3737 #endif 3738 int 3739 sys_renameat(struct thread *td, struct renameat_args *uap) 3740 { 3741 3742 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new, 3743 UIO_USERSPACE, 0)); 3744 } 3745 3746 int 3747 sys_renameat2(struct thread *td, struct renameat2_args *uap) 3748 { 3749 3750 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new, 3751 UIO_USERSPACE, uap->flags)); 3752 } 3753 3754 #ifdef MAC 3755 static int 3756 kern_renameat_mac(struct thread *td, int fromfd, const char *from, int tofd, 3757 const char *to, enum uio_seg pathseg, struct nameidata *fromnd, int op, 3758 int ndflags) 3759 { 3760 int error; 3761 3762 NDINIT_ATRIGHTS(fromnd, op, LOCKPARENT | LOCKLEAF | AUDITVNODE1 | 3763 ndflags, pathseg, from, fromfd, &cap_renameat_source_rights); 3764 if ((error = namei(fromnd)) != 0) 3765 return (error); 3766 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp, 3767 fromnd->ni_vp, &fromnd->ni_cnd); 3768 VOP_UNLOCK(fromnd->ni_dvp); 3769 if (fromnd->ni_dvp != fromnd->ni_vp) 3770 VOP_UNLOCK(fromnd->ni_vp); 3771 if (error != 0) { 3772 NDFREE_PNBUF(fromnd); 3773 vrele(fromnd->ni_dvp); 3774 vrele(fromnd->ni_vp); 3775 } 3776 return (error); 3777 } 3778 #endif 3779 3780 int 3781 kern_renameat(struct thread *td, int fromfd, const char *from, int tofd, 3782 const char *to, enum uio_seg pathseg, u_int flags) 3783 { 3784 struct mount *mp, *tmp; 3785 struct vnode *tvp, *fvp, *tdvp; 3786 struct nameidata fromnd, tond; 3787 uint64_t tondflags; 3788 int error, fndflags, op; 3789 short irflag; 3790 bool exchange; 3791 3792 if ((flags & ~(AT_RENAME_NOREPLACE | AT_RENAME_EXCHANGE)) != 0) 3793 return (EINVAL); 3794 if ((flags & (AT_RENAME_NOREPLACE | AT_RENAME_EXCHANGE)) == 3795 (AT_RENAME_NOREPLACE | AT_RENAME_EXCHANGE)) 3796 return (EINVAL); 3797 if ((flags & AT_RENAME_EXCHANGE) != 0) { 3798 op = RENAME; 3799 exchange = true; 3800 } else { 3801 op = DELETE; 3802 exchange = false; 3803 } 3804 fndflags = 0; 3805 again: 3806 tmp = mp = NULL; 3807 bwillwrite(); 3808 #ifdef MAC 3809 if (mac_vnode_check_rename_from_enabled()) { 3810 error = kern_renameat_mac(td, fromfd, from, tofd, to, pathseg, 3811 &fromnd, op, fndflags); 3812 if (error != 0) 3813 return (error); 3814 } else { 3815 #endif 3816 NDINIT_ATRIGHTS(&fromnd, op, WANTPARENT | AUDITVNODE1 | fndflags, 3817 pathseg, from, fromfd, &cap_renameat_source_rights); 3818 if ((error = namei(&fromnd)) != 0) 3819 return (error); 3820 #ifdef MAC 3821 } 3822 #endif 3823 fvp = fromnd.ni_vp; 3824 if (fromnd.ni_dvp->v_type != VDIR) { 3825 NDFREE_PNBUF(&fromnd); 3826 vrele(fromnd.ni_dvp); 3827 if (fvp != NULL) 3828 vrele(fromnd.ni_vp); 3829 return (EBUSY); 3830 } 3831 if (exchange && fvp == NULL) { 3832 NDFREE_PNBUF(&fromnd); 3833 vrele(fromnd.ni_dvp); 3834 return (ENOENT); 3835 } 3836 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2; 3837 if (fromnd.ni_vp->v_type == VDIR) 3838 tondflags |= WILLBEDIR; 3839 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, to, tofd, 3840 &cap_renameat_target_rights); 3841 if ((error = namei(&tond)) != 0) { 3842 /* Translate error code for rename("dir1", "dir2/."). */ 3843 if (error == EISDIR && fvp->v_type == VDIR) 3844 error = EINVAL; 3845 NDFREE_PNBUF(&fromnd); 3846 vrele(fromnd.ni_dvp); 3847 vrele(fvp); 3848 goto out1; 3849 } 3850 tdvp = tond.ni_dvp; 3851 tvp = tond.ni_vp; 3852 if (tdvp->v_type != VDIR) { 3853 /* 3854 * Rename of the root vnode of the mounted 3855 * filesystem. It is possible to get there with the 3856 * nullfs mount over the regular file. 3857 */ 3858 error = EBUSY; 3859 goto out; 3860 } 3861 if (tvp != NULL && (flags & AT_RENAME_NOREPLACE) != 0) { 3862 /* 3863 * Often filesystems need to relock the vnodes in 3864 * VOP_RENAME(), which opens a window for invalidation 3865 * of this check. Then, not all filesystems might 3866 * implement AT_RENAME_NOREPLACE. This leads to 3867 * situation where sometimes EOPNOTSUPP might be 3868 * returned from the VOP due to race, while most of 3869 * the time this check works. 3870 */ 3871 error = EEXIST; 3872 goto out; 3873 } 3874 if (fvp->v_type == VDIR && 3875 ((fromnd.ni_resflags | tond.ni_resflags) & NIRES_BENEATH) != 0) { 3876 /* 3877 * We must not rename a directory relative to FD_RESOLVE_BENEATH 3878 * descriptors. 3879 */ 3880 error = ENOTCAPABLE; 3881 goto out; 3882 } 3883 if (exchange) { 3884 if (tvp == NULL) { 3885 error = ENOENT; 3886 goto out; 3887 } 3888 if (tvp->v_type == VDIR && fndflags == 0) { 3889 fndflags = WILLBEDIR; 3890 again2: 3891 NDFREE_PNBUF(&fromnd); 3892 vrele(fromnd.ni_dvp); 3893 vrele(fvp); 3894 NDFREE_PNBUF(&tond); 3895 VOP_VPUT_PAIR(tdvp, &tvp, true); 3896 error = sig_intr(); 3897 if (error != 0) 3898 goto out1; 3899 goto again; 3900 } 3901 if (tvp->v_type != VDIR && fndflags != 0) { 3902 fndflags = 0; 3903 goto again2; 3904 } 3905 } 3906 error = vn_start_write(fvp, &mp, V_NOWAIT); 3907 if (error != 0) { 3908 again1: 3909 NDFREE_PNBUF(&fromnd); 3910 NDFREE_PNBUF(&tond); 3911 if (tvp != NULL) 3912 vput(tvp); 3913 if (tdvp == tvp) 3914 vrele(tdvp); 3915 else 3916 vput(tdvp); 3917 vrele(fromnd.ni_dvp); 3918 vrele(fvp); 3919 if (tmp != NULL) { 3920 lockmgr(&tmp->mnt_renamelock, LK_EXCLUSIVE, NULL); 3921 lockmgr(&tmp->mnt_renamelock, LK_RELEASE, NULL); 3922 vfs_rel(tmp); 3923 tmp = NULL; 3924 } 3925 error = sig_intr(); 3926 if (error != 0) 3927 return (error); 3928 error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH); 3929 if (error != 0) 3930 return (error); 3931 goto again; 3932 } 3933 error = VOP_GETWRITEMOUNT(tdvp, &tmp); 3934 if (error != 0 || tmp == NULL) 3935 goto again1; 3936 error = lockmgr(&tmp->mnt_renamelock, LK_EXCLUSIVE | LK_NOWAIT, NULL); 3937 if (error != 0) { 3938 vn_finished_write(mp); 3939 goto again1; 3940 } 3941 irflag = vn_irflag_read(fvp); 3942 if (((irflag & VIRF_NAMEDATTR) != 0 && tdvp != fromnd.ni_dvp) || 3943 (irflag & VIRF_NAMEDDIR) != 0) { 3944 error = EINVAL; 3945 goto out; 3946 } 3947 if (tvp != NULL) { 3948 if (!exchange) { 3949 if (fvp->v_type == VDIR && tvp->v_type != VDIR) { 3950 error = ENOTDIR; 3951 goto out; 3952 } else if (fvp->v_type != VDIR && 3953 tvp->v_type == VDIR) { 3954 error = EISDIR; 3955 goto out; 3956 } 3957 } 3958 #ifdef CAPABILITIES 3959 if (tofd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) { 3960 /* 3961 * If the target already exists we require CAP_UNLINKAT 3962 * from 'tofd', when tofd was used for the lookup. 3963 */ 3964 error = cap_check(&tond.ni_filecaps.fc_rights, 3965 &cap_unlinkat_rights); 3966 if (error != 0) 3967 goto out; 3968 } 3969 #endif 3970 } 3971 if (fvp == tdvp) { 3972 error = EINVAL; 3973 goto out; 3974 } 3975 /* 3976 * If the source is the same as the destination (that is, if they 3977 * are links to the same vnode), then there is nothing to do. 3978 */ 3979 if (fvp == tvp) 3980 error = ERESTART; 3981 #ifdef MAC 3982 else 3983 error = mac_vnode_check_rename_to(td->td_ucred, tdvp, 3984 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd); 3985 #endif 3986 out: 3987 if (error == 0) { 3988 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd, 3989 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd, flags); 3990 NDFREE_PNBUF(&fromnd); 3991 NDFREE_PNBUF(&tond); 3992 } else { 3993 NDFREE_PNBUF(&fromnd); 3994 NDFREE_PNBUF(&tond); 3995 if (tvp != NULL) 3996 vput(tvp); 3997 if (tdvp == tvp) 3998 vrele(tdvp); 3999 else 4000 vput(tdvp); 4001 vrele(fromnd.ni_dvp); 4002 vrele(fvp); 4003 } 4004 if (tmp != NULL) { 4005 lockmgr(&tmp->mnt_renamelock, LK_RELEASE, 0); 4006 vfs_rel(tmp); 4007 } 4008 if (mp != NULL) 4009 vn_finished_write(mp); 4010 out1: 4011 if (error == ERESTART) 4012 return (0); 4013 if (error == ERELOOKUP) { 4014 error = sig_intr(); 4015 if (error == 0) 4016 goto again; 4017 } 4018 return (error); 4019 } 4020 4021 /* 4022 * Make a directory file. 4023 */ 4024 #ifndef _SYS_SYSPROTO_H_ 4025 struct mkdir_args { 4026 char *path; 4027 int mode; 4028 }; 4029 #endif 4030 int 4031 sys_mkdir(struct thread *td, struct mkdir_args *uap) 4032 { 4033 4034 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE, 4035 uap->mode)); 4036 } 4037 4038 #ifndef _SYS_SYSPROTO_H_ 4039 struct mkdirat_args { 4040 int fd; 4041 char *path; 4042 mode_t mode; 4043 }; 4044 #endif 4045 int 4046 sys_mkdirat(struct thread *td, struct mkdirat_args *uap) 4047 { 4048 4049 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode)); 4050 } 4051 4052 int 4053 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg, 4054 int mode) 4055 { 4056 struct mount *mp; 4057 struct vattr vattr; 4058 struct nameidata nd; 4059 int error; 4060 4061 AUDIT_ARG_MODE(mode); 4062 NDPREINIT(&nd); 4063 restart: 4064 bwillwrite(); 4065 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | 4066 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR, 4067 segflg, path, fd, &cap_mkdirat_rights); 4068 if ((error = namei(&nd)) != 0) 4069 return (error); 4070 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 4071 NDFREE_PNBUF(&nd); 4072 vput(nd.ni_dvp); 4073 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0) 4074 return (error); 4075 goto restart; 4076 } 4077 if ((vn_irflag_read(nd.ni_dvp) & VIRF_NAMEDDIR) != 0) { 4078 error = EINVAL; 4079 goto out; 4080 } 4081 VATTR_NULL(&vattr); 4082 vattr.va_type = VDIR; 4083 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask; 4084 #ifdef MAC 4085 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd, 4086 &vattr); 4087 if (error != 0) 4088 goto out; 4089 #endif 4090 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr); 4091 out: 4092 NDFREE_PNBUF(&nd); 4093 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true); 4094 vn_finished_write(mp); 4095 if (error == ERELOOKUP) 4096 goto restart; 4097 return (error); 4098 } 4099 4100 /* 4101 * Remove a directory file. 4102 */ 4103 #ifndef _SYS_SYSPROTO_H_ 4104 struct rmdir_args { 4105 char *path; 4106 }; 4107 #endif 4108 int 4109 sys_rmdir(struct thread *td, struct rmdir_args *uap) 4110 { 4111 4112 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE, 4113 0)); 4114 } 4115 4116 int 4117 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd, 4118 enum uio_seg pathseg, int flag) 4119 { 4120 struct mount *mp; 4121 struct vnode *vp; 4122 struct file *fp; 4123 struct nameidata nd; 4124 cap_rights_t rights; 4125 int error; 4126 4127 fp = NULL; 4128 if (fd != FD_NONE) { 4129 error = getvnode(td, fd, cap_rights_init_one(&rights, 4130 CAP_LOOKUP), &fp); 4131 if (error != 0) 4132 return (error); 4133 } 4134 4135 NDPREINIT(&nd); 4136 restart: 4137 bwillwrite(); 4138 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 | 4139 at2cnpflags(flag, AT_RESOLVE_BENEATH), 4140 pathseg, path, dfd, &cap_unlinkat_rights); 4141 if ((error = namei(&nd)) != 0) 4142 goto fdout; 4143 vp = nd.ni_vp; 4144 if (vp->v_type != VDIR) { 4145 error = ENOTDIR; 4146 goto out; 4147 } 4148 /* 4149 * No rmdir "." please. 4150 */ 4151 if (nd.ni_dvp == vp) { 4152 error = EINVAL; 4153 goto out; 4154 } 4155 /* 4156 * The root of a mounted filesystem cannot be deleted. 4157 */ 4158 if (vp->v_vflag & VV_ROOT) { 4159 error = EBUSY; 4160 goto out; 4161 } 4162 4163 if (fp != NULL && fp->f_vnode != vp) { 4164 if (VN_IS_DOOMED(fp->f_vnode)) 4165 error = EBADF; 4166 else 4167 error = EDEADLK; 4168 goto out; 4169 } 4170 4171 #ifdef MAC 4172 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp, 4173 &nd.ni_cnd); 4174 if (error != 0) 4175 goto out; 4176 #endif 4177 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) { 4178 NDFREE_PNBUF(&nd); 4179 vput(vp); 4180 if (nd.ni_dvp == vp) 4181 vrele(nd.ni_dvp); 4182 else 4183 vput(nd.ni_dvp); 4184 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0) 4185 goto fdout; 4186 goto restart; 4187 } 4188 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd); 4189 vn_finished_write(mp); 4190 out: 4191 NDFREE_PNBUF(&nd); 4192 vput(vp); 4193 if (nd.ni_dvp == vp) 4194 vrele(nd.ni_dvp); 4195 else 4196 vput(nd.ni_dvp); 4197 if (error == ERELOOKUP) 4198 goto restart; 4199 fdout: 4200 if (fp != NULL) 4201 fdrop(fp, td); 4202 return (error); 4203 } 4204 4205 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11) 4206 int 4207 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count, 4208 long *basep, void (*func)(struct freebsd11_dirent *)) 4209 { 4210 struct freebsd11_dirent dstdp; 4211 struct dirent *dp, *edp; 4212 char *dirbuf; 4213 off_t base; 4214 ssize_t resid, ucount; 4215 int error; 4216 4217 /* XXX arbitrary sanity limit on `count'. */ 4218 count = min(count, 64 * 1024); 4219 4220 dirbuf = malloc(count, M_TEMP, M_WAITOK); 4221 4222 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid, 4223 UIO_SYSSPACE); 4224 if (error != 0) 4225 goto done; 4226 if (basep != NULL) 4227 *basep = base; 4228 4229 ucount = 0; 4230 for (dp = (struct dirent *)dirbuf, 4231 edp = (struct dirent *)&dirbuf[count - resid]; 4232 ucount < count && dp < edp; ) { 4233 if (dp->d_reclen == 0) 4234 break; 4235 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0)); 4236 if (dp->d_namlen >= sizeof(dstdp.d_name)) 4237 continue; 4238 dstdp.d_type = dp->d_type; 4239 dstdp.d_namlen = dp->d_namlen; 4240 dstdp.d_fileno = dp->d_fileno; /* truncate */ 4241 if (dstdp.d_fileno != dp->d_fileno) { 4242 switch (ino64_trunc_error) { 4243 default: 4244 case 0: 4245 break; 4246 case 1: 4247 error = EOVERFLOW; 4248 goto done; 4249 case 2: 4250 dstdp.d_fileno = UINT32_MAX; 4251 break; 4252 } 4253 } 4254 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) + 4255 ((dp->d_namlen + 1 + 3) &~ 3); 4256 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen); 4257 bzero(dstdp.d_name + dstdp.d_namlen, 4258 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) - 4259 dstdp.d_namlen); 4260 MPASS(dstdp.d_reclen <= dp->d_reclen); 4261 MPASS(ucount + dstdp.d_reclen <= count); 4262 if (func != NULL) 4263 func(&dstdp); 4264 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen); 4265 if (error != 0) 4266 break; 4267 dp = (struct dirent *)((char *)dp + dp->d_reclen); 4268 ucount += dstdp.d_reclen; 4269 } 4270 4271 done: 4272 free(dirbuf, M_TEMP); 4273 if (error == 0) 4274 td->td_retval[0] = ucount; 4275 return (error); 4276 } 4277 #endif /* COMPAT */ 4278 4279 #ifdef COMPAT_43 4280 static void 4281 ogetdirentries_cvt(struct freebsd11_dirent *dp) 4282 { 4283 #if (BYTE_ORDER == LITTLE_ENDIAN) 4284 /* 4285 * The expected low byte of dp->d_namlen is our dp->d_type. 4286 * The high MBZ byte of dp->d_namlen is our dp->d_namlen. 4287 */ 4288 dp->d_type = dp->d_namlen; 4289 dp->d_namlen = 0; 4290 #else 4291 /* 4292 * The dp->d_type is the high byte of the expected dp->d_namlen, 4293 * so must be zero'ed. 4294 */ 4295 dp->d_type = 0; 4296 #endif 4297 } 4298 4299 /* 4300 * Read a block of directory entries in a filesystem independent format. 4301 */ 4302 #ifndef _SYS_SYSPROTO_H_ 4303 struct ogetdirentries_args { 4304 int fd; 4305 char *buf; 4306 u_int count; 4307 long *basep; 4308 }; 4309 #endif 4310 int 4311 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap) 4312 { 4313 long loff; 4314 int error; 4315 4316 error = kern_ogetdirentries(td, uap, &loff); 4317 if (error == 0) 4318 error = copyout(&loff, uap->basep, sizeof(long)); 4319 return (error); 4320 } 4321 4322 int 4323 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap, 4324 long *ploff) 4325 { 4326 long base; 4327 int error; 4328 4329 /* XXX arbitrary sanity limit on `count'. */ 4330 if (uap->count > 64 * 1024) 4331 return (EINVAL); 4332 4333 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count, 4334 &base, ogetdirentries_cvt); 4335 4336 if (error == 0 && uap->basep != NULL) 4337 error = copyout(&base, uap->basep, sizeof(long)); 4338 4339 return (error); 4340 } 4341 #endif /* COMPAT_43 */ 4342 4343 #if defined(COMPAT_FREEBSD11) 4344 #ifndef _SYS_SYSPROTO_H_ 4345 struct freebsd11_getdirentries_args { 4346 int fd; 4347 char *buf; 4348 u_int count; 4349 long *basep; 4350 }; 4351 #endif 4352 int 4353 freebsd11_getdirentries(struct thread *td, 4354 struct freebsd11_getdirentries_args *uap) 4355 { 4356 long base; 4357 int error; 4358 4359 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count, 4360 &base, NULL); 4361 4362 if (error == 0 && uap->basep != NULL) 4363 error = copyout(&base, uap->basep, sizeof(long)); 4364 return (error); 4365 } 4366 4367 int 4368 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap) 4369 { 4370 struct freebsd11_getdirentries_args ap; 4371 4372 ap.fd = uap->fd; 4373 ap.buf = uap->buf; 4374 ap.count = uap->count; 4375 ap.basep = NULL; 4376 return (freebsd11_getdirentries(td, &ap)); 4377 } 4378 #endif /* COMPAT_FREEBSD11 */ 4379 4380 /* 4381 * Read a block of directory entries in a filesystem independent format. 4382 */ 4383 int 4384 sys_getdirentries(struct thread *td, struct getdirentries_args *uap) 4385 { 4386 off_t base; 4387 int error; 4388 4389 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base, 4390 NULL, UIO_USERSPACE); 4391 if (error != 0) 4392 return (error); 4393 if (uap->basep != NULL) 4394 error = copyout(&base, uap->basep, sizeof(off_t)); 4395 return (error); 4396 } 4397 4398 int 4399 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count, 4400 off_t *basep, ssize_t *residp, enum uio_seg bufseg) 4401 { 4402 struct vnode *vp; 4403 struct file *fp; 4404 struct uio auio; 4405 struct iovec aiov; 4406 off_t loff; 4407 int error, eofflag; 4408 off_t foffset; 4409 4410 AUDIT_ARG_FD(fd); 4411 if (count > IOSIZE_MAX) 4412 return (EINVAL); 4413 auio.uio_resid = count; 4414 error = getvnode(td, fd, &cap_read_rights, &fp); 4415 if (error != 0) 4416 return (error); 4417 if ((fp->f_flag & FREAD) == 0) { 4418 fdrop(fp, td); 4419 return (EBADF); 4420 } 4421 vp = fp->f_vnode; 4422 foffset = foffset_lock(fp, 0); 4423 unionread: 4424 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) { 4425 error = ENOENT; 4426 goto fail; 4427 } 4428 aiov.iov_base = buf; 4429 aiov.iov_len = count; 4430 auio.uio_iov = &aiov; 4431 auio.uio_iovcnt = 1; 4432 auio.uio_rw = UIO_READ; 4433 auio.uio_segflg = bufseg; 4434 auio.uio_td = td; 4435 vn_lock(vp, LK_SHARED | LK_RETRY); 4436 /* 4437 * We want to return ENOTDIR for anything that is not VDIR, but 4438 * not for VBAD, and we can't check for VBAD while the vnode is 4439 * unlocked. 4440 */ 4441 if (vp->v_type != VDIR) { 4442 if (vp->v_type == VBAD) 4443 error = EBADF; 4444 else 4445 error = ENOTDIR; 4446 VOP_UNLOCK(vp); 4447 goto fail; 4448 } 4449 AUDIT_ARG_VNODE1(vp); 4450 loff = auio.uio_offset = foffset; 4451 #ifdef MAC 4452 error = mac_vnode_check_readdir(td->td_ucred, vp); 4453 if (error == 0) 4454 #endif 4455 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL, 4456 NULL); 4457 foffset = auio.uio_offset; 4458 if (error != 0) { 4459 VOP_UNLOCK(vp); 4460 goto fail; 4461 } 4462 if (count == auio.uio_resid && 4463 (vp->v_vflag & VV_ROOT) && 4464 (vp->v_mount->mnt_flag & MNT_UNION)) { 4465 struct vnode *tvp = vp; 4466 4467 vp = vp->v_mount->mnt_vnodecovered; 4468 vref(vp); 4469 fp->f_vnode = vp; 4470 foffset = 0; 4471 vput(tvp); 4472 goto unionread; 4473 } 4474 VOP_UNLOCK(vp); 4475 *basep = loff; 4476 if (residp != NULL) 4477 *residp = auio.uio_resid; 4478 td->td_retval[0] = count - auio.uio_resid; 4479 fail: 4480 foffset_unlock(fp, foffset, 0); 4481 fdrop(fp, td); 4482 return (error); 4483 } 4484 4485 /* 4486 * Set the mode mask for creation of filesystem nodes. 4487 */ 4488 #ifndef _SYS_SYSPROTO_H_ 4489 struct umask_args { 4490 int newmask; 4491 }; 4492 #endif 4493 int 4494 sys_umask(struct thread *td, struct umask_args *uap) 4495 { 4496 struct pwddesc *pdp; 4497 4498 pdp = td->td_proc->p_pd; 4499 PWDDESC_XLOCK(pdp); 4500 td->td_retval[0] = pdp->pd_cmask; 4501 pdp->pd_cmask = uap->newmask & ALLPERMS; 4502 PWDDESC_XUNLOCK(pdp); 4503 return (0); 4504 } 4505 4506 /* 4507 * Void all references to file by ripping underlying filesystem away from 4508 * vnode. 4509 */ 4510 #ifndef _SYS_SYSPROTO_H_ 4511 struct revoke_args { 4512 char *path; 4513 }; 4514 #endif 4515 int 4516 sys_revoke(struct thread *td, struct revoke_args *uap) 4517 { 4518 struct vnode *vp; 4519 struct vattr vattr; 4520 struct nameidata nd; 4521 int error; 4522 4523 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE, 4524 uap->path); 4525 if ((error = namei(&nd)) != 0) 4526 return (error); 4527 vp = nd.ni_vp; 4528 NDFREE_PNBUF(&nd); 4529 if (vp->v_type != VCHR || vp->v_rdev == NULL) { 4530 error = EINVAL; 4531 goto out; 4532 } 4533 #ifdef MAC 4534 error = mac_vnode_check_revoke(td->td_ucred, vp); 4535 if (error != 0) 4536 goto out; 4537 #endif 4538 error = VOP_GETATTR(vp, &vattr, td->td_ucred); 4539 if (error != 0) 4540 goto out; 4541 if (td->td_ucred->cr_uid != vattr.va_uid) { 4542 error = priv_check(td, PRIV_VFS_ADMIN); 4543 if (error != 0) 4544 goto out; 4545 } 4546 if (devfs_usecount(vp) > 0) 4547 VOP_REVOKE(vp, REVOKEALL); 4548 out: 4549 vput(vp); 4550 return (error); 4551 } 4552 4553 /* 4554 * This variant of getvnode() allows O_PATH files. Caller should 4555 * ensure that returned file and vnode are only used for compatible 4556 * semantics. 4557 */ 4558 int 4559 getvnode_path(struct thread *td, int fd, const cap_rights_t *rightsp, 4560 uint8_t *flagsp, struct file **fpp) 4561 { 4562 struct file *fp; 4563 int error; 4564 4565 error = fget_unlocked_flags(td, fd, rightsp, flagsp, &fp); 4566 if (error != 0) 4567 return (error); 4568 4569 /* 4570 * The file could be not of the vnode type, or it may be not 4571 * yet fully initialized, in which case the f_vnode pointer 4572 * may be set, but f_ops is still badfileops. E.g., 4573 * devfs_open() transiently create such situation to 4574 * facilitate csw d_fdopen(). 4575 * 4576 * Dupfdopen() handling in kern_openat() installs the 4577 * half-baked file into the process descriptor table, allowing 4578 * other thread to dereference it. Guard against the race by 4579 * checking f_ops. 4580 */ 4581 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) { 4582 fdrop(fp, td); 4583 *fpp = NULL; 4584 return (EINVAL); 4585 } 4586 4587 *fpp = fp; 4588 return (0); 4589 } 4590 4591 /* 4592 * Convert a user file descriptor to a kernel file entry and check 4593 * that, if it is a capability, the correct rights are present. 4594 * A reference on the file entry is held upon returning. 4595 */ 4596 int 4597 getvnode(struct thread *td, int fd, const cap_rights_t *rightsp, 4598 struct file **fpp) 4599 { 4600 int error; 4601 4602 error = getvnode_path(td, fd, rightsp, NULL, fpp); 4603 if (__predict_false(error != 0)) 4604 return (error); 4605 4606 /* 4607 * Filter out O_PATH file descriptors, most getvnode() callers 4608 * do not call fo_ methods. 4609 */ 4610 if (__predict_false((*fpp)->f_ops == &path_fileops)) { 4611 fdrop(*fpp, td); 4612 *fpp = NULL; 4613 error = EBADF; 4614 } 4615 4616 return (error); 4617 } 4618 4619 /* 4620 * Get an (NFS) file handle. 4621 */ 4622 #ifndef _SYS_SYSPROTO_H_ 4623 struct lgetfh_args { 4624 char *fname; 4625 fhandle_t *fhp; 4626 }; 4627 #endif 4628 int 4629 sys_lgetfh(struct thread *td, struct lgetfh_args *uap) 4630 { 4631 4632 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname, 4633 UIO_USERSPACE, uap->fhp, UIO_USERSPACE)); 4634 } 4635 4636 #ifndef _SYS_SYSPROTO_H_ 4637 struct getfh_args { 4638 char *fname; 4639 fhandle_t *fhp; 4640 }; 4641 #endif 4642 int 4643 sys_getfh(struct thread *td, struct getfh_args *uap) 4644 { 4645 4646 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE, 4647 uap->fhp, UIO_USERSPACE)); 4648 } 4649 4650 /* 4651 * syscall for the rpc.lockd to use to translate an open descriptor into 4652 * a NFS file handle. 4653 * 4654 * warning: do not remove the priv_check() call or this becomes one giant 4655 * security hole. 4656 */ 4657 #ifndef _SYS_SYSPROTO_H_ 4658 struct getfhat_args { 4659 int fd; 4660 char *path; 4661 fhandle_t *fhp; 4662 int flags; 4663 }; 4664 #endif 4665 int 4666 sys_getfhat(struct thread *td, struct getfhat_args *uap) 4667 { 4668 4669 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE, 4670 uap->fhp, UIO_USERSPACE)); 4671 } 4672 4673 int 4674 kern_getfhat(struct thread *td, int flags, int fd, const char *path, 4675 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg) 4676 { 4677 struct nameidata nd; 4678 fhandle_t fh; 4679 struct vnode *vp; 4680 int error; 4681 4682 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0) 4683 return (EINVAL); 4684 error = priv_check(td, PRIV_VFS_GETFH); 4685 if (error != 0) 4686 return (error); 4687 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW | 4688 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path, 4689 fd); 4690 error = namei(&nd); 4691 if (error != 0) 4692 return (error); 4693 NDFREE_PNBUF(&nd); 4694 vp = nd.ni_vp; 4695 bzero(&fh, sizeof(fh)); 4696 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid; 4697 error = VOP_VPTOFH(vp, &fh.fh_fid); 4698 vput(vp); 4699 if (error == 0) { 4700 if (fhseg == UIO_USERSPACE) 4701 error = copyout(&fh, fhp, sizeof (fh)); 4702 else 4703 memcpy(fhp, &fh, sizeof(fh)); 4704 } 4705 return (error); 4706 } 4707 4708 #ifndef _SYS_SYSPROTO_H_ 4709 struct fhlink_args { 4710 fhandle_t *fhp; 4711 const char *to; 4712 }; 4713 #endif 4714 int 4715 sys_fhlink(struct thread *td, struct fhlink_args *uap) 4716 { 4717 4718 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp)); 4719 } 4720 4721 #ifndef _SYS_SYSPROTO_H_ 4722 struct fhlinkat_args { 4723 fhandle_t *fhp; 4724 int tofd; 4725 const char *to; 4726 }; 4727 #endif 4728 int 4729 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap) 4730 { 4731 4732 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp)); 4733 } 4734 4735 static int 4736 kern_fhlinkat(struct thread *td, int fd, const char *path, 4737 enum uio_seg pathseg, fhandle_t *fhp) 4738 { 4739 fhandle_t fh; 4740 struct mount *mp; 4741 struct vnode *vp; 4742 int error; 4743 4744 error = priv_check(td, PRIV_VFS_GETFH); 4745 if (error != 0) 4746 return (error); 4747 error = copyin(fhp, &fh, sizeof(fh)); 4748 if (error != 0) 4749 return (error); 4750 do { 4751 bwillwrite(); 4752 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4753 return (ESTALE); 4754 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp); 4755 vfs_unbusy(mp); 4756 if (error != 0) 4757 return (error); 4758 VOP_UNLOCK(vp); 4759 error = kern_linkat_vp(td, vp, fd, path, pathseg); 4760 } while (error == EAGAIN || error == ERELOOKUP); 4761 return (error); 4762 } 4763 4764 #ifndef _SYS_SYSPROTO_H_ 4765 struct fhreadlink_args { 4766 fhandle_t *fhp; 4767 char *buf; 4768 size_t bufsize; 4769 }; 4770 #endif 4771 int 4772 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap) 4773 { 4774 fhandle_t fh; 4775 struct mount *mp; 4776 struct vnode *vp; 4777 int error; 4778 4779 error = priv_check(td, PRIV_VFS_GETFH); 4780 if (error != 0) 4781 return (error); 4782 if (uap->bufsize > IOSIZE_MAX) 4783 return (EINVAL); 4784 error = copyin(uap->fhp, &fh, sizeof(fh)); 4785 if (error != 0) 4786 return (error); 4787 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4788 return (ESTALE); 4789 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp); 4790 vfs_unbusy(mp); 4791 if (error != 0) 4792 return (error); 4793 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td); 4794 vput(vp); 4795 return (error); 4796 } 4797 4798 /* 4799 * syscall for the rpc.lockd to use to translate a NFS file handle into an 4800 * open descriptor. 4801 * 4802 * warning: do not remove the priv_check() call or this becomes one giant 4803 * security hole. 4804 */ 4805 #ifndef _SYS_SYSPROTO_H_ 4806 struct fhopen_args { 4807 const struct fhandle *u_fhp; 4808 int flags; 4809 }; 4810 #endif 4811 int 4812 sys_fhopen(struct thread *td, struct fhopen_args *uap) 4813 { 4814 return (kern_fhopen(td, uap->u_fhp, uap->flags)); 4815 } 4816 4817 int 4818 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags) 4819 { 4820 struct mount *mp; 4821 struct vnode *vp; 4822 struct fhandle fhp; 4823 struct file *fp; 4824 int error, indx; 4825 bool named_attr; 4826 4827 error = priv_check(td, PRIV_VFS_FHOPEN); 4828 if (error != 0) 4829 return (error); 4830 4831 indx = -1; 4832 if ((flags & O_CREAT) != 0) 4833 return (EINVAL); 4834 error = openflags(&flags); 4835 if (error != 0) 4836 return (error); 4837 error = copyin(u_fhp, &fhp, sizeof(fhp)); 4838 if (error != 0) 4839 return (error); 4840 /* find the mount point */ 4841 mp = vfs_busyfs(&fhp.fh_fsid); 4842 if (mp == NULL) 4843 return (ESTALE); 4844 /* now give me my vnode, it gets returned to me locked */ 4845 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp); 4846 vfs_unbusy(mp); 4847 if (error != 0) 4848 return (error); 4849 4850 /* 4851 * Check to see if the file handle refers to a named attribute 4852 * directory or attribute. If it does, the O_NAMEDATTR flag 4853 * must have been specified. 4854 */ 4855 named_attr = (vn_irflag_read(vp) & 4856 (VIRF_NAMEDDIR | VIRF_NAMEDATTR)) != 0; 4857 if ((named_attr && (flags & O_NAMEDATTR) == 0) || 4858 (!named_attr && (flags & O_NAMEDATTR) != 0)) { 4859 vput(vp); 4860 return (ENOATTR); 4861 } 4862 4863 error = falloc_noinstall(td, &fp); 4864 if (error != 0) { 4865 vput(vp); 4866 return (error); 4867 } 4868 /* Set the flags early so the finit in devfs can pick them up. */ 4869 fp->f_flag = flags & FMASK; 4870 4871 #ifdef INVARIANTS 4872 td->td_dupfd = -1; 4873 #endif 4874 error = vn_open_vnode(vp, flags, td->td_ucred, td, fp); 4875 if (error != 0) { 4876 KASSERT(fp->f_ops == &badfileops, 4877 ("VOP_OPEN in fhopen() set f_ops")); 4878 KASSERT(td->td_dupfd < 0, 4879 ("fhopen() encountered fdopen()")); 4880 4881 vput(vp); 4882 goto bad; 4883 } 4884 #ifdef INVARIANTS 4885 td->td_dupfd = 0; 4886 #endif 4887 finit_open(fp, vp, flags); 4888 VOP_UNLOCK(vp); 4889 if ((flags & O_TRUNC) != 0) { 4890 error = fo_truncate(fp, 0, td->td_ucred, td); 4891 if (error != 0) 4892 goto bad; 4893 } 4894 4895 error = finstall(td, fp, &indx, flags, NULL); 4896 bad: 4897 fdrop(fp, td); 4898 td->td_retval[0] = indx; 4899 return (error); 4900 } 4901 4902 /* 4903 * Stat an (NFS) file handle. 4904 */ 4905 #ifndef _SYS_SYSPROTO_H_ 4906 struct fhstat_args { 4907 struct fhandle *u_fhp; 4908 struct stat *sb; 4909 }; 4910 #endif 4911 int 4912 sys_fhstat(struct thread *td, struct fhstat_args *uap) 4913 { 4914 struct stat sb; 4915 struct fhandle fh; 4916 int error; 4917 4918 error = copyin(uap->u_fhp, &fh, sizeof(fh)); 4919 if (error != 0) 4920 return (error); 4921 error = kern_fhstat(td, fh, &sb); 4922 if (error == 0) 4923 error = copyout(&sb, uap->sb, sizeof(sb)); 4924 return (error); 4925 } 4926 4927 int 4928 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb) 4929 { 4930 struct mount *mp; 4931 struct vnode *vp; 4932 int error; 4933 4934 error = priv_check(td, PRIV_VFS_FHSTAT); 4935 if (error != 0) 4936 return (error); 4937 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4938 return (ESTALE); 4939 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp); 4940 vfs_unbusy(mp); 4941 if (error != 0) 4942 return (error); 4943 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED); 4944 vput(vp); 4945 return (error); 4946 } 4947 4948 /* 4949 * Implement fstatfs() for (NFS) file handles. 4950 */ 4951 #ifndef _SYS_SYSPROTO_H_ 4952 struct fhstatfs_args { 4953 struct fhandle *u_fhp; 4954 struct statfs *buf; 4955 }; 4956 #endif 4957 int 4958 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap) 4959 { 4960 struct statfs *sfp; 4961 fhandle_t fh; 4962 int error; 4963 4964 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t)); 4965 if (error != 0) 4966 return (error); 4967 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK); 4968 error = kern_fhstatfs(td, fh, sfp); 4969 if (error == 0) 4970 error = copyout(sfp, uap->buf, sizeof(*sfp)); 4971 free(sfp, M_STATFS); 4972 return (error); 4973 } 4974 4975 int 4976 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf) 4977 { 4978 struct mount *mp; 4979 struct vnode *vp; 4980 int error; 4981 4982 error = priv_check(td, PRIV_VFS_FHSTATFS); 4983 if (error != 0) 4984 return (error); 4985 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL) 4986 return (ESTALE); 4987 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp); 4988 if (error != 0) { 4989 vfs_unbusy(mp); 4990 return (error); 4991 } 4992 vput(vp); 4993 error = prison_canseemount(td->td_ucred, mp); 4994 if (error != 0) 4995 goto out; 4996 #ifdef MAC 4997 error = mac_mount_check_stat(td->td_ucred, mp); 4998 if (error != 0) 4999 goto out; 5000 #endif 5001 error = VFS_STATFS(mp, buf); 5002 out: 5003 vfs_unbusy(mp); 5004 return (error); 5005 } 5006 5007 /* 5008 * Unlike madvise(2), we do not make a best effort to remember every 5009 * possible caching hint. Instead, we remember the last setting with 5010 * the exception that we will allow POSIX_FADV_NORMAL to adjust the 5011 * region of any current setting. 5012 */ 5013 int 5014 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len, 5015 int advice) 5016 { 5017 struct fadvise_info *fa, *new; 5018 struct file *fp; 5019 struct vnode *vp; 5020 off_t end; 5021 int error; 5022 5023 if (offset < 0 || len < 0 || offset > OFF_MAX - len) 5024 return (EINVAL); 5025 AUDIT_ARG_VALUE(advice); 5026 switch (advice) { 5027 case POSIX_FADV_SEQUENTIAL: 5028 case POSIX_FADV_RANDOM: 5029 case POSIX_FADV_NOREUSE: 5030 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK); 5031 break; 5032 case POSIX_FADV_NORMAL: 5033 case POSIX_FADV_WILLNEED: 5034 case POSIX_FADV_DONTNEED: 5035 new = NULL; 5036 break; 5037 default: 5038 return (EINVAL); 5039 } 5040 /* XXX: CAP_POSIX_FADVISE? */ 5041 AUDIT_ARG_FD(fd); 5042 error = fget(td, fd, &cap_no_rights, &fp); 5043 if (error != 0) 5044 goto out; 5045 AUDIT_ARG_FILE(td->td_proc, fp); 5046 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) { 5047 error = ESPIPE; 5048 goto out; 5049 } 5050 if (fp->f_type != DTYPE_VNODE) { 5051 error = ENODEV; 5052 goto out; 5053 } 5054 vp = fp->f_vnode; 5055 if (vp->v_type != VREG) { 5056 error = ENODEV; 5057 goto out; 5058 } 5059 if (len == 0) 5060 end = OFF_MAX; 5061 else 5062 end = offset + len - 1; 5063 switch (advice) { 5064 case POSIX_FADV_SEQUENTIAL: 5065 case POSIX_FADV_RANDOM: 5066 case POSIX_FADV_NOREUSE: 5067 /* 5068 * Try to merge any existing non-standard region with 5069 * this new region if possible, otherwise create a new 5070 * non-standard region for this request. 5071 */ 5072 mtx_pool_lock(mtxpool_sleep, fp); 5073 fa = fp->f_advice; 5074 if (fa != NULL && fa->fa_advice == advice && 5075 ((fa->fa_start <= end && fa->fa_end >= offset) || 5076 (end != OFF_MAX && fa->fa_start == end + 1) || 5077 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) { 5078 if (offset < fa->fa_start) 5079 fa->fa_start = offset; 5080 if (end > fa->fa_end) 5081 fa->fa_end = end; 5082 } else { 5083 new->fa_advice = advice; 5084 new->fa_start = offset; 5085 new->fa_end = end; 5086 fp->f_advice = new; 5087 new = fa; 5088 } 5089 mtx_pool_unlock(mtxpool_sleep, fp); 5090 break; 5091 case POSIX_FADV_NORMAL: 5092 /* 5093 * If a the "normal" region overlaps with an existing 5094 * non-standard region, trim or remove the 5095 * non-standard region. 5096 */ 5097 mtx_pool_lock(mtxpool_sleep, fp); 5098 fa = fp->f_advice; 5099 if (fa != NULL) { 5100 if (offset <= fa->fa_start && end >= fa->fa_end) { 5101 new = fa; 5102 fp->f_advice = NULL; 5103 } else if (offset <= fa->fa_start && 5104 end >= fa->fa_start) 5105 fa->fa_start = end + 1; 5106 else if (offset <= fa->fa_end && end >= fa->fa_end) 5107 fa->fa_end = offset - 1; 5108 else if (offset >= fa->fa_start && end <= fa->fa_end) { 5109 /* 5110 * If the "normal" region is a middle 5111 * portion of the existing 5112 * non-standard region, just remove 5113 * the whole thing rather than picking 5114 * one side or the other to 5115 * preserve. 5116 */ 5117 new = fa; 5118 fp->f_advice = NULL; 5119 } 5120 } 5121 mtx_pool_unlock(mtxpool_sleep, fp); 5122 break; 5123 case POSIX_FADV_WILLNEED: 5124 case POSIX_FADV_DONTNEED: 5125 error = VOP_ADVISE(vp, offset, end, advice); 5126 break; 5127 } 5128 out: 5129 if (fp != NULL) 5130 fdrop(fp, td); 5131 free(new, M_FADVISE); 5132 return (error); 5133 } 5134 5135 int 5136 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap) 5137 { 5138 int error; 5139 5140 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len, 5141 uap->advice); 5142 return (kern_posix_error(td, error)); 5143 } 5144 5145 int 5146 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd, 5147 off_t *outoffp, size_t len, unsigned int flags) 5148 { 5149 struct file *infp, *infp1, *outfp, *outfp1; 5150 struct vnode *invp, *outvp; 5151 int error; 5152 size_t retlen; 5153 void *rl_rcookie, *rl_wcookie; 5154 off_t inoff, outoff, savinoff, savoutoff; 5155 bool foffsets_locked, foffsets_set; 5156 5157 infp = outfp = NULL; 5158 rl_rcookie = rl_wcookie = NULL; 5159 foffsets_locked = false; 5160 foffsets_set = false; 5161 error = 0; 5162 retlen = 0; 5163 5164 if ((flags & ~COPY_FILE_RANGE_USERFLAGS) != 0) { 5165 error = EINVAL; 5166 goto out; 5167 } 5168 if (len > SSIZE_MAX) 5169 /* 5170 * Although the len argument is size_t, the return argument 5171 * is ssize_t (which is signed). Therefore a size that won't 5172 * fit in ssize_t can't be returned. 5173 */ 5174 len = SSIZE_MAX; 5175 5176 /* Get the file structures for the file descriptors. */ 5177 error = fget_read(td, infd, 5178 inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp); 5179 if (error != 0) 5180 goto out; 5181 if (infp->f_ops == &badfileops) { 5182 error = EBADF; 5183 goto out; 5184 } 5185 if (infp->f_vnode == NULL) { 5186 error = EINVAL; 5187 goto out; 5188 } 5189 error = fget_write(td, outfd, 5190 outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp); 5191 if (error != 0) 5192 goto out; 5193 if (outfp->f_ops == &badfileops) { 5194 error = EBADF; 5195 goto out; 5196 } 5197 if (outfp->f_vnode == NULL) { 5198 error = EINVAL; 5199 goto out; 5200 } 5201 5202 /* 5203 * Figure out which file offsets we're reading from and writing to. 5204 * If the offsets come from the file descriptions, we need to lock them, 5205 * and locking both offsets requires a loop to avoid deadlocks. 5206 */ 5207 infp1 = outfp1 = NULL; 5208 if (inoffp != NULL) 5209 inoff = *inoffp; 5210 else 5211 infp1 = infp; 5212 if (outoffp != NULL) 5213 outoff = *outoffp; 5214 else 5215 outfp1 = outfp; 5216 if (infp1 != NULL || outfp1 != NULL) { 5217 if (infp1 == outfp1) { 5218 /* 5219 * Overlapping ranges are not allowed. A more thorough 5220 * check appears below, but we must not lock the same 5221 * offset twice. 5222 */ 5223 error = EINVAL; 5224 goto out; 5225 } 5226 foffset_lock_pair(infp1, &inoff, outfp1, &outoff, 0); 5227 foffsets_locked = true; 5228 } else { 5229 foffsets_set = true; 5230 } 5231 savinoff = inoff; 5232 savoutoff = outoff; 5233 5234 invp = infp->f_vnode; 5235 outvp = outfp->f_vnode; 5236 /* Sanity check the f_flag bits. */ 5237 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE || 5238 (infp->f_flag & FREAD) == 0) { 5239 error = EBADF; 5240 goto out; 5241 } 5242 5243 /* If len == 0, just return 0. */ 5244 if (len == 0) 5245 goto out; 5246 5247 /* 5248 * Make sure that the ranges we check and lock below are valid. Note 5249 * that len is clamped to SSIZE_MAX above. 5250 */ 5251 if (inoff < 0 || outoff < 0) { 5252 error = EINVAL; 5253 goto out; 5254 } 5255 5256 /* 5257 * If infp and outfp refer to the same file, the byte ranges cannot 5258 * overlap. 5259 */ 5260 if (invp == outvp) { 5261 if ((inoff <= outoff && inoff + len > outoff) || 5262 (inoff > outoff && outoff + len > inoff)) { 5263 error = EINVAL; 5264 goto out; 5265 } 5266 rangelock_may_recurse(&invp->v_rl); 5267 } 5268 5269 /* Range lock the byte ranges for both invp and outvp. */ 5270 for (;;) { 5271 rl_wcookie = vn_rangelock_wlock(outvp, outoff, outoff + len); 5272 rl_rcookie = vn_rangelock_tryrlock(invp, inoff, inoff + len); 5273 if (rl_rcookie != NULL) 5274 break; 5275 vn_rangelock_unlock(outvp, rl_wcookie); 5276 rl_rcookie = vn_rangelock_rlock(invp, inoff, inoff + len); 5277 vn_rangelock_unlock(invp, rl_rcookie); 5278 } 5279 5280 retlen = len; 5281 error = vn_copy_file_range(invp, &inoff, outvp, &outoff, &retlen, 5282 flags, infp->f_cred, outfp->f_cred, td); 5283 out: 5284 if (rl_rcookie != NULL) 5285 vn_rangelock_unlock(invp, rl_rcookie); 5286 if (rl_wcookie != NULL) 5287 vn_rangelock_unlock(outvp, rl_wcookie); 5288 if ((foffsets_locked || foffsets_set) && 5289 (error == EINTR || error == ERESTART)) { 5290 inoff = savinoff; 5291 outoff = savoutoff; 5292 } 5293 if (foffsets_locked) { 5294 if (inoffp == NULL) 5295 foffset_unlock(infp, inoff, 0); 5296 else 5297 *inoffp = inoff; 5298 if (outoffp == NULL) 5299 foffset_unlock(outfp, outoff, 0); 5300 else 5301 *outoffp = outoff; 5302 } else if (foffsets_set) { 5303 *inoffp = inoff; 5304 *outoffp = outoff; 5305 } 5306 if (outfp != NULL) 5307 fdrop(outfp, td); 5308 if (infp != NULL) 5309 fdrop(infp, td); 5310 td->td_retval[0] = retlen; 5311 return (error); 5312 } 5313 5314 int 5315 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap) 5316 { 5317 off_t inoff, outoff, *inoffp, *outoffp; 5318 int error; 5319 5320 inoffp = outoffp = NULL; 5321 if (uap->inoffp != NULL) { 5322 error = copyin(uap->inoffp, &inoff, sizeof(off_t)); 5323 if (error != 0) 5324 return (error); 5325 inoffp = &inoff; 5326 } 5327 if (uap->outoffp != NULL) { 5328 error = copyin(uap->outoffp, &outoff, sizeof(off_t)); 5329 if (error != 0) 5330 return (error); 5331 outoffp = &outoff; 5332 } 5333 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd, 5334 outoffp, uap->len, uap->flags); 5335 if (error == 0 && uap->inoffp != NULL) 5336 error = copyout(inoffp, uap->inoffp, sizeof(off_t)); 5337 if (error == 0 && uap->outoffp != NULL) 5338 error = copyout(outoffp, uap->outoffp, sizeof(off_t)); 5339 return (error); 5340 } 5341