xattr_security.c 2.1 KB

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