]> git.saurik.com Git - apple/xnu.git/blob - bsd/vfs/vfs_lookup.c
128a8ce04b79918dcbbd842085f59c43992cac1c
[apple/xnu.git] / bsd / vfs / vfs_lookup.c
1 /*
2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
29 /*
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * SUCH DAMAGE.
65 *
66 * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
67 */
68 /*
69 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
72 * Version 2.0.
73 */
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/syslimits.h>
78 #include <sys/time.h>
79 #include <sys/namei.h>
80 #include <sys/vm.h>
81 #include <sys/vnode_internal.h>
82 #include <sys/mount_internal.h>
83 #include <sys/errno.h>
84 #include <sys/malloc.h>
85 #include <sys/filedesc.h>
86 #include <sys/proc_internal.h>
87 #include <sys/kdebug.h>
88 #include <sys/unistd.h> /* For _PC_NAME_MAX */
89 #include <sys/uio_internal.h>
90 #include <sys/kauth.h>
91 #include <kern/kalloc.h>
92 #include <security/audit/audit.h>
93 #include <sys/dtrace.h> /* to get the prototype for strstr() in sys/dtrace_glue.h */
94 #if CONFIG_MACF
95 #include <security/mac_framework.h>
96 #endif
97
98 #if NAMEDRSRCFORK
99 #include <sys/xattr.h>
100 #endif
101 /*
102 * The minimum volfs-style pathname is 9.
103 * Example: "/.vol/1/2"
104 */
105 #define VOLFS_MIN_PATH_LEN 9
106
107
108 static void kdebug_lookup(struct vnode *dp, struct componentname *cnp);
109
110 #if CONFIG_VOLFS
111 static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx);
112 #define MAX_VOLFS_RESTARTS 5
113 #endif
114
115 static int lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, int vbusyflags, vfs_context_t ctx);
116 static int lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx);
117 static int lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx);
118 static void lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation);
119 static int lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly,
120 int vbusyflags, int *keep_going, int nc_generation,
121 int wantparent, int atroot, vfs_context_t ctx);
122 static int lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent);
123
124 #if NAMEDRSRCFORK
125 static int lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx);
126 #endif
127
128
129
130 /*
131 * Convert a pathname into a pointer to a locked inode.
132 *
133 * The FOLLOW flag is set when symbolic links are to be followed
134 * when they occur at the end of the name translation process.
135 * Symbolic links are always followed for all other pathname
136 * components other than the last.
137 *
138 * The segflg defines whether the name is to be copied from user
139 * space or kernel space.
140 *
141 * Overall outline of namei:
142 *
143 * copy in name
144 * get starting directory
145 * while (!done && !error) {
146 * call lookup to search path.
147 * if symbolic link, massage name in buffer and continue
148 * }
149 *
150 * Returns: 0 Success
151 * ENOENT No such file or directory
152 * ELOOP Too many levels of symbolic links
153 * ENAMETOOLONG Filename too long
154 * copyinstr:EFAULT Bad address
155 * copyinstr:ENAMETOOLONG Filename too long
156 * lookup:EBADF Bad file descriptor
157 * lookup:EROFS
158 * lookup:EACCES
159 * lookup:EPERM
160 * lookup:ERECYCLE vnode was recycled from underneath us in lookup.
161 * This means we should re-drive lookup from this point.
162 * lookup: ???
163 * VNOP_READLINK:???
164 */
165 int
166 namei(struct nameidata *ndp)
167 {
168 struct filedesc *fdp; /* pointer to file descriptor state */
169 struct vnode *dp; /* the directory we are searching */
170 struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to
171 heavy vnode pressure */
172 u_long cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */
173 int error;
174 struct componentname *cnp = &ndp->ni_cnd;
175 vfs_context_t ctx = cnp->cn_context;
176 proc_t p = vfs_context_proc(ctx);
177 #if CONFIG_AUDIT
178 /* XXX ut should be from context */
179 uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread());
180 #endif
181
182 #if CONFIG_VOLFS
183 int volfs_restarts = 0;
184 #endif
185
186 fdp = p->p_fd;
187
188 #if DIAGNOSTIC
189 if (!vfs_context_ucred(ctx) || !p)
190 panic ("namei: bad cred/proc");
191 if (cnp->cn_nameiop & (~OPMASK))
192 panic ("namei: nameiop contaminated with flags");
193 if (cnp->cn_flags & OPMASK)
194 panic ("namei: flags contaminated with nameiops");
195 #endif
196
197 /*
198 * A compound VNOP found something that needs further processing:
199 * either a trigger vnode, a covered directory, or a symlink.
200 */
201 if (ndp->ni_flag & NAMEI_CONTLOOKUP) {
202 int rdonly, vbusyflags, keep_going, wantparent;
203
204 rdonly = cnp->cn_flags & RDONLY;
205 vbusyflags = ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) ? LK_NOWAIT : 0;
206 keep_going = 0;
207 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
208
209 ndp->ni_flag &= ~(NAMEI_CONTLOOKUP);
210
211 error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags,
212 &keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx);
213 if (error)
214 goto out_drop;
215 if (keep_going) {
216 if ((cnp->cn_flags & ISSYMLINK) == 0) {
217 panic("We need to keep going on a continued lookup, but for vp type %d (tag %d)\n", ndp->ni_vp->v_type, ndp->ni_vp->v_tag);
218 }
219 goto continue_symlink;
220 }
221
222 return 0;
223
224 }
225
226 vnode_recycled:
227
228 /*
229 * Get a buffer for the name to be translated, and copy the
230 * name into the buffer.
231 */
232 if ((cnp->cn_flags & HASBUF) == 0) {
233 cnp->cn_pnbuf = ndp->ni_pathbuf;
234 cnp->cn_pnlen = PATHBUFLEN;
235 }
236 #if LP64_DEBUG
237 if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0)
238 && (ndp->ni_segflg != UIO_SYSSPACE)
239 && (ndp->ni_segflg != UIO_SYSSPACE32)) {
240 panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__);
241 }
242 #endif /* LP64_DEBUG */
243
244 retry_copy:
245 if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) {
246 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
247 cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
248 } else {
249 error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf,
250 cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen);
251 }
252 if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) {
253 MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
254 if (cnp->cn_pnbuf == NULL) {
255 error = ENOMEM;
256 goto error_out;
257 }
258
259 cnp->cn_flags |= HASBUF;
260 cnp->cn_pnlen = MAXPATHLEN;
261
262 goto retry_copy;
263 }
264 if (error)
265 goto error_out;
266
267 /*
268 * Since the name cache may contain positive entries of
269 * the incorrect case, force lookup() to bypass the cache
270 * and call directly into the filesystem for each path
271 * component. Note: the FS may still consult the cache,
272 * but can apply rules to validate the results.
273 */
274 if (proc_is_forcing_hfs_case_sensitivity(p))
275 cnp->cn_flags |= CN_SKIPNAMECACHE;
276
277 #if CONFIG_VOLFS
278 /*
279 * Check for legacy volfs style pathnames.
280 *
281 * For compatibility reasons we currently allow these paths,
282 * but future versions of the OS may not support them.
283 */
284 if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN &&
285 cnp->cn_pnbuf[0] == '/' &&
286 cnp->cn_pnbuf[1] == '.' &&
287 cnp->cn_pnbuf[2] == 'v' &&
288 cnp->cn_pnbuf[3] == 'o' &&
289 cnp->cn_pnbuf[4] == 'l' &&
290 cnp->cn_pnbuf[5] == '/' ) {
291 char * realpath;
292 int realpath_err;
293 /* Attempt to resolve a legacy volfs style pathname. */
294 MALLOC_ZONE(realpath, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
295 if (realpath) {
296 /*
297 * We only error out on the ENAMETOOLONG cases where we know that
298 * vfs_getrealpath translation succeeded but the path could not fit into
299 * MAXPATHLEN characters. In other failure cases, we may be dealing with a path
300 * that legitimately looks like /.vol/1234/567 and is not meant to be translated
301 */
302 if ((realpath_err= vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) {
303 FREE_ZONE(realpath, MAXPATHLEN, M_NAMEI);
304 if (realpath_err == ENOSPC || realpath_err == ENAMETOOLONG){
305 error = ENAMETOOLONG;
306 goto error_out;
307 }
308 } else {
309 if (cnp->cn_flags & HASBUF) {
310 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
311 }
312 cnp->cn_pnbuf = realpath;
313 cnp->cn_pnlen = MAXPATHLEN;
314 ndp->ni_pathlen = strlen(realpath) + 1;
315 cnp->cn_flags |= HASBUF | CN_VOLFSPATH;
316 }
317 }
318 }
319 #endif /* CONFIG_VOLFS */
320
321 #if CONFIG_AUDIT
322 /* If we are auditing the kernel pathname, save the user pathname */
323 if (cnp->cn_flags & AUDITVNPATH1)
324 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1);
325 if (cnp->cn_flags & AUDITVNPATH2)
326 AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2);
327 #endif /* CONFIG_AUDIT */
328
329 /*
330 * Do not allow empty pathnames
331 */
332 if (*cnp->cn_pnbuf == '\0') {
333 error = ENOENT;
334 goto error_out;
335 }
336 ndp->ni_loopcnt = 0;
337
338 /*
339 * determine the starting point for the translation.
340 */
341 if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) {
342 if ( !(fdp->fd_flags & FD_CHROOT))
343 ndp->ni_rootdir = rootvnode;
344 }
345 cnp->cn_nameptr = cnp->cn_pnbuf;
346
347 ndp->ni_usedvp = NULLVP;
348
349 if (*(cnp->cn_nameptr) == '/') {
350 while (*(cnp->cn_nameptr) == '/') {
351 cnp->cn_nameptr++;
352 ndp->ni_pathlen--;
353 }
354 dp = ndp->ni_rootdir;
355 } else if (cnp->cn_flags & USEDVP) {
356 dp = ndp->ni_dvp;
357 ndp->ni_usedvp = dp;
358 } else
359 dp = vfs_context_cwd(ctx);
360
361 if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) {
362 error = ENOENT;
363 goto error_out;
364 }
365 ndp->ni_dvp = NULLVP;
366 ndp->ni_vp = NULLVP;
367
368 for (;;) {
369 ndp->ni_startdir = dp;
370
371 if ( (error = lookup(ndp)) ) {
372 goto error_out;
373 }
374
375 /*
376 * Check for symbolic link
377 */
378 if ((cnp->cn_flags & ISSYMLINK) == 0) {
379 return (0);
380 }
381
382 continue_symlink:
383 /* Gives us a new path to process, and a starting dir */
384 error = lookup_handle_symlink(ndp, &dp, ctx);
385 if (error != 0) {
386 break;
387 }
388 }
389 /*
390 * only come here if we fail to handle a SYMLINK...
391 * if either ni_dvp or ni_vp is non-NULL, then
392 * we need to drop the iocount that was picked
393 * up in the lookup routine
394 */
395 out_drop:
396 if (ndp->ni_dvp)
397 vnode_put(ndp->ni_dvp);
398 if (ndp->ni_vp)
399 vnode_put(ndp->ni_vp);
400 error_out:
401 if ( (cnp->cn_flags & HASBUF) ) {
402 cnp->cn_flags &= ~HASBUF;
403 FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI);
404 }
405 cnp->cn_pnbuf = NULL;
406 ndp->ni_vp = NULLVP;
407 ndp->ni_dvp = NULLVP;
408
409 #if CONFIG_VOLFS
410 /*
411 * Deal with volfs fallout.
412 *
413 * At this point, if we were originally given a volfs path that
414 * looks like /.vol/123/456, then we would have had to convert it into
415 * a full path. Assuming that part worked properly, we will now attempt
416 * to conduct a lookup of the item in the namespace. Under normal
417 * circumstances, if a user looked up /tmp/foo and it was not there, it
418 * would be permissible to return ENOENT.
419 *
420 * However, we may not want to do that here. Specifically, the volfs path
421 * uniquely identifies a certain item in the namespace regardless of where it
422 * lives. If the item has moved in between the time we constructed the
423 * path and now, when we're trying to do a lookup/authorization on the full
424 * path, we may have gotten an ENOENT.
425 *
426 * At this point we can no longer tell if the path no longer exists
427 * or if the item in question no longer exists. It could have been renamed
428 * away, in which case the /.vol identifier is still valid.
429 *
430 * Do this dance a maximum of MAX_VOLFS_RESTARTS times.
431 */
432 if ((error == ENOENT) && (ndp->ni_cnd.cn_flags & CN_VOLFSPATH)) {
433 if (volfs_restarts < MAX_VOLFS_RESTARTS) {
434 volfs_restarts++;
435 goto vnode_recycled;
436 }
437 }
438 #endif
439
440 if (error == ERECYCLE){
441 /* vnode was recycled underneath us. re-drive lookup to start at
442 the beginning again, since recycling invalidated last lookup*/
443 ndp->ni_cnd.cn_flags = cnpflags;
444 ndp->ni_dvp = usedvp;
445 goto vnode_recycled;
446 }
447
448
449 return (error);
450 }
451
452 int
453 namei_compound_available(vnode_t dp, struct nameidata *ndp)
454 {
455 if ((ndp->ni_flag & NAMEI_COMPOUNDOPEN) != 0) {
456 return vnode_compound_open_available(dp);
457 }
458
459 return 0;
460 }
461 static int
462 lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx)
463 {
464 #if !CONFIG_MACF
465 #pragma unused(cnp)
466 #endif
467
468 int error;
469
470 if (!dp_authorized_in_cache) {
471 error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx);
472 if (error)
473 return error;
474 }
475 #if CONFIG_MACF
476 error = mac_vnode_check_lookup(ctx, dp, cnp);
477 if (error)
478 return error;
479 #endif /* CONFIG_MACF */
480
481 return 0;
482 }
483
484 static void
485 lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation)
486 {
487 int isdot_or_dotdot;
488 isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT);
489
490 if (vp->v_name == NULL || vp->v_parent == NULLVP) {
491 int update_flags = 0;
492
493 if (isdot_or_dotdot == 0) {
494 if (vp->v_name == NULL)
495 update_flags |= VNODE_UPDATE_NAME;
496 if (dvp != NULLVP && vp->v_parent == NULLVP)
497 update_flags |= VNODE_UPDATE_PARENT;
498
499 if (update_flags)
500 vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags);
501 }
502 }
503 if ( (cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) {
504 /*
505 * missing from name cache, but should
506 * be in it... this can happen if volfs
507 * causes the vnode to be created or the
508 * name cache entry got recycled but the
509 * vnode didn't...
510 * check to make sure that ni_dvp is valid
511 * cache_lookup_path may return a NULL
512 * do a quick check to see if the generation of the
513 * directory matches our snapshot... this will get
514 * rechecked behind the name cache lock, but if it
515 * already fails to match, no need to go any further
516 */
517 if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot))
518 cache_enter_with_gen(dvp, vp, cnp, nc_generation);
519 }
520
521 }
522
523 #if NAMEDRSRCFORK
524 /*
525 * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and
526 * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory
527 * (if one was provided).
528 */
529 static int
530 lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx)
531 {
532 vnode_t svp = NULLVP;
533 enum nsoperation nsop;
534 int error;
535
536 if (dp->v_type != VREG) {
537 error = ENOENT;
538 goto out;
539 }
540 switch (cnp->cn_nameiop) {
541 case DELETE:
542 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
543 nsop = NS_DELETE;
544 } else {
545 error = EPERM;
546 goto out;
547 }
548 break;
549 case CREATE:
550 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
551 nsop = NS_CREATE;
552 } else {
553 error = EPERM;
554 goto out;
555 }
556 break;
557 case LOOKUP:
558 /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */
559 if (cnp->cn_flags & CN_ALLOWRSRCFORK) {
560 nsop = NS_OPEN;
561 } else {
562 error = EPERM;
563 goto out;
564 }
565 break;
566 default:
567 error = EPERM;
568 goto out;
569 }
570 /* Ask the file system for the resource fork. */
571 error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, 0, ctx);
572
573 /* During a create, it OK for stream vnode to be missing. */
574 if (error == ENOATTR || error == ENOENT) {
575 error = (nsop == NS_CREATE) ? 0 : ENOENT;
576 }
577 if (error) {
578 goto out;
579 }
580 /* The "parent" of the stream is the file. */
581 if (wantparent) {
582 if (ndp->ni_dvp) {
583 vnode_put(ndp->ni_dvp);
584 }
585 ndp->ni_dvp = dp;
586 } else {
587 vnode_put(dp);
588 }
589 ndp->ni_vp = svp; /* on create this may be null */
590
591 /* Restore the truncated pathname buffer (for audits). */
592 if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') {
593 ndp->ni_next[0] = '/';
594 }
595 cnp->cn_flags &= ~MAKEENTRY;
596
597 return 0;
598 out:
599 return error;
600 }
601 #endif /* NAMEDRSRCFORK */
602
603 /*
604 * iocounts in:
605 * --One on ni_vp. One on ni_dvp if there is more path, or we didn't come through the
606 * cache, or we came through the cache and the caller doesn't want the parent.
607 *
608 * iocounts out:
609 * --Leaves us in the correct state for the next step, whatever that might be.
610 * --If we find a symlink, returns with iocounts on both ni_vp and ni_dvp.
611 * --If we are to look up another component, then we have an iocount on ni_vp and
612 * nothing else.
613 * --If we are done, returns an iocount on ni_vp, and possibly on ni_dvp depending on nameidata flags.
614 * --In the event of an error, may return with ni_dvp NULL'ed out (in which case, iocount
615 * was dropped).
616 */
617 static int
618 lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly,
619 int vbusyflags, int *keep_going, int nc_generation,
620 int wantparent, int atroot, vfs_context_t ctx)
621 {
622 vnode_t dp;
623 int error;
624 char *cp;
625
626 dp = ndp->ni_vp;
627 *keep_going = 0;
628
629 if (ndp->ni_vp == NULLVP) {
630 panic("NULL ni_vp in %s\n", __FUNCTION__);
631 }
632
633 if (atroot) {
634 goto nextname;
635 }
636
637 /*
638 * Take into account any additional components consumed by
639 * the underlying filesystem.
640 */
641 if (cnp->cn_consume > 0) {
642 cnp->cn_nameptr += cnp->cn_consume;
643 ndp->ni_next += cnp->cn_consume;
644 ndp->ni_pathlen -= cnp->cn_consume;
645 cnp->cn_consume = 0;
646 } else {
647 lookup_consider_update_cache(ndp->ni_dvp, dp, cnp, nc_generation);
648 }
649
650 /*
651 * Check to see if the vnode has been mounted on...
652 * if so find the root of the mounted file system.
653 * Updates ndp->ni_vp.
654 */
655 error = lookup_traverse_mountpoints(ndp, cnp, dp, vbusyflags, ctx);
656 dp = ndp->ni_vp;
657 if (error) {
658 goto out;
659 }
660
661 #if CONFIG_MACF
662 if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) {
663 error = vnode_label(vnode_mount(dp), NULL, dp, NULL, 0, ctx);
664 if (error)
665 goto out;
666 }
667 #endif
668
669 /*
670 * Check for symbolic link
671 */
672 if ((dp->v_type == VLNK) &&
673 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
674 cnp->cn_flags |= ISSYMLINK;
675 *keep_going = 1;
676 return (0);
677 }
678
679 /*
680 * Check for bogus trailing slashes.
681 */
682 if ((ndp->ni_flag & NAMEI_TRAILINGSLASH)) {
683 if (dp->v_type != VDIR) {
684 error = ENOTDIR;
685 goto out;
686 }
687 ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH);
688 }
689
690 #if NAMEDSTREAMS
691 /*
692 * Deny namei/lookup requests to resolve paths that point to shadow files.
693 * Access to shadow files must be conducted by explicit calls to VNOP_LOOKUP
694 * directly, and not use lookup/namei
695 */
696 if (vnode_isshadow (dp)) {
697 error = ENOENT;
698 goto out;
699 }
700 #endif
701
702 nextname:
703 /*
704 * Not a symbolic link. If more pathname,
705 * continue at next component, else return.
706 *
707 * Definitely have a dvp if there's another slash
708 */
709 if (*ndp->ni_next == '/') {
710 cnp->cn_nameptr = ndp->ni_next + 1;
711 ndp->ni_pathlen--;
712 while (*cnp->cn_nameptr == '/') {
713 cnp->cn_nameptr++;
714 ndp->ni_pathlen--;
715 }
716
717 cp = cnp->cn_nameptr;
718 vnode_put(ndp->ni_dvp);
719 ndp->ni_dvp = NULLVP;
720
721 if (*cp == '\0') {
722 goto emptyname;
723 }
724
725 *keep_going = 1;
726 return 0;
727 }
728
729 /*
730 * Disallow directory write attempts on read-only file systems.
731 */
732 if (rdonly &&
733 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
734 error = EROFS;
735 goto out;
736 }
737
738 /* If SAVESTART is set, we should have a dvp */
739 if (cnp->cn_flags & SAVESTART) {
740 /*
741 * note that we already hold a reference
742 * on both dp and ni_dvp, but for some reason
743 * can't get another one... in this case we
744 * need to do vnode_put on dp in 'bad2'
745 */
746 if ( (vnode_get(ndp->ni_dvp)) ) {
747 error = ENOENT;
748 goto out;
749 }
750 ndp->ni_startdir = ndp->ni_dvp;
751 }
752 if (!wantparent && ndp->ni_dvp) {
753 vnode_put(ndp->ni_dvp);
754 ndp->ni_dvp = NULLVP;
755 }
756
757 if (cnp->cn_flags & AUDITVNPATH1)
758 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
759 else if (cnp->cn_flags & AUDITVNPATH2)
760 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
761
762 #if NAMEDRSRCFORK
763 /*
764 * Caller wants the resource fork.
765 */
766 if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) {
767 error = lookup_handle_rsrc_fork(dp, ndp, cnp, wantparent, ctx);
768 if (error != 0)
769 goto out;
770
771 dp = ndp->ni_vp;
772 }
773 #endif
774 if (kdebug_enable)
775 kdebug_lookup(ndp->ni_vp, cnp);
776
777 return 0;
778
779 emptyname:
780 error = lookup_handle_emptyname(ndp, cnp, wantparent);
781 if (error != 0)
782 goto out;
783
784 return 0;
785 out:
786 return error;
787
788 }
789
790 /*
791 * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value.
792 */
793 static int
794 lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent)
795 {
796 vnode_t dp;
797 int error = 0;
798
799 dp = ndp->ni_vp;
800 cnp->cn_namelen = 0;
801 /*
802 * A degenerate name (e.g. / or "") which is a way of
803 * talking about a directory, e.g. like "/." or ".".
804 */
805 if (dp->v_type != VDIR) {
806 error = ENOTDIR;
807 goto out;
808 }
809 if (cnp->cn_nameiop != LOOKUP) {
810 error = EISDIR;
811 goto out;
812 }
813 if (wantparent) {
814 /*
815 * note that we already hold a reference
816 * on dp, but for some reason can't
817 * get another one... in this case we
818 * need to do vnode_put on dp in 'bad'
819 */
820 if ( (vnode_get(dp)) ) {
821 error = ENOENT;
822 goto out;
823 }
824 ndp->ni_dvp = dp;
825 }
826 cnp->cn_flags &= ~ISDOTDOT;
827 cnp->cn_flags |= ISLASTCN;
828 ndp->ni_next = cnp->cn_nameptr;
829 ndp->ni_vp = dp;
830
831 if (cnp->cn_flags & AUDITVNPATH1)
832 AUDIT_ARG(vnpath, dp, ARG_VNODE1);
833 else if (cnp->cn_flags & AUDITVNPATH2)
834 AUDIT_ARG(vnpath, dp, ARG_VNODE2);
835 if (cnp->cn_flags & SAVESTART)
836 panic("lookup: SAVESTART");
837
838 return 0;
839 out:
840 return error;
841 }
842 /*
843 * Search a pathname.
844 * This is a very central and rather complicated routine.
845 *
846 * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
847 * The starting directory is taken from ni_startdir. The pathname is
848 * descended until done, or a symbolic link is encountered. The variable
849 * ni_more is clear if the path is completed; it is set to one if a
850 * symbolic link needing interpretation is encountered.
851 *
852 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
853 * whether the name is to be looked up, created, renamed, or deleted.
854 * When CREATE, RENAME, or DELETE is specified, information usable in
855 * creating, renaming, or deleting a directory entry may be calculated.
856 * If flag has LOCKPARENT or'ed into it, the parent directory is returned
857 * locked. If flag has WANTPARENT or'ed into it, the parent directory is
858 * returned unlocked. Otherwise the parent directory is not returned. If
859 * the target of the pathname exists and LOCKLEAF is or'ed into the flag
860 * the target is returned locked, otherwise it is returned unlocked.
861 * When creating or renaming and LOCKPARENT is specified, the target may not
862 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
863 *
864 * Overall outline of lookup:
865 *
866 * dirloop:
867 * identify next component of name at ndp->ni_ptr
868 * handle degenerate case where name is null string
869 * if .. and crossing mount points and on mounted filesys, find parent
870 * call VNOP_LOOKUP routine for next component name
871 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
872 * component vnode returned in ni_vp (if it exists), locked.
873 * if result vnode is mounted on and crossing mount points,
874 * find mounted on vnode
875 * if more components of name, do next level at dirloop
876 * return the answer in ni_vp, locked if LOCKLEAF set
877 * if LOCKPARENT set, return locked parent in ni_dvp
878 * if WANTPARENT set, return unlocked parent in ni_dvp
879 *
880 * Returns: 0 Success
881 * ENOENT No such file or directory
882 * EBADF Bad file descriptor
883 * ENOTDIR Not a directory
884 * EROFS Read-only file system [CREATE]
885 * EISDIR Is a directory [CREATE]
886 * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again)
887 * vnode_authorize:EROFS
888 * vnode_authorize:EACCES
889 * vnode_authorize:EPERM
890 * vnode_authorize:???
891 * VNOP_LOOKUP:ENOENT No such file or directory
892 * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL)
893 * VNOP_LOOKUP:???
894 * VFS_ROOT:ENOTSUP
895 * VFS_ROOT:ENOENT
896 * VFS_ROOT:???
897 */
898 int
899 lookup(struct nameidata *ndp)
900 {
901 char *cp; /* pointer into pathname argument */
902 vnode_t tdp; /* saved dp */
903 vnode_t dp; /* the directory we are searching */
904 int docache = 1; /* == 0 do not cache last component */
905 int wantparent; /* 1 => wantparent or lockparent flag */
906 int rdonly; /* lookup read-only flag bit */
907 int dp_authorized = 0;
908 int error = 0;
909 struct componentname *cnp = &ndp->ni_cnd;
910 vfs_context_t ctx = cnp->cn_context;
911 int vbusyflags = 0;
912 int nc_generation = 0;
913 vnode_t last_dp = NULLVP;
914 int keep_going;
915 int atroot;
916
917 /*
918 * Setup: break out flag bits into variables.
919 */
920 if (cnp->cn_flags & NOCACHE) {
921 docache = 0;
922 }
923 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
924 rdonly = cnp->cn_flags & RDONLY;
925 cnp->cn_flags &= ~ISSYMLINK;
926 cnp->cn_consume = 0;
927
928 dp = ndp->ni_startdir;
929 ndp->ni_startdir = NULLVP;
930
931 if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0)
932 vbusyflags = LK_NOWAIT;
933 cp = cnp->cn_nameptr;
934
935 if (*cp == '\0') {
936 if ( (vnode_getwithref(dp)) ) {
937 dp = NULLVP;
938 error = ENOENT;
939 goto bad;
940 }
941 ndp->ni_vp = dp;
942 error = lookup_handle_emptyname(ndp, cnp, wantparent);
943 if (error) {
944 goto bad;
945 }
946
947 return 0;
948 }
949 dirloop:
950 atroot = 0;
951 ndp->ni_vp = NULLVP;
952
953 if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp)) ) {
954 dp = NULLVP;
955 goto bad;
956 }
957 if ((cnp->cn_flags & ISLASTCN)) {
958 if (docache)
959 cnp->cn_flags |= MAKEENTRY;
960 } else
961 cnp->cn_flags |= MAKEENTRY;
962
963 dp = ndp->ni_dvp;
964
965 if (ndp->ni_vp != NULLVP) {
966 /*
967 * cache_lookup_path returned a non-NULL ni_vp then,
968 * we're guaranteed that the dp is a VDIR, it's
969 * been authorized, and vp is not ".."
970 *
971 * make sure we don't try to enter the name back into
972 * the cache if this vp is purged before we get to that
973 * check since we won't have serialized behind whatever
974 * activity is occurring in the FS that caused the purge
975 */
976 if (dp != NULLVP)
977 nc_generation = dp->v_nc_generation - 1;
978
979 goto returned_from_lookup_path;
980 }
981
982 /*
983 * Handle "..": two special cases.
984 * 1. If at root directory (e.g. after chroot)
985 * or at absolute root directory
986 * then ignore it so can't get out.
987 * 2. If this vnode is the root of a mounted
988 * filesystem, then replace it with the
989 * vnode which was mounted on so we take the
990 * .. in the other file system.
991 */
992 if ( (cnp->cn_flags & ISDOTDOT) ) {
993 /*
994 * if this is a chroot'ed process, check if the current
995 * directory is still a subdirectory of the process's
996 * root directory.
997 */
998 if (ndp->ni_rootdir && (ndp->ni_rootdir != rootvnode) &&
999 dp != ndp->ni_rootdir) {
1000 int sdir_error;
1001 int is_subdir = FALSE;
1002
1003 sdir_error = vnode_issubdir(dp, ndp->ni_rootdir,
1004 &is_subdir, vfs_context_kernel());
1005
1006 /*
1007 * If we couldn't determine if dp is a subdirectory of
1008 * ndp->ni_rootdir (sdir_error != 0), we let the request
1009 * proceed.
1010 */
1011 if (!sdir_error && !is_subdir) {
1012 vnode_put(dp);
1013 dp = ndp->ni_rootdir;
1014 /*
1015 * There's a ref on the process's root directory
1016 * but we can't use vnode_getwithref here as
1017 * there is nothing preventing that ref being
1018 * released by another thread.
1019 */
1020 if (vnode_get(dp)) {
1021 error = ENOENT;
1022 goto bad;
1023 }
1024 }
1025 }
1026
1027 for (;;) {
1028 if (dp == ndp->ni_rootdir || dp == rootvnode) {
1029 ndp->ni_dvp = dp;
1030 ndp->ni_vp = dp;
1031 /*
1032 * we're pinned at the root
1033 * we've already got one reference on 'dp'
1034 * courtesy of cache_lookup_path... take
1035 * another one for the ".."
1036 * if we fail to get the new reference, we'll
1037 * drop our original down in 'bad'
1038 */
1039 if ( (vnode_get(dp)) ) {
1040 error = ENOENT;
1041 goto bad;
1042 }
1043 atroot = 1;
1044 goto returned_from_lookup_path;
1045 }
1046 if ((dp->v_flag & VROOT) == 0 ||
1047 (cnp->cn_flags & NOCROSSMOUNT))
1048 break;
1049 if (dp->v_mount == NULL) { /* forced umount */
1050 error = EBADF;
1051 goto bad;
1052 }
1053 tdp = dp;
1054 dp = tdp->v_mount->mnt_vnodecovered;
1055
1056 vnode_put(tdp);
1057
1058 if ( (vnode_getwithref(dp)) ) {
1059 dp = NULLVP;
1060 error = ENOENT;
1061 goto bad;
1062 }
1063 ndp->ni_dvp = dp;
1064 dp_authorized = 0;
1065 }
1066 }
1067
1068 /*
1069 * We now have a segment name to search for, and a directory to search.
1070 */
1071 unionlookup:
1072 ndp->ni_vp = NULLVP;
1073
1074 if (dp->v_type != VDIR) {
1075 error = ENOTDIR;
1076 goto lookup_error;
1077 }
1078 if ( (cnp->cn_flags & DONOTAUTH) != DONOTAUTH ) {
1079 error = lookup_authorize_search(dp, cnp, dp_authorized, ctx);
1080 if (error) {
1081 goto lookup_error;
1082 }
1083 }
1084
1085 /*
1086 * Now that we've authorized a lookup, can bail out if the filesystem
1087 * will be doing a batched operation. Return an iocount on dvp.
1088 */
1089 #if NAMEDRSRCFORK
1090 if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) {
1091 #else
1092 if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp)) {
1093 #endif /* NAMEDRSRCFORK */
1094 ndp->ni_flag |= NAMEI_UNFINISHED;
1095 ndp->ni_ncgeneration = dp->v_nc_generation;
1096 return 0;
1097 }
1098
1099 nc_generation = dp->v_nc_generation;
1100
1101 /*
1102 * Note:
1103 * Filesystems that support hardlinks may want to call vnode_update_identity
1104 * if the lookup operation below will modify the in-core vnode to belong to a new point
1105 * in the namespace. VFS cannot infer whether or not the look up operation makes the vnode
1106 * name change or change parents. Without this, the lookup may make update
1107 * filesystem-specific in-core metadata but fail to update the v_parent or v_name
1108 * fields in the vnode. If VFS were to do this, it would be necessary to call
1109 * vnode_update_identity on every lookup operation -- expensive!
1110 *
1111 * However, even with this in place, multiple lookups may occur in between this lookup
1112 * and the subsequent vnop, so, at best, we could only guarantee that you would get a
1113 * valid path back, and not necessarily the one that you wanted.
1114 *
1115 * Example:
1116 * /tmp/a == /foo/b
1117 *
1118 * If you are now looking up /foo/b and the vnode for this link represents /tmp/a,
1119 * vnode_update_identity will fix the parentage so that you can get /foo/b back
1120 * through the v_parent chain (preventing you from getting /tmp/b back). It would
1121 * not fix whether or not you should or should not get /tmp/a vs. /foo/b.
1122 */
1123
1124 error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx);
1125
1126 if ( error ) {
1127 lookup_error:
1128 if ((error == ENOENT) &&
1129 (dp->v_mount != NULL) &&
1130 (dp->v_mount->mnt_flag & MNT_UNION)) {
1131 tdp = dp;
1132 error = lookup_traverse_union(tdp, &dp, ctx);
1133 vnode_put(tdp);
1134 if (error) {
1135 dp = NULLVP;
1136 goto bad;
1137 }
1138
1139 ndp->ni_dvp = dp;
1140 dp_authorized = 0;
1141 goto unionlookup;
1142 }
1143
1144 if (error != EJUSTRETURN)
1145 goto bad;
1146
1147 if (ndp->ni_vp != NULLVP)
1148 panic("leaf should be empty");
1149
1150 #if NAMEDRSRCFORK
1151 /*
1152 * At this point, error should be EJUSTRETURN.
1153 *
1154 * If CN_WANTSRSRCFORK is set, that implies that the
1155 * underlying filesystem could not find the "parent" of the
1156 * resource fork (the data fork), and we are doing a lookup
1157 * for a CREATE event.
1158 *
1159 * However, this should be converted to an error, as the
1160 * failure to find this parent should disallow further
1161 * progress to try and acquire a resource fork vnode.
1162 */
1163 if (cnp->cn_flags & CN_WANTSRSRCFORK) {
1164 error = ENOENT;
1165 goto bad;
1166 }
1167 #endif
1168
1169 error = lookup_validate_creation_path(ndp);
1170 if (error)
1171 goto bad;
1172 /*
1173 * We return with ni_vp NULL to indicate that the entry
1174 * doesn't currently exist, leaving a pointer to the
1175 * referenced directory vnode in ndp->ni_dvp.
1176 */
1177 if (cnp->cn_flags & SAVESTART) {
1178 if ( (vnode_get(ndp->ni_dvp)) ) {
1179 error = ENOENT;
1180 goto bad;
1181 }
1182 ndp->ni_startdir = ndp->ni_dvp;
1183 }
1184 if (!wantparent)
1185 vnode_put(ndp->ni_dvp);
1186
1187 if (kdebug_enable)
1188 kdebug_lookup(ndp->ni_dvp, cnp);
1189 return (0);
1190 }
1191 returned_from_lookup_path:
1192 /* We'll always have an iocount on ni_vp when this finishes. */
1193 error = lookup_handle_found_vnode(ndp, cnp, rdonly, vbusyflags, &keep_going, nc_generation, wantparent, atroot, ctx);
1194 if (error != 0) {
1195 goto bad2;
1196 }
1197
1198 if (keep_going) {
1199 dp = ndp->ni_vp;
1200
1201 /* namei() will handle symlinks */
1202 if ((dp->v_type == VLNK) &&
1203 ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) {
1204 return 0;
1205 }
1206
1207 /*
1208 * Otherwise, there's more path to process.
1209 * cache_lookup_path is now responsible for dropping io ref on dp
1210 * when it is called again in the dirloop. This ensures we hold
1211 * a ref on dp until we complete the next round of lookup.
1212 */
1213 last_dp = dp;
1214
1215 goto dirloop;
1216 }
1217
1218 return (0);
1219 bad2:
1220 if (ndp->ni_dvp)
1221 vnode_put(ndp->ni_dvp);
1222
1223 vnode_put(ndp->ni_vp);
1224 ndp->ni_vp = NULLVP;
1225
1226 if (kdebug_enable)
1227 kdebug_lookup(dp, cnp);
1228 return (error);
1229
1230 bad:
1231 if (dp)
1232 vnode_put(dp);
1233 ndp->ni_vp = NULLVP;
1234
1235 if (kdebug_enable)
1236 kdebug_lookup(dp, cnp);
1237 return (error);
1238 }
1239
1240 /*
1241 * Given a vnode in a union mount, traverse to the equivalent
1242 * vnode in the underlying mount.
1243 */
1244 int
1245 lookup_traverse_union(vnode_t dvp, vnode_t *new_dvp, vfs_context_t ctx)
1246 {
1247 char *path = NULL, *pp;
1248 const char *name, *np;
1249 int len;
1250 int error = 0;
1251 struct nameidata nd;
1252 vnode_t vp = dvp;
1253
1254 *new_dvp = NULL;
1255
1256 if (vp && vp->v_flag & VROOT) {
1257 *new_dvp = vp->v_mount->mnt_vnodecovered;
1258 if (vnode_getwithref(*new_dvp))
1259 return ENOENT;
1260 return 0;
1261 }
1262
1263 path = (char *) kalloc(MAXPATHLEN);
1264 if (path == NULL) {
1265 error = ENOMEM;
1266 goto done;
1267 }
1268
1269 /*
1270 * Walk back up to the mountpoint following the
1271 * v_parent chain and build a slash-separated path.
1272 * Then lookup that path starting with the covered vnode.
1273 */
1274 pp = path + (MAXPATHLEN - 1);
1275 *pp = '\0';
1276
1277 while (1) {
1278 name = vnode_getname(vp);
1279 if (name == NULL) {
1280 printf("lookup_traverse_union: null parent name: .%s\n", pp);
1281 error = ENOENT;
1282 goto done;
1283 }
1284 len = strlen(name);
1285 if ((len + 1) > (pp - path)) { // Enough space for this name ?
1286 error = ENAMETOOLONG;
1287 vnode_putname(name);
1288 goto done;
1289 }
1290 for (np = name + len; len > 0; len--) // Copy name backwards
1291 *--pp = *--np;
1292 vnode_putname(name);
1293 vp = vp->v_parent;
1294 if (vp == NULLVP || vp->v_flag & VROOT)
1295 break;
1296 *--pp = '/';
1297 }
1298
1299 /* Evaluate the path in the underlying mount */
1300 NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP, UIO_SYSSPACE, CAST_USER_ADDR_T(pp), ctx);
1301 nd.ni_dvp = dvp->v_mount->mnt_vnodecovered;
1302 error = namei(&nd);
1303 if (error == 0)
1304 *new_dvp = nd.ni_vp;
1305 nameidone(&nd);
1306 done:
1307 if (path)
1308 kfree(path, MAXPATHLEN);
1309 return error;
1310 }
1311
1312 int
1313 lookup_validate_creation_path(struct nameidata *ndp)
1314 {
1315 struct componentname *cnp = &ndp->ni_cnd;
1316
1317 /*
1318 * If creating and at end of pathname, then can consider
1319 * allowing file to be created.
1320 */
1321 if (cnp->cn_flags & RDONLY) {
1322 return EROFS;
1323 }
1324 if ((cnp->cn_flags & ISLASTCN) && (ndp->ni_flag & NAMEI_TRAILINGSLASH) && !(cnp->cn_flags & WILLBEDIR)) {
1325 return ENOENT;
1326 }
1327
1328 return 0;
1329 }
1330
1331 /*
1332 * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held).
1333 */
1334 static int
1335 lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp,
1336 int vbusyflags, vfs_context_t ctx)
1337 {
1338 mount_t mp;
1339 vnode_t tdp;
1340 int error = 0;
1341 uint32_t depth = 0;
1342 vnode_t mounted_on_dp;
1343 int current_mount_generation = 0;
1344 #if CONFIG_TRIGGERS
1345 vnode_t triggered_dp = NULLVP;
1346 int retry_cnt = 0;
1347 #define MAX_TRIGGER_RETRIES 1
1348 #endif
1349
1350 if (dp->v_type != VDIR || cnp->cn_flags & NOCROSSMOUNT)
1351 return 0;
1352
1353 mounted_on_dp = dp;
1354 #if CONFIG_TRIGGERS
1355 restart:
1356 #endif
1357 current_mount_generation = mount_generation;
1358
1359 while (dp->v_mountedhere) {
1360 vnode_lock_spin(dp);
1361 if ((mp = dp->v_mountedhere)) {
1362 mp->mnt_crossref++;
1363 vnode_unlock(dp);
1364 } else {
1365 vnode_unlock(dp);
1366 break;
1367 }
1368
1369 if (ISSET(mp->mnt_lflag, MNT_LFORCE)) {
1370 mount_dropcrossref(mp, dp, 0);
1371 break; // don't traverse into a forced unmount
1372 }
1373
1374
1375 if (vfs_busy(mp, vbusyflags)) {
1376 mount_dropcrossref(mp, dp, 0);
1377 if (vbusyflags == LK_NOWAIT) {
1378 error = ENOENT;
1379 goto out;
1380 }
1381
1382 continue;
1383 }
1384
1385 error = VFS_ROOT(mp, &tdp, ctx);
1386
1387 mount_dropcrossref(mp, dp, 0);
1388 vfs_unbusy(mp);
1389
1390 if (error) {
1391 goto out;
1392 }
1393
1394 vnode_put(dp);
1395 ndp->ni_vp = dp = tdp;
1396 if (dp->v_type != VDIR) {
1397 #if DEVELOPMENT || DEBUG
1398 panic("%s : Root of filesystem not a directory\n",
1399 __FUNCTION__);
1400 #else
1401 break;
1402 #endif
1403 }
1404 depth++;
1405 }
1406
1407 #if CONFIG_TRIGGERS
1408 /*
1409 * The triggered_dp check here is required but is susceptible to a
1410 * (unlikely) race in which trigger mount is done from here and is
1411 * unmounted before we get past vfs_busy above. We retry to deal with
1412 * that case but it has the side effect of unwanted retries for
1413 * "special" processes which don't want to trigger mounts.
1414 */
1415 if (dp->v_resolve && retry_cnt < MAX_TRIGGER_RETRIES) {
1416 error = vnode_trigger_resolve(dp, ndp, ctx);
1417 if (error)
1418 goto out;
1419 if (dp == triggered_dp)
1420 retry_cnt += 1;
1421 else
1422 retry_cnt = 0;
1423 triggered_dp = dp;
1424 goto restart;
1425 }
1426 #endif /* CONFIG_TRIGGERS */
1427
1428 if (depth) {
1429 mp = mounted_on_dp->v_mountedhere;
1430
1431 if (mp) {
1432 mount_lock_spin(mp);
1433 mp->mnt_realrootvp_vid = dp->v_id;
1434 mp->mnt_realrootvp = dp;
1435 mp->mnt_generation = current_mount_generation;
1436 mount_unlock(mp);
1437 }
1438 }
1439
1440 return 0;
1441
1442 out:
1443 return error;
1444 }
1445
1446 /*
1447 * Takes ni_vp and ni_dvp non-NULL. Returns with *new_dp set to the location
1448 * at which to start a lookup with a resolved path, and all other iocounts dropped.
1449 */
1450 static int
1451 lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx)
1452 {
1453 int error;
1454 char *cp; /* pointer into pathname argument */
1455 uio_t auio;
1456 union {
1457 union {
1458 struct user_iovec s_uiovec;
1459 struct kern_iovec s_kiovec;
1460 } u_iovec;
1461 struct uio s_uio;
1462 char uio_buf[ UIO_SIZEOF(1) ];
1463 } u_uio_buf; /* union only for aligning uio_buf correctly */
1464 int need_newpathbuf;
1465 u_int linklen;
1466 struct componentname *cnp = &ndp->ni_cnd;
1467 vnode_t dp;
1468 char *tmppn;
1469
1470 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
1471 return ELOOP;
1472 }
1473 #if CONFIG_MACF
1474 if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0)
1475 return error;
1476 #endif /* MAC */
1477 if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF))
1478 need_newpathbuf = 1;
1479 else
1480 need_newpathbuf = 0;
1481
1482 if (need_newpathbuf) {
1483 MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
1484 if (cp == NULL) {
1485 return ENOMEM;
1486 }
1487 } else {
1488 cp = cnp->cn_pnbuf;
1489 }
1490 auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ,
1491 &u_uio_buf.uio_buf[0], sizeof(u_uio_buf.uio_buf));
1492
1493 uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN);
1494
1495 error = VNOP_READLINK(ndp->ni_vp, auio, ctx);
1496 if (error) {
1497 if (need_newpathbuf)
1498 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
1499 return error;
1500 }
1501
1502 /*
1503 * Safe to set unsigned with a [larger] signed type here
1504 * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN
1505 * is only 1024.
1506 */
1507 linklen = MAXPATHLEN - (u_int)uio_resid(auio);
1508 if (linklen + ndp->ni_pathlen > MAXPATHLEN) {
1509 if (need_newpathbuf)
1510 FREE_ZONE(cp, MAXPATHLEN, M_NAMEI);
1511
1512 return ENAMETOOLONG;
1513 }
1514 if (need_newpathbuf) {
1515 long len = cnp->cn_pnlen;
1516
1517 tmppn = cnp->cn_pnbuf;
1518 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
1519 cnp->cn_pnbuf = cp;
1520 cnp->cn_pnlen = MAXPATHLEN;
1521
1522 if ( (cnp->cn_flags & HASBUF) )
1523 FREE_ZONE(tmppn, len, M_NAMEI);
1524 else
1525 cnp->cn_flags |= HASBUF;
1526 } else
1527 cnp->cn_pnbuf[linklen] = '\0';
1528
1529 ndp->ni_pathlen += linklen;
1530 cnp->cn_nameptr = cnp->cn_pnbuf;
1531
1532 /*
1533 * starting point for 'relative'
1534 * symbolic link path
1535 */
1536 dp = ndp->ni_dvp;
1537
1538 /*
1539 * get rid of references returned via 'lookup'
1540 */
1541 vnode_put(ndp->ni_vp);
1542 vnode_put(ndp->ni_dvp); /* ALWAYS have a dvp for a symlink */
1543
1544 ndp->ni_vp = NULLVP;
1545 ndp->ni_dvp = NULLVP;
1546
1547 /*
1548 * Check if symbolic link restarts us at the root
1549 */
1550 if (*(cnp->cn_nameptr) == '/') {
1551 while (*(cnp->cn_nameptr) == '/') {
1552 cnp->cn_nameptr++;
1553 ndp->ni_pathlen--;
1554 }
1555 if ((dp = ndp->ni_rootdir) == NULLVP) {
1556 return ENOENT;
1557 }
1558 }
1559
1560 *new_dp = dp;
1561
1562 return 0;
1563 }
1564
1565 /*
1566 * relookup - lookup a path name component
1567 * Used by lookup to re-aquire things.
1568 */
1569 int
1570 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
1571 {
1572 struct vnode *dp = NULL; /* the directory we are searching */
1573 int wantparent; /* 1 => wantparent or lockparent flag */
1574 int rdonly; /* lookup read-only flag bit */
1575 int error = 0;
1576 #ifdef NAMEI_DIAGNOSTIC
1577 int i, newhash; /* DEBUG: check name hash */
1578 char *cp; /* DEBUG: check name ptr/len */
1579 #endif
1580 vfs_context_t ctx = cnp->cn_context;;
1581
1582 /*
1583 * Setup: break out flag bits into variables.
1584 */
1585 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
1586 rdonly = cnp->cn_flags & RDONLY;
1587 cnp->cn_flags &= ~ISSYMLINK;
1588
1589 if (cnp->cn_flags & NOCACHE)
1590 cnp->cn_flags &= ~MAKEENTRY;
1591 else
1592 cnp->cn_flags |= MAKEENTRY;
1593
1594 dp = dvp;
1595
1596 /*
1597 * Check for degenerate name (e.g. / or "")
1598 * which is a way of talking about a directory,
1599 * e.g. like "/." or ".".
1600 */
1601 if (cnp->cn_nameptr[0] == '\0') {
1602 if (cnp->cn_nameiop != LOOKUP || wantparent) {
1603 error = EISDIR;
1604 goto bad;
1605 }
1606 if (dp->v_type != VDIR) {
1607 error = ENOTDIR;
1608 goto bad;
1609 }
1610 if ( (vnode_get(dp)) ) {
1611 error = ENOENT;
1612 goto bad;
1613 }
1614 *vpp = dp;
1615
1616 if (cnp->cn_flags & SAVESTART)
1617 panic("lookup: SAVESTART");
1618 return (0);
1619 }
1620 /*
1621 * We now have a segment name to search for, and a directory to search.
1622 */
1623 if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) {
1624 if (error != EJUSTRETURN)
1625 goto bad;
1626 #if DIAGNOSTIC
1627 if (*vpp != NULL)
1628 panic("leaf should be empty");
1629 #endif
1630 /*
1631 * If creating and at end of pathname, then can consider
1632 * allowing file to be created.
1633 */
1634 if (rdonly) {
1635 error = EROFS;
1636 goto bad;
1637 }
1638 /*
1639 * We return with ni_vp NULL to indicate that the entry
1640 * doesn't currently exist, leaving a pointer to the
1641 * (possibly locked) directory inode in ndp->ni_dvp.
1642 */
1643 return (0);
1644 }
1645 dp = *vpp;
1646
1647 #if DIAGNOSTIC
1648 /*
1649 * Check for symbolic link
1650 */
1651 if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
1652 panic ("relookup: symlink found.\n");
1653 #endif
1654
1655 /*
1656 * Disallow directory write attempts on read-only file systems.
1657 */
1658 if (rdonly &&
1659 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
1660 error = EROFS;
1661 goto bad2;
1662 }
1663 /* ASSERT(dvp == ndp->ni_startdir) */
1664
1665 return (0);
1666
1667 bad2:
1668 vnode_put(dp);
1669 bad:
1670 *vpp = NULL;
1671
1672 return (error);
1673 }
1674
1675 /*
1676 * Free pathname buffer
1677 */
1678 void
1679 nameidone(struct nameidata *ndp)
1680 {
1681 if (ndp->ni_cnd.cn_flags & HASBUF) {
1682 char *tmp = ndp->ni_cnd.cn_pnbuf;
1683
1684 ndp->ni_cnd.cn_pnbuf = NULL;
1685 ndp->ni_cnd.cn_flags &= ~HASBUF;
1686 FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI);
1687 }
1688 }
1689
1690
1691 /*
1692 * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used
1693 * by fs_usage. The path up to and including the current component name are
1694 * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path
1695 * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged.
1696 * That is, the truncation removes the leading portion of the path.
1697 *
1698 * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one
1699 * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END
1700 * (in addition to DBG_FUNC_START if it is also the first). There may be
1701 * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END.
1702 *
1703 * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of
1704 * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname
1705 * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass
1706 * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT
1707 * (because not all of the 12 or 16 bytes are needed for the remainder of the
1708 * path) is set to zero bytes, or '>' if there is more path beyond the
1709 * current component name (usually because an intermediate component was not
1710 * found).
1711 *
1712 * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form
1713 * 12+N*16, there will be no padding.
1714 *
1715 * TODO: If there is more path beyond the current component name, should we
1716 * force some padding? For example, a lookup for /foo_bar_baz/spam that
1717 * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with
1718 * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>".
1719 */
1720 #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST)
1721
1722 void
1723 kdebug_lookup_gen_events(long *dbg_parms, int dbg_namelen, void *dp, boolean_t lookup)
1724 {
1725 int code;
1726 unsigned int i;
1727
1728 /*
1729 * In the event that we collect multiple, consecutive pathname
1730 * entries, we must mark the start of the path's string and the end.
1731 */
1732 if (lookup == TRUE)
1733 code = VFS_LOOKUP | DBG_FUNC_START;
1734 else
1735 code = VFS_LOOKUP_DONE | DBG_FUNC_START;
1736
1737 if (dbg_namelen <= (int)(3 * sizeof(long)))
1738 code |= DBG_FUNC_END;
1739
1740 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, kdebug_vnode(dp), dbg_parms[0], dbg_parms[1], dbg_parms[2], 0);
1741
1742 code &= ~DBG_FUNC_START;
1743
1744 for (i=3, dbg_namelen -= (3 * sizeof(long)); dbg_namelen > 0; i+=4, dbg_namelen -= (4 * sizeof(long))) {
1745 if (dbg_namelen <= (int)(4 * sizeof(long)))
1746 code |= DBG_FUNC_END;
1747
1748 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE, code, dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0);
1749 }
1750 }
1751
1752 static void
1753 kdebug_lookup(vnode_t dp, struct componentname *cnp)
1754 {
1755 int dbg_namelen;
1756 char *dbg_nameptr;
1757 long dbg_parms[NUMPARMS];
1758
1759 /* Collect the pathname for tracing */
1760 dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen;
1761 dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen;
1762
1763 if (dbg_namelen > (int)sizeof(dbg_parms))
1764 dbg_namelen = sizeof(dbg_parms);
1765 dbg_nameptr -= dbg_namelen;
1766
1767 /* Copy the (possibly truncated) path itself */
1768 memcpy(dbg_parms, dbg_nameptr, dbg_namelen);
1769
1770 /* Pad with '\0' or '>' */
1771 if (dbg_namelen < (int)sizeof(dbg_parms)) {
1772 memset((char *)dbg_parms + dbg_namelen,
1773 *(cnp->cn_nameptr + cnp->cn_namelen) ? '>' : 0,
1774 sizeof(dbg_parms) - dbg_namelen);
1775 }
1776 kdebug_lookup_gen_events(dbg_parms, dbg_namelen, (void *)dp, TRUE);
1777 }
1778
1779 #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
1780
1781 void
1782 kdebug_lookup_gen_events(long *dbg_parms __unused, int dbg_namelen __unused, void *dp __unused)
1783 {
1784 }
1785
1786 static void
1787 kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused)
1788 {
1789 }
1790 #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */
1791
1792 int
1793 vfs_getbyid(fsid_t *fsid, ino64_t ino, vnode_t *vpp, vfs_context_t ctx)
1794 {
1795 mount_t mp;
1796 int error;
1797
1798 mp = mount_lookupby_volfsid(fsid->val[0], 1);
1799 if (mp == NULL) {
1800 return EINVAL;
1801 }
1802
1803 /* Get the target vnode. */
1804 if (ino == 2) {
1805 error = VFS_ROOT(mp, vpp, ctx);
1806 } else {
1807 error = VFS_VGET(mp, ino, vpp, ctx);
1808 }
1809
1810 vfs_unbusy(mp);
1811 return error;
1812 }
1813 /*
1814 * Obtain the real path from a legacy volfs style path.
1815 *
1816 * Valid formats of input path:
1817 *
1818 * "555/@"
1819 * "555/2"
1820 * "555/123456"
1821 * "555/123456/foobar"
1822 *
1823 * Where:
1824 * 555 represents the volfs file system id
1825 * '@' and '2' are aliases to the root of a file system
1826 * 123456 represents a file id
1827 * "foobar" represents a file name
1828 */
1829 #if CONFIG_VOLFS
1830 static int
1831 vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx)
1832 {
1833 vnode_t vp;
1834 struct mount *mp = NULL;
1835 char *str;
1836 char ch;
1837 uint32_t id;
1838 ino64_t ino;
1839 int error;
1840 int length;
1841
1842 /* Get file system id and move str to next component. */
1843 id = strtoul(path, &str, 10);
1844 if (id == 0 || str[0] != '/') {
1845 return (EINVAL);
1846 }
1847 while (*str == '/') {
1848 str++;
1849 }
1850 ch = *str;
1851
1852 mp = mount_lookupby_volfsid(id, 1);
1853 if (mp == NULL) {
1854 return (EINVAL); /* unexpected failure */
1855 }
1856 /* Check for an alias to a file system root. */
1857 if (ch == '@' && str[1] == '\0') {
1858 ino = 2;
1859 str++;
1860 } else {
1861 /* Get file id and move str to next component. */
1862 ino = strtouq(str, &str, 10);
1863 }
1864
1865 /* Get the target vnode. */
1866 if (ino == 2) {
1867 error = VFS_ROOT(mp, &vp, ctx);
1868 } else {
1869 error = VFS_VGET(mp, ino, &vp, ctx);
1870 }
1871 vfs_unbusy(mp);
1872 if (error) {
1873 goto out;
1874 }
1875 realpath[0] = '\0';
1876
1877 /* Get the absolute path to this vnode. */
1878 error = build_path(vp, realpath, bufsize, &length, 0, ctx);
1879 vnode_put(vp);
1880
1881 if (error == 0 && *str != '\0') {
1882 int attempt = strlcat(realpath, str, MAXPATHLEN);
1883 if (attempt > MAXPATHLEN){
1884 error = ENAMETOOLONG;
1885 }
1886 }
1887 out:
1888 return (error);
1889 }
1890 #endif
1891
1892 void
1893 lookup_compound_vnop_post_hook(int error, vnode_t dvp, vnode_t vp, struct nameidata *ndp, int did_create)
1894 {
1895 if (error == 0 && vp == NULLVP) {
1896 panic("NULL vp with error == 0.\n");
1897 }
1898
1899 /*
1900 * We don't want to do any of this if we didn't use the compound vnop
1901 * to perform the lookup... i.e. if we're allowing and using the legacy pattern,
1902 * where we did a full lookup.
1903 */
1904 if ((ndp->ni_flag & NAMEI_COMPOUND_OP_MASK) == 0) {
1905 return;
1906 }
1907
1908 /*
1909 * If we're going to continue the lookup, we'll handle
1910 * all lookup-related updates at that time.
1911 */
1912 if (error == EKEEPLOOKING) {
1913 return;
1914 }
1915
1916 /*
1917 * Only audit or update cache for *found* vnodes. For creation
1918 * neither would happen in the non-compound-vnop case.
1919 */
1920 if ((vp != NULLVP) && !did_create) {
1921 /*
1922 * If MAKEENTRY isn't set, and we've done a successful compound VNOP,
1923 * then we certainly don't want to update cache or identity.
1924 */
1925 if ((error != 0) || (ndp->ni_cnd.cn_flags & MAKEENTRY)) {
1926 lookup_consider_update_cache(dvp, vp, &ndp->ni_cnd, ndp->ni_ncgeneration);
1927 }
1928 if (ndp->ni_cnd.cn_flags & AUDITVNPATH1)
1929 AUDIT_ARG(vnpath, vp, ARG_VNODE1);
1930 else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2)
1931 AUDIT_ARG(vnpath, vp, ARG_VNODE2);
1932 }
1933
1934 /*
1935 * If you created (whether you opened or not), cut a lookup tracepoint
1936 * for the parent dir (as would happen without a compound vnop). Note: we may need
1937 * a vnode despite failure in this case!
1938 *
1939 * If you did not create:
1940 * Found child (succeeded or not): cut a tracepoint for the child.
1941 * Did not find child: cut a tracepoint with the parent.
1942 */
1943 if (kdebug_enable) {
1944 kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd);
1945 }
1946 }