fs.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Landlock LSM - Filesystem management and hooks
  4. *
  5. * Copyright © 2017-2020 Mickaël Salaün <mic@digikod.net>
  6. * Copyright © 2018-2020 ANSSI
  7. */
  8. #ifndef _SECURITY_LANDLOCK_FS_H
  9. #define _SECURITY_LANDLOCK_FS_H
  10. #include <linux/fs.h>
  11. #include <linux/init.h>
  12. #include <linux/rcupdate.h>
  13. #include "ruleset.h"
  14. #include "setup.h"
  15. /**
  16. * struct landlock_inode_security - Inode security blob
  17. *
  18. * Enable to reference a &struct landlock_object tied to an inode (i.e.
  19. * underlying object).
  20. */
  21. struct landlock_inode_security {
  22. /**
  23. * @object: Weak pointer to an allocated object. All assignments of a
  24. * new object are protected by the underlying inode->i_lock. However,
  25. * atomically disassociating @object from the inode is only protected
  26. * by @object->lock, from the time @object's usage refcount drops to
  27. * zero to the time this pointer is nulled out (cf. release_inode() and
  28. * hook_sb_delete()). Indeed, such disassociation doesn't require
  29. * inode->i_lock thanks to the careful rcu_access_pointer() check
  30. * performed by get_inode_object().
  31. */
  32. struct landlock_object __rcu *object;
  33. };
  34. /**
  35. * struct landlock_file_security - File security blob
  36. *
  37. * This information is populated when opening a file in hook_file_open, and
  38. * tracks the relevant Landlock access rights that were available at the time
  39. * of opening the file. Other LSM hooks use these rights in order to authorize
  40. * operations on already opened files.
  41. */
  42. struct landlock_file_security {
  43. /**
  44. * @allowed_access: Access rights that were available at the time of
  45. * opening the file. This is not necessarily the full set of access
  46. * rights available at that time, but it's the necessary subset as
  47. * needed to authorize later operations on the open file.
  48. */
  49. access_mask_t allowed_access;
  50. /**
  51. * @fown_domain: Domain of the task that set the PID that may receive a
  52. * signal e.g., SIGURG when writing MSG_OOB to the related socket.
  53. * This pointer is protected by the related file->f_owner->lock, as for
  54. * fown_struct's members: pid, uid, and euid.
  55. */
  56. struct landlock_ruleset *fown_domain;
  57. };
  58. /**
  59. * struct landlock_superblock_security - Superblock security blob
  60. *
  61. * Enable hook_sb_delete() to wait for concurrent calls to release_inode().
  62. */
  63. struct landlock_superblock_security {
  64. /**
  65. * @inode_refs: Number of pending inodes (from this superblock) that
  66. * are being released by release_inode().
  67. * Cf. struct super_block->s_fsnotify_inode_refs .
  68. */
  69. atomic_long_t inode_refs;
  70. };
  71. static inline struct landlock_file_security *
  72. landlock_file(const struct file *const file)
  73. {
  74. return file->f_security + landlock_blob_sizes.lbs_file;
  75. }
  76. static inline struct landlock_inode_security *
  77. landlock_inode(const struct inode *const inode)
  78. {
  79. return inode->i_security + landlock_blob_sizes.lbs_inode;
  80. }
  81. static inline struct landlock_superblock_security *
  82. landlock_superblock(const struct super_block *const superblock)
  83. {
  84. return superblock->s_security + landlock_blob_sizes.lbs_superblock;
  85. }
  86. __init void landlock_add_fs_hooks(void);
  87. int landlock_append_fs_rule(struct landlock_ruleset *const ruleset,
  88. const struct path *const path,
  89. access_mask_t access_hierarchy);
  90. #endif /* _SECURITY_LANDLOCK_FS_H */