1 /*- 2 * Copyright (c) 2008-2009 Edward Tomasz Napierała <trasz@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * ACL support routines specific to NFSv4 access control lists. These are 29 * utility routines for code common across file systems implementing NFSv4 30 * ACLs. 31 */ 32 33 #ifdef _KERNEL 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/mount.h> 40 #include <sys/priv.h> 41 #include <sys/vnode.h> 42 #include <sys/errno.h> 43 #include <sys/stat.h> 44 #include <sys/acl.h> 45 #else 46 #include <errno.h> 47 #include <assert.h> 48 #include <sys/acl.h> 49 #include <sys/stat.h> 50 #define KASSERT(a, b) assert(a) 51 #define CTASSERT(a) 52 #endif /* _KERNEL */ 53 54 #ifdef _KERNEL 55 56 static struct { 57 accmode_t accmode; 58 int mask; 59 } accmode2mask[] = {{VREAD, ACL_READ_DATA}, 60 {VWRITE, ACL_WRITE_DATA}, 61 {VAPPEND, ACL_APPEND_DATA}, 62 {VEXEC, ACL_EXECUTE}, 63 {VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS}, 64 {VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS}, 65 {VDELETE_CHILD, ACL_DELETE_CHILD}, 66 {VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES}, 67 {VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES}, 68 {VDELETE, ACL_DELETE}, 69 {VREAD_ACL, ACL_READ_ACL}, 70 {VWRITE_ACL, ACL_WRITE_ACL}, 71 {VWRITE_OWNER, ACL_WRITE_OWNER}, 72 {VSYNCHRONIZE, ACL_SYNCHRONIZE}, 73 {0, 0}}; 74 75 static int 76 _access_mask_from_accmode(accmode_t accmode) 77 { 78 int access_mask = 0, i; 79 80 for (i = 0; accmode2mask[i].accmode != 0; i++) { 81 if (accmode & accmode2mask[i].accmode) 82 access_mask |= accmode2mask[i].mask; 83 } 84 85 /* 86 * VAPPEND is just a modifier for VWRITE; if the caller asked 87 * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only. 88 */ 89 if (access_mask & ACL_APPEND_DATA) 90 access_mask &= ~ACL_WRITE_DATA; 91 92 return (access_mask); 93 } 94 95 /* 96 * Return 0, iff access is allowed, 1 otherwise. 97 */ 98 static int 99 _acl_denies(const struct acl *aclp, int access_mask, struct ucred *cred, 100 int file_uid, int file_gid, int *denied_explicitly) 101 { 102 int i; 103 const struct acl_entry *entry; 104 105 if (denied_explicitly != NULL) 106 *denied_explicitly = 0; 107 108 KASSERT(aclp->acl_cnt > 0, ("aclp->acl_cnt > 0")); 109 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES, 110 ("aclp->acl_cnt <= ACL_MAX_ENTRIES")); 111 112 for (i = 0; i < aclp->acl_cnt; i++) { 113 entry = &(aclp->acl_entry[i]); 114 115 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW && 116 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY) 117 continue; 118 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) 119 continue; 120 switch (entry->ae_tag) { 121 case ACL_USER_OBJ: 122 if (file_uid != cred->cr_uid) 123 continue; 124 break; 125 case ACL_USER: 126 if (entry->ae_id != cred->cr_uid) 127 continue; 128 break; 129 case ACL_GROUP_OBJ: 130 if (!groupmember(file_gid, cred)) 131 continue; 132 break; 133 case ACL_GROUP: 134 if (!groupmember(entry->ae_id, cred)) 135 continue; 136 break; 137 default: 138 KASSERT(entry->ae_tag == ACL_EVERYONE, 139 ("entry->ae_tag == ACL_EVERYONE")); 140 } 141 142 if (entry->ae_entry_type == ACL_ENTRY_TYPE_DENY) { 143 if (entry->ae_perm & access_mask) { 144 if (denied_explicitly != NULL) 145 *denied_explicitly = 1; 146 return (1); 147 } 148 } 149 150 access_mask &= ~(entry->ae_perm); 151 if (access_mask == 0) 152 return (0); 153 } 154 155 return (1); 156 } 157 158 int 159 vaccess_acl_nfs4(enum vtype type, uid_t file_uid, gid_t file_gid, 160 struct acl *aclp, accmode_t accmode, struct ucred *cred, int *privused) 161 { 162 accmode_t priv_granted = 0; 163 int denied, explicitly_denied, access_mask, is_directory, 164 must_be_owner = 0; 165 166 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND | 167 VEXPLICIT_DENY | VREAD_NAMED_ATTRS | VWRITE_NAMED_ATTRS | 168 VDELETE_CHILD | VREAD_ATTRIBUTES | VWRITE_ATTRIBUTES | VDELETE | 169 VREAD_ACL | VWRITE_ACL | VWRITE_OWNER | VSYNCHRONIZE)) == 0, 170 ("invalid bit in accmode")); 171 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 172 ("VAPPEND without VWRITE")); 173 174 if (privused != NULL) 175 *privused = 0; 176 177 if (accmode & VADMIN) 178 must_be_owner = 1; 179 180 /* 181 * Ignore VSYNCHRONIZE permission. 182 */ 183 accmode &= ~VSYNCHRONIZE; 184 185 access_mask = _access_mask_from_accmode(accmode); 186 187 if (type == VDIR) 188 is_directory = 1; 189 else 190 is_directory = 0; 191 192 /* 193 * File owner is always allowed to read and write the ACL 194 * and basic attributes. This is to prevent a situation 195 * where user would change ACL in a way that prevents him 196 * from undoing the change. 197 */ 198 if (file_uid == cred->cr_uid) 199 access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL | 200 ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES); 201 202 /* 203 * Ignore append permission for regular files; use write 204 * permission instead. 205 */ 206 if (!is_directory && (access_mask & ACL_APPEND_DATA)) { 207 access_mask &= ~ACL_APPEND_DATA; 208 access_mask |= ACL_WRITE_DATA; 209 } 210 211 denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid, 212 &explicitly_denied); 213 214 if (must_be_owner) { 215 if (file_uid != cred->cr_uid) 216 denied = EPERM; 217 } 218 219 if (!denied) 220 return (0); 221 222 /* 223 * Access failed. Iff it was not denied explicitly and 224 * VEXPLICIT_DENY flag was specified, allow access. 225 */ 226 if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0) 227 return (0); 228 229 accmode &= ~VEXPLICIT_DENY; 230 231 /* 232 * No match. Try to use privileges, if there are any. 233 */ 234 if (is_directory) { 235 if ((accmode & VEXEC) && !priv_check_cred(cred, 236 PRIV_VFS_LOOKUP, 0)) 237 priv_granted |= VEXEC; 238 } else { 239 if ((accmode & VEXEC) && !priv_check_cred(cred, 240 PRIV_VFS_EXEC, 0)) 241 priv_granted |= VEXEC; 242 } 243 244 if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ, 0)) 245 priv_granted |= VREAD; 246 247 if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) && 248 !priv_check_cred(cred, PRIV_VFS_WRITE, 0)) 249 priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD); 250 251 if ((accmode & VADMIN_PERMS) && 252 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0)) 253 priv_granted |= VADMIN_PERMS; 254 255 if ((accmode & VSTAT_PERMS) && 256 !priv_check_cred(cred, PRIV_VFS_STAT, 0)) 257 priv_granted |= VSTAT_PERMS; 258 259 if ((accmode & priv_granted) == accmode) { 260 if (privused != NULL) 261 *privused = 1; 262 263 return (0); 264 } 265 266 if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE)) 267 denied = EPERM; 268 else 269 denied = EACCES; 270 271 return (denied); 272 } 273 #endif /* _KERNEL */ 274 275 static int 276 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm, 277 acl_entry_type_t entry_type) 278 { 279 if (entry->ae_tag != tag) 280 return (0); 281 282 if (entry->ae_id != ACL_UNDEFINED_ID) 283 return (0); 284 285 if (entry->ae_perm != perm) 286 return (0); 287 288 if (entry->ae_entry_type != entry_type) 289 return (0); 290 291 if (entry->ae_flags != 0) 292 return (0); 293 294 return (1); 295 } 296 297 static struct acl_entry * 298 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm, 299 acl_entry_type_t entry_type) 300 { 301 struct acl_entry *entry; 302 303 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES, 304 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES")); 305 306 entry = &(aclp->acl_entry[aclp->acl_cnt]); 307 aclp->acl_cnt++; 308 309 entry->ae_tag = tag; 310 entry->ae_id = ACL_UNDEFINED_ID; 311 entry->ae_perm = perm; 312 entry->ae_entry_type = entry_type; 313 entry->ae_flags = 0; 314 315 return (entry); 316 } 317 318 static struct acl_entry * 319 _acl_duplicate_entry(struct acl *aclp, int entry_index) 320 { 321 int i; 322 323 KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES, 324 ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES")); 325 326 for (i = aclp->acl_cnt; i > entry_index; i--) 327 aclp->acl_entry[i] = aclp->acl_entry[i - 1]; 328 329 aclp->acl_cnt++; 330 331 return (&(aclp->acl_entry[entry_index + 1])); 332 } 333 334 void 335 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode, int file_owner_id) 336 { 337 int i, meets, must_append; 338 struct acl_entry *entry, *copy, *previous, 339 *a1, *a2, *a3, *a4, *a5, *a6; 340 mode_t amode; 341 const int READ = 04; 342 const int WRITE = 02; 343 const int EXEC = 01; 344 345 KASSERT(aclp->acl_cnt >= 0, ("aclp->acl_cnt >= 0")); 346 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES, 347 ("aclp->acl_cnt <= ACL_MAX_ENTRIES")); 348 349 /* 350 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt 351 * 352 * 3.16.6.3. Applying a Mode to an Existing ACL 353 */ 354 355 /* 356 * 1. For each ACE: 357 */ 358 for (i = 0; i < aclp->acl_cnt; i++) { 359 entry = &(aclp->acl_entry[i]); 360 361 /* 362 * 1.1. If the type is neither ALLOW or DENY - skip. 363 */ 364 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW && 365 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY) 366 continue; 367 368 /* 369 * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip. 370 */ 371 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) 372 continue; 373 374 /* 375 * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT 376 * are set: 377 */ 378 if (entry->ae_flags & 379 (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) { 380 /* 381 * 1.3.1. A copy of the current ACE is made, and placed 382 * in the ACL immediately following the current 383 * ACE. 384 */ 385 copy = _acl_duplicate_entry(aclp, i); 386 387 /* 388 * 1.3.2. In the first ACE, the flag 389 * ACL_ENTRY_INHERIT_ONLY is set. 390 */ 391 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY; 392 393 /* 394 * 1.3.3. In the second ACE, the following flags 395 * are cleared: 396 * ACL_ENTRY_FILE_INHERIT, 397 * ACL_ENTRY_DIRECTORY_INHERIT, 398 * ACL_ENTRY_NO_PROPAGATE_INHERIT. 399 */ 400 copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT | 401 ACL_ENTRY_DIRECTORY_INHERIT | 402 ACL_ENTRY_NO_PROPAGATE_INHERIT); 403 404 /* 405 * The algorithm continues on with the second ACE. 406 */ 407 i++; 408 entry = copy; 409 } 410 411 /* 412 * 1.4. If it's owner@, group@ or everyone@ entry, clear 413 * ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA 414 * and ACL_EXECUTE. Continue to the next entry. 415 */ 416 if (entry->ae_tag == ACL_USER_OBJ || 417 entry->ae_tag == ACL_GROUP_OBJ || 418 entry->ae_tag == ACL_EVERYONE) { 419 entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA | 420 ACL_APPEND_DATA | ACL_EXECUTE); 421 continue; 422 } 423 424 /* 425 * 1.5. Otherwise, if the "who" field did not match one 426 * of OWNER@, GROUP@, EVERYONE@: 427 * 428 * 1.5.1. If the type is ALLOW, check the preceding ACE. 429 * If it does not meet all of the following criteria: 430 */ 431 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW) 432 continue; 433 434 meets = 0; 435 if (i > 0) { 436 meets = 1; 437 previous = &(aclp->acl_entry[i - 1]); 438 439 /* 440 * 1.5.1.1. The type field is DENY, 441 */ 442 if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY) 443 meets = 0; 444 445 /* 446 * 1.5.1.2. The "who" field is the same as the current 447 * ACE, 448 * 449 * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP 450 * is the same as it is in the current ACE, 451 * and no other flag bits are set, 452 */ 453 if (previous->ae_id != entry->ae_id || 454 previous->ae_tag != entry->ae_tag) 455 meets = 0; 456 457 if (previous->ae_flags) 458 meets = 0; 459 460 /* 461 * 1.5.1.4. The mask bits are a subset of the mask bits 462 * of the current ACE, and are also subset of 463 * the following: ACL_READ_DATA, 464 * ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE 465 */ 466 if (previous->ae_perm & ~(entry->ae_perm)) 467 meets = 0; 468 469 if (previous->ae_perm & ~(ACL_READ_DATA | 470 ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE)) 471 meets = 0; 472 } 473 474 if (!meets) { 475 /* 476 * Then the ACE of type DENY, with a who equal 477 * to the current ACE, flag bits equal to 478 * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>) 479 * and no mask bits, is prepended. 480 */ 481 previous = entry; 482 entry = _acl_duplicate_entry(aclp, i); 483 484 /* Adjust counter, as we've just added an entry. */ 485 i++; 486 487 previous->ae_tag = entry->ae_tag; 488 previous->ae_id = entry->ae_id; 489 previous->ae_flags = entry->ae_flags; 490 previous->ae_perm = 0; 491 previous->ae_entry_type = ACL_ENTRY_TYPE_DENY; 492 } 493 494 /* 495 * 1.5.2. The following modifications are made to the prepended 496 * ACE. The intent is to mask the following ACE 497 * to disallow ACL_READ_DATA, ACL_WRITE_DATA, 498 * ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group 499 * permissions of the new mode. As a special case, 500 * if the ACE matches the current owner of the file, 501 * the owner bits are used, rather than the group bits. 502 * This is reflected in the algorithm below. 503 */ 504 amode = mode >> 3; 505 506 /* 507 * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field 508 * in ACE matches the owner of the file, we shift amode three 509 * more bits, in order to have the owner permission bits 510 * placed in the three low order bits of amode. 511 */ 512 if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id) 513 amode = amode >> 3; 514 515 if (entry->ae_perm & ACL_READ_DATA) { 516 if (amode & READ) 517 previous->ae_perm &= ~ACL_READ_DATA; 518 else 519 previous->ae_perm |= ACL_READ_DATA; 520 } 521 522 if (entry->ae_perm & ACL_WRITE_DATA) { 523 if (amode & WRITE) 524 previous->ae_perm &= ~ACL_WRITE_DATA; 525 else 526 previous->ae_perm |= ACL_WRITE_DATA; 527 } 528 529 if (entry->ae_perm & ACL_APPEND_DATA) { 530 if (amode & WRITE) 531 previous->ae_perm &= ~ACL_APPEND_DATA; 532 else 533 previous->ae_perm |= ACL_APPEND_DATA; 534 } 535 536 if (entry->ae_perm & ACL_EXECUTE) { 537 if (amode & EXEC) 538 previous->ae_perm &= ~ACL_EXECUTE; 539 else 540 previous->ae_perm |= ACL_EXECUTE; 541 } 542 543 /* 544 * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags 545 * of the ALLOW ace: 546 * 547 * XXX: This point is not there in the Falkner's draft. 548 */ 549 if (entry->ae_tag == ACL_GROUP && 550 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) { 551 mode_t extramode, ownermode; 552 extramode = (mode >> 3) & 07; 553 ownermode = mode >> 6; 554 extramode &= ~ownermode; 555 556 if (extramode) { 557 if (extramode & READ) { 558 entry->ae_perm &= ~ACL_READ_DATA; 559 previous->ae_perm &= ~ACL_READ_DATA; 560 } 561 562 if (extramode & WRITE) { 563 entry->ae_perm &= 564 ~(ACL_WRITE_DATA | ACL_APPEND_DATA); 565 previous->ae_perm &= 566 ~(ACL_WRITE_DATA | ACL_APPEND_DATA); 567 } 568 569 if (extramode & EXEC) { 570 entry->ae_perm &= ~ACL_EXECUTE; 571 previous->ae_perm &= ~ACL_EXECUTE; 572 } 573 } 574 } 575 } 576 577 /* 578 * 2. If there at least six ACEs, the final six ACEs are examined. 579 * If they are not equal to what we want, append six ACEs. 580 */ 581 must_append = 0; 582 if (aclp->acl_cnt < 6) { 583 must_append = 1; 584 } else { 585 a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]); 586 a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]); 587 a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]); 588 a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]); 589 a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]); 590 a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]); 591 592 if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0, 593 ACL_ENTRY_TYPE_DENY)) 594 must_append = 1; 595 if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL | 596 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES | 597 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW)) 598 must_append = 1; 599 if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0, 600 ACL_ENTRY_TYPE_DENY)) 601 must_append = 1; 602 if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0, 603 ACL_ENTRY_TYPE_ALLOW)) 604 must_append = 1; 605 if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL | 606 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES | 607 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY)) 608 must_append = 1; 609 if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL | 610 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | 611 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW)) 612 must_append = 1; 613 } 614 615 if (must_append) { 616 KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES, 617 ("aclp->acl_cnt <= ACL_MAX_ENTRIES")); 618 619 a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY); 620 a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL | 621 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES | 622 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW); 623 a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY); 624 a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW); 625 a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL | 626 ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES | 627 ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY); 628 a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL | 629 ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | 630 ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW); 631 632 KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL && 633 a5 != NULL && a6 != NULL, ("couldn't append to ACL.")); 634 } 635 636 /* 637 * 3. The final six ACEs are adjusted according to the incoming mode. 638 */ 639 if (mode & S_IRUSR) 640 a2->ae_perm |= ACL_READ_DATA; 641 else 642 a1->ae_perm |= ACL_READ_DATA; 643 if (mode & S_IWUSR) 644 a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA); 645 else 646 a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA); 647 if (mode & S_IXUSR) 648 a2->ae_perm |= ACL_EXECUTE; 649 else 650 a1->ae_perm |= ACL_EXECUTE; 651 652 if (mode & S_IRGRP) 653 a4->ae_perm |= ACL_READ_DATA; 654 else 655 a3->ae_perm |= ACL_READ_DATA; 656 if (mode & S_IWGRP) 657 a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA); 658 else 659 a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA); 660 if (mode & S_IXGRP) 661 a4->ae_perm |= ACL_EXECUTE; 662 else 663 a3->ae_perm |= ACL_EXECUTE; 664 665 if (mode & S_IROTH) 666 a6->ae_perm |= ACL_READ_DATA; 667 else 668 a5->ae_perm |= ACL_READ_DATA; 669 if (mode & S_IWOTH) 670 a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA); 671 else 672 a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA); 673 if (mode & S_IXOTH) 674 a6->ae_perm |= ACL_EXECUTE; 675 else 676 a5->ae_perm |= ACL_EXECUTE; 677 } 678 679 void 680 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp) 681 { 682 int i; 683 mode_t old_mode = *_mode, mode = 0, seen = 0; 684 const struct acl_entry *entry; 685 686 KASSERT(aclp->acl_cnt > 0, ("aclp->acl_cnt > 0")); 687 KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES, 688 ("aclp->acl_cnt <= ACL_MAX_ENTRIES")); 689 690 /* 691 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt 692 * 693 * 3.16.6.1. Recomputing mode upon SETATTR of ACL 694 */ 695 696 for (i = 0; i < aclp->acl_cnt; i++) { 697 entry = &(aclp->acl_entry[i]); 698 699 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW && 700 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY) 701 continue; 702 703 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) 704 continue; 705 706 if (entry->ae_tag == ACL_USER_OBJ) { 707 if ((entry->ae_perm & ACL_READ_DATA) && 708 ((seen & S_IRUSR) == 0)) { 709 seen |= S_IRUSR; 710 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 711 mode |= S_IRUSR; 712 } 713 if ((entry->ae_perm & ACL_WRITE_DATA) && 714 ((seen & S_IWUSR) == 0)) { 715 seen |= S_IWUSR; 716 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 717 mode |= S_IWUSR; 718 } 719 if ((entry->ae_perm & ACL_EXECUTE) && 720 ((seen & S_IXUSR) == 0)) { 721 seen |= S_IXUSR; 722 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 723 mode |= S_IXUSR; 724 } 725 } else if (entry->ae_tag == ACL_GROUP_OBJ) { 726 if ((entry->ae_perm & ACL_READ_DATA) && 727 ((seen & S_IRGRP) == 0)) { 728 seen |= S_IRGRP; 729 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 730 mode |= S_IRGRP; 731 } 732 if ((entry->ae_perm & ACL_WRITE_DATA) && 733 ((seen & S_IWGRP) == 0)) { 734 seen |= S_IWGRP; 735 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 736 mode |= S_IWGRP; 737 } 738 if ((entry->ae_perm & ACL_EXECUTE) && 739 ((seen & S_IXGRP) == 0)) { 740 seen |= S_IXGRP; 741 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 742 mode |= S_IXGRP; 743 } 744 } else if (entry->ae_tag == ACL_EVERYONE) { 745 if (entry->ae_perm & ACL_READ_DATA) { 746 if ((seen & S_IRUSR) == 0) { 747 seen |= S_IRUSR; 748 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 749 mode |= S_IRUSR; 750 } 751 if ((seen & S_IRGRP) == 0) { 752 seen |= S_IRGRP; 753 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 754 mode |= S_IRGRP; 755 } 756 if ((seen & S_IROTH) == 0) { 757 seen |= S_IROTH; 758 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 759 mode |= S_IROTH; 760 } 761 } 762 if (entry->ae_perm & ACL_WRITE_DATA) { 763 if ((seen & S_IWUSR) == 0) { 764 seen |= S_IWUSR; 765 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 766 mode |= S_IWUSR; 767 } 768 if ((seen & S_IWGRP) == 0) { 769 seen |= S_IWGRP; 770 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 771 mode |= S_IWGRP; 772 } 773 if ((seen & S_IWOTH) == 0) { 774 seen |= S_IWOTH; 775 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 776 mode |= S_IWOTH; 777 } 778 } 779 if (entry->ae_perm & ACL_EXECUTE) { 780 if ((seen & S_IXUSR) == 0) { 781 seen |= S_IXUSR; 782 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 783 mode |= S_IXUSR; 784 } 785 if ((seen & S_IXGRP) == 0) { 786 seen |= S_IXGRP; 787 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 788 mode |= S_IXGRP; 789 } 790 if ((seen & S_IXOTH) == 0) { 791 seen |= S_IXOTH; 792 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 793 mode |= S_IXOTH; 794 } 795 } 796 } 797 } 798 799 *_mode = mode | (old_mode & ACL_PRESERVE_MASK); 800 } 801 802 void 803 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp, 804 struct acl *child_aclp, mode_t mode, int file_owner_id, 805 int is_directory) 806 { 807 int i, flags; 808 const struct acl_entry *parent_entry; 809 struct acl_entry *entry, *copy; 810 811 KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0")); 812 KASSERT(parent_aclp->acl_cnt > 0, ("parent_aclp->acl_cnt > 0")); 813 KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES, 814 ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES")); 815 816 /* 817 * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt 818 * 819 * 3.16.6.2. Applying the mode given to CREATE or OPEN 820 * to an inherited ACL 821 */ 822 823 /* 824 * 1. Form an ACL that is the concatenation of all inheritable ACEs. 825 */ 826 for (i = 0; i < parent_aclp->acl_cnt; i++) { 827 parent_entry = &(parent_aclp->acl_entry[i]); 828 flags = parent_entry->ae_flags; 829 830 /* 831 * Entry is not inheritable at all. 832 */ 833 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT | 834 ACL_ENTRY_FILE_INHERIT)) == 0) 835 continue; 836 837 /* 838 * We're creating a file, but entry is not inheritable 839 * by files. 840 */ 841 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0) 842 continue; 843 844 /* 845 * Entry is inheritable only by files, but has NO_PROPAGATE 846 * flag set, and we're creating a directory, so it wouldn't 847 * propagate to any file in that directory anyway. 848 */ 849 if (is_directory && 850 (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 && 851 (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT)) 852 continue; 853 854 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES, 855 ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES")); 856 child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry; 857 child_aclp->acl_cnt++; 858 } 859 860 /* 861 * 2. For each entry in the new ACL, adjust its flags, possibly 862 * creating two entries in place of one. 863 */ 864 for (i = 0; i < child_aclp->acl_cnt; i++) { 865 entry = &(child_aclp->acl_entry[i]); 866 867 /* 868 * This is not in the specification, but SunOS 869 * apparently does that. 870 */ 871 if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) || 872 !is_directory) && 873 entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 874 entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER); 875 876 /* 877 * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object 878 * being created is not a directory, then clear the 879 * following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT, 880 * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT, 881 * ACL_ENTRY_INHERIT_ONLY. 882 */ 883 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT || 884 !is_directory) { 885 entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT | 886 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT | 887 ACL_ENTRY_INHERIT_ONLY); 888 889 /* 890 * Continue on to the next ACE. 891 */ 892 continue; 893 } 894 895 /* 896 * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT 897 * is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure 898 * that ACL_ENTRY_INHERIT_ONLY is set. Continue to the 899 * next ACE. Otherwise... 900 */ 901 /* 902 * XXX: Read it again and make sure what does the "otherwise" 903 * apply to. 904 */ 905 if (is_directory && 906 (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) && 907 ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) { 908 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY; 909 continue; 910 } 911 912 /* 913 * 2.C. If the type of the ACE is neither ALLOW nor deny, 914 * then continue. 915 */ 916 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW && 917 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY) 918 continue; 919 920 /* 921 * 2.D. Copy the original ACE into a second, adjacent ACE. 922 */ 923 copy = _acl_duplicate_entry(child_aclp, i); 924 925 /* 926 * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY 927 * is set. 928 */ 929 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY; 930 931 /* 932 * 2.F. On the second ACE, clear the following flags: 933 * ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT, 934 * ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY. 935 */ 936 copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT | 937 ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT | 938 ACL_ENTRY_INHERIT_ONLY); 939 940 /* 941 * 2.G. On the second ACE, if the type is ALLOW, 942 * an implementation MAY clear the following 943 * mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER. 944 */ 945 if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) 946 copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER); 947 948 /* 949 * Increment the counter to skip the copied entry. 950 */ 951 i++; 952 } 953 954 /* 955 * 3. To ensure that the mode is honored, apply the algorithm describe 956 * in Section 2.16.6.3, using the mode that is to be used for file 957 * creation. 958 */ 959 acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id); 960 } 961 962 #ifdef _KERNEL 963 static int 964 _acls_are_equal(const struct acl *a, const struct acl *b) 965 { 966 int i; 967 const struct acl_entry *entrya, *entryb; 968 969 if (a->acl_cnt != b->acl_cnt) 970 return (0); 971 972 for (i = 0; i < b->acl_cnt; i++) { 973 entrya = &(a->acl_entry[i]); 974 entryb = &(b->acl_entry[i]); 975 976 if (entrya->ae_tag != entryb->ae_tag || 977 entrya->ae_id != entryb->ae_id || 978 entrya->ae_perm != entryb->ae_perm || 979 entrya->ae_entry_type != entryb->ae_entry_type || 980 entrya->ae_flags != entryb->ae_flags) 981 return (0); 982 } 983 984 return (1); 985 } 986 987 /* 988 * This routine is used to determine whether to remove extended attribute 989 * that stores ACL contents. 990 */ 991 int 992 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id) 993 { 994 int trivial; 995 mode_t tmpmode = 0; 996 struct acl *tmpaclp; 997 998 if (aclp->acl_cnt != 6) 999 return (0); 1000 1001 /* 1002 * Compute the mode from the ACL, then compute new ACL from that mode. 1003 * If the ACLs are identical, then the ACL is trivial. 1004 * 1005 * XXX: I guess there is a faster way to do this. However, even 1006 * this slow implementation significantly speeds things up 1007 * for files that don't have non-trivial ACLs - it's critical 1008 * for performance to not use EA when they are not needed. 1009 */ 1010 tmpaclp = acl_alloc(M_WAITOK | M_ZERO); 1011 acl_nfs4_sync_mode_from_acl(&tmpmode, aclp); 1012 acl_nfs4_sync_acl_from_mode(tmpaclp, tmpmode, file_owner_id); 1013 trivial = _acls_are_equal(aclp, tmpaclp); 1014 acl_free(tmpaclp); 1015 1016 return (trivial); 1017 } 1018 #endif /* _KERNEL */ 1019 1020 int 1021 acl_nfs4_check(const struct acl *aclp, int is_directory) 1022 { 1023 int i; 1024 const struct acl_entry *entry; 1025 1026 /* 1027 * The spec doesn't seem to say anything about ACL validity. 1028 * It seems there is not much to do here. There is even no need 1029 * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE) 1030 * entries, as there can be several of them and that's perfectly 1031 * valid. There can be none of them too. Really. 1032 */ 1033 1034 if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0) 1035 return (EINVAL); 1036 1037 for (i = 0; i < aclp->acl_cnt; i++) { 1038 entry = &(aclp->acl_entry[i]); 1039 1040 switch (entry->ae_tag) { 1041 case ACL_USER_OBJ: 1042 case ACL_GROUP_OBJ: 1043 case ACL_EVERYONE: 1044 if (entry->ae_id != ACL_UNDEFINED_ID) 1045 return (EINVAL); 1046 break; 1047 1048 case ACL_USER: 1049 case ACL_GROUP: 1050 if (entry->ae_id == ACL_UNDEFINED_ID) 1051 return (EINVAL); 1052 break; 1053 1054 default: 1055 return (EINVAL); 1056 } 1057 1058 if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS) 1059 return (EINVAL); 1060 1061 /* 1062 * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now. 1063 */ 1064 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW && 1065 entry->ae_entry_type != ACL_ENTRY_TYPE_DENY) 1066 return (EINVAL); 1067 1068 if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS) 1069 return (EINVAL); 1070 1071 /* Disallow unimplemented flags. */ 1072 if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS | 1073 ACL_ENTRY_FAILED_ACCESS)) 1074 return (EINVAL); 1075 1076 /* Disallow flags not allowed for ordinary files. */ 1077 if (!is_directory) { 1078 if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT | 1079 ACL_ENTRY_DIRECTORY_INHERIT | 1080 ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY)) 1081 return (EINVAL); 1082 } 1083 } 1084 1085 return (0); 1086 } 1087