xattr_security.c 2.7 KB

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