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