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