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