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