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
36 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/capsicum.h>
49 #include <sys/fcntl.h>
52 #include <sys/mutex.h>
53 #include <sys/namei.h>
54 #include <sys/vnode.h>
55 #include <sys/mount.h>
56 #include <sys/filedesc.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
62 #include <sys/ktrace.h>
65 #include <security/audit/audit.h>
66 #include <security/mac/mac_framework.h>
70 #define NAMEI_DIAGNOSTIC 1
71 #undef NAMEI_DIAGNOSTIC
73 SDT_PROVIDER_DECLARE(vfs);
74 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
76 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
78 /* Allocation zone for namei. */
79 uma_zone_t namei_zone;
81 /* Placeholder vnode for mp traversal. */
82 static struct vnode *vp_crossmp;
85 crossmp_vop_islocked(struct vop_islocked_args *ap)
92 crossmp_vop_lock1(struct vop_lock1_args *ap)
95 struct lock *lk __unused;
96 const char *file __unused;
97 int flags, line __unused;
105 if ((flags & LK_SHARED) == 0)
106 panic("invalid lock request for crossmp");
108 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
109 flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
110 WITNESS_LOCK(&lk->lock_object, 0, file, line);
111 if ((flags & LK_INTERLOCK) != 0)
113 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
118 crossmp_vop_unlock(struct vop_unlock_args *ap)
121 struct lock *lk __unused;
128 if ((flags & LK_INTERLOCK) != 0)
130 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
131 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
136 static struct vop_vector crossmp_vnodeops = {
137 .vop_default = &default_vnodeops,
138 .vop_islocked = crossmp_vop_islocked,
139 .vop_lock1 = crossmp_vop_lock1,
140 .vop_unlock = crossmp_vop_unlock,
143 struct nameicap_tracker {
145 TAILQ_ENTRY(nameicap_tracker) nm_link;
148 /* Zone for cap mode tracker elements used for dotdot capability checks. */
149 static uma_zone_t nt_zone;
152 nameiinit(void *dummy __unused)
155 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
157 nt_zone = uma_zcreate("rentr", sizeof(struct nameicap_tracker),
158 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
159 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
161 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
163 static int lookup_cap_dotdot = 1;
164 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
165 &lookup_cap_dotdot, 0,
166 "enables \"..\" components in path lookup in capability mode");
167 static int lookup_cap_dotdot_nonlocal = 1;
168 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
169 &lookup_cap_dotdot_nonlocal, 0,
170 "enables \"..\" components in path lookup in capability mode "
171 "on non-local mount");
174 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
176 struct nameicap_tracker *nt;
178 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
180 nt = uma_zalloc(nt_zone, M_WAITOK);
183 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
187 nameicap_cleanup(struct nameidata *ndp)
189 struct nameicap_tracker *nt, *nt1;
191 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
192 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
193 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
194 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
196 uma_zfree(nt_zone, nt);
201 * For dotdot lookups in capability mode, only allow the component
202 * lookup to succeed if the resulting directory was already traversed
203 * during the operation. Also fail dotdot lookups for non-local
204 * filesystems, where external agents might assist local lookups to
205 * escape the compartment.
208 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
210 struct nameicap_tracker *nt;
213 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
217 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
218 (mp->mnt_flag & MNT_LOCAL) == 0)
219 return (ENOTCAPABLE);
220 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
225 return (ENOTCAPABLE);
229 namei_cleanup_cnp(struct componentname *cnp)
232 uma_zfree(namei_zone, cnp->cn_pnbuf);
234 cnp->cn_pnbuf = NULL;
235 cnp->cn_nameptr = NULL;
240 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
242 struct componentname *cnp;
245 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
247 if (KTRPOINT(curthread, KTR_CAPFAIL))
248 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
250 return (ENOTCAPABLE);
252 while (*(cnp->cn_nameptr) == '/') {
256 *dpp = ndp->ni_rootdir;
262 * Convert a pathname into a pointer to a locked vnode.
264 * The FOLLOW flag is set when symbolic links are to be followed
265 * when they occur at the end of the name translation process.
266 * Symbolic links are always followed for all other pathname
267 * components other than the last.
269 * The segflg defines whether the name is to be copied from user
270 * space or kernel space.
272 * Overall outline of namei:
275 * get starting directory
276 * while (!done && !error) {
277 * call lookup to search path.
278 * if symbolic link, massage name in buffer and continue
282 namei(struct nameidata *ndp)
284 struct filedesc *fdp; /* pointer to file descriptor state */
285 char *cp; /* pointer into pathname argument */
286 struct vnode *dp; /* the directory we are searching */
287 struct iovec aiov; /* uio for reading symbolic links */
288 struct componentname *cnp;
293 int error, linklen, startdir_used;
298 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
299 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
300 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
301 ("namei: nameiop contaminated with flags"));
302 KASSERT((cnp->cn_flags & OPMASK) == 0,
303 ("namei: flags contaminated with nameiops"));
304 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
305 ndp->ni_startdir->v_type == VBAD);
307 TAILQ_INIT(&ndp->ni_cap_tracker);
310 /* We will set this ourselves if we need it. */
311 cnp->cn_flags &= ~TRAILINGSLASH;
314 * Get a buffer for the name to be translated, and copy the
315 * name into the buffer.
317 if ((cnp->cn_flags & HASBUF) == 0)
318 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
319 if (ndp->ni_segflg == UIO_SYSSPACE)
320 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
323 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
327 * Don't allow empty pathnames.
329 if (error == 0 && *cnp->cn_pnbuf == '\0')
332 #ifdef CAPABILITY_MODE
334 * In capability mode, lookups must be restricted to happen in
335 * the subtree with the root specified by the file descriptor:
336 * - The root must be real file descriptor, not the pseudo-descriptor
338 * - The passed path must be relative and not absolute.
339 * - If lookup_cap_dotdot is disabled, path must not contain the
341 * - If lookup_cap_dotdot is enabled, we verify that all '..'
342 * components lookups result in the directories which were
343 * previously walked by us, which prevents an escape from
346 if (error == 0 && IN_CAPABILITY_MODE(td) &&
347 (cnp->cn_flags & NOCAPCHECK) == 0) {
348 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
349 if (ndp->ni_dirfd == AT_FDCWD) {
351 if (KTRPOINT(td, KTR_CAPFAIL))
352 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
359 namei_cleanup_cnp(cnp);
365 if (KTRPOINT(td, KTR_NAMEI)) {
366 KASSERT(cnp->cn_thread == curthread,
367 ("namei not using curthread"));
368 ktrnamei(cnp->cn_pnbuf);
372 * Get starting point for the translation.
375 ndp->ni_rootdir = fdp->fd_rdir;
376 vrefact(ndp->ni_rootdir);
377 ndp->ni_topdir = fdp->fd_jdir;
380 * If we are auditing the kernel pathname, save the user pathname.
382 if (cnp->cn_flags & AUDITVNODE1)
383 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
384 if (cnp->cn_flags & AUDITVNODE2)
385 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
389 cnp->cn_nameptr = cnp->cn_pnbuf;
390 if (cnp->cn_pnbuf[0] == '/') {
391 error = namei_handle_root(ndp, &dp);
393 if (ndp->ni_startdir != NULL) {
394 dp = ndp->ni_startdir;
396 } else if (ndp->ni_dirfd == AT_FDCWD) {
400 rights = ndp->ni_rightsneeded;
401 cap_rights_set(&rights, CAP_LOOKUP);
403 if (cnp->cn_flags & AUDITVNODE1)
404 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
405 if (cnp->cn_flags & AUDITVNODE2)
406 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
407 error = fgetvp_rights(td, ndp->ni_dirfd,
408 &rights, &ndp->ni_filecaps, &dp);
413 * If file descriptor doesn't have all rights,
414 * all lookups relative to it must also be
418 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
420 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
421 ndp->ni_filecaps.fc_nioctls != -1) {
422 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
426 if (error == 0 && dp->v_type != VDIR)
429 FILEDESC_SUNLOCK(fdp);
430 if (ndp->ni_startdir != NULL && !startdir_used)
431 vrele(ndp->ni_startdir);
437 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
438 lookup_cap_dotdot != 0)
439 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
440 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
443 ndp->ni_startdir = dp;
448 * If not a symbolic link, we're done.
450 if ((cnp->cn_flags & ISSYMLINK) == 0) {
451 vrele(ndp->ni_rootdir);
452 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
453 namei_cleanup_cnp(cnp);
455 cnp->cn_flags |= HASBUF;
456 nameicap_cleanup(ndp);
457 SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp);
460 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
465 if ((cnp->cn_flags & NOMACCHECK) == 0) {
466 error = mac_vnode_check_readlink(td->td_ucred,
472 if (ndp->ni_pathlen > 1)
473 cp = uma_zalloc(namei_zone, M_WAITOK);
477 aiov.iov_len = MAXPATHLEN;
478 auio.uio_iov = &aiov;
481 auio.uio_rw = UIO_READ;
482 auio.uio_segflg = UIO_SYSSPACE;
484 auio.uio_resid = MAXPATHLEN;
485 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
487 if (ndp->ni_pathlen > 1)
488 uma_zfree(namei_zone, cp);
491 linklen = MAXPATHLEN - auio.uio_resid;
493 if (ndp->ni_pathlen > 1)
494 uma_zfree(namei_zone, cp);
498 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
499 if (ndp->ni_pathlen > 1)
500 uma_zfree(namei_zone, cp);
501 error = ENAMETOOLONG;
504 if (ndp->ni_pathlen > 1) {
505 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
506 uma_zfree(namei_zone, cnp->cn_pnbuf);
509 cnp->cn_pnbuf[linklen] = '\0';
510 ndp->ni_pathlen += linklen;
514 * Check if root directory should replace current directory.
516 cnp->cn_nameptr = cnp->cn_pnbuf;
517 if (*(cnp->cn_nameptr) == '/') {
519 error = namei_handle_root(ndp, &dp);
528 vrele(ndp->ni_rootdir);
529 namei_cleanup_cnp(cnp);
530 nameicap_cleanup(ndp);
531 SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
536 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
539 if (mp == NULL || ((lkflags & LK_SHARED) &&
540 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
541 ((cnflags & ISDOTDOT) &&
542 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
543 lkflags &= ~LK_SHARED;
544 lkflags |= LK_EXCLUSIVE;
546 lkflags |= LK_NODDLKTREAT;
551 needs_exclusive_leaf(struct mount *mp, int flags)
555 * Intermediate nodes can use shared locks, we only need to
556 * force an exclusive lock for leaf nodes.
558 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
561 /* Always use exclusive locks if LOCKSHARED isn't set. */
562 if (!(flags & LOCKSHARED))
566 * For lookups during open(), if the mount point supports
567 * extended shared operations, then use a shared lock for the
568 * leaf node, otherwise use an exclusive lock.
570 if ((flags & ISOPEN) != 0)
571 return (!MNT_EXTENDED_SHARED(mp));
574 * Lookup requests outside of open() that specify LOCKSHARED
575 * only need a shared lock on the leaf vnode.
582 * This is a very central and rather complicated routine.
584 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
585 * The starting directory is taken from ni_startdir. The pathname is
586 * descended until done, or a symbolic link is encountered. The variable
587 * ni_more is clear if the path is completed; it is set to one if a
588 * symbolic link needing interpretation is encountered.
590 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
591 * whether the name is to be looked up, created, renamed, or deleted.
592 * When CREATE, RENAME, or DELETE is specified, information usable in
593 * creating, renaming, or deleting a directory entry may be calculated.
594 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
595 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
596 * returned unlocked. Otherwise the parent directory is not returned. If
597 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
598 * the target is returned locked, otherwise it is returned unlocked.
599 * When creating or renaming and LOCKPARENT is specified, the target may not
600 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
602 * Overall outline of lookup:
605 * identify next component of name at ndp->ni_ptr
606 * handle degenerate case where name is null string
607 * if .. and crossing mount points and on mounted filesys, find parent
608 * call VOP_LOOKUP routine for next component name
609 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
610 * component vnode returned in ni_vp (if it exists), locked.
611 * if result vnode is mounted on and crossing mount points,
612 * find mounted on vnode
613 * if more components of name, do next level at dirloop
614 * return the answer in ni_vp, locked if LOCKLEAF set
615 * if LOCKPARENT set, return locked parent in ni_dvp
616 * if WANTPARENT set, return unlocked parent in ni_dvp
619 lookup(struct nameidata *ndp)
621 char *cp; /* pointer into pathname argument */
622 char *prev_ni_next; /* saved ndp->ni_next */
623 struct vnode *dp = NULL; /* the directory we are searching */
624 struct vnode *tdp; /* saved dp */
625 struct mount *mp; /* mount table entry */
627 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
628 int docache; /* == 0 do not cache last component */
629 int wantparent; /* 1 => wantparent or lockparent flag */
630 int rdonly; /* lookup read-only flag bit */
632 int dpunlocked = 0; /* dp has already been unlocked */
633 int relookup = 0; /* do not consume the path component */
634 struct componentname *cnp = &ndp->ni_cnd;
639 * Setup: break out flag bits into variables.
642 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
643 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
644 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
645 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
646 if (cnp->cn_nameiop == DELETE ||
647 (wantparent && cnp->cn_nameiop != CREATE &&
648 cnp->cn_nameiop != LOOKUP))
650 rdonly = cnp->cn_flags & RDONLY;
651 cnp->cn_flags &= ~ISSYMLINK;
654 * We use shared locks until we hit the parent of the last cn then
655 * we adjust based on the requesting flags.
657 cnp->cn_lkflags = LK_SHARED;
658 dp = ndp->ni_startdir;
659 ndp->ni_startdir = NULLVP;
661 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
666 * Search a new directory.
668 * The last component of the filename is left accessible via
669 * cnp->cn_nameptr for callers that need the name. Callers needing
670 * the name set the SAVENAME flag. When done, they assume
671 * responsibility for freeing the pathname buffer.
673 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
675 cnp->cn_namelen = cp - cnp->cn_nameptr;
676 if (cnp->cn_namelen > NAME_MAX) {
677 error = ENAMETOOLONG;
680 #ifdef NAMEI_DIAGNOSTIC
683 printf("{%s}: ", cnp->cn_nameptr);
686 prev_ni_pathlen = ndp->ni_pathlen;
687 ndp->ni_pathlen -= cnp->cn_namelen;
688 KASSERT(ndp->ni_pathlen <= PATH_MAX,
689 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
690 prev_ni_next = ndp->ni_next;
694 * Replace multiple slashes by a single slash and trailing slashes
695 * by a null. This must be done before VOP_LOOKUP() because some
696 * fs's don't know about trailing slashes. Remember if there were
697 * trailing slashes to handle symlinks, existing non-directories
698 * and non-existing files that won't be directories specially later.
700 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
704 *ndp->ni_next = '\0';
705 cnp->cn_flags |= TRAILINGSLASH;
710 cnp->cn_flags |= MAKEENTRY;
711 if (*cp == '\0' && docache == 0)
712 cnp->cn_flags &= ~MAKEENTRY;
713 if (cnp->cn_namelen == 2 &&
714 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
715 cnp->cn_flags |= ISDOTDOT;
717 cnp->cn_flags &= ~ISDOTDOT;
718 if (*ndp->ni_next == 0)
719 cnp->cn_flags |= ISLASTCN;
721 cnp->cn_flags &= ~ISLASTCN;
723 if ((cnp->cn_flags & ISLASTCN) != 0 &&
724 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
725 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
730 nameicap_tracker_add(ndp, dp);
733 * Check for degenerate name (e.g. / or "")
734 * which is a way of talking about a directory,
735 * e.g. like "/." or ".".
737 if (cnp->cn_nameptr[0] == '\0') {
738 if (dp->v_type != VDIR) {
742 if (cnp->cn_nameiop != LOOKUP) {
752 if (cnp->cn_flags & AUDITVNODE1)
753 AUDIT_ARG_VNODE1(dp);
754 else if (cnp->cn_flags & AUDITVNODE2)
755 AUDIT_ARG_VNODE2(dp);
757 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
759 /* XXX This should probably move to the top of function. */
760 if (cnp->cn_flags & SAVESTART)
761 panic("lookup: SAVESTART");
766 * Handle "..": five special cases.
767 * 0. If doing a capability lookup and lookup_cap_dotdot is
768 * disabled, return ENOTCAPABLE.
769 * 1. Return an error if this is the last component of
770 * the name and the operation is DELETE or RENAME.
771 * 2. If at root directory (e.g. after chroot)
772 * or at absolute root directory
773 * then ignore it so can't get out.
774 * 3. If this vnode is the root of a mounted
775 * filesystem, then replace it with the
776 * vnode which was mounted on so we take the
777 * .. in the other filesystem.
778 * 4. If the vnode is the top directory of
779 * the jail or chroot, don't let them out.
780 * 5. If doing a capability lookup and lookup_cap_dotdot is
781 * enabled, return ENOTCAPABLE if the lookup would escape
782 * from the initial file descriptor directory. Checks are
783 * done by ensuring that namei() already traversed the
784 * result of dotdot lookup.
786 if (cnp->cn_flags & ISDOTDOT) {
787 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
788 == NI_LCF_STRICTRELATIVE) {
790 if (KTRPOINT(curthread, KTR_CAPFAIL))
791 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
796 if ((cnp->cn_flags & ISLASTCN) != 0 &&
797 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
802 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
804 if (dp == pr->pr_root)
806 if (dp == ndp->ni_rootdir ||
807 dp == ndp->ni_topdir ||
810 ((dp->v_vflag & VV_ROOT) != 0 &&
811 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
817 if ((dp->v_vflag & VV_ROOT) == 0)
819 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
824 dp = dp->v_mount->mnt_vnodecovered;
828 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
829 LK_RETRY, ISDOTDOT));
830 error = nameicap_check_dotdot(ndp, dp);
833 if (KTRPOINT(curthread, KTR_CAPFAIL))
834 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
842 * We now have a segment name to search for, and a directory to search.
846 if ((cnp->cn_flags & NOMACCHECK) == 0) {
847 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
855 ASSERT_VOP_LOCKED(dp, "lookup");
857 * If we have a shared lock we may need to upgrade the lock for the
860 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
861 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
862 vn_lock(dp, LK_UPGRADE|LK_RETRY);
863 if ((dp->v_iflag & VI_DOOMED) != 0) {
868 * If we're looking up the last component and we need an exclusive
869 * lock, adjust our lkflags.
871 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
872 cnp->cn_lkflags = LK_EXCLUSIVE;
873 #ifdef NAMEI_DIAGNOSTIC
874 vn_printf(dp, "lookup in ");
876 lkflags_save = cnp->cn_lkflags;
877 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
879 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
880 cnp->cn_lkflags = lkflags_save;
882 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
883 #ifdef NAMEI_DIAGNOSTIC
884 printf("not found\n");
886 if ((error == ENOENT) &&
887 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
888 (dp->v_mount->mnt_flag & MNT_UNION)) {
890 dp = dp->v_mount->mnt_vnodecovered;
894 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
895 LK_RETRY, cnp->cn_flags));
896 nameicap_tracker_add(ndp, dp);
900 if (error == ERELOOKUP) {
908 if (error != EJUSTRETURN)
911 * At this point, we know we're at the end of the
912 * pathname. If creating / renaming, we can consider
913 * allowing the file or directory to be created / renamed,
914 * provided we're not on a read-only filesystem.
920 /* trailing slash only allowed for directories */
921 if ((cnp->cn_flags & TRAILINGSLASH) &&
922 !(cnp->cn_flags & WILLBEDIR)) {
926 if ((cnp->cn_flags & LOCKPARENT) == 0)
929 * We return with ni_vp NULL to indicate that the entry
930 * doesn't currently exist, leaving a pointer to the
931 * (possibly locked) directory vnode in ndp->ni_dvp.
933 if (cnp->cn_flags & SAVESTART) {
934 ndp->ni_startdir = ndp->ni_dvp;
935 VREF(ndp->ni_startdir);
941 #ifdef NAMEI_DIAGNOSTIC
947 * Check to see if the vnode has been mounted on;
948 * if so find the root of the mounted filesystem.
950 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
951 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
955 if (dp != ndp->ni_dvp)
960 ndp->ni_dvp = vp_crossmp;
961 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
962 cnp->cn_flags), &tdp);
964 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
965 panic("vp_crossmp exclusively locked or reclaimed");
970 ndp->ni_vp = dp = tdp;
974 * Check for symbolic link
976 if ((dp->v_type == VLNK) &&
977 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
978 *ndp->ni_next == '/')) {
979 cnp->cn_flags |= ISSYMLINK;
980 if (dp->v_iflag & VI_DOOMED) {
982 * We can't know whether the directory was mounted with
983 * NOSYMFOLLOW, so we can't follow safely.
988 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
993 * Symlink code always expects an unlocked dvp.
995 if (ndp->ni_dvp != ndp->ni_vp) {
996 VOP_UNLOCK(ndp->ni_dvp, 0);
1004 * Not a symbolic link that we will follow. Continue with the
1005 * next component if there is any; otherwise, we're done.
1007 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1008 ("lookup: invalid path state."));
1011 ndp->ni_pathlen = prev_ni_pathlen;
1012 ndp->ni_next = prev_ni_next;
1013 if (ndp->ni_dvp != dp)
1019 if (cnp->cn_flags & ISDOTDOT) {
1020 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1023 if (KTRPOINT(curthread, KTR_CAPFAIL))
1024 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1029 if (*ndp->ni_next == '/') {
1030 cnp->cn_nameptr = ndp->ni_next;
1031 while (*cnp->cn_nameptr == '/') {
1035 if (ndp->ni_dvp != dp)
1042 * If we're processing a path with a trailing slash,
1043 * check that the end result is a directory.
1045 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1050 * Disallow directory write attempts on read-only filesystems.
1053 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1057 if (cnp->cn_flags & SAVESTART) {
1058 ndp->ni_startdir = ndp->ni_dvp;
1059 VREF(ndp->ni_startdir);
1062 ni_dvp_unlocked = 2;
1063 if (ndp->ni_dvp != dp)
1067 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1068 VOP_UNLOCK(ndp->ni_dvp, 0);
1069 ni_dvp_unlocked = 1;
1072 if (cnp->cn_flags & AUDITVNODE1)
1073 AUDIT_ARG_VNODE1(dp);
1074 else if (cnp->cn_flags & AUDITVNODE2)
1075 AUDIT_ARG_VNODE2(dp);
1077 if ((cnp->cn_flags & LOCKLEAF) == 0)
1081 * Because of shared lookup we may have the vnode shared locked, but
1082 * the caller may want it to be exclusively locked.
1084 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1085 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1086 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1087 if (dp->v_iflag & VI_DOOMED) {
1095 if (ni_dvp_unlocked != 2) {
1096 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1109 * relookup - lookup a path name component
1110 * Used by lookup to re-acquire things.
1113 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1115 struct vnode *dp = NULL; /* the directory we are searching */
1116 int wantparent; /* 1 => wantparent or lockparent flag */
1117 int rdonly; /* lookup read-only flag bit */
1120 KASSERT(cnp->cn_flags & ISLASTCN,
1121 ("relookup: Not given last component."));
1123 * Setup: break out flag bits into variables.
1125 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1126 KASSERT(wantparent, ("relookup: parent not wanted."));
1127 rdonly = cnp->cn_flags & RDONLY;
1128 cnp->cn_flags &= ~ISSYMLINK;
1130 cnp->cn_lkflags = LK_EXCLUSIVE;
1131 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1134 * Search a new directory.
1136 * The last component of the filename is left accessible via
1137 * cnp->cn_nameptr for callers that need the name. Callers needing
1138 * the name set the SAVENAME flag. When done, they assume
1139 * responsibility for freeing the pathname buffer.
1141 #ifdef NAMEI_DIAGNOSTIC
1142 printf("{%s}: ", cnp->cn_nameptr);
1146 * Check for "" which represents the root directory after slash
1149 if (cnp->cn_nameptr[0] == '\0') {
1151 * Support only LOOKUP for "/" because lookup()
1152 * can't succeed for CREATE, DELETE and RENAME.
1154 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1155 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1157 if (!(cnp->cn_flags & LOCKLEAF))
1160 /* XXX This should probably move to the top of function. */
1161 if (cnp->cn_flags & SAVESTART)
1162 panic("lookup: SAVESTART");
1166 if (cnp->cn_flags & ISDOTDOT)
1167 panic ("relookup: lookup on dot-dot");
1170 * We now have a segment name to search for, and a directory to search.
1172 #ifdef NAMEI_DIAGNOSTIC
1173 vn_printf(dp, "search in ");
1175 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1176 KASSERT(*vpp == NULL, ("leaf should be empty"));
1177 if (error != EJUSTRETURN)
1180 * If creating and at end of pathname, then can consider
1181 * allowing file to be created.
1187 /* ASSERT(dvp == ndp->ni_startdir) */
1188 if (cnp->cn_flags & SAVESTART)
1190 if ((cnp->cn_flags & LOCKPARENT) == 0)
1193 * We return with ni_vp NULL to indicate that the entry
1194 * doesn't currently exist, leaving a pointer to the
1195 * (possibly locked) directory vnode in ndp->ni_dvp.
1203 * Disallow directory write attempts on read-only filesystems.
1206 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1215 * Set the parent lock/ref state to the requested state.
1217 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1222 } else if (!wantparent)
1225 * Check for symbolic link
1227 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1228 ("relookup: symlink found.\n"));
1230 /* ASSERT(dvp == ndp->ni_startdir) */
1231 if (cnp->cn_flags & SAVESTART)
1234 if ((cnp->cn_flags & LOCKLEAF) == 0)
1244 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1245 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1249 ndp->ni_cnd.cn_nameiop = op;
1250 ndp->ni_cnd.cn_flags = flags;
1251 ndp->ni_segflg = segflg;
1252 ndp->ni_dirp = namep;
1253 ndp->ni_dirfd = dirfd;
1254 ndp->ni_startdir = startdir;
1255 if (rightsp != NULL)
1256 ndp->ni_rightsneeded = *rightsp;
1258 cap_rights_init(&ndp->ni_rightsneeded);
1259 filecaps_init(&ndp->ni_filecaps);
1260 ndp->ni_cnd.cn_thread = td;
1264 * Free data allocated by namei(); see namei(9) for details.
1267 NDFREE(struct nameidata *ndp, const u_int flags)
1275 if (!(flags & NDF_NO_FREE_PNBUF) &&
1276 (ndp->ni_cnd.cn_flags & HASBUF)) {
1277 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1278 ndp->ni_cnd.cn_flags &= ~HASBUF;
1280 if (!(flags & NDF_NO_VP_UNLOCK) &&
1281 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1283 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1292 VOP_UNLOCK(ndp->ni_vp, 0);
1293 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1294 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1295 ndp->ni_dvp != ndp->ni_vp)
1297 if (!(flags & NDF_NO_DVP_RELE) &&
1298 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1307 VOP_UNLOCK(ndp->ni_dvp, 0);
1308 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1309 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1310 vrele(ndp->ni_startdir);
1311 ndp->ni_startdir = NULL;
1316 * Determine if there is a suitable alternate filename under the specified
1317 * prefix for the specified path. If the create flag is set, then the
1318 * alternate prefix will be used so long as the parent directory exists.
1319 * This is used by the various compatibility ABIs so that Linux binaries prefer
1320 * files under /compat/linux for example. The chosen path (whether under
1321 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1322 * to by pathbuf. The caller is responsible for free'ing the buffer from
1323 * the M_TEMP bucket if one is returned.
1326 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1327 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1329 struct nameidata nd, ndroot;
1330 char *ptr, *buf, *cp;
1334 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1337 /* Copy the prefix into the new pathname as a starting point. */
1338 len = strlcpy(buf, prefix, MAXPATHLEN);
1339 if (len >= MAXPATHLEN) {
1344 sz = MAXPATHLEN - len;
1347 /* Append the filename to the prefix. */
1348 if (pathseg == UIO_SYSSPACE)
1349 error = copystr(path, ptr, sz, &len);
1351 error = copyinstr(path, ptr, sz, &len);
1359 /* Only use a prefix with absolute pathnames. */
1365 if (dirfd != AT_FDCWD) {
1367 * We want the original because the "prefix" is
1368 * included in the already opened dirfd.
1370 bcopy(ptr, buf, len);
1375 * We know that there is a / somewhere in this pathname.
1376 * Search backwards for it, to find the file's parent dir
1377 * to see if it exists in the alternate tree. If it does,
1378 * and we want to create a file (cflag is set). We don't
1379 * need to worry about the root comparison in this case.
1383 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1386 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1392 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1399 * We now compare the vnode of the prefix to the one
1400 * vnode asked. If they resolve to be the same, then we
1401 * ignore the match so that the real root gets used.
1402 * This avoids the problem of traversing "../.." to find the
1403 * root directory and never finding it, because "/" resolves
1404 * to the emulation root directory. This is expensive :-(
1406 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1409 /* We shouldn't ever get an error from this namei(). */
1410 error = namei(&ndroot);
1412 if (nd.ni_vp == ndroot.ni_vp)
1415 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1416 vrele(ndroot.ni_vp);
1420 NDFREE(&nd, NDF_ONLY_PNBUF);
1424 /* If there was an error, use the original path name. */
1426 bcopy(ptr, buf, len);