2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 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 #include <sys/dirent.h>
44 #include <sys/kernel.h>
45 #include <sys/capsicum.h>
46 #include <sys/fcntl.h>
49 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/filedesc.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/sysctl.h>
59 #include <sys/ktrace.h>
62 #include <machine/_inttypes.h>
65 #include <security/audit/audit.h>
66 #include <security/mac/mac_framework.h>
71 static void NDVALIDATE_impl(struct nameidata *, int);
72 #define NDVALIDATE(ndp) NDVALIDATE_impl(ndp, __LINE__)
74 #define NDVALIDATE(ndp)
78 * Prepare namei() to restart. Reset components to its original state and set
79 * ISRESTARTED flag which signals the underlying lookup code to change the root
80 * from ABI root to actual root and prevents a further restarts.
82 #define NDRESTART(ndp) do { \
84 ndp->ni_resflags = 0; \
85 ndp->ni_cnd.cn_flags &= ~NAMEI_INTERNAL_FLAGS; \
86 ndp->ni_cnd.cn_flags |= ISRESTARTED; \
89 SDT_PROVIDER_DEFINE(vfs);
90 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *",
91 "unsigned long", "bool");
92 SDT_PROBE_DEFINE4(vfs, namei, lookup, return, "int", "struct vnode *", "bool",
95 /* Allocation zone for namei. */
96 uma_zone_t namei_zone;
98 /* Placeholder vnode for mp traversal. */
99 static struct vnode *vp_crossmp;
102 crossmp_vop_islocked(struct vop_islocked_args *ap)
109 crossmp_vop_lock1(struct vop_lock1_args *ap)
112 struct lock *lk __diagused;
119 KASSERT((flags & (LK_SHARED | LK_NOWAIT)) == (LK_SHARED | LK_NOWAIT),
120 ("%s: invalid lock request 0x%x for crossmp", __func__, flags));
122 if ((flags & LK_INTERLOCK) != 0)
124 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, ap->a_line);
129 crossmp_vop_unlock(struct vop_unlock_args *ap)
132 struct lock *lk __diagused;
137 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
142 static struct vop_vector crossmp_vnodeops = {
143 .vop_default = &default_vnodeops,
144 .vop_islocked = crossmp_vop_islocked,
145 .vop_lock1 = crossmp_vop_lock1,
146 .vop_unlock = crossmp_vop_unlock,
149 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
150 * gets allocated early. See nameiinit for the direct call below.
153 struct nameicap_tracker {
155 TAILQ_ENTRY(nameicap_tracker) nm_link;
158 /* Zone for cap mode tracker elements used for dotdot capability checks. */
159 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot");
162 nameiinit(void *dummy __unused)
165 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
167 vfs_vector_op_register(&crossmp_vnodeops);
168 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
169 vp_crossmp->v_state = VSTATE_CONSTRUCTED;
170 vp_crossmp->v_irflag |= VIRF_CROSSMP;
172 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
174 static int lookup_cap_dotdot = 1;
175 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
176 &lookup_cap_dotdot, 0,
177 "enables \"..\" components in path lookup in capability mode");
178 static int lookup_cap_dotdot_nonlocal = 1;
179 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
180 &lookup_cap_dotdot_nonlocal, 0,
181 "enables \"..\" components in path lookup in capability mode "
182 "on non-local mount");
185 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
187 struct nameicap_tracker *nt;
189 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
191 nt = TAILQ_LAST(&ndp->ni_cap_tracker, nameicap_tracker_head);
192 if (nt != NULL && nt->dp == dp)
194 nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK);
197 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
201 nameicap_cleanup_from(struct nameidata *ndp, struct nameicap_tracker *first)
203 struct nameicap_tracker *nt, *nt1;
206 TAILQ_FOREACH_FROM_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
207 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
209 free(nt, M_NAMEITRACKER);
214 nameicap_cleanup(struct nameidata *ndp)
216 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
217 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
218 nameicap_cleanup_from(ndp, NULL);
222 * For dotdot lookups in capability mode, only allow the component
223 * lookup to succeed if the resulting directory was already traversed
224 * during the operation. This catches situations where already
225 * traversed directory is moved to different parent, and then we walk
226 * over it with dotdots.
228 * Also allow to force failure of dotdot lookups for non-local
229 * filesystems, where external agents might assist local lookups to
230 * escape the compartment.
233 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
235 struct nameicap_tracker *nt;
238 if (dp == NULL || dp->v_type != VDIR || (ndp->ni_lcf &
239 NI_LCF_STRICTRELATIVE) == 0)
241 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0)
242 return (ENOTCAPABLE);
244 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
245 (mp->mnt_flag & MNT_LOCAL) == 0)
246 return (ENOTCAPABLE);
247 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
250 nt = TAILQ_NEXT(nt, nm_link);
252 nameicap_cleanup_from(ndp, nt);
256 return (ENOTCAPABLE);
260 namei_cleanup_cnp(struct componentname *cnp)
263 uma_zfree(namei_zone, cnp->cn_pnbuf);
264 cnp->cn_pnbuf = NULL;
265 cnp->cn_nameptr = NULL;
269 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
271 struct componentname *cnp;
274 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
276 if (KTRPOINT(curthread, KTR_CAPFAIL))
277 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
279 return (ENOTCAPABLE);
281 while (*(cnp->cn_nameptr) == '/') {
285 *dpp = ndp->ni_rootdir;
291 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
293 struct componentname *cnp;
302 startdir_used = false;
306 #ifdef CAPABILITY_MODE
308 * In capability mode, lookups must be restricted to happen in
309 * the subtree with the root specified by the file descriptor:
310 * - The root must be real file descriptor, not the pseudo-descriptor
312 * - The passed path must be relative and not absolute.
313 * - If lookup_cap_dotdot is disabled, path must not contain the
315 * - If lookup_cap_dotdot is enabled, we verify that all '..'
316 * components lookups result in the directories which were
317 * previously walked by us, which prevents an escape from
320 if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
321 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
322 ndp->ni_resflags |= NIRES_STRICTREL;
323 if (ndp->ni_dirfd == AT_FDCWD) {
325 if (KTRPOINT(td, KTR_CAPFAIL))
326 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
335 * Get starting point for the translation.
339 * The reference on ni_rootdir is acquired in the block below to avoid
340 * back-to-back atomics for absolute lookups.
342 namei_setup_rootdir(ndp, cnp, pwd);
343 ndp->ni_topdir = pwd->pwd_jdir;
345 if (cnp->cn_pnbuf[0] == '/') {
346 ndp->ni_resflags |= NIRES_ABS;
347 error = namei_handle_root(ndp, dpp);
349 if (ndp->ni_startdir != NULL) {
350 *dpp = ndp->ni_startdir;
351 startdir_used = true;
352 } else if (ndp->ni_dirfd == AT_FDCWD) {
353 *dpp = pwd->pwd_cdir;
356 if (cnp->cn_flags & AUDITVNODE1)
357 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
358 if (cnp->cn_flags & AUDITVNODE2)
359 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
361 error = fgetvp_lookup(ndp->ni_dirfd, ndp, dpp);
363 if (error == 0 && (*dpp)->v_type != VDIR &&
364 (cnp->cn_pnbuf[0] != '\0' ||
365 (cnp->cn_flags & EMPTYPATH) == 0))
368 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) {
369 if (cnp->cn_pnbuf[0] == '/') {
371 } else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) {
372 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE |
378 * If we are auditing the kernel pathname, save the user pathname.
380 if (AUDITING_TD(td)) {
381 if (cnp->cn_flags & AUDITVNODE1)
382 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
383 if (cnp->cn_flags & AUDITVNODE2)
384 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
386 if (ndp->ni_startdir != NULL && !startdir_used)
387 vrele(ndp->ni_startdir);
394 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
395 lookup_cap_dotdot != 0)
396 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
397 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
398 cnp->cn_flags, false);
404 namei_getpath(struct nameidata *ndp)
406 struct componentname *cnp;
412 * Get a buffer for the name to be translated, and copy the
413 * name into the buffer.
415 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
416 if (ndp->ni_segflg == UIO_SYSSPACE) {
417 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
420 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
428 namei_emptypath(struct nameidata *ndp)
430 struct componentname *cnp;
436 MPASS(*cnp->cn_pnbuf == '\0');
437 MPASS((cnp->cn_flags & EMPTYPATH) != 0);
438 MPASS((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) == 0);
440 ndp->ni_resflags |= NIRES_EMPTYPATH;
441 error = namei_setup(ndp, &dp, &pwd);
447 * Usecount on dp already provided by namei_setup.
452 if ((cnp->cn_flags & LOCKLEAF) != 0) {
453 VOP_LOCK(dp, (cnp->cn_flags & LOCKSHARED) != 0 ?
454 LK_SHARED : LK_EXCLUSIVE);
455 if (VN_IS_DOOMED(dp)) {
461 SDT_PROBE4(vfs, namei, lookup, return, 0, ndp->ni_vp, false, ndp);
465 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
466 namei_cleanup_cnp(cnp);
470 static int __noinline
471 namei_follow_link(struct nameidata *ndp)
476 struct componentname *cnp;
484 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
489 if ((cnp->cn_flags & NOMACCHECK) == 0) {
490 error = mac_vnode_check_readlink(td->td_ucred, ndp->ni_vp);
495 if (ndp->ni_pathlen > 1)
496 cp = uma_zalloc(namei_zone, M_WAITOK);
500 aiov.iov_len = MAXPATHLEN;
501 auio.uio_iov = &aiov;
504 auio.uio_rw = UIO_READ;
505 auio.uio_segflg = UIO_SYSSPACE;
507 auio.uio_resid = MAXPATHLEN;
508 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
510 if (ndp->ni_pathlen > 1)
511 uma_zfree(namei_zone, cp);
514 linklen = MAXPATHLEN - auio.uio_resid;
516 if (ndp->ni_pathlen > 1)
517 uma_zfree(namei_zone, cp);
521 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
522 if (ndp->ni_pathlen > 1)
523 uma_zfree(namei_zone, cp);
524 error = ENAMETOOLONG;
527 if (ndp->ni_pathlen > 1) {
528 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
529 uma_zfree(namei_zone, cnp->cn_pnbuf);
532 cnp->cn_pnbuf[linklen] = '\0';
533 ndp->ni_pathlen += linklen;
539 * Convert a pathname into a pointer to a locked vnode.
541 * The FOLLOW flag is set when symbolic links are to be followed
542 * when they occur at the end of the name translation process.
543 * Symbolic links are always followed for all other pathname
544 * components other than the last.
546 * The segflg defines whether the name is to be copied from user
547 * space or kernel space.
549 * Overall outline of namei:
552 * get starting directory
553 * while (!done && !error) {
554 * call lookup to search path.
555 * if symbolic link, massage name in buffer and continue
559 namei(struct nameidata *ndp)
561 struct vnode *dp; /* the directory we are searching */
562 struct componentname *cnp;
566 enum cache_fpl_status status;
571 KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0,
572 ("%s: repeated call to namei without NDREINIT", __func__));
573 KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED,
574 ("%s: bad debugflags %d", __func__, ndp->ni_debugflags));
575 ndp->ni_debugflags |= NAMEI_DBG_CALLED;
576 if (ndp->ni_startdir != NULL)
577 ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR;
578 if (cnp->cn_flags & FAILIFEXISTS) {
579 KASSERT(cnp->cn_nameiop == CREATE,
580 ("%s: FAILIFEXISTS passed for op %d", __func__, cnp->cn_nameiop));
582 * The limitation below is to restrict hairy corner cases.
584 KASSERT((cnp->cn_flags & (LOCKPARENT | LOCKLEAF)) == LOCKPARENT,
585 ("%s: FAILIFEXISTS must be passed with LOCKPARENT and without LOCKLEAF",
589 ndp->ni_cnd.cn_cred = td->td_ucred;
590 KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n",
591 __func__, ndp->ni_resflags));
592 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
593 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
594 ("namei: unexpected flags: %" PRIx64 "\n",
595 cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
596 if (cnp->cn_flags & NOCACHE)
597 KASSERT(cnp->cn_nameiop != LOOKUP,
598 ("%s: NOCACHE passed with LOOKUP", __func__));
599 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
600 ndp->ni_startdir->v_type == VBAD);
607 error = namei_getpath(ndp);
608 if (__predict_false(error != 0)) {
609 namei_cleanup_cnp(cnp);
610 SDT_PROBE4(vfs, namei, lookup, return, error, NULL,
615 cnp->cn_nameptr = cnp->cn_pnbuf;
618 if (KTRPOINT(td, KTR_NAMEI)) {
619 ktrnamei(cnp->cn_pnbuf);
622 TSNAMEI(curthread->td_proc->p_pid, cnp->cn_pnbuf);
625 * First try looking up the target without locking any vnodes.
627 * We may need to start from scratch or pick up where it left off.
629 error = cache_fplookup(ndp, &status, &pwd);
631 case CACHE_FPL_STATUS_UNSET:
632 __assert_unreachable();
634 case CACHE_FPL_STATUS_HANDLED:
637 else if (__predict_false(pwd->pwd_adir != pwd->pwd_rdir &&
638 (cnp->cn_flags & ISRESTARTED) == 0)) {
639 namei_cleanup_cnp(cnp);
644 case CACHE_FPL_STATUS_PARTIAL:
645 TAILQ_INIT(&ndp->ni_cap_tracker);
646 dp = ndp->ni_startdir;
648 case CACHE_FPL_STATUS_DESTROYED:
650 error = namei_getpath(ndp);
651 if (__predict_false(error != 0)) {
652 namei_cleanup_cnp(cnp);
655 cnp->cn_nameptr = cnp->cn_pnbuf;
657 case CACHE_FPL_STATUS_ABORTED:
658 TAILQ_INIT(&ndp->ni_cap_tracker);
659 MPASS(ndp->ni_lcf == 0);
660 if (*cnp->cn_pnbuf == '\0') {
661 if ((cnp->cn_flags & EMPTYPATH) != 0) {
662 return (namei_emptypath(ndp));
664 namei_cleanup_cnp(cnp);
665 SDT_PROBE4(vfs, namei, lookup, return, ENOENT, NULL,
669 error = namei_setup(ndp, &dp, &pwd);
671 namei_cleanup_cnp(cnp);
681 ndp->ni_startdir = dp;
682 error = vfs_lookup(ndp);
684 if (__predict_false(pwd->pwd_adir != pwd->pwd_rdir &&
686 (cnp->cn_flags & ISRESTARTED) == 0)) {
687 nameicap_cleanup(ndp);
689 namei_cleanup_cnp(cnp);
697 * If not a symbolic link, we're done.
699 if ((cnp->cn_flags & ISSYMLINK) == 0) {
700 SDT_PROBE4(vfs, namei, lookup, return, error,
701 ndp->ni_vp, false, ndp);
702 nameicap_cleanup(ndp);
707 error = namei_follow_link(ndp);
713 * Check if root directory should replace current directory.
715 cnp->cn_nameptr = cnp->cn_pnbuf;
716 if (*(cnp->cn_nameptr) == '/') {
718 * Reset the lookup to start from the real root without
719 * origin path name reloading.
721 if (__predict_false(ndp->ni_rootdir != pwd->pwd_rdir)) {
722 cnp->cn_flags |= ISRESTARTED;
723 ndp->ni_rootdir = pwd->pwd_rdir;
726 error = namei_handle_root(ndp, &dp);
736 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp);
737 namei_cleanup_cnp(cnp);
738 nameicap_cleanup(ndp);
744 enforce_lkflags(struct mount *mp, int lkflags)
747 if (mp == NULL || ((lkflags & LK_SHARED) &&
748 !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) {
749 lkflags &= ~LK_SHARED;
750 lkflags |= LK_EXCLUSIVE;
752 lkflags |= LK_NODDLKTREAT;
757 needs_exclusive_leaf(struct mount *mp, int flags)
761 * Intermediate nodes can use shared locks, we only need to
762 * force an exclusive lock for leaf nodes.
764 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
767 /* Always use exclusive locks if LOCKSHARED isn't set. */
768 if (!(flags & LOCKSHARED))
772 * For lookups during open(), if the mount point supports
773 * extended shared operations, then use a shared lock for the
774 * leaf node, otherwise use an exclusive lock.
776 if ((flags & ISOPEN) != 0)
777 return (!MNT_EXTENDED_SHARED(mp));
780 * Lookup requests outside of open() that specify LOCKSHARED
781 * only need a shared lock on the leaf vnode.
787 * Various filesystems expect to be able to copy a name component with length
788 * bounded by NAME_MAX into a directory entry buffer of size MAXNAMLEN. Make
789 * sure that these are the same size.
791 _Static_assert(MAXNAMLEN == NAME_MAX,
792 "MAXNAMLEN and NAME_MAX have different values");
794 static int __noinline
795 vfs_lookup_degenerate(struct nameidata *ndp, struct vnode *dp, int wantparent)
797 struct componentname *cnp;
803 cnp->cn_flags |= ISLASTCN;
805 mp = atomic_load_ptr(&dp->v_mount);
806 if (needs_exclusive_leaf(mp, cnp->cn_flags)) {
807 cnp->cn_lkflags &= ~LK_SHARED;
808 cnp->cn_lkflags |= LK_EXCLUSIVE;
811 vn_lock(dp, enforce_lkflags(mp, cnp->cn_lkflags | LK_RETRY));
813 if (dp->v_type != VDIR) {
817 if (cnp->cn_nameiop != LOOKUP) {
828 if (cnp->cn_flags & AUDITVNODE1)
829 AUDIT_ARG_VNODE1(dp);
830 else if (cnp->cn_flags & AUDITVNODE2)
831 AUDIT_ARG_VNODE2(dp);
833 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
842 * FAILIFEXISTS handling.
844 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange
845 * behaviour of leaving the vnode unlocked if the target is the same
846 * vnode as the parent.
848 static int __noinline
849 vfs_lookup_failifexists(struct nameidata *ndp)
851 struct componentname *cnp __diagused;
855 MPASS((cnp->cn_flags & ISSYMLINK) == 0);
856 if (ndp->ni_vp == ndp->ni_dvp)
867 static int __noinline
868 vfs_lookup_cross_mount(struct nameidata *ndp)
870 struct componentname *cnp;
872 struct vnode *dp, *tdp;
873 int error, crosslkflags;
880 * The vnode has been mounted on, find the root of the mounted
884 mp = dp->v_mountedhere;
885 ASSERT_VOP_LOCKED(dp, __func__);
886 VNPASS((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0 && mp != NULL, dp);
888 crosslock = (dp->v_vflag & VV_CROSSLOCK) != 0;
889 crosslkflags = enforce_lkflags(mp, cnp->cn_lkflags);
890 if (__predict_false(crosslock)) {
892 * We are going to be holding the vnode lock, which
893 * in this case is shared by the root vnode of the
894 * filesystem mounted at mp, across the call to
895 * VFS_ROOT(). Make the situation clear to the
896 * filesystem by passing LK_CANRECURSE if the
897 * lock is held exclusive, or by clearinng
898 * LK_NODDLKTREAT to allow recursion on the shared
899 * lock in the presence of an exclusive waiter.
901 if (VOP_ISLOCKED(dp) == LK_EXCLUSIVE) {
902 crosslkflags &= ~LK_SHARED;
903 crosslkflags |= LK_EXCLUSIVE | LK_CANRECURSE;
904 } else if ((crosslkflags & LK_EXCLUSIVE) != 0) {
905 error = vn_lock(dp, LK_UPGRADE);
907 MPASS(error == ENOENT);
909 if (dp != ndp->ni_dvp)
915 if (dp->v_mountedhere != mp) {
917 * Note that we rely on the
918 * VIRF_MOUNTPOINT loop condition to
919 * ensure we stop iterating if dp is
920 * no longer a mountpoint at all.
925 crosslkflags &= ~LK_NODDLKTREAT;
927 if (vfs_busy(mp, 0) != 0)
929 if (__predict_true(!crosslock))
931 if (dp != ndp->ni_dvp)
936 ndp->ni_dvp = vp_crossmp;
937 error = VFS_ROOT(mp, crosslkflags, &tdp);
939 if (__predict_false(crosslock))
941 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
942 panic("vp_crossmp exclusively locked or reclaimed");
945 ndp->ni_vp = dp = tdp;
946 } while ((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0);
953 * This is a very central and rather complicated routine.
955 * The pathname is pointed to by cn_nameptr and is of length ni_pathlen.
956 * The starting directory is taken from ni_startdir. The pathname is
957 * descended until done, or a symbolic link is encountered. The cn_flags
958 * has ISLASTCN or'ed if the path is completed or ISSYMLINK or'ed if a
959 * symbolic link needing interpretation is encountered.
961 * The cn_nameiop is LOOKUP, CREATE, RENAME, or DELETE depending on
962 * whether the name is to be looked up, created, renamed, or deleted.
963 * When CREATE, RENAME, or DELETE is specified, information usable in
964 * creating, renaming, or deleting a directory entry may be calculated.
965 * If cn_flags has LOCKPARENT or'ed into it, the parent directory is returned
966 * locked. If it has WANTPARENT or'ed into it, the parent directory is
967 * returned unlocked. Otherwise the parent directory is not returned. If
968 * the target of the pathname exists and LOCKLEAF is or'ed into the cn_flags
969 * the target is returned locked, otherwise it is returned unlocked.
971 * Overall outline of lookup:
973 * handle degenerate case where name is null string
976 * identify next component of name at ndp->ni_cnd.cn_nameptr
977 * handle .. special cases related to capabilities, chroot, jail
978 * if .. and crossing mount points and on mounted filesys, find parent
979 * call VOP_LOOKUP routine for next component name
980 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
981 * component vnode returned in ni_vp (if it exists), locked.
982 * if result vnode is mounted on and crossing mount points,
983 * find mounted on vnode
984 * if more components of name, do next level at dirloop
985 * if VOP_LOOKUP returns ERELOOKUP, repeat the same level at dirloop
986 * return the answer in ni_vp, locked if LOCKLEAF set
987 * if LOCKPARENT set, return locked parent in ni_dvp
988 * if WANTPARENT set, return unlocked parent in ni_dvp
991 vfs_lookup(struct nameidata *ndp)
993 char *cp; /* pointer into pathname argument */
994 char *prev_ni_next; /* saved ndp->ni_next */
995 char *nulchar; /* location of '\0' in cn_pnbuf */
996 char *lastchar; /* location of the last character */
997 struct vnode *dp = NULL; /* the directory we are searching */
998 struct vnode *tdp; /* saved dp */
1000 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
1001 int docache; /* == 0 do not cache last component */
1002 int wantparent; /* 1 => wantparent or lockparent flag */
1003 int rdonly; /* lookup read-only flag bit */
1005 int relookup = 0; /* do not consume the path component */
1006 struct componentname *cnp = &ndp->ni_cnd;
1008 int ni_dvp_unlocked;
1011 * Setup: break out flag bits into variables.
1013 ni_dvp_unlocked = 0;
1014 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
1015 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
1016 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
1018 * When set to zero, docache causes the last component of the
1019 * pathname to be deleted from the cache and the full lookup
1020 * of the name to be done (via VOP_CACHEDLOOKUP()). Often
1021 * filesystems need some pre-computed values that are made
1022 * during the full lookup, for instance UFS sets dp->i_offset.
1024 * The docache variable is set to zero when requested by the
1025 * NOCACHE flag and for all modifying operations except CREATE.
1027 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
1028 if (cnp->cn_nameiop == DELETE ||
1029 (wantparent && cnp->cn_nameiop != CREATE &&
1030 cnp->cn_nameiop != LOOKUP))
1032 rdonly = cnp->cn_flags & RDONLY;
1033 cnp->cn_flags &= ~ISSYMLINK;
1036 cnp->cn_lkflags = LK_SHARED;
1037 dp = ndp->ni_startdir;
1038 ndp->ni_startdir = NULLVP;
1041 * Leading slashes, if any, are supposed to be skipped by the caller.
1043 MPASS(cnp->cn_nameptr[0] != '/');
1046 * Check for degenerate name (e.g. / or "") which is a way of talking
1047 * about a directory, e.g. like "/." or ".".
1049 if (__predict_false(cnp->cn_nameptr[0] == '\0')) {
1050 error = vfs_lookup_degenerate(ndp, dp, wantparent);
1052 goto success_right_lock;
1057 * Nul-out trailing slashes (e.g., "foo///" -> "foo").
1059 * This must be done before VOP_LOOKUP() because some fs's don't know
1060 * about trailing slashes. Remember if there were trailing slashes to
1061 * handle symlinks, existing non-directories and non-existing files
1062 * that won't be directories specially later.
1064 MPASS(ndp->ni_pathlen >= 2);
1065 lastchar = &cnp->cn_nameptr[ndp->ni_pathlen - 2];
1066 if (*lastchar == '/') {
1067 while (lastchar >= cnp->cn_pnbuf) {
1071 if (*lastchar != '/') {
1075 cnp->cn_flags |= TRAILINGSLASH;
1079 * We use shared locks until we hit the parent of the last cn then
1080 * we adjust based on the requesting flags.
1083 enforce_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY));
1087 * Search a new directory.
1089 * The last component of the filename is left accessible via
1090 * cnp->cn_nameptr. It has to be freed with a call to NDFREE*.
1092 * Store / as a temporary sentinel so that we only have one character
1093 * to test for. Pathnames tend to be short so this should not be
1094 * resulting in cache misses.
1096 nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1];
1097 KASSERT(*nulchar == '\0',
1098 ("%s: expected nul at %p; string [%s]\n", __func__, nulchar,
1101 for (cp = cnp->cn_nameptr; *cp != '/'; cp++) {
1102 KASSERT(*cp != '\0',
1103 ("%s: encountered unexpected nul; string [%s]\n", __func__,
1108 cnp->cn_namelen = cp - cnp->cn_nameptr;
1109 if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
1110 error = ENAMETOOLONG;
1113 prev_ni_pathlen = ndp->ni_pathlen;
1114 ndp->ni_pathlen -= cnp->cn_namelen;
1115 KASSERT(ndp->ni_pathlen <= PATH_MAX,
1116 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
1117 prev_ni_next = ndp->ni_next;
1121 * Something else should be clearing this.
1123 cnp->cn_flags &= ~(ISDOTDOT|ISLASTCN);
1125 cnp->cn_flags |= MAKEENTRY;
1126 if (*cp == '\0' && docache == 0)
1127 cnp->cn_flags &= ~MAKEENTRY;
1128 if (cnp->cn_namelen == 2 &&
1129 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
1130 cnp->cn_flags |= ISDOTDOT;
1131 if (*ndp->ni_next == 0) {
1132 cnp->cn_flags |= ISLASTCN;
1134 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
1135 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))) {
1141 nameicap_tracker_add(ndp, dp);
1144 * Make sure degenerate names don't get here, their handling was
1145 * previously found in this spot.
1147 MPASS(cnp->cn_nameptr[0] != '\0');
1150 * Handle "..": five special cases.
1151 * 0. If doing a capability lookup and lookup_cap_dotdot is
1152 * disabled, return ENOTCAPABLE.
1153 * 1. Return an error if this is the last component of
1154 * the name and the operation is DELETE or RENAME.
1155 * 2. If at root directory (e.g. after chroot)
1156 * or at absolute root directory
1157 * then ignore it so can't get out.
1158 * 3. If this vnode is the root of a mounted
1159 * filesystem, then replace it with the
1160 * vnode which was mounted on so we take the
1161 * .. in the other filesystem.
1162 * 4. If the vnode is the top directory of
1163 * the jail or chroot, don't let them out.
1164 * 5. If doing a capability lookup and lookup_cap_dotdot is
1165 * enabled, return ENOTCAPABLE if the lookup would escape
1166 * from the initial file descriptor directory. Checks are
1167 * done by ensuring that namei() already traversed the
1168 * result of dotdot lookup.
1170 if (cnp->cn_flags & ISDOTDOT) {
1171 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
1172 == NI_LCF_STRICTRELATIVE) {
1174 if (KTRPOINT(curthread, KTR_CAPFAIL))
1175 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1177 error = ENOTCAPABLE;
1180 if ((cnp->cn_flags & ISLASTCN) != 0 &&
1181 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1186 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
1188 if (dp == pr->pr_root)
1190 bool isroot = dp == ndp->ni_rootdir ||
1191 dp == ndp->ni_topdir || dp == rootvnode ||
1193 if (isroot && (ndp->ni_lcf &
1194 NI_LCF_STRICTRELATIVE) != 0) {
1195 error = ENOTCAPABLE;
1198 if (isroot || ((dp->v_vflag & VV_ROOT) != 0 &&
1199 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
1205 if ((dp->v_vflag & VV_ROOT) == 0)
1207 if (VN_IS_DOOMED(dp)) { /* forced unmount */
1212 dp = dp->v_mount->mnt_vnodecovered;
1216 enforce_lkflags(dp->v_mount, cnp->cn_lkflags |
1218 error = nameicap_check_dotdot(ndp, dp);
1222 if (KTRPOINT(curthread, KTR_CAPFAIL))
1223 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1231 * We now have a segment name to search for, and a directory to search.
1235 error = mac_vnode_check_lookup(cnp->cn_cred, dp, cnp);
1236 if (__predict_false(error))
1241 ASSERT_VOP_LOCKED(dp, "lookup");
1243 * If we have a shared lock we may need to upgrade the lock for the
1246 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1247 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1248 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1249 if (VN_IS_DOOMED(dp)) {
1254 * If we're looking up the last component and we need an exclusive
1255 * lock, adjust our lkflags.
1257 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1258 cnp->cn_lkflags = LK_EXCLUSIVE;
1259 lkflags_save = cnp->cn_lkflags;
1260 cnp->cn_lkflags = enforce_lkflags(dp->v_mount, cnp->cn_lkflags);
1261 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1262 cnp->cn_lkflags = lkflags_save;
1264 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1265 if ((error == ENOENT) &&
1266 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1267 (dp->v_mount->mnt_flag & MNT_UNION)) {
1269 dp = dp->v_mount->mnt_vnodecovered;
1273 enforce_lkflags(dp->v_mount, cnp->cn_lkflags |
1275 nameicap_tracker_add(ndp, dp);
1279 if (error == ERELOOKUP) {
1287 if (error != EJUSTRETURN)
1290 * At this point, we know we're at the end of the
1291 * pathname. If creating / renaming, we can consider
1292 * allowing the file or directory to be created / renamed,
1293 * provided we're not on a read-only filesystem.
1299 /* trailing slash only allowed for directories */
1300 if ((cnp->cn_flags & TRAILINGSLASH) &&
1301 !(cnp->cn_flags & WILLBEDIR)) {
1305 if ((cnp->cn_flags & LOCKPARENT) == 0)
1308 * We return with ni_vp NULL to indicate that the entry
1309 * doesn't currently exist, leaving a pointer to the
1310 * (possibly locked) directory vnode in ndp->ni_dvp.
1319 * Check for symbolic link
1321 if ((dp->v_type == VLNK) &&
1322 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1323 *ndp->ni_next == '/')) {
1324 cnp->cn_flags |= ISSYMLINK;
1325 if (VN_IS_DOOMED(dp)) {
1327 * We can't know whether the directory was mounted with
1328 * NOSYMFOLLOW, so we can't follow safely.
1333 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1338 * Symlink code always expects an unlocked dvp.
1340 if (ndp->ni_dvp != ndp->ni_vp) {
1341 VOP_UNLOCK(ndp->ni_dvp);
1342 ni_dvp_unlocked = 1;
1347 if ((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0 &&
1348 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1349 error = vfs_lookup_cross_mount(ndp);
1353 * FALLTHROUGH to nextname
1360 * Not a symbolic link that we will follow. Continue with the
1361 * next component if there is any; otherwise, we're done.
1363 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1364 ("lookup: invalid path state."));
1367 ndp->ni_pathlen = prev_ni_pathlen;
1368 ndp->ni_next = prev_ni_next;
1369 if (ndp->ni_dvp != dp)
1375 if (cnp->cn_flags & ISDOTDOT) {
1376 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1379 if (KTRPOINT(curthread, KTR_CAPFAIL))
1380 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1385 if (*ndp->ni_next == '/') {
1386 cnp->cn_nameptr = ndp->ni_next;
1387 while (*cnp->cn_nameptr == '/') {
1391 if (ndp->ni_dvp != dp)
1398 * If we're processing a path with a trailing slash,
1399 * check that the end result is a directory.
1401 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1406 * Disallow directory write attempts on read-only filesystems.
1409 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1414 ni_dvp_unlocked = 2;
1415 if (ndp->ni_dvp != dp)
1419 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1420 VOP_UNLOCK(ndp->ni_dvp);
1421 ni_dvp_unlocked = 1;
1424 if (cnp->cn_flags & AUDITVNODE1)
1425 AUDIT_ARG_VNODE1(dp);
1426 else if (cnp->cn_flags & AUDITVNODE2)
1427 AUDIT_ARG_VNODE2(dp);
1429 if ((cnp->cn_flags & LOCKLEAF) == 0)
1433 * FIXME: for lookups which only cross a mount point to fetch the
1434 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1435 * if either WANTPARENT or LOCKPARENT is set.
1438 * Because of shared lookup we may have the vnode shared locked, but
1439 * the caller may want it to be exclusively locked.
1441 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1442 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1443 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1444 if (VN_IS_DOOMED(dp)) {
1450 if (ndp->ni_vp != NULL) {
1451 if ((cnp->cn_flags & ISDOTDOT) == 0)
1452 nameicap_tracker_add(ndp, ndp->ni_vp);
1453 if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS)
1454 return (vfs_lookup_failifexists(ndp));
1459 if (ni_dvp_unlocked != 2) {
1460 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1473 * relookup - lookup a path name component
1474 * Used by lookup to re-acquire things.
1477 vfs_relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp,
1480 struct vnode *dp = NULL; /* the directory we are searching */
1481 int rdonly; /* lookup read-only flag bit */
1484 KASSERT(cnp->cn_flags & ISLASTCN,
1485 ("relookup: Not given last component."));
1487 * Setup: break out flag bits into variables.
1489 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0,
1490 ("relookup: parent not wanted"));
1491 rdonly = cnp->cn_flags & RDONLY;
1492 cnp->cn_flags &= ~ISSYMLINK;
1494 cnp->cn_lkflags = LK_EXCLUSIVE;
1495 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1498 * Search a new directory.
1500 * See a comment in vfs_lookup for cnp->cn_nameptr.
1502 * Check for "" which represents the root directory after slash
1505 if (cnp->cn_nameptr[0] == '\0') {
1507 * Support only LOOKUP for "/" because lookup()
1508 * can't succeed for CREATE, DELETE and RENAME.
1510 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1511 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1513 if (!(cnp->cn_flags & LOCKLEAF))
1516 /* XXX This should probably move to the top of function. */
1518 panic("lookup: SAVESTART");
1522 if (cnp->cn_flags & ISDOTDOT)
1523 panic ("relookup: lookup on dot-dot");
1526 * We now have a segment name to search for, and a directory to search.
1528 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1529 KASSERT(*vpp == NULL, ("leaf should be empty"));
1530 if (error != EJUSTRETURN)
1533 * If creating and at end of pathname, then can consider
1534 * allowing file to be created.
1540 /* ASSERT(dvp == ndp->ni_startdir) */
1543 if ((cnp->cn_flags & LOCKPARENT) == 0)
1546 * We return with ni_vp NULL to indicate that the entry
1547 * doesn't currently exist, leaving a pointer to the
1548 * (possibly locked) directory vnode in ndp->ni_dvp.
1556 * Disallow directory write attempts on read-only filesystems.
1559 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1568 * Set the parent lock/ref state to the requested state.
1570 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp)
1573 * Check for symbolic link
1575 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1576 ("relookup: symlink found.\n"));
1578 /* ASSERT(dvp == ndp->ni_startdir) */
1582 if ((cnp->cn_flags & LOCKLEAF) == 0)
1593 * Validate the final state of ndp after the lookup.
1596 NDVALIDATE_impl(struct nameidata *ndp, int line)
1598 struct componentname *cnp;
1601 if (cnp->cn_pnbuf == NULL)
1602 panic("%s: got no buf! called from %d", __func__, line);