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