2 * SPDX-License-Identifier: BSD-3-Clause
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.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/cdefs.h>
38 #include "opt_capsicum.h"
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #ifdef COMPAT_FREEBSD11
44 #include <sys/abi_compat.h>
48 #include <sys/capsicum.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/mutex.h>
53 #include <sys/sysproto.h>
54 #include <sys/namei.h>
55 #include <sys/filedesc.h>
56 #include <sys/kernel.h>
57 #include <sys/fcntl.h>
59 #include <sys/filio.h>
60 #include <sys/limits.h>
61 #include <sys/linker.h>
62 #include <sys/rwlock.h>
66 #include <sys/unistd.h>
67 #include <sys/vnode.h>
70 #include <sys/dirent.h>
72 #include <sys/syscallsubr.h>
73 #include <sys/sysctl.h>
75 #include <sys/ktrace.h>
78 #include <machine/stdarg.h>
80 #include <security/audit/audit.h>
81 #include <security/mac/mac_framework.h>
84 #include <vm/vm_object.h>
85 #include <vm/vm_page.h>
88 #include <fs/devfs/devfs.h>
90 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
92 static int kern_chflagsat(struct thread *td, int fd, const char *path,
93 enum uio_seg pathseg, u_long flags, int atflag);
94 static int setfflags(struct thread *td, struct vnode *, u_long);
95 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
96 static int getutimens(const struct timespec *, enum uio_seg,
97 struct timespec *, int *);
98 static int setutimes(struct thread *td, struct vnode *,
99 const struct timespec *, int, int);
100 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
102 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
103 enum uio_seg pathseg, fhandle_t *fhp);
104 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
105 size_t count, struct thread *td);
106 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
107 const char *path, enum uio_seg segflag);
110 at2cnpflags(u_int at_flags, u_int mask)
114 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
115 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
119 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
121 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
123 /* NOFOLLOW is pseudo flag */
124 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
125 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
128 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
134 kern_sync(struct thread *td)
136 struct mount *mp, *nmp;
139 mtx_lock(&mountlist_mtx);
140 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
141 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
142 nmp = TAILQ_NEXT(mp, mnt_list);
145 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
146 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
147 save = curthread_pflags_set(TDP_SYNCIO);
148 vfs_periodic(mp, MNT_NOWAIT);
149 VFS_SYNC(mp, MNT_NOWAIT);
150 curthread_pflags_restore(save);
151 vn_finished_write(mp);
153 mtx_lock(&mountlist_mtx);
154 nmp = TAILQ_NEXT(mp, mnt_list);
157 mtx_unlock(&mountlist_mtx);
162 * Sync each mounted filesystem.
164 #ifndef _SYS_SYSPROTO_H_
171 sys_sync(struct thread *td, struct sync_args *uap)
174 return (kern_sync(td));
178 * Change filesystem quotas.
180 #ifndef _SYS_SYSPROTO_H_
181 struct quotactl_args {
189 sys_quotactl(struct thread *td, struct quotactl_args *uap)
196 AUDIT_ARG_CMD(uap->cmd);
197 AUDIT_ARG_UID(uap->uid);
198 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
200 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
202 if ((error = namei(&nd)) != 0)
205 mp = nd.ni_vp->v_mount;
208 error = vfs_busy(mp, 0);
214 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
217 * Since quota on/off operations typically need to open quota
218 * files, the implementation may need to unbusy the mount point
219 * before calling into namei. Otherwise, unmount might be
220 * started between two vfs_busy() invocations (first is ours,
221 * second is from mount point cross-walk code in lookup()),
224 * Avoid unbusying mp if the implementation indicates it has
234 * Used by statfs conversion routines to scale the block size up if
235 * necessary so that all of the block counts are <= 'max_size'. Note
236 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
240 statfs_scale_blocks(struct statfs *sf, long max_size)
245 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
248 * Attempt to scale the block counts to give a more accurate
249 * overview to userland of the ratio of free space to used
250 * space. To do this, find the largest block count and compute
251 * a divisor that lets it fit into a signed integer <= max_size.
253 if (sf->f_bavail < 0)
254 count = -sf->f_bavail;
256 count = sf->f_bavail;
257 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
258 if (count <= max_size)
261 count >>= flsl(max_size);
268 sf->f_bsize <<= shift;
269 sf->f_blocks >>= shift;
270 sf->f_bfree >>= shift;
271 sf->f_bavail >>= shift;
275 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
281 error = vfs_busy(mp, 0);
286 error = mac_mount_check_stat(td->td_ucred, mp);
290 error = VFS_STATFS(mp, buf);
293 if (priv_check_cred_vfs_generation(td->td_ucred)) {
294 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
295 prison_enforce_statfs(td->td_ucred, mp, buf);
303 * Get filesystem statistics.
305 #ifndef _SYS_SYSPROTO_H_
312 sys_statfs(struct thread *td, struct statfs_args *uap)
317 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
318 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
320 error = copyout(sfp, uap->buf, sizeof(struct statfs));
326 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
333 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
338 mp = vfs_ref_from_vp(nd.ni_vp);
340 return (kern_do_statfs(td, mp, buf));
344 * Get filesystem statistics.
346 #ifndef _SYS_SYSPROTO_H_
347 struct fstatfs_args {
353 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
358 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
359 error = kern_fstatfs(td, uap->fd, sfp);
361 error = copyout(sfp, uap->buf, sizeof(struct statfs));
367 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
375 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
380 if (AUDITING_TD(td)) {
381 vn_lock(vp, LK_SHARED | LK_RETRY);
382 AUDIT_ARG_VNODE1(vp);
386 mp = vfs_ref_from_vp(vp);
388 return (kern_do_statfs(td, mp, buf));
392 * Get statistics on all filesystems.
394 #ifndef _SYS_SYSPROTO_H_
395 struct getfsstat_args {
402 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
407 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
409 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
410 UIO_USERSPACE, uap->mode);
412 td->td_retval[0] = count;
417 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
418 * The caller is responsible for freeing memory which will be allocated
422 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
423 size_t *countp, enum uio_seg bufseg, int mode)
425 struct mount *mp, *nmp;
426 struct statfs *sfsp, *sp, *sptmp, *tofree;
427 size_t count, maxcount;
435 if (bufseg == UIO_SYSSPACE)
440 maxcount = bufsize / sizeof(struct statfs);
444 } else if (bufseg == UIO_USERSPACE) {
447 } else /* if (bufseg == UIO_SYSSPACE) */ {
449 mtx_lock(&mountlist_mtx);
450 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
453 mtx_unlock(&mountlist_mtx);
454 if (maxcount > count)
456 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
463 * If there is no target buffer they only want the count.
465 * This could be TAILQ_FOREACH but it is open-coded to match the original
469 mtx_lock(&mountlist_mtx);
470 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
471 if (prison_canseemount(td->td_ucred, mp) != 0) {
472 nmp = TAILQ_NEXT(mp, mnt_list);
476 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
477 nmp = TAILQ_NEXT(mp, mnt_list);
482 nmp = TAILQ_NEXT(mp, mnt_list);
484 mtx_unlock(&mountlist_mtx);
490 * They want the entire thing.
492 * Short-circuit the corner case of no room for anything, avoids
499 mtx_lock(&mountlist_mtx);
500 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
501 if (prison_canseemount(td->td_ucred, mp) != 0) {
502 nmp = TAILQ_NEXT(mp, mnt_list);
506 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
507 nmp = TAILQ_NEXT(mp, mnt_list);
511 if (mode == MNT_WAIT) {
512 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
514 * If vfs_busy() failed, and MBF_NOWAIT
515 * wasn't passed, then the mp is gone.
516 * Furthermore, because of MBF_MNTLSTLOCK,
517 * the mountlist_mtx was dropped. We have
518 * no other choice than to start over.
520 mtx_unlock(&mountlist_mtx);
521 free(tofree, M_STATFS);
525 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
526 nmp = TAILQ_NEXT(mp, mnt_list);
532 * If MNT_NOWAIT is specified, do not refresh
535 if (mode != MNT_NOWAIT) {
536 error = VFS_STATFS(mp, sp);
538 mtx_lock(&mountlist_mtx);
539 nmp = TAILQ_NEXT(mp, mnt_list);
544 if (priv_check_cred_vfs_generation(td->td_ucred)) {
545 sptmp = malloc(sizeof(struct statfs), M_STATFS,
548 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
549 prison_enforce_statfs(td->td_ucred, mp, sptmp);
553 if (bufseg == UIO_SYSSPACE) {
554 bcopy(sp, sfsp, sizeof(*sp));
555 free(sptmp, M_STATFS);
556 } else /* if (bufseg == UIO_USERSPACE) */ {
557 error = copyout(sp, sfsp, sizeof(*sp));
558 free(sptmp, M_STATFS);
567 if (count == maxcount) {
572 mtx_lock(&mountlist_mtx);
573 nmp = TAILQ_NEXT(mp, mnt_list);
576 mtx_unlock(&mountlist_mtx);
582 #ifdef COMPAT_FREEBSD4
584 * Get old format filesystem statistics.
586 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
588 #ifndef _SYS_SYSPROTO_H_
589 struct freebsd4_statfs_args {
595 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
601 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
602 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
604 freebsd4_cvtstatfs(sfp, &osb);
605 error = copyout(&osb, uap->buf, sizeof(osb));
612 * Get filesystem statistics.
614 #ifndef _SYS_SYSPROTO_H_
615 struct freebsd4_fstatfs_args {
621 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
627 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
628 error = kern_fstatfs(td, uap->fd, sfp);
630 freebsd4_cvtstatfs(sfp, &osb);
631 error = copyout(&osb, uap->buf, sizeof(osb));
638 * Get statistics on all filesystems.
640 #ifndef _SYS_SYSPROTO_H_
641 struct freebsd4_getfsstat_args {
648 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
650 struct statfs *buf, *sp;
655 if (uap->bufsize < 0)
657 count = uap->bufsize / sizeof(struct ostatfs);
658 if (count > SIZE_MAX / sizeof(struct statfs))
660 size = count * sizeof(struct statfs);
661 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
664 td->td_retval[0] = count;
667 while (count != 0 && error == 0) {
668 freebsd4_cvtstatfs(sp, &osb);
669 error = copyout(&osb, uap->buf, sizeof(osb));
680 * Implement fstatfs() for (NFS) file handles.
682 #ifndef _SYS_SYSPROTO_H_
683 struct freebsd4_fhstatfs_args {
684 struct fhandle *u_fhp;
689 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
696 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
699 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
700 error = kern_fhstatfs(td, fh, sfp);
702 freebsd4_cvtstatfs(sfp, &osb);
703 error = copyout(&osb, uap->buf, sizeof(osb));
710 * Convert a new format statfs structure to an old format statfs structure.
713 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
716 statfs_scale_blocks(nsp, LONG_MAX);
717 bzero(osp, sizeof(*osp));
718 osp->f_bsize = nsp->f_bsize;
719 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
720 osp->f_blocks = nsp->f_blocks;
721 osp->f_bfree = nsp->f_bfree;
722 osp->f_bavail = nsp->f_bavail;
723 osp->f_files = MIN(nsp->f_files, LONG_MAX);
724 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
725 osp->f_owner = nsp->f_owner;
726 osp->f_type = nsp->f_type;
727 osp->f_flags = nsp->f_flags;
728 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
729 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
730 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
731 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
732 strlcpy(osp->f_fstypename, nsp->f_fstypename,
733 MIN(MFSNAMELEN, OMFSNAMELEN));
734 strlcpy(osp->f_mntonname, nsp->f_mntonname,
735 MIN(MNAMELEN, OMNAMELEN));
736 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
737 MIN(MNAMELEN, OMNAMELEN));
738 osp->f_fsid = nsp->f_fsid;
740 #endif /* COMPAT_FREEBSD4 */
742 #if defined(COMPAT_FREEBSD11)
744 * Get old format filesystem statistics.
746 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
749 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
751 struct freebsd11_statfs osb;
755 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
756 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
758 freebsd11_cvtstatfs(sfp, &osb);
759 error = copyout(&osb, uap->buf, sizeof(osb));
766 * Get filesystem statistics.
769 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
771 struct freebsd11_statfs osb;
775 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
776 error = kern_fstatfs(td, uap->fd, sfp);
778 freebsd11_cvtstatfs(sfp, &osb);
779 error = copyout(&osb, uap->buf, sizeof(osb));
786 * Get statistics on all filesystems.
789 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
791 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
795 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
796 long bufsize, int mode)
798 struct freebsd11_statfs osb;
799 struct statfs *buf, *sp;
806 count = bufsize / sizeof(struct ostatfs);
807 size = count * sizeof(struct statfs);
808 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
810 td->td_retval[0] = count;
813 while (count > 0 && error == 0) {
814 freebsd11_cvtstatfs(sp, &osb);
815 error = copyout(&osb, ubuf, sizeof(osb));
826 * Implement fstatfs() for (NFS) file handles.
829 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
831 struct freebsd11_statfs osb;
836 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
839 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
840 error = kern_fhstatfs(td, fh, sfp);
842 freebsd11_cvtstatfs(sfp, &osb);
843 error = copyout(&osb, uap->buf, sizeof(osb));
850 * Convert a new format statfs structure to an old format statfs structure.
853 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
856 bzero(osp, sizeof(*osp));
857 osp->f_version = FREEBSD11_STATFS_VERSION;
858 osp->f_type = nsp->f_type;
859 osp->f_flags = nsp->f_flags;
860 osp->f_bsize = nsp->f_bsize;
861 osp->f_iosize = nsp->f_iosize;
862 osp->f_blocks = nsp->f_blocks;
863 osp->f_bfree = nsp->f_bfree;
864 osp->f_bavail = nsp->f_bavail;
865 osp->f_files = nsp->f_files;
866 osp->f_ffree = nsp->f_ffree;
867 osp->f_syncwrites = nsp->f_syncwrites;
868 osp->f_asyncwrites = nsp->f_asyncwrites;
869 osp->f_syncreads = nsp->f_syncreads;
870 osp->f_asyncreads = nsp->f_asyncreads;
871 osp->f_namemax = nsp->f_namemax;
872 osp->f_owner = nsp->f_owner;
873 osp->f_fsid = nsp->f_fsid;
874 strlcpy(osp->f_fstypename, nsp->f_fstypename,
875 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
876 strlcpy(osp->f_mntonname, nsp->f_mntonname,
877 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
878 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
879 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
881 #endif /* COMPAT_FREEBSD11 */
884 * Change current working directory to a given file descriptor.
886 #ifndef _SYS_SYSPROTO_H_
892 sys_fchdir(struct thread *td, struct fchdir_args *uap)
894 struct vnode *vp, *tdp;
899 AUDIT_ARG_FD(uap->fd);
900 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
907 vn_lock(vp, LK_SHARED | LK_RETRY);
908 AUDIT_ARG_VNODE1(vp);
909 error = change_dir(vp, td);
910 while (!error && (mp = vp->v_mountedhere) != NULL) {
913 error = VFS_ROOT(mp, LK_SHARED, &tdp);
930 * Change current working directory (``.'').
932 #ifndef _SYS_SYSPROTO_H_
938 sys_chdir(struct thread *td, struct chdir_args *uap)
941 return (kern_chdir(td, uap->path, UIO_USERSPACE));
945 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
950 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
952 if ((error = namei(&nd)) != 0)
954 if ((error = change_dir(nd.ni_vp, td)) != 0) {
959 VOP_UNLOCK(nd.ni_vp);
961 pwd_chdir(td, nd.ni_vp);
965 static int unprivileged_chroot = 0;
966 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
967 &unprivileged_chroot, 0,
968 "Unprivileged processes can use chroot(2)");
970 * Change notion of root (``/'') directory.
972 #ifndef _SYS_SYSPROTO_H_
978 sys_chroot(struct thread *td, struct chroot_args *uap)
984 error = priv_check(td, PRIV_VFS_CHROOT);
988 if (unprivileged_chroot == 0 ||
989 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
995 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
996 UIO_USERSPACE, uap->path);
1001 error = change_dir(nd.ni_vp, td);
1005 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1009 VOP_UNLOCK(nd.ni_vp);
1010 error = pwd_chroot(td, nd.ni_vp);
1019 * Common routine for chroot and chdir. Callers must provide a locked vnode
1023 change_dir(struct vnode *vp, struct thread *td)
1029 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1030 if (vp->v_type != VDIR)
1033 error = mac_vnode_check_chdir(td->td_ucred, vp);
1037 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1040 static __inline void
1041 flags_to_rights(int flags, cap_rights_t *rightsp)
1043 if (flags & O_EXEC) {
1044 cap_rights_set_one(rightsp, CAP_FEXECVE);
1048 switch ((flags & O_ACCMODE)) {
1050 cap_rights_set_one(rightsp, CAP_READ);
1053 cap_rights_set_one(rightsp, CAP_READ);
1056 cap_rights_set_one(rightsp, CAP_WRITE);
1057 if (!(flags & (O_APPEND | O_TRUNC)))
1058 cap_rights_set_one(rightsp, CAP_SEEK);
1063 if (flags & O_CREAT)
1064 cap_rights_set_one(rightsp, CAP_CREATE);
1066 if (flags & O_TRUNC)
1067 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1069 if (flags & (O_SYNC | O_FSYNC))
1070 cap_rights_set_one(rightsp, CAP_FSYNC);
1072 if (flags & (O_EXLOCK | O_SHLOCK))
1073 cap_rights_set_one(rightsp, CAP_FLOCK);
1077 * Check permissions, allocate an open file structure, and call the device
1078 * open routine if any.
1080 #ifndef _SYS_SYSPROTO_H_
1088 sys_open(struct thread *td, struct open_args *uap)
1091 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1092 uap->flags, uap->mode));
1095 #ifndef _SYS_SYSPROTO_H_
1096 struct openat_args {
1104 sys_openat(struct thread *td, struct openat_args *uap)
1107 AUDIT_ARG_FD(uap->fd);
1108 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1113 kern_openat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1114 int flags, int mode)
1116 struct proc *p = td->td_proc;
1117 struct filedesc *fdp;
1118 struct pwddesc *pdp;
1121 struct nameidata nd;
1122 cap_rights_t rights;
1123 int cmode, error, indx;
1129 AUDIT_ARG_FFLAGS(flags);
1130 AUDIT_ARG_MODE(mode);
1131 cap_rights_init_one(&rights, CAP_LOOKUP);
1132 flags_to_rights(flags, &rights);
1135 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1136 * may be specified. On the other hand, for O_PATH any mode
1137 * except O_EXEC is ignored.
1139 if ((flags & O_PATH) != 0) {
1140 flags &= ~(O_CREAT | O_ACCMODE);
1141 } else if ((flags & O_EXEC) != 0) {
1142 if (flags & O_ACCMODE)
1144 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1147 flags = FFLAGS(flags);
1151 * Allocate a file structure. The descriptor to reference it
1152 * is allocated and used by finstall_refed() below.
1154 error = falloc_noinstall(td, &fp);
1157 /* Set the flags early so the finit in devfs can pick them up. */
1158 fp->f_flag = flags & FMASK;
1159 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1160 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1161 pathseg, path, fd, &rights);
1162 td->td_dupfd = -1; /* XXX check for fdopen */
1163 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1167 * If the vn_open replaced the method vector, something
1168 * wonderous happened deep below and we just pass it up
1169 * pretending we know what we do.
1171 if (error == ENXIO && fp->f_ops != &badfileops) {
1172 MPASS((flags & O_PATH) == 0);
1177 * Handle special fdopen() case. bleh.
1179 * Don't do this for relative (capability) lookups; we don't
1180 * understand exactly what would happen, and we don't think
1181 * that it ever should.
1183 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1184 (error == ENODEV || error == ENXIO) &&
1185 td->td_dupfd >= 0) {
1186 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1199 * Store the vnode, for any f_type. Typically, the vnode use
1200 * count is decremented by direct call to vn_closefile() for
1201 * files that switched type in the cdevsw fdopen() method.
1206 * If the file wasn't claimed by devfs bind it to the normal
1207 * vnode operations here.
1209 if (fp->f_ops == &badfileops) {
1210 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1211 ("Unexpected fifo fp %p vp %p", fp, vp));
1212 if ((flags & O_PATH) != 0) {
1213 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1214 DTYPE_VNODE, NULL, &path_fileops);
1216 finit_vnode(fp, flags, NULL, &vnops);
1221 if (flags & O_TRUNC) {
1222 error = fo_truncate(fp, 0, td->td_ucred, td);
1228 * If we haven't already installed the FD (for dupfdopen), do so now.
1231 struct filecaps *fcaps;
1234 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1235 fcaps = &nd.ni_filecaps;
1239 error = finstall_refed(td, fp, &indx, flags, fcaps);
1240 /* On success finstall_refed() consumes fcaps. */
1245 NDFREE_IOCTLCAPS(&nd);
1246 falloc_abort(td, fp);
1249 td->td_retval[0] = indx;
1252 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1253 NDFREE_IOCTLCAPS(&nd);
1254 falloc_abort(td, fp);
1262 #ifndef _SYS_SYSPROTO_H_
1263 struct ocreat_args {
1269 ocreat(struct thread *td, struct ocreat_args *uap)
1272 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1273 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1275 #endif /* COMPAT_43 */
1278 * Create a special file.
1280 #ifndef _SYS_SYSPROTO_H_
1281 struct mknodat_args {
1289 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1292 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1296 #if defined(COMPAT_FREEBSD11)
1298 freebsd11_mknod(struct thread *td,
1299 struct freebsd11_mknod_args *uap)
1302 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1303 uap->mode, uap->dev));
1307 freebsd11_mknodat(struct thread *td,
1308 struct freebsd11_mknodat_args *uap)
1311 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1314 #endif /* COMPAT_FREEBSD11 */
1317 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1318 int mode, dev_t dev)
1323 struct nameidata nd;
1324 int error, whiteout = 0;
1326 AUDIT_ARG_MODE(mode);
1328 switch (mode & S_IFMT) {
1331 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1332 if (error == 0 && dev == VNOVAL)
1336 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1340 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1351 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1352 pathseg, path, fd, &cap_mknodat_rights);
1353 if ((error = namei(&nd)) != 0)
1358 if (vp == nd.ni_dvp)
1366 vattr.va_mode = (mode & ALLPERMS) &
1367 ~td->td_proc->p_pd->pd_cmask;
1368 vattr.va_rdev = dev;
1371 switch (mode & S_IFMT) {
1373 vattr.va_type = VCHR;
1376 vattr.va_type = VBLK;
1382 panic("kern_mknod: invalid mode");
1385 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1388 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1393 if (error == 0 && !whiteout)
1394 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1395 &nd.ni_cnd, &vattr);
1399 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1401 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1402 &nd.ni_cnd, &vattr);
1405 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1407 vn_finished_write(mp);
1409 if (error == ERELOOKUP)
1415 * Create a named pipe.
1417 #ifndef _SYS_SYSPROTO_H_
1418 struct mkfifo_args {
1424 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1427 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1431 #ifndef _SYS_SYSPROTO_H_
1432 struct mkfifoat_args {
1439 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1442 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1447 kern_mkfifoat(struct thread *td, int fd, const char *path,
1448 enum uio_seg pathseg, int mode)
1452 struct nameidata nd;
1455 AUDIT_ARG_MODE(mode);
1459 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1460 pathseg, path, fd, &cap_mkfifoat_rights);
1461 if ((error = namei(&nd)) != 0)
1463 if (nd.ni_vp != NULL) {
1465 if (nd.ni_vp == nd.ni_dvp)
1472 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1475 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1480 vattr.va_type = VFIFO;
1481 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1483 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1488 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1492 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1493 vn_finished_write(mp);
1495 if (error == ERELOOKUP)
1501 * Make a hard file link.
1503 #ifndef _SYS_SYSPROTO_H_
1510 sys_link(struct thread *td, struct link_args *uap)
1513 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1514 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1517 #ifndef _SYS_SYSPROTO_H_
1518 struct linkat_args {
1527 sys_linkat(struct thread *td, struct linkat_args *uap)
1530 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1531 UIO_USERSPACE, uap->flag));
1534 int hardlink_check_uid = 0;
1535 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1536 &hardlink_check_uid, 0,
1537 "Unprivileged processes cannot create hard links to files owned by other "
1539 static int hardlink_check_gid = 0;
1540 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1541 &hardlink_check_gid, 0,
1542 "Unprivileged processes cannot create hard links to files owned by other "
1546 can_hardlink(struct vnode *vp, struct ucred *cred)
1551 if (!hardlink_check_uid && !hardlink_check_gid)
1554 error = VOP_GETATTR(vp, &va, cred);
1558 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1559 error = priv_check_cred(cred, PRIV_VFS_LINK);
1564 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1565 error = priv_check_cred(cred, PRIV_VFS_LINK);
1574 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1575 const char *path2, enum uio_seg segflag, int flag)
1577 struct nameidata nd;
1580 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1581 AT_EMPTY_PATH)) != 0)
1587 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1588 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1589 segflag, path1, fd1, &cap_linkat_source_rights);
1590 if ((error = namei(&nd)) != 0)
1593 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1594 error = priv_check(td, PRIV_VFS_FHOPEN);
1600 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1601 } while (error == EAGAIN || error == ERELOOKUP);
1606 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1607 enum uio_seg segflag)
1609 struct nameidata nd;
1613 if (vp->v_type == VDIR) {
1615 return (EPERM); /* POSIX */
1617 NDINIT_ATRIGHTS(&nd, CREATE,
1618 LOCKPARENT | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1619 &cap_linkat_target_rights);
1620 if ((error = namei(&nd)) == 0) {
1621 if (nd.ni_vp != NULL) {
1623 if (nd.ni_dvp == nd.ni_vp)
1630 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1632 * Cross-device link. No need to recheck
1633 * vp->v_type, since it cannot change, except
1640 } else if (vn_lock(vp, LK_EXCLUSIVE) == 0) {
1641 error = can_hardlink(vp, td->td_ucred);
1644 error = mac_vnode_check_link(td->td_ucred,
1645 nd.ni_dvp, vp, &nd.ni_cnd);
1653 error = vn_start_write(vp, &mp, V_NOWAIT);
1658 error = vn_start_write(NULL, &mp,
1659 V_XSLEEP | V_PCATCH);
1664 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1665 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1666 vn_finished_write(mp);
1682 * Make a symbolic link.
1684 #ifndef _SYS_SYSPROTO_H_
1685 struct symlink_args {
1691 sys_symlink(struct thread *td, struct symlink_args *uap)
1694 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1698 #ifndef _SYS_SYSPROTO_H_
1699 struct symlinkat_args {
1706 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1709 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1714 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1715 enum uio_seg segflg)
1719 const char *syspath;
1721 struct nameidata nd;
1724 if (segflg == UIO_SYSSPACE) {
1727 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1728 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1732 AUDIT_ARG_TEXT(syspath);
1736 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, segflg,
1737 path2, fd, &cap_symlinkat_rights);
1738 if ((error = namei(&nd)) != 0)
1742 if (nd.ni_vp == nd.ni_dvp)
1751 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1754 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1759 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1761 vattr.va_type = VLNK;
1762 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1767 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1771 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1772 vn_finished_write(mp);
1774 if (error == ERELOOKUP)
1777 if (segflg != UIO_SYSSPACE)
1778 uma_zfree(namei_zone, tmppath);
1783 * Delete a whiteout from the filesystem.
1785 #ifndef _SYS_SYSPROTO_H_
1786 struct undelete_args {
1791 sys_undelete(struct thread *td, struct undelete_args *uap)
1794 struct nameidata nd;
1800 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1801 UIO_USERSPACE, uap->path);
1806 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1808 if (nd.ni_vp == nd.ni_dvp)
1816 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1819 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1823 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1826 vn_finished_write(mp);
1827 if (error == ERELOOKUP)
1833 * Delete a name from the filesystem.
1835 #ifndef _SYS_SYSPROTO_H_
1836 struct unlink_args {
1841 sys_unlink(struct thread *td, struct unlink_args *uap)
1844 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1849 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1850 int flag, enum uio_seg pathseg, ino_t oldinum)
1853 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1856 if ((flag & AT_REMOVEDIR) != 0)
1857 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1859 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1862 #ifndef _SYS_SYSPROTO_H_
1863 struct unlinkat_args {
1870 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1873 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1877 #ifndef _SYS_SYSPROTO_H_
1878 struct funlinkat_args {
1886 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1889 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1894 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1895 enum uio_seg pathseg, int flag, ino_t oldinum)
1900 struct nameidata nd;
1905 if (fd != FD_NONE) {
1906 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1914 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1915 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1916 pathseg, path, dfd, &cap_unlinkat_rights);
1917 if ((error = namei(&nd)) != 0) {
1918 if (error == EINVAL)
1923 if (vp->v_type == VDIR && oldinum == 0) {
1924 error = EPERM; /* POSIX */
1925 } else if (oldinum != 0 &&
1926 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
1927 sb.st_ino != oldinum) {
1928 error = EIDRM; /* Identifier removed */
1929 } else if (fp != NULL && fp->f_vnode != vp) {
1930 if (VN_IS_DOOMED(fp->f_vnode))
1936 * The root of a mounted filesystem cannot be deleted.
1938 * XXX: can this only be a VDIR case?
1940 if (vp->v_vflag & VV_ROOT)
1944 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1947 if (vp == nd.ni_dvp)
1951 if ((error = vn_start_write(NULL, &mp,
1952 V_XSLEEP | V_PCATCH)) != 0) {
1958 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1963 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1964 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
1968 vn_finished_write(mp);
1972 if (vp == nd.ni_dvp)
1976 if (error == ERELOOKUP)
1985 * Reposition read/write file offset.
1987 #ifndef _SYS_SYSPROTO_H_
1996 sys_lseek(struct thread *td, struct lseek_args *uap)
1999 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2003 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2009 error = fget(td, fd, &cap_seek_rights, &fp);
2012 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2013 fo_seek(fp, offset, whence, td) : ESPIPE;
2018 #if defined(COMPAT_43)
2020 * Reposition read/write file offset.
2022 #ifndef _SYS_SYSPROTO_H_
2023 struct olseek_args {
2030 olseek(struct thread *td, struct olseek_args *uap)
2033 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2035 #endif /* COMPAT_43 */
2037 #if defined(COMPAT_FREEBSD6)
2038 /* Version with the 'pad' argument */
2040 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2043 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2048 * Check access permissions using passed credentials.
2051 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2057 /* Flags == 0 means only check for existence. */
2058 if (user_flags == 0)
2062 if (user_flags & R_OK)
2064 if (user_flags & W_OK)
2066 if (user_flags & X_OK)
2069 error = mac_vnode_check_access(cred, vp, accmode);
2073 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2074 error = VOP_ACCESS(vp, accmode, cred, td);
2079 * Check access permissions using "real" credentials.
2081 #ifndef _SYS_SYSPROTO_H_
2082 struct access_args {
2088 sys_access(struct thread *td, struct access_args *uap)
2091 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2095 #ifndef _SYS_SYSPROTO_H_
2096 struct faccessat_args {
2104 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2107 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2112 kern_accessat(struct thread *td, int fd, const char *path,
2113 enum uio_seg pathseg, int flag, int amode)
2115 struct ucred *cred, *usecred;
2117 struct nameidata nd;
2120 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2122 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2126 * Create and modify a temporary credential instead of one that
2127 * is potentially shared (if we need one).
2129 cred = td->td_ucred;
2130 if ((flag & AT_EACCESS) == 0 &&
2131 ((cred->cr_uid != cred->cr_ruid ||
2132 cred->cr_rgid != cred->cr_groups[0]))) {
2133 usecred = crdup(cred);
2134 usecred->cr_uid = cred->cr_ruid;
2135 usecred->cr_groups[0] = cred->cr_rgid;
2136 td->td_ucred = usecred;
2139 AUDIT_ARG_VALUE(amode);
2140 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2141 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2142 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2143 if ((error = namei(&nd)) != 0)
2147 error = vn_access(vp, amode, usecred, td);
2151 if (usecred != cred) {
2152 td->td_ucred = cred;
2159 * Check access permissions using "effective" credentials.
2161 #ifndef _SYS_SYSPROTO_H_
2162 struct eaccess_args {
2168 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2171 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2172 AT_EACCESS, uap->amode));
2175 #if defined(COMPAT_43)
2177 * Get file status; this version follows links.
2179 #ifndef _SYS_SYSPROTO_H_
2186 ostat(struct thread *td, struct ostat_args *uap)
2192 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2196 return (copyout(&osb, uap->ub, sizeof (osb)));
2200 * Get file status; this version does not follow links.
2202 #ifndef _SYS_SYSPROTO_H_
2203 struct olstat_args {
2209 olstat(struct thread *td, struct olstat_args *uap)
2215 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2216 UIO_USERSPACE, &sb);
2220 return (copyout(&osb, uap->ub, sizeof (osb)));
2224 * Convert from an old to a new stat structure.
2225 * XXX: many values are blindly truncated.
2228 cvtstat(struct stat *st, struct ostat *ost)
2231 bzero(ost, sizeof(*ost));
2232 ost->st_dev = st->st_dev;
2233 ost->st_ino = st->st_ino;
2234 ost->st_mode = st->st_mode;
2235 ost->st_nlink = st->st_nlink;
2236 ost->st_uid = st->st_uid;
2237 ost->st_gid = st->st_gid;
2238 ost->st_rdev = st->st_rdev;
2239 ost->st_size = MIN(st->st_size, INT32_MAX);
2240 ost->st_atim = st->st_atim;
2241 ost->st_mtim = st->st_mtim;
2242 ost->st_ctim = st->st_ctim;
2243 ost->st_blksize = st->st_blksize;
2244 ost->st_blocks = st->st_blocks;
2245 ost->st_flags = st->st_flags;
2246 ost->st_gen = st->st_gen;
2248 #endif /* COMPAT_43 */
2250 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2251 int ino64_trunc_error;
2252 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2253 &ino64_trunc_error, 0,
2254 "Error on truncation of device, file or inode number, or link count");
2257 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2260 ost->st_dev = st->st_dev;
2261 if (ost->st_dev != st->st_dev) {
2262 switch (ino64_trunc_error) {
2265 * Since dev_t is almost raw, don't clamp to the
2266 * maximum for case 2, but ignore the error.
2273 ost->st_ino = st->st_ino;
2274 if (ost->st_ino != st->st_ino) {
2275 switch (ino64_trunc_error) {
2282 ost->st_ino = UINT32_MAX;
2286 ost->st_mode = st->st_mode;
2287 ost->st_nlink = st->st_nlink;
2288 if (ost->st_nlink != st->st_nlink) {
2289 switch (ino64_trunc_error) {
2296 ost->st_nlink = UINT16_MAX;
2300 ost->st_uid = st->st_uid;
2301 ost->st_gid = st->st_gid;
2302 ost->st_rdev = st->st_rdev;
2303 if (ost->st_rdev != st->st_rdev) {
2304 switch (ino64_trunc_error) {
2311 ost->st_atim = st->st_atim;
2312 ost->st_mtim = st->st_mtim;
2313 ost->st_ctim = st->st_ctim;
2314 ost->st_size = st->st_size;
2315 ost->st_blocks = st->st_blocks;
2316 ost->st_blksize = st->st_blksize;
2317 ost->st_flags = st->st_flags;
2318 ost->st_gen = st->st_gen;
2320 ost->st_birthtim = st->st_birthtim;
2321 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2322 sizeof(*ost) - offsetof(struct freebsd11_stat,
2323 st_birthtim) - sizeof(ost->st_birthtim));
2328 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2331 struct freebsd11_stat osb;
2334 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2337 error = freebsd11_cvtstat(&sb, &osb);
2339 error = copyout(&osb, uap->ub, sizeof(osb));
2344 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2347 struct freebsd11_stat osb;
2350 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2351 UIO_USERSPACE, &sb);
2354 error = freebsd11_cvtstat(&sb, &osb);
2356 error = copyout(&osb, uap->ub, sizeof(osb));
2361 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2365 struct freebsd11_stat osb;
2368 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2371 error = kern_fhstat(td, fh, &sb);
2374 error = freebsd11_cvtstat(&sb, &osb);
2376 error = copyout(&osb, uap->sb, sizeof(osb));
2381 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2384 struct freebsd11_stat osb;
2387 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2388 UIO_USERSPACE, &sb);
2391 error = freebsd11_cvtstat(&sb, &osb);
2393 error = copyout(&osb, uap->buf, sizeof(osb));
2396 #endif /* COMPAT_FREEBSD11 */
2401 #ifndef _SYS_SYSPROTO_H_
2402 struct fstatat_args {
2410 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2415 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2416 UIO_USERSPACE, &sb);
2418 error = copyout(&sb, uap->buf, sizeof (sb));
2423 kern_statat(struct thread *td, int flag, int fd, const char *path,
2424 enum uio_seg pathseg, struct stat *sbp)
2426 struct nameidata nd;
2429 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2430 AT_EMPTY_PATH)) != 0)
2433 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2434 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2435 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2437 if ((error = namei(&nd)) != 0) {
2438 if (error == ENOTDIR &&
2439 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2440 error = kern_fstat(td, fd, sbp);
2443 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2446 #ifdef __STAT_TIME_T_EXT
2447 sbp->st_atim_ext = 0;
2448 sbp->st_mtim_ext = 0;
2449 sbp->st_ctim_ext = 0;
2450 sbp->st_btim_ext = 0;
2453 if (KTRPOINT(td, KTR_STRUCT))
2454 ktrstat_error(sbp, error);
2459 #if defined(COMPAT_FREEBSD11)
2461 * Implementation of the NetBSD [l]stat() functions.
2464 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2466 struct freebsd11_stat sb11;
2469 error = freebsd11_cvtstat(sb, &sb11);
2473 bzero(nsb, sizeof(*nsb));
2474 CP(sb11, *nsb, st_dev);
2475 CP(sb11, *nsb, st_ino);
2476 CP(sb11, *nsb, st_mode);
2477 CP(sb11, *nsb, st_nlink);
2478 CP(sb11, *nsb, st_uid);
2479 CP(sb11, *nsb, st_gid);
2480 CP(sb11, *nsb, st_rdev);
2481 CP(sb11, *nsb, st_atim);
2482 CP(sb11, *nsb, st_mtim);
2483 CP(sb11, *nsb, st_ctim);
2484 CP(sb11, *nsb, st_size);
2485 CP(sb11, *nsb, st_blocks);
2486 CP(sb11, *nsb, st_blksize);
2487 CP(sb11, *nsb, st_flags);
2488 CP(sb11, *nsb, st_gen);
2489 CP(sb11, *nsb, st_birthtim);
2493 #ifndef _SYS_SYSPROTO_H_
2494 struct freebsd11_nstat_args {
2500 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2506 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2509 error = freebsd11_cvtnstat(&sb, &nsb);
2511 error = copyout(&nsb, uap->ub, sizeof (nsb));
2516 * NetBSD lstat. Get file status; this version does not follow links.
2518 #ifndef _SYS_SYSPROTO_H_
2519 struct freebsd11_nlstat_args {
2525 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2531 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2532 UIO_USERSPACE, &sb);
2535 error = freebsd11_cvtnstat(&sb, &nsb);
2537 error = copyout(&nsb, uap->ub, sizeof (nsb));
2540 #endif /* COMPAT_FREEBSD11 */
2543 * Get configurable pathname variables.
2545 #ifndef _SYS_SYSPROTO_H_
2546 struct pathconf_args {
2552 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2557 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2560 td->td_retval[0] = value;
2564 #ifndef _SYS_SYSPROTO_H_
2565 struct lpathconf_args {
2571 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2576 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2579 td->td_retval[0] = value;
2584 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2585 int name, u_long flags, long *valuep)
2587 struct nameidata nd;
2590 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2592 if ((error = namei(&nd)) != 0)
2596 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2602 * Return target name of a symbolic link.
2604 #ifndef _SYS_SYSPROTO_H_
2605 struct readlink_args {
2612 sys_readlink(struct thread *td, struct readlink_args *uap)
2615 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2616 uap->buf, UIO_USERSPACE, uap->count));
2618 #ifndef _SYS_SYSPROTO_H_
2619 struct readlinkat_args {
2627 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2630 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2631 uap->buf, UIO_USERSPACE, uap->bufsize));
2635 kern_readlinkat(struct thread *td, int fd, const char *path,
2636 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2639 struct nameidata nd;
2642 if (count > IOSIZE_MAX)
2645 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2646 EMPTYPATH, pathseg, path, fd);
2648 if ((error = namei(&nd)) != 0)
2653 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2660 * Helper function to readlink from a vnode
2663 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2670 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2672 error = mac_vnode_check_readlink(td->td_ucred, vp);
2676 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2679 aiov.iov_base = buf;
2680 aiov.iov_len = count;
2681 auio.uio_iov = &aiov;
2682 auio.uio_iovcnt = 1;
2683 auio.uio_offset = 0;
2684 auio.uio_rw = UIO_READ;
2685 auio.uio_segflg = bufseg;
2687 auio.uio_resid = count;
2688 error = VOP_READLINK(vp, &auio, td->td_ucred);
2689 td->td_retval[0] = count - auio.uio_resid;
2694 * Common implementation code for chflags() and fchflags().
2697 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2703 /* We can't support the value matching VNOVAL. */
2704 if (flags == VNOVAL)
2705 return (EOPNOTSUPP);
2708 * Prevent non-root users from setting flags on devices. When
2709 * a device is reused, users can retain ownership of the device
2710 * if they are allowed to set flags and programs assume that
2711 * chown can't fail when done as root.
2713 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2714 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2719 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2722 vattr.va_flags = flags;
2723 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2725 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2728 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2730 vn_finished_write(mp);
2735 * Change flags of a file given a path name.
2737 #ifndef _SYS_SYSPROTO_H_
2738 struct chflags_args {
2744 sys_chflags(struct thread *td, struct chflags_args *uap)
2747 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2751 #ifndef _SYS_SYSPROTO_H_
2752 struct chflagsat_args {
2760 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2763 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2764 uap->flags, uap->atflag));
2768 * Same as chflags() but doesn't follow symlinks.
2770 #ifndef _SYS_SYSPROTO_H_
2771 struct lchflags_args {
2777 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2780 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2781 uap->flags, AT_SYMLINK_NOFOLLOW));
2785 kern_chflagsat(struct thread *td, int fd, const char *path,
2786 enum uio_seg pathseg, u_long flags, int atflag)
2788 struct nameidata nd;
2791 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2792 AT_EMPTY_PATH)) != 0)
2795 AUDIT_ARG_FFLAGS(flags);
2796 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2797 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2798 fd, &cap_fchflags_rights);
2799 if ((error = namei(&nd)) != 0)
2802 error = setfflags(td, nd.ni_vp, flags);
2808 * Change flags of a file given a file descriptor.
2810 #ifndef _SYS_SYSPROTO_H_
2811 struct fchflags_args {
2817 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2822 AUDIT_ARG_FD(uap->fd);
2823 AUDIT_ARG_FFLAGS(uap->flags);
2824 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2829 if (AUDITING_TD(td)) {
2830 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2831 AUDIT_ARG_VNODE1(fp->f_vnode);
2832 VOP_UNLOCK(fp->f_vnode);
2835 error = setfflags(td, fp->f_vnode, uap->flags);
2841 * Common implementation code for chmod(), lchmod() and fchmod().
2844 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2850 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2852 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2854 vattr.va_mode = mode & ALLPERMS;
2856 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2859 error = VOP_SETATTR(vp, &vattr, cred);
2861 vn_finished_write(mp);
2866 * Change mode of a file given path name.
2868 #ifndef _SYS_SYSPROTO_H_
2875 sys_chmod(struct thread *td, struct chmod_args *uap)
2878 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2882 #ifndef _SYS_SYSPROTO_H_
2883 struct fchmodat_args {
2891 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2894 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2895 uap->mode, uap->flag));
2899 * Change mode of a file given path name (don't follow links.)
2901 #ifndef _SYS_SYSPROTO_H_
2902 struct lchmod_args {
2908 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2911 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2912 uap->mode, AT_SYMLINK_NOFOLLOW));
2916 kern_fchmodat(struct thread *td, int fd, const char *path,
2917 enum uio_seg pathseg, mode_t mode, int flag)
2919 struct nameidata nd;
2922 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2923 AT_EMPTY_PATH)) != 0)
2926 AUDIT_ARG_MODE(mode);
2927 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2928 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2929 fd, &cap_fchmod_rights);
2930 if ((error = namei(&nd)) != 0)
2933 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2939 * Change mode of a file given a file descriptor.
2941 #ifndef _SYS_SYSPROTO_H_
2942 struct fchmod_args {
2948 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2953 AUDIT_ARG_FD(uap->fd);
2954 AUDIT_ARG_MODE(uap->mode);
2956 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2959 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2965 * Common implementation for chown(), lchown(), and fchown()
2968 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
2975 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2977 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2982 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
2986 error = VOP_SETATTR(vp, &vattr, cred);
2988 vn_finished_write(mp);
2993 * Set ownership given a path name.
2995 #ifndef _SYS_SYSPROTO_H_
3003 sys_chown(struct thread *td, struct chown_args *uap)
3006 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3010 #ifndef _SYS_SYSPROTO_H_
3011 struct fchownat_args {
3020 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3023 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3024 uap->gid, uap->flag));
3028 kern_fchownat(struct thread *td, int fd, const char *path,
3029 enum uio_seg pathseg, int uid, int gid, int flag)
3031 struct nameidata nd;
3034 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3035 AT_EMPTY_PATH)) != 0)
3038 AUDIT_ARG_OWNER(uid, gid);
3039 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3040 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3041 fd, &cap_fchown_rights);
3043 if ((error = namei(&nd)) != 0)
3046 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3052 * Set ownership given a path name, do not cross symlinks.
3054 #ifndef _SYS_SYSPROTO_H_
3055 struct lchown_args {
3062 sys_lchown(struct thread *td, struct lchown_args *uap)
3065 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3066 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3070 * Set ownership given a file descriptor.
3072 #ifndef _SYS_SYSPROTO_H_
3073 struct fchown_args {
3080 sys_fchown(struct thread *td, struct fchown_args *uap)
3085 AUDIT_ARG_FD(uap->fd);
3086 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3087 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3090 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3096 * Common implementation code for utimes(), lutimes(), and futimes().
3099 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3100 struct timespec *tsp)
3102 struct timeval tv[2];
3103 const struct timeval *tvp;
3106 if (usrtvp == NULL) {
3107 vfs_timestamp(&tsp[0]);
3110 if (tvpseg == UIO_SYSSPACE) {
3113 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3118 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3119 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3121 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3122 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3128 * Common implementation code for futimens(), utimensat().
3130 #define UTIMENS_NULL 0x1
3131 #define UTIMENS_EXIT 0x2
3133 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3134 struct timespec *tsp, int *retflags)
3136 struct timespec tsnow;
3139 vfs_timestamp(&tsnow);
3141 if (usrtsp == NULL) {
3144 *retflags |= UTIMENS_NULL;
3147 if (tspseg == UIO_SYSSPACE) {
3150 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3152 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3153 *retflags |= UTIMENS_EXIT;
3154 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3155 *retflags |= UTIMENS_NULL;
3156 if (tsp[0].tv_nsec == UTIME_OMIT)
3157 tsp[0].tv_sec = VNOVAL;
3158 else if (tsp[0].tv_nsec == UTIME_NOW)
3160 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3162 if (tsp[1].tv_nsec == UTIME_OMIT)
3163 tsp[1].tv_sec = VNOVAL;
3164 else if (tsp[1].tv_nsec == UTIME_NOW)
3166 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3173 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3177 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3178 int numtimes, int nullflag)
3185 setbirthtime = false;
3186 vattr.va_birthtime.tv_sec = VNOVAL;
3187 vattr.va_birthtime.tv_nsec = 0;
3189 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3191 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3192 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3193 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3194 setbirthtime = true;
3196 vattr.va_atime = ts[0];
3197 vattr.va_mtime = ts[1];
3199 vattr.va_birthtime = ts[1];
3201 vattr.va_birthtime = ts[2];
3203 vattr.va_vaflags |= VA_UTIMES_NULL;
3205 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3209 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3211 vn_finished_write(mp);
3216 * Set the access and modification times of a file.
3218 #ifndef _SYS_SYSPROTO_H_
3219 struct utimes_args {
3221 struct timeval *tptr;
3225 sys_utimes(struct thread *td, struct utimes_args *uap)
3228 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3229 uap->tptr, UIO_USERSPACE));
3232 #ifndef _SYS_SYSPROTO_H_
3233 struct futimesat_args {
3236 const struct timeval * times;
3240 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3243 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3244 uap->times, UIO_USERSPACE));
3248 kern_utimesat(struct thread *td, int fd, const char *path,
3249 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3251 struct nameidata nd;
3252 struct timespec ts[2];
3255 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3257 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3258 &cap_futimes_rights);
3260 if ((error = namei(&nd)) != 0)
3263 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3269 * Set the access and modification times of a file.
3271 #ifndef _SYS_SYSPROTO_H_
3272 struct lutimes_args {
3274 struct timeval *tptr;
3278 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3281 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3286 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3287 const struct timeval *tptr, enum uio_seg tptrseg)
3289 struct timespec ts[2];
3290 struct nameidata nd;
3293 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3295 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3296 if ((error = namei(&nd)) != 0)
3299 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3305 * Set the access and modification times of a file.
3307 #ifndef _SYS_SYSPROTO_H_
3308 struct futimes_args {
3310 struct timeval *tptr;
3314 sys_futimes(struct thread *td, struct futimes_args *uap)
3317 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3321 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3322 enum uio_seg tptrseg)
3324 struct timespec ts[2];
3329 error = getutimes(tptr, tptrseg, ts);
3332 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3336 if (AUDITING_TD(td)) {
3337 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3338 AUDIT_ARG_VNODE1(fp->f_vnode);
3339 VOP_UNLOCK(fp->f_vnode);
3342 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3348 sys_futimens(struct thread *td, struct futimens_args *uap)
3351 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3355 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3356 enum uio_seg tptrseg)
3358 struct timespec ts[2];
3363 error = getutimens(tptr, tptrseg, ts, &flags);
3366 if (flags & UTIMENS_EXIT)
3368 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3372 if (AUDITING_TD(td)) {
3373 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3374 AUDIT_ARG_VNODE1(fp->f_vnode);
3375 VOP_UNLOCK(fp->f_vnode);
3378 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3384 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3387 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3388 uap->times, UIO_USERSPACE, uap->flag));
3392 kern_utimensat(struct thread *td, int fd, const char *path,
3393 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3396 struct nameidata nd;
3397 struct timespec ts[2];
3400 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3401 AT_EMPTY_PATH)) != 0)
3404 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3406 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3407 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3408 pathseg, path, fd, &cap_futimes_rights);
3409 if ((error = namei(&nd)) != 0)
3412 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3414 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3415 * "Search permission is denied by a component of the path prefix."
3418 if ((flags & UTIMENS_EXIT) == 0)
3419 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3425 * Truncate a file given its path name.
3427 #ifndef _SYS_SYSPROTO_H_
3428 struct truncate_args {
3435 sys_truncate(struct thread *td, struct truncate_args *uap)
3438 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3442 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3448 struct nameidata nd;
3455 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3456 if ((error = namei(&nd)) != 0)
3460 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3461 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3462 vn_rangelock_unlock(vp, rl_cookie);
3466 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3467 if (vp->v_type == VDIR) {
3472 error = mac_vnode_check_write(td->td_ucred, NOCRED, vp);
3476 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
3480 error = vn_truncate_locked(vp, length, false, td->td_ucred);
3483 vn_finished_write(mp);
3484 vn_rangelock_unlock(vp, rl_cookie);
3486 if (error == ERELOOKUP)
3491 #if defined(COMPAT_43)
3493 * Truncate a file given its path name.
3495 #ifndef _SYS_SYSPROTO_H_
3496 struct otruncate_args {
3502 otruncate(struct thread *td, struct otruncate_args *uap)
3505 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3507 #endif /* COMPAT_43 */
3509 #if defined(COMPAT_FREEBSD6)
3510 /* Versions with the pad argument */
3512 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3515 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3519 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3522 return (kern_ftruncate(td, uap->fd, uap->length));
3527 kern_fsync(struct thread *td, int fd, bool fullsync)
3535 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3541 /* XXXKIB: compete outstanding aio writes */;
3544 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3547 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3548 AUDIT_ARG_VNODE1(vp);
3549 if (vp->v_object != NULL) {
3550 VM_OBJECT_WLOCK(vp->v_object);
3551 vm_object_page_clean(vp->v_object, 0, 0, 0);
3552 VM_OBJECT_WUNLOCK(vp->v_object);
3554 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3556 vn_finished_write(mp);
3557 if (error == ERELOOKUP)
3565 * Sync an open file.
3567 #ifndef _SYS_SYSPROTO_H_
3573 sys_fsync(struct thread *td, struct fsync_args *uap)
3576 return (kern_fsync(td, uap->fd, true));
3580 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3583 return (kern_fsync(td, uap->fd, false));
3587 * Rename files. Source and destination must either both be directories, or
3588 * both not be directories. If target is a directory, it must be empty.
3590 #ifndef _SYS_SYSPROTO_H_
3591 struct rename_args {
3597 sys_rename(struct thread *td, struct rename_args *uap)
3600 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3601 uap->to, UIO_USERSPACE));
3604 #ifndef _SYS_SYSPROTO_H_
3605 struct renameat_args {
3613 sys_renameat(struct thread *td, struct renameat_args *uap)
3616 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3622 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3623 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3627 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3628 pathseg, old, oldfd, &cap_renameat_source_rights);
3629 if ((error = namei(fromnd)) != 0)
3631 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3632 fromnd->ni_vp, &fromnd->ni_cnd);
3633 VOP_UNLOCK(fromnd->ni_dvp);
3634 if (fromnd->ni_dvp != fromnd->ni_vp)
3635 VOP_UNLOCK(fromnd->ni_vp);
3637 NDFREE_PNBUF(fromnd);
3638 vrele(fromnd->ni_dvp);
3639 vrele(fromnd->ni_vp);
3646 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3647 const char *new, enum uio_seg pathseg)
3649 struct mount *mp = NULL;
3650 struct vnode *tvp, *fvp, *tdvp;
3651 struct nameidata fromnd, tond;
3658 if (mac_vnode_check_rename_from_enabled()) {
3659 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3665 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | AUDITVNODE1,
3666 pathseg, old, oldfd, &cap_renameat_source_rights);
3667 if ((error = namei(&fromnd)) != 0)
3673 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2;
3674 if (fromnd.ni_vp->v_type == VDIR)
3675 tondflags |= WILLBEDIR;
3676 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3677 &cap_renameat_target_rights);
3678 if ((error = namei(&tond)) != 0) {
3679 /* Translate error code for rename("dir1", "dir2/."). */
3680 if (error == EISDIR && fvp->v_type == VDIR)
3682 NDFREE_PNBUF(&fromnd);
3683 vrele(fromnd.ni_dvp);
3689 error = vn_start_write(fvp, &mp, V_NOWAIT);
3691 NDFREE_PNBUF(&fromnd);
3692 NDFREE_PNBUF(&tond);
3699 vrele(fromnd.ni_dvp);
3701 error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH);
3707 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3710 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3715 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3717 * If the target already exists we require CAP_UNLINKAT
3718 * from 'newfd', when newfd was used for the lookup.
3720 error = cap_check(&tond.ni_filecaps.fc_rights,
3721 &cap_unlinkat_rights);
3732 * If the source is the same as the destination (that is, if they
3733 * are links to the same vnode), then there is nothing to do.
3739 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3740 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3744 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3745 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3746 NDFREE_PNBUF(&fromnd);
3747 NDFREE_PNBUF(&tond);
3749 NDFREE_PNBUF(&fromnd);
3750 NDFREE_PNBUF(&tond);
3757 vrele(fromnd.ni_dvp);
3760 vn_finished_write(mp);
3762 if (error == ERESTART)
3764 if (error == ERELOOKUP)
3770 * Make a directory file.
3772 #ifndef _SYS_SYSPROTO_H_
3779 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3782 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3786 #ifndef _SYS_SYSPROTO_H_
3787 struct mkdirat_args {
3794 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3797 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3801 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3806 struct nameidata nd;
3809 AUDIT_ARG_MODE(mode);
3813 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 |
3814 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3815 segflg, path, fd, &cap_mkdirat_rights);
3816 if ((error = namei(&nd)) != 0)
3818 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3821 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3826 vattr.va_type = VDIR;
3827 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3829 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3834 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3839 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3840 vn_finished_write(mp);
3841 if (error == ERELOOKUP)
3847 * Remove a directory file.
3849 #ifndef _SYS_SYSPROTO_H_
3855 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3858 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3863 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3864 enum uio_seg pathseg, int flag)
3869 struct nameidata nd;
3870 cap_rights_t rights;
3874 if (fd != FD_NONE) {
3875 error = getvnode(td, fd, cap_rights_init_one(&rights,
3884 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3885 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3886 pathseg, path, dfd, &cap_unlinkat_rights);
3887 if ((error = namei(&nd)) != 0)
3890 if (vp->v_type != VDIR) {
3895 * No rmdir "." please.
3897 if (nd.ni_dvp == vp) {
3902 * The root of a mounted filesystem cannot be deleted.
3904 if (vp->v_vflag & VV_ROOT) {
3909 if (fp != NULL && fp->f_vnode != vp) {
3910 if (VN_IS_DOOMED(fp->f_vnode))
3918 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3923 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3926 if (nd.ni_dvp == vp)
3930 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3934 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3935 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3936 vn_finished_write(mp);
3940 if (nd.ni_dvp == vp)
3944 if (error == ERELOOKUP)
3952 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3954 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3955 long *basep, void (*func)(struct freebsd11_dirent *))
3957 struct freebsd11_dirent dstdp;
3958 struct dirent *dp, *edp;
3961 ssize_t resid, ucount;
3964 /* XXX arbitrary sanity limit on `count'. */
3965 count = min(count, 64 * 1024);
3967 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3969 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
3977 for (dp = (struct dirent *)dirbuf,
3978 edp = (struct dirent *)&dirbuf[count - resid];
3979 ucount < count && dp < edp; ) {
3980 if (dp->d_reclen == 0)
3982 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
3983 if (dp->d_namlen >= sizeof(dstdp.d_name))
3985 dstdp.d_type = dp->d_type;
3986 dstdp.d_namlen = dp->d_namlen;
3987 dstdp.d_fileno = dp->d_fileno; /* truncate */
3988 if (dstdp.d_fileno != dp->d_fileno) {
3989 switch (ino64_trunc_error) {
3997 dstdp.d_fileno = UINT32_MAX;
4001 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4002 ((dp->d_namlen + 1 + 3) &~ 3);
4003 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4004 bzero(dstdp.d_name + dstdp.d_namlen,
4005 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4007 MPASS(dstdp.d_reclen <= dp->d_reclen);
4008 MPASS(ucount + dstdp.d_reclen <= count);
4011 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4014 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4015 ucount += dstdp.d_reclen;
4019 free(dirbuf, M_TEMP);
4021 td->td_retval[0] = ucount;
4028 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4030 #if (BYTE_ORDER == LITTLE_ENDIAN)
4032 * The expected low byte of dp->d_namlen is our dp->d_type.
4033 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4035 dp->d_type = dp->d_namlen;
4039 * The dp->d_type is the high byte of the expected dp->d_namlen,
4040 * so must be zero'ed.
4047 * Read a block of directory entries in a filesystem independent format.
4049 #ifndef _SYS_SYSPROTO_H_
4050 struct ogetdirentries_args {
4058 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4063 error = kern_ogetdirentries(td, uap, &loff);
4065 error = copyout(&loff, uap->basep, sizeof(long));
4070 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4076 /* XXX arbitrary sanity limit on `count'. */
4077 if (uap->count > 64 * 1024)
4080 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4081 &base, ogetdirentries_cvt);
4083 if (error == 0 && uap->basep != NULL)
4084 error = copyout(&base, uap->basep, sizeof(long));
4088 #endif /* COMPAT_43 */
4090 #if defined(COMPAT_FREEBSD11)
4091 #ifndef _SYS_SYSPROTO_H_
4092 struct freebsd11_getdirentries_args {
4100 freebsd11_getdirentries(struct thread *td,
4101 struct freebsd11_getdirentries_args *uap)
4106 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4109 if (error == 0 && uap->basep != NULL)
4110 error = copyout(&base, uap->basep, sizeof(long));
4115 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4117 struct freebsd11_getdirentries_args ap;
4121 ap.count = uap->count;
4123 return (freebsd11_getdirentries(td, &ap));
4125 #endif /* COMPAT_FREEBSD11 */
4128 * Read a block of directory entries in a filesystem independent format.
4131 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4136 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4137 NULL, UIO_USERSPACE);
4140 if (uap->basep != NULL)
4141 error = copyout(&base, uap->basep, sizeof(off_t));
4146 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4147 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4158 if (count > IOSIZE_MAX)
4160 auio.uio_resid = count;
4161 error = getvnode(td, fd, &cap_read_rights, &fp);
4164 if ((fp->f_flag & FREAD) == 0) {
4169 foffset = foffset_lock(fp, 0);
4171 if (vp->v_type != VDIR) {
4175 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4179 aiov.iov_base = buf;
4180 aiov.iov_len = count;
4181 auio.uio_iov = &aiov;
4182 auio.uio_iovcnt = 1;
4183 auio.uio_rw = UIO_READ;
4184 auio.uio_segflg = bufseg;
4186 vn_lock(vp, LK_SHARED | LK_RETRY);
4187 AUDIT_ARG_VNODE1(vp);
4188 loff = auio.uio_offset = foffset;
4190 error = mac_vnode_check_readdir(td->td_ucred, vp);
4193 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4195 foffset = auio.uio_offset;
4200 if (count == auio.uio_resid &&
4201 (vp->v_vflag & VV_ROOT) &&
4202 (vp->v_mount->mnt_flag & MNT_UNION)) {
4203 struct vnode *tvp = vp;
4205 vp = vp->v_mount->mnt_vnodecovered;
4215 *residp = auio.uio_resid;
4216 td->td_retval[0] = count - auio.uio_resid;
4218 foffset_unlock(fp, foffset, 0);
4224 * Set the mode mask for creation of filesystem nodes.
4226 #ifndef _SYS_SYSPROTO_H_
4232 sys_umask(struct thread *td, struct umask_args *uap)
4234 struct pwddesc *pdp;
4236 pdp = td->td_proc->p_pd;
4238 td->td_retval[0] = pdp->pd_cmask;
4239 pdp->pd_cmask = uap->newmask & ALLPERMS;
4240 PWDDESC_XUNLOCK(pdp);
4245 * Void all references to file by ripping underlying filesystem away from
4248 #ifndef _SYS_SYSPROTO_H_
4249 struct revoke_args {
4254 sys_revoke(struct thread *td, struct revoke_args *uap)
4258 struct nameidata nd;
4261 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4263 if ((error = namei(&nd)) != 0)
4267 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4272 error = mac_vnode_check_revoke(td->td_ucred, vp);
4276 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4279 if (td->td_ucred->cr_uid != vattr.va_uid) {
4280 error = priv_check(td, PRIV_VFS_ADMIN);
4284 if (devfs_usecount(vp) > 0)
4285 VOP_REVOKE(vp, REVOKEALL);
4292 * This variant of getvnode() allows O_PATH files. Caller should
4293 * ensure that returned file and vnode are only used for compatible
4297 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4303 error = fget_unlocked(td, fd, rightsp, &fp);
4308 * The file could be not of the vnode type, or it may be not
4309 * yet fully initialized, in which case the f_vnode pointer
4310 * may be set, but f_ops is still badfileops. E.g.,
4311 * devfs_open() transiently create such situation to
4312 * facilitate csw d_fdopen().
4314 * Dupfdopen() handling in kern_openat() installs the
4315 * half-baked file into the process descriptor table, allowing
4316 * other thread to dereference it. Guard against the race by
4319 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4330 * Convert a user file descriptor to a kernel file entry and check
4331 * that, if it is a capability, the correct rights are present.
4332 * A reference on the file entry is held upon returning.
4335 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4339 error = getvnode_path(td, fd, rightsp, fpp);
4340 if (__predict_false(error != 0))
4344 * Filter out O_PATH file descriptors, most getvnode() callers
4345 * do not call fo_ methods.
4347 if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4357 * Get an (NFS) file handle.
4359 #ifndef _SYS_SYSPROTO_H_
4360 struct lgetfh_args {
4366 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4369 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4370 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4373 #ifndef _SYS_SYSPROTO_H_
4380 sys_getfh(struct thread *td, struct getfh_args *uap)
4383 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4384 uap->fhp, UIO_USERSPACE));
4388 * syscall for the rpc.lockd to use to translate an open descriptor into
4389 * a NFS file handle.
4391 * warning: do not remove the priv_check() call or this becomes one giant
4394 #ifndef _SYS_SYSPROTO_H_
4395 struct getfhat_args {
4403 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4406 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4407 uap->fhp, UIO_USERSPACE));
4411 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4412 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4414 struct nameidata nd;
4419 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4421 error = priv_check(td, PRIV_VFS_GETFH);
4424 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4425 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4432 bzero(&fh, sizeof(fh));
4433 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4434 error = VOP_VPTOFH(vp, &fh.fh_fid);
4437 if (fhseg == UIO_USERSPACE)
4438 error = copyout(&fh, fhp, sizeof (fh));
4440 memcpy(fhp, &fh, sizeof(fh));
4445 #ifndef _SYS_SYSPROTO_H_
4446 struct fhlink_args {
4452 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4455 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4458 #ifndef _SYS_SYSPROTO_H_
4459 struct fhlinkat_args {
4466 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4469 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4473 kern_fhlinkat(struct thread *td, int fd, const char *path,
4474 enum uio_seg pathseg, fhandle_t *fhp)
4481 error = priv_check(td, PRIV_VFS_GETFH);
4484 error = copyin(fhp, &fh, sizeof(fh));
4489 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4491 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4496 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4497 } while (error == EAGAIN || error == ERELOOKUP);
4501 #ifndef _SYS_SYSPROTO_H_
4502 struct fhreadlink_args {
4509 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4516 error = priv_check(td, PRIV_VFS_GETFH);
4519 if (uap->bufsize > IOSIZE_MAX)
4521 error = copyin(uap->fhp, &fh, sizeof(fh));
4524 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4526 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4530 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4536 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4539 * warning: do not remove the priv_check() call or this becomes one giant
4542 #ifndef _SYS_SYSPROTO_H_
4543 struct fhopen_args {
4544 const struct fhandle *u_fhp;
4549 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4551 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4555 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4564 error = priv_check(td, PRIV_VFS_FHOPEN);
4568 fmode = FFLAGS(flags);
4569 /* why not allow a non-read/write open for our lockd? */
4570 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4572 error = copyin(u_fhp, &fhp, sizeof(fhp));
4575 /* find the mount point */
4576 mp = vfs_busyfs(&fhp.fh_fsid);
4579 /* now give me my vnode, it gets returned to me locked */
4580 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4585 error = falloc_noinstall(td, &fp);
4591 * An extra reference on `fp' has been held for us by
4592 * falloc_noinstall().
4598 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4600 KASSERT(fp->f_ops == &badfileops,
4601 ("VOP_OPEN in fhopen() set f_ops"));
4602 KASSERT(td->td_dupfd < 0,
4603 ("fhopen() encountered fdopen()"));
4612 finit_vnode(fp, fmode, NULL, &vnops);
4614 if ((fmode & O_TRUNC) != 0) {
4615 error = fo_truncate(fp, 0, td->td_ucred, td);
4620 error = finstall(td, fp, &indx, fmode, NULL);
4623 td->td_retval[0] = indx;
4628 * Stat an (NFS) file handle.
4630 #ifndef _SYS_SYSPROTO_H_
4631 struct fhstat_args {
4632 struct fhandle *u_fhp;
4637 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4643 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4646 error = kern_fhstat(td, fh, &sb);
4648 error = copyout(&sb, uap->sb, sizeof(sb));
4653 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4659 error = priv_check(td, PRIV_VFS_FHSTAT);
4662 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4664 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4668 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4674 * Implement fstatfs() for (NFS) file handles.
4676 #ifndef _SYS_SYSPROTO_H_
4677 struct fhstatfs_args {
4678 struct fhandle *u_fhp;
4683 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4689 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4692 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4693 error = kern_fhstatfs(td, fh, sfp);
4695 error = copyout(sfp, uap->buf, sizeof(*sfp));
4696 free(sfp, M_STATFS);
4701 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4707 error = priv_check(td, PRIV_VFS_FHSTATFS);
4710 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4712 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4718 error = prison_canseemount(td->td_ucred, mp);
4722 error = mac_mount_check_stat(td->td_ucred, mp);
4726 error = VFS_STATFS(mp, buf);
4733 * Unlike madvise(2), we do not make a best effort to remember every
4734 * possible caching hint. Instead, we remember the last setting with
4735 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4736 * region of any current setting.
4739 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4742 struct fadvise_info *fa, *new;
4748 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4750 AUDIT_ARG_VALUE(advice);
4752 case POSIX_FADV_SEQUENTIAL:
4753 case POSIX_FADV_RANDOM:
4754 case POSIX_FADV_NOREUSE:
4755 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4757 case POSIX_FADV_NORMAL:
4758 case POSIX_FADV_WILLNEED:
4759 case POSIX_FADV_DONTNEED:
4765 /* XXX: CAP_POSIX_FADVISE? */
4767 error = fget(td, fd, &cap_no_rights, &fp);
4770 AUDIT_ARG_FILE(td->td_proc, fp);
4771 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4775 if (fp->f_type != DTYPE_VNODE) {
4780 if (vp->v_type != VREG) {
4787 end = offset + len - 1;
4789 case POSIX_FADV_SEQUENTIAL:
4790 case POSIX_FADV_RANDOM:
4791 case POSIX_FADV_NOREUSE:
4793 * Try to merge any existing non-standard region with
4794 * this new region if possible, otherwise create a new
4795 * non-standard region for this request.
4797 mtx_pool_lock(mtxpool_sleep, fp);
4799 if (fa != NULL && fa->fa_advice == advice &&
4800 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4801 (end != OFF_MAX && fa->fa_start == end + 1) ||
4802 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4803 if (offset < fa->fa_start)
4804 fa->fa_start = offset;
4805 if (end > fa->fa_end)
4808 new->fa_advice = advice;
4809 new->fa_start = offset;
4814 mtx_pool_unlock(mtxpool_sleep, fp);
4816 case POSIX_FADV_NORMAL:
4818 * If a the "normal" region overlaps with an existing
4819 * non-standard region, trim or remove the
4820 * non-standard region.
4822 mtx_pool_lock(mtxpool_sleep, fp);
4825 if (offset <= fa->fa_start && end >= fa->fa_end) {
4827 fp->f_advice = NULL;
4828 } else if (offset <= fa->fa_start &&
4829 end >= fa->fa_start)
4830 fa->fa_start = end + 1;
4831 else if (offset <= fa->fa_end && end >= fa->fa_end)
4832 fa->fa_end = offset - 1;
4833 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4835 * If the "normal" region is a middle
4836 * portion of the existing
4837 * non-standard region, just remove
4838 * the whole thing rather than picking
4839 * one side or the other to
4843 fp->f_advice = NULL;
4846 mtx_pool_unlock(mtxpool_sleep, fp);
4848 case POSIX_FADV_WILLNEED:
4849 case POSIX_FADV_DONTNEED:
4850 error = VOP_ADVISE(vp, offset, end, advice);
4856 free(new, M_FADVISE);
4861 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4865 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4867 return (kern_posix_error(td, error));
4871 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4872 off_t *outoffp, size_t len, unsigned int flags)
4874 struct file *infp, *outfp;
4875 struct vnode *invp, *outvp;
4878 void *rl_rcookie, *rl_wcookie;
4879 off_t savinoff, savoutoff;
4881 infp = outfp = NULL;
4882 rl_rcookie = rl_wcookie = NULL;
4891 if (len > SSIZE_MAX)
4893 * Although the len argument is size_t, the return argument
4894 * is ssize_t (which is signed). Therefore a size that won't
4895 * fit in ssize_t can't be returned.
4899 /* Get the file structures for the file descriptors. */
4900 error = fget_read(td, infd,
4901 inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp);
4904 if (infp->f_ops == &badfileops) {
4908 if (infp->f_vnode == NULL) {
4912 error = fget_write(td, outfd,
4913 outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp);
4916 if (outfp->f_ops == &badfileops) {
4920 if (outfp->f_vnode == NULL) {
4925 /* Set the offset pointers to the correct place. */
4927 inoffp = &infp->f_offset;
4928 if (outoffp == NULL)
4929 outoffp = &outfp->f_offset;
4931 savoutoff = *outoffp;
4933 invp = infp->f_vnode;
4934 outvp = outfp->f_vnode;
4935 /* Sanity check the f_flag bits. */
4936 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4937 (infp->f_flag & FREAD) == 0) {
4942 /* If len == 0, just return 0. */
4947 * If infp and outfp refer to the same file, the byte ranges cannot
4950 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4951 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4957 /* Range lock the byte ranges for both invp and outvp. */
4959 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4961 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4963 if (rl_rcookie != NULL)
4965 vn_rangelock_unlock(outvp, rl_wcookie);
4966 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4967 vn_rangelock_unlock(invp, rl_rcookie);
4971 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
4972 flags, infp->f_cred, outfp->f_cred, td);
4974 if (rl_rcookie != NULL)
4975 vn_rangelock_unlock(invp, rl_rcookie);
4976 if (rl_wcookie != NULL)
4977 vn_rangelock_unlock(outvp, rl_wcookie);
4978 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
4980 *outoffp = savoutoff;
4986 td->td_retval[0] = retlen;
4991 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
4993 off_t inoff, outoff, *inoffp, *outoffp;
4996 inoffp = outoffp = NULL;
4997 if (uap->inoffp != NULL) {
4998 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5003 if (uap->outoffp != NULL) {
5004 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5009 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5010 outoffp, uap->len, uap->flags);
5011 if (error == 0 && uap->inoffp != NULL)
5012 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5013 if (error == 0 && uap->outoffp != NULL)
5014 error = copyout(outoffp, uap->outoffp, sizeof(off_t));