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