xfs_acl.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2008, Christoph Hellwig
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_shared.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_trans_resv.h"
  11. #include "xfs_mount.h"
  12. #include "xfs_inode.h"
  13. #include "xfs_attr.h"
  14. #include "xfs_trace.h"
  15. #include "xfs_error.h"
  16. #include "xfs_acl.h"
  17. #include "xfs_da_format.h"
  18. #include "xfs_da_btree.h"
  19. #include <linux/posix_acl_xattr.h>
  20. /*
  21. * Locking scheme:
  22. * - all ACL updates are protected by inode->i_mutex, which is taken before
  23. * calling into this file.
  24. */
  25. STATIC struct posix_acl *
  26. xfs_acl_from_disk(
  27. struct xfs_mount *mp,
  28. const struct xfs_acl *aclp,
  29. int len,
  30. int max_entries)
  31. {
  32. struct posix_acl_entry *acl_e;
  33. struct posix_acl *acl;
  34. const struct xfs_acl_entry *ace;
  35. unsigned int count, i;
  36. if (len < sizeof(*aclp)) {
  37. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
  38. len);
  39. return ERR_PTR(-EFSCORRUPTED);
  40. }
  41. count = be32_to_cpu(aclp->acl_cnt);
  42. if (count > max_entries || XFS_ACL_SIZE(count) != len) {
  43. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, aclp,
  44. len);
  45. return ERR_PTR(-EFSCORRUPTED);
  46. }
  47. acl = posix_acl_alloc(count, GFP_KERNEL);
  48. if (!acl)
  49. return ERR_PTR(-ENOMEM);
  50. for (i = 0; i < count; i++) {
  51. acl_e = &acl->a_entries[i];
  52. ace = &aclp->acl_entry[i];
  53. /*
  54. * The tag is 32 bits on disk and 16 bits in core.
  55. *
  56. * Because every access to it goes through the core
  57. * format first this is not a problem.
  58. */
  59. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  60. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  61. switch (acl_e->e_tag) {
  62. case ACL_USER:
  63. acl_e->e_uid = make_kuid(&init_user_ns,
  64. be32_to_cpu(ace->ae_id));
  65. break;
  66. case ACL_GROUP:
  67. acl_e->e_gid = make_kgid(&init_user_ns,
  68. be32_to_cpu(ace->ae_id));
  69. break;
  70. case ACL_USER_OBJ:
  71. case ACL_GROUP_OBJ:
  72. case ACL_MASK:
  73. case ACL_OTHER:
  74. break;
  75. default:
  76. goto fail;
  77. }
  78. }
  79. return acl;
  80. fail:
  81. posix_acl_release(acl);
  82. return ERR_PTR(-EINVAL);
  83. }
  84. STATIC void
  85. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  86. {
  87. const struct posix_acl_entry *acl_e;
  88. struct xfs_acl_entry *ace;
  89. int i;
  90. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  91. for (i = 0; i < acl->a_count; i++) {
  92. ace = &aclp->acl_entry[i];
  93. acl_e = &acl->a_entries[i];
  94. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  95. switch (acl_e->e_tag) {
  96. case ACL_USER:
  97. ace->ae_id = cpu_to_be32(
  98. from_kuid(&init_user_ns, acl_e->e_uid));
  99. break;
  100. case ACL_GROUP:
  101. ace->ae_id = cpu_to_be32(
  102. from_kgid(&init_user_ns, acl_e->e_gid));
  103. break;
  104. default:
  105. ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
  106. break;
  107. }
  108. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  109. }
  110. }
  111. struct posix_acl *
  112. xfs_get_acl(struct inode *inode, int type)
  113. {
  114. struct xfs_inode *ip = XFS_I(inode);
  115. struct xfs_mount *mp = ip->i_mount;
  116. struct posix_acl *acl = NULL;
  117. struct xfs_da_args args = {
  118. .dp = ip,
  119. .attr_filter = XFS_ATTR_ROOT,
  120. .valuelen = XFS_ACL_MAX_SIZE(mp),
  121. };
  122. int error;
  123. trace_xfs_get_acl(ip);
  124. switch (type) {
  125. case ACL_TYPE_ACCESS:
  126. args.name = SGI_ACL_FILE;
  127. break;
  128. case ACL_TYPE_DEFAULT:
  129. args.name = SGI_ACL_DEFAULT;
  130. break;
  131. default:
  132. BUG();
  133. }
  134. args.namelen = strlen(args.name);
  135. /*
  136. * If the attribute doesn't exist make sure we have a negative cache
  137. * entry, for any other error assume it is transient.
  138. */
  139. error = xfs_attr_get(&args);
  140. if (!error) {
  141. acl = xfs_acl_from_disk(mp, args.value, args.valuelen,
  142. XFS_ACL_MAX_ENTRIES(mp));
  143. } else if (error != -ENOATTR) {
  144. acl = ERR_PTR(error);
  145. }
  146. kmem_free(args.value);
  147. return acl;
  148. }
  149. int
  150. __xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  151. {
  152. struct xfs_inode *ip = XFS_I(inode);
  153. struct xfs_da_args args = {
  154. .dp = ip,
  155. .attr_filter = XFS_ATTR_ROOT,
  156. };
  157. int error;
  158. switch (type) {
  159. case ACL_TYPE_ACCESS:
  160. args.name = SGI_ACL_FILE;
  161. break;
  162. case ACL_TYPE_DEFAULT:
  163. if (!S_ISDIR(inode->i_mode))
  164. return acl ? -EACCES : 0;
  165. args.name = SGI_ACL_DEFAULT;
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. args.namelen = strlen(args.name);
  171. if (acl) {
  172. args.valuelen = XFS_ACL_SIZE(acl->a_count);
  173. args.value = kvzalloc(args.valuelen, GFP_KERNEL);
  174. if (!args.value)
  175. return -ENOMEM;
  176. xfs_acl_to_disk(args.value, acl);
  177. }
  178. error = xfs_attr_set(&args);
  179. kmem_free(args.value);
  180. /*
  181. * If the attribute didn't exist to start with that's fine.
  182. */
  183. if (!acl && error == -ENOATTR)
  184. error = 0;
  185. if (!error)
  186. set_cached_acl(inode, type, acl);
  187. return error;
  188. }
  189. static int
  190. xfs_set_mode(struct inode *inode, umode_t mode)
  191. {
  192. int error = 0;
  193. if (mode != inode->i_mode) {
  194. struct iattr iattr;
  195. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  196. iattr.ia_mode = mode;
  197. iattr.ia_ctime = current_time(inode);
  198. error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  199. }
  200. return error;
  201. }
  202. int
  203. xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  204. {
  205. umode_t mode;
  206. bool set_mode = false;
  207. int error = 0;
  208. if (!acl)
  209. goto set_acl;
  210. error = -E2BIG;
  211. if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
  212. return error;
  213. if (type == ACL_TYPE_ACCESS) {
  214. error = posix_acl_update_mode(inode, &mode, &acl);
  215. if (error)
  216. return error;
  217. set_mode = true;
  218. }
  219. set_acl:
  220. error = __xfs_set_acl(inode, acl, type);
  221. if (error)
  222. return error;
  223. /*
  224. * We set the mode after successfully updating the ACL xattr because the
  225. * xattr update can fail at ENOSPC and we don't want to change the mode
  226. * if the ACL update hasn't been applied.
  227. */
  228. if (set_mode)
  229. error = xfs_set_mode(inode, mode);
  230. return error;
  231. }
  232. /*
  233. * Invalidate any cached ACLs if the user has bypassed the ACL interface.
  234. * We don't validate the content whatsoever so it is caller responsibility to
  235. * provide data in valid format and ensure i_mode is consistent.
  236. */
  237. void
  238. xfs_forget_acl(
  239. struct inode *inode,
  240. const char *name)
  241. {
  242. if (!strcmp(name, SGI_ACL_FILE))
  243. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  244. else if (!strcmp(name, SGI_ACL_DEFAULT))
  245. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  246. }