acl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #include "fuse_i.h"
  9. #include <linux/posix_acl.h>
  10. #include <linux/posix_acl_xattr.h>
  11. static struct posix_acl *__fuse_get_acl(struct fuse_conn *fc,
  12. struct inode *inode, int type, bool rcu)
  13. {
  14. int size;
  15. const char *name;
  16. void *value = NULL;
  17. struct posix_acl *acl;
  18. if (rcu)
  19. return ERR_PTR(-ECHILD);
  20. if (fuse_is_bad(inode))
  21. return ERR_PTR(-EIO);
  22. if (fc->no_getxattr)
  23. return NULL;
  24. if (type == ACL_TYPE_ACCESS)
  25. name = XATTR_NAME_POSIX_ACL_ACCESS;
  26. else if (type == ACL_TYPE_DEFAULT)
  27. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  28. else
  29. return ERR_PTR(-EOPNOTSUPP);
  30. value = kmalloc(PAGE_SIZE, GFP_KERNEL);
  31. if (!value)
  32. return ERR_PTR(-ENOMEM);
  33. size = fuse_getxattr(inode, name, value, PAGE_SIZE);
  34. if (size > 0)
  35. acl = posix_acl_from_xattr(fc->user_ns, value, size);
  36. else if ((size == 0) || (size == -ENODATA) ||
  37. (size == -EOPNOTSUPP && fc->no_getxattr))
  38. acl = NULL;
  39. else if (size == -ERANGE)
  40. acl = ERR_PTR(-E2BIG);
  41. else
  42. acl = ERR_PTR(size);
  43. kfree(value);
  44. return acl;
  45. }
  46. static inline bool fuse_no_acl(const struct fuse_conn *fc,
  47. const struct inode *inode)
  48. {
  49. /*
  50. * Refuse interacting with POSIX ACLs for daemons that
  51. * don't support FUSE_POSIX_ACL and are not mounted on
  52. * the host to retain backwards compatibility.
  53. */
  54. return !fc->posix_acl && (i_user_ns(inode) != &init_user_ns);
  55. }
  56. struct posix_acl *fuse_get_acl(struct mnt_idmap *idmap,
  57. struct dentry *dentry, int type)
  58. {
  59. struct inode *inode = d_inode(dentry);
  60. struct fuse_conn *fc = get_fuse_conn(inode);
  61. if (fuse_no_acl(fc, inode))
  62. return ERR_PTR(-EOPNOTSUPP);
  63. return __fuse_get_acl(fc, inode, type, false);
  64. }
  65. struct posix_acl *fuse_get_inode_acl(struct inode *inode, int type, bool rcu)
  66. {
  67. struct fuse_conn *fc = get_fuse_conn(inode);
  68. /*
  69. * FUSE daemons before FUSE_POSIX_ACL was introduced could get and set
  70. * POSIX ACLs without them being used for permission checking by the
  71. * vfs. Retain that behavior for backwards compatibility as there are
  72. * filesystems that do all permission checking for acls in the daemon
  73. * and not in the kernel.
  74. */
  75. if (!fc->posix_acl)
  76. return NULL;
  77. return __fuse_get_acl(fc, inode, type, rcu);
  78. }
  79. int fuse_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  80. struct posix_acl *acl, int type)
  81. {
  82. struct inode *inode = d_inode(dentry);
  83. struct fuse_conn *fc = get_fuse_conn(inode);
  84. const char *name;
  85. int ret;
  86. if (fuse_is_bad(inode))
  87. return -EIO;
  88. if (fc->no_setxattr || fuse_no_acl(fc, inode))
  89. return -EOPNOTSUPP;
  90. if (type == ACL_TYPE_ACCESS)
  91. name = XATTR_NAME_POSIX_ACL_ACCESS;
  92. else if (type == ACL_TYPE_DEFAULT)
  93. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  94. else
  95. return -EINVAL;
  96. if (acl) {
  97. unsigned int extra_flags = 0;
  98. /*
  99. * Fuse userspace is responsible for updating access
  100. * permissions in the inode, if needed. fuse_setxattr
  101. * invalidates the inode attributes, which will force
  102. * them to be refreshed the next time they are used,
  103. * and it also updates i_ctime.
  104. */
  105. size_t size = posix_acl_xattr_size(acl->a_count);
  106. void *value;
  107. if (size > PAGE_SIZE)
  108. return -E2BIG;
  109. value = kmalloc(size, GFP_KERNEL);
  110. if (!value)
  111. return -ENOMEM;
  112. ret = posix_acl_to_xattr(fc->user_ns, acl, value, size);
  113. if (ret < 0) {
  114. kfree(value);
  115. return ret;
  116. }
  117. /*
  118. * Fuse daemons without FUSE_POSIX_ACL never changed the passed
  119. * through POSIX ACLs. Such daemons don't expect setgid bits to
  120. * be stripped.
  121. */
  122. if (fc->posix_acl &&
  123. !in_group_or_capable(idmap, inode,
  124. i_gid_into_vfsgid(idmap, inode)))
  125. extra_flags |= FUSE_SETXATTR_ACL_KILL_SGID;
  126. ret = fuse_setxattr(inode, name, value, size, 0, extra_flags);
  127. kfree(value);
  128. } else {
  129. ret = fuse_removexattr(inode, name);
  130. }
  131. if (fc->posix_acl) {
  132. /*
  133. * Fuse daemons without FUSE_POSIX_ACL never cached POSIX ACLs
  134. * and didn't invalidate attributes. Retain that behavior.
  135. */
  136. forget_all_cached_acls(inode);
  137. fuse_invalidate_attr(inode);
  138. }
  139. return ret;
  140. }