xattr_security.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * linux/fs/ext2/xattr_security.c
  3. * Handler for storing security labels as extended attributes.
  4. */
  5. #include "ext2.h"
  6. #include <linux/security.h>
  7. #include "xattr.h"
  8. static size_t
  9. ext2_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 int 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. ext2_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 ext2_xattr_get(dentry->d_inode, EXT2_XATTR_INDEX_SECURITY, name,
  28. buffer, size);
  29. }
  30. static int
  31. ext2_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 ext2_xattr_set(dentry->d_inode, EXT2_XATTR_INDEX_SECURITY, name,
  37. value, size, flags);
  38. }
  39. int ext2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  40. void *fs_info)
  41. {
  42. const struct xattr *xattr;
  43. int err = 0;
  44. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  45. err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
  46. xattr->name, xattr->value,
  47. xattr->value_len, 0);
  48. if (err < 0)
  49. break;
  50. }
  51. return err;
  52. }
  53. int
  54. ext2_init_security(struct inode *inode, struct inode *dir,
  55. const struct qstr *qstr)
  56. {
  57. return security_inode_init_security(inode, dir, qstr,
  58. &ext2_initxattrs, NULL);
  59. }
  60. const struct xattr_handler ext2_xattr_security_handler = {
  61. .prefix = XATTR_SECURITY_PREFIX,
  62. .list = ext2_xattr_security_list,
  63. .get = ext2_xattr_security_get,
  64. .set = ext2_xattr_security_set,
  65. };