xattr_security.c 2.7 KB

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