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