xattr_security.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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
  44. ext2_init_security(struct inode *inode, struct inode *dir,
  45. const struct qstr *qstr)
  46. {
  47. int err;
  48. size_t len;
  49. void *value;
  50. char *name;
  51. err = security_inode_init_security(inode, dir, qstr, &name, &value, &len);
  52. if (err) {
  53. if (err == -EOPNOTSUPP)
  54. return 0;
  55. return err;
  56. }
  57. err = ext2_xattr_set(inode, EXT2_XATTR_INDEX_SECURITY,
  58. name, value, len, 0);
  59. kfree(name);
  60. kfree(value);
  61. return err;
  62. }
  63. const struct xattr_handler ext2_xattr_security_handler = {
  64. .prefix = XATTR_SECURITY_PREFIX,
  65. .list = ext2_xattr_security_list,
  66. .get = ext2_xattr_security_get,
  67. .set = ext2_xattr_security_set,
  68. };