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