xattr_security.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * linux/fs/ext3/xattr_security.c
  3. * Handler for storing security labels as extended attributes.
  4. */
  5. #include <linux/security.h>
  6. #include "ext3.h"
  7. #include "xattr.h"
  8. static size_t
  9. ext3_xattr_security_list(struct dentry *dentry, char *list, size_t list_size,
  10. const char *name, size_t name_len, int type)
  11. {
  12. const size_t prefix_len = XATTR_SECURITY_PREFIX_LEN;
  13. const size_t total_len = prefix_len + name_len + 1;
  14. if (list && total_len <= list_size) {
  15. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  16. memcpy(list+prefix_len, name, name_len);
  17. list[prefix_len + name_len] = '\0';
  18. }
  19. return total_len;
  20. }
  21. static int
  22. ext3_xattr_security_get(struct dentry *dentry, const char *name,
  23. void *buffer, size_t size, int type)
  24. {
  25. if (strcmp(name, "") == 0)
  26. return -EINVAL;
  27. return ext3_xattr_get(dentry->d_inode, EXT3_XATTR_INDEX_SECURITY,
  28. name, buffer, size);
  29. }
  30. static int
  31. ext3_xattr_security_set(struct dentry *dentry, const char *name,
  32. const void *value, size_t size, int flags, int type)
  33. {
  34. if (strcmp(name, "") == 0)
  35. return -EINVAL;
  36. return ext3_xattr_set(dentry->d_inode, EXT3_XATTR_INDEX_SECURITY,
  37. name, value, size, flags);
  38. }
  39. int ext3_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  40. void *fs_info)
  41. {
  42. const struct xattr *xattr;
  43. handle_t *handle = fs_info;
  44. int err = 0;
  45. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  46. err = ext3_xattr_set_handle(handle, inode,
  47. EXT3_XATTR_INDEX_SECURITY,
  48. xattr->name, xattr->value,
  49. xattr->value_len, 0);
  50. if (err < 0)
  51. break;
  52. }
  53. return err;
  54. }
  55. int
  56. ext3_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
  57. const struct qstr *qstr)
  58. {
  59. return security_inode_init_security(inode, dir, qstr,
  60. &ext3_initxattrs, handle);
  61. }
  62. const struct xattr_handler ext3_xattr_security_handler = {
  63. .prefix = XATTR_SECURITY_PREFIX,
  64. .list = ext3_xattr_security_list,
  65. .get = ext3_xattr_security_get,
  66. .set = ext3_xattr_security_set,
  67. };