xattr_security.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/slab.h>
  7. #include <linux/string.h>
  8. #include <linux/fs.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 dentry *dentry, char *list, size_t list_size,
  14. const char *name, size_t name_len, int type)
  15. {
  16. const int prefix_len = XATTR_SECURITY_PREFIX_LEN;
  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 dentry *dentry, const char *name,
  27. void *buffer, size_t size, int type)
  28. {
  29. if (strcmp(name, "") == 0)
  30. return -EINVAL;
  31. return ext2_xattr_get(dentry->d_inode, EXT2_XATTR_INDEX_SECURITY, name,
  32. buffer, size);
  33. }
  34. static int
  35. ext2_xattr_security_set(struct dentry *dentry, const char *name,
  36. const void *value, size_t size, int flags, int type)
  37. {
  38. if (strcmp(name, "") == 0)
  39. return -EINVAL;
  40. return ext2_xattr_set(dentry->d_inode, EXT2_XATTR_INDEX_SECURITY, name,
  41. value, size, flags);
  42. }
  43. int ext2_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  44. void *fs_info)
  45. {
  46. const struct xattr *xattr;
  47. int err = 0;
  48. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  49. err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
  50. xattr->name, xattr->value,
  51. xattr->value_len, 0);
  52. if (err < 0)
  53. break;
  54. }
  55. return err;
  56. }
  57. int
  58. ext2_init_security(struct inode *inode, struct inode *dir,
  59. const struct qstr *qstr)
  60. {
  61. return security_inode_init_security(inode, dir, qstr,
  62. &ext2_initxattrs, NULL);
  63. }
  64. const struct xattr_handler ext2_xattr_security_handler = {
  65. .prefix = XATTR_SECURITY_PREFIX,
  66. .list = ext2_xattr_security_list,
  67. .get = ext2_xattr_security_get,
  68. .set = ext2_xattr_security_set,
  69. };