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;
126 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
127 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
132 static struct vop_vector crossmp_vnodeops = {
133 .vop_default = &default_vnodeops,
134 .vop_islocked = crossmp_vop_islocked,
135 .vop_lock1 = crossmp_vop_lock1,
136 .vop_unlock = crossmp_vop_unlock,
139 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
140 * gets allocated early. See nameiinit for the direct call below.
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 vfs_vector_op_register(&crossmp_vnodeops);
160 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
162 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
164 static int lookup_cap_dotdot = 1;
165 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
166 &lookup_cap_dotdot, 0,
167 "enables \"..\" components in path lookup in capability mode");
168 static int lookup_cap_dotdot_nonlocal = 1;
169 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
170 &lookup_cap_dotdot_nonlocal, 0,
171 "enables \"..\" components in path lookup in capability mode "
172 "on non-local mount");
175 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
177 struct nameicap_tracker *nt;
179 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
181 if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_BENEATH_LATCHED)) ==
182 NI_LCF_BENEATH_ABS) {
183 MPASS((ndp->ni_lcf & NI_LCF_LATCH) != 0);
184 if (dp != ndp->ni_beneath_latch)
186 ndp->ni_lcf |= NI_LCF_BENEATH_LATCHED;
188 nt = uma_zalloc(nt_zone, M_WAITOK);
191 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
195 nameicap_cleanup(struct nameidata *ndp, bool clean_latch)
197 struct nameicap_tracker *nt, *nt1;
199 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
200 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
201 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
202 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
204 uma_zfree(nt_zone, nt);
206 if (clean_latch && (ndp->ni_lcf & NI_LCF_LATCH) != 0) {
207 ndp->ni_lcf &= ~NI_LCF_LATCH;
208 vrele(ndp->ni_beneath_latch);
213 * For dotdot lookups in capability mode, only allow the component
214 * lookup to succeed if the resulting directory was already traversed
215 * during the operation. Also fail dotdot lookups for non-local
216 * filesystems, where external agents might assist local lookups to
217 * escape the compartment.
220 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
222 struct nameicap_tracker *nt;
225 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
229 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
230 (mp->mnt_flag & MNT_LOCAL) == 0)
231 return (ENOTCAPABLE);
232 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
237 if ((ndp->ni_lcf & NI_LCF_BENEATH_ABS) != 0) {
238 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
239 nameicap_cleanup(ndp, false);
242 return (ENOTCAPABLE);
246 namei_cleanup_cnp(struct componentname *cnp)
249 uma_zfree(namei_zone, cnp->cn_pnbuf);
251 cnp->cn_pnbuf = NULL;
252 cnp->cn_nameptr = NULL;
257 namei_handle_root(struct nameidata *ndp, struct vnode **dpp, u_int n)
259 struct componentname *cnp;
262 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
264 if (KTRPOINT(curthread, KTR_CAPFAIL))
265 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
267 return (ENOTCAPABLE);
269 if ((cnp->cn_flags & BENEATH) != 0) {
270 ndp->ni_lcf |= NI_LCF_BENEATH_ABS;
271 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
272 nameicap_cleanup(ndp, false);
274 while (*(cnp->cn_nameptr) == '/') {
278 *dpp = ndp->ni_rootdir;
284 * Convert a pathname into a pointer to a locked vnode.
286 * The FOLLOW flag is set when symbolic links are to be followed
287 * when they occur at the end of the name translation process.
288 * Symbolic links are always followed for all other pathname
289 * components other than the last.
291 * The segflg defines whether the name is to be copied from user
292 * space or kernel space.
294 * Overall outline of namei:
297 * get starting directory
298 * while (!done && !error) {
299 * call lookup to search path.
300 * if symbolic link, massage name in buffer and continue
304 namei(struct nameidata *ndp)
306 struct filedesc *fdp; /* pointer to file descriptor state */
307 char *cp; /* pointer into pathname argument */
308 struct vnode *dp; /* the directory we are searching */
309 struct iovec aiov; /* uio for reading symbolic links */
310 struct componentname *cnp;
315 struct filecaps dirfd_caps;
317 int error, linklen, startdir_used;
322 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
323 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
324 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
325 ("namei: nameiop contaminated with flags"));
326 KASSERT((cnp->cn_flags & OPMASK) == 0,
327 ("namei: flags contaminated with nameiops"));
328 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
329 ndp->ni_startdir->v_type == VBAD);
331 TAILQ_INIT(&ndp->ni_cap_tracker);
334 /* We will set this ourselves if we need it. */
335 cnp->cn_flags &= ~TRAILINGSLASH;
338 * Get a buffer for the name to be translated, and copy the
339 * name into the buffer.
341 if ((cnp->cn_flags & HASBUF) == 0)
342 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
343 if (ndp->ni_segflg == UIO_SYSSPACE)
344 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
347 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
351 * Don't allow empty pathnames.
353 if (error == 0 && *cnp->cn_pnbuf == '\0')
356 #ifdef CAPABILITY_MODE
358 * In capability mode, lookups must be restricted to happen in
359 * the subtree with the root specified by the file descriptor:
360 * - The root must be real file descriptor, not the pseudo-descriptor
362 * - The passed path must be relative and not absolute.
363 * - If lookup_cap_dotdot is disabled, path must not contain the
365 * - If lookup_cap_dotdot is enabled, we verify that all '..'
366 * components lookups result in the directories which were
367 * previously walked by us, which prevents an escape from
370 if (error == 0 && IN_CAPABILITY_MODE(td) &&
371 (cnp->cn_flags & NOCAPCHECK) == 0) {
372 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
373 if (ndp->ni_dirfd == AT_FDCWD) {
375 if (KTRPOINT(td, KTR_CAPFAIL))
376 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
383 namei_cleanup_cnp(cnp);
389 if (KTRPOINT(td, KTR_NAMEI)) {
390 KASSERT(cnp->cn_thread == curthread,
391 ("namei not using curthread"));
392 ktrnamei(cnp->cn_pnbuf);
396 * Get starting point for the translation.
400 * The reference on ni_rootdir is acquired in the block below to avoid
401 * back-to-back atomics for absolute lookups.
403 ndp->ni_rootdir = fdp->fd_rdir;
404 ndp->ni_topdir = fdp->fd_jdir;
407 * If we are auditing the kernel pathname, save the user pathname.
409 if (cnp->cn_flags & AUDITVNODE1)
410 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
411 if (cnp->cn_flags & AUDITVNODE2)
412 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
416 cnp->cn_nameptr = cnp->cn_pnbuf;
417 if (cnp->cn_pnbuf[0] == '/') {
418 ndp->ni_resflags |= NIRES_ABS;
419 error = namei_handle_root(ndp, &dp, 2);
422 * Simplify error handling, we should almost never be
425 vrefact(ndp->ni_rootdir);
428 if (ndp->ni_startdir != NULL) {
429 vrefact(ndp->ni_rootdir);
430 dp = ndp->ni_startdir;
432 } else if (ndp->ni_dirfd == AT_FDCWD) {
434 if (dp == ndp->ni_rootdir) {
437 vrefact(ndp->ni_rootdir);
441 vrefact(ndp->ni_rootdir);
442 rights = ndp->ni_rightsneeded;
443 cap_rights_set_one(&rights, CAP_LOOKUP);
445 if (cnp->cn_flags & AUDITVNODE1)
446 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
447 if (cnp->cn_flags & AUDITVNODE2)
448 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
450 * Effectively inlined fgetvp_rights, because we need to
451 * inspect the file as well as grabbing the vnode.
453 error = fget_cap_locked(fdp, ndp->ni_dirfd, &rights,
454 &dfp, &ndp->ni_filecaps);
457 * Preserve the error; it should either be EBADF
458 * or capability-related, both of which can be
459 * safely returned to the caller.
461 } else if (dfp->f_ops == &badfileops) {
463 } else if (dfp->f_vnode == NULL) {
469 if ((dfp->f_flag & FSEARCH) != 0)
470 cnp->cn_flags |= NOEXECCHECK;
474 * If file descriptor doesn't have all rights,
475 * all lookups relative to it must also be
479 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
481 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
482 ndp->ni_filecaps.fc_nioctls != -1) {
483 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
487 if (error == 0 && dp->v_type != VDIR)
490 if (error == 0 && (cnp->cn_flags & BENEATH) != 0) {
491 if (ndp->ni_dirfd == AT_FDCWD) {
492 ndp->ni_beneath_latch = fdp->fd_cdir;
493 vrefact(ndp->ni_beneath_latch);
495 rights = ndp->ni_rightsneeded;
496 cap_rights_set_one(&rights, CAP_LOOKUP);
497 error = fgetvp_rights(td, ndp->ni_dirfd, &rights,
498 &dirfd_caps, &ndp->ni_beneath_latch);
499 if (error == 0 && dp->v_type != VDIR) {
500 vrele(ndp->ni_beneath_latch);
505 ndp->ni_lcf |= NI_LCF_LATCH;
507 FILEDESC_SUNLOCK(fdp);
508 if (ndp->ni_startdir != NULL && !startdir_used)
509 vrele(ndp->ni_startdir);
515 MPASS((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_LATCH)) !=
517 if (((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
518 lookup_cap_dotdot != 0) ||
519 ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
520 (cnp->cn_flags & BENEATH) != 0))
521 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
522 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
525 ndp->ni_startdir = dp;
530 * If not a symbolic link, we're done.
532 if ((cnp->cn_flags & ISSYMLINK) == 0) {
533 vrele(ndp->ni_rootdir);
534 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
535 namei_cleanup_cnp(cnp);
537 cnp->cn_flags |= HASBUF;
538 if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS |
539 NI_LCF_BENEATH_LATCHED)) == NI_LCF_BENEATH_ABS) {
543 nameicap_cleanup(ndp, true);
544 SDT_PROBE2(vfs, namei, lookup, return, error,
545 (error == 0 ? ndp->ni_vp : NULL));
548 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
553 if ((cnp->cn_flags & NOMACCHECK) == 0) {
554 error = mac_vnode_check_readlink(td->td_ucred,
560 if (ndp->ni_pathlen > 1)
561 cp = uma_zalloc(namei_zone, M_WAITOK);
565 aiov.iov_len = MAXPATHLEN;
566 auio.uio_iov = &aiov;
569 auio.uio_rw = UIO_READ;
570 auio.uio_segflg = UIO_SYSSPACE;
572 auio.uio_resid = MAXPATHLEN;
573 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
575 if (ndp->ni_pathlen > 1)
576 uma_zfree(namei_zone, cp);
579 linklen = MAXPATHLEN - auio.uio_resid;
581 if (ndp->ni_pathlen > 1)
582 uma_zfree(namei_zone, cp);
586 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
587 if (ndp->ni_pathlen > 1)
588 uma_zfree(namei_zone, cp);
589 error = ENAMETOOLONG;
592 if (ndp->ni_pathlen > 1) {
593 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
594 uma_zfree(namei_zone, cnp->cn_pnbuf);
597 cnp->cn_pnbuf[linklen] = '\0';
598 ndp->ni_pathlen += linklen;
602 * Check if root directory should replace current directory.
604 cnp->cn_nameptr = cnp->cn_pnbuf;
605 if (*(cnp->cn_nameptr) == '/') {
607 error = namei_handle_root(ndp, &dp, 1);
616 vrele(ndp->ni_rootdir);
618 namei_cleanup_cnp(cnp);
619 nameicap_cleanup(ndp, true);
620 SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
625 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
628 if (mp == NULL || ((lkflags & LK_SHARED) &&
629 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
630 ((cnflags & ISDOTDOT) &&
631 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
632 lkflags &= ~LK_SHARED;
633 lkflags |= LK_EXCLUSIVE;
635 lkflags |= LK_NODDLKTREAT;
640 needs_exclusive_leaf(struct mount *mp, int flags)
644 * Intermediate nodes can use shared locks, we only need to
645 * force an exclusive lock for leaf nodes.
647 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
650 /* Always use exclusive locks if LOCKSHARED isn't set. */
651 if (!(flags & LOCKSHARED))
655 * For lookups during open(), if the mount point supports
656 * extended shared operations, then use a shared lock for the
657 * leaf node, otherwise use an exclusive lock.
659 if ((flags & ISOPEN) != 0)
660 return (!MNT_EXTENDED_SHARED(mp));
663 * Lookup requests outside of open() that specify LOCKSHARED
664 * only need a shared lock on the leaf vnode.
671 * This is a very central and rather complicated routine.
673 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
674 * The starting directory is taken from ni_startdir. The pathname is
675 * descended until done, or a symbolic link is encountered. The variable
676 * ni_more is clear if the path is completed; it is set to one if a
677 * symbolic link needing interpretation is encountered.
679 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
680 * whether the name is to be looked up, created, renamed, or deleted.
681 * When CREATE, RENAME, or DELETE is specified, information usable in
682 * creating, renaming, or deleting a directory entry may be calculated.
683 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
684 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
685 * returned unlocked. Otherwise the parent directory is not returned. If
686 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
687 * the target is returned locked, otherwise it is returned unlocked.
688 * When creating or renaming and LOCKPARENT is specified, the target may not
689 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
691 * Overall outline of lookup:
694 * identify next component of name at ndp->ni_ptr
695 * handle degenerate case where name is null string
696 * if .. and crossing mount points and on mounted filesys, find parent
697 * call VOP_LOOKUP routine for next component name
698 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
699 * component vnode returned in ni_vp (if it exists), locked.
700 * if result vnode is mounted on and crossing mount points,
701 * find mounted on vnode
702 * if more components of name, do next level at dirloop
703 * return the answer in ni_vp, locked if LOCKLEAF set
704 * if LOCKPARENT set, return locked parent in ni_dvp
705 * if WANTPARENT set, return unlocked parent in ni_dvp
708 lookup(struct nameidata *ndp)
710 char *cp; /* pointer into pathname argument */
711 char *prev_ni_next; /* saved ndp->ni_next */
712 struct vnode *dp = NULL; /* the directory we are searching */
713 struct vnode *tdp; /* saved dp */
714 struct mount *mp; /* mount table entry */
716 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
717 int docache; /* == 0 do not cache last component */
718 int wantparent; /* 1 => wantparent or lockparent flag */
719 int rdonly; /* lookup read-only flag bit */
721 int dpunlocked = 0; /* dp has already been unlocked */
722 int relookup = 0; /* do not consume the path component */
723 struct componentname *cnp = &ndp->ni_cnd;
728 * Setup: break out flag bits into variables.
731 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
732 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
733 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
734 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
735 if (cnp->cn_nameiop == DELETE ||
736 (wantparent && cnp->cn_nameiop != CREATE &&
737 cnp->cn_nameiop != LOOKUP))
739 rdonly = cnp->cn_flags & RDONLY;
740 cnp->cn_flags &= ~ISSYMLINK;
743 * We use shared locks until we hit the parent of the last cn then
744 * we adjust based on the requesting flags.
746 cnp->cn_lkflags = LK_SHARED;
747 dp = ndp->ni_startdir;
748 ndp->ni_startdir = NULLVP;
750 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
755 * Search a new directory.
757 * The last component of the filename is left accessible via
758 * cnp->cn_nameptr for callers that need the name. Callers needing
759 * the name set the SAVENAME flag. When done, they assume
760 * responsibility for freeing the pathname buffer.
762 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
764 cnp->cn_namelen = cp - cnp->cn_nameptr;
765 if (cnp->cn_namelen > NAME_MAX) {
766 error = ENAMETOOLONG;
769 #ifdef NAMEI_DIAGNOSTIC
772 printf("{%s}: ", cnp->cn_nameptr);
775 prev_ni_pathlen = ndp->ni_pathlen;
776 ndp->ni_pathlen -= cnp->cn_namelen;
777 KASSERT(ndp->ni_pathlen <= PATH_MAX,
778 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
779 prev_ni_next = ndp->ni_next;
783 * Replace multiple slashes by a single slash and trailing slashes
784 * by a null. This must be done before VOP_LOOKUP() because some
785 * fs's don't know about trailing slashes. Remember if there were
786 * trailing slashes to handle symlinks, existing non-directories
787 * and non-existing files that won't be directories specially later.
789 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
793 *ndp->ni_next = '\0';
794 cnp->cn_flags |= TRAILINGSLASH;
799 cnp->cn_flags |= MAKEENTRY;
800 if (*cp == '\0' && docache == 0)
801 cnp->cn_flags &= ~MAKEENTRY;
802 if (cnp->cn_namelen == 2 &&
803 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
804 cnp->cn_flags |= ISDOTDOT;
806 cnp->cn_flags &= ~ISDOTDOT;
807 if (*ndp->ni_next == 0)
808 cnp->cn_flags |= ISLASTCN;
810 cnp->cn_flags &= ~ISLASTCN;
812 if ((cnp->cn_flags & ISLASTCN) != 0 &&
813 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
814 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
819 nameicap_tracker_add(ndp, dp);
822 * Check for degenerate name (e.g. / or "")
823 * which is a way of talking about a directory,
824 * e.g. like "/." or ".".
826 if (cnp->cn_nameptr[0] == '\0') {
827 if (dp->v_type != VDIR) {
831 if (cnp->cn_nameiop != LOOKUP) {
841 if (cnp->cn_flags & AUDITVNODE1)
842 AUDIT_ARG_VNODE1(dp);
843 else if (cnp->cn_flags & AUDITVNODE2)
844 AUDIT_ARG_VNODE2(dp);
846 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
848 /* XXX This should probably move to the top of function. */
849 if (cnp->cn_flags & SAVESTART)
850 panic("lookup: SAVESTART");
855 * Handle "..": five special cases.
856 * 0. If doing a capability lookup and lookup_cap_dotdot is
857 * disabled, return ENOTCAPABLE.
858 * 1. Return an error if this is the last component of
859 * the name and the operation is DELETE or RENAME.
860 * 2. If at root directory (e.g. after chroot)
861 * or at absolute root directory
862 * then ignore it so can't get out.
863 * 3. If this vnode is the root of a mounted
864 * filesystem, then replace it with the
865 * vnode which was mounted on so we take the
866 * .. in the other filesystem.
867 * 4. If the vnode is the top directory of
868 * the jail or chroot, don't let them out.
869 * 5. If doing a capability lookup and lookup_cap_dotdot is
870 * enabled, return ENOTCAPABLE if the lookup would escape
871 * from the initial file descriptor directory. Checks are
872 * done by ensuring that namei() already traversed the
873 * result of dotdot lookup.
875 if (cnp->cn_flags & ISDOTDOT) {
876 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
877 == NI_LCF_STRICTRELATIVE) {
879 if (KTRPOINT(curthread, KTR_CAPFAIL))
880 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
885 if ((cnp->cn_flags & ISLASTCN) != 0 &&
886 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
891 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
893 if (dp == pr->pr_root)
895 if (dp == ndp->ni_rootdir ||
896 dp == ndp->ni_topdir ||
899 ((dp->v_vflag & VV_ROOT) != 0 &&
900 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
906 if ((dp->v_vflag & VV_ROOT) == 0)
908 if (VN_IS_DOOMED(dp)) { /* forced unmount */
913 dp = dp->v_mount->mnt_vnodecovered;
917 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
918 LK_RETRY, ISDOTDOT));
919 error = nameicap_check_dotdot(ndp, dp);
922 if (KTRPOINT(curthread, KTR_CAPFAIL))
923 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
931 * We now have a segment name to search for, and a directory to search.
935 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
941 ASSERT_VOP_LOCKED(dp, "lookup");
943 * If we have a shared lock we may need to upgrade the lock for the
946 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
947 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
948 vn_lock(dp, LK_UPGRADE|LK_RETRY);
949 if (VN_IS_DOOMED(dp)) {
954 * If we're looking up the last component and we need an exclusive
955 * lock, adjust our lkflags.
957 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
958 cnp->cn_lkflags = LK_EXCLUSIVE;
959 #ifdef NAMEI_DIAGNOSTIC
960 vn_printf(dp, "lookup in ");
962 lkflags_save = cnp->cn_lkflags;
963 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
965 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
966 cnp->cn_lkflags = lkflags_save;
968 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
969 #ifdef NAMEI_DIAGNOSTIC
970 printf("not found\n");
972 if ((error == ENOENT) &&
973 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
974 (dp->v_mount->mnt_flag & MNT_UNION)) {
976 dp = dp->v_mount->mnt_vnodecovered;
980 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
981 LK_RETRY, cnp->cn_flags));
982 nameicap_tracker_add(ndp, dp);
986 if (error == ERELOOKUP) {
994 if (error != EJUSTRETURN)
997 * At this point, we know we're at the end of the
998 * pathname. If creating / renaming, we can consider
999 * allowing the file or directory to be created / renamed,
1000 * provided we're not on a read-only filesystem.
1006 /* trailing slash only allowed for directories */
1007 if ((cnp->cn_flags & TRAILINGSLASH) &&
1008 !(cnp->cn_flags & WILLBEDIR)) {
1012 if ((cnp->cn_flags & LOCKPARENT) == 0)
1015 * We return with ni_vp NULL to indicate that the entry
1016 * doesn't currently exist, leaving a pointer to the
1017 * (possibly locked) directory vnode in ndp->ni_dvp.
1019 if (cnp->cn_flags & SAVESTART) {
1020 ndp->ni_startdir = ndp->ni_dvp;
1021 VREF(ndp->ni_startdir);
1027 #ifdef NAMEI_DIAGNOSTIC
1033 * Check to see if the vnode has been mounted on;
1034 * if so find the root of the mounted filesystem.
1036 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1037 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1038 if (vfs_busy(mp, 0))
1041 if (dp != ndp->ni_dvp)
1045 vrefact(vp_crossmp);
1046 ndp->ni_dvp = vp_crossmp;
1047 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1048 cnp->cn_flags), &tdp);
1050 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1051 panic("vp_crossmp exclusively locked or reclaimed");
1056 ndp->ni_vp = dp = tdp;
1060 * Check for symbolic link
1062 if ((dp->v_type == VLNK) &&
1063 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1064 *ndp->ni_next == '/')) {
1065 cnp->cn_flags |= ISSYMLINK;
1066 if (VN_IS_DOOMED(dp)) {
1068 * We can't know whether the directory was mounted with
1069 * NOSYMFOLLOW, so we can't follow safely.
1074 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1079 * Symlink code always expects an unlocked dvp.
1081 if (ndp->ni_dvp != ndp->ni_vp) {
1082 VOP_UNLOCK(ndp->ni_dvp);
1083 ni_dvp_unlocked = 1;
1090 * Not a symbolic link that we will follow. Continue with the
1091 * next component if there is any; otherwise, we're done.
1093 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1094 ("lookup: invalid path state."));
1097 ndp->ni_pathlen = prev_ni_pathlen;
1098 ndp->ni_next = prev_ni_next;
1099 if (ndp->ni_dvp != dp)
1105 if (cnp->cn_flags & ISDOTDOT) {
1106 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1109 if (KTRPOINT(curthread, KTR_CAPFAIL))
1110 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1115 if (*ndp->ni_next == '/') {
1116 cnp->cn_nameptr = ndp->ni_next;
1117 while (*cnp->cn_nameptr == '/') {
1121 if (ndp->ni_dvp != dp)
1128 * If we're processing a path with a trailing slash,
1129 * check that the end result is a directory.
1131 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1136 * Disallow directory write attempts on read-only filesystems.
1139 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1143 if (cnp->cn_flags & SAVESTART) {
1144 ndp->ni_startdir = ndp->ni_dvp;
1145 VREF(ndp->ni_startdir);
1148 ni_dvp_unlocked = 2;
1149 if (ndp->ni_dvp != dp)
1153 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1154 VOP_UNLOCK(ndp->ni_dvp);
1155 ni_dvp_unlocked = 1;
1158 if (cnp->cn_flags & AUDITVNODE1)
1159 AUDIT_ARG_VNODE1(dp);
1160 else if (cnp->cn_flags & AUDITVNODE2)
1161 AUDIT_ARG_VNODE2(dp);
1163 if ((cnp->cn_flags & LOCKLEAF) == 0)
1167 * Because of shared lookup we may have the vnode shared locked, but
1168 * the caller may want it to be exclusively locked.
1170 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1171 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1172 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1173 if (VN_IS_DOOMED(dp)) {
1181 if (ni_dvp_unlocked != 2) {
1182 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1195 * relookup - lookup a path name component
1196 * Used by lookup to re-acquire things.
1199 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1201 struct vnode *dp = NULL; /* the directory we are searching */
1202 int wantparent; /* 1 => wantparent or lockparent flag */
1203 int rdonly; /* lookup read-only flag bit */
1206 KASSERT(cnp->cn_flags & ISLASTCN,
1207 ("relookup: Not given last component."));
1209 * Setup: break out flag bits into variables.
1211 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1212 KASSERT(wantparent, ("relookup: parent not wanted."));
1213 rdonly = cnp->cn_flags & RDONLY;
1214 cnp->cn_flags &= ~ISSYMLINK;
1216 cnp->cn_lkflags = LK_EXCLUSIVE;
1217 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1220 * Search a new directory.
1222 * The last component of the filename is left accessible via
1223 * cnp->cn_nameptr for callers that need the name. Callers needing
1224 * the name set the SAVENAME flag. When done, they assume
1225 * responsibility for freeing the pathname buffer.
1227 #ifdef NAMEI_DIAGNOSTIC
1228 printf("{%s}: ", cnp->cn_nameptr);
1232 * Check for "" which represents the root directory after slash
1235 if (cnp->cn_nameptr[0] == '\0') {
1237 * Support only LOOKUP for "/" because lookup()
1238 * can't succeed for CREATE, DELETE and RENAME.
1240 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1241 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1243 if (!(cnp->cn_flags & LOCKLEAF))
1246 /* XXX This should probably move to the top of function. */
1247 if (cnp->cn_flags & SAVESTART)
1248 panic("lookup: SAVESTART");
1252 if (cnp->cn_flags & ISDOTDOT)
1253 panic ("relookup: lookup on dot-dot");
1256 * We now have a segment name to search for, and a directory to search.
1258 #ifdef NAMEI_DIAGNOSTIC
1259 vn_printf(dp, "search in ");
1261 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1262 KASSERT(*vpp == NULL, ("leaf should be empty"));
1263 if (error != EJUSTRETURN)
1266 * If creating and at end of pathname, then can consider
1267 * allowing file to be created.
1273 /* ASSERT(dvp == ndp->ni_startdir) */
1274 if (cnp->cn_flags & SAVESTART)
1276 if ((cnp->cn_flags & LOCKPARENT) == 0)
1279 * We return with ni_vp NULL to indicate that the entry
1280 * doesn't currently exist, leaving a pointer to the
1281 * (possibly locked) directory vnode in ndp->ni_dvp.
1289 * Disallow directory write attempts on read-only filesystems.
1292 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1301 * Set the parent lock/ref state to the requested state.
1303 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1308 } else if (!wantparent)
1311 * Check for symbolic link
1313 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1314 ("relookup: symlink found.\n"));
1316 /* ASSERT(dvp == ndp->ni_startdir) */
1317 if (cnp->cn_flags & SAVESTART)
1320 if ((cnp->cn_flags & LOCKLEAF) == 0)
1330 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1331 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1335 ndp->ni_cnd.cn_nameiop = op;
1336 ndp->ni_cnd.cn_flags = flags;
1337 ndp->ni_segflg = segflg;
1338 ndp->ni_dirp = namep;
1339 ndp->ni_dirfd = dirfd;
1340 ndp->ni_startdir = startdir;
1341 ndp->ni_resflags = 0;
1342 filecaps_init(&ndp->ni_filecaps);
1343 ndp->ni_cnd.cn_thread = td;
1344 if (rightsp != NULL)
1345 ndp->ni_rightsneeded = *rightsp;
1347 cap_rights_init_zero(&ndp->ni_rightsneeded);
1351 * Free data allocated by namei(); see namei(9) for details.
1354 NDFREE(struct nameidata *ndp, const u_int flags)
1362 if (!(flags & NDF_NO_FREE_PNBUF) &&
1363 (ndp->ni_cnd.cn_flags & HASBUF)) {
1364 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1365 ndp->ni_cnd.cn_flags &= ~HASBUF;
1367 if (!(flags & NDF_NO_VP_UNLOCK) &&
1368 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1370 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1371 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1372 ndp->ni_dvp != ndp->ni_vp)
1374 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1383 VOP_UNLOCK(ndp->ni_vp);
1384 if (!(flags & NDF_NO_DVP_RELE) &&
1385 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1394 VOP_UNLOCK(ndp->ni_dvp);
1395 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1396 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1397 vrele(ndp->ni_startdir);
1398 ndp->ni_startdir = NULL;
1403 * Determine if there is a suitable alternate filename under the specified
1404 * prefix for the specified path. If the create flag is set, then the
1405 * alternate prefix will be used so long as the parent directory exists.
1406 * This is used by the various compatibility ABIs so that Linux binaries prefer
1407 * files under /compat/linux for example. The chosen path (whether under
1408 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1409 * to by pathbuf. The caller is responsible for free'ing the buffer from
1410 * the M_TEMP bucket if one is returned.
1413 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1414 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1416 struct nameidata nd, ndroot;
1417 char *ptr, *buf, *cp;
1421 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1424 /* Copy the prefix into the new pathname as a starting point. */
1425 len = strlcpy(buf, prefix, MAXPATHLEN);
1426 if (len >= MAXPATHLEN) {
1431 sz = MAXPATHLEN - len;
1434 /* Append the filename to the prefix. */
1435 if (pathseg == UIO_SYSSPACE)
1436 error = copystr(path, ptr, sz, &len);
1438 error = copyinstr(path, ptr, sz, &len);
1446 /* Only use a prefix with absolute pathnames. */
1452 if (dirfd != AT_FDCWD) {
1454 * We want the original because the "prefix" is
1455 * included in the already opened dirfd.
1457 bcopy(ptr, buf, len);
1462 * We know that there is a / somewhere in this pathname.
1463 * Search backwards for it, to find the file's parent dir
1464 * to see if it exists in the alternate tree. If it does,
1465 * and we want to create a file (cflag is set). We don't
1466 * need to worry about the root comparison in this case.
1470 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1473 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1479 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1486 * We now compare the vnode of the prefix to the one
1487 * vnode asked. If they resolve to be the same, then we
1488 * ignore the match so that the real root gets used.
1489 * This avoids the problem of traversing "../.." to find the
1490 * root directory and never finding it, because "/" resolves
1491 * to the emulation root directory. This is expensive :-(
1493 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1496 /* We shouldn't ever get an error from this namei(). */
1497 error = namei(&ndroot);
1499 if (nd.ni_vp == ndroot.ni_vp)
1502 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1503 vrele(ndroot.ni_vp);
1507 NDFREE(&nd, NDF_ONLY_PNBUF);
1511 /* If there was an error, use the original path name. */
1513 bcopy(ptr, buf, len);