ruleset.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Landlock LSM - Ruleset management
  4. *
  5. * Copyright © 2016-2020 Mickaël Salaün <[email protected]>
  6. * Copyright © 2018-2020 ANSSI
  7. */
  8. #ifndef _SECURITY_LANDLOCK_RULESET_H
  9. #define _SECURITY_LANDLOCK_RULESET_H
  10. #include <linux/bitops.h>
  11. #include <linux/build_bug.h>
  12. #include <linux/mutex.h>
  13. #include <linux/rbtree.h>
  14. #include <linux/refcount.h>
  15. #include <linux/workqueue.h>
  16. #include "limits.h"
  17. #include "object.h"
  18. typedef u16 access_mask_t;
  19. /* Makes sure all filesystem access rights can be stored. */
  20. static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
  21. /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
  22. static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
  23. typedef u16 layer_mask_t;
  24. /* Makes sure all layers can be checked. */
  25. static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
  26. /**
  27. * struct landlock_layer - Access rights for a given layer
  28. */
  29. struct landlock_layer {
  30. /**
  31. * @level: Position of this layer in the layer stack.
  32. */
  33. u16 level;
  34. /**
  35. * @access: Bitfield of allowed actions on the kernel object. They are
  36. * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
  37. */
  38. access_mask_t access;
  39. };
  40. /**
  41. * struct landlock_rule - Access rights tied to an object
  42. */
  43. struct landlock_rule {
  44. /**
  45. * @node: Node in the ruleset's red-black tree.
  46. */
  47. struct rb_node node;
  48. /**
  49. * @object: Pointer to identify a kernel object (e.g. an inode). This
  50. * is used as a key for this ruleset element. This pointer is set once
  51. * and never modified. It always points to an allocated object because
  52. * each rule increments the refcount of its object.
  53. */
  54. struct landlock_object *object;
  55. /**
  56. * @num_layers: Number of entries in @layers.
  57. */
  58. u32 num_layers;
  59. /**
  60. * @layers: Stack of layers, from the latest to the newest, implemented
  61. * as a flexible array member (FAM).
  62. */
  63. struct landlock_layer layers[];
  64. };
  65. /**
  66. * struct landlock_hierarchy - Node in a ruleset hierarchy
  67. */
  68. struct landlock_hierarchy {
  69. /**
  70. * @parent: Pointer to the parent node, or NULL if it is a root
  71. * Landlock domain.
  72. */
  73. struct landlock_hierarchy *parent;
  74. /**
  75. * @usage: Number of potential children domains plus their parent
  76. * domain.
  77. */
  78. refcount_t usage;
  79. };
  80. /**
  81. * struct landlock_ruleset - Landlock ruleset
  82. *
  83. * This data structure must contain unique entries, be updatable, and quick to
  84. * match an object.
  85. */
  86. struct landlock_ruleset {
  87. /**
  88. * @root: Root of a red-black tree containing &struct landlock_rule
  89. * nodes. Once a ruleset is tied to a process (i.e. as a domain), this
  90. * tree is immutable until @usage reaches zero.
  91. */
  92. struct rb_root root;
  93. /**
  94. * @hierarchy: Enables hierarchy identification even when a parent
  95. * domain vanishes. This is needed for the ptrace protection.
  96. */
  97. struct landlock_hierarchy *hierarchy;
  98. union {
  99. /**
  100. * @work_free: Enables to free a ruleset within a lockless
  101. * section. This is only used by
  102. * landlock_put_ruleset_deferred() when @usage reaches zero.
  103. * The fields @lock, @usage, @num_rules, @num_layers and
  104. * @fs_access_masks are then unused.
  105. */
  106. struct work_struct work_free;
  107. struct {
  108. /**
  109. * @lock: Protects against concurrent modifications of
  110. * @root, if @usage is greater than zero.
  111. */
  112. struct mutex lock;
  113. /**
  114. * @usage: Number of processes (i.e. domains) or file
  115. * descriptors referencing this ruleset.
  116. */
  117. refcount_t usage;
  118. /**
  119. * @num_rules: Number of non-overlapping (i.e. not for
  120. * the same object) rules in this ruleset.
  121. */
  122. u32 num_rules;
  123. /**
  124. * @num_layers: Number of layers that are used in this
  125. * ruleset. This enables to check that all the layers
  126. * allow an access request. A value of 0 identifies a
  127. * non-merged ruleset (i.e. not a domain).
  128. */
  129. u32 num_layers;
  130. /**
  131. * @fs_access_masks: Contains the subset of filesystem
  132. * actions that are restricted by a ruleset. A domain
  133. * saves all layers of merged rulesets in a stack
  134. * (FAM), starting from the first layer to the last
  135. * one. These layers are used when merging rulesets,
  136. * for user space backward compatibility (i.e.
  137. * future-proof), and to properly handle merged
  138. * rulesets without overlapping access rights. These
  139. * layers are set once and never changed for the
  140. * lifetime of the ruleset.
  141. */
  142. access_mask_t fs_access_masks[];
  143. };
  144. };
  145. };
  146. struct landlock_ruleset *
  147. landlock_create_ruleset(const access_mask_t fs_access_mask);
  148. void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
  149. void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
  150. int landlock_insert_rule(struct landlock_ruleset *const ruleset,
  151. struct landlock_object *const object,
  152. const access_mask_t access);
  153. struct landlock_ruleset *
  154. landlock_merge_ruleset(struct landlock_ruleset *const parent,
  155. struct landlock_ruleset *const ruleset);
  156. const struct landlock_rule *
  157. landlock_find_rule(const struct landlock_ruleset *const ruleset,
  158. const struct landlock_object *const object);
  159. static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
  160. {
  161. if (ruleset)
  162. refcount_inc(&ruleset->usage);
  163. }
  164. #endif /* _SECURITY_LANDLOCK_RULESET_H */