xattr_security.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * linux/fs/ext4/xattr_security.c
  3. * Handler for storing security labels as extended attributes.
  4. */
  5. #include <linux/string.h>
  6. #include <linux/fs.h>
  7. #include <linux/security.h>
  8. #include <linux/slab.h>
  9. #include "ext4_jbd2.h"
  10. #include "ext4.h"
  11. #include "xattr.h"
  12. static size_t
  13. ext4_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 size_t 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. ext4_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 ext4_xattr_get(d_inode(dentry), EXT4_XATTR_INDEX_SECURITY,
  32. name, buffer, size);
  33. }
  34. static int
  35. ext4_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 ext4_xattr_set(d_inode(dentry), EXT4_XATTR_INDEX_SECURITY,
  41. name, value, size, flags);
  42. }
  43. static int
  44. ext4_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  45. void *fs_info)
  46. {
  47. const struct xattr *xattr;
  48. handle_t *handle = fs_info;
  49. int err = 0;
  50. for (xattr = xattr_array; xattr->name != NULL; xattr++) {
  51. err = ext4_xattr_set_handle(handle, inode,
  52. EXT4_XATTR_INDEX_SECURITY,
  53. xattr->name, xattr->value,
  54. xattr->value_len, 0);
  55. if (err < 0)
  56. break;
  57. }
  58. return err;
  59. }
  60. int
  61. ext4_init_security(handle_t *handle, struct inode *inode, struct inode *dir,
  62. const struct qstr *qstr)
  63. {
  64. return security_inode_init_security(inode, dir, qstr,
  65. &ext4_initxattrs, handle);
  66. }
  67. const struct xattr_handler ext4_xattr_security_handler = {
  68. .prefix = XATTR_SECURITY_PREFIX,
  69. .list = ext4_xattr_security_list,
  70. .get = ext4_xattr_security_get,
  71. .set = ext4_xattr_security_set,
  72. };