xattr_security.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(struct dentry *dentry, char *list, size_t list_size,
  14. const char *name, size_t name_len, int type)
  15. {
  16. const size_t prefix_len = sizeof(XATTR_SECURITY_PREFIX)-1;
  17. const size_t total_len = prefix_len + name_len + 1;
  18. if (list && total_len <= list_size) {
  19. memcpy(list, XATTR_SECURITY_PREFIX, prefix_len);
  20. memcpy(list+prefix_len, name, name_len);
  21. list[prefix_len + name_len] = '\0';
  22. }
  23. return total_len;
  24. }
  25. static int
  26. ext4_xattr_security_get(struct dentry *dentry, const char *name,
  27. void *buffer, size_t size, int type)
  28. {
  29. if (strcmp(name, "") == 0)
  30. return -EINVAL;
  31. if (strcmp(name, "sehash") == 0 && test_opt(dentry->d_sb, NO_SEHASH_XATTR))
  32. return 0;
  33. return ext4_xattr_get(dentry->d_inode, EXT4_XATTR_INDEX_SECURITY,
  34. name, buffer, size);
  35. }
  36. static int
  37. ext4_xattr_security_set(struct dentry *dentry, const char *name,
  38. const void *value, size_t size, int flags, int type)
  39. {
  40. if (strcmp(name, "") == 0)
  41. return -EINVAL;
  42. if (strcmp(name, "sehash") == 0 && test_opt(dentry->d_sb, NO_SEHASH_XATTR))
  43. return 0;
  44. return ext4_xattr_set(dentry->d_inode, EXT4_XATTR_INDEX_SECURITY,
  45. name, value, size, flags);
  46. }
  47. static int
  48. ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  49. void *fs_info)
  50. {
  51. const struct xattr *xattr;
  52. handle_t *handle = fs_info;
  53. int err = 0;
  54. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  55. err = ext4_xattr_set_handle(handle, inode,
  56. EXT4_XATTR_INDEX_SECURITY,
  57. xattr->name, xattr->value,
  58. xattr->value_len, 0);
  59. if (err < 0)
  60. break;
  61. }
  62. return err;
  63. }
  64. int
  65. ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
  66. const struct qstr *qstr)
  67. {
  68. return security_inode_init_security(inode, dir, qstr,
  69. &ext4_initxattrs, handle);
  70. }
  71. const struct xattr_handler ext4_xattr_security_handler = {
  72. .prefix = XATTR_SECURITY_PREFIX,
  73. .list = ext4_xattr_security_list,
  74. .get = ext4_xattr_security_get,
  75. .set = ext4_xattr_security_set,
  76. };