1 /*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include "opt_capsicum.h" 41 #include "opt_ktrace.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/kernel.h> 46 #include <sys/capsicum.h> 47 #include <sys/fcntl.h> 48 #include <sys/jail.h> 49 #include <sys/lock.h> 50 #include <sys/mutex.h> 51 #include <sys/namei.h> 52 #include <sys/vnode.h> 53 #include <sys/mount.h> 54 #include <sys/filedesc.h> 55 #include <sys/proc.h> 56 #include <sys/sdt.h> 57 #include <sys/syscallsubr.h> 58 #include <sys/sysctl.h> 59 #ifdef KTRACE 60 #include <sys/ktrace.h> 61 #endif 62 63 #include <security/audit/audit.h> 64 #include <security/mac/mac_framework.h> 65 66 #include <vm/uma.h> 67 68 #define NAMEI_DIAGNOSTIC 1 69 #undef NAMEI_DIAGNOSTIC 70 71 SDT_PROVIDER_DECLARE(vfs); 72 SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *", 73 "unsigned long"); 74 SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *"); 75 76 /* Allocation zone for namei. */ 77 uma_zone_t namei_zone; 78 79 /* Placeholder vnode for mp traversal. */ 80 static struct vnode *vp_crossmp; 81 82 static int 83 crossmp_vop_islocked(struct vop_islocked_args *ap) 84 { 85 86 return (LK_SHARED); 87 } 88 89 static int 90 crossmp_vop_lock1(struct vop_lock1_args *ap) 91 { 92 struct vnode *vp; 93 struct lock *lk; 94 const char *file; 95 int flags, line; 96 97 vp = ap->a_vp; 98 lk = vp->v_vnlock; 99 flags = ap->a_flags; 100 file = ap->a_file; 101 line = ap->a_line; 102 103 if ((flags & LK_SHARED) == 0) 104 panic("invalid lock request for crossmp"); 105 106 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, file, line, 107 flags & LK_INTERLOCK ? &VI_MTX(vp)->lock_object : NULL); 108 WITNESS_LOCK(&lk->lock_object, 0, file, line); 109 if ((flags & LK_INTERLOCK) != 0) 110 VI_UNLOCK(vp); 111 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, line); 112 return (0); 113 } 114 115 static int 116 crossmp_vop_unlock(struct vop_unlock_args *ap) 117 { 118 struct vnode *vp; 119 struct lock *lk; 120 int flags; 121 122 vp = ap->a_vp; 123 lk = vp->v_vnlock; 124 flags = ap->a_flags; 125 126 if ((flags & LK_INTERLOCK) != 0) 127 VI_UNLOCK(vp); 128 WITNESS_UNLOCK(&lk->lock_object, 0, LOCK_FILE, LOCK_LINE); 129 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE, 130 LOCK_LINE); 131 return (0); 132 } 133 134 static struct vop_vector crossmp_vnodeops = { 135 .vop_islocked = crossmp_vop_islocked, 136 .vop_lock1 = crossmp_vop_lock1, 137 .vop_unlock = crossmp_vop_unlock, 138 }; 139 140 struct nameicap_tracker { 141 struct vnode *dp; 142 TAILQ_ENTRY(nameicap_tracker) nm_link; 143 }; 144 145 /* Zone for cap mode tracker elements used for dotdot capability checks. */ 146 static uma_zone_t nt_zone; 147 148 static void 149 nameiinit(void *dummy __unused) 150 { 151 152 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 153 UMA_ALIGN_PTR, 0); 154 nt_zone = uma_zcreate("rentr", sizeof(struct nameicap_tracker), 155 NULL, NULL, NULL, NULL, sizeof(void *), 0); 156 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp); 157 } 158 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL); 159 160 static int lookup_shared = 1; 161 SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RWTUN, &lookup_shared, 0, 162 "enables shared locks for path name translation"); 163 164 static int lookup_cap_dotdot = 1; 165 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN, 166 &lookup_cap_dotdot, 0, 167 "enables \"..\" components in path lookup in capability mode"); 168 static int lookup_cap_dotdot_nonlocal = 1; 169 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN, 170 &lookup_cap_dotdot_nonlocal, 0, 171 "enables \"..\" components in path lookup in capability mode " 172 "on non-local mount"); 173 174 static void 175 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp) 176 { 177 struct nameicap_tracker *nt; 178 179 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR) 180 return; 181 nt = uma_zalloc(nt_zone, M_WAITOK); 182 vhold(dp); 183 nt->dp = dp; 184 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link); 185 } 186 187 static void 188 nameicap_cleanup(struct nameidata *ndp) 189 { 190 struct nameicap_tracker *nt, *nt1; 191 192 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) || 193 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative")); 194 TAILQ_FOREACH_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) { 195 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link); 196 vdrop(nt->dp); 197 uma_zfree(nt_zone, nt); 198 } 199 } 200 201 /* 202 * For dotdot lookups in capability mode, only allow the component 203 * lookup to succeed if the resulting directory was already traversed 204 * during the operation. Also fail dotdot lookups for non-local 205 * filesystems, where external agents might assist local lookups to 206 * escape the compartment. 207 */ 208 static int 209 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp) 210 { 211 struct nameicap_tracker *nt; 212 struct mount *mp; 213 214 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp == NULL || 215 dp->v_type != VDIR) 216 return (0); 217 mp = dp->v_mount; 218 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL && 219 (mp->mnt_flag & MNT_LOCAL) == 0) 220 return (ENOTCAPABLE); 221 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head, 222 nm_link) { 223 if (dp == nt->dp) 224 return (0); 225 } 226 return (ENOTCAPABLE); 227 } 228 229 static void 230 namei_cleanup_cnp(struct componentname *cnp) 231 { 232 233 uma_zfree(namei_zone, cnp->cn_pnbuf); 234 #ifdef DIAGNOSTIC 235 cnp->cn_pnbuf = NULL; 236 cnp->cn_nameptr = NULL; 237 #endif 238 } 239 240 static int 241 namei_handle_root(struct nameidata *ndp, struct vnode **dpp) 242 { 243 struct componentname *cnp; 244 245 cnp = &ndp->ni_cnd; 246 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) { 247 #ifdef KTRACE 248 if (KTRPOINT(curthread, KTR_CAPFAIL)) 249 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 250 #endif 251 return (ENOTCAPABLE); 252 } 253 while (*(cnp->cn_nameptr) == '/') { 254 cnp->cn_nameptr++; 255 ndp->ni_pathlen--; 256 } 257 *dpp = ndp->ni_rootdir; 258 vrefact(*dpp); 259 return (0); 260 } 261 262 /* 263 * Convert a pathname into a pointer to a locked vnode. 264 * 265 * The FOLLOW flag is set when symbolic links are to be followed 266 * when they occur at the end of the name translation process. 267 * Symbolic links are always followed for all other pathname 268 * components other than the last. 269 * 270 * The segflg defines whether the name is to be copied from user 271 * space or kernel space. 272 * 273 * Overall outline of namei: 274 * 275 * copy in name 276 * get starting directory 277 * while (!done && !error) { 278 * call lookup to search path. 279 * if symbolic link, massage name in buffer and continue 280 * } 281 */ 282 int 283 namei(struct nameidata *ndp) 284 { 285 struct filedesc *fdp; /* pointer to file descriptor state */ 286 char *cp; /* pointer into pathname argument */ 287 struct vnode *dp; /* the directory we are searching */ 288 struct iovec aiov; /* uio for reading symbolic links */ 289 struct componentname *cnp; 290 struct thread *td; 291 struct proc *p; 292 cap_rights_t rights; 293 struct uio auio; 294 int error, linklen, startdir_used; 295 296 cnp = &ndp->ni_cnd; 297 td = cnp->cn_thread; 298 p = td->td_proc; 299 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 300 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc")); 301 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 302 ("namei: nameiop contaminated with flags")); 303 KASSERT((cnp->cn_flags & OPMASK) == 0, 304 ("namei: flags contaminated with nameiops")); 305 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR || 306 ndp->ni_startdir->v_type == VBAD); 307 if (!lookup_shared) 308 cnp->cn_flags &= ~LOCKSHARED; 309 fdp = p->p_fd; 310 TAILQ_INIT(&ndp->ni_cap_tracker); 311 ndp->ni_lcf = 0; 312 313 /* We will set this ourselves if we need it. */ 314 cnp->cn_flags &= ~TRAILINGSLASH; 315 316 /* 317 * Get a buffer for the name to be translated, and copy the 318 * name into the buffer. 319 */ 320 if ((cnp->cn_flags & HASBUF) == 0) 321 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 322 if (ndp->ni_segflg == UIO_SYSSPACE) 323 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 324 &ndp->ni_pathlen); 325 else 326 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 327 &ndp->ni_pathlen); 328 329 /* 330 * Don't allow empty pathnames. 331 */ 332 if (error == 0 && *cnp->cn_pnbuf == '\0') 333 error = ENOENT; 334 335 #ifdef CAPABILITY_MODE 336 /* 337 * In capability mode, lookups must be restricted to happen in 338 * the subtree with the root specified by the file descriptor: 339 * - The root must be real file descriptor, not the pseudo-descriptor 340 * AT_FDCWD. 341 * - The passed path must be relative and not absolute. 342 * - If lookup_cap_dotdot is disabled, path must not contain the 343 * '..' components. 344 * - If lookup_cap_dotdot is enabled, we verify that all '..' 345 * components lookups result in the directories which were 346 * previously walked by us, which prevents an escape from 347 * the relative root. 348 */ 349 if (error == 0 && IN_CAPABILITY_MODE(td) && 350 (cnp->cn_flags & NOCAPCHECK) == 0) { 351 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE; 352 if (ndp->ni_dirfd == AT_FDCWD) { 353 #ifdef KTRACE 354 if (KTRPOINT(td, KTR_CAPFAIL)) 355 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 356 #endif 357 error = ECAPMODE; 358 } 359 } 360 #endif 361 if (error != 0) { 362 namei_cleanup_cnp(cnp); 363 ndp->ni_vp = NULL; 364 return (error); 365 } 366 ndp->ni_loopcnt = 0; 367 #ifdef KTRACE 368 if (KTRPOINT(td, KTR_NAMEI)) { 369 KASSERT(cnp->cn_thread == curthread, 370 ("namei not using curthread")); 371 ktrnamei(cnp->cn_pnbuf); 372 } 373 #endif 374 /* 375 * Get starting point for the translation. 376 */ 377 FILEDESC_SLOCK(fdp); 378 ndp->ni_rootdir = fdp->fd_rdir; 379 vrefact(ndp->ni_rootdir); 380 ndp->ni_topdir = fdp->fd_jdir; 381 382 /* 383 * If we are auditing the kernel pathname, save the user pathname. 384 */ 385 if (cnp->cn_flags & AUDITVNODE1) 386 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf); 387 if (cnp->cn_flags & AUDITVNODE2) 388 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf); 389 390 startdir_used = 0; 391 dp = NULL; 392 cnp->cn_nameptr = cnp->cn_pnbuf; 393 if (cnp->cn_pnbuf[0] == '/') { 394 error = namei_handle_root(ndp, &dp); 395 } else { 396 if (ndp->ni_startdir != NULL) { 397 dp = ndp->ni_startdir; 398 startdir_used = 1; 399 } else if (ndp->ni_dirfd == AT_FDCWD) { 400 dp = fdp->fd_cdir; 401 vrefact(dp); 402 } else { 403 rights = ndp->ni_rightsneeded; 404 cap_rights_set(&rights, CAP_LOOKUP); 405 406 if (cnp->cn_flags & AUDITVNODE1) 407 AUDIT_ARG_ATFD1(ndp->ni_dirfd); 408 if (cnp->cn_flags & AUDITVNODE2) 409 AUDIT_ARG_ATFD2(ndp->ni_dirfd); 410 error = fgetvp_rights(td, ndp->ni_dirfd, 411 &rights, &ndp->ni_filecaps, &dp); 412 if (error == EINVAL) 413 error = ENOTDIR; 414 #ifdef CAPABILITIES 415 /* 416 * If file descriptor doesn't have all rights, 417 * all lookups relative to it must also be 418 * strictly relative. 419 */ 420 CAP_ALL(&rights); 421 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights, 422 &rights) || 423 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL || 424 ndp->ni_filecaps.fc_nioctls != -1) { 425 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE; 426 } 427 #endif 428 } 429 if (error == 0 && dp->v_type != VDIR) 430 error = ENOTDIR; 431 } 432 FILEDESC_SUNLOCK(fdp); 433 if (ndp->ni_startdir != NULL && !startdir_used) 434 vrele(ndp->ni_startdir); 435 if (error != 0) { 436 if (dp != NULL) 437 vrele(dp); 438 goto out; 439 } 440 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 && 441 lookup_cap_dotdot != 0) 442 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT; 443 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf, 444 cnp->cn_flags); 445 for (;;) { 446 ndp->ni_startdir = dp; 447 error = lookup(ndp); 448 if (error != 0) 449 goto out; 450 /* 451 * If not a symbolic link, we're done. 452 */ 453 if ((cnp->cn_flags & ISSYMLINK) == 0) { 454 vrele(ndp->ni_rootdir); 455 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) { 456 namei_cleanup_cnp(cnp); 457 } else 458 cnp->cn_flags |= HASBUF; 459 nameicap_cleanup(ndp); 460 SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp); 461 return (0); 462 } 463 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 464 error = ELOOP; 465 break; 466 } 467 #ifdef MAC 468 if ((cnp->cn_flags & NOMACCHECK) == 0) { 469 error = mac_vnode_check_readlink(td->td_ucred, 470 ndp->ni_vp); 471 if (error != 0) 472 break; 473 } 474 #endif 475 if (ndp->ni_pathlen > 1) 476 cp = uma_zalloc(namei_zone, M_WAITOK); 477 else 478 cp = cnp->cn_pnbuf; 479 aiov.iov_base = cp; 480 aiov.iov_len = MAXPATHLEN; 481 auio.uio_iov = &aiov; 482 auio.uio_iovcnt = 1; 483 auio.uio_offset = 0; 484 auio.uio_rw = UIO_READ; 485 auio.uio_segflg = UIO_SYSSPACE; 486 auio.uio_td = td; 487 auio.uio_resid = MAXPATHLEN; 488 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 489 if (error != 0) { 490 if (ndp->ni_pathlen > 1) 491 uma_zfree(namei_zone, cp); 492 break; 493 } 494 linklen = MAXPATHLEN - auio.uio_resid; 495 if (linklen == 0) { 496 if (ndp->ni_pathlen > 1) 497 uma_zfree(namei_zone, cp); 498 error = ENOENT; 499 break; 500 } 501 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 502 if (ndp->ni_pathlen > 1) 503 uma_zfree(namei_zone, cp); 504 error = ENAMETOOLONG; 505 break; 506 } 507 if (ndp->ni_pathlen > 1) { 508 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 509 uma_zfree(namei_zone, cnp->cn_pnbuf); 510 cnp->cn_pnbuf = cp; 511 } else 512 cnp->cn_pnbuf[linklen] = '\0'; 513 ndp->ni_pathlen += linklen; 514 vput(ndp->ni_vp); 515 dp = ndp->ni_dvp; 516 /* 517 * Check if root directory should replace current directory. 518 */ 519 cnp->cn_nameptr = cnp->cn_pnbuf; 520 if (*(cnp->cn_nameptr) == '/') { 521 vrele(dp); 522 error = namei_handle_root(ndp, &dp); 523 if (error != 0) 524 goto out; 525 } 526 } 527 vput(ndp->ni_vp); 528 ndp->ni_vp = NULL; 529 vrele(ndp->ni_dvp); 530 out: 531 vrele(ndp->ni_rootdir); 532 namei_cleanup_cnp(cnp); 533 nameicap_cleanup(ndp); 534 SDT_PROBE2(vfs, namei, lookup, return, error, NULL); 535 return (error); 536 } 537 538 static int 539 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags) 540 { 541 542 if (mp == NULL || ((lkflags & LK_SHARED) && 543 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) || 544 ((cnflags & ISDOTDOT) && 545 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) { 546 lkflags &= ~LK_SHARED; 547 lkflags |= LK_EXCLUSIVE; 548 } 549 lkflags |= LK_NODDLKTREAT; 550 return (lkflags); 551 } 552 553 static __inline int 554 needs_exclusive_leaf(struct mount *mp, int flags) 555 { 556 557 /* 558 * Intermediate nodes can use shared locks, we only need to 559 * force an exclusive lock for leaf nodes. 560 */ 561 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 562 return (0); 563 564 /* Always use exclusive locks if LOCKSHARED isn't set. */ 565 if (!(flags & LOCKSHARED)) 566 return (1); 567 568 /* 569 * For lookups during open(), if the mount point supports 570 * extended shared operations, then use a shared lock for the 571 * leaf node, otherwise use an exclusive lock. 572 */ 573 if ((flags & ISOPEN) != 0) 574 return (!MNT_EXTENDED_SHARED(mp)); 575 576 /* 577 * Lookup requests outside of open() that specify LOCKSHARED 578 * only need a shared lock on the leaf vnode. 579 */ 580 return (0); 581 } 582 583 /* 584 * Search a pathname. 585 * This is a very central and rather complicated routine. 586 * 587 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 588 * The starting directory is taken from ni_startdir. The pathname is 589 * descended until done, or a symbolic link is encountered. The variable 590 * ni_more is clear if the path is completed; it is set to one if a 591 * symbolic link needing interpretation is encountered. 592 * 593 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 594 * whether the name is to be looked up, created, renamed, or deleted. 595 * When CREATE, RENAME, or DELETE is specified, information usable in 596 * creating, renaming, or deleting a directory entry may be calculated. 597 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 598 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 599 * returned unlocked. Otherwise the parent directory is not returned. If 600 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 601 * the target is returned locked, otherwise it is returned unlocked. 602 * When creating or renaming and LOCKPARENT is specified, the target may not 603 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 604 * 605 * Overall outline of lookup: 606 * 607 * dirloop: 608 * identify next component of name at ndp->ni_ptr 609 * handle degenerate case where name is null string 610 * if .. and crossing mount points and on mounted filesys, find parent 611 * call VOP_LOOKUP routine for next component name 612 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 613 * component vnode returned in ni_vp (if it exists), locked. 614 * if result vnode is mounted on and crossing mount points, 615 * find mounted on vnode 616 * if more components of name, do next level at dirloop 617 * return the answer in ni_vp, locked if LOCKLEAF set 618 * if LOCKPARENT set, return locked parent in ni_dvp 619 * if WANTPARENT set, return unlocked parent in ni_dvp 620 */ 621 int 622 lookup(struct nameidata *ndp) 623 { 624 char *cp; /* pointer into pathname argument */ 625 struct vnode *dp = NULL; /* the directory we are searching */ 626 struct vnode *tdp; /* saved dp */ 627 struct mount *mp; /* mount table entry */ 628 struct prison *pr; 629 int docache; /* == 0 do not cache last component */ 630 int wantparent; /* 1 => wantparent or lockparent flag */ 631 int rdonly; /* lookup read-only flag bit */ 632 int error = 0; 633 int dpunlocked = 0; /* dp has already been unlocked */ 634 int relookup = 0; /* do not consume the path component */ 635 struct componentname *cnp = &ndp->ni_cnd; 636 int lkflags_save; 637 int ni_dvp_unlocked; 638 639 /* 640 * Setup: break out flag bits into variables. 641 */ 642 ni_dvp_unlocked = 0; 643 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 644 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 645 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 646 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 647 if (cnp->cn_nameiop == DELETE || 648 (wantparent && cnp->cn_nameiop != CREATE && 649 cnp->cn_nameiop != LOOKUP)) 650 docache = 0; 651 rdonly = cnp->cn_flags & RDONLY; 652 cnp->cn_flags &= ~ISSYMLINK; 653 ndp->ni_dvp = NULL; 654 /* 655 * We use shared locks until we hit the parent of the last cn then 656 * we adjust based on the requesting flags. 657 */ 658 if (lookup_shared) 659 cnp->cn_lkflags = LK_SHARED; 660 else 661 cnp->cn_lkflags = LK_EXCLUSIVE; 662 dp = ndp->ni_startdir; 663 ndp->ni_startdir = NULLVP; 664 vn_lock(dp, 665 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY, 666 cnp->cn_flags)); 667 668 dirloop: 669 /* 670 * Search a new directory. 671 * 672 * The last component of the filename is left accessible via 673 * cnp->cn_nameptr for callers that need the name. Callers needing 674 * the name set the SAVENAME flag. When done, they assume 675 * responsibility for freeing the pathname buffer. 676 */ 677 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 678 continue; 679 cnp->cn_namelen = cp - cnp->cn_nameptr; 680 if (cnp->cn_namelen > NAME_MAX) { 681 error = ENAMETOOLONG; 682 goto bad; 683 } 684 #ifdef NAMEI_DIAGNOSTIC 685 { char c = *cp; 686 *cp = '\0'; 687 printf("{%s}: ", cnp->cn_nameptr); 688 *cp = c; } 689 #endif 690 ndp->ni_pathlen -= cnp->cn_namelen; 691 ndp->ni_next = cp; 692 693 /* 694 * Replace multiple slashes by a single slash and trailing slashes 695 * by a null. This must be done before VOP_LOOKUP() because some 696 * fs's don't know about trailing slashes. Remember if there were 697 * trailing slashes to handle symlinks, existing non-directories 698 * and non-existing files that won't be directories specially later. 699 */ 700 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 701 cp++; 702 ndp->ni_pathlen--; 703 if (*cp == '\0') { 704 *ndp->ni_next = '\0'; 705 cnp->cn_flags |= TRAILINGSLASH; 706 } 707 } 708 ndp->ni_next = cp; 709 710 cnp->cn_flags |= MAKEENTRY; 711 if (*cp == '\0' && docache == 0) 712 cnp->cn_flags &= ~MAKEENTRY; 713 if (cnp->cn_namelen == 2 && 714 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 715 cnp->cn_flags |= ISDOTDOT; 716 else 717 cnp->cn_flags &= ~ISDOTDOT; 718 if (*ndp->ni_next == 0) 719 cnp->cn_flags |= ISLASTCN; 720 else 721 cnp->cn_flags &= ~ISLASTCN; 722 723 if ((cnp->cn_flags & ISLASTCN) != 0 && 724 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 725 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 726 error = EINVAL; 727 goto bad; 728 } 729 730 nameicap_tracker_add(ndp, dp); 731 732 /* 733 * Check for degenerate name (e.g. / or "") 734 * which is a way of talking about a directory, 735 * e.g. like "/." or ".". 736 */ 737 if (cnp->cn_nameptr[0] == '\0') { 738 if (dp->v_type != VDIR) { 739 error = ENOTDIR; 740 goto bad; 741 } 742 if (cnp->cn_nameiop != LOOKUP) { 743 error = EISDIR; 744 goto bad; 745 } 746 if (wantparent) { 747 ndp->ni_dvp = dp; 748 VREF(dp); 749 } 750 ndp->ni_vp = dp; 751 752 if (cnp->cn_flags & AUDITVNODE1) 753 AUDIT_ARG_VNODE1(dp); 754 else if (cnp->cn_flags & AUDITVNODE2) 755 AUDIT_ARG_VNODE2(dp); 756 757 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 758 VOP_UNLOCK(dp, 0); 759 /* XXX This should probably move to the top of function. */ 760 if (cnp->cn_flags & SAVESTART) 761 panic("lookup: SAVESTART"); 762 goto success; 763 } 764 765 /* 766 * Handle "..": five special cases. 767 * 0. If doing a capability lookup and lookup_cap_dotdot is 768 * disabled, return ENOTCAPABLE. 769 * 1. Return an error if this is the last component of 770 * the name and the operation is DELETE or RENAME. 771 * 2. If at root directory (e.g. after chroot) 772 * or at absolute root directory 773 * then ignore it so can't get out. 774 * 3. If this vnode is the root of a mounted 775 * filesystem, then replace it with the 776 * vnode which was mounted on so we take the 777 * .. in the other filesystem. 778 * 4. If the vnode is the top directory of 779 * the jail or chroot, don't let them out. 780 * 5. If doing a capability lookup and lookup_cap_dotdot is 781 * enabled, return ENOTCAPABLE if the lookup would escape 782 * from the initial file descriptor directory. Checks are 783 * done by ensuring that namei() already traversed the 784 * result of dotdot lookup. 785 */ 786 if (cnp->cn_flags & ISDOTDOT) { 787 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT)) 788 == NI_LCF_STRICTRELATIVE) { 789 #ifdef KTRACE 790 if (KTRPOINT(curthread, KTR_CAPFAIL)) 791 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 792 #endif 793 error = ENOTCAPABLE; 794 goto bad; 795 } 796 if ((cnp->cn_flags & ISLASTCN) != 0 && 797 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 798 error = EINVAL; 799 goto bad; 800 } 801 for (;;) { 802 for (pr = cnp->cn_cred->cr_prison; pr != NULL; 803 pr = pr->pr_parent) 804 if (dp == pr->pr_root) 805 break; 806 if (dp == ndp->ni_rootdir || 807 dp == ndp->ni_topdir || 808 dp == rootvnode || 809 pr != NULL || 810 ((dp->v_vflag & VV_ROOT) != 0 && 811 (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 812 ndp->ni_dvp = dp; 813 ndp->ni_vp = dp; 814 VREF(dp); 815 goto nextname; 816 } 817 if ((dp->v_vflag & VV_ROOT) == 0) 818 break; 819 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */ 820 error = ENOENT; 821 goto bad; 822 } 823 tdp = dp; 824 dp = dp->v_mount->mnt_vnodecovered; 825 VREF(dp); 826 vput(tdp); 827 vn_lock(dp, 828 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 829 LK_RETRY, ISDOTDOT)); 830 error = nameicap_check_dotdot(ndp, dp); 831 if (error != 0) { 832 #ifdef KTRACE 833 if (KTRPOINT(curthread, KTR_CAPFAIL)) 834 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 835 #endif 836 goto bad; 837 } 838 } 839 } 840 841 /* 842 * We now have a segment name to search for, and a directory to search. 843 */ 844 unionlookup: 845 #ifdef MAC 846 if ((cnp->cn_flags & NOMACCHECK) == 0) { 847 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, 848 cnp); 849 if (error) 850 goto bad; 851 } 852 #endif 853 ndp->ni_dvp = dp; 854 ndp->ni_vp = NULL; 855 ASSERT_VOP_LOCKED(dp, "lookup"); 856 /* 857 * If we have a shared lock we may need to upgrade the lock for the 858 * last operation. 859 */ 860 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) && 861 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED) 862 vn_lock(dp, LK_UPGRADE|LK_RETRY); 863 if ((dp->v_iflag & VI_DOOMED) != 0) { 864 error = ENOENT; 865 goto bad; 866 } 867 /* 868 * If we're looking up the last component and we need an exclusive 869 * lock, adjust our lkflags. 870 */ 871 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 872 cnp->cn_lkflags = LK_EXCLUSIVE; 873 #ifdef NAMEI_DIAGNOSTIC 874 vn_printf(dp, "lookup in "); 875 #endif 876 lkflags_save = cnp->cn_lkflags; 877 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags, 878 cnp->cn_flags); 879 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp); 880 cnp->cn_lkflags = lkflags_save; 881 if (error != 0) { 882 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 883 #ifdef NAMEI_DIAGNOSTIC 884 printf("not found\n"); 885 #endif 886 if ((error == ENOENT) && 887 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 888 (dp->v_mount->mnt_flag & MNT_UNION)) { 889 tdp = dp; 890 dp = dp->v_mount->mnt_vnodecovered; 891 VREF(dp); 892 vput(tdp); 893 vn_lock(dp, 894 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 895 LK_RETRY, cnp->cn_flags)); 896 nameicap_tracker_add(ndp, dp); 897 goto unionlookup; 898 } 899 900 if (error == ERELOOKUP) { 901 vref(dp); 902 ndp->ni_vp = dp; 903 error = 0; 904 relookup = 1; 905 goto good; 906 } 907 908 if (error != EJUSTRETURN) 909 goto bad; 910 /* 911 * At this point, we know we're at the end of the 912 * pathname. If creating / renaming, we can consider 913 * allowing the file or directory to be created / renamed, 914 * provided we're not on a read-only filesystem. 915 */ 916 if (rdonly) { 917 error = EROFS; 918 goto bad; 919 } 920 /* trailing slash only allowed for directories */ 921 if ((cnp->cn_flags & TRAILINGSLASH) && 922 !(cnp->cn_flags & WILLBEDIR)) { 923 error = ENOENT; 924 goto bad; 925 } 926 if ((cnp->cn_flags & LOCKPARENT) == 0) 927 VOP_UNLOCK(dp, 0); 928 /* 929 * We return with ni_vp NULL to indicate that the entry 930 * doesn't currently exist, leaving a pointer to the 931 * (possibly locked) directory vnode in ndp->ni_dvp. 932 */ 933 if (cnp->cn_flags & SAVESTART) { 934 ndp->ni_startdir = ndp->ni_dvp; 935 VREF(ndp->ni_startdir); 936 } 937 goto success; 938 } 939 940 good: 941 #ifdef NAMEI_DIAGNOSTIC 942 printf("found\n"); 943 #endif 944 dp = ndp->ni_vp; 945 946 /* 947 * Check to see if the vnode has been mounted on; 948 * if so find the root of the mounted filesystem. 949 */ 950 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 951 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 952 if (vfs_busy(mp, 0)) 953 continue; 954 vput(dp); 955 if (dp != ndp->ni_dvp) 956 vput(ndp->ni_dvp); 957 else 958 vrele(ndp->ni_dvp); 959 vrefact(vp_crossmp); 960 ndp->ni_dvp = vp_crossmp; 961 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags, 962 cnp->cn_flags), &tdp); 963 vfs_unbusy(mp); 964 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 965 panic("vp_crossmp exclusively locked or reclaimed"); 966 if (error) { 967 dpunlocked = 1; 968 goto bad2; 969 } 970 ndp->ni_vp = dp = tdp; 971 } 972 973 /* 974 * Check for symbolic link 975 */ 976 if ((dp->v_type == VLNK) && 977 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 978 *ndp->ni_next == '/')) { 979 cnp->cn_flags |= ISSYMLINK; 980 if (dp->v_iflag & VI_DOOMED) { 981 /* 982 * We can't know whether the directory was mounted with 983 * NOSYMFOLLOW, so we can't follow safely. 984 */ 985 error = ENOENT; 986 goto bad2; 987 } 988 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 989 error = EACCES; 990 goto bad2; 991 } 992 /* 993 * Symlink code always expects an unlocked dvp. 994 */ 995 if (ndp->ni_dvp != ndp->ni_vp) { 996 VOP_UNLOCK(ndp->ni_dvp, 0); 997 ni_dvp_unlocked = 1; 998 } 999 goto success; 1000 } 1001 1002 nextname: 1003 /* 1004 * Not a symbolic link that we will follow. Continue with the 1005 * next component if there is any; otherwise, we're done. 1006 */ 1007 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 1008 ("lookup: invalid path state.")); 1009 if (relookup) { 1010 relookup = 0; 1011 if (ndp->ni_dvp != dp) 1012 vput(ndp->ni_dvp); 1013 else 1014 vrele(ndp->ni_dvp); 1015 goto dirloop; 1016 } 1017 if (cnp->cn_flags & ISDOTDOT) { 1018 error = nameicap_check_dotdot(ndp, ndp->ni_vp); 1019 if (error != 0) { 1020 #ifdef KTRACE 1021 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1022 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1023 #endif 1024 goto bad2; 1025 } 1026 } 1027 if (*ndp->ni_next == '/') { 1028 cnp->cn_nameptr = ndp->ni_next; 1029 while (*cnp->cn_nameptr == '/') { 1030 cnp->cn_nameptr++; 1031 ndp->ni_pathlen--; 1032 } 1033 if (ndp->ni_dvp != dp) 1034 vput(ndp->ni_dvp); 1035 else 1036 vrele(ndp->ni_dvp); 1037 goto dirloop; 1038 } 1039 /* 1040 * If we're processing a path with a trailing slash, 1041 * check that the end result is a directory. 1042 */ 1043 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 1044 error = ENOTDIR; 1045 goto bad2; 1046 } 1047 /* 1048 * Disallow directory write attempts on read-only filesystems. 1049 */ 1050 if (rdonly && 1051 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1052 error = EROFS; 1053 goto bad2; 1054 } 1055 if (cnp->cn_flags & SAVESTART) { 1056 ndp->ni_startdir = ndp->ni_dvp; 1057 VREF(ndp->ni_startdir); 1058 } 1059 if (!wantparent) { 1060 ni_dvp_unlocked = 2; 1061 if (ndp->ni_dvp != dp) 1062 vput(ndp->ni_dvp); 1063 else 1064 vrele(ndp->ni_dvp); 1065 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 1066 VOP_UNLOCK(ndp->ni_dvp, 0); 1067 ni_dvp_unlocked = 1; 1068 } 1069 1070 if (cnp->cn_flags & AUDITVNODE1) 1071 AUDIT_ARG_VNODE1(dp); 1072 else if (cnp->cn_flags & AUDITVNODE2) 1073 AUDIT_ARG_VNODE2(dp); 1074 1075 if ((cnp->cn_flags & LOCKLEAF) == 0) 1076 VOP_UNLOCK(dp, 0); 1077 success: 1078 /* 1079 * Because of lookup_shared we may have the vnode shared locked, but 1080 * the caller may want it to be exclusively locked. 1081 */ 1082 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 1083 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 1084 vn_lock(dp, LK_UPGRADE | LK_RETRY); 1085 if (dp->v_iflag & VI_DOOMED) { 1086 error = ENOENT; 1087 goto bad2; 1088 } 1089 } 1090 return (0); 1091 1092 bad2: 1093 if (ni_dvp_unlocked != 2) { 1094 if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 1095 vput(ndp->ni_dvp); 1096 else 1097 vrele(ndp->ni_dvp); 1098 } 1099 bad: 1100 if (!dpunlocked) 1101 vput(dp); 1102 ndp->ni_vp = NULL; 1103 return (error); 1104 } 1105 1106 /* 1107 * relookup - lookup a path name component 1108 * Used by lookup to re-acquire things. 1109 */ 1110 int 1111 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 1112 { 1113 struct vnode *dp = NULL; /* the directory we are searching */ 1114 int wantparent; /* 1 => wantparent or lockparent flag */ 1115 int rdonly; /* lookup read-only flag bit */ 1116 int error = 0; 1117 1118 KASSERT(cnp->cn_flags & ISLASTCN, 1119 ("relookup: Not given last component.")); 1120 /* 1121 * Setup: break out flag bits into variables. 1122 */ 1123 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 1124 KASSERT(wantparent, ("relookup: parent not wanted.")); 1125 rdonly = cnp->cn_flags & RDONLY; 1126 cnp->cn_flags &= ~ISSYMLINK; 1127 dp = dvp; 1128 cnp->cn_lkflags = LK_EXCLUSIVE; 1129 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 1130 1131 /* 1132 * Search a new directory. 1133 * 1134 * The last component of the filename is left accessible via 1135 * cnp->cn_nameptr for callers that need the name. Callers needing 1136 * the name set the SAVENAME flag. When done, they assume 1137 * responsibility for freeing the pathname buffer. 1138 */ 1139 #ifdef NAMEI_DIAGNOSTIC 1140 printf("{%s}: ", cnp->cn_nameptr); 1141 #endif 1142 1143 /* 1144 * Check for "" which represents the root directory after slash 1145 * removal. 1146 */ 1147 if (cnp->cn_nameptr[0] == '\0') { 1148 /* 1149 * Support only LOOKUP for "/" because lookup() 1150 * can't succeed for CREATE, DELETE and RENAME. 1151 */ 1152 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 1153 KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 1154 1155 if (!(cnp->cn_flags & LOCKLEAF)) 1156 VOP_UNLOCK(dp, 0); 1157 *vpp = dp; 1158 /* XXX This should probably move to the top of function. */ 1159 if (cnp->cn_flags & SAVESTART) 1160 panic("lookup: SAVESTART"); 1161 return (0); 1162 } 1163 1164 if (cnp->cn_flags & ISDOTDOT) 1165 panic ("relookup: lookup on dot-dot"); 1166 1167 /* 1168 * We now have a segment name to search for, and a directory to search. 1169 */ 1170 #ifdef NAMEI_DIAGNOSTIC 1171 vn_printf(dp, "search in "); 1172 #endif 1173 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 1174 KASSERT(*vpp == NULL, ("leaf should be empty")); 1175 if (error != EJUSTRETURN) 1176 goto bad; 1177 /* 1178 * If creating and at end of pathname, then can consider 1179 * allowing file to be created. 1180 */ 1181 if (rdonly) { 1182 error = EROFS; 1183 goto bad; 1184 } 1185 /* ASSERT(dvp == ndp->ni_startdir) */ 1186 if (cnp->cn_flags & SAVESTART) 1187 VREF(dvp); 1188 if ((cnp->cn_flags & LOCKPARENT) == 0) 1189 VOP_UNLOCK(dp, 0); 1190 /* 1191 * We return with ni_vp NULL to indicate that the entry 1192 * doesn't currently exist, leaving a pointer to the 1193 * (possibly locked) directory vnode in ndp->ni_dvp. 1194 */ 1195 return (0); 1196 } 1197 1198 dp = *vpp; 1199 1200 /* 1201 * Disallow directory write attempts on read-only filesystems. 1202 */ 1203 if (rdonly && 1204 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1205 if (dvp == dp) 1206 vrele(dvp); 1207 else 1208 vput(dvp); 1209 error = EROFS; 1210 goto bad; 1211 } 1212 /* 1213 * Set the parent lock/ref state to the requested state. 1214 */ 1215 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 1216 if (wantparent) 1217 VOP_UNLOCK(dvp, 0); 1218 else 1219 vput(dvp); 1220 } else if (!wantparent) 1221 vrele(dvp); 1222 /* 1223 * Check for symbolic link 1224 */ 1225 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1226 ("relookup: symlink found.\n")); 1227 1228 /* ASSERT(dvp == ndp->ni_startdir) */ 1229 if (cnp->cn_flags & SAVESTART) 1230 VREF(dvp); 1231 1232 if ((cnp->cn_flags & LOCKLEAF) == 0) 1233 VOP_UNLOCK(dp, 0); 1234 return (0); 1235 bad: 1236 vput(dp); 1237 *vpp = NULL; 1238 return (error); 1239 } 1240 1241 void 1242 NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg, 1243 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp, 1244 struct thread *td) 1245 { 1246 1247 ndp->ni_cnd.cn_nameiop = op; 1248 ndp->ni_cnd.cn_flags = flags; 1249 ndp->ni_segflg = segflg; 1250 ndp->ni_dirp = namep; 1251 ndp->ni_dirfd = dirfd; 1252 ndp->ni_startdir = startdir; 1253 if (rightsp != NULL) 1254 ndp->ni_rightsneeded = *rightsp; 1255 else 1256 cap_rights_init(&ndp->ni_rightsneeded); 1257 filecaps_init(&ndp->ni_filecaps); 1258 ndp->ni_cnd.cn_thread = td; 1259 } 1260 1261 /* 1262 * Free data allocated by namei(); see namei(9) for details. 1263 */ 1264 void 1265 NDFREE(struct nameidata *ndp, const u_int flags) 1266 { 1267 int unlock_dvp; 1268 int unlock_vp; 1269 1270 unlock_dvp = 0; 1271 unlock_vp = 0; 1272 1273 if (!(flags & NDF_NO_FREE_PNBUF) && 1274 (ndp->ni_cnd.cn_flags & HASBUF)) { 1275 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 1276 ndp->ni_cnd.cn_flags &= ~HASBUF; 1277 } 1278 if (!(flags & NDF_NO_VP_UNLOCK) && 1279 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 1280 unlock_vp = 1; 1281 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 1282 if (unlock_vp) { 1283 vput(ndp->ni_vp); 1284 unlock_vp = 0; 1285 } else 1286 vrele(ndp->ni_vp); 1287 ndp->ni_vp = NULL; 1288 } 1289 if (unlock_vp) 1290 VOP_UNLOCK(ndp->ni_vp, 0); 1291 if (!(flags & NDF_NO_DVP_UNLOCK) && 1292 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 1293 ndp->ni_dvp != ndp->ni_vp) 1294 unlock_dvp = 1; 1295 if (!(flags & NDF_NO_DVP_RELE) && 1296 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 1297 if (unlock_dvp) { 1298 vput(ndp->ni_dvp); 1299 unlock_dvp = 0; 1300 } else 1301 vrele(ndp->ni_dvp); 1302 ndp->ni_dvp = NULL; 1303 } 1304 if (unlock_dvp) 1305 VOP_UNLOCK(ndp->ni_dvp, 0); 1306 if (!(flags & NDF_NO_STARTDIR_RELE) && 1307 (ndp->ni_cnd.cn_flags & SAVESTART)) { 1308 vrele(ndp->ni_startdir); 1309 ndp->ni_startdir = NULL; 1310 } 1311 } 1312 1313 /* 1314 * Determine if there is a suitable alternate filename under the specified 1315 * prefix for the specified path. If the create flag is set, then the 1316 * alternate prefix will be used so long as the parent directory exists. 1317 * This is used by the various compatibility ABIs so that Linux binaries prefer 1318 * files under /compat/linux for example. The chosen path (whether under 1319 * the prefix or under /) is returned in a kernel malloc'd buffer pointed 1320 * to by pathbuf. The caller is responsible for free'ing the buffer from 1321 * the M_TEMP bucket if one is returned. 1322 */ 1323 int 1324 kern_alternate_path(struct thread *td, const char *prefix, const char *path, 1325 enum uio_seg pathseg, char **pathbuf, int create, int dirfd) 1326 { 1327 struct nameidata nd, ndroot; 1328 char *ptr, *buf, *cp; 1329 size_t len, sz; 1330 int error; 1331 1332 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1333 *pathbuf = buf; 1334 1335 /* Copy the prefix into the new pathname as a starting point. */ 1336 len = strlcpy(buf, prefix, MAXPATHLEN); 1337 if (len >= MAXPATHLEN) { 1338 *pathbuf = NULL; 1339 free(buf, M_TEMP); 1340 return (EINVAL); 1341 } 1342 sz = MAXPATHLEN - len; 1343 ptr = buf + len; 1344 1345 /* Append the filename to the prefix. */ 1346 if (pathseg == UIO_SYSSPACE) 1347 error = copystr(path, ptr, sz, &len); 1348 else 1349 error = copyinstr(path, ptr, sz, &len); 1350 1351 if (error) { 1352 *pathbuf = NULL; 1353 free(buf, M_TEMP); 1354 return (error); 1355 } 1356 1357 /* Only use a prefix with absolute pathnames. */ 1358 if (*ptr != '/') { 1359 error = EINVAL; 1360 goto keeporig; 1361 } 1362 1363 if (dirfd != AT_FDCWD) { 1364 /* 1365 * We want the original because the "prefix" is 1366 * included in the already opened dirfd. 1367 */ 1368 bcopy(ptr, buf, len); 1369 return (0); 1370 } 1371 1372 /* 1373 * We know that there is a / somewhere in this pathname. 1374 * Search backwards for it, to find the file's parent dir 1375 * to see if it exists in the alternate tree. If it does, 1376 * and we want to create a file (cflag is set). We don't 1377 * need to worry about the root comparison in this case. 1378 */ 1379 1380 if (create) { 1381 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1382 *cp = '\0'; 1383 1384 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1385 error = namei(&nd); 1386 *cp = '/'; 1387 if (error != 0) 1388 goto keeporig; 1389 } else { 1390 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1391 1392 error = namei(&nd); 1393 if (error != 0) 1394 goto keeporig; 1395 1396 /* 1397 * We now compare the vnode of the prefix to the one 1398 * vnode asked. If they resolve to be the same, then we 1399 * ignore the match so that the real root gets used. 1400 * This avoids the problem of traversing "../.." to find the 1401 * root directory and never finding it, because "/" resolves 1402 * to the emulation root directory. This is expensive :-( 1403 */ 1404 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, 1405 td); 1406 1407 /* We shouldn't ever get an error from this namei(). */ 1408 error = namei(&ndroot); 1409 if (error == 0) { 1410 if (nd.ni_vp == ndroot.ni_vp) 1411 error = ENOENT; 1412 1413 NDFREE(&ndroot, NDF_ONLY_PNBUF); 1414 vrele(ndroot.ni_vp); 1415 } 1416 } 1417 1418 NDFREE(&nd, NDF_ONLY_PNBUF); 1419 vrele(nd.ni_vp); 1420 1421 keeporig: 1422 /* If there was an error, use the original path name. */ 1423 if (error) 1424 bcopy(ptr, buf, len); 1425 return (error); 1426 } 1427