xattr_security.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <linux/reiserfs_fs.h>
  2. #include <linux/errno.h>
  3. #include <linux/fs.h>
  4. #include <linux/pagemap.h>
  5. #include <linux/xattr.h>
  6. #include <linux/reiserfs_xattr.h>
  7. #include <linux/security.h>
  8. #include <asm/uaccess.h>
  9. static int
  10. security_get(struct inode *inode, const char *name, void *buffer, size_t size)
  11. {
  12. if (strlen(name) < sizeof(XATTR_SECURITY_PREFIX))
  13. return -EINVAL;
  14. if (IS_PRIVATE(inode))
  15. return -EPERM;
  16. return reiserfs_xattr_get(inode, name, buffer, size);
  17. }
  18. static int
  19. security_set(struct inode *inode, const char *name, const void *buffer,
  20. size_t size, int flags)
  21. {
  22. if (strlen(name) < sizeof(XATTR_SECURITY_PREFIX))
  23. return -EINVAL;
  24. if (IS_PRIVATE(inode))
  25. return -EPERM;
  26. return reiserfs_xattr_set(inode, name, buffer, size, flags);
  27. }
  28. static size_t security_list(struct inode *inode, char *list, size_t list_len,
  29. const char *name, size_t namelen)
  30. {
  31. const size_t len = namelen + 1;
  32. if (IS_PRIVATE(inode))
  33. return 0;
  34. if (list && len <= list_len) {
  35. memcpy(list, name, namelen);
  36. list[namelen] = '\0';
  37. }
  38. return len;
  39. }
  40. /* Initializes the security context for a new inode and returns the number
  41. * of blocks needed for the transaction. If successful, reiserfs_security
  42. * must be released using reiserfs_security_free when the caller is done. */
  43. int reiserfs_security_init(struct inode *dir, struct inode *inode,
  44. struct reiserfs_security_handle *sec)
  45. {
  46. int blocks = 0;
  47. int error;
  48. sec->name = NULL;
  49. /* Don't add selinux attributes on xattrs - they'll never get used */
  50. if (IS_PRIVATE(dir))
  51. return 0;
  52. error = security_inode_init_security(inode, dir, &sec->name,
  53. &sec->value, &sec->length);
  54. if (error) {
  55. if (error == -EOPNOTSUPP)
  56. error = 0;
  57. sec->name = NULL;
  58. sec->value = NULL;
  59. sec->length = 0;
  60. return error;
  61. }
  62. if (sec->length) {
  63. blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  64. reiserfs_xattr_nblocks(inode, sec->length);
  65. /* We don't want to count the directories twice if we have
  66. * a default ACL. */
  67. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  68. }
  69. return blocks;
  70. }
  71. int reiserfs_security_write(struct reiserfs_transaction_handle *th,
  72. struct inode *inode,
  73. struct reiserfs_security_handle *sec)
  74. {
  75. int error;
  76. if (strlen(sec->name) < sizeof(XATTR_SECURITY_PREFIX))
  77. return -EINVAL;
  78. error = reiserfs_xattr_set_handle(th, inode, sec->name, sec->value,
  79. sec->length, XATTR_CREATE);
  80. if (error == -ENODATA || error == -EOPNOTSUPP)
  81. error = 0;
  82. return error;
  83. }
  84. void reiserfs_security_free(struct reiserfs_security_handle *sec)
  85. {
  86. kfree(sec->name);
  87. kfree(sec->value);
  88. sec->name = NULL;
  89. sec->value = NULL;
  90. }
  91. struct xattr_handler reiserfs_xattr_security_handler = {
  92. .prefix = XATTR_SECURITY_PREFIX,
  93. .get = security_get,
  94. .set = security_set,
  95. .list = security_list,
  96. };