xattr_security.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * linux/fs/ext4/xattr_security.c
  3. * Handler for storing security labels as extended attributes.
  4. */
  5. #include <linux/string.h>
  6. #include <linux/fs.h>
  7. #include <linux/security.h>
  8. #include <linux/slab.h>
  9. #include "ext4_jbd2.h"
  10. #include "ext4.h"
  11. #include "xattr.h"
  12. static size_t
  13. ext4_xattr_security_list(const struct xattr_handler *handler,
  14. struct dentry *dentry, char *list, size_t list_size,
  15. const char *name, size_t name_len)
  16. {
  17. const size_t prefix_len = sizeof(XATTR_SECURITY_PREFIX)-1;
  18. const size_t total_len = prefix_len + name_len + 1;
  19. if (list && total_len <= list_size) {
  20. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  21. memcpy(list+prefix_len, name, name_len);
  22. list[prefix_len + name_len] = '\0';
  23. }
  24. return total_len;
  25. }
  26. static int
  27. ext4_xattr_security_get(const struct xattr_handler *handler,
  28. struct dentry *dentry, const char *name,
  29. void *buffer, size_t size)
  30. {
  31. if (strcmp(name, "") == 0)
  32. return -EINVAL;
  33. return ext4_xattr_get(d_inode(dentry), EXT4_XATTR_INDEX_SECURITY,
  34. name, buffer, size);
  35. }
  36. static int
  37. ext4_xattr_security_set(const struct xattr_handler *handler,
  38. struct dentry *dentry, const char *name,
  39. const void *value, size_t size, int flags)
  40. {
  41. if (strcmp(name, "") == 0)
  42. return -EINVAL;
  43. return ext4_xattr_set(d_inode(dentry), EXT4_XATTR_INDEX_SECURITY,
  44. name, value, size, flags);
  45. }
  46. static int
  47. ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  48. void *fs_info)
  49. {
  50. const struct xattr *xattr;
  51. handle_t *handle = fs_info;
  52. int err = 0;
  53. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  54. err = ext4_xattr_set_handle(handle, inode,
  55. EXT4_XATTR_INDEX_SECURITY,
  56. xattr->name, xattr->value,
  57. xattr->value_len, 0);
  58. if (err < 0)
  59. break;
  60. }
  61. return err;
  62. }
  63. int
  64. ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
  65. const struct qstr *qstr)
  66. {
  67. return security_inode_init_security(inode, dir, qstr,
  68. &ext4_initxattrs, handle);
  69. }
  70. const struct xattr_handler ext4_xattr_security_handler = {
  71. .prefix = XATTR_SECURITY_PREFIX,
  72. .list = ext4_xattr_security_list,
  73. .get = ext4_xattr_security_get,
  74. .set = ext4_xattr_security_set,
  75. };