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