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