xattr_security.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <linux/nls.h>
  10. #include "hfsplus_fs.h"
  11. #include "xattr.h"
  12. #include "acl.h"
  13. static int hfsplus_security_getxattr(struct dentry *dentry, const char *name,
  14. void *buffer, size_t size, int type)
  15. {
  16. char *xattr_name;
  17. int res;
  18. if (!strcmp(name, ""))
  19. return -EINVAL;
  20. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  21. GFP_KERNEL);
  22. if (!xattr_name)
  23. return -ENOMEM;
  24. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  25. strcpy(xattr_name + XATTR_SECURITY_PREFIX_LEN, name);
  26. res = hfsplus_getxattr(dentry, xattr_name, buffer, size);
  27. kfree(xattr_name);
  28. return res;
  29. }
  30. static int hfsplus_security_setxattr(struct dentry *dentry, const char *name,
  31. const void *buffer, size_t size, int flags, int type)
  32. {
  33. char *xattr_name;
  34. int res;
  35. if (!strcmp(name, ""))
  36. return -EINVAL;
  37. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  38. GFP_KERNEL);
  39. if (!xattr_name)
  40. return -ENOMEM;
  41. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  42. strcpy(xattr_name + XATTR_SECURITY_PREFIX_LEN, name);
  43. res = hfsplus_setxattr(dentry, xattr_name, buffer, size, flags);
  44. kfree(xattr_name);
  45. return res;
  46. }
  47. static size_t hfsplus_security_listxattr(struct dentry *dentry, char *list,
  48. size_t list_size, const char *name, size_t name_len, int type)
  49. {
  50. /*
  51. * This method is not used.
  52. * It is used hfsplus_listxattr() instead of generic_listxattr().
  53. */
  54. return -EOPNOTSUPP;
  55. }
  56. static int hfsplus_initxattrs(struct inode *inode,
  57. const struct xattr *xattr_array,
  58. void *fs_info)
  59. {
  60. const struct xattr *xattr;
  61. char *xattr_name;
  62. int err = 0;
  63. xattr_name = kmalloc(NLS_MAX_CHARSET_SIZE * HFSPLUS_ATTR_MAX_STRLEN + 1,
  64. GFP_KERNEL);
  65. if (!xattr_name)
  66. return -ENOMEM;
  67. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  68. if (!strcmp(xattr->name, ""))
  69. continue;
  70. strcpy(xattr_name, XATTR_SECURITY_PREFIX);
  71. strcpy(xattr_name +
  72. XATTR_SECURITY_PREFIX_LEN, xattr->name);
  73. memset(xattr_name +
  74. XATTR_SECURITY_PREFIX_LEN + strlen(xattr->name), 0, 1);
  75. err = __hfsplus_setxattr(inode, xattr_name,
  76. xattr->value, xattr->value_len, 0);
  77. if (err)
  78. break;
  79. }
  80. kfree(xattr_name);
  81. return err;
  82. }
  83. int hfsplus_init_security(struct inode *inode, struct inode *dir,
  84. const struct qstr *qstr)
  85. {
  86. return security_inode_init_security(inode, dir, qstr,
  87. &hfsplus_initxattrs, NULL);
  88. }
  89. int hfsplus_init_inode_security(struct inode *inode,
  90. struct inode *dir,
  91. const struct qstr *qstr)
  92. {
  93. int err;
  94. err = hfsplus_init_posix_acl(inode, dir);
  95. if (!err)
  96. err = hfsplus_init_security(inode, dir, qstr);
  97. return err;
  98. }
  99. const struct xattr_handler hfsplus_xattr_security_handler = {
  100. .prefix = XATTR_SECURITY_PREFIX,
  101. .list = hfsplus_security_listxattr,
  102. .get = hfsplus_security_getxattr,
  103. .set = hfsplus_security_setxattr,
  104. };