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