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