xattr_security.c 2.8 KB

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