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