^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * FUSE: Filesystem in Userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2016 Canonical Ltd. <seth.forshee@canonical.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This program can be distributed under the terms of the GNU GPL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * See the file COPYING.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "fuse_i.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/posix_acl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/posix_acl_xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) struct posix_acl *fuse_get_acl(struct inode *inode, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) void *value = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct posix_acl *acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) if (!fc->posix_acl || fc->no_getxattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (type == ACL_TYPE_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) name = XATTR_NAME_POSIX_ACL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) else if (type == ACL_TYPE_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) name = XATTR_NAME_POSIX_ACL_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return ERR_PTR(-EOPNOTSUPP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) value = kmalloc(PAGE_SIZE, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) size = fuse_getxattr(inode, name, value, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) acl = posix_acl_from_xattr(fc->user_ns, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) else if ((size == 0) || (size == -ENODATA) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) (size == -EOPNOTSUPP && fc->no_getxattr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) acl = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) else if (size == -ERANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) acl = ERR_PTR(-E2BIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) acl = ERR_PTR(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return acl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct fuse_conn *fc = get_fuse_conn(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (fuse_is_bad(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (!fc->posix_acl || fc->no_setxattr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (type == ACL_TYPE_ACCESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) name = XATTR_NAME_POSIX_ACL_ACCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) else if (type == ACL_TYPE_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) name = XATTR_NAME_POSIX_ACL_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (acl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * Fuse userspace is responsible for updating access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * permissions in the inode, if needed. fuse_setxattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * invalidates the inode attributes, which will force
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * them to be refreshed the next time they are used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * and it also updates i_ctime.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) size_t size = posix_acl_xattr_size(acl->a_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) void *value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (size > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) value = kmalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ret = fuse_setxattr(inode, name, value, size, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ret = fuse_removexattr(inode, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) forget_all_cached_acls(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) fuse_invalidate_attr(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }