xattr_security.c 2.9 KB

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