Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 1 | /* |
| 2 | * FUSE: Filesystem in Userspace |
| 3 | * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com> |
| 4 | * |
| 5 | * This program can be distributed under the terms of the GNU GPL. |
| 6 | * See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | |
| 11 | #include <linux/posix_acl.h> |
| 12 | #include <linux/posix_acl_xattr.h> |
| 13 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 14 | struct posix_acl *fuse_get_acl(struct inode *inode, int type, bool rcu) |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 15 | { |
| 16 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 17 | int size; |
| 18 | const char *name; |
| 19 | void *value = NULL; |
| 20 | struct posix_acl *acl; |
| 21 | |
Miklos Szeredi | 0cad624 | 2021-08-18 22:08:24 +0200 | [diff] [blame] | 22 | if (rcu) |
| 23 | return ERR_PTR(-ECHILD); |
| 24 | |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 25 | if (fuse_is_bad(inode)) |
| 26 | return ERR_PTR(-EIO); |
| 27 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 28 | if (!fc->posix_acl || fc->no_getxattr) |
| 29 | return NULL; |
| 30 | |
| 31 | if (type == ACL_TYPE_ACCESS) |
| 32 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
| 33 | else if (type == ACL_TYPE_DEFAULT) |
| 34 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
| 35 | else |
| 36 | return ERR_PTR(-EOPNOTSUPP); |
| 37 | |
| 38 | value = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 39 | if (!value) |
| 40 | return ERR_PTR(-ENOMEM); |
| 41 | size = fuse_getxattr(inode, name, value, PAGE_SIZE); |
| 42 | if (size > 0) |
Eric W. Biederman | 8cb0832 | 2018-02-21 11:18:07 -0600 | [diff] [blame] | 43 | acl = posix_acl_from_xattr(fc->user_ns, value, size); |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 44 | else if ((size == 0) || (size == -ENODATA) || |
| 45 | (size == -EOPNOTSUPP && fc->no_getxattr)) |
| 46 | acl = NULL; |
| 47 | else if (size == -ERANGE) |
| 48 | acl = ERR_PTR(-E2BIG); |
| 49 | else |
| 50 | acl = ERR_PTR(size); |
| 51 | |
| 52 | kfree(value); |
| 53 | return acl; |
| 54 | } |
| 55 | |
Christian Brauner | 549c729 | 2021-01-21 14:19:43 +0100 | [diff] [blame] | 56 | int fuse_set_acl(struct user_namespace *mnt_userns, struct inode *inode, |
| 57 | struct posix_acl *acl, int type) |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 58 | { |
| 59 | struct fuse_conn *fc = get_fuse_conn(inode); |
| 60 | const char *name; |
| 61 | int ret; |
| 62 | |
Miklos Szeredi | 5d069db | 2020-12-10 15:33:14 +0100 | [diff] [blame] | 63 | if (fuse_is_bad(inode)) |
| 64 | return -EIO; |
| 65 | |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 66 | if (!fc->posix_acl || fc->no_setxattr) |
| 67 | return -EOPNOTSUPP; |
| 68 | |
| 69 | if (type == ACL_TYPE_ACCESS) |
| 70 | name = XATTR_NAME_POSIX_ACL_ACCESS; |
| 71 | else if (type == ACL_TYPE_DEFAULT) |
| 72 | name = XATTR_NAME_POSIX_ACL_DEFAULT; |
| 73 | else |
| 74 | return -EINVAL; |
| 75 | |
| 76 | if (acl) { |
Vivek Goyal | 550a7d3 | 2021-03-25 11:18:23 -0400 | [diff] [blame] | 77 | unsigned int extra_flags = 0; |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 78 | /* |
| 79 | * Fuse userspace is responsible for updating access |
| 80 | * permissions in the inode, if needed. fuse_setxattr |
| 81 | * invalidates the inode attributes, which will force |
| 82 | * them to be refreshed the next time they are used, |
| 83 | * and it also updates i_ctime. |
| 84 | */ |
| 85 | size_t size = posix_acl_xattr_size(acl->a_count); |
| 86 | void *value; |
| 87 | |
| 88 | if (size > PAGE_SIZE) |
| 89 | return -E2BIG; |
| 90 | |
| 91 | value = kmalloc(size, GFP_KERNEL); |
| 92 | if (!value) |
| 93 | return -ENOMEM; |
| 94 | |
Eric W. Biederman | 8cb0832 | 2018-02-21 11:18:07 -0600 | [diff] [blame] | 95 | ret = posix_acl_to_xattr(fc->user_ns, acl, value, size); |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 96 | if (ret < 0) { |
| 97 | kfree(value); |
| 98 | return ret; |
| 99 | } |
| 100 | |
Vivek Goyal | 550a7d3 | 2021-03-25 11:18:23 -0400 | [diff] [blame] | 101 | if (!in_group_p(i_gid_into_mnt(&init_user_ns, inode)) && |
| 102 | !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID)) |
| 103 | extra_flags |= FUSE_SETXATTR_ACL_KILL_SGID; |
| 104 | |
| 105 | ret = fuse_setxattr(inode, name, value, size, 0, extra_flags); |
Seth Forshee | 60bcc88 | 2016-08-29 08:46:37 -0500 | [diff] [blame] | 106 | kfree(value); |
| 107 | } else { |
| 108 | ret = fuse_removexattr(inode, name); |
| 109 | } |
| 110 | forget_all_cached_acls(inode); |
| 111 | fuse_invalidate_attr(inode); |
| 112 | |
| 113 | return ret; |
| 114 | } |