xattr_security.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * linux/fs/ext2/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/smp_lock.h>
  9. #include <linux/ext2_fs.h>
  10. #include <linux/security.h>
  11. #include "xattr.h"
  12. static size_t
  13. ext2_xattr_security_list(struct inode *inode, char *list, size_t list_size,
  14. const char *name, size_t name_len)
  15. {
  16. const int 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. ext2_xattr_security_get(struct inode *inode, const char *name,
  27. void *buffer, size_t size)
  28. {
  29. if (strcmp(name, "") == 0)
  30. return -EINVAL;
  31. return ext2_xattr_get(inode, EXT2_XATTR_INDEX_SECURITY, name,
  32. buffer, size);
  33. }
  34. static int
  35. ext2_xattr_security_set(struct inode *inode, const char *name,
  36. const void *value, size_t size, int flags)
  37. {
  38. if (strcmp(name, "") == 0)
  39. return -EINVAL;
  40. return ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY, name,
  41. value, size, flags);
  42. }
  43. int
  44. ext2_init_security(struct inode *inode, struct inode *dir)
  45. {
  46. int err;
  47. size_t len;
  48. void *value;
  49. char *name;
  50. err = security_inode_init_security(inode, dir, &name, &value, &len);
  51. if (err) {
  52. if (err == -EOPNOTSUPP)
  53. return 0;
  54. return err;
  55. }
  56. err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
  57. name, value, len, 0);
  58. kfree(name);
  59. kfree(value);
  60. return err;
  61. }
  62. struct xattr_handler ext2_xattr_security_handler = {
  63. .prefix = XATTR_SECURITY_PREFIX,
  64. .list = ext2_xattr_security_list,
  65. .get = ext2_xattr_security_get,
  66. .set = ext2_xattr_security_set,
  67. };