1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/param.h> 28 #include <sys/time.h> 29 #include <sys/systm.h> 30 #include <sys/sysmacros.h> 31 #include <sys/resource.h> 32 #include <sys/vfs.h> 33 #include <sys/vnode.h> 34 #include <sys/sid.h> 35 #include <sys/file.h> 36 #include <sys/stat.h> 37 #include <sys/kmem.h> 38 #include <sys/cmn_err.h> 39 #include <sys/errno.h> 40 #include <sys/unistd.h> 41 #include <sys/sdt.h> 42 #include <sys/fs/zfs.h> 43 #include <sys/mode.h> 44 #include <sys/policy.h> 45 #include <sys/zfs_znode.h> 46 #include <sys/zfs_fuid.h> 47 #include <sys/zfs_acl.h> 48 #include <sys/zfs_dir.h> 49 #include <sys/zfs_vfsops.h> 50 #include <sys/dmu.h> 51 #include <sys/dnode.h> 52 #include <sys/zap.h> 53 #include "fs/fs_subr.h" 54 #include <acl/acl_common.h> 55 56 #define ALLOW ACE_ACCESS_ALLOWED_ACE_TYPE 57 #define DENY ACE_ACCESS_DENIED_ACE_TYPE 58 #define MAX_ACE_TYPE ACE_SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE 59 #define MIN_ACE_TYPE ALLOW 60 61 #define OWNING_GROUP (ACE_GROUP|ACE_IDENTIFIER_GROUP) 62 #define EVERYONE_ALLOW_MASK (ACE_READ_ACL|ACE_READ_ATTRIBUTES | \ 63 ACE_READ_NAMED_ATTRS|ACE_SYNCHRONIZE) 64 #define EVERYONE_DENY_MASK (ACE_WRITE_ACL|ACE_WRITE_OWNER | \ 65 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS) 66 #define OWNER_ALLOW_MASK (ACE_WRITE_ACL | ACE_WRITE_OWNER | \ 67 ACE_WRITE_ATTRIBUTES|ACE_WRITE_NAMED_ATTRS) 68 69 #define ZFS_CHECKED_MASKS (ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_READ_DATA| \ 70 ACE_READ_NAMED_ATTRS|ACE_WRITE_DATA|ACE_WRITE_ATTRIBUTES| \ 71 ACE_WRITE_NAMED_ATTRS|ACE_APPEND_DATA|ACE_EXECUTE|ACE_WRITE_OWNER| \ 72 ACE_WRITE_ACL|ACE_DELETE|ACE_DELETE_CHILD|ACE_SYNCHRONIZE) 73 74 #define WRITE_MASK_DATA (ACE_WRITE_DATA|ACE_APPEND_DATA|ACE_WRITE_NAMED_ATTRS) 75 #define WRITE_MASK_ATTRS (ACE_WRITE_ACL|ACE_WRITE_OWNER|ACE_WRITE_ATTRIBUTES| \ 76 ACE_DELETE|ACE_DELETE_CHILD) 77 #define WRITE_MASK (WRITE_MASK_DATA|WRITE_MASK_ATTRS) 78 79 #define OGE_CLEAR (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \ 80 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE) 81 82 #define OKAY_MASK_BITS (ACE_READ_DATA|ACE_LIST_DIRECTORY|ACE_WRITE_DATA| \ 83 ACE_ADD_FILE|ACE_APPEND_DATA|ACE_ADD_SUBDIRECTORY|ACE_EXECUTE) 84 85 #define ALL_INHERIT (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE | \ 86 ACE_NO_PROPAGATE_INHERIT_ACE|ACE_INHERIT_ONLY_ACE|ACE_INHERITED_ACE) 87 88 #define RESTRICTED_CLEAR (ACE_WRITE_ACL|ACE_WRITE_OWNER) 89 90 #define V4_ACL_WIDE_FLAGS (ZFS_ACL_AUTO_INHERIT|ZFS_ACL_DEFAULTED|\ 91 ZFS_ACL_PROTECTED) 92 93 #define ZFS_ACL_WIDE_FLAGS (V4_ACL_WIDE_FLAGS|ZFS_ACL_TRIVIAL|ZFS_INHERIT_ACE|\ 94 ZFS_ACL_OBJ_ACE) 95 96 static uint16_t 97 zfs_ace_v0_get_type(void *acep) 98 { 99 return (((zfs_oldace_t *)acep)->z_type); 100 } 101 102 static uint16_t 103 zfs_ace_v0_get_flags(void *acep) 104 { 105 return (((zfs_oldace_t *)acep)->z_flags); 106 } 107 108 static uint32_t 109 zfs_ace_v0_get_mask(void *acep) 110 { 111 return (((zfs_oldace_t *)acep)->z_access_mask); 112 } 113 114 static uint64_t 115 zfs_ace_v0_get_who(void *acep) 116 { 117 return (((zfs_oldace_t *)acep)->z_fuid); 118 } 119 120 static void 121 zfs_ace_v0_set_type(void *acep, uint16_t type) 122 { 123 ((zfs_oldace_t *)acep)->z_type = type; 124 } 125 126 static void 127 zfs_ace_v0_set_flags(void *acep, uint16_t flags) 128 { 129 ((zfs_oldace_t *)acep)->z_flags = flags; 130 } 131 132 static void 133 zfs_ace_v0_set_mask(void *acep, uint32_t mask) 134 { 135 ((zfs_oldace_t *)acep)->z_access_mask = mask; 136 } 137 138 static void 139 zfs_ace_v0_set_who(void *acep, uint64_t who) 140 { 141 ((zfs_oldace_t *)acep)->z_fuid = who; 142 } 143 144 /*ARGSUSED*/ 145 static size_t 146 zfs_ace_v0_size(void *acep) 147 { 148 return (sizeof (zfs_oldace_t)); 149 } 150 151 static size_t 152 zfs_ace_v0_abstract_size(void) 153 { 154 return (sizeof (zfs_oldace_t)); 155 } 156 157 static int 158 zfs_ace_v0_mask_off(void) 159 { 160 return (offsetof(zfs_oldace_t, z_access_mask)); 161 } 162 163 /*ARGSUSED*/ 164 static int 165 zfs_ace_v0_data(void *acep, void **datap) 166 { 167 *datap = NULL; 168 return (0); 169 } 170 171 static acl_ops_t zfs_acl_v0_ops = { 172 zfs_ace_v0_get_mask, 173 zfs_ace_v0_set_mask, 174 zfs_ace_v0_get_flags, 175 zfs_ace_v0_set_flags, 176 zfs_ace_v0_get_type, 177 zfs_ace_v0_set_type, 178 zfs_ace_v0_get_who, 179 zfs_ace_v0_set_who, 180 zfs_ace_v0_size, 181 zfs_ace_v0_abstract_size, 182 zfs_ace_v0_mask_off, 183 zfs_ace_v0_data 184 }; 185 186 static uint16_t 187 zfs_ace_fuid_get_type(void *acep) 188 { 189 return (((zfs_ace_hdr_t *)acep)->z_type); 190 } 191 192 static uint16_t 193 zfs_ace_fuid_get_flags(void *acep) 194 { 195 return (((zfs_ace_hdr_t *)acep)->z_flags); 196 } 197 198 static uint32_t 199 zfs_ace_fuid_get_mask(void *acep) 200 { 201 return (((zfs_ace_hdr_t *)acep)->z_access_mask); 202 } 203 204 static uint64_t 205 zfs_ace_fuid_get_who(void *args) 206 { 207 uint16_t entry_type; 208 zfs_ace_t *acep = args; 209 210 entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS; 211 212 if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP || 213 entry_type == ACE_EVERYONE) 214 return (-1); 215 return (((zfs_ace_t *)acep)->z_fuid); 216 } 217 218 static void 219 zfs_ace_fuid_set_type(void *acep, uint16_t type) 220 { 221 ((zfs_ace_hdr_t *)acep)->z_type = type; 222 } 223 224 static void 225 zfs_ace_fuid_set_flags(void *acep, uint16_t flags) 226 { 227 ((zfs_ace_hdr_t *)acep)->z_flags = flags; 228 } 229 230 static void 231 zfs_ace_fuid_set_mask(void *acep, uint32_t mask) 232 { 233 ((zfs_ace_hdr_t *)acep)->z_access_mask = mask; 234 } 235 236 static void 237 zfs_ace_fuid_set_who(void *arg, uint64_t who) 238 { 239 zfs_ace_t *acep = arg; 240 241 uint16_t entry_type = acep->z_hdr.z_flags & ACE_TYPE_FLAGS; 242 243 if (entry_type == ACE_OWNER || entry_type == OWNING_GROUP || 244 entry_type == ACE_EVERYONE) 245 return; 246 acep->z_fuid = who; 247 } 248 249 static size_t 250 zfs_ace_fuid_size(void *acep) 251 { 252 zfs_ace_hdr_t *zacep = acep; 253 uint16_t entry_type; 254 255 switch (zacep->z_type) { 256 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 257 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 258 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 259 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 260 return (sizeof (zfs_object_ace_t)); 261 case ALLOW: 262 case DENY: 263 entry_type = 264 (((zfs_ace_hdr_t *)acep)->z_flags & ACE_TYPE_FLAGS); 265 if (entry_type == ACE_OWNER || 266 entry_type == OWNING_GROUP || 267 entry_type == ACE_EVERYONE) 268 return (sizeof (zfs_ace_hdr_t)); 269 /*FALLTHROUGH*/ 270 default: 271 return (sizeof (zfs_ace_t)); 272 } 273 } 274 275 static size_t 276 zfs_ace_fuid_abstract_size(void) 277 { 278 return (sizeof (zfs_ace_hdr_t)); 279 } 280 281 static int 282 zfs_ace_fuid_mask_off(void) 283 { 284 return (offsetof(zfs_ace_hdr_t, z_access_mask)); 285 } 286 287 static int 288 zfs_ace_fuid_data(void *acep, void **datap) 289 { 290 zfs_ace_t *zacep = acep; 291 zfs_object_ace_t *zobjp; 292 293 switch (zacep->z_hdr.z_type) { 294 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 295 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 296 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 297 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 298 zobjp = acep; 299 *datap = (caddr_t)zobjp + sizeof (zfs_ace_t); 300 return (sizeof (zfs_object_ace_t) - sizeof (zfs_ace_t)); 301 default: 302 *datap = NULL; 303 return (0); 304 } 305 } 306 307 static acl_ops_t zfs_acl_fuid_ops = { 308 zfs_ace_fuid_get_mask, 309 zfs_ace_fuid_set_mask, 310 zfs_ace_fuid_get_flags, 311 zfs_ace_fuid_set_flags, 312 zfs_ace_fuid_get_type, 313 zfs_ace_fuid_set_type, 314 zfs_ace_fuid_get_who, 315 zfs_ace_fuid_set_who, 316 zfs_ace_fuid_size, 317 zfs_ace_fuid_abstract_size, 318 zfs_ace_fuid_mask_off, 319 zfs_ace_fuid_data 320 }; 321 322 static int 323 zfs_acl_version(int version) 324 { 325 if (version < ZPL_VERSION_FUID) 326 return (ZFS_ACL_VERSION_INITIAL); 327 else 328 return (ZFS_ACL_VERSION_FUID); 329 } 330 331 static int 332 zfs_acl_version_zp(znode_t *zp) 333 { 334 return (zfs_acl_version(zp->z_zfsvfs->z_version)); 335 } 336 337 static zfs_acl_t * 338 zfs_acl_alloc(int vers) 339 { 340 zfs_acl_t *aclp; 341 342 aclp = kmem_zalloc(sizeof (zfs_acl_t), KM_SLEEP); 343 list_create(&aclp->z_acl, sizeof (zfs_acl_node_t), 344 offsetof(zfs_acl_node_t, z_next)); 345 aclp->z_version = vers; 346 if (vers == ZFS_ACL_VERSION_FUID) 347 aclp->z_ops = zfs_acl_fuid_ops; 348 else 349 aclp->z_ops = zfs_acl_v0_ops; 350 return (aclp); 351 } 352 353 static zfs_acl_node_t * 354 zfs_acl_node_alloc(size_t bytes) 355 { 356 zfs_acl_node_t *aclnode; 357 358 aclnode = kmem_zalloc(sizeof (zfs_acl_node_t), KM_SLEEP); 359 if (bytes) { 360 aclnode->z_acldata = kmem_alloc(bytes, KM_SLEEP); 361 aclnode->z_allocdata = aclnode->z_acldata; 362 aclnode->z_allocsize = bytes; 363 aclnode->z_size = bytes; 364 } 365 366 return (aclnode); 367 } 368 369 static void 370 zfs_acl_node_free(zfs_acl_node_t *aclnode) 371 { 372 if (aclnode->z_allocsize) 373 kmem_free(aclnode->z_allocdata, aclnode->z_allocsize); 374 kmem_free(aclnode, sizeof (zfs_acl_node_t)); 375 } 376 377 static void 378 zfs_acl_release_nodes(zfs_acl_t *aclp) 379 { 380 zfs_acl_node_t *aclnode; 381 382 while (aclnode = list_head(&aclp->z_acl)) { 383 list_remove(&aclp->z_acl, aclnode); 384 zfs_acl_node_free(aclnode); 385 } 386 aclp->z_acl_count = 0; 387 aclp->z_acl_bytes = 0; 388 } 389 390 void 391 zfs_acl_free(zfs_acl_t *aclp) 392 { 393 zfs_acl_release_nodes(aclp); 394 list_destroy(&aclp->z_acl); 395 kmem_free(aclp, sizeof (zfs_acl_t)); 396 } 397 398 static boolean_t 399 zfs_acl_valid_ace_type(uint_t type, uint_t flags) 400 { 401 uint16_t entry_type; 402 403 switch (type) { 404 case ALLOW: 405 case DENY: 406 case ACE_SYSTEM_AUDIT_ACE_TYPE: 407 case ACE_SYSTEM_ALARM_ACE_TYPE: 408 entry_type = flags & ACE_TYPE_FLAGS; 409 return (entry_type == ACE_OWNER || 410 entry_type == OWNING_GROUP || 411 entry_type == ACE_EVERYONE || entry_type == 0 || 412 entry_type == ACE_IDENTIFIER_GROUP); 413 default: 414 if (type >= MIN_ACE_TYPE && type <= MAX_ACE_TYPE) 415 return (B_TRUE); 416 } 417 return (B_FALSE); 418 } 419 420 static boolean_t 421 zfs_ace_valid(vtype_t obj_type, zfs_acl_t *aclp, uint16_t type, uint16_t iflags) 422 { 423 /* 424 * first check type of entry 425 */ 426 427 if (!zfs_acl_valid_ace_type(type, iflags)) 428 return (B_FALSE); 429 430 switch (type) { 431 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 432 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 433 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 434 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 435 if (aclp->z_version < ZFS_ACL_VERSION_FUID) 436 return (B_FALSE); 437 aclp->z_hints |= ZFS_ACL_OBJ_ACE; 438 } 439 440 /* 441 * next check inheritance level flags 442 */ 443 444 if (obj_type == VDIR && 445 (iflags & (ACE_FILE_INHERIT_ACE|ACE_DIRECTORY_INHERIT_ACE))) 446 aclp->z_hints |= ZFS_INHERIT_ACE; 447 448 if (iflags & (ACE_INHERIT_ONLY_ACE|ACE_NO_PROPAGATE_INHERIT_ACE)) { 449 if ((iflags & (ACE_FILE_INHERIT_ACE| 450 ACE_DIRECTORY_INHERIT_ACE)) == 0) { 451 return (B_FALSE); 452 } 453 } 454 455 return (B_TRUE); 456 } 457 458 static void * 459 zfs_acl_next_ace(zfs_acl_t *aclp, void *start, uint64_t *who, 460 uint32_t *access_mask, uint16_t *iflags, uint16_t *type) 461 { 462 zfs_acl_node_t *aclnode; 463 464 if (start == NULL) { 465 aclnode = list_head(&aclp->z_acl); 466 if (aclnode == NULL) 467 return (NULL); 468 469 aclp->z_next_ace = aclnode->z_acldata; 470 aclp->z_curr_node = aclnode; 471 aclnode->z_ace_idx = 0; 472 } 473 474 aclnode = aclp->z_curr_node; 475 476 if (aclnode == NULL) 477 return (NULL); 478 479 if (aclnode->z_ace_idx >= aclnode->z_ace_count) { 480 aclnode = list_next(&aclp->z_acl, aclnode); 481 if (aclnode == NULL) 482 return (NULL); 483 else { 484 aclp->z_curr_node = aclnode; 485 aclnode->z_ace_idx = 0; 486 aclp->z_next_ace = aclnode->z_acldata; 487 } 488 } 489 490 if (aclnode->z_ace_idx < aclnode->z_ace_count) { 491 void *acep = aclp->z_next_ace; 492 size_t ace_size; 493 494 /* 495 * Make sure we don't overstep our bounds 496 */ 497 ace_size = aclp->z_ops.ace_size(acep); 498 499 if (((caddr_t)acep + ace_size) > 500 ((caddr_t)aclnode->z_acldata + aclnode->z_size)) { 501 return (NULL); 502 } 503 504 *iflags = aclp->z_ops.ace_flags_get(acep); 505 *type = aclp->z_ops.ace_type_get(acep); 506 *access_mask = aclp->z_ops.ace_mask_get(acep); 507 *who = aclp->z_ops.ace_who_get(acep); 508 aclp->z_next_ace = (caddr_t)aclp->z_next_ace + ace_size; 509 aclnode->z_ace_idx++; 510 return ((void *)acep); 511 } 512 return (NULL); 513 } 514 515 /*ARGSUSED*/ 516 static uint64_t 517 zfs_ace_walk(void *datap, uint64_t cookie, int aclcnt, 518 uint16_t *flags, uint16_t *type, uint32_t *mask) 519 { 520 zfs_acl_t *aclp = datap; 521 zfs_ace_hdr_t *acep = (zfs_ace_hdr_t *)(uintptr_t)cookie; 522 uint64_t who; 523 524 acep = zfs_acl_next_ace(aclp, acep, &who, mask, 525 flags, type); 526 return ((uint64_t)(uintptr_t)acep); 527 } 528 529 static zfs_acl_node_t * 530 zfs_acl_curr_node(zfs_acl_t *aclp) 531 { 532 ASSERT(aclp->z_curr_node); 533 return (aclp->z_curr_node); 534 } 535 536 /* 537 * Copy ACE to internal ZFS format. 538 * While processing the ACL each ACE will be validated for correctness. 539 * ACE FUIDs will be created later. 540 */ 541 int 542 zfs_copy_ace_2_fuid(zfsvfs_t *zfsvfs, vtype_t obj_type, zfs_acl_t *aclp, 543 void *datap, zfs_ace_t *z_acl, int aclcnt, size_t *size, 544 zfs_fuid_info_t **fuidp, cred_t *cr) 545 { 546 int i; 547 uint16_t entry_type; 548 zfs_ace_t *aceptr = z_acl; 549 ace_t *acep = datap; 550 zfs_object_ace_t *zobjacep; 551 ace_object_t *aceobjp; 552 553 for (i = 0; i != aclcnt; i++) { 554 aceptr->z_hdr.z_access_mask = acep->a_access_mask; 555 aceptr->z_hdr.z_flags = acep->a_flags; 556 aceptr->z_hdr.z_type = acep->a_type; 557 entry_type = aceptr->z_hdr.z_flags & ACE_TYPE_FLAGS; 558 if (entry_type != ACE_OWNER && entry_type != OWNING_GROUP && 559 entry_type != ACE_EVERYONE) { 560 aceptr->z_fuid = zfs_fuid_create(zfsvfs, acep->a_who, 561 cr, (entry_type == 0) ? 562 ZFS_ACE_USER : ZFS_ACE_GROUP, fuidp); 563 } 564 565 /* 566 * Make sure ACE is valid 567 */ 568 if (zfs_ace_valid(obj_type, aclp, aceptr->z_hdr.z_type, 569 aceptr->z_hdr.z_flags) != B_TRUE) 570 return (EINVAL); 571 572 switch (acep->a_type) { 573 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 574 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 575 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 576 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 577 zobjacep = (zfs_object_ace_t *)aceptr; 578 aceobjp = (ace_object_t *)acep; 579 580 bcopy(aceobjp->a_obj_type, zobjacep->z_object_type, 581 sizeof (aceobjp->a_obj_type)); 582 bcopy(aceobjp->a_inherit_obj_type, 583 zobjacep->z_inherit_type, 584 sizeof (aceobjp->a_inherit_obj_type)); 585 acep = (ace_t *)((caddr_t)acep + sizeof (ace_object_t)); 586 break; 587 default: 588 acep = (ace_t *)((caddr_t)acep + sizeof (ace_t)); 589 } 590 591 aceptr = (zfs_ace_t *)((caddr_t)aceptr + 592 aclp->z_ops.ace_size(aceptr)); 593 } 594 595 *size = (caddr_t)aceptr - (caddr_t)z_acl; 596 597 return (0); 598 } 599 600 /* 601 * Copy ZFS ACEs to fixed size ace_t layout 602 */ 603 static void 604 zfs_copy_fuid_2_ace(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, cred_t *cr, 605 void *datap, int filter) 606 { 607 uint64_t who; 608 uint32_t access_mask; 609 uint16_t iflags, type; 610 zfs_ace_hdr_t *zacep = NULL; 611 ace_t *acep = datap; 612 ace_object_t *objacep; 613 zfs_object_ace_t *zobjacep; 614 size_t ace_size; 615 uint16_t entry_type; 616 617 while (zacep = zfs_acl_next_ace(aclp, zacep, 618 &who, &access_mask, &iflags, &type)) { 619 620 switch (type) { 621 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 622 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 623 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 624 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 625 if (filter) { 626 continue; 627 } 628 zobjacep = (zfs_object_ace_t *)zacep; 629 objacep = (ace_object_t *)acep; 630 bcopy(zobjacep->z_object_type, 631 objacep->a_obj_type, 632 sizeof (zobjacep->z_object_type)); 633 bcopy(zobjacep->z_inherit_type, 634 objacep->a_inherit_obj_type, 635 sizeof (zobjacep->z_inherit_type)); 636 ace_size = sizeof (ace_object_t); 637 break; 638 default: 639 ace_size = sizeof (ace_t); 640 break; 641 } 642 643 entry_type = (iflags & ACE_TYPE_FLAGS); 644 if ((entry_type != ACE_OWNER && 645 entry_type != OWNING_GROUP && 646 entry_type != ACE_EVERYONE)) { 647 acep->a_who = zfs_fuid_map_id(zfsvfs, who, 648 cr, (entry_type & ACE_IDENTIFIER_GROUP) ? 649 ZFS_ACE_GROUP : ZFS_ACE_USER); 650 } else { 651 acep->a_who = (uid_t)(int64_t)who; 652 } 653 acep->a_access_mask = access_mask; 654 acep->a_flags = iflags; 655 acep->a_type = type; 656 acep = (ace_t *)((caddr_t)acep + ace_size); 657 } 658 } 659 660 static int 661 zfs_copy_ace_2_oldace(vtype_t obj_type, zfs_acl_t *aclp, ace_t *acep, 662 zfs_oldace_t *z_acl, int aclcnt, size_t *size) 663 { 664 int i; 665 zfs_oldace_t *aceptr = z_acl; 666 667 for (i = 0; i != aclcnt; i++, aceptr++) { 668 aceptr->z_access_mask = acep[i].a_access_mask; 669 aceptr->z_type = acep[i].a_type; 670 aceptr->z_flags = acep[i].a_flags; 671 aceptr->z_fuid = acep[i].a_who; 672 /* 673 * Make sure ACE is valid 674 */ 675 if (zfs_ace_valid(obj_type, aclp, aceptr->z_type, 676 aceptr->z_flags) != B_TRUE) 677 return (EINVAL); 678 } 679 *size = (caddr_t)aceptr - (caddr_t)z_acl; 680 return (0); 681 } 682 683 /* 684 * convert old ACL format to new 685 */ 686 void 687 zfs_acl_xform(znode_t *zp, zfs_acl_t *aclp, cred_t *cr) 688 { 689 zfs_oldace_t *oldaclp; 690 int i; 691 uint16_t type, iflags; 692 uint32_t access_mask; 693 uint64_t who; 694 void *cookie = NULL; 695 zfs_acl_node_t *newaclnode; 696 697 ASSERT(aclp->z_version == ZFS_ACL_VERSION_INITIAL); 698 /* 699 * First create the ACE in a contiguous piece of memory 700 * for zfs_copy_ace_2_fuid(). 701 * 702 * We only convert an ACL once, so this won't happen 703 * everytime. 704 */ 705 oldaclp = kmem_alloc(sizeof (zfs_oldace_t) * aclp->z_acl_count, 706 KM_SLEEP); 707 i = 0; 708 while (cookie = zfs_acl_next_ace(aclp, cookie, &who, 709 &access_mask, &iflags, &type)) { 710 oldaclp[i].z_flags = iflags; 711 oldaclp[i].z_type = type; 712 oldaclp[i].z_fuid = who; 713 oldaclp[i++].z_access_mask = access_mask; 714 } 715 716 newaclnode = zfs_acl_node_alloc(aclp->z_acl_count * 717 sizeof (zfs_object_ace_t)); 718 aclp->z_ops = zfs_acl_fuid_ops; 719 VERIFY(zfs_copy_ace_2_fuid(zp->z_zfsvfs, ZTOV(zp)->v_type, aclp, 720 oldaclp, newaclnode->z_acldata, aclp->z_acl_count, 721 &newaclnode->z_size, NULL, cr) == 0); 722 newaclnode->z_ace_count = aclp->z_acl_count; 723 aclp->z_version = ZFS_ACL_VERSION; 724 kmem_free(oldaclp, aclp->z_acl_count * sizeof (zfs_oldace_t)); 725 726 /* 727 * Release all previous ACL nodes 728 */ 729 730 zfs_acl_release_nodes(aclp); 731 732 list_insert_head(&aclp->z_acl, newaclnode); 733 734 aclp->z_acl_bytes = newaclnode->z_size; 735 aclp->z_acl_count = newaclnode->z_ace_count; 736 737 } 738 739 /* 740 * Convert unix access mask to v4 access mask 741 */ 742 static uint32_t 743 zfs_unix_to_v4(uint32_t access_mask) 744 { 745 uint32_t new_mask = 0; 746 747 if (access_mask & S_IXOTH) 748 new_mask |= ACE_EXECUTE; 749 if (access_mask & S_IWOTH) 750 new_mask |= ACE_WRITE_DATA; 751 if (access_mask & S_IROTH) 752 new_mask |= ACE_READ_DATA; 753 return (new_mask); 754 } 755 756 static void 757 zfs_set_ace(zfs_acl_t *aclp, void *acep, uint32_t access_mask, 758 uint16_t access_type, uint64_t fuid, uint16_t entry_type) 759 { 760 uint16_t type = entry_type & ACE_TYPE_FLAGS; 761 762 aclp->z_ops.ace_mask_set(acep, access_mask); 763 aclp->z_ops.ace_type_set(acep, access_type); 764 aclp->z_ops.ace_flags_set(acep, entry_type); 765 if ((type != ACE_OWNER && type != OWNING_GROUP && 766 type != ACE_EVERYONE)) 767 aclp->z_ops.ace_who_set(acep, fuid); 768 } 769 770 /* 771 * Determine mode of file based on ACL. 772 * Also, create FUIDs for any User/Group ACEs 773 */ 774 static uint64_t 775 zfs_mode_compute(znode_t *zp, zfs_acl_t *aclp) 776 { 777 int entry_type; 778 mode_t mode; 779 mode_t seen = 0; 780 zfs_ace_hdr_t *acep = NULL; 781 uint64_t who; 782 uint16_t iflags, type; 783 uint32_t access_mask; 784 785 mode = (zp->z_phys->zp_mode & (S_IFMT | S_ISUID | S_ISGID | S_ISVTX)); 786 787 while (acep = zfs_acl_next_ace(aclp, acep, &who, 788 &access_mask, &iflags, &type)) { 789 790 if (!zfs_acl_valid_ace_type(type, iflags)) 791 continue; 792 793 entry_type = (iflags & ACE_TYPE_FLAGS); 794 795 /* 796 * Skip over owner@, group@ or everyone@ inherit only ACEs 797 */ 798 if ((iflags & ACE_INHERIT_ONLY_ACE) && 799 (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE || 800 entry_type == OWNING_GROUP)) 801 continue; 802 803 if (entry_type == ACE_OWNER) { 804 if ((access_mask & ACE_READ_DATA) && 805 (!(seen & S_IRUSR))) { 806 seen |= S_IRUSR; 807 if (type == ALLOW) { 808 mode |= S_IRUSR; 809 } 810 } 811 if ((access_mask & ACE_WRITE_DATA) && 812 (!(seen & S_IWUSR))) { 813 seen |= S_IWUSR; 814 if (type == ALLOW) { 815 mode |= S_IWUSR; 816 } 817 } 818 if ((access_mask & ACE_EXECUTE) && 819 (!(seen & S_IXUSR))) { 820 seen |= S_IXUSR; 821 if (type == ALLOW) { 822 mode |= S_IXUSR; 823 } 824 } 825 } else if (entry_type == OWNING_GROUP) { 826 if ((access_mask & ACE_READ_DATA) && 827 (!(seen & S_IRGRP))) { 828 seen |= S_IRGRP; 829 if (type == ALLOW) { 830 mode |= S_IRGRP; 831 } 832 } 833 if ((access_mask & ACE_WRITE_DATA) && 834 (!(seen & S_IWGRP))) { 835 seen |= S_IWGRP; 836 if (type == ALLOW) { 837 mode |= S_IWGRP; 838 } 839 } 840 if ((access_mask & ACE_EXECUTE) && 841 (!(seen & S_IXGRP))) { 842 seen |= S_IXGRP; 843 if (type == ALLOW) { 844 mode |= S_IXGRP; 845 } 846 } 847 } else if (entry_type == ACE_EVERYONE) { 848 if ((access_mask & ACE_READ_DATA)) { 849 if (!(seen & S_IRUSR)) { 850 seen |= S_IRUSR; 851 if (type == ALLOW) { 852 mode |= S_IRUSR; 853 } 854 } 855 if (!(seen & S_IRGRP)) { 856 seen |= S_IRGRP; 857 if (type == ALLOW) { 858 mode |= S_IRGRP; 859 } 860 } 861 if (!(seen & S_IROTH)) { 862 seen |= S_IROTH; 863 if (type == ALLOW) { 864 mode |= S_IROTH; 865 } 866 } 867 } 868 if ((access_mask & ACE_WRITE_DATA)) { 869 if (!(seen & S_IWUSR)) { 870 seen |= S_IWUSR; 871 if (type == ALLOW) { 872 mode |= S_IWUSR; 873 } 874 } 875 if (!(seen & S_IWGRP)) { 876 seen |= S_IWGRP; 877 if (type == ALLOW) { 878 mode |= S_IWGRP; 879 } 880 } 881 if (!(seen & S_IWOTH)) { 882 seen |= S_IWOTH; 883 if (type == ALLOW) { 884 mode |= S_IWOTH; 885 } 886 } 887 } 888 if ((access_mask & ACE_EXECUTE)) { 889 if (!(seen & S_IXUSR)) { 890 seen |= S_IXUSR; 891 if (type == ALLOW) { 892 mode |= S_IXUSR; 893 } 894 } 895 if (!(seen & S_IXGRP)) { 896 seen |= S_IXGRP; 897 if (type == ALLOW) { 898 mode |= S_IXGRP; 899 } 900 } 901 if (!(seen & S_IXOTH)) { 902 seen |= S_IXOTH; 903 if (type == ALLOW) { 904 mode |= S_IXOTH; 905 } 906 } 907 } 908 } 909 } 910 return (mode); 911 } 912 913 static zfs_acl_t * 914 zfs_acl_node_read_internal(znode_t *zp, boolean_t will_modify) 915 { 916 zfs_acl_t *aclp; 917 zfs_acl_node_t *aclnode; 918 919 aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_version); 920 921 /* 922 * Version 0 to 1 znode_acl_phys has the size/count fields swapped. 923 * Version 0 didn't have a size field, only a count. 924 */ 925 if (zp->z_phys->zp_acl.z_acl_version == ZFS_ACL_VERSION_INITIAL) { 926 aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_size; 927 aclp->z_acl_bytes = ZFS_ACL_SIZE(aclp->z_acl_count); 928 } else { 929 aclp->z_acl_count = zp->z_phys->zp_acl.z_acl_count; 930 aclp->z_acl_bytes = zp->z_phys->zp_acl.z_acl_size; 931 } 932 933 aclnode = zfs_acl_node_alloc(will_modify ? aclp->z_acl_bytes : 0); 934 aclnode->z_ace_count = aclp->z_acl_count; 935 if (will_modify) { 936 bcopy(zp->z_phys->zp_acl.z_ace_data, aclnode->z_acldata, 937 aclp->z_acl_bytes); 938 } else { 939 aclnode->z_size = aclp->z_acl_bytes; 940 aclnode->z_acldata = &zp->z_phys->zp_acl.z_ace_data[0]; 941 } 942 943 list_insert_head(&aclp->z_acl, aclnode); 944 945 return (aclp); 946 } 947 948 /* 949 * Read an external acl object. 950 */ 951 static int 952 zfs_acl_node_read(znode_t *zp, zfs_acl_t **aclpp, boolean_t will_modify) 953 { 954 uint64_t extacl = zp->z_phys->zp_acl.z_acl_extern_obj; 955 zfs_acl_t *aclp; 956 size_t aclsize; 957 size_t acl_count; 958 zfs_acl_node_t *aclnode; 959 int error; 960 961 ASSERT(MUTEX_HELD(&zp->z_acl_lock)); 962 963 if (zp->z_phys->zp_acl.z_acl_extern_obj == 0) { 964 *aclpp = zfs_acl_node_read_internal(zp, will_modify); 965 return (0); 966 } 967 968 aclp = zfs_acl_alloc(zp->z_phys->zp_acl.z_acl_version); 969 if (zp->z_phys->zp_acl.z_acl_version == ZFS_ACL_VERSION_INITIAL) { 970 zfs_acl_phys_v0_t *zacl0 = 971 (zfs_acl_phys_v0_t *)&zp->z_phys->zp_acl; 972 973 aclsize = ZFS_ACL_SIZE(zacl0->z_acl_count); 974 acl_count = zacl0->z_acl_count; 975 } else { 976 aclsize = zp->z_phys->zp_acl.z_acl_size; 977 acl_count = zp->z_phys->zp_acl.z_acl_count; 978 if (aclsize == 0) 979 aclsize = acl_count * sizeof (zfs_ace_t); 980 } 981 aclnode = zfs_acl_node_alloc(aclsize); 982 list_insert_head(&aclp->z_acl, aclnode); 983 error = dmu_read(zp->z_zfsvfs->z_os, extacl, 0, 984 aclsize, aclnode->z_acldata); 985 aclnode->z_ace_count = acl_count; 986 aclp->z_acl_count = acl_count; 987 aclp->z_acl_bytes = aclsize; 988 989 if (error != 0) { 990 zfs_acl_free(aclp); 991 /* convert checksum errors into IO errors */ 992 if (error == ECKSUM) 993 error = EIO; 994 return (error); 995 } 996 997 *aclpp = aclp; 998 return (0); 999 } 1000 1001 /* 1002 * common code for setting ACLs. 1003 * 1004 * This function is called from zfs_mode_update, zfs_perm_init, and zfs_setacl. 1005 * zfs_setacl passes a non-NULL inherit pointer (ihp) to indicate that it's 1006 * already checked the acl and knows whether to inherit. 1007 */ 1008 int 1009 zfs_aclset_common(znode_t *zp, zfs_acl_t *aclp, cred_t *cr, dmu_tx_t *tx) 1010 { 1011 int error; 1012 znode_phys_t *zphys = zp->z_phys; 1013 zfs_acl_phys_t *zacl = &zphys->zp_acl; 1014 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1015 uint64_t aoid = zphys->zp_acl.z_acl_extern_obj; 1016 uint64_t off = 0; 1017 dmu_object_type_t otype; 1018 zfs_acl_node_t *aclnode; 1019 1020 dmu_buf_will_dirty(zp->z_dbuf, tx); 1021 1022 zphys->zp_mode = zfs_mode_compute(zp, aclp); 1023 1024 /* 1025 * Decide which opbject type to use. If we are forced to 1026 * use old ACL format than transform ACL into zfs_oldace_t 1027 * layout. 1028 */ 1029 if (!zfsvfs->z_use_fuids) { 1030 otype = DMU_OT_OLDACL; 1031 } else { 1032 if ((aclp->z_version == ZFS_ACL_VERSION_INITIAL) && 1033 (zfsvfs->z_version >= ZPL_VERSION_FUID)) 1034 zfs_acl_xform(zp, aclp, cr); 1035 ASSERT(aclp->z_version >= ZFS_ACL_VERSION_FUID); 1036 otype = DMU_OT_ACL; 1037 } 1038 1039 if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1040 /* 1041 * If ACL was previously external and we are now 1042 * converting to new ACL format then release old 1043 * ACL object and create a new one. 1044 */ 1045 if (aoid && aclp->z_version != zacl->z_acl_version) { 1046 error = dmu_object_free(zfsvfs->z_os, 1047 zp->z_phys->zp_acl.z_acl_extern_obj, tx); 1048 if (error) 1049 return (error); 1050 aoid = 0; 1051 } 1052 if (aoid == 0) { 1053 aoid = dmu_object_alloc(zfsvfs->z_os, 1054 otype, aclp->z_acl_bytes, 1055 otype == DMU_OT_ACL ? DMU_OT_SYSACL : DMU_OT_NONE, 1056 otype == DMU_OT_ACL ? DN_MAX_BONUSLEN : 0, tx); 1057 } else { 1058 (void) dmu_object_set_blocksize(zfsvfs->z_os, aoid, 1059 aclp->z_acl_bytes, 0, tx); 1060 } 1061 zphys->zp_acl.z_acl_extern_obj = aoid; 1062 for (aclnode = list_head(&aclp->z_acl); aclnode; 1063 aclnode = list_next(&aclp->z_acl, aclnode)) { 1064 if (aclnode->z_ace_count == 0) 1065 continue; 1066 dmu_write(zfsvfs->z_os, aoid, off, 1067 aclnode->z_size, aclnode->z_acldata, tx); 1068 off += aclnode->z_size; 1069 } 1070 } else { 1071 void *start = zacl->z_ace_data; 1072 /* 1073 * Migrating back embedded? 1074 */ 1075 if (zphys->zp_acl.z_acl_extern_obj) { 1076 error = dmu_object_free(zfsvfs->z_os, 1077 zp->z_phys->zp_acl.z_acl_extern_obj, tx); 1078 if (error) 1079 return (error); 1080 zphys->zp_acl.z_acl_extern_obj = 0; 1081 } 1082 1083 for (aclnode = list_head(&aclp->z_acl); aclnode; 1084 aclnode = list_next(&aclp->z_acl, aclnode)) { 1085 if (aclnode->z_ace_count == 0) 1086 continue; 1087 bcopy(aclnode->z_acldata, start, aclnode->z_size); 1088 start = (caddr_t)start + aclnode->z_size; 1089 } 1090 } 1091 1092 /* 1093 * If Old version then swap count/bytes to match old 1094 * layout of znode_acl_phys_t. 1095 */ 1096 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) { 1097 zphys->zp_acl.z_acl_size = aclp->z_acl_count; 1098 zphys->zp_acl.z_acl_count = aclp->z_acl_bytes; 1099 } else { 1100 zphys->zp_acl.z_acl_size = aclp->z_acl_bytes; 1101 zphys->zp_acl.z_acl_count = aclp->z_acl_count; 1102 } 1103 1104 zphys->zp_acl.z_acl_version = aclp->z_version; 1105 1106 /* 1107 * Replace ACL wide bits, but first clear them. 1108 */ 1109 zp->z_phys->zp_flags &= ~ZFS_ACL_WIDE_FLAGS; 1110 1111 zp->z_phys->zp_flags |= aclp->z_hints; 1112 1113 if (ace_trivial_common(aclp, 0, zfs_ace_walk) == 0) 1114 zp->z_phys->zp_flags |= ZFS_ACL_TRIVIAL; 1115 1116 return (0); 1117 } 1118 1119 /* 1120 * Update access mask for prepended ACE 1121 * 1122 * This applies the "groupmask" value for aclmode property. 1123 */ 1124 static void 1125 zfs_acl_prepend_fixup(zfs_acl_t *aclp, void *acep, void *origacep, 1126 mode_t mode, uint64_t owner) 1127 { 1128 int rmask, wmask, xmask; 1129 int user_ace; 1130 uint16_t aceflags; 1131 uint32_t origmask, acepmask; 1132 uint64_t fuid; 1133 1134 aceflags = aclp->z_ops.ace_flags_get(acep); 1135 fuid = aclp->z_ops.ace_who_get(acep); 1136 origmask = aclp->z_ops.ace_mask_get(origacep); 1137 acepmask = aclp->z_ops.ace_mask_get(acep); 1138 1139 user_ace = (!(aceflags & 1140 (ACE_OWNER|ACE_GROUP|ACE_IDENTIFIER_GROUP))); 1141 1142 if (user_ace && (fuid == owner)) { 1143 rmask = S_IRUSR; 1144 wmask = S_IWUSR; 1145 xmask = S_IXUSR; 1146 } else { 1147 rmask = S_IRGRP; 1148 wmask = S_IWGRP; 1149 xmask = S_IXGRP; 1150 } 1151 1152 if (origmask & ACE_READ_DATA) { 1153 if (mode & rmask) { 1154 acepmask &= ~ACE_READ_DATA; 1155 } else { 1156 acepmask |= ACE_READ_DATA; 1157 } 1158 } 1159 1160 if (origmask & ACE_WRITE_DATA) { 1161 if (mode & wmask) { 1162 acepmask &= ~ACE_WRITE_DATA; 1163 } else { 1164 acepmask |= ACE_WRITE_DATA; 1165 } 1166 } 1167 1168 if (origmask & ACE_APPEND_DATA) { 1169 if (mode & wmask) { 1170 acepmask &= ~ACE_APPEND_DATA; 1171 } else { 1172 acepmask |= ACE_APPEND_DATA; 1173 } 1174 } 1175 1176 if (origmask & ACE_EXECUTE) { 1177 if (mode & xmask) { 1178 acepmask &= ~ACE_EXECUTE; 1179 } else { 1180 acepmask |= ACE_EXECUTE; 1181 } 1182 } 1183 aclp->z_ops.ace_mask_set(acep, acepmask); 1184 } 1185 1186 /* 1187 * Apply mode to canonical six ACEs. 1188 */ 1189 static void 1190 zfs_acl_fixup_canonical_six(zfs_acl_t *aclp, mode_t mode) 1191 { 1192 zfs_acl_node_t *aclnode = list_tail(&aclp->z_acl); 1193 void *acep; 1194 int maskoff = aclp->z_ops.ace_mask_off(); 1195 size_t abstract_size = aclp->z_ops.ace_abstract_size(); 1196 1197 ASSERT(aclnode != NULL); 1198 1199 acep = (void *)((caddr_t)aclnode->z_acldata + 1200 aclnode->z_size - (abstract_size * 6)); 1201 1202 /* 1203 * Fixup final ACEs to match the mode 1204 */ 1205 1206 adjust_ace_pair_common(acep, maskoff, abstract_size, 1207 (mode & 0700) >> 6); /* owner@ */ 1208 1209 acep = (caddr_t)acep + (abstract_size * 2); 1210 1211 adjust_ace_pair_common(acep, maskoff, abstract_size, 1212 (mode & 0070) >> 3); /* group@ */ 1213 1214 acep = (caddr_t)acep + (abstract_size * 2); 1215 adjust_ace_pair_common(acep, maskoff, 1216 abstract_size, mode); /* everyone@ */ 1217 } 1218 1219 1220 static int 1221 zfs_acl_ace_match(zfs_acl_t *aclp, void *acep, int allow_deny, 1222 int entry_type, int accessmask) 1223 { 1224 uint32_t mask = aclp->z_ops.ace_mask_get(acep); 1225 uint16_t type = aclp->z_ops.ace_type_get(acep); 1226 uint16_t flags = aclp->z_ops.ace_flags_get(acep); 1227 1228 return (mask == accessmask && type == allow_deny && 1229 ((flags & ACE_TYPE_FLAGS) == entry_type)); 1230 } 1231 1232 /* 1233 * Can prepended ACE be reused? 1234 */ 1235 static int 1236 zfs_reuse_deny(zfs_acl_t *aclp, void *acep, void *prevacep) 1237 { 1238 int okay_masks; 1239 uint16_t prevtype; 1240 uint16_t prevflags; 1241 uint16_t flags; 1242 uint32_t mask, prevmask; 1243 1244 if (prevacep == NULL) 1245 return (B_FALSE); 1246 1247 prevtype = aclp->z_ops.ace_type_get(prevacep); 1248 prevflags = aclp->z_ops.ace_flags_get(prevacep); 1249 flags = aclp->z_ops.ace_flags_get(acep); 1250 mask = aclp->z_ops.ace_mask_get(acep); 1251 prevmask = aclp->z_ops.ace_mask_get(prevacep); 1252 1253 if (prevtype != DENY) 1254 return (B_FALSE); 1255 1256 if (prevflags != (flags & ACE_IDENTIFIER_GROUP)) 1257 return (B_FALSE); 1258 1259 okay_masks = (mask & OKAY_MASK_BITS); 1260 1261 if (prevmask & ~okay_masks) 1262 return (B_FALSE); 1263 1264 return (B_TRUE); 1265 } 1266 1267 1268 /* 1269 * Insert new ACL node into chain of zfs_acl_node_t's 1270 * 1271 * This will result in two possible results. 1272 * 1. If the ACL is currently just a single zfs_acl_node and 1273 * we are prepending the entry then current acl node will have 1274 * a new node inserted above it. 1275 * 1276 * 2. If we are inserting in the middle of current acl node then 1277 * the current node will be split in two and new node will be inserted 1278 * in between the two split nodes. 1279 */ 1280 static zfs_acl_node_t * 1281 zfs_acl_ace_insert(zfs_acl_t *aclp, void *acep) 1282 { 1283 zfs_acl_node_t *newnode; 1284 zfs_acl_node_t *trailernode = NULL; 1285 zfs_acl_node_t *currnode = zfs_acl_curr_node(aclp); 1286 int curr_idx = aclp->z_curr_node->z_ace_idx; 1287 int trailer_count; 1288 size_t oldsize; 1289 1290 newnode = zfs_acl_node_alloc(aclp->z_ops.ace_size(acep)); 1291 newnode->z_ace_count = 1; 1292 1293 oldsize = currnode->z_size; 1294 1295 if (curr_idx != 1) { 1296 trailernode = zfs_acl_node_alloc(0); 1297 trailernode->z_acldata = acep; 1298 1299 trailer_count = currnode->z_ace_count - curr_idx + 1; 1300 currnode->z_ace_count = curr_idx - 1; 1301 currnode->z_size = (caddr_t)acep - (caddr_t)currnode->z_acldata; 1302 trailernode->z_size = oldsize - currnode->z_size; 1303 trailernode->z_ace_count = trailer_count; 1304 } 1305 1306 aclp->z_acl_count += 1; 1307 aclp->z_acl_bytes += aclp->z_ops.ace_size(acep); 1308 1309 if (curr_idx == 1) 1310 list_insert_before(&aclp->z_acl, currnode, newnode); 1311 else 1312 list_insert_after(&aclp->z_acl, currnode, newnode); 1313 if (trailernode) { 1314 list_insert_after(&aclp->z_acl, newnode, trailernode); 1315 aclp->z_curr_node = trailernode; 1316 trailernode->z_ace_idx = 1; 1317 } 1318 1319 return (newnode); 1320 } 1321 1322 /* 1323 * Prepend deny ACE 1324 */ 1325 static void * 1326 zfs_acl_prepend_deny(uint64_t uid, zfs_acl_t *aclp, void *acep, 1327 mode_t mode) 1328 { 1329 zfs_acl_node_t *aclnode; 1330 void *newacep; 1331 uint64_t fuid; 1332 uint16_t flags; 1333 1334 aclnode = zfs_acl_ace_insert(aclp, acep); 1335 newacep = aclnode->z_acldata; 1336 fuid = aclp->z_ops.ace_who_get(acep); 1337 flags = aclp->z_ops.ace_flags_get(acep); 1338 zfs_set_ace(aclp, newacep, 0, DENY, fuid, (flags & ACE_TYPE_FLAGS)); 1339 zfs_acl_prepend_fixup(aclp, newacep, acep, mode, uid); 1340 1341 return (newacep); 1342 } 1343 1344 /* 1345 * Split an inherited ACE into inherit_only ACE 1346 * and original ACE with inheritance flags stripped off. 1347 */ 1348 static void 1349 zfs_acl_split_ace(zfs_acl_t *aclp, zfs_ace_hdr_t *acep) 1350 { 1351 zfs_acl_node_t *aclnode; 1352 zfs_acl_node_t *currnode; 1353 void *newacep; 1354 uint16_t type, flags; 1355 uint32_t mask; 1356 uint64_t fuid; 1357 1358 type = aclp->z_ops.ace_type_get(acep); 1359 flags = aclp->z_ops.ace_flags_get(acep); 1360 mask = aclp->z_ops.ace_mask_get(acep); 1361 fuid = aclp->z_ops.ace_who_get(acep); 1362 1363 aclnode = zfs_acl_ace_insert(aclp, acep); 1364 newacep = aclnode->z_acldata; 1365 1366 aclp->z_ops.ace_type_set(newacep, type); 1367 aclp->z_ops.ace_flags_set(newacep, flags | ACE_INHERIT_ONLY_ACE); 1368 aclp->z_ops.ace_mask_set(newacep, mask); 1369 aclp->z_ops.ace_type_set(newacep, type); 1370 aclp->z_ops.ace_who_set(newacep, fuid); 1371 aclp->z_next_ace = acep; 1372 flags &= ~ALL_INHERIT; 1373 aclp->z_ops.ace_flags_set(acep, flags); 1374 currnode = zfs_acl_curr_node(aclp); 1375 ASSERT(currnode->z_ace_idx >= 1); 1376 currnode->z_ace_idx -= 1; 1377 } 1378 1379 /* 1380 * Are ACES started at index i, the canonical six ACES? 1381 */ 1382 static int 1383 zfs_have_canonical_six(zfs_acl_t *aclp) 1384 { 1385 void *acep; 1386 zfs_acl_node_t *aclnode = list_tail(&aclp->z_acl); 1387 int i = 0; 1388 size_t abstract_size = aclp->z_ops.ace_abstract_size(); 1389 1390 ASSERT(aclnode != NULL); 1391 1392 if (aclnode->z_ace_count < 6) 1393 return (0); 1394 1395 acep = (void *)((caddr_t)aclnode->z_acldata + 1396 aclnode->z_size - (aclp->z_ops.ace_abstract_size() * 6)); 1397 1398 if ((zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), 1399 DENY, ACE_OWNER, 0) && 1400 zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), 1401 ALLOW, ACE_OWNER, OWNER_ALLOW_MASK) && 1402 zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), DENY, 1403 OWNING_GROUP, 0) && zfs_acl_ace_match(aclp, (caddr_t)acep + 1404 (abstract_size * i++), 1405 ALLOW, OWNING_GROUP, 0) && 1406 zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), 1407 DENY, ACE_EVERYONE, EVERYONE_DENY_MASK) && 1408 zfs_acl_ace_match(aclp, (caddr_t)acep + (abstract_size * i++), 1409 ALLOW, ACE_EVERYONE, EVERYONE_ALLOW_MASK))) { 1410 return (1); 1411 } else { 1412 return (0); 1413 } 1414 } 1415 1416 1417 /* 1418 * Apply step 1g, to group entries 1419 * 1420 * Need to deal with corner case where group may have 1421 * greater permissions than owner. If so then limit 1422 * group permissions, based on what extra permissions 1423 * group has. 1424 */ 1425 static void 1426 zfs_fixup_group_entries(zfs_acl_t *aclp, void *acep, void *prevacep, 1427 mode_t mode) 1428 { 1429 uint32_t prevmask = aclp->z_ops.ace_mask_get(prevacep); 1430 uint32_t mask = aclp->z_ops.ace_mask_get(acep); 1431 uint16_t prevflags = aclp->z_ops.ace_flags_get(prevacep); 1432 mode_t extramode = (mode >> 3) & 07; 1433 mode_t ownermode = (mode >> 6); 1434 1435 if (prevflags & ACE_IDENTIFIER_GROUP) { 1436 1437 extramode &= ~ownermode; 1438 1439 if (extramode) { 1440 if (extramode & S_IROTH) { 1441 prevmask &= ~ACE_READ_DATA; 1442 mask &= ~ACE_READ_DATA; 1443 } 1444 if (extramode & S_IWOTH) { 1445 prevmask &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA); 1446 mask &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA); 1447 } 1448 if (extramode & S_IXOTH) { 1449 prevmask &= ~ACE_EXECUTE; 1450 mask &= ~ACE_EXECUTE; 1451 } 1452 } 1453 } 1454 aclp->z_ops.ace_mask_set(acep, mask); 1455 aclp->z_ops.ace_mask_set(prevacep, prevmask); 1456 } 1457 1458 /* 1459 * Apply the chmod algorithm as described 1460 * in PSARC/2002/240 1461 */ 1462 static void 1463 zfs_acl_chmod(zfsvfs_t *zfsvfs, uint64_t uid, 1464 uint64_t mode, zfs_acl_t *aclp) 1465 { 1466 void *acep = NULL, *prevacep = NULL; 1467 uint64_t who; 1468 int i; 1469 int entry_type; 1470 int reuse_deny; 1471 int need_canonical_six = 1; 1472 uint16_t iflags, type; 1473 uint32_t access_mask; 1474 1475 /* 1476 * If discard then just discard all ACL nodes which 1477 * represent the ACEs. 1478 * 1479 * New owner@/group@/everone@ ACEs will be added 1480 * later. 1481 */ 1482 if (zfsvfs->z_acl_mode == ZFS_ACL_DISCARD) 1483 zfs_acl_release_nodes(aclp); 1484 1485 while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask, 1486 &iflags, &type)) { 1487 1488 entry_type = (iflags & ACE_TYPE_FLAGS); 1489 iflags = (iflags & ALL_INHERIT); 1490 1491 if ((type != ALLOW && type != DENY) || 1492 (iflags & ACE_INHERIT_ONLY_ACE)) { 1493 if (iflags) 1494 aclp->z_hints |= ZFS_INHERIT_ACE; 1495 switch (type) { 1496 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 1497 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 1498 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 1499 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 1500 aclp->z_hints |= ZFS_ACL_OBJ_ACE; 1501 break; 1502 } 1503 goto nextace; 1504 } 1505 1506 /* 1507 * Need to split ace into two? 1508 */ 1509 if ((iflags & (ACE_FILE_INHERIT_ACE| 1510 ACE_DIRECTORY_INHERIT_ACE)) && 1511 (!(iflags & ACE_INHERIT_ONLY_ACE))) { 1512 zfs_acl_split_ace(aclp, acep); 1513 aclp->z_hints |= ZFS_INHERIT_ACE; 1514 goto nextace; 1515 } 1516 1517 if (entry_type == ACE_OWNER || entry_type == ACE_EVERYONE || 1518 (entry_type == OWNING_GROUP)) { 1519 access_mask &= ~OGE_CLEAR; 1520 aclp->z_ops.ace_mask_set(acep, access_mask); 1521 goto nextace; 1522 } else { 1523 reuse_deny = B_TRUE; 1524 if (type == ALLOW) { 1525 1526 /* 1527 * Check preceding ACE if any, to see 1528 * if we need to prepend a DENY ACE. 1529 * This is only applicable when the acl_mode 1530 * property == groupmask. 1531 */ 1532 if (zfsvfs->z_acl_mode == ZFS_ACL_GROUPMASK) { 1533 1534 reuse_deny = zfs_reuse_deny(aclp, acep, 1535 prevacep); 1536 1537 if (!reuse_deny) { 1538 prevacep = 1539 zfs_acl_prepend_deny(uid, 1540 aclp, acep, mode); 1541 } else { 1542 zfs_acl_prepend_fixup( 1543 aclp, prevacep, 1544 acep, mode, uid); 1545 } 1546 zfs_fixup_group_entries(aclp, acep, 1547 prevacep, mode); 1548 } 1549 } 1550 } 1551 nextace: 1552 prevacep = acep; 1553 } 1554 1555 /* 1556 * Check out last six aces, if we have six. 1557 */ 1558 1559 if (aclp->z_acl_count >= 6) { 1560 if (zfs_have_canonical_six(aclp)) { 1561 need_canonical_six = 0; 1562 } 1563 } 1564 1565 if (need_canonical_six) { 1566 size_t abstract_size = aclp->z_ops.ace_abstract_size(); 1567 void *zacep; 1568 zfs_acl_node_t *aclnode = 1569 zfs_acl_node_alloc(abstract_size * 6); 1570 1571 aclnode->z_size = abstract_size * 6; 1572 aclnode->z_ace_count = 6; 1573 aclp->z_acl_bytes += aclnode->z_size; 1574 list_insert_tail(&aclp->z_acl, aclnode); 1575 1576 zacep = aclnode->z_acldata; 1577 1578 i = 0; 1579 zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 1580 0, DENY, -1, ACE_OWNER); 1581 zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 1582 OWNER_ALLOW_MASK, ALLOW, -1, ACE_OWNER); 1583 zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 0, 1584 DENY, -1, OWNING_GROUP); 1585 zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 0, 1586 ALLOW, -1, OWNING_GROUP); 1587 zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 1588 EVERYONE_DENY_MASK, DENY, -1, ACE_EVERYONE); 1589 zfs_set_ace(aclp, (caddr_t)zacep + (abstract_size * i++), 1590 EVERYONE_ALLOW_MASK, ALLOW, -1, ACE_EVERYONE); 1591 aclp->z_acl_count += 6; 1592 } 1593 1594 zfs_acl_fixup_canonical_six(aclp, mode); 1595 } 1596 1597 int 1598 zfs_acl_chmod_setattr(znode_t *zp, zfs_acl_t **aclp, uint64_t mode) 1599 { 1600 int error; 1601 1602 mutex_enter(&zp->z_lock); 1603 mutex_enter(&zp->z_acl_lock); 1604 *aclp = NULL; 1605 error = zfs_acl_node_read(zp, aclp, B_TRUE); 1606 if (error == 0) { 1607 (*aclp)->z_hints = zp->z_phys->zp_flags & V4_ACL_WIDE_FLAGS; 1608 zfs_acl_chmod(zp->z_zfsvfs, zp->z_phys->zp_uid, mode, *aclp); 1609 } 1610 mutex_exit(&zp->z_acl_lock); 1611 mutex_exit(&zp->z_lock); 1612 return (error); 1613 } 1614 1615 /* 1616 * strip off write_owner and write_acl 1617 */ 1618 static void 1619 zfs_restricted_update(zfsvfs_t *zfsvfs, zfs_acl_t *aclp, void *acep) 1620 { 1621 uint32_t mask = aclp->z_ops.ace_mask_get(acep); 1622 1623 if ((zfsvfs->z_acl_inherit == ZFS_ACL_RESTRICTED) && 1624 (aclp->z_ops.ace_type_get(acep) == ALLOW)) { 1625 mask &= ~RESTRICTED_CLEAR; 1626 aclp->z_ops.ace_mask_set(acep, mask); 1627 } 1628 } 1629 1630 /* 1631 * Should ACE be inherited? 1632 */ 1633 static int 1634 zfs_ace_can_use(vtype_t vtype, uint16_t acep_flags) 1635 { 1636 int iflags = (acep_flags & 0xf); 1637 1638 if ((vtype == VDIR) && (iflags & ACE_DIRECTORY_INHERIT_ACE)) 1639 return (1); 1640 else if (iflags & ACE_FILE_INHERIT_ACE) 1641 return (!((vtype == VDIR) && 1642 (iflags & ACE_NO_PROPAGATE_INHERIT_ACE))); 1643 return (0); 1644 } 1645 1646 /* 1647 * inherit inheritable ACEs from parent 1648 */ 1649 static zfs_acl_t * 1650 zfs_acl_inherit(zfsvfs_t *zfsvfs, vtype_t vtype, zfs_acl_t *paclp, 1651 uint64_t mode, boolean_t *need_chmod) 1652 { 1653 void *pacep; 1654 void *acep, *acep2; 1655 zfs_acl_node_t *aclnode, *aclnode2; 1656 zfs_acl_t *aclp = NULL; 1657 uint64_t who; 1658 uint32_t access_mask; 1659 uint16_t iflags, newflags, type; 1660 size_t ace_size; 1661 void *data1, *data2; 1662 size_t data1sz, data2sz; 1663 boolean_t vdir = vtype == VDIR; 1664 boolean_t vreg = vtype == VREG; 1665 boolean_t passthrough, passthrough_x, noallow; 1666 1667 passthrough_x = 1668 zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH_X; 1669 passthrough = passthrough_x || 1670 zfsvfs->z_acl_inherit == ZFS_ACL_PASSTHROUGH; 1671 noallow = 1672 zfsvfs->z_acl_inherit == ZFS_ACL_NOALLOW; 1673 1674 *need_chmod = B_TRUE; 1675 pacep = NULL; 1676 aclp = zfs_acl_alloc(paclp->z_version); 1677 if (zfsvfs->z_acl_inherit == ZFS_ACL_DISCARD) 1678 return (aclp); 1679 while (pacep = zfs_acl_next_ace(paclp, pacep, &who, 1680 &access_mask, &iflags, &type)) { 1681 1682 /* 1683 * don't inherit bogus ACEs 1684 */ 1685 if (!zfs_acl_valid_ace_type(type, iflags)) 1686 continue; 1687 1688 if (noallow && type == ALLOW) 1689 continue; 1690 1691 ace_size = aclp->z_ops.ace_size(pacep); 1692 1693 if (!zfs_ace_can_use(vtype, iflags)) 1694 continue; 1695 1696 /* 1697 * If owner@, group@, or everyone@ inheritable 1698 * then zfs_acl_chmod() isn't needed. 1699 */ 1700 if (passthrough && 1701 ((iflags & (ACE_OWNER|ACE_EVERYONE)) || 1702 ((iflags & OWNING_GROUP) == 1703 OWNING_GROUP)) && (vreg || (vdir && (iflags & 1704 ACE_DIRECTORY_INHERIT_ACE)))) { 1705 *need_chmod = B_FALSE; 1706 1707 if (!vdir && passthrough_x && 1708 ((mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)) { 1709 access_mask &= ~ACE_EXECUTE; 1710 } 1711 } 1712 1713 aclnode = zfs_acl_node_alloc(ace_size); 1714 list_insert_tail(&aclp->z_acl, aclnode); 1715 acep = aclnode->z_acldata; 1716 1717 zfs_set_ace(aclp, acep, access_mask, type, 1718 who, iflags|ACE_INHERITED_ACE); 1719 1720 /* 1721 * Copy special opaque data if any 1722 */ 1723 if ((data1sz = paclp->z_ops.ace_data(pacep, &data1)) != 0) { 1724 VERIFY((data2sz = aclp->z_ops.ace_data(acep, 1725 &data2)) == data1sz); 1726 bcopy(data1, data2, data2sz); 1727 } 1728 aclp->z_acl_count++; 1729 aclnode->z_ace_count++; 1730 aclp->z_acl_bytes += aclnode->z_size; 1731 newflags = aclp->z_ops.ace_flags_get(acep); 1732 1733 if (vdir) 1734 aclp->z_hints |= ZFS_INHERIT_ACE; 1735 1736 if ((iflags & ACE_NO_PROPAGATE_INHERIT_ACE) || !vdir) { 1737 newflags &= ~ALL_INHERIT; 1738 aclp->z_ops.ace_flags_set(acep, 1739 newflags|ACE_INHERITED_ACE); 1740 zfs_restricted_update(zfsvfs, aclp, acep); 1741 continue; 1742 } 1743 1744 ASSERT(vdir); 1745 1746 newflags = aclp->z_ops.ace_flags_get(acep); 1747 if ((iflags & (ACE_FILE_INHERIT_ACE | 1748 ACE_DIRECTORY_INHERIT_ACE)) != 1749 ACE_FILE_INHERIT_ACE) { 1750 aclnode2 = zfs_acl_node_alloc(ace_size); 1751 list_insert_tail(&aclp->z_acl, aclnode2); 1752 acep2 = aclnode2->z_acldata; 1753 zfs_set_ace(aclp, acep2, 1754 access_mask, type, who, 1755 iflags|ACE_INHERITED_ACE); 1756 newflags |= ACE_INHERIT_ONLY_ACE; 1757 aclp->z_ops.ace_flags_set(acep, newflags); 1758 newflags &= ~ALL_INHERIT; 1759 aclp->z_ops.ace_flags_set(acep2, 1760 newflags|ACE_INHERITED_ACE); 1761 1762 /* 1763 * Copy special opaque data if any 1764 */ 1765 if ((data1sz = aclp->z_ops.ace_data(acep, 1766 &data1)) != 0) { 1767 VERIFY((data2sz = 1768 aclp->z_ops.ace_data(acep2, 1769 &data2)) == data1sz); 1770 bcopy(data1, data2, data1sz); 1771 } 1772 aclp->z_acl_count++; 1773 aclnode2->z_ace_count++; 1774 aclp->z_acl_bytes += aclnode->z_size; 1775 zfs_restricted_update(zfsvfs, aclp, acep2); 1776 } else { 1777 newflags |= ACE_INHERIT_ONLY_ACE; 1778 aclp->z_ops.ace_flags_set(acep, 1779 newflags|ACE_INHERITED_ACE); 1780 } 1781 } 1782 return (aclp); 1783 } 1784 1785 /* 1786 * Create file system object initial permissions 1787 * including inheritable ACEs. 1788 */ 1789 int 1790 zfs_acl_ids_create(znode_t *dzp, int flag, vattr_t *vap, cred_t *cr, 1791 vsecattr_t *vsecp, zfs_acl_ids_t *acl_ids) 1792 { 1793 int error; 1794 zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1795 zfs_acl_t *paclp; 1796 gid_t gid; 1797 boolean_t need_chmod = B_TRUE; 1798 1799 bzero(acl_ids, sizeof (zfs_acl_ids_t)); 1800 acl_ids->z_mode = MAKEIMODE(vap->va_type, vap->va_mode); 1801 1802 if (vsecp) 1803 if ((error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, cr, 1804 &acl_ids->z_fuidp, &acl_ids->z_aclp)) != 0) 1805 return (error); 1806 1807 /* 1808 * Determine uid and gid. 1809 */ 1810 if ((flag & (IS_ROOT_NODE | IS_REPLAY)) || 1811 ((flag & IS_XATTR) && (vap->va_type == VDIR))) { 1812 acl_ids->z_fuid = zfs_fuid_create(zfsvfs, 1813 (uint64_t)vap->va_uid, cr, 1814 ZFS_OWNER, &acl_ids->z_fuidp); 1815 acl_ids->z_fgid = zfs_fuid_create(zfsvfs, 1816 (uint64_t)vap->va_gid, cr, 1817 ZFS_GROUP, &acl_ids->z_fuidp); 1818 gid = vap->va_gid; 1819 } else { 1820 acl_ids->z_fuid = zfs_fuid_create_cred(zfsvfs, ZFS_OWNER, 1821 cr, &acl_ids->z_fuidp); 1822 acl_ids->z_fgid = 0; 1823 if (vap->va_mask & AT_GID) { 1824 acl_ids->z_fgid = zfs_fuid_create(zfsvfs, 1825 (uint64_t)vap->va_gid, 1826 cr, ZFS_GROUP, &acl_ids->z_fuidp); 1827 gid = vap->va_gid; 1828 if (acl_ids->z_fgid != dzp->z_phys->zp_gid && 1829 !groupmember(vap->va_gid, cr) && 1830 secpolicy_vnode_create_gid(cr) != 0) 1831 acl_ids->z_fgid = 0; 1832 } 1833 if (acl_ids->z_fgid == 0) { 1834 if (dzp->z_phys->zp_mode & S_ISGID) { 1835 acl_ids->z_fgid = dzp->z_phys->zp_gid; 1836 gid = zfs_fuid_map_id(zfsvfs, acl_ids->z_fgid, 1837 cr, ZFS_GROUP); 1838 } else { 1839 acl_ids->z_fgid = zfs_fuid_create_cred(zfsvfs, 1840 ZFS_GROUP, cr, &acl_ids->z_fuidp); 1841 gid = crgetgid(cr); 1842 } 1843 } 1844 } 1845 1846 /* 1847 * If we're creating a directory, and the parent directory has the 1848 * set-GID bit set, set in on the new directory. 1849 * Otherwise, if the user is neither privileged nor a member of the 1850 * file's new group, clear the file's set-GID bit. 1851 */ 1852 1853 if (!(flag & IS_ROOT_NODE) && (dzp->z_phys->zp_mode & S_ISGID) && 1854 (vap->va_type == VDIR)) { 1855 acl_ids->z_mode |= S_ISGID; 1856 } else { 1857 if ((acl_ids->z_mode & S_ISGID) && 1858 secpolicy_vnode_setids_setgids(cr, gid) != 0) 1859 acl_ids->z_mode &= ~S_ISGID; 1860 } 1861 1862 if (acl_ids->z_aclp == NULL) { 1863 mutex_enter(&dzp->z_lock); 1864 if (!(flag & IS_ROOT_NODE) && (ZTOV(dzp)->v_type == VDIR && 1865 (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)) && 1866 !(dzp->z_phys->zp_flags & ZFS_XATTR)) { 1867 mutex_enter(&dzp->z_acl_lock); 1868 VERIFY(0 == zfs_acl_node_read(dzp, &paclp, B_FALSE)); 1869 mutex_exit(&dzp->z_acl_lock); 1870 acl_ids->z_aclp = zfs_acl_inherit(zfsvfs, 1871 vap->va_type, paclp, acl_ids->z_mode, &need_chmod); 1872 zfs_acl_free(paclp); 1873 } else { 1874 acl_ids->z_aclp = 1875 zfs_acl_alloc(zfs_acl_version_zp(dzp)); 1876 } 1877 mutex_exit(&dzp->z_lock); 1878 if (need_chmod) { 1879 acl_ids->z_aclp->z_hints = (vap->va_type == VDIR) ? 1880 ZFS_ACL_AUTO_INHERIT : 0; 1881 zfs_acl_chmod(zfsvfs, acl_ids->z_fuid, 1882 acl_ids->z_mode, acl_ids->z_aclp); 1883 } 1884 } 1885 1886 return (0); 1887 } 1888 1889 /* 1890 * Free ACL and fuid_infop, but not the acl_ids structure 1891 */ 1892 void 1893 zfs_acl_ids_free(zfs_acl_ids_t *acl_ids) 1894 { 1895 if (acl_ids->z_aclp) 1896 zfs_acl_free(acl_ids->z_aclp); 1897 if (acl_ids->z_fuidp) 1898 zfs_fuid_info_free(acl_ids->z_fuidp); 1899 acl_ids->z_aclp = NULL; 1900 acl_ids->z_fuidp = NULL; 1901 } 1902 1903 1904 /* 1905 * Retrieve a files ACL 1906 */ 1907 int 1908 zfs_getacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr) 1909 { 1910 zfs_acl_t *aclp; 1911 ulong_t mask; 1912 int error; 1913 int count = 0; 1914 int largeace = 0; 1915 1916 mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT | 1917 VSA_ACE_ACLFLAGS | VSA_ACE_ALLTYPES); 1918 1919 if (error = zfs_zaccess(zp, ACE_READ_ACL, 0, skipaclchk, cr)) 1920 return (error); 1921 1922 if (mask == 0) 1923 return (ENOSYS); 1924 1925 mutex_enter(&zp->z_acl_lock); 1926 1927 error = zfs_acl_node_read(zp, &aclp, B_FALSE); 1928 if (error != 0) { 1929 mutex_exit(&zp->z_acl_lock); 1930 return (error); 1931 } 1932 1933 /* 1934 * Scan ACL to determine number of ACEs 1935 */ 1936 if ((zp->z_phys->zp_flags & ZFS_ACL_OBJ_ACE) && 1937 !(mask & VSA_ACE_ALLTYPES)) { 1938 void *zacep = NULL; 1939 uint64_t who; 1940 uint32_t access_mask; 1941 uint16_t type, iflags; 1942 1943 while (zacep = zfs_acl_next_ace(aclp, zacep, 1944 &who, &access_mask, &iflags, &type)) { 1945 switch (type) { 1946 case ACE_ACCESS_ALLOWED_OBJECT_ACE_TYPE: 1947 case ACE_ACCESS_DENIED_OBJECT_ACE_TYPE: 1948 case ACE_SYSTEM_AUDIT_OBJECT_ACE_TYPE: 1949 case ACE_SYSTEM_ALARM_OBJECT_ACE_TYPE: 1950 largeace++; 1951 continue; 1952 default: 1953 count++; 1954 } 1955 } 1956 vsecp->vsa_aclcnt = count; 1957 } else 1958 count = aclp->z_acl_count; 1959 1960 if (mask & VSA_ACECNT) { 1961 vsecp->vsa_aclcnt = count; 1962 } 1963 1964 if (mask & VSA_ACE) { 1965 size_t aclsz; 1966 1967 zfs_acl_node_t *aclnode = list_head(&aclp->z_acl); 1968 1969 aclsz = count * sizeof (ace_t) + 1970 sizeof (ace_object_t) * largeace; 1971 1972 vsecp->vsa_aclentp = kmem_alloc(aclsz, KM_SLEEP); 1973 vsecp->vsa_aclentsz = aclsz; 1974 1975 if (aclp->z_version == ZFS_ACL_VERSION_FUID) 1976 zfs_copy_fuid_2_ace(zp->z_zfsvfs, aclp, cr, 1977 vsecp->vsa_aclentp, !(mask & VSA_ACE_ALLTYPES)); 1978 else { 1979 bcopy(aclnode->z_acldata, vsecp->vsa_aclentp, 1980 count * sizeof (ace_t)); 1981 } 1982 } 1983 if (mask & VSA_ACE_ACLFLAGS) { 1984 vsecp->vsa_aclflags = 0; 1985 if (zp->z_phys->zp_flags & ZFS_ACL_DEFAULTED) 1986 vsecp->vsa_aclflags |= ACL_DEFAULTED; 1987 if (zp->z_phys->zp_flags & ZFS_ACL_PROTECTED) 1988 vsecp->vsa_aclflags |= ACL_PROTECTED; 1989 if (zp->z_phys->zp_flags & ZFS_ACL_AUTO_INHERIT) 1990 vsecp->vsa_aclflags |= ACL_AUTO_INHERIT; 1991 } 1992 1993 mutex_exit(&zp->z_acl_lock); 1994 1995 zfs_acl_free(aclp); 1996 1997 return (0); 1998 } 1999 2000 int 2001 zfs_vsec_2_aclp(zfsvfs_t *zfsvfs, vtype_t obj_type, 2002 vsecattr_t *vsecp, cred_t *cr, zfs_fuid_info_t **fuidp, zfs_acl_t **zaclp) 2003 { 2004 zfs_acl_t *aclp; 2005 zfs_acl_node_t *aclnode; 2006 int aclcnt = vsecp->vsa_aclcnt; 2007 int error; 2008 2009 if (vsecp->vsa_aclcnt > MAX_ACL_ENTRIES || vsecp->vsa_aclcnt <= 0) 2010 return (EINVAL); 2011 2012 aclp = zfs_acl_alloc(zfs_acl_version(zfsvfs->z_version)); 2013 2014 aclp->z_hints = 0; 2015 aclnode = zfs_acl_node_alloc(aclcnt * sizeof (zfs_object_ace_t)); 2016 if (aclp->z_version == ZFS_ACL_VERSION_INITIAL) { 2017 if ((error = zfs_copy_ace_2_oldace(obj_type, aclp, 2018 (ace_t *)vsecp->vsa_aclentp, aclnode->z_acldata, 2019 aclcnt, &aclnode->z_size)) != 0) { 2020 zfs_acl_free(aclp); 2021 zfs_acl_node_free(aclnode); 2022 return (error); 2023 } 2024 } else { 2025 if ((error = zfs_copy_ace_2_fuid(zfsvfs, obj_type, aclp, 2026 vsecp->vsa_aclentp, aclnode->z_acldata, aclcnt, 2027 &aclnode->z_size, fuidp, cr)) != 0) { 2028 zfs_acl_free(aclp); 2029 zfs_acl_node_free(aclnode); 2030 return (error); 2031 } 2032 } 2033 aclp->z_acl_bytes = aclnode->z_size; 2034 aclnode->z_ace_count = aclcnt; 2035 aclp->z_acl_count = aclcnt; 2036 list_insert_head(&aclp->z_acl, aclnode); 2037 2038 /* 2039 * If flags are being set then add them to z_hints 2040 */ 2041 if (vsecp->vsa_mask & VSA_ACE_ACLFLAGS) { 2042 if (vsecp->vsa_aclflags & ACL_PROTECTED) 2043 aclp->z_hints |= ZFS_ACL_PROTECTED; 2044 if (vsecp->vsa_aclflags & ACL_DEFAULTED) 2045 aclp->z_hints |= ZFS_ACL_DEFAULTED; 2046 if (vsecp->vsa_aclflags & ACL_AUTO_INHERIT) 2047 aclp->z_hints |= ZFS_ACL_AUTO_INHERIT; 2048 } 2049 2050 *zaclp = aclp; 2051 2052 return (0); 2053 } 2054 2055 /* 2056 * Set a files ACL 2057 */ 2058 int 2059 zfs_setacl(znode_t *zp, vsecattr_t *vsecp, boolean_t skipaclchk, cred_t *cr) 2060 { 2061 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2062 zilog_t *zilog = zfsvfs->z_log; 2063 ulong_t mask = vsecp->vsa_mask & (VSA_ACE | VSA_ACECNT); 2064 dmu_tx_t *tx; 2065 int error; 2066 zfs_acl_t *aclp; 2067 zfs_fuid_info_t *fuidp = NULL; 2068 boolean_t fuid_dirtied; 2069 2070 if (mask == 0) 2071 return (ENOSYS); 2072 2073 if (zp->z_phys->zp_flags & ZFS_IMMUTABLE) 2074 return (EPERM); 2075 2076 if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr)) 2077 return (error); 2078 2079 error = zfs_vsec_2_aclp(zfsvfs, ZTOV(zp)->v_type, vsecp, cr, &fuidp, 2080 &aclp); 2081 if (error) 2082 return (error); 2083 2084 /* 2085 * If ACL wide flags aren't being set then preserve any 2086 * existing flags. 2087 */ 2088 if (!(vsecp->vsa_mask & VSA_ACE_ACLFLAGS)) { 2089 aclp->z_hints |= (zp->z_phys->zp_flags & V4_ACL_WIDE_FLAGS); 2090 } 2091 top: 2092 if (error = zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr)) { 2093 zfs_acl_free(aclp); 2094 return (error); 2095 } 2096 2097 mutex_enter(&zp->z_lock); 2098 mutex_enter(&zp->z_acl_lock); 2099 2100 tx = dmu_tx_create(zfsvfs->z_os); 2101 dmu_tx_hold_bonus(tx, zp->z_id); 2102 2103 if (zp->z_phys->zp_acl.z_acl_extern_obj) { 2104 /* Are we upgrading ACL? */ 2105 if (zfsvfs->z_version <= ZPL_VERSION_FUID && 2106 zp->z_phys->zp_acl.z_acl_version == 2107 ZFS_ACL_VERSION_INITIAL) { 2108 dmu_tx_hold_free(tx, 2109 zp->z_phys->zp_acl.z_acl_extern_obj, 2110 0, DMU_OBJECT_END); 2111 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2112 0, aclp->z_acl_bytes); 2113 } else { 2114 dmu_tx_hold_write(tx, 2115 zp->z_phys->zp_acl.z_acl_extern_obj, 2116 0, aclp->z_acl_bytes); 2117 } 2118 } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 2119 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, aclp->z_acl_bytes); 2120 } 2121 fuid_dirtied = zfsvfs->z_fuid_dirty; 2122 if (fuid_dirtied) { 2123 if (zfsvfs->z_fuid_obj == 0) { 2124 dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2125 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 2126 FUID_SIZE_ESTIMATE(zfsvfs)); 2127 dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 2128 } else { 2129 dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 2130 dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 2131 FUID_SIZE_ESTIMATE(zfsvfs)); 2132 } 2133 } 2134 2135 error = dmu_tx_assign(tx, TXG_NOWAIT); 2136 if (error) { 2137 mutex_exit(&zp->z_acl_lock); 2138 mutex_exit(&zp->z_lock); 2139 2140 if (error == ERESTART) { 2141 dmu_tx_wait(tx); 2142 dmu_tx_abort(tx); 2143 goto top; 2144 } 2145 dmu_tx_abort(tx); 2146 zfs_acl_free(aclp); 2147 return (error); 2148 } 2149 2150 error = zfs_aclset_common(zp, aclp, cr, tx); 2151 ASSERT(error == 0); 2152 2153 if (fuid_dirtied) 2154 zfs_fuid_sync(zfsvfs, tx); 2155 2156 zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2157 zfs_log_acl(zilog, tx, zp, vsecp, fuidp); 2158 2159 if (fuidp) 2160 zfs_fuid_info_free(fuidp); 2161 zfs_acl_free(aclp); 2162 dmu_tx_commit(tx); 2163 done: 2164 mutex_exit(&zp->z_acl_lock); 2165 mutex_exit(&zp->z_lock); 2166 2167 return (error); 2168 } 2169 2170 /* 2171 * working_mode returns the permissions that were not granted 2172 */ 2173 static int 2174 zfs_zaccess_common(znode_t *zp, uint32_t v4_mode, uint32_t *working_mode, 2175 boolean_t *check_privs, boolean_t skipaclchk, cred_t *cr) 2176 { 2177 zfs_acl_t *aclp; 2178 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2179 int error; 2180 uid_t uid = crgetuid(cr); 2181 uint64_t who; 2182 uint16_t type, iflags; 2183 uint16_t entry_type; 2184 uint32_t access_mask; 2185 uint32_t deny_mask = 0; 2186 zfs_ace_hdr_t *acep = NULL; 2187 boolean_t checkit; 2188 uid_t fowner; 2189 uid_t gowner; 2190 2191 /* 2192 * Short circuit empty requests 2193 */ 2194 if (v4_mode == 0) 2195 return (0); 2196 2197 *check_privs = B_TRUE; 2198 2199 if (zfsvfs->z_replay) { 2200 *working_mode = 0; 2201 return (0); 2202 } 2203 2204 *working_mode = v4_mode; 2205 2206 if ((v4_mode & WRITE_MASK) && 2207 (zp->z_zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) && 2208 (!IS_DEVVP(ZTOV(zp)) || 2209 (IS_DEVVP(ZTOV(zp)) && (v4_mode & WRITE_MASK_ATTRS)))) { 2210 *check_privs = B_FALSE; 2211 return (EROFS); 2212 } 2213 2214 /* 2215 * Only check for READONLY on non-directories. 2216 */ 2217 if ((v4_mode & WRITE_MASK_DATA) && 2218 (((ZTOV(zp)->v_type != VDIR) && 2219 (zp->z_phys->zp_flags & (ZFS_READONLY | ZFS_IMMUTABLE))) || 2220 (ZTOV(zp)->v_type == VDIR && 2221 (zp->z_phys->zp_flags & ZFS_IMMUTABLE)))) { 2222 *check_privs = B_FALSE; 2223 return (EPERM); 2224 } 2225 2226 if ((v4_mode & (ACE_DELETE | ACE_DELETE_CHILD)) && 2227 (zp->z_phys->zp_flags & ZFS_NOUNLINK)) { 2228 *check_privs = B_FALSE; 2229 return (EPERM); 2230 } 2231 2232 if (((v4_mode & (ACE_READ_DATA|ACE_EXECUTE)) && 2233 (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED))) { 2234 *check_privs = B_FALSE; 2235 return (EACCES); 2236 } 2237 2238 /* 2239 * The caller requested that the ACL check be skipped. This 2240 * would only happen if the caller checked VOP_ACCESS() with a 2241 * 32 bit ACE mask and already had the appropriate permissions. 2242 */ 2243 if (skipaclchk) { 2244 *working_mode = 0; 2245 return (0); 2246 } 2247 2248 zfs_fuid_map_ids(zp, cr, &fowner, &gowner); 2249 2250 mutex_enter(&zp->z_acl_lock); 2251 2252 error = zfs_acl_node_read(zp, &aclp, B_FALSE); 2253 if (error != 0) { 2254 mutex_exit(&zp->z_acl_lock); 2255 return (error); 2256 } 2257 2258 while (acep = zfs_acl_next_ace(aclp, acep, &who, &access_mask, 2259 &iflags, &type)) { 2260 2261 if (!zfs_acl_valid_ace_type(type, iflags)) 2262 continue; 2263 2264 if (ZTOV(zp)->v_type == VDIR && (iflags & ACE_INHERIT_ONLY_ACE)) 2265 continue; 2266 2267 entry_type = (iflags & ACE_TYPE_FLAGS); 2268 2269 checkit = B_FALSE; 2270 2271 switch (entry_type) { 2272 case ACE_OWNER: 2273 if (uid == fowner) 2274 checkit = B_TRUE; 2275 break; 2276 case OWNING_GROUP: 2277 who = gowner; 2278 /*FALLTHROUGH*/ 2279 case ACE_IDENTIFIER_GROUP: 2280 checkit = zfs_groupmember(zfsvfs, who, cr); 2281 break; 2282 case ACE_EVERYONE: 2283 checkit = B_TRUE; 2284 break; 2285 2286 /* USER Entry */ 2287 default: 2288 if (entry_type == 0) { 2289 uid_t newid; 2290 2291 newid = zfs_fuid_map_id(zfsvfs, who, cr, 2292 ZFS_ACE_USER); 2293 if (newid != IDMAP_WK_CREATOR_OWNER_UID && 2294 uid == newid) 2295 checkit = B_TRUE; 2296 break; 2297 } else { 2298 zfs_acl_free(aclp); 2299 mutex_exit(&zp->z_acl_lock); 2300 return (EIO); 2301 } 2302 } 2303 2304 if (checkit) { 2305 uint32_t mask_matched = (access_mask & *working_mode); 2306 2307 if (mask_matched) { 2308 if (type == DENY) 2309 deny_mask |= mask_matched; 2310 2311 *working_mode &= ~mask_matched; 2312 } 2313 } 2314 2315 /* Are we done? */ 2316 if (*working_mode == 0) 2317 break; 2318 } 2319 2320 mutex_exit(&zp->z_acl_lock); 2321 zfs_acl_free(aclp); 2322 2323 /* Put the found 'denies' back on the working mode */ 2324 if (deny_mask) { 2325 *working_mode |= deny_mask; 2326 return (EACCES); 2327 } else if (*working_mode) { 2328 return (-1); 2329 } 2330 2331 return (0); 2332 } 2333 2334 static int 2335 zfs_zaccess_append(znode_t *zp, uint32_t *working_mode, boolean_t *check_privs, 2336 cred_t *cr) 2337 { 2338 if (*working_mode != ACE_WRITE_DATA) 2339 return (EACCES); 2340 2341 return (zfs_zaccess_common(zp, ACE_APPEND_DATA, working_mode, 2342 check_privs, B_FALSE, cr)); 2343 } 2344 2345 /* 2346 * Determine whether Access should be granted/denied, invoking least 2347 * priv subsytem when a deny is determined. 2348 */ 2349 int 2350 zfs_zaccess(znode_t *zp, int mode, int flags, boolean_t skipaclchk, cred_t *cr) 2351 { 2352 uint32_t working_mode; 2353 int error; 2354 int is_attr; 2355 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2356 boolean_t check_privs; 2357 znode_t *xzp; 2358 znode_t *check_zp = zp; 2359 2360 is_attr = ((zp->z_phys->zp_flags & ZFS_XATTR) && 2361 (ZTOV(zp)->v_type == VDIR)); 2362 2363 /* 2364 * If attribute then validate against base file 2365 */ 2366 if (is_attr) { 2367 if ((error = zfs_zget(zp->z_zfsvfs, 2368 zp->z_phys->zp_parent, &xzp)) != 0) { 2369 return (error); 2370 } 2371 2372 check_zp = xzp; 2373 2374 /* 2375 * fixup mode to map to xattr perms 2376 */ 2377 2378 if (mode & (ACE_WRITE_DATA|ACE_APPEND_DATA)) { 2379 mode &= ~(ACE_WRITE_DATA|ACE_APPEND_DATA); 2380 mode |= ACE_WRITE_NAMED_ATTRS; 2381 } 2382 2383 if (mode & (ACE_READ_DATA|ACE_EXECUTE)) { 2384 mode &= ~(ACE_READ_DATA|ACE_EXECUTE); 2385 mode |= ACE_READ_NAMED_ATTRS; 2386 } 2387 } 2388 2389 if ((error = zfs_zaccess_common(check_zp, mode, &working_mode, 2390 &check_privs, skipaclchk, cr)) == 0) { 2391 if (is_attr) 2392 VN_RELE(ZTOV(xzp)); 2393 return (0); 2394 } 2395 2396 if (error && !check_privs) { 2397 if (is_attr) 2398 VN_RELE(ZTOV(xzp)); 2399 return (error); 2400 } 2401 2402 if (error && (flags & V_APPEND)) { 2403 error = zfs_zaccess_append(zp, &working_mode, &check_privs, cr); 2404 } 2405 2406 if (error && check_privs) { 2407 uid_t owner; 2408 mode_t checkmode = 0; 2409 2410 owner = zfs_fuid_map_id(zfsvfs, check_zp->z_phys->zp_uid, cr, 2411 ZFS_OWNER); 2412 2413 /* 2414 * First check for implicit owner permission on 2415 * read_acl/read_attributes 2416 */ 2417 2418 error = 0; 2419 ASSERT(working_mode != 0); 2420 2421 if ((working_mode & (ACE_READ_ACL|ACE_READ_ATTRIBUTES) && 2422 owner == crgetuid(cr))) 2423 working_mode &= ~(ACE_READ_ACL|ACE_READ_ATTRIBUTES); 2424 2425 if (working_mode & (ACE_READ_DATA|ACE_READ_NAMED_ATTRS| 2426 ACE_READ_ACL|ACE_READ_ATTRIBUTES|ACE_SYNCHRONIZE)) 2427 checkmode |= VREAD; 2428 if (working_mode & (ACE_WRITE_DATA|ACE_WRITE_NAMED_ATTRS| 2429 ACE_APPEND_DATA|ACE_WRITE_ATTRIBUTES|ACE_SYNCHRONIZE)) 2430 checkmode |= VWRITE; 2431 if (working_mode & ACE_EXECUTE) 2432 checkmode |= VEXEC; 2433 2434 if (checkmode) 2435 error = secpolicy_vnode_access(cr, ZTOV(check_zp), 2436 owner, checkmode); 2437 2438 if (error == 0 && (working_mode & ACE_WRITE_OWNER)) 2439 error = secpolicy_vnode_chown(cr, B_TRUE); 2440 if (error == 0 && (working_mode & ACE_WRITE_ACL)) 2441 error = secpolicy_vnode_setdac(cr, owner); 2442 2443 if (error == 0 && (working_mode & 2444 (ACE_DELETE|ACE_DELETE_CHILD))) 2445 error = secpolicy_vnode_remove(cr); 2446 2447 if (error == 0 && (working_mode & ACE_SYNCHRONIZE)) { 2448 error = secpolicy_vnode_chown(cr, B_FALSE); 2449 } 2450 if (error == 0) { 2451 /* 2452 * See if any bits other than those already checked 2453 * for are still present. If so then return EACCES 2454 */ 2455 if (working_mode & ~(ZFS_CHECKED_MASKS)) { 2456 error = EACCES; 2457 } 2458 } 2459 } 2460 2461 if (is_attr) 2462 VN_RELE(ZTOV(xzp)); 2463 2464 return (error); 2465 } 2466 2467 /* 2468 * Translate traditional unix VREAD/VWRITE/VEXEC mode into 2469 * native ACL format and call zfs_zaccess() 2470 */ 2471 int 2472 zfs_zaccess_rwx(znode_t *zp, mode_t mode, int flags, cred_t *cr) 2473 { 2474 return (zfs_zaccess(zp, zfs_unix_to_v4(mode >> 6), flags, B_FALSE, cr)); 2475 } 2476 2477 /* 2478 * Access function for secpolicy_vnode_setattr 2479 */ 2480 int 2481 zfs_zaccess_unix(znode_t *zp, mode_t mode, cred_t *cr) 2482 { 2483 int v4_mode = zfs_unix_to_v4(mode >> 6); 2484 2485 return (zfs_zaccess(zp, v4_mode, 0, B_FALSE, cr)); 2486 } 2487 2488 static int 2489 zfs_delete_final_check(znode_t *zp, znode_t *dzp, 2490 mode_t missing_perms, cred_t *cr) 2491 { 2492 int error; 2493 uid_t downer; 2494 zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2495 2496 downer = zfs_fuid_map_id(zfsvfs, dzp->z_phys->zp_uid, cr, ZFS_OWNER); 2497 2498 error = secpolicy_vnode_access(cr, ZTOV(dzp), downer, missing_perms); 2499 2500 if (error == 0) 2501 error = zfs_sticky_remove_access(dzp, zp, cr); 2502 2503 return (error); 2504 } 2505 2506 /* 2507 * Determine whether Access should be granted/deny, without 2508 * consulting least priv subsystem. 2509 * 2510 * 2511 * The following chart is the recommended NFSv4 enforcement for 2512 * ability to delete an object. 2513 * 2514 * ------------------------------------------------------- 2515 * | Parent Dir | Target Object Permissions | 2516 * | permissions | | 2517 * ------------------------------------------------------- 2518 * | | ACL Allows | ACL Denies| Delete | 2519 * | | Delete | Delete | unspecified| 2520 * ------------------------------------------------------- 2521 * | ACL Allows | Permit | Permit | Permit | 2522 * | DELETE_CHILD | | 2523 * ------------------------------------------------------- 2524 * | ACL Denies | Permit | Deny | Deny | 2525 * | DELETE_CHILD | | | | 2526 * ------------------------------------------------------- 2527 * | ACL specifies | | | | 2528 * | only allow | Permit | Permit | Permit | 2529 * | write and | | | | 2530 * | execute | | | | 2531 * ------------------------------------------------------- 2532 * | ACL denies | | | | 2533 * | write and | Permit | Deny | Deny | 2534 * | execute | | | | 2535 * ------------------------------------------------------- 2536 * ^ 2537 * | 2538 * No search privilege, can't even look up file? 2539 * 2540 */ 2541 int 2542 zfs_zaccess_delete(znode_t *dzp, znode_t *zp, cred_t *cr) 2543 { 2544 uint32_t dzp_working_mode = 0; 2545 uint32_t zp_working_mode = 0; 2546 int dzp_error, zp_error; 2547 mode_t missing_perms; 2548 boolean_t dzpcheck_privs = B_TRUE; 2549 boolean_t zpcheck_privs = B_TRUE; 2550 2551 /* 2552 * We want specific DELETE permissions to 2553 * take precedence over WRITE/EXECUTE. We don't 2554 * want an ACL such as this to mess us up. 2555 * user:joe:write_data:deny,user:joe:delete:allow 2556 * 2557 * However, deny permissions may ultimately be overridden 2558 * by secpolicy_vnode_access(). 2559 * 2560 * We will ask for all of the necessary permissions and then 2561 * look at the working modes from the directory and target object 2562 * to determine what was found. 2563 */ 2564 2565 if (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_NOUNLINK)) 2566 return (EPERM); 2567 2568 /* 2569 * First row 2570 * If the directory permissions allow the delete, we are done. 2571 */ 2572 if ((dzp_error = zfs_zaccess_common(dzp, ACE_DELETE_CHILD, 2573 &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0) 2574 return (0); 2575 2576 /* 2577 * If target object has delete permission then we are done 2578 */ 2579 if ((zp_error = zfs_zaccess_common(zp, ACE_DELETE, &zp_working_mode, 2580 &zpcheck_privs, B_FALSE, cr)) == 0) 2581 return (0); 2582 2583 ASSERT(dzp_error && zp_error); 2584 2585 if (!dzpcheck_privs) 2586 return (dzp_error); 2587 if (!zpcheck_privs) 2588 return (zp_error); 2589 2590 /* 2591 * Second row 2592 * 2593 * If directory returns EACCES then delete_child was denied 2594 * due to deny delete_child. In this case send the request through 2595 * secpolicy_vnode_remove(). We don't use zfs_delete_final_check() 2596 * since that *could* allow the delete based on write/execute permission 2597 * and we want delete permissions to override write/execute. 2598 */ 2599 2600 if (dzp_error == EACCES) 2601 return (secpolicy_vnode_remove(cr)); 2602 2603 /* 2604 * Third Row 2605 * only need to see if we have write/execute on directory. 2606 */ 2607 2608 if ((dzp_error = zfs_zaccess_common(dzp, ACE_EXECUTE|ACE_WRITE_DATA, 2609 &dzp_working_mode, &dzpcheck_privs, B_FALSE, cr)) == 0) 2610 return (zfs_sticky_remove_access(dzp, zp, cr)); 2611 2612 if (!dzpcheck_privs) 2613 return (dzp_error); 2614 2615 /* 2616 * Fourth row 2617 */ 2618 2619 missing_perms = (dzp_working_mode & ACE_WRITE_DATA) ? VWRITE : 0; 2620 missing_perms |= (dzp_working_mode & ACE_EXECUTE) ? VEXEC : 0; 2621 2622 ASSERT(missing_perms); 2623 2624 return (zfs_delete_final_check(zp, dzp, missing_perms, cr)); 2625 2626 } 2627 2628 int 2629 zfs_zaccess_rename(znode_t *sdzp, znode_t *szp, znode_t *tdzp, 2630 znode_t *tzp, cred_t *cr) 2631 { 2632 int add_perm; 2633 int error; 2634 2635 if (szp->z_phys->zp_flags & ZFS_AV_QUARANTINED) 2636 return (EACCES); 2637 2638 add_perm = (ZTOV(szp)->v_type == VDIR) ? 2639 ACE_ADD_SUBDIRECTORY : ACE_ADD_FILE; 2640 2641 /* 2642 * Rename permissions are combination of delete permission + 2643 * add file/subdir permission. 2644 */ 2645 2646 /* 2647 * first make sure we do the delete portion. 2648 * 2649 * If that succeeds then check for add_file/add_subdir permissions 2650 */ 2651 2652 if (error = zfs_zaccess_delete(sdzp, szp, cr)) 2653 return (error); 2654 2655 /* 2656 * If we have a tzp, see if we can delete it? 2657 */ 2658 if (tzp) { 2659 if (error = zfs_zaccess_delete(tdzp, tzp, cr)) 2660 return (error); 2661 } 2662 2663 /* 2664 * Now check for add permissions 2665 */ 2666 error = zfs_zaccess(tdzp, add_perm, 0, B_FALSE, cr); 2667 2668 return (error); 2669 } 2670