xattr_security.c 2.4 KB

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