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