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