LCOV - code coverage report
Current view: top level - fs/nfs_common - nfsacl.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 90 0.0 %
Date: 2014-02-18 Functions: 0 6 0.0 %
Branches: 0 89 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * fs/nfs_common/nfsacl.c
       3                 :            :  *
       4                 :            :  *  Copyright (C) 2002-2003 Andreas Gruenbacher <agruen@suse.de>
       5                 :            :  */
       6                 :            : 
       7                 :            : /*
       8                 :            :  * The Solaris nfsacl protocol represents some ACLs slightly differently
       9                 :            :  * than POSIX 1003.1e draft 17 does (and we do):
      10                 :            :  *
      11                 :            :  *  - Minimal ACLs always have an ACL_MASK entry, so they have
      12                 :            :  *    four instead of three entries.
      13                 :            :  *  - The ACL_MASK entry in such minimal ACLs always has the same
      14                 :            :  *    permissions as the ACL_GROUP_OBJ entry. (In extended ACLs
      15                 :            :  *    the ACL_MASK and ACL_GROUP_OBJ entries may differ.)
      16                 :            :  *  - The identifier fields of the ACL_USER_OBJ and ACL_GROUP_OBJ
      17                 :            :  *    entries contain the identifiers of the owner and owning group.
      18                 :            :  *    (In POSIX ACLs we always set them to ACL_UNDEFINED_ID).
      19                 :            :  *  - ACL entries in the kernel are kept sorted in ascending order
      20                 :            :  *    of (e_tag, e_id). Solaris ACLs are unsorted.
      21                 :            :  */
      22                 :            : 
      23                 :            : #include <linux/module.h>
      24                 :            : #include <linux/fs.h>
      25                 :            : #include <linux/gfp.h>
      26                 :            : #include <linux/sunrpc/xdr.h>
      27                 :            : #include <linux/nfsacl.h>
      28                 :            : #include <linux/nfs3.h>
      29                 :            : #include <linux/sort.h>
      30                 :            : 
      31                 :            : MODULE_LICENSE("GPL");
      32                 :            : 
      33                 :            : EXPORT_SYMBOL_GPL(nfsacl_encode);
      34                 :            : EXPORT_SYMBOL_GPL(nfsacl_decode);
      35                 :            : 
      36                 :            : struct nfsacl_encode_desc {
      37                 :            :         struct xdr_array2_desc desc;
      38                 :            :         unsigned int count;
      39                 :            :         struct posix_acl *acl;
      40                 :            :         int typeflag;
      41                 :            :         kuid_t uid;
      42                 :            :         kgid_t gid;
      43                 :            : };
      44                 :            : 
      45                 :            : struct nfsacl_simple_acl {
      46                 :            :         struct posix_acl acl;
      47                 :            :         struct posix_acl_entry ace[4];
      48                 :            : };
      49                 :            : 
      50                 :            : static int
      51                 :          0 : xdr_nfsace_encode(struct xdr_array2_desc *desc, void *elem)
      52                 :            : {
      53                 :            :         struct nfsacl_encode_desc *nfsacl_desc =
      54                 :            :                 (struct nfsacl_encode_desc *) desc;
      55                 :            :         __be32 *p = elem;
      56                 :            : 
      57                 :          0 :         struct posix_acl_entry *entry =
      58                 :          0 :                 &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
      59                 :            : 
      60         [ #  # ]:          0 :         *p++ = htonl(entry->e_tag | nfsacl_desc->typeflag);
      61   [ #  #  #  #  :          0 :         switch(entry->e_tag) {
                      # ]
      62                 :            :                 case ACL_USER_OBJ:
      63                 :          0 :                         *p++ = htonl(from_kuid(&init_user_ns, nfsacl_desc->uid));
      64                 :          0 :                         break;
      65                 :            :                 case ACL_GROUP_OBJ:
      66                 :          0 :                         *p++ = htonl(from_kgid(&init_user_ns, nfsacl_desc->gid));
      67                 :          0 :                         break;
      68                 :            :                 case ACL_USER:
      69                 :          0 :                         *p++ = htonl(from_kuid(&init_user_ns, entry->e_uid));
      70                 :          0 :                         break;
      71                 :            :                 case ACL_GROUP:
      72                 :          0 :                         *p++ = htonl(from_kgid(&init_user_ns, entry->e_gid));
      73                 :          0 :                         break;
      74                 :            :                 default:  /* Solaris depends on that! */
      75                 :          0 :                         *p++ = 0;
      76                 :          0 :                         break;
      77                 :            :         }
      78         [ #  # ]:          0 :         *p++ = htonl(entry->e_perm & S_IRWXO);
      79                 :          0 :         return 0;
      80                 :            : }
      81                 :            : 
      82                 :            : /**
      83                 :            :  * nfsacl_encode - Encode an NFSv3 ACL
      84                 :            :  *
      85                 :            :  * @buf: destination xdr_buf to contain XDR encoded ACL
      86                 :            :  * @base: byte offset in xdr_buf where XDR'd ACL begins
      87                 :            :  * @inode: inode of file whose ACL this is
      88                 :            :  * @acl: posix_acl to encode
      89                 :            :  * @encode_entries: whether to encode ACEs as well
      90                 :            :  * @typeflag: ACL type: NFS_ACL_DEFAULT or zero
      91                 :            :  *
      92                 :            :  * Returns size of encoded ACL in bytes or a negative errno value.
      93                 :            :  */
      94                 :          0 : int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
      95                 :            :                   struct posix_acl *acl, int encode_entries, int typeflag)
      96                 :            : {
      97 [ #  # ][ #  # ]:          0 :         int entries = (acl && acl->a_count) ? max_t(int, acl->a_count, 4) : 0;
      98         [ #  # ]:          0 :         struct nfsacl_encode_desc nfsacl_desc = {
      99                 :            :                 .desc = {
     100                 :            :                         .elem_size = 12,
     101                 :            :                         .array_len = encode_entries ? entries : 0,
     102                 :            :                         .xcode = xdr_nfsace_encode,
     103                 :            :                 },
     104                 :            :                 .acl = acl,
     105                 :            :                 .typeflag = typeflag,
     106                 :          0 :                 .uid = inode->i_uid,
     107                 :          0 :                 .gid = inode->i_gid,
     108                 :            :         };
     109                 :            :         struct nfsacl_simple_acl aclbuf;
     110                 :            :         int err;
     111                 :            : 
     112   [ #  #  #  # ]:          0 :         if (entries > NFS_ACL_MAX_ENTRIES ||
     113                 :          0 :             xdr_encode_word(buf, base, entries))
     114                 :            :                 return -EINVAL;
     115 [ #  # ][ #  # ]:          0 :         if (encode_entries && acl && acl->a_count == 3) {
     116                 :            :                 struct posix_acl *acl2 = &aclbuf.acl;
     117                 :            : 
     118                 :            :                 /* Avoid the use of posix_acl_alloc().  nfsacl_encode() is
     119                 :            :                  * invoked in contexts where a memory allocation failure is
     120                 :            :                  * fatal.  Fortunately this fake ACL is small enough to
     121                 :            :                  * construct on the stack. */
     122                 :          0 :                 posix_acl_init(acl2, 4);
     123                 :            : 
     124                 :            :                 /* Insert entries in canonical order: other orders seem
     125                 :            :                  to confuse Solaris VxFS. */
     126                 :          0 :                 acl2->a_entries[0] = acl->a_entries[0];  /* ACL_USER_OBJ */
     127                 :          0 :                 acl2->a_entries[1] = acl->a_entries[1];  /* ACL_GROUP_OBJ */
     128                 :          0 :                 acl2->a_entries[2] = acl->a_entries[1];  /* ACL_MASK */
     129                 :          0 :                 acl2->a_entries[2].e_tag = ACL_MASK;
     130                 :          0 :                 acl2->a_entries[3] = acl->a_entries[2];  /* ACL_OTHER */
     131                 :          0 :                 nfsacl_desc.acl = acl2;
     132                 :            :         }
     133                 :          0 :         err = xdr_encode_array2(buf, base + 4, &nfsacl_desc.desc);
     134         [ #  # ]:          0 :         if (!err)
     135                 :          0 :                 err = 8 + nfsacl_desc.desc.elem_size *
     136                 :          0 :                           nfsacl_desc.desc.array_len;
     137                 :          0 :         return err;
     138                 :            : }
     139                 :            : 
     140                 :            : struct nfsacl_decode_desc {
     141                 :            :         struct xdr_array2_desc desc;
     142                 :            :         unsigned int count;
     143                 :            :         struct posix_acl *acl;
     144                 :            : };
     145                 :            : 
     146                 :            : static int
     147                 :          0 : xdr_nfsace_decode(struct xdr_array2_desc *desc, void *elem)
     148                 :            : {
     149                 :            :         struct nfsacl_decode_desc *nfsacl_desc =
     150                 :            :                 (struct nfsacl_decode_desc *) desc;
     151                 :            :         __be32 *p = elem;
     152                 :            :         struct posix_acl_entry *entry;
     153                 :            :         unsigned int id;
     154                 :            : 
     155         [ #  # ]:          0 :         if (!nfsacl_desc->acl) {
     156         [ #  # ]:          0 :                 if (desc->array_len > NFS_ACL_MAX_ENTRIES)
     157                 :            :                         return -EINVAL;
     158                 :          0 :                 nfsacl_desc->acl = posix_acl_alloc(desc->array_len, GFP_KERNEL);
     159         [ #  # ]:          0 :                 if (!nfsacl_desc->acl)
     160                 :            :                         return -ENOMEM;
     161                 :          0 :                 nfsacl_desc->count = 0;
     162                 :            :         }
     163                 :            : 
     164                 :          0 :         entry = &nfsacl_desc->acl->a_entries[nfsacl_desc->count++];
     165                 :          0 :         entry->e_tag = ntohl(*p++) & ~NFS_ACL_DEFAULT;
     166                 :          0 :         id = ntohl(*p++);
     167                 :          0 :         entry->e_perm = ntohl(*p++);
     168                 :            : 
     169   [ #  #  #  #  :          0 :         switch(entry->e_tag) {
                      # ]
     170                 :            :                 case ACL_USER:
     171                 :          0 :                         entry->e_uid = make_kuid(&init_user_ns, id);
     172         [ #  # ]:          0 :                         if (!uid_valid(entry->e_uid))
     173                 :            :                                 return -EINVAL;
     174                 :            :                         break;
     175                 :            :                 case ACL_GROUP:
     176                 :          0 :                         entry->e_gid = make_kgid(&init_user_ns, id);
     177         [ #  # ]:          0 :                         if (!gid_valid(entry->e_gid))
     178                 :            :                                 return -EINVAL;
     179                 :            :                         break;
     180                 :            :                 case ACL_USER_OBJ:
     181                 :            :                 case ACL_GROUP_OBJ:
     182                 :            :                 case ACL_OTHER:
     183         [ #  # ]:          0 :                         if (entry->e_perm & ~S_IRWXO)
     184                 :            :                                 return -EINVAL;
     185                 :            :                         break;
     186                 :            :                 case ACL_MASK:
     187                 :            :                         /* Solaris sometimes sets additional bits in the mask */
     188                 :          0 :                         entry->e_perm &= S_IRWXO;
     189                 :          0 :                         break;
     190                 :            :                 default:
     191                 :            :                         return -EINVAL;
     192                 :            :         }
     193                 :            : 
     194                 :            :         return 0;
     195                 :            : }
     196                 :            : 
     197                 :            : static int
     198                 :          0 : cmp_acl_entry(const void *x, const void *y)
     199                 :            : {
     200                 :            :         const struct posix_acl_entry *a = x, *b = y;
     201                 :            : 
     202         [ #  # ]:          0 :         if (a->e_tag != b->e_tag)
     203                 :          0 :                 return a->e_tag - b->e_tag;
     204 [ #  # ][ #  # ]:          0 :         else if ((a->e_tag == ACL_USER) && uid_gt(a->e_uid, b->e_uid))
     205                 :            :                 return 1;
     206 [ #  # ][ #  # ]:          0 :         else if ((a->e_tag == ACL_USER) && uid_lt(a->e_uid, b->e_uid))
     207                 :            :                 return -1;
     208 [ #  # ][ #  # ]:          0 :         else if ((a->e_tag == ACL_GROUP) && gid_gt(a->e_gid, b->e_gid))
     209                 :            :                 return 1;
     210 [ #  # ][ #  # ]:          0 :         else if ((a->e_tag == ACL_GROUP) && gid_lt(a->e_gid, b->e_gid))
     211                 :            :                 return -1;
     212                 :            :         else
     213                 :          0 :                 return 0;
     214                 :            : }
     215                 :            : 
     216                 :            : /*
     217                 :            :  * Convert from a Solaris ACL to a POSIX 1003.1e draft 17 ACL.
     218                 :            :  */
     219                 :            : static int
     220                 :          0 : posix_acl_from_nfsacl(struct posix_acl *acl)
     221                 :            : {
     222                 :            :         struct posix_acl_entry *pa, *pe,
     223                 :            :                *group_obj = NULL, *mask = NULL;
     224                 :            : 
     225         [ #  # ]:          0 :         if (!acl)
     226                 :            :                 return 0;
     227                 :            : 
     228                 :          0 :         sort(acl->a_entries, acl->a_count, sizeof(struct posix_acl_entry),
     229                 :            :              cmp_acl_entry, NULL);
     230                 :            : 
     231                 :            :         /* Find the ACL_GROUP_OBJ and ACL_MASK entries. */
     232         [ #  # ]:          0 :         FOREACH_ACL_ENTRY(pa, acl, pe) {
     233      [ #  #  # ]:          0 :                 switch(pa->e_tag) {
     234                 :            :                         case ACL_USER_OBJ:
     235                 :            :                                 break;
     236                 :            :                         case ACL_GROUP_OBJ:
     237                 :            :                                 group_obj = pa;
     238                 :          0 :                                 break;
     239                 :            :                         case ACL_MASK:
     240                 :            :                                 mask = pa;
     241                 :            :                                 /* fall through */
     242                 :            :                         case ACL_OTHER:
     243                 :            :                                 break;
     244                 :            :                 }
     245                 :            :         }
     246 [ #  # ][ #  # ]:          0 :         if (acl->a_count == 4 && group_obj && mask &&
                 [ #  # ]
     247                 :          0 :             mask->e_perm == group_obj->e_perm) {
     248                 :            :                 /* remove bogus ACL_MASK entry */
     249                 :          0 :                 memmove(mask, mask+1, (3 - (mask - acl->a_entries)) *
     250                 :            :                                       sizeof(struct posix_acl_entry));
     251                 :          0 :                 acl->a_count = 3;
     252                 :            :         }
     253                 :            :         return 0;
     254                 :            : }
     255                 :            : 
     256                 :            : /**
     257                 :            :  * nfsacl_decode - Decode an NFSv3 ACL
     258                 :            :  *
     259                 :            :  * @buf: xdr_buf containing XDR'd ACL data to decode
     260                 :            :  * @base: byte offset in xdr_buf where XDR'd ACL begins
     261                 :            :  * @aclcnt: count of ACEs in decoded posix_acl
     262                 :            :  * @pacl: buffer in which to place decoded posix_acl
     263                 :            :  *
     264                 :            :  * Returns the length of the decoded ACL in bytes, or a negative errno value.
     265                 :            :  */
     266                 :          0 : int nfsacl_decode(struct xdr_buf *buf, unsigned int base, unsigned int *aclcnt,
     267                 :            :                   struct posix_acl **pacl)
     268                 :            : {
     269                 :          0 :         struct nfsacl_decode_desc nfsacl_desc = {
     270                 :            :                 .desc = {
     271                 :            :                         .elem_size = 12,
     272         [ #  # ]:          0 :                         .xcode = pacl ? xdr_nfsace_decode : NULL,
     273                 :            :                 },
     274                 :            :         };
     275                 :            :         u32 entries;
     276                 :            :         int err;
     277                 :            : 
     278 [ #  # ][ #  # ]:          0 :         if (xdr_decode_word(buf, base, &entries) ||
     279                 :          0 :             entries > NFS_ACL_MAX_ENTRIES)
     280                 :            :                 return -EINVAL;
     281                 :          0 :         nfsacl_desc.desc.array_maxlen = entries;
     282                 :          0 :         err = xdr_decode_array2(buf, base + 4, &nfsacl_desc.desc);
     283         [ #  # ]:          0 :         if (err)
     284                 :            :                 return err;
     285         [ #  # ]:          0 :         if (pacl) {
     286   [ #  #  #  # ]:          0 :                 if (entries != nfsacl_desc.desc.array_len ||
     287                 :          0 :                     posix_acl_from_nfsacl(nfsacl_desc.acl) != 0) {
     288                 :          0 :                         posix_acl_release(nfsacl_desc.acl);
     289                 :            :                         return -EINVAL;
     290                 :            :                 }
     291                 :          0 :                 *pacl = nfsacl_desc.acl;
     292                 :            :         }
     293         [ #  # ]:          0 :         if (aclcnt)
     294                 :          0 :                 *aclcnt = entries;
     295                 :          0 :         return 8 + nfsacl_desc.desc.elem_size *
     296                 :          0 :                    nfsacl_desc.desc.array_len;
     297                 :            : }

Generated by: LCOV version 1.9