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