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