xfs_xattr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Christoph Hellwig.
  4. * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_format.h"
  8. #include "xfs_log_format.h"
  9. #include "xfs_trans_resv.h"
  10. #include "xfs_mount.h"
  11. #include "xfs_da_format.h"
  12. #include "xfs_inode.h"
  13. #include "xfs_attr.h"
  14. #include "xfs_attr_leaf.h"
  15. #include "xfs_acl.h"
  16. #include <linux/posix_acl_xattr.h>
  17. #include <linux/xattr.h>
  18. static int
  19. xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
  20. struct inode *inode, const char *name, void *value, size_t size)
  21. {
  22. int xflags = handler->flags;
  23. struct xfs_inode *ip = XFS_I(inode);
  24. int error, asize = size;
  25. /* Convert Linux syscall to XFS internal ATTR flags */
  26. if (!size) {
  27. xflags |= ATTR_KERNOVAL;
  28. value = NULL;
  29. }
  30. error = xfs_attr_get(ip, (unsigned char *)name, value, &asize, xflags);
  31. if (error)
  32. return error;
  33. return asize;
  34. }
  35. void
  36. xfs_forget_acl(
  37. struct inode *inode,
  38. const char *name,
  39. int xflags)
  40. {
  41. /*
  42. * Invalidate any cached ACLs if the user has bypassed the ACL
  43. * interface. We don't validate the content whatsoever so it is caller
  44. * responsibility to provide data in valid format and ensure i_mode is
  45. * consistent.
  46. */
  47. if (xflags & ATTR_ROOT) {
  48. #ifdef CONFIG_XFS_POSIX_ACL
  49. if (!strcmp(name, SGI_ACL_FILE))
  50. forget_cached_acl(inode, ACL_TYPE_ACCESS);
  51. else if (!strcmp(name, SGI_ACL_DEFAULT))
  52. forget_cached_acl(inode, ACL_TYPE_DEFAULT);
  53. #endif
  54. }
  55. }
  56. static int
  57. xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused,
  58. struct inode *inode, const char *name, const void *value,
  59. size_t size, int flags)
  60. {
  61. int xflags = handler->flags;
  62. struct xfs_inode *ip = XFS_I(inode);
  63. int error;
  64. /* Convert Linux syscall to XFS internal ATTR flags */
  65. if (flags & XATTR_CREATE)
  66. xflags |= ATTR_CREATE;
  67. if (flags & XATTR_REPLACE)
  68. xflags |= ATTR_REPLACE;
  69. if (!value)
  70. return xfs_attr_remove(ip, (unsigned char *)name, xflags);
  71. error = xfs_attr_set(ip, (unsigned char *)name,
  72. (void *)value, size, xflags);
  73. if (!error)
  74. xfs_forget_acl(inode, name, xflags);
  75. return error;
  76. }
  77. static const struct xattr_handler xfs_xattr_user_handler = {
  78. .prefix = XATTR_USER_PREFIX,
  79. .flags = 0, /* no flags implies user namespace */
  80. .get = xfs_xattr_get,
  81. .set = xfs_xattr_set,
  82. };
  83. static const struct xattr_handler xfs_xattr_trusted_handler = {
  84. .prefix = XATTR_TRUSTED_PREFIX,
  85. .flags = ATTR_ROOT,
  86. .get = xfs_xattr_get,
  87. .set = xfs_xattr_set,
  88. };
  89. static const struct xattr_handler xfs_xattr_security_handler = {
  90. .prefix = XATTR_SECURITY_PREFIX,
  91. .flags = ATTR_SECURE,
  92. .get = xfs_xattr_get,
  93. .set = xfs_xattr_set,
  94. };
  95. const struct xattr_handler *xfs_xattr_handlers[] = {
  96. &xfs_xattr_user_handler,
  97. &xfs_xattr_trusted_handler,
  98. &xfs_xattr_security_handler,
  99. #ifdef CONFIG_XFS_POSIX_ACL
  100. &posix_acl_access_xattr_handler,
  101. &posix_acl_default_xattr_handler,
  102. #endif
  103. NULL
  104. };
  105. static void
  106. __xfs_xattr_put_listent(
  107. struct xfs_attr_list_context *context,
  108. char *prefix,
  109. int prefix_len,
  110. unsigned char *name,
  111. int namelen)
  112. {
  113. char *offset;
  114. int arraytop;
  115. if (context->count < 0 || context->seen_enough)
  116. return;
  117. if (!context->alist)
  118. goto compute_size;
  119. arraytop = context->count + prefix_len + namelen + 1;
  120. if (arraytop > context->firstu) {
  121. context->count = -1; /* insufficient space */
  122. context->seen_enough = 1;
  123. return;
  124. }
  125. offset = (char *)context->alist + context->count;
  126. strncpy(offset, prefix, prefix_len);
  127. offset += prefix_len;
  128. strncpy(offset, (char *)name, namelen); /* real name */
  129. offset += namelen;
  130. *offset = '\0';
  131. compute_size:
  132. context->count += prefix_len + namelen + 1;
  133. return;
  134. }
  135. static void
  136. xfs_xattr_put_listent(
  137. struct xfs_attr_list_context *context,
  138. int flags,
  139. unsigned char *name,
  140. int namelen,
  141. int valuelen)
  142. {
  143. char *prefix;
  144. int prefix_len;
  145. ASSERT(context->count >= 0);
  146. if (flags & XFS_ATTR_ROOT) {
  147. #ifdef CONFIG_XFS_POSIX_ACL
  148. if (namelen == SGI_ACL_FILE_SIZE &&
  149. strncmp(name, SGI_ACL_FILE,
  150. SGI_ACL_FILE_SIZE) == 0) {
  151. __xfs_xattr_put_listent(
  152. context, XATTR_SYSTEM_PREFIX,
  153. XATTR_SYSTEM_PREFIX_LEN,
  154. XATTR_POSIX_ACL_ACCESS,
  155. strlen(XATTR_POSIX_ACL_ACCESS));
  156. } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
  157. strncmp(name, SGI_ACL_DEFAULT,
  158. SGI_ACL_DEFAULT_SIZE) == 0) {
  159. __xfs_xattr_put_listent(
  160. context, XATTR_SYSTEM_PREFIX,
  161. XATTR_SYSTEM_PREFIX_LEN,
  162. XATTR_POSIX_ACL_DEFAULT,
  163. strlen(XATTR_POSIX_ACL_DEFAULT));
  164. }
  165. #endif
  166. /*
  167. * Only show root namespace entries if we are actually allowed to
  168. * see them.
  169. */
  170. if (!capable(CAP_SYS_ADMIN))
  171. return;
  172. prefix = XATTR_TRUSTED_PREFIX;
  173. prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  174. } else if (flags & XFS_ATTR_SECURE) {
  175. prefix = XATTR_SECURITY_PREFIX;
  176. prefix_len = XATTR_SECURITY_PREFIX_LEN;
  177. } else {
  178. prefix = XATTR_USER_PREFIX;
  179. prefix_len = XATTR_USER_PREFIX_LEN;
  180. }
  181. __xfs_xattr_put_listent(context, prefix, prefix_len, name,
  182. namelen);
  183. return;
  184. }
  185. ssize_t
  186. xfs_vn_listxattr(
  187. struct dentry *dentry,
  188. char *data,
  189. size_t size)
  190. {
  191. struct xfs_attr_list_context context;
  192. struct attrlist_cursor_kern cursor = { 0 };
  193. struct inode *inode = d_inode(dentry);
  194. int error;
  195. /*
  196. * First read the regular on-disk attributes.
  197. */
  198. memset(&context, 0, sizeof(context));
  199. context.dp = XFS_I(inode);
  200. context.cursor = &cursor;
  201. context.resynch = 1;
  202. context.alist = size ? data : NULL;
  203. context.bufsize = size;
  204. context.firstu = context.bufsize;
  205. context.put_listent = xfs_xattr_put_listent;
  206. error = xfs_attr_list_int(&context);
  207. if (error)
  208. return error;
  209. if (context.count < 0)
  210. return -ERANGE;
  211. return context.count;
  212. }