2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com>
5 * This program can be distributed under the terms of the GNU GPL.
6 * See the file COPYING.
11 #include <linux/posix_acl.h>
12 #include <linux/posix_acl_xattr.h>
14 static struct posix_acl *__fuse_get_acl(struct fuse_conn *fc,
15 struct inode *inode, int type, bool rcu)
20 struct posix_acl *acl;
23 return ERR_PTR(-ECHILD);
25 if (fuse_is_bad(inode))
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;
36 return ERR_PTR(-EOPNOTSUPP);
38 value = kmalloc(PAGE_SIZE, GFP_KERNEL);
40 return ERR_PTR(-ENOMEM);
41 size = fuse_getxattr(inode, name, value, PAGE_SIZE);
43 acl = posix_acl_from_xattr(fc->user_ns, value, size);
44 else if ((size == 0) || (size == -ENODATA) ||
45 (size == -EOPNOTSUPP && fc->no_getxattr))
47 else if (size == -ERANGE)
48 acl = ERR_PTR(-E2BIG);
56 static inline bool fuse_no_acl(const struct fuse_conn *fc,
57 const struct inode *inode)
60 * Refuse interacting with POSIX ACLs for daemons that
61 * don't support FUSE_POSIX_ACL and are not mounted on
62 * the host to retain backwards compatibility.
64 return !fc->posix_acl && (i_user_ns(inode) != &init_user_ns);
67 struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap,
68 struct dentry *dentry, int type)
70 struct inode *inode = d_inode(dentry);
71 struct fuse_conn *fc = get_fuse_conn(inode);
73 if (fuse_no_acl(fc, inode))
74 return ERR_PTR(-EOPNOTSUPP);
76 return __fuse_get_acl(fc, inode, type, false);
79 struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu)
81 struct fuse_conn *fc = get_fuse_conn(inode);
84 * FUSE daemons before FUSE_POSIX_ACL was introduced could get and set
85 * POSIX ACLs without them being used for permission checking by the
86 * vfs. Retain that behavior for backwards compatibility as there are
87 * filesystems that do all permission checking for acls in the daemon
88 * and not in the kernel.
92 return __fuse_get_acl(fc, inode, type, rcu);
95 int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
96 struct posix_acl *acl, int type)
98 struct inode *inode = d_inode(dentry);
99 struct fuse_conn *fc = get_fuse_conn(inode);
103 if (fuse_is_bad(inode))
106 if (fc->no_setxattr || fuse_no_acl(fc, inode))
109 if (type == ACL_TYPE_ACCESS)
110 name = XATTR_NAME_POSIX_ACL_ACCESS;
111 else if (type == ACL_TYPE_DEFAULT)
112 name = XATTR_NAME_POSIX_ACL_DEFAULT;
117 unsigned int extra_flags = 0;
119 * Fuse userspace is responsible for updating access
120 * permissions in the inode, if needed. fuse_setxattr
121 * invalidates the inode attributes, which will force
122 * them to be refreshed the next time they are used,
123 * and it also updates i_ctime.
125 size_t size = posix_acl_xattr_size(acl->a_count);
128 if (size > PAGE_SIZE)
131 value = kmalloc(size, GFP_KERNEL);
135 ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
142 * Fuse daemons without FUSE_POSIX_ACL never changed the passed
143 * through POSIX ACLs. Such daemons don't expect setgid bits to
147 !in_group_or_capable(idmap, inode,
148 i_gid_into_vfsgid(idmap, inode)))
149 extra_flags |= FUSE_SETXATTR_ACL_KILL_SGID;
151 ret = fuse_setxattr(inode, name, value, size, 0, extra_flags);
154 ret = fuse_removexattr(inode, name);
159 * Fuse daemons without FUSE_POSIX_ACL never cached POSIX ACLs
160 * and didn't invalidate attributes. Retain that behavior.
162 forget_all_cached_acls(inode);
163 fuse_invalidate_attr(inode);