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