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 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 486 KASSERT(cnp->cn_cred && td->td_proc, ("namei: bad cred/proc")); 487 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 488 ("namei: nameiop contaminated with flags")); 489 KASSERT((cnp->cn_flags & OPMASK) == 0, 490 ("namei: flags contaminated with nameiops")); 491 KASSERT((cnp->cn_flags & NAMEI_INTERNAL_FLAGS) == 0, 492 ("namei: unexpected flags: %" PRIx64 "\n", 493 cnp->cn_flags & NAMEI_INTERNAL_FLAGS)); 494 if (cnp->cn_flags & NOCACHE) 495 KASSERT(cnp->cn_nameiop != LOOKUP, 496 ("%s: NOCACHE passed with LOOKUP", __func__)); 497 MPASS(ndp->ni_startdir == NULL || ndp->ni_startdir->v_type == VDIR || 498 ndp->ni_startdir->v_type == VBAD); 499 500 ndp->ni_lcf = 0; 501 ndp->ni_vp = NULL; 502 503 /* 504 * Get a buffer for the name to be translated, and copy the 505 * name into the buffer. 506 */ 507 if ((cnp->cn_flags & HASBUF) == 0) 508 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 509 if (ndp->ni_segflg == UIO_SYSSPACE) 510 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 511 &ndp->ni_pathlen); 512 else 513 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, MAXPATHLEN, 514 &ndp->ni_pathlen); 515 516 if (__predict_false(error != 0)) { 517 namei_cleanup_cnp(cnp); 518 return (error); 519 } 520 521 /* 522 * Don't allow empty pathnames. 523 */ 524 if (__predict_false(*cnp->cn_pnbuf == '\0')) { 525 namei_cleanup_cnp(cnp); 526 return (ENOENT); 527 } 528 529 #ifdef KTRACE 530 if (KTRPOINT(td, KTR_NAMEI)) { 531 KASSERT(cnp->cn_thread == curthread, 532 ("namei not using curthread")); 533 ktrnamei(cnp->cn_pnbuf); 534 } 535 #endif 536 537 cnp->cn_nameptr = cnp->cn_pnbuf; 538 539 /* 540 * First try looking up the target without locking any vnodes. 541 * 542 * We may need to start from scratch or pick up where it left off. 543 */ 544 error = cache_fplookup(ndp, &status, &pwd); 545 switch (status) { 546 case CACHE_FPL_STATUS_UNSET: 547 __assert_unreachable(); 548 break; 549 case CACHE_FPL_STATUS_HANDLED: 550 return (error); 551 case CACHE_FPL_STATUS_PARTIAL: 552 TAILQ_INIT(&ndp->ni_cap_tracker); 553 dp = ndp->ni_startdir; 554 break; 555 case CACHE_FPL_STATUS_ABORTED: 556 TAILQ_INIT(&ndp->ni_cap_tracker); 557 error = namei_setup(ndp, &dp, &pwd); 558 if (error != 0) { 559 namei_cleanup_cnp(cnp); 560 return (error); 561 } 562 break; 563 } 564 565 ndp->ni_loopcnt = 0; 566 567 /* 568 * Locked lookup. 569 */ 570 for (;;) { 571 ndp->ni_startdir = dp; 572 error = lookup(ndp); 573 if (error != 0) 574 goto out; 575 /* 576 * If not a symbolic link, we're done. 577 */ 578 if ((cnp->cn_flags & ISSYMLINK) == 0) { 579 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) { 580 namei_cleanup_cnp(cnp); 581 } else 582 cnp->cn_flags |= HASBUF; 583 if ((ndp->ni_lcf & (NI_LCF_BENEATH_ABS | 584 NI_LCF_BENEATH_LATCHED)) == NI_LCF_BENEATH_ABS) { 585 NDFREE(ndp, 0); 586 error = ENOTCAPABLE; 587 } 588 nameicap_cleanup(ndp, true); 589 SDT_PROBE3(vfs, namei, lookup, return, error, 590 (error == 0 ? ndp->ni_vp : NULL), false); 591 pwd_drop(pwd); 592 return (error); 593 } 594 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 595 error = ELOOP; 596 break; 597 } 598 #ifdef MAC 599 if ((cnp->cn_flags & NOMACCHECK) == 0) { 600 error = mac_vnode_check_readlink(td->td_ucred, 601 ndp->ni_vp); 602 if (error != 0) 603 break; 604 } 605 #endif 606 if (ndp->ni_pathlen > 1) 607 cp = uma_zalloc(namei_zone, M_WAITOK); 608 else 609 cp = cnp->cn_pnbuf; 610 aiov.iov_base = cp; 611 aiov.iov_len = MAXPATHLEN; 612 auio.uio_iov = &aiov; 613 auio.uio_iovcnt = 1; 614 auio.uio_offset = 0; 615 auio.uio_rw = UIO_READ; 616 auio.uio_segflg = UIO_SYSSPACE; 617 auio.uio_td = td; 618 auio.uio_resid = MAXPATHLEN; 619 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 620 if (error != 0) { 621 if (ndp->ni_pathlen > 1) 622 uma_zfree(namei_zone, cp); 623 break; 624 } 625 linklen = MAXPATHLEN - auio.uio_resid; 626 if (linklen == 0) { 627 if (ndp->ni_pathlen > 1) 628 uma_zfree(namei_zone, cp); 629 error = ENOENT; 630 break; 631 } 632 if (linklen + ndp->ni_pathlen > MAXPATHLEN) { 633 if (ndp->ni_pathlen > 1) 634 uma_zfree(namei_zone, cp); 635 error = ENAMETOOLONG; 636 break; 637 } 638 if (ndp->ni_pathlen > 1) { 639 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 640 uma_zfree(namei_zone, cnp->cn_pnbuf); 641 cnp->cn_pnbuf = cp; 642 } else 643 cnp->cn_pnbuf[linklen] = '\0'; 644 ndp->ni_pathlen += linklen; 645 vput(ndp->ni_vp); 646 dp = ndp->ni_dvp; 647 /* 648 * Check if root directory should replace current directory. 649 */ 650 cnp->cn_nameptr = cnp->cn_pnbuf; 651 if (*(cnp->cn_nameptr) == '/') { 652 vrele(dp); 653 error = namei_handle_root(ndp, &dp); 654 if (error != 0) 655 goto out; 656 } 657 } 658 vput(ndp->ni_vp); 659 ndp->ni_vp = NULL; 660 vrele(ndp->ni_dvp); 661 out: 662 MPASS(error != 0); 663 namei_cleanup_cnp(cnp); 664 nameicap_cleanup(ndp, true); 665 SDT_PROBE3(vfs, namei, lookup, return, error, NULL, false); 666 pwd_drop(pwd); 667 return (error); 668 } 669 670 static int 671 compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags) 672 { 673 674 if (mp == NULL || ((lkflags & LK_SHARED) && 675 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) || 676 ((cnflags & ISDOTDOT) && 677 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) { 678 lkflags &= ~LK_SHARED; 679 lkflags |= LK_EXCLUSIVE; 680 } 681 lkflags |= LK_NODDLKTREAT; 682 return (lkflags); 683 } 684 685 static __inline int 686 needs_exclusive_leaf(struct mount *mp, int flags) 687 { 688 689 /* 690 * Intermediate nodes can use shared locks, we only need to 691 * force an exclusive lock for leaf nodes. 692 */ 693 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 694 return (0); 695 696 /* Always use exclusive locks if LOCKSHARED isn't set. */ 697 if (!(flags & LOCKSHARED)) 698 return (1); 699 700 /* 701 * For lookups during open(), if the mount point supports 702 * extended shared operations, then use a shared lock for the 703 * leaf node, otherwise use an exclusive lock. 704 */ 705 if ((flags & ISOPEN) != 0) 706 return (!MNT_EXTENDED_SHARED(mp)); 707 708 /* 709 * Lookup requests outside of open() that specify LOCKSHARED 710 * only need a shared lock on the leaf vnode. 711 */ 712 return (0); 713 } 714 715 /* 716 * Search a pathname. 717 * This is a very central and rather complicated routine. 718 * 719 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 720 * The starting directory is taken from ni_startdir. The pathname is 721 * descended until done, or a symbolic link is encountered. The variable 722 * ni_more is clear if the path is completed; it is set to one if a 723 * symbolic link needing interpretation is encountered. 724 * 725 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 726 * whether the name is to be looked up, created, renamed, or deleted. 727 * When CREATE, RENAME, or DELETE is specified, information usable in 728 * creating, renaming, or deleting a directory entry may be calculated. 729 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 730 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 731 * returned unlocked. Otherwise the parent directory is not returned. If 732 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 733 * the target is returned locked, otherwise it is returned unlocked. 734 * When creating or renaming and LOCKPARENT is specified, the target may not 735 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 736 * 737 * Overall outline of lookup: 738 * 739 * dirloop: 740 * identify next component of name at ndp->ni_ptr 741 * handle degenerate case where name is null string 742 * if .. and crossing mount points and on mounted filesys, find parent 743 * call VOP_LOOKUP routine for next component name 744 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 745 * component vnode returned in ni_vp (if it exists), locked. 746 * if result vnode is mounted on and crossing mount points, 747 * find mounted on vnode 748 * if more components of name, do next level at dirloop 749 * return the answer in ni_vp, locked if LOCKLEAF set 750 * if LOCKPARENT set, return locked parent in ni_dvp 751 * if WANTPARENT set, return unlocked parent in ni_dvp 752 */ 753 int 754 lookup(struct nameidata *ndp) 755 { 756 char *cp; /* pointer into pathname argument */ 757 char *prev_ni_next; /* saved ndp->ni_next */ 758 struct vnode *dp = NULL; /* the directory we are searching */ 759 struct vnode *tdp; /* saved dp */ 760 struct mount *mp; /* mount table entry */ 761 struct prison *pr; 762 size_t prev_ni_pathlen; /* saved ndp->ni_pathlen */ 763 int docache; /* == 0 do not cache last component */ 764 int wantparent; /* 1 => wantparent or lockparent flag */ 765 int rdonly; /* lookup read-only flag bit */ 766 int error = 0; 767 int dpunlocked = 0; /* dp has already been unlocked */ 768 int relookup = 0; /* do not consume the path component */ 769 struct componentname *cnp = &ndp->ni_cnd; 770 int lkflags_save; 771 int ni_dvp_unlocked; 772 773 /* 774 * Setup: break out flag bits into variables. 775 */ 776 ni_dvp_unlocked = 0; 777 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 778 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 779 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 780 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 781 if (cnp->cn_nameiop == DELETE || 782 (wantparent && cnp->cn_nameiop != CREATE && 783 cnp->cn_nameiop != LOOKUP)) 784 docache = 0; 785 rdonly = cnp->cn_flags & RDONLY; 786 cnp->cn_flags &= ~ISSYMLINK; 787 ndp->ni_dvp = NULL; 788 /* 789 * We use shared locks until we hit the parent of the last cn then 790 * we adjust based on the requesting flags. 791 */ 792 cnp->cn_lkflags = LK_SHARED; 793 dp = ndp->ni_startdir; 794 ndp->ni_startdir = NULLVP; 795 vn_lock(dp, 796 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY, 797 cnp->cn_flags)); 798 799 dirloop: 800 /* 801 * Search a new directory. 802 * 803 * The last component of the filename is left accessible via 804 * cnp->cn_nameptr for callers that need the name. Callers needing 805 * the name set the SAVENAME flag. When done, they assume 806 * responsibility for freeing the pathname buffer. 807 */ 808 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 809 continue; 810 cnp->cn_namelen = cp - cnp->cn_nameptr; 811 if (cnp->cn_namelen > NAME_MAX) { 812 error = ENAMETOOLONG; 813 goto bad; 814 } 815 #ifdef NAMEI_DIAGNOSTIC 816 { char c = *cp; 817 *cp = '\0'; 818 printf("{%s}: ", cnp->cn_nameptr); 819 *cp = c; } 820 #endif 821 prev_ni_pathlen = ndp->ni_pathlen; 822 ndp->ni_pathlen -= cnp->cn_namelen; 823 KASSERT(ndp->ni_pathlen <= PATH_MAX, 824 ("%s: ni_pathlen underflow to %zd\n", __func__, ndp->ni_pathlen)); 825 prev_ni_next = ndp->ni_next; 826 ndp->ni_next = cp; 827 828 /* 829 * Replace multiple slashes by a single slash and trailing slashes 830 * by a null. This must be done before VOP_LOOKUP() because some 831 * fs's don't know about trailing slashes. Remember if there were 832 * trailing slashes to handle symlinks, existing non-directories 833 * and non-existing files that won't be directories specially later. 834 */ 835 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 836 cp++; 837 ndp->ni_pathlen--; 838 if (*cp == '\0') { 839 *ndp->ni_next = '\0'; 840 cnp->cn_flags |= TRAILINGSLASH; 841 } 842 } 843 ndp->ni_next = cp; 844 845 cnp->cn_flags |= MAKEENTRY; 846 if (*cp == '\0' && docache == 0) 847 cnp->cn_flags &= ~MAKEENTRY; 848 if (cnp->cn_namelen == 2 && 849 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 850 cnp->cn_flags |= ISDOTDOT; 851 else 852 cnp->cn_flags &= ~ISDOTDOT; 853 if (*ndp->ni_next == 0) 854 cnp->cn_flags |= ISLASTCN; 855 else 856 cnp->cn_flags &= ~ISLASTCN; 857 858 if ((cnp->cn_flags & ISLASTCN) != 0 && 859 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 860 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 861 error = EINVAL; 862 goto bad; 863 } 864 865 nameicap_tracker_add(ndp, dp); 866 867 /* 868 * Check for degenerate name (e.g. / or "") 869 * which is a way of talking about a directory, 870 * e.g. like "/." or ".". 871 */ 872 if (cnp->cn_nameptr[0] == '\0') { 873 if (dp->v_type != VDIR) { 874 error = ENOTDIR; 875 goto bad; 876 } 877 if (cnp->cn_nameiop != LOOKUP) { 878 error = EISDIR; 879 goto bad; 880 } 881 if (wantparent) { 882 ndp->ni_dvp = dp; 883 VREF(dp); 884 } 885 ndp->ni_vp = dp; 886 887 if (cnp->cn_flags & AUDITVNODE1) 888 AUDIT_ARG_VNODE1(dp); 889 else if (cnp->cn_flags & AUDITVNODE2) 890 AUDIT_ARG_VNODE2(dp); 891 892 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 893 VOP_UNLOCK(dp); 894 /* XXX This should probably move to the top of function. */ 895 if (cnp->cn_flags & SAVESTART) 896 panic("lookup: SAVESTART"); 897 goto success; 898 } 899 900 /* 901 * Handle "..": five special cases. 902 * 0. If doing a capability lookup and lookup_cap_dotdot is 903 * disabled, return ENOTCAPABLE. 904 * 1. Return an error if this is the last component of 905 * the name and the operation is DELETE or RENAME. 906 * 2. If at root directory (e.g. after chroot) 907 * or at absolute root directory 908 * then ignore it so can't get out. 909 * 3. If this vnode is the root of a mounted 910 * filesystem, then replace it with the 911 * vnode which was mounted on so we take the 912 * .. in the other filesystem. 913 * 4. If the vnode is the top directory of 914 * the jail or chroot, don't let them out. 915 * 5. If doing a capability lookup and lookup_cap_dotdot is 916 * enabled, return ENOTCAPABLE if the lookup would escape 917 * from the initial file descriptor directory. Checks are 918 * done by ensuring that namei() already traversed the 919 * result of dotdot lookup. 920 */ 921 if (cnp->cn_flags & ISDOTDOT) { 922 if ((ndp->ni_lcf & (NI_LCF_STRICTRELATIVE | NI_LCF_CAP_DOTDOT)) 923 == NI_LCF_STRICTRELATIVE) { 924 #ifdef KTRACE 925 if (KTRPOINT(curthread, KTR_CAPFAIL)) 926 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 927 #endif 928 error = ENOTCAPABLE; 929 goto bad; 930 } 931 if ((cnp->cn_flags & ISLASTCN) != 0 && 932 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 933 error = EINVAL; 934 goto bad; 935 } 936 for (;;) { 937 for (pr = cnp->cn_cred->cr_prison; pr != NULL; 938 pr = pr->pr_parent) 939 if (dp == pr->pr_root) 940 break; 941 if (dp == ndp->ni_rootdir || 942 dp == ndp->ni_topdir || 943 dp == rootvnode || 944 pr != NULL || 945 ((dp->v_vflag & VV_ROOT) != 0 && 946 (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 947 ndp->ni_dvp = dp; 948 ndp->ni_vp = dp; 949 VREF(dp); 950 goto nextname; 951 } 952 if ((dp->v_vflag & VV_ROOT) == 0) 953 break; 954 if (VN_IS_DOOMED(dp)) { /* forced unmount */ 955 error = ENOENT; 956 goto bad; 957 } 958 tdp = dp; 959 dp = dp->v_mount->mnt_vnodecovered; 960 VREF(dp); 961 vput(tdp); 962 vn_lock(dp, 963 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 964 LK_RETRY, ISDOTDOT)); 965 error = nameicap_check_dotdot(ndp, dp); 966 if (error != 0) { 967 #ifdef KTRACE 968 if (KTRPOINT(curthread, KTR_CAPFAIL)) 969 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 970 #endif 971 goto bad; 972 } 973 } 974 } 975 976 /* 977 * We now have a segment name to search for, and a directory to search. 978 */ 979 unionlookup: 980 #ifdef MAC 981 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, cnp); 982 if (error) 983 goto bad; 984 #endif 985 ndp->ni_dvp = dp; 986 ndp->ni_vp = NULL; 987 ASSERT_VOP_LOCKED(dp, "lookup"); 988 /* 989 * If we have a shared lock we may need to upgrade the lock for the 990 * last operation. 991 */ 992 if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) && 993 dp != vp_crossmp && VOP_ISLOCKED(dp) == LK_SHARED) 994 vn_lock(dp, LK_UPGRADE|LK_RETRY); 995 if (VN_IS_DOOMED(dp)) { 996 error = ENOENT; 997 goto bad; 998 } 999 /* 1000 * If we're looking up the last component and we need an exclusive 1001 * lock, adjust our lkflags. 1002 */ 1003 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 1004 cnp->cn_lkflags = LK_EXCLUSIVE; 1005 #ifdef NAMEI_DIAGNOSTIC 1006 vn_printf(dp, "lookup in "); 1007 #endif 1008 lkflags_save = cnp->cn_lkflags; 1009 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags, 1010 cnp->cn_flags); 1011 error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp); 1012 cnp->cn_lkflags = lkflags_save; 1013 if (error != 0) { 1014 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 1015 #ifdef NAMEI_DIAGNOSTIC 1016 printf("not found\n"); 1017 #endif 1018 if ((error == ENOENT) && 1019 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 1020 (dp->v_mount->mnt_flag & MNT_UNION)) { 1021 tdp = dp; 1022 dp = dp->v_mount->mnt_vnodecovered; 1023 VREF(dp); 1024 vput(tdp); 1025 vn_lock(dp, 1026 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 1027 LK_RETRY, cnp->cn_flags)); 1028 nameicap_tracker_add(ndp, dp); 1029 goto unionlookup; 1030 } 1031 1032 if (error == ERELOOKUP) { 1033 vref(dp); 1034 ndp->ni_vp = dp; 1035 error = 0; 1036 relookup = 1; 1037 goto good; 1038 } 1039 1040 if (error != EJUSTRETURN) 1041 goto bad; 1042 /* 1043 * At this point, we know we're at the end of the 1044 * pathname. If creating / renaming, we can consider 1045 * allowing the file or directory to be created / renamed, 1046 * provided we're not on a read-only filesystem. 1047 */ 1048 if (rdonly) { 1049 error = EROFS; 1050 goto bad; 1051 } 1052 /* trailing slash only allowed for directories */ 1053 if ((cnp->cn_flags & TRAILINGSLASH) && 1054 !(cnp->cn_flags & WILLBEDIR)) { 1055 error = ENOENT; 1056 goto bad; 1057 } 1058 if ((cnp->cn_flags & LOCKPARENT) == 0) 1059 VOP_UNLOCK(dp); 1060 /* 1061 * We return with ni_vp NULL to indicate that the entry 1062 * doesn't currently exist, leaving a pointer to the 1063 * (possibly locked) directory vnode in ndp->ni_dvp. 1064 */ 1065 if (cnp->cn_flags & SAVESTART) { 1066 ndp->ni_startdir = ndp->ni_dvp; 1067 VREF(ndp->ni_startdir); 1068 } 1069 goto success; 1070 } 1071 1072 good: 1073 #ifdef NAMEI_DIAGNOSTIC 1074 printf("found\n"); 1075 #endif 1076 dp = ndp->ni_vp; 1077 1078 /* 1079 * Check to see if the vnode has been mounted on; 1080 * if so find the root of the mounted filesystem. 1081 */ 1082 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 1083 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 1084 if (vfs_busy(mp, 0)) 1085 continue; 1086 vput(dp); 1087 if (dp != ndp->ni_dvp) 1088 vput(ndp->ni_dvp); 1089 else 1090 vrele(ndp->ni_dvp); 1091 vrefact(vp_crossmp); 1092 ndp->ni_dvp = vp_crossmp; 1093 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags, 1094 cnp->cn_flags), &tdp); 1095 vfs_unbusy(mp); 1096 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 1097 panic("vp_crossmp exclusively locked or reclaimed"); 1098 if (error) { 1099 dpunlocked = 1; 1100 goto bad2; 1101 } 1102 ndp->ni_vp = dp = tdp; 1103 } 1104 1105 /* 1106 * Check for symbolic link 1107 */ 1108 if ((dp->v_type == VLNK) && 1109 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 1110 *ndp->ni_next == '/')) { 1111 cnp->cn_flags |= ISSYMLINK; 1112 if (VN_IS_DOOMED(dp)) { 1113 /* 1114 * We can't know whether the directory was mounted with 1115 * NOSYMFOLLOW, so we can't follow safely. 1116 */ 1117 error = ENOENT; 1118 goto bad2; 1119 } 1120 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 1121 error = EACCES; 1122 goto bad2; 1123 } 1124 /* 1125 * Symlink code always expects an unlocked dvp. 1126 */ 1127 if (ndp->ni_dvp != ndp->ni_vp) { 1128 VOP_UNLOCK(ndp->ni_dvp); 1129 ni_dvp_unlocked = 1; 1130 } 1131 goto success; 1132 } 1133 1134 nextname: 1135 /* 1136 * Not a symbolic link that we will follow. Continue with the 1137 * next component if there is any; otherwise, we're done. 1138 */ 1139 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 1140 ("lookup: invalid path state.")); 1141 if (relookup) { 1142 relookup = 0; 1143 ndp->ni_pathlen = prev_ni_pathlen; 1144 ndp->ni_next = prev_ni_next; 1145 if (ndp->ni_dvp != dp) 1146 vput(ndp->ni_dvp); 1147 else 1148 vrele(ndp->ni_dvp); 1149 goto dirloop; 1150 } 1151 if (cnp->cn_flags & ISDOTDOT) { 1152 error = nameicap_check_dotdot(ndp, ndp->ni_vp); 1153 if (error != 0) { 1154 #ifdef KTRACE 1155 if (KTRPOINT(curthread, KTR_CAPFAIL)) 1156 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 1157 #endif 1158 goto bad2; 1159 } 1160 } 1161 if (*ndp->ni_next == '/') { 1162 cnp->cn_nameptr = ndp->ni_next; 1163 while (*cnp->cn_nameptr == '/') { 1164 cnp->cn_nameptr++; 1165 ndp->ni_pathlen--; 1166 } 1167 if (ndp->ni_dvp != dp) 1168 vput(ndp->ni_dvp); 1169 else 1170 vrele(ndp->ni_dvp); 1171 goto dirloop; 1172 } 1173 /* 1174 * If we're processing a path with a trailing slash, 1175 * check that the end result is a directory. 1176 */ 1177 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 1178 error = ENOTDIR; 1179 goto bad2; 1180 } 1181 /* 1182 * Disallow directory write attempts on read-only filesystems. 1183 */ 1184 if (rdonly && 1185 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1186 error = EROFS; 1187 goto bad2; 1188 } 1189 if (cnp->cn_flags & SAVESTART) { 1190 ndp->ni_startdir = ndp->ni_dvp; 1191 VREF(ndp->ni_startdir); 1192 } 1193 if (!wantparent) { 1194 ni_dvp_unlocked = 2; 1195 if (ndp->ni_dvp != dp) 1196 vput(ndp->ni_dvp); 1197 else 1198 vrele(ndp->ni_dvp); 1199 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 1200 VOP_UNLOCK(ndp->ni_dvp); 1201 ni_dvp_unlocked = 1; 1202 } 1203 1204 if (cnp->cn_flags & AUDITVNODE1) 1205 AUDIT_ARG_VNODE1(dp); 1206 else if (cnp->cn_flags & AUDITVNODE2) 1207 AUDIT_ARG_VNODE2(dp); 1208 1209 if ((cnp->cn_flags & LOCKLEAF) == 0) 1210 VOP_UNLOCK(dp); 1211 success: 1212 /* 1213 * Because of shared lookup we may have the vnode shared locked, but 1214 * the caller may want it to be exclusively locked. 1215 */ 1216 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 1217 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 1218 vn_lock(dp, LK_UPGRADE | LK_RETRY); 1219 if (VN_IS_DOOMED(dp)) { 1220 error = ENOENT; 1221 goto bad2; 1222 } 1223 } 1224 return (0); 1225 1226 bad2: 1227 if (ni_dvp_unlocked != 2) { 1228 if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 1229 vput(ndp->ni_dvp); 1230 else 1231 vrele(ndp->ni_dvp); 1232 } 1233 bad: 1234 if (!dpunlocked) 1235 vput(dp); 1236 ndp->ni_vp = NULL; 1237 return (error); 1238 } 1239 1240 /* 1241 * relookup - lookup a path name component 1242 * Used by lookup to re-acquire things. 1243 */ 1244 int 1245 relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 1246 { 1247 struct vnode *dp = NULL; /* the directory we are searching */ 1248 int wantparent; /* 1 => wantparent or lockparent flag */ 1249 int rdonly; /* lookup read-only flag bit */ 1250 int error = 0; 1251 1252 KASSERT(cnp->cn_flags & ISLASTCN, 1253 ("relookup: Not given last component.")); 1254 /* 1255 * Setup: break out flag bits into variables. 1256 */ 1257 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 1258 KASSERT(wantparent, ("relookup: parent not wanted.")); 1259 rdonly = cnp->cn_flags & RDONLY; 1260 cnp->cn_flags &= ~ISSYMLINK; 1261 dp = dvp; 1262 cnp->cn_lkflags = LK_EXCLUSIVE; 1263 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 1264 1265 /* 1266 * Search a new directory. 1267 * 1268 * The last component of the filename is left accessible via 1269 * cnp->cn_nameptr for callers that need the name. Callers needing 1270 * the name set the SAVENAME flag. When done, they assume 1271 * responsibility for freeing the pathname buffer. 1272 */ 1273 #ifdef NAMEI_DIAGNOSTIC 1274 printf("{%s}: ", cnp->cn_nameptr); 1275 #endif 1276 1277 /* 1278 * Check for "" which represents the root directory after slash 1279 * removal. 1280 */ 1281 if (cnp->cn_nameptr[0] == '\0') { 1282 /* 1283 * Support only LOOKUP for "/" because lookup() 1284 * can't succeed for CREATE, DELETE and RENAME. 1285 */ 1286 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 1287 KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 1288 1289 if (!(cnp->cn_flags & LOCKLEAF)) 1290 VOP_UNLOCK(dp); 1291 *vpp = dp; 1292 /* XXX This should probably move to the top of function. */ 1293 if (cnp->cn_flags & SAVESTART) 1294 panic("lookup: SAVESTART"); 1295 return (0); 1296 } 1297 1298 if (cnp->cn_flags & ISDOTDOT) 1299 panic ("relookup: lookup on dot-dot"); 1300 1301 /* 1302 * We now have a segment name to search for, and a directory to search. 1303 */ 1304 #ifdef NAMEI_DIAGNOSTIC 1305 vn_printf(dp, "search in "); 1306 #endif 1307 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 1308 KASSERT(*vpp == NULL, ("leaf should be empty")); 1309 if (error != EJUSTRETURN) 1310 goto bad; 1311 /* 1312 * If creating and at end of pathname, then can consider 1313 * allowing file to be created. 1314 */ 1315 if (rdonly) { 1316 error = EROFS; 1317 goto bad; 1318 } 1319 /* ASSERT(dvp == ndp->ni_startdir) */ 1320 if (cnp->cn_flags & SAVESTART) 1321 VREF(dvp); 1322 if ((cnp->cn_flags & LOCKPARENT) == 0) 1323 VOP_UNLOCK(dp); 1324 /* 1325 * We return with ni_vp NULL to indicate that the entry 1326 * doesn't currently exist, leaving a pointer to the 1327 * (possibly locked) directory vnode in ndp->ni_dvp. 1328 */ 1329 return (0); 1330 } 1331 1332 dp = *vpp; 1333 1334 /* 1335 * Disallow directory write attempts on read-only filesystems. 1336 */ 1337 if (rdonly && 1338 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1339 if (dvp == dp) 1340 vrele(dvp); 1341 else 1342 vput(dvp); 1343 error = EROFS; 1344 goto bad; 1345 } 1346 /* 1347 * Set the parent lock/ref state to the requested state. 1348 */ 1349 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 1350 if (wantparent) 1351 VOP_UNLOCK(dvp); 1352 else 1353 vput(dvp); 1354 } else if (!wantparent) 1355 vrele(dvp); 1356 /* 1357 * Check for symbolic link 1358 */ 1359 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1360 ("relookup: symlink found.\n")); 1361 1362 /* ASSERT(dvp == ndp->ni_startdir) */ 1363 if (cnp->cn_flags & SAVESTART) 1364 VREF(dvp); 1365 1366 if ((cnp->cn_flags & LOCKLEAF) == 0) 1367 VOP_UNLOCK(dp); 1368 return (0); 1369 bad: 1370 vput(dp); 1371 *vpp = NULL; 1372 return (error); 1373 } 1374 1375 /* 1376 * Free data allocated by namei(); see namei(9) for details. 1377 */ 1378 void 1379 NDFREE_PNBUF(struct nameidata *ndp) 1380 { 1381 1382 if ((ndp->ni_cnd.cn_flags & HASBUF) != 0) { 1383 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 1384 ndp->ni_cnd.cn_flags &= ~HASBUF; 1385 } 1386 } 1387 1388 void 1389 (NDFREE)(struct nameidata *ndp, const u_int flags) 1390 { 1391 int unlock_dvp; 1392 int unlock_vp; 1393 1394 unlock_dvp = 0; 1395 unlock_vp = 0; 1396 1397 if (!(flags & NDF_NO_FREE_PNBUF)) { 1398 NDFREE_PNBUF(ndp); 1399 } 1400 if (!(flags & NDF_NO_VP_UNLOCK) && 1401 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 1402 unlock_vp = 1; 1403 if (!(flags & NDF_NO_DVP_UNLOCK) && 1404 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 1405 ndp->ni_dvp != ndp->ni_vp) 1406 unlock_dvp = 1; 1407 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 1408 if (unlock_vp) { 1409 vput(ndp->ni_vp); 1410 unlock_vp = 0; 1411 } else 1412 vrele(ndp->ni_vp); 1413 ndp->ni_vp = NULL; 1414 } 1415 if (unlock_vp) 1416 VOP_UNLOCK(ndp->ni_vp); 1417 if (!(flags & NDF_NO_DVP_RELE) && 1418 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 1419 if (unlock_dvp) { 1420 vput(ndp->ni_dvp); 1421 unlock_dvp = 0; 1422 } else 1423 vrele(ndp->ni_dvp); 1424 ndp->ni_dvp = NULL; 1425 } 1426 if (unlock_dvp) 1427 VOP_UNLOCK(ndp->ni_dvp); 1428 if (!(flags & NDF_NO_STARTDIR_RELE) && 1429 (ndp->ni_cnd.cn_flags & SAVESTART)) { 1430 vrele(ndp->ni_startdir); 1431 ndp->ni_startdir = NULL; 1432 } 1433 } 1434 1435 /* 1436 * Determine if there is a suitable alternate filename under the specified 1437 * prefix for the specified path. If the create flag is set, then the 1438 * alternate prefix will be used so long as the parent directory exists. 1439 * This is used by the various compatibility ABIs so that Linux binaries prefer 1440 * files under /compat/linux for example. The chosen path (whether under 1441 * the prefix or under /) is returned in a kernel malloc'd buffer pointed 1442 * to by pathbuf. The caller is responsible for free'ing the buffer from 1443 * the M_TEMP bucket if one is returned. 1444 */ 1445 int 1446 kern_alternate_path(struct thread *td, const char *prefix, const char *path, 1447 enum uio_seg pathseg, char **pathbuf, int create, int dirfd) 1448 { 1449 struct nameidata nd, ndroot; 1450 char *ptr, *buf, *cp; 1451 size_t len, sz; 1452 int error; 1453 1454 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1455 *pathbuf = buf; 1456 1457 /* Copy the prefix into the new pathname as a starting point. */ 1458 len = strlcpy(buf, prefix, MAXPATHLEN); 1459 if (len >= MAXPATHLEN) { 1460 *pathbuf = NULL; 1461 free(buf, M_TEMP); 1462 return (EINVAL); 1463 } 1464 sz = MAXPATHLEN - len; 1465 ptr = buf + len; 1466 1467 /* Append the filename to the prefix. */ 1468 if (pathseg == UIO_SYSSPACE) 1469 error = copystr(path, ptr, sz, &len); 1470 else 1471 error = copyinstr(path, ptr, sz, &len); 1472 1473 if (error) { 1474 *pathbuf = NULL; 1475 free(buf, M_TEMP); 1476 return (error); 1477 } 1478 1479 /* Only use a prefix with absolute pathnames. */ 1480 if (*ptr != '/') { 1481 error = EINVAL; 1482 goto keeporig; 1483 } 1484 1485 if (dirfd != AT_FDCWD) { 1486 /* 1487 * We want the original because the "prefix" is 1488 * included in the already opened dirfd. 1489 */ 1490 bcopy(ptr, buf, len); 1491 return (0); 1492 } 1493 1494 /* 1495 * We know that there is a / somewhere in this pathname. 1496 * Search backwards for it, to find the file's parent dir 1497 * to see if it exists in the alternate tree. If it does, 1498 * and we want to create a file (cflag is set). We don't 1499 * need to worry about the root comparison in this case. 1500 */ 1501 1502 if (create) { 1503 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1504 *cp = '\0'; 1505 1506 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td); 1507 error = namei(&nd); 1508 *cp = '/'; 1509 if (error != 0) 1510 goto keeporig; 1511 } else { 1512 NDINIT(&nd, LOOKUP, NOFOLLOW, UIO_SYSSPACE, buf, td); 1513 1514 error = namei(&nd); 1515 if (error != 0) 1516 goto keeporig; 1517 1518 /* 1519 * We now compare the vnode of the prefix to the one 1520 * vnode asked. If they resolve to be the same, then we 1521 * ignore the match so that the real root gets used. 1522 * This avoids the problem of traversing "../.." to find the 1523 * root directory and never finding it, because "/" resolves 1524 * to the emulation root directory. This is expensive :-( 1525 */ 1526 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, 1527 td); 1528 1529 /* We shouldn't ever get an error from this namei(). */ 1530 error = namei(&ndroot); 1531 if (error == 0) { 1532 if (nd.ni_vp == ndroot.ni_vp) 1533 error = ENOENT; 1534 1535 NDFREE(&ndroot, NDF_ONLY_PNBUF); 1536 vrele(ndroot.ni_vp); 1537 } 1538 } 1539 1540 NDFREE(&nd, NDF_ONLY_PNBUF); 1541 vrele(nd.ni_vp); 1542 1543 keeporig: 1544 /* If there was an error, use the original path name. */ 1545 if (error) 1546 bcopy(ptr, buf, len); 1547 return (error); 1548 } 1549