xattr_security.c 2.9 KB

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