1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2002,2003 by Andreas Gruenbacher <a.gruenbacher@computer.org> 4 * 5 * Fixes from William Schumacher incorporated on 15 March 2001. 6 * (Reported by Charles Bertsch, <CBertsch@microtest.com>). 7 */ 8 9 /* 10 * This file contains generic functions for manipulating 11 * POSIX 1003.1e draft standard 17 ACLs. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/slab.h> 16 #include <linux/atomic.h> 17 #include <linux/fs.h> 18 #include <linux/sched.h> 19 #include <linux/cred.h> 20 #include <linux/posix_acl.h> 21 #include <linux/posix_acl_xattr.h> 22 #include <linux/xattr.h> 23 #include <linux/export.h> 24 #include <linux/user_namespace.h> 25 #include <linux/namei.h> 26 #include <linux/mnt_idmapping.h> 27 #include <linux/iversion.h> 28 #include <linux/security.h> 29 #include <linux/fsnotify.h> 30 #include <linux/filelock.h> 31 32 #include "internal.h" 33 34 static struct posix_acl **acl_by_type(struct inode *inode, int type) 35 { 36 switch (type) { 37 case ACL_TYPE_ACCESS: 38 return &inode->i_acl; 39 case ACL_TYPE_DEFAULT: 40 return &inode->i_default_acl; 41 default: 42 BUG(); 43 } 44 } 45 46 struct posix_acl *get_cached_acl(struct inode *inode, int type) 47 { 48 struct posix_acl **p = acl_by_type(inode, type); 49 struct posix_acl *acl; 50 51 for (;;) { 52 rcu_read_lock(); 53 acl = rcu_dereference(*p); 54 if (!acl || is_uncached_acl(acl) || 55 refcount_inc_not_zero(&acl->a_refcount)) 56 break; 57 rcu_read_unlock(); 58 cpu_relax(); 59 } 60 rcu_read_unlock(); 61 return acl; 62 } 63 EXPORT_SYMBOL(get_cached_acl); 64 65 struct posix_acl *get_cached_acl_rcu(struct inode *inode, int type) 66 { 67 struct posix_acl *acl = rcu_dereference(*acl_by_type(inode, type)); 68 69 if (acl == ACL_DONT_CACHE) { 70 struct posix_acl *ret; 71 72 ret = inode->i_op->get_inode_acl(inode, type, LOOKUP_RCU); 73 if (!IS_ERR(ret)) 74 acl = ret; 75 } 76 77 return acl; 78 } 79 EXPORT_SYMBOL(get_cached_acl_rcu); 80 81 void set_cached_acl(struct inode *inode, int type, struct posix_acl *acl) 82 { 83 struct posix_acl **p = acl_by_type(inode, type); 84 struct posix_acl *old; 85 86 old = xchg(p, posix_acl_dup(acl)); 87 if (!is_uncached_acl(old)) 88 posix_acl_release(old); 89 } 90 EXPORT_SYMBOL(set_cached_acl); 91 92 static void __forget_cached_acl(struct posix_acl **p) 93 { 94 struct posix_acl *old; 95 96 /* 97 * ACL_DONT_CACHE is expected to be a "const" value and xchg it with 98 * ACL_NOT_CACHED would enable acl caching for the inode - 99 * clearly not what the caller has intended. 100 */ 101 if (READ_ONCE(*p) == ACL_DONT_CACHE) 102 return; 103 old = xchg(p, ACL_NOT_CACHED); 104 if (!is_uncached_acl(old)) 105 posix_acl_release(old); 106 } 107 108 void forget_cached_acl(struct inode *inode, int type) 109 { 110 __forget_cached_acl(acl_by_type(inode, type)); 111 } 112 EXPORT_SYMBOL(forget_cached_acl); 113 114 void forget_all_cached_acls(struct inode *inode) 115 { 116 __forget_cached_acl(&inode->i_acl); 117 __forget_cached_acl(&inode->i_default_acl); 118 } 119 EXPORT_SYMBOL(forget_all_cached_acls); 120 121 static struct posix_acl *__get_acl(struct mnt_idmap *idmap, 122 struct dentry *dentry, struct inode *inode, 123 int type) 124 { 125 struct posix_acl *sentinel; 126 struct posix_acl **p; 127 struct posix_acl *acl; 128 129 /* 130 * The sentinel is used to detect when another operation like 131 * set_cached_acl() or forget_cached_acl() races with get_inode_acl(). 132 * It is guaranteed that is_uncached_acl(sentinel) is true. 133 */ 134 135 acl = get_cached_acl(inode, type); 136 if (!is_uncached_acl(acl)) 137 return acl; 138 139 if (!IS_POSIXACL(inode)) 140 return NULL; 141 142 sentinel = uncached_acl_sentinel(current); 143 p = acl_by_type(inode, type); 144 145 /* 146 * If the ACL isn't being read yet, set our sentinel. Otherwise, the 147 * current value of the ACL will not be ACL_NOT_CACHED and so our own 148 * sentinel will not be set; another task will update the cache. We 149 * could wait for that other task to complete its job, but it's easier 150 * to just call ->get_inode_acl to fetch the ACL ourself. (This is 151 * going to be an unlikely race.) 152 */ 153 cmpxchg(p, ACL_NOT_CACHED, sentinel); 154 155 /* 156 * Normally, the ACL returned by ->get{_inode}_acl will be cached. 157 * A filesystem can prevent that by calling 158 * forget_cached_acl(inode, type) in ->get{_inode}_acl. 159 * 160 * If the filesystem doesn't have a get{_inode}_ acl() function at all, 161 * we'll just create the negative cache entry. 162 */ 163 if (dentry && inode->i_op->get_acl) { 164 acl = inode->i_op->get_acl(idmap, dentry, type); 165 } else if (inode->i_op->get_inode_acl) { 166 acl = inode->i_op->get_inode_acl(inode, type, false); 167 } else { 168 set_cached_acl(inode, type, NULL); 169 return NULL; 170 } 171 if (IS_ERR(acl)) { 172 /* 173 * Remove our sentinel so that we don't block future attempts 174 * to cache the ACL. 175 */ 176 cmpxchg(p, sentinel, ACL_NOT_CACHED); 177 return acl; 178 } 179 180 /* 181 * Cache the result, but only if our sentinel is still in place. 182 */ 183 posix_acl_dup(acl); 184 if (unlikely(!try_cmpxchg(p, &sentinel, acl))) 185 posix_acl_release(acl); 186 return acl; 187 } 188 189 struct posix_acl *get_inode_acl(struct inode *inode, int type) 190 { 191 return __get_acl(&nop_mnt_idmap, NULL, inode, type); 192 } 193 EXPORT_SYMBOL(get_inode_acl); 194 195 /* 196 * Init a fresh posix_acl 197 */ 198 void 199 posix_acl_init(struct posix_acl *acl, int count) 200 { 201 refcount_set(&acl->a_refcount, 1); 202 acl->a_count = count; 203 } 204 EXPORT_SYMBOL(posix_acl_init); 205 206 /* 207 * Allocate a new ACL with the specified number of entries. 208 */ 209 struct posix_acl * 210 posix_acl_alloc(unsigned int count, gfp_t flags) 211 { 212 struct posix_acl *acl; 213 214 acl = kmalloc_flex(*acl, a_entries, count, flags); 215 if (acl) 216 posix_acl_init(acl, count); 217 return acl; 218 } 219 EXPORT_SYMBOL(posix_acl_alloc); 220 221 /* 222 * Clone an ACL. 223 */ 224 struct posix_acl * 225 posix_acl_clone(const struct posix_acl *acl, gfp_t flags) 226 { 227 struct posix_acl *clone = NULL; 228 229 if (acl) { 230 clone = kmemdup(acl, struct_size(acl, a_entries, acl->a_count), 231 flags); 232 if (clone) 233 refcount_set(&clone->a_refcount, 1); 234 } 235 return clone; 236 } 237 EXPORT_SYMBOL_GPL(posix_acl_clone); 238 239 /* 240 * Check if an acl is valid. Returns 0 if it is, or -E... otherwise. 241 */ 242 int 243 posix_acl_valid(struct user_namespace *user_ns, const struct posix_acl *acl) 244 { 245 const struct posix_acl_entry *pa, *pe; 246 int state = ACL_USER_OBJ; 247 int needs_mask = 0; 248 249 FOREACH_ACL_ENTRY(pa, acl, pe) { 250 if (pa->e_perm & ~(ACL_READ|ACL_WRITE|ACL_EXECUTE)) 251 return -EINVAL; 252 switch (pa->e_tag) { 253 case ACL_USER_OBJ: 254 if (state == ACL_USER_OBJ) { 255 state = ACL_USER; 256 break; 257 } 258 return -EINVAL; 259 260 case ACL_USER: 261 if (state != ACL_USER) 262 return -EINVAL; 263 if (!kuid_has_mapping(user_ns, pa->e_uid)) 264 return -EINVAL; 265 needs_mask = 1; 266 break; 267 268 case ACL_GROUP_OBJ: 269 if (state == ACL_USER) { 270 state = ACL_GROUP; 271 break; 272 } 273 return -EINVAL; 274 275 case ACL_GROUP: 276 if (state != ACL_GROUP) 277 return -EINVAL; 278 if (!kgid_has_mapping(user_ns, pa->e_gid)) 279 return -EINVAL; 280 needs_mask = 1; 281 break; 282 283 case ACL_MASK: 284 if (state != ACL_GROUP) 285 return -EINVAL; 286 state = ACL_OTHER; 287 break; 288 289 case ACL_OTHER: 290 if (state == ACL_OTHER || 291 (state == ACL_GROUP && !needs_mask)) { 292 state = 0; 293 break; 294 } 295 return -EINVAL; 296 297 default: 298 return -EINVAL; 299 } 300 } 301 if (state == 0) 302 return 0; 303 return -EINVAL; 304 } 305 EXPORT_SYMBOL(posix_acl_valid); 306 307 /* 308 * Returns 0 if the acl can be exactly represented in the traditional 309 * file mode permission bits, or else 1. Returns -E... on error. 310 */ 311 int 312 posix_acl_equiv_mode(const struct posix_acl *acl, umode_t *mode_p) 313 { 314 const struct posix_acl_entry *pa, *pe; 315 umode_t mode = 0; 316 int not_equiv = 0; 317 318 /* 319 * A null ACL can always be presented as mode bits. 320 */ 321 if (!acl) 322 return 0; 323 324 FOREACH_ACL_ENTRY(pa, acl, pe) { 325 switch (pa->e_tag) { 326 case ACL_USER_OBJ: 327 mode |= (pa->e_perm & S_IRWXO) << 6; 328 break; 329 case ACL_GROUP_OBJ: 330 mode |= (pa->e_perm & S_IRWXO) << 3; 331 break; 332 case ACL_OTHER: 333 mode |= pa->e_perm & S_IRWXO; 334 break; 335 case ACL_MASK: 336 mode = (mode & ~S_IRWXG) | 337 ((pa->e_perm & S_IRWXO) << 3); 338 not_equiv = 1; 339 break; 340 case ACL_USER: 341 case ACL_GROUP: 342 not_equiv = 1; 343 break; 344 default: 345 return -EINVAL; 346 } 347 } 348 if (mode_p) 349 *mode_p = (*mode_p & ~S_IRWXUGO) | mode; 350 return not_equiv; 351 } 352 EXPORT_SYMBOL(posix_acl_equiv_mode); 353 354 /* 355 * Create an ACL representing the file mode permission bits of an inode. 356 */ 357 struct posix_acl * 358 posix_acl_from_mode(umode_t mode, gfp_t flags) 359 { 360 struct posix_acl *acl = posix_acl_alloc(3, flags); 361 if (!acl) 362 return ERR_PTR(-ENOMEM); 363 364 acl->a_entries[0].e_tag = ACL_USER_OBJ; 365 acl->a_entries[0].e_perm = (mode & S_IRWXU) >> 6; 366 367 acl->a_entries[1].e_tag = ACL_GROUP_OBJ; 368 acl->a_entries[1].e_perm = (mode & S_IRWXG) >> 3; 369 370 acl->a_entries[2].e_tag = ACL_OTHER; 371 acl->a_entries[2].e_perm = (mode & S_IRWXO); 372 return acl; 373 } 374 EXPORT_SYMBOL(posix_acl_from_mode); 375 376 /* 377 * Return 0 if current is granted want access to the inode 378 * by the acl. Returns -E... otherwise. 379 */ 380 int 381 posix_acl_permission(struct mnt_idmap *idmap, struct inode *inode, 382 const struct posix_acl *acl, int want) 383 { 384 const struct posix_acl_entry *pa, *pe, *mask_obj; 385 struct user_namespace *fs_userns = i_user_ns(inode); 386 int found = 0; 387 vfsuid_t vfsuid; 388 vfsgid_t vfsgid; 389 390 want &= MAY_READ | MAY_WRITE | MAY_EXEC; 391 392 FOREACH_ACL_ENTRY(pa, acl, pe) { 393 switch(pa->e_tag) { 394 case ACL_USER_OBJ: 395 /* (May have been checked already) */ 396 vfsuid = i_uid_into_vfsuid(idmap, inode); 397 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) 398 goto check_perm; 399 break; 400 case ACL_USER: 401 vfsuid = make_vfsuid(idmap, fs_userns, 402 pa->e_uid); 403 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) 404 goto mask; 405 break; 406 case ACL_GROUP_OBJ: 407 vfsgid = i_gid_into_vfsgid(idmap, inode); 408 if (vfsgid_in_group_p(vfsgid)) { 409 found = 1; 410 if ((pa->e_perm & want) == want) 411 goto mask; 412 } 413 break; 414 case ACL_GROUP: 415 vfsgid = make_vfsgid(idmap, fs_userns, 416 pa->e_gid); 417 if (vfsgid_in_group_p(vfsgid)) { 418 found = 1; 419 if ((pa->e_perm & want) == want) 420 goto mask; 421 } 422 break; 423 case ACL_MASK: 424 break; 425 case ACL_OTHER: 426 if (found) 427 return -EACCES; 428 else 429 goto check_perm; 430 default: 431 return -EIO; 432 } 433 } 434 return -EIO; 435 436 mask: 437 for (mask_obj = pa+1; mask_obj != pe; mask_obj++) { 438 if (mask_obj->e_tag == ACL_MASK) { 439 if ((pa->e_perm & mask_obj->e_perm & want) == want) 440 return 0; 441 return -EACCES; 442 } 443 } 444 445 check_perm: 446 if ((pa->e_perm & want) == want) 447 return 0; 448 return -EACCES; 449 } 450 451 /* 452 * Modify acl when creating a new inode. The caller must ensure the acl is 453 * only referenced once. 454 * 455 * mode_p initially must contain the mode parameter to the open() / creat() 456 * system calls. All permissions that are not granted by the acl are removed. 457 * The permissions in the acl are changed to reflect the mode_p parameter. 458 */ 459 static int posix_acl_create_masq(struct posix_acl *acl, umode_t *mode_p) 460 { 461 struct posix_acl_entry *pa, *pe; 462 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; 463 umode_t mode = *mode_p; 464 int not_equiv = 0; 465 466 /* assert(atomic_read(acl->a_refcount) == 1); */ 467 468 FOREACH_ACL_ENTRY(pa, acl, pe) { 469 switch(pa->e_tag) { 470 case ACL_USER_OBJ: 471 pa->e_perm &= (mode >> 6) | ~S_IRWXO; 472 mode &= (pa->e_perm << 6) | ~S_IRWXU; 473 break; 474 475 case ACL_USER: 476 case ACL_GROUP: 477 not_equiv = 1; 478 break; 479 480 case ACL_GROUP_OBJ: 481 group_obj = pa; 482 break; 483 484 case ACL_OTHER: 485 pa->e_perm &= mode | ~S_IRWXO; 486 mode &= pa->e_perm | ~S_IRWXO; 487 break; 488 489 case ACL_MASK: 490 mask_obj = pa; 491 not_equiv = 1; 492 break; 493 494 default: 495 return -EIO; 496 } 497 } 498 499 if (mask_obj) { 500 mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO; 501 mode &= (mask_obj->e_perm << 3) | ~S_IRWXG; 502 } else { 503 if (!group_obj) 504 return -EIO; 505 group_obj->e_perm &= (mode >> 3) | ~S_IRWXO; 506 mode &= (group_obj->e_perm << 3) | ~S_IRWXG; 507 } 508 509 *mode_p = (*mode_p & ~S_IRWXUGO) | mode; 510 return not_equiv; 511 } 512 513 /* 514 * Modify the ACL for the chmod syscall. 515 */ 516 static int __posix_acl_chmod_masq(struct posix_acl *acl, umode_t mode) 517 { 518 struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL; 519 struct posix_acl_entry *pa, *pe; 520 521 /* assert(atomic_read(acl->a_refcount) == 1); */ 522 523 FOREACH_ACL_ENTRY(pa, acl, pe) { 524 switch(pa->e_tag) { 525 case ACL_USER_OBJ: 526 pa->e_perm = (mode & S_IRWXU) >> 6; 527 break; 528 529 case ACL_USER: 530 case ACL_GROUP: 531 break; 532 533 case ACL_GROUP_OBJ: 534 group_obj = pa; 535 break; 536 537 case ACL_MASK: 538 mask_obj = pa; 539 break; 540 541 case ACL_OTHER: 542 pa->e_perm = (mode & S_IRWXO); 543 break; 544 545 default: 546 return -EIO; 547 } 548 } 549 550 if (mask_obj) { 551 mask_obj->e_perm = (mode & S_IRWXG) >> 3; 552 } else { 553 if (!group_obj) 554 return -EIO; 555 group_obj->e_perm = (mode & S_IRWXG) >> 3; 556 } 557 558 return 0; 559 } 560 561 int 562 __posix_acl_create(struct posix_acl **acl, gfp_t gfp, umode_t *mode_p) 563 { 564 struct posix_acl *clone = posix_acl_clone(*acl, gfp); 565 int err = -ENOMEM; 566 if (clone) { 567 err = posix_acl_create_masq(clone, mode_p); 568 if (err < 0) { 569 posix_acl_release(clone); 570 clone = NULL; 571 } 572 } 573 posix_acl_release(*acl); 574 *acl = clone; 575 return err; 576 } 577 EXPORT_SYMBOL(__posix_acl_create); 578 579 int 580 __posix_acl_chmod(struct posix_acl **acl, gfp_t gfp, umode_t mode) 581 { 582 struct posix_acl *clone = posix_acl_clone(*acl, gfp); 583 int err = -ENOMEM; 584 if (clone) { 585 err = __posix_acl_chmod_masq(clone, mode); 586 if (err) { 587 posix_acl_release(clone); 588 clone = NULL; 589 } 590 } 591 posix_acl_release(*acl); 592 *acl = clone; 593 return err; 594 } 595 EXPORT_SYMBOL(__posix_acl_chmod); 596 597 /** 598 * posix_acl_chmod - chmod a posix acl 599 * 600 * @idmap: idmap of the mount @inode was found from 601 * @dentry: dentry to check permissions on 602 * @mode: the new mode of @inode 603 * 604 * If the dentry has been found through an idmapped mount the idmap of 605 * the vfsmount must be passed through @idmap. This function will then 606 * take care to map the inode according to @idmap before checking 607 * permissions. On non-idmapped mounts or if permission checking is to be 608 * performed on the raw inode simply pass @nop_mnt_idmap. 609 */ 610 int 611 posix_acl_chmod(struct mnt_idmap *idmap, struct dentry *dentry, 612 umode_t mode) 613 { 614 struct inode *inode = d_inode(dentry); 615 struct posix_acl *acl; 616 int ret = 0; 617 618 if (!IS_POSIXACL(inode)) 619 return 0; 620 if (!inode->i_op->set_acl) 621 return -EOPNOTSUPP; 622 623 acl = get_inode_acl(inode, ACL_TYPE_ACCESS); 624 if (IS_ERR_OR_NULL(acl)) { 625 if (acl == ERR_PTR(-EOPNOTSUPP)) 626 return 0; 627 return PTR_ERR(acl); 628 } 629 630 ret = __posix_acl_chmod(&acl, GFP_KERNEL, mode); 631 if (ret) 632 return ret; 633 ret = inode->i_op->set_acl(idmap, dentry, acl, ACL_TYPE_ACCESS); 634 posix_acl_release(acl); 635 return ret; 636 } 637 EXPORT_SYMBOL(posix_acl_chmod); 638 639 int 640 posix_acl_create(struct inode *dir, umode_t *mode, 641 struct posix_acl **default_acl, struct posix_acl **acl) 642 { 643 struct posix_acl *p; 644 struct posix_acl *clone; 645 int ret; 646 647 *acl = NULL; 648 *default_acl = NULL; 649 650 if (S_ISLNK(*mode) || !IS_POSIXACL(dir)) 651 return 0; 652 653 p = get_inode_acl(dir, ACL_TYPE_DEFAULT); 654 if (!p || p == ERR_PTR(-EOPNOTSUPP)) { 655 *mode &= ~current_umask(); 656 return 0; 657 } 658 if (IS_ERR(p)) 659 return PTR_ERR(p); 660 661 ret = -ENOMEM; 662 clone = posix_acl_clone(p, GFP_NOFS); 663 if (!clone) 664 goto err_release; 665 666 ret = posix_acl_create_masq(clone, mode); 667 if (ret < 0) 668 goto err_release_clone; 669 670 if (ret == 0) 671 posix_acl_release(clone); 672 else 673 *acl = clone; 674 675 if (!S_ISDIR(*mode)) 676 posix_acl_release(p); 677 else 678 *default_acl = p; 679 680 return 0; 681 682 err_release_clone: 683 posix_acl_release(clone); 684 err_release: 685 posix_acl_release(p); 686 return ret; 687 } 688 EXPORT_SYMBOL_GPL(posix_acl_create); 689 690 /** 691 * posix_acl_update_mode - update mode in set_acl 692 * @idmap: idmap of the mount @inode was found from 693 * @inode: target inode 694 * @mode_p: mode (pointer) for update 695 * @acl: acl pointer 696 * 697 * Update the file mode when setting an ACL: compute the new file permission 698 * bits based on the ACL. In addition, if the ACL is equivalent to the new 699 * file mode, set *@acl to NULL to indicate that no ACL should be set. 700 * 701 * As with chmod, clear the setgid bit if the caller is not in the owning group 702 * or capable of CAP_FSETID (see inode_change_ok). 703 * 704 * If the inode has been found through an idmapped mount the idmap of 705 * the vfsmount must be passed through @idmap. This function will then 706 * take care to map the inode according to @idmap before checking 707 * permissions. On non-idmapped mounts or if permission checking is to be 708 * performed on the raw inode simply pass @nop_mnt_idmap. 709 * 710 * Called from set_acl inode operations. 711 */ 712 int posix_acl_update_mode(struct mnt_idmap *idmap, 713 struct inode *inode, umode_t *mode_p, 714 struct posix_acl **acl) 715 { 716 umode_t mode = inode->i_mode; 717 int error; 718 719 error = posix_acl_equiv_mode(*acl, &mode); 720 if (error < 0) 721 return error; 722 if (error == 0) 723 *acl = NULL; 724 if (!in_group_or_capable(idmap, inode, 725 i_gid_into_vfsgid(idmap, inode))) 726 mode &= ~S_ISGID; 727 *mode_p = mode; 728 return 0; 729 } 730 EXPORT_SYMBOL(posix_acl_update_mode); 731 732 /* 733 * Fix up the uids and gids in posix acl extended attributes in place. 734 */ 735 static int posix_acl_fix_xattr_common(const void *value, size_t size) 736 { 737 const struct posix_acl_xattr_header *header = value; 738 int count; 739 740 if (!header) 741 return -EINVAL; 742 if (size < sizeof(struct posix_acl_xattr_header)) 743 return -EINVAL; 744 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION)) 745 return -EOPNOTSUPP; 746 747 count = posix_acl_xattr_count(size); 748 if (count < 0) 749 return -EINVAL; 750 if (count == 0) 751 return 0; 752 753 return count; 754 } 755 756 /** 757 * posix_acl_from_xattr - convert POSIX ACLs from backing store to VFS format 758 * @userns: the filesystem's idmapping 759 * @value: the uapi representation of POSIX ACLs 760 * @size: the size of @void 761 * 762 * Filesystems that store POSIX ACLs in the unaltered uapi format should use 763 * posix_acl_from_xattr() when reading them from the backing store and 764 * converting them into the struct posix_acl VFS format. The helper is 765 * specifically intended to be called from the acl inode operation. 766 * 767 * The posix_acl_from_xattr() function will map the raw {g,u}id values stored 768 * in ACL_{GROUP,USER} entries into idmapping in @userns. 769 * 770 * Note that posix_acl_from_xattr() does not take idmapped mounts into account. 771 * If it did it calling it from the get acl inode operation would return POSIX 772 * ACLs mapped according to an idmapped mount which would mean that the value 773 * couldn't be cached for the filesystem. Idmapped mounts are taken into 774 * account on the fly during permission checking or right at the VFS - 775 * userspace boundary before reporting them to the user. 776 * 777 * Return: Allocated struct posix_acl on success, NULL for a valid header but 778 * without actual POSIX ACL entries, or ERR_PTR() encoded error code. 779 */ 780 struct posix_acl *posix_acl_from_xattr(struct user_namespace *userns, 781 const void *value, size_t size) 782 { 783 const struct posix_acl_xattr_header *header = value; 784 const struct posix_acl_xattr_entry *entry = (const void *)(header + 1), *end; 785 int count; 786 struct posix_acl *acl; 787 struct posix_acl_entry *acl_e; 788 789 count = posix_acl_fix_xattr_common(value, size); 790 if (count < 0) 791 return ERR_PTR(count); 792 if (count == 0) 793 return NULL; 794 795 acl = posix_acl_alloc(count, GFP_NOFS); 796 if (!acl) 797 return ERR_PTR(-ENOMEM); 798 acl_e = acl->a_entries; 799 800 for (end = entry + count; entry != end; acl_e++, entry++) { 801 acl_e->e_tag = le16_to_cpu(entry->e_tag); 802 acl_e->e_perm = le16_to_cpu(entry->e_perm); 803 804 switch(acl_e->e_tag) { 805 case ACL_USER_OBJ: 806 case ACL_GROUP_OBJ: 807 case ACL_MASK: 808 case ACL_OTHER: 809 break; 810 811 case ACL_USER: 812 acl_e->e_uid = make_kuid(userns, 813 le32_to_cpu(entry->e_id)); 814 if (!uid_valid(acl_e->e_uid)) 815 goto fail; 816 break; 817 case ACL_GROUP: 818 acl_e->e_gid = make_kgid(userns, 819 le32_to_cpu(entry->e_id)); 820 if (!gid_valid(acl_e->e_gid)) 821 goto fail; 822 break; 823 824 default: 825 goto fail; 826 } 827 } 828 return acl; 829 830 fail: 831 posix_acl_release(acl); 832 return ERR_PTR(-EINVAL); 833 } 834 EXPORT_SYMBOL (posix_acl_from_xattr); 835 836 /* 837 * Convert from in-memory to extended attribute representation. 838 */ 839 void * 840 posix_acl_to_xattr(struct user_namespace *user_ns, const struct posix_acl *acl, 841 size_t *sizep, gfp_t gfp) 842 { 843 struct posix_acl_xattr_header *ext_acl; 844 struct posix_acl_xattr_entry *ext_entry; 845 size_t size; 846 int n; 847 848 size = posix_acl_xattr_size(acl->a_count); 849 ext_acl = kmalloc(size, gfp); 850 if (!ext_acl) 851 return NULL; 852 853 ext_entry = (void *)(ext_acl + 1); 854 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); 855 856 for (n=0; n < acl->a_count; n++, ext_entry++) { 857 const struct posix_acl_entry *acl_e = &acl->a_entries[n]; 858 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag); 859 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); 860 switch(acl_e->e_tag) { 861 case ACL_USER: 862 ext_entry->e_id = 863 cpu_to_le32(from_kuid(user_ns, acl_e->e_uid)); 864 break; 865 case ACL_GROUP: 866 ext_entry->e_id = 867 cpu_to_le32(from_kgid(user_ns, acl_e->e_gid)); 868 break; 869 default: 870 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); 871 break; 872 } 873 } 874 *sizep = size; 875 return ext_acl; 876 } 877 EXPORT_SYMBOL (posix_acl_to_xattr); 878 879 /** 880 * vfs_posix_acl_to_xattr - convert from kernel to userspace representation 881 * @idmap: idmap of the mount 882 * @inode: inode the posix acls are set on 883 * @acl: the posix acls as represented by the vfs 884 * @buffer: the buffer into which to convert @acl 885 * @size: size of @buffer 886 * 887 * This converts @acl from the VFS representation in the filesystem idmapping 888 * to the uapi form reportable to userspace. And mount and caller idmappings 889 * are handled appropriately. 890 * 891 * Return: On success, the size of the stored uapi posix acls, on error a 892 * negative errno. 893 */ 894 static ssize_t vfs_posix_acl_to_xattr(struct mnt_idmap *idmap, 895 struct inode *inode, 896 const struct posix_acl *acl, void *buffer, 897 size_t size) 898 899 { 900 struct posix_acl_xattr_header *ext_acl = buffer; 901 struct posix_acl_xattr_entry *ext_entry; 902 struct user_namespace *fs_userns, *caller_userns; 903 ssize_t real_size, n; 904 vfsuid_t vfsuid; 905 vfsgid_t vfsgid; 906 907 real_size = posix_acl_xattr_size(acl->a_count); 908 if (!buffer) 909 return real_size; 910 if (real_size > size) 911 return -ERANGE; 912 913 ext_entry = (void *)(ext_acl + 1); 914 ext_acl->a_version = cpu_to_le32(POSIX_ACL_XATTR_VERSION); 915 916 fs_userns = i_user_ns(inode); 917 caller_userns = current_user_ns(); 918 for (n=0; n < acl->a_count; n++, ext_entry++) { 919 const struct posix_acl_entry *acl_e = &acl->a_entries[n]; 920 ext_entry->e_tag = cpu_to_le16(acl_e->e_tag); 921 ext_entry->e_perm = cpu_to_le16(acl_e->e_perm); 922 switch(acl_e->e_tag) { 923 case ACL_USER: 924 vfsuid = make_vfsuid(idmap, fs_userns, acl_e->e_uid); 925 ext_entry->e_id = cpu_to_le32(from_kuid( 926 caller_userns, vfsuid_into_kuid(vfsuid))); 927 break; 928 case ACL_GROUP: 929 vfsgid = make_vfsgid(idmap, fs_userns, acl_e->e_gid); 930 ext_entry->e_id = cpu_to_le32(from_kgid( 931 caller_userns, vfsgid_into_kgid(vfsgid))); 932 break; 933 default: 934 ext_entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID); 935 break; 936 } 937 } 938 return real_size; 939 } 940 941 int 942 set_posix_acl(struct mnt_idmap *idmap, struct dentry *dentry, 943 int type, struct posix_acl *acl) 944 { 945 struct inode *inode = d_inode(dentry); 946 947 if (!IS_POSIXACL(inode)) 948 return -EOPNOTSUPP; 949 if (!inode->i_op->set_acl) 950 return -EOPNOTSUPP; 951 952 if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) 953 return acl ? -EACCES : 0; 954 if (!inode_owner_or_capable(idmap, inode)) 955 return -EPERM; 956 957 if (acl) { 958 int ret = posix_acl_valid(inode->i_sb->s_user_ns, acl); 959 if (ret) 960 return ret; 961 } 962 return inode->i_op->set_acl(idmap, dentry, acl, type); 963 } 964 EXPORT_SYMBOL(set_posix_acl); 965 966 int posix_acl_listxattr(struct inode *inode, char **buffer, 967 ssize_t *remaining_size) 968 { 969 int err; 970 971 if (!IS_POSIXACL(inode)) 972 return 0; 973 974 if (inode->i_acl) { 975 err = xattr_list_one(buffer, remaining_size, 976 XATTR_NAME_POSIX_ACL_ACCESS); 977 if (err) 978 return err; 979 } 980 981 if (inode->i_default_acl) { 982 err = xattr_list_one(buffer, remaining_size, 983 XATTR_NAME_POSIX_ACL_DEFAULT); 984 if (err) 985 return err; 986 } 987 988 return 0; 989 } 990 991 static bool 992 posix_acl_xattr_list(struct dentry *dentry) 993 { 994 return IS_POSIXACL(d_backing_inode(dentry)); 995 } 996 997 /* 998 * nop_posix_acl_access - legacy xattr handler for access POSIX ACLs 999 * 1000 * This is the legacy POSIX ACL access xattr handler. It is used by some 1001 * filesystems to implement their ->listxattr() inode operation. New code 1002 * should never use them. 1003 */ 1004 const struct xattr_handler nop_posix_acl_access = { 1005 .name = XATTR_NAME_POSIX_ACL_ACCESS, 1006 .list = posix_acl_xattr_list, 1007 }; 1008 EXPORT_SYMBOL_GPL(nop_posix_acl_access); 1009 1010 /* 1011 * nop_posix_acl_default - legacy xattr handler for default POSIX ACLs 1012 * 1013 * This is the legacy POSIX ACL default xattr handler. It is used by some 1014 * filesystems to implement their ->listxattr() inode operation. New code 1015 * should never use them. 1016 */ 1017 const struct xattr_handler nop_posix_acl_default = { 1018 .name = XATTR_NAME_POSIX_ACL_DEFAULT, 1019 .list = posix_acl_xattr_list, 1020 }; 1021 EXPORT_SYMBOL_GPL(nop_posix_acl_default); 1022 1023 int simple_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 1024 struct posix_acl *acl, int type) 1025 { 1026 int error; 1027 struct inode *inode = d_inode(dentry); 1028 1029 if (type == ACL_TYPE_ACCESS) { 1030 error = posix_acl_update_mode(idmap, inode, 1031 &inode->i_mode, &acl); 1032 if (error) 1033 return error; 1034 } 1035 1036 inode_set_ctime_current(inode); 1037 if (IS_I_VERSION(inode)) 1038 inode_inc_iversion(inode); 1039 set_cached_acl(inode, type, acl); 1040 return 0; 1041 } 1042 1043 int simple_acl_create(struct inode *dir, struct inode *inode) 1044 { 1045 struct posix_acl *default_acl, *acl; 1046 int error; 1047 1048 error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl); 1049 if (error) 1050 return error; 1051 1052 set_cached_acl(inode, ACL_TYPE_DEFAULT, default_acl); 1053 set_cached_acl(inode, ACL_TYPE_ACCESS, acl); 1054 1055 if (default_acl) 1056 posix_acl_release(default_acl); 1057 if (acl) 1058 posix_acl_release(acl); 1059 return 0; 1060 } 1061 1062 static int vfs_set_acl_idmapped_mnt(struct mnt_idmap *idmap, 1063 struct user_namespace *fs_userns, 1064 struct posix_acl *acl) 1065 { 1066 for (int n = 0; n < acl->a_count; n++) { 1067 struct posix_acl_entry *acl_e = &acl->a_entries[n]; 1068 1069 switch (acl_e->e_tag) { 1070 case ACL_USER: 1071 acl_e->e_uid = from_vfsuid(idmap, fs_userns, 1072 VFSUIDT_INIT(acl_e->e_uid)); 1073 break; 1074 case ACL_GROUP: 1075 acl_e->e_gid = from_vfsgid(idmap, fs_userns, 1076 VFSGIDT_INIT(acl_e->e_gid)); 1077 break; 1078 } 1079 } 1080 1081 return 0; 1082 } 1083 1084 /** 1085 * vfs_set_acl - set posix acls 1086 * @idmap: idmap of the mount 1087 * @dentry: the dentry based on which to set the posix acls 1088 * @acl_name: the name of the posix acl 1089 * @kacl: the posix acls in the appropriate VFS format 1090 * 1091 * This function sets @kacl. The caller must all posix_acl_release() on @kacl 1092 * afterwards. 1093 * 1094 * Return: On success 0, on error negative errno. 1095 */ 1096 int vfs_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 1097 const char *acl_name, struct posix_acl *kacl) 1098 { 1099 int acl_type; 1100 int error; 1101 struct inode *inode = d_inode(dentry); 1102 struct delegated_inode delegated_inode = { }; 1103 1104 acl_type = posix_acl_type(acl_name); 1105 if (acl_type < 0) 1106 return -EINVAL; 1107 1108 if (kacl) { 1109 /* 1110 * If we're on an idmapped mount translate from mount specific 1111 * vfs{g,u}id_t into global filesystem k{g,u}id_t. 1112 * Afterwards we can cache the POSIX ACLs filesystem wide and - 1113 * if this is a filesystem with a backing store - ultimately 1114 * translate them to backing store values. 1115 */ 1116 error = vfs_set_acl_idmapped_mnt(idmap, i_user_ns(inode), kacl); 1117 if (error) 1118 return error; 1119 } 1120 1121 retry_deleg: 1122 inode_lock(inode); 1123 1124 /* 1125 * We only care about restrictions the inode struct itself places upon 1126 * us otherwise POSIX ACLs aren't subject to any VFS restrictions. 1127 */ 1128 error = may_write_xattr(idmap, inode); 1129 if (error) 1130 goto out_inode_unlock; 1131 1132 error = security_inode_set_acl(idmap, dentry, acl_name, kacl); 1133 if (error) 1134 goto out_inode_unlock; 1135 1136 error = try_break_deleg(inode, 0, &delegated_inode); 1137 if (error) 1138 goto out_inode_unlock; 1139 1140 if (likely(!is_bad_inode(inode))) 1141 error = set_posix_acl(idmap, dentry, acl_type, kacl); 1142 else 1143 error = -EIO; 1144 if (!error) { 1145 fsnotify_xattr(dentry); 1146 security_inode_post_set_acl(dentry, acl_name, kacl); 1147 } 1148 1149 out_inode_unlock: 1150 inode_unlock(inode); 1151 1152 if (is_delegated(&delegated_inode)) { 1153 error = break_deleg_wait(&delegated_inode); 1154 if (!error) 1155 goto retry_deleg; 1156 } 1157 1158 return error; 1159 } 1160 EXPORT_SYMBOL_GPL(vfs_set_acl); 1161 1162 /** 1163 * vfs_get_acl - get posix acls 1164 * @idmap: idmap of the mount 1165 * @dentry: the dentry based on which to retrieve the posix acls 1166 * @acl_name: the name of the posix acl 1167 * 1168 * This function retrieves @kacl from the filesystem. The caller must all 1169 * posix_acl_release() on @kacl. 1170 * 1171 * Return: On success POSIX ACLs in VFS format, on error negative errno. 1172 */ 1173 struct posix_acl *vfs_get_acl(struct mnt_idmap *idmap, 1174 struct dentry *dentry, const char *acl_name) 1175 { 1176 struct inode *inode = d_inode(dentry); 1177 struct posix_acl *acl; 1178 int acl_type, error; 1179 1180 acl_type = posix_acl_type(acl_name); 1181 if (acl_type < 0) 1182 return ERR_PTR(-EINVAL); 1183 1184 /* 1185 * The VFS has no restrictions on reading POSIX ACLs so calling 1186 * something like xattr_permission() isn't needed. Only LSMs get a say. 1187 */ 1188 error = security_inode_get_acl(idmap, dentry, acl_name); 1189 if (error) 1190 return ERR_PTR(error); 1191 1192 if (!IS_POSIXACL(inode)) 1193 return ERR_PTR(-EOPNOTSUPP); 1194 if (S_ISLNK(inode->i_mode)) 1195 return ERR_PTR(-EOPNOTSUPP); 1196 1197 acl = __get_acl(idmap, dentry, inode, acl_type); 1198 if (IS_ERR(acl)) 1199 return acl; 1200 if (!acl) 1201 return ERR_PTR(-ENODATA); 1202 1203 return acl; 1204 } 1205 EXPORT_SYMBOL_GPL(vfs_get_acl); 1206 1207 /** 1208 * vfs_remove_acl - remove posix acls 1209 * @idmap: idmap of the mount 1210 * @dentry: the dentry based on which to retrieve the posix acls 1211 * @acl_name: the name of the posix acl 1212 * 1213 * This function removes posix acls. 1214 * 1215 * Return: On success 0, on error negative errno. 1216 */ 1217 int vfs_remove_acl(struct mnt_idmap *idmap, struct dentry *dentry, 1218 const char *acl_name) 1219 { 1220 int acl_type; 1221 int error; 1222 struct inode *inode = d_inode(dentry); 1223 struct delegated_inode delegated_inode = { }; 1224 1225 acl_type = posix_acl_type(acl_name); 1226 if (acl_type < 0) 1227 return -EINVAL; 1228 1229 retry_deleg: 1230 inode_lock(inode); 1231 1232 /* 1233 * We only care about restrictions the inode struct itself places upon 1234 * us otherwise POSIX ACLs aren't subject to any VFS restrictions. 1235 */ 1236 error = may_write_xattr(idmap, inode); 1237 if (error) 1238 goto out_inode_unlock; 1239 1240 error = security_inode_remove_acl(idmap, dentry, acl_name); 1241 if (error) 1242 goto out_inode_unlock; 1243 1244 error = try_break_deleg(inode, 0, &delegated_inode); 1245 if (error) 1246 goto out_inode_unlock; 1247 1248 if (likely(!is_bad_inode(inode))) 1249 error = set_posix_acl(idmap, dentry, acl_type, NULL); 1250 else 1251 error = -EIO; 1252 if (!error) { 1253 fsnotify_xattr(dentry); 1254 security_inode_post_remove_acl(idmap, dentry, acl_name); 1255 } 1256 1257 out_inode_unlock: 1258 inode_unlock(inode); 1259 1260 if (is_delegated(&delegated_inode)) { 1261 error = break_deleg_wait(&delegated_inode); 1262 if (!error) 1263 goto retry_deleg; 1264 } 1265 1266 return error; 1267 } 1268 EXPORT_SYMBOL_GPL(vfs_remove_acl); 1269 1270 int do_set_acl(struct mnt_idmap *idmap, struct dentry *dentry, 1271 const char *acl_name, const void *kvalue, size_t size) 1272 { 1273 int error; 1274 struct posix_acl *acl = NULL; 1275 1276 if (size) { 1277 /* 1278 * Note that posix_acl_from_xattr() uses GFP_NOFS when it 1279 * probably doesn't need to here. 1280 */ 1281 acl = posix_acl_from_xattr(current_user_ns(), kvalue, size); 1282 if (IS_ERR(acl)) 1283 return PTR_ERR(acl); 1284 } 1285 1286 error = vfs_set_acl(idmap, dentry, acl_name, acl); 1287 posix_acl_release(acl); 1288 return error; 1289 } 1290 1291 ssize_t do_get_acl(struct mnt_idmap *idmap, struct dentry *dentry, 1292 const char *acl_name, void *kvalue, size_t size) 1293 { 1294 ssize_t error; 1295 struct posix_acl *acl; 1296 1297 acl = vfs_get_acl(idmap, dentry, acl_name); 1298 if (IS_ERR(acl)) 1299 return PTR_ERR(acl); 1300 1301 error = vfs_posix_acl_to_xattr(idmap, d_inode(dentry), 1302 acl, kvalue, size); 1303 posix_acl_release(acl); 1304 return error; 1305 } 1306