vfs_lookup.c revision 289798
1/*- 2 * Copyright (c) 1982, 1986, 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)vfs_lookup.c 8.4 (Berkeley) 2/16/94 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: stable/10/sys/kern/vfs_lookup.c 289798 2015-10-23 07:40:43Z avg $"); 39 40#include "opt_capsicum.h" 41#include "opt_kdtrace.h" 42#include "opt_ktrace.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/kernel.h> 47#include <sys/capsicum.h> 48#include <sys/fcntl.h> 49#include <sys/jail.h> 50#include <sys/lock.h> 51#include <sys/mutex.h> 52#include <sys/namei.h> 53#include <sys/vnode.h> 54#include <sys/mount.h> 55#include <sys/filedesc.h> 56#include <sys/proc.h> 57#include <sys/sdt.h> 58#include <sys/syscallsubr.h> 59#include <sys/sysctl.h> 60#ifdef KTRACE 61#include <sys/ktrace.h> 62#endif 63 64#include <security/audit/audit.h> 65#include <security/mac/mac_framework.h> 66 67#include <vm/uma.h> 68 69#define NAMEI_DIAGNOSTIC 1 70#undef NAMEI_DIAGNOSTIC 71 72SDT_PROVIDER_DECLARE(vfs); 73SDT_PROBE_DEFINE3(vfs, namei, lookup, entry, "struct vnode *", "char *", 74 "unsigned long"); 75SDT_PROBE_DEFINE2(vfs, namei, lookup, return, "int", "struct vnode *"); 76 77/* 78 * Allocation zone for namei 79 */ 80uma_zone_t namei_zone; 81/* 82 * Placeholder vnode for mp traversal 83 */ 84static struct vnode *vp_crossmp; 85 86static void 87nameiinit(void *dummy __unused) 88{ 89 90 namei_zone = uma_zcreate("NAMEI", MAXPATHLEN, NULL, NULL, NULL, NULL, 91 UMA_ALIGN_PTR, 0); 92 getnewvnode("crossmp", NULL, &dead_vnodeops, &vp_crossmp); 93 vn_lock(vp_crossmp, LK_EXCLUSIVE); 94 VN_LOCK_ASHARE(vp_crossmp); 95 VOP_UNLOCK(vp_crossmp, 0); 96} 97SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_SECOND, nameiinit, NULL); 98 99static int lookup_shared = 1; 100SYSCTL_INT(_vfs, OID_AUTO, lookup_shared, CTLFLAG_RW, &lookup_shared, 0, 101 "Enables/Disables shared locks for path name translation"); 102TUNABLE_INT("vfs.lookup_shared", &lookup_shared); 103 104/* 105 * Convert a pathname into a pointer to a locked vnode. 106 * 107 * The FOLLOW flag is set when symbolic links are to be followed 108 * when they occur at the end of the name translation process. 109 * Symbolic links are always followed for all other pathname 110 * components other than the last. 111 * 112 * The segflg defines whether the name is to be copied from user 113 * space or kernel space. 114 * 115 * Overall outline of namei: 116 * 117 * copy in name 118 * get starting directory 119 * while (!done && !error) { 120 * call lookup to search path. 121 * if symbolic link, massage name in buffer and continue 122 * } 123 */ 124static void 125namei_cleanup_cnp(struct componentname *cnp) 126{ 127 uma_zfree(namei_zone, cnp->cn_pnbuf); 128#ifdef DIAGNOSTIC 129 cnp->cn_pnbuf = NULL; 130 cnp->cn_nameptr = NULL; 131#endif 132} 133 134int 135namei(struct nameidata *ndp) 136{ 137 struct filedesc *fdp; /* pointer to file descriptor state */ 138 char *cp; /* pointer into pathname argument */ 139 struct vnode *dp; /* the directory we are searching */ 140 struct iovec aiov; /* uio for reading symbolic links */ 141 struct uio auio; 142 int error, linklen; 143 struct componentname *cnp = &ndp->ni_cnd; 144 struct thread *td = cnp->cn_thread; 145 struct proc *p = td->td_proc; 146 147 ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_thread->td_ucred; 148 KASSERT(cnp->cn_cred && p, ("namei: bad cred/proc")); 149 KASSERT((cnp->cn_nameiop & (~OPMASK)) == 0, 150 ("namei: nameiop contaminated with flags")); 151 KASSERT((cnp->cn_flags & OPMASK) == 0, 152 ("namei: flags contaminated with nameiops")); 153 if (!lookup_shared) 154 cnp->cn_flags &= ~LOCKSHARED; 155 fdp = p->p_fd; 156 157 /* We will set this ourselves if we need it. */ 158 cnp->cn_flags &= ~TRAILINGSLASH; 159 160 /* 161 * Get a buffer for the name to be translated, and copy the 162 * name into the buffer. 163 */ 164 if ((cnp->cn_flags & HASBUF) == 0) 165 cnp->cn_pnbuf = uma_zalloc(namei_zone, M_WAITOK); 166 if (ndp->ni_segflg == UIO_SYSSPACE) 167 error = copystr(ndp->ni_dirp, cnp->cn_pnbuf, 168 MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 169 else 170 error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, 171 MAXPATHLEN, (size_t *)&ndp->ni_pathlen); 172 173 /* 174 * Don't allow empty pathnames. 175 */ 176 if (!error && *cnp->cn_pnbuf == '\0') 177 error = ENOENT; 178 179#ifdef CAPABILITY_MODE 180 /* 181 * In capability mode, lookups must be "strictly relative" (i.e. 182 * not an absolute path, and not containing '..' components) to 183 * a real file descriptor, not the pseudo-descriptor AT_FDCWD. 184 */ 185 if (error == 0 && IN_CAPABILITY_MODE(td) && 186 (cnp->cn_flags & NOCAPCHECK) == 0) { 187 ndp->ni_strictrelative = 1; 188 if (ndp->ni_dirfd == AT_FDCWD) { 189#ifdef KTRACE 190 if (KTRPOINT(td, KTR_CAPFAIL)) 191 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 192#endif 193 error = ECAPMODE; 194 } 195 } 196#endif 197 if (error) { 198 namei_cleanup_cnp(cnp); 199 ndp->ni_vp = NULL; 200 return (error); 201 } 202 ndp->ni_loopcnt = 0; 203#ifdef KTRACE 204 if (KTRPOINT(td, KTR_NAMEI)) { 205 KASSERT(cnp->cn_thread == curthread, 206 ("namei not using curthread")); 207 ktrnamei(cnp->cn_pnbuf); 208 } 209#endif 210 /* 211 * Get starting point for the translation. 212 */ 213 FILEDESC_SLOCK(fdp); 214 ndp->ni_rootdir = fdp->fd_rdir; 215 ndp->ni_topdir = fdp->fd_jdir; 216 217 /* 218 * If we are auditing the kernel pathname, save the user pathname. 219 */ 220 if (cnp->cn_flags & AUDITVNODE1) 221 AUDIT_ARG_UPATH1(td, ndp->ni_dirfd, cnp->cn_pnbuf); 222 if (cnp->cn_flags & AUDITVNODE2) 223 AUDIT_ARG_UPATH2(td, ndp->ni_dirfd, cnp->cn_pnbuf); 224 225 dp = NULL; 226 if (cnp->cn_pnbuf[0] != '/') { 227 if (ndp->ni_startdir != NULL) { 228 dp = ndp->ni_startdir; 229 error = 0; 230 } else if (ndp->ni_dirfd != AT_FDCWD) { 231 cap_rights_t rights; 232 233 rights = ndp->ni_rightsneeded; 234 cap_rights_set(&rights, CAP_LOOKUP); 235 236 if (cnp->cn_flags & AUDITVNODE1) 237 AUDIT_ARG_ATFD1(ndp->ni_dirfd); 238 if (cnp->cn_flags & AUDITVNODE2) 239 AUDIT_ARG_ATFD2(ndp->ni_dirfd); 240 error = fgetvp_rights(td, ndp->ni_dirfd, 241 &rights, &ndp->ni_filecaps, &dp); 242#ifdef CAPABILITIES 243 /* 244 * If file descriptor doesn't have all rights, 245 * all lookups relative to it must also be 246 * strictly relative. 247 */ 248 CAP_ALL(&rights); 249 if (!cap_rights_contains(&ndp->ni_filecaps.fc_rights, 250 &rights) || 251 ndp->ni_filecaps.fc_fcntls != CAP_FCNTL_ALL || 252 ndp->ni_filecaps.fc_nioctls != -1) { 253 ndp->ni_strictrelative = 1; 254 } 255#endif 256 } 257 if (error != 0 || dp != NULL) { 258 FILEDESC_SUNLOCK(fdp); 259 if (error == 0 && dp->v_type != VDIR) { 260 vrele(dp); 261 error = ENOTDIR; 262 } 263 } 264 if (error) { 265 namei_cleanup_cnp(cnp); 266 return (error); 267 } 268 } 269 if (dp == NULL) { 270 dp = fdp->fd_cdir; 271 VREF(dp); 272 FILEDESC_SUNLOCK(fdp); 273 if (ndp->ni_startdir != NULL) 274 vrele(ndp->ni_startdir); 275 } 276 SDT_PROBE3(vfs, namei, lookup, entry, dp, cnp->cn_pnbuf, 277 cnp->cn_flags); 278 for (;;) { 279 /* 280 * Check if root directory should replace current directory. 281 * Done at start of translation and after symbolic link. 282 */ 283 cnp->cn_nameptr = cnp->cn_pnbuf; 284 if (*(cnp->cn_nameptr) == '/') { 285 vrele(dp); 286 if (ndp->ni_strictrelative != 0) { 287#ifdef KTRACE 288 if (KTRPOINT(curthread, KTR_CAPFAIL)) 289 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 290#endif 291 namei_cleanup_cnp(cnp); 292 return (ENOTCAPABLE); 293 } 294 while (*(cnp->cn_nameptr) == '/') { 295 cnp->cn_nameptr++; 296 ndp->ni_pathlen--; 297 } 298 dp = ndp->ni_rootdir; 299 VREF(dp); 300 } 301 ndp->ni_startdir = dp; 302 error = lookup(ndp); 303 if (error) { 304 namei_cleanup_cnp(cnp); 305 SDT_PROBE2(vfs, namei, lookup, return, error, NULL); 306 return (error); 307 } 308 /* 309 * If not a symbolic link, we're done. 310 */ 311 if ((cnp->cn_flags & ISSYMLINK) == 0) { 312 if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0) { 313 namei_cleanup_cnp(cnp); 314 } else 315 cnp->cn_flags |= HASBUF; 316 317 SDT_PROBE2(vfs, namei, lookup, return, 0, ndp->ni_vp); 318 return (0); 319 } 320 if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { 321 error = ELOOP; 322 break; 323 } 324#ifdef MAC 325 if ((cnp->cn_flags & NOMACCHECK) == 0) { 326 error = mac_vnode_check_readlink(td->td_ucred, 327 ndp->ni_vp); 328 if (error) 329 break; 330 } 331#endif 332 if (ndp->ni_pathlen > 1) 333 cp = uma_zalloc(namei_zone, M_WAITOK); 334 else 335 cp = cnp->cn_pnbuf; 336 aiov.iov_base = cp; 337 aiov.iov_len = MAXPATHLEN; 338 auio.uio_iov = &aiov; 339 auio.uio_iovcnt = 1; 340 auio.uio_offset = 0; 341 auio.uio_rw = UIO_READ; 342 auio.uio_segflg = UIO_SYSSPACE; 343 auio.uio_td = td; 344 auio.uio_resid = MAXPATHLEN; 345 error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred); 346 if (error) { 347 if (ndp->ni_pathlen > 1) 348 uma_zfree(namei_zone, cp); 349 break; 350 } 351 linklen = MAXPATHLEN - auio.uio_resid; 352 if (linklen == 0) { 353 if (ndp->ni_pathlen > 1) 354 uma_zfree(namei_zone, cp); 355 error = ENOENT; 356 break; 357 } 358 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) { 359 if (ndp->ni_pathlen > 1) 360 uma_zfree(namei_zone, cp); 361 error = ENAMETOOLONG; 362 break; 363 } 364 if (ndp->ni_pathlen > 1) { 365 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); 366 uma_zfree(namei_zone, cnp->cn_pnbuf); 367 cnp->cn_pnbuf = cp; 368 } else 369 cnp->cn_pnbuf[linklen] = '\0'; 370 ndp->ni_pathlen += linklen; 371 vput(ndp->ni_vp); 372 dp = ndp->ni_dvp; 373 } 374 namei_cleanup_cnp(cnp); 375 vput(ndp->ni_vp); 376 ndp->ni_vp = NULL; 377 vrele(ndp->ni_dvp); 378 SDT_PROBE2(vfs, namei, lookup, return, error, NULL); 379 return (error); 380} 381 382static int 383compute_cn_lkflags(struct mount *mp, int lkflags, int cnflags) 384{ 385 386 if (mp == NULL || ((lkflags & LK_SHARED) && 387 (!(mp->mnt_kern_flag & MNTK_LOOKUP_SHARED) || 388 ((cnflags & ISDOTDOT) && 389 (mp->mnt_kern_flag & MNTK_LOOKUP_EXCL_DOTDOT))))) { 390 lkflags &= ~LK_SHARED; 391 lkflags |= LK_EXCLUSIVE; 392 } 393 lkflags |= LK_NODDLKTREAT; 394 return (lkflags); 395} 396 397static __inline int 398needs_exclusive_leaf(struct mount *mp, int flags) 399{ 400 401 /* 402 * Intermediate nodes can use shared locks, we only need to 403 * force an exclusive lock for leaf nodes. 404 */ 405 if ((flags & (ISLASTCN | LOCKLEAF)) != (ISLASTCN | LOCKLEAF)) 406 return (0); 407 408 /* Always use exclusive locks if LOCKSHARED isn't set. */ 409 if (!(flags & LOCKSHARED)) 410 return (1); 411 412 /* 413 * For lookups during open(), if the mount point supports 414 * extended shared operations, then use a shared lock for the 415 * leaf node, otherwise use an exclusive lock. 416 */ 417 if ((flags & ISOPEN) != 0) 418 return (!MNT_EXTENDED_SHARED(mp)); 419 420 /* 421 * Lookup requests outside of open() that specify LOCKSHARED 422 * only need a shared lock on the leaf vnode. 423 */ 424 return (0); 425} 426 427/* 428 * Search a pathname. 429 * This is a very central and rather complicated routine. 430 * 431 * The pathname is pointed to by ni_ptr and is of length ni_pathlen. 432 * The starting directory is taken from ni_startdir. The pathname is 433 * descended until done, or a symbolic link is encountered. The variable 434 * ni_more is clear if the path is completed; it is set to one if a 435 * symbolic link needing interpretation is encountered. 436 * 437 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on 438 * whether the name is to be looked up, created, renamed, or deleted. 439 * When CREATE, RENAME, or DELETE is specified, information usable in 440 * creating, renaming, or deleting a directory entry may be calculated. 441 * If flag has LOCKPARENT or'ed into it, the parent directory is returned 442 * locked. If flag has WANTPARENT or'ed into it, the parent directory is 443 * returned unlocked. Otherwise the parent directory is not returned. If 444 * the target of the pathname exists and LOCKLEAF is or'ed into the flag 445 * the target is returned locked, otherwise it is returned unlocked. 446 * When creating or renaming and LOCKPARENT is specified, the target may not 447 * be ".". When deleting and LOCKPARENT is specified, the target may be ".". 448 * 449 * Overall outline of lookup: 450 * 451 * dirloop: 452 * identify next component of name at ndp->ni_ptr 453 * handle degenerate case where name is null string 454 * if .. and crossing mount points and on mounted filesys, find parent 455 * call VOP_LOOKUP routine for next component name 456 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set 457 * component vnode returned in ni_vp (if it exists), locked. 458 * if result vnode is mounted on and crossing mount points, 459 * find mounted on vnode 460 * if more components of name, do next level at dirloop 461 * return the answer in ni_vp, locked if LOCKLEAF set 462 * if LOCKPARENT set, return locked parent in ni_dvp 463 * if WANTPARENT set, return unlocked parent in ni_dvp 464 */ 465int 466lookup(struct nameidata *ndp) 467{ 468 char *cp; /* pointer into pathname argument */ 469 struct vnode *dp = 0; /* the directory we are searching */ 470 struct vnode *tdp; /* saved dp */ 471 struct mount *mp; /* mount table entry */ 472 struct prison *pr; 473 int docache; /* == 0 do not cache last component */ 474 int wantparent; /* 1 => wantparent or lockparent flag */ 475 int rdonly; /* lookup read-only flag bit */ 476 int error = 0; 477 int dpunlocked = 0; /* dp has already been unlocked */ 478 struct componentname *cnp = &ndp->ni_cnd; 479 int lkflags_save; 480 int ni_dvp_unlocked; 481 482 /* 483 * Setup: break out flag bits into variables. 484 */ 485 ni_dvp_unlocked = 0; 486 wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); 487 KASSERT(cnp->cn_nameiop == LOOKUP || wantparent, 488 ("CREATE, DELETE, RENAME require LOCKPARENT or WANTPARENT.")); 489 docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE; 490 if (cnp->cn_nameiop == DELETE || 491 (wantparent && cnp->cn_nameiop != CREATE && 492 cnp->cn_nameiop != LOOKUP)) 493 docache = 0; 494 rdonly = cnp->cn_flags & RDONLY; 495 cnp->cn_flags &= ~ISSYMLINK; 496 ndp->ni_dvp = NULL; 497 /* 498 * We use shared locks until we hit the parent of the last cn then 499 * we adjust based on the requesting flags. 500 */ 501 if (lookup_shared) 502 cnp->cn_lkflags = LK_SHARED; 503 else 504 cnp->cn_lkflags = LK_EXCLUSIVE; 505 dp = ndp->ni_startdir; 506 ndp->ni_startdir = NULLVP; 507 vn_lock(dp, 508 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | LK_RETRY, 509 cnp->cn_flags)); 510 511dirloop: 512 /* 513 * Search a new directory. 514 * 515 * The last component of the filename is left accessible via 516 * cnp->cn_nameptr for callers that need the name. Callers needing 517 * the name set the SAVENAME flag. When done, they assume 518 * responsibility for freeing the pathname buffer. 519 */ 520 cnp->cn_consume = 0; 521 for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++) 522 continue; 523 cnp->cn_namelen = cp - cnp->cn_nameptr; 524 if (cnp->cn_namelen > NAME_MAX) { 525 error = ENAMETOOLONG; 526 goto bad; 527 } 528#ifdef NAMEI_DIAGNOSTIC 529 { char c = *cp; 530 *cp = '\0'; 531 printf("{%s}: ", cnp->cn_nameptr); 532 *cp = c; } 533#endif 534 ndp->ni_pathlen -= cnp->cn_namelen; 535 ndp->ni_next = cp; 536 537 /* 538 * Replace multiple slashes by a single slash and trailing slashes 539 * by a null. This must be done before VOP_LOOKUP() because some 540 * fs's don't know about trailing slashes. Remember if there were 541 * trailing slashes to handle symlinks, existing non-directories 542 * and non-existing files that won't be directories specially later. 543 */ 544 while (*cp == '/' && (cp[1] == '/' || cp[1] == '\0')) { 545 cp++; 546 ndp->ni_pathlen--; 547 if (*cp == '\0') { 548 *ndp->ni_next = '\0'; 549 cnp->cn_flags |= TRAILINGSLASH; 550 } 551 } 552 ndp->ni_next = cp; 553 554 cnp->cn_flags |= MAKEENTRY; 555 if (*cp == '\0' && docache == 0) 556 cnp->cn_flags &= ~MAKEENTRY; 557 if (cnp->cn_namelen == 2 && 558 cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.') 559 cnp->cn_flags |= ISDOTDOT; 560 else 561 cnp->cn_flags &= ~ISDOTDOT; 562 if (*ndp->ni_next == 0) 563 cnp->cn_flags |= ISLASTCN; 564 else 565 cnp->cn_flags &= ~ISLASTCN; 566 567 if ((cnp->cn_flags & ISLASTCN) != 0 && 568 cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.' && 569 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 570 error = EINVAL; 571 goto bad; 572 } 573 574 /* 575 * Check for degenerate name (e.g. / or "") 576 * which is a way of talking about a directory, 577 * e.g. like "/." or ".". 578 */ 579 if (cnp->cn_nameptr[0] == '\0') { 580 if (dp->v_type != VDIR) { 581 error = ENOTDIR; 582 goto bad; 583 } 584 if (cnp->cn_nameiop != LOOKUP) { 585 error = EISDIR; 586 goto bad; 587 } 588 if (wantparent) { 589 ndp->ni_dvp = dp; 590 VREF(dp); 591 } 592 ndp->ni_vp = dp; 593 594 if (cnp->cn_flags & AUDITVNODE1) 595 AUDIT_ARG_VNODE1(dp); 596 else if (cnp->cn_flags & AUDITVNODE2) 597 AUDIT_ARG_VNODE2(dp); 598 599 if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF))) 600 VOP_UNLOCK(dp, 0); 601 /* XXX This should probably move to the top of function. */ 602 if (cnp->cn_flags & SAVESTART) 603 panic("lookup: SAVESTART"); 604 goto success; 605 } 606 607 /* 608 * Handle "..": five special cases. 609 * 0. If doing a capability lookup, return ENOTCAPABLE (this is a 610 * fairly conservative design choice, but it's the only one that we 611 * are satisfied guarantees the property we're looking for). 612 * 1. Return an error if this is the last component of 613 * the name and the operation is DELETE or RENAME. 614 * 2. If at root directory (e.g. after chroot) 615 * or at absolute root directory 616 * then ignore it so can't get out. 617 * 3. If this vnode is the root of a mounted 618 * filesystem, then replace it with the 619 * vnode which was mounted on so we take the 620 * .. in the other filesystem. 621 * 4. If the vnode is the top directory of 622 * the jail or chroot, don't let them out. 623 */ 624 if (cnp->cn_flags & ISDOTDOT) { 625 if (ndp->ni_strictrelative != 0) { 626#ifdef KTRACE 627 if (KTRPOINT(curthread, KTR_CAPFAIL)) 628 ktrcapfail(CAPFAIL_LOOKUP, NULL, NULL); 629#endif 630 error = ENOTCAPABLE; 631 goto bad; 632 } 633 if ((cnp->cn_flags & ISLASTCN) != 0 && 634 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 635 error = EINVAL; 636 goto bad; 637 } 638 for (;;) { 639 for (pr = cnp->cn_cred->cr_prison; pr != NULL; 640 pr = pr->pr_parent) 641 if (dp == pr->pr_root) 642 break; 643 if (dp == ndp->ni_rootdir || 644 dp == ndp->ni_topdir || 645 dp == rootvnode || 646 pr != NULL || 647 ((dp->v_vflag & VV_ROOT) != 0 && 648 (cnp->cn_flags & NOCROSSMOUNT) != 0)) { 649 ndp->ni_dvp = dp; 650 ndp->ni_vp = dp; 651 VREF(dp); 652 goto nextname; 653 } 654 if ((dp->v_vflag & VV_ROOT) == 0) 655 break; 656 if (dp->v_iflag & VI_DOOMED) { /* forced unmount */ 657 error = ENOENT; 658 goto bad; 659 } 660 tdp = dp; 661 dp = dp->v_mount->mnt_vnodecovered; 662 VREF(dp); 663 vput(tdp); 664 vn_lock(dp, 665 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 666 LK_RETRY, ISDOTDOT)); 667 } 668 } 669 670 /* 671 * We now have a segment name to search for, and a directory to search. 672 */ 673unionlookup: 674#ifdef MAC 675 if ((cnp->cn_flags & NOMACCHECK) == 0) { 676 error = mac_vnode_check_lookup(cnp->cn_thread->td_ucred, dp, 677 cnp); 678 if (error) 679 goto bad; 680 } 681#endif 682 ndp->ni_dvp = dp; 683 ndp->ni_vp = NULL; 684 ASSERT_VOP_LOCKED(dp, "lookup"); 685 /* 686 * If we have a shared lock we may need to upgrade the lock for the 687 * last operation. 688 */ 689 if (dp != vp_crossmp && 690 VOP_ISLOCKED(dp) == LK_SHARED && 691 (cnp->cn_flags & ISLASTCN) && (cnp->cn_flags & LOCKPARENT)) 692 vn_lock(dp, LK_UPGRADE|LK_RETRY); 693 if ((dp->v_iflag & VI_DOOMED) != 0) { 694 error = ENOENT; 695 goto bad; 696 } 697 /* 698 * If we're looking up the last component and we need an exclusive 699 * lock, adjust our lkflags. 700 */ 701 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags)) 702 cnp->cn_lkflags = LK_EXCLUSIVE; 703#ifdef NAMEI_DIAGNOSTIC 704 vprint("lookup in", dp); 705#endif 706 lkflags_save = cnp->cn_lkflags; 707 cnp->cn_lkflags = compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags, 708 cnp->cn_flags); 709 if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) { 710 cnp->cn_lkflags = lkflags_save; 711 KASSERT(ndp->ni_vp == NULL, ("leaf should be empty")); 712#ifdef NAMEI_DIAGNOSTIC 713 printf("not found\n"); 714#endif 715 if ((error == ENOENT) && 716 (dp->v_vflag & VV_ROOT) && (dp->v_mount != NULL) && 717 (dp->v_mount->mnt_flag & MNT_UNION)) { 718 tdp = dp; 719 dp = dp->v_mount->mnt_vnodecovered; 720 VREF(dp); 721 vput(tdp); 722 vn_lock(dp, 723 compute_cn_lkflags(dp->v_mount, cnp->cn_lkflags | 724 LK_RETRY, cnp->cn_flags)); 725 goto unionlookup; 726 } 727 728 if (error != EJUSTRETURN) 729 goto bad; 730 /* 731 * At this point, we know we're at the end of the 732 * pathname. If creating / renaming, we can consider 733 * allowing the file or directory to be created / renamed, 734 * provided we're not on a read-only filesystem. 735 */ 736 if (rdonly) { 737 error = EROFS; 738 goto bad; 739 } 740 /* trailing slash only allowed for directories */ 741 if ((cnp->cn_flags & TRAILINGSLASH) && 742 !(cnp->cn_flags & WILLBEDIR)) { 743 error = ENOENT; 744 goto bad; 745 } 746 if ((cnp->cn_flags & LOCKPARENT) == 0) 747 VOP_UNLOCK(dp, 0); 748 /* 749 * We return with ni_vp NULL to indicate that the entry 750 * doesn't currently exist, leaving a pointer to the 751 * (possibly locked) directory vnode in ndp->ni_dvp. 752 */ 753 if (cnp->cn_flags & SAVESTART) { 754 ndp->ni_startdir = ndp->ni_dvp; 755 VREF(ndp->ni_startdir); 756 } 757 goto success; 758 } else 759 cnp->cn_lkflags = lkflags_save; 760#ifdef NAMEI_DIAGNOSTIC 761 printf("found\n"); 762#endif 763 /* 764 * Take into account any additional components consumed by 765 * the underlying filesystem. 766 */ 767 if (cnp->cn_consume > 0) { 768 cnp->cn_nameptr += cnp->cn_consume; 769 ndp->ni_next += cnp->cn_consume; 770 ndp->ni_pathlen -= cnp->cn_consume; 771 cnp->cn_consume = 0; 772 } 773 774 dp = ndp->ni_vp; 775 776 /* 777 * Check to see if the vnode has been mounted on; 778 * if so find the root of the mounted filesystem. 779 */ 780 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) && 781 (cnp->cn_flags & NOCROSSMOUNT) == 0) { 782 if (vfs_busy(mp, 0)) 783 continue; 784 vput(dp); 785 if (dp != ndp->ni_dvp) 786 vput(ndp->ni_dvp); 787 else 788 vrele(ndp->ni_dvp); 789 vref(vp_crossmp); 790 ndp->ni_dvp = vp_crossmp; 791 error = VFS_ROOT(mp, compute_cn_lkflags(mp, cnp->cn_lkflags, 792 cnp->cn_flags), &tdp); 793 vfs_unbusy(mp); 794 if (vn_lock(vp_crossmp, LK_SHARED | LK_NOWAIT)) 795 panic("vp_crossmp exclusively locked or reclaimed"); 796 if (error) { 797 dpunlocked = 1; 798 goto bad2; 799 } 800 ndp->ni_vp = dp = tdp; 801 } 802 803 /* 804 * Check for symbolic link 805 */ 806 if ((dp->v_type == VLNK) && 807 ((cnp->cn_flags & FOLLOW) || (cnp->cn_flags & TRAILINGSLASH) || 808 *ndp->ni_next == '/')) { 809 cnp->cn_flags |= ISSYMLINK; 810 if (dp->v_iflag & VI_DOOMED) { 811 /* 812 * We can't know whether the directory was mounted with 813 * NOSYMFOLLOW, so we can't follow safely. 814 */ 815 error = ENOENT; 816 goto bad2; 817 } 818 if (dp->v_mount->mnt_flag & MNT_NOSYMFOLLOW) { 819 error = EACCES; 820 goto bad2; 821 } 822 /* 823 * Symlink code always expects an unlocked dvp. 824 */ 825 if (ndp->ni_dvp != ndp->ni_vp) { 826 VOP_UNLOCK(ndp->ni_dvp, 0); 827 ni_dvp_unlocked = 1; 828 } 829 goto success; 830 } 831 832nextname: 833 /* 834 * Not a symbolic link that we will follow. Continue with the 835 * next component if there is any; otherwise, we're done. 836 */ 837 KASSERT((cnp->cn_flags & ISLASTCN) || *ndp->ni_next == '/', 838 ("lookup: invalid path state.")); 839 if (*ndp->ni_next == '/') { 840 cnp->cn_nameptr = ndp->ni_next; 841 while (*cnp->cn_nameptr == '/') { 842 cnp->cn_nameptr++; 843 ndp->ni_pathlen--; 844 } 845 if (ndp->ni_dvp != dp) 846 vput(ndp->ni_dvp); 847 else 848 vrele(ndp->ni_dvp); 849 goto dirloop; 850 } 851 /* 852 * If we're processing a path with a trailing slash, 853 * check that the end result is a directory. 854 */ 855 if ((cnp->cn_flags & TRAILINGSLASH) && dp->v_type != VDIR) { 856 error = ENOTDIR; 857 goto bad2; 858 } 859 /* 860 * Disallow directory write attempts on read-only filesystems. 861 */ 862 if (rdonly && 863 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 864 error = EROFS; 865 goto bad2; 866 } 867 if (cnp->cn_flags & SAVESTART) { 868 ndp->ni_startdir = ndp->ni_dvp; 869 VREF(ndp->ni_startdir); 870 } 871 if (!wantparent) { 872 ni_dvp_unlocked = 2; 873 if (ndp->ni_dvp != dp) 874 vput(ndp->ni_dvp); 875 else 876 vrele(ndp->ni_dvp); 877 } else if ((cnp->cn_flags & LOCKPARENT) == 0 && ndp->ni_dvp != dp) { 878 VOP_UNLOCK(ndp->ni_dvp, 0); 879 ni_dvp_unlocked = 1; 880 } 881 882 if (cnp->cn_flags & AUDITVNODE1) 883 AUDIT_ARG_VNODE1(dp); 884 else if (cnp->cn_flags & AUDITVNODE2) 885 AUDIT_ARG_VNODE2(dp); 886 887 if ((cnp->cn_flags & LOCKLEAF) == 0) 888 VOP_UNLOCK(dp, 0); 889success: 890 /* 891 * Because of lookup_shared we may have the vnode shared locked, but 892 * the caller may want it to be exclusively locked. 893 */ 894 if (needs_exclusive_leaf(dp->v_mount, cnp->cn_flags) && 895 VOP_ISLOCKED(dp) != LK_EXCLUSIVE) { 896 vn_lock(dp, LK_UPGRADE | LK_RETRY); 897 if (dp->v_iflag & VI_DOOMED) { 898 error = ENOENT; 899 goto bad2; 900 } 901 } 902 return (0); 903 904bad2: 905 if (ni_dvp_unlocked != 2) { 906 if (dp != ndp->ni_dvp && !ni_dvp_unlocked) 907 vput(ndp->ni_dvp); 908 else 909 vrele(ndp->ni_dvp); 910 } 911bad: 912 if (!dpunlocked) 913 vput(dp); 914 ndp->ni_vp = NULL; 915 return (error); 916} 917 918/* 919 * relookup - lookup a path name component 920 * Used by lookup to re-acquire things. 921 */ 922int 923relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) 924{ 925 struct vnode *dp = 0; /* the directory we are searching */ 926 int wantparent; /* 1 => wantparent or lockparent flag */ 927 int rdonly; /* lookup read-only flag bit */ 928 int error = 0; 929 930 KASSERT(cnp->cn_flags & ISLASTCN, 931 ("relookup: Not given last component.")); 932 /* 933 * Setup: break out flag bits into variables. 934 */ 935 wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); 936 KASSERT(wantparent, ("relookup: parent not wanted.")); 937 rdonly = cnp->cn_flags & RDONLY; 938 cnp->cn_flags &= ~ISSYMLINK; 939 dp = dvp; 940 cnp->cn_lkflags = LK_EXCLUSIVE; 941 vn_lock(dp, LK_EXCLUSIVE | LK_RETRY); 942 943 /* 944 * Search a new directory. 945 * 946 * The last component of the filename is left accessible via 947 * cnp->cn_nameptr for callers that need the name. Callers needing 948 * the name set the SAVENAME flag. When done, they assume 949 * responsibility for freeing the pathname buffer. 950 */ 951#ifdef NAMEI_DIAGNOSTIC 952 printf("{%s}: ", cnp->cn_nameptr); 953#endif 954 955 /* 956 * Check for "" which represents the root directory after slash 957 * removal. 958 */ 959 if (cnp->cn_nameptr[0] == '\0') { 960 /* 961 * Support only LOOKUP for "/" because lookup() 962 * can't succeed for CREATE, DELETE and RENAME. 963 */ 964 KASSERT(cnp->cn_nameiop == LOOKUP, ("nameiop must be LOOKUP")); 965 KASSERT(dp->v_type == VDIR, ("dp is not a directory")); 966 967 if (!(cnp->cn_flags & LOCKLEAF)) 968 VOP_UNLOCK(dp, 0); 969 *vpp = dp; 970 /* XXX This should probably move to the top of function. */ 971 if (cnp->cn_flags & SAVESTART) 972 panic("lookup: SAVESTART"); 973 return (0); 974 } 975 976 if (cnp->cn_flags & ISDOTDOT) 977 panic ("relookup: lookup on dot-dot"); 978 979 /* 980 * We now have a segment name to search for, and a directory to search. 981 */ 982#ifdef NAMEI_DIAGNOSTIC 983 vprint("search in:", dp); 984#endif 985 if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) { 986 KASSERT(*vpp == NULL, ("leaf should be empty")); 987 if (error != EJUSTRETURN) 988 goto bad; 989 /* 990 * If creating and at end of pathname, then can consider 991 * allowing file to be created. 992 */ 993 if (rdonly) { 994 error = EROFS; 995 goto bad; 996 } 997 /* ASSERT(dvp == ndp->ni_startdir) */ 998 if (cnp->cn_flags & SAVESTART) 999 VREF(dvp); 1000 if ((cnp->cn_flags & LOCKPARENT) == 0) 1001 VOP_UNLOCK(dp, 0); 1002 /* 1003 * We return with ni_vp NULL to indicate that the entry 1004 * doesn't currently exist, leaving a pointer to the 1005 * (possibly locked) directory vnode in ndp->ni_dvp. 1006 */ 1007 return (0); 1008 } 1009 1010 dp = *vpp; 1011 1012 /* 1013 * Disallow directory write attempts on read-only filesystems. 1014 */ 1015 if (rdonly && 1016 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { 1017 if (dvp == dp) 1018 vrele(dvp); 1019 else 1020 vput(dvp); 1021 error = EROFS; 1022 goto bad; 1023 } 1024 /* 1025 * Set the parent lock/ref state to the requested state. 1026 */ 1027 if ((cnp->cn_flags & LOCKPARENT) == 0 && dvp != dp) { 1028 if (wantparent) 1029 VOP_UNLOCK(dvp, 0); 1030 else 1031 vput(dvp); 1032 } else if (!wantparent) 1033 vrele(dvp); 1034 /* 1035 * Check for symbolic link 1036 */ 1037 KASSERT(dp->v_type != VLNK || !(cnp->cn_flags & FOLLOW), 1038 ("relookup: symlink found.\n")); 1039 1040 /* ASSERT(dvp == ndp->ni_startdir) */ 1041 if (cnp->cn_flags & SAVESTART) 1042 VREF(dvp); 1043 1044 if ((cnp->cn_flags & LOCKLEAF) == 0) 1045 VOP_UNLOCK(dp, 0); 1046 return (0); 1047bad: 1048 vput(dp); 1049 *vpp = NULL; 1050 return (error); 1051} 1052 1053void 1054NDINIT_ALL(struct nameidata *ndp, u_long op, u_long flags, enum uio_seg segflg, 1055 const char *namep, int dirfd, struct vnode *startdir, cap_rights_t *rightsp, 1056 struct thread *td) 1057{ 1058 1059 ndp->ni_cnd.cn_nameiop = op; 1060 ndp->ni_cnd.cn_flags = flags; 1061 ndp->ni_segflg = segflg; 1062 ndp->ni_dirp = namep; 1063 ndp->ni_dirfd = dirfd; 1064 ndp->ni_startdir = startdir; 1065 ndp->ni_strictrelative = 0; 1066 if (rightsp != NULL) 1067 ndp->ni_rightsneeded = *rightsp; 1068 else 1069 cap_rights_init(&ndp->ni_rightsneeded); 1070 filecaps_init(&ndp->ni_filecaps); 1071 ndp->ni_cnd.cn_thread = td; 1072} 1073 1074/* 1075 * Free data allocated by namei(); see namei(9) for details. 1076 */ 1077void 1078NDFREE(struct nameidata *ndp, const u_int flags) 1079{ 1080 int unlock_dvp; 1081 int unlock_vp; 1082 1083 unlock_dvp = 0; 1084 unlock_vp = 0; 1085 1086 if (!(flags & NDF_NO_FREE_PNBUF) && 1087 (ndp->ni_cnd.cn_flags & HASBUF)) { 1088 uma_zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 1089 ndp->ni_cnd.cn_flags &= ~HASBUF; 1090 } 1091 if (!(flags & NDF_NO_VP_UNLOCK) && 1092 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 1093 unlock_vp = 1; 1094 if (!(flags & NDF_NO_VP_RELE) && ndp->ni_vp) { 1095 if (unlock_vp) { 1096 vput(ndp->ni_vp); 1097 unlock_vp = 0; 1098 } else 1099 vrele(ndp->ni_vp); 1100 ndp->ni_vp = NULL; 1101 } 1102 if (unlock_vp) 1103 VOP_UNLOCK(ndp->ni_vp, 0); 1104 if (!(flags & NDF_NO_DVP_UNLOCK) && 1105 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 1106 ndp->ni_dvp != ndp->ni_vp) 1107 unlock_dvp = 1; 1108 if (!(flags & NDF_NO_DVP_RELE) && 1109 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 1110 if (unlock_dvp) { 1111 vput(ndp->ni_dvp); 1112 unlock_dvp = 0; 1113 } else 1114 vrele(ndp->ni_dvp); 1115 ndp->ni_dvp = NULL; 1116 } 1117 if (unlock_dvp) 1118 VOP_UNLOCK(ndp->ni_dvp, 0); 1119 if (!(flags & NDF_NO_STARTDIR_RELE) && 1120 (ndp->ni_cnd.cn_flags & SAVESTART)) { 1121 vrele(ndp->ni_startdir); 1122 ndp->ni_startdir = NULL; 1123 } 1124} 1125 1126/* 1127 * Determine if there is a suitable alternate filename under the specified 1128 * prefix for the specified path. If the create flag is set, then the 1129 * alternate prefix will be used so long as the parent directory exists. 1130 * This is used by the various compatiblity ABIs so that Linux binaries prefer 1131 * files under /compat/linux for example. The chosen path (whether under 1132 * the prefix or under /) is returned in a kernel malloc'd buffer pointed 1133 * to by pathbuf. The caller is responsible for free'ing the buffer from 1134 * the M_TEMP bucket if one is returned. 1135 */ 1136int 1137kern_alternate_path(struct thread *td, const char *prefix, const char *path, 1138 enum uio_seg pathseg, char **pathbuf, int create, int dirfd) 1139{ 1140 struct nameidata nd, ndroot; 1141 char *ptr, *buf, *cp; 1142 size_t len, sz; 1143 int error; 1144 1145 buf = (char *) malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 1146 *pathbuf = buf; 1147 1148 /* Copy the prefix into the new pathname as a starting point. */ 1149 len = strlcpy(buf, prefix, MAXPATHLEN); 1150 if (len >= MAXPATHLEN) { 1151 *pathbuf = NULL; 1152 free(buf, M_TEMP); 1153 return (EINVAL); 1154 } 1155 sz = MAXPATHLEN - len; 1156 ptr = buf + len; 1157 1158 /* Append the filename to the prefix. */ 1159 if (pathseg == UIO_SYSSPACE) 1160 error = copystr(path, ptr, sz, &len); 1161 else 1162 error = copyinstr(path, ptr, sz, &len); 1163 1164 if (error) { 1165 *pathbuf = NULL; 1166 free(buf, M_TEMP); 1167 return (error); 1168 } 1169 1170 /* Only use a prefix with absolute pathnames. */ 1171 if (*ptr != '/') { 1172 error = EINVAL; 1173 goto keeporig; 1174 } 1175 1176 if (dirfd != AT_FDCWD) { 1177 /* 1178 * We want the original because the "prefix" is 1179 * included in the already opened dirfd. 1180 */ 1181 bcopy(ptr, buf, len); 1182 return (0); 1183 } 1184 1185 /* 1186 * We know that there is a / somewhere in this pathname. 1187 * Search backwards for it, to find the file's parent dir 1188 * to see if it exists in the alternate tree. If it does, 1189 * and we want to create a file (cflag is set). We don't 1190 * need to worry about the root comparison in this case. 1191 */ 1192 1193 if (create) { 1194 for (cp = &ptr[len] - 1; *cp != '/'; cp--); 1195 *cp = '\0'; 1196 1197 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1198 error = namei(&nd); 1199 *cp = '/'; 1200 if (error != 0) 1201 goto keeporig; 1202 } else { 1203 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, buf, td); 1204 1205 error = namei(&nd); 1206 if (error != 0) 1207 goto keeporig; 1208 1209 /* 1210 * We now compare the vnode of the prefix to the one 1211 * vnode asked. If they resolve to be the same, then we 1212 * ignore the match so that the real root gets used. 1213 * This avoids the problem of traversing "../.." to find the 1214 * root directory and never finding it, because "/" resolves 1215 * to the emulation root directory. This is expensive :-( 1216 */ 1217 NDINIT(&ndroot, LOOKUP, FOLLOW, UIO_SYSSPACE, prefix, 1218 td); 1219 1220 /* We shouldn't ever get an error from this namei(). */ 1221 error = namei(&ndroot); 1222 if (error == 0) { 1223 if (nd.ni_vp == ndroot.ni_vp) 1224 error = ENOENT; 1225 1226 NDFREE(&ndroot, NDF_ONLY_PNBUF); 1227 vrele(ndroot.ni_vp); 1228 } 1229 } 1230 1231 NDFREE(&nd, NDF_ONLY_PNBUF); 1232 vrele(nd.ni_vp); 1233 1234keeporig: 1235 /* If there was an error, use the original path name. */ 1236 if (error) 1237 bcopy(ptr, buf, len); 1238 return (error); 1239} 1240