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