Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * FUSE: Filesystem in Userspace
0003  * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com>
0004  *
0005  * This program can be distributed under the terms of the GNU GPL.
0006  * See the file COPYING.
0007  */
0008 
0009 #include "fuse_i.h"
0010 
0011 #include <linux/posix_acl.h>
0012 #include <linux/posix_acl_xattr.h>
0013 
0014 struct posix_acl *fuse_get_acl(struct inode *inode, int type, bool rcu)
0015 {
0016     struct fuse_conn *fc = get_fuse_conn(inode);
0017     int size;
0018     const char *name;
0019     void *value = NULL;
0020     struct posix_acl *acl;
0021 
0022     if (rcu)
0023         return ERR_PTR(-ECHILD);
0024 
0025     if (fuse_is_bad(inode))
0026         return ERR_PTR(-EIO);
0027 
0028     if (!fc->posix_acl || fc->no_getxattr)
0029         return NULL;
0030 
0031     if (type == ACL_TYPE_ACCESS)
0032         name = XATTR_NAME_POSIX_ACL_ACCESS;
0033     else if (type == ACL_TYPE_DEFAULT)
0034         name = XATTR_NAME_POSIX_ACL_DEFAULT;
0035     else
0036         return ERR_PTR(-EOPNOTSUPP);
0037 
0038     value = kmalloc(PAGE_SIZE, GFP_KERNEL);
0039     if (!value)
0040         return ERR_PTR(-ENOMEM);
0041     size = fuse_getxattr(inode, name, value, PAGE_SIZE);
0042     if (size > 0)
0043         acl = posix_acl_from_xattr(fc->user_ns, value, size);
0044     else if ((size == 0) || (size == -ENODATA) ||
0045          (size == -EOPNOTSUPP && fc->no_getxattr))
0046         acl = NULL;
0047     else if (size == -ERANGE)
0048         acl = ERR_PTR(-E2BIG);
0049     else
0050         acl = ERR_PTR(size);
0051 
0052     kfree(value);
0053     return acl;
0054 }
0055 
0056 int fuse_set_acl(struct user_namespace *mnt_userns, struct inode *inode,
0057          struct posix_acl *acl, int type)
0058 {
0059     struct fuse_conn *fc = get_fuse_conn(inode);
0060     const char *name;
0061     int ret;
0062 
0063     if (fuse_is_bad(inode))
0064         return -EIO;
0065 
0066     if (!fc->posix_acl || fc->no_setxattr)
0067         return -EOPNOTSUPP;
0068 
0069     if (type == ACL_TYPE_ACCESS)
0070         name = XATTR_NAME_POSIX_ACL_ACCESS;
0071     else if (type == ACL_TYPE_DEFAULT)
0072         name = XATTR_NAME_POSIX_ACL_DEFAULT;
0073     else
0074         return -EINVAL;
0075 
0076     if (acl) {
0077         unsigned int extra_flags = 0;
0078         /*
0079          * Fuse userspace is responsible for updating access
0080          * permissions in the inode, if needed. fuse_setxattr
0081          * invalidates the inode attributes, which will force
0082          * them to be refreshed the next time they are used,
0083          * and it also updates i_ctime.
0084          */
0085         size_t size = posix_acl_xattr_size(acl->a_count);
0086         void *value;
0087 
0088         if (size > PAGE_SIZE)
0089             return -E2BIG;
0090 
0091         value = kmalloc(size, GFP_KERNEL);
0092         if (!value)
0093             return -ENOMEM;
0094 
0095         ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
0096         if (ret < 0) {
0097             kfree(value);
0098             return ret;
0099         }
0100 
0101         if (!in_group_p(i_gid_into_mnt(&init_user_ns, inode)) &&
0102             !capable_wrt_inode_uidgid(&init_user_ns, inode, CAP_FSETID))
0103             extra_flags |= FUSE_SETXATTR_ACL_KILL_SGID;
0104 
0105         ret = fuse_setxattr(inode, name, value, size, 0, extra_flags);
0106         kfree(value);
0107     } else {
0108         ret = fuse_removexattr(inode, name);
0109     }
0110     forget_all_cached_acls(inode);
0111     fuse_invalidate_attr(inode);
0112 
0113     return ret;
0114 }