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>
86 #include <vm/vnode_pager.h>
89 #include <fs/devfs/devfs.h>
91 MALLOC_DEFINE(M_FADVISE, "fadvise", "posix_fadvise(2) information");
93 static int kern_chflagsat(struct thread *td, int fd, const char *path,
94 enum uio_seg pathseg, u_long flags, int atflag);
95 static int setfflags(struct thread *td, struct vnode *, u_long);
96 static int getutimes(const struct timeval *, enum uio_seg, struct timespec *);
97 static int getutimens(const struct timespec *, enum uio_seg,
98 struct timespec *, int *);
99 static int setutimes(struct thread *td, struct vnode *,
100 const struct timespec *, int, int);
101 static int vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
103 static int kern_fhlinkat(struct thread *td, int fd, const char *path,
104 enum uio_seg pathseg, fhandle_t *fhp);
105 static int kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg,
106 size_t count, struct thread *td);
107 static int kern_linkat_vp(struct thread *td, struct vnode *vp, int fd,
108 const char *path, enum uio_seg segflag);
111 at2cnpflags(u_int at_flags, u_int mask)
115 MPASS((at_flags & (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW)) !=
116 (AT_SYMLINK_FOLLOW | AT_SYMLINK_NOFOLLOW));
120 if ((at_flags & AT_RESOLVE_BENEATH) != 0)
122 if ((at_flags & AT_SYMLINK_FOLLOW) != 0)
124 /* NOFOLLOW is pseudo flag */
125 if ((mask & AT_SYMLINK_NOFOLLOW) != 0) {
126 res |= (at_flags & AT_SYMLINK_NOFOLLOW) != 0 ? NOFOLLOW :
129 if ((mask & AT_EMPTY_PATH) != 0 && (at_flags & AT_EMPTY_PATH) != 0)
135 kern_sync(struct thread *td)
137 struct mount *mp, *nmp;
140 mtx_lock(&mountlist_mtx);
141 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
142 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
143 nmp = TAILQ_NEXT(mp, mnt_list);
146 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
147 vn_start_write(NULL, &mp, V_NOWAIT) == 0) {
148 save = curthread_pflags_set(TDP_SYNCIO);
149 vfs_periodic(mp, MNT_NOWAIT);
150 VFS_SYNC(mp, MNT_NOWAIT);
151 curthread_pflags_restore(save);
152 vn_finished_write(mp);
154 mtx_lock(&mountlist_mtx);
155 nmp = TAILQ_NEXT(mp, mnt_list);
158 mtx_unlock(&mountlist_mtx);
163 * Sync each mounted filesystem.
165 #ifndef _SYS_SYSPROTO_H_
172 sys_sync(struct thread *td, struct sync_args *uap)
175 return (kern_sync(td));
179 * Change filesystem quotas.
181 #ifndef _SYS_SYSPROTO_H_
182 struct quotactl_args {
190 sys_quotactl(struct thread *td, struct quotactl_args *uap)
197 AUDIT_ARG_CMD(uap->cmd);
198 AUDIT_ARG_UID(uap->uid);
199 if (!prison_allow(td->td_ucred, PR_ALLOW_QUOTAS))
201 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
203 if ((error = namei(&nd)) != 0)
206 mp = nd.ni_vp->v_mount;
209 error = vfs_busy(mp, 0);
215 error = VFS_QUOTACTL(mp, uap->cmd, uap->uid, uap->arg, &mp_busy);
218 * Since quota on/off operations typically need to open quota
219 * files, the implementation may need to unbusy the mount point
220 * before calling into namei. Otherwise, unmount might be
221 * started between two vfs_busy() invocations (first is ours,
222 * second is from mount point cross-walk code in lookup()),
225 * Avoid unbusying mp if the implementation indicates it has
235 * Used by statfs conversion routines to scale the block size up if
236 * necessary so that all of the block counts are <= 'max_size'. Note
237 * that 'max_size' should be a bitmask, i.e. 2^n - 1 for some non-zero
241 statfs_scale_blocks(struct statfs *sf, long max_size)
246 KASSERT(powerof2(max_size + 1), ("%s: invalid max_size", __func__));
249 * Attempt to scale the block counts to give a more accurate
250 * overview to userland of the ratio of free space to used
251 * space. To do this, find the largest block count and compute
252 * a divisor that lets it fit into a signed integer <= max_size.
254 if (sf->f_bavail < 0)
255 count = -sf->f_bavail;
257 count = sf->f_bavail;
258 count = MAX(sf->f_blocks, MAX(sf->f_bfree, count));
259 if (count <= max_size)
262 count >>= flsl(max_size);
269 sf->f_bsize <<= shift;
270 sf->f_blocks >>= shift;
271 sf->f_bfree >>= shift;
272 sf->f_bavail >>= shift;
276 kern_do_statfs(struct thread *td, struct mount *mp, struct statfs *buf)
282 error = vfs_busy(mp, 0);
287 error = mac_mount_check_stat(td->td_ucred, mp);
291 error = VFS_STATFS(mp, buf);
294 if (priv_check_cred_vfs_generation(td->td_ucred)) {
295 buf->f_fsid.val[0] = buf->f_fsid.val[1] = 0;
296 prison_enforce_statfs(td->td_ucred, mp, buf);
304 * Get filesystem statistics.
306 #ifndef _SYS_SYSPROTO_H_
313 sys_statfs(struct thread *td, struct statfs_args *uap)
318 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
319 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
321 error = copyout(sfp, uap->buf, sizeof(struct statfs));
327 kern_statfs(struct thread *td, const char *path, enum uio_seg pathseg,
334 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
339 mp = vfs_ref_from_vp(nd.ni_vp);
341 return (kern_do_statfs(td, mp, buf));
345 * Get filesystem statistics.
347 #ifndef _SYS_SYSPROTO_H_
348 struct fstatfs_args {
354 sys_fstatfs(struct thread *td, struct fstatfs_args *uap)
359 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
360 error = kern_fstatfs(td, uap->fd, sfp);
362 error = copyout(sfp, uap->buf, sizeof(struct statfs));
368 kern_fstatfs(struct thread *td, int fd, struct statfs *buf)
376 error = getvnode_path(td, fd, &cap_fstatfs_rights, &fp);
381 if (AUDITING_TD(td)) {
382 vn_lock(vp, LK_SHARED | LK_RETRY);
383 AUDIT_ARG_VNODE1(vp);
387 mp = vfs_ref_from_vp(vp);
389 return (kern_do_statfs(td, mp, buf));
393 * Get statistics on all filesystems.
395 #ifndef _SYS_SYSPROTO_H_
396 struct getfsstat_args {
403 sys_getfsstat(struct thread *td, struct getfsstat_args *uap)
408 if (uap->bufsize < 0 || uap->bufsize > SIZE_MAX)
410 error = kern_getfsstat(td, &uap->buf, uap->bufsize, &count,
411 UIO_USERSPACE, uap->mode);
413 td->td_retval[0] = count;
418 * If (bufsize > 0 && bufseg == UIO_SYSSPACE)
419 * The caller is responsible for freeing memory which will be allocated
423 kern_getfsstat(struct thread *td, struct statfs **buf, size_t bufsize,
424 size_t *countp, enum uio_seg bufseg, int mode)
426 struct mount *mp, *nmp;
427 struct statfs *sfsp, *sp, *sptmp, *tofree;
428 size_t count, maxcount;
436 if (bufseg == UIO_SYSSPACE)
441 maxcount = bufsize / sizeof(struct statfs);
445 } else if (bufseg == UIO_USERSPACE) {
448 } else /* if (bufseg == UIO_SYSSPACE) */ {
450 mtx_lock(&mountlist_mtx);
451 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
454 mtx_unlock(&mountlist_mtx);
455 if (maxcount > count)
457 tofree = sfsp = *buf = malloc(maxcount * sizeof(struct statfs),
464 * If there is no target buffer they only want the count.
466 * This could be TAILQ_FOREACH but it is open-coded to match the original
470 mtx_lock(&mountlist_mtx);
471 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
472 if (prison_canseemount(td->td_ucred, mp) != 0) {
473 nmp = TAILQ_NEXT(mp, mnt_list);
477 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
478 nmp = TAILQ_NEXT(mp, mnt_list);
483 nmp = TAILQ_NEXT(mp, mnt_list);
485 mtx_unlock(&mountlist_mtx);
491 * They want the entire thing.
493 * Short-circuit the corner case of no room for anything, avoids
500 mtx_lock(&mountlist_mtx);
501 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
502 if (prison_canseemount(td->td_ucred, mp) != 0) {
503 nmp = TAILQ_NEXT(mp, mnt_list);
507 if (mac_mount_check_stat(td->td_ucred, mp) != 0) {
508 nmp = TAILQ_NEXT(mp, mnt_list);
512 if (mode == MNT_WAIT) {
513 if (vfs_busy(mp, MBF_MNTLSTLOCK) != 0) {
515 * If vfs_busy() failed, and MBF_NOWAIT
516 * wasn't passed, then the mp is gone.
517 * Furthermore, because of MBF_MNTLSTLOCK,
518 * the mountlist_mtx was dropped. We have
519 * no other choice than to start over.
521 mtx_unlock(&mountlist_mtx);
522 free(tofree, M_STATFS);
526 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
527 nmp = TAILQ_NEXT(mp, mnt_list);
533 * If MNT_NOWAIT is specified, do not refresh
536 if (mode != MNT_NOWAIT) {
537 error = VFS_STATFS(mp, sp);
539 mtx_lock(&mountlist_mtx);
540 nmp = TAILQ_NEXT(mp, mnt_list);
545 if (priv_check_cred_vfs_generation(td->td_ucred)) {
546 sptmp = malloc(sizeof(struct statfs), M_STATFS,
549 sptmp->f_fsid.val[0] = sptmp->f_fsid.val[1] = 0;
550 prison_enforce_statfs(td->td_ucred, mp, sptmp);
554 if (bufseg == UIO_SYSSPACE) {
555 bcopy(sp, sfsp, sizeof(*sp));
556 free(sptmp, M_STATFS);
557 } else /* if (bufseg == UIO_USERSPACE) */ {
558 error = copyout(sp, sfsp, sizeof(*sp));
559 free(sptmp, M_STATFS);
568 if (count == maxcount) {
573 mtx_lock(&mountlist_mtx);
574 nmp = TAILQ_NEXT(mp, mnt_list);
577 mtx_unlock(&mountlist_mtx);
583 #ifdef COMPAT_FREEBSD4
585 * Get old format filesystem statistics.
587 static void freebsd4_cvtstatfs(struct statfs *, struct ostatfs *);
589 #ifndef _SYS_SYSPROTO_H_
590 struct freebsd4_statfs_args {
596 freebsd4_statfs(struct thread *td, struct freebsd4_statfs_args *uap)
602 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
603 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
605 freebsd4_cvtstatfs(sfp, &osb);
606 error = copyout(&osb, uap->buf, sizeof(osb));
613 * Get filesystem statistics.
615 #ifndef _SYS_SYSPROTO_H_
616 struct freebsd4_fstatfs_args {
622 freebsd4_fstatfs(struct thread *td, struct freebsd4_fstatfs_args *uap)
628 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
629 error = kern_fstatfs(td, uap->fd, sfp);
631 freebsd4_cvtstatfs(sfp, &osb);
632 error = copyout(&osb, uap->buf, sizeof(osb));
639 * Get statistics on all filesystems.
641 #ifndef _SYS_SYSPROTO_H_
642 struct freebsd4_getfsstat_args {
649 freebsd4_getfsstat(struct thread *td, struct freebsd4_getfsstat_args *uap)
651 struct statfs *buf, *sp;
656 if (uap->bufsize < 0)
658 count = uap->bufsize / sizeof(struct ostatfs);
659 if (count > SIZE_MAX / sizeof(struct statfs))
661 size = count * sizeof(struct statfs);
662 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE,
665 td->td_retval[0] = count;
668 while (count != 0 && error == 0) {
669 freebsd4_cvtstatfs(sp, &osb);
670 error = copyout(&osb, uap->buf, sizeof(osb));
681 * Implement fstatfs() for (NFS) file handles.
683 #ifndef _SYS_SYSPROTO_H_
684 struct freebsd4_fhstatfs_args {
685 struct fhandle *u_fhp;
690 freebsd4_fhstatfs(struct thread *td, struct freebsd4_fhstatfs_args *uap)
697 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
700 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
701 error = kern_fhstatfs(td, fh, sfp);
703 freebsd4_cvtstatfs(sfp, &osb);
704 error = copyout(&osb, uap->buf, sizeof(osb));
711 * Convert a new format statfs structure to an old format statfs structure.
714 freebsd4_cvtstatfs(struct statfs *nsp, struct ostatfs *osp)
717 statfs_scale_blocks(nsp, LONG_MAX);
718 bzero(osp, sizeof(*osp));
719 osp->f_bsize = nsp->f_bsize;
720 osp->f_iosize = MIN(nsp->f_iosize, LONG_MAX);
721 osp->f_blocks = nsp->f_blocks;
722 osp->f_bfree = nsp->f_bfree;
723 osp->f_bavail = nsp->f_bavail;
724 osp->f_files = MIN(nsp->f_files, LONG_MAX);
725 osp->f_ffree = MIN(nsp->f_ffree, LONG_MAX);
726 osp->f_owner = nsp->f_owner;
727 osp->f_type = nsp->f_type;
728 osp->f_flags = nsp->f_flags;
729 osp->f_syncwrites = MIN(nsp->f_syncwrites, LONG_MAX);
730 osp->f_asyncwrites = MIN(nsp->f_asyncwrites, LONG_MAX);
731 osp->f_syncreads = MIN(nsp->f_syncreads, LONG_MAX);
732 osp->f_asyncreads = MIN(nsp->f_asyncreads, LONG_MAX);
733 strlcpy(osp->f_fstypename, nsp->f_fstypename,
734 MIN(MFSNAMELEN, OMFSNAMELEN));
735 strlcpy(osp->f_mntonname, nsp->f_mntonname,
736 MIN(MNAMELEN, OMNAMELEN));
737 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
738 MIN(MNAMELEN, OMNAMELEN));
739 osp->f_fsid = nsp->f_fsid;
741 #endif /* COMPAT_FREEBSD4 */
743 #if defined(COMPAT_FREEBSD11)
745 * Get old format filesystem statistics.
747 static void freebsd11_cvtstatfs(struct statfs *, struct freebsd11_statfs *);
750 freebsd11_statfs(struct thread *td, struct freebsd11_statfs_args *uap)
752 struct freebsd11_statfs osb;
756 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
757 error = kern_statfs(td, uap->path, UIO_USERSPACE, sfp);
759 freebsd11_cvtstatfs(sfp, &osb);
760 error = copyout(&osb, uap->buf, sizeof(osb));
767 * Get filesystem statistics.
770 freebsd11_fstatfs(struct thread *td, struct freebsd11_fstatfs_args *uap)
772 struct freebsd11_statfs osb;
776 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
777 error = kern_fstatfs(td, uap->fd, sfp);
779 freebsd11_cvtstatfs(sfp, &osb);
780 error = copyout(&osb, uap->buf, sizeof(osb));
787 * Get statistics on all filesystems.
790 freebsd11_getfsstat(struct thread *td, struct freebsd11_getfsstat_args *uap)
792 return (kern_freebsd11_getfsstat(td, uap->buf, uap->bufsize, uap->mode));
796 kern_freebsd11_getfsstat(struct thread *td, struct freebsd11_statfs * ubuf,
797 long bufsize, int mode)
799 struct freebsd11_statfs osb;
800 struct statfs *buf, *sp;
807 count = bufsize / sizeof(struct ostatfs);
808 size = count * sizeof(struct statfs);
809 error = kern_getfsstat(td, &buf, size, &count, UIO_SYSSPACE, mode);
811 td->td_retval[0] = count;
814 while (count > 0 && error == 0) {
815 freebsd11_cvtstatfs(sp, &osb);
816 error = copyout(&osb, ubuf, sizeof(osb));
827 * Implement fstatfs() for (NFS) file handles.
830 freebsd11_fhstatfs(struct thread *td, struct freebsd11_fhstatfs_args *uap)
832 struct freebsd11_statfs osb;
837 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
840 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
841 error = kern_fhstatfs(td, fh, sfp);
843 freebsd11_cvtstatfs(sfp, &osb);
844 error = copyout(&osb, uap->buf, sizeof(osb));
851 * Convert a new format statfs structure to an old format statfs structure.
854 freebsd11_cvtstatfs(struct statfs *nsp, struct freebsd11_statfs *osp)
857 bzero(osp, sizeof(*osp));
858 osp->f_version = FREEBSD11_STATFS_VERSION;
859 osp->f_type = nsp->f_type;
860 osp->f_flags = nsp->f_flags;
861 osp->f_bsize = nsp->f_bsize;
862 osp->f_iosize = nsp->f_iosize;
863 osp->f_blocks = nsp->f_blocks;
864 osp->f_bfree = nsp->f_bfree;
865 osp->f_bavail = nsp->f_bavail;
866 osp->f_files = nsp->f_files;
867 osp->f_ffree = nsp->f_ffree;
868 osp->f_syncwrites = nsp->f_syncwrites;
869 osp->f_asyncwrites = nsp->f_asyncwrites;
870 osp->f_syncreads = nsp->f_syncreads;
871 osp->f_asyncreads = nsp->f_asyncreads;
872 osp->f_namemax = nsp->f_namemax;
873 osp->f_owner = nsp->f_owner;
874 osp->f_fsid = nsp->f_fsid;
875 strlcpy(osp->f_fstypename, nsp->f_fstypename,
876 MIN(MFSNAMELEN, sizeof(osp->f_fstypename)));
877 strlcpy(osp->f_mntonname, nsp->f_mntonname,
878 MIN(MNAMELEN, sizeof(osp->f_mntonname)));
879 strlcpy(osp->f_mntfromname, nsp->f_mntfromname,
880 MIN(MNAMELEN, sizeof(osp->f_mntfromname)));
882 #endif /* COMPAT_FREEBSD11 */
885 * Change current working directory to a given file descriptor.
887 #ifndef _SYS_SYSPROTO_H_
893 sys_fchdir(struct thread *td, struct fchdir_args *uap)
895 struct vnode *vp, *tdp;
900 AUDIT_ARG_FD(uap->fd);
901 error = getvnode_path(td, uap->fd, &cap_fchdir_rights,
908 vn_lock(vp, LK_SHARED | LK_RETRY);
909 AUDIT_ARG_VNODE1(vp);
910 error = change_dir(vp, td);
911 while (!error && (mp = vp->v_mountedhere) != NULL) {
914 error = VFS_ROOT(mp, LK_SHARED, &tdp);
931 * Change current working directory (``.'').
933 #ifndef _SYS_SYSPROTO_H_
939 sys_chdir(struct thread *td, struct chdir_args *uap)
942 return (kern_chdir(td, uap->path, UIO_USERSPACE));
946 kern_chdir(struct thread *td, const char *path, enum uio_seg pathseg)
951 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
953 if ((error = namei(&nd)) != 0)
955 if ((error = change_dir(nd.ni_vp, td)) != 0) {
960 VOP_UNLOCK(nd.ni_vp);
962 pwd_chdir(td, nd.ni_vp);
966 static int unprivileged_chroot = 0;
967 SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_chroot, CTLFLAG_RW,
968 &unprivileged_chroot, 0,
969 "Unprivileged processes can use chroot(2)");
971 * Change notion of root (``/'') directory.
973 #ifndef _SYS_SYSPROTO_H_
979 sys_chroot(struct thread *td, struct chroot_args *uap)
985 error = priv_check(td, PRIV_VFS_CHROOT);
989 if (unprivileged_chroot == 0 ||
990 (p->p_flag2 & P2_NO_NEW_PRIVS) == 0) {
996 NDINIT(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1,
997 UIO_USERSPACE, uap->path);
1002 error = change_dir(nd.ni_vp, td);
1006 error = mac_vnode_check_chroot(td->td_ucred, nd.ni_vp);
1010 VOP_UNLOCK(nd.ni_vp);
1011 error = pwd_chroot(td, nd.ni_vp);
1020 * Common routine for chroot and chdir. Callers must provide a locked vnode
1024 change_dir(struct vnode *vp, struct thread *td)
1030 ASSERT_VOP_LOCKED(vp, "change_dir(): vp not locked");
1031 if (vp->v_type != VDIR)
1034 error = mac_vnode_check_chdir(td->td_ucred, vp);
1038 return (VOP_ACCESS(vp, VEXEC, td->td_ucred, td));
1041 static __inline void
1042 flags_to_rights(int flags, cap_rights_t *rightsp)
1044 if (flags & O_EXEC) {
1045 cap_rights_set_one(rightsp, CAP_FEXECVE);
1049 switch ((flags & O_ACCMODE)) {
1051 cap_rights_set_one(rightsp, CAP_READ);
1054 cap_rights_set_one(rightsp, CAP_READ);
1057 cap_rights_set_one(rightsp, CAP_WRITE);
1058 if (!(flags & (O_APPEND | O_TRUNC)))
1059 cap_rights_set_one(rightsp, CAP_SEEK);
1064 if (flags & O_CREAT)
1065 cap_rights_set_one(rightsp, CAP_CREATE);
1067 if (flags & O_TRUNC)
1068 cap_rights_set_one(rightsp, CAP_FTRUNCATE);
1070 if (flags & (O_SYNC | O_FSYNC))
1071 cap_rights_set_one(rightsp, CAP_FSYNC);
1073 if (flags & (O_EXLOCK | O_SHLOCK))
1074 cap_rights_set_one(rightsp, CAP_FLOCK);
1078 * Check permissions, allocate an open file structure, and call the device
1079 * open routine if any.
1081 #ifndef _SYS_SYSPROTO_H_
1089 sys_open(struct thread *td, struct open_args *uap)
1092 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1093 uap->flags, uap->mode));
1096 #ifndef _SYS_SYSPROTO_H_
1097 struct openat_args {
1105 sys_openat(struct thread *td, struct openat_args *uap)
1108 AUDIT_ARG_FD(uap->fd);
1109 return (kern_openat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
1114 * If fpp != NULL, opened file is not installed into the file
1115 * descriptor table, instead it is returned in *fpp. This is
1116 * incompatible with fdopen(), in which case we return EINVAL.
1119 openatfp(struct thread *td, int dirfd, const char *path,
1120 enum uio_seg pathseg, int flags, int mode, struct file **fpp)
1123 struct filedesc *fdp;
1124 struct pwddesc *pdp;
1127 struct filecaps *fcaps;
1128 struct nameidata nd;
1129 cap_rights_t rights;
1130 int cmode, error, indx;
1137 AUDIT_ARG_FFLAGS(flags);
1138 AUDIT_ARG_MODE(mode);
1139 cap_rights_init_one(&rights, CAP_LOOKUP);
1140 flags_to_rights(flags, &rights);
1143 * Only one of the O_EXEC, O_RDONLY, O_WRONLY and O_RDWR flags
1144 * may be specified. On the other hand, for O_PATH any mode
1145 * except O_EXEC is ignored.
1147 if ((flags & O_PATH) != 0) {
1148 flags &= ~(O_CREAT | O_ACCMODE);
1149 } else if ((flags & O_EXEC) != 0) {
1150 if (flags & O_ACCMODE)
1152 } else if ((flags & O_ACCMODE) == O_ACCMODE) {
1155 flags = FFLAGS(flags);
1159 * Allocate a file structure. The descriptor to reference it
1160 * is allocated and used by finstall_refed() below.
1162 error = falloc_noinstall(td, &fp);
1165 /* Set the flags early so the finit in devfs can pick them up. */
1166 fp->f_flag = flags & FMASK;
1167 cmode = ((mode & ~pdp->pd_cmask) & ALLPERMS) & ~S_ISTXT;
1168 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1 | WANTIOCTLCAPS,
1169 pathseg, path, dirfd, &rights);
1170 td->td_dupfd = -1; /* XXX check for fdopen */
1171 error = vn_open_cred(&nd, &flags, cmode, VN_OPEN_WANTIOCTLCAPS,
1175 * If the vn_open replaced the method vector, something
1176 * wonderous happened deep below and we just pass it up
1177 * pretending we know what we do.
1179 if (error == ENXIO && fp->f_ops != &badfileops) {
1180 MPASS((flags & O_PATH) == 0);
1185 * Handle special fdopen() case. bleh.
1187 * Don't do this for relative (capability) lookups; we don't
1188 * understand exactly what would happen, and we don't think
1189 * that it ever should.
1191 if ((nd.ni_resflags & NIRES_STRICTREL) == 0 &&
1192 (error == ENODEV || error == ENXIO) &&
1193 td->td_dupfd >= 0) {
1195 error = dupfdopen(td, fdp, td->td_dupfd, flags, error,
1208 * Store the vnode, for any f_type. Typically, the vnode use
1209 * count is decremented by direct call to vn_closefile() for
1210 * files that switched type in the cdevsw fdopen() method.
1215 * If the file wasn't claimed by devfs bind it to the normal
1216 * vnode operations here.
1218 if (fp->f_ops == &badfileops) {
1219 KASSERT(vp->v_type != VFIFO || (flags & O_PATH) != 0,
1220 ("Unexpected fifo fp %p vp %p", fp, vp));
1221 if ((flags & O_PATH) != 0) {
1222 finit(fp, (flags & FMASK) | (fp->f_flag & FKQALLOWED),
1223 DTYPE_VNODE, NULL, &path_fileops);
1225 finit_vnode(fp, flags, NULL, &vnops);
1230 if (flags & O_TRUNC) {
1231 error = fo_truncate(fp, 0, td->td_ucred, td);
1238 NDFREE_IOCTLCAPS(&nd);
1244 * If we haven't already installed the FD (for dupfdopen), do so now.
1248 if ((nd.ni_resflags & NIRES_STRICTREL) != 0)
1249 fcaps = &nd.ni_filecaps;
1253 error = finstall_refed(td, fp, &indx, flags, fcaps);
1254 /* On success finstall_refed() consumes fcaps. */
1259 NDFREE_IOCTLCAPS(&nd);
1260 falloc_abort(td, fp);
1263 td->td_retval[0] = indx;
1266 KASSERT(indx == -1, ("indx=%d, should be -1", indx));
1267 NDFREE_IOCTLCAPS(&nd);
1268 falloc_abort(td, fp);
1273 kern_openat(struct thread *td, int dirfd, const char *path,
1274 enum uio_seg pathseg, int flags, int mode)
1276 return (openatfp(td, dirfd, path, pathseg, flags, mode, NULL));
1280 kern_openatfp(struct thread *td, int dirfd, const char *path,
1281 enum uio_seg pathseg, int flags, int mode, struct file **fpp)
1283 int error, old_dupfd;
1285 old_dupfd = td->td_dupfd;
1287 error = openatfp(td, dirfd, path, pathseg, flags, mode, fpp);
1288 td->td_dupfd = old_dupfd;
1296 #ifndef _SYS_SYSPROTO_H_
1297 struct ocreat_args {
1303 ocreat(struct thread *td, struct ocreat_args *uap)
1306 return (kern_openat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1307 O_WRONLY | O_CREAT | O_TRUNC, uap->mode));
1309 #endif /* COMPAT_43 */
1312 * Create a special file.
1314 #ifndef _SYS_SYSPROTO_H_
1315 struct mknodat_args {
1323 sys_mknodat(struct thread *td, struct mknodat_args *uap)
1326 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1330 #if defined(COMPAT_FREEBSD11)
1332 freebsd11_mknod(struct thread *td,
1333 struct freebsd11_mknod_args *uap)
1336 return (kern_mknodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1337 uap->mode, uap->dev));
1341 freebsd11_mknodat(struct thread *td,
1342 struct freebsd11_mknodat_args *uap)
1345 return (kern_mknodat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode,
1348 #endif /* COMPAT_FREEBSD11 */
1351 kern_mknodat(struct thread *td, int fd, const char *path, enum uio_seg pathseg,
1352 int mode, dev_t dev)
1357 struct nameidata nd;
1358 int error, whiteout = 0;
1360 AUDIT_ARG_MODE(mode);
1362 switch (mode & S_IFMT) {
1365 error = priv_check(td, PRIV_VFS_MKNOD_DEV);
1366 if (error == 0 && dev == VNOVAL)
1370 error = priv_check(td, PRIV_VFS_MKNOD_WHT);
1374 return (kern_mkfifoat(td, fd, path, pathseg, mode));
1385 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1386 pathseg, path, fd, &cap_mknodat_rights);
1387 if ((error = namei(&nd)) != 0)
1392 if (vp == nd.ni_dvp)
1400 vattr.va_mode = (mode & ALLPERMS) &
1401 ~td->td_proc->p_pd->pd_cmask;
1402 vattr.va_rdev = dev;
1405 switch (mode & S_IFMT) {
1407 vattr.va_type = VCHR;
1410 vattr.va_type = VBLK;
1416 panic("kern_mknod: invalid mode");
1419 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1422 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1427 if (error == 0 && !whiteout)
1428 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp,
1429 &nd.ni_cnd, &vattr);
1433 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, CREATE);
1435 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp,
1436 &nd.ni_cnd, &vattr);
1439 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 && !whiteout ? &nd.ni_vp : NULL,
1441 vn_finished_write(mp);
1443 if (error == ERELOOKUP)
1449 * Create a named pipe.
1451 #ifndef _SYS_SYSPROTO_H_
1452 struct mkfifo_args {
1458 sys_mkfifo(struct thread *td, struct mkfifo_args *uap)
1461 return (kern_mkfifoat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
1465 #ifndef _SYS_SYSPROTO_H_
1466 struct mkfifoat_args {
1473 sys_mkfifoat(struct thread *td, struct mkfifoat_args *uap)
1476 return (kern_mkfifoat(td, uap->fd, uap->path, UIO_USERSPACE,
1481 kern_mkfifoat(struct thread *td, int fd, const char *path,
1482 enum uio_seg pathseg, int mode)
1486 struct nameidata nd;
1489 AUDIT_ARG_MODE(mode);
1493 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE,
1494 pathseg, path, fd, &cap_mkfifoat_rights);
1495 if ((error = namei(&nd)) != 0)
1497 if (nd.ni_vp != NULL) {
1499 if (nd.ni_vp == nd.ni_dvp)
1506 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1509 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1514 vattr.va_type = VFIFO;
1515 vattr.va_mode = (mode & ALLPERMS) & ~td->td_proc->p_pd->pd_cmask;
1517 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1522 error = VOP_MKNOD(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
1526 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1527 vn_finished_write(mp);
1529 if (error == ERELOOKUP)
1535 * Make a hard file link.
1537 #ifndef _SYS_SYSPROTO_H_
1544 sys_link(struct thread *td, struct link_args *uap)
1547 return (kern_linkat(td, AT_FDCWD, AT_FDCWD, uap->path, uap->link,
1548 UIO_USERSPACE, AT_SYMLINK_FOLLOW));
1551 #ifndef _SYS_SYSPROTO_H_
1552 struct linkat_args {
1561 sys_linkat(struct thread *td, struct linkat_args *uap)
1564 return (kern_linkat(td, uap->fd1, uap->fd2, uap->path1, uap->path2,
1565 UIO_USERSPACE, uap->flag));
1568 int hardlink_check_uid = 0;
1569 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_uid, CTLFLAG_RW,
1570 &hardlink_check_uid, 0,
1571 "Unprivileged processes cannot create hard links to files owned by other "
1573 static int hardlink_check_gid = 0;
1574 SYSCTL_INT(_security_bsd, OID_AUTO, hardlink_check_gid, CTLFLAG_RW,
1575 &hardlink_check_gid, 0,
1576 "Unprivileged processes cannot create hard links to files owned by other "
1580 can_hardlink(struct vnode *vp, struct ucred *cred)
1585 if (!hardlink_check_uid && !hardlink_check_gid)
1588 error = VOP_GETATTR(vp, &va, cred);
1592 if (hardlink_check_uid && cred->cr_uid != va.va_uid) {
1593 error = priv_check_cred(cred, PRIV_VFS_LINK);
1598 if (hardlink_check_gid && !groupmember(va.va_gid, cred)) {
1599 error = priv_check_cred(cred, PRIV_VFS_LINK);
1608 kern_linkat(struct thread *td, int fd1, int fd2, const char *path1,
1609 const char *path2, enum uio_seg segflag, int flag)
1611 struct nameidata nd;
1614 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH |
1615 AT_EMPTY_PATH)) != 0)
1621 NDINIT_ATRIGHTS(&nd, LOOKUP, AUDITVNODE1 | at2cnpflags(flag,
1622 AT_SYMLINK_FOLLOW | AT_RESOLVE_BENEATH | AT_EMPTY_PATH),
1623 segflag, path1, fd1, &cap_linkat_source_rights);
1624 if ((error = namei(&nd)) != 0)
1627 if ((nd.ni_resflags & NIRES_EMPTYPATH) != 0) {
1628 error = priv_check(td, PRIV_VFS_FHOPEN);
1634 error = kern_linkat_vp(td, nd.ni_vp, fd2, path2, segflag);
1635 } while (error == EAGAIN || error == ERELOOKUP);
1640 kern_linkat_vp(struct thread *td, struct vnode *vp, int fd, const char *path,
1641 enum uio_seg segflag)
1643 struct nameidata nd;
1647 if (vp->v_type == VDIR) {
1649 return (EPERM); /* POSIX */
1651 NDINIT_ATRIGHTS(&nd, CREATE,
1652 LOCKPARENT | AUDITVNODE2 | NOCACHE, segflag, path, fd,
1653 &cap_linkat_target_rights);
1654 if ((error = namei(&nd)) == 0) {
1655 if (nd.ni_vp != NULL) {
1657 if (nd.ni_dvp == nd.ni_vp)
1664 } else if (nd.ni_dvp->v_mount != vp->v_mount) {
1666 * Cross-device link. No need to recheck
1667 * vp->v_type, since it cannot change, except
1674 } else if (vn_lock(vp, LK_EXCLUSIVE) == 0) {
1675 error = can_hardlink(vp, td->td_ucred);
1678 error = mac_vnode_check_link(td->td_ucred,
1679 nd.ni_dvp, vp, &nd.ni_cnd);
1687 error = vn_start_write(vp, &mp, V_NOWAIT);
1692 error = vn_start_write(NULL, &mp,
1693 V_XSLEEP | V_PCATCH);
1698 error = VOP_LINK(nd.ni_dvp, vp, &nd.ni_cnd);
1699 VOP_VPUT_PAIR(nd.ni_dvp, &vp, true);
1700 vn_finished_write(mp);
1716 * Make a symbolic link.
1718 #ifndef _SYS_SYSPROTO_H_
1719 struct symlink_args {
1725 sys_symlink(struct thread *td, struct symlink_args *uap)
1728 return (kern_symlinkat(td, uap->path, AT_FDCWD, uap->link,
1732 #ifndef _SYS_SYSPROTO_H_
1733 struct symlinkat_args {
1740 sys_symlinkat(struct thread *td, struct symlinkat_args *uap)
1743 return (kern_symlinkat(td, uap->path1, uap->fd, uap->path2,
1748 kern_symlinkat(struct thread *td, const char *path1, int fd, const char *path2,
1749 enum uio_seg segflg)
1753 const char *syspath;
1755 struct nameidata nd;
1758 if (segflg == UIO_SYSSPACE) {
1761 tmppath = uma_zalloc(namei_zone, M_WAITOK);
1762 if ((error = copyinstr(path1, tmppath, MAXPATHLEN, NULL)) != 0)
1766 AUDIT_ARG_TEXT(syspath);
1770 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 | NOCACHE, segflg,
1771 path2, fd, &cap_symlinkat_rights);
1772 if ((error = namei(&nd)) != 0)
1776 if (nd.ni_vp == nd.ni_dvp)
1785 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1788 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1793 vattr.va_mode = ACCESSPERMS &~ td->td_proc->p_pd->pd_cmask;
1795 vattr.va_type = VLNK;
1796 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
1801 error = VOP_SYMLINK(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr, syspath);
1805 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
1806 vn_finished_write(mp);
1808 if (error == ERELOOKUP)
1811 if (segflg != UIO_SYSSPACE)
1812 uma_zfree(namei_zone, tmppath);
1817 * Delete a whiteout from the filesystem.
1819 #ifndef _SYS_SYSPROTO_H_
1820 struct undelete_args {
1825 sys_undelete(struct thread *td, struct undelete_args *uap)
1828 struct nameidata nd;
1834 NDINIT(&nd, DELETE, LOCKPARENT | DOWHITEOUT | AUDITVNODE1,
1835 UIO_USERSPACE, uap->path);
1840 if (nd.ni_vp != NULLVP || !(nd.ni_cnd.cn_flags & ISWHITEOUT)) {
1842 if (nd.ni_vp == nd.ni_dvp)
1850 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1853 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
1857 error = VOP_WHITEOUT(nd.ni_dvp, &nd.ni_cnd, DELETE);
1860 vn_finished_write(mp);
1861 if (error == ERELOOKUP)
1867 * Delete a name from the filesystem.
1869 #ifndef _SYS_SYSPROTO_H_
1870 struct unlink_args {
1875 sys_unlink(struct thread *td, struct unlink_args *uap)
1878 return (kern_funlinkat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
1883 kern_funlinkat_ex(struct thread *td, int dfd, const char *path, int fd,
1884 int flag, enum uio_seg pathseg, ino_t oldinum)
1887 if ((flag & ~(AT_REMOVEDIR | AT_RESOLVE_BENEATH)) != 0)
1890 if ((flag & AT_REMOVEDIR) != 0)
1891 return (kern_frmdirat(td, dfd, path, fd, UIO_USERSPACE, 0));
1893 return (kern_funlinkat(td, dfd, path, fd, UIO_USERSPACE, 0, 0));
1896 #ifndef _SYS_SYSPROTO_H_
1897 struct unlinkat_args {
1904 sys_unlinkat(struct thread *td, struct unlinkat_args *uap)
1907 return (kern_funlinkat_ex(td, uap->fd, uap->path, FD_NONE, uap->flag,
1911 #ifndef _SYS_SYSPROTO_H_
1912 struct funlinkat_args {
1920 sys_funlinkat(struct thread *td, struct funlinkat_args *uap)
1923 return (kern_funlinkat_ex(td, uap->dfd, uap->path, uap->fd, uap->flag,
1928 kern_funlinkat(struct thread *td, int dfd, const char *path, int fd,
1929 enum uio_seg pathseg, int flag, ino_t oldinum)
1934 struct nameidata nd;
1939 if (fd != FD_NONE) {
1940 error = getvnode_path(td, fd, &cap_no_rights, &fp);
1948 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
1949 at2cnpflags(flag, AT_RESOLVE_BENEATH),
1950 pathseg, path, dfd, &cap_unlinkat_rights);
1951 if ((error = namei(&nd)) != 0) {
1952 if (error == EINVAL)
1957 if (vp->v_type == VDIR && oldinum == 0) {
1958 error = EPERM; /* POSIX */
1959 } else if (oldinum != 0 &&
1960 ((error = VOP_STAT(vp, &sb, td->td_ucred, NOCRED)) == 0) &&
1961 sb.st_ino != oldinum) {
1962 error = EIDRM; /* Identifier removed */
1963 } else if (fp != NULL && fp->f_vnode != vp) {
1964 if (VN_IS_DOOMED(fp->f_vnode))
1970 * The root of a mounted filesystem cannot be deleted.
1972 * XXX: can this only be a VDIR case?
1974 if (vp->v_vflag & VV_ROOT)
1978 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
1981 if (vp == nd.ni_dvp)
1985 if ((error = vn_start_write(NULL, &mp,
1986 V_XSLEEP | V_PCATCH)) != 0) {
1992 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
1997 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
1998 error = VOP_REMOVE(nd.ni_dvp, vp, &nd.ni_cnd);
2002 vn_finished_write(mp);
2006 if (vp == nd.ni_dvp)
2010 if (error == ERELOOKUP)
2019 * Reposition read/write file offset.
2021 #ifndef _SYS_SYSPROTO_H_
2030 sys_lseek(struct thread *td, struct lseek_args *uap)
2033 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2037 kern_lseek(struct thread *td, int fd, off_t offset, int whence)
2043 error = fget(td, fd, &cap_seek_rights, &fp);
2046 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
2047 fo_seek(fp, offset, whence, td) : ESPIPE;
2052 #if defined(COMPAT_43)
2054 * Reposition read/write file offset.
2056 #ifndef _SYS_SYSPROTO_H_
2057 struct olseek_args {
2064 olseek(struct thread *td, struct olseek_args *uap)
2067 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2069 #endif /* COMPAT_43 */
2071 #if defined(COMPAT_FREEBSD6)
2072 /* Version with the 'pad' argument */
2074 freebsd6_lseek(struct thread *td, struct freebsd6_lseek_args *uap)
2077 return (kern_lseek(td, uap->fd, uap->offset, uap->whence));
2082 * Check access permissions using passed credentials.
2085 vn_access(struct vnode *vp, int user_flags, struct ucred *cred,
2091 /* Flags == 0 means only check for existence. */
2092 if (user_flags == 0)
2096 if (user_flags & R_OK)
2098 if (user_flags & W_OK)
2100 if (user_flags & X_OK)
2103 error = mac_vnode_check_access(cred, vp, accmode);
2107 if ((accmode & VWRITE) == 0 || (error = vn_writechk(vp)) == 0)
2108 error = VOP_ACCESS(vp, accmode, cred, td);
2113 * Check access permissions using "real" credentials.
2115 #ifndef _SYS_SYSPROTO_H_
2116 struct access_args {
2122 sys_access(struct thread *td, struct access_args *uap)
2125 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2129 #ifndef _SYS_SYSPROTO_H_
2130 struct faccessat_args {
2138 sys_faccessat(struct thread *td, struct faccessat_args *uap)
2141 return (kern_accessat(td, uap->fd, uap->path, UIO_USERSPACE, uap->flag,
2146 kern_accessat(struct thread *td, int fd, const char *path,
2147 enum uio_seg pathseg, int flag, int amode)
2149 struct ucred *cred, *usecred;
2151 struct nameidata nd;
2154 if ((flag & ~(AT_EACCESS | AT_RESOLVE_BENEATH | AT_EMPTY_PATH)) != 0)
2156 if (amode != F_OK && (amode & ~(R_OK | W_OK | X_OK)) != 0)
2160 * Create and modify a temporary credential instead of one that
2161 * is potentially shared (if we need one).
2163 cred = td->td_ucred;
2164 if ((flag & AT_EACCESS) == 0 &&
2165 ((cred->cr_uid != cred->cr_ruid ||
2166 cred->cr_rgid != cred->cr_groups[0]))) {
2167 usecred = crdup(cred);
2168 usecred->cr_uid = cred->cr_ruid;
2169 usecred->cr_groups[0] = cred->cr_rgid;
2170 td->td_ucred = usecred;
2173 AUDIT_ARG_VALUE(amode);
2174 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | LOCKSHARED | LOCKLEAF |
2175 AUDITVNODE1 | at2cnpflags(flag, AT_RESOLVE_BENEATH |
2176 AT_EMPTY_PATH), pathseg, path, fd, &cap_fstat_rights);
2177 if ((error = namei(&nd)) != 0)
2181 error = vn_access(vp, amode, usecred, td);
2185 if (usecred != cred) {
2186 td->td_ucred = cred;
2193 * Check access permissions using "effective" credentials.
2195 #ifndef _SYS_SYSPROTO_H_
2196 struct eaccess_args {
2202 sys_eaccess(struct thread *td, struct eaccess_args *uap)
2205 return (kern_accessat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2206 AT_EACCESS, uap->amode));
2209 #if defined(COMPAT_43)
2211 * Get file status; this version follows links.
2213 #ifndef _SYS_SYSPROTO_H_
2220 ostat(struct thread *td, struct ostat_args *uap)
2226 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2230 return (copyout(&osb, uap->ub, sizeof (osb)));
2234 * Get file status; this version does not follow links.
2236 #ifndef _SYS_SYSPROTO_H_
2237 struct olstat_args {
2243 olstat(struct thread *td, struct olstat_args *uap)
2249 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2250 UIO_USERSPACE, &sb);
2254 return (copyout(&osb, uap->ub, sizeof (osb)));
2258 * Convert from an old to a new stat structure.
2259 * XXX: many values are blindly truncated.
2262 cvtstat(struct stat *st, struct ostat *ost)
2265 bzero(ost, sizeof(*ost));
2266 ost->st_dev = st->st_dev;
2267 ost->st_ino = st->st_ino;
2268 ost->st_mode = st->st_mode;
2269 ost->st_nlink = st->st_nlink;
2270 ost->st_uid = st->st_uid;
2271 ost->st_gid = st->st_gid;
2272 ost->st_rdev = st->st_rdev;
2273 ost->st_size = MIN(st->st_size, INT32_MAX);
2274 ost->st_atim = st->st_atim;
2275 ost->st_mtim = st->st_mtim;
2276 ost->st_ctim = st->st_ctim;
2277 ost->st_blksize = st->st_blksize;
2278 ost->st_blocks = st->st_blocks;
2279 ost->st_flags = st->st_flags;
2280 ost->st_gen = st->st_gen;
2282 #endif /* COMPAT_43 */
2284 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
2285 int ino64_trunc_error;
2286 SYSCTL_INT(_vfs, OID_AUTO, ino64_trunc_error, CTLFLAG_RW,
2287 &ino64_trunc_error, 0,
2288 "Error on truncation of device, file or inode number, or link count");
2291 freebsd11_cvtstat(struct stat *st, struct freebsd11_stat *ost)
2294 ost->st_dev = st->st_dev;
2295 if (ost->st_dev != st->st_dev) {
2296 switch (ino64_trunc_error) {
2299 * Since dev_t is almost raw, don't clamp to the
2300 * maximum for case 2, but ignore the error.
2307 ost->st_ino = st->st_ino;
2308 if (ost->st_ino != st->st_ino) {
2309 switch (ino64_trunc_error) {
2316 ost->st_ino = UINT32_MAX;
2320 ost->st_mode = st->st_mode;
2321 ost->st_nlink = st->st_nlink;
2322 if (ost->st_nlink != st->st_nlink) {
2323 switch (ino64_trunc_error) {
2330 ost->st_nlink = UINT16_MAX;
2334 ost->st_uid = st->st_uid;
2335 ost->st_gid = st->st_gid;
2336 ost->st_rdev = st->st_rdev;
2337 if (ost->st_rdev != st->st_rdev) {
2338 switch (ino64_trunc_error) {
2345 ost->st_atim = st->st_atim;
2346 ost->st_mtim = st->st_mtim;
2347 ost->st_ctim = st->st_ctim;
2348 ost->st_size = st->st_size;
2349 ost->st_blocks = st->st_blocks;
2350 ost->st_blksize = st->st_blksize;
2351 ost->st_flags = st->st_flags;
2352 ost->st_gen = st->st_gen;
2354 ost->st_birthtim = st->st_birthtim;
2355 bzero((char *)&ost->st_birthtim + sizeof(ost->st_birthtim),
2356 sizeof(*ost) - offsetof(struct freebsd11_stat,
2357 st_birthtim) - sizeof(ost->st_birthtim));
2362 freebsd11_stat(struct thread *td, struct freebsd11_stat_args* uap)
2365 struct freebsd11_stat osb;
2368 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2371 error = freebsd11_cvtstat(&sb, &osb);
2373 error = copyout(&osb, uap->ub, sizeof(osb));
2378 freebsd11_lstat(struct thread *td, struct freebsd11_lstat_args* uap)
2381 struct freebsd11_stat osb;
2384 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2385 UIO_USERSPACE, &sb);
2388 error = freebsd11_cvtstat(&sb, &osb);
2390 error = copyout(&osb, uap->ub, sizeof(osb));
2395 freebsd11_fhstat(struct thread *td, struct freebsd11_fhstat_args* uap)
2399 struct freebsd11_stat osb;
2402 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
2405 error = kern_fhstat(td, fh, &sb);
2408 error = freebsd11_cvtstat(&sb, &osb);
2410 error = copyout(&osb, uap->sb, sizeof(osb));
2415 freebsd11_fstatat(struct thread *td, struct freebsd11_fstatat_args* uap)
2418 struct freebsd11_stat osb;
2421 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2422 UIO_USERSPACE, &sb);
2425 error = freebsd11_cvtstat(&sb, &osb);
2427 error = copyout(&osb, uap->buf, sizeof(osb));
2430 #endif /* COMPAT_FREEBSD11 */
2435 #ifndef _SYS_SYSPROTO_H_
2436 struct fstatat_args {
2444 sys_fstatat(struct thread *td, struct fstatat_args *uap)
2449 error = kern_statat(td, uap->flag, uap->fd, uap->path,
2450 UIO_USERSPACE, &sb);
2452 error = copyout(&sb, uap->buf, sizeof (sb));
2457 kern_statat(struct thread *td, int flag, int fd, const char *path,
2458 enum uio_seg pathseg, struct stat *sbp)
2460 struct nameidata nd;
2463 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2464 AT_EMPTY_PATH)) != 0)
2467 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_RESOLVE_BENEATH |
2468 AT_SYMLINK_NOFOLLOW | AT_EMPTY_PATH) | LOCKSHARED | LOCKLEAF |
2469 AUDITVNODE1, pathseg, path, fd, &cap_fstat_rights);
2471 if ((error = namei(&nd)) != 0) {
2472 if (error == ENOTDIR &&
2473 (nd.ni_resflags & NIRES_EMPTYPATH) != 0)
2474 error = kern_fstat(td, fd, sbp);
2477 error = VOP_STAT(nd.ni_vp, sbp, td->td_ucred, NOCRED);
2480 #ifdef __STAT_TIME_T_EXT
2481 sbp->st_atim_ext = 0;
2482 sbp->st_mtim_ext = 0;
2483 sbp->st_ctim_ext = 0;
2484 sbp->st_btim_ext = 0;
2487 if (KTRPOINT(td, KTR_STRUCT))
2488 ktrstat_error(sbp, error);
2493 #if defined(COMPAT_FREEBSD11)
2495 * Implementation of the NetBSD [l]stat() functions.
2498 freebsd11_cvtnstat(struct stat *sb, struct nstat *nsb)
2500 struct freebsd11_stat sb11;
2503 error = freebsd11_cvtstat(sb, &sb11);
2507 bzero(nsb, sizeof(*nsb));
2508 CP(sb11, *nsb, st_dev);
2509 CP(sb11, *nsb, st_ino);
2510 CP(sb11, *nsb, st_mode);
2511 CP(sb11, *nsb, st_nlink);
2512 CP(sb11, *nsb, st_uid);
2513 CP(sb11, *nsb, st_gid);
2514 CP(sb11, *nsb, st_rdev);
2515 CP(sb11, *nsb, st_atim);
2516 CP(sb11, *nsb, st_mtim);
2517 CP(sb11, *nsb, st_ctim);
2518 CP(sb11, *nsb, st_size);
2519 CP(sb11, *nsb, st_blocks);
2520 CP(sb11, *nsb, st_blksize);
2521 CP(sb11, *nsb, st_flags);
2522 CP(sb11, *nsb, st_gen);
2523 CP(sb11, *nsb, st_birthtim);
2527 #ifndef _SYS_SYSPROTO_H_
2528 struct freebsd11_nstat_args {
2534 freebsd11_nstat(struct thread *td, struct freebsd11_nstat_args *uap)
2540 error = kern_statat(td, 0, AT_FDCWD, uap->path, UIO_USERSPACE, &sb);
2543 error = freebsd11_cvtnstat(&sb, &nsb);
2545 error = copyout(&nsb, uap->ub, sizeof (nsb));
2550 * NetBSD lstat. Get file status; this version does not follow links.
2552 #ifndef _SYS_SYSPROTO_H_
2553 struct freebsd11_nlstat_args {
2559 freebsd11_nlstat(struct thread *td, struct freebsd11_nlstat_args *uap)
2565 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->path,
2566 UIO_USERSPACE, &sb);
2569 error = freebsd11_cvtnstat(&sb, &nsb);
2571 error = copyout(&nsb, uap->ub, sizeof (nsb));
2574 #endif /* COMPAT_FREEBSD11 */
2577 * Get configurable pathname variables.
2579 #ifndef _SYS_SYSPROTO_H_
2580 struct pathconf_args {
2586 sys_pathconf(struct thread *td, struct pathconf_args *uap)
2591 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name, FOLLOW,
2594 td->td_retval[0] = value;
2598 #ifndef _SYS_SYSPROTO_H_
2599 struct lpathconf_args {
2605 sys_lpathconf(struct thread *td, struct lpathconf_args *uap)
2610 error = kern_pathconf(td, uap->path, UIO_USERSPACE, uap->name,
2613 td->td_retval[0] = value;
2618 kern_pathconf(struct thread *td, const char *path, enum uio_seg pathseg,
2619 int name, u_long flags, long *valuep)
2621 struct nameidata nd;
2624 NDINIT(&nd, LOOKUP, LOCKSHARED | LOCKLEAF | AUDITVNODE1 | flags,
2626 if ((error = namei(&nd)) != 0)
2630 error = VOP_PATHCONF(nd.ni_vp, name, valuep);
2636 * Return target name of a symbolic link.
2638 #ifndef _SYS_SYSPROTO_H_
2639 struct readlink_args {
2646 sys_readlink(struct thread *td, struct readlink_args *uap)
2649 return (kern_readlinkat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2650 uap->buf, UIO_USERSPACE, uap->count));
2652 #ifndef _SYS_SYSPROTO_H_
2653 struct readlinkat_args {
2661 sys_readlinkat(struct thread *td, struct readlinkat_args *uap)
2664 return (kern_readlinkat(td, uap->fd, uap->path, UIO_USERSPACE,
2665 uap->buf, UIO_USERSPACE, uap->bufsize));
2669 kern_readlinkat(struct thread *td, int fd, const char *path,
2670 enum uio_seg pathseg, char *buf, enum uio_seg bufseg, size_t count)
2673 struct nameidata nd;
2676 if (count > IOSIZE_MAX)
2679 NDINIT_AT(&nd, LOOKUP, NOFOLLOW | LOCKSHARED | LOCKLEAF | AUDITVNODE1 |
2680 EMPTYPATH, pathseg, path, fd);
2682 if ((error = namei(&nd)) != 0)
2687 error = kern_readlink_vp(vp, buf, bufseg, count, td);
2694 * Helper function to readlink from a vnode
2697 kern_readlink_vp(struct vnode *vp, char *buf, enum uio_seg bufseg, size_t count,
2704 ASSERT_VOP_LOCKED(vp, "kern_readlink_vp(): vp not locked");
2706 error = mac_vnode_check_readlink(td->td_ucred, vp);
2710 if (vp->v_type != VLNK && (vp->v_vflag & VV_READLINK) == 0)
2713 aiov.iov_base = buf;
2714 aiov.iov_len = count;
2715 auio.uio_iov = &aiov;
2716 auio.uio_iovcnt = 1;
2717 auio.uio_offset = 0;
2718 auio.uio_rw = UIO_READ;
2719 auio.uio_segflg = bufseg;
2721 auio.uio_resid = count;
2722 error = VOP_READLINK(vp, &auio, td->td_ucred);
2723 td->td_retval[0] = count - auio.uio_resid;
2728 * Common implementation code for chflags() and fchflags().
2731 setfflags(struct thread *td, struct vnode *vp, u_long flags)
2737 /* We can't support the value matching VNOVAL. */
2738 if (flags == VNOVAL)
2739 return (EOPNOTSUPP);
2742 * Prevent non-root users from setting flags on devices. When
2743 * a device is reused, users can retain ownership of the device
2744 * if they are allowed to set flags and programs assume that
2745 * chown can't fail when done as root.
2747 if (vp->v_type == VCHR || vp->v_type == VBLK) {
2748 error = priv_check(td, PRIV_VFS_CHFLAGS_DEV);
2753 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2756 vattr.va_flags = flags;
2757 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2759 error = mac_vnode_check_setflags(td->td_ucred, vp, vattr.va_flags);
2762 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
2764 vn_finished_write(mp);
2769 * Change flags of a file given a path name.
2771 #ifndef _SYS_SYSPROTO_H_
2772 struct chflags_args {
2778 sys_chflags(struct thread *td, struct chflags_args *uap)
2781 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2785 #ifndef _SYS_SYSPROTO_H_
2786 struct chflagsat_args {
2794 sys_chflagsat(struct thread *td, struct chflagsat_args *uap)
2797 return (kern_chflagsat(td, uap->fd, uap->path, UIO_USERSPACE,
2798 uap->flags, uap->atflag));
2802 * Same as chflags() but doesn't follow symlinks.
2804 #ifndef _SYS_SYSPROTO_H_
2805 struct lchflags_args {
2811 sys_lchflags(struct thread *td, struct lchflags_args *uap)
2814 return (kern_chflagsat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2815 uap->flags, AT_SYMLINK_NOFOLLOW));
2819 kern_chflagsat(struct thread *td, int fd, const char *path,
2820 enum uio_seg pathseg, u_long flags, int atflag)
2822 struct nameidata nd;
2825 if ((atflag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2826 AT_EMPTY_PATH)) != 0)
2829 AUDIT_ARG_FFLAGS(flags);
2830 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(atflag, AT_SYMLINK_NOFOLLOW |
2831 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2832 fd, &cap_fchflags_rights);
2833 if ((error = namei(&nd)) != 0)
2836 error = setfflags(td, nd.ni_vp, flags);
2842 * Change flags of a file given a file descriptor.
2844 #ifndef _SYS_SYSPROTO_H_
2845 struct fchflags_args {
2851 sys_fchflags(struct thread *td, struct fchflags_args *uap)
2856 AUDIT_ARG_FD(uap->fd);
2857 AUDIT_ARG_FFLAGS(uap->flags);
2858 error = getvnode(td, uap->fd, &cap_fchflags_rights,
2863 if (AUDITING_TD(td)) {
2864 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
2865 AUDIT_ARG_VNODE1(fp->f_vnode);
2866 VOP_UNLOCK(fp->f_vnode);
2869 error = setfflags(td, fp->f_vnode, uap->flags);
2875 * Common implementation code for chmod(), lchmod() and fchmod().
2878 setfmode(struct thread *td, struct ucred *cred, struct vnode *vp, int mode)
2884 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
2886 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2888 vattr.va_mode = mode & ALLPERMS;
2890 error = mac_vnode_check_setmode(cred, vp, vattr.va_mode);
2893 error = VOP_SETATTR(vp, &vattr, cred);
2895 vn_finished_write(mp);
2900 * Change mode of a file given path name.
2902 #ifndef _SYS_SYSPROTO_H_
2909 sys_chmod(struct thread *td, struct chmod_args *uap)
2912 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2916 #ifndef _SYS_SYSPROTO_H_
2917 struct fchmodat_args {
2925 sys_fchmodat(struct thread *td, struct fchmodat_args *uap)
2928 return (kern_fchmodat(td, uap->fd, uap->path, UIO_USERSPACE,
2929 uap->mode, uap->flag));
2933 * Change mode of a file given path name (don't follow links.)
2935 #ifndef _SYS_SYSPROTO_H_
2936 struct lchmod_args {
2942 sys_lchmod(struct thread *td, struct lchmod_args *uap)
2945 return (kern_fchmodat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
2946 uap->mode, AT_SYMLINK_NOFOLLOW));
2950 kern_fchmodat(struct thread *td, int fd, const char *path,
2951 enum uio_seg pathseg, mode_t mode, int flag)
2953 struct nameidata nd;
2956 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
2957 AT_EMPTY_PATH)) != 0)
2960 AUDIT_ARG_MODE(mode);
2961 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
2962 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
2963 fd, &cap_fchmod_rights);
2964 if ((error = namei(&nd)) != 0)
2967 error = setfmode(td, td->td_ucred, nd.ni_vp, mode);
2973 * Change mode of a file given a file descriptor.
2975 #ifndef _SYS_SYSPROTO_H_
2976 struct fchmod_args {
2982 sys_fchmod(struct thread *td, struct fchmod_args *uap)
2987 AUDIT_ARG_FD(uap->fd);
2988 AUDIT_ARG_MODE(uap->mode);
2990 error = fget(td, uap->fd, &cap_fchmod_rights, &fp);
2993 error = fo_chmod(fp, uap->mode, td->td_ucred, td);
2999 * Common implementation for chown(), lchown(), and fchown()
3002 setfown(struct thread *td, struct ucred *cred, struct vnode *vp, uid_t uid,
3009 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3011 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3016 error = mac_vnode_check_setowner(cred, vp, vattr.va_uid,
3020 error = VOP_SETATTR(vp, &vattr, cred);
3022 vn_finished_write(mp);
3027 * Set ownership given a path name.
3029 #ifndef _SYS_SYSPROTO_H_
3037 sys_chown(struct thread *td, struct chown_args *uap)
3040 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE, uap->uid,
3044 #ifndef _SYS_SYSPROTO_H_
3045 struct fchownat_args {
3054 sys_fchownat(struct thread *td, struct fchownat_args *uap)
3057 return (kern_fchownat(td, uap->fd, uap->path, UIO_USERSPACE, uap->uid,
3058 uap->gid, uap->flag));
3062 kern_fchownat(struct thread *td, int fd, const char *path,
3063 enum uio_seg pathseg, int uid, int gid, int flag)
3065 struct nameidata nd;
3068 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3069 AT_EMPTY_PATH)) != 0)
3072 AUDIT_ARG_OWNER(uid, gid);
3073 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3074 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1, pathseg, path,
3075 fd, &cap_fchown_rights);
3077 if ((error = namei(&nd)) != 0)
3080 error = setfown(td, td->td_ucred, nd.ni_vp, uid, gid);
3086 * Set ownership given a path name, do not cross symlinks.
3088 #ifndef _SYS_SYSPROTO_H_
3089 struct lchown_args {
3096 sys_lchown(struct thread *td, struct lchown_args *uap)
3099 return (kern_fchownat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3100 uap->uid, uap->gid, AT_SYMLINK_NOFOLLOW));
3104 * Set ownership given a file descriptor.
3106 #ifndef _SYS_SYSPROTO_H_
3107 struct fchown_args {
3114 sys_fchown(struct thread *td, struct fchown_args *uap)
3119 AUDIT_ARG_FD(uap->fd);
3120 AUDIT_ARG_OWNER(uap->uid, uap->gid);
3121 error = fget(td, uap->fd, &cap_fchown_rights, &fp);
3124 error = fo_chown(fp, uap->uid, uap->gid, td->td_ucred, td);
3130 * Common implementation code for utimes(), lutimes(), and futimes().
3133 getutimes(const struct timeval *usrtvp, enum uio_seg tvpseg,
3134 struct timespec *tsp)
3136 struct timeval tv[2];
3137 const struct timeval *tvp;
3140 if (usrtvp == NULL) {
3141 vfs_timestamp(&tsp[0]);
3144 if (tvpseg == UIO_SYSSPACE) {
3147 if ((error = copyin(usrtvp, tv, sizeof(tv))) != 0)
3152 if (tvp[0].tv_usec < 0 || tvp[0].tv_usec >= 1000000 ||
3153 tvp[1].tv_usec < 0 || tvp[1].tv_usec >= 1000000)
3155 TIMEVAL_TO_TIMESPEC(&tvp[0], &tsp[0]);
3156 TIMEVAL_TO_TIMESPEC(&tvp[1], &tsp[1]);
3162 * Common implementation code for futimens(), utimensat().
3164 #define UTIMENS_NULL 0x1
3165 #define UTIMENS_EXIT 0x2
3167 getutimens(const struct timespec *usrtsp, enum uio_seg tspseg,
3168 struct timespec *tsp, int *retflags)
3170 struct timespec tsnow;
3173 vfs_timestamp(&tsnow);
3175 if (usrtsp == NULL) {
3178 *retflags |= UTIMENS_NULL;
3181 if (tspseg == UIO_SYSSPACE) {
3184 } else if ((error = copyin(usrtsp, tsp, sizeof(*tsp) * 2)) != 0)
3186 if (tsp[0].tv_nsec == UTIME_OMIT && tsp[1].tv_nsec == UTIME_OMIT)
3187 *retflags |= UTIMENS_EXIT;
3188 if (tsp[0].tv_nsec == UTIME_NOW && tsp[1].tv_nsec == UTIME_NOW)
3189 *retflags |= UTIMENS_NULL;
3190 if (tsp[0].tv_nsec == UTIME_OMIT)
3191 tsp[0].tv_sec = VNOVAL;
3192 else if (tsp[0].tv_nsec == UTIME_NOW)
3194 else if (tsp[0].tv_nsec < 0 || tsp[0].tv_nsec >= 1000000000L)
3196 if (tsp[1].tv_nsec == UTIME_OMIT)
3197 tsp[1].tv_sec = VNOVAL;
3198 else if (tsp[1].tv_nsec == UTIME_NOW)
3200 else if (tsp[1].tv_nsec < 0 || tsp[1].tv_nsec >= 1000000000L)
3207 * Common implementation code for utimes(), lutimes(), futimes(), futimens(),
3211 setutimes(struct thread *td, struct vnode *vp, const struct timespec *ts,
3212 int numtimes, int nullflag)
3219 setbirthtime = false;
3220 vattr.va_birthtime.tv_sec = VNOVAL;
3221 vattr.va_birthtime.tv_nsec = 0;
3223 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0)
3225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3226 if (numtimes < 3 && VOP_GETATTR(vp, &vattr, td->td_ucred) == 0 &&
3227 timespeccmp(&ts[1], &vattr.va_birthtime, < ))
3228 setbirthtime = true;
3230 vattr.va_atime = ts[0];
3231 vattr.va_mtime = ts[1];
3233 vattr.va_birthtime = ts[1];
3235 vattr.va_birthtime = ts[2];
3237 vattr.va_vaflags |= VA_UTIMES_NULL;
3239 error = mac_vnode_check_setutimes(td->td_ucred, vp, vattr.va_atime,
3243 error = VOP_SETATTR(vp, &vattr, td->td_ucred);
3245 vn_finished_write(mp);
3250 * Set the access and modification times of a file.
3252 #ifndef _SYS_SYSPROTO_H_
3253 struct utimes_args {
3255 struct timeval *tptr;
3259 sys_utimes(struct thread *td, struct utimes_args *uap)
3262 return (kern_utimesat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3263 uap->tptr, UIO_USERSPACE));
3266 #ifndef _SYS_SYSPROTO_H_
3267 struct futimesat_args {
3270 const struct timeval * times;
3274 sys_futimesat(struct thread *td, struct futimesat_args *uap)
3277 return (kern_utimesat(td, uap->fd, uap->path, UIO_USERSPACE,
3278 uap->times, UIO_USERSPACE));
3282 kern_utimesat(struct thread *td, int fd, const char *path,
3283 enum uio_seg pathseg, const struct timeval *tptr, enum uio_seg tptrseg)
3285 struct nameidata nd;
3286 struct timespec ts[2];
3289 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3291 NDINIT_ATRIGHTS(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path, fd,
3292 &cap_futimes_rights);
3294 if ((error = namei(&nd)) != 0)
3297 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3303 * Set the access and modification times of a file.
3305 #ifndef _SYS_SYSPROTO_H_
3306 struct lutimes_args {
3308 struct timeval *tptr;
3312 sys_lutimes(struct thread *td, struct lutimes_args *uap)
3315 return (kern_lutimes(td, uap->path, UIO_USERSPACE, uap->tptr,
3320 kern_lutimes(struct thread *td, const char *path, enum uio_seg pathseg,
3321 const struct timeval *tptr, enum uio_seg tptrseg)
3323 struct timespec ts[2];
3324 struct nameidata nd;
3327 if ((error = getutimes(tptr, tptrseg, ts)) != 0)
3329 NDINIT(&nd, LOOKUP, NOFOLLOW | AUDITVNODE1, pathseg, path);
3330 if ((error = namei(&nd)) != 0)
3333 error = setutimes(td, nd.ni_vp, ts, 2, tptr == NULL);
3339 * Set the access and modification times of a file.
3341 #ifndef _SYS_SYSPROTO_H_
3342 struct futimes_args {
3344 struct timeval *tptr;
3348 sys_futimes(struct thread *td, struct futimes_args *uap)
3351 return (kern_futimes(td, uap->fd, uap->tptr, UIO_USERSPACE));
3355 kern_futimes(struct thread *td, int fd, const struct timeval *tptr,
3356 enum uio_seg tptrseg)
3358 struct timespec ts[2];
3363 error = getutimes(tptr, tptrseg, ts);
3366 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3370 if (AUDITING_TD(td)) {
3371 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3372 AUDIT_ARG_VNODE1(fp->f_vnode);
3373 VOP_UNLOCK(fp->f_vnode);
3376 error = setutimes(td, fp->f_vnode, ts, 2, tptr == NULL);
3382 sys_futimens(struct thread *td, struct futimens_args *uap)
3385 return (kern_futimens(td, uap->fd, uap->times, UIO_USERSPACE));
3389 kern_futimens(struct thread *td, int fd, const struct timespec *tptr,
3390 enum uio_seg tptrseg)
3392 struct timespec ts[2];
3397 error = getutimens(tptr, tptrseg, ts, &flags);
3400 if (flags & UTIMENS_EXIT)
3402 error = getvnode(td, fd, &cap_futimes_rights, &fp);
3406 if (AUDITING_TD(td)) {
3407 vn_lock(fp->f_vnode, LK_SHARED | LK_RETRY);
3408 AUDIT_ARG_VNODE1(fp->f_vnode);
3409 VOP_UNLOCK(fp->f_vnode);
3412 error = setutimes(td, fp->f_vnode, ts, 2, flags & UTIMENS_NULL);
3418 sys_utimensat(struct thread *td, struct utimensat_args *uap)
3421 return (kern_utimensat(td, uap->fd, uap->path, UIO_USERSPACE,
3422 uap->times, UIO_USERSPACE, uap->flag));
3426 kern_utimensat(struct thread *td, int fd, const char *path,
3427 enum uio_seg pathseg, const struct timespec *tptr, enum uio_seg tptrseg,
3430 struct nameidata nd;
3431 struct timespec ts[2];
3434 if ((flag & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH |
3435 AT_EMPTY_PATH)) != 0)
3438 if ((error = getutimens(tptr, tptrseg, ts, &flags)) != 0)
3440 NDINIT_ATRIGHTS(&nd, LOOKUP, at2cnpflags(flag, AT_SYMLINK_NOFOLLOW |
3441 AT_RESOLVE_BENEATH | AT_EMPTY_PATH) | AUDITVNODE1,
3442 pathseg, path, fd, &cap_futimes_rights);
3443 if ((error = namei(&nd)) != 0)
3446 * We are allowed to call namei() regardless of 2xUTIME_OMIT.
3448 * "If both tv_nsec fields are UTIME_OMIT... EACCESS may be detected."
3449 * "Search permission is denied by a component of the path prefix."
3452 if ((flags & UTIMENS_EXIT) == 0)
3453 error = setutimes(td, nd.ni_vp, ts, 2, flags & UTIMENS_NULL);
3459 * Truncate a file given its path name.
3461 #ifndef _SYS_SYSPROTO_H_
3462 struct truncate_args {
3469 sys_truncate(struct thread *td, struct truncate_args *uap)
3472 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3476 kern_truncate(struct thread *td, const char *path, enum uio_seg pathseg,
3482 struct nameidata nd;
3489 NDINIT(&nd, LOOKUP, FOLLOW | AUDITVNODE1, pathseg, path);
3490 if ((error = namei(&nd)) != 0)
3494 rl_cookie = vn_rangelock_wlock(vp, 0, OFF_MAX);
3495 if ((error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH)) != 0) {
3496 vn_rangelock_unlock(vp, rl_cookie);
3500 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3501 if (vp->v_type == VDIR) {
3506 error = mac_vnode_check_write(td->td_ucred, NOCRED, vp);
3510 error = VOP_ACCESS(vp, VWRITE, td->td_ucred, td);
3514 error = vn_truncate_locked(vp, length, false, td->td_ucred);
3517 vn_finished_write(mp);
3518 vn_rangelock_unlock(vp, rl_cookie);
3520 if (error == ERELOOKUP)
3525 #if defined(COMPAT_43)
3527 * Truncate a file given its path name.
3529 #ifndef _SYS_SYSPROTO_H_
3530 struct otruncate_args {
3536 otruncate(struct thread *td, struct otruncate_args *uap)
3539 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3541 #endif /* COMPAT_43 */
3543 #if defined(COMPAT_FREEBSD6)
3544 /* Versions with the pad argument */
3546 freebsd6_truncate(struct thread *td, struct freebsd6_truncate_args *uap)
3549 return (kern_truncate(td, uap->path, UIO_USERSPACE, uap->length));
3553 freebsd6_ftruncate(struct thread *td, struct freebsd6_ftruncate_args *uap)
3556 return (kern_ftruncate(td, uap->fd, uap->length));
3561 kern_fsync(struct thread *td, int fd, bool fullsync)
3569 error = getvnode(td, fd, &cap_fsync_rights, &fp);
3575 /* XXXKIB: compete outstanding aio writes */;
3578 error = vn_start_write(vp, &mp, V_WAIT | V_PCATCH);
3581 vn_lock(vp, vn_lktype_write(mp, vp) | LK_RETRY);
3582 AUDIT_ARG_VNODE1(vp);
3583 vnode_pager_clean_async(vp);
3584 error = fullsync ? VOP_FSYNC(vp, MNT_WAIT, td) : VOP_FDATASYNC(vp, td);
3586 vn_finished_write(mp);
3587 if (error == ERELOOKUP)
3595 * Sync an open file.
3597 #ifndef _SYS_SYSPROTO_H_
3603 sys_fsync(struct thread *td, struct fsync_args *uap)
3606 return (kern_fsync(td, uap->fd, true));
3610 sys_fdatasync(struct thread *td, struct fdatasync_args *uap)
3613 return (kern_fsync(td, uap->fd, false));
3617 * Rename files. Source and destination must either both be directories, or
3618 * both not be directories. If target is a directory, it must be empty.
3620 #ifndef _SYS_SYSPROTO_H_
3621 struct rename_args {
3627 sys_rename(struct thread *td, struct rename_args *uap)
3630 return (kern_renameat(td, AT_FDCWD, uap->from, AT_FDCWD,
3631 uap->to, UIO_USERSPACE));
3634 #ifndef _SYS_SYSPROTO_H_
3635 struct renameat_args {
3643 sys_renameat(struct thread *td, struct renameat_args *uap)
3646 return (kern_renameat(td, uap->oldfd, uap->old, uap->newfd, uap->new,
3652 kern_renameat_mac(struct thread *td, int oldfd, const char *old, int newfd,
3653 const char *new, enum uio_seg pathseg, struct nameidata *fromnd)
3657 NDINIT_ATRIGHTS(fromnd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1,
3658 pathseg, old, oldfd, &cap_renameat_source_rights);
3659 if ((error = namei(fromnd)) != 0)
3661 error = mac_vnode_check_rename_from(td->td_ucred, fromnd->ni_dvp,
3662 fromnd->ni_vp, &fromnd->ni_cnd);
3663 VOP_UNLOCK(fromnd->ni_dvp);
3664 if (fromnd->ni_dvp != fromnd->ni_vp)
3665 VOP_UNLOCK(fromnd->ni_vp);
3667 NDFREE_PNBUF(fromnd);
3668 vrele(fromnd->ni_dvp);
3669 vrele(fromnd->ni_vp);
3676 kern_renameat(struct thread *td, int oldfd, const char *old, int newfd,
3677 const char *new, enum uio_seg pathseg)
3679 struct mount *mp = NULL;
3680 struct vnode *tvp, *fvp, *tdvp;
3681 struct nameidata fromnd, tond;
3688 if (mac_vnode_check_rename_from_enabled()) {
3689 error = kern_renameat_mac(td, oldfd, old, newfd, new, pathseg,
3695 NDINIT_ATRIGHTS(&fromnd, DELETE, WANTPARENT | AUDITVNODE1,
3696 pathseg, old, oldfd, &cap_renameat_source_rights);
3697 if ((error = namei(&fromnd)) != 0)
3703 tondflags = LOCKPARENT | LOCKLEAF | NOCACHE | AUDITVNODE2;
3704 if (fromnd.ni_vp->v_type == VDIR)
3705 tondflags |= WILLBEDIR;
3706 NDINIT_ATRIGHTS(&tond, RENAME, tondflags, pathseg, new, newfd,
3707 &cap_renameat_target_rights);
3708 if ((error = namei(&tond)) != 0) {
3709 /* Translate error code for rename("dir1", "dir2/."). */
3710 if (error == EISDIR && fvp->v_type == VDIR)
3712 NDFREE_PNBUF(&fromnd);
3713 vrele(fromnd.ni_dvp);
3719 error = vn_start_write(fvp, &mp, V_NOWAIT);
3721 NDFREE_PNBUF(&fromnd);
3722 NDFREE_PNBUF(&tond);
3729 vrele(fromnd.ni_dvp);
3731 error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH);
3737 if (fvp->v_type == VDIR && tvp->v_type != VDIR) {
3740 } else if (fvp->v_type != VDIR && tvp->v_type == VDIR) {
3745 if (newfd != AT_FDCWD && (tond.ni_resflags & NIRES_ABS) == 0) {
3747 * If the target already exists we require CAP_UNLINKAT
3748 * from 'newfd', when newfd was used for the lookup.
3750 error = cap_check(&tond.ni_filecaps.fc_rights,
3751 &cap_unlinkat_rights);
3762 * If the source is the same as the destination (that is, if they
3763 * are links to the same vnode), then there is nothing to do.
3769 error = mac_vnode_check_rename_to(td->td_ucred, tdvp,
3770 tond.ni_vp, fromnd.ni_dvp == tdvp, &tond.ni_cnd);
3774 error = VOP_RENAME(fromnd.ni_dvp, fromnd.ni_vp, &fromnd.ni_cnd,
3775 tond.ni_dvp, tond.ni_vp, &tond.ni_cnd);
3776 NDFREE_PNBUF(&fromnd);
3777 NDFREE_PNBUF(&tond);
3779 NDFREE_PNBUF(&fromnd);
3780 NDFREE_PNBUF(&tond);
3787 vrele(fromnd.ni_dvp);
3790 vn_finished_write(mp);
3792 if (error == ERESTART)
3794 if (error == ERELOOKUP)
3800 * Make a directory file.
3802 #ifndef _SYS_SYSPROTO_H_
3809 sys_mkdir(struct thread *td, struct mkdir_args *uap)
3812 return (kern_mkdirat(td, AT_FDCWD, uap->path, UIO_USERSPACE,
3816 #ifndef _SYS_SYSPROTO_H_
3817 struct mkdirat_args {
3824 sys_mkdirat(struct thread *td, struct mkdirat_args *uap)
3827 return (kern_mkdirat(td, uap->fd, uap->path, UIO_USERSPACE, uap->mode));
3831 kern_mkdirat(struct thread *td, int fd, const char *path, enum uio_seg segflg,
3836 struct nameidata nd;
3839 AUDIT_ARG_MODE(mode);
3843 NDINIT_ATRIGHTS(&nd, CREATE, LOCKPARENT | AUDITVNODE1 |
3844 NC_NOMAKEENTRY | NC_KEEPPOSENTRY | FAILIFEXISTS | WILLBEDIR,
3845 segflg, path, fd, &cap_mkdirat_rights);
3846 if ((error = namei(&nd)) != 0)
3848 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3851 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3856 vattr.va_type = VDIR;
3857 vattr.va_mode = (mode & ACCESSPERMS) &~ td->td_proc->p_pd->pd_cmask;
3859 error = mac_vnode_check_create(td->td_ucred, nd.ni_dvp, &nd.ni_cnd,
3864 error = VOP_MKDIR(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vattr);
3869 VOP_VPUT_PAIR(nd.ni_dvp, error == 0 ? &nd.ni_vp : NULL, true);
3870 vn_finished_write(mp);
3871 if (error == ERELOOKUP)
3877 * Remove a directory file.
3879 #ifndef _SYS_SYSPROTO_H_
3885 sys_rmdir(struct thread *td, struct rmdir_args *uap)
3888 return (kern_frmdirat(td, AT_FDCWD, uap->path, FD_NONE, UIO_USERSPACE,
3893 kern_frmdirat(struct thread *td, int dfd, const char *path, int fd,
3894 enum uio_seg pathseg, int flag)
3899 struct nameidata nd;
3900 cap_rights_t rights;
3904 if (fd != FD_NONE) {
3905 error = getvnode(td, fd, cap_rights_init_one(&rights,
3914 NDINIT_ATRIGHTS(&nd, DELETE, LOCKPARENT | LOCKLEAF | AUDITVNODE1 |
3915 at2cnpflags(flag, AT_RESOLVE_BENEATH),
3916 pathseg, path, dfd, &cap_unlinkat_rights);
3917 if ((error = namei(&nd)) != 0)
3920 if (vp->v_type != VDIR) {
3925 * No rmdir "." please.
3927 if (nd.ni_dvp == vp) {
3932 * The root of a mounted filesystem cannot be deleted.
3934 if (vp->v_vflag & VV_ROOT) {
3939 if (fp != NULL && fp->f_vnode != vp) {
3940 if (VN_IS_DOOMED(fp->f_vnode))
3948 error = mac_vnode_check_unlink(td->td_ucred, nd.ni_dvp, vp,
3953 if (vn_start_write(nd.ni_dvp, &mp, V_NOWAIT) != 0) {
3956 if (nd.ni_dvp == vp)
3960 if ((error = vn_start_write(NULL, &mp, V_XSLEEP | V_PCATCH)) != 0)
3964 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_UNLINK);
3965 error = VOP_RMDIR(nd.ni_dvp, nd.ni_vp, &nd.ni_cnd);
3966 vn_finished_write(mp);
3970 if (nd.ni_dvp == vp)
3974 if (error == ERELOOKUP)
3982 #if defined(COMPAT_43) || defined(COMPAT_FREEBSD11)
3984 freebsd11_kern_getdirentries(struct thread *td, int fd, char *ubuf, u_int count,
3985 long *basep, void (*func)(struct freebsd11_dirent *))
3987 struct freebsd11_dirent dstdp;
3988 struct dirent *dp, *edp;
3991 ssize_t resid, ucount;
3994 /* XXX arbitrary sanity limit on `count'. */
3995 count = min(count, 64 * 1024);
3997 dirbuf = malloc(count, M_TEMP, M_WAITOK);
3999 error = kern_getdirentries(td, fd, dirbuf, count, &base, &resid,
4007 for (dp = (struct dirent *)dirbuf,
4008 edp = (struct dirent *)&dirbuf[count - resid];
4009 ucount < count && dp < edp; ) {
4010 if (dp->d_reclen == 0)
4012 MPASS(dp->d_reclen >= _GENERIC_DIRLEN(0));
4013 if (dp->d_namlen >= sizeof(dstdp.d_name))
4015 dstdp.d_type = dp->d_type;
4016 dstdp.d_namlen = dp->d_namlen;
4017 dstdp.d_fileno = dp->d_fileno; /* truncate */
4018 if (dstdp.d_fileno != dp->d_fileno) {
4019 switch (ino64_trunc_error) {
4027 dstdp.d_fileno = UINT32_MAX;
4031 dstdp.d_reclen = sizeof(dstdp) - sizeof(dstdp.d_name) +
4032 ((dp->d_namlen + 1 + 3) &~ 3);
4033 bcopy(dp->d_name, dstdp.d_name, dstdp.d_namlen);
4034 bzero(dstdp.d_name + dstdp.d_namlen,
4035 dstdp.d_reclen - offsetof(struct freebsd11_dirent, d_name) -
4037 MPASS(dstdp.d_reclen <= dp->d_reclen);
4038 MPASS(ucount + dstdp.d_reclen <= count);
4041 error = copyout(&dstdp, ubuf + ucount, dstdp.d_reclen);
4044 dp = (struct dirent *)((char *)dp + dp->d_reclen);
4045 ucount += dstdp.d_reclen;
4049 free(dirbuf, M_TEMP);
4051 td->td_retval[0] = ucount;
4058 ogetdirentries_cvt(struct freebsd11_dirent *dp)
4060 #if (BYTE_ORDER == LITTLE_ENDIAN)
4062 * The expected low byte of dp->d_namlen is our dp->d_type.
4063 * The high MBZ byte of dp->d_namlen is our dp->d_namlen.
4065 dp->d_type = dp->d_namlen;
4069 * The dp->d_type is the high byte of the expected dp->d_namlen,
4070 * so must be zero'ed.
4077 * Read a block of directory entries in a filesystem independent format.
4079 #ifndef _SYS_SYSPROTO_H_
4080 struct ogetdirentries_args {
4088 ogetdirentries(struct thread *td, struct ogetdirentries_args *uap)
4093 error = kern_ogetdirentries(td, uap, &loff);
4095 error = copyout(&loff, uap->basep, sizeof(long));
4100 kern_ogetdirentries(struct thread *td, struct ogetdirentries_args *uap,
4106 /* XXX arbitrary sanity limit on `count'. */
4107 if (uap->count > 64 * 1024)
4110 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4111 &base, ogetdirentries_cvt);
4113 if (error == 0 && uap->basep != NULL)
4114 error = copyout(&base, uap->basep, sizeof(long));
4118 #endif /* COMPAT_43 */
4120 #if defined(COMPAT_FREEBSD11)
4121 #ifndef _SYS_SYSPROTO_H_
4122 struct freebsd11_getdirentries_args {
4130 freebsd11_getdirentries(struct thread *td,
4131 struct freebsd11_getdirentries_args *uap)
4136 error = freebsd11_kern_getdirentries(td, uap->fd, uap->buf, uap->count,
4139 if (error == 0 && uap->basep != NULL)
4140 error = copyout(&base, uap->basep, sizeof(long));
4145 freebsd11_getdents(struct thread *td, struct freebsd11_getdents_args *uap)
4147 struct freebsd11_getdirentries_args ap;
4151 ap.count = uap->count;
4153 return (freebsd11_getdirentries(td, &ap));
4155 #endif /* COMPAT_FREEBSD11 */
4158 * Read a block of directory entries in a filesystem independent format.
4161 sys_getdirentries(struct thread *td, struct getdirentries_args *uap)
4166 error = kern_getdirentries(td, uap->fd, uap->buf, uap->count, &base,
4167 NULL, UIO_USERSPACE);
4170 if (uap->basep != NULL)
4171 error = copyout(&base, uap->basep, sizeof(off_t));
4176 kern_getdirentries(struct thread *td, int fd, char *buf, size_t count,
4177 off_t *basep, ssize_t *residp, enum uio_seg bufseg)
4188 if (count > IOSIZE_MAX)
4190 auio.uio_resid = count;
4191 error = getvnode(td, fd, &cap_read_rights, &fp);
4194 if ((fp->f_flag & FREAD) == 0) {
4199 foffset = foffset_lock(fp, 0);
4201 if (vp->v_type != VDIR) {
4205 if (__predict_false((vp->v_vflag & VV_UNLINKED) != 0)) {
4209 aiov.iov_base = buf;
4210 aiov.iov_len = count;
4211 auio.uio_iov = &aiov;
4212 auio.uio_iovcnt = 1;
4213 auio.uio_rw = UIO_READ;
4214 auio.uio_segflg = bufseg;
4216 vn_lock(vp, LK_SHARED | LK_RETRY);
4217 AUDIT_ARG_VNODE1(vp);
4218 loff = auio.uio_offset = foffset;
4220 error = mac_vnode_check_readdir(td->td_ucred, vp);
4223 error = VOP_READDIR(vp, &auio, fp->f_cred, &eofflag, NULL,
4225 foffset = auio.uio_offset;
4230 if (count == auio.uio_resid &&
4231 (vp->v_vflag & VV_ROOT) &&
4232 (vp->v_mount->mnt_flag & MNT_UNION)) {
4233 struct vnode *tvp = vp;
4235 vp = vp->v_mount->mnt_vnodecovered;
4245 *residp = auio.uio_resid;
4246 td->td_retval[0] = count - auio.uio_resid;
4248 foffset_unlock(fp, foffset, 0);
4254 * Set the mode mask for creation of filesystem nodes.
4256 #ifndef _SYS_SYSPROTO_H_
4262 sys_umask(struct thread *td, struct umask_args *uap)
4264 struct pwddesc *pdp;
4266 pdp = td->td_proc->p_pd;
4268 td->td_retval[0] = pdp->pd_cmask;
4269 pdp->pd_cmask = uap->newmask & ALLPERMS;
4270 PWDDESC_XUNLOCK(pdp);
4275 * Void all references to file by ripping underlying filesystem away from
4278 #ifndef _SYS_SYSPROTO_H_
4279 struct revoke_args {
4284 sys_revoke(struct thread *td, struct revoke_args *uap)
4288 struct nameidata nd;
4291 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_USERSPACE,
4293 if ((error = namei(&nd)) != 0)
4297 if (vp->v_type != VCHR || vp->v_rdev == NULL) {
4302 error = mac_vnode_check_revoke(td->td_ucred, vp);
4306 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
4309 if (td->td_ucred->cr_uid != vattr.va_uid) {
4310 error = priv_check(td, PRIV_VFS_ADMIN);
4314 if (devfs_usecount(vp) > 0)
4315 VOP_REVOKE(vp, REVOKEALL);
4322 * This variant of getvnode() allows O_PATH files. Caller should
4323 * ensure that returned file and vnode are only used for compatible
4327 getvnode_path(struct thread *td, int fd, cap_rights_t *rightsp,
4333 error = fget_unlocked(td, fd, rightsp, &fp);
4338 * The file could be not of the vnode type, or it may be not
4339 * yet fully initialized, in which case the f_vnode pointer
4340 * may be set, but f_ops is still badfileops. E.g.,
4341 * devfs_open() transiently create such situation to
4342 * facilitate csw d_fdopen().
4344 * Dupfdopen() handling in kern_openat() installs the
4345 * half-baked file into the process descriptor table, allowing
4346 * other thread to dereference it. Guard against the race by
4349 if (__predict_false(fp->f_vnode == NULL || fp->f_ops == &badfileops)) {
4360 * Convert a user file descriptor to a kernel file entry and check
4361 * that, if it is a capability, the correct rights are present.
4362 * A reference on the file entry is held upon returning.
4365 getvnode(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
4369 error = getvnode_path(td, fd, rightsp, fpp);
4370 if (__predict_false(error != 0))
4374 * Filter out O_PATH file descriptors, most getvnode() callers
4375 * do not call fo_ methods.
4377 if (__predict_false((*fpp)->f_ops == &path_fileops)) {
4387 * Get an (NFS) file handle.
4389 #ifndef _SYS_SYSPROTO_H_
4390 struct lgetfh_args {
4396 sys_lgetfh(struct thread *td, struct lgetfh_args *uap)
4399 return (kern_getfhat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, uap->fname,
4400 UIO_USERSPACE, uap->fhp, UIO_USERSPACE));
4403 #ifndef _SYS_SYSPROTO_H_
4410 sys_getfh(struct thread *td, struct getfh_args *uap)
4413 return (kern_getfhat(td, 0, AT_FDCWD, uap->fname, UIO_USERSPACE,
4414 uap->fhp, UIO_USERSPACE));
4418 * syscall for the rpc.lockd to use to translate an open descriptor into
4419 * a NFS file handle.
4421 * warning: do not remove the priv_check() call or this becomes one giant
4424 #ifndef _SYS_SYSPROTO_H_
4425 struct getfhat_args {
4433 sys_getfhat(struct thread *td, struct getfhat_args *uap)
4436 return (kern_getfhat(td, uap->flags, uap->fd, uap->path, UIO_USERSPACE,
4437 uap->fhp, UIO_USERSPACE));
4441 kern_getfhat(struct thread *td, int flags, int fd, const char *path,
4442 enum uio_seg pathseg, fhandle_t *fhp, enum uio_seg fhseg)
4444 struct nameidata nd;
4449 if ((flags & ~(AT_SYMLINK_NOFOLLOW | AT_RESOLVE_BENEATH)) != 0)
4451 error = priv_check(td, PRIV_VFS_GETFH);
4454 NDINIT_AT(&nd, LOOKUP, at2cnpflags(flags, AT_SYMLINK_NOFOLLOW |
4455 AT_RESOLVE_BENEATH) | LOCKLEAF | AUDITVNODE1, pathseg, path,
4462 bzero(&fh, sizeof(fh));
4463 fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
4464 error = VOP_VPTOFH(vp, &fh.fh_fid);
4467 if (fhseg == UIO_USERSPACE)
4468 error = copyout(&fh, fhp, sizeof (fh));
4470 memcpy(fhp, &fh, sizeof(fh));
4475 #ifndef _SYS_SYSPROTO_H_
4476 struct fhlink_args {
4482 sys_fhlink(struct thread *td, struct fhlink_args *uap)
4485 return (kern_fhlinkat(td, AT_FDCWD, uap->to, UIO_USERSPACE, uap->fhp));
4488 #ifndef _SYS_SYSPROTO_H_
4489 struct fhlinkat_args {
4496 sys_fhlinkat(struct thread *td, struct fhlinkat_args *uap)
4499 return (kern_fhlinkat(td, uap->tofd, uap->to, UIO_USERSPACE, uap->fhp));
4503 kern_fhlinkat(struct thread *td, int fd, const char *path,
4504 enum uio_seg pathseg, fhandle_t *fhp)
4511 error = priv_check(td, PRIV_VFS_GETFH);
4514 error = copyin(fhp, &fh, sizeof(fh));
4519 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4521 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4526 error = kern_linkat_vp(td, vp, fd, path, pathseg);
4527 } while (error == EAGAIN || error == ERELOOKUP);
4531 #ifndef _SYS_SYSPROTO_H_
4532 struct fhreadlink_args {
4539 sys_fhreadlink(struct thread *td, struct fhreadlink_args *uap)
4546 error = priv_check(td, PRIV_VFS_GETFH);
4549 if (uap->bufsize > IOSIZE_MAX)
4551 error = copyin(uap->fhp, &fh, sizeof(fh));
4554 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4556 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_SHARED, &vp);
4560 error = kern_readlink_vp(vp, uap->buf, UIO_USERSPACE, uap->bufsize, td);
4566 * syscall for the rpc.lockd to use to translate a NFS file handle into an
4569 * warning: do not remove the priv_check() call or this becomes one giant
4572 #ifndef _SYS_SYSPROTO_H_
4573 struct fhopen_args {
4574 const struct fhandle *u_fhp;
4579 sys_fhopen(struct thread *td, struct fhopen_args *uap)
4581 return (kern_fhopen(td, uap->u_fhp, uap->flags));
4585 kern_fhopen(struct thread *td, const struct fhandle *u_fhp, int flags)
4594 error = priv_check(td, PRIV_VFS_FHOPEN);
4598 fmode = FFLAGS(flags);
4599 /* why not allow a non-read/write open for our lockd? */
4600 if (((fmode & (FREAD | FWRITE)) == 0) || (fmode & O_CREAT))
4602 error = copyin(u_fhp, &fhp, sizeof(fhp));
4605 /* find the mount point */
4606 mp = vfs_busyfs(&fhp.fh_fsid);
4609 /* now give me my vnode, it gets returned to me locked */
4610 error = VFS_FHTOVP(mp, &fhp.fh_fid, LK_EXCLUSIVE, &vp);
4615 error = falloc_noinstall(td, &fp);
4621 * An extra reference on `fp' has been held for us by
4622 * falloc_noinstall().
4628 error = vn_open_vnode(vp, fmode, td->td_ucred, td, fp);
4630 KASSERT(fp->f_ops == &badfileops,
4631 ("VOP_OPEN in fhopen() set f_ops"));
4632 KASSERT(td->td_dupfd < 0,
4633 ("fhopen() encountered fdopen()"));
4642 finit_vnode(fp, fmode, NULL, &vnops);
4644 if ((fmode & O_TRUNC) != 0) {
4645 error = fo_truncate(fp, 0, td->td_ucred, td);
4650 error = finstall(td, fp, &indx, fmode, NULL);
4653 td->td_retval[0] = indx;
4658 * Stat an (NFS) file handle.
4660 #ifndef _SYS_SYSPROTO_H_
4661 struct fhstat_args {
4662 struct fhandle *u_fhp;
4667 sys_fhstat(struct thread *td, struct fhstat_args *uap)
4673 error = copyin(uap->u_fhp, &fh, sizeof(fh));
4676 error = kern_fhstat(td, fh, &sb);
4678 error = copyout(&sb, uap->sb, sizeof(sb));
4683 kern_fhstat(struct thread *td, struct fhandle fh, struct stat *sb)
4689 error = priv_check(td, PRIV_VFS_FHSTAT);
4692 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4694 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4698 error = VOP_STAT(vp, sb, td->td_ucred, NOCRED);
4704 * Implement fstatfs() for (NFS) file handles.
4706 #ifndef _SYS_SYSPROTO_H_
4707 struct fhstatfs_args {
4708 struct fhandle *u_fhp;
4713 sys_fhstatfs(struct thread *td, struct fhstatfs_args *uap)
4719 error = copyin(uap->u_fhp, &fh, sizeof(fhandle_t));
4722 sfp = malloc(sizeof(struct statfs), M_STATFS, M_WAITOK);
4723 error = kern_fhstatfs(td, fh, sfp);
4725 error = copyout(sfp, uap->buf, sizeof(*sfp));
4726 free(sfp, M_STATFS);
4731 kern_fhstatfs(struct thread *td, fhandle_t fh, struct statfs *buf)
4737 error = priv_check(td, PRIV_VFS_FHSTATFS);
4740 if ((mp = vfs_busyfs(&fh.fh_fsid)) == NULL)
4742 error = VFS_FHTOVP(mp, &fh.fh_fid, LK_EXCLUSIVE, &vp);
4748 error = prison_canseemount(td->td_ucred, mp);
4752 error = mac_mount_check_stat(td->td_ucred, mp);
4756 error = VFS_STATFS(mp, buf);
4763 * Unlike madvise(2), we do not make a best effort to remember every
4764 * possible caching hint. Instead, we remember the last setting with
4765 * the exception that we will allow POSIX_FADV_NORMAL to adjust the
4766 * region of any current setting.
4769 kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len,
4772 struct fadvise_info *fa, *new;
4778 if (offset < 0 || len < 0 || offset > OFF_MAX - len)
4780 AUDIT_ARG_VALUE(advice);
4782 case POSIX_FADV_SEQUENTIAL:
4783 case POSIX_FADV_RANDOM:
4784 case POSIX_FADV_NOREUSE:
4785 new = malloc(sizeof(*fa), M_FADVISE, M_WAITOK);
4787 case POSIX_FADV_NORMAL:
4788 case POSIX_FADV_WILLNEED:
4789 case POSIX_FADV_DONTNEED:
4795 /* XXX: CAP_POSIX_FADVISE? */
4797 error = fget(td, fd, &cap_no_rights, &fp);
4800 AUDIT_ARG_FILE(td->td_proc, fp);
4801 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
4805 if (fp->f_type != DTYPE_VNODE) {
4810 if (vp->v_type != VREG) {
4817 end = offset + len - 1;
4819 case POSIX_FADV_SEQUENTIAL:
4820 case POSIX_FADV_RANDOM:
4821 case POSIX_FADV_NOREUSE:
4823 * Try to merge any existing non-standard region with
4824 * this new region if possible, otherwise create a new
4825 * non-standard region for this request.
4827 mtx_pool_lock(mtxpool_sleep, fp);
4829 if (fa != NULL && fa->fa_advice == advice &&
4830 ((fa->fa_start <= end && fa->fa_end >= offset) ||
4831 (end != OFF_MAX && fa->fa_start == end + 1) ||
4832 (fa->fa_end != OFF_MAX && fa->fa_end + 1 == offset))) {
4833 if (offset < fa->fa_start)
4834 fa->fa_start = offset;
4835 if (end > fa->fa_end)
4838 new->fa_advice = advice;
4839 new->fa_start = offset;
4844 mtx_pool_unlock(mtxpool_sleep, fp);
4846 case POSIX_FADV_NORMAL:
4848 * If a the "normal" region overlaps with an existing
4849 * non-standard region, trim or remove the
4850 * non-standard region.
4852 mtx_pool_lock(mtxpool_sleep, fp);
4855 if (offset <= fa->fa_start && end >= fa->fa_end) {
4857 fp->f_advice = NULL;
4858 } else if (offset <= fa->fa_start &&
4859 end >= fa->fa_start)
4860 fa->fa_start = end + 1;
4861 else if (offset <= fa->fa_end && end >= fa->fa_end)
4862 fa->fa_end = offset - 1;
4863 else if (offset >= fa->fa_start && end <= fa->fa_end) {
4865 * If the "normal" region is a middle
4866 * portion of the existing
4867 * non-standard region, just remove
4868 * the whole thing rather than picking
4869 * one side or the other to
4873 fp->f_advice = NULL;
4876 mtx_pool_unlock(mtxpool_sleep, fp);
4878 case POSIX_FADV_WILLNEED:
4879 case POSIX_FADV_DONTNEED:
4880 error = VOP_ADVISE(vp, offset, end, advice);
4886 free(new, M_FADVISE);
4891 sys_posix_fadvise(struct thread *td, struct posix_fadvise_args *uap)
4895 error = kern_posix_fadvise(td, uap->fd, uap->offset, uap->len,
4897 return (kern_posix_error(td, error));
4901 kern_copy_file_range(struct thread *td, int infd, off_t *inoffp, int outfd,
4902 off_t *outoffp, size_t len, unsigned int flags)
4904 struct file *infp, *outfp;
4905 struct vnode *invp, *outvp;
4908 void *rl_rcookie, *rl_wcookie;
4909 off_t savinoff, savoutoff;
4911 infp = outfp = NULL;
4912 rl_rcookie = rl_wcookie = NULL;
4921 if (len > SSIZE_MAX)
4923 * Although the len argument is size_t, the return argument
4924 * is ssize_t (which is signed). Therefore a size that won't
4925 * fit in ssize_t can't be returned.
4929 /* Get the file structures for the file descriptors. */
4930 error = fget_read(td, infd,
4931 inoffp != NULL ? &cap_pread_rights : &cap_read_rights, &infp);
4934 if (infp->f_ops == &badfileops) {
4938 if (infp->f_vnode == NULL) {
4942 error = fget_write(td, outfd,
4943 outoffp != NULL ? &cap_pwrite_rights : &cap_write_rights, &outfp);
4946 if (outfp->f_ops == &badfileops) {
4950 if (outfp->f_vnode == NULL) {
4955 /* Set the offset pointers to the correct place. */
4957 inoffp = &infp->f_offset;
4958 if (outoffp == NULL)
4959 outoffp = &outfp->f_offset;
4961 savoutoff = *outoffp;
4963 invp = infp->f_vnode;
4964 outvp = outfp->f_vnode;
4965 /* Sanity check the f_flag bits. */
4966 if ((outfp->f_flag & (FWRITE | FAPPEND)) != FWRITE ||
4967 (infp->f_flag & FREAD) == 0) {
4972 /* If len == 0, just return 0. */
4977 * If infp and outfp refer to the same file, the byte ranges cannot
4980 if (invp == outvp && ((savinoff <= savoutoff && savinoff + len >
4981 savoutoff) || (savinoff > savoutoff && savoutoff + len >
4987 /* Range lock the byte ranges for both invp and outvp. */
4989 rl_wcookie = vn_rangelock_wlock(outvp, *outoffp, *outoffp +
4991 rl_rcookie = vn_rangelock_tryrlock(invp, *inoffp, *inoffp +
4993 if (rl_rcookie != NULL)
4995 vn_rangelock_unlock(outvp, rl_wcookie);
4996 rl_rcookie = vn_rangelock_rlock(invp, *inoffp, *inoffp + len);
4997 vn_rangelock_unlock(invp, rl_rcookie);
5001 error = vn_copy_file_range(invp, inoffp, outvp, outoffp, &retlen,
5002 flags, infp->f_cred, outfp->f_cred, td);
5004 if (rl_rcookie != NULL)
5005 vn_rangelock_unlock(invp, rl_rcookie);
5006 if (rl_wcookie != NULL)
5007 vn_rangelock_unlock(outvp, rl_wcookie);
5008 if (savinoff != -1 && (error == EINTR || error == ERESTART)) {
5010 *outoffp = savoutoff;
5016 td->td_retval[0] = retlen;
5021 sys_copy_file_range(struct thread *td, struct copy_file_range_args *uap)
5023 off_t inoff, outoff, *inoffp, *outoffp;
5026 inoffp = outoffp = NULL;
5027 if (uap->inoffp != NULL) {
5028 error = copyin(uap->inoffp, &inoff, sizeof(off_t));
5033 if (uap->outoffp != NULL) {
5034 error = copyin(uap->outoffp, &outoff, sizeof(off_t));
5039 error = kern_copy_file_range(td, uap->infd, inoffp, uap->outfd,
5040 outoffp, uap->len, uap->flags);
5041 if (error == 0 && uap->inoffp != NULL)
5042 error = copyout(inoffp, uap->inoffp, sizeof(off_t));
5043 if (error == 0 && uap->outoffp != NULL)
5044 error = copyout(outoffp, uap->outoffp, sizeof(off_t));