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 #include "opt_capsicum.h" 41 #include "opt_ktrace.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/dirent.h> 46 #include <sys/kernel.h> 47 #include <sys/capsicum.h> 48 #include <sys/fcntl.h> 49 #include <sys/jail.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/namei.h> 53 #include <sys/vnode.h> 54 #include <sys/mount.h> 55 #include <sys/filedesc.h> 56 #include <sys/proc.h> 57 #include <sys/sdt.h> 58 #include <sys/syscallsubr.h> 59 #include <sys/sysctl.h> 60 #ifdef KTRACE 61 #include <sys/ktrace.h> 62 #endif 63 #ifdef INVARIANTS 64 #include <machine/_inttypes.h> 65 #endif 66 67 #include <security/audit/audit.h> 68 #include <security/mac/mac_framework.h> 69 70 #include <vm/uma.h> 71 72 #ifdef INVARIANTS 73 static void NDVALIDATE_impl(struct nameidata *, int); 74 #define NDVALIDATE(ndp) NDVALIDATE_impl(ndp, __LINE__) 75 #else 76 #define NDVALIDATE(ndp) 77 #endif 78 79 /* 80 * Prepare namei() to restart. Reset components to its original state and set 81 * ISRESTARTED flag which signals the underlying lookup code to change the root 82 * from ABI root to actual root and prevents a further restarts. 83 */ 84 #define NDRESTART(ndp) do { \ 85 NDREINIT_DBG(ndp); \ 86 ndp->ni_resflags = 0; \ 87 ndp->ni_cnd.cn_flags &= ~NAMEI_INTERNAL_FLAGS; \ 88 ndp->ni_cnd.cn_flags |= ISRESTARTED; \ 89 } while (0) 90 91 SDT_PROVIDER_DEFINE(vfs); 92 SDT_PROBE_DEFINE4(vfs, namei, lookup, entry, "struct vnode *", "char *", 93 "unsigned long", "bool"); 94 SDT_PROBE_DEFINE4(vfs, namei, lookup, return, "int", "struct vnode *", "bool", 95 "struct nameidata"); 96 97 /* Allocation zone for namei. */ 98 uma_zone_t namei_zone; 99 100 /* Placeholder vnode for mp traversal. */ 101 static struct vnode *vp_crossmp; 102 103 static int 104 crossmp_vop_islocked(struct vop_islocked_args *ap) 105 { 106 107 return (LK_SHARED); 108 } 109 110 static int 111 crossmp_vop_lock1(struct vop_lock1_args *ap) 112 { 113 struct vnode *vp; 114 struct lock *lk __diagused; 115 int flags; 116 117 vp = ap->a_vp; 118 lk = vp->v_vnlock; 119 flags = ap->a_flags; 120 121 KASSERT((flags & (LK_SHARED | LK_NOWAIT)) == (LK_SHARED | LK_NOWAIT), 122 ("%s: invalid lock request 0x%x for crossmp", __func__, flags)); 123 124 if ((flags & LK_INTERLOCK) != 0) 125 VI_UNLOCK(vp); 126 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, ap->a_file, ap->a_line); 127 return (0); 128 } 129 130 static int 131 crossmp_vop_unlock(struct vop_unlock_args *ap) 132 { 133 struct vnode *vp; 134 struct lock *lk __diagused; 135 136 vp = ap->a_vp; 137 lk = vp->v_vnlock; 138 139 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, LOCK_FILE, 140 LOCK_LINE); 141 return (0); 142 } 143 144 static struct vop_vector crossmp_vnodeops = { 145 .vop_default = &default_vnodeops, 146 .vop_islocked = crossmp_vop_islocked, 147 .vop_lock1 = crossmp_vop_lock1, 148 .vop_unlock = crossmp_vop_unlock, 149 }; 150 /* 151 * VFS_VOP_VECTOR_REGISTER(crossmp_vnodeops) is not used here since the vnode 152 * gets allocated early. See nameiinit for the direct call below. 153 */ 154 155 struct nameicap_tracker { 156 struct vnode *dp; 157 TAILQ_ENTRY(nameicap_tracker) nm_link; 158 }; 159 160 /* Zone for cap mode tracker elements used for dotdot capability checks. */ 161 MALLOC_DEFINE(M_NAMEITRACKER, "namei_tracker", "namei tracking for dotdot"); 162 163 static void 164 nameiinit(void *dummy __unused) 165 { 166 167 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 168 UMA_ALIGN_PTR, 0); 169 vfs_vector_op_register(&crossmp_vnodeops); 170 getnewvnode("crossmp", NULL, &crossmp_vnodeops, &vp_crossmp); 171 vp_crossmp->v_state = VSTATE_CONSTRUCTED; 172 vp_crossmp->v_irflag |= VIRF_CROSSMP; 173 } 174 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL); 175 176 static int lookup_cap_dotdot = 1; 177 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot, CTLFLAG_RWTUN, 178 &lookup_cap_dotdot, 0, 179 "enables \"..\" components in path lookup in capability mode"); 180 static int lookup_cap_dotdot_nonlocal = 1; 181 SYSCTL_INT(_vfs, OID_AUTO, lookup_cap_dotdot_nonlocal, CTLFLAG_RWTUN, 182 &lookup_cap_dotdot_nonlocal, 0, 183 "enables \"..\" components in path lookup in capability mode " 184 "on non-local mount"); 185 186 static void 187 nameicap_tracker_add(struct nameidata *ndp, struct vnode *dp) 188 { 189 struct nameicap_tracker *nt; 190 191 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0 || dp->v_type != VDIR) 192 return; 193 nt = TAILQ_LAST(&ndp->ni_cap_tracker, nameicap_tracker_head); 194 if (nt != NULL && nt->dp == dp) 195 return; 196 nt = malloc(sizeof(*nt), M_NAMEITRACKER, M_WAITOK); 197 vhold(dp); 198 nt->dp = dp; 199 TAILQ_INSERT_TAIL(&ndp->ni_cap_tracker, nt, nm_link); 200 } 201 202 static void 203 nameicap_cleanup_from(struct nameidata *ndp, struct nameicap_tracker *first) 204 { 205 struct nameicap_tracker *nt, *nt1; 206 207 nt = first; 208 TAILQ_FOREACH_FROM_SAFE(nt, &ndp->ni_cap_tracker, nm_link, nt1) { 209 TAILQ_REMOVE(&ndp->ni_cap_tracker, nt, nm_link); 210 vdrop(nt->dp); 211 free(nt, M_NAMEITRACKER); 212 } 213 } 214 215 static void 216 nameicap_cleanup(struct nameidata *ndp) 217 { 218 KASSERT(TAILQ_EMPTY(&ndp->ni_cap_tracker) || 219 (ndp->ni_lcf & NI_LCF_CAP_DOTDOT) != 0, ("not strictrelative")); 220 nameicap_cleanup_from(ndp, NULL); 221 } 222 223 /* 224 * For dotdot lookups in capability mode, only allow the component 225 * lookup to succeed if the resulting directory was already traversed 226 * during the operation. This catches situations where already 227 * traversed directory is moved to different parent, and then we walk 228 * over it with dotdots. 229 * 230 * Also allow to force failure of dotdot lookups for non-local 231 * filesystems, where external agents might assist local lookups to 232 * escape the compartment. 233 */ 234 static int 235 nameicap_check_dotdot(struct nameidata *ndp, struct vnode *dp) 236 { 237 struct nameicap_tracker *nt; 238 struct mount *mp; 239 240 if (dp == NULL || dp->v_type != VDIR || (ndp->ni_lcf & 241 NI_LCF_STRICTRELATIVE) == 0) 242 return (0); 243 if ((ndp->ni_lcf & NI_LCF_CAP_DOTDOT) == 0) 244 return (ENOTCAPABLE); 245 mp = dp->v_mount; 246 if (lookup_cap_dotdot_nonlocal == 0 && mp != NULL && 247 (mp->mnt_flag & MNT_LOCAL) == 0) 248 return (ENOTCAPABLE); 249 TAILQ_FOREACH_REVERSE(nt, &ndp->ni_cap_tracker, nameicap_tracker_head, 250 nm_link) { 251 if (dp == nt->dp) { 252 nt = TAILQ_NEXT(nt, nm_link); 253 if (nt != NULL) 254 nameicap_cleanup_from(ndp, nt); 255 return (0); 256 } 257 } 258 return (ENOTCAPABLE); 259 } 260 261 static void 262 namei_cleanup_cnp(struct componentname *cnp) 263 { 264 265 uma_zfree(namei_zone, cnp->cn_pnbuf); 266 cnp->cn_pnbuf = NULL; 267 cnp->cn_nameptr = NULL; 268 } 269 270 static int 271 namei_handle_root(struct nameidata *ndp, struct vnode **dpp) 272 { 273 struct componentname *cnp; 274 275 cnp = &ndp->ni_cnd; 276 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0) { 277 #ifdef KTRACE 278 if (KTRPOINT(curthread, KTR_CAPFAIL)) 279 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 280 #endif 281 return (ENOTCAPABLE); 282 } 283 while (*(cnp->cn_nameptr) == '/') { 284 cnp->cn_nameptr++; 285 ndp->ni_pathlen--; 286 } 287 *dpp = ndp->ni_rootdir; 288 vrefact(*dpp); 289 return (0); 290 } 291 292 static int 293 namei_setup(struct nameidata *ndp, struct vnode **dpp, struct pwd **pwdp) 294 { 295 struct componentname *cnp; 296 struct thread *td; 297 struct pwd *pwd; 298 int error; 299 bool startdir_used; 300 301 cnp = &ndp->ni_cnd; 302 td = curthread; 303 304 startdir_used = false; 305 *pwdp = NULL; 306 *dpp = NULL; 307 308 #ifdef CAPABILITY_MODE 309 /* 310 * In capability mode, lookups must be restricted to happen in 311 * the subtree with the root specified by the file descriptor: 312 * - The root must be real file descriptor, not the pseudo-descriptor 313 * AT_FDCWD. 314 * - The passed path must be relative and not absolute. 315 * - If lookup_cap_dotdot is disabled, path must not contain the 316 * '..' components. 317 * - If lookup_cap_dotdot is enabled, we verify that all '..' 318 * components lookups result in the directories which were 319 * previously walked by us, which prevents an escape from 320 * the relative root. 321 */ 322 if (IN_CAPABILITY_MODE(td) && (cnp->cn_flags & NOCAPCHECK) == 0) { 323 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE; 324 ndp->ni_resflags |= NIRES_STRICTREL; 325 if (ndp->ni_dirfd == AT_FDCWD) { 326 #ifdef KTRACE 327 if (KTRPOINT(td, KTR_CAPFAIL)) 328 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 329 #endif 330 return (ECAPMODE); 331 } 332 } 333 #endif 334 error = 0; 335 336 /* 337 * Get starting point for the translation. 338 */ 339 pwd = pwd_hold(td); 340 /* 341 * The reference on ni_rootdir is acquired in the block below to avoid 342 * back-to-back atomics for absolute lookups. 343 */ 344 namei_setup_rootdir(ndp, cnp, pwd); 345 ndp->ni_topdir = pwd->pwd_jdir; 346 347 if (cnp->cn_pnbuf[0] == '/') { 348 ndp->ni_resflags |= NIRES_ABS; 349 error = namei_handle_root(ndp, dpp); 350 } else { 351 if (ndp->ni_startdir != NULL) { 352 *dpp = ndp->ni_startdir; 353 startdir_used = true; 354 } else if (ndp->ni_dirfd == AT_FDCWD) { 355 *dpp = pwd->pwd_cdir; 356 vrefact(*dpp); 357 } else { 358 if (cnp->cn_flags & AUDITVNODE1) 359 AUDIT_ARG_ATFD1(ndp->ni_dirfd); 360 if (cnp->cn_flags & AUDITVNODE2) 361 AUDIT_ARG_ATFD2(ndp->ni_dirfd); 362 363 error = fgetvp_lookup(ndp->ni_dirfd, ndp, dpp); 364 } 365 if (error == 0 && (*dpp)->v_type != VDIR && 366 (cnp->cn_pnbuf[0] != '\0' || 367 (cnp->cn_flags & EMPTYPATH) == 0)) 368 error = ENOTDIR; 369 } 370 if (error == 0 && (cnp->cn_flags & RBENEATH) != 0) { 371 if (cnp->cn_pnbuf[0] == '/') { 372 error = ENOTCAPABLE; 373 } else if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) == 0) { 374 ndp->ni_lcf |= NI_LCF_STRICTRELATIVE | 375 NI_LCF_CAP_DOTDOT; 376 } 377 } 378 379 /* 380 * If we are auditing the kernel pathname, save the user pathname. 381 */ 382 if (AUDITING_TD(td)) { 383 if (cnp->cn_flags & AUDITVNODE1) 384 AUDIT_ARG_UPATH1_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf); 385 if (cnp->cn_flags & AUDITVNODE2) 386 AUDIT_ARG_UPATH2_VP(td, ndp->ni_rootdir, *dpp, cnp->cn_pnbuf); 387 } 388 if (ndp->ni_startdir != NULL && !startdir_used) 389 vrele(ndp->ni_startdir); 390 if (error != 0) { 391 if (*dpp != NULL) 392 vrele(*dpp); 393 pwd_drop(pwd); 394 return (error); 395 } 396 if ((ndp->ni_lcf & NI_LCF_STRICTRELATIVE) != 0 && 397 lookup_cap_dotdot != 0) 398 ndp->ni_lcf |= NI_LCF_CAP_DOTDOT; 399 SDT_PROBE4(vfs, namei, lookup, entry, *dpp, cnp->cn_pnbuf, 400 cnp->cn_flags, false); 401 *pwdp = pwd; 402 return (0); 403 } 404 405 static int 406 namei_getpath(struct nameidata *ndp) 407 { 408 struct componentname *cnp; 409 int error; 410 411 cnp = &ndp->ni_cnd; 412 413 /* 414 * Get a buffer for the name to be translated, and copy the 415 * name into the buffer. 416 */ 417 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 418 if (ndp->ni_segflg == UIO_SYSSPACE) { 419 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 420 &ndp->ni_pathlen); 421 } else { 422 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 423 &ndp->ni_pathlen); 424 } 425 426 return (error); 427 } 428 429 static int 430 namei_emptypath(struct nameidata *ndp) 431 { 432 struct componentname *cnp; 433 struct pwd *pwd; 434 struct vnode *dp; 435 int error; 436 437 cnp = &ndp->ni_cnd; 438 MPASS(*cnp->cn_pnbuf == '\0'); 439 MPASS((cnp->cn_flags & EMPTYPATH) != 0); 440 MPASS((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) == 0); 441 442 ndp->ni_resflags |= NIRES_EMPTYPATH; 443 error = namei_setup(ndp, &dp, &pwd); 444 if (error != 0) { 445 goto errout; 446 } 447 448 /* 449 * Usecount on dp already provided by namei_setup. 450 */ 451 ndp->ni_vp = dp; 452 pwd_drop(pwd); 453 NDVALIDATE(ndp); 454 if ((cnp->cn_flags & LOCKLEAF) != 0) { 455 VOP_LOCK(dp, (cnp->cn_flags & LOCKSHARED) != 0 ? 456 LK_SHARED : LK_EXCLUSIVE); 457 if (VN_IS_DOOMED(dp)) { 458 vput(dp); 459 error = ENOENT; 460 goto errout; 461 } 462 } 463 SDT_PROBE4(vfs, namei, lookup, return, 0, ndp->ni_vp, false, ndp); 464 return (0); 465 466 errout: 467 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp); 468 namei_cleanup_cnp(cnp); 469 return (error); 470 } 471 472 static int __noinline 473 namei_follow_link(struct nameidata *ndp) 474 { 475 char *cp; 476 struct iovec aiov; 477 struct uio auio; 478 struct componentname *cnp; 479 struct thread *td; 480 int error, linklen; 481 482 error = 0; 483 cnp = &ndp->ni_cnd; 484 td = curthread; 485 486 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 487 error = ELOOP; 488 goto out; 489 } 490 #ifdef MAC 491 if ((cnp->cn_flags & NOMACCHECK) == 0) { 492 error = mac_vnode_check_readlink(td->td_ucred, ndp->ni_vp); 493 if (error != 0) 494 goto out; 495 } 496 #endif 497 if (ndp->ni_pathlen > 1) 498 cp = uma_zalloc(namei_zone, M_WAITOK); 499 else 500 cp = cnp->cn_pnbuf; 501 aiov.iov_base = cp; 502 aiov.iov_len = MAXPATHLEN; 503 auio.uio_iov = &aiov; 504 auio.uio_iovcnt = 1; 505 auio.uio_offset = 0; 506 auio.uio_rw = UIO_READ; 507 auio.uio_segflg = UIO_SYSSPACE; 508 auio.uio_td = td; 509 auio.uio_resid = MAXPATHLEN; 510 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 511 if (error != 0) { 512 if (ndp->ni_pathlen > 1) 513 uma_zfree(namei_zone, cp); 514 goto out; 515 } 516 linklen = MAXPATHLEN - auio.uio_resid; 517 if (linklen == 0) { 518 if (ndp->ni_pathlen > 1) 519 uma_zfree(namei_zone, cp); 520 error = ENOENT; 521 goto out; 522 } 523 if (linklen + ndp->ni_pathlen > MAXPATHLEN) { 524 if (ndp->ni_pathlen > 1) 525 uma_zfree(namei_zone, cp); 526 error = ENAMETOOLONG; 527 goto out; 528 } 529 if (ndp->ni_pathlen > 1) { 530 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 531 uma_zfree(namei_zone, cnp->cn_pnbuf); 532 cnp->cn_pnbuf = cp; 533 } else 534 cnp->cn_pnbuf[linklen] = '\0'; 535 ndp->ni_pathlen += linklen; 536 out: 537 return (error); 538 } 539 540 /* 541 * Convert a pathname into a pointer to a locked vnode. 542 * 543 * The FOLLOW flag is set when symbolic links are to be followed 544 * when they occur at the end of the name translation process. 545 * Symbolic links are always followed for all other pathname 546 * components other than the last. 547 * 548 * The segflg defines whether the name is to be copied from user 549 * space or kernel space. 550 * 551 * Overall outline of namei: 552 * 553 * copy in name 554 * get starting directory 555 * while (!done && !error) { 556 * call lookup to search path. 557 * if symbolic link, massage name in buffer and continue 558 * } 559 */ 560 int 561 namei(struct nameidata *ndp) 562 { 563 struct vnode *dp; /* the directory we are searching */ 564 struct componentname *cnp; 565 struct thread *td; 566 struct pwd *pwd; 567 int error; 568 enum cache_fpl_status status; 569 570 cnp = &ndp->ni_cnd; 571 td = curthread; 572 #ifdef INVARIANTS 573 KASSERT((ndp->ni_debugflags & NAMEI_DBG_CALLED) == 0, 574 ("%s: repeated call to namei without NDREINIT", __func__)); 575 KASSERT(ndp->ni_debugflags == NAMEI_DBG_INITED, 576 ("%s: bad debugflags %d", __func__, ndp->ni_debugflags)); 577 ndp->ni_debugflags |= NAMEI_DBG_CALLED; 578 if (ndp->ni_startdir != NULL) 579 ndp->ni_debugflags |= NAMEI_DBG_HADSTARTDIR; 580 if (cnp->cn_flags & FAILIFEXISTS) { 581 KASSERT(cnp->cn_nameiop == CREATE, 582 ("%s: FAILIFEXISTS passed for op %d", __func__, cnp->cn_nameiop)); 583 /* 584 * The limitation below is to restrict hairy corner cases. 585 */ 586 KASSERT((cnp->cn_flags & (LOCKPARENT | LOCKLEAF)) == LOCKPARENT, 587 ("%s: FAILIFEXISTS must be passed with LOCKPARENT and without LOCKLEAF", 588 __func__)); 589 } 590 #endif 591 ndp->ni_cnd.cn_cred = td->td_ucred; 592 KASSERT(ndp->ni_resflags == 0, ("%s: garbage in ni_resflags: %x\n", 593 __func__, ndp->ni_resflags)); 594 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc")); 595 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0, 596 ("namei: unexpected flags: %" PRIx64 "\n", 597 cnp->cn_flags & NAMEI_INTERNAL_FLAGS)); 598 if (cnp->cn_flags & NOCACHE) 599 KASSERT(cnp->cn_nameiop != LOOKUP, 600 ("%s: NOCACHE passed with LOOKUP", __func__)); 601 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR || 602 ndp->ni_startdir->v_type == VBAD); 603 604 restart: 605 ndp->ni_lcf = 0; 606 ndp->ni_loopcnt = 0; 607 ndp->ni_vp = NULL; 608 609 error = namei_getpath(ndp); 610 if (__predict_false(error != 0)) { 611 namei_cleanup_cnp(cnp); 612 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, 613 false, ndp); 614 return (error); 615 } 616 617 cnp->cn_nameptr = cnp->cn_pnbuf; 618 619 #ifdef KTRACE 620 if (KTRPOINT(td, KTR_NAMEI)) { 621 ktrnamei(cnp->cn_pnbuf); 622 } 623 #endif 624 TSNAMEI(curthread->td_proc->p_pid, cnp->cn_pnbuf); 625 626 /* 627 * First try looking up the target without locking any vnodes. 628 * 629 * We may need to start from scratch or pick up where it left off. 630 */ 631 error = cache_fplookup(ndp, &status, &pwd); 632 switch (status) { 633 case CACHE_FPL_STATUS_UNSET: 634 __assert_unreachable(); 635 break; 636 case CACHE_FPL_STATUS_HANDLED: 637 if (error == 0) 638 NDVALIDATE(ndp); 639 else if (__predict_false(pwd->pwd_adir != pwd->pwd_rdir && 640 (cnp->cn_flags & ISRESTARTED) == 0)) { 641 namei_cleanup_cnp(cnp); 642 NDRESTART(ndp); 643 goto restart; 644 } 645 return (error); 646 case CACHE_FPL_STATUS_PARTIAL: 647 TAILQ_INIT(&ndp->ni_cap_tracker); 648 dp = ndp->ni_startdir; 649 break; 650 case CACHE_FPL_STATUS_DESTROYED: 651 ndp->ni_loopcnt = 0; 652 error = namei_getpath(ndp); 653 if (__predict_false(error != 0)) { 654 namei_cleanup_cnp(cnp); 655 return (error); 656 } 657 cnp->cn_nameptr = cnp->cn_pnbuf; 658 /* FALLTHROUGH */ 659 case CACHE_FPL_STATUS_ABORTED: 660 TAILQ_INIT(&ndp->ni_cap_tracker); 661 MPASS(ndp->ni_lcf == 0); 662 if (*cnp->cn_pnbuf == '\0') { 663 if ((cnp->cn_flags & EMPTYPATH) != 0) { 664 return (namei_emptypath(ndp)); 665 } 666 namei_cleanup_cnp(cnp); 667 SDT_PROBE4(vfs, namei, lookup, return, ENOENT, NULL, 668 false, ndp); 669 return (ENOENT); 670 } 671 error = namei_setup(ndp, &dp, &pwd); 672 if (error != 0) { 673 namei_cleanup_cnp(cnp); 674 return (error); 675 } 676 break; 677 } 678 679 /* 680 * Locked lookup. 681 */ 682 for (;;) { 683 ndp->ni_startdir = dp; 684 error = vfs_lookup(ndp); 685 if (error != 0) { 686 if (__predict_false(pwd->pwd_adir != pwd->pwd_rdir && 687 error == ENOENT && 688 (cnp->cn_flags & ISRESTARTED) == 0)) { 689 nameicap_cleanup(ndp); 690 pwd_drop(pwd); 691 namei_cleanup_cnp(cnp); 692 NDRESTART(ndp); 693 goto restart; 694 } else 695 goto out; 696 } 697 698 /* 699 * If not a symbolic link, we're done. 700 */ 701 if ((cnp->cn_flags & ISSYMLINK) == 0) { 702 SDT_PROBE4(vfs, namei, lookup, return, error, 703 ndp->ni_vp, false, ndp); 704 nameicap_cleanup(ndp); 705 pwd_drop(pwd); 706 NDVALIDATE(ndp); 707 return (0); 708 } 709 error = namei_follow_link(ndp); 710 if (error != 0) 711 break; 712 vput(ndp->ni_vp); 713 dp = ndp->ni_dvp; 714 /* 715 * Check if root directory should replace current directory. 716 */ 717 cnp->cn_nameptr = cnp->cn_pnbuf; 718 if (*(cnp->cn_nameptr) == '/') { 719 /* 720 * Reset the lookup to start from the real root without 721 * origin path name reloading. 722 */ 723 if (__predict_false(ndp->ni_rootdir != pwd->pwd_rdir)) { 724 cnp->cn_flags |= ISRESTARTED; 725 ndp->ni_rootdir = pwd->pwd_rdir; 726 } 727 vrele(dp); 728 error = namei_handle_root(ndp, &dp); 729 if (error != 0) 730 goto out; 731 } 732 } 733 vput(ndp->ni_vp); 734 ndp->ni_vp = NULL; 735 vrele(ndp->ni_dvp); 736 out: 737 MPASS(error != 0); 738 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp); 739 namei_cleanup_cnp(cnp); 740 nameicap_cleanup(ndp); 741 pwd_drop(pwd); 742 return (error); 743 } 744 745 static int 746 enforce_lkflags(struct mount *mp, int lkflags) 747 { 748 749 if (mp == NULL || ((lkflags & LK_SHARED) && 750 !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) { 751 lkflags &= ~LK_SHARED; 752 lkflags |= LK_EXCLUSIVE; 753 } 754 lkflags |= LK_NODDLKTREAT; 755 return (lkflags); 756 } 757 758 static __inline int 759 needs_exclusive_leaf(struct mount *mp, int flags) 760 { 761 762 /* 763 * Intermediate nodes can use shared locks, we only need to 764 * force an exclusive lock for leaf nodes. 765 */ 766 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 767 return (0); 768 769 /* Always use exclusive locks if LOCKSHARED isn't set. */ 770 if (!(flags & LOCKSHARED)) 771 return (1); 772 773 /* 774 * For lookups during open(), if the mount point supports 775 * extended shared operations, then use a shared lock for the 776 * leaf node, otherwise use an exclusive lock. 777 */ 778 if ((flags & ISOPEN) != 0) 779 return (!MNT_EXTENDED_SHARED(mp)); 780 781 /* 782 * Lookup requests outside of open() that specify LOCKSHARED 783 * only need a shared lock on the leaf vnode. 784 */ 785 return (0); 786 } 787 788 /* 789 * Various filesystems expect to be able to copy a name component with length 790 * bounded by NAME_MAX into a directory entry buffer of size MAXNAMLEN. Make 791 * sure that these are the same size. 792 */ 793 _Static_assert(MAXNAMLEN == NAME_MAX, 794 "MAXNAMLEN and NAME_MAX have different values"); 795 796 static int __noinline 797 vfs_lookup_degenerate(struct nameidata *ndp, struct vnode *dp, int wantparent) 798 { 799 struct componentname *cnp; 800 struct mount *mp; 801 int error; 802 803 cnp = &ndp->ni_cnd; 804 805 cnp->cn_flags |= ISLASTCN; 806 807 mp = atomic_load_ptr(&dp->v_mount); 808 if (needs_exclusive_leaf(mp, cnp->cn_flags)) { 809 cnp->cn_lkflags &= ~LK_SHARED; 810 cnp->cn_lkflags |= LK_EXCLUSIVE; 811 } 812 813 vn_lock(dp, enforce_lkflags(mp, cnp->cn_lkflags | LK_RETRY)); 814 815 if (dp->v_type != VDIR) { 816 error = ENOTDIR; 817 goto bad; 818 } 819 if (cnp->cn_nameiop != LOOKUP) { 820 error = EISDIR; 821 goto bad; 822 } 823 if (wantparent) { 824 ndp->ni_dvp = dp; 825 VREF(dp); 826 } 827 ndp->ni_vp = dp; 828 cnp->cn_namelen = 0; 829 830 if (cnp->cn_flags & AUDITVNODE1) 831 AUDIT_ARG_VNODE1(dp); 832 else if (cnp->cn_flags & AUDITVNODE2) 833 AUDIT_ARG_VNODE2(dp); 834 835 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 836 VOP_UNLOCK(dp); 837 return (0); 838 bad: 839 VOP_UNLOCK(dp); 840 return (error); 841 } 842 843 /* 844 * FAILIFEXISTS handling. 845 * 846 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange 847 * behaviour of leaving the vnode unlocked if the target is the same 848 * vnode as the parent. 849 */ 850 static int __noinline 851 vfs_lookup_failifexists(struct nameidata *ndp) 852 { 853 struct componentname *cnp __diagused; 854 855 cnp = &ndp->ni_cnd; 856 857 MPASS((cnp->cn_flags & ISSYMLINK) == 0); 858 if (ndp->ni_vp == ndp->ni_dvp) 859 vrele(ndp->ni_dvp); 860 else 861 vput(ndp->ni_dvp); 862 vrele(ndp->ni_vp); 863 ndp->ni_dvp = NULL; 864 ndp->ni_vp = NULL; 865 NDFREE_PNBUF(ndp); 866 return (EEXIST); 867 } 868 869 static int __noinline 870 vfs_lookup_cross_mount(struct nameidata *ndp) 871 { 872 struct componentname *cnp; 873 struct mount *mp; 874 struct vnode *dp, *tdp; 875 int error, crosslkflags; 876 bool crosslock; 877 878 cnp = &ndp->ni_cnd; 879 dp = ndp->ni_vp; 880 881 /* 882 * The vnode has been mounted on, find the root of the mounted 883 * filesystem. 884 */ 885 for (;;) { 886 mp = dp->v_mountedhere; 887 ASSERT_VOP_LOCKED(dp, __func__); 888 VNPASS((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0 && mp != NULL, dp); 889 890 crosslock = (dp->v_vflag & VV_CROSSLOCK) != 0; 891 crosslkflags = enforce_lkflags(mp, cnp->cn_lkflags); 892 if (__predict_false(crosslock)) { 893 /* 894 * We are going to be holding the vnode lock, which 895 * in this case is shared by the root vnode of the 896 * filesystem mounted at mp, across the call to 897 * VFS_ROOT(). Make the situation clear to the 898 * filesystem by passing LK_CANRECURSE if the 899 * lock is held exclusive, or by clearinng 900 * LK_NODDLKTREAT to allow recursion on the shared 901 * lock in the presence of an exclusive waiter. 902 */ 903 if (VOP_ISLOCKED(dp) == LK_EXCLUSIVE) { 904 crosslkflags &= ~LK_SHARED; 905 crosslkflags |= LK_EXCLUSIVE | LK_CANRECURSE; 906 } else if ((crosslkflags & LK_EXCLUSIVE) != 0) { 907 error = vn_lock(dp, LK_UPGRADE); 908 if (error != 0) 909 break; 910 if (dp->v_mountedhere != mp) { 911 continue; 912 } 913 } else 914 crosslkflags &= ~LK_NODDLKTREAT; 915 } 916 if (vfs_busy(mp, 0) != 0) 917 continue; 918 if (__predict_true(!crosslock)) 919 vput(dp); 920 if (dp != ndp->ni_dvp) 921 vput(ndp->ni_dvp); 922 else 923 vrele(ndp->ni_dvp); 924 vrefact(vp_crossmp); 925 ndp->ni_dvp = vp_crossmp; 926 error = VFS_ROOT(mp, crosslkflags, &tdp); 927 vfs_unbusy(mp); 928 if (__predict_false(crosslock)) 929 vput(dp); 930 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 931 panic("vp_crossmp exclusively locked or reclaimed"); 932 if (error != 0) 933 break; 934 ndp->ni_vp = dp = tdp; 935 if ((vn_irflag_read(dp) & VIRF_MOUNTPOINT) == 0) 936 break; 937 } 938 939 return (error); 940 } 941 942 /* 943 * Search a pathname. 944 * This is a very central and rather complicated routine. 945 * 946 * The pathname is pointed to by cn_nameptr and is of length ni_pathlen. 947 * The starting directory is taken from ni_startdir. The pathname is 948 * descended until done, or a symbolic link is encountered. The cn_flags 949 * has ISLASTCN or'ed if the path is completed or ISSYMLINK or'ed if a 950 * symbolic link needing interpretation is encountered. 951 * 952 * The cn_nameiop is LOOKUP, CREATE, RENAME, or DELETE depending on 953 * whether the name is to be looked up, created, renamed, or deleted. 954 * When CREATE, RENAME, or DELETE is specified, information usable in 955 * creating, renaming, or deleting a directory entry may be calculated. 956 * If cn_flags has LOCKPARENT or'ed into it, the parent directory is returned 957 * locked. If it has WANTPARENT or'ed into it, the parent directory is 958 * returned unlocked. Otherwise the parent directory is not returned. If 959 * the target of the pathname exists and LOCKLEAF is or'ed into the cn_flags 960 * the target is returned locked, otherwise it is returned unlocked. 961 * 962 * Overall outline of lookup: 963 * 964 * handle degenerate case where name is null string 965 * 966 * dirloop: 967 * identify next component of name at ndp->ni_cnd.cn_nameptr 968 * handle .. special cases related to capabilities, chroot, jail 969 * if .. and crossing mount points and on mounted filesys, find parent 970 * call VOP_LOOKUP routine for next component name 971 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 972 * component vnode returned in ni_vp (if it exists), locked. 973 * if result vnode is mounted on and crossing mount points, 974 * find mounted on vnode 975 * if more components of name, do next level at dirloop 976 * if VOP_LOOKUP returns ERELOOKUP, repeat the same level at dirloop 977 * return the answer in ni_vp, locked if LOCKLEAF set 978 * if LOCKPARENT set, return locked parent in ni_dvp 979 * if WANTPARENT set, return unlocked parent in ni_dvp 980 */ 981 int 982 vfs_lookup(struct nameidata *ndp) 983 { 984 char *cp; /* pointer into pathname argument */ 985 char *prev_ni_next; /* saved ndp->ni_next */ 986 char *nulchar; /* location of '\0' in cn_pnbuf */ 987 char *lastchar; /* location of the last character */ 988 struct vnode *dp = NULL; /* the directory we are searching */ 989 struct vnode *tdp; /* saved dp */ 990 struct prison *pr; 991 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */ 992 int docache; /* == 0 do not cache last component */ 993 int wantparent; /* 1 => wantparent or lockparent flag */ 994 int rdonly; /* lookup read-only flag bit */ 995 int error = 0; 996 int relookup = 0; /* do not consume the path component */ 997 struct componentname *cnp = &ndp->ni_cnd; 998 int lkflags_save; 999 int ni_dvp_unlocked; 1000 1001 /* 1002 * Setup: break out flag bits into variables. 1003 */ 1004 ni_dvp_unlocked = 0; 1005 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 1006 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 1007 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 1008 /* 1009 * When set to zero, docache causes the last component of the 1010 * pathname to be deleted from the cache and the full lookup 1011 * of the name to be done (via VOP_CACHEDLOOKUP()). Often 1012 * filesystems need some pre-computed values that are made 1013 * during the full lookup, for instance UFS sets dp->i_offset. 1014 * 1015 * The docache variable is set to zero when requested by the 1016 * NOCACHE flag and for all modifying operations except CREATE. 1017 */ 1018 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 1019 if (cnp->cn_nameiop == DELETE || 1020 (wantparent && cnp->cn_nameiop != CREATE && 1021 cnp->cn_nameiop != LOOKUP)) 1022 docache = 0; 1023 rdonly = cnp->cn_flags & RDONLY; 1024 cnp->cn_flags &= ~ISSYMLINK; 1025 ndp->ni_dvp = NULL; 1026 1027 cnp->cn_lkflags = LK_SHARED; 1028 dp = ndp->ni_startdir; 1029 ndp->ni_startdir = NULLVP; 1030 1031 /* 1032 * Leading slashes, if any, are supposed to be skipped by the caller. 1033 */ 1034 MPASS(cnp->cn_nameptr[0] != '/'); 1035 1036 /* 1037 * Check for degenerate name (e.g. / or "") which is a way of talking 1038 * about a directory, e.g. like "/." or ".". 1039 */ 1040 if (__predict_false(cnp->cn_nameptr[0] == '\0')) { 1041 error = vfs_lookup_degenerate(ndp, dp, wantparent); 1042 if (error == 0) 1043 goto success_right_lock; 1044 goto bad_unlocked; 1045 } 1046 1047 /* 1048 * Nul-out trailing slashes (e.g., "foo///" -> "foo"). 1049 * 1050 * This must be done before VOP_LOOKUP() because some fs's don't know 1051 * about trailing slashes. Remember if there were trailing slashes to 1052 * handle symlinks, existing non-directories and non-existing files 1053 * that won't be directories specially later. 1054 */ 1055 MPASS(ndp->ni_pathlen >= 2); 1056 lastchar = &cnp->cn_nameptr[ndp->ni_pathlen - 2]; 1057 if (*lastchar == '/') { 1058 while (lastchar >= cnp->cn_pnbuf) { 1059 *lastchar = '\0'; 1060 lastchar--; 1061 ndp->ni_pathlen--; 1062 if (*lastchar != '/') { 1063 break; 1064 } 1065 } 1066 cnp->cn_flags |= TRAILINGSLASH; 1067 } 1068 1069 /* 1070 * We use shared locks until we hit the parent of the last cn then 1071 * we adjust based on the requesting flags. 1072 */ 1073 vn_lock(dp, 1074 enforce_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY)); 1075 1076 dirloop: 1077 /* 1078 * Search a new directory. 1079 * 1080 * The last component of the filename is left accessible via 1081 * cnp->cn_nameptr. It has to be freed with a call to NDFREE*. 1082 * 1083 * Store / as a temporary sentinel so that we only have one character 1084 * to test for. Pathnames tend to be short so this should not be 1085 * resulting in cache misses. 1086 */ 1087 nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1]; 1088 KASSERT(*nulchar == '\0', 1089 ("%s: expected nul at %p; string [%s]\n", __func__, nulchar, 1090 cnp->cn_pnbuf)); 1091 *nulchar = '/'; 1092 for (cp = cnp->cn_nameptr; *cp != '/'; cp++) { 1093 KASSERT(*cp != '\0', 1094 ("%s: encountered unexpected nul; string [%s]\n", __func__, 1095 cnp->cn_nameptr)); 1096 continue; 1097 } 1098 *nulchar = '\0'; 1099 cnp->cn_namelen = cp - cnp->cn_nameptr; 1100 if (__predict_false(cnp->cn_namelen > NAME_MAX)) { 1101 error = ENAMETOOLONG; 1102 goto bad; 1103 } 1104 prev_ni_pathlen = ndp->ni_pathlen; 1105 ndp->ni_pathlen -= cnp->cn_namelen; 1106 KASSERT(ndp->ni_pathlen <= PATH_MAX, 1107 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen)); 1108 prev_ni_next = ndp->ni_next; 1109 ndp->ni_next = cp; 1110 1111 /* 1112 * Something else should be clearing this. 1113 */ 1114 cnp->cn_flags &= ~(ISDOTDOT|ISLASTCN); 1115 1116 cnp->cn_flags |= MAKEENTRY; 1117 if (*cp == '\0' && docache == 0) 1118 cnp->cn_flags &= ~MAKEENTRY; 1119 if (cnp->cn_namelen == 2 && 1120 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 1121 cnp->cn_flags |= ISDOTDOT; 1122 if (*ndp->ni_next == 0) { 1123 cnp->cn_flags |= ISLASTCN; 1124 1125 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 1126 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))) { 1127 error = EINVAL; 1128 goto bad; 1129 } 1130 } 1131 1132 nameicap_tracker_add(ndp, dp); 1133 1134 /* 1135 * Make sure degenerate names don't get here, their handling was 1136 * previously found in this spot. 1137 */ 1138 MPASS(cnp->cn_nameptr[0] != '\0'); 1139 1140 /* 1141 * Handle "..": five special cases. 1142 * 0. If doing a capability lookup and lookup_cap_dotdot is 1143 * disabled, return ENOTCAPABLE. 1144 * 1. Return an error if this is the last component of 1145 * the name and the operation is DELETE or RENAME. 1146 * 2. If at root directory (e.g. after chroot) 1147 * or at absolute root directory 1148 * then ignore it so can't get out. 1149 * 3. If this vnode is the root of a mounted 1150 * filesystem, then replace it with the 1151 * vnode which was mounted on so we take the 1152 * .. in the other filesystem. 1153 * 4. If the vnode is the top directory of 1154 * the jail or chroot, don't let them out. 1155 * 5. If doing a capability lookup and lookup_cap_dotdot is 1156 * enabled, return ENOTCAPABLE if the lookup would escape 1157 * from the initial file descriptor directory. Checks are 1158 * done by ensuring that namei() already traversed the 1159 * result of dotdot lookup. 1160 */ 1161 if (cnp->cn_flags & ISDOTDOT) { 1162 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT)) 1163 == NI_LCF_STRICTRELATIVE) { 1164 #ifdef KTRACE 1165 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1166 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1167 #endif 1168 error = ENOTCAPABLE; 1169 goto bad; 1170 } 1171 if ((cnp->cn_flags & ISLASTCN) != 0 && 1172 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1173 error = EINVAL; 1174 goto bad; 1175 } 1176 for (;;) { 1177 for (pr = cnp->cn_cred->cr_prison; pr != NULL; 1178 pr = pr->pr_parent) 1179 if (dp == pr->pr_root) 1180 break; 1181 bool isroot = dp == ndp->ni_rootdir || 1182 dp == ndp->ni_topdir || dp == rootvnode || 1183 pr != NULL; 1184 if (isroot && (ndp->ni_lcf & 1185 NI_LCF_STRICTRELATIVE) != 0) { 1186 error = ENOTCAPABLE; 1187 goto capdotdot; 1188 } 1189 if (isroot || ((dp->v_vflag & VV_ROOT) != 0 && 1190 (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 1191 ndp->ni_dvp = dp; 1192 ndp->ni_vp = dp; 1193 VREF(dp); 1194 goto nextname; 1195 } 1196 if ((dp->v_vflag & VV_ROOT) == 0) 1197 break; 1198 if (VN_IS_DOOMED(dp)) { /* forced unmount */ 1199 error = ENOENT; 1200 goto bad; 1201 } 1202 tdp = dp; 1203 dp = dp->v_mount->mnt_vnodecovered; 1204 VREF(dp); 1205 vput(tdp); 1206 vn_lock(dp, 1207 enforce_lkflags(dp->v_mount, cnp->cn_lkflags | 1208 LK_RETRY)); 1209 error = nameicap_check_dotdot(ndp, dp); 1210 if (error != 0) { 1211 capdotdot: 1212 #ifdef KTRACE 1213 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1214 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1215 #endif 1216 goto bad; 1217 } 1218 } 1219 } 1220 1221 /* 1222 * We now have a segment name to search for, and a directory to search. 1223 */ 1224 unionlookup: 1225 #ifdef MAC 1226 error = mac_vnode_check_lookup(cnp->cn_cred, dp, cnp); 1227 if (__predict_false(error)) 1228 goto bad; 1229 #endif 1230 ndp->ni_dvp = dp; 1231 ndp->ni_vp = NULL; 1232 ASSERT_VOP_LOCKED(dp, "lookup"); 1233 /* 1234 * If we have a shared lock we may need to upgrade the lock for the 1235 * last operation. 1236 */ 1237 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) && 1238 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED) 1239 vn_lock(dp, LK_UPGRADE|LK_RETRY); 1240 if (VN_IS_DOOMED(dp)) { 1241 error = ENOENT; 1242 goto bad; 1243 } 1244 /* 1245 * If we're looking up the last component and we need an exclusive 1246 * lock, adjust our lkflags. 1247 */ 1248 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 1249 cnp->cn_lkflags = LK_EXCLUSIVE; 1250 lkflags_save = cnp->cn_lkflags; 1251 cnp->cn_lkflags = enforce_lkflags(dp->v_mount, cnp->cn_lkflags); 1252 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp); 1253 cnp->cn_lkflags = lkflags_save; 1254 if (error != 0) { 1255 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 1256 if ((error == ENOENT) && 1257 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 1258 (dp->v_mount->mnt_flag & MNT_UNION)) { 1259 tdp = dp; 1260 dp = dp->v_mount->mnt_vnodecovered; 1261 VREF(dp); 1262 vput(tdp); 1263 vn_lock(dp, 1264 enforce_lkflags(dp->v_mount, cnp->cn_lkflags | 1265 LK_RETRY)); 1266 nameicap_tracker_add(ndp, dp); 1267 goto unionlookup; 1268 } 1269 1270 if (error == ERELOOKUP) { 1271 vref(dp); 1272 ndp->ni_vp = dp; 1273 error = 0; 1274 relookup = 1; 1275 goto good; 1276 } 1277 1278 if (error != EJUSTRETURN) 1279 goto bad; 1280 /* 1281 * At this point, we know we're at the end of the 1282 * pathname. If creating / renaming, we can consider 1283 * allowing the file or directory to be created / renamed, 1284 * provided we're not on a read-only filesystem. 1285 */ 1286 if (rdonly) { 1287 error = EROFS; 1288 goto bad; 1289 } 1290 /* trailing slash only allowed for directories */ 1291 if ((cnp->cn_flags & TRAILINGSLASH) && 1292 !(cnp->cn_flags & WILLBEDIR)) { 1293 error = ENOENT; 1294 goto bad; 1295 } 1296 if ((cnp->cn_flags & LOCKPARENT) == 0) 1297 VOP_UNLOCK(dp); 1298 /* 1299 * We return with ni_vp NULL to indicate that the entry 1300 * doesn't currently exist, leaving a pointer to the 1301 * (possibly locked) directory vnode in ndp->ni_dvp. 1302 */ 1303 goto success; 1304 } 1305 1306 good: 1307 dp = ndp->ni_vp; 1308 1309 /* 1310 * Check for symbolic link 1311 */ 1312 if ((dp->v_type == VLNK) && 1313 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 1314 *ndp->ni_next == '/')) { 1315 cnp->cn_flags |= ISSYMLINK; 1316 if (VN_IS_DOOMED(dp)) { 1317 /* 1318 * We can't know whether the directory was mounted with 1319 * NOSYMFOLLOW, so we can't follow safely. 1320 */ 1321 error = ENOENT; 1322 goto bad2; 1323 } 1324 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 1325 error = EACCES; 1326 goto bad2; 1327 } 1328 /* 1329 * Symlink code always expects an unlocked dvp. 1330 */ 1331 if (ndp->ni_dvp != ndp->ni_vp) { 1332 VOP_UNLOCK(ndp->ni_dvp); 1333 ni_dvp_unlocked = 1; 1334 } 1335 goto success; 1336 } 1337 1338 if ((vn_irflag_read(dp) & VIRF_MOUNTPOINT) != 0 && 1339 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 1340 error = vfs_lookup_cross_mount(ndp); 1341 if (error != 0) 1342 goto bad_unlocked; 1343 /* 1344 * FALLTHROUGH to nextname 1345 */ 1346 dp = ndp->ni_vp; 1347 } 1348 1349 nextname: 1350 /* 1351 * Not a symbolic link that we will follow. Continue with the 1352 * next component if there is any; otherwise, we're done. 1353 */ 1354 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 1355 ("lookup: invalid path state.")); 1356 if (relookup) { 1357 relookup = 0; 1358 ndp->ni_pathlen = prev_ni_pathlen; 1359 ndp->ni_next = prev_ni_next; 1360 if (ndp->ni_dvp != dp) 1361 vput(ndp->ni_dvp); 1362 else 1363 vrele(ndp->ni_dvp); 1364 goto dirloop; 1365 } 1366 if (cnp->cn_flags & ISDOTDOT) { 1367 error = nameicap_check_dotdot(ndp, ndp->ni_vp); 1368 if (error != 0) { 1369 #ifdef KTRACE 1370 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1371 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1372 #endif 1373 goto bad2; 1374 } 1375 } 1376 if (*ndp->ni_next == '/') { 1377 cnp->cn_nameptr = ndp->ni_next; 1378 while (*cnp->cn_nameptr == '/') { 1379 cnp->cn_nameptr++; 1380 ndp->ni_pathlen--; 1381 } 1382 if (ndp->ni_dvp != dp) 1383 vput(ndp->ni_dvp); 1384 else 1385 vrele(ndp->ni_dvp); 1386 goto dirloop; 1387 } 1388 /* 1389 * If we're processing a path with a trailing slash, 1390 * check that the end result is a directory. 1391 */ 1392 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 1393 error = ENOTDIR; 1394 goto bad2; 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 error = EROFS; 1402 goto bad2; 1403 } 1404 if (!wantparent) { 1405 ni_dvp_unlocked = 2; 1406 if (ndp->ni_dvp != dp) 1407 vput(ndp->ni_dvp); 1408 else 1409 vrele(ndp->ni_dvp); 1410 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 1411 VOP_UNLOCK(ndp->ni_dvp); 1412 ni_dvp_unlocked = 1; 1413 } 1414 1415 if (cnp->cn_flags & AUDITVNODE1) 1416 AUDIT_ARG_VNODE1(dp); 1417 else if (cnp->cn_flags & AUDITVNODE2) 1418 AUDIT_ARG_VNODE2(dp); 1419 1420 if ((cnp->cn_flags & LOCKLEAF) == 0) 1421 VOP_UNLOCK(dp); 1422 success: 1423 /* 1424 * FIXME: for lookups which only cross a mount point to fetch the 1425 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem 1426 * if either WANTPARENT or LOCKPARENT is set. 1427 */ 1428 /* 1429 * Because of shared lookup we may have the vnode shared locked, but 1430 * the caller may want it to be exclusively locked. 1431 */ 1432 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 1433 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 1434 vn_lock(dp, LK_UPGRADE | LK_RETRY); 1435 if (VN_IS_DOOMED(dp)) { 1436 error = ENOENT; 1437 goto bad2; 1438 } 1439 } 1440 success_right_lock: 1441 if (ndp->ni_vp != NULL) { 1442 if ((cnp->cn_flags & ISDOTDOT) == 0) 1443 nameicap_tracker_add(ndp, ndp->ni_vp); 1444 if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS) 1445 return (vfs_lookup_failifexists(ndp)); 1446 } 1447 return (0); 1448 1449 bad2: 1450 if (ni_dvp_unlocked != 2) { 1451 if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 1452 vput(ndp->ni_dvp); 1453 else 1454 vrele(ndp->ni_dvp); 1455 } 1456 bad: 1457 vput(dp); 1458 bad_unlocked: 1459 ndp->ni_vp = NULL; 1460 return (error); 1461 } 1462 1463 /* 1464 * relookup - lookup a path name component 1465 * Used by lookup to re-acquire things. 1466 */ 1467 int 1468 vfs_relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp, 1469 bool refstart) 1470 { 1471 struct vnode *dp = NULL; /* the directory we are searching */ 1472 int rdonly; /* lookup read-only flag bit */ 1473 int error = 0; 1474 1475 KASSERT(cnp->cn_flags & ISLASTCN, 1476 ("relookup: Not given last component.")); 1477 /* 1478 * Setup: break out flag bits into variables. 1479 */ 1480 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0, 1481 ("relookup: parent not wanted")); 1482 rdonly = cnp->cn_flags & RDONLY; 1483 cnp->cn_flags &= ~ISSYMLINK; 1484 dp = dvp; 1485 cnp->cn_lkflags = LK_EXCLUSIVE; 1486 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 1487 1488 /* 1489 * Search a new directory. 1490 * 1491 * See a comment in vfs_lookup for cnp->cn_nameptr. 1492 * 1493 * Check for "" which represents the root directory after slash 1494 * removal. 1495 */ 1496 if (cnp->cn_nameptr[0] == '\0') { 1497 /* 1498 * Support only LOOKUP for "/" because lookup() 1499 * can't succeed for CREATE, DELETE and RENAME. 1500 */ 1501 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 1502 KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 1503 1504 if (!(cnp->cn_flags & LOCKLEAF)) 1505 VOP_UNLOCK(dp); 1506 *vpp = dp; 1507 /* XXX This should probably move to the top of function. */ 1508 if (refstart) 1509 panic("lookup: SAVESTART"); 1510 return (0); 1511 } 1512 1513 if (cnp->cn_flags & ISDOTDOT) 1514 panic ("relookup: lookup on dot-dot"); 1515 1516 /* 1517 * We now have a segment name to search for, and a directory to search. 1518 */ 1519 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 1520 KASSERT(*vpp == NULL, ("leaf should be empty")); 1521 if (error != EJUSTRETURN) 1522 goto bad; 1523 /* 1524 * If creating and at end of pathname, then can consider 1525 * allowing file to be created. 1526 */ 1527 if (rdonly) { 1528 error = EROFS; 1529 goto bad; 1530 } 1531 /* ASSERT(dvp == ndp->ni_startdir) */ 1532 if (refstart) 1533 VREF(dvp); 1534 if ((cnp->cn_flags & LOCKPARENT) == 0) 1535 VOP_UNLOCK(dp); 1536 /* 1537 * We return with ni_vp NULL to indicate that the entry 1538 * doesn't currently exist, leaving a pointer to the 1539 * (possibly locked) directory vnode in ndp->ni_dvp. 1540 */ 1541 return (0); 1542 } 1543 1544 dp = *vpp; 1545 1546 /* 1547 * Disallow directory write attempts on read-only filesystems. 1548 */ 1549 if (rdonly && 1550 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1551 if (dvp == dp) 1552 vrele(dvp); 1553 else 1554 vput(dvp); 1555 error = EROFS; 1556 goto bad; 1557 } 1558 /* 1559 * Set the parent lock/ref state to the requested state. 1560 */ 1561 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) 1562 VOP_UNLOCK(dvp); 1563 /* 1564 * Check for symbolic link 1565 */ 1566 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1567 ("relookup: symlink found.\n")); 1568 1569 /* ASSERT(dvp == ndp->ni_startdir) */ 1570 if (refstart) 1571 VREF(dvp); 1572 1573 if ((cnp->cn_flags & LOCKLEAF) == 0) 1574 VOP_UNLOCK(dp); 1575 return (0); 1576 bad: 1577 vput(dp); 1578 *vpp = NULL; 1579 return (error); 1580 } 1581 1582 #ifdef INVARIANTS 1583 /* 1584 * Validate the final state of ndp after the lookup. 1585 */ 1586 static void 1587 NDVALIDATE_impl(struct nameidata *ndp, int line) 1588 { 1589 struct componentname *cnp; 1590 1591 cnp = &ndp->ni_cnd; 1592 if (cnp->cn_pnbuf == NULL) 1593 panic("%s: got no buf! called from %d", __func__, line); 1594 } 1595 1596 #endif 1597