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