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