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