xattr_security.c 3.0 KB

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