Skip to contentSkip to conversation

BPF interface for applying Landlock rulesets

PATCH Superseded Justin Suess 4 participants 34 messages Jul 31 – Aug 28

Subsystems (7)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover BPF interface for applying Landlock rulesets

Howdy,

This series lets BPF programs apply an existing, userspace-created Landlock ruleset to a program during exec. The goal is unchanged from the RFC [1]: BPF does not create, inspect, or mutate Landlock policy, it only decides whether a ruleset that was already created and validated through Landlock's existing userspace API should be applied, based on runtime exec context.

Motivation
---
Deploying Landlock today requires the sandboxed program's cooperation. A process can only restrict itself, so applying a policy system-wide means wrapping every launch path with a helper that calls landlock_restrict_self(2) before exec, and anything spawned outside those wrappers runs unconfined. Supervising exec from userspace instead (ptrace, seccomp user notifications) is racy and slow. Meanwhile, the tools that do enforce system-wide policy with BPF LSM programs today (container security agents such as KubeArmor and Tetragon) end up reimplementing path-based access control in BPF, fraught with horrors of path reconstruction, bind mounts, rename races, and worse, which is exactly the problem Landlock already solves in the kernel, with maintained and versioned semantics.

This series composes BPF and Landlock along their natural grain. The intended deployment: a supervisor creates one ruleset per policy class through the existing syscalls, a syscall BPF program parks them in map kptr fields, and an LSM BPF program picks which (if any) to apply to an execution based on its runtime context. The policy semantics stay Landlock's; BPF contributes only the programmable decision of when and to whom. Deciding inside the exec path closes the race a userspace supervisor cannot: the policy is in place before the first instruction of the new program runs.

Addressing RFC feedback
---

The original thread at [1] received useful feedback from the BPF, LSM and Landlock maintainers. I've hopefully been able to address all of it here.

The interface is redesigned around the conclusions of the RFC thread. While it's been over a week since sending [2] detailing some of the redesign, I figure it's better to show the code.

Four main takeaways came out of the RFC feedback:

1) no kernel or BPF code may call directly into an individual LSM, every LSM interface must go through the LSM framework.

2) LSMs interact with the kernel subsystems through LSM hooks. kfuncs are just hooks from the LSM perspective.

3) A new map type is untenable; new maps will not be added for single consumers. Especially because new map types become ABI.

4) We must implement a generic interface usable by all LSM, without forgoing the type safety guarantees provided by BPF or making a ioctl-like multiplexer.

This series does all, as proposed in [2]: the BPF subsystem owns per-LSM, strongly typed kfuncs in kernel/bpf/bpf_lsm.c, and every kfunc call passes through a generic LSM hook. Individual LSMs implement ordinary LSM hooks and define no BPF interfaces at all.

The three generic hooks carry the policy reference across the LSM boundary in union lsm_policy_kptr, tagged by the LSM_ID_* value passed alongside. The shims dispatch only to the LSM matching that id (following the setprocattr pattern) and return -EOPNOTSUPP when it is compiled out or not enabled, so program loading stays independent of the boot-time LSM configuration and kernel/bpf/ has no build-time dependency on Landlock.

The LSM hooks
===

security_policy_kptr_from_fd(lsmid, fd, &policy) security_policy_kptr_put(lsmid, &policy) security_bprm_enforce_policy_kptr(lsmid, bprm, &policy, flags)

Landlock implements them as ordinary LSM hooks. The from_fd hook validates a ruleset fd the same way as the Landlock syscalls; the enforce hook stages a restriction on the credentials prepared for an execution, computed by the same helpers as landlock_restrict_self(2); and a bprm_committing_creds() hook applies it past the exec point of no return, where nothing can fail. An execution either starts confined by the domain or leaves the calling task untouched: a failed execution releases the staged restriction when its aborted credentials are freed, so no domain is created for an execution that never happens.

The Landlock kfuncs
===

bpf_landlock_get_ruleset_from_fd(fd) KF_ACQUIRE | KF_RET_NULL
bpf_landlock_restrict_binprm(bprm, ruleset, flags)
bpf_landlock_put_ruleset(ruleset) KF_RELEASE

All sleepable-only. The acquire kfunc is exclusive to syscall programs, which run in the context of the task whose fd table gives a ruleset fd meaning; enforcement is exclusive to LSM programs attached to bprm_creds_for_exec or bprm_creds_from_file; release works in both. A kptr destructor is registered so acquired rulesets can be stashed in ordinary map kptr fields, which is why the put hook must tolerate non-sleepable contexts (the free is deferred).

The selftests exercise the deployment described above end to end:
a syscall program acquires the supervisor's ruleset and parks it in a map kptr field; the LSM program takes it from the map and enforces it on the executions it monitors. The landlock_restrict_self(2) flags apply with their usual semantics, except LANDLOCK_RESTRICT_SELF_TSYNC, which targets the calling threads rather than the execution and is rejected with -EINVAL.

Changes since the RFC (quite big):
- every kfunc call now passes through a generic LSM hook; the kfuncs moved from security/landlock to kernel/bpf/bpf_lsm.c and nothing calls directly into Landlock
- the BPF-facing interface stays strongly typed per LSM; the tagged union only travels across the LSM boundary. This is superior to the void* model proposed in [2].
- BPF_MAP_TYPE_LANDLOCK_RULESET is nixed: the acquire kfunc plus an ordinary map kptr field with a registered destructor replaces the dedicated map type
- the whole restriction is now staged in the bprm credentials and applied at bprm_committing_creds(), where nothing can fail, instead of only staging the no_new_privs bit
- domain allocations on the kfunc path no longer charge the mediated task's memcg: the enforce hook computes the restriction in a root memcg charging scope
- LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS was split into its own series
[3] and is not carried here; this series only stages the Landlock domain. Nothing about it was necessary for this series.
- no new UAPI: no map type, no new flag, no ABI bump (since this series touches no Landlock ABI directly and the kfunc/syscall side share common code paths).

Subsystem Summary
---

To LSM maintainers: This was the biggest change. There are three new generic hooks and the union lsm_policy_kptr to abstract the lsm-specific features declared next to lsm_prop in security.h with one member per participating LSM. The hooks are ordinary, LSM-agnostic entry points usable by any kernel-internal caller, with targeted dispatch by lsm_id following the setprocattr precedent. It will return -EOPNOTSUPP when the named LSM is compiled out or disabled. Nothing in security/ knows about BPF, and BPF knows nothing about the enabled LSMs.

To Landlock maintainers: there is no ABI or behavior change for existing users. The hook implementations reuse the syscall code
paths: ruleset fd validation is shared with the Landlock syscalls, and the restriction is computed by the same helpers as landlock_restrict_self(2), factored into prepare/apply steps so future restrict_self features can hopefully work for both entry points. What is new is the entry point: a restriction staged on the bprm credentials and committed at bprm_committing_creds(), past the point of no return. The memory accounting issue pointed out by Mickaël is addressed as well. [4]

To BPF maintainers: BPF owns the three strongly typed kfuncs in kernel/bpf/bpf_lsm.c, gated per program type (acquire in syscall programs, enforcement in sleepable LSM programs on the two bprm hooks), with a registered kptr destructor so rulesets can live in ordinary map kptr fields. No new map type, no BPF internals touched. kernel/bpf/ has no build-time dependency on Landlock or any other LSM since it just calls the LSM hooks.

Patches 1-3 add the LSM hooks, 4-5 prepare Landlock (fd lookup exposure, the restrict_self refactoring), 6 implements the hooks, 7-10 add the BPF-owned kfuncs, 11 selftests (end-to-end enforcement, TSYNC rejection, staged-restriction replacement and discard on a failed exec, plus six verifier rejection cases), 12-13 documentation.

Tested with a BPF CI run and manual run of the Landlock selftests.

Based on bpf-next/master, but applies cleanly to lsm/next.

[1] https://lore.kernel.org/linux-security-module/[email protected]/
[2] https://lore.kernel.org/linux-security-module/al_TBtXYUNGLZHC2@zenbox/
[3] https://lore.kernel.org/linux-security-module/[email protected]/
[4] https://lore.kernel.org/linux-security-module/[email protected]/

Justin Suess (13):
lsm: Add LSM hook security_policy_kptr_from_fd
lsm: Add LSM hook security_policy_kptr_put
lsm: Add LSM hook security_bprm_enforce_policy_kptr
landlock: Expose the ruleset fd lookup to the rest of Landlock
landlock: Factor the credential restriction out of
landlock_restrict_self()
landlock: Implement the LSM policy kptr hooks
bpf: Add the LSM policy kfunc infrastructure
bpf: Add the bpf_landlock_put_ruleset kfunc and ruleset destructor
bpf: Add the bpf_landlock_get_ruleset_from_fd kfunc
bpf: Add the bpf_landlock_restrict_binprm kfunc selftests/bpf: Add tests for the Landlock policy kfuncs
landlock: Document the BPF kfunc interface
lsm: Document the LSM policy kptr hooks

 Documentation/security/landlock.rst           |  25 ++
 Documentation/security/lsm-development.rst    |  27 ++
 include/linux/lsm_hook_defs.h                 |   5 +
 include/linux/security.h                      |  43 +++
 kernel/bpf/bpf_lsm.c                          | 202 +++++++++++
 security/landlock/Makefile                    |   2 +
 security/landlock/bpf.c                       | 130 +++++++
 security/landlock/bpf.h                       |  21 ++
 security/landlock/cred.c                      | 116 +++++-
 security/landlock/cred.h                      |  51 +++
 security/landlock/limits.h                    |   4 +
 security/landlock/ruleset.c                   |   2 +-
 security/landlock/ruleset.h                   |   3 +
 security/landlock/setup.c                     |   2 +
 security/landlock/syscalls.c                  |  70 +---
 security/security.c                           | 106 ++++++
 tools/testing/selftests/bpf/config            |   1 +
 tools/testing/selftests/bpf/config.x86_64     |   2 +-
 .../bpf/prog_tests/lsm_policy_kfuncs.c        | 329 ++++++++++++++++++
 .../bpf/progs/lsm_policy_kfuncs_failure.c     | 100 ++++++
 .../bpf/progs/lsm_policy_kfuncs_success.c     | 107 ++++++
 21 files changed, 1292 insertions(+), 56 deletions(-)
 create mode 100644 security/landlock/bpf.c
 create mode 100644 security/landlock/bpf.h
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_success.c

base-commit: f0e80dee4e32fd11e6ee1b714b75f681c7cafd3e

Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/13 lsm: Add LSM hook security_policy_kptr_from_fd

Add a generic LSM hook handing out a reference to an LSM policy object on behalf of a kernel-internal caller:

security_policy_kptr_from_fd(lsmid, fd, &policy)

together with union lsm_policy_kptr, the tagged payload carrying such a reference across the LSM boundary.

The union holds one per-LSM member; which member is valid is determined by the lsmid the caller passes. The pointers the members hold are the BTF-visible handles whose provenance the BPF verifier guarantees, i.e. referenced kptrs, hence the hook naming:
the reference stays strongly typed from the BPF program through the hook to the owning LSM, which resolves the handle to its internal policy representation.

This hook backs BPF kfuncs: it lets an LSM hand out a reference to one of its policy objects (identified by a file descriptor created through the LSM's own userspace API) without exporting any symbol or defining any BPF interface itself. The reference is released through security_policy_kptr_put(), added by the next patch.

The hook uses targeted dispatch: the shim walks the hook list and only calls the implementation registered by the LSM matching @lsmid, following the security_getprocattr()/security_setprocattr() patterns for generic hooks carrying LSM-specific payloads. When no active LSM matches, the shim returns -EOPNOTSUPP: a kfunc call for an LSM that is compiled out or not enabled fails at runtime rather than being hidden from the BPF program at verification time. For the same reason the union members are not guarded by the LSMs' CONFIG options: the callers are built independently of any individual LSM.

The hook is excluded from the "bpf" LSM's attachment points. The targeted dispatch would only reach a program attached there for calls with LSM_ID_BPF, which no caller passes, so the attachment point would be dead.

Cc Paul Moore <[email protected]>
Cc Casey Schaufler <[email protected]>

---

Notes:
The hook naming choice of policy_kptr_from_fd and the other hooks is up in the air for me. I decided to include the kptr part in the name because it's relevant to the task being
performed: we are simply getting a pointer to a kernel policy object from a file descriptor.

I'm open to better ideas for the name...

 include/linux/lsm_hook_defs.h |  2 ++
 include/linux/security.h      | 25 +++++++++++++++++++++++
 kernel/bpf/bpf_lsm.c          |  1 +
 security/security.c           | 38 +++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+)
include/linux/lsm_hook_defs.h +2 -0
index 65c9609ec207..afd5b3f932a9 100644
@@ -452,6 +452,8 @@ LSM_HOOK(int, 0, bpf_token_create, struct bpf_token *token, union bpf_attr *attr
 LSM_HOOK(void, LSM_RET_VOID, bpf_token_free, struct bpf_token *token)
 LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd)
 LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
+LSM_HOOK(int, -EOPNOTSUPP, policy_kptr_from_fd, int fd,
+	 union lsm_policy_kptr *policy)
 #endif /* CONFIG_BPF_SYSCALL */
 
 LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
include/linux/security.h +25 -0
index 153e9043058f..db807e61d310 100644
@@ -168,6 +168,23 @@ struct lsm_prop {
 	struct lsm_prop_bpf bpf;
 };
 
+struct bpf_landlock_ruleset;
+
+struct lsm_policy_landlock {
+	struct bpf_landlock_ruleset *ruleset;
+};
+
+/*
+ * A reference to an LSM policy object, tagged by the LSM_ID_* value
+ * passed alongside: only the matching LSM's member is valid.  The
+ * members are not guarded by the LSMs' CONFIG options: the callers
+ * are built independently of any individual LSM and a call for a
+ * missing LSM must fail at runtime, not at build time.
+ */
+union lsm_policy_kptr {
+	struct lsm_policy_landlock landlock;
+};
+
 extern const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX+1];
 
 /* These functions are in security/commoncap.c */
@@ -2312,6 +2329,8 @@ extern int security_bpf_token_create(struct bpf_token *token, union bpf_attr *at
 extern void security_bpf_token_free(struct bpf_token *token);
 extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cmd);
 extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
+extern int security_policy_kptr_from_fd(u64 lsmid, int fd,
+					union lsm_policy_kptr *policy);
 #else
 static inline int security_bpf(int cmd, union bpf_attr *attr,
 			       unsigned int size, bool kernel)
@@ -2365,6 +2384,12 @@ static inline int security_bpf_token_capable(const struct bpf_token *token, int
 {
 	return 0;
 }
+
+static inline int security_policy_kptr_from_fd(u64 lsmid, int fd,
+					       union lsm_policy_kptr *policy)
+{
+	return -EOPNOTSUPP;
+}
 #endif /* CONFIG_SECURITY */
 #endif /* CONFIG_BPF_SYSCALL */
 
kernel/bpf/bpf_lsm.c +1 -0
index 3983b4ce73c8..9fa514204fb5 100644
@@ -56,6 +56,7 @@ BTF_ID(func, bpf_lsm_xfrm_decode_session)
 #endif
 BTF_ID(func, bpf_lsm_ismaclabel)
 BTF_ID(func, bpf_lsm_file_alloc_security)
+BTF_ID(func, bpf_lsm_policy_kptr_from_fd)
 BTF_SET_END(bpf_lsm_disabled_hooks)
 
 /* List of LSM hooks that should operate on 'current' cgroup regardless
security/security.c +38 -0
index 71aea8fdf014..14fd8b878cd0 100644
@@ -5441,6 +5441,44 @@ int security_bpf_token_capable(const struct bpf_token *token, int cap)
 	return call_int_hook(bpf_token_capable, token, cap);
 }
 
+/**
+ * security_policy_kptr_from_fd() - Get an LSM policy object from a fd
+ * @lsmid: LSM_ID_* value of the LSM asked to interpret @fd
+ * @fd: file descriptor referring to a policy object, resolved in the
+ *      calling task's file descriptor table
+ * @policy: receives the referenced policy object in the member of the
+ *          LSM identified by @lsmid
+ *
+ * Ask the LSM identified by @lsmid to translate @fd into a reference
+ * counted policy object.  The caller must not dereference the
+ * returned handle, must only hand it back to the same LSM, e.g.
+ * through security_bprm_enforce_policy_kptr(), and must release it
+ * with security_policy_kptr_put().  Only the hook implementation of
+ * the LSM identified by @lsmid is called.  The hook is only called
+ * from a context that may sleep.
+ *
+ * An implementation must fill its own member of @policy with a
+ * reference that remains valid until it is released through the
+ * policy_kptr_put hook, and must not assume anything about @fd beyond
+ * what its own userspace API created it with.
+ *
+ * Return: Returns 0 if @policy holds a reference counted policy
+ * object, -EOPNOTSUPP if the LSM does not implement the hook, negative
+ * values on other failures.
+ */
+int security_policy_kptr_from_fd(u64 lsmid, int fd,
+				 union lsm_policy_kptr *policy)
+{
+	struct lsm_static_call *scall;
+
+	lsm_for_each_hook(scall, policy_kptr_from_fd) {
+		if (scall->hl->lsmid->id != lsmid)
+			continue;
+		return scall->hl->hook.policy_kptr_from_fd(fd, policy);
+	}
+	return LSM_RET_DEFAULT(policy_kptr_from_fd);
+}
+
 /**
  * security_bpf_map_free() - Free a bpf map's LSM blob
  * @map: bpf map
Signature · 1 line
2.54.0
#2 lore raw

PATCH 2/13 lsm: Add LSM hook security_policy_kptr_put

In reply to Justin Suess

Add the generic LSM hook releasing a reference obtained through security_policy_kptr_from_fd():

security_policy_kptr_put(lsmid, &policy)

The shim uses the same targeted dispatch by @lsmid as the get hook:
only the implementation registered by the matching LSM is called, and it only ever reads its own member of union lsm_policy_kptr, so a policy object only ever travels back to the LSM that produced it. The hook is void: releasing a reference cannot fail. A reference can only come from the matching LSM's policy_kptr_from_fd hook, so a dispatch miss means a caller passed the wrong lsmid or an LSM implemented the get hook without the put hook; the shim warns instead of silently leaking the reference.

An implementation must support being called from a context that cannot
sleep: the release of BPF managed references may be driven from object destructors.

Like the get hook, this hook is excluded from the "bpf" LSM's attachment points, as the targeted dispatch makes an attachment there unreachable.

Cc Paul Moore <[email protected]>
Cc Casey Schaufler <[email protected]>
---
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  6 ++++++
 kernel/bpf/bpf_lsm.c          |  1 +
 security/security.c           | 27 +++++++++++++++++++++++++++
 4 files changed, 35 insertions(+)
include/linux/lsm_hook_defs.h +1 -0
index afd5b3f932a9..0800622e317f 100644
@@ -454,6 +454,7 @@ LSM_HOOK(int, 0, bpf_token_cmd, const struct bpf_token *token, enum bpf_cmd cmd)
 LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
 LSM_HOOK(int, -EOPNOTSUPP, policy_kptr_from_fd, int fd,
 	 union lsm_policy_kptr *policy)
+LSM_HOOK(void, LSM_RET_VOID, policy_kptr_put, union lsm_policy_kptr *policy)
 #endif /* CONFIG_BPF_SYSCALL */
 
 LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
include/linux/security.h +6 -0
index db807e61d310..5017a335918c 100644
@@ -2331,6 +2331,7 @@ extern int security_bpf_token_cmd(const struct bpf_token *token, enum bpf_cmd cm
 extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
 extern int security_policy_kptr_from_fd(u64 lsmid, int fd,
 					union lsm_policy_kptr *policy);
+extern void security_policy_kptr_put(u64 lsmid, union lsm_policy_kptr *policy);
 #else
 static inline int security_bpf(int cmd, union bpf_attr *attr,
 			       unsigned int size, bool kernel)
@@ -2390,6 +2391,11 @@ static inline int security_policy_kptr_from_fd(u64 lsmid, int fd,
 {
 	return -EOPNOTSUPP;
 }
+
+static inline void security_policy_kptr_put(u64 lsmid,
+					    union lsm_policy_kptr *policy)
+{
+}
 #endif /* CONFIG_SECURITY */
 #endif /* CONFIG_BPF_SYSCALL */
 
kernel/bpf/bpf_lsm.c +1 -0
index 9fa514204fb5..e9059d43e92c 100644
@@ -57,6 +57,7 @@ BTF_ID(func, bpf_lsm_xfrm_decode_session)
 BTF_ID(func, bpf_lsm_ismaclabel)
 BTF_ID(func, bpf_lsm_file_alloc_security)
 BTF_ID(func, bpf_lsm_policy_kptr_from_fd)
+BTF_ID(func, bpf_lsm_policy_kptr_put)
 BTF_SET_END(bpf_lsm_disabled_hooks)
 
 /* List of LSM hooks that should operate on 'current' cgroup regardless
security/security.c +27 -0
index 14fd8b878cd0..fd535bd00c24 100644
@@ -5479,6 +5479,33 @@ int security_policy_kptr_from_fd(u64 lsmid, int fd,
 	return LSM_RET_DEFAULT(policy_kptr_from_fd);
 }
 
+/**
+ * security_policy_kptr_put() - Put a reference on an LSM policy object
+ * @lsmid: LSM_ID_* value of the LSM owning @policy
+ * @policy: the policy object, in the member of the LSM identified by
+ *          @lsmid
+ *
+ * Release a reference previously obtained with
+ * security_policy_kptr_from_fd().  Only the hook implementation
+ * of the LSM identified by @lsmid is called, and @policy must have
+ * been obtained from that same LSM.  An implementation must support
+ * being called from a context that cannot sleep: the release of BPF
+ * managed references may be driven from object destructors.
+ */
+void security_policy_kptr_put(u64 lsmid, union lsm_policy_kptr *policy)
+{
+	struct lsm_static_call *scall;
+
+	lsm_for_each_hook(scall, policy_kptr_put) {
+		if (scall->hl->lsmid->id != lsmid)
+			continue;
+		scall->hl->hook.policy_kptr_put(policy);
+		return;
+	}
+	/* A held reference implies the matching LSM implements the hook. */
+	WARN_ON_ONCE(1);
+}
+
 /**
  * security_bpf_map_free() - Free a bpf map's LSM blob
  * @map: bpf map
Signature · 1 line
2.54.0
#4 lore raw

PATCH 3/13 lsm: Add LSM hook security_bprm_enforce_policy_kptr

In reply to Justin Suess

Add a generic LSM hook enforcing an LSM policy object on the credentials prepared for an execution:

security_bprm_enforce_policy_kptr(lsmid, bprm, &policy, flags)

The policy object is obtained from the owning LSM through security_policy_kptr_from_fd(), travels in that LSM's member of union lsm_policy_kptr, and is handed back only to that same LSM: the shim uses the same targeted dispatch by @lsmid as the policy kptr lifetime hooks, and returns -EOPNOTSUPP when no active LSM matches.

This is the first policy operation backed by the BPF-owned LSM
kfuncs: it lets a sleepable LSM BPF program attached to bprm_creds_for_exec() or bprm_creds_from_file() arrange for the executed task to start confined by a policy created through the LSM's own userspace API, e.g. a Landlock ruleset applied to a binprm. The BPF-facing kfunc keeps the policy pointer strongly BTF-typed all the way to the union member the implementing LSM reads back.

The hook contract is LSM agnostic: any LSM with a notion of a per-task policy object can implement it, with its own semantics for how the policy composes with restrictions the credentials already carry and for the meaning of @flags, unsupported values of which it must reject with -EINVAL. Implementations can rely on being called only between the preparation and the commitment of the bprm's credentials.

Like the policy kptr lifetime hooks, this hook is excluded from the "bpf" LSM's attachment points, as the targeted dispatch makes an attachment there unreachable.

Cc Paul Moore <[email protected]>
Cc Casey Schaufler <[email protected]>
---
 include/linux/lsm_hook_defs.h |  2 ++
 include/linux/security.h      | 12 ++++++++++
 kernel/bpf/bpf_lsm.c          |  1 +
 security/security.c           | 41 +++++++++++++++++++++++++++++++++++
 4 files changed, 56 insertions(+)
include/linux/lsm_hook_defs.h +2 -0
index 0800622e317f..a70edbd7b761 100644
@@ -455,6 +455,8 @@ LSM_HOOK(int, 0, bpf_token_capable, const struct bpf_token *token, int cap)
 LSM_HOOK(int, -EOPNOTSUPP, policy_kptr_from_fd, int fd,
 	 union lsm_policy_kptr *policy)
 LSM_HOOK(void, LSM_RET_VOID, policy_kptr_put, union lsm_policy_kptr *policy)
+LSM_HOOK(int, -EOPNOTSUPP, bprm_enforce_policy_kptr, struct linux_binprm *bprm,
+	 union lsm_policy_kptr *policy, u32 flags)
 #endif /* CONFIG_BPF_SYSCALL */
 
 LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
include/linux/security.h +12 -0
index 5017a335918c..40dfa96b6a71 100644
@@ -2332,6 +2332,10 @@ extern int security_bpf_token_capable(const struct bpf_token *token, int cap);
 extern int security_policy_kptr_from_fd(u64 lsmid, int fd,
 					union lsm_policy_kptr *policy);
 extern void security_policy_kptr_put(u64 lsmid, union lsm_policy_kptr *policy);
+extern int security_bprm_enforce_policy_kptr(u64 lsmid,
+					     struct linux_binprm *bprm,
+					     union lsm_policy_kptr *policy,
+					     u32 flags);
 #else
 static inline int security_bpf(int cmd, union bpf_attr *attr,
 			       unsigned int size, bool kernel)
@@ -2396,6 +2400,14 @@ static inline void security_policy_kptr_put(u64 lsmid,
 					    union lsm_policy_kptr *policy)
 {
 }
+
+static inline int security_bprm_enforce_policy_kptr(u64 lsmid,
+						    struct linux_binprm *bprm,
+						    union lsm_policy_kptr *policy,
+						    u32 flags)
+{
+	return -EOPNOTSUPP;
+}
 #endif /* CONFIG_SECURITY */
 #endif /* CONFIG_BPF_SYSCALL */
 
kernel/bpf/bpf_lsm.c +1 -0
index e9059d43e92c..d847a180489f 100644
@@ -58,6 +58,7 @@ BTF_ID(func, bpf_lsm_ismaclabel)
 BTF_ID(func, bpf_lsm_file_alloc_security)
 BTF_ID(func, bpf_lsm_policy_kptr_from_fd)
 BTF_ID(func, bpf_lsm_policy_kptr_put)
+BTF_ID(func, bpf_lsm_bprm_enforce_policy_kptr)
 BTF_SET_END(bpf_lsm_disabled_hooks)
 
 /* List of LSM hooks that should operate on 'current' cgroup regardless
security/security.c +41 -0

47 lines not on this page · raw message

Signature · 1 line
2.54.0
#5 lore raw

PATCH 4/13 landlock: Expose the ruleset fd lookup to the rest of Landlock

In reply to Justin Suess

Rename get_ruleset_from_fd() to landlock_get_ruleset_from_fd() and give it external linkage within Landlock, declared in ruleset.h next to the other ruleset lifetime helpers.

A following commit implements the LSM kfunc policy hooks, which need to translate a ruleset fd into a landlock_ruleset reference from outside syscalls.c. No behavioral change.

Cc Mickaël Salaün <[email protected]>
---
 security/landlock/ruleset.h  | 3 +++
 security/landlock/syscalls.c | 9 +++++----
 2 files changed, 8 insertions(+), 4 deletions(-)
security/landlock/ruleset.h +3 -0
index 0437adf17428..b65d1c07e192 100644
@@ -225,6 +225,9 @@ struct landlock_ruleset *
 landlock_merge_ruleset(struct landlock_ruleset *const parent,
 		       struct landlock_ruleset *const ruleset);
 
+struct landlock_ruleset *landlock_get_ruleset_from_fd(const int fd,
+						      const fmode_t mode);
+
 const struct landlock_rule *
 landlock_find_rule(const struct landlock_ruleset *const ruleset,
 		   const struct landlock_id id);
security/landlock/syscalls.c +5 -4
index 36b02892c62f..9af2407274b2 100644
@@ -293,8 +293,8 @@ SYSCALL_DEFINE3(landlock_create_ruleset,
  * Returns an owned ruleset from a FD. It is thus needed to call
  * landlock_put_ruleset() on the return value.
  */
-static struct landlock_ruleset *get_ruleset_from_fd(const int fd,
-						    const fmode_t mode)
+struct landlock_ruleset *landlock_get_ruleset_from_fd(const int fd,
+						      const fmode_t mode)
 {
 	CLASS(fd, ruleset_f)(fd);
 	struct landlock_ruleset *ruleset;
@@ -476,7 +476,7 @@ SYSCALL_DEFINE4(landlock_add_rule, const int, ruleset_fd,
 		return -EINVAL;
 
 	/* Gets and checks the ruleset. */
-	ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
+	ruleset = landlock_get_ruleset_from_fd(ruleset_fd, FMODE_CAN_WRITE);
 	if (IS_ERR(ruleset))
 		return PTR_ERR(ruleset);
 
@@ -564,7 +564,8 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32,
 	      (flags & ~LANDLOCK_RESTRICT_SELF_TSYNC) ==
 		      LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF)) {
 		/* Gets and checks the ruleset. */
-		ruleset = get_ruleset_from_fd(ruleset_fd, FMODE_CAN_READ);
+		ruleset = landlock_get_ruleset_from_fd(ruleset_fd,
+						       FMODE_CAN_READ);
 		if (IS_ERR(ruleset))
 			return PTR_ERR(ruleset);
 	}
Signature · 1 line
2.54.0
#6 lore raw

PATCH 5/13 landlock: Factor the credential restriction out of landlock_restrict_self()

In reply to Justin Suess

Split the core of landlock_restrict_self() into two credential helpers:

landlock_prepare_restriction() - translate the
landlock_restrict_self(2) flags, merge the ruleset with the credentials' domain, and configure the new domain's audit log state, producing a struct landlock_restriction: the complete new state that the enforcement gives to a credential.

landlock_apply_restriction() - enforce a computed restriction on
credentials exclusively owned by the caller. This step cannot fail, so a caller may run it past its last point of failure.

The syscall behaves exactly as before: prepare and apply run back to back on the prepared credentials. The no_new_privs/CAP_SYS_ADMIN precheck, the flag mask check, and the TSYNC handling are syscall policy and stay in place.

The point of the split is that application is decoupled from
computation: a following commit restricts an execution from a BPF kfunc by staging a prepared restriction in the binprm credentials and applying it at the exec point of no return. With the flag translation, domain merge, and audit log configuration in one shared place.

Cc Mickaël Salaün <[email protected]>
---
 security/landlock/cred.c     | 100 +++++++++++++++++++++++++++++++++++
 security/landlock/cred.h     |  33 ++++++++++++
 security/landlock/syscalls.c |  61 +++++----------------
 3 files changed, 147 insertions(+), 47 deletions(-)
security/landlock/cred.c +100 -0
index cc419de75cd6..13b3952c31c5 100644
@@ -8,14 +8,114 @@
  */
 
 #include <linux/binfmts.h>
+#include <linux/bits.h>
 #include <linux/cred.h>
+#include <linux/err.h>
+#include <linux/errno.h>
 #include <linux/lsm_hooks.h>
+#include <uapi/linux/landlock.h>
 
 #include "common.h"
 #include "cred.h"
+#include "domain.h"
 #include "ruleset.h"
 #include "setup.h"
 
+/**
+ * landlock_prepare_restriction - Compute a credential restriction
+ *
+ * @llcred: Landlock credentials to restrict: provides the parent domain and
+ *          the previous log configuration.  Not modified.
+ * @ruleset: Ruleset to enforce, or NULL for a log-configuration-only change.
+ * @flags: landlock_restrict_self(2) flags.  The caller is responsible for
+ *         validating them against the set of flags it supports.
+ * @restriction: Computed restriction.  On success, holds a reference on
+ *               @restriction->domain (if any), which
+ *               landlock_apply_restriction() transfers to the restricted
+ *               credentials.
+ *
+ * The restriction builds on @llcred's current state: the caller must apply
+ * it to (or stage it for) these same credentials.
+ *
+ * Return: 0 on success, -errno on failure.
+ */
+int landlock_prepare_restriction(
+	const struct landlock_cred_security *const llcred,
+	struct landlock_ruleset *const ruleset, const u32 flags,
+	struct landlock_restriction *const restriction)
+{
+#ifdef CONFIG_AUDIT
+	/* Translates "off" and "on" flags to booleans. */
+	const bool log_same_exec =
+		!(flags & LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF);
+	const bool log_new_exec =
+		!!(flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON);
+	const bool log_subdomains =
+		!(flags & LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
+	const bool prev_log_subdomains = !llcred->log_subdomains_off;
+#endif /* CONFIG_AUDIT */
+
+	*restriction = (struct landlock_restriction){};
+
+#ifdef CONFIG_AUDIT
+	restriction->log_subdomains_off = !prev_log_subdomains ||
+					  !log_subdomains;
+#endif /* CONFIG_AUDIT */
+
+	if (!ruleset)
+		return 0;
+
+	restriction->domain = landlock_merge_ruleset(llcred->domain, ruleset);
+	if (IS_ERR(restriction->domain)) {
+		const int err = PTR_ERR(restriction->domain);
+
+		restriction->domain = NULL;
+		return err;
+	}
+
+#ifdef CONFIG_AUDIT
+	restriction->domain->hierarchy->log_same_exec = log_same_exec;
+	restriction->domain->hierarchy->log_new_exec = log_new_exec;
+	if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains)
+		restriction->domain->hierarchy->log_status =
+			LANDLOCK_LOG_DISABLED;
+#endif /* CONFIG_AUDIT */
+
+	return 0;
+}
+
+/**
+ * landlock_apply_restriction - Enforce a computed restriction on credentials
+ *
+ * @llcred: Landlock credentials to restrict, exclusively owned by the caller
+ *          (prepared and not yet committed).
+ * @restriction: Restriction computed by landlock_prepare_restriction()
+ *               against the same credential state; its domain reference is
+ *               transferred to @llcred.
+ *
+ * Cannot fail, so that a caller may apply a restriction past its last point
+ * of failure, e.g. an exec point of no return.
+ */
+void landlock_apply_restriction(struct landlock_cred_security *const llcred,
+				struct landlock_restriction *const restriction)
+{
+#ifdef CONFIG_AUDIT
+	llcred->log_subdomains_off = restriction->log_subdomains_off;
+#endif /* CONFIG_AUDIT */
+
+	if (!restriction->domain)
+		return;
+
+	/* Replaces the old domain. */
+	landlock_put_ruleset(llcred->domain);
+	llcred->domain = restriction->domain;
+	restriction->domain = NULL;
+
+#ifdef CONFIG_AUDIT
+	llcred->domain_exec |= BIT(llcred->domain->num_layers - 1);
+#endif /* CONFIG_AUDIT */
+}
+
 static void hook_cred_transfer(struct cred *const new,
 			       const struct cred *const old)
 {
security/landlock/cred.h +33 -0
index f287c56b5fd4..1d5039b46ce7 100644
@@ -20,6 +20,31 @@
 #include "ruleset.h"
 #include "setup.h"
 
+/**
+ * struct landlock_restriction - Computed credential restriction
+ *
+ * The result of landlock_prepare_restriction(): the new state that
+ * enforcing a ruleset with a set of landlock_restrict_self(2) flags
+ * gives to a credential, decoupled from its application.  It is
+ * enforced with landlock_apply_restriction(), either right away
+ * (landlock_restrict_self(2)) or after a staging period (restriction
+ * of an execution).
+ */
+struct landlock_restriction {
+	/**
+	 * @domain: New domain to enforce, owning a reference.  NULL if the
+	 * restriction only carries a log configuration change.
+	 */
+	struct landlock_ruleset *domain;
+#ifdef CONFIG_AUDIT
+	/**
+	 * @log_subdomains_off: New value of the credentials'
+	 * @landlock_cred_security.log_subdomains_off.
+	 */
+	u8 log_subdomains_off : 1;
+#endif /* CONFIG_AUDIT */
+};
+
 /**
  * struct landlock_cred_security - Credential security blob
  *
@@ -153,6 +178,14 @@ landlock_get_applicable_subject(const struct cred *const cred,
 	return NULL;
 }
 
+int landlock_prepare_restriction(
+	const struct landlock_cred_security *const llcred,
+	struct landlock_ruleset *const ruleset, const u32 flags,
+	struct landlock_restriction *const restriction);
+
+void landlock_apply_restriction(struct landlock_cred_security *const llcred,
+				struct landlock_restriction *const restriction);
+
 __init void landlock_add_cred_hooks(void);
 
 #endif /* _SECURITY_LANDLOCK_CRED_H */
security/landlock/syscalls.c +14 -47

89 lines not on this page · raw message

Signature · 1 line
2.54.0
#8 lore raw

PATCH 6/13 landlock: Implement the LSM policy kptr hooks

In reply to Justin Suess

Implement the generic LSM hooks backing the BPF-owned Landlock kfuncs. The new code is gated on CONFIG_BPF_LSM, the only configuration where the kfuncs calling the hooks exist.

The policy objects travel in the Landlock member of union lsm_policy_kptr as struct bpf_landlock_ruleset handles, the BTF-visible type the verifier tracks; bpf.c is the only place converting between the handle and struct landlock_ruleset.

- policy_kptr_from_fd() translates a ruleset fd, created with landlock_create_ruleset(2) and populated with landlock_add_rule(2), into an owned landlock_ruleset reference. The fd is validated the same way as for the Landlock syscalls (ruleset file type, FMODE_CAN_READ).

- policy_kptr_put() releases such a reference. The free is always deferred as the hook may be reached from BPF object destructors that cannot sleep.

- bprm_enforce_policy_kptr() shares the landlock_restrict_self(2)
path: it calls landlock_prepare_restriction() on the credentials prepared in the binprm and stages the computed restriction in their Landlock blob. Nothing is applied at this point, and there is no flag logic of its own: a new landlock_restrict_self(2) feature implemented in the shared helpers works here as well. The only divergence is the flag mask: LANDLOCK_RESTRICT_SELF_TSYNC is rejected with -EINVAL because the restriction targets the execution, not the calling threads. The restriction is computed in a root memcg charging scope: the domain confines the execution on behalf of the BPF program, so its GFP_KERNEL_ACCOUNT allocations are not charged to the mediated task.

The staged restriction is enforced by a bprm_committing_creds() hook with the same landlock_apply_restriction() call as the syscall, past the exec point of no return: a failed execution can no longer return to the calling program at that point and the application cannot fail, so an execution either starts confined by the domain or leaves the calling task untouched. The applied layer is accounted in
domain_exec: for audit, the confined execution is the one that enforced the domain, so the LOG_SAME_EXEC and LOG_NEW_EXEC flags follow the executed program. There is no no_new_privs/CAP_SYS_ADMIN precondition here: gating who may load a policy-applying BPF program is the BPF attachment's privilege model.

The staged restriction's lifetime is fully covered: a second bprm_enforce_policy_kptr() call on the same execution releases and replaces the previously staged restriction. An execution failing before the point of no return releases it through hook_cred_free() when the prepared credentials are aborted, and the application clears the staging field so committed task credentials never carry a staged restriction. The credential copy helpers (hook_cred_transfer(), landlock_cred_copy()) uphold that invariant by never copying a staged restriction.

Cc Mickaël Salaün <[email protected]>
---
 security/landlock/Makefile  |   2 +
 security/landlock/bpf.c     | 130 ++++++++++++++++++++++++++++++++++++
 security/landlock/bpf.h     |  21 ++++++
 security/landlock/cred.c    |  16 ++++-
 security/landlock/cred.h    |  18 +++++
 security/landlock/limits.h  |   4 ++
 security/landlock/ruleset.c |   2 +-
 security/landlock/setup.c   |   2 +
 8 files changed, 191 insertions(+), 4 deletions(-)
 create mode 100644 security/landlock/bpf.c
 create mode 100644 security/landlock/bpf.h
security/landlock/Makefile +2 -0
index ffa7646d99f3..9ba28c06b5ac 100644
@@ -16,3 +16,5 @@ landlock-$(CONFIG_AUDIT) += \
 	id.o \
 	audit.o \
 	domain.o
+
+landlock-$(CONFIG_BPF_LSM) += bpf.o
security/landlock/bpf.c new file +130 -0

130 lines not on this page · raw message

security/landlock/bpf.h new file +21 -0
new file mode 100644
index 000000000000..e57729e6a564
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Landlock - LSM policy kptr hooks
+ *
+ * Copyright © 2026 Justin Suess <[email protected]>
+ */
+
+#ifndef _SECURITY_LANDLOCK_BPF_H
+#define _SECURITY_LANDLOCK_BPF_H
+
+#include <linux/init.h>
+
+#ifdef CONFIG_BPF_LSM
+__init void landlock_add_bpf_hooks(void);
+#else /* CONFIG_BPF_LSM */
+static inline void landlock_add_bpf_hooks(void)
+{
+}
+#endif /* CONFIG_BPF_LSM */
+
+#endif /* _SECURITY_LANDLOCK_BPF_H */
security/landlock/cred.c +13 -3
index 13b3952c31c5..efbfd0c20475 100644
@@ -124,6 +124,12 @@ static void hook_cred_transfer(struct cred *const new,
 
 	landlock_get_ruleset(old_llcred->domain);
 	*landlock_cred(new) = *old_llcred;
+
+#ifdef CONFIG_BPF_LSM
+	/* Only bprm credentials own a staged restriction: never copied. */
+	WARN_ON_ONCE(landlock_cred(new)->staged.domain);
+	landlock_cred(new)->staged = (struct landlock_restriction){};
+#endif /* CONFIG_BPF_LSM */
 }
 
 static int hook_cred_prepare(struct cred *const new,
@@ -135,10 +141,14 @@ static int hook_cred_prepare(struct cred *const new,
 
 static void hook_cred_free(struct cred *const cred)
 {
-	struct landlock_ruleset *const dom = landlock_cred(cred)->domain;
+	struct landlock_cred_security *const llcred = landlock_cred(cred);
+
+	landlock_put_ruleset_deferred(llcred->domain);
 
-	if (dom)
-		landlock_put_ruleset_deferred(dom);
+#ifdef CONFIG_BPF_LSM
+	/* Releases a restriction staged for an aborted execution. */
+	landlock_put_ruleset_deferred(llcred->staged.domain);
+#endif /* CONFIG_BPF_LSM */
 }
 
 #ifdef CONFIG_AUDIT
security/landlock/cred.h +18 -0
index 1d5039b46ce7..74f8c9808dc4 100644
@@ -60,6 +60,18 @@ struct landlock_cred_security {
 	 */
 	struct landlock_ruleset *domain;
 
+#ifdef CONFIG_BPF_LSM
+	/**
+	 * @staged: Restriction staged by the bprm_enforce_policy_kptr() hook,
+	 * owning its domain reference and applied at the point of no return of
+	 * the execution (bprm_committing_creds).  Only ever set on the
+	 * credentials prepared for an execution, between the staging and
+	 * either the application or the release of the aborted credentials;
+	 * committed task credentials never carry a staged restriction.
+	 */
+	struct landlock_restriction staged;
+#endif /* CONFIG_BPF_LSM */
+
 #ifdef CONFIG_AUDIT
 	/**
 	 * @domain_exec: Bitmask identifying the domain layers that were enforced by
@@ -100,6 +112,12 @@ static inline void landlock_cred_copy(struct landlock_cred_security *dst,
 	*dst = *src;
 
 	landlock_get_ruleset(src->domain);
+
+#ifdef CONFIG_BPF_LSM
+	/* Only bprm credentials own a staged restriction: never copied. */
+	WARN_ON_ONCE(src->staged.domain);
+	dst->staged = (struct landlock_restriction){};
+#endif /* CONFIG_BPF_LSM */
 }
 
 static inline struct landlock_ruleset *landlock_get_current_domain(void)
security/landlock/limits.h +4 -0
index 08d5f2f6d321..0bedfe650ea0 100644
@@ -37,6 +37,10 @@
 #define LANDLOCK_LAST_RESTRICT_SELF	LANDLOCK_RESTRICT_SELF_TSYNC
 #define LANDLOCK_MASK_RESTRICT_SELF	((LANDLOCK_LAST_RESTRICT_SELF << 1) - 1)
 
+/* Subset of the restrict-self flags applicable to an execution. */
+#define LANDLOCK_MASK_RESTRICT_BINPRM \
+	(LANDLOCK_MASK_RESTRICT_SELF & ~LANDLOCK_RESTRICT_SELF_TSYNC)
+
 /* clang-format on */
 
 #endif /* _SECURITY_LANDLOCK_LIMITS_H */
security/landlock/ruleset.c +1 -1
index 4dd09ea22c84..176c626f9f10 100644
@@ -520,7 +520,7 @@ static void free_ruleset_work(struct work_struct *const work)
 	free_ruleset(ruleset);
 }
 
-/* Only called by hook_cred_free(). */
+/* For contexts that cannot sleep, e.g. hook_cred_free(). */
 void landlock_put_ruleset_deferred(struct landlock_ruleset *const ruleset)
 {
 	if (ruleset && refcount_dec_and_test(&ruleset->usage)) {
security/landlock/setup.c +2 -0
index 47dac1736f10..3b7e18edadfb 100644
@@ -11,6 +11,7 @@
 #include <linux/lsm_hooks.h>
 #include <uapi/linux/lsm.h>
 
+#include "bpf.h"
 #include "common.h"
 #include "cred.h"
 #include "errata.h"
@@ -68,6 +69,7 @@ static int __init landlock_init(void)
 	landlock_add_task_hooks();
 	landlock_add_fs_hooks();
 	landlock_add_net_hooks();
+	landlock_add_bpf_hooks();
 	landlock_init_id();
 	landlock_initialized = true;
 	pr_info("Up and running.\n");
Signature · 1 line
2.54.0
#9 lore raw

PATCH 7/13 bpf: Add the LSM policy kfunc infrastructure

In reply to Justin Suess

Prepare kernel/bpf/bpf_lsm.c to host kfuncs that let BPF programs apply a userspace-created Landlock ruleset to an execution. The kfuncs will be thin front ends to the generic LSM policy kptr hooks (security_policy_kptr_from_fd(), security_policy_kptr_put(), security_bprm_enforce_policy_kptr()), invoked with LSM_ID_LANDLOCK so that the LSM framework's targeted dispatch only ever reaches Landlock's hook implementations.

Because of the hook indirection, kernel/bpf/ has no build-time dependency on Landlock: the kfuncs are registered whenever CONFIG_BPF_LSM is enabled, and calling them while Landlock is compiled out or not enabled in the LSM order fails at runtime with -EOPNOTSUPP through the dispatch miss, keeping BPF program loading independent of the boot-time LSM configuration.

Add the section hosting the kfuncs: struct bpf_landlock_ruleset, the opaque BTF-typed handle for a Landlock ruleset that only Landlock resolves; the kfunc id set, registered for both BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL; and the kfunc filter. The two program types share their kfunc lookup buckets with other program types, so restricting the kfuncs to them requires a filter. The set starts empty and the filter has no per-kfunc rules yet; the following patches add the kfuncs together with their filter rules.

---

Notes:
I decided to put the kfunc implementations in kernel/bpf to better delineate the separation between the BPF facing interface and the LSM framework. Since this file contains things like the BPF contexts the kfuncs are allowed to be called from, it's important for BPF to control that aspect.

I'm open to moving it if there is a better preferred location for these under kernel/bpf/ other than kernel/bpf/bpf_lsm.c.

 kernel/bpf/bpf_lsm.c | 50 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)
kernel/bpf/bpf_lsm.c +50 -0
index d847a180489f..dd58c5bd0119 100644
@@ -473,3 +473,53 @@ int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
 	}
 	return 0;
 }
+
+/* LSM policy kfuncs */
+
+/*
+ * Opaque handle for a Landlock ruleset.  Only Landlock resolves it.
+ */
+struct bpf_landlock_ruleset {};
+
+BTF_KFUNCS_START(bpf_landlock_kfunc_ids)
+BTF_KFUNCS_END(bpf_landlock_kfunc_ids)
+
+/*
+ * BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL share their kfunc
+ * lookup buckets with other program types, so restricting the LSM
+ * policy kfuncs requires a filter.
+ */
+static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
+{
+	if (!btf_id_set8_contains(&bpf_landlock_kfunc_ids, kfunc_id))
+		return 0;
+
+	switch (prog->type) {
+	case BPF_PROG_TYPE_SYSCALL:
+		return 0;
+	case BPF_PROG_TYPE_LSM:
+		return 0;
+	default:
+		return -EACCES;
+	}
+}
+
+static const struct btf_kfunc_id_set bpf_landlock_kfunc_set = {
+	.owner = THIS_MODULE,
+	.set = &bpf_landlock_kfunc_ids,
+	.filter = bpf_landlock_kfunc_filter,
+};
+
+static int __init bpf_lsm_policy_kfunc_init(void)
+{
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM,
+					&bpf_landlock_kfunc_set);
+	if (ret)
+		return ret;
+
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+					&bpf_landlock_kfunc_set);
+}
+late_initcall(bpf_lsm_policy_kfunc_init);
Signature · 1 line
2.54.0
#11 lore raw

PATCH 8/13 bpf: Add the bpf_landlock_put_ruleset kfunc and ruleset destructor

In reply to Justin Suess

Add the release kfunc for Landlock ruleset references:

bpf_landlock_put_ruleset(ruleset) KF_RELEASE

It is a thin front end to security_policy_kptr_put(), invoked with LSM_ID_LANDLOCK; the handle travels in the Landlock member of union lsm_policy_kptr, staying typed end to end.

A ruleset reference is meant to be handed over through a map kptr field, so also register a destructor for struct bpf_landlock_ruleset:
map-held references are dropped on map teardown. The release path may thus run from a context that cannot sleep, which the policy_kptr_put() hook contract requires implementations to support.

The release kfunc is available to both program types the kfunc set is registered for. For BPF_PROG_TYPE_LSM, the filter only accepts programs attached to the bprm_creds_for_exec() or bprm_creds_from_file() hooks, where the upcoming enforcement kfunc is specified to operate, and rejects BPF_LSM_CGROUP programs, which run under classic RCU; KF_SLEEPABLE limits the callers to sleepable programs.

---
 kernel/bpf/bpf_lsm.c | 67 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 66 insertions(+), 1 deletion(-)
kernel/bpf/bpf_lsm.c +66 -1
index dd58c5bd0119..877dd0352607 100644
@@ -14,8 +14,10 @@
 #include <net/bpf_sk_storage.h>
 #include <linux/bpf_local_storage.h>
 #include <linux/btf_ids.h>
+#include <linux/cfi.h>
 #include <linux/ima.h>
 #include <linux/bpf-cgroup.h>
+#include <uapi/linux/lsm.h>
 
 /* For every LSM hook that allows attachment of BPF programs, declare a nop
  * function where a BPF program can be attached. Notably, we qualify each with
@@ -481,9 +483,49 @@ int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
  */
 struct bpf_landlock_ruleset {};
 
+/*
+ * The sleepable LSM hooks bpf_landlock_put_ruleset() may be called
+ * from.
+ */
+BTF_SET_START(bpf_landlock_kfunc_hooks)
+BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
+BTF_ID(func, bpf_lsm_bprm_creds_from_file)
+BTF_SET_END(bpf_landlock_kfunc_hooks)
+
+__bpf_kfunc_start_defs();
+
+/**
+ * bpf_landlock_put_ruleset - Put a Landlock ruleset
+ * @ruleset: Landlock ruleset to put
+ *
+ * Release an acquired reference on a Landlock ruleset.
+ */
+__bpf_kfunc void bpf_landlock_put_ruleset(struct bpf_landlock_ruleset *ruleset)
+{
+	union lsm_policy_kptr policy = { .landlock.ruleset = ruleset };
+
+	security_policy_kptr_put(LSM_ID_LANDLOCK, &policy);
+}
+
+/* Destructor for referenced bpf_landlock_ruleset kptrs. */
+__bpf_kfunc void bpf_landlock_put_ruleset_dtor(void *ruleset)
+{
+	union lsm_policy_kptr policy = { .landlock.ruleset = ruleset };
+
+	security_policy_kptr_put(LSM_ID_LANDLOCK, &policy);
+}
+CFI_NOSEAL(bpf_landlock_put_ruleset_dtor);
+
+__bpf_kfunc_end_defs();
+
 BTF_KFUNCS_START(bpf_landlock_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_landlock_put_ruleset, KF_RELEASE | KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_landlock_kfunc_ids)
 
+BTF_ID_LIST(bpf_landlock_dtor_ids)
+BTF_ID(struct, bpf_landlock_ruleset)
+BTF_ID(func, bpf_landlock_put_ruleset_dtor)
+
 /*
  * BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL share their kfunc
  * lookup buckets with other program types, so restricting the LSM
@@ -498,6 +540,17 @@ static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 	case BPF_PROG_TYPE_SYSCALL:
 		return 0;
 	case BPF_PROG_TYPE_LSM:
+		/*
+		 * BPF_LSM_CGROUP programs run under classic RCU and
+		 * cannot sleep.
+		 */
+		if (prog->expected_attach_type == BPF_LSM_CGROUP)
+			return -EACCES;
+
+		if (!btf_id_set_contains(&bpf_landlock_kfunc_hooks,
+					 prog->aux->attach_btf_id))
+			return -EACCES;
+
 		return 0;
 	default:
 		return -EACCES;
@@ -512,6 +565,12 @@ static const struct btf_kfunc_id_set bpf_landlock_kfunc_set = {
 
 static int __init bpf_lsm_policy_kfunc_init(void)
 {
+	const struct btf_id_dtor_kfunc bpf_landlock_dtors[] = {
+		{
+			.btf_id = bpf_landlock_dtor_ids[0],
+			.kfunc_btf_id = bpf_landlock_dtor_ids[1],
+		},
+	};
 	int ret;
 
 	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM,
@@ -519,7 +578,13 @@ static int __init bpf_lsm_policy_kfunc_init(void)
 	if (ret)
 		return ret;
 
-	return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
 					&bpf_landlock_kfunc_set);
+	if (ret)
+		return ret;
+
+	return register_btf_id_dtor_kfuncs(bpf_landlock_dtors,
+					   ARRAY_SIZE(bpf_landlock_dtors),
+					   THIS_MODULE);
 }
 late_initcall(bpf_lsm_policy_kfunc_init);
Signature · 1 line
2.54.0
#13 lore raw

PATCH 9/13 bpf: Add the bpf_landlock_get_ruleset_from_fd kfunc

In reply to Justin Suess

Add the acquire kfunc for Landlock rulesets:

bpf_landlock_get_ruleset_from_fd(fd) KF_ACQUIRE|KF_RET_NULL

It acquires a reference on the Landlock ruleset referred to by @fd, as created by landlock_create_ruleset(2) and populated with landlock_add_rule(2), through security_policy_kptr_from_fd() invoked with LSM_ID_LANDLOCK. When Landlock is compiled out or not enabled in the LSM order, the call returns NULL.

A ruleset fd is only meaningful in the fd table of the process that set the ruleset up, while an LSM program runs in the context of the task it mediates, so the filter makes this kfunc exclusive to syscall programs (BPF_PROG_TYPE_SYSCALL), which run in the context of the task invoking them. The acquired ruleset is meant to be handed over through a map kptr field to an enforcement program, and must be released with bpf_landlock_put_ruleset().

---
 kernel/bpf/bpf_lsm.c | 46 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 43 insertions(+), 3 deletions(-)
kernel/bpf/bpf_lsm.c +43 -3
index 877dd0352607..9ff1c35fcd6e 100644
@@ -479,7 +479,9 @@ int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
 /* LSM policy kfuncs */
 
 /*
- * Opaque handle for a Landlock ruleset.  Only Landlock resolves it.
+ * Opaque handle for a Landlock ruleset.  Only
+ * bpf_landlock_get_ruleset_from_fd() produces one, and only Landlock
+ * resolves it.
  */
 struct bpf_landlock_ruleset {};
 
@@ -494,11 +496,37 @@ BTF_SET_END(bpf_landlock_kfunc_hooks)
 
 __bpf_kfunc_start_defs();
 
+/**
+ * bpf_landlock_get_ruleset_from_fd - Get a Landlock ruleset from a fd
+ * @fd: file descriptor of a Landlock ruleset, resolved in the file
+ *      descriptor table of the task running the program
+ *
+ * Acquire a reference on the Landlock ruleset referred to by @fd, as
+ * created by landlock_create_ruleset(2) and populated with
+ * landlock_add_rule(2).  Only syscall programs may call this kfunc:
+ * they run in the context of the task invoking them, where the
+ * ruleset fd is meaningful.  The acquired ruleset can be handed to an
+ * enforcement program through a map kptr field.  The reference must
+ * be released with bpf_landlock_put_ruleset().
+ *
+ * Return: A referenced ruleset handle, or NULL if @fd is not a
+ * readable Landlock ruleset fd or the Landlock LSM is not enabled.
+ */
+__bpf_kfunc struct bpf_landlock_ruleset *
+bpf_landlock_get_ruleset_from_fd(int fd)
+{
+	union lsm_policy_kptr policy;
+
+	if (security_policy_kptr_from_fd(LSM_ID_LANDLOCK, fd, &policy))
+		return NULL;
+	return policy.landlock.ruleset;
+}
+
 /**
  * bpf_landlock_put_ruleset - Put a Landlock ruleset
  * @ruleset: Landlock ruleset to put
  *
- * Release an acquired reference on a Landlock ruleset.
+ * Release a reference acquired with bpf_landlock_get_ruleset_from_fd().
  */
 __bpf_kfunc void bpf_landlock_put_ruleset(struct bpf_landlock_ruleset *ruleset)
 {
@@ -519,6 +547,8 @@ CFI_NOSEAL(bpf_landlock_put_ruleset_dtor);
 __bpf_kfunc_end_defs();
 
 BTF_KFUNCS_START(bpf_landlock_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_landlock_get_ruleset_from_fd,
+	     KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_landlock_put_ruleset, KF_RELEASE | KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_landlock_kfunc_ids)
 
@@ -526,10 +556,17 @@ BTF_ID_LIST(bpf_landlock_dtor_ids)
 BTF_ID(struct, bpf_landlock_ruleset)
 BTF_ID(func, bpf_landlock_put_ruleset_dtor)
 
+BTF_ID_LIST_SINGLE(bpf_landlock_get_ruleset_ids, func,
+		   bpf_landlock_get_ruleset_from_fd)
+
 /*
  * BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL share their kfunc
  * lookup buckets with other program types, so restricting the LSM
- * policy kfuncs requires a filter.
+ * policy kfuncs requires a filter.  A ruleset fd is only meaningful
+ * in the fd table of the task that set the ruleset up, so
+ * bpf_landlock_get_ruleset_from_fd() is exclusive to syscall
+ * programs, which run in that task's context; an LSM program runs in
+ * the context of the task it mediates.
  */
 static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 {
@@ -540,6 +577,9 @@ static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 	case BPF_PROG_TYPE_SYSCALL:
 		return 0;
 	case BPF_PROG_TYPE_LSM:
+		if (kfunc_id == bpf_landlock_get_ruleset_ids[0])
+			return -EACCES;
+
 		/*
 		 * BPF_LSM_CGROUP programs run under classic RCU and
 		 * cannot sleep.
Signature · 1 line
2.54.0
#14 lore raw

PATCH 10/13 bpf: Add the bpf_landlock_restrict_binprm kfunc

In reply to Justin Suess

Add the enforcement kfunc for Landlock rulesets:

bpf_landlock_restrict_binprm(bprm, ruleset, flags)

It restricts the credentials prepared in @bprm with @ruleset, so that the executed task starts confined by it, through security_bprm_enforce_policy_kptr() invoked with LSM_ID_LANDLOCK. When Landlock is compiled out or not enabled in the LSM order, the call fails with -EOPNOTSUPP.

The kfunc keeps the BPF-facing interface strongly BTF-typed: with kfunc arguments trusted by default, both the binprm (from the LSM hook context) and the ruleset (an acquired reference) must be trusted pointers whose provenance the verifier guarantees, not merely type-matching ones.

The flags take the landlock_restrict_self(2) flags with their usual semantics. Only LANDLOCK_RESTRICT_SELF_TSYNC is rejected (-EINVAL), as it targets the calling threads rather than the execution.

The filter makes enforcement exclusive to the sleepable LSM programs attached to the bprm_creds_for_exec() or bprm_creds_from_file() hooks.

---
 kernel/bpf/bpf_lsm.c | 50 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)
kernel/bpf/bpf_lsm.c +47 -3
index 9ff1c35fcd6e..67aa902f1257 100644
@@ -486,8 +486,8 @@ int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
 struct bpf_landlock_ruleset {};
 
 /*
- * The sleepable LSM hooks bpf_landlock_put_ruleset() may be called
- * from.
+ * The sleepable LSM hooks bpf_landlock_restrict_binprm() and
+ * bpf_landlock_put_ruleset() may be called from.
  */
 BTF_SET_START(bpf_landlock_kfunc_hooks)
 BTF_ID(func, bpf_lsm_bprm_creds_for_exec)
@@ -535,6 +535,42 @@ __bpf_kfunc void bpf_landlock_put_ruleset(struct bpf_landlock_ruleset *ruleset)
 	security_policy_kptr_put(LSM_ID_LANDLOCK, &policy);
 }
 
+/**
+ * bpf_landlock_restrict_binprm - Enforce a Landlock ruleset on exec
+ *                                credentials
+ * @bprm: execution context providing the prepared credentials to
+ *        restrict
+ * @ruleset: Landlock ruleset to enforce
+ * @flags: landlock_restrict_self(2) flags, except
+ *         %LANDLOCK_RESTRICT_SELF_TSYNC
+ *
+ * Restrict the credentials prepared in @bprm with @ruleset, so that
+ * the executed task starts confined by it, following the same domain
+ * composition rules as landlock_restrict_self(2).  The restriction is
+ * staged and only committed at the execution's point of no return: an
+ * execution that fails before that point is unaffected, and a later
+ * call on the same execution replaces a previously staged
+ * restriction.  @ruleset is only borrowed: the caller keeps its
+ * reference.
+ *
+ * The landlock_restrict_self(2) flags apply with their usual
+ * semantics.  Only %LANDLOCK_RESTRICT_SELF_TSYNC is rejected, as it
+ * targets the calling threads rather than the execution.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the Landlock LSM is not
+ * enabled, -EINVAL if @flags contains an unsupported flag, other
+ * negative values on failure as for landlock_restrict_self(2).
+ */
+__bpf_kfunc int bpf_landlock_restrict_binprm(struct linux_binprm *bprm,
+					     struct bpf_landlock_ruleset *ruleset,
+					     u32 flags)
+{
+	union lsm_policy_kptr policy = { .landlock.ruleset = ruleset };
+
+	return security_bprm_enforce_policy_kptr(LSM_ID_LANDLOCK, bprm,
+						 &policy, flags);
+}
+
 /* Destructor for referenced bpf_landlock_ruleset kptrs. */
 __bpf_kfunc void bpf_landlock_put_ruleset_dtor(void *ruleset)
 {
@@ -550,6 +586,7 @@ BTF_KFUNCS_START(bpf_landlock_kfunc_ids)
 BTF_ID_FLAGS(func, bpf_landlock_get_ruleset_from_fd,
 	     KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
 BTF_ID_FLAGS(func, bpf_landlock_put_ruleset, KF_RELEASE | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_landlock_restrict_binprm, KF_SLEEPABLE)
 BTF_KFUNCS_END(bpf_landlock_kfunc_ids)
 
 BTF_ID_LIST(bpf_landlock_dtor_ids)
@@ -558,6 +595,8 @@ BTF_ID(func, bpf_landlock_put_ruleset_dtor)
 
 BTF_ID_LIST_SINGLE(bpf_landlock_get_ruleset_ids, func,
 		   bpf_landlock_get_ruleset_from_fd)
+BTF_ID_LIST_SINGLE(bpf_landlock_restrict_binprm_ids, func,
+		   bpf_landlock_restrict_binprm)
 
 /*
  * BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL share their kfunc
@@ -566,7 +605,10 @@ BTF_ID_LIST_SINGLE(bpf_landlock_get_ruleset_ids, func,
  * in the fd table of the task that set the ruleset up, so
  * bpf_landlock_get_ruleset_from_fd() is exclusive to syscall
  * programs, which run in that task's context; an LSM program runs in
- * the context of the task it mediates.
+ * the context of the task it mediates.  Enforcement is exclusive to
+ * the sleepable bprm LSM hooks the policy operation is specified
+ * for, and the release kfunc is allowed wherever a reference can be
+ * held.
  */
 static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 {
@@ -575,6 +617,8 @@ static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 
 	switch (prog->type) {
 	case BPF_PROG_TYPE_SYSCALL:
+		if (kfunc_id == bpf_landlock_restrict_binprm_ids[0])
+			return -EACCES;
 		return 0;
 	case BPF_PROG_TYPE_LSM:
 		if (kfunc_id == bpf_landlock_get_ruleset_ids[0])
Signature · 1 line
2.54.0
#17 lore raw

PATCH 11/13 selftests/bpf: Add tests for the Landlock policy kfuncs

In reply to Justin Suess

Exercise the LSM policy kfuncs end to end and cover the verifier-time restrictions enforced by the BPF-side kfunc filter.

The success flow mirrors the intended deployment: a syscall program, run by the test runner through BPF_PROG_RUN, acquires the ruleset created by the runner from its fd (resolved in the runner's own fd table) and parks it in a map kptr slot; a program attached to the sleepable bprm_creds_for_exec() hook takes it from the map, enforces it on the monitored execution with bpf_landlock_restrict_binprm() with flags configured per scenario, and puts it back for the next execution. The runner creates a real Landlock ruleset handling LANDLOCK_ACCESS_FS_WRITE_FILE without any rule and checks that:

- a monitored child ends up landlocked (writing to a tmp file fails while a control execution succeeds);
- the audit log flags are accepted;
- LANDLOCK_RESTRICT_SELF_TSYNC is rejected with -EINVAL and leaves the execution unrestricted;
- a second bpf_landlock_restrict_binprm() call on the same execution replaces the previously staged domain instead of failing or stacking;
- a staged restriction is discarded when the execution fails after the bprm hook: the child execs an ENOEXEC file with a restriction staged, and after the failed execve(2) verifies that it is not landlocked;
- a negative fd resolved through the acquire kfunc returns NULL.

The failure programs check that verification rejects:
- a tracing program calling the kfuncs (LSM and syscall programs only),
- an LSM program calling the acquire kfunc (syscall programs only, where the ruleset fd is meaningful),
- a syscall program calling the enforcement kfunc (sleepable bprm LSM hooks only),
- an LSM program on a hook other than the sleepable bprm hooks,
- a non-sleepable LSM program on an allowed hook,
- a program leaking the acquired ruleset reference.

The test needs CONFIG_SECURITY_LANDLOCK and the landlock LSM enabled in the test kernel; the runner skips if the Landlock syscalls are unavailable.

---
 tools/testing/selftests/bpf/config            |   1 +
 tools/testing/selftests/bpf/config.x86_64     |   2 +-
 .../bpf/prog_tests/lsm_policy_kfuncs.c        | 329 ++++++++++++++++++
 .../bpf/progs/lsm_policy_kfuncs_failure.c     | 100 ++++++
 .../bpf/progs/lsm_policy_kfuncs_success.c     | 107 ++++++
 5 files changed, 538 insertions(+), 1 deletion(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_success.c
tools/testing/selftests/bpf/config +1 -0
index ea7044f30adc..2fa734497461 100644
@@ -120,6 +120,7 @@ CONFIG_SAMPLES=y
 CONFIG_SAMPLE_LIVEPATCH=m
 CONFIG_SECURITY=y
 CONFIG_SECURITYFS=y
+CONFIG_SECURITY_LANDLOCK=y
 CONFIG_SYN_COOKIES=y
 CONFIG_TEST_BPF=m
 CONFIG_UDMABUF=y
tools/testing/selftests/bpf/config.x86_64 +1 -1
index 523e0d29bbd4..2c4d857f69f5 100644
@@ -125,7 +125,7 @@ CONFIG_LEGACY_VSYSCALL_NONE=y
 CONFIG_LOG_BUF_SHIFT=21
 CONFIG_LOG_CPU_MAX_BUF_SHIFT=0
 CONFIG_LOGO=y
-CONFIG_LSM="selinux,bpf,integrity"
+CONFIG_LSM="landlock,selinux,bpf,integrity"
 CONFIG_MAC_PARTITION=y
 CONFIG_MAGIC_SYSRQ=y
 CONFIG_MCORE2=y
tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c new file +329 -0

329 lines not on this page · raw message

tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c new file +100 -0

100 lines not on this page · raw message

tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_success.c new file +107 -0

107 lines not on this page · raw message

Signature · 1 line
2.54.0
#18 lore raw

PATCH 12/13 landlock: Document the BPF kfunc interface

In reply to Justin Suess

Describe the new BPF kfuncs for Landlock.

Cc Mickaël Salaün <[email protected]>
---
 Documentation/security/landlock.rst | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
Documentation/security/landlock.rst +25 -0
index c5186526e76f..01197c7580ec 100644
@@ -129,6 +129,31 @@ The reasoning is:
   restrictions, because access within the same scope is already
   allowed based on ``LANDLOCK_ACCESS_FS_RESOLVE_UNIX``.
 
+BPF kfuncs
+==========
+
+BPF programs can apply a userspace-created Landlock ruleset to an
+execution.  A syscall program (``BPF_PROG_TYPE_SYSCALL``), running in
+the context of the process that set the ruleset up, acquires the
+ruleset from its file descriptor and typically hands it over through
+a map kptr field; a sleepable LSM BPF program attached to the
+``bprm_creds_for_exec`` or ``bprm_creds_from_file`` hooks then
+enforces it on an execution.
+
+This can be used to inspect the runtime context of a pending execution,
+and enforce a Landlock policy through BPF.
+
+The restriction is staged in the Landlock blob of the
+credentials prepared for the execution and committed past the exec
+point of no return, so a failed execution leaves the calling task
+untouched.  The ``landlock_restrict_self(2)`` flags apply, with the
+exception of ``LANDLOCK_RESTRICT_SELF_TSYNC``.
+
+.. kernel-doc:: kernel/bpf/bpf_lsm.c
+    :identifiers: bpf_landlock_get_ruleset_from_fd
+                  bpf_landlock_put_ruleset
+                  bpf_landlock_restrict_binprm
+
 Tests
 =====
 
Signature · 1 line
2.54.0
#21 lore raw

PATCH 13/13 lsm: Document the LSM policy kptr hooks

In reply to Justin Suess

Describe the split of responsibilities in lsm-development.rst: an LSM exposes policy operations to any kernel-internal caller through generic LSM hooks. The BPF subsystem owns the strongly typed kfuncs built on top of them, and the providing LSM only implements ordinary LSM hooks.

Cc Paul Moore <[email protected]>
Cc Casey Schaufler <[email protected]>

---

Notes:
This attempts to clarify some of the conclusions in what an LSM can and can't do from Paul and Casey's feedback and make it hard documentation. Let me know if another place is more deserving of it.

 Documentation/security/lsm-development.rst | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)
Documentation/security/lsm-development.rst +27 -0
index 5895e529da7f..fc3af206e795 100644
@@ -15,3 +15,30 @@ see ``security/security.c`` and associated structures:
 
 .. kernel-doc:: security/security.c
    :export:
+
+LSM policy kptr hooks and BPF kfuncs
+====================================
+
+The LSM framework implements an interface for individual LSMs to
+expose their configuration through BPF kfuncs and kptrs. An LSM
+may not export any kfunc or other BPF interface directly.
+
+An LSM wishing to expose a BPF kfunc must reuse an existing security
+hook or implement a new sufficiently generic LSM hook for the desired
+interface. The hooks are then called from kfunc definitions in
+``kernel/bpf``. This allows LSM hooks to remain sufficiently generic
+while allowing BPF programs to take advantage of the strong typing
+and runtime checking offered by the BPF verifier.
+
+The LSM providing an operation implements the operation's hook with
+``LSM_HOOK_INIT()`` like any other hook. A BPF program calling an
+LSM kfunc therefore reaches the LSM the same way every other
+kernel caller does: through an LSM hook. The hooks backing kfuncs
+follow the usual rules for new LSM hooks: their contract must be
+LSM agnostic so that other LSMs could provide a meaningful
+implementation of the same operation.
+
+Whether the LSM providing an operation is built in and active is a
+runtime property: the kfuncs are always registered when
+``CONFIG_BPF_LSM`` is enabled. BPF program loading is thus
+independent of the boot-time LSM configuration.
Signature · 1 line
2.54.0
#3 lore raw

Re: [PATCH bpf-next 02/13] lsm: Add LSM hook security_policy_kptr_put

In reply to Justin Suess
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The WARN_ON_ONCE for a dispatch miss is bypassed if the caller erroneously passes LSM_ID_BPF…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The WARN_ON_ONCE for a dispatch miss is bypassed if the caller erroneously passes LSM_ID_BPF, resulting in a silent leak. --

--- Patch [2]: [PATCH bpf-next 02/13] lsm: Add LSM hook security_policy_kptr_put --- commit 6653ce935258422e0de519b7ec4e4610455b2e51
Author: Justin Suess <[email protected]>

lsm: Add LSM hook security_policy_kptr_put

This commit adds a generic LSM hook for releasing a reference obtained through security_policy_kptr_from_fd(). The hook uses targeted dispatch by lsmid and includes a warning on a dispatch miss to prevent silent leaks.

Quoted 4 lines
security/security.c +0 -0
index 14fd8b878cd07..fd535bd00c244 100644

[ ... ]

Quoted 20 lines
security/security.c +16 -0
@@ -5479,6 +5479,33 @@ int security_policy_kptr_from_fd(u64 lsmid, int fd,
 	return LSM_RET_DEFAULT(policy_kptr_from_fd);
 }
 
+/**
+ * security_policy_kptr_put() - Put a reference on an LSM policy object
+ * @lsmid: LSM_ID_* value of the LSM owning @policy
+ * @policy: the policy object, in the member of the LSM identified by
+ *          @lsmid
+ *
+ * Release a reference previously obtained with
+ * security_policy_kptr_from_fd().  Only the hook implementation
+ * of the LSM identified by @lsmid is called, and @policy must have
+ * been obtained from that same LSM.  An implementation must support
+ * being called from a context that cannot sleep: the release of BPF
+ * managed references may be driven from object destructors.
+ */
+void security_policy_kptr_put(u64 lsmid, union lsm_policy_kptr *policy)
+{
+	struct lsm_static_call *scall;

security/security.c int security_policy_kptr_from_fd(u64 lsmid, int fd, · line 5507

Quoted 10 lines
security/security.c +10 -0
+
+	lsm_for_each_hook(scall, policy_kptr_put) {
+		if (scall->hl->lsmid->id != lsmid)
+			continue;
+		scall->hl->hook.policy_kptr_put(policy);
+		return;
+	}
+	/* A held reference implies the matching LSM implements the hook. */
+	WARN_ON_ONCE(1);
+}

[Severity: Medium]
Will this warning be bypassed if a caller mistakenly passes LSM_ID_BPF?

The commit message states that a dispatch miss means a caller passed the wrong lsmid, and that the shim warns instead of silently leaking the reference.

However, since the BPF LSM automatically registers a void-returning stub for all LSM hooks (bpf_lsm_policy_kptr_put), it appears the dispatch loop will match LSM_ID_BPF, call the empty stub, and return early.

Could this result in a silent leak without triggering the WARN_ON_ONCE(1)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#19 lore raw

Re: [PATCH bpf-next 12/13] landlock: Document the BPF kfunc interface

In reply to Justin Suess
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The kfunc filter excessively restricts `bpf_landlock_put_ruleset`, contradicting the documented…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The kfunc filter excessively restricts bpf_landlock_put_ruleset, contradicting the documented intent and severely limiting ruleset lifecycle management. --

--- Patch [12]: [PATCH bpf-next 12/13] landlock: Document the BPF kfunc interface --- commit f2d332c72e5178d7fb211de4b8685c5f171ac8da
Author: Justin Suess <[email protected]>

landlock: Document the BPF kfunc interface

This commit adds documentation for the new Landlock BPF kfuncs. It describes how a syscall program can acquire a ruleset from a file descriptor and pass it via a map to a sleepable LSM BPF program for enforcement on an execution.

Quoted 22 lines
Documentation/security/landlock.rst +14 -0
index c5186526e76f6..01197c7580ec0 100644
@@ -129,6 +129,31 @@ The reasoning is:
   restrictions, because access within the same scope is already
   allowed based on ``LANDLOCK_ACCESS_FS_RESOLVE_UNIX``.
 
+BPF kfuncs
+==========
+
+BPF programs can apply a userspace-created Landlock ruleset to an
+execution.  A syscall program (``BPF_PROG_TYPE_SYSCALL``), running in
+the context of the process that set the ruleset up, acquires the
+ruleset from its file descriptor and typically hands it over through
+a map kptr field; a sleepable LSM BPF program attached to the
+``bprm_creds_for_exec`` or ``bprm_creds_from_file`` hooks then
+enforces it on an execution.
+
+This can be used to inspect the runtime context of a pending execution,
+and enforce a Landlock policy through BPF.
+

Documentation/security/landlock.rst The reasoning is: · line 155

Quoted 10 lines
Documentation/security/landlock.rst +10 -0
+The restriction is staged in the Landlock blob of the
+credentials prepared for the execution and committed past the exec
+point of no return, so a failed execution leaves the calling task
+untouched.  The ``landlock_restrict_self(2)`` flags apply, with the
+exception of ``LANDLOCK_RESTRICT_SELF_TSYNC``.
+
+.. kernel-doc:: kernel/bpf/bpf_lsm.c
+    :identifiers: bpf_landlock_get_ruleset_from_fd
+                  bpf_landlock_put_ruleset
+                  bpf_landlock_restrict_binprm

[Severity: Medium]
Does the kfunc filter excessively restrict bpf_landlock_put_ruleset, contradicting the documented intent?

Looking at bpf_landlock_kfunc_filter() in kernel/bpf/bpf_lsm.c, it appears to apply -EACCES to bpf_landlock_put_ruleset for most program types and hooks:

kernel/bpf/bpf_lsm.c:bpf_landlock_kfunc_filter() {
	if (!btf_id_set8_contains(&bpf_landlock_kfunc_ids, kfunc_id))
		return 0;

	switch (prog->type) {
// ...
	case BPF_PROG_TYPE_LSM:
// ...
		if (!btf_id_set_contains(&bpf_landlock_kfunc_hooks,
					 prog->aux->attach_btf_id))
			return -EACCES;

		return 0;
	default:
		return -EACCES;
	}
}

This prevents BPF programs of types other than SYSCALL or two specific sleepable LSM hooks from legitimately dropping a reference to a bpf_landlock_ruleset they acquire from a map via bpf_kptr_xchg().

The verifier forces the program to drop the acquired reference, but rejects the call to bpf_landlock_put_ruleset().

Additionally, looking at bpf_landlock_kfunc_ids:

kernel/bpf/bpf_lsm.c:
BTF_ID_FLAGS(func, bpf_landlock_put_ruleset, KF_RELEASE | KF_SLEEPABLE)

The KF_SLEEPABLE flag seems to prevent calling the release kfunc from non-sleepable programs, even though the underlying release mechanism (security_policy_kptr_put) is designed to support non-sleepable contexts.

Could this severely limit ruleset lifecycle management by making the kptr functionally unusable across most BPF program types?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#15 lore raw

Re: [PATCH bpf-next 10/13] bpf: Add the bpf_landlock_restrict_binprm kfunc

In reply to Justin Suess
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Multiple calls to bpf_landlock_restrict_binprm() do not compose; a later call replaces a…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Multiple calls to bpf_landlock_restrict_binprm() do not compose; a later call replaces a previously staged restriction instead of intersecting it, violating BPF LSM composition principles.
- [Medium] bpf_landlock_kfunc_filter incorrectly restricts the release kfunc bpf_landlock_put_ruleset to only the two bprm sleepable hooks, violating the stated design and preventing valid reference cleanup in other LSM hooks. --

--- Patch [10]: [PATCH bpf-next 10/13] bpf: Add the bpf_landlock_restrict_binprm kfunc --- commit fe3f43b90a0bd65c9f7f90c67126bdf361901840
Author: Justin Suess <[email protected]>

bpf: Add the bpf_landlock_restrict_binprm kfunc

This commit adds the bpf_landlock_restrict_binprm kfunc to enforce Landlock rulesets on execution credentials. The restriction is staged and committed at the execution's point of no return. It also limits the enforcement to specific LSM hooks.

Quoted 4 lines
kernel/bpf/bpf_lsm.c +0 -0
index 9ff1c35fcd6e5..67aa902f12570 100644

[ ... ]

Quoted 28 lines
kernel/bpf/bpf_lsm.c +24 -0
@@ -535,6 +535,42 @@ __bpf_kfunc void bpf_landlock_put_ruleset(struct bpf_landlock_ruleset *ruleset)
 	security_policy_kptr_put(LSM_ID_LANDLOCK, &policy);
 }
 
+/**
+ * bpf_landlock_restrict_binprm - Enforce a Landlock ruleset on exec
+ *                                credentials
+ * @bprm: execution context providing the prepared credentials to
+ *        restrict
+ * @ruleset: Landlock ruleset to enforce
+ * @flags: landlock_restrict_self(2) flags, except
+ *         %LANDLOCK_RESTRICT_SELF_TSYNC
+ *
+ * Restrict the credentials prepared in @bprm with @ruleset, so that
+ * the executed task starts confined by it, following the same domain
+ * composition rules as landlock_restrict_self(2).  The restriction is
+ * staged and only committed at the execution's point of no return: an
+ * execution that fails before that point is unaffected, and a later
+ * call on the same execution replaces a previously staged
+ * restriction.  @ruleset is only borrowed: the caller keeps its
+ * reference.
+ *
+ * The landlock_restrict_self(2) flags apply with their usual
+ * semantics.  Only %LANDLOCK_RESTRICT_SELF_TSYNC is rejected, as it
+ * targets the calling threads rather than the execution.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the Landlock LSM is not
+ * enabled, -EINVAL if @flags contains an unsupported flag, other

kernel/bpf/bpf_lsm.c __bpf_kfunc void bpf_landlock_put_ruleset(struct bpf_landlock_ruleset *ruleset) · line 571

Quoted 10 lines
kernel/bpf/bpf_lsm.c +10 -0
+ * negative values on failure as for landlock_restrict_self(2).
+ */
+__bpf_kfunc int bpf_landlock_restrict_binprm(struct linux_binprm *bprm,
+					     struct bpf_landlock_ruleset *ruleset,
+					     u32 flags)
+{
+	union lsm_policy_kptr policy = { .landlock.ruleset = ruleset };
+
+	return security_bprm_enforce_policy_kptr(LSM_ID_LANDLOCK, bprm,
+						 &policy, flags);

[Severity: High]
Does replacing instead of intersecting the ruleset break the BPF LSM composition principles?

If multiple BPF LSM programs are attached to the same hook, a later call to bpf_landlock_restrict_binprm() replaces a previously staged restriction rather than layering or intersecting it.

This allows a subsequent program to completely bypass and discard the security policy enforced by an earlier program.

Quoted 2 lines
kernel/bpf/bpf_lsm.c +2 -0
+}
+

[ ... ]

Quoted 13 lines
kernel/bpf/bpf_lsm.c +4 -1
@@ -566,7 +605,10 @@ BTF_ID_LIST_SINGLE(bpf_landlock_get_ruleset_ids, func,
  * in the fd table of the task that set the ruleset up, so
  * bpf_landlock_get_ruleset_from_fd() is exclusive to syscall
  * programs, which run in that task's context; an LSM program runs in
- * the context of the task it mediates.
+ * the context of the task it mediates.  Enforcement is exclusive to
+ * the sleepable bprm LSM hooks the policy operation is specified
+ * for, and the release kfunc is allowed wherever a reference can be
+ * held.
  */
 static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 {

kernel/bpf/bpf_lsm.c static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id) · line 624

Quoted 8 lines
kernel/bpf/bpf_lsm.c +2 -0
@@ -575,6 +617,8 @@ static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 
 	switch (prog->type) {
 	case BPF_PROG_TYPE_SYSCALL:
+		if (kfunc_id == bpf_landlock_restrict_binprm_ids[0])
+			return -EACCES;
 		return 0;
 	case BPF_PROG_TYPE_LSM:
 		if (kfunc_id == bpf_landlock_get_ruleset_ids[0])

[Severity: Medium]
Does the bpf_landlock_kfunc_filter() logic inadvertently restrict the release kfunc as well?

The updated comment states that the release kfunc (bpf_landlock_put_ruleset) is allowed wherever a reference can be held.

However, in the BPF_PROG_TYPE_LSM case, the filter still applies this check to all kfuncs (including the release kfunc) instead of isolating it to the restriction kfunc:

	if (!btf_id_set_contains(&bpf_landlock_kfunc_hooks,
				 prog->aux->attach_btf_id))
		return -EACCES;

This prevents BPF LSM programs attached to non-bprm hooks from calling bpf_landlock_put_ruleset() to release acquired ruleset references.

Should there be an exemption for the release kfunc here?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#12 lore raw

Re: [PATCH bpf-next 08/13] bpf: Add the bpf_landlock_put_ruleset kfunc and ruleset destructor

In reply to Justin Suess
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] bpf_landlock_put_ruleset is incorrectly restricted to only two LSM hooks, contradicting the…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] bpf_landlock_put_ruleset is incorrectly restricted to only two LSM hooks, contradicting the documented design.
- [Medium] bpf_landlock_put_ruleset is marked KF_SLEEPABLE, which breaks kptr semantics by preventing non-sleepable programs from releasing acquired rulesets. --

--- Patch [8]: [PATCH bpf-next 08/13] bpf: Add the bpf_landlock_put_ruleset kfunc and ruleset destructor --- commit c8093884a93ea01a073e8cc7633e2b790c416510
Author: Justin Suess <[email protected]>

bpf: Add the bpf_landlock_put_ruleset kfunc and ruleset destructor

This commit adds a release kfunc for Landlock ruleset references. It provides a way to drop references when handing them over through a map kptr field, registering a destructor for map teardown.

Quoted 4 lines
kernel/bpf/bpf_lsm.c +0 -0
index dd58c5bd01193..877dd03526077 100644

[ ... ]

Quoted 3 lines

BTF_KFUNCS_START(bpf_landlock_kfunc_ids)
+BTF_ID_FLAGS(func, bpf_landlock_put_ruleset, KF_RELEASE | KF_SLEEPABLE)
BTF_KFUNCS_END(bpf_landlock_kfunc_ids)

[Severity: Medium]
The commit message mentions that "the release path may thus run from a context that cannot sleep". Does adding the KF_SLEEPABLE flag here contradict that requirement?

It appears this flag will cause the verifier to reject non-sleepable LSM programs (like standard RCU-based programs) if they try to release a ruleset reference they just acquired from a map.

[ ... ]

Quoted 6 lines
kernel/bpf/bpf_lsm.c +2 -0
@@ -498,6 +540,17 @@ static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id)
 	case BPF_PROG_TYPE_SYSCALL:
 		return 0;
 	case BPF_PROG_TYPE_LSM:
+		/*
+		 * BPF_LSM_CGROUP programs run under classic RCU and

kernel/bpf/bpf_lsm.c static int bpf_landlock_kfunc_filter(const struct bpf_prog *prog, u32 kfunc_id) · line 554

Quoted 10 lines
kernel/bpf/bpf_lsm.c +9 -0
+		 * cannot sleep.
+		 */
+		if (prog->expected_attach_type == BPF_LSM_CGROUP)
+			return -EACCES;
+
+		if (!btf_id_set_contains(&bpf_landlock_kfunc_hooks,
+					 prog->aux->attach_btf_id))
+			return -EACCES;
+
 		return 0;

[Severity: Medium]
Does this check unintentionally restrict the bpf_landlock_put_ruleset kfunc from being used by other LSM hooks?

Because bpf_landlock_put_ruleset is evaluated by this filter, it seems any LSM program attached to a hook outside of bpf_landlock_kfunc_hooks (bprm_creds_for_exec and bprm_creds_from_file) will fail verification if it tries to release a ruleset. If an LSM program on a different hook retrieves a ruleset via bpf_kptr_xchg, shouldn't it be permitted to release it?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#20 lore raw

Re: [PATCH bpf-next 12/13] landlock: Document the BPF kfunc interface

In reply to [email protected]
Quoted 42 lines · On Fri, Jul 31, 2026 at 02:45:50AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The kfunc filter excessively restricts bpf_landlock_put_ruleset, contradicting the documented intent and severely limiting ruleset lifecycle management.

[...]

[Severity: Medium]
Does the kfunc filter excessively restrict bpf_landlock_put_ruleset, contradicting the documented intent?

Looking at bpf_landlock_kfunc_filter() in kernel/bpf/bpf_lsm.c, it appears to apply -EACCES to bpf_landlock_put_ruleset for most program types and hooks:

kernel/bpf/bpf_lsm.c:bpf_landlock_kfunc_filter() {
	if (!btf_id_set8_contains(&bpf_landlock_kfunc_ids, kfunc_id))
		return 0;

	switch (prog->type) {
// ...
	case BPF_PROG_TYPE_LSM:
// ...
		if (!btf_id_set_contains(&bpf_landlock_kfunc_hooks,
					 prog->aux->attach_btf_id))
			return -EACCES;

		return 0;
	default:
		return -EACCES;
	}
}

This prevents BPF programs of types other than SYSCALL or two specific sleepable LSM hooks from legitimately dropping a reference to a bpf_landlock_ruleset they acquire from a map via bpf_kptr_xchg().

The verifier forces the program to drop the acquired reference, but rejects the call to bpf_landlock_put_ruleset().

Additionally, looking at bpf_landlock_kfunc_ids:

kernel/bpf/bpf_lsm.c:
BTF_ID_FLAGS(func, bpf_landlock_put_ruleset, KF_RELEASE | KF_SLEEPABLE)

Quoted 7 lines

The KF_SLEEPABLE flag seems to prevent calling the release kfunc from non-sleepable programs, even though the underlying release mechanism (security_policy_kptr_put) is designed to support non-sleepable contexts.

Could this severely limit ruleset lifecycle management by making the kptr functionally unusable across most BPF program types?

Sashiko repeated this same feedback a few times.

It seems reasonable.

I don't see why the bpf_landlock_put_ruleset's allowed calling context can't be broadened out.

It's probably ok to allow it for all BPF_PROG_TYPE_LSM programs (including non-sleepable ones) in addition to BPF_PROG_TYPE_SYSCALL, and drop KF_SLEEPABLE in accordance with the security_policy_kptr_put docs stating that the hook consumer cannot sleep.

Same goes for the bpf_landlock_ruleset dtor.

Justin

Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
#16 lore raw

Re: [PATCH bpf-next 10/13] bpf: Add the bpf_landlock_restrict_binprm kfunc

In reply to [email protected]
Quoted 8 lines · On Fri, Jul 31, 2026 at 02:46:37AM +0000, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Multiple calls to bpf_landlock_restrict_binprm() do not compose; a later call replaces a previously staged restriction instead of intersecting it, violating BPF LSM composition principles.
- [Medium] bpf_landlock_kfunc_filter incorrectly restricts the release kfunc bpf_landlock_put_ruleset to only the two bprm sleepable hooks, violating the stated design and preventing valid reference cleanup in other LSM hooks.

[Severity: High]
Does replacing instead of intersecting the ruleset break the BPF LSM composition principles?

Quoted 7 lines

If multiple BPF LSM programs are attached to the same hook, a later call to bpf_landlock_restrict_binprm() replaces a previously staged restriction rather than layering or intersecting it.

This allows a subsequent program to completely bypass and discard the security policy enforced by an earlier program.

It was agreed upon here [1] that it's better to just replace the domain rather than allowing multiple calls to this function to build a multi-layer domain. Merging staged domains creates issues of running into the maximum layers and introduces unecessary complexity.

As for multiple BPF LSM programs trying to overwrite eachother's rulesets:
that's nonesense. There is no security boundary broken here. The ruleset composition should happen in userspace before this kfunc ever gets called. Pathological BPF programs are not part of the threat model.

Justin

[1] https://github.com/landlock-lsm/linux/issues/56#issuecomment-4825919649

Quoted 4 lines
Quoted 2 lines
kernel/bpf/bpf_lsm.c +2 -0
+}
+

[ ... ]

maintainer
#22 lore raw
In reply to Justin Suess
Quoted 25 lines · Justin Suess

Howdy,

This series lets BPF programs apply an existing, userspace-created Landlock ruleset to a program during exec. The goal is unchanged from the RFC [1]: BPF does not create, inspect, or mutate Landlock policy, it only decides whether a ruleset that was already created and validated through Landlock's existing userspace API should be applied, based on runtime exec context.

Motivation
---
Deploying Landlock today requires the sandboxed program's cooperation. A process can only restrict itself, so applying a policy system-wide means wrapping every launch path with a helper that calls landlock_restrict_self(2) before exec, and anything spawned outside those wrappers runs unconfined. Supervising exec from userspace instead (ptrace, seccomp user notifications) is racy and slow. Meanwhile, the tools that do enforce system-wide policy with BPF LSM programs today (container security agents such as KubeArmor and Tetragon) end up reimplementing path-based access control in BPF, fraught with horrors of path reconstruction, bind mounts, rename races, and worse, which is exactly the problem Landlock already solves in the kernel, with maintained and versioned semantics.

Quoted 10 lines

This series composes BPF and Landlock along their natural grain. The intended deployment: a supervisor creates one ruleset per policy class through the existing syscalls, a syscall BPF program parks them in map kptr fields, and an LSM BPF program picks which (if any) to apply to an execution based on its runtime context. The policy semantics stay Landlock's; BPF contributes only the programmable decision of when and to whom. Deciding inside the exec path closes the race a userspace supervisor cannot: the policy is in place before the first instruction of the new program runs.

Discretionary models are fun, and useful in certain situations, but they do have their limitations ;) To be fair, mandatory models have their limitations too :)

I've only just barely skimmed some of the patches in this patchset, and I didn't have a chance to fully read your reply in the previous revision yet, so I wanted to get you a quick response here ... as incomplete as it may be.

As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

I need to look closer at both the LSM hooks and the BPF kfuncs before I can really definitively comment on them. It looks like there has been some progress towards an LSM agnostic interface, but I'm concerned more work might be needed. However, as I said earlier, a closer examination is needed and perhaps that will reveal something different.

It's also worth mentioning that this functionality looks an awful lot like a call to lsm_set_self_attr(LSM_ATTR_EXEC, <lsm_ctx:LSM_ID_LANDLOCK>, size, 0) with a lsm_ctx::ctx set to a policy fd. Once again, perhaps a closer inspection will reveal that it is nothing like that, but it kept popping into my head while reading things so I wanted to mention it, especially as it a lot of the framework infrastructure already exists.

Signature · 1 line
paul-moore.com
#23 lore raw
Quoted 37 lines · Paul Moore
Quoted 25 lines · Justin Suess

Howdy,

This series lets BPF programs apply an existing, userspace-created Landlock ruleset to a program during exec. The goal is unchanged from the RFC [1]: BPF does not create, inspect, or mutate Landlock policy, it only decides whether a ruleset that was already created and validated through Landlock's existing userspace API should be applied, based on runtime exec context.

Motivation
---
Deploying Landlock today requires the sandboxed program's cooperation. A process can only restrict itself, so applying a policy system-wide means wrapping every launch path with a helper that calls landlock_restrict_self(2) before exec, and anything spawned outside those wrappers runs unconfined. Supervising exec from userspace instead (ptrace, seccomp user notifications) is racy and slow. Meanwhile, the tools that do enforce system-wide policy with BPF LSM programs today (container security agents such as KubeArmor and Tetragon) end up reimplementing path-based access control in BPF, fraught with horrors of path reconstruction, bind mounts, rename races, and worse, which is exactly the problem Landlock already solves in the kernel, with maintained and versioned semantics.

Quoted 10 lines

This series composes BPF and Landlock along their natural grain. The intended deployment: a supervisor creates one ruleset per policy class through the existing syscalls, a syscall BPF program parks them in map kptr fields, and an LSM BPF program picks which (if any) to apply to an execution based on its runtime context. The policy semantics stay Landlock's; BPF contributes only the programmable decision of when and to whom. Deciding inside the exec path closes the race a userspace supervisor cannot: the policy is in place before the first instruction of the new program runs.

Quoted 4 lines

Discretionary models are fun, and useful in certain situations, but they do have their limitations ;) To be fair, mandatory models have their limitations too :)

This series does kind of blur the lines between DAC and MAC a little... interesting thought.

Quoted 5 lines

I've only just barely skimmed some of the patches in this patchset, and I didn't have a chance to fully read your reply in the previous revision yet, so I wanted to get you a quick response here ... as incomplete as it may be.

Quoted 7 lines

As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

I'll hold off on further iterations until that's resolved.

Moving these kfuncs anywhere fortunately amounts to a trivial cut/paste on end anyway.

Quoted 7 lines

I need to look closer at both the LSM hooks and the BPF kfuncs before I can really definitively comment on them. It looks like there has been some progress towards an LSM agnostic interface, but I'm concerned more work might be needed. However, as I said earlier, a closer examination is needed and perhaps that will reveal something different.

Quoted 7 lines

It's also worth mentioning that this functionality looks an awful lot like a call to lsm_set_self_attr(LSM_ATTR_EXEC, <lsm_ctx:LSM_ID_LANDLOCK>, size, 0) with a lsm_ctx::ctx set to a policy fd. Once again, perhaps a closer inspection will reveal that it is nothing like that, but it kept popping into my head while reading things so I wanted to mention it, especially as it a lot of the framework infrastructure already exists.

They were the direct inspiration for the hooks (and set/getprocattr).

Main difference being the object in the hook is a trusted kernel pointer rather than a lsm_ctx blob or fd. We can't use an fd here since the sandboxed task and supervisor don't share an fd table.

I'll wait for your full review, no rush.

Justin

Quoted 3 lines
Signature · 2 lines
-- 
paul-moore.com
maintainer
#24 lore raw
Quoted 79 lines · Justin Suess
Quoted 37 lines · Paul Moore
Quoted 25 lines · Justin Suess

Howdy,

This series lets BPF programs apply an existing, userspace-created Landlock ruleset to a program during exec. The goal is unchanged from the RFC [1]: BPF does not create, inspect, or mutate Landlock policy, it only decides whether a ruleset that was already created and validated through Landlock's existing userspace API should be applied, based on runtime exec context.

Motivation
---
Deploying Landlock today requires the sandboxed program's cooperation. A process can only restrict itself, so applying a policy system-wide means wrapping every launch path with a helper that calls landlock_restrict_self(2) before exec, and anything spawned outside those wrappers runs unconfined. Supervising exec from userspace instead (ptrace, seccomp user notifications) is racy and slow. Meanwhile, the tools that do enforce system-wide policy with BPF LSM programs today (container security agents such as KubeArmor and Tetragon) end up reimplementing path-based access control in BPF, fraught with horrors of path reconstruction, bind mounts, rename races, and worse, which is exactly the problem Landlock already solves in the kernel, with maintained and versioned semantics.

Quoted 10 lines

This series composes BPF and Landlock along their natural grain. The intended deployment: a supervisor creates one ruleset per policy class through the existing syscalls, a syscall BPF program parks them in map kptr fields, and an LSM BPF program picks which (if any) to apply to an execution based on its runtime context. The policy semantics stay Landlock's; BPF contributes only the programmable decision of when and to whom. Deciding inside the exec path closes the race a userspace supervisor cannot: the policy is in place before the first instruction of the new program runs.

Quoted 4 lines

Discretionary models are fun, and useful in certain situations, but they do have their limitations ;) To be fair, mandatory models have their limitations too :)

This series does kind of blur the lines between DAC and MAC a little... interesting thought.

Quoted 5 lines

I've only just barely skimmed some of the patches in this patchset, and I didn't have a chance to fully read your reply in the previous revision yet, so I wanted to get you a quick response here ... as incomplete as it may be.

Quoted 7 lines

As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

I'll hold off on further iterations until that's resolved.

Moving these kfuncs anywhere fortunately amounts to a trivial cut/paste on end anyway.

Quoted 7 lines

I need to look closer at both the LSM hooks and the BPF kfuncs before I can really definitively comment on them. It looks like there has been some progress towards an LSM agnostic interface, but I'm concerned more work might be needed. However, as I said earlier, a closer examination is needed and perhaps that will reveal something different.

Quoted 7 lines

It's also worth mentioning that this functionality looks an awful lot like a call to lsm_set_self_attr(LSM_ATTR_EXEC, <lsm_ctx:LSM_ID_LANDLOCK>, size, 0) with a lsm_ctx::ctx set to a policy fd. Once again, perhaps a closer inspection will reveal that it is nothing like that, but it kept popping into my head while reading things so I wanted to mention it, especially as it a lot of the framework infrastructure already exists.

Quoted 7 lines

They were the direct inspiration for the hooks (and set/getprocattr).

Main difference being the object in the hook is a trusted kernel pointer rather than a lsm_ctx blob or fd. We can't use an fd here since the sandboxed task and supervisor don't share an fd table.

I'll wait for your full review, no rush.

I appreciate your patience, thanks.

Signature · 1 line
paul-moore.com
#7 lore raw

Re: [PATCH bpf-next 05/13] landlock: Factor the credential restriction out of landlock_restrict_self()

In reply to Justin Suess
This isn't a bug, but would keeping the merged domain in a local (as the pre-refactor new_dom did) and only storing it into restriction->domain on success read a bit closer to the surrounding…
Quoted 4 lines
security/landlock/cred.c +0 -0
index cc419de75cd6b..13b3952c31c57 100644

[ ... ]

Quoted 16 lines
security/landlock/cred.c +5 -0
@@ -8,14 +8,114 @@
  */
 
 #include <linux/binfmts.h>
+#include <linux/bits.h>
 #include <linux/cred.h>
+#include <linux/err.h>
+#include <linux/errno.h>
 #include <linux/lsm_hooks.h>
+#include <uapi/linux/landlock.h>
 
 #include "common.h"
 #include "cred.h"
+#include "domain.h"
 #include "ruleset.h"
 #include "setup.h"

[ ... ]

Quoted 34 lines
security/landlock/cred.c +34 -0
+int landlock_prepare_restriction(
+	const struct landlock_cred_security *const llcred,
+	struct landlock_ruleset *const ruleset, const u32 flags,
+	struct landlock_restriction *const restriction)
+{
+#ifdef CONFIG_AUDIT
+	/* Translates "off" and "on" flags to booleans. */
+	const bool log_same_exec =
+		!(flags & LANDLOCK_RESTRICT_SELF_LOG_SAME_EXEC_OFF);
+	const bool log_new_exec =
+		!!(flags & LANDLOCK_RESTRICT_SELF_LOG_NEW_EXEC_ON);
+	const bool log_subdomains =
+		!(flags & LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF);
+	const bool prev_log_subdomains = !llcred->log_subdomains_off;
+#endif /* CONFIG_AUDIT */
+
+	*restriction = (struct landlock_restriction){};
+
+#ifdef CONFIG_AUDIT
+	restriction->log_subdomains_off = !prev_log_subdomains ||
+					  !log_subdomains;
+#endif /* CONFIG_AUDIT */
+
+	if (!ruleset)
+		return 0;
+
+	restriction->domain = landlock_merge_ruleset(llcred->domain, ruleset);
+	if (IS_ERR(restriction->domain)) {
+		const int err = PTR_ERR(restriction->domain);
+
+		restriction->domain = NULL;
+		return err;
+	}
+

security/landlock/cred.c line 85

Quoted 10 lines
security/landlock/cred.c +10 -0
+#ifdef CONFIG_AUDIT
+	restriction->domain->hierarchy->log_same_exec = log_same_exec;
+	restriction->domain->hierarchy->log_new_exec = log_new_exec;
+	if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains)
+		restriction->domain->hierarchy->log_status =
+			LANDLOCK_LOG_DISABLED;
+#endif /* CONFIG_AUDIT */
+
+	return 0;
+}

This isn't a bug, but would keeping the merged domain in a local (as the pre-refactor new_dom did) and only storing it into restriction->domain on success read a bit closer to the surrounding landlock code, and let the explicit NULL reset go away?

The merged domain is only ever referred to through restriction->domain:
five uses in the error handling and three restriction->domain->hierarchy->... chains for the audit configuration. The code this was factored out of used a local for exactly this (struct landlock_ruleset *const new_dom = landlock_merge_ruleset(...), then new_dom->hierarchy->log_same_exec = ...), and the neighbouring landlock code consistently uses that shape (e.g. security/landlock/syscalls.c: ruleset = landlock_get_ruleset_from_fd(...); if (IS_ERR(ruleset)) return PTR_ERR(ruleset);).

A local struct landlock_ruleset *domain assigned to restriction->domain on success would also fold the explicit restriction->domain = NULL; reset away, since restriction is already zeroed by the *restriction = (struct landlock_restriction){}; above.

Quoted 22 lines
security/landlock/cred.h +14 -0
index f287c56b5fd4b..1d5039b46ce7a 100644
@@ -20,6 +20,31 @@
 #include "ruleset.h"
 #include "setup.h"
 
+/**
+ * struct landlock_restriction - Computed credential restriction
+ *
+ * The result of landlock_prepare_restriction(): the new state that
+ * enforcing a ruleset with a set of landlock_restrict_self(2) flags
+ * gives to a credential, decoupled from its application.  It is
+ * enforced with landlock_apply_restriction(), either right away
+ * (landlock_restrict_self(2)) or after a staging period (restriction
+ * of an execution).
+ */
+struct landlock_restriction {
+	/**
+	 * @domain: New domain to enforce, owning a reference.  NULL if the
+	 * restriction only carries a log configuration change.

security/landlock/cred.h line 46

Quoted 10 lines
security/landlock/cred.h +10 -0
+	 */
+	struct landlock_ruleset *domain;
+#ifdef CONFIG_AUDIT
+	/**
+	 * @log_subdomains_off: New value of the credentials'
+	 * @landlock_cred_security.log_subdomains_off.
+	 */
+	u8 log_subdomains_off : 1;
+#endif /* CONFIG_AUDIT */
+};

This isn't a bug, but would &landlock_cred_security.log_subdomains_off work better here, so the rendered docs link to the other struct the way ruleset.h does for &landlock_ruleset.root_inode?

The member description cross-references another struct's member with @landlock_cred_security.log_subdomains_off. Per Documentation/doc-guide/kernel-doc.rst, @parameter is for members of the struct being documented (formatting only, no cross-referencing), while a member of a different struct is written &struct_name.member to generate an actual cross-reference.

cred.h is pulled into the rendered docs by Documentation/security/landlock.rst (.. kernel-doc::
security/landlock/cred.h), so the reference renders as plain bold text instead of a link to struct landlock_cred_security. The surrounding subsystem already uses the & form for this, e.g. security/landlock/ruleset.h:71 (Type of &landlock_ruleset.root_inode's node keys).

security/landlock/syscalls.c

Quoted 4 lines
security/landlock/syscalls.c +0 -0
index 9af2407274b25..899601af7c4e1 100644

This isn't a bug, but since this hunk drops the last BIT() and hierarchy/LANDLOCK_LOG_* users in this file, could <linux/bitops.h> and "domain.h" be dropped from syscalls.c too, mirroring the includes added to cred.c?

Moving the domain merge and audit-log configuration into cred.c removes the last users of two includes in syscalls.c, but both includes stay. <linux/bitops.h> (earlier in the file) was needed only for the BIT(new_dom->num_layers - 1) in the deleted domain_exec update, and "domain.h" only for LANDLOCK_LOG_DISABLED and the new_dom->hierarchy->log_* stores.

Grepping the post-patch file for every symbol either header declares (BIT/GENMASK/hweight/fls/test_bit/set_bit/... and LANDLOCK_LOG_*, landlock_hierarchy, landlock_details, landlock_init_hierarchy_log, landlock_get_hierarchy, landlock_put_hierarchy, LANDLOCK_PATH_MAX_SIZE) returns no hits.

cred.c correctly gains <linux/bits.h> and "domain.h" in the same commit, so the dependency moved rather than disappeared. Note <linux/compiler_types.h> is still needed (__always_inline at line 66) even though the __maybe_unused user went away.

[ ... ]

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/30599733854

#10 lore raw

Re: [PATCH bpf-next 07/13] bpf: Add the LSM policy kfunc infrastructure

In reply to Justin Suess
This isn't a bug, but could the last paragraph be trimmed to the shared-bucket rationale? The rest enumerates what the diff already shows, and the same sentence about shared buckets appears as a…
Quoted 15 lines

Prepare kernel/bpf/bpf_lsm.c to host kfuncs that let BPF programs apply a userspace-created Landlock ruleset to an execution. The kfuncs will be thin front ends to the generic LSM policy kptr hooks (security_policy_kptr_from_fd(), security_policy_kptr_put(), security_bprm_enforce_policy_kptr()), invoked with LSM_ID_LANDLOCK so that the LSM framework's targeted dispatch only ever reaches Landlock's hook implementations.

Because of the hook indirection, kernel/bpf/ has no build-time dependency on Landlock: the kfuncs are registered whenever CONFIG_BPF_LSM is enabled, and calling them while Landlock is compiled out or not enabled in the LSM order fails at runtime with -EOPNOTSUPP through the dispatch miss, keeping BPF program loading independent of the boot-time LSM configuration.

Quoted 8 lines

Add the section hosting the kfuncs: struct bpf_landlock_ruleset, the opaque BTF-typed handle for a Landlock ruleset that only Landlock resolves; the kfunc id set, registered for both BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL; and the kfunc filter. The two program types share their kfunc lookup buckets with other program types, so restricting the kfuncs to them requires a filter. The set starts empty and the filter has no per-kfunc rules yet; the following patches add the kfuncs together with their filter rules.

This isn't a bug, but could the last paragraph be trimmed to the shared-bucket rationale? The rest enumerates what the diff already shows, and the same sentence about shared buckets appears as a comment above the filter function.

Quoted 18 lines
kernel/bpf/bpf_lsm.c +10 -0
index d847a180489f9..dd58c5bd01193 100644
@@ -473,3 +473,53 @@ int bpf_lsm_get_retval_range(const struct bpf_prog *prog,
 	}
 	return 0;
 }
+
+/* LSM policy kfuncs */
+
+/*
+ * Opaque handle for a Landlock ruleset.  Only Landlock resolves it.
+ */
+struct bpf_landlock_ruleset {};
+
+BTF_KFUNCS_START(bpf_landlock_kfunc_ids)
+BTF_KFUNCS_END(bpf_landlock_kfunc_ids)

[ ... ]

kernel/bpf/bpf_lsm.c int bpf_lsm_get_retval_range(const struct bpf_prog *prog, · line 525

Quoted 13 lines
kernel/bpf/bpf_lsm.c +13 -0
+static int __init bpf_lsm_policy_kfunc_init(void)
+{
+	int ret;
+
+	ret = register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM,
+					&bpf_landlock_kfunc_set);
+	if (ret)
+		return ret;
+
+	return register_btf_kfunc_id_set(BPF_PROG_TYPE_SYSCALL,
+					&bpf_landlock_kfunc_set);
+}
+late_initcall(bpf_lsm_policy_kfunc_init);

This isn't a bug, but since the empty set makes both register_btf_kfunc_id_set() calls return early in btf_populate_kfunc_set(), this patch has no effect on its own.

Would it read more naturally folded into the patch that adds the first kfunc, so the set, the filter arms, and the registration land together with a user?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/30599733854

#25 lore raw
In reply to Paul Moore
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

One issue with just security/ is it's not CONFIG_SECURITY_BPF. But security/bpf is. Right now security/bpf only has hooks.c so it's free real estate.

That way things are more greppable... (important!) and we can have proper MAINTAINERS entries per file so emails get routed properly.

(linux-security-module, bpf, and whatever lsm list)

Justin

#26 lore raw
Quoted 10 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quoted 3 lines

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

*lsm-specific kfuncs, sorry should have been more clear.

Justin

Quoted 14 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

One issue with just security/ is it's not CONFIG_SECURITY_BPF. But security/bpf is. Right now security/bpf only has hooks.c so it's free real estate.

That way things are more greppable... (important!) and we can have proper MAINTAINERS entries per file so emails get routed properly.

(linux-security-module, bpf, and whatever lsm list)

Justin

maintainer
#27 lore raw
In reply to Justin Suess
Quoted 10 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quoted 6 lines

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

Signature · 1 line
paul-moore.com
#28 lore raw
Quoted 14 lines · Paul Moore
Quoted 13 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

Quoted 8 lines
Quoted 3 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another

I'm 100% on board with the no calling directly into individual LSMs part.

Quoted 2 lines

instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

The intention is the opposite. The point of the separate directory is that the kfuncs can never call into an individual LSM, they only get the LSM framework API in <linux/security.h>.

Every kfunc is a thin wrapper over the generic policy kptr hooks:

    bpf_landlock_get_ruleset_from_fd()
      -> security_policy_kptr_from_fd(LSM_ID_LANDLOCK, ...)
        -> Landlock's hook implementation

So kfunc -> generic lsm hook -> individual LSM, same as any other caller in the kernel. There's no build dependency on Landlock either:
the kfuncs register under CONFIG_BPF_LSM, and if Landlock is compiled out or not in the lsm order, the hook dispatch by lsm id misses and the call returns -EOPNOTSUPP.

The only Landlock-specific part is what the BPF program sees: the kfunc names and the opaque handle (an empty struct bpf_landlock_ruleset).

Permit me to use SELinux-specific interface through LSM as an example.

The existing userspace API already has this exact pattern (partially from [1], thanks Casey it was a great talk!):

  ctx->id      = LSM_ID_SELINUX;
  ctx->flags   = 0;
  ctx->len     = sizeof(struct lsm_ctx) + ctx_len;  

ctx->ctx_len = ctx_len;
memcpy(ctx->ctx, "unconfined_u:unconfined_r:foo_t:s0", ctx_len);

  lsm_set_self_attr(LSM_ATTR_EXEC, ctx, ctx->len, 0);

If you think about it; that's what this patch is doing! "unconfined_u:unconfined_r:foo_t:s0" is as LSM specific as bpf_landlock_ruleset* is.

This is a generic framework syscall, targeted at one LSM by lsm id, carrying an LSM-specific payload. Our kptr is basically the lsm_ctx; the difference is that the lsm id and payload type move out of runtime fields and into the BTF type, so a mismatch fails at program load instead of at runtime. Much better for security! (fail fast and fail hard)

The verifier is why the lsm id can't stay runtime data the way lsm_ctx carries it. Say LSM xyz's policy struct is protected by a mutex (the caller must be able to sleep) while LSM abc's is accessed under RCU. Those rules are enforced at program load time through the KF_* annotations and argument types of the kfunc itself, so a single generic policy kfunc can't carry both. Per-LSM kfuncs above the generic hooks are what let the verifier *prove* each LSM's objects are only accessed in the right context.

Userspace only gets away with the fully generic lsm_ctx because its calling context is always the same: syscall context. BPF programs are everywhere from syscalls to LSM hooks, so the context requirements have to be part of the interface.

So I don't see these kfuncs as a Landlock-specific interface. They're the same generic interface the framework already gives userspace, with the lsm id and access rules promoted into *types* the verifier can check at load time.

Justin

[1] https://static.sched.com/hosted_files/lssna24/1a/2024-04-LSSNA-liblsm.pdf

maintainer
#29 lore raw
Quoted 33 lines · Justin Suess
Quoted 14 lines · Paul Moore
Quoted 13 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

Quoted 8 lines
Quoted 3 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another

I'm 100% on board with the no calling directly into individual LSMs part.

Quoted 2 lines

instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

The intention is the opposite. The point of the separate directory is that the kfuncs can never call into an individual LSM, they only get the LSM framework API in <linux/security.h>.

Quoted 8 lines

Every kfunc is a thin wrapper over the generic policy kptr hooks:

    bpf_landlock_get_ruleset_from_fd()
      -> security_policy_kptr_from_fd(LSM_ID_LANDLOCK, ...)
        -> Landlock's hook implementation

So kfunc -> generic lsm hook -> individual LSM, same as any other caller in the kernel.

Not exactly. That "bpf_*landlock*_XXX" kfuncs are a move away from an LSM agnostic API and not something we currently do in the kernel. Some will, and have, argued that this is more akin to the Landlock syscalls, but I see (at least) two problems with that comparison: the kfuncs being presented aren't syscalls, they are cross-subsystem kernel function calls; the Landlock syscalls were created in a different time, today these would need to be reframed as LSM syscalls*.

(* To be clear, we're not going to remove the Landlock syscalls for all the obvious reasons, but we're also not going to support APIs like that unless we have throughly exhausted all other options.)

Signature · 1 line
paul-moore.com
#30 lore raw
Quoted 43 lines · Paul Moore
Quoted 33 lines · Justin Suess
Quoted 14 lines · Paul Moore
Quoted 13 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

Quoted 8 lines
Quoted 3 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another

I'm 100% on board with the no calling directly into individual LSMs part.

Quoted 2 lines

instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

The intention is the opposite. The point of the separate directory is that the kfuncs can never call into an individual LSM, they only get the LSM framework API in <linux/security.h>.

Quoted 8 lines

Every kfunc is a thin wrapper over the generic policy kptr hooks:

    bpf_landlock_get_ruleset_from_fd()
      -> security_policy_kptr_from_fd(LSM_ID_LANDLOCK, ...)
        -> Landlock's hook implementation

So kfunc -> generic lsm hook -> individual LSM, same as any other caller in the kernel.

Quoted 6 lines

Not exactly. That "bpf_*landlock*_XXX" kfuncs are a move away from an LSM agnostic API and not something we currently do in the kernel. Some will, and have, argued that this is more akin to the Landlock syscalls, but I see (at least) two problems with that comparison: the kfuncs being presented aren't syscalls, they are cross-subsystem kernel function calls; the Landlock syscalls were created in a

I see the argument for normal in-tree kernel interfaces.

Unlike normal kernel interfaces, kfuncs:

1. Can exist without in-tree callers.
2. Are explicitly allowed to change or be removed at any time [1].
3. Can't break builds or other in-tree subsystems when they do.

This isn't hypothetical: the entire KF_KPTR_GET class (bpf_task_kptr_get(), bpf_cgroup_kptr_get(), the flag itself) was removed and replaced with a better abstraction within about a year of introduction.

If Landlock (or any LSM) dies, there's zero uapi/in-tree cost to removing the kfuncs, unlike syscalls which are burned into the uapi forever, or ones with in-tree callers where we can break builds.

I argue that the transient, low-commitment nature of kfuncs mitigates maintainability issues that arise from lsm-specific interfaces with in-tree callers. (which we are both opposed to).

Quoted 3 lines

different time, today these would need to be reframed as LSM syscalls*.

LSM-specific interfaces exist both in the LSM syscalls and in my design.

The only disagreement we have is the abstraction layer that the "LSM-specific" part comes into effect.

1) In the lsm syscalls, it's inside of the lsm_ctx and the LSM_ID.

2) In my design for kfuncs, it's in the function signature and the kptr types.

It's fine to have binary blobs like lsm_ctx where we always assume userspace is untrusted. They can pass garbage through the syscall that we have to handle, which is expected.

BPF and kfuncs are a ring-0, kernel internal interface, trusted after verification. You can't cast pointers, or deserialize them from binary blobs like lsm_ctx in BPF. The ownership and types of pointers must be verified at load time. kfuncs, their annotations, (KF_ACQUIRE/KF_RELEASE) and kptrs are the primary interface by which BPF checks correctness.

An LSM-agnostic bpf_lsm_policy_from_fd(lsm_id, fd) multiplexer would lock every current and future LSM's policy object into a single set of lifetime and context rules, and would move type errors from load time to runtime.

This would prove to be less maintainable, because the main way of ensuring correctness (kfunc signatures + kptr types) would be taken away.

To avoid strawman style arguments, I ask what you would see as an alternative interface?

Justin

[1] https://docs.kernel.org/bpf/kfuncs.html

Quoted 7 lines

(* To be clear, we're not going to remove the Landlock syscalls for all the obvious reasons, but we're also not going to support APIs like that unless we have throughly exhausted all other options.)

Signature · 1 line
paul-moore.com
maintainer
#31 lore raw
Quoted 50 lines · Justin Suess
Quoted 43 lines · Paul Moore
Quoted 33 lines · Justin Suess
Quoted 14 lines · Paul Moore
Quoted 13 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

Quoted 8 lines
Quoted 3 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another

I'm 100% on board with the no calling directly into individual LSMs part.

Quoted 2 lines

instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

The intention is the opposite. The point of the separate directory is that the kfuncs can never call into an individual LSM, they only get the LSM framework API in <linux/security.h>.

Quoted 8 lines

Every kfunc is a thin wrapper over the generic policy kptr hooks:

    bpf_landlock_get_ruleset_from_fd()
      -> security_policy_kptr_from_fd(LSM_ID_LANDLOCK, ...)
        -> Landlock's hook implementation

So kfunc -> generic lsm hook -> individual LSM, same as any other caller in the kernel.

Quoted 6 lines

Not exactly. That "bpf_*landlock*_XXX" kfuncs are a move away from an LSM agnostic API and not something we currently do in the kernel. Some will, and have, argued that this is more akin to the Landlock syscalls, but I see (at least) two problems with that comparison: the kfuncs being presented aren't syscalls, they are cross-subsystem kernel function calls; the Landlock syscalls were created in a

Quoted 5 lines

I see the argument for normal in-tree kernel interfaces.

Unlike normal kernel interfaces, kfuncs:

1. Can exist without in-tree callers.

Yes, although I'm not sure how relevant that is to our discussion. I can say that it isn't relevant to my decisions.

Quoted 1 line

2. Are explicitly allowed to change or be removed at any time [1].

FWIW, the LSM hooks can be changed or removed at any time as well. For obvious reasons we try to avoid churn where possible, but there are plenty of cases where hooks have been modified, removed, relocated, etc. (some without our explicit permission, but that's another issue for another time).

Quoted 1 line

3. Can't break builds or other in-tree subsystems when they do.

Of course. Rule #1 of any kernel subsystem is don't break the build :)

Quoted 5 lines

This isn't hypothetical: the entire KF_KPTR_GET class (bpf_task_kptr_get(), bpf_cgroup_kptr_get(), the flag itself) was removed and replaced with a better abstraction within about a year of introduction.

Quoted 7 lines

If Landlock (or any LSM) dies, there's zero uapi/in-tree cost to removing the kfuncs, unlike syscalls which are burned into the uapi forever, or ones with in-tree callers where we can break builds.

I argue that the transient, low-commitment nature of kfuncs mitigates maintainability issues that arise from lsm-specific interfaces with in-tree callers. (which we are both opposed to).

Sadly, the current situation between the BPF and LSM devs is not good, which means any discussion around LSM kfuncs has a good chance of turning ugly and something that should be relatively easy to maintain is likely to turn into a significant headache. To be clear, this doesn't mean I'm opposed to LSM kfuncs, I just don't agree that they are "low-commitment" at this point in time or in the foreseeable future.

Quoted 2 lines

To avoid strawman style arguments, I ask what you would see as an alternative interface?

As I've mentioned a couple of times now, you need to grant me the time to properly review your existing patches before I can comment in detail on the interface. You've been quick to post with new thoughts, ideas, arguments, etc., which is fine, but replying to them steals my time away from the very patchset you want me to review ;)

It's up to you how you want to handle things, but my suggestion would be to pause some of these thoughts until I've had a chance to review your patchset in detail; then we can have a better discussion.

Signature · 1 line
paul-moore.com
#32 lore raw
Quoted 102 lines · Paul Moore
Quoted 50 lines · Justin Suess
Quoted 43 lines · Paul Moore
Quoted 33 lines · Justin Suess
Quoted 14 lines · Paul Moore
Quoted 13 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

Quoted 8 lines
Quoted 3 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another

I'm 100% on board with the no calling directly into individual LSMs part.

Quoted 2 lines

instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

The intention is the opposite. The point of the separate directory is that the kfuncs can never call into an individual LSM, they only get the LSM framework API in <linux/security.h>.

Quoted 8 lines

Every kfunc is a thin wrapper over the generic policy kptr hooks:

    bpf_landlock_get_ruleset_from_fd()
      -> security_policy_kptr_from_fd(LSM_ID_LANDLOCK, ...)
        -> Landlock's hook implementation

So kfunc -> generic lsm hook -> individual LSM, same as any other caller in the kernel.

Quoted 6 lines

Not exactly. That "bpf_*landlock*_XXX" kfuncs are a move away from an LSM agnostic API and not something we currently do in the kernel. Some will, and have, argued that this is more akin to the Landlock syscalls, but I see (at least) two problems with that comparison: the kfuncs being presented aren't syscalls, they are cross-subsystem kernel function calls; the Landlock syscalls were created in a

Quoted 5 lines

I see the argument for normal in-tree kernel interfaces.

Unlike normal kernel interfaces, kfuncs:

1. Can exist without in-tree callers.

Yes, although I'm not sure how relevant that is to our discussion. I can say that it isn't relevant to my decisions.

Quoted 1 line

2. Are explicitly allowed to change or be removed at any time [1].

FWIW, the LSM hooks can be changed or removed at any time as well. For obvious reasons we try to avoid churn where possible, but there are plenty of cases where hooks have been modified, removed, relocated, etc. (some without our explicit permission, but that's another issue for another time).

Quoted 1 line

3. Can't break builds or other in-tree subsystems when they do.

Of course. Rule #1 of any kernel subsystem is don't break the build :)

Quoted 5 lines

This isn't hypothetical: the entire KF_KPTR_GET class (bpf_task_kptr_get(), bpf_cgroup_kptr_get(), the flag itself) was removed and replaced with a better abstraction within about a year of introduction.

Quoted 7 lines

If Landlock (or any LSM) dies, there's zero uapi/in-tree cost to removing the kfuncs, unlike syscalls which are burned into the uapi forever, or ones with in-tree callers where we can break builds.

I argue that the transient, low-commitment nature of kfuncs mitigates maintainability issues that arise from lsm-specific interfaces with in-tree callers. (which we are both opposed to).

Sadly, the current situation between the BPF and LSM devs is not good, which means any discussion around LSM kfuncs has a good chance of turning ugly and something that should be relatively easy to maintain is likely to turn into a significant headache. To be clear, this doesn't mean I'm opposed to LSM kfuncs, I just don't agree that they are "low-commitment" at this point in time or in the foreseeable future.

Quoted 2 lines

To avoid strawman style arguments, I ask what you would see as an alternative interface?

As I've mentioned a couple of times now, you need to grant me the time to properly review your existing patches before I can comment in detail on the interface. You've been quick to post with new thoughts, ideas, arguments, etc., which is fine, but replying to them steals my time away from the very patchset you want me to review ;)

Quoted 4 lines

It's up to you how you want to handle things, but my suggestion would be to pause some of these thoughts until I've had a chance to review your patchset in detail; then we can have a better discussion.

Apologies! I appreciate the engagement thus far, it's been helpful even if it's not 100% agreement. (wouldn't be interesting if I don't learn anything, or go back to drawing board).

Especially with merge window upcoming I am sure everyone is busy.

Justin

Quoted 2 lines
Signature · 1 line
paul-moore.com
#33 lore raw
In reply to Paul Moore
Quoted 102 lines · Paul Moore
Quoted 50 lines · Justin Suess
Quoted 43 lines · Paul Moore
Quoted 33 lines · Justin Suess
Quoted 14 lines · Paul Moore
Quoted 13 lines · Justin Suess
Quoted 8 lines · Paul Moore

On Thu, Jul 30, 2026 at 10:21 PM Justin Suess <[email protected]> wrote:
[...]
As you may, or may not have seen, there is currently an ongoing debate regarding the location of LSM kfuncs that will impact this patchset. Sadly, we don't appear to be approaching an agreement on this issue which introduces some additional risk to this patchset. We'll have to see how that ends up, but I just wanted you to be aware of the situation.

Quick aside question: Would security/bpf/ be a better place for these type of kfuncs?

Quoted 8 lines
Quoted 3 lines

security/bpf/bpf_lsm_kfuncs.c could be for LSM framework kfuncs, and each LSM could maintain their own security/bpf/<lsm>_kfuncs.c for kfuncs dealing with lsm-specific types.

This gets back to the other issue in the patchset that we've
discussed: general LSM interfaces vs Landlock specific interfaces. There are plenty of reasons why we don't support the kernel calling directly into individual LSMs, and from my perspective this is another

I'm 100% on board with the no calling directly into individual LSMs part.

Quoted 2 lines

instance of that. Here it just happens to be that the kernel caller was written in BPF and not C (or Rust for that matter).

The intention is the opposite. The point of the separate directory is that the kfuncs can never call into an individual LSM, they only get the LSM framework API in <linux/security.h>.

Quoted 8 lines

Every kfunc is a thin wrapper over the generic policy kptr hooks:

    bpf_landlock_get_ruleset_from_fd()
      -> security_policy_kptr_from_fd(LSM_ID_LANDLOCK, ...)
        -> Landlock's hook implementation

So kfunc -> generic lsm hook -> individual LSM, same as any other caller in the kernel.

Quoted 6 lines

Not exactly. That "bpf_*landlock*_XXX" kfuncs are a move away from an LSM agnostic API and not something we currently do in the kernel. Some will, and have, argued that this is more akin to the Landlock syscalls, but I see (at least) two problems with that comparison: the kfuncs being presented aren't syscalls, they are cross-subsystem kernel function calls; the Landlock syscalls were created in a

Quoted 5 lines

I see the argument for normal in-tree kernel interfaces.

Unlike normal kernel interfaces, kfuncs:

1. Can exist without in-tree callers.

Yes, although I'm not sure how relevant that is to our discussion. I can say that it isn't relevant to my decisions.

Quoted 1 line

2. Are explicitly allowed to change or be removed at any time [1].

FWIW, the LSM hooks can be changed or removed at any time as well. For obvious reasons we try to avoid churn where possible, but there are plenty of cases where hooks have been modified, removed, relocated, etc. (some without our explicit permission, but that's another issue for another time).

Quoted 1 line

3. Can't break builds or other in-tree subsystems when they do.

Of course. Rule #1 of any kernel subsystem is don't break the build :)

Quoted 5 lines

This isn't hypothetical: the entire KF_KPTR_GET class (bpf_task_kptr_get(), bpf_cgroup_kptr_get(), the flag itself) was removed and replaced with a better abstraction within about a year of introduction.

Quoted 7 lines

If Landlock (or any LSM) dies, there's zero uapi/in-tree cost to removing the kfuncs, unlike syscalls which are burned into the uapi forever, or ones with in-tree callers where we can break builds.

I argue that the transient, low-commitment nature of kfuncs mitigates maintainability issues that arise from lsm-specific interfaces with in-tree callers. (which we are both opposed to).

Sadly, the current situation between the BPF and LSM devs is not good, which means any discussion around LSM kfuncs has a good chance of turning ugly and something that should be relatively easy to maintain is likely to turn into a significant headache. To be clear, this doesn't mean I'm opposed to LSM kfuncs, I just don't agree that they are "low-commitment" at this point in time or in the foreseeable future.

Quoted 2 lines

To avoid strawman style arguments, I ask what you would see as an alternative interface?

As I've mentioned a couple of times now, you need to grant me the time to properly review your existing patches before I can comment in detail on the interface. You've been quick to post with new thoughts, ideas, arguments, etc., which is fine, but replying to them steals my time away from the very patchset you want me to review ;)

Quoted 4 lines

It's up to you how you want to handle things, but my suggestion would be to pause some of these thoughts until I've had a chance to review your patchset in detail; then we can have a better discussion.

Hi Paul,

Gonna admit I was wrong on this one. It is entirely possible to make a generic kfunc interface for this, without it turning into an ioctl style multiplexer either. Honestly I think it's the better design anyway. Just took a month of staring at the code before the realization hit me.

Since it's been about a month since the original submission, my plan is to send a revised version based on generic kfuncs in security/bpf_lsm_kfuncs.c, which expose no LSM-specific interface:

  bpf_lsm_policy_from_fd(fd, flags)                                   KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE
  bpf_lsm_policy_acquire(struct lsm_policy_object *)                  KF_ACQUIRE | KF_RCU | KF_RET_NULL
  bpf_lsm_policy_release(struct lsm_policy_object *)                  KF_RELEASE
  bpf_lsm_policy_apply_bprm(struct lsm_policy_object *, bprm, flags)  KF_SLEEPABLE

struct lsm_policy_object { /* initialized in LSM object structs */

          u64 lsmid;
          u32 type;

};

I know I said I'd hold off on revisions until your review, so if you're already reviewing the current set (or still plan to), just say so and I'll sit on it. Otherwise I'd rather send the new version than have you waste time on a stale patchset, or on making a point you've already convinced me of.

This generic design is better, and I've already experimented with implementing the same hooks/kfuncs for AppArmor / SELinux exec-time transitions (possible future patchsets?). This would allow writing LSM-agnostic BPF programs (like liblsm).

Thank you so much for your feedback Paul.

Mickäel,

I think this should address your concerns about the multiplexer design, this avoids it handily by making the rulesets/"policy object" self-identifying so there's no need for an opcode/lsmid parameter. There's a small lsm_policy_struct that gets embedded in the ruleset with the lsmid already initialized, and then the original ruleset is retrieved via container_of.

Otherwise, the semantics of the API, the flags, and implementation are identical to this set, with the exception that ruleset references can be taken under RCU.

Thank you for both your time and feedback,
Justin

Quoted 2 lines
Signature · 1 line
paul-moore.com