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 if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_BENEATH_LATCHED)) ==
181 NI_LCF_BENEATH_ABS) {
182 MPASS((ndp->ni_lcf & NI_LCF_LATCH) != 0);
183 if (dp != ndp->ni_beneath_latch)
185 ndp->ni_lcf |= NI_LCF_BENEATH_LATCHED;
187 nt = uma_zalloc(nt_zone, M_WAITOK);
190 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
194 nameicap_cleanup(struct nameidata *ndp, bool clean_latch)
196 struct nameicap_tracker *nt, *nt1;
198 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
199 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
200 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
201 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
203 uma_zfree(nt_zone, nt);
205 if (clean_latch && (ndp->ni_lcf & NI_LCF_LATCH) != 0) {
206 ndp->ni_lcf &= ~NI_LCF_LATCH;
207 vrele(ndp->ni_beneath_latch);
212 * For dotdot lookups in capability mode, only allow the component
213 * lookup to succeed if the resulting directory was already traversed
214 * during the operation. Also fail dotdot lookups for non-local
215 * filesystems, where external agents might assist local lookups to
216 * escape the compartment.
219 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
221 struct nameicap_tracker *nt;
224 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
228 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
229 (mp->mnt_flag & MNT_LOCAL) == 0)
230 return (ENOTCAPABLE);
231 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
236 if ((ndp->ni_lcf & NI_LCF_BENEATH_ABS) != 0) {
237 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
238 nameicap_cleanup(ndp, false);
241 return (ENOTCAPABLE);
245 namei_cleanup_cnp(struct componentname *cnp)
248 uma_zfree(namei_zone, cnp->cn_pnbuf);
250 cnp->cn_pnbuf = NULL;
251 cnp->cn_nameptr = NULL;
256 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
258 struct componentname *cnp;
261 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
263 if (KTRPOINT(curthread, KTR_CAPFAIL))
264 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
266 return (ENOTCAPABLE);
268 if ((cnp->cn_flags & BENEATH) != 0) {
269 ndp->ni_lcf |= NI_LCF_BENEATH_ABS;
270 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
271 nameicap_cleanup(ndp, false);
273 while (*(cnp->cn_nameptr) == '/') {
277 *dpp = ndp->ni_rootdir;
283 * Convert a pathname into a pointer to a locked vnode.
285 * The FOLLOW flag is set when symbolic links are to be followed
286 * when they occur at the end of the name translation process.
287 * Symbolic links are always followed for all other pathname
288 * components other than the last.
290 * The segflg defines whether the name is to be copied from user
291 * space or kernel space.
293 * Overall outline of namei:
296 * get starting directory
297 * while (!done && !error) {
298 * call lookup to search path.
299 * if symbolic link, massage name in buffer and continue
303 namei(struct nameidata *ndp)
305 struct filedesc *fdp; /* pointer to file descriptor state */
306 char *cp; /* pointer into pathname argument */
307 struct vnode *dp; /* the directory we are searching */
308 struct iovec aiov; /* uio for reading symbolic links */
309 struct componentname *cnp;
313 struct filecaps dirfd_caps;
315 int error, linklen, startdir_used;
320 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
321 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc"));
322 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0,
323 ("namei: nameiop contaminated with flags"));
324 KASSERT((cnp->cn_flags & OPMASK) == 0,
325 ("namei: flags contaminated with nameiops"));
326 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
327 ndp->ni_startdir->v_type == VBAD);
329 TAILQ_INIT(&ndp->ni_cap_tracker);
332 /* We will set this ourselves if we need it. */
333 cnp->cn_flags &= ~TRAILINGSLASH;
336 * Get a buffer for the name to be translated, and copy the
337 * name into the buffer.
339 if ((cnp->cn_flags & HASBUF) == 0)
340 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
341 if (ndp->ni_segflg == UIO_SYSSPACE)
342 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
345 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
349 * Don't allow empty pathnames.
351 if (error == 0 && *cnp->cn_pnbuf == '\0')
354 #ifdef CAPABILITY_MODE
356 * In capability mode, lookups must be restricted to happen in
357 * the subtree with the root specified by the file descriptor:
358 * - The root must be real file descriptor, not the pseudo-descriptor
360 * - The passed path must be relative and not absolute.
361 * - If lookup_cap_dotdot is disabled, path must not contain the
363 * - If lookup_cap_dotdot is enabled, we verify that all '..'
364 * components lookups result in the directories which were
365 * previously walked by us, which prevents an escape from
368 if (error == 0 && IN_CAPABILITY_MODE(td) &&
369 (cnp->cn_flags & NOCAPCHECK) == 0) {
370 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
371 if (ndp->ni_dirfd == AT_FDCWD) {
373 if (KTRPOINT(td, KTR_CAPFAIL))
374 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
381 namei_cleanup_cnp(cnp);
387 if (KTRPOINT(td, KTR_NAMEI)) {
388 KASSERT(cnp->cn_thread == curthread,
389 ("namei not using curthread"));
390 ktrnamei(cnp->cn_pnbuf);
394 * Get starting point for the translation.
397 ndp->ni_rootdir = fdp->fd_rdir;
398 vrefact(ndp->ni_rootdir);
399 ndp->ni_topdir = fdp->fd_jdir;
402 * If we are auditing the kernel pathname, save the user pathname.
404 if (cnp->cn_flags & AUDITVNODE1)
405 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf);
406 if (cnp->cn_flags & AUDITVNODE2)
407 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf);
411 cnp->cn_nameptr = cnp->cn_pnbuf;
412 if (cnp->cn_pnbuf[0] == '/') {
413 error = namei_handle_root(ndp, &dp);
415 if (ndp->ni_startdir != NULL) {
416 dp = ndp->ni_startdir;
418 } else if (ndp->ni_dirfd == AT_FDCWD) {
422 rights = ndp->ni_rightsneeded;
423 cap_rights_set(&rights, CAP_LOOKUP);
425 if (cnp->cn_flags & AUDITVNODE1)
426 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
427 if (cnp->cn_flags & AUDITVNODE2)
428 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
429 error = fgetvp_rights(td, ndp->ni_dirfd,
430 &rights, &ndp->ni_filecaps, &dp);
435 * If file descriptor doesn't have all rights,
436 * all lookups relative to it must also be
440 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
442 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
443 ndp->ni_filecaps.fc_nioctls != -1) {
444 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
448 if (error == 0 && dp->v_type != VDIR)
451 if (error == 0 && (cnp->cn_flags & BENEATH) != 0) {
452 if (ndp->ni_dirfd == AT_FDCWD) {
453 ndp->ni_beneath_latch = fdp->fd_cdir;
454 vrefact(ndp->ni_beneath_latch);
456 rights = ndp->ni_rightsneeded;
457 cap_rights_set(&rights, CAP_LOOKUP);
458 error = fgetvp_rights(td, ndp->ni_dirfd, &rights,
459 &dirfd_caps, &ndp->ni_beneath_latch);
460 if (error == 0 && dp->v_type != VDIR) {
461 vrele(ndp->ni_beneath_latch);
466 ndp->ni_lcf |= NI_LCF_LATCH;
468 FILEDESC_SUNLOCK(fdp);
469 if (ndp->ni_startdir != NULL && !startdir_used)
470 vrele(ndp->ni_startdir);
476 MPASS((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_LATCH)) !=
478 if (((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
479 lookup_cap_dotdot != 0) ||
480 ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
481 (cnp->cn_flags & BENEATH) != 0))
482 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
483 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf,
486 ndp->ni_startdir = dp;
491 * If not a symbolic link, we're done.
493 if ((cnp->cn_flags & ISSYMLINK) == 0) {
494 vrele(ndp->ni_rootdir);
495 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
496 namei_cleanup_cnp(cnp);
498 cnp->cn_flags |= HASBUF;
499 if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS |
500 NI_LCF_BENEATH_LATCHED)) == NI_LCF_BENEATH_ABS) {
504 nameicap_cleanup(ndp, true);
505 SDT_PROBE2(vfs, namei, lookup, return, error,
506 (error == 0 ? ndp->ni_vp : NULL));
509 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
514 if ((cnp->cn_flags & NOMACCHECK) == 0) {
515 error = mac_vnode_check_readlink(td->td_ucred,
521 if (ndp->ni_pathlen > 1)
522 cp = uma_zalloc(namei_zone, M_WAITOK);
526 aiov.iov_len = MAXPATHLEN;
527 auio.uio_iov = &aiov;
530 auio.uio_rw = UIO_READ;
531 auio.uio_segflg = UIO_SYSSPACE;
533 auio.uio_resid = MAXPATHLEN;
534 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
536 if (ndp->ni_pathlen > 1)
537 uma_zfree(namei_zone, cp);
540 linklen = MAXPATHLEN - auio.uio_resid;
542 if (ndp->ni_pathlen > 1)
543 uma_zfree(namei_zone, cp);
547 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
548 if (ndp->ni_pathlen > 1)
549 uma_zfree(namei_zone, cp);
550 error = ENAMETOOLONG;
553 if (ndp->ni_pathlen > 1) {
554 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
555 uma_zfree(namei_zone, cnp->cn_pnbuf);
558 cnp->cn_pnbuf[linklen] = '\0';
559 ndp->ni_pathlen += linklen;
563 * Check if root directory should replace current directory.
565 cnp->cn_nameptr = cnp->cn_pnbuf;
566 if (*(cnp->cn_nameptr) == '/') {
568 error = namei_handle_root(ndp, &dp);
577 vrele(ndp->ni_rootdir);
579 namei_cleanup_cnp(cnp);
580 nameicap_cleanup(ndp, true);
581 SDT_PROBE2(vfs, namei, lookup, return, error, NULL);
586 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
589 if (mp == NULL || ((lkflags & LK_SHARED) &&
590 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
591 ((cnflags & ISDOTDOT) &&
592 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
593 lkflags &= ~LK_SHARED;
594 lkflags |= LK_EXCLUSIVE;
596 lkflags |= LK_NODDLKTREAT;
601 needs_exclusive_leaf(struct mount *mp, int flags)
605 * Intermediate nodes can use shared locks, we only need to
606 * force an exclusive lock for leaf nodes.
608 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
611 /* Always use exclusive locks if LOCKSHARED isn't set. */
612 if (!(flags & LOCKSHARED))
616 * For lookups during open(), if the mount point supports
617 * extended shared operations, then use a shared lock for the
618 * leaf node, otherwise use an exclusive lock.
620 if ((flags & ISOPEN) != 0)
621 return (!MNT_EXTENDED_SHARED(mp));
624 * Lookup requests outside of open() that specify LOCKSHARED
625 * only need a shared lock on the leaf vnode.
632 * This is a very central and rather complicated routine.
634 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
635 * The starting directory is taken from ni_startdir. The pathname is
636 * descended until done, or a symbolic link is encountered. The variable
637 * ni_more is clear if the path is completed; it is set to one if a
638 * symbolic link needing interpretation is encountered.
640 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
641 * whether the name is to be looked up, created, renamed, or deleted.
642 * When CREATE, RENAME, or DELETE is specified, information usable in
643 * creating, renaming, or deleting a directory entry may be calculated.
644 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
645 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
646 * returned unlocked. Otherwise the parent directory is not returned. If
647 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
648 * the target is returned locked, otherwise it is returned unlocked.
649 * When creating or renaming and LOCKPARENT is specified, the target may not
650 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
652 * Overall outline of lookup:
655 * identify next component of name at ndp->ni_ptr
656 * handle degenerate case where name is null string
657 * if .. and crossing mount points and on mounted filesys, find parent
658 * call VOP_LOOKUP routine for next component name
659 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
660 * component vnode returned in ni_vp (if it exists), locked.
661 * if result vnode is mounted on and crossing mount points,
662 * find mounted on vnode
663 * if more components of name, do next level at dirloop
664 * return the answer in ni_vp, locked if LOCKLEAF set
665 * if LOCKPARENT set, return locked parent in ni_dvp
666 * if WANTPARENT set, return unlocked parent in ni_dvp
669 lookup(struct nameidata *ndp)
671 char *cp; /* pointer into pathname argument */
672 char *prev_ni_next; /* saved ndp->ni_next */
673 struct vnode *dp = NULL; /* the directory we are searching */
674 struct vnode *tdp; /* saved dp */
675 struct mount *mp; /* mount table entry */
677 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */
678 int docache; /* == 0 do not cache last component */
679 int wantparent; /* 1 => wantparent or lockparent flag */
680 int rdonly; /* lookup read-only flag bit */
682 int dpunlocked = 0; /* dp has already been unlocked */
683 int relookup = 0; /* do not consume the path component */
684 struct componentname *cnp = &ndp->ni_cnd;
689 * Setup: break out flag bits into variables.
692 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
693 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
694 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
695 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
696 if (cnp->cn_nameiop == DELETE ||
697 (wantparent && cnp->cn_nameiop != CREATE &&
698 cnp->cn_nameiop != LOOKUP))
700 rdonly = cnp->cn_flags & RDONLY;
701 cnp->cn_flags &= ~ISSYMLINK;
704 * We use shared locks until we hit the parent of the last cn then
705 * we adjust based on the requesting flags.
707 cnp->cn_lkflags = LK_SHARED;
708 dp = ndp->ni_startdir;
709 ndp->ni_startdir = NULLVP;
711 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
716 * Search a new directory.
718 * The last component of the filename is left accessible via
719 * cnp->cn_nameptr for callers that need the name. Callers needing
720 * the name set the SAVENAME flag. When done, they assume
721 * responsibility for freeing the pathname buffer.
723 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
725 cnp->cn_namelen = cp - cnp->cn_nameptr;
726 if (cnp->cn_namelen > NAME_MAX) {
727 error = ENAMETOOLONG;
730 #ifdef NAMEI_DIAGNOSTIC
733 printf("{%s}: ", cnp->cn_nameptr);
736 prev_ni_pathlen = ndp->ni_pathlen;
737 ndp->ni_pathlen -= cnp->cn_namelen;
738 KASSERT(ndp->ni_pathlen <= PATH_MAX,
739 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
740 prev_ni_next = ndp->ni_next;
744 * Replace multiple slashes by a single slash and trailing slashes
745 * by a null. This must be done before VOP_LOOKUP() because some
746 * fs's don't know about trailing slashes. Remember if there were
747 * trailing slashes to handle symlinks, existing non-directories
748 * and non-existing files that won't be directories specially later.
750 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
754 *ndp->ni_next = '\0';
755 cnp->cn_flags |= TRAILINGSLASH;
760 cnp->cn_flags |= MAKEENTRY;
761 if (*cp == '\0' && docache == 0)
762 cnp->cn_flags &= ~MAKEENTRY;
763 if (cnp->cn_namelen == 2 &&
764 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
765 cnp->cn_flags |= ISDOTDOT;
767 cnp->cn_flags &= ~ISDOTDOT;
768 if (*ndp->ni_next == 0)
769 cnp->cn_flags |= ISLASTCN;
771 cnp->cn_flags &= ~ISLASTCN;
773 if ((cnp->cn_flags & ISLASTCN) != 0 &&
774 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
775 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
780 nameicap_tracker_add(ndp, dp);
783 * Check for degenerate name (e.g. / or "")
784 * which is a way of talking about a directory,
785 * e.g. like "/." or ".".
787 if (cnp->cn_nameptr[0] == '\0') {
788 if (dp->v_type != VDIR) {
792 if (cnp->cn_nameiop != LOOKUP) {
802 if (cnp->cn_flags & AUDITVNODE1)
803 AUDIT_ARG_VNODE1(dp);
804 else if (cnp->cn_flags & AUDITVNODE2)
805 AUDIT_ARG_VNODE2(dp);
807 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
809 /* XXX This should probably move to the top of function. */
810 if (cnp->cn_flags & SAVESTART)
811 panic("lookup: SAVESTART");
816 * Handle "..": five special cases.
817 * 0. If doing a capability lookup and lookup_cap_dotdot is
818 * disabled, return ENOTCAPABLE.
819 * 1. Return an error if this is the last component of
820 * the name and the operation is DELETE or RENAME.
821 * 2. If at root directory (e.g. after chroot)
822 * or at absolute root directory
823 * then ignore it so can't get out.
824 * 3. If this vnode is the root of a mounted
825 * filesystem, then replace it with the
826 * vnode which was mounted on so we take the
827 * .. in the other filesystem.
828 * 4. If the vnode is the top directory of
829 * the jail or chroot, don't let them out.
830 * 5. If doing a capability lookup and lookup_cap_dotdot is
831 * enabled, return ENOTCAPABLE if the lookup would escape
832 * from the initial file descriptor directory. Checks are
833 * done by ensuring that namei() already traversed the
834 * result of dotdot lookup.
836 if (cnp->cn_flags & ISDOTDOT) {
837 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
838 == NI_LCF_STRICTRELATIVE) {
840 if (KTRPOINT(curthread, KTR_CAPFAIL))
841 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
846 if ((cnp->cn_flags & ISLASTCN) != 0 &&
847 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
852 for (pr = cnp->cn_cred->cr_prison; pr != NULL;
854 if (dp == pr->pr_root)
856 if (dp == ndp->ni_rootdir ||
857 dp == ndp->ni_topdir ||
860 ((dp->v_vflag & VV_ROOT) != 0 &&
861 (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
867 if ((dp->v_vflag & VV_ROOT) == 0)
869 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */
874 dp = dp->v_mount->mnt_vnodecovered;
878 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
879 LK_RETRY, ISDOTDOT));
880 error = nameicap_check_dotdot(ndp, dp);
883 if (KTRPOINT(curthread, KTR_CAPFAIL))
884 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
892 * We now have a segment name to search for, and a directory to search.
896 if ((cnp->cn_flags & NOMACCHECK) == 0) {
897 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp,
905 ASSERT_VOP_LOCKED(dp, "lookup");
907 * If we have a shared lock we may need to upgrade the lock for the
910 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
911 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
912 vn_lock(dp, LK_UPGRADE|LK_RETRY);
913 if ((dp->v_iflag & VI_DOOMED) != 0) {
918 * If we're looking up the last component and we need an exclusive
919 * lock, adjust our lkflags.
921 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
922 cnp->cn_lkflags = LK_EXCLUSIVE;
923 #ifdef NAMEI_DIAGNOSTIC
924 vn_printf(dp, "lookup in ");
926 lkflags_save = cnp->cn_lkflags;
927 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
929 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
930 cnp->cn_lkflags = lkflags_save;
932 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
933 #ifdef NAMEI_DIAGNOSTIC
934 printf("not found\n");
936 if ((error == ENOENT) &&
937 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
938 (dp->v_mount->mnt_flag & MNT_UNION)) {
940 dp = dp->v_mount->mnt_vnodecovered;
944 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
945 LK_RETRY, cnp->cn_flags));
946 nameicap_tracker_add(ndp, dp);
950 if (error == ERELOOKUP) {
958 if (error != EJUSTRETURN)
961 * At this point, we know we're at the end of the
962 * pathname. If creating / renaming, we can consider
963 * allowing the file or directory to be created / renamed,
964 * provided we're not on a read-only filesystem.
970 /* trailing slash only allowed for directories */
971 if ((cnp->cn_flags & TRAILINGSLASH) &&
972 !(cnp->cn_flags & WILLBEDIR)) {
976 if ((cnp->cn_flags & LOCKPARENT) == 0)
979 * We return with ni_vp NULL to indicate that the entry
980 * doesn't currently exist, leaving a pointer to the
981 * (possibly locked) directory vnode in ndp->ni_dvp.
983 if (cnp->cn_flags & SAVESTART) {
984 ndp->ni_startdir = ndp->ni_dvp;
985 VREF(ndp->ni_startdir);
991 #ifdef NAMEI_DIAGNOSTIC
997 * Check to see if the vnode has been mounted on;
998 * if so find the root of the mounted filesystem.
1000 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1001 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1002 if (vfs_busy(mp, 0))
1005 if (dp != ndp->ni_dvp)
1009 vrefact(vp_crossmp);
1010 ndp->ni_dvp = vp_crossmp;
1011 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1012 cnp->cn_flags), &tdp);
1014 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1015 panic("vp_crossmp exclusively locked or reclaimed");
1020 ndp->ni_vp = dp = tdp;
1024 * Check for symbolic link
1026 if ((dp->v_type == VLNK) &&
1027 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1028 *ndp->ni_next == '/')) {
1029 cnp->cn_flags |= ISSYMLINK;
1030 if (dp->v_iflag & VI_DOOMED) {
1032 * We can't know whether the directory was mounted with
1033 * NOSYMFOLLOW, so we can't follow safely.
1038 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1043 * Symlink code always expects an unlocked dvp.
1045 if (ndp->ni_dvp != ndp->ni_vp) {
1046 VOP_UNLOCK(ndp->ni_dvp, 0);
1047 ni_dvp_unlocked = 1;
1054 * Not a symbolic link that we will follow. Continue with the
1055 * next component if there is any; otherwise, we're done.
1057 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1058 ("lookup: invalid path state."));
1061 ndp->ni_pathlen = prev_ni_pathlen;
1062 ndp->ni_next = prev_ni_next;
1063 if (ndp->ni_dvp != dp)
1069 if (cnp->cn_flags & ISDOTDOT) {
1070 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1073 if (KTRPOINT(curthread, KTR_CAPFAIL))
1074 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1079 if (*ndp->ni_next == '/') {
1080 cnp->cn_nameptr = ndp->ni_next;
1081 while (*cnp->cn_nameptr == '/') {
1085 if (ndp->ni_dvp != dp)
1092 * If we're processing a path with a trailing slash,
1093 * check that the end result is a directory.
1095 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1100 * Disallow directory write attempts on read-only filesystems.
1103 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1107 if (cnp->cn_flags & SAVESTART) {
1108 ndp->ni_startdir = ndp->ni_dvp;
1109 VREF(ndp->ni_startdir);
1112 ni_dvp_unlocked = 2;
1113 if (ndp->ni_dvp != dp)
1117 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1118 VOP_UNLOCK(ndp->ni_dvp, 0);
1119 ni_dvp_unlocked = 1;
1122 if (cnp->cn_flags & AUDITVNODE1)
1123 AUDIT_ARG_VNODE1(dp);
1124 else if (cnp->cn_flags & AUDITVNODE2)
1125 AUDIT_ARG_VNODE2(dp);
1127 if ((cnp->cn_flags & LOCKLEAF) == 0)
1131 * Because of shared lookup we may have the vnode shared locked, but
1132 * the caller may want it to be exclusively locked.
1134 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1135 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1136 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1137 if (dp->v_iflag & VI_DOOMED) {
1145 if (ni_dvp_unlocked != 2) {
1146 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1159 * relookup - lookup a path name component
1160 * Used by lookup to re-acquire things.
1163 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1165 struct vnode *dp = NULL; /* the directory we are searching */
1166 int wantparent; /* 1 => wantparent or lockparent flag */
1167 int rdonly; /* lookup read-only flag bit */
1170 KASSERT(cnp->cn_flags & ISLASTCN,
1171 ("relookup: Not given last component."));
1173 * Setup: break out flag bits into variables.
1175 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1176 KASSERT(wantparent, ("relookup: parent not wanted."));
1177 rdonly = cnp->cn_flags & RDONLY;
1178 cnp->cn_flags &= ~ISSYMLINK;
1180 cnp->cn_lkflags = LK_EXCLUSIVE;
1181 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1184 * Search a new directory.
1186 * The last component of the filename is left accessible via
1187 * cnp->cn_nameptr for callers that need the name. Callers needing
1188 * the name set the SAVENAME flag. When done, they assume
1189 * responsibility for freeing the pathname buffer.
1191 #ifdef NAMEI_DIAGNOSTIC
1192 printf("{%s}: ", cnp->cn_nameptr);
1196 * Check for "" which represents the root directory after slash
1199 if (cnp->cn_nameptr[0] == '\0') {
1201 * Support only LOOKUP for "/" because lookup()
1202 * can't succeed for CREATE, DELETE and RENAME.
1204 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1205 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1207 if (!(cnp->cn_flags & LOCKLEAF))
1210 /* XXX This should probably move to the top of function. */
1211 if (cnp->cn_flags & SAVESTART)
1212 panic("lookup: SAVESTART");
1216 if (cnp->cn_flags & ISDOTDOT)
1217 panic ("relookup: lookup on dot-dot");
1220 * We now have a segment name to search for, and a directory to search.
1222 #ifdef NAMEI_DIAGNOSTIC
1223 vn_printf(dp, "search in ");
1225 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1226 KASSERT(*vpp == NULL, ("leaf should be empty"));
1227 if (error != EJUSTRETURN)
1230 * If creating and at end of pathname, then can consider
1231 * allowing file to be created.
1237 /* ASSERT(dvp == ndp->ni_startdir) */
1238 if (cnp->cn_flags & SAVESTART)
1240 if ((cnp->cn_flags & LOCKPARENT) == 0)
1243 * We return with ni_vp NULL to indicate that the entry
1244 * doesn't currently exist, leaving a pointer to the
1245 * (possibly locked) directory vnode in ndp->ni_dvp.
1253 * Disallow directory write attempts on read-only filesystems.
1256 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1265 * Set the parent lock/ref state to the requested state.
1267 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1272 } else if (!wantparent)
1275 * Check for symbolic link
1277 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1278 ("relookup: symlink found.\n"));
1280 /* ASSERT(dvp == ndp->ni_startdir) */
1281 if (cnp->cn_flags & SAVESTART)
1284 if ((cnp->cn_flags & LOCKLEAF) == 0)
1294 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg,
1295 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp,
1299 ndp->ni_cnd.cn_nameiop = op;
1300 ndp->ni_cnd.cn_flags = flags;
1301 ndp->ni_segflg = segflg;
1302 ndp->ni_dirp = namep;
1303 ndp->ni_dirfd = dirfd;
1304 ndp->ni_startdir = startdir;
1305 filecaps_init(&ndp->ni_filecaps);
1306 ndp->ni_cnd.cn_thread = td;
1307 if (rightsp != NULL)
1308 ndp->ni_rightsneeded = *rightsp;
1310 cap_rights_init(&ndp->ni_rightsneeded);
1314 * Free data allocated by namei(); see namei(9) for details.
1317 NDFREE(struct nameidata *ndp, const u_int flags)
1325 if (!(flags & NDF_NO_FREE_PNBUF) &&
1326 (ndp->ni_cnd.cn_flags & HASBUF)) {
1327 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1328 ndp->ni_cnd.cn_flags &= ~HASBUF;
1330 if (!(flags & NDF_NO_VP_UNLOCK) &&
1331 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1333 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1342 VOP_UNLOCK(ndp->ni_vp, 0);
1343 if (!(flags & NDF_NO_DVP_UNLOCK) &&
1344 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1345 ndp->ni_dvp != ndp->ni_vp)
1347 if (!(flags & NDF_NO_DVP_RELE) &&
1348 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1357 VOP_UNLOCK(ndp->ni_dvp, 0);
1358 if (!(flags & NDF_NO_STARTDIR_RELE) &&
1359 (ndp->ni_cnd.cn_flags & SAVESTART)) {
1360 vrele(ndp->ni_startdir);
1361 ndp->ni_startdir = NULL;
1366 * Determine if there is a suitable alternate filename under the specified
1367 * prefix for the specified path. If the create flag is set, then the
1368 * alternate prefix will be used so long as the parent directory exists.
1369 * This is used by the various compatibility ABIs so that Linux binaries prefer
1370 * files under /compat/linux for example. The chosen path (whether under
1371 * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1372 * to by pathbuf. The caller is responsible for free'ing the buffer from
1373 * the M_TEMP bucket if one is returned.
1376 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1377 enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1379 struct nameidata nd, ndroot;
1380 char *ptr, *buf, *cp;
1384 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1387 /* Copy the prefix into the new pathname as a starting point. */
1388 len = strlcpy(buf, prefix, MAXPATHLEN);
1389 if (len >= MAXPATHLEN) {
1394 sz = MAXPATHLEN - len;
1397 /* Append the filename to the prefix. */
1398 if (pathseg == UIO_SYSSPACE)
1399 error = copystr(path, ptr, sz, &len);
1401 error = copyinstr(path, ptr, sz, &len);
1409 /* Only use a prefix with absolute pathnames. */
1415 if (dirfd != AT_FDCWD) {
1417 * We want the original because the "prefix" is
1418 * included in the already opened dirfd.
1420 bcopy(ptr, buf, len);
1425 * We know that there is a / somewhere in this pathname.
1426 * Search backwards for it, to find the file's parent dir
1427 * to see if it exists in the alternate tree. If it does,
1428 * and we want to create a file (cflag is set). We don't
1429 * need to worry about the root comparison in this case.
1433 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1436 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1442 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1449 * We now compare the vnode of the prefix to the one
1450 * vnode asked. If they resolve to be the same, then we
1451 * ignore the match so that the real root gets used.
1452 * This avoids the problem of traversing "../.." to find the
1453 * root directory and never finding it, because "/" resolves
1454 * to the emulation root directory. This is expensive :-(
1456 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1459 /* We shouldn't ever get an error from this namei(). */
1460 error = namei(&ndroot);
1462 if (nd.ni_vp == ndroot.ni_vp)
1465 NDFREE(&ndroot, NDF_ONLY_PNBUF);
1466 vrele(ndroot.ni_vp);
1470 NDFREE(&nd, NDF_ONLY_PNBUF);
1474 /* If there was an error, use the original path name. */
1476 bcopy(ptr, buf, len);