vfs_lookup.c revision 1.112.2.1 1 /* $NetBSD: vfs_lookup.c,v 1.112.2.1 2009/05/13 17:21:58 jym Exp $ */
2
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.10 (Berkeley) 5/27/95
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.112.2.1 2009/05/13 17:21:58 jym Exp $");
41
42 #include "opt_magiclinks.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/syslimits.h>
48 #include <sys/time.h>
49 #include <sys/namei.h>
50 #include <sys/vnode.h>
51 #include <sys/mount.h>
52 #include <sys/errno.h>
53 #include <sys/filedesc.h>
54 #include <sys/hash.h>
55 #include <sys/proc.h>
56 #include <sys/syslog.h>
57 #include <sys/kauth.h>
58 #include <sys/ktrace.h>
59
60 #ifndef MAGICLINKS
61 #define MAGICLINKS 0
62 #endif
63
64 struct pathname_internal {
65 char *pathbuf;
66 bool needfree;
67 };
68
69 int vfs_magiclinks = MAGICLINKS;
70
71 pool_cache_t pnbuf_cache; /* pathname buffer cache */
72
73 /*
74 * Substitute replacement text for 'magic' strings in symlinks.
75 * Returns 0 if successful, and returns non-zero if an error
76 * occurs. (Currently, the only possible error is running out
77 * of temporary pathname space.)
78 *
79 * Looks for "@<string>" and "@<string>/", where <string> is a
80 * recognized 'magic' string. Replaces the "@<string>" with the
81 * appropriate replacement text. (Note that in some cases the
82 * replacement text may have zero length.)
83 *
84 * This would have been table driven, but the variance in
85 * replacement strings (and replacement string lengths) made
86 * that impractical.
87 */
88 #define VNL(x) \
89 (sizeof(x) - 1)
90
91 #define VO '{'
92 #define VC '}'
93
94 #define MATCH(str) \
95 ((termchar == '/' && i + VNL(str) == *len) || \
96 (i + VNL(str) < *len && \
97 cp[i + VNL(str)] == termchar)) && \
98 !strncmp((str), &cp[i], VNL(str))
99
100 #define SUBSTITUTE(m, s, sl) \
101 if ((newlen + (sl)) > MAXPATHLEN) \
102 return (1); \
103 i += VNL(m); \
104 if (termchar != '/') \
105 i++; \
106 memcpy(&tmp[newlen], (s), (sl)); \
107 newlen += (sl); \
108 change = 1; \
109 termchar = '/';
110
111 static int
112 symlink_magic(struct proc *p, char *cp, int *len)
113 {
114 char *tmp;
115 int change, i, newlen;
116 int termchar = '/';
117 char uidtmp[11]; /* XXX elad */
118
119
120 tmp = PNBUF_GET();
121 for (change = i = newlen = 0; i < *len; ) {
122 if (cp[i] != '@') {
123 tmp[newlen++] = cp[i++];
124 continue;
125 }
126
127 i++;
128
129 /* Check for @{var} syntax. */
130 if (cp[i] == VO) {
131 termchar = VC;
132 i++;
133 }
134
135 /*
136 * The following checks should be ordered according
137 * to frequency of use.
138 */
139 if (MATCH("machine_arch")) {
140 SUBSTITUTE("machine_arch", MACHINE_ARCH,
141 sizeof(MACHINE_ARCH) - 1);
142 } else if (MATCH("machine")) {
143 SUBSTITUTE("machine", MACHINE,
144 sizeof(MACHINE) - 1);
145 } else if (MATCH("hostname")) {
146 SUBSTITUTE("hostname", hostname,
147 hostnamelen);
148 } else if (MATCH("osrelease")) {
149 SUBSTITUTE("osrelease", osrelease,
150 strlen(osrelease));
151 } else if (MATCH("emul")) {
152 SUBSTITUTE("emul", p->p_emul->e_name,
153 strlen(p->p_emul->e_name));
154 } else if (MATCH("kernel_ident")) {
155 SUBSTITUTE("kernel_ident", kernel_ident,
156 strlen(kernel_ident));
157 } else if (MATCH("domainname")) {
158 SUBSTITUTE("domainname", domainname,
159 domainnamelen);
160 } else if (MATCH("ostype")) {
161 SUBSTITUTE("ostype", ostype,
162 strlen(ostype));
163 } else if (MATCH("uid")) {
164 (void)snprintf(uidtmp, sizeof(uidtmp), "%u",
165 kauth_cred_geteuid(kauth_cred_get()));
166 SUBSTITUTE("uid", uidtmp, strlen(uidtmp));
167 } else if (MATCH("ruid")) {
168 (void)snprintf(uidtmp, sizeof(uidtmp), "%u",
169 kauth_cred_getuid(kauth_cred_get()));
170 SUBSTITUTE("ruid", uidtmp, strlen(uidtmp));
171 } else {
172 tmp[newlen++] = '@';
173 if (termchar == VC)
174 tmp[newlen++] = VO;
175 }
176 }
177
178 if (change) {
179 memcpy(cp, tmp, newlen);
180 *len = newlen;
181 }
182 PNBUF_PUT(tmp);
183
184 return (0);
185 }
186
187 #undef VNL
188 #undef VO
189 #undef VC
190 #undef MATCH
191 #undef SUBSTITUTE
192
193 /*
194 * Convert a pathname into a pointer to a locked vnode.
195 *
196 * The FOLLOW flag is set when symbolic links are to be followed
197 * when they occur at the end of the name translation process.
198 * Symbolic links are always followed for all other pathname
199 * components other than the last.
200 *
201 * The segflg defines whether the name is to be copied from user
202 * space or kernel space.
203 *
204 * Overall outline of namei:
205 *
206 * copy in name
207 * get starting directory
208 * while (!done && !error) {
209 * call lookup to search path.
210 * if symbolic link, massage name in buffer and continue
211 * }
212 */
213 int
214 namei(struct nameidata *ndp)
215 {
216 struct cwdinfo *cwdi; /* pointer to cwd state */
217 char *cp; /* pointer into pathname argument */
218 struct vnode *dp; /* the directory we are searching */
219 struct iovec aiov; /* uio for reading symbolic links */
220 struct lwp *l = curlwp; /* thread doing namei() */
221 struct uio auio;
222 int error, linklen;
223 struct componentname *cnp = &ndp->ni_cnd;
224
225 #ifdef DIAGNOSTIC
226 if (!cnp->cn_cred)
227 panic("namei: bad cred/proc");
228 if (cnp->cn_nameiop & (~OPMASK))
229 panic("namei: nameiop contaminated with flags");
230 if (cnp->cn_flags & OPMASK)
231 panic("namei: flags contaminated with nameiops");
232 #endif
233
234 /*
235 * Get a buffer for the name to be translated, and copy the
236 * name into the buffer.
237 */
238 if ((cnp->cn_flags & HASBUF) == 0)
239 cnp->cn_pnbuf = PNBUF_GET();
240 emul_retry:
241 if (ndp->ni_segflg == UIO_SYSSPACE)
242 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
243 MAXPATHLEN, &ndp->ni_pathlen);
244 else
245 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
246 MAXPATHLEN, &ndp->ni_pathlen);
247
248 /*
249 * POSIX.1 requirement: "" is not a valid file name.
250 */
251 if (!error && ndp->ni_pathlen == 1)
252 error = ENOENT;
253
254 if (error) {
255 PNBUF_PUT(cnp->cn_pnbuf);
256 ndp->ni_vp = NULL;
257 return (error);
258 }
259 ndp->ni_loopcnt = 0;
260
261 /*
262 * Get root directory for the translation.
263 */
264 cwdi = l->l_proc->p_cwdi;
265 rw_enter(&cwdi->cwdi_lock, RW_READER);
266 dp = cwdi->cwdi_rdir;
267 if (dp == NULL)
268 dp = rootvnode;
269 ndp->ni_rootdir = dp;
270
271 /*
272 * Check if starting from root directory or current directory.
273 */
274 if (cnp->cn_pnbuf[0] == '/') {
275 if (cnp->cn_flags & TRYEMULROOT) {
276 if (cnp->cn_flags & EMULROOTSET) {
277 /* Called from (eg) emul_find_interp() */
278 dp = ndp->ni_erootdir;
279 } else {
280 if (cwdi->cwdi_edir == NULL
281 || (cnp->cn_pnbuf[1] == '.'
282 && cnp->cn_pnbuf[2] == '.'
283 && cnp->cn_pnbuf[3] == '/')) {
284 ndp->ni_erootdir = NULL;
285 } else {
286 dp = cwdi->cwdi_edir;
287 ndp->ni_erootdir = dp;
288 }
289 }
290 } else {
291 ndp->ni_erootdir = NULL;
292 if (cnp->cn_flags & NOCHROOT)
293 dp = ndp->ni_rootdir = rootvnode;
294 }
295 } else {
296 dp = cwdi->cwdi_cdir;
297 ndp->ni_erootdir = NULL;
298 }
299 VREF(dp);
300 rw_exit(&cwdi->cwdi_lock);
301
302 if (ktrpoint(KTR_NAMEI)) {
303 if (ndp->ni_erootdir != NULL) {
304 /*
305 * To make any sense, the trace entry need to have the
306 * text of the emulation path prepended.
307 * Usually we can get this from the current process,
308 * but when called from emul_find_interp() it is only
309 * in the exec_package - so we get it passed in ni_next
310 * (this is a hack).
311 */
312 const char *emul_path;
313 if (cnp->cn_flags & EMULROOTSET)
314 emul_path = ndp->ni_next;
315 else
316 emul_path = l->l_proc->p_emul->e_path;
317 ktrnamei2(emul_path, strlen(emul_path),
318 cnp->cn_pnbuf, ndp->ni_pathlen);
319 } else
320 ktrnamei(cnp->cn_pnbuf, ndp->ni_pathlen);
321 }
322
323 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
324 /* Loop through symbolic links */
325 for (;;) {
326 if (!dp->v_mount) {
327 /* Give up if the directory is no longer mounted */
328 vput(dp);
329 PNBUF_PUT(cnp->cn_pnbuf);
330 return (ENOENT);
331 }
332 cnp->cn_nameptr = cnp->cn_pnbuf;
333 ndp->ni_startdir = dp;
334 error = lookup(ndp);
335 if (error != 0) {
336 if (ndp->ni_dvp) {
337 vput(ndp->ni_dvp);
338 }
339 if (ndp->ni_erootdir != NULL) {
340 /* Retry the whole thing from the normal root */
341 cnp->cn_flags &= ~TRYEMULROOT;
342 goto emul_retry;
343 }
344 PNBUF_PUT(cnp->cn_pnbuf);
345 return (error);
346 }
347
348 /*
349 * Check for symbolic link
350 */
351 if ((cnp->cn_flags & ISSYMLINK) == 0) {
352 if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
353 if (ndp->ni_dvp == ndp->ni_vp) {
354 vrele(ndp->ni_dvp);
355 } else {
356 vput(ndp->ni_dvp);
357 }
358 }
359 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) {
360 PNBUF_PUT(cnp->cn_pnbuf);
361 #if defined(DIAGNOSTIC)
362 cnp->cn_pnbuf = NULL;
363 #endif /* defined(DIAGNOSTIC) */
364 } else {
365 cnp->cn_flags |= HASBUF;
366 }
367 return (0);
368 }
369
370 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
371 error = ELOOP;
372 break;
373 }
374 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
375 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred);
376 if (error != 0)
377 break;
378 }
379 if (ndp->ni_pathlen > 1)
380 cp = PNBUF_GET();
381 else
382 cp = cnp->cn_pnbuf;
383 aiov.iov_base = cp;
384 aiov.iov_len = MAXPATHLEN;
385 auio.uio_iov = &aiov;
386 auio.uio_iovcnt = 1;
387 auio.uio_offset = 0;
388 auio.uio_rw = UIO_READ;
389 auio.uio_resid = MAXPATHLEN;
390 UIO_SETUP_SYSSPACE(&auio);
391 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
392 if (error) {
393 badlink:
394 if (ndp->ni_pathlen > 1)
395 PNBUF_PUT(cp);
396 break;
397 }
398 linklen = MAXPATHLEN - auio.uio_resid;
399 if (linklen == 0) {
400 error = ENOENT;
401 goto badlink;
402 }
403
404 /*
405 * Do symlink substitution, if appropriate, and
406 * check length for potential overflow.
407 */
408 if ((vfs_magiclinks &&
409 symlink_magic(l->l_proc, cp, &linklen)) ||
410 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
411 error = ENAMETOOLONG;
412 goto badlink;
413 }
414 if (ndp->ni_pathlen > 1) {
415 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
416 PNBUF_PUT(cnp->cn_pnbuf);
417 cnp->cn_pnbuf = cp;
418 } else
419 cnp->cn_pnbuf[linklen] = '\0';
420 ndp->ni_pathlen += linklen;
421 vput(ndp->ni_vp);
422 dp = ndp->ni_dvp;
423
424 /*
425 * Check if root directory should replace current directory.
426 */
427 if (cnp->cn_pnbuf[0] == '/') {
428 vput(dp);
429 /* Keep absolute symbolic links inside emulation root */
430 dp = ndp->ni_erootdir;
431 if (dp == NULL || (cnp->cn_pnbuf[1] == '.'
432 && cnp->cn_pnbuf[2] == '.'
433 && cnp->cn_pnbuf[3] == '/')) {
434 ndp->ni_erootdir = NULL;
435 dp = ndp->ni_rootdir;
436 }
437 VREF(dp);
438 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
439 }
440 }
441 /* Failed to process a symbolic link */
442 KASSERT(ndp->ni_dvp != ndp->ni_vp);
443 vput(ndp->ni_dvp);
444 vput(ndp->ni_vp);
445 ndp->ni_vp = NULL;
446 PNBUF_PUT(cnp->cn_pnbuf);
447 return (error);
448 }
449
450 /*
451 * Determine the namei hash (for cn_hash) for name.
452 * If *ep != NULL, hash from name to ep-1.
453 * If *ep == NULL, hash from name until the first NUL or '/', and
454 * return the location of this termination character in *ep.
455 *
456 * This function returns an equivalent hash to the MI hash32_strn().
457 * The latter isn't used because in the *ep == NULL case, determining
458 * the length of the string to the first NUL or `/' and then calling
459 * hash32_strn() involves unnecessary double-handling of the data.
460 */
461 uint32_t
462 namei_hash(const char *name, const char **ep)
463 {
464 uint32_t hash;
465
466 hash = HASH32_STR_INIT;
467 if (*ep != NULL) {
468 for (; name < *ep; name++)
469 hash = hash * 33 + *(const uint8_t *)name;
470 } else {
471 for (; *name != '\0' && *name != '/'; name++)
472 hash = hash * 33 + *(const uint8_t *)name;
473 *ep = name;
474 }
475 return (hash + (hash >> 5));
476 }
477
478 /*
479 * Search a pathname.
480 * This is a very central and rather complicated routine.
481 *
482 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
483 * The starting directory is taken from ni_startdir. The pathname is
484 * descended until done, or a symbolic link is encountered. The variable
485 * ni_more is clear if the path is completed; it is set to one if a
486 * symbolic link needing interpretation is encountered.
487 *
488 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
489 * whether the name is to be looked up, created, renamed, or deleted.
490 * When CREATE, RENAME, or DELETE is specified, information usable in
491 * creating, renaming, or deleting a directory entry may be calculated.
492 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
493 * locked. Otherwise the parent directory is not returned. If the target
494 * of the pathname exists and LOCKLEAF is or'ed into the flag the target
495 * is returned locked, otherwise it is returned unlocked. When creating
496 * or renaming and LOCKPARENT is specified, the target may not be ".".
497 * When deleting and LOCKPARENT is specified, the target may be ".".
498 *
499 * Overall outline of lookup:
500 *
501 * dirloop:
502 * identify next component of name at ndp->ni_ptr
503 * handle degenerate case where name is null string
504 * if .. and crossing mount points and on mounted filesys, find parent
505 * call VOP_LOOKUP routine for next component name
506 * directory vnode returned in ni_dvp, locked.
507 * component vnode returned in ni_vp (if it exists), locked.
508 * if result vnode is mounted on and crossing mount points,
509 * find mounted on vnode
510 * if more components of name, do next level at dirloop
511 * return the answer in ni_vp, locked if LOCKLEAF set
512 * if LOCKPARENT set, return locked parent in ni_dvp
513 */
514 int
515 lookup(struct nameidata *ndp)
516 {
517 const char *cp; /* pointer into pathname argument */
518 struct vnode *dp = 0; /* the directory we are searching */
519 struct vnode *tdp; /* saved dp */
520 struct mount *mp; /* mount table entry */
521 int docache; /* == 0 do not cache last component */
522 int rdonly; /* lookup read-only flag bit */
523 int error = 0;
524 int slashes;
525 struct componentname *cnp = &ndp->ni_cnd;
526 struct lwp *l = curlwp;
527
528 /*
529 * Setup: break out flag bits into variables.
530 */
531 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
532 if (cnp->cn_nameiop == DELETE)
533 docache = 0;
534 rdonly = cnp->cn_flags & RDONLY;
535 ndp->ni_dvp = NULL;
536 cnp->cn_flags &= ~ISSYMLINK;
537 dp = ndp->ni_startdir;
538 ndp->ni_startdir = NULLVP;
539
540 /*
541 * If we have a leading string of slashes, remove them, and just make
542 * sure the current node is a directory.
543 */
544 cp = cnp->cn_nameptr;
545 if (*cp == '/') {
546 do {
547 cp++;
548 } while (*cp == '/');
549 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
550 cnp->cn_nameptr = cp;
551
552 if (dp->v_type != VDIR) {
553 error = ENOTDIR;
554 vput(dp);
555 goto bad;
556 }
557
558 /*
559 * If we've exhausted the path name, then just return the
560 * current node.
561 */
562 if (cnp->cn_nameptr[0] == '\0') {
563 ndp->ni_vp = dp;
564 cnp->cn_flags |= ISLASTCN;
565 goto terminal;
566 }
567 }
568
569 dirloop:
570 /*
571 * Search a new directory.
572 *
573 * The cn_hash value is for use by vfs_cache.
574 * The last component of the filename is left accessible via
575 * cnp->cn_nameptr for callers that need the name. Callers needing
576 * the name set the SAVENAME flag. When done, they assume
577 * responsibility for freeing the pathname buffer.
578 *
579 * At this point, our only vnode state is that "dp" is held and locked.
580 */
581 cnp->cn_consume = 0;
582 cp = NULL;
583 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
584 cnp->cn_namelen = cp - cnp->cn_nameptr;
585 if (cnp->cn_namelen > NAME_MAX) {
586 vput(dp);
587 error = ENAMETOOLONG;
588 ndp->ni_dvp = NULL;
589 goto bad;
590 }
591 #ifdef NAMEI_DIAGNOSTIC
592 { char c = *cp;
593 *(char *)cp = '\0';
594 printf("{%s}: ", cnp->cn_nameptr);
595 *(char *)cp = c; }
596 #endif /* NAMEI_DIAGNOSTIC */
597 ndp->ni_pathlen -= cnp->cn_namelen;
598 ndp->ni_next = cp;
599 /*
600 * If this component is followed by a slash, then move the pointer to
601 * the next component forward, and remember that this component must be
602 * a directory.
603 */
604 if (*cp == '/') {
605 do {
606 cp++;
607 } while (*cp == '/');
608 slashes = cp - ndp->ni_next;
609 ndp->ni_pathlen -= slashes;
610 ndp->ni_next = cp;
611 cnp->cn_flags |= REQUIREDIR;
612 } else {
613 slashes = 0;
614 cnp->cn_flags &= ~REQUIREDIR;
615 }
616 /*
617 * We do special processing on the last component, whether or not it's
618 * a directory. Cache all intervening lookups, but not the final one.
619 */
620 if (*cp == '\0') {
621 if (docache)
622 cnp->cn_flags |= MAKEENTRY;
623 else
624 cnp->cn_flags &= ~MAKEENTRY;
625 cnp->cn_flags |= ISLASTCN;
626 } else {
627 cnp->cn_flags |= MAKEENTRY;
628 cnp->cn_flags &= ~ISLASTCN;
629 }
630 if (cnp->cn_namelen == 2 &&
631 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
632 cnp->cn_flags |= ISDOTDOT;
633 else
634 cnp->cn_flags &= ~ISDOTDOT;
635
636 /*
637 * Handle "..": two special cases.
638 * 1. If at root directory (e.g. after chroot)
639 * or at absolute root directory
640 * then ignore it so can't get out.
641 * 1a. If at the root of the emulation filesystem go to the real
642 * root. So "/../<path>" is always absolute.
643 * 1b. If we have somehow gotten out of a jail, warn
644 * and also ignore it so we can't get farther out.
645 * 2. If this vnode is the root of a mounted
646 * filesystem, then replace it with the
647 * vnode which was mounted on so we take the
648 * .. in the other file system.
649 */
650 if (cnp->cn_flags & ISDOTDOT) {
651 struct proc *p = l->l_proc;
652
653 for (;;) {
654 if (dp == ndp->ni_rootdir || dp == rootvnode) {
655 ndp->ni_dvp = dp;
656 ndp->ni_vp = dp;
657 VREF(dp);
658 goto nextname;
659 }
660 if (ndp->ni_rootdir != rootvnode) {
661 int retval;
662
663 VOP_UNLOCK(dp, 0);
664 retval = vn_isunder(dp, ndp->ni_rootdir, l);
665 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
666 if (!retval) {
667 /* Oops! We got out of jail! */
668 log(LOG_WARNING,
669 "chrooted pid %d uid %d (%s) "
670 "detected outside of its chroot\n",
671 p->p_pid, kauth_cred_geteuid(l->l_cred),
672 p->p_comm);
673 /* Put us at the jail root. */
674 vput(dp);
675 dp = ndp->ni_rootdir;
676 ndp->ni_dvp = dp;
677 ndp->ni_vp = dp;
678 VREF(dp);
679 VREF(dp);
680 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
681 goto nextname;
682 }
683 }
684 if ((dp->v_vflag & VV_ROOT) == 0 ||
685 (cnp->cn_flags & NOCROSSMOUNT))
686 break;
687 tdp = dp;
688 dp = dp->v_mount->mnt_vnodecovered;
689 vput(tdp);
690 VREF(dp);
691 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
692 }
693 }
694
695 /*
696 * We now have a segment name to search for, and a directory to search.
697 * Again, our only vnode state is that "dp" is held and locked.
698 */
699 unionlookup:
700 ndp->ni_dvp = dp;
701 ndp->ni_vp = NULL;
702 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
703 if (error != 0) {
704 #ifdef DIAGNOSTIC
705 if (ndp->ni_vp != NULL)
706 panic("leaf `%s' should be empty", cnp->cn_nameptr);
707 #endif /* DIAGNOSTIC */
708 #ifdef NAMEI_DIAGNOSTIC
709 printf("not found\n");
710 #endif /* NAMEI_DIAGNOSTIC */
711 if ((error == ENOENT) &&
712 (dp->v_vflag & VV_ROOT) &&
713 (dp->v_mount->mnt_flag & MNT_UNION)) {
714 tdp = dp;
715 dp = dp->v_mount->mnt_vnodecovered;
716 vput(tdp);
717 VREF(dp);
718 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
719 goto unionlookup;
720 }
721
722 if (error != EJUSTRETURN)
723 goto bad;
724
725 /*
726 * If this was not the last component, or there were trailing
727 * slashes, and we are not going to create a directory,
728 * then the name must exist.
729 */
730 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
731 error = ENOENT;
732 goto bad;
733 }
734
735 /*
736 * If creating and at end of pathname, then can consider
737 * allowing file to be created.
738 */
739 if (rdonly) {
740 error = EROFS;
741 goto bad;
742 }
743
744 /*
745 * We return with ni_vp NULL to indicate that the entry
746 * doesn't currently exist, leaving a pointer to the
747 * (possibly locked) directory vnode in ndp->ni_dvp.
748 */
749 if (cnp->cn_flags & SAVESTART) {
750 ndp->ni_startdir = ndp->ni_dvp;
751 VREF(ndp->ni_startdir);
752 }
753 return (0);
754 }
755 #ifdef NAMEI_DIAGNOSTIC
756 printf("found\n");
757 #endif /* NAMEI_DIAGNOSTIC */
758
759 /*
760 * Take into account any additional components consumed by the
761 * underlying filesystem. This will include any trailing slashes after
762 * the last component consumed.
763 */
764 if (cnp->cn_consume > 0) {
765 ndp->ni_pathlen -= cnp->cn_consume - slashes;
766 ndp->ni_next += cnp->cn_consume - slashes;
767 cnp->cn_consume = 0;
768 if (ndp->ni_next[0] == '\0')
769 cnp->cn_flags |= ISLASTCN;
770 }
771
772 dp = ndp->ni_vp;
773
774 /*
775 * "dp" and "ndp->ni_dvp" are both locked and held,
776 * and may be the same vnode.
777 */
778
779 /*
780 * Check to see if the vnode has been mounted on;
781 * if so find the root of the mounted file system.
782 */
783 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
784 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
785 error = vfs_busy(mp, NULL);
786 if (error != 0) {
787 vput(dp);
788 goto bad;
789 }
790 KASSERT(ndp->ni_dvp != dp);
791 VOP_UNLOCK(ndp->ni_dvp, 0);
792 vput(dp);
793 error = VFS_ROOT(mp, &tdp);
794 vfs_unbusy(mp, false, NULL);
795 if (error) {
796 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
797 goto bad;
798 }
799 VOP_UNLOCK(tdp, 0);
800 ndp->ni_vp = dp = tdp;
801 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
802 vn_lock(ndp->ni_vp, LK_EXCLUSIVE | LK_RETRY);
803 }
804
805 /*
806 * Check for symbolic link. Back up over any slashes that we skipped,
807 * as we will need them again.
808 */
809 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
810 ndp->ni_pathlen += slashes;
811 ndp->ni_next -= slashes;
812 cnp->cn_flags |= ISSYMLINK;
813 return (0);
814 }
815
816 /*
817 * Check for directory, if the component was followed by a series of
818 * slashes.
819 */
820 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
821 error = ENOTDIR;
822 KASSERT(dp != ndp->ni_dvp);
823 vput(dp);
824 goto bad;
825 }
826
827 nextname:
828
829 /*
830 * Not a symbolic link. If this was not the last component, then
831 * continue at the next component, else return.
832 */
833 if (!(cnp->cn_flags & ISLASTCN)) {
834 cnp->cn_nameptr = ndp->ni_next;
835 if (ndp->ni_dvp == dp) {
836 vrele(ndp->ni_dvp);
837 } else {
838 vput(ndp->ni_dvp);
839 }
840 goto dirloop;
841 }
842
843 terminal:
844 if (dp == ndp->ni_erootdir) {
845 /*
846 * We are about to return the emulation root.
847 * This isn't a good idea because code might repeatedly
848 * lookup ".." until the file matches that returned
849 * for "/" and loop forever.
850 * So convert it to the real root.
851 */
852 if (ndp->ni_dvp == dp)
853 vrele(dp);
854 else
855 if (ndp->ni_dvp != NULL)
856 vput(ndp->ni_dvp);
857 ndp->ni_dvp = NULL;
858 vput(dp);
859 dp = ndp->ni_rootdir;
860 VREF(dp);
861 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
862 ndp->ni_vp = dp;
863 }
864
865 /*
866 * If the caller requested the parent node (i.e.
867 * it's a CREATE, DELETE, or RENAME), and we don't have one
868 * (because this is the root directory), then we must fail.
869 */
870 if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
871 switch (cnp->cn_nameiop) {
872 case CREATE:
873 error = EEXIST;
874 break;
875 case DELETE:
876 case RENAME:
877 error = EBUSY;
878 break;
879 default:
880 KASSERT(0);
881 }
882 vput(dp);
883 goto bad;
884 }
885
886 /*
887 * Disallow directory write attempts on read-only lookups.
888 * Prefers EEXIST over EROFS for the CREATE case.
889 */
890 if (rdonly &&
891 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
892 error = EROFS;
893 if (dp != ndp->ni_dvp) {
894 vput(dp);
895 }
896 goto bad;
897 }
898 if (ndp->ni_dvp != NULL) {
899 if (cnp->cn_flags & SAVESTART) {
900 ndp->ni_startdir = ndp->ni_dvp;
901 VREF(ndp->ni_startdir);
902 }
903 }
904 if ((cnp->cn_flags & LOCKLEAF) == 0) {
905 VOP_UNLOCK(dp, 0);
906 }
907 return (0);
908
909 bad:
910 ndp->ni_vp = NULL;
911 return (error);
912 }
913
914 /*
915 * Reacquire a path name component.
916 * dvp is locked on entry and exit.
917 * *vpp is locked on exit unless it's NULL.
918 */
919 int
920 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
921 {
922 int rdonly; /* lookup read-only flag bit */
923 int error = 0;
924 #ifdef DEBUG
925 uint32_t newhash; /* DEBUG: check name hash */
926 const char *cp; /* DEBUG: check name ptr/len */
927 #endif /* DEBUG */
928
929 /*
930 * Setup: break out flag bits into variables.
931 */
932 rdonly = cnp->cn_flags & RDONLY;
933 cnp->cn_flags &= ~ISSYMLINK;
934
935 /*
936 * Search a new directory.
937 *
938 * The cn_hash value is for use by vfs_cache.
939 * The last component of the filename is left accessible via
940 * cnp->cn_nameptr for callers that need the name. Callers needing
941 * the name set the SAVENAME flag. When done, they assume
942 * responsibility for freeing the pathname buffer.
943 */
944 #ifdef DEBUG
945 cp = NULL;
946 newhash = namei_hash(cnp->cn_nameptr, &cp);
947 if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
948 panic("relookup: bad hash");
949 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
950 panic("relookup: bad len");
951 while (*cp == '/')
952 cp++;
953 if (*cp != 0)
954 panic("relookup: not last component");
955 #endif /* DEBUG */
956
957 /*
958 * Check for degenerate name (e.g. / or "")
959 * which is a way of talking about a directory,
960 * e.g. like "/." or ".".
961 */
962 if (cnp->cn_nameptr[0] == '\0')
963 panic("relookup: null name");
964
965 if (cnp->cn_flags & ISDOTDOT)
966 panic("relookup: lookup on dot-dot");
967
968 /*
969 * We now have a segment name to search for, and a directory to search.
970 */
971 if ((error = VOP_LOOKUP(dvp, vpp, cnp)) != 0) {
972 #ifdef DIAGNOSTIC
973 if (*vpp != NULL)
974 panic("leaf `%s' should be empty", cnp->cn_nameptr);
975 #endif
976 if (error != EJUSTRETURN)
977 goto bad;
978 }
979
980 #ifdef DIAGNOSTIC
981 /*
982 * Check for symbolic link
983 */
984 if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
985 panic("relookup: symlink found");
986 #endif
987
988 /*
989 * Check for read-only lookups.
990 */
991 if (rdonly && cnp->cn_nameiop != LOOKUP) {
992 error = EROFS;
993 if (*vpp) {
994 vput(*vpp);
995 }
996 goto bad;
997 }
998 if (cnp->cn_flags & SAVESTART)
999 VREF(dvp);
1000 return (0);
1001
1002 bad:
1003 *vpp = NULL;
1004 return (error);
1005 }
1006