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