1 /* 2 * linux/fs/ext4/acl.c 3 * 4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de> 5 */ 6 7 #include <linux/init.h> 8 #include <linux/sched.h> 9 #include <linux/slab.h> 10 #include <linux/capability.h> 11 #include <linux/fs.h> 12 #include "ext4_jbd2.h" 13 #include "ext4.h" 14 #include "xattr.h" 15 #include "acl.h" 16 17 /* 18 * Convert from filesystem to in-memory representation. 19 */ 20 static struct posix_acl * 21 ext4_acl_from_disk(const void *value, size_t size) 22 { 23 const char *end = (char *)value + size; 24 int n, count; 25 struct posix_acl *acl; 26 27 if (!value) 28 return NULL; 29 if (size < sizeof(ext4_acl_header)) 30 return ERR_PTR(-EINVAL); 31 if (((ext4_acl_header *)value)->a_version != 32 cpu_to_le32(EXT4_ACL_VERSION)) 33 return ERR_PTR(-EINVAL); 34 value = (char *)value + sizeof(ext4_acl_header); 35 count = ext4_acl_count(size); 36 if (count < 0) 37 return ERR_PTR(-EINVAL); 38 if (count == 0) 39 return NULL; 40 acl = posix_acl_alloc(count, GFP_NOFS); 41 if (!acl) 42 return ERR_PTR(-ENOMEM); 43 for (n = 0; n < count; n++) { 44 ext4_acl_entry *entry = 45 (ext4_acl_entry *)value; 46 if ((char *)value + sizeof(ext4_acl_entry_short) > end) 47 goto fail; 48 acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag); 49 acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm); 50 51 switch (acl->a_entries[n].e_tag) { 52 case ACL_USER_OBJ: 53 case ACL_GROUP_OBJ: 54 case ACL_MASK: 55 case ACL_OTHER: 56 value = (char *)value + 57 sizeof(ext4_acl_entry_short); 58 acl->a_entries[n].e_id = ACL_UNDEFINED_ID; 59 break; 60 61 case ACL_USER: 62 case ACL_GROUP: 63 value = (char *)value + sizeof(ext4_acl_entry); 64 if ((char *)value > end) 65 goto fail; 66 acl->a_entries[n].e_id = 67 le32_to_cpu(entry->e_id); 68 break; 69 70 default: 71 goto fail; 72 } 73 } 74 if (value != end) 75 goto fail; 76 return acl; 77 78 fail: 79 posix_acl_release(acl); 80 return ERR_PTR(-EINVAL); 81 } 82 83 /* 84 * Convert from in-memory to filesystem representation. 85 */ 86 static void * 87 ext4_acl_to_disk(const struct posix_acl *acl, size_t *size) 88 { 89 ext4_acl_header *ext_acl; 90 char *e; 91 size_t n; 92 93 *size = ext4_acl_size(acl->a_count); 94 ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count * 95 sizeof(ext4_acl_entry), GFP_NOFS); 96 if (!ext_acl) 97 return ERR_PTR(-ENOMEM); 98 ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION); 99 e = (char *)ext_acl + sizeof(ext4_acl_header); 100 for (n = 0; n < acl->a_count; n++) { 101 ext4_acl_entry *entry = (ext4_acl_entry *)e; 102 entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag); 103 entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm); 104 switch (acl->a_entries[n].e_tag) { 105 case ACL_USER: 106 case ACL_GROUP: 107 entry->e_id = cpu_to_le32(acl->a_entries[n].e_id); 108 e += sizeof(ext4_acl_entry); 109 break; 110 111 case ACL_USER_OBJ: 112 case ACL_GROUP_OBJ: 113 case ACL_MASK: 114 case ACL_OTHER: 115 e += sizeof(ext4_acl_entry_short); 116 break; 117 118 default: 119 goto fail; 120 } 121 } 122 return (char *)ext_acl; 123 124 fail: 125 kfree(ext_acl); 126 return ERR_PTR(-EINVAL); 127 } 128 129 static inline struct posix_acl * 130 ext4_iget_acl(struct inode *inode, struct posix_acl **i_acl) 131 { 132 struct posix_acl *acl = ACCESS_ONCE(*i_acl); 133 134 if (acl) { 135 spin_lock(&inode->i_lock); 136 acl = *i_acl; 137 if (acl != EXT4_ACL_NOT_CACHED) 138 acl = posix_acl_dup(acl); 139 spin_unlock(&inode->i_lock); 140 } 141 142 return acl; 143 } 144 145 static inline void 146 ext4_iset_acl(struct inode *inode, struct posix_acl **i_acl, 147 struct posix_acl *acl) 148 { 149 spin_lock(&inode->i_lock); 150 if (*i_acl != EXT4_ACL_NOT_CACHED) 151 posix_acl_release(*i_acl); 152 *i_acl = posix_acl_dup(acl); 153 spin_unlock(&inode->i_lock); 154 } 155 156 /* 157 * Inode operation get_posix_acl(). 158 * 159 * inode->i_mutex: don't care 160 */ 161 static struct posix_acl * 162 ext4_get_acl(struct inode *inode, int type) 163 { 164 struct ext4_inode_info *ei = EXT4_I(inode); 165 int name_index; 166 char *value = NULL; 167 struct posix_acl *acl; 168 int retval; 169 170 if (!test_opt(inode->i_sb, POSIX_ACL)) 171 return NULL; 172 173 switch (type) { 174 case ACL_TYPE_ACCESS: 175 acl = ext4_iget_acl(inode, &ei->i_acl); 176 if (acl != EXT4_ACL_NOT_CACHED) 177 return acl; 178 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; 179 break; 180 181 case ACL_TYPE_DEFAULT: 182 acl = ext4_iget_acl(inode, &ei->i_default_acl); 183 if (acl != EXT4_ACL_NOT_CACHED) 184 return acl; 185 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; 186 break; 187 188 default: 189 return ERR_PTR(-EINVAL); 190 } 191 retval = ext4_xattr_get(inode, name_index, "", NULL, 0); 192 if (retval > 0) { 193 value = kmalloc(retval, GFP_NOFS); 194 if (!value) 195 return ERR_PTR(-ENOMEM); 196 retval = ext4_xattr_get(inode, name_index, "", value, retval); 197 } 198 if (retval > 0) 199 acl = ext4_acl_from_disk(value, retval); 200 else if (retval == -ENODATA || retval == -ENOSYS) 201 acl = NULL; 202 else 203 acl = ERR_PTR(retval); 204 kfree(value); 205 206 if (!IS_ERR(acl)) { 207 switch (type) { 208 case ACL_TYPE_ACCESS: 209 ext4_iset_acl(inode, &ei->i_acl, acl); 210 break; 211 212 case ACL_TYPE_DEFAULT: 213 ext4_iset_acl(inode, &ei->i_default_acl, acl); 214 break; 215 } 216 } 217 return acl; 218 } 219 220 /* 221 * Set the access or default ACL of an inode. 222 * 223 * inode->i_mutex: down unless called from ext4_new_inode 224 */ 225 static int 226 ext4_set_acl(handle_t *handle, struct inode *inode, int type, 227 struct posix_acl *acl) 228 { 229 struct ext4_inode_info *ei = EXT4_I(inode); 230 int name_index; 231 void *value = NULL; 232 size_t size = 0; 233 int error; 234 235 if (S_ISLNK(inode->i_mode)) 236 return -EOPNOTSUPP; 237 238 switch (type) { 239 case ACL_TYPE_ACCESS: 240 name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS; 241 if (acl) { 242 mode_t mode = inode->i_mode; 243 error = posix_acl_equiv_mode(acl, &mode); 244 if (error < 0) 245 return error; 246 else { 247 inode->i_mode = mode; 248 ext4_mark_inode_dirty(handle, inode); 249 if (error == 0) 250 acl = NULL; 251 } 252 } 253 break; 254 255 case ACL_TYPE_DEFAULT: 256 name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT; 257 if (!S_ISDIR(inode->i_mode)) 258 return acl ? -EACCES : 0; 259 break; 260 261 default: 262 return -EINVAL; 263 } 264 if (acl) { 265 value = ext4_acl_to_disk(acl, &size); 266 if (IS_ERR(value)) 267 return (int)PTR_ERR(value); 268 } 269 270 error = ext4_xattr_set_handle(handle, inode, name_index, "", 271 value, size, 0); 272 273 kfree(value); 274 if (!error) { 275 switch (type) { 276 case ACL_TYPE_ACCESS: 277 ext4_iset_acl(inode, &ei->i_acl, acl); 278 break; 279 280 case ACL_TYPE_DEFAULT: 281 ext4_iset_acl(inode, &ei->i_default_acl, acl); 282 break; 283 } 284 } 285 return error; 286 } 287 288 static int 289 ext4_check_acl(struct inode *inode, int mask) 290 { 291 struct posix_acl *acl = ext4_get_acl(inode, ACL_TYPE_ACCESS); 292 293 if (IS_ERR(acl)) 294 return PTR_ERR(acl); 295 if (acl) { 296 int error = posix_acl_permission(inode, acl, mask); 297 posix_acl_release(acl); 298 return error; 299 } 300 301 return -EAGAIN; 302 } 303 304 int 305 ext4_permission(struct inode *inode, int mask) 306 { 307 return generic_permission(inode, mask, ext4_check_acl); 308 } 309 310 /* 311 * Initialize the ACLs of a new inode. Called from ext4_new_inode. 312 * 313 * dir->i_mutex: down 314 * inode->i_mutex: up (access to inode is still exclusive) 315 */ 316 int 317 ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir) 318 { 319 struct posix_acl *acl = NULL; 320 int error = 0; 321 322 if (!S_ISLNK(inode->i_mode)) { 323 if (test_opt(dir->i_sb, POSIX_ACL)) { 324 acl = ext4_get_acl(dir, ACL_TYPE_DEFAULT); 325 if (IS_ERR(acl)) 326 return PTR_ERR(acl); 327 } 328 if (!acl) 329 inode->i_mode &= ~current_umask(); 330 } 331 if (test_opt(inode->i_sb, POSIX_ACL) && acl) { 332 struct posix_acl *clone; 333 mode_t mode; 334 335 if (S_ISDIR(inode->i_mode)) { 336 error = ext4_set_acl(handle, inode, 337 ACL_TYPE_DEFAULT, acl); 338 if (error) 339 goto cleanup; 340 } 341 clone = posix_acl_clone(acl, GFP_NOFS); 342 error = -ENOMEM; 343 if (!clone) 344 goto cleanup; 345 346 mode = inode->i_mode; 347 error = posix_acl_create_masq(clone, &mode); 348 if (error >= 0) { 349 inode->i_mode = mode; 350 if (error > 0) { 351 /* This is an extended ACL */ 352 error = ext4_set_acl(handle, inode, 353 ACL_TYPE_ACCESS, clone); 354 } 355 } 356 posix_acl_release(clone); 357 } 358 cleanup: 359 posix_acl_release(acl); 360 return error; 361 } 362 363 /* 364 * Does chmod for an inode that may have an Access Control List. The 365 * inode->i_mode field must be updated to the desired value by the caller 366 * before calling this function. 367 * Returns 0 on success, or a negative error number. 368 * 369 * We change the ACL rather than storing some ACL entries in the file 370 * mode permission bits (which would be more efficient), because that 371 * would break once additional permissions (like ACL_APPEND, ACL_DELETE 372 * for directories) are added. There are no more bits available in the 373 * file mode. 374 * 375 * inode->i_mutex: down 376 */ 377 int 378 ext4_acl_chmod(struct inode *inode) 379 { 380 struct posix_acl *acl, *clone; 381 int error; 382 383 if (S_ISLNK(inode->i_mode)) 384 return -EOPNOTSUPP; 385 if (!test_opt(inode->i_sb, POSIX_ACL)) 386 return 0; 387 acl = ext4_get_acl(inode, ACL_TYPE_ACCESS); 388 if (IS_ERR(acl) || !acl) 389 return PTR_ERR(acl); 390 clone = posix_acl_clone(acl, GFP_KERNEL); 391 posix_acl_release(acl); 392 if (!clone) 393 return -ENOMEM; 394 error = posix_acl_chmod_masq(clone, inode->i_mode); 395 if (!error) { 396 handle_t *handle; 397 int retries = 0; 398 399 retry: 400 handle = ext4_journal_start(inode, 401 EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 402 if (IS_ERR(handle)) { 403 error = PTR_ERR(handle); 404 ext4_std_error(inode->i_sb, error); 405 goto out; 406 } 407 error = ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, clone); 408 ext4_journal_stop(handle); 409 if (error == -ENOSPC && 410 ext4_should_retry_alloc(inode->i_sb, &retries)) 411 goto retry; 412 } 413 out: 414 posix_acl_release(clone); 415 return error; 416 } 417 418 /* 419 * Extended attribute handlers 420 */ 421 static size_t 422 ext4_xattr_list_acl_access(struct inode *inode, char *list, size_t list_len, 423 const char *name, size_t name_len) 424 { 425 const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS); 426 427 if (!test_opt(inode->i_sb, POSIX_ACL)) 428 return 0; 429 if (list && size <= list_len) 430 memcpy(list, POSIX_ACL_XATTR_ACCESS, size); 431 return size; 432 } 433 434 static size_t 435 ext4_xattr_list_acl_default(struct inode *inode, char *list, size_t list_len, 436 const char *name, size_t name_len) 437 { 438 const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT); 439 440 if (!test_opt(inode->i_sb, POSIX_ACL)) 441 return 0; 442 if (list && size <= list_len) 443 memcpy(list, POSIX_ACL_XATTR_DEFAULT, size); 444 return size; 445 } 446 447 static int 448 ext4_xattr_get_acl(struct inode *inode, int type, void *buffer, size_t size) 449 { 450 struct posix_acl *acl; 451 int error; 452 453 if (!test_opt(inode->i_sb, POSIX_ACL)) 454 return -EOPNOTSUPP; 455 456 acl = ext4_get_acl(inode, type); 457 if (IS_ERR(acl)) 458 return PTR_ERR(acl); 459 if (acl == NULL) 460 return -ENODATA; 461 error = posix_acl_to_xattr(acl, buffer, size); 462 posix_acl_release(acl); 463 464 return error; 465 } 466 467 static int 468 ext4_xattr_get_acl_access(struct inode *inode, const char *name, 469 void *buffer, size_t size) 470 { 471 if (strcmp(name, "") != 0) 472 return -EINVAL; 473 return ext4_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size); 474 } 475 476 static int 477 ext4_xattr_get_acl_default(struct inode *inode, const char *name, 478 void *buffer, size_t size) 479 { 480 if (strcmp(name, "") != 0) 481 return -EINVAL; 482 return ext4_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size); 483 } 484 485 static int 486 ext4_xattr_set_acl(struct inode *inode, int type, const void *value, 487 size_t size) 488 { 489 handle_t *handle; 490 struct posix_acl *acl; 491 int error, retries = 0; 492 493 if (!test_opt(inode->i_sb, POSIX_ACL)) 494 return -EOPNOTSUPP; 495 if (!is_owner_or_cap(inode)) 496 return -EPERM; 497 498 if (value) { 499 acl = posix_acl_from_xattr(value, size); 500 if (IS_ERR(acl)) 501 return PTR_ERR(acl); 502 else if (acl) { 503 error = posix_acl_valid(acl); 504 if (error) 505 goto release_and_out; 506 } 507 } else 508 acl = NULL; 509 510 retry: 511 handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb)); 512 if (IS_ERR(handle)) 513 return PTR_ERR(handle); 514 error = ext4_set_acl(handle, inode, type, acl); 515 ext4_journal_stop(handle); 516 if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 517 goto retry; 518 519 release_and_out: 520 posix_acl_release(acl); 521 return error; 522 } 523 524 static int 525 ext4_xattr_set_acl_access(struct inode *inode, const char *name, 526 const void *value, size_t size, int flags) 527 { 528 if (strcmp(name, "") != 0) 529 return -EINVAL; 530 return ext4_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size); 531 } 532 533 static int 534 ext4_xattr_set_acl_default(struct inode *inode, const char *name, 535 const void *value, size_t size, int flags) 536 { 537 if (strcmp(name, "") != 0) 538 return -EINVAL; 539 return ext4_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size); 540 } 541 542 struct xattr_handler ext4_xattr_acl_access_handler = { 543 .prefix = POSIX_ACL_XATTR_ACCESS, 544 .list = ext4_xattr_list_acl_access, 545 .get = ext4_xattr_get_acl_access, 546 .set = ext4_xattr_set_acl_access, 547 }; 548 549 struct xattr_handler ext4_xattr_acl_default_handler = { 550 .prefix = POSIX_ACL_XATTR_DEFAULT, 551 .list = ext4_xattr_list_acl_default, 552 .get = ext4_xattr_get_acl_default, 553 .set = ext4_xattr_set_acl_default, 554 }; 555