xattr_security.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 mnt_idmap *idmap, 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. static int
  37. reiserfs_initxattrs(struct inode *inode, const struct xattr *xattr_array,
  38. void *fs_info)
  39. {
  40. struct reiserfs_security_handle *sec = fs_info;
  41. sec->value = kmemdup(xattr_array->value, xattr_array->value_len,
  42. GFP_KERNEL);
  43. if (!sec->value)
  44. return -ENOMEM;
  45. sec->name = xattr_array->name;
  46. sec->length = xattr_array->value_len;
  47. return 0;
  48. }
  49. /* Initializes the security context for a new inode and returns the number
  50. * of blocks needed for the transaction. If successful, reiserfs_security
  51. * must be released using reiserfs_security_free when the caller is done. */
  52. int reiserfs_security_init(struct inode *dir, struct inode *inode,
  53. const struct qstr *qstr,
  54. struct reiserfs_security_handle *sec)
  55. {
  56. int blocks = 0;
  57. int error;
  58. sec->name = NULL;
  59. sec->value = NULL;
  60. sec->length = 0;
  61. /* Don't add selinux attributes on xattrs - they'll never get used */
  62. if (IS_PRIVATE(dir))
  63. return 0;
  64. error = security_inode_init_security(inode, dir, qstr,
  65. &reiserfs_initxattrs, sec);
  66. if (error) {
  67. sec->name = NULL;
  68. sec->value = NULL;
  69. sec->length = 0;
  70. return error;
  71. }
  72. if (sec->length && reiserfs_xattrs_initialized(inode->i_sb)) {
  73. blocks = reiserfs_xattr_jcreate_nblocks(inode) +
  74. reiserfs_xattr_nblocks(inode, sec->length);
  75. /* We don't want to count the directories twice if we have
  76. * a default ACL. */
  77. REISERFS_I(inode)->i_flags |= i_has_xattr_dir;
  78. }
  79. return blocks;
  80. }
  81. int reiserfs_security_write(struct reiserfs_transaction_handle *th,
  82. struct inode *inode,
  83. struct reiserfs_security_handle *sec)
  84. {
  85. char xattr_name[XATTR_NAME_MAX + 1] = XATTR_SECURITY_PREFIX;
  86. int error;
  87. if (XATTR_SECURITY_PREFIX_LEN + strlen(sec->name) > XATTR_NAME_MAX)
  88. return -EINVAL;
  89. strlcat(xattr_name, sec->name, sizeof(xattr_name));
  90. error = reiserfs_xattr_set_handle(th, inode, xattr_name, sec->value,
  91. sec->length, XATTR_CREATE);
  92. if (error == -ENODATA || error == -EOPNOTSUPP)
  93. error = 0;
  94. return error;
  95. }
  96. void reiserfs_security_free(struct reiserfs_security_handle *sec)
  97. {
  98. kfree(sec->value);
  99. sec->name = NULL;
  100. sec->value = NULL;
  101. }
  102. const struct xattr_handler reiserfs_xattr_security_handler = {
  103. .prefix = XATTR_SECURITY_PREFIX,
  104. .get = security_get,
  105. .set = security_set,
  106. .list = security_list,
  107. };