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