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