Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Common NFSv4 ACL handling code. |
| 3 | * |
| 4 | * Copyright (c) 2002, 2003 The Regents of the University of Michigan. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Marius Aamodt Eriksen <marius@umich.edu> |
| 8 | * Jeff Sedlak <jsedlak@umich.edu> |
| 9 | * J. Bruce Fields <bfields@umich.edu> |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * 3. Neither the name of the University nor the names of its |
| 21 | * contributors may be used to endorse or promote products derived |
| 22 | * from this software without specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 31 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/nfs_fs.h> |
Paul Gortmaker | afeacc8 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 39 | #include <linux/export.h> |
J. Bruce Fields | 3554116 | 2014-01-08 09:49:01 -0500 | [diff] [blame^] | 40 | #include "nfsd.h" |
J. Bruce Fields | 2ca72e1 | 2011-01-04 17:37:15 -0500 | [diff] [blame] | 41 | #include "acl.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | |
| 44 | /* mode bit translations: */ |
| 45 | #define NFS4_READ_MODE (NFS4_ACE_READ_DATA) |
| 46 | #define NFS4_WRITE_MODE (NFS4_ACE_WRITE_DATA | NFS4_ACE_APPEND_DATA) |
| 47 | #define NFS4_EXECUTE_MODE NFS4_ACE_EXECUTE |
| 48 | #define NFS4_ANYONE_MODE (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL | NFS4_ACE_SYNCHRONIZE) |
| 49 | #define NFS4_OWNER_MODE (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL) |
| 50 | |
| 51 | /* We don't support these bits; insist they be neither allowed nor denied */ |
| 52 | #define NFS4_MASK_UNSUPP (NFS4_ACE_DELETE | NFS4_ACE_WRITE_OWNER \ |
| 53 | | NFS4_ACE_READ_NAMED_ATTRS | NFS4_ACE_WRITE_NAMED_ATTRS) |
| 54 | |
| 55 | /* flags used to simulate posix default ACLs */ |
| 56 | #define NFS4_INHERITANCE_FLAGS (NFS4_ACE_FILE_INHERIT_ACE \ |
J. Bruce Fields | 7bdfa68 | 2007-02-16 01:28:28 -0800 | [diff] [blame] | 57 | | NFS4_ACE_DIRECTORY_INHERIT_ACE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
J. Bruce Fields | 7bdfa68 | 2007-02-16 01:28:28 -0800 | [diff] [blame] | 59 | #define NFS4_SUPPORTED_FLAGS (NFS4_INHERITANCE_FLAGS \ |
| 60 | | NFS4_ACE_INHERIT_ONLY_ACE \ |
| 61 | | NFS4_ACE_IDENTIFIER_GROUP) |
J.Bruce Fields | b548edc | 2006-10-04 02:16:12 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define MASK_EQUAL(mask1, mask2) \ |
| 64 | ( ((mask1) & NFS4_ACE_MASK_ALL) == ((mask2) & NFS4_ACE_MASK_ALL) ) |
| 65 | |
| 66 | static u32 |
| 67 | mask_from_posix(unsigned short perm, unsigned int flags) |
| 68 | { |
| 69 | int mask = NFS4_ANYONE_MODE; |
| 70 | |
| 71 | if (flags & NFS4_ACL_OWNER) |
| 72 | mask |= NFS4_OWNER_MODE; |
| 73 | if (perm & ACL_READ) |
| 74 | mask |= NFS4_READ_MODE; |
| 75 | if (perm & ACL_WRITE) |
| 76 | mask |= NFS4_WRITE_MODE; |
| 77 | if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR)) |
| 78 | mask |= NFS4_ACE_DELETE_CHILD; |
| 79 | if (perm & ACL_EXECUTE) |
| 80 | mask |= NFS4_EXECUTE_MODE; |
| 81 | return mask; |
| 82 | } |
| 83 | |
| 84 | static u32 |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 85 | deny_mask_from_posix(unsigned short perm, u32 flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 87 | u32 mask = 0; |
| 88 | |
| 89 | if (perm & ACL_READ) |
| 90 | mask |= NFS4_READ_MODE; |
| 91 | if (perm & ACL_WRITE) |
| 92 | mask |= NFS4_WRITE_MODE; |
| 93 | if ((perm & ACL_WRITE) && (flags & NFS4_ACL_DIR)) |
| 94 | mask |= NFS4_ACE_DELETE_CHILD; |
| 95 | if (perm & ACL_EXECUTE) |
| 96 | mask |= NFS4_EXECUTE_MODE; |
| 97 | return mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /* XXX: modify functions to return NFS errors; they're only ever |
| 101 | * used by nfs code, after all.... */ |
| 102 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 103 | /* We only map from NFSv4 to POSIX ACLs when setting ACLs, when we err on the |
| 104 | * side of being more restrictive, so the mode bit mapping below is |
| 105 | * pessimistic. An optimistic version would be needed to handle DENY's, |
| 106 | * but we espect to coalesce all ALLOWs and DENYs before mapping to mode |
| 107 | * bits. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 109 | static void |
| 110 | low_mode_from_nfs4(u32 perm, unsigned short *mode, unsigned int flags) |
| 111 | { |
| 112 | u32 write_mode = NFS4_WRITE_MODE; |
| 113 | |
| 114 | if (flags & NFS4_ACL_DIR) |
| 115 | write_mode |= NFS4_ACE_DELETE_CHILD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | *mode = 0; |
| 117 | if ((perm & NFS4_READ_MODE) == NFS4_READ_MODE) |
| 118 | *mode |= ACL_READ; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 119 | if ((perm & write_mode) == write_mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | *mode |= ACL_WRITE; |
| 121 | if ((perm & NFS4_EXECUTE_MODE) == NFS4_EXECUTE_MODE) |
| 122 | *mode |= ACL_EXECUTE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | struct ace_container { |
| 126 | struct nfs4_ace *ace; |
| 127 | struct list_head ace_l; |
| 128 | }; |
| 129 | |
| 130 | static short ace2type(struct nfs4_ace *); |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 131 | static void _posix_to_nfsv4_one(struct posix_acl *, struct nfs4_acl *, |
| 132 | unsigned int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | struct nfs4_acl * |
| 135 | nfs4_acl_posix_to_nfsv4(struct posix_acl *pacl, struct posix_acl *dpacl, |
| 136 | unsigned int flags) |
| 137 | { |
| 138 | struct nfs4_acl *acl; |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 139 | int size = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 141 | if (pacl) { |
| 142 | if (posix_acl_valid(pacl) < 0) |
| 143 | return ERR_PTR(-EINVAL); |
| 144 | size += 2*pacl->a_count; |
| 145 | } |
| 146 | if (dpacl) { |
| 147 | if (posix_acl_valid(dpacl) < 0) |
| 148 | return ERR_PTR(-EINVAL); |
| 149 | size += 2*dpacl->a_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 152 | /* Allocate for worst case: one (deny, allow) pair each: */ |
| 153 | acl = nfs4_acl_new(size); |
| 154 | if (acl == NULL) |
| 155 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 157 | if (pacl) |
| 158 | _posix_to_nfsv4_one(pacl, acl, flags & ~NFS4_ACL_TYPE_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 160 | if (dpacl) |
| 161 | _posix_to_nfsv4_one(dpacl, acl, flags | NFS4_ACL_TYPE_DEFAULT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | return acl; |
| 164 | } |
| 165 | |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 166 | struct posix_acl_summary { |
| 167 | unsigned short owner; |
| 168 | unsigned short users; |
| 169 | unsigned short group; |
| 170 | unsigned short groups; |
| 171 | unsigned short other; |
| 172 | unsigned short mask; |
| 173 | }; |
| 174 | |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 175 | static void |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 176 | summarize_posix_acl(struct posix_acl *acl, struct posix_acl_summary *pas) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | { |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 178 | struct posix_acl_entry *pa, *pe; |
J. Bruce Fields | f7fede4 | 2007-07-17 04:04:36 -0700 | [diff] [blame] | 179 | |
| 180 | /* |
| 181 | * Only pas.users and pas.groups need initialization; previous |
| 182 | * posix_acl_valid() calls ensure that the other fields will be |
| 183 | * initialized in the following loop. But, just to placate gcc: |
| 184 | */ |
| 185 | memset(pas, 0, sizeof(*pas)); |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 186 | pas->mask = 07; |
| 187 | |
| 188 | pe = acl->a_entries + acl->a_count; |
| 189 | |
| 190 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 191 | switch (pa->e_tag) { |
| 192 | case ACL_USER_OBJ: |
| 193 | pas->owner = pa->e_perm; |
| 194 | break; |
| 195 | case ACL_GROUP_OBJ: |
| 196 | pas->group = pa->e_perm; |
| 197 | break; |
| 198 | case ACL_USER: |
| 199 | pas->users |= pa->e_perm; |
| 200 | break; |
| 201 | case ACL_GROUP: |
| 202 | pas->groups |= pa->e_perm; |
| 203 | break; |
| 204 | case ACL_OTHER: |
| 205 | pas->other = pa->e_perm; |
| 206 | break; |
| 207 | case ACL_MASK: |
| 208 | pas->mask = pa->e_perm; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | /* We'll only care about effective permissions: */ |
| 213 | pas->users &= pas->mask; |
| 214 | pas->group &= pas->mask; |
| 215 | pas->groups &= pas->mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* We assume the acl has been verified with posix_acl_valid. */ |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 219 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | _posix_to_nfsv4_one(struct posix_acl *pacl, struct nfs4_acl *acl, |
| 221 | unsigned int flags) |
| 222 | { |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 223 | struct posix_acl_entry *pa, *group_owner_entry; |
| 224 | struct nfs4_ace *ace; |
| 225 | struct posix_acl_summary pas; |
| 226 | unsigned short deny; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | int eflag = ((flags & NFS4_ACL_TYPE_DEFAULT) ? |
Bruce Fields | 54c04409 | 2007-03-26 21:32:09 -0800 | [diff] [blame] | 228 | NFS4_INHERITANCE_FLAGS | NFS4_ACE_INHERIT_ONLY_ACE : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | BUG_ON(pacl->a_count < 3); |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 231 | summarize_posix_acl(pacl, &pas); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | pa = pacl->a_entries; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 234 | ace = acl->aces + acl->naces; |
| 235 | |
| 236 | /* We could deny everything not granted by the owner: */ |
| 237 | deny = ~pas.owner; |
| 238 | /* |
| 239 | * but it is equivalent (and simpler) to deny only what is not |
| 240 | * granted by later entries: |
| 241 | */ |
| 242 | deny &= pas.users | pas.group | pas.groups | pas.other; |
| 243 | if (deny) { |
| 244 | ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE; |
| 245 | ace->flag = eflag; |
| 246 | ace->access_mask = deny_mask_from_posix(deny, flags); |
| 247 | ace->whotype = NFS4_ACL_WHO_OWNER; |
| 248 | ace++; |
| 249 | acl->naces++; |
| 250 | } |
| 251 | |
| 252 | ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE; |
| 253 | ace->flag = eflag; |
| 254 | ace->access_mask = mask_from_posix(pa->e_perm, flags | NFS4_ACL_OWNER); |
| 255 | ace->whotype = NFS4_ACL_WHO_OWNER; |
| 256 | ace++; |
| 257 | acl->naces++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | pa++; |
| 259 | |
| 260 | while (pa->e_tag == ACL_USER) { |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 261 | deny = ~(pa->e_perm & pas.mask); |
| 262 | deny &= pas.groups | pas.group | pas.other; |
| 263 | if (deny) { |
| 264 | ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE; |
| 265 | ace->flag = eflag; |
| 266 | ace->access_mask = deny_mask_from_posix(deny, flags); |
| 267 | ace->whotype = NFS4_ACL_WHO_NAMED; |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 268 | ace->who_uid = pa->e_uid; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 269 | ace++; |
| 270 | acl->naces++; |
| 271 | } |
| 272 | ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE; |
| 273 | ace->flag = eflag; |
| 274 | ace->access_mask = mask_from_posix(pa->e_perm & pas.mask, |
| 275 | flags); |
| 276 | ace->whotype = NFS4_ACL_WHO_NAMED; |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 277 | ace->who_uid = pa->e_uid; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 278 | ace++; |
| 279 | acl->naces++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | pa++; |
| 281 | } |
| 282 | |
| 283 | /* In the case of groups, we apply allow ACEs first, then deny ACEs, |
| 284 | * since a user can be in more than one group. */ |
| 285 | |
| 286 | /* allow ACEs */ |
| 287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | group_owner_entry = pa; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 289 | |
| 290 | ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE; |
| 291 | ace->flag = eflag; |
| 292 | ace->access_mask = mask_from_posix(pas.group, flags); |
| 293 | ace->whotype = NFS4_ACL_WHO_GROUP; |
| 294 | ace++; |
| 295 | acl->naces++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | pa++; |
| 297 | |
| 298 | while (pa->e_tag == ACL_GROUP) { |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 299 | ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE; |
| 300 | ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP; |
| 301 | ace->access_mask = mask_from_posix(pa->e_perm & pas.mask, |
| 302 | flags); |
| 303 | ace->whotype = NFS4_ACL_WHO_NAMED; |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 304 | ace->who_gid = pa->e_gid; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 305 | ace++; |
| 306 | acl->naces++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | pa++; |
| 308 | } |
| 309 | |
| 310 | /* deny ACEs */ |
| 311 | |
| 312 | pa = group_owner_entry; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 313 | |
| 314 | deny = ~pas.group & pas.other; |
| 315 | if (deny) { |
| 316 | ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE; |
Frank Filz | d8d0b85 | 2009-08-27 17:35:41 -0400 | [diff] [blame] | 317 | ace->flag = eflag; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 318 | ace->access_mask = deny_mask_from_posix(deny, flags); |
| 319 | ace->whotype = NFS4_ACL_WHO_GROUP; |
| 320 | ace++; |
| 321 | acl->naces++; |
| 322 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | pa++; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | while (pa->e_tag == ACL_GROUP) { |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 326 | deny = ~(pa->e_perm & pas.mask); |
| 327 | deny &= pas.other; |
| 328 | if (deny) { |
| 329 | ace->type = NFS4_ACE_ACCESS_DENIED_ACE_TYPE; |
| 330 | ace->flag = eflag | NFS4_ACE_IDENTIFIER_GROUP; |
Frank Filz | 55bb55d | 2009-08-14 15:02:30 -0700 | [diff] [blame] | 331 | ace->access_mask = deny_mask_from_posix(deny, flags); |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 332 | ace->whotype = NFS4_ACL_WHO_NAMED; |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 333 | ace->who_gid = pa->e_gid; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 334 | ace++; |
| 335 | acl->naces++; |
| 336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | pa++; |
| 338 | } |
| 339 | |
| 340 | if (pa->e_tag == ACL_MASK) |
| 341 | pa++; |
J. Bruce Fields | bec50c4 | 2007-02-16 01:28:36 -0800 | [diff] [blame] | 342 | ace->type = NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE; |
| 343 | ace->flag = eflag; |
| 344 | ace->access_mask = mask_from_posix(pa->e_perm, flags); |
| 345 | ace->whotype = NFS4_ACL_WHO_EVERYONE; |
| 346 | acl->naces++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 349 | static bool |
| 350 | pace_gt(struct posix_acl_entry *pace1, struct posix_acl_entry *pace2) |
| 351 | { |
| 352 | if (pace1->e_tag != pace2->e_tag) |
| 353 | return pace1->e_tag > pace2->e_tag; |
| 354 | if (pace1->e_tag == ACL_USER) |
| 355 | return uid_gt(pace1->e_uid, pace2->e_uid); |
| 356 | if (pace1->e_tag == ACL_GROUP) |
| 357 | return gid_gt(pace1->e_gid, pace2->e_gid); |
| 358 | return false; |
| 359 | } |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | static void |
| 362 | sort_pacl_range(struct posix_acl *pacl, int start, int end) { |
| 363 | int sorted = 0, i; |
| 364 | struct posix_acl_entry tmp; |
| 365 | |
| 366 | /* We just do a bubble sort; easy to do in place, and we're not |
| 367 | * expecting acl's to be long enough to justify anything more. */ |
| 368 | while (!sorted) { |
| 369 | sorted = 1; |
| 370 | for (i = start; i < end; i++) { |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 371 | if (pace_gt(&pacl->a_entries[i], |
| 372 | &pacl->a_entries[i+1])) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | sorted = 0; |
| 374 | tmp = pacl->a_entries[i]; |
| 375 | pacl->a_entries[i] = pacl->a_entries[i+1]; |
| 376 | pacl->a_entries[i+1] = tmp; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | sort_pacl(struct posix_acl *pacl) |
| 384 | { |
| 385 | /* posix_acl_valid requires that users and groups be in order |
| 386 | * by uid/gid. */ |
| 387 | int i, j; |
| 388 | |
| 389 | if (pacl->a_count <= 4) |
| 390 | return; /* no users or groups */ |
| 391 | i = 1; |
| 392 | while (pacl->a_entries[i].e_tag == ACL_USER) |
| 393 | i++; |
| 394 | sort_pacl_range(pacl, 1, i-1); |
| 395 | |
| 396 | BUG_ON(pacl->a_entries[i].e_tag != ACL_GROUP_OBJ); |
Frank Filz | aba24d7 | 2009-10-21 16:45:02 -0700 | [diff] [blame] | 397 | j = ++i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | while (pacl->a_entries[j].e_tag == ACL_GROUP) |
| 399 | j++; |
| 400 | sort_pacl_range(pacl, i, j-1); |
| 401 | return; |
| 402 | } |
| 403 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 404 | /* |
| 405 | * While processing the NFSv4 ACE, this maintains bitmasks representing |
| 406 | * which permission bits have been allowed and which denied to a given |
| 407 | * entity: */ |
| 408 | struct posix_ace_state { |
| 409 | u32 allow; |
| 410 | u32 deny; |
| 411 | }; |
| 412 | |
| 413 | struct posix_user_ace_state { |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 414 | union { |
| 415 | kuid_t uid; |
| 416 | kgid_t gid; |
| 417 | }; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 418 | struct posix_ace_state perms; |
| 419 | }; |
| 420 | |
| 421 | struct posix_ace_state_array { |
| 422 | int n; |
| 423 | struct posix_user_ace_state aces[]; |
| 424 | }; |
| 425 | |
| 426 | /* |
| 427 | * While processing the NFSv4 ACE, this maintains the partial permissions |
| 428 | * calculated so far: */ |
| 429 | |
| 430 | struct posix_acl_state { |
J. Bruce Fields | 3160a71 | 2007-02-16 01:28:37 -0800 | [diff] [blame] | 431 | int empty; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 432 | struct posix_ace_state owner; |
| 433 | struct posix_ace_state group; |
| 434 | struct posix_ace_state other; |
| 435 | struct posix_ace_state everyone; |
| 436 | struct posix_ace_state mask; /* Deny unused in this case */ |
| 437 | struct posix_ace_state_array *users; |
| 438 | struct posix_ace_state_array *groups; |
| 439 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | |
| 441 | static int |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 442 | init_state(struct posix_acl_state *state, int cnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 444 | int alloc; |
| 445 | |
| 446 | memset(state, 0, sizeof(struct posix_acl_state)); |
J. Bruce Fields | 3160a71 | 2007-02-16 01:28:37 -0800 | [diff] [blame] | 447 | state->empty = 1; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 448 | /* |
| 449 | * In the worst case, each individual acl could be for a distinct |
| 450 | * named user or group, but we don't no which, so we allocate |
| 451 | * enough space for either: |
| 452 | */ |
| 453 | alloc = sizeof(struct posix_ace_state_array) |
J. Bruce Fields | 91b8096 | 2008-08-29 19:18:45 -0400 | [diff] [blame] | 454 | + cnt*sizeof(struct posix_user_ace_state); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 455 | state->users = kzalloc(alloc, GFP_KERNEL); |
| 456 | if (!state->users) |
| 457 | return -ENOMEM; |
| 458 | state->groups = kzalloc(alloc, GFP_KERNEL); |
| 459 | if (!state->groups) { |
| 460 | kfree(state->users); |
| 461 | return -ENOMEM; |
| 462 | } |
| 463 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 466 | static void |
| 467 | free_state(struct posix_acl_state *state) { |
| 468 | kfree(state->users); |
| 469 | kfree(state->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | } |
| 471 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 472 | static inline void add_to_mask(struct posix_acl_state *state, struct posix_ace_state *astate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 474 | state->mask.allow |= astate->allow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | } |
| 476 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 477 | /* |
| 478 | * Certain bits (SYNCHRONIZE, DELETE, WRITE_OWNER, READ/WRITE_NAMED_ATTRS, |
| 479 | * READ_ATTRIBUTES, READ_ACL) are currently unenforceable and don't translate |
| 480 | * to traditional read/write/execute permissions. |
| 481 | * |
| 482 | * It's problematic to reject acls that use certain mode bits, because it |
| 483 | * places the burden on users to learn the rules about which bits one |
| 484 | * particular server sets, without giving the user a lot of help--we return an |
| 485 | * error that could mean any number of different things. To make matters |
| 486 | * worse, the problematic bits might be introduced by some application that's |
| 487 | * automatically mapping from some other acl model. |
| 488 | * |
| 489 | * So wherever possible we accept anything, possibly erring on the side of |
| 490 | * denying more permissions than necessary. |
| 491 | * |
| 492 | * However we do reject *explicit* DENY's of a few bits representing |
| 493 | * permissions we could never deny: |
| 494 | */ |
| 495 | |
| 496 | static inline int check_deny(u32 mask, int isowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 498 | if (mask & (NFS4_ACE_READ_ATTRIBUTES | NFS4_ACE_READ_ACL)) |
| 499 | return -EINVAL; |
| 500 | if (!isowner) |
| 501 | return 0; |
| 502 | if (mask & (NFS4_ACE_WRITE_ATTRIBUTES | NFS4_ACE_WRITE_ACL)) |
| 503 | return -EINVAL; |
| 504 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | } |
| 506 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 507 | static struct posix_acl * |
| 508 | posix_state_to_acl(struct posix_acl_state *state, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 510 | struct posix_acl_entry *pace; |
| 511 | struct posix_acl *pacl; |
| 512 | int nace; |
| 513 | int i, error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
J. Bruce Fields | 3160a71 | 2007-02-16 01:28:37 -0800 | [diff] [blame] | 515 | /* |
| 516 | * ACLs with no ACEs are treated differently in the inheritable |
| 517 | * and effective cases: when there are no inheritable ACEs, we |
| 518 | * set a zero-length default posix acl: |
| 519 | */ |
| 520 | if (state->empty && (flags & NFS4_ACL_TYPE_DEFAULT)) { |
| 521 | pacl = posix_acl_alloc(0, GFP_KERNEL); |
| 522 | return pacl ? pacl : ERR_PTR(-ENOMEM); |
| 523 | } |
| 524 | /* |
| 525 | * When there are no effective ACEs, the following will end |
| 526 | * up setting a 3-element effective posix ACL with all |
| 527 | * permissions zero. |
| 528 | */ |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 529 | nace = 4 + state->users->n + state->groups->n; |
| 530 | pacl = posix_acl_alloc(nace, GFP_KERNEL); |
| 531 | if (!pacl) |
| 532 | return ERR_PTR(-ENOMEM); |
| 533 | |
| 534 | pace = pacl->a_entries; |
| 535 | pace->e_tag = ACL_USER_OBJ; |
| 536 | error = check_deny(state->owner.deny, 1); |
| 537 | if (error) |
| 538 | goto out_err; |
| 539 | low_mode_from_nfs4(state->owner.allow, &pace->e_perm, flags); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 540 | |
| 541 | for (i=0; i < state->users->n; i++) { |
| 542 | pace++; |
| 543 | pace->e_tag = ACL_USER; |
| 544 | error = check_deny(state->users->aces[i].perms.deny, 0); |
| 545 | if (error) |
| 546 | goto out_err; |
| 547 | low_mode_from_nfs4(state->users->aces[i].perms.allow, |
| 548 | &pace->e_perm, flags); |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 549 | pace->e_uid = state->users->aces[i].uid; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 550 | add_to_mask(state, &state->users->aces[i].perms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | } |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 552 | |
| 553 | pace++; |
| 554 | pace->e_tag = ACL_GROUP_OBJ; |
| 555 | error = check_deny(state->group.deny, 0); |
| 556 | if (error) |
| 557 | goto out_err; |
| 558 | low_mode_from_nfs4(state->group.allow, &pace->e_perm, flags); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 559 | add_to_mask(state, &state->group); |
| 560 | |
| 561 | for (i=0; i < state->groups->n; i++) { |
| 562 | pace++; |
| 563 | pace->e_tag = ACL_GROUP; |
| 564 | error = check_deny(state->groups->aces[i].perms.deny, 0); |
| 565 | if (error) |
| 566 | goto out_err; |
| 567 | low_mode_from_nfs4(state->groups->aces[i].perms.allow, |
| 568 | &pace->e_perm, flags); |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 569 | pace->e_gid = state->groups->aces[i].gid; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 570 | add_to_mask(state, &state->groups->aces[i].perms); |
| 571 | } |
| 572 | |
| 573 | pace++; |
| 574 | pace->e_tag = ACL_MASK; |
| 575 | low_mode_from_nfs4(state->mask.allow, &pace->e_perm, flags); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 576 | |
| 577 | pace++; |
| 578 | pace->e_tag = ACL_OTHER; |
| 579 | error = check_deny(state->other.deny, 0); |
| 580 | if (error) |
| 581 | goto out_err; |
| 582 | low_mode_from_nfs4(state->other.allow, &pace->e_perm, flags); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 583 | |
| 584 | return pacl; |
| 585 | out_err: |
| 586 | posix_acl_release(pacl); |
| 587 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | } |
| 589 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 590 | static inline void allow_bits(struct posix_ace_state *astate, u32 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 592 | /* Allow all bits in the mask not already denied: */ |
| 593 | astate->allow |= mask & ~astate->deny; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
| 595 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 596 | static inline void deny_bits(struct posix_ace_state *astate, u32 mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 598 | /* Deny all bits in the mask not already allowed: */ |
| 599 | astate->deny |= mask & ~astate->allow; |
| 600 | } |
| 601 | |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 602 | static int find_uid(struct posix_acl_state *state, kuid_t uid) |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 603 | { |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 604 | struct posix_ace_state_array *a = state->users; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 605 | int i; |
| 606 | |
| 607 | for (i = 0; i < a->n; i++) |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 608 | if (uid_eq(a->aces[i].uid, uid)) |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 609 | return i; |
| 610 | /* Not found: */ |
| 611 | a->n++; |
| 612 | a->aces[i].uid = uid; |
| 613 | a->aces[i].perms.allow = state->everyone.allow; |
| 614 | a->aces[i].perms.deny = state->everyone.deny; |
| 615 | |
| 616 | return i; |
| 617 | } |
| 618 | |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 619 | static int find_gid(struct posix_acl_state *state, kgid_t gid) |
| 620 | { |
| 621 | struct posix_ace_state_array *a = state->groups; |
| 622 | int i; |
| 623 | |
| 624 | for (i = 0; i < a->n; i++) |
| 625 | if (gid_eq(a->aces[i].gid, gid)) |
| 626 | return i; |
| 627 | /* Not found: */ |
| 628 | a->n++; |
| 629 | a->aces[i].gid = gid; |
| 630 | a->aces[i].perms.allow = state->everyone.allow; |
| 631 | a->aces[i].perms.deny = state->everyone.deny; |
| 632 | |
| 633 | return i; |
| 634 | } |
| 635 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 636 | static void deny_bits_array(struct posix_ace_state_array *a, u32 mask) |
| 637 | { |
| 638 | int i; |
| 639 | |
| 640 | for (i=0; i < a->n; i++) |
| 641 | deny_bits(&a->aces[i].perms, mask); |
| 642 | } |
| 643 | |
| 644 | static void allow_bits_array(struct posix_ace_state_array *a, u32 mask) |
| 645 | { |
| 646 | int i; |
| 647 | |
| 648 | for (i=0; i < a->n; i++) |
| 649 | allow_bits(&a->aces[i].perms, mask); |
| 650 | } |
| 651 | |
| 652 | static void process_one_v4_ace(struct posix_acl_state *state, |
| 653 | struct nfs4_ace *ace) |
| 654 | { |
| 655 | u32 mask = ace->access_mask; |
| 656 | int i; |
| 657 | |
J. Bruce Fields | 3160a71 | 2007-02-16 01:28:37 -0800 | [diff] [blame] | 658 | state->empty = 0; |
| 659 | |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 660 | switch (ace2type(ace)) { |
| 661 | case ACL_USER_OBJ: |
| 662 | if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) { |
| 663 | allow_bits(&state->owner, mask); |
| 664 | } else { |
| 665 | deny_bits(&state->owner, mask); |
| 666 | } |
| 667 | break; |
| 668 | case ACL_USER: |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 669 | i = find_uid(state, ace->who_uid); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 670 | if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) { |
| 671 | allow_bits(&state->users->aces[i].perms, mask); |
| 672 | } else { |
| 673 | deny_bits(&state->users->aces[i].perms, mask); |
| 674 | mask = state->users->aces[i].perms.deny; |
| 675 | deny_bits(&state->owner, mask); |
| 676 | } |
| 677 | break; |
| 678 | case ACL_GROUP_OBJ: |
| 679 | if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) { |
| 680 | allow_bits(&state->group, mask); |
| 681 | } else { |
| 682 | deny_bits(&state->group, mask); |
| 683 | mask = state->group.deny; |
| 684 | deny_bits(&state->owner, mask); |
| 685 | deny_bits(&state->everyone, mask); |
| 686 | deny_bits_array(state->users, mask); |
| 687 | deny_bits_array(state->groups, mask); |
| 688 | } |
| 689 | break; |
| 690 | case ACL_GROUP: |
Eric W. Biederman | ab8e4ae | 2013-02-02 05:18:08 -0800 | [diff] [blame] | 691 | i = find_gid(state, ace->who_gid); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 692 | if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) { |
| 693 | allow_bits(&state->groups->aces[i].perms, mask); |
| 694 | } else { |
| 695 | deny_bits(&state->groups->aces[i].perms, mask); |
| 696 | mask = state->groups->aces[i].perms.deny; |
| 697 | deny_bits(&state->owner, mask); |
| 698 | deny_bits(&state->group, mask); |
| 699 | deny_bits(&state->everyone, mask); |
| 700 | deny_bits_array(state->users, mask); |
| 701 | deny_bits_array(state->groups, mask); |
| 702 | } |
| 703 | break; |
| 704 | case ACL_OTHER: |
| 705 | if (ace->type == NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE) { |
| 706 | allow_bits(&state->owner, mask); |
| 707 | allow_bits(&state->group, mask); |
| 708 | allow_bits(&state->other, mask); |
| 709 | allow_bits(&state->everyone, mask); |
| 710 | allow_bits_array(state->users, mask); |
| 711 | allow_bits_array(state->groups, mask); |
| 712 | } else { |
| 713 | deny_bits(&state->owner, mask); |
| 714 | deny_bits(&state->group, mask); |
| 715 | deny_bits(&state->other, mask); |
| 716 | deny_bits(&state->everyone, mask); |
| 717 | deny_bits_array(state->users, mask); |
| 718 | deny_bits_array(state->groups, mask); |
| 719 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 723 | int nfs4_acl_nfsv4_to_posix(struct nfs4_acl *acl, struct posix_acl **pacl, |
| 724 | struct posix_acl **dpacl, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 726 | struct posix_acl_state effective_acl_state, default_acl_state; |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 727 | struct nfs4_ace *ace; |
| 728 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 730 | ret = init_state(&effective_acl_state, acl->naces); |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 731 | if (ret) |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 732 | return ret; |
| 733 | ret = init_state(&default_acl_state, acl->naces); |
| 734 | if (ret) |
| 735 | goto out_estate; |
| 736 | ret = -EINVAL; |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 737 | for (ace = acl->aces; ace < acl->aces + acl->naces; ace++) { |
J.Bruce Fields | 09229ed | 2006-10-04 02:16:11 -0700 | [diff] [blame] | 738 | if (ace->type != NFS4_ACE_ACCESS_ALLOWED_ACE_TYPE && |
| 739 | ace->type != NFS4_ACE_ACCESS_DENIED_ACE_TYPE) |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 740 | goto out_dstate; |
J.Bruce Fields | b548edc | 2006-10-04 02:16:12 -0700 | [diff] [blame] | 741 | if (ace->flag & ~NFS4_SUPPORTED_FLAGS) |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 742 | goto out_dstate; |
J. Bruce Fields | 7bdfa68 | 2007-02-16 01:28:28 -0800 | [diff] [blame] | 743 | if ((ace->flag & NFS4_INHERITANCE_FLAGS) == 0) { |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 744 | process_one_v4_ace(&effective_acl_state, ace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | continue; |
J. Bruce Fields | 7bdfa68 | 2007-02-16 01:28:28 -0800 | [diff] [blame] | 746 | } |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 747 | if (!(flags & NFS4_ACL_DIR)) |
| 748 | goto out_dstate; |
J. Bruce Fields | 7bdfa68 | 2007-02-16 01:28:28 -0800 | [diff] [blame] | 749 | /* |
| 750 | * Note that when only one of FILE_INHERIT or DIRECTORY_INHERIT |
| 751 | * is set, we're effectively turning on the other. That's OK, |
| 752 | * according to rfc 3530. |
| 753 | */ |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 754 | process_one_v4_ace(&default_acl_state, ace); |
| 755 | |
| 756 | if (!(ace->flag & NFS4_ACE_INHERIT_ONLY_ACE)) |
| 757 | process_one_v4_ace(&effective_acl_state, ace); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 759 | *pacl = posix_state_to_acl(&effective_acl_state, flags); |
| 760 | if (IS_ERR(*pacl)) { |
| 761 | ret = PTR_ERR(*pacl); |
J. Bruce Fields | 4b2ca38 | 2007-07-17 04:04:37 -0700 | [diff] [blame] | 762 | *pacl = NULL; |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 763 | goto out_dstate; |
| 764 | } |
J. Bruce Fields | 3160a71 | 2007-02-16 01:28:37 -0800 | [diff] [blame] | 765 | *dpacl = posix_state_to_acl(&default_acl_state, |
| 766 | flags | NFS4_ACL_TYPE_DEFAULT); |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 767 | if (IS_ERR(*dpacl)) { |
| 768 | ret = PTR_ERR(*dpacl); |
J. Bruce Fields | 4b2ca38 | 2007-07-17 04:04:37 -0700 | [diff] [blame] | 769 | *dpacl = NULL; |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 770 | posix_acl_release(*pacl); |
J. Bruce Fields | 4b2ca38 | 2007-07-17 04:04:37 -0700 | [diff] [blame] | 771 | *pacl = NULL; |
J. Bruce Fields | 575a629 | 2007-02-16 01:28:29 -0800 | [diff] [blame] | 772 | goto out_dstate; |
| 773 | } |
| 774 | sort_pacl(*pacl); |
| 775 | sort_pacl(*dpacl); |
| 776 | ret = 0; |
| 777 | out_dstate: |
| 778 | free_state(&default_acl_state); |
| 779 | out_estate: |
| 780 | free_state(&effective_acl_state); |
| 781 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static short |
| 785 | ace2type(struct nfs4_ace *ace) |
| 786 | { |
| 787 | switch (ace->whotype) { |
| 788 | case NFS4_ACL_WHO_NAMED: |
| 789 | return (ace->flag & NFS4_ACE_IDENTIFIER_GROUP ? |
| 790 | ACL_GROUP : ACL_USER); |
| 791 | case NFS4_ACL_WHO_OWNER: |
| 792 | return ACL_USER_OBJ; |
| 793 | case NFS4_ACL_WHO_GROUP: |
| 794 | return ACL_GROUP_OBJ; |
| 795 | case NFS4_ACL_WHO_EVERYONE: |
| 796 | return ACL_OTHER; |
| 797 | } |
| 798 | BUG(); |
| 799 | return -1; |
| 800 | } |
| 801 | |
| 802 | EXPORT_SYMBOL(nfs4_acl_posix_to_nfsv4); |
| 803 | EXPORT_SYMBOL(nfs4_acl_nfsv4_to_posix); |
| 804 | |
| 805 | struct nfs4_acl * |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 806 | nfs4_acl_new(int n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | { |
| 808 | struct nfs4_acl *acl; |
| 809 | |
J. Bruce Fields | 28e05dd | 2007-02-16 01:28:30 -0800 | [diff] [blame] | 810 | acl = kmalloc(sizeof(*acl) + n*sizeof(struct nfs4_ace), GFP_KERNEL); |
| 811 | if (acl == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | acl->naces = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | return acl; |
| 815 | } |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | static struct { |
| 818 | char *string; |
| 819 | int stringlen; |
| 820 | int type; |
| 821 | } s2t_map[] = { |
| 822 | { |
| 823 | .string = "OWNER@", |
| 824 | .stringlen = sizeof("OWNER@") - 1, |
| 825 | .type = NFS4_ACL_WHO_OWNER, |
| 826 | }, |
| 827 | { |
| 828 | .string = "GROUP@", |
| 829 | .stringlen = sizeof("GROUP@") - 1, |
| 830 | .type = NFS4_ACL_WHO_GROUP, |
| 831 | }, |
| 832 | { |
| 833 | .string = "EVERYONE@", |
| 834 | .stringlen = sizeof("EVERYONE@") - 1, |
| 835 | .type = NFS4_ACL_WHO_EVERYONE, |
| 836 | }, |
| 837 | }; |
| 838 | |
| 839 | int |
| 840 | nfs4_acl_get_whotype(char *p, u32 len) |
| 841 | { |
| 842 | int i; |
| 843 | |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 844 | for (i = 0; i < ARRAY_SIZE(s2t_map); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | if (s2t_map[i].stringlen == len && |
| 846 | 0 == memcmp(s2t_map[i].string, p, len)) |
| 847 | return s2t_map[i].type; |
| 848 | } |
| 849 | return NFS4_ACL_WHO_NAMED; |
| 850 | } |
| 851 | |
J. Bruce Fields | 3554116 | 2014-01-08 09:49:01 -0500 | [diff] [blame^] | 852 | __be32 nfs4_acl_write_who(int who, __be32 **p, int *len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
| 854 | int i; |
J. Bruce Fields | 3554116 | 2014-01-08 09:49:01 -0500 | [diff] [blame^] | 855 | int bytes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 857 | for (i = 0; i < ARRAY_SIZE(s2t_map); i++) { |
J. Bruce Fields | 3554116 | 2014-01-08 09:49:01 -0500 | [diff] [blame^] | 858 | if (s2t_map[i].type != who) |
| 859 | continue; |
| 860 | bytes = 4 + (XDR_QUADLEN(s2t_map[i].stringlen) << 2); |
| 861 | if (bytes > *len) |
| 862 | return nfserr_resource; |
| 863 | *p = xdr_encode_opaque(*p, s2t_map[i].string, |
| 864 | s2t_map[i].stringlen); |
| 865 | *len -= bytes; |
| 866 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | } |
J. Bruce Fields | 3554116 | 2014-01-08 09:49:01 -0500 | [diff] [blame^] | 868 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | return -1; |
| 870 | } |
| 871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | EXPORT_SYMBOL(nfs4_acl_new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | EXPORT_SYMBOL(nfs4_acl_get_whotype); |
| 874 | EXPORT_SYMBOL(nfs4_acl_write_who); |