[PATCH v2 12/15] landlock: Free rulesets after an RCU grace period
From: Justin Suess
Date: Mon Aug 31 2026 - 14:01:07 EST
Defer every ruleset free behind an RCU grace period, and keep the
fields that stay readable while a free is pending out of the union
that overlays the deferred-free work item.
The policy_object_get LSM hook lets a caller holding only an
RCU-protected pointer to a ruleset (e.g. loaded from a BPF map kptr
field under rcu_read_lock()) race a refcount_inc_not_zero() against
the drop of the last reference. For that to be sound, the ruleset's
memory, and its reference count in particular, must remain valid
until every RCU reader that could still observe the pointer is done:
free the ruleset through queue_rcu_work(), which waits for a grace
period before running the free work.
The work item is overlaid with the fields that no one may touch once
@usage reaches zero: @lock, @quiet_masks and @handled_masks. @usage
itself stays outside the union so a racing reader observes zero
instead of the work item's bytes, and the tracing fields @version and
@id stay outside too because the landlock_free_ruleset trace event
reads them when the queued work finally runs.
Since queueing the work never sleeps, the might_sleep() annotation
is dropped: a following commit releases ruleset references from
BPF object destructors that cannot sleep.
Cc: Mickaël Salaün <mic@xxxxxxxxxxx>
Signed-off-by: Justin Suess <utilityemal77@xxxxxxxxx>
---
security/landlock/ruleset.c | 24 ++++++++++++++---
security/landlock/ruleset.h | 54 ++++++++++++++++++++++++-------------
2 files changed, 57 insertions(+), 21 deletions(-)
diff --git a/security/landlock/ruleset.c b/security/landlock/ruleset.c
index 0d07707523cd..00a6b9938fd1 100644
--- a/security/landlock/ruleset.c
+++ b/security/landlock/ruleset.c
@@ -21,6 +21,7 @@
#include <linux/refcount.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
+#include <linux/workqueue.h>
#include <uapi/linux/landlock.h>
#include "access.h"
@@ -346,9 +347,26 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)
kfree(ruleset);
}
+static void free_ruleset_work(struct work_struct *const work)
+{
+ struct landlock_ruleset *ruleset;
+
+ ruleset = container_of(to_rcu_work(work), struct landlock_ruleset,
+ work_free);
+ free_ruleset(ruleset);
+}
+
+/*
+ * RCU readers (cf. the policy_object_get LSM hook) may call
+ * refcount_inc_not_zero() on a ruleset they hold no reference to: the memory
+ * must survive a grace period after the last put. Queueing the free also
+ * makes this callable from contexts that cannot sleep (cf. the
+ * policy_object_put LSM hook).
+ */
void landlock_put_ruleset(struct landlock_ruleset *const ruleset)
{
- might_sleep();
- if (ruleset && refcount_dec_and_test(&ruleset->usage))
- free_ruleset(ruleset);
+ if (ruleset && refcount_dec_and_test(&ruleset->usage)) {
+ INIT_RCU_WORK(&ruleset->work_free, free_ruleset_work);
+ queue_rcu_work(system_dfl_wq, &ruleset->work_free);
+ }
}
diff --git a/security/landlock/ruleset.h b/security/landlock/ruleset.h
index b58e3d9846af..1465f8a5c464 100644
--- a/security/landlock/ruleset.h
+++ b/security/landlock/ruleset.h
@@ -15,6 +15,7 @@
#include <linux/mutex.h>
#include <linux/rbtree.h>
#include <linux/refcount.h>
+#include <linux/workqueue.h>
#include "access.h"
#include "limits.h"
@@ -157,12 +158,10 @@ struct landlock_ruleset {
*/
struct landlock_rules rules;
/**
- * @lock: Protects against concurrent modifications of @rules, if @usage
- * is greater than zero.
- */
- struct mutex lock;
- /**
- * @usage: Number of file descriptors referencing this ruleset.
+ * @usage: Number of file descriptors referencing this ruleset. Kept
+ * outside the union with @work_free: RCU readers may still call
+ * refcount_inc_not_zero() while a queued free waits out the grace
+ * period.
*/
refcount_t usage;
@@ -175,22 +174,41 @@ struct landlock_ruleset {
*/
u32 version;
/**
- * @id: Unique identifier for this ruleset, used for tracing.
+ * @id: Unique identifier for this ruleset, used for tracing. Kept
+ * outside the union with @work_free: the free_ruleset trace event
+ * reads it after the free has been queued.
*/
u64 id;
#endif /* CONFIG_TRACEPOINTS */
- /**
- * @quiet_masks: Stores the quiet flags for an unmerged ruleset. For a
- * merged domain, this is stored in each layer's struct
- * landlock_hierarchy instead.
- */
- struct access_masks quiet_masks;
- /**
- * @handled_masks: Contains the subset of filesystem and network actions
- * that are handled by this ruleset.
- */
- struct access_masks handled_masks;
+ union {
+ /**
+ * @work_free: Enables to free a ruleset after an RCU grace
+ * period, within a lockless section. This is queued by
+ * landlock_put_ruleset() when @usage reaches zero. The
+ * fields @lock, @quiet_masks and @handled_masks are then
+ * unused.
+ */
+ struct rcu_work work_free;
+ struct {
+ /**
+ * @lock: Protects against concurrent modifications of
+ * @rules, if @usage is greater than zero.
+ */
+ struct mutex lock;
+ /**
+ * @quiet_masks: Stores the quiet flags for an unmerged
+ * ruleset. For a merged domain, this is stored in each
+ * layer's struct landlock_hierarchy instead.
+ */
+ struct access_masks quiet_masks;
+ /**
+ * @handled_masks: Contains the subset of filesystem and
+ * network actions that are handled by this ruleset.
+ */
+ struct access_masks handled_masks;
+ };
+ };
};
struct landlock_ruleset *
--
2.55.0