1 /* $NetBSD: umap_vnops.c,v 1.49 2010/06/06 08:01:31 hannken Exp $ */ 2 3 /* 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software donated to Berkeley by 8 * the UCLA Ficus project. 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 * 3. 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 * @(#)umap_vnops.c 8.6 (Berkeley) 5/22/95 35 */ 36 37 /* 38 * Umap Layer 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: umap_vnops.c,v 1.49 2010/06/06 08:01:31 hannken Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/time.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/malloc.h> 51 #include <sys/buf.h> 52 #include <sys/kauth.h> 53 54 #include <miscfs/umapfs/umap.h> 55 #include <miscfs/genfs/genfs.h> 56 #include <miscfs/genfs/layer_extern.h> 57 58 /* 59 * Note: If the LAYERFS_MBYPASSDEBUG flag is set, it is possible 60 * that the debug printing will bomb out, because kauth routines 61 * do not handle NOCRED or FSCRED like other credentials and end 62 * up dereferencing an inappropriate pointer. 63 * 64 * That should be fixed in kauth rather than here. 65 */ 66 67 int umap_lookup(void *); 68 int umap_getattr(void *); 69 int umap_print(void *); 70 int umap_rename(void *); 71 72 /* 73 * Global vfs data structures 74 */ 75 /* 76 * XXX - strategy, bwrite are hand coded currently. They should 77 * go away with a merged buffer/block cache. 78 * 79 */ 80 int (**umap_vnodeop_p)(void *); 81 const struct vnodeopv_entry_desc umap_vnodeop_entries[] = { 82 { &vop_default_desc, umap_bypass }, 83 84 { &vop_lookup_desc, umap_lookup }, 85 { &vop_getattr_desc, umap_getattr }, 86 { &vop_print_desc, umap_print }, 87 { &vop_rename_desc, umap_rename }, 88 89 { &vop_lock_desc, layer_lock }, 90 { &vop_unlock_desc, layer_unlock }, 91 { &vop_islocked_desc, layer_islocked }, 92 { &vop_fsync_desc, layer_fsync }, 93 { &vop_inactive_desc, layer_inactive }, 94 { &vop_reclaim_desc, layer_reclaim }, 95 { &vop_open_desc, layer_open }, 96 { &vop_setattr_desc, layer_setattr }, 97 { &vop_access_desc, layer_access }, 98 { &vop_remove_desc, layer_remove }, 99 { &vop_rmdir_desc, layer_rmdir }, 100 101 { &vop_bwrite_desc, layer_bwrite }, 102 { &vop_bmap_desc, layer_bmap }, 103 { &vop_getpages_desc, layer_getpages }, 104 { &vop_putpages_desc, layer_putpages }, 105 106 { NULL, NULL } 107 }; 108 const struct vnodeopv_desc umapfs_vnodeop_opv_desc = 109 { &umap_vnodeop_p, umap_vnodeop_entries }; 110 111 /* 112 * This is the 08-June-1999 bypass routine. 113 * See layer_vnops.c:layer_bypass for more details. 114 */ 115 int 116 umap_bypass(void *v) 117 { 118 struct vop_generic_args /* { 119 struct vnodeop_desc *a_desc; 120 <other random data follows, presumably> 121 } */ *ap = v; 122 int (**our_vnodeop_p)(void *); 123 kauth_cred_t *credpp = NULL, credp = 0; 124 kauth_cred_t savecredp = 0, savecompcredp = 0; 125 kauth_cred_t compcredp = 0; 126 struct vnode **this_vp_p; 127 int error; 128 struct vnode *old_vps[VDESC_MAX_VPS], *vp0; 129 struct vnode **vps_p[VDESC_MAX_VPS]; 130 struct vnode ***vppp; 131 struct vnodeop_desc *descp = ap->a_desc; 132 int reles, i, flags; 133 struct componentname **compnamepp = 0; 134 135 #ifdef DIAGNOSTIC 136 /* 137 * We require at least one vp. 138 */ 139 if (descp->vdesc_vp_offsets == NULL || 140 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET) 141 panic("%s: no vp's in map.\n", __func__); 142 #endif 143 144 vps_p[0] = 145 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 146 vp0 = *vps_p[0]; 147 flags = MOUNTTOUMAPMOUNT(vp0->v_mount)->umapm_flags; 148 our_vnodeop_p = vp0->v_op; 149 150 if (flags & LAYERFS_MBYPASSDEBUG) 151 printf("%s: %s\n", __func__, descp->vdesc_name); 152 153 /* 154 * Map the vnodes going in. 155 * Later, we'll invoke the operation based on 156 * the first mapped vnode's operation vector. 157 */ 158 reles = descp->vdesc_flags; 159 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 160 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 161 break; /* bail out at end of list */ 162 vps_p[i] = this_vp_p = 163 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], 164 ap); 165 /* 166 * We're not guaranteed that any but the first vnode 167 * are of our type. Check for and don't map any 168 * that aren't. (We must always map first vp or vclean fails.) 169 */ 170 if (i && (*this_vp_p == NULL || 171 (*this_vp_p)->v_op != our_vnodeop_p)) { 172 old_vps[i] = NULL; 173 } else { 174 old_vps[i] = *this_vp_p; 175 *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p); 176 /* 177 * XXX - Several operations have the side effect 178 * of vrele'ing their vp's. We must account for 179 * that. (This should go away in the future.) 180 */ 181 if (reles & VDESC_VP0_WILLRELE) 182 vref(*this_vp_p); 183 } 184 185 } 186 187 /* 188 * Fix the credentials. (That's the purpose of this layer.) 189 */ 190 191 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 192 193 credpp = VOPARG_OFFSETTO(kauth_cred_t*, 194 descp->vdesc_cred_offset, ap); 195 196 /* Save old values */ 197 198 savecredp = *credpp; 199 if (savecredp != NOCRED && savecredp != FSCRED) 200 *credpp = kauth_cred_dup(savecredp); 201 credp = *credpp; 202 203 if ((flags & LAYERFS_MBYPASSDEBUG) && 204 kauth_cred_geteuid(credp) != 0) 205 printf("umap_bypass: user was %d, group %d\n", 206 kauth_cred_geteuid(credp), kauth_cred_getegid(credp)); 207 208 /* Map all ids in the credential structure. */ 209 210 umap_mapids(vp0->v_mount, credp); 211 212 if ((flags & LAYERFS_MBYPASSDEBUG) && 213 kauth_cred_geteuid(credp) != 0) 214 printf("umap_bypass: user now %d, group %d\n", 215 kauth_cred_geteuid(credp), kauth_cred_getegid(credp)); 216 } 217 218 /* BSD often keeps a credential in the componentname structure 219 * for speed. If there is one, it better get mapped, too. 220 */ 221 222 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 223 224 compnamepp = VOPARG_OFFSETTO(struct componentname**, 225 descp->vdesc_componentname_offset, ap); 226 227 savecompcredp = (*compnamepp)->cn_cred; 228 if (savecompcredp != NOCRED && savecompcredp != FSCRED) 229 (*compnamepp)->cn_cred = kauth_cred_dup(savecompcredp); 230 compcredp = (*compnamepp)->cn_cred; 231 232 if ((flags & LAYERFS_MBYPASSDEBUG) && 233 kauth_cred_geteuid(compcredp) != 0) 234 printf("umap_bypass: component credit user was %d, group %d\n", 235 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 236 237 /* Map all ids in the credential structure. */ 238 239 umap_mapids(vp0->v_mount, compcredp); 240 241 if ((flags & LAYERFS_MBYPASSDEBUG) && 242 kauth_cred_geteuid(compcredp) != 0) 243 printf("umap_bypass: component credit user now %d, group %d\n", 244 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 245 } 246 247 /* 248 * Call the operation on the lower layer 249 * with the modified argument structure. 250 */ 251 error = VCALL(*vps_p[0], descp->vdesc_offset, ap); 252 253 /* 254 * Maintain the illusion of call-by-value 255 * by restoring vnodes in the argument structure 256 * to their original value. 257 */ 258 reles = descp->vdesc_flags; 259 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 260 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 261 break; /* bail out at end of list */ 262 if (old_vps[i]) { 263 *(vps_p[i]) = old_vps[i]; 264 if (reles & VDESC_VP0_WILLRELE) 265 vrele(*(vps_p[i])); 266 } 267 } 268 269 /* 270 * Map the possible out-going vpp 271 * (Assumes that the lower layer always returns 272 * a VREF'ed vpp unless it gets an error.) 273 */ 274 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 275 !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 276 !error) { 277 /* 278 * XXX - even though some ops have vpp returned vp's, 279 * several ops actually vrele this before returning. 280 * We must avoid these ops. 281 * (This should go away when these ops are regularized.) 282 */ 283 if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 284 goto out; 285 vppp = VOPARG_OFFSETTO(struct vnode***, 286 descp->vdesc_vpp_offset, ap); 287 /* 288 * Only vop_lookup, vop_create, vop_makedir, vop_bmap, 289 * vop_mknod, and vop_symlink return vpp's. vop_bmap 290 * doesn't call bypass as the lower vpp is fine (we're just 291 * going to do i/o on it). vop_lookup doesn't call bypass 292 * as a lookup on "." would generate a locking error. 293 * So all the calls which get us here have a locked vpp. :-) 294 */ 295 error = layer_node_create(old_vps[0]->v_mount, **vppp, *vppp); 296 if (error) { 297 vput(**vppp); 298 **vppp = NULL; 299 } 300 } 301 302 out: 303 /* 304 * Free duplicate cred structure and restore old one. 305 */ 306 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 307 if ((flags & LAYERFS_MBYPASSDEBUG) && credp && 308 kauth_cred_geteuid(credp) != 0) 309 printf("umap_bypass: returning-user was %d\n", 310 kauth_cred_geteuid(credp)); 311 312 if (savecredp != NOCRED && savecredp != FSCRED && credpp) { 313 kauth_cred_free(credp); 314 *credpp = savecredp; 315 if ((flags & LAYERFS_MBYPASSDEBUG) && credpp && 316 kauth_cred_geteuid(*credpp) != 0) 317 printf("umap_bypass: returning-user now %d\n\n", 318 kauth_cred_geteuid(savecredp)); 319 } 320 } 321 322 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 323 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 324 kauth_cred_geteuid(compcredp) != 0) 325 printf("umap_bypass: returning-component-user was %d\n", 326 kauth_cred_geteuid(compcredp)); 327 328 if (savecompcredp != NOCRED && savecompcredp != FSCRED) { 329 kauth_cred_free(compcredp); 330 (*compnamepp)->cn_cred = savecompcredp; 331 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 332 kauth_cred_geteuid(savecompcredp) != 0) 333 printf("umap_bypass: returning-component-user now %d\n", 334 kauth_cred_geteuid(savecompcredp)); 335 } 336 } 337 338 return (error); 339 } 340 341 /* 342 * This is based on the 08-June-1999 bypass routine. 343 * See layer_vnops.c:layer_bypass for more details. 344 */ 345 int 346 umap_lookup(void *v) 347 { 348 struct vop_lookup_args /* { 349 struct vnodeop_desc *a_desc; 350 struct vnode * a_dvp; 351 struct vnode ** a_vpp; 352 struct componentname * a_cnp; 353 } */ *ap = v; 354 struct componentname *cnp = ap->a_cnp; 355 kauth_cred_t savecompcredp = NULL; 356 kauth_cred_t compcredp = NULL; 357 struct vnode *dvp, *vp, *ldvp; 358 struct mount *mp; 359 int error; 360 int flags, cnf = cnp->cn_flags; 361 362 dvp = ap->a_dvp; 363 mp = dvp->v_mount; 364 365 if ((cnf & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 366 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 367 return (EROFS); 368 369 flags = MOUNTTOUMAPMOUNT(mp)->umapm_flags; 370 ldvp = UMAPVPTOLOWERVP(dvp); 371 372 if (flags & LAYERFS_MBYPASSDEBUG) 373 printf("umap_lookup\n"); 374 375 /* 376 * Fix the credentials. (That's the purpose of this layer.) 377 * 378 * BSD often keeps a credential in the componentname structure 379 * for speed. If there is one, it better get mapped, too. 380 */ 381 382 if ((savecompcredp = cnp->cn_cred)) { 383 compcredp = kauth_cred_dup(savecompcredp); 384 cnp->cn_cred = compcredp; 385 386 if ((flags & LAYERFS_MBYPASSDEBUG) && 387 kauth_cred_geteuid(compcredp) != 0) 388 printf("umap_lookup: component credit user was %d, group %d\n", 389 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 390 391 /* Map all ids in the credential structure. */ 392 umap_mapids(mp, compcredp); 393 } 394 395 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 396 kauth_cred_geteuid(compcredp) != 0) 397 printf("umap_lookup: component credit user now %d, group %d\n", 398 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 399 400 ap->a_dvp = ldvp; 401 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap); 402 vp = *ap->a_vpp; 403 *ap->a_vpp = NULL; 404 405 if (error == EJUSTRETURN && (cnf & ISLASTCN) && 406 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 407 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 408 error = EROFS; 409 410 /* Do locking fixup as appropriate. See layer_lookup() for info */ 411 if (ldvp == vp) { 412 *ap->a_vpp = dvp; 413 vref(dvp); 414 vrele(vp); 415 } else if (vp != NULL) { 416 error = layer_node_create(mp, vp, ap->a_vpp); 417 if (error) { 418 vput(vp); 419 } 420 } 421 422 /* 423 * Free duplicate cred structure and restore old one. 424 */ 425 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 426 kauth_cred_geteuid(compcredp) != 0) 427 printf("umap_lookup: returning-component-user was %d\n", 428 kauth_cred_geteuid(compcredp)); 429 430 if (savecompcredp != NOCRED && savecompcredp != FSCRED) { 431 if (compcredp) 432 kauth_cred_free(compcredp); 433 cnp->cn_cred = savecompcredp; 434 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 435 kauth_cred_geteuid(savecompcredp) != 0) 436 printf("umap_lookup: returning-component-user now %d\n", 437 kauth_cred_geteuid(savecompcredp)); 438 } 439 440 return (error); 441 } 442 443 /* 444 * We handle getattr to change the fsid. 445 */ 446 int 447 umap_getattr(void *v) 448 { 449 struct vop_getattr_args /* { 450 struct vnode *a_vp; 451 struct vattr *a_vap; 452 kauth_cred_t a_cred; 453 struct lwp *a_l; 454 } */ *ap = v; 455 uid_t uid; 456 gid_t gid; 457 int error, tmpid, nentries, gnentries, flags; 458 u_long (*mapdata)[2]; 459 u_long (*gmapdata)[2]; 460 struct vnode **vp1p; 461 const struct vnodeop_desc *descp = ap->a_desc; 462 463 if ((error = umap_bypass(ap)) != 0) 464 return (error); 465 /* Requires that arguments be restored. */ 466 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 467 468 flags = MOUNTTOUMAPMOUNT(ap->a_vp->v_mount)->umapm_flags; 469 /* 470 * Umap needs to map the uid and gid returned by a stat 471 * into the proper values for this site. This involves 472 * finding the returned uid in the mapping information, 473 * translating it into the uid on the other end, 474 * and filling in the proper field in the vattr 475 * structure pointed to by ap->a_vap. The group 476 * is easier, since currently all groups will be 477 * translate to the NULLGROUP. 478 */ 479 480 /* Find entry in map */ 481 482 uid = ap->a_vap->va_uid; 483 gid = ap->a_vap->va_gid; 484 if ((flags & LAYERFS_MBYPASSDEBUG)) 485 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid, 486 gid); 487 488 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 489 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries; 490 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata); 491 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries; 492 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata); 493 494 /* Reverse map the uid for the vnode. Since it's a reverse 495 map, we can't use umap_mapids() to do it. */ 496 497 tmpid = umap_reverse_findid(uid, mapdata, nentries); 498 499 if (tmpid != -1) { 500 ap->a_vap->va_uid = (uid_t) tmpid; 501 if ((flags & LAYERFS_MBYPASSDEBUG)) 502 printf("umap_getattr: original uid = %d\n", uid); 503 } else 504 ap->a_vap->va_uid = (uid_t) NOBODY; 505 506 /* Reverse map the gid for the vnode. */ 507 508 tmpid = umap_reverse_findid(gid, gmapdata, gnentries); 509 510 if (tmpid != -1) { 511 ap->a_vap->va_gid = (gid_t) tmpid; 512 if ((flags & LAYERFS_MBYPASSDEBUG)) 513 printf("umap_getattr: original gid = %d\n", gid); 514 } else 515 ap->a_vap->va_gid = (gid_t) NULLGROUP; 516 517 return (0); 518 } 519 520 int 521 umap_print(void *v) 522 { 523 struct vop_print_args /* { 524 struct vnode *a_vp; 525 } */ *ap = v; 526 struct vnode *vp = ap->a_vp; 527 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, 528 UMAPVPTOLOWERVP(vp)); 529 return (0); 530 } 531 532 int 533 umap_rename(void *v) 534 { 535 struct vop_rename_args /* { 536 struct vnode *a_fdvp; 537 struct vnode *a_fvp; 538 struct componentname *a_fcnp; 539 struct vnode *a_tdvp; 540 struct vnode *a_tvp; 541 struct componentname *a_tcnp; 542 } */ *ap = v; 543 int error, flags; 544 struct componentname *compnamep; 545 kauth_cred_t compcredp, savecompcredp; 546 struct vnode *vp; 547 struct vnode *tvp; 548 549 /* 550 * Rename is irregular, having two componentname structures. 551 * We need to map the cre in the second structure, 552 * and then bypass takes care of the rest. 553 */ 554 555 vp = ap->a_fdvp; 556 flags = MOUNTTOUMAPMOUNT(vp->v_mount)->umapm_flags; 557 compnamep = ap->a_tcnp; 558 compcredp = compnamep->cn_cred; 559 560 savecompcredp = compcredp; 561 compcredp = compnamep->cn_cred = kauth_cred_dup(savecompcredp); 562 563 if ((flags & LAYERFS_MBYPASSDEBUG) && 564 kauth_cred_geteuid(compcredp) != 0) 565 printf("umap_rename: rename component credit user was %d, group %d\n", 566 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 567 568 /* Map all ids in the credential structure. */ 569 570 umap_mapids(vp->v_mount, compcredp); 571 572 if ((flags & LAYERFS_MBYPASSDEBUG) && 573 kauth_cred_geteuid(compcredp) != 0) 574 printf("umap_rename: rename component credit user now %d, group %d\n", 575 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 576 577 tvp = ap->a_tvp; 578 if (tvp) { 579 if (tvp->v_mount != vp->v_mount) 580 tvp = NULL; 581 else 582 vref(tvp); 583 } 584 error = umap_bypass(ap); 585 if (tvp) { 586 if (error == 0) 587 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED; 588 vrele(tvp); 589 } 590 591 /* Restore the additional mapped componentname cred structure. */ 592 593 kauth_cred_free(compcredp); 594 compnamep->cn_cred = savecompcredp; 595 596 return error; 597 } 598