ruleset.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Landlock LSM - Ruleset management
  4. *
  5. * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
  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/kernel.h>
  13. #include <linux/mutex.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/refcount.h>
  16. #include <linux/workqueue.h>
  17. #include <uapi/linux/landlock.h>
  18. #include "limits.h"
  19. #include "object.h"
  20. /*
  21. * All access rights that are denied by default whether they are handled or not
  22. * by a ruleset/layer. This must be ORed with all ruleset->access_masks[]
  23. * entries when we need to get the absolute handled access masks.
  24. */
  25. /* clang-format off */
  26. #define LANDLOCK_ACCESS_FS_INITIALLY_DENIED ( \
  27. LANDLOCK_ACCESS_FS_REFER)
  28. /* clang-format on */
  29. typedef u16 access_mask_t;
  30. /* Makes sure all filesystem access rights can be stored. */
  31. static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
  32. /* Makes sure all network access rights can be stored. */
  33. static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_NET);
  34. /* Makes sure all scoped rights can be stored. */
  35. static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_SCOPE);
  36. /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
  37. static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
  38. /* Ruleset access masks. */
  39. struct access_masks {
  40. access_mask_t fs : LANDLOCK_NUM_ACCESS_FS;
  41. access_mask_t net : LANDLOCK_NUM_ACCESS_NET;
  42. access_mask_t scope : LANDLOCK_NUM_SCOPE;
  43. };
  44. union access_masks_all {
  45. struct access_masks masks;
  46. u32 all;
  47. };
  48. /* Makes sure all fields are covered. */
  49. static_assert(sizeof(typeof_member(union access_masks_all, masks)) ==
  50. sizeof(typeof_member(union access_masks_all, all)));
  51. typedef u16 layer_mask_t;
  52. /* Makes sure all layers can be checked. */
  53. static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
  54. /**
  55. * struct landlock_layer - Access rights for a given layer
  56. */
  57. struct landlock_layer {
  58. /**
  59. * @level: Position of this layer in the layer stack.
  60. */
  61. u16 level;
  62. /**
  63. * @access: Bitfield of allowed actions on the kernel object. They are
  64. * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
  65. */
  66. access_mask_t access;
  67. };
  68. /**
  69. * union landlock_key - Key of a ruleset's red-black tree
  70. */
  71. union landlock_key {
  72. /**
  73. * @object: Pointer to identify a kernel object (e.g. an inode).
  74. */
  75. struct landlock_object *object;
  76. /**
  77. * @data: Raw data to identify an arbitrary 32-bit value
  78. * (e.g. a TCP port).
  79. */
  80. uintptr_t data;
  81. };
  82. /**
  83. * enum landlock_key_type - Type of &union landlock_key
  84. */
  85. enum landlock_key_type {
  86. /**
  87. * @LANDLOCK_KEY_INODE: Type of &landlock_ruleset.root_inode's node
  88. * keys.
  89. */
  90. LANDLOCK_KEY_INODE = 1,
  91. /**
  92. * @LANDLOCK_KEY_NET_PORT: Type of &landlock_ruleset.root_net_port's
  93. * node keys.
  94. */
  95. LANDLOCK_KEY_NET_PORT,
  96. };
  97. /**
  98. * struct landlock_id - Unique rule identifier for a ruleset
  99. */
  100. struct landlock_id {
  101. /**
  102. * @key: Identifies either a kernel object (e.g. an inode) or
  103. * a raw value (e.g. a TCP port).
  104. */
  105. union landlock_key key;
  106. /**
  107. * @type: Type of a landlock_ruleset's root tree.
  108. */
  109. const enum landlock_key_type type;
  110. };
  111. /**
  112. * struct landlock_rule - Access rights tied to an object
  113. */
  114. struct landlock_rule {
  115. /**
  116. * @node: Node in the ruleset's red-black tree.
  117. */
  118. struct rb_node node;
  119. /**
  120. * @key: A union to identify either a kernel object (e.g. an inode) or
  121. * a raw data value (e.g. a network socket port). This is used as a key
  122. * for this ruleset element. The pointer is set once and never
  123. * modified. It always points to an allocated object because each rule
  124. * increments the refcount of its object.
  125. */
  126. union landlock_key key;
  127. /**
  128. * @num_layers: Number of entries in @layers.
  129. */
  130. u32 num_layers;
  131. /**
  132. * @layers: Stack of layers, from the latest to the newest, implemented
  133. * as a flexible array member (FAM).
  134. */
  135. struct landlock_layer layers[] __counted_by(num_layers);
  136. };
  137. /**
  138. * struct landlock_hierarchy - Node in a ruleset hierarchy
  139. */
  140. struct landlock_hierarchy {
  141. /**
  142. * @parent: Pointer to the parent node, or NULL if it is a root
  143. * Landlock domain.
  144. */
  145. struct landlock_hierarchy *parent;
  146. /**
  147. * @usage: Number of potential children domains plus their parent
  148. * domain.
  149. */
  150. refcount_t usage;
  151. };
  152. /**
  153. * struct landlock_ruleset - Landlock ruleset
  154. *
  155. * This data structure must contain unique entries, be updatable, and quick to
  156. * match an object.
  157. */
  158. struct landlock_ruleset {
  159. /**
  160. * @root_inode: Root of a red-black tree containing &struct
  161. * landlock_rule nodes with inode object. Once a ruleset is tied to a
  162. * process (i.e. as a domain), this tree is immutable until @usage
  163. * reaches zero.
  164. */
  165. struct rb_root root_inode;
  166. #if IS_ENABLED(CONFIG_INET)
  167. /**
  168. * @root_net_port: Root of a red-black tree containing &struct
  169. * landlock_rule nodes with network port. Once a ruleset is tied to a
  170. * process (i.e. as a domain), this tree is immutable until @usage
  171. * reaches zero.
  172. */
  173. struct rb_root root_net_port;
  174. #endif /* IS_ENABLED(CONFIG_INET) */
  175. /**
  176. * @hierarchy: Enables hierarchy identification even when a parent
  177. * domain vanishes. This is needed for the ptrace protection.
  178. */
  179. struct landlock_hierarchy *hierarchy;
  180. union {
  181. /**
  182. * @work_free: Enables to free a ruleset within a lockless
  183. * section. This is only used by
  184. * landlock_put_ruleset_deferred() when @usage reaches zero.
  185. * The fields @lock, @usage, @num_rules, @num_layers and
  186. * @access_masks are then unused.
  187. */
  188. struct work_struct work_free;
  189. struct {
  190. /**
  191. * @lock: Protects against concurrent modifications of
  192. * @root, if @usage is greater than zero.
  193. */
  194. struct mutex lock;
  195. /**
  196. * @usage: Number of processes (i.e. domains) or file
  197. * descriptors referencing this ruleset.
  198. */
  199. refcount_t usage;
  200. /**
  201. * @num_rules: Number of non-overlapping (i.e. not for
  202. * the same object) rules in this ruleset.
  203. */
  204. u32 num_rules;
  205. /**
  206. * @num_layers: Number of layers that are used in this
  207. * ruleset. This enables to check that all the layers
  208. * allow an access request. A value of 0 identifies a
  209. * non-merged ruleset (i.e. not a domain).
  210. */
  211. u32 num_layers;
  212. /**
  213. * @access_masks: Contains the subset of filesystem and
  214. * network actions that are restricted by a ruleset.
  215. * A domain saves all layers of merged rulesets in a
  216. * stack (FAM), starting from the first layer to the
  217. * last one. These layers are used when merging
  218. * rulesets, for user space backward compatibility
  219. * (i.e. future-proof), and to properly handle merged
  220. * rulesets without overlapping access rights. These
  221. * layers are set once and never changed for the
  222. * lifetime of the ruleset.
  223. */
  224. struct access_masks access_masks[];
  225. };
  226. };
  227. };
  228. struct landlock_ruleset *
  229. landlock_create_ruleset(const access_mask_t access_mask_fs,
  230. const access_mask_t access_mask_net,
  231. const access_mask_t scope_mask);
  232. void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
  233. void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
  234. int landlock_insert_rule(struct landlock_ruleset *const ruleset,
  235. const struct landlock_id id,
  236. const access_mask_t access);
  237. struct landlock_ruleset *
  238. landlock_merge_ruleset(struct landlock_ruleset *const parent,
  239. struct landlock_ruleset *const ruleset);
  240. const struct landlock_rule *
  241. landlock_find_rule(const struct landlock_ruleset *const ruleset,
  242. const struct landlock_id id);
  243. static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
  244. {
  245. if (ruleset)
  246. refcount_inc(&ruleset->usage);
  247. }
  248. /**
  249. * landlock_union_access_masks - Return all access rights handled in the
  250. * domain
  251. *
  252. * @domain: Landlock ruleset (used as a domain)
  253. *
  254. * Returns: an access_masks result of the OR of all the domain's access masks.
  255. */
  256. static inline struct access_masks
  257. landlock_union_access_masks(const struct landlock_ruleset *const domain)
  258. {
  259. union access_masks_all matches = {};
  260. size_t layer_level;
  261. for (layer_level = 0; layer_level < domain->num_layers; layer_level++) {
  262. union access_masks_all layer = {
  263. .masks = domain->access_masks[layer_level],
  264. };
  265. matches.all |= layer.all;
  266. }
  267. return matches.masks;
  268. }
  269. /**
  270. * landlock_get_applicable_domain - Return @domain if it applies to (handles)
  271. * at least one of the access rights specified
  272. * in @masks
  273. *
  274. * @domain: Landlock ruleset (used as a domain)
  275. * @masks: access masks
  276. *
  277. * Returns: @domain if any access rights specified in @masks is handled, or
  278. * NULL otherwise.
  279. */
  280. static inline const struct landlock_ruleset *
  281. landlock_get_applicable_domain(const struct landlock_ruleset *const domain,
  282. const struct access_masks masks)
  283. {
  284. const union access_masks_all masks_all = {
  285. .masks = masks,
  286. };
  287. union access_masks_all merge = {};
  288. if (!domain)
  289. return NULL;
  290. merge.masks = landlock_union_access_masks(domain);
  291. if (merge.all & masks_all.all)
  292. return domain;
  293. return NULL;
  294. }
  295. static inline void
  296. landlock_add_fs_access_mask(struct landlock_ruleset *const ruleset,
  297. const access_mask_t fs_access_mask,
  298. const u16 layer_level)
  299. {
  300. access_mask_t fs_mask = fs_access_mask & LANDLOCK_MASK_ACCESS_FS;
  301. /* Should already be checked in sys_landlock_create_ruleset(). */
  302. WARN_ON_ONCE(fs_access_mask != fs_mask);
  303. ruleset->access_masks[layer_level].fs |= fs_mask;
  304. }
  305. static inline void
  306. landlock_add_net_access_mask(struct landlock_ruleset *const ruleset,
  307. const access_mask_t net_access_mask,
  308. const u16 layer_level)
  309. {
  310. access_mask_t net_mask = net_access_mask & LANDLOCK_MASK_ACCESS_NET;
  311. /* Should already be checked in sys_landlock_create_ruleset(). */
  312. WARN_ON_ONCE(net_access_mask != net_mask);
  313. ruleset->access_masks[layer_level].net |= net_mask;
  314. }
  315. static inline void
  316. landlock_add_scope_mask(struct landlock_ruleset *const ruleset,
  317. const access_mask_t scope_mask, const u16 layer_level)
  318. {
  319. access_mask_t mask = scope_mask & LANDLOCK_MASK_SCOPE;
  320. /* Should already be checked in sys_landlock_create_ruleset(). */
  321. WARN_ON_ONCE(scope_mask != mask);
  322. ruleset->access_masks[layer_level].scope |= mask;
  323. }
  324. static inline access_mask_t
  325. landlock_get_fs_access_mask(const struct landlock_ruleset *const ruleset,
  326. const u16 layer_level)
  327. {
  328. /* Handles all initially denied by default access rights. */
  329. return ruleset->access_masks[layer_level].fs |
  330. LANDLOCK_ACCESS_FS_INITIALLY_DENIED;
  331. }
  332. static inline access_mask_t
  333. landlock_get_net_access_mask(const struct landlock_ruleset *const ruleset,
  334. const u16 layer_level)
  335. {
  336. return ruleset->access_masks[layer_level].net;
  337. }
  338. static inline access_mask_t
  339. landlock_get_scope_mask(const struct landlock_ruleset *const ruleset,
  340. const u16 layer_level)
  341. {
  342. return ruleset->access_masks[layer_level].scope;
  343. }
  344. bool landlock_unmask_layers(const struct landlock_rule *const rule,
  345. const access_mask_t access_request,
  346. layer_mask_t (*const layer_masks)[],
  347. const size_t masks_array_size);
  348. access_mask_t
  349. landlock_init_layer_masks(const struct landlock_ruleset *const domain,
  350. const access_mask_t access_request,
  351. layer_mask_t (*const layer_masks)[],
  352. const enum landlock_key_type key_type);
  353. #endif /* _SECURITY_LANDLOCK_RULESET_H */