1 /*- 2 * Copyright (c) 1999-2006 Robert N. M. Watson 3 * All rights reserved. 4 * 5 * This software was developed by Robert Watson for the TrustedBSD Project. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 /* 29 * Developed by the TrustedBSD Project. 30 * 31 * ACL support routines specific to POSIX.1e access control lists. These are 32 * utility routines for code common across file systems implementing POSIX.1e 33 * ACLs. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/mount.h> 42 #include <sys/vnode.h> 43 #include <sys/errno.h> 44 #include <sys/stat.h> 45 #include <sys/acl.h> 46 47 /* 48 * Implement a version of vaccess() that understands POSIX.1e ACL semantics; 49 * the access ACL has already been prepared for evaluation by the file 50 * system and is passed via 'uid', 'gid', and 'acl'. Return 0 on success, 51 * else an errno value. 52 */ 53 int 54 vaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid, 55 struct acl *acl, mode_t acc_mode, struct ucred *cred, int *privused) 56 { 57 struct acl_entry *acl_other, *acl_mask; 58 mode_t dac_granted; 59 mode_t cap_granted; 60 mode_t acl_mask_granted; 61 int group_matched, i; 62 63 /* 64 * Look for a normal, non-privileged way to access the file/directory 65 * as requested. If it exists, go with that. Otherwise, attempt to 66 * use privileges granted via cap_granted. In some cases, which 67 * privileges to use may be ambiguous due to "best match", in which 68 * case fall back on first match for the time being. 69 */ 70 if (privused != NULL) 71 *privused = 0; 72 73 /* 74 * Determine privileges now, but don't apply until we've found a DAC 75 * entry that matches but has failed to allow access. POSIX.1e 76 * capabilities are not implemented, but we document how they would 77 * behave here if implemented. 78 */ 79 #ifndef CAPABILITIES 80 if (suser_cred(cred, SUSER_ALLOWJAIL) == 0) 81 cap_granted = VALLPERM; 82 else 83 cap_granted = 0; 84 #else 85 cap_granted = 0; 86 87 if (type == VDIR) { 88 if ((acc_mode & VEXEC) && !cap_check(cred, NULL, 89 CAP_DAC_READ_SEARCH, SUSER_ALLOWJAIL)) 90 cap_granted |= VEXEC; 91 } else { 92 if ((acc_mode & VEXEC) && !cap_check(cred, NULL, 93 CAP_DAC_EXECUTE, SUSER_ALLOWJAIL)) 94 cap_granted |= VEXEC; 95 } 96 97 if ((acc_mode & VREAD) && !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, 98 SUSER_ALLOWJAIL)) 99 cap_granted |= VREAD; 100 101 if (((acc_mode & VWRITE) || (acc_mode & VAPPEND)) && 102 !cap_check(cred, NULL, CAP_DAC_WRITE, SUSER_ALLOWJAIL)) 103 cap_granted |= (VWRITE | VAPPEND); 104 105 if ((acc_mode & VADMIN) && !cap_check(cred, NULL, CAP_FOWNER, 106 SUSER_ALLOWJAIL)) 107 cap_granted |= VADMIN; 108 #endif /* CAPABILITIES */ 109 110 /* 111 * The owner matches if the effective uid associated with the 112 * credential matches that of the ACL_USER_OBJ entry. While we're 113 * doing the first scan, also cache the location of the ACL_MASK and 114 * ACL_OTHER entries, preventing some future iterations. 115 */ 116 acl_mask = acl_other = NULL; 117 for (i = 0; i < acl->acl_cnt; i++) { 118 switch (acl->acl_entry[i].ae_tag) { 119 case ACL_USER_OBJ: 120 if (file_uid != cred->cr_uid) 121 break; 122 dac_granted = 0; 123 dac_granted |= VADMIN; 124 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 125 dac_granted |= VEXEC; 126 if (acl->acl_entry[i].ae_perm & ACL_READ) 127 dac_granted |= VREAD; 128 if (acl->acl_entry[i].ae_perm & ACL_WRITE) 129 dac_granted |= (VWRITE | VAPPEND); 130 if ((acc_mode & dac_granted) == acc_mode) 131 return (0); 132 if ((acc_mode & (dac_granted | cap_granted)) == 133 acc_mode) { 134 if (privused != NULL) 135 *privused = 1; 136 return (0); 137 } 138 goto error; 139 140 case ACL_MASK: 141 acl_mask = &acl->acl_entry[i]; 142 break; 143 144 case ACL_OTHER: 145 acl_other = &acl->acl_entry[i]; 146 break; 147 148 default: 149 break; 150 } 151 } 152 153 /* 154 * An ACL_OTHER entry should always exist in a valid access ACL. If 155 * it doesn't, then generate a serious failure. For now, this means 156 * a debugging message and EPERM, but in the future should probably 157 * be a panic. 158 */ 159 if (acl_other == NULL) { 160 /* 161 * XXX This should never happen 162 */ 163 printf("vaccess_acl_posix1e: ACL_OTHER missing\n"); 164 return (EPERM); 165 } 166 167 /* 168 * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields are 169 * masked by an ACL_MASK entry, if any. As such, first identify the 170 * ACL_MASK field, then iterate through identifying potential user 171 * matches, then group matches. If there is no ACL_MASK, assume that 172 * the mask allows all requests to succeed. 173 */ 174 if (acl_mask != NULL) { 175 acl_mask_granted = 0; 176 if (acl_mask->ae_perm & ACL_EXECUTE) 177 acl_mask_granted |= VEXEC; 178 if (acl_mask->ae_perm & ACL_READ) 179 acl_mask_granted |= VREAD; 180 if (acl_mask->ae_perm & ACL_WRITE) 181 acl_mask_granted |= (VWRITE | VAPPEND); 182 } else 183 acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND; 184 185 /* 186 * Iterate through user ACL entries. Do checks twice, first without 187 * privilege, and then if a match is found but failed, a second time 188 * with privilege. 189 */ 190 191 /* 192 * Check ACL_USER ACL entries. 193 */ 194 for (i = 0; i < acl->acl_cnt; i++) { 195 switch (acl->acl_entry[i].ae_tag) { 196 case ACL_USER: 197 if (acl->acl_entry[i].ae_id != cred->cr_uid) 198 break; 199 dac_granted = 0; 200 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 201 dac_granted |= VEXEC; 202 if (acl->acl_entry[i].ae_perm & ACL_READ) 203 dac_granted |= VREAD; 204 if (acl->acl_entry[i].ae_perm & ACL_WRITE) 205 dac_granted |= (VWRITE | VAPPEND); 206 dac_granted &= acl_mask_granted; 207 if ((acc_mode & dac_granted) == acc_mode) 208 return (0); 209 if ((acc_mode & (dac_granted | cap_granted)) != 210 acc_mode) 211 goto error; 212 213 if (privused != NULL) 214 *privused = 1; 215 return (0); 216 } 217 } 218 219 /* 220 * Group match is best-match, not first-match, so find a "best" 221 * match. Iterate across, testing each potential group match. Make 222 * sure we keep track of whether we found a match or not, so that we 223 * know if we should try again with any available privilege, or if we 224 * should move on to ACL_OTHER. 225 */ 226 group_matched = 0; 227 for (i = 0; i < acl->acl_cnt; i++) { 228 switch (acl->acl_entry[i].ae_tag) { 229 case ACL_GROUP_OBJ: 230 if (!groupmember(file_gid, cred)) 231 break; 232 dac_granted = 0; 233 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 234 dac_granted |= VEXEC; 235 if (acl->acl_entry[i].ae_perm & ACL_READ) 236 dac_granted |= VREAD; 237 if (acl->acl_entry[i].ae_perm & ACL_WRITE) 238 dac_granted |= (VWRITE | VAPPEND); 239 dac_granted &= acl_mask_granted; 240 241 if ((acc_mode & dac_granted) == acc_mode) 242 return (0); 243 244 group_matched = 1; 245 break; 246 247 case ACL_GROUP: 248 if (!groupmember(acl->acl_entry[i].ae_id, cred)) 249 break; 250 dac_granted = 0; 251 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 252 dac_granted |= VEXEC; 253 if (acl->acl_entry[i].ae_perm & ACL_READ) 254 dac_granted |= VREAD; 255 if (acl->acl_entry[i].ae_perm & ACL_WRITE) 256 dac_granted |= (VWRITE | VAPPEND); 257 dac_granted &= acl_mask_granted; 258 259 if ((acc_mode & dac_granted) == acc_mode) 260 return (0); 261 262 group_matched = 1; 263 break; 264 265 default: 266 break; 267 } 268 } 269 270 if (group_matched == 1) { 271 /* 272 * There was a match, but it did not grant rights via pure 273 * DAC. Try again, this time with privilege. 274 */ 275 for (i = 0; i < acl->acl_cnt; i++) { 276 switch (acl->acl_entry[i].ae_tag) { 277 case ACL_GROUP_OBJ: 278 if (!groupmember(file_gid, cred)) 279 break; 280 dac_granted = 0; 281 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 282 dac_granted |= VEXEC; 283 if (acl->acl_entry[i].ae_perm & ACL_READ) 284 dac_granted |= VREAD; 285 if (acl->acl_entry[i].ae_perm & ACL_WRITE) 286 dac_granted |= (VWRITE | VAPPEND); 287 dac_granted &= acl_mask_granted; 288 289 if ((acc_mode & (dac_granted | cap_granted)) != 290 acc_mode) 291 break; 292 293 if (privused != NULL) 294 *privused = 1; 295 return (0); 296 297 case ACL_GROUP: 298 if (!groupmember(acl->acl_entry[i].ae_id, 299 cred)) 300 break; 301 dac_granted = 0; 302 if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 303 dac_granted |= VEXEC; 304 if (acl->acl_entry[i].ae_perm & ACL_READ) 305 dac_granted |= VREAD; 306 if (acl->acl_entry[i].ae_perm & ACL_WRITE) 307 dac_granted |= (VWRITE | VAPPEND); 308 dac_granted &= acl_mask_granted; 309 310 if ((acc_mode & (dac_granted | cap_granted)) != 311 acc_mode) 312 break; 313 314 if (privused != NULL) 315 *privused = 1; 316 return (0); 317 318 default: 319 break; 320 } 321 } 322 /* 323 * Even with privilege, group membership was not sufficient. 324 * Return failure. 325 */ 326 goto error; 327 } 328 329 /* 330 * Fall back on ACL_OTHER. ACL_MASK is not applied to ACL_OTHER. 331 */ 332 dac_granted = 0; 333 if (acl_other->ae_perm & ACL_EXECUTE) 334 dac_granted |= VEXEC; 335 if (acl_other->ae_perm & ACL_READ) 336 dac_granted |= VREAD; 337 if (acl_other->ae_perm & ACL_WRITE) 338 dac_granted |= (VWRITE | VAPPEND); 339 340 if ((acc_mode & dac_granted) == acc_mode) 341 return (0); 342 if ((acc_mode & (dac_granted | cap_granted)) == acc_mode) { 343 if (privused != NULL) 344 *privused = 1; 345 return (0); 346 } 347 348 error: 349 return ((acc_mode & VADMIN) ? EPERM : EACCES); 350 } 351 352 /* 353 * For the purposes of filesystems maintaining the _OBJ entries in an inode 354 * with a mode_t field, this routine converts a mode_t entry to an 355 * acl_perm_t. 356 */ 357 acl_perm_t 358 acl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode) 359 { 360 acl_perm_t perm = 0; 361 362 switch(tag) { 363 case ACL_USER_OBJ: 364 if (mode & S_IXUSR) 365 perm |= ACL_EXECUTE; 366 if (mode & S_IRUSR) 367 perm |= ACL_READ; 368 if (mode & S_IWUSR) 369 perm |= ACL_WRITE; 370 return (perm); 371 372 case ACL_GROUP_OBJ: 373 if (mode & S_IXGRP) 374 perm |= ACL_EXECUTE; 375 if (mode & S_IRGRP) 376 perm |= ACL_READ; 377 if (mode & S_IWGRP) 378 perm |= ACL_WRITE; 379 return (perm); 380 381 case ACL_OTHER: 382 if (mode & S_IXOTH) 383 perm |= ACL_EXECUTE; 384 if (mode & S_IROTH) 385 perm |= ACL_READ; 386 if (mode & S_IWOTH) 387 perm |= ACL_WRITE; 388 return (perm); 389 390 default: 391 printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag); 392 return (0); 393 } 394 } 395 396 /* 397 * Given inode information (uid, gid, mode), return an acl entry of the 398 * appropriate type. 399 */ 400 struct acl_entry 401 acl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode) 402 { 403 struct acl_entry acl_entry; 404 405 acl_entry.ae_tag = tag; 406 acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode); 407 switch(tag) { 408 case ACL_USER_OBJ: 409 acl_entry.ae_id = uid; 410 break; 411 412 case ACL_GROUP_OBJ: 413 acl_entry.ae_id = gid; 414 break; 415 416 case ACL_OTHER: 417 acl_entry.ae_id = ACL_UNDEFINED_ID; 418 break; 419 420 default: 421 acl_entry.ae_id = ACL_UNDEFINED_ID; 422 printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag); 423 } 424 425 return (acl_entry); 426 } 427 428 /* 429 * Utility function to generate a file mode given appropriate ACL entries. 430 */ 431 mode_t 432 acl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry, 433 struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry) 434 { 435 mode_t mode; 436 437 mode = 0; 438 if (acl_user_obj_entry->ae_perm & ACL_EXECUTE) 439 mode |= S_IXUSR; 440 if (acl_user_obj_entry->ae_perm & ACL_READ) 441 mode |= S_IRUSR; 442 if (acl_user_obj_entry->ae_perm & ACL_WRITE) 443 mode |= S_IWUSR; 444 if (acl_group_obj_entry->ae_perm & ACL_EXECUTE) 445 mode |= S_IXGRP; 446 if (acl_group_obj_entry->ae_perm & ACL_READ) 447 mode |= S_IRGRP; 448 if (acl_group_obj_entry->ae_perm & ACL_WRITE) 449 mode |= S_IWGRP; 450 if (acl_other_entry->ae_perm & ACL_EXECUTE) 451 mode |= S_IXOTH; 452 if (acl_other_entry->ae_perm & ACL_READ) 453 mode |= S_IROTH; 454 if (acl_other_entry->ae_perm & ACL_WRITE) 455 mode |= S_IWOTH; 456 457 return (mode); 458 } 459 460 /* 461 * Utility function to generate a file mode given a complete POSIX.1e access 462 * ACL. Note that if the ACL is improperly formed, this may result in a 463 * panic. 464 */ 465 mode_t 466 acl_posix1e_acl_to_mode(struct acl *acl) 467 { 468 struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other; 469 int i; 470 471 /* 472 * Find the ACL entries relevant to a POSIX permission mode. 473 */ 474 acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL; 475 for (i = 0; i < acl->acl_cnt; i++) { 476 switch (acl->acl_entry[i].ae_tag) { 477 case ACL_USER_OBJ: 478 acl_user_obj = &acl->acl_entry[i]; 479 break; 480 481 case ACL_GROUP_OBJ: 482 acl_group_obj = &acl->acl_entry[i]; 483 break; 484 485 case ACL_OTHER: 486 acl_other = &acl->acl_entry[i]; 487 break; 488 489 case ACL_MASK: 490 acl_mask = &acl->acl_entry[i]; 491 break; 492 493 case ACL_USER: 494 case ACL_GROUP: 495 break; 496 497 default: 498 panic("acl_posix1e_acl_to_mode: bad ae_tag"); 499 } 500 } 501 502 if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL) 503 panic("acl_posix1e_acl_to_mode: missing base ae_tags"); 504 505 /* 506 * POSIX.1e specifies that if there is an ACL_MASK entry, we replace 507 * the mode "group" bits with its permissions. If there isn't, we 508 * use the ACL_GROUP_OBJ permissions. 509 */ 510 if (acl_mask != NULL) 511 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask, 512 acl_other)); 513 else 514 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj, 515 acl_other)); 516 } 517 518 /* 519 * Perform a syntactic check of the ACL, sufficient to allow an implementing 520 * filesystem to determine if it should accept this and rely on the POSIX.1e 521 * ACL properties. 522 */ 523 int 524 acl_posix1e_check(struct acl *acl) 525 { 526 int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group; 527 int num_acl_mask, num_acl_other, i; 528 529 /* 530 * Verify that the number of entries does not exceed the maximum 531 * defined for acl_t. 532 * 533 * Verify that the correct number of various sorts of ae_tags are 534 * present: 535 * Exactly one ACL_USER_OBJ 536 * Exactly one ACL_GROUP_OBJ 537 * Exactly one ACL_OTHER 538 * If any ACL_USER or ACL_GROUP entries appear, then exactly one 539 * ACL_MASK entry must also appear. 540 * 541 * Verify that all ae_perm entries are in ACL_PERM_BITS. 542 * 543 * Verify all ae_tag entries are understood by this implementation. 544 * 545 * Note: Does not check for uniqueness of qualifier (ae_id) field. 546 */ 547 num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group = 548 num_acl_mask = num_acl_other = 0; 549 if (acl->acl_cnt > ACL_MAX_ENTRIES || acl->acl_cnt < 0) 550 return (EINVAL); 551 for (i = 0; i < acl->acl_cnt; i++) { 552 /* 553 * Check for a valid tag. 554 */ 555 switch(acl->acl_entry[i].ae_tag) { 556 case ACL_USER_OBJ: 557 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 558 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 559 return (EINVAL); 560 num_acl_user_obj++; 561 break; 562 case ACL_GROUP_OBJ: 563 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 564 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 565 return (EINVAL); 566 num_acl_group_obj++; 567 break; 568 case ACL_USER: 569 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID) 570 return (EINVAL); 571 num_acl_user++; 572 break; 573 case ACL_GROUP: 574 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID) 575 return (EINVAL); 576 num_acl_group++; 577 break; 578 case ACL_OTHER: 579 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 580 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 581 return (EINVAL); 582 num_acl_other++; 583 break; 584 case ACL_MASK: 585 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 586 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 587 return (EINVAL); 588 num_acl_mask++; 589 break; 590 default: 591 return (EINVAL); 592 } 593 /* 594 * Check for valid perm entries. 595 */ 596 if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) != 597 ACL_PERM_BITS) 598 return (EINVAL); 599 } 600 if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) || 601 (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1)) 602 return (EINVAL); 603 if (((num_acl_group != 0) || (num_acl_user != 0)) && 604 (num_acl_mask != 1)) 605 return (EINVAL); 606 return (0); 607 } 608 609 /* 610 * Given a requested mode for a new object, and a default ACL, combine the 611 * two to produce a new mode. Be careful not to clear any bits that aren't 612 * intended to be affected by the POSIX.1e ACL. Eventually, this might also 613 * take the cmask as an argument, if we push that down into 614 * per-filesystem-code. 615 */ 616 mode_t 617 acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl) 618 { 619 mode_t mode; 620 621 mode = cmode; 622 /* 623 * The current composition policy is that a permission bit must be 624 * set in *both* the ACL and the requested creation mode for it to 625 * appear in the resulting mode/ACL. First clear any possibly 626 * effected bits, then reconstruct. 627 */ 628 mode &= ACL_PRESERVE_MASK; 629 mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl)); 630 631 return (mode); 632 } 633