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