1 // SPDX-License-Identifier: GPL-2.0-only
3 * Landlock LSM - Ruleset management
5 * Copyright © 2016-2020 Mickaël Salaün <mic@digikod.net>
6 * Copyright © 2018-2020 ANSSI
9 #include <linux/bits.h>
10 #include <linux/bug.h>
11 #include <linux/compiler_types.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/lockdep.h>
16 #include <linux/overflow.h>
17 #include <linux/rbtree.h>
18 #include <linux/refcount.h>
19 #include <linux/slab.h>
20 #include <linux/spinlock.h>
21 #include <linux/workqueue.h>
27 static struct landlock_ruleset *create_ruleset(const u32 num_layers)
29 struct landlock_ruleset *new_ruleset;
32 kzalloc(struct_size(new_ruleset, fs_access_masks, num_layers),
35 return ERR_PTR(-ENOMEM);
36 refcount_set(&new_ruleset->usage, 1);
37 mutex_init(&new_ruleset->lock);
38 new_ruleset->root = RB_ROOT;
39 new_ruleset->num_layers = num_layers;
43 * fs_access_masks[] = 0
48 struct landlock_ruleset *
49 landlock_create_ruleset(const access_mask_t fs_access_mask)
51 struct landlock_ruleset *new_ruleset;
53 /* Informs about useless ruleset. */
55 return ERR_PTR(-ENOMSG);
56 new_ruleset = create_ruleset(1);
57 if (!IS_ERR(new_ruleset))
58 new_ruleset->fs_access_masks[0] = fs_access_mask;
62 static void build_check_rule(void)
64 const struct landlock_rule rule = {
68 BUILD_BUG_ON(rule.num_layers < LANDLOCK_MAX_NUM_LAYERS);
71 static struct landlock_rule *
72 create_rule(struct landlock_object *const object,
73 const struct landlock_layer (*const layers)[], const u32 num_layers,
74 const struct landlock_layer *const new_layer)
76 struct landlock_rule *new_rule;
81 /* Should already be checked by landlock_merge_ruleset(). */
82 if (WARN_ON_ONCE(num_layers >= LANDLOCK_MAX_NUM_LAYERS))
83 return ERR_PTR(-E2BIG);
84 new_num_layers = num_layers + 1;
86 new_num_layers = num_layers;
88 new_rule = kzalloc(struct_size(new_rule, layers, new_num_layers),
91 return ERR_PTR(-ENOMEM);
92 RB_CLEAR_NODE(&new_rule->node);
93 landlock_get_object(object);
94 new_rule->object = object;
95 new_rule->num_layers = new_num_layers;
96 /* Copies the original layer stack. */
97 memcpy(new_rule->layers, layers,
98 flex_array_size(new_rule, layers, num_layers));
100 /* Adds a copy of @new_layer on the layer stack. */
101 new_rule->layers[new_rule->num_layers - 1] = *new_layer;
105 static void free_rule(struct landlock_rule *const rule)
110 landlock_put_object(rule->object);
114 static void build_check_ruleset(void)
116 const struct landlock_ruleset ruleset = {
120 typeof(ruleset.fs_access_masks[0]) fs_access_mask = ~0;
122 BUILD_BUG_ON(ruleset.num_rules < LANDLOCK_MAX_NUM_RULES);
123 BUILD_BUG_ON(ruleset.num_layers < LANDLOCK_MAX_NUM_LAYERS);
124 BUILD_BUG_ON(fs_access_mask < LANDLOCK_MASK_ACCESS_FS);
128 * insert_rule - Create and insert a rule in a ruleset
130 * @ruleset: The ruleset to be updated.
131 * @object: The object to build the new rule with. The underlying kernel
132 * object must be held by the caller.
133 * @layers: One or multiple layers to be copied into the new rule.
134 * @num_layers: The number of @layers entries.
136 * When user space requests to add a new rule to a ruleset, @layers only
137 * contains one entry and this entry is not assigned to any level. In this
138 * case, the new rule will extend @ruleset, similarly to a boolean OR between
141 * When merging a ruleset in a domain, or copying a domain, @layers will be
142 * added to @ruleset as new constraints, similarly to a boolean AND between
145 static int insert_rule(struct landlock_ruleset *const ruleset,
146 struct landlock_object *const object,
147 const struct landlock_layer (*const layers)[],
150 struct rb_node **walker_node;
151 struct rb_node *parent_node = NULL;
152 struct landlock_rule *new_rule;
155 lockdep_assert_held(&ruleset->lock);
156 if (WARN_ON_ONCE(!object || !layers))
158 walker_node = &(ruleset->root.rb_node);
159 while (*walker_node) {
160 struct landlock_rule *const this =
161 rb_entry(*walker_node, struct landlock_rule, node);
163 if (this->object != object) {
164 parent_node = *walker_node;
165 if (this->object < object)
166 walker_node = &((*walker_node)->rb_right);
168 walker_node = &((*walker_node)->rb_left);
172 /* Only a single-level layer should match an existing rule. */
173 if (WARN_ON_ONCE(num_layers != 1))
176 /* If there is a matching rule, updates it. */
177 if ((*layers)[0].level == 0) {
179 * Extends access rights when the request comes from
180 * landlock_add_rule(2), i.e. @ruleset is not a domain.
182 if (WARN_ON_ONCE(this->num_layers != 1))
184 if (WARN_ON_ONCE(this->layers[0].level != 0))
186 this->layers[0].access |= (*layers)[0].access;
190 if (WARN_ON_ONCE(this->layers[0].level == 0))
194 * Intersects access rights when it is a merge between a
195 * ruleset and a domain.
197 new_rule = create_rule(object, &this->layers, this->num_layers,
199 if (IS_ERR(new_rule))
200 return PTR_ERR(new_rule);
201 rb_replace_node(&this->node, &new_rule->node, &ruleset->root);
206 /* There is no match for @object. */
207 build_check_ruleset();
208 if (ruleset->num_rules >= LANDLOCK_MAX_NUM_RULES)
210 new_rule = create_rule(object, layers, num_layers, NULL);
211 if (IS_ERR(new_rule))
212 return PTR_ERR(new_rule);
213 rb_link_node(&new_rule->node, parent_node, walker_node);
214 rb_insert_color(&new_rule->node, &ruleset->root);
215 ruleset->num_rules++;
219 static void build_check_layer(void)
221 const struct landlock_layer layer = {
226 BUILD_BUG_ON(layer.level < LANDLOCK_MAX_NUM_LAYERS);
227 BUILD_BUG_ON(layer.access < LANDLOCK_MASK_ACCESS_FS);
230 /* @ruleset must be locked by the caller. */
231 int landlock_insert_rule(struct landlock_ruleset *const ruleset,
232 struct landlock_object *const object,
233 const access_mask_t access)
235 struct landlock_layer layers[] = { {
237 /* When @level is zero, insert_rule() extends @ruleset. */
242 return insert_rule(ruleset, object, &layers, ARRAY_SIZE(layers));
245 static inline void get_hierarchy(struct landlock_hierarchy *const hierarchy)
248 refcount_inc(&hierarchy->usage);
251 static void put_hierarchy(struct landlock_hierarchy *hierarchy)
253 while (hierarchy && refcount_dec_and_test(&hierarchy->usage)) {
254 const struct landlock_hierarchy *const freeme = hierarchy;
256 hierarchy = hierarchy->parent;
261 static int merge_ruleset(struct landlock_ruleset *const dst,
262 struct landlock_ruleset *const src)
264 struct landlock_rule *walker_rule, *next_rule;
268 /* Should already be checked by landlock_merge_ruleset() */
269 if (WARN_ON_ONCE(!src))
271 /* Only merge into a domain. */
272 if (WARN_ON_ONCE(!dst || !dst->hierarchy))
275 /* Locks @dst first because we are its only owner. */
276 mutex_lock(&dst->lock);
277 mutex_lock_nested(&src->lock, SINGLE_DEPTH_NESTING);
279 /* Stacks the new layer. */
280 if (WARN_ON_ONCE(src->num_layers != 1 || dst->num_layers < 1)) {
284 dst->fs_access_masks[dst->num_layers - 1] = src->fs_access_masks[0];
286 /* Merges the @src tree. */
287 rbtree_postorder_for_each_entry_safe(walker_rule, next_rule, &src->root,
289 struct landlock_layer layers[] = { {
290 .level = dst->num_layers,
293 if (WARN_ON_ONCE(walker_rule->num_layers != 1)) {
297 if (WARN_ON_ONCE(walker_rule->layers[0].level != 0)) {
301 layers[0].access = walker_rule->layers[0].access;
302 err = insert_rule(dst, walker_rule->object, &layers,
309 mutex_unlock(&src->lock);
310 mutex_unlock(&dst->lock);
314 static int inherit_ruleset(struct landlock_ruleset *const parent,
315 struct landlock_ruleset *const child)
317 struct landlock_rule *walker_rule, *next_rule;
324 /* Locks @child first because we are its only owner. */
325 mutex_lock(&child->lock);
326 mutex_lock_nested(&parent->lock, SINGLE_DEPTH_NESTING);
328 /* Copies the @parent tree. */
329 rbtree_postorder_for_each_entry_safe(walker_rule, next_rule,
330 &parent->root, node) {
331 err = insert_rule(child, walker_rule->object,
332 &walker_rule->layers,
333 walker_rule->num_layers);
338 if (WARN_ON_ONCE(child->num_layers <= parent->num_layers)) {
342 /* Copies the parent layer stack and leaves a space for the new layer. */
343 memcpy(child->fs_access_masks, parent->fs_access_masks,
344 flex_array_size(parent, fs_access_masks, parent->num_layers));
346 if (WARN_ON_ONCE(!parent->hierarchy)) {
350 get_hierarchy(parent->hierarchy);
351 child->hierarchy->parent = parent->hierarchy;
354 mutex_unlock(&parent->lock);
355 mutex_unlock(&child->lock);
359 static void free_ruleset(struct landlock_ruleset *const ruleset)
361 struct landlock_rule *freeme, *next;
364 rbtree_postorder_for_each_entry_safe(freeme, next, &ruleset->root, node)
366 put_hierarchy(ruleset->hierarchy);
370 void landlock_put_ruleset(struct landlock_ruleset *const ruleset)
373 if (ruleset && refcount_dec_and_test(&ruleset->usage))
374 free_ruleset(ruleset);
377 static void free_ruleset_work(struct work_struct *const work)
379 struct landlock_ruleset *ruleset;
381 ruleset = container_of(work, struct landlock_ruleset, work_free);
382 free_ruleset(ruleset);
385 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset)
387 if (ruleset && refcount_dec_and_test(&ruleset->usage)) {
388 INIT_WORK(&ruleset->work_free, free_ruleset_work);
389 schedule_work(&ruleset->work_free);
394 * landlock_merge_ruleset - Merge a ruleset with a domain
396 * @parent: Parent domain.
397 * @ruleset: New ruleset to be merged.
399 * Returns the intersection of @parent and @ruleset, or returns @parent if
400 * @ruleset is empty, or returns a duplicate of @ruleset if @parent is empty.
402 struct landlock_ruleset *
403 landlock_merge_ruleset(struct landlock_ruleset *const parent,
404 struct landlock_ruleset *const ruleset)
406 struct landlock_ruleset *new_dom;
411 if (WARN_ON_ONCE(!ruleset || parent == ruleset))
412 return ERR_PTR(-EINVAL);
415 if (parent->num_layers >= LANDLOCK_MAX_NUM_LAYERS)
416 return ERR_PTR(-E2BIG);
417 num_layers = parent->num_layers + 1;
422 /* Creates a new domain... */
423 new_dom = create_ruleset(num_layers);
427 kzalloc(sizeof(*new_dom->hierarchy), GFP_KERNEL_ACCOUNT);
428 if (!new_dom->hierarchy) {
432 refcount_set(&new_dom->hierarchy->usage, 1);
434 /* ...as a child of @parent... */
435 err = inherit_ruleset(parent, new_dom);
439 /* ...and including @ruleset. */
440 err = merge_ruleset(new_dom, ruleset);
447 landlock_put_ruleset(new_dom);
452 * The returned access has the same lifetime as @ruleset.
454 const struct landlock_rule *
455 landlock_find_rule(const struct landlock_ruleset *const ruleset,
456 const struct landlock_object *const object)
458 const struct rb_node *node;
462 node = ruleset->root.rb_node;
464 struct landlock_rule *this =
465 rb_entry(node, struct landlock_rule, node);
467 if (this->object == object)
469 if (this->object < object)
470 node = node->rb_right;
472 node = node->rb_left;