Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 2 | * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 4 | * Fixes from William Schumacher incorporated on 15 March 2001. |
| 5 | * (Reported by Charles Bertsch, <CBertsch@microtest.com>). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This file contains generic functions for manipulating |
| 10 | * POSIX 1003.1e draft standard 17 ACLs. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/slab.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 15 | #include <linux/atomic.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/fs.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/posix_acl.h> |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 19 | #include <linux/posix_acl_xattr.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 20 | #include <linux/export.h> |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 21 | #include <linux/user_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 23 | EXPORT_SYMBOL(posix_acl_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | EXPORT_SYMBOL(posix_acl_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | EXPORT_SYMBOL(posix_acl_valid); |
| 26 | EXPORT_SYMBOL(posix_acl_equiv_mode); |
| 27 | EXPORT_SYMBOL(posix_acl_from_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Christoph Hellwig | 2982baa | 2013-12-20 05:16:38 -0800 | [diff] [blame] | 29 | struct posix_acl *get_acl(struct inode *inode, int type) |
| 30 | { |
| 31 | struct posix_acl *acl; |
| 32 | |
| 33 | acl = get_cached_acl(inode, type); |
| 34 | if (acl != ACL_NOT_CACHED) |
| 35 | return acl; |
| 36 | |
| 37 | if (!IS_POSIXACL(inode)) |
| 38 | return NULL; |
| 39 | |
| 40 | /* |
| 41 | * A filesystem can force a ACL callback by just never filling the |
| 42 | * ACL cache. But normally you'd fill the cache either at inode |
| 43 | * instantiation time, or on the first ->get_acl call. |
| 44 | * |
| 45 | * If the filesystem doesn't have a get_acl() function at all, we'll |
| 46 | * just create the negative cache entry. |
| 47 | */ |
| 48 | if (!inode->i_op->get_acl) { |
| 49 | set_cached_acl(inode, type, NULL); |
| 50 | return NULL; |
| 51 | } |
| 52 | return inode->i_op->get_acl(inode, type); |
| 53 | } |
| 54 | EXPORT_SYMBOL(get_acl); |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | /* |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 57 | * Init a fresh posix_acl |
| 58 | */ |
| 59 | void |
| 60 | posix_acl_init(struct posix_acl *acl, int count) |
| 61 | { |
| 62 | atomic_set(&acl->a_refcount, 1); |
| 63 | acl->a_count = count; |
| 64 | } |
| 65 | |
| 66 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * Allocate a new ACL with the specified number of entries. |
| 68 | */ |
| 69 | struct posix_acl * |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 70 | posix_acl_alloc(int count, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | const size_t size = sizeof(struct posix_acl) + |
| 73 | count * sizeof(struct posix_acl_entry); |
| 74 | struct posix_acl *acl = kmalloc(size, flags); |
Chuck Lever | f61f6da | 2011-01-21 03:05:38 +0000 | [diff] [blame] | 75 | if (acl) |
| 76 | posix_acl_init(acl, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | return acl; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Clone an ACL. |
| 82 | */ |
Al Viro | edde854 | 2011-07-23 03:27:37 -0400 | [diff] [blame] | 83 | static struct posix_acl * |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 84 | posix_acl_clone(const struct posix_acl *acl, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | struct posix_acl *clone = NULL; |
| 87 | |
| 88 | if (acl) { |
| 89 | int size = sizeof(struct posix_acl) + acl->a_count * |
| 90 | sizeof(struct posix_acl_entry); |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 91 | clone = kmemdup(acl, size, flags); |
| 92 | if (clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | atomic_set(&clone->a_refcount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
| 95 | return clone; |
| 96 | } |
| 97 | |
| 98 | /* |
| 99 | * Check if an acl is valid. Returns 0 if it is, or -E... otherwise. |
| 100 | */ |
| 101 | int |
| 102 | posix_acl_valid(const struct posix_acl *acl) |
| 103 | { |
| 104 | const struct posix_acl_entry *pa, *pe; |
| 105 | int state = ACL_USER_OBJ; |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 106 | kuid_t prev_uid = INVALID_UID; |
| 107 | kgid_t prev_gid = INVALID_GID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | int needs_mask = 0; |
| 109 | |
| 110 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 111 | if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE)) |
| 112 | return -EINVAL; |
| 113 | switch (pa->e_tag) { |
| 114 | case ACL_USER_OBJ: |
| 115 | if (state == ACL_USER_OBJ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | state = ACL_USER; |
| 117 | break; |
| 118 | } |
| 119 | return -EINVAL; |
| 120 | |
| 121 | case ACL_USER: |
| 122 | if (state != ACL_USER) |
| 123 | return -EINVAL; |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 124 | if (!uid_valid(pa->e_uid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | return -EINVAL; |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 126 | if (uid_valid(prev_uid) && |
| 127 | uid_lte(pa->e_uid, prev_uid)) |
| 128 | return -EINVAL; |
| 129 | prev_uid = pa->e_uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | needs_mask = 1; |
| 131 | break; |
| 132 | |
| 133 | case ACL_GROUP_OBJ: |
| 134 | if (state == ACL_USER) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | state = ACL_GROUP; |
| 136 | break; |
| 137 | } |
| 138 | return -EINVAL; |
| 139 | |
| 140 | case ACL_GROUP: |
| 141 | if (state != ACL_GROUP) |
| 142 | return -EINVAL; |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 143 | if (!gid_valid(pa->e_gid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | return -EINVAL; |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 145 | if (gid_valid(prev_gid) && |
| 146 | gid_lte(pa->e_gid, prev_gid)) |
| 147 | return -EINVAL; |
| 148 | prev_gid = pa->e_gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | needs_mask = 1; |
| 150 | break; |
| 151 | |
| 152 | case ACL_MASK: |
| 153 | if (state != ACL_GROUP) |
| 154 | return -EINVAL; |
| 155 | state = ACL_OTHER; |
| 156 | break; |
| 157 | |
| 158 | case ACL_OTHER: |
| 159 | if (state == ACL_OTHER || |
| 160 | (state == ACL_GROUP && !needs_mask)) { |
| 161 | state = 0; |
| 162 | break; |
| 163 | } |
| 164 | return -EINVAL; |
| 165 | |
| 166 | default: |
| 167 | return -EINVAL; |
| 168 | } |
| 169 | } |
| 170 | if (state == 0) |
| 171 | return 0; |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Returns 0 if the acl can be exactly represented in the traditional |
| 177 | * file mode permission bits, or else 1. Returns -E... on error. |
| 178 | */ |
| 179 | int |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 180 | posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | { |
| 182 | const struct posix_acl_entry *pa, *pe; |
Al Viro | d695212 | 2011-07-23 18:56:36 -0400 | [diff] [blame] | 183 | umode_t mode = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int not_equiv = 0; |
| 185 | |
| 186 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 187 | switch (pa->e_tag) { |
| 188 | case ACL_USER_OBJ: |
| 189 | mode |= (pa->e_perm & S_IRWXO) << 6; |
| 190 | break; |
| 191 | case ACL_GROUP_OBJ: |
| 192 | mode |= (pa->e_perm & S_IRWXO) << 3; |
| 193 | break; |
| 194 | case ACL_OTHER: |
| 195 | mode |= pa->e_perm & S_IRWXO; |
| 196 | break; |
| 197 | case ACL_MASK: |
| 198 | mode = (mode & ~S_IRWXG) | |
| 199 | ((pa->e_perm & S_IRWXO) << 3); |
| 200 | not_equiv = 1; |
| 201 | break; |
| 202 | case ACL_USER: |
| 203 | case ACL_GROUP: |
| 204 | not_equiv = 1; |
| 205 | break; |
| 206 | default: |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | } |
| 210 | if (mode_p) |
| 211 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; |
| 212 | return not_equiv; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Create an ACL representing the file mode permission bits of an inode. |
| 217 | */ |
| 218 | struct posix_acl * |
Al Viro | 3a5fba1 | 2011-07-23 19:01:48 -0400 | [diff] [blame] | 219 | posix_acl_from_mode(umode_t mode, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | struct posix_acl *acl = posix_acl_alloc(3, flags); |
| 222 | if (!acl) |
| 223 | return ERR_PTR(-ENOMEM); |
| 224 | |
| 225 | acl->a_entries[0].e_tag = ACL_USER_OBJ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6; |
| 227 | |
| 228 | acl->a_entries[1].e_tag = ACL_GROUP_OBJ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3; |
| 230 | |
| 231 | acl->a_entries[2].e_tag = ACL_OTHER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | acl->a_entries[2].e_perm = (mode & S_IRWXO); |
| 233 | return acl; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Return 0 if current is granted want access to the inode |
| 238 | * by the acl. Returns -E... otherwise. |
| 239 | */ |
| 240 | int |
| 241 | posix_acl_permission(struct inode *inode, const struct posix_acl *acl, int want) |
| 242 | { |
| 243 | const struct posix_acl_entry *pa, *pe, *mask_obj; |
| 244 | int found = 0; |
| 245 | |
Andreas Gruenbacher | d124b60 | 2011-10-23 23:13:32 +0530 | [diff] [blame] | 246 | want &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK; |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 249 | switch(pa->e_tag) { |
| 250 | case ACL_USER_OBJ: |
| 251 | /* (May have been checked already) */ |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 252 | if (uid_eq(inode->i_uid, current_fsuid())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | goto check_perm; |
| 254 | break; |
| 255 | case ACL_USER: |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 256 | if (uid_eq(pa->e_uid, current_fsuid())) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | goto mask; |
| 258 | break; |
| 259 | case ACL_GROUP_OBJ: |
| 260 | if (in_group_p(inode->i_gid)) { |
| 261 | found = 1; |
| 262 | if ((pa->e_perm & want) == want) |
| 263 | goto mask; |
| 264 | } |
| 265 | break; |
| 266 | case ACL_GROUP: |
Eric W. Biederman | 2f6f065 | 2012-02-07 18:52:57 -0800 | [diff] [blame] | 267 | if (in_group_p(pa->e_gid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | found = 1; |
| 269 | if ((pa->e_perm & want) == want) |
| 270 | goto mask; |
| 271 | } |
| 272 | break; |
| 273 | case ACL_MASK: |
| 274 | break; |
| 275 | case ACL_OTHER: |
| 276 | if (found) |
| 277 | return -EACCES; |
| 278 | else |
| 279 | goto check_perm; |
| 280 | default: |
| 281 | return -EIO; |
| 282 | } |
| 283 | } |
| 284 | return -EIO; |
| 285 | |
| 286 | mask: |
| 287 | for (mask_obj = pa+1; mask_obj != pe; mask_obj++) { |
| 288 | if (mask_obj->e_tag == ACL_MASK) { |
| 289 | if ((pa->e_perm & mask_obj->e_perm & want) == want) |
| 290 | return 0; |
| 291 | return -EACCES; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | check_perm: |
| 296 | if ((pa->e_perm & want) == want) |
| 297 | return 0; |
| 298 | return -EACCES; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Modify acl when creating a new inode. The caller must ensure the acl is |
| 303 | * only referenced once. |
| 304 | * |
| 305 | * mode_p initially must contain the mode parameter to the open() / creat() |
| 306 | * system calls. All permissions that are not granted by the acl are removed. |
| 307 | * The permissions in the acl are changed to reflect the mode_p parameter. |
| 308 | */ |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 309 | static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| 311 | struct posix_acl_entry *pa, *pe; |
| 312 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 313 | umode_t mode = *mode_p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | int not_equiv = 0; |
| 315 | |
| 316 | /* assert(atomic_read(acl->a_refcount) == 1); */ |
| 317 | |
| 318 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 319 | switch(pa->e_tag) { |
| 320 | case ACL_USER_OBJ: |
| 321 | pa->e_perm &= (mode >> 6) | ~S_IRWXO; |
| 322 | mode &= (pa->e_perm << 6) | ~S_IRWXU; |
| 323 | break; |
| 324 | |
| 325 | case ACL_USER: |
| 326 | case ACL_GROUP: |
| 327 | not_equiv = 1; |
| 328 | break; |
| 329 | |
| 330 | case ACL_GROUP_OBJ: |
| 331 | group_obj = pa; |
| 332 | break; |
| 333 | |
| 334 | case ACL_OTHER: |
| 335 | pa->e_perm &= mode | ~S_IRWXO; |
| 336 | mode &= pa->e_perm | ~S_IRWXO; |
| 337 | break; |
| 338 | |
| 339 | case ACL_MASK: |
| 340 | mask_obj = pa; |
| 341 | not_equiv = 1; |
| 342 | break; |
| 343 | |
| 344 | default: |
| 345 | return -EIO; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if (mask_obj) { |
| 350 | mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO; |
| 351 | mode &= (mask_obj->e_perm << 3) | ~S_IRWXG; |
| 352 | } else { |
| 353 | if (!group_obj) |
| 354 | return -EIO; |
| 355 | group_obj->e_perm &= (mode >> 3) | ~S_IRWXO; |
| 356 | mode &= (group_obj->e_perm << 3) | ~S_IRWXG; |
| 357 | } |
| 358 | |
| 359 | *mode_p = (*mode_p & ~S_IRWXUGO) | mode; |
| 360 | return not_equiv; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Modify the ACL for the chmod syscall. |
| 365 | */ |
Al Viro | 86bc704 | 2011-07-23 19:03:11 -0400 | [diff] [blame] | 366 | static int posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
| 368 | struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; |
| 369 | struct posix_acl_entry *pa, *pe; |
| 370 | |
| 371 | /* assert(atomic_read(acl->a_refcount) == 1); */ |
| 372 | |
| 373 | FOREACH_ACL_ENTRY(pa, acl, pe) { |
| 374 | switch(pa->e_tag) { |
| 375 | case ACL_USER_OBJ: |
| 376 | pa->e_perm = (mode & S_IRWXU) >> 6; |
| 377 | break; |
| 378 | |
| 379 | case ACL_USER: |
| 380 | case ACL_GROUP: |
| 381 | break; |
| 382 | |
| 383 | case ACL_GROUP_OBJ: |
| 384 | group_obj = pa; |
| 385 | break; |
| 386 | |
| 387 | case ACL_MASK: |
| 388 | mask_obj = pa; |
| 389 | break; |
| 390 | |
| 391 | case ACL_OTHER: |
| 392 | pa->e_perm = (mode & S_IRWXO); |
| 393 | break; |
| 394 | |
| 395 | default: |
| 396 | return -EIO; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | if (mask_obj) { |
| 401 | mask_obj->e_perm = (mode & S_IRWXG) >> 3; |
| 402 | } else { |
| 403 | if (!group_obj) |
| 404 | return -EIO; |
| 405 | group_obj->e_perm = (mode & S_IRWXG) >> 3; |
| 406 | } |
| 407 | |
| 408 | return 0; |
| 409 | } |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 410 | |
| 411 | int |
Al Viro | d3fb612 | 2011-07-23 18:37:50 -0400 | [diff] [blame] | 412 | posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) |
Al Viro | 826cae2 | 2011-07-23 03:10:32 -0400 | [diff] [blame] | 413 | { |
| 414 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); |
| 415 | int err = -ENOMEM; |
| 416 | if (clone) { |
| 417 | err = posix_acl_create_masq(clone, mode_p); |
| 418 | if (err < 0) { |
| 419 | posix_acl_release(clone); |
| 420 | clone = NULL; |
| 421 | } |
| 422 | } |
| 423 | posix_acl_release(*acl); |
| 424 | *acl = clone; |
| 425 | return err; |
| 426 | } |
| 427 | EXPORT_SYMBOL(posix_acl_create); |
| 428 | |
| 429 | int |
Al Viro | 86bc704 | 2011-07-23 19:03:11 -0400 | [diff] [blame] | 430 | posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) |
Al Viro | bc26ab5 | 2011-07-23 00:18:02 -0400 | [diff] [blame] | 431 | { |
| 432 | struct posix_acl *clone = posix_acl_clone(*acl, gfp); |
| 433 | int err = -ENOMEM; |
| 434 | if (clone) { |
| 435 | err = posix_acl_chmod_masq(clone, mode); |
| 436 | if (err) { |
| 437 | posix_acl_release(clone); |
| 438 | clone = NULL; |
| 439 | } |
| 440 | } |
| 441 | posix_acl_release(*acl); |
| 442 | *acl = clone; |
| 443 | return err; |
| 444 | } |
| 445 | EXPORT_SYMBOL(posix_acl_chmod); |
Christoph Hellwig | 5c8ebd5 | 2013-12-20 05:16:37 -0800 | [diff] [blame] | 446 | |
| 447 | /* |
| 448 | * Fix up the uids and gids in posix acl extended attributes in place. |
| 449 | */ |
| 450 | static void posix_acl_fix_xattr_userns( |
| 451 | struct user_namespace *to, struct user_namespace *from, |
| 452 | void *value, size_t size) |
| 453 | { |
| 454 | posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; |
| 455 | posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; |
| 456 | int count; |
| 457 | kuid_t uid; |
| 458 | kgid_t gid; |
| 459 | |
| 460 | if (!value) |
| 461 | return; |
| 462 | if (size < sizeof(posix_acl_xattr_header)) |
| 463 | return; |
| 464 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) |
| 465 | return; |
| 466 | |
| 467 | count = posix_acl_xattr_count(size); |
| 468 | if (count < 0) |
| 469 | return; |
| 470 | if (count == 0) |
| 471 | return; |
| 472 | |
| 473 | for (end = entry + count; entry != end; entry++) { |
| 474 | switch(le16_to_cpu(entry->e_tag)) { |
| 475 | case ACL_USER: |
| 476 | uid = make_kuid(from, le32_to_cpu(entry->e_id)); |
| 477 | entry->e_id = cpu_to_le32(from_kuid(to, uid)); |
| 478 | break; |
| 479 | case ACL_GROUP: |
| 480 | gid = make_kgid(from, le32_to_cpu(entry->e_id)); |
| 481 | entry->e_id = cpu_to_le32(from_kgid(to, gid)); |
| 482 | break; |
| 483 | default: |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void posix_acl_fix_xattr_from_user(void *value, size_t size) |
| 490 | { |
| 491 | struct user_namespace *user_ns = current_user_ns(); |
| 492 | if (user_ns == &init_user_ns) |
| 493 | return; |
| 494 | posix_acl_fix_xattr_userns(&init_user_ns, user_ns, value, size); |
| 495 | } |
| 496 | |
| 497 | void posix_acl_fix_xattr_to_user(void *value, size_t size) |
| 498 | { |
| 499 | struct user_namespace *user_ns = current_user_ns(); |
| 500 | if (user_ns == &init_user_ns) |
| 501 | return; |
| 502 | posix_acl_fix_xattr_userns(user_ns, &init_user_ns, value, size); |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Convert from extended attribute to in-memory representation. |
| 507 | */ |
| 508 | struct posix_acl * |
| 509 | posix_acl_from_xattr(struct user_namespace *user_ns, |
| 510 | const void *value, size_t size) |
| 511 | { |
| 512 | posix_acl_xattr_header *header = (posix_acl_xattr_header *)value; |
| 513 | posix_acl_xattr_entry *entry = (posix_acl_xattr_entry *)(header+1), *end; |
| 514 | int count; |
| 515 | struct posix_acl *acl; |
| 516 | struct posix_acl_entry *acl_e; |
| 517 | |
| 518 | if (!value) |
| 519 | return NULL; |
| 520 | if (size < sizeof(posix_acl_xattr_header)) |
| 521 | return ERR_PTR(-EINVAL); |
| 522 | if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) |
| 523 | return ERR_PTR(-EOPNOTSUPP); |
| 524 | |
| 525 | count = posix_acl_xattr_count(size); |
| 526 | if (count < 0) |
| 527 | return ERR_PTR(-EINVAL); |
| 528 | if (count == 0) |
| 529 | return NULL; |
| 530 | |
| 531 | acl = posix_acl_alloc(count, GFP_NOFS); |
| 532 | if (!acl) |
| 533 | return ERR_PTR(-ENOMEM); |
| 534 | acl_e = acl->a_entries; |
| 535 | |
| 536 | for (end = entry + count; entry != end; acl_e++, entry++) { |
| 537 | acl_e->e_tag = le16_to_cpu(entry->e_tag); |
| 538 | acl_e->e_perm = le16_to_cpu(entry->e_perm); |
| 539 | |
| 540 | switch(acl_e->e_tag) { |
| 541 | case ACL_USER_OBJ: |
| 542 | case ACL_GROUP_OBJ: |
| 543 | case ACL_MASK: |
| 544 | case ACL_OTHER: |
| 545 | break; |
| 546 | |
| 547 | case ACL_USER: |
| 548 | acl_e->e_uid = |
| 549 | make_kuid(user_ns, |
| 550 | le32_to_cpu(entry->e_id)); |
| 551 | if (!uid_valid(acl_e->e_uid)) |
| 552 | goto fail; |
| 553 | break; |
| 554 | case ACL_GROUP: |
| 555 | acl_e->e_gid = |
| 556 | make_kgid(user_ns, |
| 557 | le32_to_cpu(entry->e_id)); |
| 558 | if (!gid_valid(acl_e->e_gid)) |
| 559 | goto fail; |
| 560 | break; |
| 561 | |
| 562 | default: |
| 563 | goto fail; |
| 564 | } |
| 565 | } |
| 566 | return acl; |
| 567 | |
| 568 | fail: |
| 569 | posix_acl_release(acl); |
| 570 | return ERR_PTR(-EINVAL); |
| 571 | } |
| 572 | EXPORT_SYMBOL (posix_acl_from_xattr); |
| 573 | |
| 574 | /* |
| 575 | * Convert from in-memory to extended attribute representation. |
| 576 | */ |
| 577 | int |
| 578 | posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, |
| 579 | void *buffer, size_t size) |
| 580 | { |
| 581 | posix_acl_xattr_header *ext_acl = (posix_acl_xattr_header *)buffer; |
| 582 | posix_acl_xattr_entry *ext_entry = ext_acl->a_entries; |
| 583 | int real_size, n; |
| 584 | |
| 585 | real_size = posix_acl_xattr_size(acl->a_count); |
| 586 | if (!buffer) |
| 587 | return real_size; |
| 588 | if (real_size > size) |
| 589 | return -ERANGE; |
| 590 | |
| 591 | ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); |
| 592 | |
| 593 | for (n=0; n < acl->a_count; n++, ext_entry++) { |
| 594 | const struct posix_acl_entry *acl_e = &acl->a_entries[n]; |
| 595 | ext_entry->e_tag = cpu_to_le16(acl_e->e_tag); |
| 596 | ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); |
| 597 | switch(acl_e->e_tag) { |
| 598 | case ACL_USER: |
| 599 | ext_entry->e_id = |
| 600 | cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); |
| 601 | break; |
| 602 | case ACL_GROUP: |
| 603 | ext_entry->e_id = |
| 604 | cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); |
| 605 | break; |
| 606 | default: |
| 607 | ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); |
| 608 | break; |
| 609 | } |
| 610 | } |
| 611 | return real_size; |
| 612 | } |
| 613 | EXPORT_SYMBOL (posix_acl_to_xattr); |