2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/capsicum.h>
47 #include <sys/fcntl.h>
50 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/filedesc.h>
57 #include <sys/syscallsubr.h>
58 #include <sys/sysctl.h>
60 #include <sys/ktrace.h>
63 #include <security/audit/audit.h>
64 #include <security/mac/mac_framework.h>
68 #define NAMEI_DIAGNOSTIC 1
69 #undef NAMEI_DIAGNOSTIC
71 SDT_PROVIDER_DECLARE(vfs);
72 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *",
74 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *");
76 /* Allocation zone for namei. */
77 uma_zone_t namei_zone;
79 /* Placeholder vnode for mp traversal. */
80 static struct vnode *vp_crossmp;
83 crossmp_vop_islocked(struct vop_islocked_args *ap)
90 crossmp_vop_lock1(struct vop_lock1_args *ap)
103 if ((flags & LK_SHARED) == 0)
104 panic("invalid lock request for crossmp");
106 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
107 flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
108 WITNESS_LOCK(&lk->lock_object, 0, file, line);
109 if ((flags & LK_INTERLOCK) != 0)
111 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
116 crossmp_vop_unlock(struct vop_unlock_args *ap)
126 if ((flags & LK_INTERLOCK) != 0)
128 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
129 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
134 static struct vop_vector crossmp_vnodeops = {
135 .vop_islocked = crossmp_vop_islocked,
136 .vop_lock1 = crossmp_vop_lock1,
137 .vop_unlock = crossmp_vop_unlock,
140 struct nameicap_tracker {
142 TAILQ_ENTRY(nameicap_tracker) nm_link;
145 /* Zone for cap mode tracker elements used for dotdot capability checks. */
146 static uma_zone_t nt_zone;
149 nameiinit(void *dummy __unused)
152 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
154 nt_zone = uma_zcreate("rentr", sizeof(struct nameicap_tracker),
155 NULL, NULL, NULL, NULL, sizeof(void *), 0);
156 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
158 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
160 static int lookup_shared = 1;
161 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RWTUN, &lookup_shared, 0,
162 "enables shared locks for path name translation");
165 * Intent is that lookup_cap_dotdot becomes unconditionally enabled,
166 * but it defaults to the disabled state until verification efforts
169 static int lookup_cap_dotdot = 0;
170 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
171 &lookup_cap_dotdot, 0,
172 "enables \"..\" components in path lookup in capability mode");
173 static int lookup_cap_dotdot_nonlocal = 0;
174 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
175 &lookup_cap_dotdot_nonlocal, 0,
176 "enables \"..\" components in path lookup in capability mode "
177 "on non-local mount");
180 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
182 struct nameicap_tracker *nt;
184 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
186 nt = uma_zalloc(nt_zone, M_WAITOK);
189 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
193 nameicap_cleanup(struct nameidata *ndp)
195 struct nameicap_tracker *nt, *nt1;
197 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
198 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
199 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
200 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
202 uma_zfree(nt_zone, nt);
207 * For dotdot lookups in capability mode, only allow the component
208 * lookup to succeed if the resulting directory was already traversed
209 * during the operation. Also fail dotdot lookups for non-local
210 * filesystems, where external agents might assist local lookups to
211 * escape the compartment.
214 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
216 struct nameicap_tracker *nt;
219 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
223 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
224 (mp->mnt_flag & MNT_LOCAL) == 0)
225 return (ENOTCAPABLE);
226 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
231 return (ENOTCAPABLE);
235 namei_cleanup_cnp(struct componentname *cnp)
238 uma_zfree(namei_zone, cnp->cn_pnbuf);
240 cnp->cn_pnbuf = NULL;
241 cnp->cn_nameptr = NULL;
246 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
248 struct componentname *cnp;
251 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
253 if (KTRPOINT(curthread, KTR_CAPFAIL))
254 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
256 return (ENOTCAPABLE);
258 while (*(cnp->cn_nameptr) == '/') {
262 *dpp = ndp->ni_rootdir;
268 * Convert a pathname into a pointer to a locked vnode.
270 * The FOLLOW flag is set when symbolic links are to be followed
271 * when they occur at the end of the name translation process.
272 * Symbolic links are always followed for all other pathname
273 * components other than the last.
275 * The segflg defines whether the name is to be copied from user
276 * space or kernel space.
278 * Overall outline of namei:
281 * get starting directory
282 * while (!done && !error) {
283 * call lookup to search path.
284 * if symbolic link, massage name in buffer and continue
288 namei(struct nameidata *ndp)
290 struct filedesc *fdp; /* pointer to file descriptor state */
291 char *cp; /* pointer into pathname argument */
292 struct vnode *dp; /* the directory we are searching */
293 struct iovec aiov; /* uio for reading symbolic links */
294 struct componentname *cnp;
299 int error, linklen, startdir_used;
304 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
305 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
306 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
307 ("namei: nameiop contaminated with flags"));
308 KASSERT((cnp->cn_flags & OPMASK) == 0,
309 ("namei: flags contaminated with nameiops"));
310 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
311 ndp->ni_startdir->v_type == VBAD);
313 cnp->cn_flags &= ~LOCKSHARED;
315 TAILQ_INIT(&ndp->ni_cap_tracker);
318 /* We will set this ourselves if we need it. */
319 cnp->cn_flags &= ~TRAILINGSLASH;
322 * Get a buffer for the name to be translated, and copy the
323 * name into the buffer.
325 if ((cnp->cn_flags & HASBUF) == 0)
326 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
327 if (ndp->ni_segflg == UIO_SYSSPACE)
328 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
331 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
335 * Don't allow empty pathnames.
337 if (error == 0 && *cnp->cn_pnbuf == '\0')
340 #ifdef CAPABILITY_MODE
342 * In capability mode, lookups must be restricted to happen in
343 * the subtree with the root specified by the file descriptor:
344 * - The root must be real file descriptor, not the pseudo-descriptor
346 * - The passed path must be relative and not absolute.
347 * - If lookup_cap_dotdot is disabled, path must not contain the
349 * - If lookup_cap_dotdot is enabled, we verify that all '..'
350 * components lookups result in the directories which were
351 * previously walked by us, which prevents an escape from
354 if (error == 0 && IN_CAPABILITY_MODE(td) &&
355 (cnp->cn_flags & NOCAPCHECK) == 0) {
356 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
357 if (ndp->ni_dirfd == AT_FDCWD) {
359 if (KTRPOINT(td, KTR_CAPFAIL))
360 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
367 namei_cleanup_cnp(cnp);
373 if (KTRPOINT(td, KTR_NAMEI)) {
374 KASSERT(cnp->cn_thread == curthread,
375 ("namei not using curthread"));
376 ktrnamei(cnp->cn_pnbuf);
380 * Get starting point for the translation.
383 ndp->ni_rootdir = fdp->fd_rdir;
384 VREF(ndp->ni_rootdir);
385 ndp->ni_topdir = fdp->fd_jdir;
388 * If we are auditing the kernel pathname, save the user pathname.
390 if (cnp->cn_flags & AUDITVNODE1)
391 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
392 if (cnp->cn_flags & AUDITVNODE2)
393 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
397 cnp->cn_nameptr = cnp->cn_pnbuf;
398 if (cnp->cn_pnbuf[0] == '/') {
399 error = namei_handle_root(ndp, &dp);
401 if (ndp->ni_startdir != NULL) {
402 dp = ndp->ni_startdir;
404 } else if (ndp->ni_dirfd == AT_FDCWD) {
408 rights = ndp->ni_rightsneeded;
409 cap_rights_set(&rights, CAP_LOOKUP);
411 if (cnp->cn_flags & AUDITVNODE1)
412 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
413 if (cnp->cn_flags & AUDITVNODE2)
414 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
415 error = fgetvp_rights(td, ndp->ni_dirfd,
416 &rights, &ndp->ni_filecaps, &dp);
421 * If file descriptor doesn't have all rights,
422 * all lookups relative to it must also be
426 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
428 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
429 ndp->ni_filecaps.fc_nioctls != -1) {
430 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
434 if (error == 0 && dp->v_type != VDIR)
437 FILEDESC_SUNLOCK(fdp);
438 if (ndp->ni_startdir != NULL && !startdir_used)
439 vrele(ndp->ni_startdir);
445 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
446 lookup_cap_dotdot != 0)
447 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
448 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
451 ndp->ni_startdir = dp;
456 * If not a symbolic link, we're done.
458 if ((cnp->cn_flags & ISSYMLINK) == 0) {
459 vrele(ndp->ni_rootdir);
460 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
461 namei_cleanup_cnp(cnp);
463 cnp->cn_flags |= HASBUF;
464 nameicap_cleanup(ndp);
465 SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp);
468 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
473 if ((cnp->cn_flags & NOMACCHECK) == 0) {
474 error = mac_vnode_check_readlink(td->td_ucred,
480 if (ndp->ni_pathlen > 1)
481 cp = uma_zalloc(namei_zone, M_WAITOK);
485 aiov.iov_len = MAXPATHLEN;
486 auio.uio_iov = &aiov;
489 auio.uio_rw = UIO_READ;
490 auio.uio_segflg = UIO_SYSSPACE;
492 auio.uio_resid = MAXPATHLEN;
493 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
495 if (ndp->ni_pathlen > 1)
496 uma_zfree(namei_zone, cp);
499 linklen = MAXPATHLEN - auio.uio_resid;
501 if (ndp->ni_pathlen > 1)
502 uma_zfree(namei_zone, cp);
506 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
507 if (ndp->ni_pathlen > 1)
508 uma_zfree(namei_zone, cp);
509 error = ENAMETOOLONG;
512 if (ndp->ni_pathlen > 1) {
513 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
514 uma_zfree(namei_zone, cnp->cn_pnbuf);
517 cnp->cn_pnbuf[linklen] = '\0';
518 ndp->ni_pathlen += linklen;
522 * Check if root directory should replace current directory.
524 cnp->cn_nameptr = cnp->cn_pnbuf;
525 if (*(cnp->cn_nameptr) == '/') {
527 error = namei_handle_root(ndp, &dp);
536 vrele(ndp->ni_rootdir);
537 namei_cleanup_cnp(cnp);
538 nameicap_cleanup(ndp);
539 SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
544 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
547 if (mp == NULL || ((lkflags & LK_SHARED) &&
548 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
549 ((cnflags & ISDOTDOT) &&
550 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
551 lkflags &= ~LK_SHARED;
552 lkflags |= LK_EXCLUSIVE;
554 lkflags |= LK_NODDLKTREAT;
559 needs_exclusive_leaf(struct mount *mp, int flags)
563 * Intermediate nodes can use shared locks, we only need to
564 * force an exclusive lock for leaf nodes.
566 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
569 /* Always use exclusive locks if LOCKSHARED isn't set. */
570 if (!(flags & LOCKSHARED))
574 * For lookups during open(), if the mount point supports
575 * extended shared operations, then use a shared lock for the
576 * leaf node, otherwise use an exclusive lock.
578 if ((flags & ISOPEN) != 0)
579 return (!MNT_EXTENDED_SHARED(mp));
582 * Lookup requests outside of open() that specify LOCKSHARED
583 * only need a shared lock on the leaf vnode.
590 * This is a very central and rather complicated routine.
592 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
593 * The starting directory is taken from ni_startdir. The pathname is
594 * descended until done, or a symbolic link is encountered. The variable
595 * ni_more is clear if the path is completed; it is set to one if a
596 * symbolic link needing interpretation is encountered.
598 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
599 * whether the name is to be looked up, created, renamed, or deleted.
600 * When CREATE, RENAME, or DELETE is specified, information usable in
601 * creating, renaming, or deleting a directory entry may be calculated.
602 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
603 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
604 * returned unlocked. Otherwise the parent directory is not returned. If
605 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
606 * the target is returned locked, otherwise it is returned unlocked.
607 * When creating or renaming and LOCKPARENT is specified, the target may not
608 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
610 * Overall outline of lookup:
613 * identify next component of name at ndp->ni_ptr
614 * handle degenerate case where name is null string
615 * if .. and crossing mount points and on mounted filesys, find parent
616 * call VOP_LOOKUP routine for next component name
617 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
618 * component vnode returned in ni_vp (if it exists), locked.
619 * if result vnode is mounted on and crossing mount points,
620 * find mounted on vnode
621 * if more components of name, do next level at dirloop
622 * return the answer in ni_vp, locked if LOCKLEAF set
623 * if LOCKPARENT set, return locked parent in ni_dvp
624 * if WANTPARENT set, return unlocked parent in ni_dvp
627 lookup(struct nameidata *ndp)
629 char *cp; /* pointer into pathname argument */
630 struct vnode *dp = NULL; /* the directory we are searching */
631 struct vnode *tdp; /* saved dp */
632 struct mount *mp; /* mount table entry */
634 int docache; /* == 0 do not cache last component */
635 int wantparent; /* 1 => wantparent or lockparent flag */
636 int rdonly; /* lookup read-only flag bit */
638 int dpunlocked = 0; /* dp has already been unlocked */
639 int relookup = 0; /* do not consume the path component */
640 struct componentname *cnp = &ndp->ni_cnd;
645 * Setup: break out flag bits into variables.
648 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
649 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
650 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
651 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
652 if (cnp->cn_nameiop == DELETE ||
653 (wantparent && cnp->cn_nameiop != CREATE &&
654 cnp->cn_nameiop != LOOKUP))
656 rdonly = cnp->cn_flags & RDONLY;
657 cnp->cn_flags &= ~ISSYMLINK;
660 * We use shared locks until we hit the parent of the last cn then
661 * we adjust based on the requesting flags.
664 cnp->cn_lkflags = LK_SHARED;
666 cnp->cn_lkflags = LK_EXCLUSIVE;
667 dp = ndp->ni_startdir;
668 ndp->ni_startdir = NULLVP;
670 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
675 * Search a new directory.
677 * The last component of the filename is left accessible via
678 * cnp->cn_nameptr for callers that need the name. Callers needing
679 * the name set the SAVENAME flag. When done, they assume
680 * responsibility for freeing the pathname buffer.
682 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
684 cnp->cn_namelen = cp - cnp->cn_nameptr;
685 if (cnp->cn_namelen > NAME_MAX) {
686 error = ENAMETOOLONG;
689 #ifdef NAMEI_DIAGNOSTIC
692 printf("{%s}: ", cnp->cn_nameptr);
695 ndp->ni_pathlen -= cnp->cn_namelen;
699 * Replace multiple slashes by a single slash and trailing slashes
700 * by a null. This must be done before VOP_LOOKUP() because some
701 * fs's don't know about trailing slashes. Remember if there were
702 * trailing slashes to handle symlinks, existing non-directories
703 * and non-existing files that won't be directories specially later.
705 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
709 *ndp->ni_next = '\0';
710 cnp->cn_flags |= TRAILINGSLASH;
715 cnp->cn_flags |= MAKEENTRY;
716 if (*cp == '\0' && docache == 0)
717 cnp->cn_flags &= ~MAKEENTRY;
718 if (cnp->cn_namelen == 2 &&
719 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
720 cnp->cn_flags |= ISDOTDOT;
722 cnp->cn_flags &= ~ISDOTDOT;
723 if (*ndp->ni_next == 0)
724 cnp->cn_flags |= ISLASTCN;
726 cnp->cn_flags &= ~ISLASTCN;
728 if ((cnp->cn_flags & ISLASTCN) != 0 &&
729 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
730 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
735 nameicap_tracker_add(ndp, dp);
738 * Check for degenerate name (e.g. / or "")
739 * which is a way of talking about a directory,
740 * e.g. like "/." or ".".
742 if (cnp->cn_nameptr[0] == '\0') {
743 if (dp->v_type != VDIR) {
747 if (cnp->cn_nameiop != LOOKUP) {
757 if (cnp->cn_flags & AUDITVNODE1)
758 AUDIT_ARG_VNODE1(dp);
759 else if (cnp->cn_flags & AUDITVNODE2)
760 AUDIT_ARG_VNODE2(dp);
762 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
764 /* XXX This should probably move to the top of function. */
765 if (cnp->cn_flags & SAVESTART)
766 panic("lookup: SAVESTART");
771 * Handle "..": five special cases.
772 * 0. If doing a capability lookup and lookup_cap_dotdot is
773 * disabled, return ENOTCAPABLE.
774 * 1. Return an error if this is the last component of
775 * the name and the operation is DELETE or RENAME.
776 * 2. If at root directory (e.g. after chroot)
777 * or at absolute root directory
778 * then ignore it so can't get out.
779 * 3. If this vnode is the root of a mounted
780 * filesystem, then replace it with the
781 * vnode which was mounted on so we take the
782 * .. in the other filesystem.
783 * 4. If the vnode is the top directory of
784 * the jail or chroot, don't let them out.
785 * 5. If doing a capability lookup and lookup_cap_dotdot is
786 * enabled, return ENOTCAPABLE if the lookup would escape
787 * from the initial file descriptor directory. Checks are
788 * done by ensuring that namei() already traversed the
789 * result of dotdot lookup.
791 if (cnp->cn_flags & ISDOTDOT) {
792 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
793 == NI_LCF_STRICTRELATIVE) {
795 if (KTRPOINT(curthread, KTR_CAPFAIL))
796 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
801 if ((cnp->cn_flags & ISLASTCN) != 0 &&
802 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
807 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
809 if (dp == pr->pr_root)
811 if (dp == ndp->ni_rootdir ||
812 dp == ndp->ni_topdir ||
815 ((dp->v_vflag & VV_ROOT) != 0 &&
816 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
822 if ((dp->v_vflag & VV_ROOT) == 0)
824 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
829 dp = dp->v_mount->mnt_vnodecovered;
833 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
834 LK_RETRY, ISDOTDOT));
835 error = nameicap_check_dotdot(ndp, dp);
838 if (KTRPOINT(curthread, KTR_CAPFAIL))
839 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
847 * We now have a segment name to search for, and a directory to search.
851 if ((cnp->cn_flags & NOMACCHECK) == 0) {
852 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
860 ASSERT_VOP_LOCKED(dp, "lookup");
862 * If we have a shared lock we may need to upgrade the lock for the
865 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
866 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
867 vn_lock(dp, LK_UPGRADE|LK_RETRY);
868 if ((dp->v_iflag & VI_DOOMED) != 0) {
873 * If we're looking up the last component and we need an exclusive
874 * lock, adjust our lkflags.
876 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
877 cnp->cn_lkflags = LK_EXCLUSIVE;
878 #ifdef NAMEI_DIAGNOSTIC
879 vn_printf(dp, "lookup in ");
881 lkflags_save = cnp->cn_lkflags;
882 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
884 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
885 cnp->cn_lkflags = lkflags_save;
887 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
888 #ifdef NAMEI_DIAGNOSTIC
889 printf("not found\n");
891 if ((error == ENOENT) &&
892 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
893 (dp->v_mount->mnt_flag & MNT_UNION)) {
895 dp = dp->v_mount->mnt_vnodecovered;
899 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
900 LK_RETRY, cnp->cn_flags));
901 nameicap_tracker_add(ndp, dp);
905 if (error == ERELOOKUP) {
913 if (error != EJUSTRETURN)
916 * At this point, we know we're at the end of the
917 * pathname. If creating / renaming, we can consider
918 * allowing the file or directory to be created / renamed,
919 * provided we're not on a read-only filesystem.
925 /* trailing slash only allowed for directories */
926 if ((cnp->cn_flags & TRAILINGSLASH) &&
927 !(cnp->cn_flags & WILLBEDIR)) {
931 if ((cnp->cn_flags & LOCKPARENT) == 0)
934 * We return with ni_vp NULL to indicate that the entry
935 * doesn't currently exist, leaving a pointer to the
936 * (possibly locked) directory vnode in ndp->ni_dvp.
938 if (cnp->cn_flags & SAVESTART) {
939 ndp->ni_startdir = ndp->ni_dvp;
940 VREF(ndp->ni_startdir);
946 #ifdef NAMEI_DIAGNOSTIC
952 * Check to see if the vnode has been mounted on;
953 * if so find the root of the mounted filesystem.
955 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
956 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
960 if (dp != ndp->ni_dvp)
965 ndp->ni_dvp = vp_crossmp;
966 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
967 cnp->cn_flags), &tdp);
969 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
970 panic("vp_crossmp exclusively locked or reclaimed");
975 ndp->ni_vp = dp = tdp;
979 * Check for symbolic link
981 if ((dp->v_type == VLNK) &&
982 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
983 *ndp->ni_next == '/')) {
984 cnp->cn_flags |= ISSYMLINK;
985 if (dp->v_iflag & VI_DOOMED) {
987 * We can't know whether the directory was mounted with
988 * NOSYMFOLLOW, so we can't follow safely.
993 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
998 * Symlink code always expects an unlocked dvp.
1000 if (ndp->ni_dvp != ndp->ni_vp) {
1001 VOP_UNLOCK(ndp->ni_dvp, 0);
1002 ni_dvp_unlocked = 1;
1009 * Not a symbolic link that we will follow. Continue with the
1010 * next component if there is any; otherwise, we're done.
1012 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1013 ("lookup: invalid path state."));
1016 if (ndp->ni_dvp != dp)
1022 if (cnp->cn_flags & ISDOTDOT) {
1023 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1026 if (KTRPOINT(curthread, KTR_CAPFAIL))
1027 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1032 if (*ndp->ni_next == '/') {
1033 cnp->cn_nameptr = ndp->ni_next;
1034 while (*cnp->cn_nameptr == '/') {
1038 if (ndp->ni_dvp != dp)
1045 * If we're processing a path with a trailing slash,
1046 * check that the end result is a directory.
1048 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1053 * Disallow directory write attempts on read-only filesystems.
1056 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1060 if (cnp->cn_flags & SAVESTART) {
1061 ndp->ni_startdir = ndp->ni_dvp;
1062 VREF(ndp->ni_startdir);
1065 ni_dvp_unlocked = 2;
1066 if (ndp->ni_dvp != dp)
1070 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1071 VOP_UNLOCK(ndp->ni_dvp, 0);
1072 ni_dvp_unlocked = 1;
1075 if (cnp->cn_flags & AUDITVNODE1)
1076 AUDIT_ARG_VNODE1(dp);
1077 else if (cnp->cn_flags & AUDITVNODE2)
1078 AUDIT_ARG_VNODE2(dp);
1080 if ((cnp->cn_flags & LOCKLEAF) == 0)
1084 * Because of lookup_shared we may have the vnode shared locked, but
1085 * the caller may want it to be exclusively locked.
1087 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1088 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1089 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1090 if (dp->v_iflag & VI_DOOMED) {
1098 if (ni_dvp_unlocked != 2) {
1099 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1112 * relookup - lookup a path name component
1113 * Used by lookup to re-acquire things.
1116 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1118 struct vnode *dp = NULL; /* the directory we are searching */
1119 int wantparent; /* 1 => wantparent or lockparent flag */
1120 int rdonly; /* lookup read-only flag bit */
1123 KASSERT(cnp->cn_flags & ISLASTCN,
1124 ("relookup: Not given last component."));
1126 * Setup: break out flag bits into variables.
1128 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1129 KASSERT(wantparent, ("relookup: parent not wanted."));
1130 rdonly = cnp->cn_flags & RDONLY;
1131 cnp->cn_flags &= ~ISSYMLINK;
1133 cnp->cn_lkflags = LK_EXCLUSIVE;
1134 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1137 * Search a new directory.
1139 * The last component of the filename is left accessible via
1140 * cnp->cn_nameptr for callers that need the name. Callers needing
1141 * the name set the SAVENAME flag. When done, they assume
1142 * responsibility for freeing the pathname buffer.
1144 #ifdef NAMEI_DIAGNOSTIC
1145 printf("{%s}: ", cnp->cn_nameptr);
1149 * Check for "" which represents the root directory after slash
1152 if (cnp->cn_nameptr[0] == '\0') {
1154 * Support only LOOKUP for "/" because lookup()
1155 * can't succeed for CREATE, DELETE and RENAME.
1157 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1158 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1160 if (!(cnp->cn_flags & LOCKLEAF))
1163 /* XXX This should probably move to the top of function. */
1164 if (cnp->cn_flags & SAVESTART)
1165 panic("lookup: SAVESTART");
1169 if (cnp->cn_flags & ISDOTDOT)
1170 panic ("relookup: lookup on dot-dot");
1173 * We now have a segment name to search for, and a directory to search.
1175 #ifdef NAMEI_DIAGNOSTIC
1176 vn_printf(dp, "search in ");
1178 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1179 KASSERT(*vpp == NULL, ("leaf should be empty"));
1180 if (error != EJUSTRETURN)
1183 * If creating and at end of pathname, then can consider
1184 * allowing file to be created.
1190 /* ASSERT(dvp == ndp->ni_startdir) */
1191 if (cnp->cn_flags & SAVESTART)
1193 if ((cnp->cn_flags & LOCKPARENT) == 0)
1196 * We return with ni_vp NULL to indicate that the entry
1197 * doesn't currently exist, leaving a pointer to the
1198 * (possibly locked) directory vnode in ndp->ni_dvp.
1206 * Disallow directory write attempts on read-only filesystems.
1209 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1218 * Set the parent lock/ref state to the requested state.
1220 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1225 } else if (!wantparent)
1228 * Check for symbolic link
1230 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1231 ("relookup: symlink found.\n"));
1233 /* ASSERT(dvp == ndp->ni_startdir) */
1234 if (cnp->cn_flags & SAVESTART)
1237 if ((cnp->cn_flags & LOCKLEAF) == 0)
1247 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1248 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1252 ndp->ni_cnd.cn_nameiop = op;
1253 ndp->ni_cnd.cn_flags = flags;
1254 ndp->ni_segflg = segflg;
1255 ndp->ni_dirp = namep;
1256 ndp->ni_dirfd = dirfd;
1257 ndp->ni_startdir = startdir;
1258 if (rightsp != NULL)
1259 ndp->ni_rightsneeded = *rightsp;
1261 cap_rights_init(&ndp->ni_rightsneeded);
1262 filecaps_init(&ndp->ni_filecaps);
1263 ndp->ni_cnd.cn_thread = td;
1267 * Free data allocated by namei(); see namei(9) for details.
1270 NDFREE(struct nameidata *ndp, const u_int flags)
1278 if (!(flags & NDF_NO_FREE_PNBUF) &&
1279 (ndp->ni_cnd.cn_flags & HASBUF)) {
1280 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1281 ndp->ni_cnd.cn_flags &= ~HASBUF;
1283 if (!(flags & NDF_NO_VP_UNLOCK) &&
1284 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1286 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1295 VOP_UNLOCK(ndp->ni_vp, 0);
1296 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1297 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1298 ndp->ni_dvp != ndp->ni_vp)
1300 if (!(flags & NDF_NO_DVP_RELE) &&
1301 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1310 VOP_UNLOCK(ndp->ni_dvp, 0);
1311 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1312 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1313 vrele(ndp->ni_startdir);
1314 ndp->ni_startdir = NULL;
1319 * Determine if there is a suitable alternate filename under the specified
1320 * prefix for the specified path. If the create flag is set, then the
1321 * alternate prefix will be used so long as the parent directory exists.
1322 * This is used by the various compatibility ABIs so that Linux binaries prefer
1323 * files under /compat/linux for example. The chosen path (whether under
1324 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1325 * to by pathbuf. The caller is responsible for free'ing the buffer from
1326 * the M_TEMP bucket if one is returned.
1329 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1330 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1332 struct nameidata nd, ndroot;
1333 char *ptr, *buf, *cp;
1337 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1340 /* Copy the prefix into the new pathname as a starting point. */
1341 len = strlcpy(buf, prefix, MAXPATHLEN);
1342 if (len >= MAXPATHLEN) {
1347 sz = MAXPATHLEN - len;
1350 /* Append the filename to the prefix. */
1351 if (pathseg == UIO_SYSSPACE)
1352 error = copystr(path, ptr, sz, &len);
1354 error = copyinstr(path, ptr, sz, &len);
1362 /* Only use a prefix with absolute pathnames. */
1368 if (dirfd != AT_FDCWD) {
1370 * We want the original because the "prefix" is
1371 * included in the already opened dirfd.
1373 bcopy(ptr, buf, len);
1378 * We know that there is a / somewhere in this pathname.
1379 * Search backwards for it, to find the file's parent dir
1380 * to see if it exists in the alternate tree. If it does,
1381 * and we want to create a file (cflag is set). We don't
1382 * need to worry about the root comparison in this case.
1386 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1389 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1395 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td);
1402 * We now compare the vnode of the prefix to the one
1403 * vnode asked. If they resolve to be the same, then we
1404 * ignore the match so that the real root gets used.
1405 * This avoids the problem of traversing "../.." to find the
1406 * root directory and never finding it, because "/" resolves
1407 * to the emulation root directory. This is expensive :-(
1409 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1412 /* We shouldn't ever get an error from this namei(). */
1413 error = namei(&ndroot);
1415 if (nd.ni_vp == ndroot.ni_vp)
1418 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1419 vrele(ndroot.ni_vp);
1423 NDFREE(&nd, NDF_ONLY_PNBUF);
1427 /* If there was an error, use the original path name. */
1429 bcopy(ptr, buf, len);