xattr_security.c 2.9 KB

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