160bcc88aSSeth Forshee /*
260bcc88aSSeth Forshee * FUSE: Filesystem in Userspace
360bcc88aSSeth Forshee * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com>
460bcc88aSSeth Forshee *
560bcc88aSSeth Forshee * This program can be distributed under the terms of the GNU GPL.
660bcc88aSSeth Forshee * See the file COPYING.
760bcc88aSSeth Forshee */
860bcc88aSSeth Forshee
960bcc88aSSeth Forshee #include "fuse_i.h"
1060bcc88aSSeth Forshee
1160bcc88aSSeth Forshee #include <linux/posix_acl.h>
1260bcc88aSSeth Forshee #include <linux/posix_acl_xattr.h>
1360bcc88aSSeth Forshee
__fuse_get_acl(struct fuse_conn * fc,struct mnt_idmap * idmap,struct inode * inode,int type,bool rcu)14facd6105SChristian Brauner static struct posix_acl *__fuse_get_acl(struct fuse_conn *fc,
15*05e6295fSLinus Torvalds struct mnt_idmap *idmap,
16facd6105SChristian Brauner struct inode *inode, int type, bool rcu)
1760bcc88aSSeth Forshee {
1860bcc88aSSeth Forshee int size;
1960bcc88aSSeth Forshee const char *name;
2060bcc88aSSeth Forshee void *value = NULL;
2160bcc88aSSeth Forshee struct posix_acl *acl;
2260bcc88aSSeth Forshee
230cad6246SMiklos Szeredi if (rcu)
240cad6246SMiklos Szeredi return ERR_PTR(-ECHILD);
250cad6246SMiklos Szeredi
265d069dbeSMiklos Szeredi if (fuse_is_bad(inode))
275d069dbeSMiklos Szeredi return ERR_PTR(-EIO);
285d069dbeSMiklos Szeredi
29facd6105SChristian Brauner if (fc->no_getxattr)
3060bcc88aSSeth Forshee return NULL;
3160bcc88aSSeth Forshee
3260bcc88aSSeth Forshee if (type == ACL_TYPE_ACCESS)
3360bcc88aSSeth Forshee name = XATTR_NAME_POSIX_ACL_ACCESS;
3460bcc88aSSeth Forshee else if (type == ACL_TYPE_DEFAULT)
3560bcc88aSSeth Forshee name = XATTR_NAME_POSIX_ACL_DEFAULT;
3660bcc88aSSeth Forshee else
3760bcc88aSSeth Forshee return ERR_PTR(-EOPNOTSUPP);
3860bcc88aSSeth Forshee
3960bcc88aSSeth Forshee value = kmalloc(PAGE_SIZE, GFP_KERNEL);
4060bcc88aSSeth Forshee if (!value)
4160bcc88aSSeth Forshee return ERR_PTR(-ENOMEM);
4260bcc88aSSeth Forshee size = fuse_getxattr(inode, name, value, PAGE_SIZE);
4360bcc88aSSeth Forshee if (size > 0)
448cb08329SEric W. Biederman acl = posix_acl_from_xattr(fc->user_ns, value, size);
4560bcc88aSSeth Forshee else if ((size == 0) || (size == -ENODATA) ||
4660bcc88aSSeth Forshee (size == -EOPNOTSUPP && fc->no_getxattr))
4760bcc88aSSeth Forshee acl = NULL;
4860bcc88aSSeth Forshee else if (size == -ERANGE)
4960bcc88aSSeth Forshee acl = ERR_PTR(-E2BIG);
5060bcc88aSSeth Forshee else
5160bcc88aSSeth Forshee acl = ERR_PTR(size);
5260bcc88aSSeth Forshee
5360bcc88aSSeth Forshee kfree(value);
5460bcc88aSSeth Forshee return acl;
5560bcc88aSSeth Forshee }
5660bcc88aSSeth Forshee
fuse_no_acl(const struct fuse_conn * fc,const struct inode * inode)57facd6105SChristian Brauner static inline bool fuse_no_acl(const struct fuse_conn *fc,
58facd6105SChristian Brauner const struct inode *inode)
59facd6105SChristian Brauner {
60facd6105SChristian Brauner /*
61facd6105SChristian Brauner * Refuse interacting with POSIX ACLs for daemons that
62facd6105SChristian Brauner * don't support FUSE_POSIX_ACL and are not mounted on
63facd6105SChristian Brauner * the host to retain backwards compatibility.
64facd6105SChristian Brauner */
65facd6105SChristian Brauner return !fc->posix_acl && (i_user_ns(inode) != &init_user_ns);
66facd6105SChristian Brauner }
67facd6105SChristian Brauner
fuse_get_acl(struct mnt_idmap * idmap,struct dentry * dentry,int type)68*05e6295fSLinus Torvalds struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap,
69facd6105SChristian Brauner struct dentry *dentry, int type)
70facd6105SChristian Brauner {
71facd6105SChristian Brauner struct inode *inode = d_inode(dentry);
72facd6105SChristian Brauner struct fuse_conn *fc = get_fuse_conn(inode);
73facd6105SChristian Brauner
74facd6105SChristian Brauner if (fuse_no_acl(fc, inode))
75facd6105SChristian Brauner return ERR_PTR(-EOPNOTSUPP);
76facd6105SChristian Brauner
77*05e6295fSLinus Torvalds return __fuse_get_acl(fc, idmap, inode, type, false);
78facd6105SChristian Brauner }
79facd6105SChristian Brauner
fuse_get_inode_acl(struct inode * inode,int type,bool rcu)80facd6105SChristian Brauner struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu)
81facd6105SChristian Brauner {
82facd6105SChristian Brauner struct fuse_conn *fc = get_fuse_conn(inode);
83facd6105SChristian Brauner
84facd6105SChristian Brauner /*
85facd6105SChristian Brauner * FUSE daemons before FUSE_POSIX_ACL was introduced could get and set
86facd6105SChristian Brauner * POSIX ACLs without them being used for permission checking by the
87facd6105SChristian Brauner * vfs. Retain that behavior for backwards compatibility as there are
88facd6105SChristian Brauner * filesystems that do all permission checking for acls in the daemon
89facd6105SChristian Brauner * and not in the kernel.
90facd6105SChristian Brauner */
91facd6105SChristian Brauner if (!fc->posix_acl)
92facd6105SChristian Brauner return NULL;
93facd6105SChristian Brauner
94*05e6295fSLinus Torvalds return __fuse_get_acl(fc, &nop_mnt_idmap, inode, type, rcu);
95facd6105SChristian Brauner }
96facd6105SChristian Brauner
fuse_set_acl(struct mnt_idmap * idmap,struct dentry * dentry,struct posix_acl * acl,int type)9713e83a49SChristian Brauner int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
98549c7297SChristian Brauner struct posix_acl *acl, int type)
9960bcc88aSSeth Forshee {
100138060baSChristian Brauner struct inode *inode = d_inode(dentry);
10160bcc88aSSeth Forshee struct fuse_conn *fc = get_fuse_conn(inode);
10260bcc88aSSeth Forshee const char *name;
10360bcc88aSSeth Forshee int ret;
10460bcc88aSSeth Forshee
1055d069dbeSMiklos Szeredi if (fuse_is_bad(inode))
1065d069dbeSMiklos Szeredi return -EIO;
1075d069dbeSMiklos Szeredi
108facd6105SChristian Brauner if (fc->no_setxattr || fuse_no_acl(fc, inode))
10960bcc88aSSeth Forshee return -EOPNOTSUPP;
11060bcc88aSSeth Forshee
11160bcc88aSSeth Forshee if (type == ACL_TYPE_ACCESS)
11260bcc88aSSeth Forshee name = XATTR_NAME_POSIX_ACL_ACCESS;
11360bcc88aSSeth Forshee else if (type == ACL_TYPE_DEFAULT)
11460bcc88aSSeth Forshee name = XATTR_NAME_POSIX_ACL_DEFAULT;
11560bcc88aSSeth Forshee else
11660bcc88aSSeth Forshee return -EINVAL;
11760bcc88aSSeth Forshee
11860bcc88aSSeth Forshee if (acl) {
119550a7d3bSVivek Goyal unsigned int extra_flags = 0;
12060bcc88aSSeth Forshee /*
12160bcc88aSSeth Forshee * Fuse userspace is responsible for updating access
12260bcc88aSSeth Forshee * permissions in the inode, if needed. fuse_setxattr
12360bcc88aSSeth Forshee * invalidates the inode attributes, which will force
12460bcc88aSSeth Forshee * them to be refreshed the next time they are used,
12560bcc88aSSeth Forshee * and it also updates i_ctime.
12660bcc88aSSeth Forshee */
12760bcc88aSSeth Forshee size_t size = posix_acl_xattr_size(acl->a_count);
12860bcc88aSSeth Forshee void *value;
12960bcc88aSSeth Forshee
13060bcc88aSSeth Forshee if (size > PAGE_SIZE)
13160bcc88aSSeth Forshee return -E2BIG;
13260bcc88aSSeth Forshee
13360bcc88aSSeth Forshee value = kmalloc(size, GFP_KERNEL);
13460bcc88aSSeth Forshee if (!value)
13560bcc88aSSeth Forshee return -ENOMEM;
13660bcc88aSSeth Forshee
1378cb08329SEric W. Biederman ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
13860bcc88aSSeth Forshee if (ret < 0) {
13960bcc88aSSeth Forshee kfree(value);
14060bcc88aSSeth Forshee return ret;
14160bcc88aSSeth Forshee }
14260bcc88aSSeth Forshee
143facd6105SChristian Brauner /*
144facd6105SChristian Brauner * Fuse daemons without FUSE_POSIX_ACL never changed the passed
145facd6105SChristian Brauner * through POSIX ACLs. Such daemons don't expect setgid bits to
146facd6105SChristian Brauner * be stripped.
147facd6105SChristian Brauner */
148facd6105SChristian Brauner if (fc->posix_acl &&
149*05e6295fSLinus Torvalds !vfsgid_in_group_p(i_gid_into_vfsgid(&nop_mnt_idmap, inode)) &&
1509452e93eSChristian Brauner !capable_wrt_inode_uidgid(&nop_mnt_idmap, inode, CAP_FSETID))
151550a7d3bSVivek Goyal extra_flags |= FUSE_SETXATTR_ACL_KILL_SGID;
152550a7d3bSVivek Goyal
153550a7d3bSVivek Goyal ret = fuse_setxattr(inode, name, value, size, 0, extra_flags);
15460bcc88aSSeth Forshee kfree(value);
15560bcc88aSSeth Forshee } else {
15660bcc88aSSeth Forshee ret = fuse_removexattr(inode, name);
15760bcc88aSSeth Forshee }
158facd6105SChristian Brauner
159facd6105SChristian Brauner if (fc->posix_acl) {
160facd6105SChristian Brauner /*
161facd6105SChristian Brauner * Fuse daemons without FUSE_POSIX_ACL never cached POSIX ACLs
162facd6105SChristian Brauner * and didn't invalidate attributes. Retain that behavior.
163facd6105SChristian Brauner */
16460bcc88aSSeth Forshee forget_all_cached_acls(inode);
16560bcc88aSSeth Forshee fuse_invalidate_attr(inode);
166facd6105SChristian Brauner }
16760bcc88aSSeth Forshee
16860bcc88aSSeth Forshee return ret;
16960bcc88aSSeth Forshee }
170