vfs_lookup.c revision 1.88 1 /* $NetBSD: vfs_lookup.c,v 1.88 2007/04/26 16:27:32 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.88 2007/04/26 16:27:32 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 dp = cwdi->cwdi_edir;
334 ndp->ni_erootdir = dp;
335 } else {
336 cnp->cn_flags &= ~TRYEMULROOT;
337 ndp->ni_erootdir = NULL;
338 }
339 }
340 } else
341 ndp->ni_erootdir = NULL;
342 } else {
343 dp = cwdi->cwdi_cdir;
344 cnp->cn_flags &= ~TRYEMULROOT;
345 ndp->ni_erootdir = NULL;
346 }
347
348 #ifdef KTRACE
349 if (KTRPOINT(cnp->cn_lwp->l_proc, KTR_NAMEI)) {
350 if (cnp->cn_flags & TRYEMULROOT) {
351 if (cnp->cn_flags & EMULROOTSET)
352 ktrnamei2(cnp->cn_lwp, "/emul/???", 9,
353 cnp->cn_pnbuf, ndp->ni_pathlen);
354 else
355 ktrnamei2(cnp->cn_lwp,
356 cnp->cn_lwp->l_proc->p_emul->e_path,
357 strlen(cnp->cn_lwp->l_proc->p_emul->e_path),
358 cnp->cn_pnbuf, ndp->ni_pathlen);
359 } else
360 ktrnamei(cnp->cn_lwp, cnp->cn_pnbuf, ndp->ni_pathlen);
361 }
362 #endif
363 #ifdef SYSTRACE
364 if (ISSET(cnp->cn_lwp->l_proc->p_flag, PK_SYSTRACE))
365 systrace_namei(ndp);
366 #endif
367
368 VREF(dp);
369 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
370 /* Loop through symbolic links */
371 for (;;) {
372 if (!dp->v_mount) {
373 /* Give up if the directory is no longer mounted */
374 vput(dp);
375 PNBUF_PUT(cnp->cn_pnbuf);
376 return (ENOENT);
377 }
378 cnp->cn_nameptr = cnp->cn_pnbuf;
379 ndp->ni_startdir = dp;
380 error = lookup(ndp);
381 if (error != 0) {
382 if (ndp->ni_dvp) {
383 vput(ndp->ni_dvp);
384 }
385 if (cnp->cn_flags & TRYEMULROOT) {
386 /* Retry the whole thing from the normal root */
387 cnp->cn_flags &= ~TRYEMULROOT;
388 goto emul_retry;
389 }
390 PNBUF_PUT(cnp->cn_pnbuf);
391 return (error);
392 }
393
394 /*
395 * Check for symbolic link
396 */
397 if ((cnp->cn_flags & ISSYMLINK) == 0) {
398 if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp) {
399 if (ndp->ni_dvp == ndp->ni_vp) {
400 vrele(ndp->ni_dvp);
401 } else {
402 vput(ndp->ni_dvp);
403 }
404 }
405 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
406 PNBUF_PUT(cnp->cn_pnbuf);
407 else
408 cnp->cn_flags |= HASBUF;
409 return (0);
410 }
411
412 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
413 error = ELOOP;
414 break;
415 }
416 if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
417 error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
418 cnp->cn_lwp);
419 if (error != 0)
420 break;
421 }
422 if (ndp->ni_pathlen > 1)
423 cp = PNBUF_GET();
424 else
425 cp = cnp->cn_pnbuf;
426 aiov.iov_base = cp;
427 aiov.iov_len = MAXPATHLEN;
428 auio.uio_iov = &aiov;
429 auio.uio_iovcnt = 1;
430 auio.uio_offset = 0;
431 auio.uio_rw = UIO_READ;
432 auio.uio_resid = MAXPATHLEN;
433 UIO_SETUP_SYSSPACE(&auio);
434 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
435 if (error) {
436 badlink:
437 if (ndp->ni_pathlen > 1)
438 PNBUF_PUT(cp);
439 break;
440 }
441 linklen = MAXPATHLEN - auio.uio_resid;
442 if (linklen == 0) {
443 error = ENOENT;
444 goto badlink;
445 }
446
447 /*
448 * Do symlink substitution, if appropriate, and
449 * check length for potential overflow.
450 */
451 if ((vfs_magiclinks &&
452 symlink_magic(cnp->cn_lwp->l_proc, cp, &linklen)) ||
453 (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
454 error = ENAMETOOLONG;
455 goto badlink;
456 }
457 if (ndp->ni_pathlen > 1) {
458 memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
459 PNBUF_PUT(cnp->cn_pnbuf);
460 cnp->cn_pnbuf = cp;
461 } else
462 cnp->cn_pnbuf[linklen] = '\0';
463 ndp->ni_pathlen += linklen;
464 vput(ndp->ni_vp);
465 dp = ndp->ni_dvp;
466
467 /*
468 * Check if root directory should replace current directory.
469 */
470 if (cnp->cn_pnbuf[0] == '/') {
471 vput(dp);
472 /* Keep absolute symbolic links inside emulation root */
473 dp = ndp->ni_erootdir;
474 if (dp == NULL)
475 dp = ndp->ni_rootdir;
476 VREF(dp);
477 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
478 }
479 }
480 /* Failed to process a symbolic link */
481 KASSERT(ndp->ni_dvp != ndp->ni_vp);
482 vput(ndp->ni_dvp);
483 vput(ndp->ni_vp);
484 ndp->ni_vp = NULL;
485 PNBUF_PUT(cnp->cn_pnbuf);
486 return (error);
487 }
488
489 /*
490 * Determine the namei hash (for cn_hash) for name.
491 * If *ep != NULL, hash from name to ep-1.
492 * If *ep == NULL, hash from name until the first NUL or '/', and
493 * return the location of this termination character in *ep.
494 *
495 * This function returns an equivalent hash to the MI hash32_strn().
496 * The latter isn't used because in the *ep == NULL case, determining
497 * the length of the string to the first NUL or `/' and then calling
498 * hash32_strn() involves unnecessary double-handling of the data.
499 */
500 uint32_t
501 namei_hash(const char *name, const char **ep)
502 {
503 uint32_t hash;
504
505 hash = HASH32_STR_INIT;
506 if (*ep != NULL) {
507 for (; name < *ep; name++)
508 hash = hash * 33 + *(const uint8_t *)name;
509 } else {
510 for (; *name != '\0' && *name != '/'; name++)
511 hash = hash * 33 + *(const uint8_t *)name;
512 *ep = name;
513 }
514 return (hash + (hash >> 5));
515 }
516
517 /*
518 * Search a pathname.
519 * This is a very central and rather complicated routine.
520 *
521 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
522 * The starting directory is taken from ni_startdir. The pathname is
523 * descended until done, or a symbolic link is encountered. The variable
524 * ni_more is clear if the path is completed; it is set to one if a
525 * symbolic link needing interpretation is encountered.
526 *
527 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
528 * whether the name is to be looked up, created, renamed, or deleted.
529 * When CREATE, RENAME, or DELETE is specified, information usable in
530 * creating, renaming, or deleting a directory entry may be calculated.
531 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
532 * locked. Otherwise the parent directory is not returned. If the target
533 * of the pathname exists and LOCKLEAF is or'ed into the flag the target
534 * is returned locked, otherwise it is returned unlocked. When creating
535 * or renaming and LOCKPARENT is specified, the target may not be ".".
536 * When deleting and LOCKPARENT is specified, the target may be ".".
537 *
538 * Overall outline of lookup:
539 *
540 * dirloop:
541 * identify next component of name at ndp->ni_ptr
542 * handle degenerate case where name is null string
543 * if .. and crossing mount points and on mounted filesys, find parent
544 * call VOP_LOOKUP routine for next component name
545 * directory vnode returned in ni_dvp, locked.
546 * component vnode returned in ni_vp (if it exists), locked.
547 * if result vnode is mounted on and crossing mount points,
548 * find mounted on vnode
549 * if more components of name, do next level at dirloop
550 * return the answer in ni_vp, locked if LOCKLEAF set
551 * if LOCKPARENT set, return locked parent in ni_dvp
552 */
553 int
554 lookup(struct nameidata *ndp)
555 {
556 const char *cp; /* pointer into pathname argument */
557 struct vnode *dp = 0; /* the directory we are searching */
558 struct vnode *tdp; /* saved dp */
559 struct mount *mp; /* mount table entry */
560 int docache; /* == 0 do not cache last component */
561 int rdonly; /* lookup read-only flag bit */
562 int error = 0;
563 int slashes;
564 struct componentname *cnp = &ndp->ni_cnd;
565 struct lwp *l = cnp->cn_lwp;
566
567 /*
568 * Setup: break out flag bits into variables.
569 */
570 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
571 if (cnp->cn_nameiop == DELETE)
572 docache = 0;
573 rdonly = cnp->cn_flags & RDONLY;
574 ndp->ni_dvp = NULL;
575 cnp->cn_flags &= ~ISSYMLINK;
576 dp = ndp->ni_startdir;
577 ndp->ni_startdir = NULLVP;
578
579 /*
580 * If we have a leading string of slashes, remove them, and just make
581 * sure the current node is a directory.
582 */
583 cp = cnp->cn_nameptr;
584 if (*cp == '/') {
585 do {
586 cp++;
587 } while (*cp == '/');
588 ndp->ni_pathlen -= cp - cnp->cn_nameptr;
589 cnp->cn_nameptr = cp;
590
591 if (dp->v_type != VDIR) {
592 error = ENOTDIR;
593 vput(dp);
594 goto bad;
595 }
596
597 /*
598 * If we've exhausted the path name, then just return the
599 * current node.
600 */
601 if (cnp->cn_nameptr[0] == '\0') {
602 ndp->ni_vp = dp;
603 cnp->cn_flags |= ISLASTCN;
604 goto terminal;
605 }
606 }
607
608 dirloop:
609 /*
610 * Search a new directory.
611 *
612 * The cn_hash value is for use by vfs_cache.
613 * The last component of the filename is left accessible via
614 * cnp->cn_nameptr for callers that need the name. Callers needing
615 * the name set the SAVENAME flag. When done, they assume
616 * responsibility for freeing the pathname buffer.
617 *
618 * At this point, our only vnode state is that "dp" is held and locked.
619 */
620 cnp->cn_consume = 0;
621 cp = NULL;
622 cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
623 cnp->cn_namelen = cp - cnp->cn_nameptr;
624 if (cnp->cn_namelen > NAME_MAX) {
625 vput(dp);
626 error = ENAMETOOLONG;
627 ndp->ni_dvp = NULL;
628 goto bad;
629 }
630 #ifdef NAMEI_DIAGNOSTIC
631 { char c = *cp;
632 *(char *)cp = '\0';
633 printf("{%s}: ", cnp->cn_nameptr);
634 *(char *)cp = c; }
635 #endif /* NAMEI_DIAGNOSTIC */
636 ndp->ni_pathlen -= cnp->cn_namelen;
637 ndp->ni_next = cp;
638 /*
639 * If this component is followed by a slash, then move the pointer to
640 * the next component forward, and remember that this component must be
641 * a directory.
642 */
643 if (*cp == '/') {
644 do {
645 cp++;
646 } while (*cp == '/');
647 slashes = cp - ndp->ni_next;
648 ndp->ni_pathlen -= slashes;
649 ndp->ni_next = cp;
650 cnp->cn_flags |= REQUIREDIR;
651 } else {
652 slashes = 0;
653 cnp->cn_flags &= ~REQUIREDIR;
654 }
655 /*
656 * We do special processing on the last component, whether or not it's
657 * a directory. Cache all intervening lookups, but not the final one.
658 */
659 if (*cp == '\0') {
660 if (docache)
661 cnp->cn_flags |= MAKEENTRY;
662 else
663 cnp->cn_flags &= ~MAKEENTRY;
664 cnp->cn_flags |= ISLASTCN;
665 } else {
666 cnp->cn_flags |= MAKEENTRY;
667 cnp->cn_flags &= ~ISLASTCN;
668 }
669 if (cnp->cn_namelen == 2 &&
670 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
671 cnp->cn_flags |= ISDOTDOT;
672 else
673 cnp->cn_flags &= ~ISDOTDOT;
674
675 /*
676 * Handle "..": two special cases.
677 * 1. If at root directory (e.g. after chroot)
678 * or at absolute root directory
679 * then ignore it so can't get out.
680 * 1a. If at the root of the emulation filesystem go to the real
681 * root. So "/../<path>" is always absolute.
682 * 1b. If we have somehow gotten out of a jail, warn
683 * and also ignore it so we can't get farther out.
684 * 2. If this vnode is the root of a mounted
685 * filesystem, then replace it with the
686 * vnode which was mounted on so we take the
687 * .. in the other file system.
688 */
689 if (cnp->cn_flags & ISDOTDOT) {
690 struct proc *p = l->l_proc;
691
692 for (;;) {
693 if (dp == ndp->ni_rootdir || dp == ndp->ni_erootdir
694 || dp == rootvnode) {
695 /* ".." at emulation root goes to real root */
696 if (dp != ndp->ni_rootdir)
697 goto setrootdir;
698 ndp->ni_dvp = dp;
699 ndp->ni_vp = dp;
700 VREF(dp);
701 goto nextname;
702 }
703 if (ndp->ni_rootdir != rootvnode) {
704 int retval;
705
706 VOP_UNLOCK(dp, 0);
707 retval = vn_isunder(dp, ndp->ni_rootdir, l);
708 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
709 if (!retval) {
710 /* Oops! We got out of jail! */
711 log(LOG_WARNING,
712 "chrooted pid %d uid %d (%s) "
713 "detected outside of its chroot\n",
714 p->p_pid, kauth_cred_geteuid(l->l_cred),
715 p->p_comm);
716 /* Put us at the jail root. */
717 setrootdir:
718 ndp->ni_erootdir = NULL;
719 vput(dp);
720 dp = ndp->ni_rootdir;
721 ndp->ni_dvp = dp;
722 ndp->ni_vp = dp;
723 VREF(dp);
724 VREF(dp);
725 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
726 goto nextname;
727 }
728 }
729 if ((dp->v_flag & VROOT) == 0 ||
730 (cnp->cn_flags & NOCROSSMOUNT))
731 break;
732 tdp = dp;
733 dp = dp->v_mount->mnt_vnodecovered;
734 vput(tdp);
735 VREF(dp);
736 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
737 }
738 }
739
740 /*
741 * We now have a segment name to search for, and a directory to search.
742 * Again, our only vnode state is that "dp" is held and locked.
743 */
744 unionlookup:
745 ndp->ni_dvp = dp;
746 ndp->ni_vp = NULL;
747 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp);
748 if (error != 0) {
749 #ifdef DIAGNOSTIC
750 if (ndp->ni_vp != NULL)
751 panic("leaf `%s' should be empty", cnp->cn_nameptr);
752 #endif /* DIAGNOSTIC */
753 #ifdef NAMEI_DIAGNOSTIC
754 printf("not found\n");
755 #endif /* NAMEI_DIAGNOSTIC */
756 if ((error == ENOENT) &&
757 (dp->v_flag & VROOT) &&
758 (dp->v_mount->mnt_flag & MNT_UNION)) {
759 tdp = dp;
760 dp = dp->v_mount->mnt_vnodecovered;
761 vput(tdp);
762 VREF(dp);
763 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
764 goto unionlookup;
765 }
766
767 if (error != EJUSTRETURN)
768 goto bad;
769
770 /*
771 * If this was not the last component, or there were trailing
772 * slashes, and we are not going to create a directory,
773 * then the name must exist.
774 */
775 if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
776 error = ENOENT;
777 goto bad;
778 }
779
780 /*
781 * If creating and at end of pathname, then can consider
782 * allowing file to be created.
783 */
784 if (rdonly) {
785 error = EROFS;
786 goto bad;
787 }
788
789 /*
790 * We return with ni_vp NULL to indicate that the entry
791 * doesn't currently exist, leaving a pointer to the
792 * (possibly locked) directory vnode in ndp->ni_dvp.
793 */
794 if (cnp->cn_flags & SAVESTART) {
795 ndp->ni_startdir = ndp->ni_dvp;
796 VREF(ndp->ni_startdir);
797 }
798 return (0);
799 }
800 #ifdef NAMEI_DIAGNOSTIC
801 printf("found\n");
802 #endif /* NAMEI_DIAGNOSTIC */
803
804 /*
805 * Take into account any additional components consumed by the
806 * underlying filesystem. This will include any trailing slashes after
807 * the last component consumed.
808 */
809 if (cnp->cn_consume > 0) {
810 ndp->ni_pathlen -= cnp->cn_consume - slashes;
811 ndp->ni_next += cnp->cn_consume - slashes;
812 cnp->cn_consume = 0;
813 if (ndp->ni_next[0] == '\0')
814 cnp->cn_flags |= ISLASTCN;
815 }
816
817 dp = ndp->ni_vp;
818
819 /*
820 * "dp" and "ndp->ni_dvp" are both locked and held,
821 * and may be the same vnode.
822 */
823
824 /*
825 * Check to see if the vnode has been mounted on;
826 * if so find the root of the mounted file system.
827 */
828 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
829 (cnp->cn_flags & NOCROSSMOUNT) == 0) {
830 if (vfs_busy(mp, 0, 0))
831 continue;
832
833 KASSERT(ndp->ni_dvp != dp);
834 VOP_UNLOCK(ndp->ni_dvp, 0);
835 vput(dp);
836 error = VFS_ROOT(mp, &tdp);
837 vfs_unbusy(mp);
838 if (error) {
839 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
840 goto bad;
841 }
842 VOP_UNLOCK(tdp, 0);
843 ndp->ni_vp = dp = tdp;
844 vn_lock(ndp->ni_dvp, LK_EXCLUSIVE | LK_RETRY);
845 vn_lock(ndp->ni_vp, LK_EXCLUSIVE | LK_RETRY);
846 }
847
848 /*
849 * Check for symbolic link. Back up over any slashes that we skipped,
850 * as we will need them again.
851 */
852 if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
853 ndp->ni_pathlen += slashes;
854 ndp->ni_next -= slashes;
855 cnp->cn_flags |= ISSYMLINK;
856 return (0);
857 }
858
859 /*
860 * Check for directory, if the component was followed by a series of
861 * slashes.
862 */
863 if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
864 error = ENOTDIR;
865 KASSERT(dp != ndp->ni_dvp);
866 vput(dp);
867 goto bad;
868 }
869
870 nextname:
871
872 /*
873 * Not a symbolic link. If this was not the last component, then
874 * continue at the next component, else return.
875 */
876 if (!(cnp->cn_flags & ISLASTCN)) {
877 cnp->cn_nameptr = ndp->ni_next;
878 if (ndp->ni_dvp == dp) {
879 vrele(ndp->ni_dvp);
880 } else {
881 vput(ndp->ni_dvp);
882 }
883 goto dirloop;
884 }
885
886 terminal:
887 if (dp == ndp->ni_erootdir) {
888 /*
889 * We are about to return the emulation root.
890 * This isn't a good idea because code might repeatedly
891 * lookup ".." until the file matches that returned
892 * for "/" and loop forever.
893 * So convert it to the real root.
894 */
895 if (ndp->ni_dvp == dp)
896 vrele(dp);
897 else
898 if (ndp->ni_dvp != NULL)
899 vput(ndp->ni_dvp);
900 ndp->ni_dvp = NULL;
901 vput(dp);
902 dp = ndp->ni_rootdir;
903 VREF(dp);
904 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
905 ndp->ni_vp = dp;
906 }
907
908 /*
909 * If the caller requested the parent node (i.e.
910 * it's a CREATE, DELETE, or RENAME), and we don't have one
911 * (because this is the root directory), then we must fail.
912 */
913 if (ndp->ni_dvp == NULL && cnp->cn_nameiop != LOOKUP) {
914 switch (cnp->cn_nameiop) {
915 case CREATE:
916 error = EEXIST;
917 break;
918 case DELETE:
919 case RENAME:
920 error = EBUSY;
921 break;
922 default:
923 KASSERT(0);
924 }
925 vput(dp);
926 goto bad;
927 }
928
929 /*
930 * Disallow directory write attempts on read-only file systems.
931 */
932 if (rdonly &&
933 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
934
935 /*
936 * Disallow directory write attempts on read-only
937 * file systems.
938 */
939 error = EROFS;
940 if (dp != ndp->ni_dvp) {
941 vput(dp);
942 }
943 goto bad;
944 }
945 if (ndp->ni_dvp != NULL) {
946 if (cnp->cn_flags & SAVESTART) {
947 ndp->ni_startdir = ndp->ni_dvp;
948 VREF(ndp->ni_startdir);
949 }
950 }
951 if ((cnp->cn_flags & LOCKLEAF) == 0) {
952 VOP_UNLOCK(dp, 0);
953 }
954 return (0);
955
956 bad:
957 ndp->ni_vp = NULL;
958 return (error);
959 }
960
961 /*
962 * Reacquire a path name component.
963 * dvp is locked on entry and exit.
964 * *vpp is locked on exit unless it's NULL.
965 */
966 int
967 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
968 {
969 int rdonly; /* lookup read-only flag bit */
970 int error = 0;
971 #ifdef DEBUG
972 uint32_t newhash; /* DEBUG: check name hash */
973 const char *cp; /* DEBUG: check name ptr/len */
974 #endif /* DEBUG */
975
976 /*
977 * Setup: break out flag bits into variables.
978 */
979 rdonly = cnp->cn_flags & RDONLY;
980 cnp->cn_flags &= ~ISSYMLINK;
981
982 /*
983 * Search a new directory.
984 *
985 * The cn_hash value is for use by vfs_cache.
986 * The last component of the filename is left accessible via
987 * cnp->cn_nameptr for callers that need the name. Callers needing
988 * the name set the SAVENAME flag. When done, they assume
989 * responsibility for freeing the pathname buffer.
990 */
991 #ifdef DEBUG
992 cp = NULL;
993 newhash = namei_hash(cnp->cn_nameptr, &cp);
994 if ((uint32_t)newhash != (uint32_t)cnp->cn_hash)
995 panic("relookup: bad hash");
996 if (cnp->cn_namelen != cp - cnp->cn_nameptr)
997 panic("relookup: bad len");
998 while (*cp == '/')
999 cp++;
1000 if (*cp != 0)
1001 panic("relookup: not last component");
1002 #endif /* DEBUG */
1003
1004 /*
1005 * Check for degenerate name (e.g. / or "")
1006 * which is a way of talking about a directory,
1007 * e.g. like "/." or ".".
1008 */
1009 if (cnp->cn_nameptr[0] == '\0')
1010 panic("relookup: null name");
1011
1012 if (cnp->cn_flags & ISDOTDOT)
1013 panic("relookup: lookup on dot-dot");
1014
1015 /*
1016 * We now have a segment name to search for, and a directory to search.
1017 */
1018 if ((error = VOP_LOOKUP(dvp, vpp, cnp)) != 0) {
1019 #ifdef DIAGNOSTIC
1020 if (*vpp != NULL)
1021 panic("leaf `%s' should be empty", cnp->cn_nameptr);
1022 #endif
1023 if (error != EJUSTRETURN)
1024 goto bad;
1025 }
1026
1027 #ifdef DIAGNOSTIC
1028 /*
1029 * Check for symbolic link
1030 */
1031 if (*vpp && (*vpp)->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1032 panic("relookup: symlink found");
1033 #endif
1034
1035 /*
1036 * Check for read-only file systems.
1037 */
1038 if (rdonly && cnp->cn_nameiop != LOOKUP) {
1039 error = EROFS;
1040 if (*vpp) {
1041 vput(*vpp);
1042 }
1043 goto bad;
1044 }
1045 if (cnp->cn_flags & SAVESTART)
1046 VREF(dvp);
1047 return (0);
1048
1049 bad:
1050 *vpp = NULL;
1051 return (error);
1052 }
1053