xattr_security.c 2.7 KB

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