subr_acl_posix1e.c revision 212002
1218585Sjkim/*- 2218585Sjkim * Copyright (c) 1999-2006 Robert N. M. Watson 3218585Sjkim * All rights reserved. 4218585Sjkim * 5218585Sjkim * This software was developed by Robert Watson for the TrustedBSD Project. 6218585Sjkim * 7218585Sjkim * Redistribution and use in source and binary forms, with or without 8316303Sjkim * modification, are permitted provided that the following conditions 9316303Sjkim * are met: 10316303Sjkim * 1. Redistributions of source code must retain the above copyright 11316303Sjkim * notice, this list of conditions and the following disclaimer. 12316303Sjkim * 2. Redistributions in binary form must reproduce the above copyright 13218585Sjkim * notice, this list of conditions and the following disclaimer in the 14218585Sjkim * documentation and/or other materials provided with the distribution. 15316303Sjkim * 16316303Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17316303Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18316303Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19316303Sjkim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20316303Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21316303Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22316303Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23316303Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24316303Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25316303Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26316303Sjkim * SUCH DAMAGE. 27316303Sjkim */ 28316303Sjkim/* 29316303Sjkim * Developed by the TrustedBSD Project. 30316303Sjkim * 31316303Sjkim * ACL support routines specific to POSIX.1e access control lists. These are 32316303Sjkim * utility routines for code common across file systems implementing POSIX.1e 33316303Sjkim * ACLs. 34316303Sjkim */ 35316303Sjkim 36316303Sjkim#include <sys/cdefs.h> 37316303Sjkim__FBSDID("$FreeBSD: head/sys/kern/subr_acl_posix1e.c 212002 2010-08-30 16:30:18Z jh $"); 38316303Sjkim 39316303Sjkim#include <sys/param.h> 40316303Sjkim#include <sys/systm.h> 41316303Sjkim#include <sys/mount.h> 42316303Sjkim#include <sys/priv.h> 43316303Sjkim#include <sys/vnode.h> 44316303Sjkim#include <sys/errno.h> 45316303Sjkim#include <sys/stat.h> 46316303Sjkim#include <sys/acl.h> 47316303Sjkim 48316303Sjkim/* 49316303Sjkim * Implement a version of vaccess() that understands POSIX.1e ACL semantics; 50316303Sjkim * the access ACL has already been prepared for evaluation by the file system 51316303Sjkim * and is passed via 'uid', 'gid', and 'acl'. Return 0 on success, else an 52316303Sjkim * errno value. 53316303Sjkim */ 54316303Sjkimint 55316303Sjkimvaccess_acl_posix1e(enum vtype type, uid_t file_uid, gid_t file_gid, 56316303Sjkim struct acl *acl, accmode_t accmode, struct ucred *cred, int *privused) 57316303Sjkim{ 58316303Sjkim struct acl_entry *acl_other, *acl_mask; 59316303Sjkim accmode_t dac_granted; 60316303Sjkim accmode_t priv_granted; 61316303Sjkim accmode_t acl_mask_granted; 62316303Sjkim int group_matched, i; 63316303Sjkim 64316303Sjkim KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0, 65316303Sjkim ("invalid bit in accmode")); 66316303Sjkim KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE), 67316303Sjkim ("VAPPEND without VWRITE")); 68316303Sjkim 69316303Sjkim /* 70316303Sjkim * Look for a normal, non-privileged way to access the file/directory 71316303Sjkim * as requested. If it exists, go with that. Otherwise, attempt to 72316303Sjkim * use privileges granted via priv_granted. In some cases, which 73316303Sjkim * privileges to use may be ambiguous due to "best match", in which 74316303Sjkim * case fall back on first match for the time being. 75316303Sjkim */ 76316303Sjkim if (privused != NULL) 77316303Sjkim *privused = 0; 78316303Sjkim 79316303Sjkim /* 80316303Sjkim * Determine privileges now, but don't apply until we've found a DAC 81316303Sjkim * entry that matches but has failed to allow access. 82316303Sjkim * 83316303Sjkim * XXXRW: Ideally, we'd determine the privileges required before 84316303Sjkim * asking for them. 85316303Sjkim */ 86316303Sjkim priv_granted = 0; 87316303Sjkim 88316303Sjkim if (type == VDIR) { 89316303Sjkim if ((accmode & VEXEC) && !priv_check_cred(cred, 90316303Sjkim PRIV_VFS_LOOKUP, 0)) 91316303Sjkim priv_granted |= VEXEC; 92316303Sjkim } else { 93316303Sjkim /* 94316303Sjkim * Ensure that at least one execute bit is on. Otherwise, 95316303Sjkim * a privileged user will always succeed, and we don't want 96316303Sjkim * this to happen unless the file really is executable. 97316303Sjkim */ 98316303Sjkim if ((accmode & VEXEC) && (acl_posix1e_acl_to_mode(acl) & 99316303Sjkim (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 && 100316303Sjkim !priv_check_cred(cred, PRIV_VFS_EXEC, 0)) 101316303Sjkim priv_granted |= VEXEC; 102316303Sjkim } 103316303Sjkim 104316303Sjkim if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ, 0)) 105316303Sjkim priv_granted |= VREAD; 106316303Sjkim 107316303Sjkim if (((accmode & VWRITE) || (accmode & VAPPEND)) && 108316303Sjkim !priv_check_cred(cred, PRIV_VFS_WRITE, 0)) 109316303Sjkim priv_granted |= (VWRITE | VAPPEND); 110316303Sjkim 111316303Sjkim if ((accmode & VADMIN) && !priv_check_cred(cred, PRIV_VFS_ADMIN, 0)) 112316303Sjkim priv_granted |= VADMIN; 113316303Sjkim 114316303Sjkim /* 115316303Sjkim * The owner matches if the effective uid associated with the 116316303Sjkim * credential matches that of the ACL_USER_OBJ entry. While we're 117316303Sjkim * doing the first scan, also cache the location of the ACL_MASK and 118316303Sjkim * ACL_OTHER entries, preventing some future iterations. 119316303Sjkim */ 120218585Sjkim acl_mask = acl_other = NULL; 121218585Sjkim for (i = 0; i < acl->acl_cnt; i++) { 122218585Sjkim switch (acl->acl_entry[i].ae_tag) { 123218585Sjkim case ACL_USER_OBJ: 124218585Sjkim if (file_uid != cred->cr_uid) 125218585Sjkim break; 126218585Sjkim dac_granted = 0; 127218585Sjkim dac_granted |= VADMIN; 128218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 129218585Sjkim dac_granted |= VEXEC; 130218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_READ) 131218585Sjkim dac_granted |= VREAD; 132218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_WRITE) 133218585Sjkim dac_granted |= (VWRITE | VAPPEND); 134218585Sjkim if ((accmode & dac_granted) == accmode) 135316303Sjkim return (0); 136316303Sjkim 137316303Sjkim /* 138316303Sjkim * XXXRW: Do privilege lookup here. 139316303Sjkim */ 140316303Sjkim if ((accmode & (dac_granted | priv_granted)) == 141316303Sjkim accmode) { 142316303Sjkim if (privused != NULL) 143316303Sjkim *privused = 1; 144316303Sjkim return (0); 145316303Sjkim } 146316303Sjkim goto error; 147316303Sjkim 148218585Sjkim case ACL_MASK: 149218585Sjkim acl_mask = &acl->acl_entry[i]; 150218585Sjkim break; 151316303Sjkim 152218585Sjkim case ACL_OTHER: 153218590Sjkim acl_other = &acl->acl_entry[i]; 154218590Sjkim break; 155218590Sjkim 156218590Sjkim default: 157218590Sjkim break; 158298714Sjkim } 159218585Sjkim } 160218585Sjkim 161218585Sjkim /* 162218585Sjkim * An ACL_OTHER entry should always exist in a valid access ACL. If 163218585Sjkim * it doesn't, then generate a serious failure. For now, this means 164218585Sjkim * a debugging message and EPERM, but in the future should probably 165218585Sjkim * be a panic. 166218585Sjkim */ 167218585Sjkim if (acl_other == NULL) { 168218585Sjkim /* 169218585Sjkim * XXX This should never happen 170218585Sjkim */ 171218585Sjkim printf("vaccess_acl_posix1e: ACL_OTHER missing\n"); 172218585Sjkim return (EPERM); 173218585Sjkim } 174218585Sjkim 175218585Sjkim /* 176218585Sjkim * Checks against ACL_USER, ACL_GROUP_OBJ, and ACL_GROUP fields are 177218585Sjkim * masked by an ACL_MASK entry, if any. As such, first identify the 178218585Sjkim * ACL_MASK field, then iterate through identifying potential user 179218585Sjkim * matches, then group matches. If there is no ACL_MASK, assume that 180218585Sjkim * the mask allows all requests to succeed. 181218585Sjkim */ 182218585Sjkim if (acl_mask != NULL) { 183218585Sjkim acl_mask_granted = 0; 184218585Sjkim if (acl_mask->ae_perm & ACL_EXECUTE) 185218585Sjkim acl_mask_granted |= VEXEC; 186218585Sjkim if (acl_mask->ae_perm & ACL_READ) 187218585Sjkim acl_mask_granted |= VREAD; 188218585Sjkim if (acl_mask->ae_perm & ACL_WRITE) 189218585Sjkim acl_mask_granted |= (VWRITE | VAPPEND); 190218585Sjkim } else 191218585Sjkim acl_mask_granted = VEXEC | VREAD | VWRITE | VAPPEND; 192218585Sjkim 193218585Sjkim /* 194218585Sjkim * Check ACL_USER ACL entries. There will either be one or no 195218585Sjkim * matches; if there is one, we accept or rejected based on the 196218585Sjkim * match; otherwise, we continue on to groups. 197218585Sjkim */ 198218585Sjkim for (i = 0; i < acl->acl_cnt; i++) { 199218585Sjkim switch (acl->acl_entry[i].ae_tag) { 200218585Sjkim case ACL_USER: 201218585Sjkim if (acl->acl_entry[i].ae_id != cred->cr_uid) 202218585Sjkim break; 203218585Sjkim dac_granted = 0; 204218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 205218585Sjkim dac_granted |= VEXEC; 206218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_READ) 207218585Sjkim dac_granted |= VREAD; 208218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_WRITE) 209218585Sjkim dac_granted |= (VWRITE | VAPPEND); 210218585Sjkim dac_granted &= acl_mask_granted; 211218585Sjkim if ((accmode & dac_granted) == accmode) 212218585Sjkim return (0); 213218585Sjkim /* 214218585Sjkim * XXXRW: Do privilege lookup here. 215218585Sjkim */ 216218585Sjkim if ((accmode & (dac_granted | priv_granted)) != 217218585Sjkim accmode) 218218585Sjkim goto error; 219218585Sjkim 220218585Sjkim if (privused != NULL) 221218585Sjkim *privused = 1; 222218585Sjkim return (0); 223218585Sjkim } 224218585Sjkim } 225218585Sjkim 226218585Sjkim /* 227218585Sjkim * Group match is best-match, not first-match, so find a "best" 228218585Sjkim * match. Iterate across, testing each potential group match. Make 229218585Sjkim * sure we keep track of whether we found a match or not, so that we 230218585Sjkim * know if we should try again with any available privilege, or if we 231298714Sjkim * should move on to ACL_OTHER. 232298714Sjkim */ 233298714Sjkim group_matched = 0; 234298714Sjkim for (i = 0; i < acl->acl_cnt; i++) { 235298714Sjkim switch (acl->acl_entry[i].ae_tag) { 236298714Sjkim case ACL_GROUP_OBJ: 237327557Sjkim if (!groupmember(file_gid, cred)) 238327557Sjkim break; 239218585Sjkim dac_granted = 0; 240218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 241218585Sjkim dac_granted |= VEXEC; 242218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_READ) 243218585Sjkim dac_granted |= VREAD; 244218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_WRITE) 245218585Sjkim dac_granted |= (VWRITE | VAPPEND); 246218585Sjkim dac_granted &= acl_mask_granted; 247218585Sjkim 248218585Sjkim if ((accmode & dac_granted) == accmode) 249218585Sjkim return (0); 250218585Sjkim 251218585Sjkim group_matched = 1; 252218585Sjkim break; 253218585Sjkim 254218585Sjkim case ACL_GROUP: 255218585Sjkim if (!groupmember(acl->acl_entry[i].ae_id, cred)) 256218585Sjkim break; 257218585Sjkim dac_granted = 0; 258218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 259218585Sjkim dac_granted |= VEXEC; 260218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_READ) 261218585Sjkim dac_granted |= VREAD; 262250838Sjkim if (acl->acl_entry[i].ae_perm & ACL_WRITE) 263218585Sjkim dac_granted |= (VWRITE | VAPPEND); 264218585Sjkim dac_granted &= acl_mask_granted; 265218585Sjkim 266218585Sjkim if ((accmode & dac_granted) == accmode) 267218585Sjkim return (0); 268218585Sjkim 269218585Sjkim group_matched = 1; 270218585Sjkim break; 271218585Sjkim 272218585Sjkim default: 273218585Sjkim break; 274218585Sjkim } 275218585Sjkim } 276218585Sjkim 277218585Sjkim if (group_matched == 1) { 278218585Sjkim /* 279218585Sjkim * There was a match, but it did not grant rights via pure 280218585Sjkim * DAC. Try again, this time with privilege. 281218585Sjkim */ 282218585Sjkim for (i = 0; i < acl->acl_cnt; i++) { 283218585Sjkim switch (acl->acl_entry[i].ae_tag) { 284218585Sjkim case ACL_GROUP_OBJ: 285218585Sjkim if (!groupmember(file_gid, cred)) 286218585Sjkim break; 287218585Sjkim dac_granted = 0; 288218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 289218585Sjkim dac_granted |= VEXEC; 290218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_READ) 291218585Sjkim dac_granted |= VREAD; 292218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_WRITE) 293218585Sjkim dac_granted |= (VWRITE | VAPPEND); 294218585Sjkim dac_granted &= acl_mask_granted; 295218585Sjkim 296218585Sjkim /* 297218585Sjkim * XXXRW: Do privilege lookup here. 298218585Sjkim */ 299218585Sjkim if ((accmode & (dac_granted | priv_granted)) 300218585Sjkim != accmode) 301218585Sjkim break; 302218585Sjkim 303218585Sjkim if (privused != NULL) 304218585Sjkim *privused = 1; 305218585Sjkim return (0); 306218585Sjkim 307218585Sjkim case ACL_GROUP: 308218585Sjkim if (!groupmember(acl->acl_entry[i].ae_id, 309218585Sjkim cred)) 310218585Sjkim break; 311218585Sjkim dac_granted = 0; 312218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_EXECUTE) 313218585Sjkim dac_granted |= VEXEC; 314218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_READ) 315218585Sjkim dac_granted |= VREAD; 316218585Sjkim if (acl->acl_entry[i].ae_perm & ACL_WRITE) 317218585Sjkim dac_granted |= (VWRITE | VAPPEND); 318218585Sjkim dac_granted &= acl_mask_granted; 319218585Sjkim 320218585Sjkim /* 321218585Sjkim * XXXRW: Do privilege lookup here. 322218585Sjkim */ 323218585Sjkim if ((accmode & (dac_granted | priv_granted)) 324218585Sjkim != accmode) 325218585Sjkim break; 326218585Sjkim 327218585Sjkim if (privused != NULL) 328218585Sjkim *privused = 1; 329218585Sjkim return (0); 330218585Sjkim 331327557Sjkim default: 332327557Sjkim break; 333327557Sjkim } 334327557Sjkim } 335218585Sjkim /* 336327557Sjkim * Even with privilege, group membership was not sufficient. 337327557Sjkim * Return failure. 338218585Sjkim */ 339327557Sjkim goto error; 340218585Sjkim } 341218585Sjkim 342218585Sjkim /* 343218585Sjkim * Fall back on ACL_OTHER. ACL_MASK is not applied to ACL_OTHER. 344218585Sjkim */ 345218585Sjkim dac_granted = 0; 346218585Sjkim if (acl_other->ae_perm & ACL_EXECUTE) 347218585Sjkim dac_granted |= VEXEC; 348298714Sjkim if (acl_other->ae_perm & ACL_READ) 349298714Sjkim dac_granted |= VREAD; 350218585Sjkim if (acl_other->ae_perm & ACL_WRITE) 351218585Sjkim dac_granted |= (VWRITE | VAPPEND); 352218585Sjkim 353218585Sjkim if ((accmode & dac_granted) == accmode) 354218585Sjkim return (0); 355218585Sjkim /* 356218585Sjkim * XXXRW: Do privilege lookup here. 357218585Sjkim */ 358218585Sjkim if ((accmode & (dac_granted | priv_granted)) == accmode) { 359218585Sjkim if (privused != NULL) 360218585Sjkim *privused = 1; 361218585Sjkim return (0); 362218585Sjkim } 363218585Sjkim 364218585Sjkimerror: 365218585Sjkim return ((accmode & VADMIN) ? EPERM : EACCES); 366218585Sjkim} 367218585Sjkim 368218585Sjkim/* 369218585Sjkim * For the purposes of filesystems maintaining the _OBJ entries in an inode 370218585Sjkim * with a mode_t field, this routine converts a mode_t entry to an 371218585Sjkim * acl_perm_t. 372218585Sjkim */ 373218585Sjkimacl_perm_t 374218585Sjkimacl_posix1e_mode_to_perm(acl_tag_t tag, mode_t mode) 375218585Sjkim{ 376218585Sjkim acl_perm_t perm = 0; 377218585Sjkim 378218585Sjkim switch(tag) { 379218585Sjkim case ACL_USER_OBJ: 380218585Sjkim if (mode & S_IXUSR) 381218585Sjkim perm |= ACL_EXECUTE; 382218585Sjkim if (mode & S_IRUSR) 383218585Sjkim perm |= ACL_READ; 384218585Sjkim if (mode & S_IWUSR) 385218585Sjkim perm |= ACL_WRITE; 386218585Sjkim return (perm); 387218585Sjkim 388218585Sjkim case ACL_GROUP_OBJ: 389218585Sjkim if (mode & S_IXGRP) 390218585Sjkim perm |= ACL_EXECUTE; 391218585Sjkim if (mode & S_IRGRP) 392218585Sjkim perm |= ACL_READ; 393298714Sjkim if (mode & S_IWGRP) 394298714Sjkim perm |= ACL_WRITE; 395218585Sjkim return (perm); 396218585Sjkim 397218585Sjkim case ACL_OTHER: 398218585Sjkim if (mode & S_IXOTH) 399218585Sjkim perm |= ACL_EXECUTE; 400218585Sjkim if (mode & S_IROTH) 401218585Sjkim perm |= ACL_READ; 402241973Sjkim if (mode & S_IWOTH) 403218585Sjkim perm |= ACL_WRITE; 404218585Sjkim return (perm); 405218585Sjkim 406218585Sjkim default: 407218585Sjkim printf("acl_posix1e_mode_to_perm: invalid tag (%d)\n", tag); 408218585Sjkim return (0); 409218585Sjkim } 410218585Sjkim} 411218585Sjkim 412218585Sjkim/* 413218585Sjkim * Given inode information (uid, gid, mode), return an acl entry of the 414218585Sjkim * appropriate type. 415218585Sjkim */ 416218585Sjkimstruct acl_entry 417218585Sjkimacl_posix1e_mode_to_entry(acl_tag_t tag, uid_t uid, gid_t gid, mode_t mode) 418218585Sjkim{ 419218585Sjkim struct acl_entry acl_entry; 420218585Sjkim 421218585Sjkim acl_entry.ae_tag = tag; 422218585Sjkim acl_entry.ae_perm = acl_posix1e_mode_to_perm(tag, mode); 423298714Sjkim acl_entry.ae_entry_type = 0; 424298714Sjkim acl_entry.ae_flags = 0; 425298714Sjkim switch(tag) { 426298714Sjkim case ACL_USER_OBJ: 427298714Sjkim acl_entry.ae_id = uid; 428298714Sjkim break; 429218585Sjkim 430298714Sjkim case ACL_GROUP_OBJ: 431298714Sjkim acl_entry.ae_id = gid; 432218585Sjkim break; 433218585Sjkim 434218585Sjkim case ACL_OTHER: 435218585Sjkim acl_entry.ae_id = ACL_UNDEFINED_ID; 436218585Sjkim break; 437218585Sjkim 438218585Sjkim default: 439218585Sjkim acl_entry.ae_id = ACL_UNDEFINED_ID; 440218585Sjkim printf("acl_posix1e_mode_to_entry: invalid tag (%d)\n", tag); 441218585Sjkim } 442218585Sjkim 443218585Sjkim return (acl_entry); 444218585Sjkim} 445218585Sjkim 446218585Sjkim/* 447218585Sjkim * Utility function to generate a file mode given appropriate ACL entries. 448218585Sjkim */ 449298714Sjkimmode_t 450298714Sjkimacl_posix1e_perms_to_mode(struct acl_entry *acl_user_obj_entry, 451298714Sjkim struct acl_entry *acl_group_obj_entry, struct acl_entry *acl_other_entry) 452218585Sjkim{ 453218585Sjkim mode_t mode; 454218585Sjkim 455218585Sjkim mode = 0; 456218585Sjkim if (acl_user_obj_entry->ae_perm & ACL_EXECUTE) 457218585Sjkim mode |= S_IXUSR; 458218585Sjkim if (acl_user_obj_entry->ae_perm & ACL_READ) 459218585Sjkim mode |= S_IRUSR; 460218585Sjkim if (acl_user_obj_entry->ae_perm & ACL_WRITE) 461218585Sjkim mode |= S_IWUSR; 462218585Sjkim if (acl_group_obj_entry->ae_perm & ACL_EXECUTE) 463218585Sjkim mode |= S_IXGRP; 464218585Sjkim if (acl_group_obj_entry->ae_perm & ACL_READ) 465218585Sjkim mode |= S_IRGRP; 466218585Sjkim if (acl_group_obj_entry->ae_perm & ACL_WRITE) 467250838Sjkim mode |= S_IWGRP; 468218585Sjkim if (acl_other_entry->ae_perm & ACL_EXECUTE) 469218585Sjkim mode |= S_IXOTH; 470316303Sjkim if (acl_other_entry->ae_perm & ACL_READ) 471218585Sjkim mode |= S_IROTH; 472298714Sjkim if (acl_other_entry->ae_perm & ACL_WRITE) 473218585Sjkim mode |= S_IWOTH; 474218585Sjkim 475218585Sjkim return (mode); 476218585Sjkim} 477298714Sjkim 478218585Sjkim/* 479218585Sjkim * Utility function to generate a file mode given a complete POSIX.1e access 480218585Sjkim * ACL. Note that if the ACL is improperly formed, this may result in a 481218585Sjkim * panic. 482218585Sjkim */ 483218585Sjkimmode_t 484218585Sjkimacl_posix1e_acl_to_mode(struct acl *acl) 485218585Sjkim{ 486218585Sjkim struct acl_entry *acl_mask, *acl_user_obj, *acl_group_obj, *acl_other; 487218585Sjkim int i; 488218585Sjkim 489218585Sjkim /* 490218585Sjkim * Find the ACL entries relevant to a POSIX permission mode. 491218585Sjkim */ 492218585Sjkim acl_user_obj = acl_group_obj = acl_other = acl_mask = NULL; 493218585Sjkim for (i = 0; i < acl->acl_cnt; i++) { 494218585Sjkim switch (acl->acl_entry[i].ae_tag) { 495218585Sjkim case ACL_USER_OBJ: 496218585Sjkim acl_user_obj = &acl->acl_entry[i]; 497218585Sjkim break; 498218585Sjkim 499218585Sjkim case ACL_GROUP_OBJ: 500218585Sjkim acl_group_obj = &acl->acl_entry[i]; 501298714Sjkim break; 502298714Sjkim 503218585Sjkim case ACL_OTHER: 504218585Sjkim acl_other = &acl->acl_entry[i]; 505218585Sjkim break; 506218585Sjkim 507218585Sjkim case ACL_MASK: 508218585Sjkim acl_mask = &acl->acl_entry[i]; 509218585Sjkim break; 510218585Sjkim 511218585Sjkim case ACL_USER: 512218585Sjkim case ACL_GROUP: 513218585Sjkim break; 514218585Sjkim 515218585Sjkim default: 516218585Sjkim panic("acl_posix1e_acl_to_mode: bad ae_tag"); 517218585Sjkim } 518218585Sjkim } 519218585Sjkim 520218585Sjkim if (acl_user_obj == NULL || acl_group_obj == NULL || acl_other == NULL) 521218585Sjkim panic("acl_posix1e_acl_to_mode: missing base ae_tags"); 522218585Sjkim 523218585Sjkim /* 524218585Sjkim * POSIX.1e specifies that if there is an ACL_MASK entry, we replace 525218585Sjkim * the mode "group" bits with its permissions. If there isn't, we 526218585Sjkim * use the ACL_GROUP_OBJ permissions. 527 */ 528 if (acl_mask != NULL) 529 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_mask, 530 acl_other)); 531 else 532 return (acl_posix1e_perms_to_mode(acl_user_obj, acl_group_obj, 533 acl_other)); 534} 535 536/* 537 * Perform a syntactic check of the ACL, sufficient to allow an implementing 538 * filesystem to determine if it should accept this and rely on the POSIX.1e 539 * ACL properties. 540 */ 541int 542acl_posix1e_check(struct acl *acl) 543{ 544 int num_acl_user_obj, num_acl_user, num_acl_group_obj, num_acl_group; 545 int num_acl_mask, num_acl_other, i; 546 547 /* 548 * Verify that the number of entries does not exceed the maximum 549 * defined for acl_t. 550 * 551 * Verify that the correct number of various sorts of ae_tags are 552 * present: 553 * Exactly one ACL_USER_OBJ 554 * Exactly one ACL_GROUP_OBJ 555 * Exactly one ACL_OTHER 556 * If any ACL_USER or ACL_GROUP entries appear, then exactly one 557 * ACL_MASK entry must also appear. 558 * 559 * Verify that all ae_perm entries are in ACL_PERM_BITS. 560 * 561 * Verify all ae_tag entries are understood by this implementation. 562 * 563 * Note: Does not check for uniqueness of qualifier (ae_id) field. 564 */ 565 num_acl_user_obj = num_acl_user = num_acl_group_obj = num_acl_group = 566 num_acl_mask = num_acl_other = 0; 567 if (acl->acl_cnt > ACL_MAX_ENTRIES) 568 return (EINVAL); 569 for (i = 0; i < acl->acl_cnt; i++) { 570 /* 571 * Check for a valid tag. 572 */ 573 switch(acl->acl_entry[i].ae_tag) { 574 case ACL_USER_OBJ: 575 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 576 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 577 return (EINVAL); 578 num_acl_user_obj++; 579 break; 580 case ACL_GROUP_OBJ: 581 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 582 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 583 return (EINVAL); 584 num_acl_group_obj++; 585 break; 586 case ACL_USER: 587 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID) 588 return (EINVAL); 589 num_acl_user++; 590 break; 591 case ACL_GROUP: 592 if (acl->acl_entry[i].ae_id == ACL_UNDEFINED_ID) 593 return (EINVAL); 594 num_acl_group++; 595 break; 596 case ACL_OTHER: 597 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 598 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 599 return (EINVAL); 600 num_acl_other++; 601 break; 602 case ACL_MASK: 603 acl->acl_entry[i].ae_id = ACL_UNDEFINED_ID; /* XXX */ 604 if (acl->acl_entry[i].ae_id != ACL_UNDEFINED_ID) 605 return (EINVAL); 606 num_acl_mask++; 607 break; 608 default: 609 return (EINVAL); 610 } 611 /* 612 * Check for valid perm entries. 613 */ 614 if ((acl->acl_entry[i].ae_perm | ACL_PERM_BITS) != 615 ACL_PERM_BITS) 616 return (EINVAL); 617 } 618 if ((num_acl_user_obj != 1) || (num_acl_group_obj != 1) || 619 (num_acl_other != 1) || (num_acl_mask != 0 && num_acl_mask != 1)) 620 return (EINVAL); 621 if (((num_acl_group != 0) || (num_acl_user != 0)) && 622 (num_acl_mask != 1)) 623 return (EINVAL); 624 return (0); 625} 626 627/* 628 * Given a requested mode for a new object, and a default ACL, combine the 629 * two to produce a new mode. Be careful not to clear any bits that aren't 630 * intended to be affected by the POSIX.1e ACL. Eventually, this might also 631 * take the cmask as an argument, if we push that down into 632 * per-filesystem-code. 633 */ 634mode_t 635acl_posix1e_newfilemode(mode_t cmode, struct acl *dacl) 636{ 637 mode_t mode; 638 639 mode = cmode; 640 /* 641 * The current composition policy is that a permission bit must be 642 * set in *both* the ACL and the requested creation mode for it to 643 * appear in the resulting mode/ACL. First clear any possibly 644 * effected bits, then reconstruct. 645 */ 646 mode &= ACL_PRESERVE_MASK; 647 mode |= (ACL_OVERRIDE_MASK & cmode & acl_posix1e_acl_to_mode(dacl)); 648 649 return (mode); 650} 651