xattr_security.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * linux/fs/ext4/xattr_security.c
  3. * Handler for storing security labels as extended attributes.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/string.h>
  7. #include <linux/fs.h>
  8. #include <linux/security.h>
  9. #include <linux/slab.h>
  10. #include "ext4_jbd2.h"
  11. #include "ext4.h"
  12. #include "xattr.h"
  13. static size_t
  14. ext4_xattr_security_list(struct dentry *dentry, char *list, size_t list_size,
  15. const char *name, size_t name_len, int type)
  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(struct dentry *dentry, const char *name,
  28. void *buffer, size_t size, int type)
  29. {
  30. if (strcmp(name, "") == 0)
  31. return -EINVAL;
  32. return ext4_xattr_get(dentry->d_inode, EXT4_XATTR_INDEX_SECURITY,
  33. name, buffer, size);
  34. }
  35. static int
  36. ext4_xattr_security_set(struct dentry *dentry, const char *name,
  37. const void *value, size_t size, int flags, int type)
  38. {
  39. if (strcmp(name, "") == 0)
  40. return -EINVAL;
  41. return ext4_xattr_set(dentry->d_inode, EXT4_XATTR_INDEX_SECURITY,
  42. name, value, size, flags);
  43. }
  44. int
  45. ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
  46. const struct qstr *qstr)
  47. {
  48. int err;
  49. size_t len;
  50. void *value;
  51. char *name;
  52. err = security_inode_init_security(inode, dir, qstr, &name, &value, &len);
  53. if (err) {
  54. if (err == -EOPNOTSUPP)
  55. return 0;
  56. return err;
  57. }
  58. err = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_SECURITY,
  59. name, value, len, 0);
  60. kfree(name);
  61. kfree(value);
  62. return err;
  63. }
  64. const struct xattr_handler ext4_xattr_security_handler = {
  65. .prefix = XATTR_SECURITY_PREFIX,
  66. .list = ext4_xattr_security_list,
  67. .get = ext4_xattr_security_get,
  68. .set = ext4_xattr_security_set,
  69. };