xattr_security.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/hfsplus/xattr_trusted.c
  4. *
  5. * Vyacheslav Dubeyko <[email protected]>
  6. *
  7. * Handler for storing security labels as extended attributes.
  8. */
  9. #include <linux/security.h>
  10. #include <linux/nls.h>
  11. #include "hfsplus_fs.h"
  12. #include "xattr.h"
  13. static int hfsplus_security_getxattr(const struct xattr_handler *handler,
  14. struct dentry *unused, struct inode *inode,
  15. const char *name, void *buffer, size_t size)
  16. {
  17. return hfsplus_getxattr(inode, name, buffer, size,
  18. XATTR_SECURITY_PREFIX,
  19. XATTR_SECURITY_PREFIX_LEN);
  20. }
  21. static int hfsplus_security_setxattr(const struct xattr_handler *handler,
  22. struct user_namespace *mnt_userns,
  23. struct dentry *unused, struct inode *inode,
  24. const char *name, const void *buffer,
  25. size_t size, int flags)
  26. {
  27. return hfsplus_setxattr(inode, name, buffer, size, flags,
  28. XATTR_SECURITY_PREFIX,
  29. XATTR_SECURITY_PREFIX_LEN);
  30. }
  31. static int hfsplus_initxattrs(struct inode *inode,
  32. const struct xattr *xattr_array,
  33. void *fs_info)
  34. {
  35. const struct xattr *xattr;
  36. char *xattr_name;
  37. int err = 0;
  38. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  39. GFP_KERNEL);
  40. if (!xattr_name)
  41. return -ENOMEM;
  42. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  43. if (!strcmp(xattr->name, ""))
  44. continue;
  45. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  46. strcpy(xattr_name +
  47. XATTR_SECURITY_PREFIX_LEN, xattr->name);
  48. memset(xattr_name +
  49. XATTR_SECURITY_PREFIX_LEN + strlen(xattr->name), 0, 1);
  50. err = __hfsplus_setxattr(inode, xattr_name,
  51. xattr->value, xattr->value_len, 0);
  52. if (err)
  53. break;
  54. }
  55. kfree(xattr_name);
  56. return err;
  57. }
  58. int hfsplus_init_security(struct inode *inode, struct inode *dir,
  59. const struct qstr *qstr)
  60. {
  61. return security_inode_init_security(inode, dir, qstr,
  62. &hfsplus_initxattrs, NULL);
  63. }
  64. const struct xattr_handler hfsplus_xattr_security_handler = {
  65. .prefix = XATTR_SECURITY_PREFIX,
  66. .get = hfsplus_security_getxattr,
  67. .set = hfsplus_security_setxattr,
  68. };