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