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