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