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);
26 typedef u16 layer_mask_t;
27 /* Makes sure all layers can be checked. */
28 static_assert(BITS_PER_TYPE(layer_mask_t) >= LANDLOCK_MAX_NUM_LAYERS);
31 * struct landlock_layer - Access rights for a given layer
33 struct landlock_layer {
35 * @level: Position of this layer in the layer stack.
39 * @access: Bitfield of allowed actions on the kernel object. They are
40 * relative to the object type (e.g. %LANDLOCK_ACTION_FS_READ).
46 * struct landlock_rule - Access rights tied to an object
48 struct landlock_rule {
50 * @node: Node in the ruleset's red-black tree.
54 * @object: Pointer to identify a kernel object (e.g. an inode). This
55 * is used as a key for this ruleset element. This pointer is set once
56 * and never modified. It always points to an allocated object because
57 * each rule increments the refcount of its object.
59 struct landlock_object *object;
61 * @num_layers: Number of entries in @layers.
65 * @layers: Stack of layers, from the latest to the newest, implemented
66 * as a flexible array member (FAM).
68 struct landlock_layer layers[];
72 * struct landlock_hierarchy - Node in a ruleset hierarchy
74 struct landlock_hierarchy {
76 * @parent: Pointer to the parent node, or NULL if it is a root
79 struct landlock_hierarchy *parent;
81 * @usage: Number of potential children domains plus their parent
88 * struct landlock_ruleset - Landlock ruleset
90 * This data structure must contain unique entries, be updatable, and quick to
93 struct landlock_ruleset {
95 * @root: Root of a red-black tree containing &struct landlock_rule
96 * nodes. Once a ruleset is tied to a process (i.e. as a domain), this
97 * tree is immutable until @usage reaches zero.
101 * @hierarchy: Enables hierarchy identification even when a parent
102 * domain vanishes. This is needed for the ptrace protection.
104 struct landlock_hierarchy *hierarchy;
107 * @work_free: Enables to free a ruleset within a lockless
108 * section. This is only used by
109 * landlock_put_ruleset_deferred() when @usage reaches zero.
110 * The fields @lock, @usage, @num_rules, @num_layers and
111 * @fs_access_masks are then unused.
113 struct work_struct work_free;
116 * @lock: Protects against concurrent modifications of
117 * @root, if @usage is greater than zero.
121 * @usage: Number of processes (i.e. domains) or file
122 * descriptors referencing this ruleset.
126 * @num_rules: Number of non-overlapping (i.e. not for
127 * the same object) rules in this ruleset.
131 * @num_layers: Number of layers that are used in this
132 * ruleset. This enables to check that all the layers
133 * allow an access request. A value of 0 identifies a
134 * non-merged ruleset (i.e. not a domain).
138 * @fs_access_masks: Contains the subset of filesystem
139 * actions that are restricted by a ruleset. A domain
140 * saves all layers of merged rulesets in a stack
141 * (FAM), starting from the first layer to the last
142 * one. These layers are used when merging rulesets,
143 * for user space backward compatibility (i.e.
144 * future-proof), and to properly handle merged
145 * rulesets without overlapping access rights. These
146 * layers are set once and never changed for the
147 * lifetime of the ruleset.
149 access_mask_t fs_access_masks[];
154 struct landlock_ruleset *
155 landlock_create_ruleset(const access_mask_t fs_access_mask);
157 void landlock_put_ruleset(struct landlock_ruleset *const ruleset);
158 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset);
160 int landlock_insert_rule(struct landlock_ruleset *const ruleset,
161 struct landlock_object *const object,
162 const access_mask_t access);
164 struct landlock_ruleset *
165 landlock_merge_ruleset(struct landlock_ruleset *const parent,
166 struct landlock_ruleset *const ruleset);
168 const struct landlock_rule *
169 landlock_find_rule(const struct landlock_ruleset *const ruleset,
170 const struct landlock_object *const object);
172 static inline void landlock_get_ruleset(struct landlock_ruleset *const ruleset)
175 refcount_inc(&ruleset->usage);
178 #endif /* _SECURITY_LANDLOCK_RULESET_H */