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