xattr_security.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/xattr_security.c
  4. * Handler for storing security labels as extended attributes.
  5. */
  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 int
  14. ext4_xattr_security_get(const struct xattr_handler *handler,
  15. struct dentry *unused, struct inode *inode,
  16. const char *name, void *buffer, size_t size)
  17. {
  18. return ext4_xattr_get(inode, EXT4_XATTR_INDEX_SECURITY,
  19. name, buffer, size);
  20. }
  21. static int
  22. ext4_xattr_security_set(const struct xattr_handler *handler,
  23. struct dentry *unused, struct inode *inode,
  24. const char *name, const void *value,
  25. size_t size, int flags)
  26. {
  27. return ext4_xattr_set(inode, EXT4_XATTR_INDEX_SECURITY,
  28. name, value, size, flags);
  29. }
  30. static int
  31. ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  32. void *fs_info)
  33. {
  34. const struct xattr *xattr;
  35. handle_t *handle = fs_info;
  36. int err = 0;
  37. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  38. err = ext4_xattr_set_handle(handle, inode,
  39. EXT4_XATTR_INDEX_SECURITY,
  40. xattr->name, xattr->value,
  41. xattr->value_len, XATTR_CREATE);
  42. if (err < 0)
  43. break;
  44. }
  45. return err;
  46. }
  47. int
  48. ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
  49. const struct qstr *qstr)
  50. {
  51. return security_inode_init_security(inode, dir, qstr,
  52. &ext4_initxattrs, handle);
  53. }
  54. const struct xattr_handler ext4_xattr_security_handler = {
  55. .prefix = XATTR_SECURITY_PREFIX,
  56. .get = ext4_xattr_security_get,
  57. .set = ext4_xattr_security_set,
  58. };