The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_acl_nfs4.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2008-2010 Edward Tomasz NapieraƂa <trasz@FreeBSD.org>
    5  * All rights reserved.
    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 /*
   30  * ACL support routines specific to NFSv4 access control lists.  These are
   31  * utility routines for code common across file systems implementing NFSv4
   32  * ACLs.
   33  */
   34 
   35 #ifdef _KERNEL
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/12.0/sys/kern/subr_acl_nfs4.c 326271 2017-11-27 15:20:12Z pfg $");
   38 
   39 #include <sys/param.h>
   40 #include <sys/kernel.h>
   41 #include <sys/module.h>
   42 #include <sys/systm.h>
   43 #include <sys/mount.h>
   44 #include <sys/priv.h>
   45 #include <sys/vnode.h>
   46 #include <sys/errno.h>
   47 #include <sys/stat.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/acl.h>
   50 #else
   51 #include <errno.h>
   52 #include <assert.h>
   53 #include <sys/acl.h>
   54 #include <sys/stat.h>
   55 #define KASSERT(a, b) assert(a)
   56 #define CTASSERT(a)
   57 
   58 #endif /* !_KERNEL */
   59 
   60 #ifdef _KERNEL
   61 
   62 static void     acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode);
   63 
   64 static int      acl_nfs4_old_semantics = 0;
   65 
   66 SYSCTL_INT(_vfs, OID_AUTO, acl_nfs4_old_semantics, CTLFLAG_RW,
   67     &acl_nfs4_old_semantics, 0, "Use pre-PSARC/2010/029 NFSv4 ACL semantics");
   68 
   69 static struct {
   70         accmode_t accmode;
   71         int mask;
   72 } accmode2mask[] = {{VREAD, ACL_READ_DATA},
   73                     {VWRITE, ACL_WRITE_DATA},
   74                     {VAPPEND, ACL_APPEND_DATA},
   75                     {VEXEC, ACL_EXECUTE},
   76                     {VREAD_NAMED_ATTRS, ACL_READ_NAMED_ATTRS},
   77                     {VWRITE_NAMED_ATTRS, ACL_WRITE_NAMED_ATTRS},
   78                     {VDELETE_CHILD, ACL_DELETE_CHILD},
   79                     {VREAD_ATTRIBUTES, ACL_READ_ATTRIBUTES},
   80                     {VWRITE_ATTRIBUTES, ACL_WRITE_ATTRIBUTES},
   81                     {VDELETE, ACL_DELETE},
   82                     {VREAD_ACL, ACL_READ_ACL},
   83                     {VWRITE_ACL, ACL_WRITE_ACL},
   84                     {VWRITE_OWNER, ACL_WRITE_OWNER},
   85                     {VSYNCHRONIZE, ACL_SYNCHRONIZE},
   86                     {0, 0}};
   87 
   88 static int
   89 _access_mask_from_accmode(accmode_t accmode)
   90 {
   91         int access_mask = 0, i;
   92 
   93         for (i = 0; accmode2mask[i].accmode != 0; i++) {
   94                 if (accmode & accmode2mask[i].accmode)
   95                         access_mask |= accmode2mask[i].mask;
   96         }
   97 
   98         /*
   99          * VAPPEND is just a modifier for VWRITE; if the caller asked
  100          * for 'VAPPEND | VWRITE', we want to check for ACL_APPEND_DATA only.
  101          */
  102         if (access_mask & ACL_APPEND_DATA)
  103                 access_mask &= ~ACL_WRITE_DATA;
  104 
  105         return (access_mask);
  106 }
  107 
  108 /*
  109  * Return 0, iff access is allowed, 1 otherwise.
  110  */
  111 static int
  112 _acl_denies(const struct acl *aclp, int access_mask, struct ucred *cred,
  113     int file_uid, int file_gid, int *denied_explicitly)
  114 {
  115         int i;
  116         const struct acl_entry *entry;
  117 
  118         if (denied_explicitly != NULL)
  119                 *denied_explicitly = 0;
  120 
  121         KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
  122             ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
  123 
  124         for (i = 0; i < aclp->acl_cnt; i++) {
  125                 entry = &(aclp->acl_entry[i]);
  126 
  127                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
  128                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
  129                         continue;
  130                 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
  131                         continue;
  132                 switch (entry->ae_tag) {
  133                 case ACL_USER_OBJ:
  134                         if (file_uid != cred->cr_uid)
  135                                 continue;
  136                         break;
  137                 case ACL_USER:
  138                         if (entry->ae_id != cred->cr_uid)
  139                                 continue;
  140                         break;
  141                 case ACL_GROUP_OBJ:
  142                         if (!groupmember(file_gid, cred))
  143                                 continue;
  144                         break;
  145                 case ACL_GROUP:
  146                         if (!groupmember(entry->ae_id, cred))
  147                                 continue;
  148                         break;
  149                 default:
  150                         KASSERT(entry->ae_tag == ACL_EVERYONE,
  151                             ("entry->ae_tag == ACL_EVERYONE"));
  152                 }
  153 
  154                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_DENY) {
  155                         if (entry->ae_perm & access_mask) {
  156                                 if (denied_explicitly != NULL)
  157                                         *denied_explicitly = 1;
  158                                 return (1);
  159                         }
  160                 }
  161 
  162                 access_mask &= ~(entry->ae_perm);
  163                 if (access_mask == 0)
  164                         return (0);
  165         }
  166 
  167         if (access_mask == 0)
  168                 return (0);
  169 
  170         return (1);
  171 }
  172 
  173 int
  174 vaccess_acl_nfs4(enum vtype type, uid_t file_uid, gid_t file_gid,
  175     struct acl *aclp, accmode_t accmode, struct ucred *cred, int *privused)
  176 {
  177         accmode_t priv_granted = 0;
  178         int denied, explicitly_denied, access_mask, is_directory,
  179             must_be_owner = 0;
  180         mode_t file_mode = 0;
  181 
  182         KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND |
  183             VEXPLICIT_DENY | VREAD_NAMED_ATTRS | VWRITE_NAMED_ATTRS |
  184             VDELETE_CHILD | VREAD_ATTRIBUTES | VWRITE_ATTRIBUTES | VDELETE |
  185             VREAD_ACL | VWRITE_ACL | VWRITE_OWNER | VSYNCHRONIZE)) == 0,
  186             ("invalid bit in accmode"));
  187         KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
  188                 ("VAPPEND without VWRITE"));
  189 
  190         if (privused != NULL)
  191                 *privused = 0;
  192 
  193         if (accmode & VADMIN)
  194                 must_be_owner = 1;
  195 
  196         /*
  197          * Ignore VSYNCHRONIZE permission.
  198          */
  199         accmode &= ~VSYNCHRONIZE;
  200 
  201         access_mask = _access_mask_from_accmode(accmode);
  202 
  203         if (type == VDIR)
  204                 is_directory = 1;
  205         else
  206                 is_directory = 0;
  207 
  208         /*
  209          * File owner is always allowed to read and write the ACL
  210          * and basic attributes.  This is to prevent a situation
  211          * where user would change ACL in a way that prevents him
  212          * from undoing the change.
  213          */
  214         if (file_uid == cred->cr_uid)
  215                 access_mask &= ~(ACL_READ_ACL | ACL_WRITE_ACL |
  216                     ACL_READ_ATTRIBUTES | ACL_WRITE_ATTRIBUTES);
  217 
  218         /*
  219          * Ignore append permission for regular files; use write
  220          * permission instead.
  221          */
  222         if (!is_directory && (access_mask & ACL_APPEND_DATA)) {
  223                 access_mask &= ~ACL_APPEND_DATA;
  224                 access_mask |= ACL_WRITE_DATA;
  225         }
  226 
  227         denied = _acl_denies(aclp, access_mask, cred, file_uid, file_gid,
  228             &explicitly_denied);
  229 
  230         if (must_be_owner) {
  231                 if (file_uid != cred->cr_uid)
  232                         denied = EPERM;
  233         }
  234 
  235         /*
  236          * For VEXEC, ensure that at least one execute bit is set for
  237          * non-directories. We have to check the mode here to stay
  238          * consistent with execve(2). See the test in
  239          * exec_check_permissions().
  240          */
  241         acl_nfs4_sync_mode_from_acl(&file_mode, aclp);
  242         if (!denied && !is_directory && (accmode & VEXEC) &&
  243             (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) == 0)
  244                 denied = EACCES;
  245 
  246         if (!denied)
  247                 return (0);
  248 
  249         /*
  250          * Access failed.  Iff it was not denied explicitly and
  251          * VEXPLICIT_DENY flag was specified, allow access.
  252          */
  253         if ((accmode & VEXPLICIT_DENY) && explicitly_denied == 0)
  254                 return (0);
  255 
  256         accmode &= ~VEXPLICIT_DENY;
  257 
  258         /*
  259          * No match.  Try to use privileges, if there are any.
  260          */
  261         if (is_directory) {
  262                 if ((accmode & VEXEC) && !priv_check_cred(cred,
  263                     PRIV_VFS_LOOKUP, 0))
  264                         priv_granted |= VEXEC;
  265         } else {
  266                 /*
  267                  * Ensure that at least one execute bit is on. Otherwise,
  268                  * a privileged user will always succeed, and we don't want
  269                  * this to happen unless the file really is executable.
  270                  */
  271                 if ((accmode & VEXEC) && (file_mode &
  272                     (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
  273                     !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
  274                         priv_granted |= VEXEC;
  275         }
  276 
  277         if ((accmode & VREAD) && !priv_check_cred(cred, PRIV_VFS_READ, 0))
  278                 priv_granted |= VREAD;
  279 
  280         if ((accmode & (VWRITE | VAPPEND | VDELETE_CHILD)) &&
  281             !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
  282                 priv_granted |= (VWRITE | VAPPEND | VDELETE_CHILD);
  283 
  284         if ((accmode & VADMIN_PERMS) &&
  285             !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
  286                 priv_granted |= VADMIN_PERMS;
  287 
  288         if ((accmode & VSTAT_PERMS) &&
  289             !priv_check_cred(cred, PRIV_VFS_STAT, 0))
  290                 priv_granted |= VSTAT_PERMS;
  291 
  292         if ((accmode & priv_granted) == accmode) {
  293                 if (privused != NULL)
  294                         *privused = 1;
  295 
  296                 return (0);
  297         }
  298 
  299         if (accmode & (VADMIN_PERMS | VDELETE_CHILD | VDELETE))
  300                 denied = EPERM;
  301         else
  302                 denied = EACCES;
  303 
  304         return (denied);
  305 }
  306 #endif /* _KERNEL */
  307 
  308 static int
  309 _acl_entry_matches(struct acl_entry *entry, acl_tag_t tag, acl_perm_t perm,
  310     acl_entry_type_t entry_type)
  311 {
  312         if (entry->ae_tag != tag)
  313                 return (0);
  314 
  315         if (entry->ae_id != ACL_UNDEFINED_ID)
  316                 return (0);
  317 
  318         if (entry->ae_perm != perm)
  319                 return (0);
  320 
  321         if (entry->ae_entry_type != entry_type)
  322                 return (0);
  323 
  324         if (entry->ae_flags != 0)
  325                 return (0);
  326 
  327         return (1);
  328 }
  329 
  330 static struct acl_entry *
  331 _acl_append(struct acl *aclp, acl_tag_t tag, acl_perm_t perm,
  332     acl_entry_type_t entry_type)
  333 {
  334         struct acl_entry *entry;
  335 
  336         KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
  337             ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
  338 
  339         entry = &(aclp->acl_entry[aclp->acl_cnt]);
  340         aclp->acl_cnt++;
  341 
  342         entry->ae_tag = tag;
  343         entry->ae_id = ACL_UNDEFINED_ID;
  344         entry->ae_perm = perm;
  345         entry->ae_entry_type = entry_type;
  346         entry->ae_flags = 0;
  347 
  348         return (entry);
  349 }
  350 
  351 static struct acl_entry *
  352 _acl_duplicate_entry(struct acl *aclp, int entry_index)
  353 {
  354         int i;
  355 
  356         KASSERT(aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
  357             ("aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
  358 
  359         for (i = aclp->acl_cnt; i > entry_index; i--)
  360                 aclp->acl_entry[i] = aclp->acl_entry[i - 1];
  361 
  362         aclp->acl_cnt++;
  363 
  364         return (&(aclp->acl_entry[entry_index + 1]));
  365 }
  366 
  367 static void
  368 acl_nfs4_sync_acl_from_mode_draft(struct acl *aclp, mode_t mode,
  369     int file_owner_id)
  370 {
  371         int i, meets, must_append;
  372         struct acl_entry *entry, *copy, *previous,
  373             *a1, *a2, *a3, *a4, *a5, *a6;
  374         mode_t amode;
  375         const int READ = 04;
  376         const int WRITE = 02;
  377         const int EXEC = 01;
  378 
  379         KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
  380             ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
  381 
  382         /*
  383          * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
  384          *
  385          * 3.16.6.3. Applying a Mode to an Existing ACL
  386          */
  387 
  388         /*
  389          * 1. For each ACE:
  390          */
  391         for (i = 0; i < aclp->acl_cnt; i++) {
  392                 entry = &(aclp->acl_entry[i]);
  393 
  394                 /*
  395                  * 1.1. If the type is neither ALLOW or DENY - skip.
  396                  */
  397                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
  398                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
  399                         continue;
  400 
  401                 /*
  402                  * 1.2. If ACL_ENTRY_INHERIT_ONLY is set - skip.
  403                  */
  404                 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
  405                         continue;
  406 
  407                 /*
  408                  * 1.3. If ACL_ENTRY_FILE_INHERIT or ACL_ENTRY_DIRECTORY_INHERIT
  409                  *      are set:
  410                  */
  411                 if (entry->ae_flags &
  412                     (ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT)) {
  413                         /*
  414                          * 1.3.1. A copy of the current ACE is made, and placed
  415                          *        in the ACL immediately following the current
  416                          *        ACE.
  417                          */
  418                         copy = _acl_duplicate_entry(aclp, i);
  419 
  420                         /*
  421                          * 1.3.2. In the first ACE, the flag
  422                          *        ACL_ENTRY_INHERIT_ONLY is set.
  423                          */
  424                         entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
  425 
  426                         /*
  427                          * 1.3.3. In the second ACE, the following flags
  428                          *        are cleared:
  429                          *        ACL_ENTRY_FILE_INHERIT,
  430                          *        ACL_ENTRY_DIRECTORY_INHERIT,
  431                          *        ACL_ENTRY_NO_PROPAGATE_INHERIT.
  432                          */
  433                         copy->ae_flags &= ~(ACL_ENTRY_FILE_INHERIT |
  434                             ACL_ENTRY_DIRECTORY_INHERIT |
  435                             ACL_ENTRY_NO_PROPAGATE_INHERIT);
  436 
  437                         /*
  438                          * The algorithm continues on with the second ACE.
  439                          */
  440                         i++;
  441                         entry = copy;
  442                 }
  443 
  444                 /*
  445                  * 1.4. If it's owner@, group@ or everyone@ entry, clear
  446                  *      ACL_READ_DATA, ACL_WRITE_DATA, ACL_APPEND_DATA
  447                  *      and ACL_EXECUTE.  Continue to the next entry.
  448                  */
  449                 if (entry->ae_tag == ACL_USER_OBJ ||
  450                     entry->ae_tag == ACL_GROUP_OBJ ||
  451                     entry->ae_tag == ACL_EVERYONE) {
  452                         entry->ae_perm &= ~(ACL_READ_DATA | ACL_WRITE_DATA |
  453                             ACL_APPEND_DATA | ACL_EXECUTE);
  454                         continue;
  455                 }
  456 
  457                 /*
  458                  * 1.5. Otherwise, if the "who" field did not match one
  459                  *      of OWNER@, GROUP@, EVERYONE@:
  460                  *
  461                  * 1.5.1. If the type is ALLOW, check the preceding ACE.
  462                  *        If it does not meet all of the following criteria:
  463                  */
  464                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW)
  465                         continue;
  466 
  467                 meets = 0;
  468                 if (i > 0) {
  469                         meets = 1;
  470                         previous = &(aclp->acl_entry[i - 1]);
  471 
  472                         /*
  473                          * 1.5.1.1. The type field is DENY,
  474                          */
  475                         if (previous->ae_entry_type != ACL_ENTRY_TYPE_DENY)
  476                                 meets = 0;
  477 
  478                         /*
  479                          * 1.5.1.2. The "who" field is the same as the current
  480                          *          ACE,
  481                          *
  482                          * 1.5.1.3. The flag bit ACE4_IDENTIFIER_GROUP
  483                          *          is the same as it is in the current ACE,
  484                          *          and no other flag bits are set,
  485                          */
  486                         if (previous->ae_id != entry->ae_id ||
  487                             previous->ae_tag != entry->ae_tag)
  488                                 meets = 0;
  489 
  490                         if (previous->ae_flags)
  491                                 meets = 0;
  492 
  493                         /*
  494                          * 1.5.1.4. The mask bits are a subset of the mask bits
  495                          *          of the current ACE, and are also subset of
  496                          *          the following: ACL_READ_DATA,
  497                          *          ACL_WRITE_DATA, ACL_APPEND_DATA, ACL_EXECUTE
  498                          */
  499                         if (previous->ae_perm & ~(entry->ae_perm))
  500                                 meets = 0;
  501 
  502                         if (previous->ae_perm & ~(ACL_READ_DATA |
  503                             ACL_WRITE_DATA | ACL_APPEND_DATA | ACL_EXECUTE))
  504                                 meets = 0;
  505                 }
  506 
  507                 if (!meets) {
  508                         /*
  509                          * Then the ACE of type DENY, with a who equal
  510                          * to the current ACE, flag bits equal to
  511                          * (<current ACE flags> & <ACE_IDENTIFIER_GROUP>)
  512                          * and no mask bits, is prepended.
  513                          */
  514                         previous = entry;
  515                         entry = _acl_duplicate_entry(aclp, i);
  516 
  517                         /* Adjust counter, as we've just added an entry. */
  518                         i++;
  519 
  520                         previous->ae_tag = entry->ae_tag;
  521                         previous->ae_id = entry->ae_id;
  522                         previous->ae_flags = entry->ae_flags;
  523                         previous->ae_perm = 0;
  524                         previous->ae_entry_type = ACL_ENTRY_TYPE_DENY;
  525                 }
  526 
  527                 /*
  528                  * 1.5.2. The following modifications are made to the prepended
  529                  *        ACE.  The intent is to mask the following ACE
  530                  *        to disallow ACL_READ_DATA, ACL_WRITE_DATA,
  531                  *        ACL_APPEND_DATA, or ACL_EXECUTE, based upon the group
  532                  *        permissions of the new mode.  As a special case,
  533                  *        if the ACE matches the current owner of the file,
  534                  *        the owner bits are used, rather than the group bits.
  535                  *        This is reflected in the algorithm below.
  536                  */
  537                 amode = mode >> 3;
  538 
  539                 /*
  540                  * If ACE4_IDENTIFIER_GROUP is not set, and the "who" field
  541                  * in ACE matches the owner of the file, we shift amode three
  542                  * more bits, in order to have the owner permission bits
  543                  * placed in the three low order bits of amode.
  544                  */
  545                 if (entry->ae_tag == ACL_USER && entry->ae_id == file_owner_id)
  546                         amode = amode >> 3;
  547 
  548                 if (entry->ae_perm & ACL_READ_DATA) {
  549                         if (amode & READ)
  550                                 previous->ae_perm &= ~ACL_READ_DATA;
  551                         else
  552                                 previous->ae_perm |= ACL_READ_DATA;
  553                 }
  554 
  555                 if (entry->ae_perm & ACL_WRITE_DATA) {
  556                         if (amode & WRITE)
  557                                 previous->ae_perm &= ~ACL_WRITE_DATA;
  558                         else
  559                                 previous->ae_perm |= ACL_WRITE_DATA;
  560                 }
  561 
  562                 if (entry->ae_perm & ACL_APPEND_DATA) {
  563                         if (amode & WRITE)
  564                                 previous->ae_perm &= ~ACL_APPEND_DATA;
  565                         else
  566                                 previous->ae_perm |= ACL_APPEND_DATA;
  567                 }
  568 
  569                 if (entry->ae_perm & ACL_EXECUTE) {
  570                         if (amode & EXEC)
  571                                 previous->ae_perm &= ~ACL_EXECUTE;
  572                         else
  573                                 previous->ae_perm |= ACL_EXECUTE;
  574                 }
  575 
  576                 /*
  577                  * 1.5.3. If ACE4_IDENTIFIER_GROUP is set in the flags
  578                  *        of the ALLOW ace:
  579                  *
  580                  * XXX: This point is not there in the Falkner's draft.
  581                  */
  582                 if (entry->ae_tag == ACL_GROUP &&
  583                     entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW) {
  584                         mode_t extramode, ownermode;
  585                         extramode = (mode >> 3) & 07;
  586                         ownermode = mode >> 6;
  587                         extramode &= ~ownermode;
  588 
  589                         if (extramode) {
  590                                 if (extramode & READ) {
  591                                         entry->ae_perm &= ~ACL_READ_DATA;
  592                                         previous->ae_perm &= ~ACL_READ_DATA;
  593                                 }
  594 
  595                                 if (extramode & WRITE) {
  596                                         entry->ae_perm &=
  597                                             ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
  598                                         previous->ae_perm &=
  599                                             ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
  600                                 }
  601 
  602                                 if (extramode & EXEC) {
  603                                         entry->ae_perm &= ~ACL_EXECUTE;
  604                                         previous->ae_perm &= ~ACL_EXECUTE;
  605                                 }
  606                         }
  607                 }
  608         }
  609 
  610         /*
  611          * 2. If there at least six ACEs, the final six ACEs are examined.
  612          *    If they are not equal to what we want, append six ACEs.
  613          */
  614         must_append = 0;
  615         if (aclp->acl_cnt < 6) {
  616                 must_append = 1;
  617         } else {
  618                 a6 = &(aclp->acl_entry[aclp->acl_cnt - 1]);
  619                 a5 = &(aclp->acl_entry[aclp->acl_cnt - 2]);
  620                 a4 = &(aclp->acl_entry[aclp->acl_cnt - 3]);
  621                 a3 = &(aclp->acl_entry[aclp->acl_cnt - 4]);
  622                 a2 = &(aclp->acl_entry[aclp->acl_cnt - 5]);
  623                 a1 = &(aclp->acl_entry[aclp->acl_cnt - 6]);
  624 
  625                 if (!_acl_entry_matches(a1, ACL_USER_OBJ, 0,
  626                     ACL_ENTRY_TYPE_DENY))
  627                         must_append = 1;
  628                 if (!_acl_entry_matches(a2, ACL_USER_OBJ, ACL_WRITE_ACL |
  629                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
  630                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW))
  631                         must_append = 1;
  632                 if (!_acl_entry_matches(a3, ACL_GROUP_OBJ, 0,
  633                     ACL_ENTRY_TYPE_DENY))
  634                         must_append = 1;
  635                 if (!_acl_entry_matches(a4, ACL_GROUP_OBJ, 0,
  636                     ACL_ENTRY_TYPE_ALLOW))
  637                         must_append = 1;
  638                 if (!_acl_entry_matches(a5, ACL_EVERYONE, ACL_WRITE_ACL |
  639                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
  640                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY))
  641                         must_append = 1;
  642                 if (!_acl_entry_matches(a6, ACL_EVERYONE, ACL_READ_ACL |
  643                     ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
  644                     ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW))
  645                         must_append = 1;
  646         }
  647 
  648         if (must_append) {
  649                 KASSERT(aclp->acl_cnt + 6 <= ACL_MAX_ENTRIES,
  650                     ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
  651 
  652                 a1 = _acl_append(aclp, ACL_USER_OBJ, 0, ACL_ENTRY_TYPE_DENY);
  653                 a2 = _acl_append(aclp, ACL_USER_OBJ, ACL_WRITE_ACL |
  654                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
  655                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_ALLOW);
  656                 a3 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_DENY);
  657                 a4 = _acl_append(aclp, ACL_GROUP_OBJ, 0, ACL_ENTRY_TYPE_ALLOW);
  658                 a5 = _acl_append(aclp, ACL_EVERYONE, ACL_WRITE_ACL |
  659                     ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
  660                     ACL_WRITE_NAMED_ATTRS, ACL_ENTRY_TYPE_DENY);
  661                 a6 = _acl_append(aclp, ACL_EVERYONE, ACL_READ_ACL |
  662                     ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS |
  663                     ACL_SYNCHRONIZE, ACL_ENTRY_TYPE_ALLOW);
  664 
  665                 KASSERT(a1 != NULL && a2 != NULL && a3 != NULL && a4 != NULL &&
  666                     a5 != NULL && a6 != NULL, ("couldn't append to ACL."));
  667         }
  668 
  669         /*
  670          * 3. The final six ACEs are adjusted according to the incoming mode.
  671          */
  672         if (mode & S_IRUSR)
  673                 a2->ae_perm |= ACL_READ_DATA;
  674         else
  675                 a1->ae_perm |= ACL_READ_DATA;
  676         if (mode & S_IWUSR)
  677                 a2->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
  678         else
  679                 a1->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
  680         if (mode & S_IXUSR)
  681                 a2->ae_perm |= ACL_EXECUTE;
  682         else
  683                 a1->ae_perm |= ACL_EXECUTE;
  684 
  685         if (mode & S_IRGRP)
  686                 a4->ae_perm |= ACL_READ_DATA;
  687         else
  688                 a3->ae_perm |= ACL_READ_DATA;
  689         if (mode & S_IWGRP)
  690                 a4->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
  691         else
  692                 a3->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
  693         if (mode & S_IXGRP)
  694                 a4->ae_perm |= ACL_EXECUTE;
  695         else
  696                 a3->ae_perm |= ACL_EXECUTE;
  697 
  698         if (mode & S_IROTH)
  699                 a6->ae_perm |= ACL_READ_DATA;
  700         else
  701                 a5->ae_perm |= ACL_READ_DATA;
  702         if (mode & S_IWOTH)
  703                 a6->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
  704         else
  705                 a5->ae_perm |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
  706         if (mode & S_IXOTH)
  707                 a6->ae_perm |= ACL_EXECUTE;
  708         else
  709                 a5->ae_perm |= ACL_EXECUTE;
  710 }
  711 
  712 #ifdef _KERNEL
  713 void
  714 acl_nfs4_sync_acl_from_mode(struct acl *aclp, mode_t mode,
  715     int file_owner_id)
  716 {
  717 
  718         if (acl_nfs4_old_semantics)
  719                 acl_nfs4_sync_acl_from_mode_draft(aclp, mode, file_owner_id);
  720         else
  721                 acl_nfs4_trivial_from_mode(aclp, mode);
  722 }
  723 #endif /* _KERNEL */
  724 
  725 void
  726 acl_nfs4_sync_mode_from_acl(mode_t *_mode, const struct acl *aclp)
  727 {
  728         int i;
  729         mode_t old_mode = *_mode, mode = 0, seen = 0;
  730         const struct acl_entry *entry;
  731 
  732         KASSERT(aclp->acl_cnt <= ACL_MAX_ENTRIES,
  733             ("aclp->acl_cnt <= ACL_MAX_ENTRIES"));
  734 
  735         /*
  736          * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
  737          *
  738          * 3.16.6.1. Recomputing mode upon SETATTR of ACL
  739          */
  740 
  741         for (i = 0; i < aclp->acl_cnt; i++) {
  742                 entry = &(aclp->acl_entry[i]);
  743 
  744                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
  745                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
  746                         continue;
  747 
  748                 if (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY)
  749                         continue;
  750 
  751                 if (entry->ae_tag == ACL_USER_OBJ) {
  752                         if ((entry->ae_perm & ACL_READ_DATA) &&
  753                             ((seen & S_IRUSR) == 0)) {
  754                                 seen |= S_IRUSR;
  755                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  756                                         mode |= S_IRUSR;
  757                         }
  758                         if ((entry->ae_perm & ACL_WRITE_DATA) &&
  759                              ((seen & S_IWUSR) == 0)) {
  760                                 seen |= S_IWUSR;
  761                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  762                                         mode |= S_IWUSR;
  763                         }
  764                         if ((entry->ae_perm & ACL_EXECUTE) &&
  765                             ((seen & S_IXUSR) == 0)) {
  766                                 seen |= S_IXUSR;
  767                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  768                                         mode |= S_IXUSR;
  769                         }
  770                 } else if (entry->ae_tag == ACL_GROUP_OBJ) {
  771                         if ((entry->ae_perm & ACL_READ_DATA) &&
  772                             ((seen & S_IRGRP) == 0)) {
  773                                 seen |= S_IRGRP;
  774                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  775                                         mode |= S_IRGRP;
  776                         }
  777                         if ((entry->ae_perm & ACL_WRITE_DATA) &&
  778                             ((seen & S_IWGRP) == 0)) {
  779                                 seen |= S_IWGRP;
  780                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  781                                         mode |= S_IWGRP;
  782                         }
  783                         if ((entry->ae_perm & ACL_EXECUTE) &&
  784                             ((seen & S_IXGRP) == 0)) {
  785                                 seen |= S_IXGRP;
  786                                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  787                                         mode |= S_IXGRP;
  788                         }
  789                 } else if (entry->ae_tag == ACL_EVERYONE) {
  790                         if (entry->ae_perm & ACL_READ_DATA) {
  791                                 if ((seen & S_IRUSR) == 0) {
  792                                         seen |= S_IRUSR;
  793                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  794                                                 mode |= S_IRUSR;
  795                                 }
  796                                 if ((seen & S_IRGRP) == 0) {
  797                                         seen |= S_IRGRP;
  798                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  799                                                 mode |= S_IRGRP;
  800                                 }
  801                                 if ((seen & S_IROTH) == 0) {
  802                                         seen |= S_IROTH;
  803                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  804                                                 mode |= S_IROTH;
  805                                 }
  806                         }
  807                         if (entry->ae_perm & ACL_WRITE_DATA) {
  808                                 if ((seen & S_IWUSR) == 0) {
  809                                         seen |= S_IWUSR;
  810                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  811                                                 mode |= S_IWUSR;
  812                                 }
  813                                 if ((seen & S_IWGRP) == 0) {
  814                                         seen |= S_IWGRP;
  815                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  816                                                 mode |= S_IWGRP;
  817                                 }
  818                                 if ((seen & S_IWOTH) == 0) {
  819                                         seen |= S_IWOTH;
  820                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  821                                                 mode |= S_IWOTH;
  822                                 }
  823                         }
  824                         if (entry->ae_perm & ACL_EXECUTE) {
  825                                 if ((seen & S_IXUSR) == 0) {
  826                                         seen |= S_IXUSR;
  827                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  828                                                 mode |= S_IXUSR;
  829                                 }
  830                                 if ((seen & S_IXGRP) == 0) {
  831                                         seen |= S_IXGRP;
  832                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  833                                                 mode |= S_IXGRP;
  834                                 }
  835                                 if ((seen & S_IXOTH) == 0) {
  836                                         seen |= S_IXOTH;
  837                                         if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  838                                                 mode |= S_IXOTH;
  839                                 }
  840                         }
  841                 }
  842         }
  843 
  844         *_mode = mode | (old_mode & ACL_PRESERVE_MASK);
  845 }
  846 
  847 #ifdef _KERNEL
  848 /*
  849  * Calculate inherited ACL in a manner compatible with NFSv4 Minor Version 1,
  850  * draft-ietf-nfsv4-minorversion1-03.txt.
  851  */
  852 static void             
  853 acl_nfs4_compute_inherited_acl_draft(const struct acl *parent_aclp,
  854     struct acl *child_aclp, mode_t mode, int file_owner_id,
  855     int is_directory)
  856 {
  857         int i, flags;
  858         const struct acl_entry *parent_entry;
  859         struct acl_entry *entry, *copy;
  860 
  861         KASSERT(child_aclp->acl_cnt == 0, ("child_aclp->acl_cnt == 0"));
  862         KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
  863             ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
  864 
  865         /*
  866          * NFSv4 Minor Version 1, draft-ietf-nfsv4-minorversion1-03.txt
  867          *
  868          * 3.16.6.2. Applying the mode given to CREATE or OPEN
  869          *           to an inherited ACL
  870          */
  871 
  872         /*
  873          * 1. Form an ACL that is the concatenation of all inheritable ACEs.
  874          */
  875         for (i = 0; i < parent_aclp->acl_cnt; i++) {
  876                 parent_entry = &(parent_aclp->acl_entry[i]);
  877                 flags = parent_entry->ae_flags;
  878 
  879                 /*
  880                  * Entry is not inheritable at all.
  881                  */
  882                 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
  883                     ACL_ENTRY_FILE_INHERIT)) == 0)
  884                         continue;
  885 
  886                 /*
  887                  * We're creating a file, but entry is not inheritable
  888                  * by files.
  889                  */
  890                 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
  891                         continue;
  892 
  893                 /*
  894                  * Entry is inheritable only by files, but has NO_PROPAGATE
  895                  * flag set, and we're creating a directory, so it wouldn't
  896                  * propagate to any file in that directory anyway.
  897                  */
  898                 if (is_directory &&
  899                     (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
  900                     (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
  901                         continue;
  902 
  903                 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
  904                     ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
  905                 child_aclp->acl_entry[child_aclp->acl_cnt] = *parent_entry;
  906                 child_aclp->acl_cnt++;
  907         }
  908 
  909         /*
  910          * 2. For each entry in the new ACL, adjust its flags, possibly
  911          *    creating two entries in place of one.
  912          */
  913         for (i = 0; i < child_aclp->acl_cnt; i++) {
  914                 entry = &(child_aclp->acl_entry[i]);
  915 
  916                 /*
  917                  * This is not in the specification, but SunOS
  918                  * apparently does that.
  919                  */
  920                 if (((entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT) ||
  921                     !is_directory) &&
  922                     entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  923                         entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
  924 
  925                 /*
  926                  * 2.A. If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if the object
  927                  *      being created is not a directory, then clear the
  928                  *      following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
  929                  *      ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
  930                  *      ACL_ENTRY_INHERIT_ONLY.
  931                  */
  932                 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
  933                     !is_directory) {
  934                         entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
  935                         ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
  936                         ACL_ENTRY_INHERIT_ONLY);
  937 
  938                         /*
  939                          * Continue on to the next ACE.
  940                          */
  941                         continue;
  942                 }
  943 
  944                 /*
  945                  * 2.B. If the object is a directory and ACL_ENTRY_FILE_INHERIT
  946                  *      is set, but ACL_ENTRY_NO_PROPAGATE_INHERIT is not set, ensure
  947                  *      that ACL_ENTRY_INHERIT_ONLY is set.  Continue to the
  948                  *      next ACE.  Otherwise...
  949                  */
  950                 /*
  951                  * XXX: Read it again and make sure what does the "otherwise"
  952                  *      apply to.
  953                  */
  954                 if (is_directory &&
  955                     (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
  956                     ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
  957                         entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
  958                         continue;
  959                 }
  960 
  961                 /*
  962                  * 2.C. If the type of the ACE is neither ALLOW nor deny,
  963                  *      then continue.
  964                  */
  965                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
  966                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
  967                         continue;
  968 
  969                 /*
  970                  * 2.D. Copy the original ACE into a second, adjacent ACE.
  971                  */
  972                 copy = _acl_duplicate_entry(child_aclp, i);
  973 
  974                 /*
  975                  * 2.E. On the first ACE, ensure that ACL_ENTRY_INHERIT_ONLY
  976                  *      is set.
  977                  */
  978                 entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
  979 
  980                 /*
  981                  * 2.F. On the second ACE, clear the following flags:
  982                  *      ACL_ENTRY_NO_PROPAGATE_INHERIT, ACL_ENTRY_FILE_INHERIT,
  983                  *      ACL_ENTRY_DIRECTORY_INHERIT, ACL_ENTRY_INHERIT_ONLY.
  984                  */
  985                 copy->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
  986                     ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
  987                     ACL_ENTRY_INHERIT_ONLY);
  988 
  989                 /*
  990                  * 2.G. On the second ACE, if the type is ALLOW,
  991                  *      an implementation MAY clear the following
  992                  *      mask bits: ACL_WRITE_ACL, ACL_WRITE_OWNER.
  993                  */
  994                 if (copy->ae_entry_type == ACL_ENTRY_TYPE_ALLOW)
  995                         copy->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER);
  996 
  997                 /*
  998                  * Increment the counter to skip the copied entry.
  999                  */
 1000                 i++;
 1001         }
 1002 
 1003         /*
 1004          * 3. To ensure that the mode is honored, apply the algorithm describe
 1005          *    in Section 2.16.6.3, using the mode that is to be used for file
 1006          *    creation.
 1007          */
 1008         acl_nfs4_sync_acl_from_mode(child_aclp, mode, file_owner_id);
 1009 }
 1010 #endif /* _KERNEL */
 1011 
 1012 /*
 1013  * Populate the ACL with entries inherited from parent_aclp.
 1014  */
 1015 static void             
 1016 acl_nfs4_inherit_entries(const struct acl *parent_aclp,
 1017     struct acl *child_aclp, mode_t mode, int file_owner_id,
 1018     int is_directory)
 1019 {
 1020         int i, flags, tag;
 1021         const struct acl_entry *parent_entry;
 1022         struct acl_entry *entry;
 1023 
 1024         KASSERT(parent_aclp->acl_cnt <= ACL_MAX_ENTRIES,
 1025             ("parent_aclp->acl_cnt <= ACL_MAX_ENTRIES"));
 1026 
 1027         for (i = 0; i < parent_aclp->acl_cnt; i++) {
 1028                 parent_entry = &(parent_aclp->acl_entry[i]);
 1029                 flags = parent_entry->ae_flags;
 1030                 tag = parent_entry->ae_tag;
 1031 
 1032                 /*
 1033                  * Don't inherit owner@, group@, or everyone@ entries.
 1034                  */
 1035                 if (tag == ACL_USER_OBJ || tag == ACL_GROUP_OBJ ||
 1036                     tag == ACL_EVERYONE)
 1037                         continue;
 1038 
 1039                 /*
 1040                  * Entry is not inheritable at all.
 1041                  */
 1042                 if ((flags & (ACL_ENTRY_DIRECTORY_INHERIT |
 1043                     ACL_ENTRY_FILE_INHERIT)) == 0)
 1044                         continue;
 1045 
 1046                 /*
 1047                  * We're creating a file, but entry is not inheritable
 1048                  * by files.
 1049                  */
 1050                 if (!is_directory && (flags & ACL_ENTRY_FILE_INHERIT) == 0)
 1051                         continue;
 1052 
 1053                 /*
 1054                  * Entry is inheritable only by files, but has NO_PROPAGATE
 1055                  * flag set, and we're creating a directory, so it wouldn't
 1056                  * propagate to any file in that directory anyway.
 1057                  */
 1058                 if (is_directory &&
 1059                     (flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0 &&
 1060                     (flags & ACL_ENTRY_NO_PROPAGATE_INHERIT))
 1061                         continue;
 1062 
 1063                 /*
 1064                  * Entry qualifies for being inherited.
 1065                  */
 1066                 KASSERT(child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES,
 1067                     ("child_aclp->acl_cnt + 1 <= ACL_MAX_ENTRIES"));
 1068                 entry = &(child_aclp->acl_entry[child_aclp->acl_cnt]);
 1069                 *entry = *parent_entry;
 1070                 child_aclp->acl_cnt++;
 1071 
 1072                 entry->ae_flags &= ~ACL_ENTRY_INHERIT_ONLY;
 1073                 entry->ae_flags |= ACL_ENTRY_INHERITED;
 1074 
 1075                 /*
 1076                  * If the type of the ACE is neither ALLOW nor DENY,
 1077                  * then leave it as it is and proceed to the next one.
 1078                  */
 1079                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
 1080                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
 1081                         continue;
 1082 
 1083                 /*
 1084                  * If the ACL_ENTRY_NO_PROPAGATE_INHERIT is set, or if
 1085                  * the object being created is not a directory, then clear
 1086                  * the following flags: ACL_ENTRY_NO_PROPAGATE_INHERIT,
 1087                  * ACL_ENTRY_FILE_INHERIT, ACL_ENTRY_DIRECTORY_INHERIT,
 1088                  * ACL_ENTRY_INHERIT_ONLY.
 1089                  */
 1090                 if (entry->ae_flags & ACL_ENTRY_NO_PROPAGATE_INHERIT ||
 1091                     !is_directory) {
 1092                         entry->ae_flags &= ~(ACL_ENTRY_NO_PROPAGATE_INHERIT |
 1093                         ACL_ENTRY_FILE_INHERIT | ACL_ENTRY_DIRECTORY_INHERIT |
 1094                         ACL_ENTRY_INHERIT_ONLY);
 1095                 }
 1096 
 1097                 /*
 1098                  * If the object is a directory and ACL_ENTRY_FILE_INHERIT
 1099                  * is set, but ACL_ENTRY_DIRECTORY_INHERIT is not set, ensure
 1100                  * that ACL_ENTRY_INHERIT_ONLY is set.
 1101                  */
 1102                 if (is_directory &&
 1103                     (entry->ae_flags & ACL_ENTRY_FILE_INHERIT) &&
 1104                     ((entry->ae_flags & ACL_ENTRY_DIRECTORY_INHERIT) == 0)) {
 1105                         entry->ae_flags |= ACL_ENTRY_INHERIT_ONLY;
 1106                 }
 1107 
 1108                 if (entry->ae_entry_type == ACL_ENTRY_TYPE_ALLOW &&
 1109                     (entry->ae_flags & ACL_ENTRY_INHERIT_ONLY) == 0) {
 1110                         /*
 1111                          * Some permissions must never be inherited.
 1112                          */
 1113                         entry->ae_perm &= ~(ACL_WRITE_ACL | ACL_WRITE_OWNER |
 1114                             ACL_WRITE_NAMED_ATTRS | ACL_WRITE_ATTRIBUTES);
 1115 
 1116                         /*
 1117                          * Others must be masked according to the file mode.
 1118                          */
 1119                         if ((mode & S_IRGRP) == 0)
 1120                                 entry->ae_perm &= ~ACL_READ_DATA;
 1121                         if ((mode & S_IWGRP) == 0)
 1122                                 entry->ae_perm &=
 1123                                     ~(ACL_WRITE_DATA | ACL_APPEND_DATA);
 1124                         if ((mode & S_IXGRP) == 0)
 1125                                 entry->ae_perm &= ~ACL_EXECUTE;
 1126                 }
 1127         }
 1128 }
 1129 
 1130 /*
 1131  * Calculate inherited ACL in a manner compatible with PSARC/2010/029.
 1132  * It's also being used to calculate a trivial ACL, by inheriting from
 1133  * a NULL ACL.
 1134  */
 1135 static void             
 1136 acl_nfs4_compute_inherited_acl_psarc(const struct acl *parent_aclp,
 1137     struct acl *aclp, mode_t mode, int file_owner_id, int is_directory)
 1138 {
 1139         acl_perm_t user_allow_first = 0, user_deny = 0, group_deny = 0;
 1140         acl_perm_t user_allow, group_allow, everyone_allow;
 1141 
 1142         KASSERT(aclp->acl_cnt == 0, ("aclp->acl_cnt == 0"));
 1143 
 1144         user_allow = group_allow = everyone_allow = ACL_READ_ACL |
 1145             ACL_READ_ATTRIBUTES | ACL_READ_NAMED_ATTRS | ACL_SYNCHRONIZE;
 1146         user_allow |= ACL_WRITE_ACL | ACL_WRITE_OWNER | ACL_WRITE_ATTRIBUTES |
 1147             ACL_WRITE_NAMED_ATTRS;
 1148 
 1149         if (mode & S_IRUSR)
 1150                 user_allow |= ACL_READ_DATA;
 1151         if (mode & S_IWUSR)
 1152                 user_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
 1153         if (mode & S_IXUSR)
 1154                 user_allow |= ACL_EXECUTE;
 1155 
 1156         if (mode & S_IRGRP)
 1157                 group_allow |= ACL_READ_DATA;
 1158         if (mode & S_IWGRP)
 1159                 group_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
 1160         if (mode & S_IXGRP)
 1161                 group_allow |= ACL_EXECUTE;
 1162 
 1163         if (mode & S_IROTH)
 1164                 everyone_allow |= ACL_READ_DATA;
 1165         if (mode & S_IWOTH)
 1166                 everyone_allow |= (ACL_WRITE_DATA | ACL_APPEND_DATA);
 1167         if (mode & S_IXOTH)
 1168                 everyone_allow |= ACL_EXECUTE;
 1169 
 1170         user_deny = ((group_allow | everyone_allow) & ~user_allow);
 1171         group_deny = everyone_allow & ~group_allow;
 1172         user_allow_first = group_deny & ~user_deny;
 1173 
 1174         if (user_allow_first != 0)
 1175                 _acl_append(aclp, ACL_USER_OBJ, user_allow_first,
 1176                     ACL_ENTRY_TYPE_ALLOW);
 1177         if (user_deny != 0)
 1178                 _acl_append(aclp, ACL_USER_OBJ, user_deny,
 1179                     ACL_ENTRY_TYPE_DENY);
 1180         if (group_deny != 0)
 1181                 _acl_append(aclp, ACL_GROUP_OBJ, group_deny,
 1182                     ACL_ENTRY_TYPE_DENY);
 1183 
 1184         if (parent_aclp != NULL)
 1185                 acl_nfs4_inherit_entries(parent_aclp, aclp, mode,
 1186                     file_owner_id, is_directory);
 1187 
 1188         _acl_append(aclp, ACL_USER_OBJ, user_allow, ACL_ENTRY_TYPE_ALLOW);
 1189         _acl_append(aclp, ACL_GROUP_OBJ, group_allow, ACL_ENTRY_TYPE_ALLOW);
 1190         _acl_append(aclp, ACL_EVERYONE, everyone_allow, ACL_ENTRY_TYPE_ALLOW);
 1191 }
 1192 
 1193 #ifdef _KERNEL
 1194 void            
 1195 acl_nfs4_compute_inherited_acl(const struct acl *parent_aclp,
 1196     struct acl *child_aclp, mode_t mode, int file_owner_id,
 1197     int is_directory)
 1198 {
 1199 
 1200         if (acl_nfs4_old_semantics)
 1201                 acl_nfs4_compute_inherited_acl_draft(parent_aclp, child_aclp,
 1202                     mode, file_owner_id, is_directory);
 1203         else
 1204                 acl_nfs4_compute_inherited_acl_psarc(parent_aclp, child_aclp,
 1205                     mode, file_owner_id, is_directory);
 1206 }
 1207 #endif /* _KERNEL */
 1208 
 1209 /*
 1210  * Calculate trivial ACL in a manner compatible with PSARC/2010/029.
 1211  * Note that this results in an ACL different from (but semantically
 1212  * equal to) the "canonical six" trivial ACL computed using algorithm
 1213  * described in draft-ietf-nfsv4-minorversion1-03.txt, 3.16.6.2.
 1214  */
 1215 static void
 1216 acl_nfs4_trivial_from_mode(struct acl *aclp, mode_t mode)
 1217 {
 1218 
 1219         aclp->acl_cnt = 0;
 1220         acl_nfs4_compute_inherited_acl_psarc(NULL, aclp, mode, -1, -1);
 1221 }
 1222 
 1223 #ifndef _KERNEL
 1224 /*
 1225  * This routine is used by libc to implement acl_strip_np(3)
 1226  * and acl_is_trivial_np(3).
 1227  */
 1228 void
 1229 acl_nfs4_trivial_from_mode_libc(struct acl *aclp, int mode, int canonical_six)
 1230 {
 1231 
 1232         aclp->acl_cnt = 0;
 1233         if (canonical_six)
 1234                 acl_nfs4_sync_acl_from_mode_draft(aclp, mode, -1);
 1235         else
 1236                 acl_nfs4_trivial_from_mode(aclp, mode);
 1237 }
 1238 #endif /* !_KERNEL */
 1239 
 1240 #ifdef _KERNEL
 1241 static int
 1242 _acls_are_equal(const struct acl *a, const struct acl *b)
 1243 {
 1244         int i;
 1245         const struct acl_entry *entrya, *entryb;
 1246 
 1247         if (a->acl_cnt != b->acl_cnt)
 1248                 return (0);
 1249 
 1250         for (i = 0; i < b->acl_cnt; i++) {
 1251                 entrya = &(a->acl_entry[i]);
 1252                 entryb = &(b->acl_entry[i]);
 1253 
 1254                 if (entrya->ae_tag != entryb->ae_tag ||
 1255                     entrya->ae_id != entryb->ae_id ||
 1256                     entrya->ae_perm != entryb->ae_perm ||
 1257                     entrya->ae_entry_type != entryb->ae_entry_type ||
 1258                     entrya->ae_flags != entryb->ae_flags)
 1259                         return (0);
 1260         }
 1261 
 1262         return (1);
 1263 }
 1264 
 1265 /*
 1266  * This routine is used to determine whether to remove extended attribute
 1267  * that stores ACL contents.
 1268  */
 1269 int
 1270 acl_nfs4_is_trivial(const struct acl *aclp, int file_owner_id)
 1271 {
 1272         int trivial;
 1273         mode_t tmpmode = 0;
 1274         struct acl *tmpaclp;
 1275 
 1276         if (aclp->acl_cnt > 6)
 1277                 return (0);
 1278 
 1279         /*
 1280          * Compute the mode from the ACL, then compute new ACL from that mode.
 1281          * If the ACLs are identical, then the ACL is trivial.
 1282          *
 1283          * XXX: I guess there is a faster way to do this.  However, even
 1284          *      this slow implementation significantly speeds things up
 1285          *      for files that don't have non-trivial ACLs - it's critical
 1286          *      for performance to not use EA when they are not needed.
 1287          *
 1288          * First try the PSARC/2010/029 semantics.
 1289          */
 1290         tmpaclp = acl_alloc(M_WAITOK | M_ZERO);
 1291         acl_nfs4_sync_mode_from_acl(&tmpmode, aclp);
 1292         acl_nfs4_trivial_from_mode(tmpaclp, tmpmode);
 1293         trivial = _acls_are_equal(aclp, tmpaclp);
 1294         if (trivial) {
 1295                 acl_free(tmpaclp);
 1296                 return (trivial);
 1297         }
 1298 
 1299         /*
 1300          * Check if it's a draft-ietf-nfsv4-minorversion1-03.txt trivial ACL.
 1301          */
 1302         tmpaclp->acl_cnt = 0;
 1303         acl_nfs4_sync_acl_from_mode_draft(tmpaclp, tmpmode, file_owner_id);
 1304         trivial = _acls_are_equal(aclp, tmpaclp);
 1305         acl_free(tmpaclp);
 1306 
 1307         return (trivial);
 1308 }
 1309 #endif /* _KERNEL */
 1310 
 1311 int
 1312 acl_nfs4_check(const struct acl *aclp, int is_directory)
 1313 {
 1314         int i;
 1315         const struct acl_entry *entry;
 1316 
 1317         /*
 1318          * The spec doesn't seem to say anything about ACL validity.
 1319          * It seems there is not much to do here.  There is even no need
 1320          * to count "owner@" or "everyone@" (ACL_USER_OBJ and ACL_EVERYONE)
 1321          * entries, as there can be several of them and that's perfectly
 1322          * valid.  There can be none of them too.  Really.
 1323          */
 1324 
 1325         if (aclp->acl_cnt > ACL_MAX_ENTRIES || aclp->acl_cnt <= 0)
 1326                 return (EINVAL);
 1327 
 1328         for (i = 0; i < aclp->acl_cnt; i++) {
 1329                 entry = &(aclp->acl_entry[i]);
 1330 
 1331                 switch (entry->ae_tag) {
 1332                 case ACL_USER_OBJ:
 1333                 case ACL_GROUP_OBJ:
 1334                 case ACL_EVERYONE:
 1335                         if (entry->ae_id != ACL_UNDEFINED_ID)
 1336                                 return (EINVAL);
 1337                         break;
 1338 
 1339                 case ACL_USER:
 1340                 case ACL_GROUP:
 1341                         if (entry->ae_id == ACL_UNDEFINED_ID)
 1342                                 return (EINVAL);
 1343                         break;
 1344 
 1345                 default:
 1346                         return (EINVAL);
 1347                 }
 1348 
 1349                 if ((entry->ae_perm | ACL_NFS4_PERM_BITS) != ACL_NFS4_PERM_BITS)
 1350                         return (EINVAL);
 1351 
 1352                 /*
 1353                  * Disallow ACL_ENTRY_TYPE_AUDIT and ACL_ENTRY_TYPE_ALARM for now.
 1354                  */
 1355                 if (entry->ae_entry_type != ACL_ENTRY_TYPE_ALLOW &&
 1356                     entry->ae_entry_type != ACL_ENTRY_TYPE_DENY)
 1357                         return (EINVAL);
 1358 
 1359                 if ((entry->ae_flags | ACL_FLAGS_BITS) != ACL_FLAGS_BITS)
 1360                         return (EINVAL);
 1361 
 1362                 /* Disallow unimplemented flags. */
 1363                 if (entry->ae_flags & (ACL_ENTRY_SUCCESSFUL_ACCESS |
 1364                     ACL_ENTRY_FAILED_ACCESS))
 1365                         return (EINVAL);
 1366 
 1367                 /* Disallow flags not allowed for ordinary files. */
 1368                 if (!is_directory) {
 1369                         if (entry->ae_flags & (ACL_ENTRY_FILE_INHERIT |
 1370                             ACL_ENTRY_DIRECTORY_INHERIT |
 1371                             ACL_ENTRY_NO_PROPAGATE_INHERIT | ACL_ENTRY_INHERIT_ONLY))
 1372                                 return (EINVAL);
 1373                 }
 1374         }
 1375 
 1376         return (0);
 1377 }
 1378 
 1379 #ifdef  _KERNEL
 1380 static int
 1381 acl_nfs4_modload(module_t module, int what, void *arg)
 1382 {
 1383         int ret;
 1384 
 1385         ret = 0;
 1386 
 1387         switch (what) {
 1388         case MOD_LOAD:
 1389         case MOD_SHUTDOWN:
 1390                 break;
 1391 
 1392         case MOD_QUIESCE:
 1393                 /* XXX TODO */
 1394                 ret = 0;
 1395                 break;
 1396 
 1397         case MOD_UNLOAD:
 1398                 /* XXX TODO */
 1399                 ret = 0;
 1400                 break;
 1401         default:
 1402                 ret = EINVAL;
 1403                 break;
 1404         }
 1405 
 1406         return (ret);
 1407 }
 1408 
 1409 static moduledata_t acl_nfs4_mod = {
 1410         "acl_nfs4",
 1411         acl_nfs4_modload,
 1412         NULL
 1413 };
 1414 
 1415 /*
 1416  * XXX TODO: which subsystem, order?
 1417  */
 1418 DECLARE_MODULE(acl_nfs4, acl_nfs4_mod, SI_SUB_VFS, SI_ORDER_FIRST);
 1419 MODULE_VERSION(acl_nfs4, 1);
 1420 #endif  /* _KERNEL */

Cache object: ef8cfc9f05bd22270c0365e33e368f8b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.