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