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 #ifndef _SECURITY_LANDLOCK_RULESET_H
10 #define _SECURITY_LANDLOCK_RULESET_H
12 #include <linux/bitops.h>
13 #include <linux/build_bug.h>
14 #include <linux/mutex.h>
15 #include <linux/rbtree.h>
16 #include <linux/refcount.h>
17 #include <linux/workqueue.h>
22 typedef u16 access_mask_t;
23 /* Makes sure all filesystem access rights can be stored. */
24 static_assert(BITS_PER_TYPE(access_mask_t) >= LANDLOCK_NUM_ACCESS_FS);
25 /* Makes sure for_each_set_bit() and for_each_clear_bit() calls are OK. */
26 static_assert(sizeof(unsigned long) >= sizeof(access_mask_t));
28 typedef u16 layer_mask_t;
29 /* Makes sure all layers can be checked. */
30 static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
33 * struct landlock_layer - Access rights for a given layer
35 struct landlock_layer {
37 * @level: Position of this layer in the layer stack.
41 * @access: Bitfield of allowed actions on the kernel object. They are
42 * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
48 * struct landlock_rule - Access rights tied to an object
50 struct landlock_rule {
52 * @node: Node in the ruleset's red-black tree.
56 * @object: Pointer to identify a kernel object (e.g. an inode). This
57 * is used as a key for this ruleset element. This pointer is set once
58 * and never modified. It always points to an allocated object because
59 * each rule increments the refcount of its object.
61 struct landlock_object *object;
63 * @num_layers: Number of entries in @layers.
67 * @layers: Stack of layers, from the latest to the newest, implemented
68 * as a flexible array member (FAM).
70 struct landlock_layer layers[] __counted_by(num_layers);
74 * struct landlock_hierarchy - Node in a ruleset hierarchy
76 struct landlock_hierarchy {
78 * @parent: Pointer to the parent node, or NULL if it is a root
81 struct landlock_hierarchy *parent;
83 * @usage: Number of potential children domains plus their parent
90 * struct landlock_ruleset - Landlock ruleset
92 * This data structure must contain unique entries, be updatable, and quick to
95 struct landlock_ruleset {
97 * @root: Root of a red-black tree containing &struct landlock_rule
98 * nodes. Once a ruleset is tied to a process (i.e. as a domain), this
99 * tree is immutable until @usage reaches zero.
103 * @hierarchy: Enables hierarchy identification even when a parent
104 * domain vanishes. This is needed for the ptrace protection.
106 struct landlock_hierarchy *hierarchy;
109 * @work_free: Enables to free a ruleset within a lockless
110 * section. This is only used by
111 * landlock_put_ruleset_deferred() when @usage reaches zero.
112 * The fields @lock, @usage, @num_rules, @num_layers and
113 * @fs_access_masks are then unused.
115 struct work_struct work_free;
118 * @lock: Protects against concurrent modifications of
119 * @root, if @usage is greater than zero.
123 * @usage: Number of processes (i.e. domains) or file
124 * descriptors referencing this ruleset.
128 * @num_rules: Number of non-overlapping (i.e. not for
129 * the same object) rules in this ruleset.
133 * @num_layers: Number of layers that are used in this
134 * ruleset. This enables to check that all the layers
135 * allow an access request. A value of 0 identifies a
136 * non-merged ruleset (i.e. not a domain).
140 * @fs_access_masks: Contains the subset of filesystem
141 * actions that are restricted by a ruleset. A domain
142 * saves all layers of merged rulesets in a stack
143 * (FAM), starting from the first layer to the last
144 * one. These layers are used when merging rulesets,
145 * for user space backward compatibility (i.e.
146 * future-proof), and to properly handle merged
147 * rulesets without overlapping access rights. These
148 * layers are set once and never changed for the
149 * lifetime of the ruleset.
151 access_mask_t fs_access_masks[];
156 struct landlock_ruleset *
157 landlock_create_ruleset(const access_mask_t fs_access_mask);
159 void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
160 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
162 int landlock_insert_rule(struct landlock_ruleset *const ruleset,
163 struct landlock_object *const object,
164 const access_mask_t access);
166 struct landlock_ruleset *
167 landlock_merge_ruleset(struct landlock_ruleset *const parent,
168 struct landlock_ruleset *const ruleset);
170 const struct landlock_rule *
171 landlock_find_rule(const struct landlock_ruleset *const ruleset,
172 const struct landlock_object *const object);
174 static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
177 refcount_inc(&ruleset->usage);
180 #endif /* _SECURITY_LANDLOCK_RULESET_H */