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