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 (error == 0 ? ndp->ni_vp : NULL), 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 if (error == 0) 700 NDVALIDATE(ndp); 701 return (error); 702 } 703 error = namei_follow_link(ndp); 704 if (error != 0) 705 break; 706 vput(ndp->ni_vp); 707 dp = ndp->ni_dvp; 708 /* 709 * Check if root directory should replace current directory. 710 */ 711 cnp->cn_nameptr = cnp->cn_pnbuf; 712 if (*(cnp->cn_nameptr) == '/') { 713 vrele(dp); 714 error = namei_handle_root(ndp, &dp); 715 if (error != 0) 716 goto out; 717 } 718 } 719 vput(ndp->ni_vp); 720 ndp->ni_vp = NULL; 721 vrele(ndp->ni_dvp); 722 out: 723 MPASS(error != 0); 724 SDT_PROBE4(vfs, namei, lookup, return, error, NULL, false, ndp); 725 namei_cleanup_cnp(cnp); 726 nameicap_cleanup(ndp); 727 pwd_drop(pwd); 728 return (error); 729 } 730 731 static int 732 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags) 733 { 734 735 if (mp == NULL || ((lkflags & LK_SHARED) && 736 !(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED))) { 737 lkflags &= ~LK_SHARED; 738 lkflags |= LK_EXCLUSIVE; 739 } 740 lkflags |= LK_NODDLKTREAT; 741 return (lkflags); 742 } 743 744 static __inline int 745 needs_exclusive_leaf(struct mount *mp, int flags) 746 { 747 748 /* 749 * Intermediate nodes can use shared locks, we only need to 750 * force an exclusive lock for leaf nodes. 751 */ 752 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 753 return (0); 754 755 /* Always use exclusive locks if LOCKSHARED isn't set. */ 756 if (!(flags & LOCKSHARED)) 757 return (1); 758 759 /* 760 * For lookups during open(), if the mount point supports 761 * extended shared operations, then use a shared lock for the 762 * leaf node, otherwise use an exclusive lock. 763 */ 764 if ((flags & ISOPEN) != 0) 765 return (!MNT_EXTENDED_SHARED(mp)); 766 767 /* 768 * Lookup requests outside of open() that specify LOCKSHARED 769 * only need a shared lock on the leaf vnode. 770 */ 771 return (0); 772 } 773 774 /* 775 * Various filesystems expect to be able to copy a name component with length 776 * bounded by NAME_MAX into a directory entry buffer of size MAXNAMLEN. Make 777 * sure that these are the same size. 778 */ 779 _Static_assert(MAXNAMLEN == NAME_MAX, 780 "MAXNAMLEN and NAME_MAX have different values"); 781 782 static int __noinline 783 vfs_lookup_degenerate(struct nameidata *ndp, struct vnode *dp, int wantparent) 784 { 785 struct componentname *cnp; 786 struct mount *mp; 787 int error; 788 789 cnp = &ndp->ni_cnd; 790 791 cnp->cn_flags |= ISLASTCN; 792 793 mp = atomic_load_ptr(&dp->v_mount); 794 if (needs_exclusive_leaf(mp, cnp->cn_flags)) { 795 cnp->cn_lkflags &= ~LK_SHARED; 796 cnp->cn_lkflags |= LK_EXCLUSIVE; 797 } 798 799 vn_lock(dp, 800 compute_cn_lkflags(mp, cnp->cn_lkflags | LK_RETRY, 801 cnp->cn_flags)); 802 803 if (dp->v_type != VDIR) { 804 error = ENOTDIR; 805 goto bad; 806 } 807 if (cnp->cn_nameiop != LOOKUP) { 808 error = EISDIR; 809 goto bad; 810 } 811 if (wantparent) { 812 ndp->ni_dvp = dp; 813 VREF(dp); 814 } 815 ndp->ni_vp = dp; 816 cnp->cn_namelen = 0; 817 818 if (cnp->cn_flags & AUDITVNODE1) 819 AUDIT_ARG_VNODE1(dp); 820 else if (cnp->cn_flags & AUDITVNODE2) 821 AUDIT_ARG_VNODE2(dp); 822 823 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 824 VOP_UNLOCK(dp); 825 /* XXX This should probably move to the top of function. */ 826 if (cnp->cn_flags & SAVESTART) 827 panic("lookup: SAVESTART"); 828 return (0); 829 bad: 830 VOP_UNLOCK(dp); 831 return (error); 832 } 833 834 /* 835 * FAILIFEXISTS handling. 836 * 837 * XXX namei called with LOCKPARENT but not LOCKLEAF has the strange 838 * behaviour of leaving the vnode unlocked if the target is the same 839 * vnode as the parent. 840 */ 841 static int __noinline 842 vfs_lookup_failifexists(struct nameidata *ndp) 843 { 844 struct componentname *cnp __diagused; 845 846 cnp = &ndp->ni_cnd; 847 848 MPASS((cnp->cn_flags & ISSYMLINK) == 0); 849 if (ndp->ni_vp == ndp->ni_dvp) 850 vrele(ndp->ni_dvp); 851 else 852 vput(ndp->ni_dvp); 853 vrele(ndp->ni_vp); 854 ndp->ni_dvp = NULL; 855 ndp->ni_vp = NULL; 856 NDFREE_PNBUF(ndp); 857 return (EEXIST); 858 } 859 860 /* 861 * Search a pathname. 862 * This is a very central and rather complicated routine. 863 * 864 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 865 * The starting directory is taken from ni_startdir. The pathname is 866 * descended until done, or a symbolic link is encountered. The variable 867 * ni_more is clear if the path is completed; it is set to one if a 868 * symbolic link needing interpretation is encountered. 869 * 870 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 871 * whether the name is to be looked up, created, renamed, or deleted. 872 * When CREATE, RENAME, or DELETE is specified, information usable in 873 * creating, renaming, or deleting a directory entry may be calculated. 874 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 875 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 876 * returned unlocked. Otherwise the parent directory is not returned. If 877 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 878 * the target is returned locked, otherwise it is returned unlocked. 879 * When creating or renaming and LOCKPARENT is specified, the target may not 880 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 881 * 882 * Overall outline of lookup: 883 * 884 * dirloop: 885 * identify next component of name at ndp->ni_ptr 886 * handle degenerate case where name is null string 887 * if .. and crossing mount points and on mounted filesys, find parent 888 * call VOP_LOOKUP routine for next component name 889 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 890 * component vnode returned in ni_vp (if it exists), locked. 891 * if result vnode is mounted on and crossing mount points, 892 * find mounted on vnode 893 * if more components of name, do next level at dirloop 894 * return the answer in ni_vp, locked if LOCKLEAF set 895 * if LOCKPARENT set, return locked parent in ni_dvp 896 * if WANTPARENT set, return unlocked parent in ni_dvp 897 */ 898 int 899 vfs_lookup(struct nameidata *ndp) 900 { 901 char *cp; /* pointer into pathname argument */ 902 char *prev_ni_next; /* saved ndp->ni_next */ 903 char *nulchar; /* location of '\0' in cn_pnbuf */ 904 char *lastchar; /* location of the last character */ 905 struct vnode *dp = NULL; /* the directory we are searching */ 906 struct vnode *tdp; /* saved dp */ 907 struct mount *mp; /* mount table entry */ 908 struct prison *pr; 909 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */ 910 int docache; /* == 0 do not cache last component */ 911 int wantparent; /* 1 => wantparent or lockparent flag */ 912 int rdonly; /* lookup read-only flag bit */ 913 int error = 0; 914 int dpunlocked = 0; /* dp has already been unlocked */ 915 int relookup = 0; /* do not consume the path component */ 916 struct componentname *cnp = &ndp->ni_cnd; 917 int lkflags_save; 918 int ni_dvp_unlocked; 919 920 /* 921 * Setup: break out flag bits into variables. 922 */ 923 ni_dvp_unlocked = 0; 924 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 925 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 926 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 927 /* 928 * When set to zero, docache causes the last component of the 929 * pathname to be deleted from the cache and the full lookup 930 * of the name to be done (via VOP_CACHEDLOOKUP()). Often 931 * filesystems need some pre-computed values that are made 932 * during the full lookup, for instance UFS sets dp->i_offset. 933 * 934 * The docache variable is set to zero when requested by the 935 * NOCACHE flag and for all modifying operations except CREATE. 936 */ 937 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 938 if (cnp->cn_nameiop == DELETE || 939 (wantparent && cnp->cn_nameiop != CREATE && 940 cnp->cn_nameiop != LOOKUP)) 941 docache = 0; 942 rdonly = cnp->cn_flags & RDONLY; 943 cnp->cn_flags &= ~ISSYMLINK; 944 ndp->ni_dvp = NULL; 945 946 cnp->cn_lkflags = LK_SHARED; 947 dp = ndp->ni_startdir; 948 ndp->ni_startdir = NULLVP; 949 950 /* 951 * Leading slashes, if any, are supposed to be skipped by the caller. 952 */ 953 MPASS(cnp->cn_nameptr[0] != '/'); 954 955 /* 956 * Check for degenerate name (e.g. / or "") which is a way of talking 957 * about a directory, e.g. like "/." or ".". 958 */ 959 if (__predict_false(cnp->cn_nameptr[0] == '\0')) { 960 error = vfs_lookup_degenerate(ndp, dp, wantparent); 961 if (error == 0) 962 goto success_right_lock; 963 goto bad_unlocked; 964 } 965 966 /* 967 * Nul-out trailing slashes (e.g., "foo///" -> "foo"). 968 * 969 * This must be done before VOP_LOOKUP() because some fs's don't know 970 * about trailing slashes. Remember if there were trailing slashes to 971 * handle symlinks, existing non-directories and non-existing files 972 * that won't be directories specially later. 973 */ 974 MPASS(ndp->ni_pathlen >= 2); 975 lastchar = &cnp->cn_nameptr[ndp->ni_pathlen - 2]; 976 if (*lastchar == '/') { 977 while (lastchar >= cnp->cn_pnbuf) { 978 *lastchar = '\0'; 979 lastchar--; 980 ndp->ni_pathlen--; 981 if (*lastchar != '/') { 982 break; 983 } 984 } 985 cnp->cn_flags |= TRAILINGSLASH; 986 } 987 988 /* 989 * We use shared locks until we hit the parent of the last cn then 990 * we adjust based on the requesting flags. 991 */ 992 vn_lock(dp, 993 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY, 994 cnp->cn_flags)); 995 996 dirloop: 997 /* 998 * Search a new directory. 999 * 1000 * The last component of the filename is left accessible via 1001 * cnp->cn_nameptr for callers that need the name. Callers needing 1002 * the name set the SAVENAME flag. When done, they assume 1003 * responsibility for freeing the pathname buffer. 1004 * 1005 * Store / as a temporary sentinel so that we only have one character 1006 * to test for. Pathnames tend to be short so this should not be 1007 * resulting in cache misses. 1008 */ 1009 nulchar = &cnp->cn_nameptr[ndp->ni_pathlen - 1]; 1010 KASSERT(*nulchar == '\0', 1011 ("%s: expected nul at %p; string [%s]\n", __func__, nulchar, 1012 cnp->cn_pnbuf)); 1013 *nulchar = '/'; 1014 for (cp = cnp->cn_nameptr; *cp != '/'; cp++) { 1015 KASSERT(*cp != '\0', 1016 ("%s: encountered unexpected nul; string [%s]\n", __func__, 1017 cnp->cn_nameptr)); 1018 continue; 1019 } 1020 *nulchar = '\0'; 1021 cnp->cn_namelen = cp - cnp->cn_nameptr; 1022 if (__predict_false(cnp->cn_namelen > NAME_MAX)) { 1023 error = ENAMETOOLONG; 1024 goto bad; 1025 } 1026 #ifdef NAMEI_DIAGNOSTIC 1027 { char c = *cp; 1028 *cp = '\0'; 1029 printf("{%s}: ", cnp->cn_nameptr); 1030 *cp = c; } 1031 #endif 1032 prev_ni_pathlen = ndp->ni_pathlen; 1033 ndp->ni_pathlen -= cnp->cn_namelen; 1034 KASSERT(ndp->ni_pathlen <= PATH_MAX, 1035 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen)); 1036 prev_ni_next = ndp->ni_next; 1037 ndp->ni_next = cp; 1038 1039 /* 1040 * Something else should be clearing this. 1041 */ 1042 cnp->cn_flags &= ~(ISDOTDOT|ISLASTCN); 1043 1044 cnp->cn_flags |= MAKEENTRY; 1045 if (*cp == '\0' && docache == 0) 1046 cnp->cn_flags &= ~MAKEENTRY; 1047 if (cnp->cn_namelen == 2 && 1048 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 1049 cnp->cn_flags |= ISDOTDOT; 1050 if (*ndp->ni_next == 0) { 1051 cnp->cn_flags |= ISLASTCN; 1052 1053 if (__predict_false(cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 1054 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))) { 1055 error = EINVAL; 1056 goto bad; 1057 } 1058 } 1059 1060 nameicap_tracker_add(ndp, dp); 1061 1062 /* 1063 * Make sure degenerate names don't get here, their handling was 1064 * previously found in this spot. 1065 */ 1066 MPASS(cnp->cn_nameptr[0] != '\0'); 1067 1068 /* 1069 * Handle "..": five special cases. 1070 * 0. If doing a capability lookup and lookup_cap_dotdot is 1071 * disabled, return ENOTCAPABLE. 1072 * 1. Return an error if this is the last component of 1073 * the name and the operation is DELETE or RENAME. 1074 * 2. If at root directory (e.g. after chroot) 1075 * or at absolute root directory 1076 * then ignore it so can't get out. 1077 * 3. If this vnode is the root of a mounted 1078 * filesystem, then replace it with the 1079 * vnode which was mounted on so we take the 1080 * .. in the other filesystem. 1081 * 4. If the vnode is the top directory of 1082 * the jail or chroot, don't let them out. 1083 * 5. If doing a capability lookup and lookup_cap_dotdot is 1084 * enabled, return ENOTCAPABLE if the lookup would escape 1085 * from the initial file descriptor directory. Checks are 1086 * done by ensuring that namei() already traversed the 1087 * result of dotdot lookup. 1088 */ 1089 if (cnp->cn_flags & ISDOTDOT) { 1090 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT)) 1091 == NI_LCF_STRICTRELATIVE) { 1092 #ifdef KTRACE 1093 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1094 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1095 #endif 1096 error = ENOTCAPABLE; 1097 goto bad; 1098 } 1099 if ((cnp->cn_flags & ISLASTCN) != 0 && 1100 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1101 error = EINVAL; 1102 goto bad; 1103 } 1104 for (;;) { 1105 for (pr = cnp->cn_cred->cr_prison; pr != NULL; 1106 pr = pr->pr_parent) 1107 if (dp == pr->pr_root) 1108 break; 1109 if (dp == ndp->ni_rootdir || 1110 dp == ndp->ni_topdir || 1111 dp == rootvnode || 1112 pr != NULL || 1113 ((dp->v_vflag & VV_ROOT) != 0 && 1114 (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 1115 ndp->ni_dvp = dp; 1116 ndp->ni_vp = dp; 1117 VREF(dp); 1118 goto nextname; 1119 } 1120 if ((dp->v_vflag & VV_ROOT) == 0) 1121 break; 1122 if (VN_IS_DOOMED(dp)) { /* forced unmount */ 1123 error = ENOENT; 1124 goto bad; 1125 } 1126 tdp = dp; 1127 dp = dp->v_mount->mnt_vnodecovered; 1128 VREF(dp); 1129 vput(tdp); 1130 vn_lock(dp, 1131 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 1132 LK_RETRY, ISDOTDOT)); 1133 error = nameicap_check_dotdot(ndp, dp); 1134 if (error != 0) { 1135 #ifdef KTRACE 1136 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1137 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1138 #endif 1139 goto bad; 1140 } 1141 } 1142 } 1143 1144 /* 1145 * We now have a segment name to search for, and a directory to search. 1146 */ 1147 unionlookup: 1148 #ifdef MAC 1149 error = mac_vnode_check_lookup(cnp->cn_cred, dp, cnp); 1150 if (__predict_false(error)) 1151 goto bad; 1152 #endif 1153 ndp->ni_dvp = dp; 1154 ndp->ni_vp = NULL; 1155 ASSERT_VOP_LOCKED(dp, "lookup"); 1156 /* 1157 * If we have a shared lock we may need to upgrade the lock for the 1158 * last operation. 1159 */ 1160 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) && 1161 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED) 1162 vn_lock(dp, LK_UPGRADE|LK_RETRY); 1163 if (VN_IS_DOOMED(dp)) { 1164 error = ENOENT; 1165 goto bad; 1166 } 1167 /* 1168 * If we're looking up the last component and we need an exclusive 1169 * lock, adjust our lkflags. 1170 */ 1171 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 1172 cnp->cn_lkflags = LK_EXCLUSIVE; 1173 #ifdef NAMEI_DIAGNOSTIC 1174 vn_printf(dp, "lookup in "); 1175 #endif 1176 lkflags_save = cnp->cn_lkflags; 1177 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags, 1178 cnp->cn_flags); 1179 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp); 1180 cnp->cn_lkflags = lkflags_save; 1181 if (error != 0) { 1182 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 1183 #ifdef NAMEI_DIAGNOSTIC 1184 printf("not found\n"); 1185 #endif 1186 if ((error == ENOENT) && 1187 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 1188 (dp->v_mount->mnt_flag & MNT_UNION)) { 1189 tdp = dp; 1190 dp = dp->v_mount->mnt_vnodecovered; 1191 VREF(dp); 1192 vput(tdp); 1193 vn_lock(dp, 1194 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 1195 LK_RETRY, cnp->cn_flags)); 1196 nameicap_tracker_add(ndp, dp); 1197 goto unionlookup; 1198 } 1199 1200 if (error == ERELOOKUP) { 1201 vref(dp); 1202 ndp->ni_vp = dp; 1203 error = 0; 1204 relookup = 1; 1205 goto good; 1206 } 1207 1208 if (error != EJUSTRETURN) 1209 goto bad; 1210 /* 1211 * At this point, we know we're at the end of the 1212 * pathname. If creating / renaming, we can consider 1213 * allowing the file or directory to be created / renamed, 1214 * provided we're not on a read-only filesystem. 1215 */ 1216 if (rdonly) { 1217 error = EROFS; 1218 goto bad; 1219 } 1220 /* trailing slash only allowed for directories */ 1221 if ((cnp->cn_flags & TRAILINGSLASH) && 1222 !(cnp->cn_flags & WILLBEDIR)) { 1223 error = ENOENT; 1224 goto bad; 1225 } 1226 if ((cnp->cn_flags & LOCKPARENT) == 0) 1227 VOP_UNLOCK(dp); 1228 /* 1229 * We return with ni_vp NULL to indicate that the entry 1230 * doesn't currently exist, leaving a pointer to the 1231 * (possibly locked) directory vnode in ndp->ni_dvp. 1232 */ 1233 if (cnp->cn_flags & SAVESTART) { 1234 ndp->ni_startdir = ndp->ni_dvp; 1235 VREF(ndp->ni_startdir); 1236 } 1237 goto success; 1238 } 1239 1240 good: 1241 #ifdef NAMEI_DIAGNOSTIC 1242 printf("found\n"); 1243 #endif 1244 dp = ndp->ni_vp; 1245 1246 /* 1247 * Check to see if the vnode has been mounted on; 1248 * if so find the root of the mounted filesystem. 1249 */ 1250 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 1251 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 1252 if (vfs_busy(mp, 0)) 1253 continue; 1254 vput(dp); 1255 if (dp != ndp->ni_dvp) 1256 vput(ndp->ni_dvp); 1257 else 1258 vrele(ndp->ni_dvp); 1259 vrefact(vp_crossmp); 1260 ndp->ni_dvp = vp_crossmp; 1261 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags, 1262 cnp->cn_flags), &tdp); 1263 vfs_unbusy(mp); 1264 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 1265 panic("vp_crossmp exclusively locked or reclaimed"); 1266 if (error) { 1267 dpunlocked = 1; 1268 goto bad2; 1269 } 1270 ndp->ni_vp = dp = tdp; 1271 } 1272 1273 /* 1274 * Check for symbolic link 1275 */ 1276 if ((dp->v_type == VLNK) && 1277 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 1278 *ndp->ni_next == '/')) { 1279 cnp->cn_flags |= ISSYMLINK; 1280 if (VN_IS_DOOMED(dp)) { 1281 /* 1282 * We can't know whether the directory was mounted with 1283 * NOSYMFOLLOW, so we can't follow safely. 1284 */ 1285 error = ENOENT; 1286 goto bad2; 1287 } 1288 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 1289 error = EACCES; 1290 goto bad2; 1291 } 1292 /* 1293 * Symlink code always expects an unlocked dvp. 1294 */ 1295 if (ndp->ni_dvp != ndp->ni_vp) { 1296 VOP_UNLOCK(ndp->ni_dvp); 1297 ni_dvp_unlocked = 1; 1298 } 1299 goto success; 1300 } 1301 1302 nextname: 1303 /* 1304 * Not a symbolic link that we will follow. Continue with the 1305 * next component if there is any; otherwise, we're done. 1306 */ 1307 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 1308 ("lookup: invalid path state.")); 1309 if (relookup) { 1310 relookup = 0; 1311 ndp->ni_pathlen = prev_ni_pathlen; 1312 ndp->ni_next = prev_ni_next; 1313 if (ndp->ni_dvp != dp) 1314 vput(ndp->ni_dvp); 1315 else 1316 vrele(ndp->ni_dvp); 1317 goto dirloop; 1318 } 1319 if (cnp->cn_flags & ISDOTDOT) { 1320 error = nameicap_check_dotdot(ndp, ndp->ni_vp); 1321 if (error != 0) { 1322 #ifdef KTRACE 1323 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1324 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1325 #endif 1326 goto bad2; 1327 } 1328 } 1329 if (*ndp->ni_next == '/') { 1330 cnp->cn_nameptr = ndp->ni_next; 1331 while (*cnp->cn_nameptr == '/') { 1332 cnp->cn_nameptr++; 1333 ndp->ni_pathlen--; 1334 } 1335 if (ndp->ni_dvp != dp) 1336 vput(ndp->ni_dvp); 1337 else 1338 vrele(ndp->ni_dvp); 1339 goto dirloop; 1340 } 1341 /* 1342 * If we're processing a path with a trailing slash, 1343 * check that the end result is a directory. 1344 */ 1345 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 1346 error = ENOTDIR; 1347 goto bad2; 1348 } 1349 /* 1350 * Disallow directory write attempts on read-only filesystems. 1351 */ 1352 if (rdonly && 1353 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1354 error = EROFS; 1355 goto bad2; 1356 } 1357 if (cnp->cn_flags & SAVESTART) { 1358 ndp->ni_startdir = ndp->ni_dvp; 1359 VREF(ndp->ni_startdir); 1360 } 1361 if (!wantparent) { 1362 ni_dvp_unlocked = 2; 1363 if (ndp->ni_dvp != dp) 1364 vput(ndp->ni_dvp); 1365 else 1366 vrele(ndp->ni_dvp); 1367 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 1368 VOP_UNLOCK(ndp->ni_dvp); 1369 ni_dvp_unlocked = 1; 1370 } 1371 1372 if (cnp->cn_flags & AUDITVNODE1) 1373 AUDIT_ARG_VNODE1(dp); 1374 else if (cnp->cn_flags & AUDITVNODE2) 1375 AUDIT_ARG_VNODE2(dp); 1376 1377 if ((cnp->cn_flags & LOCKLEAF) == 0) 1378 VOP_UNLOCK(dp); 1379 success: 1380 /* 1381 * FIXME: for lookups which only cross a mount point to fetch the 1382 * root vnode, ni_dvp will be set to vp_crossmp. This can be a problem 1383 * if either WANTPARENT or LOCKPARENT is set. 1384 */ 1385 /* 1386 * Because of shared lookup we may have the vnode shared locked, but 1387 * the caller may want it to be exclusively locked. 1388 */ 1389 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 1390 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 1391 vn_lock(dp, LK_UPGRADE | LK_RETRY); 1392 if (VN_IS_DOOMED(dp)) { 1393 error = ENOENT; 1394 goto bad2; 1395 } 1396 } 1397 success_right_lock: 1398 if (ndp->ni_vp != NULL) { 1399 if ((cnp->cn_flags & ISDOTDOT) == 0) 1400 nameicap_tracker_add(ndp, ndp->ni_vp); 1401 if ((cnp->cn_flags & (FAILIFEXISTS | ISSYMLINK)) == FAILIFEXISTS) 1402 return (vfs_lookup_failifexists(ndp)); 1403 } 1404 return (0); 1405 1406 bad2: 1407 if (ni_dvp_unlocked != 2) { 1408 if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 1409 vput(ndp->ni_dvp); 1410 else 1411 vrele(ndp->ni_dvp); 1412 } 1413 bad: 1414 if (!dpunlocked) 1415 vput(dp); 1416 bad_unlocked: 1417 ndp->ni_vp = NULL; 1418 return (error); 1419 } 1420 1421 /* 1422 * relookup - lookup a path name component 1423 * Used by lookup to re-acquire things. 1424 */ 1425 int 1426 vfs_relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 1427 { 1428 struct vnode *dp = NULL; /* the directory we are searching */ 1429 int rdonly; /* lookup read-only flag bit */ 1430 int error = 0; 1431 1432 KASSERT(cnp->cn_flags & ISLASTCN, 1433 ("relookup: Not given last component.")); 1434 /* 1435 * Setup: break out flag bits into variables. 1436 */ 1437 KASSERT((cnp->cn_flags & (LOCKPARENT | WANTPARENT)) != 0, 1438 ("relookup: parent not wanted")); 1439 rdonly = cnp->cn_flags & RDONLY; 1440 cnp->cn_flags &= ~ISSYMLINK; 1441 dp = dvp; 1442 cnp->cn_lkflags = LK_EXCLUSIVE; 1443 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 1444 1445 /* 1446 * Search a new directory. 1447 * 1448 * The last component of the filename is left accessible via 1449 * cnp->cn_nameptr for callers that need the name. Callers needing 1450 * the name set the SAVENAME flag. When done, they assume 1451 * responsibility for freeing the pathname buffer. 1452 */ 1453 #ifdef NAMEI_DIAGNOSTIC 1454 printf("{%s}: ", cnp->cn_nameptr); 1455 #endif 1456 1457 /* 1458 * Check for "" which represents the root directory after slash 1459 * removal. 1460 */ 1461 if (cnp->cn_nameptr[0] == '\0') { 1462 /* 1463 * Support only LOOKUP for "/" because lookup() 1464 * can't succeed for CREATE, DELETE and RENAME. 1465 */ 1466 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 1467 KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 1468 1469 if (!(cnp->cn_flags & LOCKLEAF)) 1470 VOP_UNLOCK(dp); 1471 *vpp = dp; 1472 /* XXX This should probably move to the top of function. */ 1473 if (cnp->cn_flags & SAVESTART) 1474 panic("lookup: SAVESTART"); 1475 return (0); 1476 } 1477 1478 if (cnp->cn_flags & ISDOTDOT) 1479 panic ("relookup: lookup on dot-dot"); 1480 1481 /* 1482 * We now have a segment name to search for, and a directory to search. 1483 */ 1484 #ifdef NAMEI_DIAGNOSTIC 1485 vn_printf(dp, "search in "); 1486 #endif 1487 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 1488 KASSERT(*vpp == NULL, ("leaf should be empty")); 1489 if (error != EJUSTRETURN) 1490 goto bad; 1491 /* 1492 * If creating and at end of pathname, then can consider 1493 * allowing file to be created. 1494 */ 1495 if (rdonly) { 1496 error = EROFS; 1497 goto bad; 1498 } 1499 /* ASSERT(dvp == ndp->ni_startdir) */ 1500 if (cnp->cn_flags & SAVESTART) 1501 VREF(dvp); 1502 if ((cnp->cn_flags & LOCKPARENT) == 0) 1503 VOP_UNLOCK(dp); 1504 /* 1505 * We return with ni_vp NULL to indicate that the entry 1506 * doesn't currently exist, leaving a pointer to the 1507 * (possibly locked) directory vnode in ndp->ni_dvp. 1508 */ 1509 return (0); 1510 } 1511 1512 dp = *vpp; 1513 1514 /* 1515 * Disallow directory write attempts on read-only filesystems. 1516 */ 1517 if (rdonly && 1518 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1519 if (dvp == dp) 1520 vrele(dvp); 1521 else 1522 vput(dvp); 1523 error = EROFS; 1524 goto bad; 1525 } 1526 /* 1527 * Set the parent lock/ref state to the requested state. 1528 */ 1529 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) 1530 VOP_UNLOCK(dvp); 1531 /* 1532 * Check for symbolic link 1533 */ 1534 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1535 ("relookup: symlink found.\n")); 1536 1537 /* ASSERT(dvp == ndp->ni_startdir) */ 1538 if (cnp->cn_flags & SAVESTART) 1539 VREF(dvp); 1540 1541 if ((cnp->cn_flags & LOCKLEAF) == 0) 1542 VOP_UNLOCK(dp); 1543 return (0); 1544 bad: 1545 vput(dp); 1546 *vpp = NULL; 1547 return (error); 1548 } 1549 1550 /* 1551 * Free data allocated by namei(); see namei(9) for details. 1552 */ 1553 void 1554 NDFREE_PNBUF(struct nameidata *ndp) 1555 { 1556 1557 if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) { 1558 MPASS((ndp->ni_cnd.cn_flags & (SAVENAME | SAVESTART)) != 0); 1559 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 1560 ndp->ni_cnd.cn_flags &= ~HASBUF; 1561 } 1562 } 1563 1564 /* 1565 * NDFREE_PNBUF replacement for callers that know there is no buffer. 1566 * 1567 * This is a hack. Preferably the VFS layer would not produce anything more 1568 * than it was asked to do. Unfortunately several non-LOOKUP cases can add the 1569 * HASBUF flag to the result. Even then an interface could be implemented where 1570 * the caller specifies what they expected to see in the result and what they 1571 * are going to take care of. 1572 * 1573 * In the meantime provide this kludge as a trivial replacement for NDFREE_PNBUF 1574 * calls scattered throughout the kernel where we know for a fact the flag must not 1575 * be seen. 1576 */ 1577 #ifdef INVARIANTS 1578 void 1579 NDFREE_NOTHING(struct nameidata *ndp) 1580 { 1581 struct componentname *cnp; 1582 1583 cnp = &ndp->ni_cnd; 1584 KASSERT(cnp->cn_nameiop == LOOKUP, ("%s: got non-LOOKUP op %d\n", 1585 __func__, cnp->cn_nameiop)); 1586 KASSERT((cnp->cn_flags & (SAVENAME | HASBUF)) == 0, 1587 ("%s: bad flags \%" PRIx64 "\n", __func__, cnp->cn_flags)); 1588 } 1589 #endif 1590 1591 void 1592 (NDFREE)(struct nameidata *ndp, const u_int flags) 1593 { 1594 int unlock_dvp; 1595 int unlock_vp; 1596 1597 unlock_dvp = 0; 1598 unlock_vp = 0; 1599 1600 if (!(flags & NDF_NO_FREE_PNBUF)) { 1601 NDFREE_PNBUF(ndp); 1602 } 1603 if (!(flags & NDF_NO_VP_UNLOCK) && 1604 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 1605 unlock_vp = 1; 1606 if (!(flags & NDF_NO_DVP_UNLOCK) && 1607 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 1608 ndp->ni_dvp != ndp->ni_vp) 1609 unlock_dvp = 1; 1610 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 1611 if (unlock_vp) { 1612 vput(ndp->ni_vp); 1613 unlock_vp = 0; 1614 } else 1615 vrele(ndp->ni_vp); 1616 ndp->ni_vp = NULL; 1617 } 1618 if (unlock_vp) 1619 VOP_UNLOCK(ndp->ni_vp); 1620 if (!(flags & NDF_NO_DVP_RELE) && 1621 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 1622 if (unlock_dvp) { 1623 vput(ndp->ni_dvp); 1624 unlock_dvp = 0; 1625 } else 1626 vrele(ndp->ni_dvp); 1627 ndp->ni_dvp = NULL; 1628 } 1629 if (unlock_dvp) 1630 VOP_UNLOCK(ndp->ni_dvp); 1631 if (!(flags & NDF_NO_STARTDIR_RELE) && 1632 (ndp->ni_cnd.cn_flags & SAVESTART)) { 1633 vrele(ndp->ni_startdir); 1634 ndp->ni_startdir = NULL; 1635 } 1636 } 1637 1638 #ifdef INVARIANTS 1639 /* 1640 * Validate the final state of ndp after the lookup. 1641 * 1642 * Historically filesystems were allowed to modify cn_flags. Most notably they 1643 * can add SAVENAME to the request, resulting in HASBUF and pushing subsequent 1644 * clean up to the consumer. In practice this seems to only concern != LOOKUP 1645 * operations. 1646 * 1647 * As a step towards stricter API contract this routine validates the state to 1648 * clean up. Note validation is a work in progress with the intent of becoming 1649 * stricter over time. 1650 */ 1651 #define NDMODIFYINGFLAGS (LOCKLEAF | LOCKPARENT | WANTPARENT | SAVENAME | SAVESTART | HASBUF) 1652 static void 1653 NDVALIDATE(struct nameidata *ndp) 1654 { 1655 struct componentname *cnp; 1656 uint64_t used, orig; 1657 1658 cnp = &ndp->ni_cnd; 1659 orig = cnp->cn_origflags; 1660 used = cnp->cn_flags; 1661 switch (cnp->cn_nameiop) { 1662 case LOOKUP: 1663 /* 1664 * For plain lookup we require strict conformance -- nothing 1665 * to clean up if it was not requested by the caller. 1666 */ 1667 orig &= NDMODIFYINGFLAGS; 1668 used &= NDMODIFYINGFLAGS; 1669 if ((orig & (SAVENAME | SAVESTART)) != 0) 1670 orig |= HASBUF; 1671 if (orig != used) { 1672 goto out_mismatch; 1673 } 1674 break; 1675 case CREATE: 1676 case DELETE: 1677 case RENAME: 1678 /* 1679 * Some filesystems set SAVENAME to provoke HASBUF, accommodate 1680 * for it until it gets fixed. 1681 */ 1682 orig &= NDMODIFYINGFLAGS; 1683 orig |= (SAVENAME | HASBUF); 1684 used &= NDMODIFYINGFLAGS; 1685 used |= (SAVENAME | HASBUF); 1686 if (orig != used) { 1687 goto out_mismatch; 1688 } 1689 break; 1690 } 1691 return; 1692 out_mismatch: 1693 panic("%s: mismatched flags for op %d: added %" PRIx64 ", " 1694 "removed %" PRIx64" (%" PRIx64" != %" PRIx64"; stored %" PRIx64" != %" PRIx64")", 1695 __func__, cnp->cn_nameiop, used & ~orig, orig &~ used, 1696 orig, used, cnp->cn_origflags, cnp->cn_flags); 1697 } 1698 #endif 1699 1700 /* 1701 * Determine if there is a suitable alternate filename under the specified 1702 * prefix for the specified path. If the create flag is set, then the 1703 * alternate prefix will be used so long as the parent directory exists. 1704 * This is used by the various compatibility ABIs so that Linux binaries prefer 1705 * files under /compat/linux for example. The chosen path (whether under 1706 * the prefix or under /) is returned in a kernel malloc'd buffer pointed 1707 * to by pathbuf. The caller is responsible for free'ing the buffer from 1708 * the M_TEMP bucket if one is returned. 1709 */ 1710 int 1711 kern_alternate_path(const char *prefix, const char *path, enum uio_seg pathseg, 1712 char **pathbuf, int create, int dirfd) 1713 { 1714 struct nameidata nd, ndroot; 1715 char *ptr, *buf, *cp; 1716 size_t len, sz; 1717 int error; 1718 1719 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1720 *pathbuf = buf; 1721 1722 /* Copy the prefix into the new pathname as a starting point. */ 1723 len = strlcpy(buf, prefix, MAXPATHLEN); 1724 if (len >= MAXPATHLEN) { 1725 *pathbuf = NULL; 1726 free(buf, M_TEMP); 1727 return (EINVAL); 1728 } 1729 sz = MAXPATHLEN - len; 1730 ptr = buf + len; 1731 1732 /* Append the filename to the prefix. */ 1733 if (pathseg == UIO_SYSSPACE) 1734 error = copystr(path, ptr, sz, &len); 1735 else 1736 error = copyinstr(path, ptr, sz, &len); 1737 1738 if (error) { 1739 *pathbuf = NULL; 1740 free(buf, M_TEMP); 1741 return (error); 1742 } 1743 1744 /* Only use a prefix with absolute pathnames. */ 1745 if (*ptr != '/') { 1746 error = EINVAL; 1747 goto keeporig; 1748 } 1749 1750 if (dirfd != AT_FDCWD) { 1751 /* 1752 * We want the original because the "prefix" is 1753 * included in the already opened dirfd. 1754 */ 1755 bcopy(ptr, buf, len); 1756 return (0); 1757 } 1758 1759 /* 1760 * We know that there is a / somewhere in this pathname. 1761 * Search backwards for it, to find the file's parent dir 1762 * to see if it exists in the alternate tree. If it does, 1763 * and we want to create a file (cflag is set). We don't 1764 * need to worry about the root comparison in this case. 1765 */ 1766 1767 if (create) { 1768 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1769 *cp = '\0'; 1770 1771 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf); 1772 error = namei(&nd); 1773 *cp = '/'; 1774 if (error != 0) 1775 goto keeporig; 1776 } else { 1777 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf); 1778 1779 error = namei(&nd); 1780 if (error != 0) 1781 goto keeporig; 1782 1783 /* 1784 * We now compare the vnode of the prefix to the one 1785 * vnode asked. If they resolve to be the same, then we 1786 * ignore the match so that the real root gets used. 1787 * This avoids the problem of traversing "../.." to find the 1788 * root directory and never finding it, because "/" resolves 1789 * to the emulation root directory. This is expensive :-( 1790 */ 1791 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix); 1792 1793 /* We shouldn't ever get an error from this namei(). */ 1794 error = namei(&ndroot); 1795 if (error == 0) { 1796 if (nd.ni_vp == ndroot.ni_vp) 1797 error = ENOENT; 1798 1799 NDFREE_PNBUF(&ndroot); 1800 vrele(ndroot.ni_vp); 1801 } 1802 } 1803 1804 NDFREE_PNBUF(&nd); 1805 vrele(nd.ni_vp); 1806 1807 keeporig: 1808 /* If there was an error, use the original path name. */ 1809 if (error) 1810 bcopy(ptr, buf, len); 1811 return (error); 1812 } 1813