xattr_security.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * linux/fs/hfsplus/xattr_trusted.c
  3. *
  4. * Vyacheslav Dubeyko <slava@dubeyko.com>
  5. *
  6. * Handler for storing security labels as extended attributes.
  7. */
  8. #include <linux/security.h>
  9. #include "hfsplus_fs.h"
  10. #include "xattr.h"
  11. #include "acl.h"
  12. static int hfsplus_security_getxattr(struct dentry *dentry, const char *name,
  13. void *buffer, size_t size, int type)
  14. {
  15. char xattr_name[HFSPLUS_ATTR_MAX_STRLEN + 1] = {0};
  16. size_t len = strlen(name);
  17. if (!strcmp(name, ""))
  18. return -EINVAL;
  19. if (len + XATTR_SECURITY_PREFIX_LEN > HFSPLUS_ATTR_MAX_STRLEN)
  20. return -EOPNOTSUPP;
  21. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  22. strcpy(xattr_name + XATTR_SECURITY_PREFIX_LEN, name);
  23. return hfsplus_getxattr(dentry, xattr_name, buffer, size);
  24. }
  25. static int hfsplus_security_setxattr(struct dentry *dentry, const char *name,
  26. const void *buffer, size_t size, int flags, int type)
  27. {
  28. char xattr_name[HFSPLUS_ATTR_MAX_STRLEN + 1] = {0};
  29. size_t len = strlen(name);
  30. if (!strcmp(name, ""))
  31. return -EINVAL;
  32. if (len + XATTR_SECURITY_PREFIX_LEN > HFSPLUS_ATTR_MAX_STRLEN)
  33. return -EOPNOTSUPP;
  34. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  35. strcpy(xattr_name + XATTR_SECURITY_PREFIX_LEN, name);
  36. return hfsplus_setxattr(dentry, xattr_name, buffer, size, flags);
  37. }
  38. static size_t hfsplus_security_listxattr(struct dentry *dentry, char *list,
  39. size_t list_size, const char *name, size_t name_len, int type)
  40. {
  41. /*
  42. * This method is not used.
  43. * It is used hfsplus_listxattr() instead of generic_listxattr().
  44. */
  45. return -EOPNOTSUPP;
  46. }
  47. static int hfsplus_initxattrs(struct inode *inode,
  48. const struct xattr *xattr_array,
  49. void *fs_info)
  50. {
  51. const struct xattr *xattr;
  52. char xattr_name[HFSPLUS_ATTR_MAX_STRLEN + 1] = {0};
  53. size_t xattr_name_len;
  54. int err = 0;
  55. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  56. xattr_name_len = strlen(xattr->name);
  57. if (xattr_name_len == 0)
  58. continue;
  59. if (xattr_name_len + XATTR_SECURITY_PREFIX_LEN >
  60. HFSPLUS_ATTR_MAX_STRLEN)
  61. return -EOPNOTSUPP;
  62. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  63. strcpy(xattr_name +
  64. XATTR_SECURITY_PREFIX_LEN, xattr->name);
  65. memset(xattr_name +
  66. XATTR_SECURITY_PREFIX_LEN + xattr_name_len, 0, 1);
  67. err = __hfsplus_setxattr(inode, xattr_name,
  68. xattr->value, xattr->value_len, 0);
  69. if (err)
  70. break;
  71. }
  72. return err;
  73. }
  74. int hfsplus_init_security(struct inode *inode, struct inode *dir,
  75. const struct qstr *qstr)
  76. {
  77. return security_inode_init_security(inode, dir, qstr,
  78. &hfsplus_initxattrs, NULL);
  79. }
  80. int hfsplus_init_inode_security(struct inode *inode,
  81. struct inode *dir,
  82. const struct qstr *qstr)
  83. {
  84. int err;
  85. err = hfsplus_init_posix_acl(inode, dir);
  86. if (!err)
  87. err = hfsplus_init_security(inode, dir, qstr);
  88. return err;
  89. }
  90. const struct xattr_handler hfsplus_xattr_security_handler = {
  91. .prefix = XATTR_SECURITY_PREFIX,
  92. .list = hfsplus_security_listxattr,
  93. .get = hfsplus_security_getxattr,
  94. .set = hfsplus_security_setxattr,
  95. };