]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/vfs_lookup.c
Only clear latch for BENEATH when we walk out of the startdir,
[FreeBSD/FreeBSD.git] / sys / kern / vfs_lookup.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
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.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  *      @(#)vfs_lookup.c        8.4 (Berkeley) 2/16/94
37  */
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 #include "opt_capsicum.h"
43 #include "opt_ktrace.h"
44
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>
50 #include <sys/jail.h>
51 #include <sys/lock.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>
57 #include <sys/proc.h>
58 #include <sys/sdt.h>
59 #include <sys/syscallsubr.h>
60 #include <sys/sysctl.h>
61 #ifdef KTRACE
62 #include <sys/ktrace.h>
63 #endif
64 #ifdef INVARIANTS
65 #include <machine/_inttypes.h>
66 #endif
67
68 #include <security/audit/audit.h>
69 #include <security/mac/mac_framework.h>
70
71 #include <vm/uma.h>
72
73 #define NAMEI_DIAGNOSTIC 1
74 #undef NAMEI_DIAGNOSTIC
75
76 SDT_PROVIDER_DEFINE(vfs);
77 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *",
78     "unsigned long", "bool");
79 SDT_PROBE_DEFINE3(vfs, namei, lookup, return, "int", "struct vnode *", "bool");
80
81 /* Allocation zone for namei. */
82 uma_zone_t namei_zone;
83
84 /* Placeholder vnode for mp traversal. */
85 static struct vnode *vp_crossmp;
86
87 static int
88 crossmp_vop_islocked(struct vop_islocked_args *ap)
89 {
90
91         return (LK_SHARED);
92 }
93
94 static int
95 crossmp_vop_lock1(struct vop_lock1_args *ap)
96 {
97         struct vnode *vp;
98         struct lock *lk __unused;
99         const char *file __unused;
100         int flags, line __unused;
101
102         vp = ap->a_vp;
103         lk = vp->v_vnlock;
104         flags = ap->a_flags;
105         file = ap->a_file;
106         line = ap->a_line;
107
108         if ((flags & LK_SHARED) == 0)
109                 panic("invalid lock request for crossmp");
110
111         WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line,
112             flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL);
113         WITNESS_LOCK(&lk->lock_object, 0, file, line);
114         if ((flags & LK_INTERLOCK) != 0)
115                 VI_UNLOCK(vp);
116         LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line);
117         return (0);
118 }
119
120 static int
121 crossmp_vop_unlock(struct vop_unlock_args *ap)
122 {
123         struct vnode *vp;
124         struct lock *lk __unused;
125
126         vp = ap->a_vp;
127         lk = vp->v_vnlock;
128
129         WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE);
130         LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE,
131             LOCK_LINE);
132         return (0);
133 }
134
135 static struct vop_vector crossmp_vnodeops = {
136         .vop_default =          &default_vnodeops,
137         .vop_islocked =         crossmp_vop_islocked,
138         .vop_lock1 =            crossmp_vop_lock1,
139         .vop_unlock =           crossmp_vop_unlock,
140 };
141 /*
142  * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode
143  * gets allocated early. See nameiinit for the direct call below.
144  */
145
146 struct nameicap_tracker {
147         struct vnode *dp;
148         TAILQ_ENTRY(nameicap_tracker) nm_link;
149 };
150
151 /* Zone for cap mode tracker elements used for dotdot capability checks. */
152 static uma_zone_t nt_zone;
153
154 static void
155 nameiinit(void *dummy __unused)
156 {
157
158         namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL,
159             UMA_ALIGN_PTR, 0);
160         nt_zone = uma_zcreate("rentr", sizeof(struct nameicap_tracker),
161             NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
162         vfs_vector_op_register(&crossmp_vnodeops);
163         getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp);
164 }
165 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL);
166
167 static int lookup_cap_dotdot = 1;
168 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN,
169     &lookup_cap_dotdot, 0,
170     "enables \"..\" components in path lookup in capability mode");
171 static int lookup_cap_dotdot_nonlocal = 1;
172 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN,
173     &lookup_cap_dotdot_nonlocal, 0,
174     "enables \"..\" components in path lookup in capability mode "
175     "on non-local mount");
176
177 static void
178 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp)
179 {
180         struct nameicap_tracker *nt;
181
182         if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR)
183                 return;
184         if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_BENEATH_LATCHED)) ==
185             NI_LCF_BENEATH_ABS) {
186                 MPASS((ndp->ni_lcf & NI_LCF_LATCH) != 0);
187                 if (dp != ndp->ni_beneath_latch)
188                         return;
189                 ndp->ni_lcf |= NI_LCF_BENEATH_LATCHED;
190         }
191         nt = uma_zalloc(nt_zone, M_WAITOK);
192         vhold(dp);
193         nt->dp = dp;
194         TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link);
195 }
196
197 static void
198 nameicap_cleanup(struct nameidata *ndp, bool clean_latch)
199 {
200         struct nameicap_tracker *nt, *nt1;
201
202         KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) ||
203             (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative"));
204         TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) {
205                 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link);
206                 vdrop(nt->dp);
207                 uma_zfree(nt_zone, nt);
208         }
209         if (clean_latch && (ndp->ni_lcf & NI_LCF_LATCH) != 0) {
210                 ndp->ni_lcf &= ~NI_LCF_LATCH;
211                 vrele(ndp->ni_beneath_latch);
212         }
213 }
214
215 /*
216  * For dotdot lookups in capability mode, only allow the component
217  * lookup to succeed if the resulting directory was already traversed
218  * during the operation.  This catches situations where already
219  * traversed directory is moved to different parent, and then we walk
220  * over it with dotdots.
221  *
222  * Also allow to force failure of dotdot lookups for non-local
223  * filesystems, where external agents might assist local lookups to
224  * escape the compartment.
225  */
226 static int
227 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp)
228 {
229         struct nameicap_tracker *nt;
230         struct mount *mp;
231
232         if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL ||
233             dp->v_type != VDIR)
234                 return (0);
235         mp = dp->v_mount;
236         if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL &&
237             (mp->mnt_flag & MNT_LOCAL) == 0)
238                 return (ENOTCAPABLE);
239         TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head,
240             nm_link) {
241                 if ((ndp->ni_lcf & NI_LCF_LATCH) != 0 &&
242                     ndp->ni_beneath_latch == nt->dp) {
243                         ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
244                         nameicap_cleanup(ndp, false);
245                         return (0);
246                 }
247                 if (dp == nt->dp)
248                         return (0);
249         }
250         return (ENOTCAPABLE);
251 }
252
253 static void
254 namei_cleanup_cnp(struct componentname *cnp)
255 {
256
257         uma_zfree(namei_zone, cnp->cn_pnbuf);
258 #ifdef DIAGNOSTIC
259         cnp->cn_pnbuf = NULL;
260         cnp->cn_nameptr = NULL;
261 #endif
262 }
263
264 static int
265 namei_handle_root(struct nameidata *ndp, struct vnode **dpp)
266 {
267         struct componentname *cnp;
268
269         cnp = &ndp->ni_cnd;
270         if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) {
271 #ifdef KTRACE
272                 if (KTRPOINT(curthread, KTR_CAPFAIL))
273                         ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
274 #endif
275                 return (ENOTCAPABLE);
276         }
277         if ((cnp->cn_flags & BENEATH) != 0) {
278                 ndp->ni_lcf |= NI_LCF_BENEATH_ABS;
279                 ndp->ni_lcf &= ~NI_LCF_BENEATH_LATCHED;
280                 nameicap_cleanup(ndp, false);
281         }
282         while (*(cnp->cn_nameptr) == '/') {
283                 cnp->cn_nameptr++;
284                 ndp->ni_pathlen--;
285         }
286         *dpp = ndp->ni_rootdir;
287         vrefact(*dpp);
288         return (0);
289 }
290
291 static int
292 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp)
293 {
294         struct componentname *cnp;
295         struct file *dfp;
296         struct thread *td;
297         struct pwd *pwd;
298         cap_rights_t rights;
299         struct filecaps dirfd_caps;
300         int error;
301         bool startdir_used;
302
303         cnp = &ndp->ni_cnd;
304         td = cnp->cn_thread;
305
306         startdir_used = false;
307         *pwdp = NULL;
308         *dpp = NULL;
309
310 #ifdef CAPABILITY_MODE
311         /*
312          * In capability mode, lookups must be restricted to happen in
313          * the subtree with the root specified by the file descriptor:
314          * - The root must be real file descriptor, not the pseudo-descriptor
315          *   AT_FDCWD.
316          * - The passed path must be relative and not absolute.
317          * - If lookup_cap_dotdot is disabled, path must not contain the
318          *   '..' components.
319          * - If lookup_cap_dotdot is enabled, we verify that all '..'
320          *   components lookups result in the directories which were
321          *   previously walked by us, which prevents an escape from
322          *   the relative root.
323          */
324         if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) {
325                 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
326                 ndp->ni_resflags |= NIRES_STRICTREL;
327                 if (ndp->ni_dirfd == AT_FDCWD) {
328 #ifdef KTRACE
329                         if (KTRPOINT(td, KTR_CAPFAIL))
330                                 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
331 #endif
332                         return (ECAPMODE);
333                 }
334         }
335 #endif
336         error = 0;
337
338         /*
339          * Get starting point for the translation.
340          */
341         pwd = pwd_hold(td);
342         /*
343          * The reference on ni_rootdir is acquired in the block below to avoid
344          * back-to-back atomics for absolute lookups.
345          */
346         ndp->ni_rootdir = pwd->pwd_rdir;
347         ndp->ni_topdir = pwd->pwd_jdir;
348
349         if (cnp->cn_pnbuf[0] == '/') {
350                 ndp->ni_resflags |= NIRES_ABS;
351                 error = namei_handle_root(ndp, dpp);
352         } else {
353                 if (ndp->ni_startdir != NULL) {
354                         *dpp = ndp->ni_startdir;
355                         startdir_used = true;
356                 } else if (ndp->ni_dirfd == AT_FDCWD) {
357                         *dpp = pwd->pwd_cdir;
358                         vrefact(*dpp);
359                 } else {
360                         rights = *ndp->ni_rightsneeded;
361                         cap_rights_set_one(&rights, CAP_LOOKUP);
362
363                         if (cnp->cn_flags & AUDITVNODE1)
364                                 AUDIT_ARG_ATFD1(ndp->ni_dirfd);
365                         if (cnp->cn_flags & AUDITVNODE2)
366                                 AUDIT_ARG_ATFD2(ndp->ni_dirfd);
367                         /*
368                          * Effectively inlined fgetvp_rights, because we need to
369                          * inspect the file as well as grabbing the vnode.
370                          */
371                         error = fget_cap(td, ndp->ni_dirfd, &rights,
372                             &dfp, &ndp->ni_filecaps);
373                         if (error != 0) {
374                                 /*
375                                  * Preserve the error; it should either be EBADF
376                                  * or capability-related, both of which can be
377                                  * safely returned to the caller.
378                                  */
379                         } else {
380                                 if (dfp->f_ops == &badfileops) {
381                                         error = EBADF;
382                                 } else if (dfp->f_vnode == NULL) {
383                                         error = ENOTDIR;
384                                 } else {
385                                         *dpp = dfp->f_vnode;
386                                         vrefact(*dpp);
387
388                                         if ((dfp->f_flag & FSEARCH) != 0)
389                                                 cnp->cn_flags |= NOEXECCHECK;
390                                 }
391                                 fdrop(dfp, td);
392                         }
393 #ifdef CAPABILITIES
394                         /*
395                          * If file descriptor doesn't have all rights,
396                          * all lookups relative to it must also be
397                          * strictly relative.
398                          */
399                         CAP_ALL(&rights);
400                         if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights,
401                             &rights) ||
402                             ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL ||
403                             ndp->ni_filecaps.fc_nioctls != -1) {
404                                 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE;
405                                 ndp->ni_resflags |= NIRES_STRICTREL;
406                         }
407 #endif
408                 }
409                 if (error == 0 && (*dpp)->v_type != VDIR)
410                         error = ENOTDIR;
411         }
412         if (error == 0 && (cnp->cn_flags & BENEATH) != 0) {
413                 if (ndp->ni_dirfd == AT_FDCWD) {
414                         ndp->ni_beneath_latch = pwd->pwd_cdir;
415                         vrefact(ndp->ni_beneath_latch);
416                 } else {
417                         rights = *ndp->ni_rightsneeded;
418                         cap_rights_set_one(&rights, CAP_LOOKUP);
419                         error = fgetvp_rights(td, ndp->ni_dirfd, &rights,
420                             &dirfd_caps, &ndp->ni_beneath_latch);
421                         if (error == 0 && (*dpp)->v_type != VDIR) {
422                                 vrele(ndp->ni_beneath_latch);
423                                 error = ENOTDIR;
424                         }
425                 }
426                 if (error == 0)
427                         ndp->ni_lcf |= NI_LCF_LATCH;
428         }
429         /*
430          * If we are auditing the kernel pathname, save the user pathname.
431          */
432         if (cnp->cn_flags & AUDITVNODE1)
433                 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
434         if (cnp->cn_flags & AUDITVNODE2)
435                 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf);
436         if (ndp->ni_startdir != NULL && !startdir_used)
437                 vrele(ndp->ni_startdir);
438         if (error != 0) {
439                 if (*dpp != NULL)
440                         vrele(*dpp);
441                 pwd_drop(pwd);
442                 return (error);
443         }
444         MPASS((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | NI_LCF_LATCH)) !=
445             NI_LCF_BENEATH_ABS);
446         if (((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 &&
447             lookup_cap_dotdot != 0) ||
448             ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0 &&
449             (cnp->cn_flags & BENEATH) != 0))
450                 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT;
451         SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf,
452             cnp->cn_flags, false);
453         *pwdp = pwd;
454         return (0);
455 }
456
457 /*
458  * Convert a pathname into a pointer to a locked vnode.
459  *
460  * The FOLLOW flag is set when symbolic links are to be followed
461  * when they occur at the end of the name translation process.
462  * Symbolic links are always followed for all other pathname
463  * components other than the last.
464  *
465  * The segflg defines whether the name is to be copied from user
466  * space or kernel space.
467  *
468  * Overall outline of namei:
469  *
470  *      copy in name
471  *      get starting directory
472  *      while (!done && !error) {
473  *              call lookup to search path.
474  *              if symbolic link, massage name in buffer and continue
475  *      }
476  */
477 int
478 namei(struct nameidata *ndp)
479 {
480         char *cp;               /* pointer into pathname argument */
481         struct vnode *dp;       /* the directory we are searching */
482         struct iovec aiov;              /* uio for reading symbolic links */
483         struct componentname *cnp;
484         struct thread *td;
485         struct pwd *pwd;
486         struct uio auio;
487         int error, linklen;
488         enum cache_fpl_status status;
489
490         cnp = &ndp->ni_cnd;
491         td = cnp->cn_thread;
492 #ifdef INVARIANTS
493         /*
494          * For NDVALIDATE.
495          *
496          * While NDINIT may seem like a more natural place to do it, there are
497          * callers which directly modify flags past invoking init.
498          */
499         cnp->cn_origflags = cnp->cn_flags;
500 #endif
501         ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred;
502         KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc"));
503         KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0,
504             ("namei: unexpected flags: %" PRIx64 "\n",
505             cnp->cn_flags & NAMEI_INTERNAL_FLAGS));
506         if (cnp->cn_flags & NOCACHE)
507                 KASSERT(cnp->cn_nameiop != LOOKUP,
508                     ("%s: NOCACHE passed with LOOKUP", __func__));
509         MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR ||
510             ndp->ni_startdir->v_type == VBAD);
511
512         ndp->ni_lcf = 0;
513         ndp->ni_vp = NULL;
514
515         /*
516          * Get a buffer for the name to be translated, and copy the
517          * name into the buffer.
518          */
519         cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK);
520         if (ndp->ni_segflg == UIO_SYSSPACE)
521                 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
522                     &ndp->ni_pathlen);
523         else
524                 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN,
525                     &ndp->ni_pathlen);
526
527         if (__predict_false(error != 0)) {
528                 namei_cleanup_cnp(cnp);
529                 return (error);
530         }
531
532         /*
533          * Don't allow empty pathnames.
534          */
535         if (__predict_false(*cnp->cn_pnbuf == '\0')) {
536                 namei_cleanup_cnp(cnp);
537                 return (ENOENT);
538         }
539
540 #ifdef KTRACE
541         if (KTRPOINT(td, KTR_NAMEI)) {
542                 KASSERT(cnp->cn_thread == curthread,
543                     ("namei not using curthread"));
544                 ktrnamei(cnp->cn_pnbuf);
545         }
546 #endif
547
548         cnp->cn_nameptr = cnp->cn_pnbuf;
549
550         /*
551          * First try looking up the target without locking any vnodes.
552          *
553          * We may need to start from scratch or pick up where it left off.
554          */
555         error = cache_fplookup(ndp, &status, &pwd);
556         switch (status) {
557         case CACHE_FPL_STATUS_UNSET:
558                 __assert_unreachable();
559                 break;
560         case CACHE_FPL_STATUS_HANDLED:
561                 if (error == 0)
562                         NDVALIDATE(ndp);
563                 return (error);
564         case CACHE_FPL_STATUS_PARTIAL:
565                 TAILQ_INIT(&ndp->ni_cap_tracker);
566                 dp = ndp->ni_startdir;
567                 break;
568         case CACHE_FPL_STATUS_ABORTED:
569                 TAILQ_INIT(&ndp->ni_cap_tracker);
570                 error = namei_setup(ndp, &dp, &pwd);
571                 if (error != 0) {
572                         namei_cleanup_cnp(cnp);
573                         return (error);
574                 }
575                 break;
576         }
577
578         ndp->ni_loopcnt = 0;
579
580         /*
581          * Locked lookup.
582          */
583         for (;;) {
584                 ndp->ni_startdir = dp;
585                 error = lookup(ndp);
586                 if (error != 0)
587                         goto out;
588                 /*
589                  * If not a symbolic link, we're done.
590                  */
591                 if ((cnp->cn_flags & ISSYMLINK) == 0) {
592                         if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
593                                 namei_cleanup_cnp(cnp);
594                         } else
595                                 cnp->cn_flags |= HASBUF;
596                         if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS |
597                             NI_LCF_BENEATH_LATCHED)) == NI_LCF_BENEATH_ABS) {
598                                 NDFREE(ndp, 0);
599                                 error = ENOTCAPABLE;
600                         }
601                         nameicap_cleanup(ndp, true);
602                         SDT_PROBE3(vfs, namei, lookup, return, error,
603                             (error == 0 ? ndp->ni_vp : NULL), false);
604                         pwd_drop(pwd);
605                         if (error == 0)
606                                 NDVALIDATE(ndp);
607                         return (error);
608                 }
609                 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
610                         error = ELOOP;
611                         break;
612                 }
613 #ifdef MAC
614                 if ((cnp->cn_flags & NOMACCHECK) == 0) {
615                         error = mac_vnode_check_readlink(td->td_ucred,
616                             ndp->ni_vp);
617                         if (error != 0)
618                                 break;
619                 }
620 #endif
621                 if (ndp->ni_pathlen > 1)
622                         cp = uma_zalloc(namei_zone, M_WAITOK);
623                 else
624                         cp = cnp->cn_pnbuf;
625                 aiov.iov_base = cp;
626                 aiov.iov_len = MAXPATHLEN;
627                 auio.uio_iov = &aiov;
628                 auio.uio_iovcnt = 1;
629                 auio.uio_offset = 0;
630                 auio.uio_rw = UIO_READ;
631                 auio.uio_segflg = UIO_SYSSPACE;
632                 auio.uio_td = td;
633                 auio.uio_resid = MAXPATHLEN;
634                 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
635                 if (error != 0) {
636                         if (ndp->ni_pathlen > 1)
637                                 uma_zfree(namei_zone, cp);
638                         break;
639                 }
640                 linklen = MAXPATHLEN - auio.uio_resid;
641                 if (linklen == 0) {
642                         if (ndp->ni_pathlen > 1)
643                                 uma_zfree(namei_zone, cp);
644                         error = ENOENT;
645                         break;
646                 }
647                 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
648                         if (ndp->ni_pathlen > 1)
649                                 uma_zfree(namei_zone, cp);
650                         error = ENAMETOOLONG;
651                         break;
652                 }
653                 if (ndp->ni_pathlen > 1) {
654                         bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
655                         uma_zfree(namei_zone, cnp->cn_pnbuf);
656                         cnp->cn_pnbuf = cp;
657                 } else
658                         cnp->cn_pnbuf[linklen] = '\0';
659                 ndp->ni_pathlen += linklen;
660                 vput(ndp->ni_vp);
661                 dp = ndp->ni_dvp;
662                 /*
663                  * Check if root directory should replace current directory.
664                  */
665                 cnp->cn_nameptr = cnp->cn_pnbuf;
666                 if (*(cnp->cn_nameptr) == '/') {
667                         vrele(dp);
668                         error = namei_handle_root(ndp, &dp);
669                         if (error != 0)
670                                 goto out;
671                 }
672         }
673         vput(ndp->ni_vp);
674         ndp->ni_vp = NULL;
675         vrele(ndp->ni_dvp);
676 out:
677         MPASS(error != 0);
678         namei_cleanup_cnp(cnp);
679         nameicap_cleanup(ndp, true);
680         SDT_PROBE3(vfs, namei, lookup, return, error, NULL, false);
681         pwd_drop(pwd);
682         return (error);
683 }
684
685 static int
686 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags)
687 {
688
689         if (mp == NULL || ((lkflags & LK_SHARED) &&
690             (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) ||
691             ((cnflags & ISDOTDOT) &&
692             (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) {
693                 lkflags &= ~LK_SHARED;
694                 lkflags |= LK_EXCLUSIVE;
695         }
696         lkflags |= LK_NODDLKTREAT;
697         return (lkflags);
698 }
699
700 static __inline int
701 needs_exclusive_leaf(struct mount *mp, int flags)
702 {
703
704         /*
705          * Intermediate nodes can use shared locks, we only need to
706          * force an exclusive lock for leaf nodes.
707          */
708         if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF))
709                 return (0);
710
711         /* Always use exclusive locks if LOCKSHARED isn't set. */
712         if (!(flags & LOCKSHARED))
713                 return (1);
714
715         /*
716          * For lookups during open(), if the mount point supports
717          * extended shared operations, then use a shared lock for the
718          * leaf node, otherwise use an exclusive lock.
719          */
720         if ((flags & ISOPEN) != 0)
721                 return (!MNT_EXTENDED_SHARED(mp));
722
723         /*
724          * Lookup requests outside of open() that specify LOCKSHARED
725          * only need a shared lock on the leaf vnode.
726          */
727         return (0);
728 }
729
730 /*
731  * Search a pathname.
732  * This is a very central and rather complicated routine.
733  *
734  * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
735  * The starting directory is taken from ni_startdir. The pathname is
736  * descended until done, or a symbolic link is encountered. The variable
737  * ni_more is clear if the path is completed; it is set to one if a
738  * symbolic link needing interpretation is encountered.
739  *
740  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
741  * whether the name is to be looked up, created, renamed, or deleted.
742  * When CREATE, RENAME, or DELETE is specified, information usable in
743  * creating, renaming, or deleting a directory entry may be calculated.
744  * If flag has LOCKPARENT or'ed into it, the parent directory is returned
745  * locked. If flag has WANTPARENT or'ed into it, the parent directory is
746  * returned unlocked. Otherwise the parent directory is not returned. If
747  * the target of the pathname exists and LOCKLEAF is or'ed into the flag
748  * the target is returned locked, otherwise it is returned unlocked.
749  * When creating or renaming and LOCKPARENT is specified, the target may not
750  * be ".".  When deleting and LOCKPARENT is specified, the target may be ".".
751  *
752  * Overall outline of lookup:
753  *
754  * dirloop:
755  *      identify next component of name at ndp->ni_ptr
756  *      handle degenerate case where name is null string
757  *      if .. and crossing mount points and on mounted filesys, find parent
758  *      call VOP_LOOKUP routine for next component name
759  *          directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
760  *          component vnode returned in ni_vp (if it exists), locked.
761  *      if result vnode is mounted on and crossing mount points,
762  *          find mounted on vnode
763  *      if more components of name, do next level at dirloop
764  *      return the answer in ni_vp, locked if LOCKLEAF set
765  *          if LOCKPARENT set, return locked parent in ni_dvp
766  *          if WANTPARENT set, return unlocked parent in ni_dvp
767  */
768 int
769 lookup(struct nameidata *ndp)
770 {
771         char *cp;                       /* pointer into pathname argument */
772         char *prev_ni_next;             /* saved ndp->ni_next */
773         struct vnode *dp = NULL;        /* the directory we are searching */
774         struct vnode *tdp;              /* saved dp */
775         struct mount *mp;               /* mount table entry */
776         struct prison *pr;
777         size_t prev_ni_pathlen;         /* saved ndp->ni_pathlen */
778         int docache;                    /* == 0 do not cache last component */
779         int wantparent;                 /* 1 => wantparent or lockparent flag */
780         int rdonly;                     /* lookup read-only flag bit */
781         int error = 0;
782         int dpunlocked = 0;             /* dp has already been unlocked */
783         int relookup = 0;               /* do not consume the path component */
784         struct componentname *cnp = &ndp->ni_cnd;
785         int lkflags_save;
786         int ni_dvp_unlocked;
787
788         /*
789          * Setup: break out flag bits into variables.
790          */
791         ni_dvp_unlocked = 0;
792         wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
793         KASSERT(cnp->cn_nameiop == LOOKUP || wantparent,
794             ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT."));
795         /*
796          * When set to zero, docache causes the last component of the
797          * pathname to be deleted from the cache and the full lookup
798          * of the name to be done (via VOP_CACHEDLOOKUP()). Often
799          * filesystems need some pre-computed values that are made
800          * during the full lookup, for instance UFS sets dp->i_offset.
801          *
802          * The docache variable is set to zero when requested by the
803          * NOCACHE flag and for all modifying operations except CREATE.
804          */
805         docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
806         if (cnp->cn_nameiop == DELETE ||
807             (wantparent && cnp->cn_nameiop != CREATE &&
808              cnp->cn_nameiop != LOOKUP))
809                 docache = 0;
810         rdonly = cnp->cn_flags & RDONLY;
811         cnp->cn_flags &= ~ISSYMLINK;
812         ndp->ni_dvp = NULL;
813         /*
814          * We use shared locks until we hit the parent of the last cn then
815          * we adjust based on the requesting flags.
816          */
817         cnp->cn_lkflags = LK_SHARED;
818         dp = ndp->ni_startdir;
819         ndp->ni_startdir = NULLVP;
820         vn_lock(dp,
821             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY,
822             cnp->cn_flags));
823
824 dirloop:
825         /*
826          * Search a new directory.
827          *
828          * The last component of the filename is left accessible via
829          * cnp->cn_nameptr for callers that need the name. Callers needing
830          * the name set the SAVENAME flag. When done, they assume
831          * responsibility for freeing the pathname buffer.
832          */
833         for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
834                 continue;
835         cnp->cn_namelen = cp - cnp->cn_nameptr;
836         if (cnp->cn_namelen > NAME_MAX) {
837                 error = ENAMETOOLONG;
838                 goto bad;
839         }
840 #ifdef NAMEI_DIAGNOSTIC
841         { char c = *cp;
842         *cp = '\0';
843         printf("{%s}: ", cnp->cn_nameptr);
844         *cp = c; }
845 #endif
846         prev_ni_pathlen = ndp->ni_pathlen;
847         ndp->ni_pathlen -= cnp->cn_namelen;
848         KASSERT(ndp->ni_pathlen <= PATH_MAX,
849             ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen));
850         prev_ni_next = ndp->ni_next;
851         ndp->ni_next = cp;
852
853         /*
854          * Replace multiple slashes by a single slash and trailing slashes
855          * by a null.  This must be done before VOP_LOOKUP() because some
856          * fs's don't know about trailing slashes.  Remember if there were
857          * trailing slashes to handle symlinks, existing non-directories
858          * and non-existing files that won't be directories specially later.
859          */
860         while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) {
861                 cp++;
862                 ndp->ni_pathlen--;
863                 if (*cp == '\0') {
864                         *ndp->ni_next = '\0';
865                         cnp->cn_flags |= TRAILINGSLASH;
866                 }
867         }
868         ndp->ni_next = cp;
869
870         cnp->cn_flags |= MAKEENTRY;
871         if (*cp == '\0' && docache == 0)
872                 cnp->cn_flags &= ~MAKEENTRY;
873         if (cnp->cn_namelen == 2 &&
874             cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
875                 cnp->cn_flags |= ISDOTDOT;
876         else
877                 cnp->cn_flags &= ~ISDOTDOT;
878         if (*ndp->ni_next == 0)
879                 cnp->cn_flags |= ISLASTCN;
880         else
881                 cnp->cn_flags &= ~ISLASTCN;
882
883         if ((cnp->cn_flags & ISLASTCN) != 0 &&
884             cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' &&
885             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
886                 error = EINVAL;
887                 goto bad;
888         }
889
890         nameicap_tracker_add(ndp, dp);
891
892         /*
893          * Check for degenerate name (e.g. / or "")
894          * which is a way of talking about a directory,
895          * e.g. like "/." or ".".
896          */
897         if (cnp->cn_nameptr[0] == '\0') {
898                 if (dp->v_type != VDIR) {
899                         error = ENOTDIR;
900                         goto bad;
901                 }
902                 if (cnp->cn_nameiop != LOOKUP) {
903                         error = EISDIR;
904                         goto bad;
905                 }
906                 if (wantparent) {
907                         ndp->ni_dvp = dp;
908                         VREF(dp);
909                 }
910                 ndp->ni_vp = dp;
911
912                 if (cnp->cn_flags & AUDITVNODE1)
913                         AUDIT_ARG_VNODE1(dp);
914                 else if (cnp->cn_flags & AUDITVNODE2)
915                         AUDIT_ARG_VNODE2(dp);
916
917                 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
918                         VOP_UNLOCK(dp);
919                 /* XXX This should probably move to the top of function. */
920                 if (cnp->cn_flags & SAVESTART)
921                         panic("lookup: SAVESTART");
922                 goto success;
923         }
924
925         /*
926          * Handle "..": five special cases.
927          * 0. If doing a capability lookup and lookup_cap_dotdot is
928          *    disabled, return ENOTCAPABLE.
929          * 1. Return an error if this is the last component of
930          *    the name and the operation is DELETE or RENAME.
931          * 2. If at root directory (e.g. after chroot)
932          *    or at absolute root directory
933          *    then ignore it so can't get out.
934          * 3. If this vnode is the root of a mounted
935          *    filesystem, then replace it with the
936          *    vnode which was mounted on so we take the
937          *    .. in the other filesystem.
938          * 4. If the vnode is the top directory of
939          *    the jail or chroot, don't let them out.
940          * 5. If doing a capability lookup and lookup_cap_dotdot is
941          *    enabled, return ENOTCAPABLE if the lookup would escape
942          *    from the initial file descriptor directory.  Checks are
943          *    done by ensuring that namei() already traversed the
944          *    result of dotdot lookup.
945          */
946         if (cnp->cn_flags & ISDOTDOT) {
947                 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT))
948                     == NI_LCF_STRICTRELATIVE) {
949 #ifdef KTRACE
950                         if (KTRPOINT(curthread, KTR_CAPFAIL))
951                                 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
952 #endif
953                         error = ENOTCAPABLE;
954                         goto bad;
955                 }
956                 if ((cnp->cn_flags & ISLASTCN) != 0 &&
957                     (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
958                         error = EINVAL;
959                         goto bad;
960                 }
961                 for (;;) {
962                         for (pr = cnp->cn_cred->cr_prison; pr != NULL;
963                              pr = pr->pr_parent)
964                                 if (dp == pr->pr_root)
965                                         break;
966                         if (dp == ndp->ni_rootdir || 
967                             dp == ndp->ni_topdir || 
968                             dp == rootvnode ||
969                             pr != NULL ||
970                             ((dp->v_vflag & VV_ROOT) != 0 &&
971                              (cnp->cn_flags & NOCROSSMOUNT) != 0)) {
972                                 ndp->ni_dvp = dp;
973                                 ndp->ni_vp = dp;
974                                 VREF(dp);
975                                 goto nextname;
976                         }
977                         if ((dp->v_vflag & VV_ROOT) == 0)
978                                 break;
979                         if (VN_IS_DOOMED(dp)) { /* forced unmount */
980                                 error = ENOENT;
981                                 goto bad;
982                         }
983                         tdp = dp;
984                         dp = dp->v_mount->mnt_vnodecovered;
985                         VREF(dp);
986                         vput(tdp);
987                         vn_lock(dp,
988                             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
989                             LK_RETRY, ISDOTDOT));
990                         error = nameicap_check_dotdot(ndp, dp);
991                         if (error != 0) {
992 #ifdef KTRACE
993                                 if (KTRPOINT(curthread, KTR_CAPFAIL))
994                                         ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
995 #endif
996                                 goto bad;
997                         }
998                 }
999         }
1000
1001         /*
1002          * We now have a segment name to search for, and a directory to search.
1003          */
1004 unionlookup:
1005 #ifdef MAC
1006         error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp);
1007         if (error)
1008                 goto bad;
1009 #endif
1010         ndp->ni_dvp = dp;
1011         ndp->ni_vp = NULL;
1012         ASSERT_VOP_LOCKED(dp, "lookup");
1013         /*
1014          * If we have a shared lock we may need to upgrade the lock for the
1015          * last operation.
1016          */
1017         if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
1018             dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED)
1019                 vn_lock(dp, LK_UPGRADE|LK_RETRY);
1020         if (VN_IS_DOOMED(dp)) {
1021                 error = ENOENT;
1022                 goto bad;
1023         }
1024         /*
1025          * If we're looking up the last component and we need an exclusive
1026          * lock, adjust our lkflags.
1027          */
1028         if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags))
1029                 cnp->cn_lkflags = LK_EXCLUSIVE;
1030 #ifdef NAMEI_DIAGNOSTIC
1031         vn_printf(dp, "lookup in ");
1032 #endif
1033         lkflags_save = cnp->cn_lkflags;
1034         cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags,
1035             cnp->cn_flags);
1036         error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
1037         cnp->cn_lkflags = lkflags_save;
1038         if (error != 0) {
1039                 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty"));
1040 #ifdef NAMEI_DIAGNOSTIC
1041                 printf("not found\n");
1042 #endif
1043                 if ((error == ENOENT) &&
1044                     (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) &&
1045                     (dp->v_mount->mnt_flag & MNT_UNION)) {
1046                         tdp = dp;
1047                         dp = dp->v_mount->mnt_vnodecovered;
1048                         VREF(dp);
1049                         vput(tdp);
1050                         vn_lock(dp,
1051                             compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags |
1052                             LK_RETRY, cnp->cn_flags));
1053                         nameicap_tracker_add(ndp, dp);
1054                         goto unionlookup;
1055                 }
1056
1057                 if (error == ERELOOKUP) {
1058                         vref(dp);
1059                         ndp->ni_vp = dp;
1060                         error = 0;
1061                         relookup = 1;
1062                         goto good;
1063                 }
1064
1065                 if (error != EJUSTRETURN)
1066                         goto bad;
1067                 /*
1068                  * At this point, we know we're at the end of the
1069                  * pathname.  If creating / renaming, we can consider
1070                  * allowing the file or directory to be created / renamed,
1071                  * provided we're not on a read-only filesystem.
1072                  */
1073                 if (rdonly) {
1074                         error = EROFS;
1075                         goto bad;
1076                 }
1077                 /* trailing slash only allowed for directories */
1078                 if ((cnp->cn_flags & TRAILINGSLASH) &&
1079                     !(cnp->cn_flags & WILLBEDIR)) {
1080                         error = ENOENT;
1081                         goto bad;
1082                 }
1083                 if ((cnp->cn_flags & LOCKPARENT) == 0)
1084                         VOP_UNLOCK(dp);
1085                 /*
1086                  * We return with ni_vp NULL to indicate that the entry
1087                  * doesn't currently exist, leaving a pointer to the
1088                  * (possibly locked) directory vnode in ndp->ni_dvp.
1089                  */
1090                 if (cnp->cn_flags & SAVESTART) {
1091                         ndp->ni_startdir = ndp->ni_dvp;
1092                         VREF(ndp->ni_startdir);
1093                 }
1094                 goto success;
1095         }
1096
1097 good:
1098 #ifdef NAMEI_DIAGNOSTIC
1099         printf("found\n");
1100 #endif
1101         dp = ndp->ni_vp;
1102
1103         /*
1104          * Check to see if the vnode has been mounted on;
1105          * if so find the root of the mounted filesystem.
1106          */
1107         while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
1108                (cnp->cn_flags & NOCROSSMOUNT) == 0) {
1109                 if (vfs_busy(mp, 0))
1110                         continue;
1111                 vput(dp);
1112                 if (dp != ndp->ni_dvp)
1113                         vput(ndp->ni_dvp);
1114                 else
1115                         vrele(ndp->ni_dvp);
1116                 vrefact(vp_crossmp);
1117                 ndp->ni_dvp = vp_crossmp;
1118                 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags,
1119                     cnp->cn_flags), &tdp);
1120                 vfs_unbusy(mp);
1121                 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT))
1122                         panic("vp_crossmp exclusively locked or reclaimed");
1123                 if (error) {
1124                         dpunlocked = 1;
1125                         goto bad2;
1126                 }
1127                 ndp->ni_vp = dp = tdp;
1128         }
1129
1130         /*
1131          * Check for symbolic link
1132          */
1133         if ((dp->v_type == VLNK) &&
1134             ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) ||
1135              *ndp->ni_next == '/')) {
1136                 cnp->cn_flags |= ISSYMLINK;
1137                 if (VN_IS_DOOMED(dp)) {
1138                         /*
1139                          * We can't know whether the directory was mounted with
1140                          * NOSYMFOLLOW, so we can't follow safely.
1141                          */
1142                         error = ENOENT;
1143                         goto bad2;
1144                 }
1145                 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) {
1146                         error = EACCES;
1147                         goto bad2;
1148                 }
1149                 /*
1150                  * Symlink code always expects an unlocked dvp.
1151                  */
1152                 if (ndp->ni_dvp != ndp->ni_vp) {
1153                         VOP_UNLOCK(ndp->ni_dvp);
1154                         ni_dvp_unlocked = 1;
1155                 }
1156                 goto success;
1157         }
1158
1159 nextname:
1160         /*
1161          * Not a symbolic link that we will follow.  Continue with the
1162          * next component if there is any; otherwise, we're done.
1163          */
1164         KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/',
1165             ("lookup: invalid path state."));
1166         if (relookup) {
1167                 relookup = 0;
1168                 ndp->ni_pathlen = prev_ni_pathlen;
1169                 ndp->ni_next = prev_ni_next;
1170                 if (ndp->ni_dvp != dp)
1171                         vput(ndp->ni_dvp);
1172                 else
1173                         vrele(ndp->ni_dvp);
1174                 goto dirloop;
1175         }
1176         if (cnp->cn_flags & ISDOTDOT) {
1177                 error = nameicap_check_dotdot(ndp, ndp->ni_vp);
1178                 if (error != 0) {
1179 #ifdef KTRACE
1180                         if (KTRPOINT(curthread, KTR_CAPFAIL))
1181                                 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL);
1182 #endif
1183                         goto bad2;
1184                 }
1185         }
1186         if (*ndp->ni_next == '/') {
1187                 cnp->cn_nameptr = ndp->ni_next;
1188                 while (*cnp->cn_nameptr == '/') {
1189                         cnp->cn_nameptr++;
1190                         ndp->ni_pathlen--;
1191                 }
1192                 if (ndp->ni_dvp != dp)
1193                         vput(ndp->ni_dvp);
1194                 else
1195                         vrele(ndp->ni_dvp);
1196                 goto dirloop;
1197         }
1198         /*
1199          * If we're processing a path with a trailing slash,
1200          * check that the end result is a directory.
1201          */
1202         if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) {
1203                 error = ENOTDIR;
1204                 goto bad2;
1205         }
1206         /*
1207          * Disallow directory write attempts on read-only filesystems.
1208          */
1209         if (rdonly &&
1210             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1211                 error = EROFS;
1212                 goto bad2;
1213         }
1214         if (cnp->cn_flags & SAVESTART) {
1215                 ndp->ni_startdir = ndp->ni_dvp;
1216                 VREF(ndp->ni_startdir);
1217         }
1218         if (!wantparent) {
1219                 ni_dvp_unlocked = 2;
1220                 if (ndp->ni_dvp != dp)
1221                         vput(ndp->ni_dvp);
1222                 else
1223                         vrele(ndp->ni_dvp);
1224         } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) {
1225                 VOP_UNLOCK(ndp->ni_dvp);
1226                 ni_dvp_unlocked = 1;
1227         }
1228
1229         if (cnp->cn_flags & AUDITVNODE1)
1230                 AUDIT_ARG_VNODE1(dp);
1231         else if (cnp->cn_flags & AUDITVNODE2)
1232                 AUDIT_ARG_VNODE2(dp);
1233
1234         if ((cnp->cn_flags & LOCKLEAF) == 0)
1235                 VOP_UNLOCK(dp);
1236 success:
1237         /*
1238          * FIXME: for lookups which only cross a mount point to fetch the
1239          * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem
1240          * if either WANTPARENT or LOCKPARENT is set.
1241          */
1242         /*
1243          * Because of shared lookup we may have the vnode shared locked, but
1244          * the caller may want it to be exclusively locked.
1245          */
1246         if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) &&
1247             VOP_ISLOCKED(dp) != LK_EXCLUSIVE) {
1248                 vn_lock(dp, LK_UPGRADE | LK_RETRY);
1249                 if (VN_IS_DOOMED(dp)) {
1250                         error = ENOENT;
1251                         goto bad2;
1252                 }
1253         }
1254         if (ndp->ni_vp != NULL && ndp->ni_vp->v_type == VDIR)
1255                 nameicap_tracker_add(ndp, ndp->ni_vp);
1256         return (0);
1257
1258 bad2:
1259         if (ni_dvp_unlocked != 2) {
1260                 if (dp != ndp->ni_dvp && !ni_dvp_unlocked)
1261                         vput(ndp->ni_dvp);
1262                 else
1263                         vrele(ndp->ni_dvp);
1264         }
1265 bad:
1266         if (!dpunlocked)
1267                 vput(dp);
1268         ndp->ni_vp = NULL;
1269         return (error);
1270 }
1271
1272 /*
1273  * relookup - lookup a path name component
1274  *    Used by lookup to re-acquire things.
1275  */
1276 int
1277 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1278 {
1279         struct vnode *dp = NULL;                /* the directory we are searching */
1280         int wantparent;                 /* 1 => wantparent or lockparent flag */
1281         int rdonly;                     /* lookup read-only flag bit */
1282         int error = 0;
1283
1284         KASSERT(cnp->cn_flags & ISLASTCN,
1285             ("relookup: Not given last component."));
1286         /*
1287          * Setup: break out flag bits into variables.
1288          */
1289         wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1290         KASSERT(wantparent, ("relookup: parent not wanted."));
1291         rdonly = cnp->cn_flags & RDONLY;
1292         cnp->cn_flags &= ~ISSYMLINK;
1293         dp = dvp;
1294         cnp->cn_lkflags = LK_EXCLUSIVE;
1295         vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
1296
1297         /*
1298          * Search a new directory.
1299          *
1300          * The last component of the filename is left accessible via
1301          * cnp->cn_nameptr for callers that need the name. Callers needing
1302          * the name set the SAVENAME flag. When done, they assume
1303          * responsibility for freeing the pathname buffer.
1304          */
1305 #ifdef NAMEI_DIAGNOSTIC
1306         printf("{%s}: ", cnp->cn_nameptr);
1307 #endif
1308
1309         /*
1310          * Check for "" which represents the root directory after slash
1311          * removal.
1312          */
1313         if (cnp->cn_nameptr[0] == '\0') {
1314                 /*
1315                  * Support only LOOKUP for "/" because lookup()
1316                  * can't succeed for CREATE, DELETE and RENAME.
1317                  */
1318                 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP"));
1319                 KASSERT(dp->v_type == VDIR, ("dp is not a directory"));
1320
1321                 if (!(cnp->cn_flags & LOCKLEAF))
1322                         VOP_UNLOCK(dp);
1323                 *vpp = dp;
1324                 /* XXX This should probably move to the top of function. */
1325                 if (cnp->cn_flags & SAVESTART)
1326                         panic("lookup: SAVESTART");
1327                 return (0);
1328         }
1329
1330         if (cnp->cn_flags & ISDOTDOT)
1331                 panic ("relookup: lookup on dot-dot");
1332
1333         /*
1334          * We now have a segment name to search for, and a directory to search.
1335          */
1336 #ifdef NAMEI_DIAGNOSTIC
1337         vn_printf(dp, "search in ");
1338 #endif
1339         if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
1340                 KASSERT(*vpp == NULL, ("leaf should be empty"));
1341                 if (error != EJUSTRETURN)
1342                         goto bad;
1343                 /*
1344                  * If creating and at end of pathname, then can consider
1345                  * allowing file to be created.
1346                  */
1347                 if (rdonly) {
1348                         error = EROFS;
1349                         goto bad;
1350                 }
1351                 /* ASSERT(dvp == ndp->ni_startdir) */
1352                 if (cnp->cn_flags & SAVESTART)
1353                         VREF(dvp);
1354                 if ((cnp->cn_flags & LOCKPARENT) == 0)
1355                         VOP_UNLOCK(dp);
1356                 /*
1357                  * We return with ni_vp NULL to indicate that the entry
1358                  * doesn't currently exist, leaving a pointer to the
1359                  * (possibly locked) directory vnode in ndp->ni_dvp.
1360                  */
1361                 return (0);
1362         }
1363
1364         dp = *vpp;
1365
1366         /*
1367          * Disallow directory write attempts on read-only filesystems.
1368          */
1369         if (rdonly &&
1370             (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1371                 if (dvp == dp)
1372                         vrele(dvp);
1373                 else
1374                         vput(dvp);
1375                 error = EROFS;
1376                 goto bad;
1377         }
1378         /*
1379          * Set the parent lock/ref state to the requested state.
1380          */
1381         if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) {
1382                 if (wantparent)
1383                         VOP_UNLOCK(dvp);
1384                 else
1385                         vput(dvp);
1386         } else if (!wantparent)
1387                 vrele(dvp);
1388         /*
1389          * Check for symbolic link
1390          */
1391         KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW),
1392             ("relookup: symlink found.\n"));
1393
1394         /* ASSERT(dvp == ndp->ni_startdir) */
1395         if (cnp->cn_flags & SAVESTART)
1396                 VREF(dvp);
1397
1398         if ((cnp->cn_flags & LOCKLEAF) == 0)
1399                 VOP_UNLOCK(dp);
1400         return (0);
1401 bad:
1402         vput(dp);
1403         *vpp = NULL;
1404         return (error);
1405 }
1406
1407 /*
1408  * Free data allocated by namei(); see namei(9) for details.
1409  */
1410 void
1411 NDFREE_PNBUF(struct nameidata *ndp)
1412 {
1413
1414         if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) {
1415                 MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0);
1416                 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
1417                 ndp->ni_cnd.cn_flags &= ~HASBUF;
1418         }
1419 }
1420
1421 void
1422 (NDFREE)(struct nameidata *ndp, const u_int flags)
1423 {
1424         int unlock_dvp;
1425         int unlock_vp;
1426
1427         unlock_dvp = 0;
1428         unlock_vp = 0;
1429
1430         if (!(flags & NDF_NO_FREE_PNBUF)) {
1431                 NDFREE_PNBUF(ndp);
1432         }
1433         if (!(flags & NDF_NO_VP_UNLOCK) &&
1434             (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
1435                 unlock_vp = 1;
1436         if (!(flags & NDF_NO_DVP_UNLOCK) &&
1437             (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
1438             ndp->ni_dvp != ndp->ni_vp)
1439                 unlock_dvp = 1;
1440         if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) {
1441                 if (unlock_vp) {
1442                         vput(ndp->ni_vp);
1443                         unlock_vp = 0;
1444                 } else
1445                         vrele(ndp->ni_vp);
1446                 ndp->ni_vp = NULL;
1447         }
1448         if (unlock_vp)
1449                 VOP_UNLOCK(ndp->ni_vp);
1450         if (!(flags & NDF_NO_DVP_RELE) &&
1451             (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
1452                 if (unlock_dvp) {
1453                         vput(ndp->ni_dvp);
1454                         unlock_dvp = 0;
1455                 } else
1456                         vrele(ndp->ni_dvp);
1457                 ndp->ni_dvp = NULL;
1458         }
1459         if (unlock_dvp)
1460                 VOP_UNLOCK(ndp->ni_dvp);
1461         if (!(flags & NDF_NO_STARTDIR_RELE) &&
1462             (ndp->ni_cnd.cn_flags & SAVESTART)) {
1463                 vrele(ndp->ni_startdir);
1464                 ndp->ni_startdir = NULL;
1465         }
1466 }
1467
1468 #ifdef INVARIANTS
1469 /*
1470  * Validate the final state of ndp after the lookup.
1471  *
1472  * Historically filesystems were allowed to modify cn_flags. Most notably they
1473  * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent
1474  * clean up to the consumer. In practice this seems to only concern != LOOKUP
1475  * operations.
1476  *
1477  * As a step towards stricter API contract this routine validates the state to
1478  * clean up. Note validation is a work in progress with the intent of becoming
1479  * stricter over time.
1480  */
1481 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF)
1482 void
1483 NDVALIDATE(struct nameidata *ndp)
1484 {
1485         struct componentname *cnp;
1486         u_int64_t used, orig;
1487
1488         cnp = &ndp->ni_cnd;
1489         orig = cnp->cn_origflags;
1490         used = cnp->cn_flags;
1491         switch (cnp->cn_nameiop) {
1492         case LOOKUP:
1493                 /*
1494                  * For plain lookup we require strict conformance -- nothing
1495                  * to clean up if it was not requested by the caller.
1496                  */
1497                 orig &= NDMODIFYINGFLAGS;
1498                 used &= NDMODIFYINGFLAGS;
1499                 if ((orig & (SAVENAME | SAVESTART)) != 0)
1500                         orig |= HASBUF;
1501                 if (orig != used) {
1502                         goto out_mismatch;
1503                 }
1504                 break;
1505         case CREATE:
1506         case DELETE:
1507         case RENAME:
1508                 /*
1509                  * Some filesystems set SAVENAME to provoke HASBUF, accomodate
1510                  * for it until it gets fixed.
1511                  */
1512                 orig &= NDMODIFYINGFLAGS;
1513                 orig |= (SAVENAME | HASBUF);
1514                 used &= NDMODIFYINGFLAGS;
1515                 used |= (SAVENAME | HASBUF);
1516                 if (orig != used) {
1517                         goto out_mismatch;
1518                 }
1519                 break;
1520         }
1521         return;
1522 out_mismatch:
1523         panic("%s: mismatched flags for op %d: added %" PRIx64 ", "
1524             "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")",
1525             __func__, cnp->cn_nameiop, used & ~orig, orig &~ used,
1526             orig, used, cnp->cn_origflags, cnp->cn_flags);
1527 }
1528 #endif
1529
1530 /*
1531  * Determine if there is a suitable alternate filename under the specified
1532  * prefix for the specified path.  If the create flag is set, then the
1533  * alternate prefix will be used so long as the parent directory exists.
1534  * This is used by the various compatibility ABIs so that Linux binaries prefer
1535  * files under /compat/linux for example.  The chosen path (whether under
1536  * the prefix or under /) is returned in a kernel malloc'd buffer pointed
1537  * to by pathbuf.  The caller is responsible for free'ing the buffer from
1538  * the M_TEMP bucket if one is returned.
1539  */
1540 int
1541 kern_alternate_path(struct thread *td, const char *prefix, const char *path,
1542     enum uio_seg pathseg, char **pathbuf, int create, int dirfd)
1543 {
1544         struct nameidata nd, ndroot;
1545         char *ptr, *buf, *cp;
1546         size_t len, sz;
1547         int error;
1548
1549         buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1550         *pathbuf = buf;
1551
1552         /* Copy the prefix into the new pathname as a starting point. */
1553         len = strlcpy(buf, prefix, MAXPATHLEN);
1554         if (len >= MAXPATHLEN) {
1555                 *pathbuf = NULL;
1556                 free(buf, M_TEMP);
1557                 return (EINVAL);
1558         }
1559         sz = MAXPATHLEN - len;
1560         ptr = buf + len;
1561
1562         /* Append the filename to the prefix. */
1563         if (pathseg == UIO_SYSSPACE)
1564                 error = copystr(path, ptr, sz, &len);
1565         else
1566                 error = copyinstr(path, ptr, sz, &len);
1567
1568         if (error) {
1569                 *pathbuf = NULL;
1570                 free(buf, M_TEMP);
1571                 return (error);
1572         }
1573
1574         /* Only use a prefix with absolute pathnames. */
1575         if (*ptr != '/') {
1576                 error = EINVAL;
1577                 goto keeporig;
1578         }
1579
1580         if (dirfd != AT_FDCWD) {
1581                 /*
1582                  * We want the original because the "prefix" is
1583                  * included in the already opened dirfd.
1584                  */
1585                 bcopy(ptr, buf, len);
1586                 return (0);
1587         }
1588
1589         /*
1590          * We know that there is a / somewhere in this pathname.
1591          * Search backwards for it, to find the file's parent dir
1592          * to see if it exists in the alternate tree. If it does,
1593          * and we want to create a file (cflag is set). We don't
1594          * need to worry about the root comparison in this case.
1595          */
1596
1597         if (create) {
1598                 for (cp = &ptr[len] - 1; *cp != '/'; cp--);
1599                 *cp = '\0';
1600
1601                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1602                 error = namei(&nd);
1603                 *cp = '/';
1604                 if (error != 0)
1605                         goto keeporig;
1606         } else {
1607                 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td);
1608
1609                 error = namei(&nd);
1610                 if (error != 0)
1611                         goto keeporig;
1612
1613                 /*
1614                  * We now compare the vnode of the prefix to the one
1615                  * vnode asked. If they resolve to be the same, then we
1616                  * ignore the match so that the real root gets used.
1617                  * This avoids the problem of traversing "../.." to find the
1618                  * root directory and never finding it, because "/" resolves
1619                  * to the emulation root directory. This is expensive :-(
1620                  */
1621                 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix,
1622                     td);
1623
1624                 /* We shouldn't ever get an error from this namei(). */
1625                 error = namei(&ndroot);
1626                 if (error == 0) {
1627                         if (nd.ni_vp == ndroot.ni_vp)
1628                                 error = ENOENT;
1629
1630                         NDFREE(&ndroot, NDF_ONLY_PNBUF);
1631                         vrele(ndroot.ni_vp);
1632                 }
1633         }
1634
1635         NDFREE(&nd, NDF_ONLY_PNBUF);
1636         vrele(nd.ni_vp);
1637
1638 keeporig:
1639         /* If there was an error, use the original path name. */
1640         if (error)
1641                 bcopy(ptr, buf, len);
1642         return (error);
1643 }