BPF interface for applying Landlock rulesets
Subsystems (9)
v3 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], v1 [2], and v2 [3]: 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. The policy is in place before the first instruction of the new program runs, closing the race a userspace supervisor cannot.
v3 is v2 rebased onto bpf-next, plus small fixes; the design is unchanged. The Landlock prerequisites (the ruleset/domain split, the tracepoint series, and LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS) went upstream in the 7.3 merge window, so the series now applies directly to bpf-next.
The interface, for reference:
bpf_lsm_policy_from_fd(fd, flags) KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE bpf_lsm_policy_acquire(object) KF_ACQUIRE | KF_RCU | KF_RET_NULL bpf_lsm_policy_release(object) KF_RELEASE
bpf_lsm_policy_apply_bprm(object, bprm, flags) KF_SLEEPABLE
The kfuncs are LSM-generic: they operate on struct lsm_policy_object, which the owning LSM embeds in its own policy structure, and dispatch to that LSM through four ordinary LSM hooks (policy_object_from_fd, policy_object_get, policy_object_put, bprm_apply_policy_object). No kfunc argument names an LSM anywhere in the interface, yet it is not an ioctl-like multiplexer. Landlock is the first provider.
Rather than repeating the whole design here, see the v2 cover letter [3] for the details, as the core design and API are identical to the previous iteration.
Changes since v2
===
- Rebased onto bpf-next; prerequisites are now met.
- The kfunc filter's BPF_LSM_CGROUP case is dropped: since commit 5b038319be44 ("bpf: Reject sleepable BPF_LSM_CGROUP programs at load time") such programs cannot be sleepable, so KF_SLEEPABLE already excludes them from the apply kfunc, making that case redundant.
- The apply_bprm patch now documents why the attach-point filter, not the verifier's argument typing, is the authorization boundary:
trusted linux_binprm pointers are also available at the other bprm hooks and to tp_btf programs via the exec tracepoints, which share the LSM programs' kfunc registration bucket.
- Fixed a pipe fd leak on the fork() error path of test_restrict_binprm_discard() (Sashiko AI review).
Changes since v1 are summarized in the v2 cover letter [3].
The series is structured with LSM framework patches first: patches 1-2 add the hooks, 3 is trivial macro motion, 4-7 the kfuncs, 8 the interface documentation, and 9 its LSM-independent selftests. The Landlock provider follows: patches 10-13 add it, 14 its selftests, and 15 its documentation.
[1] https://lore.kernel.org/linux-security-module/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
[3] https://lore.kernel.org/bpf/[email protected]/
Justin Suess (15):
lsm: Add the LSM policy object lifetime hooks
lsm: Add the bprm_apply_policy_object LSM hook
lsm: Move the lsm_for_each_hook() macro to security/lsm.h
lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
lsm: Add the bpf_lsm_policy_from_fd kfunc
lsm: Add the bpf_lsm_policy_acquire kfunc
lsm: Add the bpf_lsm_policy_apply_bprm kfunc
lsm: Document the LSM policy object interface selftests/bpf: Add tests for the LSM policy object kfuncs
landlock: Expose the ruleset fd lookup to the rest of Landlock
landlock: Factor the credential restriction out of
landlock_restrict_self()
landlock: Free rulesets after an RCU grace period
landlock: Implement the LSM policy object hooks selftests/bpf: Test the LSM policy object kfuncs with Landlock
landlock: Document the BPF policy interface
Documentation/security/landlock.rst | 38 ++ Documentation/security/lsm-development.rst | 49 ++ Documentation/trace/events-landlock.rst | 5 +- MAINTAINERS | 1 + include/linux/lsm_hook_defs.h | 6 + include/linux/security.h | 11 + include/trace/events/landlock.h | 15 +- kernel/bpf/bpf_lsm.c | 4 + kernel/bpf/verifier.c | 3 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 247 ++++++++ security/landlock/Makefile | 2 + security/landlock/bpf.c | 152 +++++ security/landlock/bpf.h | 21 + security/landlock/cred.c | 148 ++++- security/landlock/cred.h | 47 ++ security/landlock/limits.h | 4 + security/landlock/ruleset.c | 30 +- security/landlock/ruleset.h | 75 ++- security/landlock/setup.c | 2 + security/landlock/syscalls.c | 105 +--- security/lsm.h | 6 + security/security.c | 5 - tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/config.x86_64 | 2 +- .../bpf/prog_tests/lsm_policy_kfuncs.c | 54 ++ .../bpf/prog_tests/lsm_policy_landlock.c | 525 ++++++++++++++++++ .../selftests/bpf/progs/lsm_policy_kfuncs.c | 52 ++ .../bpf/progs/lsm_policy_kfuncs_failure.c | 154 +++++ .../selftests/bpf/progs/lsm_policy_landlock.c | 142 +++++ 30 files changed, 1785 insertions(+), 123 deletions(-) create mode 100644 security/bpf_lsm_kfuncs.c 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/prog_tests/lsm_policy_landlock.c create mode 100644 tools/testing/selftests/bpf/progs/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_landlock.c
base-commit: af0b84a9215d951d16f26b7ee34353b970cf5d4e
Signature · 1 line
2.55.0
PATCH v3 1/15 lsm: Add the LSM policy object lifetime hooks
Add struct lsm_policy_object, the identity an LSM embeds in a policy object it shares with BPF programs, and the three hooks managing such an object's lifetime:
policy_object_from_fd(fd, &object)
policy_object_get(object)
policy_object_put(object)
The object records the owning LSM's LSM_ID_* value. The BPF kfuncs built on these hooks dispatch each call on an object to the one LSM matching its lsmid, which resolves the containing object with container_of(); the framework never interprets an object beyond its lsmid. The type field discriminates between the owning LSM's own policy object kinds and is private to it, with 0 reserved as "unset" so a zeroed, untagged object fails every type check.
from_fd has no object to route by: the fd refers to a file set up through the owning LSM's own userspace interface, so the fd itself identifies its LSM. The framework offers the fd to every implementation in turn; an LSM declines a fd that is not one of its policy objects with -EOPNOTSUPP, and any other error is a definitive translation failure.
The hooks back referenced BPF kptrs, which imposes the same lifetime contract on every implementation: from_fd returns a reference on a live object, get acquires with inc-not-zero semantics and fails with -ENOENT once the count dropped to zero, put may be called from contexts that cannot sleep (BPF drives it from map destructors), and the containing object is freed only after an RCU grace period, as programs load policy object kptrs from maps under RCU and may examine an object concurrently with its last put.
The hooks are excluded from the "bpf" LSM's attachment points. The object-routed hooks are unreachable there, as LSM_ID_BPF policy objects cannot exist; for from_fd, whose walk visits every implementation, a BPF program cannot fill the object out parameter, so an attachment returning 0 would hand the caller an uninitialized pointer.
---
Notes:
v2->v3:
- No change.
include/linux/lsm_hook_defs.h | 4 ++++ include/linux/security.h | 11 +++++++++++ kernel/bpf/bpf_lsm.c | 3 +++ 3 files changed, 18 insertions(+)
include/linux/lsm_hook_defs.h +4 -0
@@ -452,6 +452,10 @@ 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_object_from_fd, int fd, + struct lsm_policy_object **object) +LSM_HOOK(int, -EOPNOTSUPP, policy_object_get, struct lsm_policy_object *object) +LSM_HOOK(void, LSM_RET_VOID, policy_object_put, struct lsm_policy_object *object)#endif /* CONFIG_BPF_SYSCALL */ LSM_HOOK(int, 0, locked_down, enum lockdown_reason what)
include/linux/security.h +11 -0
@@ -168,6 +168,17 @@ struct lsm_prop {struct lsm_prop_bpf bpf; }; +/* +* Identity of a policy object an LSM shares with BPF programs, +* embedded in the LSM's own object. @lsmid identifies the owning +* LSM; @type discriminates that LSM's policy object types, with 0 +* reserved as "unset". +*/ +struct lsm_policy_object { +u64 lsmid; +u32 type; +}; +extern const char *const lockdown_reasons[LOCKDOWN_CONFIDENTIALITY_MAX +1 ];/* These functions are in security/commoncap.c */
kernel/bpf/bpf_lsm.c +3 -0
@@ -56,6 +56,9 @@ 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_object_from_fd) +BTF_ID(func, bpf_lsm_policy_object_get) +BTF_ID(func, bpf_lsm_policy_object_put)BTF_SET_END (bpf_lsm_disabled_hooks)/* List of LSM hooks that should operate on 'current' cgroup regardless
Signature · 1 line
2.55.0
PATCH v3 2/15 lsm: Add the bprm_apply_policy_object LSM hook
In reply to Justin SuessAdd the first policy object operation, applying a policy to the credentials prepared for an execution:
bprm_apply_policy_object(bprm, object, flags)
The hook is only called between the preparation and the commitment of the bprm's credentials, i.e. from a bprm_creds_for_exec() or bprm_creds_from_file() context, where the executed task can still be arranged to start confined by the policy.
How the policy composes with restrictions the credentials already carry, and the meaning of @flags, are defined by the implementing LSM, which must reject unsupported flags with -EINVAL. An LSM with no notion of applying a policy object to an execution does not implement the hook, and the calling kfunc fails with -EOPNOTSUPP.
Like the lifetime hooks, this hook is excluded from the "bpf" LSM's attachment points, as the targeted dispatch makes an attachment there unreachable.
---
Notes:
v2->v3:
- No change.
include/linux/lsm_hook_defs.h | 2 ++ kernel/bpf/bpf_lsm.c | 1 + 2 files changed, 3 insertions(+)
include/linux/lsm_hook_defs.h +2 -0
@@ -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, bprm_apply_policy_object, struct linux_binprm *bprm, + struct lsm_policy_object *object, u32 flags) LSM_HOOK(int, -EOPNOTSUPP, policy_object_from_fd, int fd, struct lsm_policy_object **object) LSM_HOOK(int, -EOPNOTSUPP, policy_object_get, struct lsm_policy_object *object)
kernel/bpf/bpf_lsm.c +1 -0
@@ -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_bprm_apply_policy_object) BTF_ID(func, bpf_lsm_policy_object_from_fd) BTF_ID(func, bpf_lsm_policy_object_get)BTF_ID (func, bpf_lsm_policy_object_put)
Signature · 1 line
2.55.0
PATCH v3 3/15 lsm: Move the lsm_for_each_hook() macro to security/lsm.h
In reply to Justin SuessMove the lsm_for_each_hook() iterator from security/security.c to security/lsm.h, verbatim: a following commit adds a user outside security.c, the file implementing the LSM policy object kfuncs. No functional change.
---
Notes:
v2->v3:
- No change.
security/lsm.h | 6 ++++++ security/security.c | 5 ----- 2 files changed, 6 insertions(+), 5 deletions(-)
security/lsm.h +6 -0
@@ -24,6 +24,12 @@ extern bool lsm_debug;extern unsigned int lsm_active_cnt;extern const struct lsm_id *lsm_idlist[]; +/* Iterate over the active implementations of a given hook */ +#define lsm_for_each_hook (scall,NAME ) \ + for (scall = static_calls_table.NAME; \ + scall - static_calls_table.NAME < MAX_LSM_COUNT; scall++) \ + if (static_key_enabled(&scall->active->key)) +/* LSM blob configuration */ extern struct lsm_blob_sizes blob_sizes;
security/security.c +0 -5
@@ -495,11 +495,6 @@ OUT: \RC ; \ }) -#define lsm_for_each_hook (scall,NAME ) \ - for (scall = static_calls_table.NAME; \ - scall - static_calls_table.NAME < MAX_LSM_COUNT; scall++) \ - if (static_key_enabled(&scall->active->key)) -/* Security operations */ /**
Signature · 1 line
2.55.0
PATCH v3 4/15 lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Justin SuessAdd security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object) KF_RELEASE
The kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for structlsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which the policy_object_put() hook contract accounts for. For the same reason the kfunc is not KF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed because BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.
---
Notes:
v2->v3:
- No change.
MAINTAINERS | 1 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 98 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 security/bpf_lsm_kfuncs.c
MAINTAINERS +1 -0
@@ -5081,6 +5081,7 @@ F: kernel/bpf/bpf_lsm.c F: kernel/bpf/bpf_lsm_proto.c F: kernel/trace/bpf_trace.c F: security/bpf/ +F: security/bpf_lsm_kfuncs.c BPF [SELFTESTS] (Test Runners & Infrastructure) M: Andrii Nakryiko <[email protected]>
security/Makefile +1 -1
@@ -23,7 +23,7 @@ obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/ obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/ obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/ obj-$(CONFIG_CGROUPS) += device_cgroup.o -obj-$(CONFIG_BPF_LSM) += bpf/ +obj-$(CONFIG_BPF_LSM) += bpf/ bpf_lsm_kfuncs.o obj-$(CONFIG_SECURITY_LANDLOCK) += landlock/ obj-$(CONFIG_SECURITY_IPE) += ipe/
security/bpf_lsm_kfuncs.c new file +98 -0
98 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Is the justification for the kfunc filter fully accurate?
Quoted 14 lines
Add
security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object)KF_RELEASEThe kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
Quoted 10 lines
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for struct
lsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which thepolicy_object_put()hook contract accounts for. For the same reason the kfunc is notKF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed becauseBPF_PROG_TYPE_LSMandBPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.
Is the justification for the kfunc filter fully accurate?
Looking at bpf_prog_type_to_kfunc_hook() in kernel/bpf/btf.c, bucket selection shows:
case BPF_PROG_TYPE_TRACING:
case BPF_PROG_TYPE_TRACEPOINT:
case BPF_PROG_TYPE_RAW_TRACEPOINT:
case BPF_PROG_TYPE_PERF_EVENT:
case BPF_PROG_TYPE_LSM:
return BTF_KFUNC_HOOK_TRACING;
case BPF_PROG_TYPE_SYSCALL:
return BTF_KFUNC_HOOK_SYSCALL;BPF_PROG_TYPE_LSM does share BTF_KFUNC_HOOK_TRACING with TRACING, TRACEPOINT, RAW_TRACEPOINT and PERF_EVENT program types, so the filter is required for the register_btf_kfunc_id_set(BPF_PROG_TYPE_LSM, ...) call.
However, BPF_PROG_TYPE_SYSCALL is the sole program type mapping to BTF_KFUNC_HOOK_SYSCALL. No other program type can reach that bucket, so the filter's SYSCALL arm simply allows the program type that already owns the bucket.
The code itself is correct and matches the pattern used by bpf_ksock_kfunc_filter in net/core/bpf_ksock.c, which registers for the same two program types.
Should the changelog be narrowed to say the filter is needed for BPF_PROG_TYPE_LSM specifically, since that's the one sharing its bucket with other program types?
---
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/34398515657
Paul Moore
maintainerRe: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Justin SuessQuoted 35 lines · Justin Suess
Add
security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object)KF_RELEASEThe kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for struct
lsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which thepolicy_object_put()hook contract accounts for. For the same reason the kfunc is notKF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed becauseBPF_PROG_TYPE_LSMandBPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- No change.
Quoted 5 lines
MAINTAINERS | 1 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 98 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 security/bpf_lsm_kfuncs.c
While not a new concern, it has been a few months so I want to mention it again to see where things stand with the BPF crowd: are the BPF folks still opposed to BPF kfuncs in security/bpf_lsm_kfuncs.c, similar to fs/bpf_fs_kfuncs.c?
If the answer remains yes, the BPF devs are not able to tolerate kfuncs under security/, then I'm afraid this patchset is stuck. However, if the BPF devs are willing to ACK kfuncs in security/bpf_lsm_kfuncs.c, I'll add this patchset to the review queue for the LSM tree.
MAINTAINERS F: kernel/bpf/bpf_lsm.c · line 5084
Quoted 9 lines
MAINTAINERS +1
-0@@ -5081,6 +5081,7 @@ F: kernel/bpf/bpf_lsm.c F: kernel/bpf/bpf_lsm_proto.c F: kernel/trace/bpf_trace.c F: security/bpf/ +F: security/bpf_lsm_kfuncs.c
We would need the maintenence of security/bpf_lsm_kfuncs.c to fall to the LSM framework, just as the maintenence of bpf_fs_funcs.c falls to the VFS.
% ./scripts/get_maintainer.pl -f fs/bpf_fs_kfuncs.c Alexander Viro
Christian Brauner
Jan Kara
linux-fsdevel@vger
linux-kernel@vger
Signature · 1 line
paul-moore.com
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 47 lines · Paul Moore
Quoted 35 lines · Justin Suess
Add
security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object)KF_RELEASEThe kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for struct
lsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which thepolicy_object_put()hook contract accounts for. For the same reason the kfunc is notKF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed becauseBPF_PROG_TYPE_LSMandBPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- No change.Quoted 5 lines
MAINTAINERS | 1 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 98 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 security/bpf_lsm_kfuncs.cWhile not a new concern, it has been a few months so I want to mention it again to see where things stand with the BPF crowd: are the BPF folks still opposed to BPF kfuncs in
security/bpf_lsm_kfuncs.c, similar tofs/bpf_fs_kfuncs.c?
Quoted 6 lines
If the answer remains yes, the BPF devs are not able to tolerate kfuncs under
security/, then I'm afraid this patchset is stuck. However, if the BPF devs are willing to ACK kfuncs insecurity/bpf_lsm_kfuncs.c, I'll add this patchset to the review queue for the LSM tree.
To avocate for the current placement:
Placing these kfuncs outside where it is in security/ would be very awkward, the kfuncs use lsm internal APIs (lsm_for_each_hook, active static keys, lsm dispatch table) that have no buisness outside the LSM framework.
And logically, the kfuncs operate entirely on in-tree lsm objects.
Quoted 5 lines
Quoted 5 lines
MAINTAINERS +0
-0@@ -5081,6 +5081,7 @@ F: kernel/bpf/bpf_lsm.c
Quoted 9 lines
MAINTAINERS F: kernel/bpf/bpf_lsm.c · line 5084
Quoted 4 lines
MAINTAINERS +1
-0F: kernel/bpf/bpf_lsm_proto.c F: kernel/trace/bpf_trace.c F: security/bpf/ +F: security/bpf_lsm_kfuncs.cWe would need the maintenence of
security/bpf_lsm_kfuncs.cto fall to the LSM framework, just as the maintenence ofbpf_fs_funcs.cfalls to the VFS.
Agreed.
I intend that any security/ kfuncs go through both LSM and BPF review at a minimum. As a bug in any of these kfuncs or their permitted calling context / signatures could cause regressions in either subsystem.
Justin
Quoted 9 lines
%
./scripts/get_maintainer.pl-ffs/bpf_fs_kfuncs.cAlexander Viro
Christian Brauner
Jan Kara
linux-fsdevel@vger
linux-kernel@vgerSignature · 1 line
paul-moore.com
Paul Moore
maintainerRe: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 77 lines · Justin Suess
Quoted 47 lines · Paul Moore
Quoted 35 lines · Justin Suess
Add
security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object)KF_RELEASEThe kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for struct
lsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which thepolicy_object_put()hook contract accounts for. For the same reason the kfunc is notKF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed becauseBPF_PROG_TYPE_LSMandBPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- No change.Quoted 5 lines
MAINTAINERS | 1 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 98 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 security/bpf_lsm_kfuncs.cWhile not a new concern, it has been a few months so I want to mention it again to see where things stand with the BPF crowd: are the BPF folks still opposed to BPF kfuncs in
security/bpf_lsm_kfuncs.c, similar tofs/bpf_fs_kfuncs.c?Quoted 6 lines
If the answer remains yes, the BPF devs are not able to tolerate kfuncs under
security/, then I'm afraid this patchset is stuck. However, if the BPF devs are willing to ACK kfuncs insecurity/bpf_lsm_kfuncs.c, I'll add this patchset to the review queue for the LSM tree.To avocate for the current placement:
Placing these kfuncs outside where it is in
security/would be very awkward, the kfuncs use lsm internal APIs (lsm_for_each_hook, active static keys, lsm dispatch table) that have no buisness outside the LSM framework.And logically, the kfuncs operate entirely on in-tree lsm objects.
Quoted 5 lines
Quoted 5 lines
MAINTAINERS +0
-0@@ -5081,6 +5081,7 @@ F: kernel/bpf/bpf_lsm.cQuoted 8 lines
MAINTAINERS F: kernel/bpf/bpf_lsm.c · line 5084
Quoted 4 lines
MAINTAINERS +1
-0F: kernel/bpf/bpf_lsm_proto.c F: kernel/trace/bpf_trace.c F: security/bpf/ +F: security/bpf_lsm_kfuncs.cWe would need the maintenence of
security/bpf_lsm_kfuncs.cto fall to the LSM framework, just as the maintenence ofbpf_fs_funcs.cfalls to the VFS.
Quoted 5 lines
Agreed.
I intend that any
security/kfuncs go through both LSM and BPF review at a minimum. As a bug in any of these kfuncs or their permitted calling context / signatures could cause regressions in either subsystem.
While some other subsystems are lax on this, it's general practice in the LSM space to *really* try to get ACK's from other affected subsystems before merging. Sometimes we move forward anyway, e.g. patches being ignored for almost a year, but those are exceptions rather than the rule.
I only bring this up because the MAINTAINERS entry was pointed at the BPF devs, not the LSM devs. I would expect that patches going into security/bpf_lsm_kfuncs.c would need to be ACK'd both the LSM framework and BPF LSM maintainers.
Signature · 1 line
paul-moore.com
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 90 lines · Paul Moore
Quoted 77 lines · Justin Suess
Quoted 47 lines · Paul Moore
Quoted 35 lines · Justin Suess
Add
security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object)KF_RELEASEThe kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for struct
lsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which thepolicy_object_put()hook contract accounts for. For the same reason the kfunc is notKF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed becauseBPF_PROG_TYPE_LSMandBPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- No change.Quoted 5 lines
MAINTAINERS | 1 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 98 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 security/bpf_lsm_kfuncs.cWhile not a new concern, it has been a few months so I want to mention it again to see where things stand with the BPF crowd: are the BPF folks still opposed to BPF kfuncs in
security/bpf_lsm_kfuncs.c, similar tofs/bpf_fs_kfuncs.c?Quoted 6 lines
If the answer remains yes, the BPF devs are not able to tolerate kfuncs under
security/, then I'm afraid this patchset is stuck. However, if the BPF devs are willing to ACK kfuncs insecurity/bpf_lsm_kfuncs.c, I'll add this patchset to the review queue for the LSM tree.To avocate for the current placement:
Placing these kfuncs outside where it is in
security/would be very awkward, the kfuncs use lsm internal APIs (lsm_for_each_hook, active static keys, lsm dispatch table) that have no buisness outside the LSM framework.And logically, the kfuncs operate entirely on in-tree lsm objects.
Quoted 5 lines
Quoted 5 lines
MAINTAINERS +0
-0@@ -5081,6 +5081,7 @@ F: kernel/bpf/bpf_lsm.cQuoted 8 lines
MAINTAINERS F: kernel/bpf/bpf_lsm.c · line 5084
Quoted 4 lines
MAINTAINERS +1
-0F: kernel/bpf/bpf_lsm_proto.c F: kernel/trace/bpf_trace.c F: security/bpf/ +F: security/bpf_lsm_kfuncs.cWe would need the maintenence of
security/bpf_lsm_kfuncs.cto fall to the LSM framework, just as the maintenence ofbpf_fs_funcs.cfalls to the VFS.Quoted 5 lines
Agreed.
I intend that any
security/kfuncs go through both LSM and BPF review at a minimum. As a bug in any of these kfuncs or their permitted calling context / signatures could cause regressions in either subsystem.While some other subsystems are lax on this, it's general practice in the LSM space to *really* try to get ACK's from other affected subsystems before merging. Sometimes we move forward anyway, e.g. patches being ignored for almost a year, but those are exceptions rather than the rule.
Quoted 4 lines
I only bring this up because the
MAINTAINERSentry was pointed at the BPF devs, not the LSM devs. I would expect that patches going intosecurity/bpf_lsm_kfuncs.cwould need to be ACK'd both the LSM framework and BPF LSM maintainers.
There is will be no kfuncs under security/.
Period.
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 91 lines · Alexei Starovoitov
Quoted 90 lines · Paul Moore
Quoted 77 lines · Justin Suess
Quoted 47 lines · Paul Moore
Quoted 35 lines · Justin Suess
Add
security/bpf_lsm_kfuncs.c, the home of the kfuncs exposing LSM policy objects to BPF programs, with the first of them:
bpf_lsm_policy_release(object)KF_RELEASEThe kfuncs are the LSM framework's own BPF interface: there is no per-LSM kfunc and no intermediate security_*() layer. Each kfunc walks the matching hook's implementation list and calls the one registered by the LSM whose lsmid the policy object carries. Calling a kfunc for an LSM that is not active or has no policy object support fails at runtime rather than hiding the kfunc at verification time, so BPF program loading is independent of the boot-time LSM configuration.
A policy object reference is meant to be handed over through a map kptr field, so also register a destructor for struct
lsm_policy_object: map-held references are dropped on map teardown, possibly from a context that cannot sleep, which thepolicy_object_put()hook contract accounts for. For the same reason the kfunc is notKF_SLEEPABLE, and the filter adds no per-kfunc rule:
releasing a reference must be allowed wherever one can be held. The filter itself is needed becauseBPF_PROG_TYPE_LSMandBPF_PROG_TYPE_SYSCALL, the two registered program types, share their kfunc lookup buckets with other program types.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- No change.Quoted 5 lines
MAINTAINERS | 1 + security/Makefile | 2 +- security/bpf_lsm_kfuncs.c | 98 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 security/bpf_lsm_kfuncs.cWhile not a new concern, it has been a few months so I want to mention it again to see where things stand with the BPF crowd: are the BPF folks still opposed to BPF kfuncs in
security/bpf_lsm_kfuncs.c, similar tofs/bpf_fs_kfuncs.c?Quoted 6 lines
If the answer remains yes, the BPF devs are not able to tolerate kfuncs under
security/, then I'm afraid this patchset is stuck. However, if the BPF devs are willing to ACK kfuncs insecurity/bpf_lsm_kfuncs.c, I'll add this patchset to the review queue for the LSM tree.To avocate for the current placement:
Placing these kfuncs outside where it is in
security/would be very awkward, the kfuncs use lsm internal APIs (lsm_for_each_hook, active static keys, lsm dispatch table) that have no buisness outside the LSM framework.And logically, the kfuncs operate entirely on in-tree lsm objects.
Quoted 5 lines
Quoted 5 lines
MAINTAINERS +0
-0@@ -5081,6 +5081,7 @@ F: kernel/bpf/bpf_lsm.cQuoted 8 lines
MAINTAINERS F: kernel/bpf/bpf_lsm.c · line 5084
Quoted 4 lines
MAINTAINERS +1
-0F: kernel/bpf/bpf_lsm_proto.c F: kernel/trace/bpf_trace.c F: security/bpf/ +F: security/bpf_lsm_kfuncs.cWe would need the maintenence of
security/bpf_lsm_kfuncs.cto fall to the LSM framework, just as the maintenence ofbpf_fs_funcs.cfalls to the VFS.Quoted 5 lines
Agreed.
I intend that any
security/kfuncs go through both LSM and BPF review at a minimum. As a bug in any of these kfuncs or their permitted calling context / signatures could cause regressions in either subsystem.While some other subsystems are lax on this, it's general practice in the LSM space to *really* try to get ACK's from other affected subsystems before merging. Sometimes we move forward anyway, e.g. patches being ignored for almost a year, but those are exceptions rather than the rule.
Quoted 8 lines
Quoted 4 lines
I only bring this up because the
MAINTAINERSentry was pointed at the BPF devs, not the LSM devs. I would expect that patches going intosecurity/bpf_lsm_kfuncs.cwould need to be ACK'd both the LSM framework and BPF LSM maintainers.There is will be no kfuncs under
security/.
Period.
Howdy Alexei,
If fs/, mm/, drivers/, net/, are permitted to have kfuncs, then why not security/? kfuncs.rst doesn't seem to forbid this.
Placement in security/, on my initial thought, was the most logically relevant place for kfuncs that interact with in-tree LSMs, as we place fs kfuncs under fs/, net kfuncs under net/ etc.
Where would you point me to?
Thanks,
Justin
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.
because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.
kfuncs are not stable. we keep refactoring them.
while lsm folks treat anything within *lsm* and *security* as stable apis. So any kfunc where they have a say, will become a huge burden for further bpf development.
Quoted 1 line
Where would you point me to?
landlock was designed for unprivileged users. bpf needs CAP_BPF. There is a fundamental disconnect. If you can tolerate CAP_BPF then just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.
selinux/landlock shouldn't be calling into bpf
and bpf shouldn't be calling into selinux/landlock internals.
Paul Moore
maintainerRe: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 2 lines
kfuncs are not stable. we keep refactoring them.
while lsm folks treat anything within *lsm* and *security* as stable apis.
Most everyone involved in LSM development knows that statement is simply incorrect. A quick glance at the git log, our process guidelines on modifying LSM hooks (look in the MAINTAINERS file for a pointer), or even just the patch I referenced above should provide quick confirmation.
Signature · 1 line
paul-moore.com
LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 13 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.
Quoted 8 lines
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
security*() calls sprinkled around the kernel are not LSM territory. It's a decision of subsystem maintainers to pass more or less objects into lsm hooks.
VFS folks could have decided to pass a subset of the object to avoid exposing internal details to abusive LSMs or, as in this case, they decided to pass struct mnt_idmap as well to increase visibility into vfs operations.
Paul,
it's not your call to ack or nack their decisions. Your job is to review security/*.c side of changes and give feedback.
The needs of the kernel subsystem are far greater
than all LSM concerns combined.
Daan's patch:
-int security_inode_create(struct inode *dir, struct dentry *dentry, umode_t mode);
+int security_inode_create(struct mnt_idmap *idmap, struct inode *dir,
+ struct dentry *dentry, umode_t mode);
void security_inode_post_create_tmpfile(struct mnt_idmap *idmap,
struct inode *inode);
-int security_inode_link(struct dentry *old_dentry, struct inode *dir,
- struct dentry *new_dentry);
+int security_inode_link(struct mnt_idmap *idmap, struct dentry *old_dentry,
+ struct inode *dir, struct dentry *new_dentry);
broke a bunch of production bpf-lsm programs for many companies. It's annoying, but I cannot complain, since it was VFS folks' decision to change their hooks.
Pls at least cc bpf@vger next time.
If you didn't mention it we would have found out about the breakage only during the next merge window.
Anyway, back to LSM's land grab...
Because of LSM people's behavior, networking folks dont want to add new LSM hooks.
It's really starting to feel that LSM hooks are like tracepoints of the past.
They could be in the way of subsystem development. Hence it's important to stress that it's a subsystem maintainers' decision to shape LSM hooks in their subsystem. Neither LSM people nor BPF cannot nack VFS's LSM hook changes.
Paul Moore
maintainerRe: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 20 lines · Alexei Starovoitov
Quoted 19 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
Quoted 4 lines
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
Signature · 1 line
paul-moore.com
Re: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 25 lines · Paul Moore
Quoted 22 lines · Alexei Starovoitov
Quoted 20 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
Quoted 7 lines
Quoted 2 lines
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
You're still missing the point.
Christian could have landed it via vfs tree with your ack for security/*. You have no power over lsm hook changes within vfs.
Re: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 29 lines · Alexei Starovoitov
Quoted 28 lines · On Sun, Sep 13, 2026 at 5:10???PM Paul Moore <[email protected]> wrote:
Quoted 24 lines · On Sun, Sep 13, 2026 at 7:25???PM Alexei Starovoitov…
Quoted 12 lines · On Sun, Sep 13, 2026 at 12:41???PM Paul Moore <[email protected]> wrote:
Quoted 9 lines · On Sat, Sep 12, 2026 at 3:33???PM Alexei Starovoitov…
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.Quoted 8 lines
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
Quoted 8 lines
Quoted 4 lines
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
You're still missing the point.
Christian could have landed it via vfs tree with your ack forsecurity/*. You have no power over lsm hook changes within vfs.
I've indicated to a number of colleagues that this issue would begin to generate considerable dialogue, once everyone finished with their summer holidays, I see that fact is now eventuating.
There is an important discussion to be had that affects the ability of Linux to serve the security needs of its constituency, which at this point now, is pretty much the planet and beyond. Advancing a relevant discussion requires casting the underlying issue involved in all of this into perhaps the glow of harsh sunlight.
The issue at hand is not who gets to ACK or NACK various security relevant infrastructure, based on who manages source code in a particular directory heirarchy.
The underlying motivation driving all of this is who gets to control and define what can be advertised as 'mainline' Linux security. This has been the bane of Linux security development for a quarter century now, Linus openly acknowledges this fact, ie. security people can never agree on how to do things.
Given the power that this control conveys on certain parties, the objective of maintaining this type of control is certainly understandable.
I have deep respect for Linus, but he has failed to demonstrate appropriate leadership on this front. Indicating that people should simply figure out how to use what is already in Linux, given the current technological tsunami of machine modeling/learning overtaking all of us, is so hopelessly naive that I cannot believe that he actually believes what he is saying.
The current static security architectures in Linux, supported by the LSM model and its development processes, while still technically useful, will not be operationally relevant to the technology industry by the end of the decade.
We need a technical architecture to fix to what is a political problem. That seems consistent with Linux development ostensibly being about technology rather than politics.
The steps in recovery are admitting, and then identifying, that one has a problem, before it can be fixed. Given the agendas involved it seems like a great role for the Linux Foundation to assist with.
We now return you to your regularily scheduled programming of who gets to ACK/NACK kfuncs.
Greg
My opinions and those of my Golden Retriever Hezzie only.
Re: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Alexei StarovoitovQuoted 29 lines · Alexei Starovoitov
Quoted 28 lines · Paul Moore
Quoted 24 lines · Alexei Starovoitov
Quoted 12 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.Quoted 8 lines
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
Quoted 8 lines
Quoted 4 lines
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
You're still missing the point.
Christian could have landed it via vfs tree with your ack forsecurity/*. You have no power over lsm hook changes within vfs.
Which tbf is generally how I like to handle thing especially since we usually have a lot of changes. But I'm open to shared branches as well.
So I think we naturally have to modify existing LSM hooks. But it's shared pain. Since we use BPF LSM heavily in systemd we generally have to absorb such growing pains as well.
Paul Moore
maintainerRe: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 39 lines · Christian Brauner
Quoted 29 lines · Alexei Starovoitov
Quoted 28 lines · Paul Moore
Quoted 24 lines · Alexei Starovoitov
Quoted 12 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.Quoted 8 lines
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
Quoted 8 lines
Quoted 4 lines
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
You're still missing the point.
Christian could have landed it via vfs tree with your ack forsecurity/*. You have no power over lsm hook changes within vfs.
Quoted 6 lines
Which tbf is generally how I like to handle thing especially since we usually have a lot of changes. But I'm open to shared branches as well.
So I think we naturally have to modify existing LSM hooks. But it's shared pain. Since we use BPF LSM heavily in systemd we generally have to absorb such growing pains as well.
I have no problem with topic branches that various trees can pull, but I do have an issue with the VFS tree merging patchsets that haven't been properly ACK'd as well as the VFS tree creating topic branches for patchsets that aren't primarily VFS changes. See below:
Christian, if you want us to move forward with topic branches in the LSM tree for LSM changes that touch fs/ code I'm happy to do that, but I need an acknowledgement from you that the behaviors mentioned above will be cleaned up in the future.
Signature · 1 line
paul-moore.com
Re: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 52 lines · Paul Moore
Quoted 39 lines · Christian Brauner
Quoted 29 lines · Alexei Starovoitov
Quoted 28 lines · Paul Moore
Quoted 24 lines · Alexei Starovoitov
Quoted 12 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.Quoted 8 lines
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
Quoted 8 lines
Quoted 4 lines
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
You're still missing the point.
Christian could have landed it via vfs tree with your ack forsecurity/*. You have no power over lsm hook changes within vfs.Quoted 6 lines
Which tbf is generally how I like to handle thing especially since we usually have a lot of changes. But I'm open to shared branches as well.
So I think we naturally have to modify existing LSM hooks. But it's shared pain. Since we use BPF LSM heavily in systemd we generally have to absorb such growing pains as well.
I have no problem with topic branches that various trees can pull, but I do have an issue with the VFS tree merging patchsets that haven't been properly ACK'd as well as the VFS tree creating topic branches for patchsets that aren't primarily VFS changes. See below:
Quoted 6 lines
Christian, if you want us to move forward with topic branches in the LSM tree for LSM changes that touch
fs/code I'm happy to do that, but I need an acknowledgement from you that the behaviors mentioned above will be cleaned up in the future.
This is another instance where one of your mails around how to organize process ends up reading like you're lecturing your peers. This is not just my impression btw. That impression is shared by other subsystem maintainers - including in your own.
This is a simple case where something got picked up that you didn't want to have picked up yet. Plus, I haven't gotten around to reading this yet. VFS isn't exactly a dormant subsystem.
The easy way is to just say "Hey, I don't think this is ready yet. Could you please drop it. I also would like to quickly sync on how we want to merge this.".
If another subsystems hooks into the vfs layer or filesystems that the vfs trees maintain then the default will always be that the vfs layer provides the shared tree to pull.
Paul Moore
maintainerRe: LSM boundaries. Was: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 60 lines · Christian Brauner
Quoted 52 lines · Paul Moore
Quoted 39 lines · Christian Brauner
Quoted 29 lines · Alexei Starovoitov
Quoted 28 lines · Paul Moore
Quoted 24 lines · Alexei Starovoitov
Quoted 12 lines · Paul Moore
Quoted 9 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.Quoted 8 lines
It's amusing to read this when just this week we merged a patchset into the LSM tree for the benefit of those writing BPF LSMs. If you look at the discussion around patch 2/2 you will even see a reasonable exchange between Matt Bobrowski, a BPF LSM maintainer, and me about the patch. Alexei is obviously welcome to his own opinion, but I would encourage those reading this thread to look beyond his comments.
https://lore.kernel.org/linux-security-module/[email protected]/
And that's an example of unacceptable land grab by LSM folks that I'm concerned about.
Quoted 8 lines
Quoted 4 lines
I made sure that patchset was ACK'd or Reviewed-by'd a VFS maintainer, a BPF LSM maintainer, and the Smack maintainer before merging (I covered the LSM and SELinux parts). You might want to double check your definition of "land grab".
You're still missing the point.
Christian could have landed it via vfs tree with your ack forsecurity/*. You have no power over lsm hook changes within vfs.Quoted 6 lines
Which tbf is generally how I like to handle thing especially since we usually have a lot of changes. But I'm open to shared branches as well.
So I think we naturally have to modify existing LSM hooks. But it's shared pain. Since we use BPF LSM heavily in systemd we generally have to absorb such growing pains as well.
I have no problem with topic branches that various trees can pull, but I do have an issue with the VFS tree merging patchsets that haven't been properly ACK'd as well as the VFS tree creating topic branches for patchsets that aren't primarily VFS changes. See below:
Quoted 6 lines
Christian, if you want us to move forward with topic branches in the LSM tree for LSM changes that touch
fs/code I'm happy to do that, but I need an acknowledgement from you that the behaviors mentioned above will be cleaned up in the future.
Quoted 4 lines
This is another instance where one of your mails around how to organize process ends up reading like you're lecturing your peers. This is not just my impression btw. That impression is shared by other subsystem maintainers - including in your own.
If my comments came across as a lecture, you are likely hearing the frustration of having to revisit a topic where my past requests for some mutual understanding on process have been ignored or silently discarded. If you have been offended by the tone of my comments, please understand that I have been offended by your actions.
Quoted 3 lines
If another subsystems hooks into the vfs layer or filesystems that the vfs trees maintain then the default will always be that the vfs layer provides the shared tree to pull.
Regardless of how we got here, I think we can agree that we both want to sort this out and find a way to work better moving forward, yes?
My request is the same as it ever was: please don't merge code for one of the subsystems I am responsible for maintaining without an explicit ACK or reviewed-by tag on the patch{set} you are merging. As for who merges what, and where, considering past history, I think it's probably going to be difficult to find a single generic rule that will make both of us happy. Perhaps we can simply agree to have a discussion about where the patches are merged when we are ACK'ing/Review'ing them? In other words, there would be no default merging tree/branch, the default would be for us to discuss how best to get the changes in Linus' tree on a per-patchset basis.
I'm sure you have your own thoughts, but I figured it would be helpful to get something out here as a start.
Signature · 1 line
paul-moore.com
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Alexei StarovoitovQuoted 5 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.
Quoted 9 lines
because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.
kfuncs are not stable. we keep refactoring them.
while lsm folks treat anything within *lsm* and *security* as stable apis. So any kfunc where they have a say, will become a huge burden for further bpf development.
There are patches on the front page of lore.kernel.org/linux-security-module refactoring security hooks and changing signatures. Security hooks and the LSM interface is not stable, this is explicitly documented.
I deliberately chose kfuncs over helpers to avoid having the interface be ossified.
Quoted 4 lines
Quoted 1 line
Where would you point me to?
landlock was designed for unprivileged users. bpf needs
CAP_BPF. There is a fundamental disconnect. If you can tolerateCAP_BPFthen
Landlock is for privileged and unprivileged users alike. This patchset is designed to compose with unprivileged use seamlessly, which works because of Landlock's monotonicity.
Quoted 2 lines
just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.
Sure, in some perfect world in the future where every verifier challenge is solved and BPF has feature parity with in-tree c on a 1:1 basis, you could implement something like SELinux, or Landlock in pure eBPF. It would be about as easy and trivial as building a skyscraper from lego bricks.
There is an entire in-tree ecosystem of LSMs that would provide BPF users with well-maintained, versioned, and tested access control. Why force every eBPF program that needs to make security decisions to reeinvent the wheel?
There are real eBPF users such as Tetragon, Falco, Tracee, that could use an actively maintained implementation of security models that exist in-tree in security/. This would make access control in eBPF much less painful.
Instead of implementing every filesystem, network, and socket access control, they can use existing solutions, and with the userspace tooling that already exists. And they can mix-and-match, doing custom logic/policy in BPF, and use what they need in LSM, with BPF making policy decisions.
It would fix a real challenge that users have with eBPF, and wouldn't take away from BPF's ability to do what it already does.
Quoted 3 lines
and bpf shouldn't be calling into selinux/landlock internals.
Agreed. And this patchset doesn't call into landlock, it calls a security hook. The LSM hook then dispatches to whatever LSM owns the object.
BPF already calls into LSM through security hooks. This is no different than bpf_map_create hooks.
Nothing about the way BPF works changes with this patchset. There's no verifier internal changes. It doesn't call into BPF internals at all, or define any new core constructs. It just uses the same kfunc and kptr interface that is available to everyone else in the kernel. I think this code could even be written with zero changes in kernel/bpf, or files in BPF's maintainer's entry, though I'd rather have both subsystems coordinating and cooperating on the interface.
Thanks for your feedback (genuinely), I hope you know I make these arguments in good faith even when things seem heated.
Thanks,
Justin
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 5 lines · Justin Suess
Sure, in some perfect world in the future where every verifier challenge is solved and BPF has feature parity with in-tree c on a 1:1 basis, you could implement something like SELinux, or Landlock in pure eBPF.
Try.. give it a shot? What is missing in the verifier?
Quoted 2 lines
Why force every eBPF program that needs to make security decisions to reeinvent the wheel?
What specific reinvention are you talking about?
Quoted 2 lines
BPF already calls into LSM through security hooks. This is no different than
bpf_map_createhooks.
what? It doesn't. bpf progs avoid lsm hooks as a plague. Not a single kfuncs calls into lsm directly.
It may call into security_*() by accident because it calls some kernel mechanisms.
Quoted 2 lines
Nothing about the way BPF works changes with this patchset. There's no verifier internal changes.
If the verifier is in the way of what you want to accomplish then please improve it.
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 9 lines · Alexei Starovoitov
Quoted 5 lines · Justin Suess
Sure, in some perfect world in the future where every verifier challenge is solved and BPF has feature parity with in-tree c on a 1:1 basis, you could implement something like SELinux, or Landlock in pure eBPF.
Try.. give it a shot? What is missing in the verifier?
Howdy Alexei,
I'll stick to Landlock as the concrete case, as it's what I'm most familiar with.
From security/landlock/fs.c: hook_sb_delete() holds the superblock's s_inode_list_lock, takes a nested lock of the inode's i_lock inside it, takes the RCU read lock inside that to dereference the per-inode Landlock object, and then the object's own lock, which is four levels of lock nesting, and even with all of that it still has to handle a race by re-checking inode_state_read() for specific flags to maintain VFS invariants. BPF cannot take any of these kernel locks, and the current BPF locking model deliberately excludes any nesting (bpf_spin_lock) or resolves contention by bailing out (trylock style); the opposite of what maintaining VFS invariants requires. To do effective inode-based access control without TOCTOU or bailing out of a long path walk, you'd need to solve nested locking with colored
locks: inode spinlocks, superblock locks, unix_state_lock, etc, and solve nested locking along the way.
You'd also need to convince the verifier that an upward VFS walk (dget_parent) is bounded, and find a way to cross mount boundaries; getting from a vfsmount to its struct mount is container_of(). (Pointer is arithmetic rejected even on trusted pointers). Pointers walked via d_parent become untrusted, so they can't be passed to any kfunc (bpf_path_d_path(), bpf_inode_storage_get()): inode local storage, which is otherwise an idiomatic inode security blob replacement, is only usable for the object at hand, but not its ancestors required for VFS walk.
Making it string/pathname based instead doesn't help either: any string-based method fails because the same file can be linked from multiple places, the same path string means different things in different mount namespaces, and strings aren't TOCTOU safe (the path can change during the walk). And you can't currently take references or locks on inodes, dentries or mounts from BPF, so any hierarchy walk is a lockless, unreferenced snapshot racing against rename, whereas Landlock's walk holds path_get()/dget_parent() holds the references at every step.
You'd also need credential-attached storage. BPF task storage has a different lifecycle, and is no substitute. It does not define how policy is shared by threads using the same credentials, copied or replaced during credential transitions, propogated through file->f_cred, or synchronized across a thread group. Recreating those semantics in BPF would require explicit BPF APIs for credential storage to be consistent.
Quoted 5 lines
Quoted 2 lines
Why force every eBPF program that needs to make security decisions to reeinvent the wheel?
What specific reinvention are you talking about?
Path based access control, done to the same correctness level as in Landlock, (TOCTOU free).
Quoted 8 lines
Quoted 2 lines
BPF already calls into LSM through security hooks. This is no different than
bpf_map_createhooks.what? It doesn't. bpf progs avoid lsm hooks as a plague. Not a single kfuncs calls into lsm directly.
It may call into security_*() by accident because it calls some kernel mechanisms.
I was referring to the security_bpf_map_create()
call in kernel/bpf/syscall.c, where BPF calls into LSM via an LSM hook just to show that kernel/bpf already calls security hooks directly (and now that I look, there are other security_bpf_*() calls in kernel/bpf/ too). Those hooks exist so LSM can mediate what BPF is allowed to do.
(These in this patch hooks are contrary; they are giving BPF control over LSM policy. And, indeed, no hook is called from kfuncs, as you stated).
The proposal is intended to add a narrow generic bridge, not to expose Landlock-specific kfuncs or make BPF depend on Landlock internals:
/* setup (BPF_PROG_TYPE_SYSCALL context) */
obj = bpf_lsm_policy_from_fd(fd, 0);
old = bpf_kptr_xchg(&map_val->policy, obj);
/* enforcement (sleepable BPF_PROG_TYPE_LSM on a bprm hook) */
bpf_rcu_read_lock();
obj = bpf_lsm_policy_acquire(map_val->policy);
bpf_rcu_read_unlock();
if (obj) {
bpf_lsm_policy_apply_bprm(obj, bprm, 0);
bpf_lsm_policy_release(obj);
}BPF never sees a Landlock kptr or any Landlock-specific type. It holds an opaque policy reference and invokes a generic operation. The LSM can evolve its internal representation and its hook-specific implementation without exporting breakage into the BPF ABI: the signature of the data the LSM sees can change independently of the kfunc (that's what the body-less hooks in patches 1-2 are for), avoiding breakage and easing refactoring.
Quoted 5 lines
Quoted 2 lines
Nothing about the way BPF works changes with this patchset. There's no verifier internal changes.
If the verifier is in the way of what you want to accomplish then please improve it.
There are also verifier improvements I'd like to make, (I can also work on these if you desire prerequisites or seperate fixes).
For instance, my silly bpf_lsm_policy_apply_bprm allowlist exists because there's no way to distinguish a linux_binprm seen from a tracepoint from one seen in an LSM hook (both are KF_TRUSTED) and it matters because there are tracepoints where it's no longer safe to modify the linux_binprm.
I'd rather have the verifier do that legwork through its type system than through BTF_ID sets, and I think you feel the same way.
....
The goal here is to keep LSM firmly *out of the way* of BPF business and let each subsystem focus on its core technology. It's my belief BPF and LSM subsystems can benefit from something like this given a chance, with a well-defined structured interface / guardrails.
All in good faith,
Justin
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 52 lines · Justin Suess
Quoted 8 lines · Alexei Starovoitov
Quoted 5 lines · On Sun, Sep 13, 2026 at 5:20???PM Justin Suess <[email protected]> wrote:
Sure, in some perfect world in the future where every verifier challenge is solved and BPF has feature parity with in-tree c on a 1:1 basis, you could implement something like SELinux, or Landlock in pure eBPF.
Try.. give it a shot? What is missing in the verifier?
Howdy Alexei,
I'll stick to Landlock as the concrete case, as it's what I'm most familiar with.
From
security/landlock/fs.c:hook_sb_delete()holds the superblock'ss_inode_list_lock, takes a nested lock of the inode'si_lockinside it, takes the RCU read lock inside that to dereference the per-inode Landlock object, and then the object's own lock, which is four levels of lock nesting, and even with all of that it still has to handle a race by re-checkinginode_state_read()for specific flags to maintain VFS invariants.BPF cannot take any of these kernel locks, and the current BPF locking model deliberately excludes any nesting (
bpf_spin_lock) or resolves contention by bailing out (trylock style); the opposite of what maintaining VFS invariants requires. To do effective inode-based access control without TOCTOU or bailing out of a long path walk, you'd need to solve nested locking with colored locks:
inode spinlocks, superblock locks,unix_state_lock, etc, and solve nested locking along the way.You'd also need to convince the verifier that an upward VFS walk (
dget_parent) is bounded, and find a way to cross mount boundaries; getting from a vfsmount to its struct mount iscontainer_of(). (Pointer is arithmetic rejected even on trusted pointers). Pointers walked viad_parentbecome untrusted, so they can't be passed to any kfunc (bpf_path_d_path(),bpf_inode_storage_get()): inode local storage, which is otherwise an idiomatic inode security blob replacement, is only usable for the object at hand, but not its ancestors required for VFS walk.Making it string/pathname based instead doesn't help either: any string-based method fails because the same file can be linked from multiple places, the same path string means different things in different mount namespaces, and strings aren't TOCTOU safe (the path can change during the walk). And you can't currently take references or locks on inodes, dentries or mounts from BPF, so any hierarchy walk is a lockless, unreferenced snapshot racing against rename, whereas Landlock's walk holds path_get()/dget_parent() holds the references at every step.
Quoted 7 lines
You'd also need credential-attached storage. BPF task storage has a different lifecycle, and is no substitute. It does not define how policy is shared by threads using the same credentials, copied or replaced during credential transitions, propogated through
file->f_cred, or synchronized across a thread group. Recreating those semantics in BPF would require explicit BPF APIs for credential storage to be consistent.
Excellent summary.
Alexei, for those of us contemplating potentially large BPF-LSM projects, does this imply that eBPF may never be ready for other than small boutique based security implementations?
Interested in your thoughts.
Quoted 7 lines
(These in this patch hooks are contrary; they are giving BPF control over LSM policy. And, indeed, no hook is called from kfuncs, as you stated).
The proposal is intended to add a narrow generic bridge, not to expose Landlock-specific kfuncs or make BPF depend on Landlock internals:
Based on Micka's mail that we responded to, this seems contrary to the intent of what you are trying to implement.
We can see where you are not making BPF depend on LandLock internals.
However, from his description, the intent is to have a process compose a LandLock policy and then hand enforcement off to an eBPF program, which on the surface would seem to suggest that the intent is to allow eBPF programs to enforce LandLock specific functionality and controls.
Perhaps we still misunderstand.
Quoted 2 lines
All in good faith,
Justin
The same.
Greg
My opinions and those of my Golden Retriever Hezzie only.
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Justin SuessQuoted 50 lines · Justin Suess
Quoted 5 lines · Alexei Starovoitov
Quoted 3 lines · Justin Suess
If
fs/,mm/,drivers/,net/, are permitted to have kfuncs, then why notsecurity/?kfuncs.rstdoesn't seem to forbid this.Quoted 8 lines
because fs, mm, net see the value in bpf. bpf helps these subsytems to focus on their core technologies and moves policy decisions out of kernel and into bpf.
while lsm people treat bpf as arch-enemy.
kfuncs are not stable. we keep refactoring them.
while lsm folks treat anything within *lsm* and *security* as stable apis. So any kfunc where they have a say, will become a huge burden for further bpf development.There are patches on the front page of lore.kernel.org/linux-security-module refactoring security hooks and changing signatures. Security hooks and the LSM interface is not stable, this is explicitly documented.
I deliberately chose kfuncs over helpers to avoid having the interface be ossified.Quoted 4 lines
Quoted 1 line
Where would you point me to?
landlock was designed for unprivileged users. bpf needs
CAP_BPF. There is a fundamental disconnect. If you can tolerateCAP_BPFthenLandlock is for privileged and unprivileged users alike. This patchset is designed to compose with unprivileged use seamlessly, which works because of Landlock's monotonicity.
Quoted 2 lines
just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.
Sure, in some perfect world in the future where every verifier challenge is solved and BPF has feature parity with in-tree c on a 1:1 basis, you could implement something like SELinux, or Landlock in pure eBPF. It would be about as easy and trivial as building a skyscraper from lego bricks.
There is an entire in-tree ecosystem of LSMs that would provide BPF users with well-maintained, versioned, and tested access control. Why force every eBPF program that needs to make security decisions to reeinvent the wheel?
There are real eBPF users such as Tetragon, Falco, Tracee, that could use an actively maintained implementation of security models that exist in-tree in
security/. This would make access control in eBPF much less painful.
Quoted 8 lines
Instead of implementing every filesystem, network, and socket access control, they can use existing solutions, and with the userspace tooling that already exists. And they can mix-and-match, doing custom logic/policy in BPF, and use what they need in LSM, with BPF making policy decisions.
It would fix a real challenge that users have with eBPF, and wouldn't take away from BPF's ability to do what it already does.
Justin, I find your thoughts and perspective on this interesting.
First a general question:
If we understand correctly, your work is to enhance the capabilities of LandLock. You are fortunate to be in the kernel already and you have at least a system call interface as a control plane and of course the possibility of implementing a pseudo-filesystem interface if something beyond the system call interface was needed.
Given that, what is the motivation for bringing eBPF and all the assorted drama related to that into the picture?
In addition to all the political drama, eBPF signing is still a bit up in the air as is the ability to completely lock eBPF based policy into the kernel so an adversary cannot displace it.
The other issue with this work, seems best described as a problem of 'Mixing of the Blood', as it was once referred to in the Iron Triangle country of south central North Dakota.
This effort seeks to merge two diametrically opposed viewpoints on how Linux security should be implemented. The camp that believes that all security policy should only be implemented, and most importantly, controlled through requiring inclusion in the kernel and the camp seeking the ability to implement kernel based security policies without years of contentious political struggle.
You raise the question above of as to why we should be forcing every eBPF program developer to dump out a pile of Lego's and start stacking them. The reason is simple, it is the only way to move forward, particularily in a timely manner, given how fast the security landscape and challenges are advancing.
While work like this could undoubtedly benefit the Tetragon, Falco, Tracee and others of this world it seems likely to run into issues over whether policy should be in the hands of what amounts to userspace.
Quoted 2 lines
Thanks,
Justin
Thanks for any insight you may have, especially with respect to the advantages of endowing in tree LSM's with eBPF capabilities.
Have a good remainder of the week.
Greg
Mickaël Salaün
maintainerRe: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Alexei StarovoitovHi,
Quoted 1 line · Alexei Starovoitov
On Fri Sep 11, 2026 at 10:26 PM PDT, Justin Suess wrote:
[...]
Quoted 1 line
landlock was designed for unprivileged users. bpf needs
CAP_BPF.
Yep, and this series would kind of bridge these two worlds. ;)
Quoted 3 lines
There is a fundamental disconnect. If you can tolerate
CAP_BPFthen just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.
The question is not if BPF LSM can or cannot do what Landlock does. Landlock provides a way for userspace to sandbox processes, and that comes with a well-defined semantic, logs, and an user space ABI, which are specific and dedicated to Landlock. Some user space depend on that.
The goal of this patch series is to improve BPF LSM to enable it to (also) enforce a Landlock security policy defined by user space thanks to the Landlock syscalls. The security policy still has the properties that makes it possible to safely compose different ones (e.g. monotonicity/stacking enforcement, inheritance, separation of duty, no cover channel, safe privilege reduction without confused deputy issues).
A process can currently define such security policy, get a file descriptor referring to it, and pass this FD to another process that would enforce it. This helps design secure architectures by splitting ownership, trust, and privileges (e.g. CAP_BPF). Being able to use BPF programs instead of user space programs to enforce (e.g. an already defined) security policy would make this enforcement more powerful thanks to the context BPF have access to. Of course, BPF LSM can (and should) also enforce other kind of complementary restrictions.
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 1 line · Micka??l Sala??n
Hi,
Hezzie says hi too.
Quoted 8 lines · Alexei Starovoitov
Quoted 1 line · Justin Suess
[...]
Quoted 1 line
landlock was designed for unprivileged users. bpf needs
CAP_BPF.Yep, and this series would kind of bridge these two worlds. ;)
See my comments to Justin about this 'Mixing of the Blood'... ;-)
Quoted 16 lines
Quoted 3 lines
There is a fundamental disconnect. If you can tolerate
CAP_BPFthen just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.The question is not if BPF LSM can or cannot do what Landlock does. Landlock provides a way for userspace to sandbox processes, and that comes with a well-defined semantic, logs, and an user space ABI, which are specific and dedicated to Landlock. Some user space depend on that.
The goal of this patch series is to improve BPF LSM to enable it to (also) enforce a Landlock security policy defined by user space thanks to the Landlock syscalls. The security policy still has the properties that makes it possible to safely compose different ones (e.g. monotonicity/stacking enforcement, inheritance, separation of duty, no cover channel, safe privilege reduction without confused deputy issues).
Quoted 8 lines
A process can currently define such security policy, get a file descriptor referring to it, and pass this FD to another process that would enforce it. This helps design secure architectures by splitting ownership, trust, and privileges (e.g.
CAP_BPF). Being able to use BPF programs instead of user space programs to enforce (e.g. an already defined) security policy would make this enforcement more powerful thanks to the context BPF have access to. Of course, BPF LSM can (and should) also enforce other kind of complementary restrictions.
This may be the answer to the question that I was asking Justin, lets see if we can unpack it a bit for those of us that are dense.
Big picture, as Justin so elegantly pointed out, there are a bunch of things that eBPF cannot do now, and may never be able to do in the future, particularily when it comes to 'raceless free' path based security controls, ie. TOCTOU avoidance.
So, the objective is to have a program, systemd for example, compose a set of different security policies, that it then hands out to various process heirarchies, which then uses eBPF to call LandLock kernel internals to do the heavy security lifting with kernel level privileges and capabilities, particularily when it comes to path based controls.
Is that close?
If so, the question remains, why take on this amount of drama?
Something like systemd does at least 10,000 other things, composing and handing out multiple variants of file descriptor based process hierarchy security policies would seem like chump change to it.
I'm assuming the answer is that the eBPF programs involved are going to be implementing other, perhaps proprietary, security controls that extend beyond the path based controls that eBPF can't do.
We will be interested in your reflections.
Greg
My opinions and those of my Golden Retriever Hezzie only.
Mickaël Salaün
maintainerRe: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
Quoted 9 lines · Dr. Greg
Quoted 1 line · Micka??l Sala??n
Hi,
Hezzie says hi too.
Quoted 3 lines · Alexei Starovoitov
Quoted 1 line · Justin Suess
Quoted 7 lines
Quoted 5 lines
[...]
Quoted 1 line
landlock was designed for unprivileged users. bpf needs
CAP_BPF.Yep, and this series would kind of bridge these two worlds. ;)
See my comments to Justin about this 'Mixing of the Blood'... ;-)
I'm pragmatic and there are different use cases, different constraints, and different trust models... Landlock has some good properties which makes it fit for being used by unprivileged/untrusted processes, and who can do more can do less.
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
There is a fundamental disconnect. If you can tolerate
CAP_BPFthen just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.
Please keep this in mind:
Quoted 22 lines
Quoted 13 lines
The question is not if BPF LSM can or cannot do what Landlock does.
Landlock provides a way for userspace to sandbox processes, and that comes with a well-defined semantic, logs, and an user space ABI, which are specific and dedicated to Landlock. Some user space depend on that.
The goal of this patch series is to improve BPF LSM to enable it to (also) enforce a Landlock security policy defined by user space thanks to the Landlock syscalls. The security policy still has the properties that makes it possible to safely compose different ones (e.g. monotonicity/stacking enforcement, inheritance, separation of duty, no cover channel, safe privilege reduction without confused deputy issues).
Quoted 8 lines
A process can currently define such security policy, get a file descriptor referring to it, and pass this FD to another process that would enforce it. This helps design secure architectures by splitting ownership, trust, and privileges (e.g.
CAP_BPF). Being able to use BPF programs instead of user space programs to enforce (e.g. an already defined) security policy would make this enforcement more powerful thanks to the context BPF have access to. Of course, BPF LSM can (and should) also enforce other kind of complementary restrictions.
Quoted 7 lines
This may be the answer to the question that I was asking Justin, lets see if we can unpack it a bit for those of us that are dense.
Big picture, as Justin so elegantly pointed out, there are a bunch of things that eBPF cannot do now, and may never be able to do in the future, particularily when it comes to 'raceless free' path based security controls, ie. TOCTOU avoidance.
That's a different problem/challenge, but BPF is gradually being improved, and I'm sure your contributions would be welcome.
Quoted 9 lines
So, the objective is to have a program, systemd for example, compose a set of different security policies, that it then hands out to various process heirarchies, which then uses eBPF to call LandLock kernel internals to do the heavy security lifting with kernel level privileges and capabilities, particularily when it comes to path based controls.
Is that close?
That's kind of the opposite: systemd, as a privileged process, is in charge of loading BPF programs, but this is not a good example here. That would make more sense with e.g., a dedicated process that manages a set of security policies (which might be owned by different teams), pass these (Landlock) policies to the a privileged process that enrich them with context, and load the related BPF programs to the kernel. That's only one simple example, but it should give a good idea of what is possible.
Quoted 10 lines
If so, the question remains, why take on this amount of drama?
Something like systemd does at least 10,000 other things, composing and handing out multiple variants of file descriptor based process hierarchy security policies would seem like chump change to it.
I'm assuming the answer is that the eBPF programs involved are going to be implementing other, perhaps proprietary, security controls that extend beyond the path based controls that eBPF can't do.
Yes, that's a possibility, but a simpler approach would be to just map a runtime context to a set of security policies (e.g. sandbox all instances of /usr/bin/foo with a custom profile).
Quoted 2 lines
We will be interested in your reflections.
Re: [PATCH bpf-next v3 04/15] lsm: Add the bpf_lsm_policy_release kfunc and policy object destructor
In reply to Mickaël SalaünQuoted 25 lines · Mickaël Salaün
Quoted 1 line · Alexei Starovoitov
On Fri Sep 11, 2026 at 10:26 PM PDT, Justin Suess wrote:
[...]
Quoted 1 line
landlock was designed for unprivileged users. bpf needs
CAP_BPF.Yep, and this series would kind of bridge these two worlds. ;)
Quoted 3 lines
There is a fundamental disconnect. If you can tolerate
CAP_BPFthen just use bpf-lsm and implement whatever policy you need there. If bpf-lsm is missing a feature we can fix that.The question is not if BPF LSM can or cannot do what Landlock does. Landlock provides a way for userspace to sandbox processes, and that comes with a well-defined semantic, logs, and an user space ABI, which are specific and dedicated to Landlock. Some user space depend on that.
The goal of this patch series is to improve BPF LSM to enable it to (also) enforce a Landlock security policy defined by user space thanks to the Landlock syscalls. The security policy still has the properties that makes it possible to safely compose different ones (e.g. monotonicity/stacking enforcement, inheritance, separation of duty, no cover channel, safe privilege reduction without confused deputy issues).
Quoted 8 lines
A process can currently define such security policy, get a file descriptor referring to it, and pass this FD to another process that would enforce it. This helps design secure architectures by splitting ownership, trust, and privileges (e.g.
CAP_BPF). Being able to use BPF programs instead of user space programs to enforce (e.g. an already defined) security policy would make this enforcement more powerful thanks to the context BPF have access to. Of course, BPF LSM can (and should) also enforce other kind of complementary restrictions.
I'd like to voice my support for this here and maybe try to make more concrete the "bridging of the two worlds".
For normal Landlock use, CAP_BPF can *not* be assumed because Landlock is designed to be used by unprivileged processes. (See
https://landlock.io/integrations/ for an incomplete list of programs already using it.) It works similar to the unprivileged use of
seccomp-bpf: The unprivileged process both defines and enforces the Landlock policy, by invoking unprivileged syscalls.
So a full rewrite of Landlock in eBPF seems infeasible (and TBH, it would be quite a drastic deviation from the original scope of this patch set).
However: It *is* true that a program that would use Justin's patch set *would* need CAP_BPF to use it, and therefore that program *could* also implement custom logic with the BPF-LSM.
But then again, since Landlock anyway already ships with well-reviewed policy logic for the normal unprivileged use-case, I don't understand why it should not be allowed for CAP_BPF programs to lean on that? It may be technically feasible to reimplement similar policies in eBPF, but it might also avoid a lot of complication and potential bugs if people could lean on a maintained solution like Landlock for the aspects where it fits the purpose?
–Günther
PATCH v3 5/15 lsm: Add the bpf_lsm_policy_from_fd kfunc
In reply to Justin SuessAdd the kfunc translating a file descriptor into a referenced policy object:
bpf_lsm_policy_from_fd(fd, flags)
KF_ACQUIRE|KF_RET_NULL|KF_SLEEPABLE
No argument names an LSM: a policy object fd refers to a file set up through the owning LSM's own userspace interface so the fd itself identifies the LSM asked to translate it. The kfunc offers the fd to every policy_object_from_fd implementation in turn until one claims it.
Following the convention of the lsm_*(2) syscalls, @flags belongs to the framework and is reserved: the kfunc returns NULL for @flags != 0.
A policy object fd is only meaningful in the fd table of the process that set the object 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 object may be released with bpf_lsm_policy_release().
---
Notes:
v2->v3:
- No change.
security/bpf_lsm_kfuncs.c | 53 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-)
security/bpf_lsm_kfuncs.c +51 -2
@@ -14,11 +14,50 @@__bpf_kfunc_start_defs (); +/** +* bpf_lsm_policy_from_fd - Get an LSM policy object from a fd +* @fd: file descriptor referring to a policy object, resolved in the +* file descriptor table of the task running the program +* @flags: reserved for future use, must be 0 +* +* Translate @fd, as set up through the owning LSM's own userspace +* interface, into a referenced policy object. The fd identifies the +* LSM asked to translate it: each LSM recognizes its own fds and +* declines every other. Only syscall programs may call this kfunc: +* they run in the context of the task invoking them, where the fd is +* meaningful. The reference must be released with +* bpf_lsm_policy_release(). +* +* Return: A referenced policy object, or NULL if @flags is not 0, if +* no enabled LSM recognizes @fd as one of its policy objects, or if +* the recognizing LSM fails to translate it. +*/ +__bpf_kfunc struct lsm_policy_object *bpf_lsm_policy_from_fd(int fd, u32 flags) +{ +struct lsm_static_call *scall; +struct lsm_policy_object *object; +int err; + +if (flags) +return NULL ; + +lsm_for_each_hook (scall, policy_object_from_fd) { + err = scall->hl->hook.policy_object_from_fd (fd, &object); +if (err == -EOPNOTSUPP ) +/* Not this LSM's fd: let another claim it. */ +continue ; +if (err) +return NULL ; +return object; + } +return NULL ; +} +/** * bpf_lsm_policy_release - Release a policy object reference * @object: policy object to release * -* Release an acquired reference on a policy object. +* Release a reference acquired with bpf_lsm_policy_from_fd(). */ __bpf_kfunc void bpf_lsm_policy_release(struct lsm_policy_object *object) {
@@ -44,6 +83,8 @@ CFI_NOSEAL(bpf_lsm_policy_release_dtor);__bpf_kfunc_end_defs (); BTF_KFUNCS_START(bpf_lsm_policy_kfunc_ids) +BTF_ID_FLAGS(func, bpf_lsm_policy_from_fd, + KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_lsm_policy_release, KF_RELEASE) BTF_KFUNCS_END(bpf_lsm_policy_kfunc_ids)
@@ -51,10 +92,14 @@ BTF_ID_LIST(bpf_lsm_policy_dtor_ids) BTF_ID(struct, lsm_policy_object) BTF_ID(func, bpf_lsm_policy_release_dtor) +BTF_ID_LIST_SINGLE (bpf_lsm_policy_from_fd_ids, func, bpf_lsm_policy_from_fd) +/* * BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL share their kfunc * lookup buckets with other program types, so restricting the policy -* kfuncs requires a filter. +* kfuncs requires a filter. A policy object fd is only meaningful in +* the fd table of the task that set the object up: the fd kfunc is +* exclusive to syscall programs, which run in that task's context. */static int bpf_lsm_policy_kfunc_filter (const struct bpf_prog *prog,u32 kfunc_id)
@@ -64,7 +109,11 @@ static int bpf_lsm_policy_kfunc_filter(const struct bpf_prog *prog,switch (prog->type) {case BPF_PROG_TYPE_SYSCALL : +return 0 ;case BPF_PROG_TYPE_LSM : +if (kfunc_id == bpf_lsm_policy_from_fd_ids[0 ]) +return -EACCES ; +return 0 ;default :return -EACCES ;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 05/15] lsm: Add the bpf_lsm_policy_from_fd kfunc
This isn't a bug, but would keeping the more general wording here ('Release an acquired reference on a policy object') age better than listing the acquiring kfuncs? A reference can also come out of…
Quoted 4 lines
security/bpf_lsm_kfuncs.c +0
-0
[ ... ]
Quoted 40 lines
security/bpf_lsm_kfuncs.c +36
-0@@ -14,11 +14,50 @@__bpf_kfunc_start_defs (); +/** +* bpf_lsm_policy_from_fd - Get an LSM policy object from a fd +* @fd: file descriptor referring to a policy object, resolved in the +* file descriptor table of the task running the program +* @flags: reserved for future use, must be 0 +* +* Translate @fd, as set up through the owning LSM's own userspace +* interface, into a referenced policy object. The fd identifies the +* LSM asked to translate it: each LSM recognizes its own fds and +* declines every other. Only syscall programs may call this kfunc: +* they run in the context of the task invoking them, where the fd is +* meaningful. The reference must be released with +* bpf_lsm_policy_release(). +* +* Return: A referenced policy object, or NULL if @flags is not 0, if +* no enabled LSM recognizes @fd as one of its policy objects, or if +* the recognizing LSM fails to translate it. +*/ +__bpf_kfunc struct lsm_policy_object *bpf_lsm_policy_from_fd(int fd, u32 flags) +{ +struct lsm_static_call *scall; +struct lsm_policy_object *object; +int err; + +if (flags) +return NULL ; + +lsm_for_each_hook (scall, policy_object_from_fd) { + err = scall->hl->hook.policy_object_from_fd (fd, &object); +if (err == -EOPNOTSUPP ) +/* Not this LSM's fd: let another claim it. */ +continue ; +if (err) +return NULL ; +return object; + }
security/bpf_lsm_kfuncs.c line 61
Quoted 10 lines
security/bpf_lsm_kfuncs.c +4
-1+return NULL ; +} +/** * bpf_lsm_policy_release - Release a policy object reference * @object: policy object to release * -* Release an acquired reference on a policy object. +* Release a reference acquired with bpf_lsm_policy_from_fd(). */
This isn't a bug, but would keeping the more general wording here ('Release an acquired reference on a policy object') age better than listing the acquiring kfuncs? A reference can also come out of a map kptr field via bpf_kptr_xchg(), so the comment might need amending again when other acquiring kfuncs are added.
---
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/34398515657
PATCH v3 6/15 lsm: Add the bpf_lsm_policy_acquire kfunc
In reply to Justin SuessAdd the kfunc acquiring a reference on a policy object the program does not own:
bpf_lsm_policy_acquire(object) KF_ACQUIRE|KF_RCU|KF_RET_NULL
bpf_kptr_xchg() is the only way to take an owned pointer out of a map kptr field, and it empties the slot: concurrent executions of an enforcement program would race for the one stored reference. Modeled after bpf_task_acquire(), this kfunc removes the exclusivity: a program loads the kptr field with a plain read under bpf_rcu_read_lock(), acquires its own reference through the policy_object_get hook, and leaves the map slot untouched. The acquired reference survives bpf_rcu_read_unlock(), carrying over to a sleepable bpf_lsm_policy_apply_bprm() call, and is released with bpf_lsm_policy_release().
Adding struct lsm_policy_object to the verifier's rcu_protected_types set makes the plain load yield an RCU-protected pointer instead of an untrusted one. This is where the policy object contract's RCU requirements become load-bearing: the kfunc and the get hook examine the object concurrently with a possible last put, which is safe because implementations free only after an RCU grace period and acquire with inc-not-zero semantics. A failed get makes the kfunc return NULL, per KF_RET_NULL.
The kfunc does not sleep and is meaningful wherever a policy object pointer can be loaded, so the filter adds no per-kfunc rule.
---
Notes:
v2->v3:
- No change.
kernel/bpf/verifier.c | 3 +++ security/bpf_lsm_kfuncs.c | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +3 -0
@@ -4660,6 +4660,9 @@ BTF_ID(struct, bpf_crypto_ctx)#ifdef CONFIG_INET BTF_ID(struct, bpf_ksock)#endif +#ifdef CONFIG_BPF_LSM +BTF_ID(struct, lsm_policy_object) +#endif BTF_SET_END (rcu_protected_types) static bool rcu_protected_object(const struct btf *btf, u32 btf_id)
security/bpf_lsm_kfuncs.c +33 -1
@@ -14,6 +14,36 @@__bpf_kfunc_start_defs (); +/** +* bpf_lsm_policy_acquire - Acquire a reference on a shared policy object +* @object: RCU-protected pointer to a policy object, e.g. loaded from +* a map kptr field under bpf_rcu_read_lock() +* +* Acquire a reference of its own on a policy object the program does +* not own, so that any number of concurrent program executions can +* use the object shared through one map kptr field, without emptying +* it as bpf_kptr_xchg() would. The returned reference stays valid +* after bpf_rcu_read_unlock() and must be released with +* bpf_lsm_policy_release(). +* +* Return: A referenced policy object, or NULL if the object's +* reference count concurrently dropped to zero. +*/ +__bpf_kfunc struct lsm_policy_object * +bpf_lsm_policy_acquire (struct lsm_policy_object *object) +{ +struct lsm_static_call *scall; + +lsm_for_each_hook (scall, policy_object_get) { +if (scall->hl->lsmid->id != object->lsmid) +continue ; +if (scall->hl->hook.policy_object_get (object)) +return NULL ; +return object; + } +return NULL ; +} +/** * bpf_lsm_policy_from_fd - Get an LSM policy object from a fd * @fd: file descriptor referring to a policy object, resolved in the
@@ -57,7 +87,8 @@ __bpf_kfunc struct lsm_policy_object *bpf_lsm_policy_from_fd(int fd, u32 flags)* bpf_lsm_policy_release - Release a policy object reference * @object: policy object to release * -* Release a reference acquired with bpf_lsm_policy_from_fd(). +* Release a reference acquired with bpf_lsm_policy_from_fd() or +* bpf_lsm_policy_acquire(). */ __bpf_kfunc void bpf_lsm_policy_release(struct lsm_policy_object *object) {
@@ -83,6 +114,7 @@ CFI_NOSEAL(bpf_lsm_policy_release_dtor);__bpf_kfunc_end_defs (); BTF_KFUNCS_START(bpf_lsm_policy_kfunc_ids) +BTF_ID_FLAGS(func, bpf_lsm_policy_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_lsm_policy_from_fd, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_lsm_policy_release, KF_RELEASE)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 06/15] lsm: Add the bpf_lsm_policy_acquire kfunc
This isn't a bug, but would it be worth spelling out the other NULL cases here -- no enabled LSM owning @object->lsmid, or the owning LSM's get hook failing -- the way bpf_lsm_policy_from_fd()'s…
Quoted 13 lines
security/bpf_lsm_kfuncs.c +5
-0@@ -14,6 +14,36 @@__bpf_kfunc_start_defs (); +/** +* bpf_lsm_policy_acquire - Acquire a reference on a shared policy object +* @object: RCU-protected pointer to a policy object, e.g. loaded from +* a map kptr field under bpf_rcu_read_lock() +*
security/bpf_lsm_kfuncs.c line 31
Quoted 10 lines
security/bpf_lsm_kfuncs.c +10
-0+* Acquire a reference of its own on a policy object the program does +* not own, so that any number of concurrent program executions can +* use the object shared through one map kptr field, without emptying +* it as bpf_kptr_xchg() would. The returned reference stays valid +* after bpf_rcu_read_unlock() and must be released with +* bpf_lsm_policy_release(). +* +* Return: A referenced policy object, or NULL if the object's +* reference count concurrently dropped to zero. +*/
This isn't a bug, but would it be worth spelling out the other NULL cases here -- no enabled LSM owning @object->lsmid, or the owning LSM's get hook failing -- the way bpf_lsm_policy_from_fd()'s Return: section does just below?
The current text names one of the three ways the kfunc yields NULL. The body also returns NULL when no enabled LSM's policy_object_get static call matches @object->lsmid, and when the owning LSM's hook fails for a reason other than a dead refcount.
Quoted 4 lines
security/bpf_lsm_kfuncs.c +4
-0+__bpf_kfunc struct lsm_policy_object * +bpf_lsm_policy_acquire (struct lsm_policy_object *object) +{ +struct lsm_static_call *scall;
security/bpf_lsm_kfuncs.c line 45
Quoted 10 lines
security/bpf_lsm_kfuncs.c +10
-0+ +lsm_for_each_hook (scall, policy_object_get) { +if (scall->hl->lsmid->id != object->lsmid) +continue ; +if (scall->hl->hook.policy_object_get (object)) +return NULL ; +return object; + } +return NULL ; +}
---
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/34398515657
PATCH v3 7/15 lsm: Add the bpf_lsm_policy_apply_bprm kfunc
In reply to Justin SuessAdd the kfunc applying a policy object to an execution:
bpf_lsm_policy_apply_bprm(object, bprm, flags) KF_SLEEPABLE
It asks the LSM owning @object, through the bprm_apply_policy_object hook, to restrict the credentials prepared in @bprm, so that the executed task starts confined by the policy. The meaning of @flags and the composition with restrictions the credentials already carry are the owning LSM's; an LSM without execution policy support makes the call fail with -EOPNOTSUPP.
The kfunc runs the hook in a root memcg charging scope: the policy restricts the execution on behalf of the BPF program, not of the mediated task, so what the owning LSM allocates to compute it, e.g. Landlock's merged domain, is not charged to the task the program supervises.
The filter makes the kfunc exclusive to the sleepable LSM programs attached to the bprm_creds_for_exec() or bprm_creds_from_file() hooks, the only contexts where the bprm's credentials are prepared but not yet committed. The verifier's argument typing does not draw this boundary on its own: a trusted struct linux_binprm pointer is also available at the other bprm hooks -- bprm_check_security(), which runs per binfmt while an interpreter may still rewrite the execution, and bprm_committing_creds()/bprm_committed_creds(), which run at or past the point of no return, where the prepared credentials are frozen or installed -- and to tp_btf programs via the sched_prepare_exec and sched_process_exec tracepoints, which resolve kfuncs from the same registration bucket as LSM programs. The attach-point filter, not the argument type, is the authorization boundary.
No filter case is needed for BPF_LSM_CGROUP programs: since commit 5b038319be44 ("bpf: Reject sleepable BPF_LSM_CGROUP programs at load time") they cannot be sleepable, so KF_SLEEPABLE already excludes them.
---
Notes:
v2->v3:
- Drop the BPF_LSM_CGROUP case from the kfunc filter: since commit 5b038319be44 ("bpf: Reject sleepable BPF_LSM_CGROUP programs at load time") such programs cannot be sleepable, so KF_SLEEPABLE already excludes them from the apply kfunc.
- Document, in the commit message and the filter comment, why the attach-point filter rather than the verifier's argument typing is the authorization boundary.
security/bpf_lsm_kfuncs.c | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
security/bpf_lsm_kfuncs.c +68 -0
@@ -2,16 +2,25 @@/* BPF kfuncs exposing LSM policy objects. */ +#include <linux/binfmts.h> #include <linux/bpf.h> #include <linux/btf.h> #include <linux/btf_ids.h> #include <linux/cfi.h> #include <linux/init.h> #include <linux/lsm_hooks.h> +#include <linux/memcontrol.h> +#include <linux/sched/mm.h> #include <linux/security.h> #include "lsm.h" +/* The sleepable LSM hooks bpf_lsm_policy_apply_bprm() may be called from. */ +BTF_SET_START(bpf_lsm_policy_bprm_hooks) +BTF_ID(func, bpf_lsm_bprm_creds_for_exec) +BTF_ID(func, bpf_lsm_bprm_creds_from_file) +BTF_SET_END(bpf_lsm_policy_bprm_hooks) + __bpf_kfunc_start_defs();/**
@@ -44,6 +53,49 @@ bpf_lsm_policy_acquire(struct lsm_policy_object *object)return NULL ; } +/** +* bpf_lsm_policy_apply_bprm - Apply a policy object to exec credentials +* @object: policy object to apply +* @bprm: execution context providing the prepared credentials to +* restrict +* @flags: flags defined by the LSM owning @object +* +* Ask the LSM owning @object to restrict the credentials prepared in +* @bprm with it, so that the executed task starts confined by the +* policy. How the policy composes with restrictions the credentials +* already carry, and the meaning of @flags, are defined by the owning +* LSM. @object is only borrowed: the caller keeps its reference. +* The hook runs in a root memcg charging scope: policy the LSM +* computes on behalf of the program is not charged to the mediated +* task. +* +* Return: 0 on success, -EOPNOTSUPP if the LSM owning @object does +* not support applying policy to an execution, -EINVAL on unsupported +* @flags, other negative values on LSM-specific failures. +*/ +__bpf_kfunc int bpf_lsm_policy_apply_bprm(struct lsm_policy_object *object, +struct linux_binprm *bprm,u32 flags) +{ +struct lsm_static_call *scall; +struct mem_cgroup *old_memcg; +int err; + +lsm_for_each_hook (scall, bprm_apply_policy_object) { +if (scall->hl->lsmid->id != object->lsmid) +continue ; +/* +* The hook runs on behalf of the BPF program, not of the +* mediated task: charge its allocations to the root memcg. +*/ + old_memcg =set_active_memcg (root_mem_cgroup); + err = scall->hl->hook.bprm_apply_policy_object (bprm, object, + flags); +set_active_memcg (old_memcg); +return err; + } +return -EOPNOTSUPP ; +} +/** * bpf_lsm_policy_from_fd - Get an LSM policy object from a fd * @fd: file descriptor referring to a policy object, resolved in the
@@ -115,6 +167,7 @@ __bpf_kfunc_end_defs(); BTF_KFUNCS_START(bpf_lsm_policy_kfunc_ids) BTF_ID_FLAGS(func, bpf_lsm_policy_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_lsm_policy_apply_bprm, KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_lsm_policy_from_fd, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_lsm_policy_release, KF_RELEASE)
@@ -124,6 +177,8 @@ BTF_ID_LIST(bpf_lsm_policy_dtor_ids) BTF_ID(struct, lsm_policy_object) BTF_ID(func, bpf_lsm_policy_release_dtor) +BTF_ID_LIST_SINGLE (bpf_lsm_policy_apply_bprm_ids, func, + bpf_lsm_policy_apply_bprm)BTF_ID_LIST_SINGLE (bpf_lsm_policy_from_fd_ids, func, bpf_lsm_policy_from_fd)/*
@@ -132,6 +187,12 @@ BTF_ID_LIST_SINGLE(bpf_lsm_policy_from_fd_ids, func, bpf_lsm_policy_from_fd)* kfuncs requires a filter. A policy object fd is only meaningful in * the fd table of the task that set the object up: the fd kfunc is * exclusive to syscall programs, which run in that task's context. +* Applying policy to an execution is exclusive to the sleepable bprm +* LSM hooks the operation is specified for. The bprm argument's type +* alone does not draw that boundary: other bprm hooks and the tp_btf +* exec tracepoints, which share the LSM programs' kfunc bucket, also +* provide a trusted bprm pointer outside the window where the +* prepared credentials may still be updated. */ static int bpf_lsm_policy_kfunc_filter (const struct bpf_prog *prog,u32 kfunc_id)
@@ -141,11 +202,18 @@ static int bpf_lsm_policy_kfunc_filter(const struct bpf_prog *prog,switch (prog->type) {case BPF_PROG_TYPE_SYSCALL : +if (kfunc_id == bpf_lsm_policy_apply_bprm_ids[0 ]) +return -EACCES ;return 0 ;case BPF_PROG_TYPE_LSM :if (kfunc_id == bpf_lsm_policy_from_fd_ids[0 ])return -EACCES ; +if (kfunc_id == bpf_lsm_policy_apply_bprm_ids[0 ] && + !btf_id_set_contains (&bpf_lsm_policy_bprm_hooks, + prog->aux->attach_btf_id)) +return -EACCES ; +return 0 ;default :return -EACCES ;
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 07/15] lsm: Add the bpf_lsm_policy_apply_bprm kfunc
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The attach-point authorization boundary for `bpf_lsm_policy_apply_bprm` can be bypassed using BPF…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The attach-point authorization boundary for bpf_lsm_policy_apply_bprm can be bypassed using BPF tail calls. --
commit 7b53e20161b20c35fb85beb1fba538fe85514074
Author: Justin Suess <[email protected]> Date: Wed Sep 9 19:37:10 2026 +0000
lsm: Add the bpf_lsm_policy_apply_bprm kfunc
This commit introduces the bpf_lsm_policy_apply_bprm kfunc, allowing sleepable LSM BPF programs to restrict credentials prepared in a linux_binprm. It restricts this kfunc to specific execution hooks (bprm_creds_for_exec and bprm_creds_from_file) to prevent policies from being applied at unsafe points.
Quoted 4 lines
security/bpf_lsm_kfuncs.c +0
-0
[ ... ]
Quoted 5 lines
security/bpf_lsm_kfuncs.c +1
-0@@ -141,11 +202,18 @@ static int bpf_lsm_policy_kfunc_filter(const struct bpf_prog *prog,switch (prog->type) {case BPF_PROG_TYPE_SYSCALL : +if (kfunc_id == bpf_lsm_policy_apply_bprm_ids[0 ])
security/bpf_lsm_kfuncs.c static int bpf_lsm_policy_kfunc_filter(const struct bpf_prog *prog, · line 215
Quoted 10 lines
security/bpf_lsm_kfuncs.c +5
-0+return -EACCES ;return 0 ;case BPF_PROG_TYPE_LSM :if (kfunc_id == bpf_lsm_policy_from_fd_ids[0 ])return -EACCES ; +if (kfunc_id == bpf_lsm_policy_apply_bprm_ids[0 ] && + !btf_id_set_contains (&bpf_lsm_policy_bprm_hooks, + prog->aux->attach_btf_id)) +return -EACCES ;
[Severity: High]
Can the attach-point authorization boundary be bypassed using BPF tail calls?
This verification-time check inspects the initial program's attach point, but does it account for runtime context switches via tail calls?
For example, if a privileged user loads Program A attached to bprm_creds_for_exec that calls bpf_lsm_policy_apply_bprm(), it passes this filter and can be inserted into a PROG_ARRAY map.
If they then load Program B attached to bprm_check_security and perform a bpf_tail_call() into Program A, the tail call is permitted because both hooks share identical BTF function prototypes (int (struct linux_binprm *)).
Does this allow bpf_lsm_policy_apply_bprm() to be executed at bprm_check_security, creating a TOCTOU vulnerability since the execution can still be rewritten by an interpreter?
Quoted 4 lines
security/bpf_lsm_kfuncs.c +1
-0+return 0 ;default :return -EACCES ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH bpf-next v3 07/15] lsm: Add the bpf_lsm_policy_apply_bprm kfunc
Quoted 48 lines · On Wed, Sep 09, 2026 at 07:55:00PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The attach-point authorization boundary forbpf_lsm_policy_apply_bprmcan be bypassed using BPF tail calls. --commit
7b53e20161b20c35fb85beb1fba538fe85514074Author: Justin Suess <[email protected]> Date: Wed Sep 9 19:37:10 2026 +0000lsm: Add the
bpf_lsm_policy_apply_bprmkfuncThis commit introduces the
bpf_lsm_policy_apply_bprmkfunc, allowing sleepable LSM BPF programs to restrict credentials prepared in alinux_binprm. It restricts this kfunc to specific execution hooks (bprm_creds_for_execandbprm_creds_from_file) to prevent policies from being applied at unsafe points.Quoted 4 lines
security/bpf_lsm_kfuncs.c +0
-0[ ... ]
Quoted 5 lines
security/bpf_lsm_kfuncs.c +1
-0@@ -141,11 +202,18 @@ static int bpf_lsm_policy_kfunc_filter(const struct bpf_prog *prog,switch (prog->type) {case BPF_PROG_TYPE_SYSCALL : +if (kfunc_id == bpf_lsm_policy_apply_bprm_ids[0 ])security/bpf_lsm_kfuncs.c static int bpf_lsm_policy_kfunc_filter(const struct bpf_prog *prog, · line 215
Quoted 10 lines
security/bpf_lsm_kfuncs.c +5
-0+return -EACCES ;return 0 ;case BPF_PROG_TYPE_LSM :if (kfunc_id == bpf_lsm_policy_from_fd_ids[0 ])return -EACCES ; +if (kfunc_id == bpf_lsm_policy_apply_bprm_ids[0 ] && + !btf_id_set_contains (&bpf_lsm_policy_bprm_hooks, + prog->aux->attach_btf_id)) +return -EACCES ;[Severity: High]
Can the attach-point authorization boundary be bypassed using BPF tail calls?This verification-time check inspects the initial program's attach point, but does it account for runtime context switches via tail calls?
For example, if a privileged user loads Program A attached to
bprm_creds_for_execthat callsbpf_lsm_policy_apply_bprm(), it passes this filter and can be inserted into aPROG_ARRAYmap.
Quoted 8 lines
If they then load Program B attached to
bprm_check_securityand perform abpf_tail_call()into Program A, the tail call is permitted because both hooks share identical BTF function prototypes (int (structlinux_binprm*)).Does this allow
bpf_lsm_policy_apply_bprm()to be executed atbprm_check_security, creating a TOCTOU vulnerability since the execution can still be rewritten by an interpreter?
Yes you can do this, but no it's not a vulnerability. No incorrect state would be created here (in bprm_check_security, creds are still uncommitted), so there's no issue in actually just letting a user apply their policy there...
I'll probably just add bprm_check_security to the allowed hook list.
Quoted 7 lines
Quoted 4 lines
security/bpf_lsm_kfuncs.c +1
-0+return 0 ;default :return -EACCES ;Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Re: [PATCH bpf-next v3 07/15] lsm: Add the bpf_lsm_policy_apply_bprm kfunc
In reply to Justin SuessQuoted 120 lines · Justin Suess
Add the kfunc applying a policy object to an execution:
bpf_lsm_policy_apply_bprm(object, bprm, flags)KF_SLEEPABLEIt asks the LSM owning @object, through the
bprm_apply_policy_objecthook, to restrict the credentials prepared in @bprm, so that the executed task starts confined by the policy. The meaning of @flags and the composition with restrictions the credentials already carry are the owning LSM's; an LSM without execution policy support makes the call fail with-EOPNOTSUPP.The kfunc runs the hook in a root memcg charging scope: the policy restricts the execution on behalf of the BPF program, not of the mediated task, so what the owning LSM allocates to compute it, e.g. Landlock's merged domain, is not charged to the task the program supervises.
The filter makes the kfunc exclusive to the sleepable LSM programs attached to the
bprm_creds_for_exec()orbprm_creds_from_file()hooks, the only contexts where the bprm's credentials are prepared but not yet committed. The verifier's argument typing does not draw this boundary on its own: a trusted structlinux_binprmpointer is also available at the other bprm hooks --bprm_check_security(), which runs per binfmt while an interpreter may still rewrite the execution, and bprm_committing_creds()/bprm_committed_creds(), which run at or past the point of no return, where the prepared credentials are frozen or installed -- and totp_btfprograms via thesched_prepare_execandsched_process_exectracepoints, which resolve kfuncs from the same registration bucket as LSM programs. The attach-point filter, not the argument type, is the authorization boundary.No filter case is needed for
BPF_LSM_CGROUPprograms: since commit5b038319be44("bpf: Reject sleepableBPF_LSM_CGROUPprograms at load time") they cannot be sleepable, soKF_SLEEPABLEalready excludes them.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- Drop theBPF_LSM_CGROUPcase from the kfunc filter: since commit5b038319be44("bpf: Reject sleepableBPF_LSM_CGROUPprograms at load time") such programs cannot be sleepable, soKF_SLEEPABLEalready excludes them from the apply kfunc.
- Document, in the commit message and the filter comment, why the attach-point filter rather than the verifier's argument typing is the authorization boundary.security/bpf_lsm_kfuncs.c | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)security/bpf_lsm_kfuncs.c +40
-0@@ -2,16 +2,25 @@/* BPF kfuncs exposing LSM policy objects. */ +#include <linux/binfmts.h> #include <linux/bpf.h> #include <linux/btf.h> #include <linux/btf_ids.h> #include <linux/cfi.h> #include <linux/init.h> #include <linux/lsm_hooks.h> +#include <linux/memcontrol.h> +#include <linux/sched/mm.h> #include <linux/security.h> #include "lsm.h" +/* The sleepable LSM hooks bpf_lsm_policy_apply_bprm() may be called from. */ +BTF_SET_START(bpf_lsm_policy_bprm_hooks) +BTF_ID(func, bpf_lsm_bprm_creds_for_exec) +BTF_ID(func, bpf_lsm_bprm_creds_from_file) +BTF_SET_END(bpf_lsm_policy_bprm_hooks) + __bpf_kfunc_start_defs();/** @@ -44,6 +53,49 @@ bpf_lsm_policy_acquire(struct lsm_policy_object *object)return NULL ; } +/** +* bpf_lsm_policy_apply_bprm - Apply a policy object to exec credentials +* @object: policy object to apply +* @bprm: execution context providing the prepared credentials to +* restrict +* @flags: flags defined by the LSM owning @object +* +* Ask the LSM owning @object to restrict the credentials prepared in +* @bprm with it, so that the executed task starts confined by the +* policy. How the policy composes with restrictions the credentials +* already carry, and the meaning of @flags, are defined by the owning +* LSM. @object is only borrowed: the caller keeps its reference. +* The hook runs in a root memcg charging scope: policy the LSM +* computes on behalf of the program is not charged to the mediated +* task. +* +* Return: 0 on success, -EOPNOTSUPP if the LSM owning @object does +* not support applying policy to an execution, -EINVAL on unsupported +* @flags, other negative values on LSM-specific failures. +*/ +__bpf_kfunc int bpf_lsm_policy_apply_bprm(struct lsm_policy_object *object, +struct linux_binprm *bprm,u32 flags) +{ +struct lsm_static_call *scall; +struct mem_cgroup *old_memcg; +int err; + +lsm_for_each_hook (scall, bprm_apply_policy_object) { +if (scall->hl->lsmid->id != object->lsmid) +continue ; +/*
security/bpf_lsm_kfuncs.c bpf_lsm_policy_acquire(struct lsm_policy_object *object) · line 96
Quoted 10 lines
security/bpf_lsm_kfuncs.c +10
-0+* The hook runs on behalf of the BPF program, not of the +* mediated task: charge its allocations to the root memcg. +*/ + old_memcg =set_active_memcg (root_mem_cgroup); + err = scall->hl->hook.bprm_apply_policy_object (bprm, object, + flags); +set_active_memcg (old_memcg); +return err; + } +return -EOPNOTSUPP ;
Nack.
Re: [PATCH bpf-next v3 07/15] lsm: Add the bpf_lsm_policy_apply_bprm kfunc
Quoted 125 lines · Alexei Starovoitov
Quoted 124 lines · Justin Suess
Add the kfunc applying a policy object to an execution:
bpf_lsm_policy_apply_bprm(object, bprm, flags)KF_SLEEPABLEIt asks the LSM owning @object, through the
bprm_apply_policy_objecthook, to restrict the credentials prepared in @bprm, so that the executed task starts confined by the policy. The meaning of @flags and the composition with restrictions the credentials already carry are the owning LSM's; an LSM without execution policy support makes the call fail with-EOPNOTSUPP.The kfunc runs the hook in a root memcg charging scope: the policy restricts the execution on behalf of the BPF program, not of the mediated task, so what the owning LSM allocates to compute it, e.g. Landlock's merged domain, is not charged to the task the program supervises.
The filter makes the kfunc exclusive to the sleepable LSM programs attached to the
bprm_creds_for_exec()orbprm_creds_from_file()hooks, the only contexts where the bprm's credentials are prepared but not yet committed. The verifier's argument typing does not draw this boundary on its own: a trusted structlinux_binprmpointer is also available at the other bprm hooks --bprm_check_security(), which runs per binfmt while an interpreter may still rewrite the execution, and bprm_committing_creds()/bprm_committed_creds(), which run at or past the point of no return, where the prepared credentials are frozen or installed -- and totp_btfprograms via thesched_prepare_execandsched_process_exectracepoints, which resolve kfuncs from the same registration bucket as LSM programs. The attach-point filter, not the argument type, is the authorization boundary.No filter case is needed for
BPF_LSM_CGROUPprograms: since commit5b038319be44("bpf: Reject sleepableBPF_LSM_CGROUPprograms at load time") they cannot be sleepable, soKF_SLEEPABLEalready excludes them.Cc Paul Moore <[email protected]>Cc KP Singh <[email protected]>Signed-off-by Justin Suess <[email protected]>---
Notes:
v2->v3:
- Drop theBPF_LSM_CGROUPcase from the kfunc filter: since commit5b038319be44("bpf: Reject sleepableBPF_LSM_CGROUPprograms at load time") such programs cannot be sleepable, soKF_SLEEPABLEalready excludes them from the apply kfunc.
- Document, in the commit message and the filter comment, why the attach-point filter rather than the verifier's argument typing is the authorization boundary.security/bpf_lsm_kfuncs.c | 68 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)security/bpf_lsm_kfuncs.c +44
-0@@ -2,16 +2,25 @@/* BPF kfuncs exposing LSM policy objects. */ +#include <linux/binfmts.h> #include <linux/bpf.h> #include <linux/btf.h> #include <linux/btf_ids.h> #include <linux/cfi.h> #include <linux/init.h> #include <linux/lsm_hooks.h> +#include <linux/memcontrol.h> +#include <linux/sched/mm.h> #include <linux/security.h> #include "lsm.h" +/* The sleepable LSM hooks bpf_lsm_policy_apply_bprm() may be called from. */ +BTF_SET_START(bpf_lsm_policy_bprm_hooks) +BTF_ID(func, bpf_lsm_bprm_creds_for_exec) +BTF_ID(func, bpf_lsm_bprm_creds_from_file) +BTF_SET_END(bpf_lsm_policy_bprm_hooks) + __bpf_kfunc_start_defs();/** @@ -44,6 +53,49 @@ bpf_lsm_policy_acquire(struct lsm_policy_object *object)return NULL ; } +/** +* bpf_lsm_policy_apply_bprm - Apply a policy object to exec credentials +* @object: policy object to apply +* @bprm: execution context providing the prepared credentials to +* restrict +* @flags: flags defined by the LSM owning @object +* +* Ask the LSM owning @object to restrict the credentials prepared in +* @bprm with it, so that the executed task starts confined by the +* policy. How the policy composes with restrictions the credentials +* already carry, and the meaning of @flags, are defined by the owning +* LSM. @object is only borrowed: the caller keeps its reference. +* The hook runs in a root memcg charging scope: policy the LSM +* computes on behalf of the program is not charged to the mediated +* task. +* +* Return: 0 on success, -EOPNOTSUPP if the LSM owning @object does +* not support applying policy to an execution, -EINVAL on unsupported +* @flags, other negative values on LSM-specific failures. +*/ +__bpf_kfunc int bpf_lsm_policy_apply_bprm(struct lsm_policy_object *object, +struct linux_binprm *bprm,u32 flags) +{ +struct lsm_static_call *scall; +struct mem_cgroup *old_memcg; +int err; + +lsm_for_each_hook (scall, bprm_apply_policy_object) { +if (scall->hl->lsmid->id != object->lsmid) +continue ; +/* +* The hook runs on behalf of the BPF program, not of the +* mediated task: charge its allocations to the root memcg. +*/ + old_memcg =set_active_memcg (root_mem_cgroup);
Quoted 8 lines
security/bpf_lsm_kfuncs.c bpf_lsm_policy_acquire(struct lsm_policy_object *object) · line 96
Quoted 6 lines
security/bpf_lsm_kfuncs.c +6
-0+ err = scall->hl->hook.bprm_apply_policy_object (bprm, object, + flags); +set_active_memcg (old_memcg); +return err; + } +return -EOPNOTSUPP ;Nack.
It was hard to tell why you Nack'd this patch in particular from the context.
If you wouldn't mind, providing some guidance as to why the code is unacceptable in the current form would give a direction to work towards...
(This is for the kfunc placement in security/ I assume?)
Thanks,
Justin
PATCH v3 8/15 lsm: Document the LSM policy object interface
In reply to Justin SuessDescribe the split of responsibilities in lsm-development.rst: the LSM framework owns the BPF-facing kfuncs, an LSM opts in by embedding struct lsm_policy_object, tagged with its lsmid and an LSM-private type, and implementing ordinary LSM hooks, and the lifetime contract those hooks must satisfy comes from BPF's execution model.
---
Notes:
v2->v3:
- No change.
Documentation/security/lsm-development.rst | 49 ++++++++++++++++++++++ 1 file changed, 49 insertions(+)
Documentation/security/lsm-development.rst +49 -0
@@ -15,3 +15,52 @@ see ``security/security.c`` and associated structures: .. kernel-doc:: security/security.c :export: + +LSM policy objects and BPF kfuncs +================================= + +The LSM framework implements an interface for individual LSMs to +expose their policy through BPF kfuncs and kptrs. An LSM may not +export any kfunc or other BPF interface directly. + +An LSM opts in by embedding ``struct lsm_policy_object`` in one of +its own objects, setting its ``lsmid`` to the LSM's ``LSM_ID_*`` +value and its ``type`` to a nonzero value of the LSM's choosing, and +implementing the policy object hooks (``policy_object_from_fd``, +``policy_object_get``, ``policy_object_put``, and per-operation hooks +such as ``bprm_apply_policy_object``) like any other hook, resolving +the containing object with ``container_of()``. The ``type`` namespace +is private to the owning LSM, which uses it to tell its own policy +object kinds apart; the framework never interprets it, and 0 is +reserved as "unset". + +The kfuncs, defined once in ``security/bpf_lsm_kfuncs.c``, dispatch +each call on an object to the single LSM matching its ``lsmid``. The +fd translation has no object yet: the framework offers the fd to +every ``policy_object_from_fd`` implementation in turn, and an LSM +declines a fd that is not one of its own with ``-EOPNOTSUPP``; any +other error fails the translation. A program that expects a policy of +a specific LSM can read the returned object's ``lsmid``. Either way, +a BPF program reaches an LSM the same way every other kernel caller +does, through an LSM hook, while the BPF verifier tracks the object +as a referenced kptr. + +An LSM opting in must satisfy the lifetime contract that BPF's +execution model imposes: the containing object is reference counted, +``policy_object_get`` acquires with inc-not-zero semantics and fails +once the count dropped to zero, ``policy_object_put`` may be called +from contexts that cannot sleep (map destructors), and the object's +memory is freed only after an RCU grace period, as programs load +policy object kptrs from BPF maps under RCU. Hooks for operations an +LSM does not provide are simply not implemented: the corresponding +kfunc then fails with ``-EOPNOTSUPP`` at runtime. Whether the LSM +providing an operation is built in and active is likewise a runtime +property: the kfuncs are always registered when ``CONFIG_BPF_LSM`` is +enabled, so BPF program loading is independent of the boot-time LSM +configuration. + +.. kernel-doc:: security/bpf_lsm_kfuncs.c + :identifiers: bpf_lsm_policy_acquire + bpf_lsm_policy_apply_bprm + bpf_lsm_policy_from_fd + bpf_lsm_policy_release
Signature · 1 line
2.55.0
PATCH v3 9/15 selftests/bpf: Add tests for the LSM policy object kfuncs
In reply to Justin SuessTest the properties of the policy object interface that hold independently of any LSM implementing the hooks.
The failure programs pin down the verifier-side contract: the kfuncs are rejected in tracing programs, the fd kfunc in LSM programs, the apply kfunc in syscall programs, on non-bprm LSM hooks and in non-sleepable programs, leaked references fail verification, and a kptr loaded outside an RCU read-side section cannot be acquired.
The syscall program checks the runtime contract of bpf_lsm_policy_from_fd(): a bad fd, a fd that is no LSM's policy object, and a nonzero value of the reserved flags all resolve to NULL.
Exercising the kfuncs against an LSM actually providing policy objects is left to that LSM's own tests.
---
Notes:
v2->v3:
- No change.
.../bpf/prog_tests/lsm_policy_kfuncs.c | 54 ++++++ .../selftests/bpf/progs/lsm_policy_kfuncs.c | 52 ++++++ .../bpf/progs/lsm_policy_kfuncs_failure.c | 154 ++++++++++++++++++ 3 files changed, 260 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c
tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c new file +54 -0
@@ -0,0 +1,54 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright © 2026 Justin Suess <[email protected]> */ + +#include <test_progs.h> +#include <stdlib.h> +#include <unistd.h> + +#include "lsm_policy_kfuncs.skel.h" +#include "lsm_policy_kfuncs_failure.skel.h" + +/* +* Runtime contract of bpf_lsm_policy_from_fd(), independent of any +* LSM implementing the policy object hooks: a bad fd, a fd that is no +* LSM's policy object, and a nonzero value of the reserved flags all +* resolve to NULL. +*/ +static void test_from_fd_null (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct lsm_policy_kfuncs *skel; +char tmp_path[] ="/tmp/lsm_policy_kfuncs_XXXXXX" ; +int tmp_fd, err; + + tmp_fd =mkstemp (tmp_path); +if (!ASSERT_GE (tmp_fd,0 ,"mkstemp" )) +return ; + + skel =lsm_policy_kfuncs__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) + goto out_close; + skel->bss->plain_fd = tmp_fd; + + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.check_from_fd), + &opts); +if (!ASSERT_OK (err,"check_from_fd_run" ) || + !ASSERT_OK (opts.retval,"check_from_fd_retval" )) + goto out_destroy; + +ASSERT_TRUE (skel->bss->got_null_for_bad_fd,"bad_fd_null" ); +ASSERT_TRUE (skel->bss->got_null_for_plain_fd,"plain_fd_null" ); +ASSERT_TRUE (skel->bss->got_null_for_bad_flags,"bad_flags_null" ); +out_destroy: +lsm_policy_kfuncs__destroy (skel); +out_close: +close (tmp_fd); +unlink (tmp_path); +} + +void test_lsm_policy_kfuncs (void ) +{ +if (test__start_subtest ("from_fd_null" )) +test_from_fd_null (); +RUN_TESTS (lsm_policy_kfuncs_failure); +}
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs.c new file +52 -0
52 lines not on this page · raw message
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c new file +154 -0
154 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 09/15] selftests/bpf: Add tests for the LSM policy object kfuncs
A subsystem pattern flags this as potentially concerning: the "bad_flags_null" assertion passes whether or not the kernel honors the reserved-flags contract.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c new file +0
-0@@ -0,0 +1,54 @@
[ ... ]
Quoted 15 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c new file +15
-0+static void test_from_fd_null (void ) +{ +LIBBPF_OPTS (bpf_test_run_opts, opts); +struct lsm_policy_kfuncs *skel; +char tmp_path[] ="/tmp/lsm_policy_kfuncs_XXXXXX" ; +int tmp_fd, err; + + tmp_fd =mkstemp (tmp_path); +if (!ASSERT_GE (tmp_fd,0 ,"mkstemp" )) +return ; + + skel =lsm_policy_kfuncs__open_and_load (); +if (!ASSERT_OK_PTR (skel,"skel_open_and_load" )) + goto out_close; + skel->bss->plain_fd = tmp_fd;
tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c line 41
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_kfuncs.c new file +10
-0+ + err =bpf_prog_test_run_opts (bpf_program__fd (skel->progs.check_from_fd), + &opts); +if (!ASSERT_OK (err,"check_from_fd_run" ) || + !ASSERT_OK (opts.retval,"check_from_fd_retval" )) + goto out_destroy; + +ASSERT_TRUE (skel->bss->got_null_for_bad_fd,"bad_fd_null" ); +ASSERT_TRUE (skel->bss->got_null_for_plain_fd,"plain_fd_null" ); +ASSERT_TRUE (skel->bss->got_null_for_bad_flags,"bad_flags_null" );
^^^^^^^^^^^^^^^^
A subsystem pattern flags this as potentially concerning: the "bad_flags_null" assertion passes whether or not the kernel honors the reserved-flags contract.
The fd used for the flags test is the plain temporary file created above, which no LSM claims. In bpf_lsm_policy_from_fd() (security/bpf_lsm_kfuncs.c) there are two independent paths to NULL for this input:
if (flags)
return NULL; /* the check under test */
lsm_for_each_hook(scall, policy_object_from_fd) {
err = scall->hl->hook.policy_object_from_fd(fd, &object);
if (err == -EOPNOTSUPP)
continue;
...
}
return NULL; /* unrelated: nobody claims a /tmp file */Because the /tmp file is claimed by no LSM, the loop falls through to the trailing return NULL on its own. Delete the if (flags) guard entirely and "bad_flags_null" still passes. The assertion therefore holds whether or not the kernel implements the reserved-flags contract that the commit message claims this test verifies.
The other two arms do discriminate once an LSM implements the hook: for fd == -1 the owning LSM's policy_object_from_fd() fails with a non-EOPNOTSUPP error, and for a plain fd the LSM must return -EOPNOTSUPP rather than claiming the file. Only the flags arm is unconditioned.
A discriminating flags test needs an fd that would resolve successfully with flags == 0, which by design belongs in the LSM-specific test rather than here. Could the commit message and comment clarify that the reserved-flags contract is only partially covered here, or could this test be deferred to an LSM that provides real policy objects?
Quoted 6 lines
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c new file +0
-0@@ -0,0 +1,154 @@
[ ... ]
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c line 56
Quoted 7 lines
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c new file +7
-0+/* +* The fd kfunc is exclusive to syscall programs: it must be rejected +* in an LSM program, even on an allowed hook. +*/ +SEC("lsm.s/bprm_creds_for_exec") +__failure __msg("calling kernel function bpf_lsm_policy_from_fd is not allowed") +int BPF_PROG (lsm_get ,struct linux_binprm *bprm)
^^^^^^^^
[ ... ]
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c line 72
Quoted 7 lines
tools/testing/selftests/bpf/progs/lsm_policy_kfuncs_failure.c new file +7
-0+/* +* The enforcement kfunc is exclusive to the sleepable bprm LSM +* hooks: it must be rejected in a syscall program. +*/ +SEC("syscall") +__failure __msg("calling kernel function bpf_lsm_policy_apply_bprm is not allowed") +int syscall_restrict (void *ctx)
^^^^^^^^^^^^^^^
This isn't a bug, but six of the eight programs in this file are named after the condition they pin down (tracing_prog, wrong_hook, nonsleepable_prog, leak_policy, acquire_untrusted, leak_shared_policy), which makes a RUN_TESTS failure line self-explanatory. The other two are named after the program type plus an operation that doesn't appear in the
interface: lsm_get and syscall_restrict. There is no 'get' or 'restrict' kfunc. Would names in the same style as the neighbours - e.g. lsm_from_fd and syscall_apply_bprm - make the failing subtest easier to place from the RUN_TESTS output?
---
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/34398515657
PATCH v3 10/15 landlock: Expose the ruleset fd lookup to the rest of Landlock
In reply to Justin SuessRename 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.
---
Notes:
v2->v3:
- No change.
security/landlock/ruleset.h | 3 +++ security/landlock/syscalls.c | 9 +++++---- 2 files changed, 8 insertions(+), 4 deletions(-)
security/landlock/ruleset.h +3 -0
@@ -214,6 +214,9 @@ int landlock_store_rule(struct landlock_rules *const rules,void landlock_free_rules (struct landlock_rules *const rules); +struct landlock_ruleset *landlock_get_ruleset_from_fd (const int fd, +const fmode_t mode); +/** * landlock_get_rule_root - Get the root of a rule tree by key type *
security/landlock/syscalls.c +5 -4
@@ -305,8 +305,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;
@@ -486,7 +486,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);
@@ -585,7 +585,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.55.0
PATCH v3 11/15 landlock: Factor the credential restriction out of landlock_restrict_self()
In reply to Justin SuessSplit 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 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 merge runs under @ruleset->lock and the landlock_create_domain trace event is emitted before the lock is released, exactly as in the syscall before this change: the event still observes the ruleset snapshot that was merged, and it still fires before any thread-sync wait. Committing the hierarchy out of LANDLOCK_LOG_UNCOMMITTED moves along with it, so every domain computed by landlock_prepare_restriction() has its create/free trace events balanced, whether or not it ends up enforced.
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, the TSYNC handling, and the landlock_enforce_domain trace event 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 log configuration in one shared place.
The restriction records the flags it was computed with instead of translating them into per-flag fields: consumers read the staged flags at application time, so a future flag that must be honored at enforcement travels with the restriction automatically, with no per-flag plumbing in the callers.
---
Notes:
v2->v3:
- No change.
security/landlock/cred.c | 132 +++++++++++++++++++++++++++++++++++ security/landlock/cred.h | 31 ++++++++ security/landlock/syscalls.c | 96 ++++--------------------- 3 files changed, 178 insertions(+), 81 deletions(-)
security/landlock/cred.c +132 -0
@@ -8,14 +8,146 @@*/ #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 <linux/mutex.h> +#include <uapi/linux/landlock.h> #include "common.h" #include "cred.h" +#include "domain.h" #include "ruleset.h" #include "setup.h" +#include <trace/events/landlock.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) +{ +struct landlock_domain *new_dom; +#ifdef CONFIG_SECURITY_LANDLOCK_LOG +/* 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 prev_log_subdomains = !llcred->log_subdomains_off; +#endif /* CONFIG_SECURITY_LANDLOCK_LOG */ + + *restriction = (struct landlock_restriction ){ + .flags = flags, + }; + +if (!ruleset) +return 0 ; + +mutex_lock (&ruleset->lock); + new_dom =landlock_merge_ruleset (llcred->domain, ruleset); +if (IS_ERR (new_dom)) { +mutex_unlock (&ruleset->lock); +return PTR_ERR (new_dom); + } +/* +* Emits the domain-creation event while @ruleset->lock is still +* held, right after the merge, so an eBPF program attached to +* the tracepoint reads the exact ruleset that was merged into +* the domain: a consistent snapshot that a concurrent +* landlock_add_rule() (which holds the same lock) cannot +* modify. +* +* This must not be delayed past the return of this function. +* Holding @ruleset->lock across +* landlock_restrict_sibling_threads() would hang: a sibling +* thread blocked in landlock_add_rule() on the same +* @ruleset->lock cannot run the task_work that thread-sync +* waits for (the lock wait is uninterruptible). Emitting here +* keeps the lock off the thread-sync path. +* +* The trade-off is that the event fires for a domain that may +* never be enforced: a later (rare) thread-sync failure or an +* aborted execution drops it. Those paths free the domain, +* which emits the matching free_domain event so the create/free +* pair stays balanced. +*/ +trace_landlock_create_domain (new_dom, ruleset); +mutex_unlock (&ruleset->lock); + +#ifdef CONFIG_SECURITY_LANDLOCK_LOG + new_dom->hierarchy->log_same_exec = log_same_exec; + new_dom->hierarchy->log_new_exec = log_new_exec; +/* +* The creation event fired above, so move the domain out of +* LANDLOCK_LOG_UNCOMMITTED: its free_domain event must fire +* too, even if the domain is dropped before being enforced. +* Audit logging may still be disabled (DISABLED); tracing +* observes it anyway. +*/ +if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains) + new_dom->hierarchy->log_status =LANDLOCK_LOG_DISABLED ; +else + new_dom->hierarchy->log_status =LANDLOCK_LOG_PENDING ; +#endif /* CONFIG_SECURITY_LANDLOCK_LOG */ + + restriction->domain = new_dom; +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_SECURITY_LANDLOCK_LOG +if (restriction->flags &LANDLOCK_RESTRICT_SELF_LOG_SUBDOMAINS_OFF ) + llcred->log_subdomains_off = true; +#endif /* CONFIG_SECURITY_LANDLOCK_LOG */ + +if (!restriction->domain) +return ; + +/* Replaces the old domain. */ +landlock_put_domain (llcred->domain); + llcred->domain = restriction->domain; + restriction->domain =NULL ; + +#ifdef CONFIG_SECURITY_LANDLOCK_LOG + llcred->domain_exec |=BIT (llcred->domain->num_layers -1 ); +#endif /* CONFIG_SECURITY_LANDLOCK_LOG */ +} +static void hook_cred_transfer (struct cred *const new,const struct cred *const old) {
security/landlock/cred.h +31 -0
@@ -21,6 +21,29 @@#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_domain *domain; +/** +* @flags: landlock_restrict_self(2) flags the restriction was +* computed with, validated by the caller. +*/ +u32 flags; +}; +/** * struct landlock_cred_security - Credential security blob *
@@ -152,6 +175,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 +15 -81
137 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 11/15] landlock: Factor the credential restriction out of landlock_restrict_self()
This isn't a bug, but is <linux/errno.h> needed here? The new code returns PTR_ERR(new_dom) (from <linux/err.h>) and never names an -Exxx constant.
security/landlock/cred.c line 14
Quoted 12 lines
security/landlock/cred.c +3
-0@@ -8,14 +8,146 @@*/ #include <linux/binfmts.h> +#include <linux/bits.h> #include <linux/cred.h> +#include <linux/err.h> +#include <linux/errno.h>
This isn't a bug, but is <linux/errno.h> needed here? The new code returns PTR_ERR(new_dom) (from <linux/err.h>) and never names an -Exxx constant.
Quoted 35 lines
security/landlock/cred.c +29
-0+#include <linux/lsm_hooks.h> +#include <linux/mutex.h> +#include <uapi/linux/landlock.h> #include "common.h" #include "cred.h" +#include "domain.h" #include "ruleset.h" #include "setup.h" +#include <trace/events/landlock.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) +{
[ ... ]
Quoted 5 lines
security/landlock/cred.c +5
-0+#ifdef CONFIG_SECURITY_LANDLOCK_LOG + new_dom->hierarchy->log_same_exec = log_same_exec; + new_dom->hierarchy->log_new_exec = log_new_exec; +/* +* The creation event fired above, so move the domain out of
security/landlock/cred.c line 112
Quoted 10 lines
security/landlock/cred.c +10
-0+* LANDLOCK_LOG_UNCOMMITTED: its free_domain event must fire +* too, even if the domain is dropped before being enforced. +* Audit logging may still be disabled (DISABLED); tracing +* observes it anyway. +*/ +if ((!log_same_exec && !log_new_exec) || !prev_log_subdomains) + new_dom->hierarchy->log_status =LANDLOCK_LOG_DISABLED ; +else + new_dom->hierarchy->log_status =LANDLOCK_LOG_PENDING ; +#endif /* CONFIG_SECURITY_LANDLOCK_LOG */
The LANDLOCK_LOG_UNCOMMITTED -> PENDING/DISABLED transition moves from sys_landlock_restrict_self() into landlock_prepare_restriction(), but comments that document this contract by naming the old site remain unrevisioned, and no later commit in the 02756181d382..1cc3305917e4 range updates them either.
In security/landlock/domain.c (landlock_init_hierarchy_log):
/*
* The hierarchy is born unobservable: landlock_restrict_self() moves it
* out of LANDLOCK_LOG_UNCOMMITTED once it has emitted the creation
* event, so the matching free_domain event fires for it and not for a
* hierarchy whose creation was never observed.
*/
In security/landlock/trace.c (landlock_trace_free_domain):
* Fires only for a hierarchy whose creation event was emitted, i.e. one that
* left LANDLOCK_LOG_UNCOMMITTED in landlock_restrict_self(). ...
* while a domain that landlock_restrict_self() created and a
* thread-sync failure then aborted still fires free_domain, ...
In include/trace/events/landlock.h (Lifecycle consistency block):
The window is still described as 'A rare thread-sync failure aborts the just-created domain' even though the new helper's documentation (cred.c:89-93) states it also covers an aborted execution, introduced two commits later in 0a88c757f4f0.
After this commit, landlock_prepare_restriction() performs the transition, and 0a88c757f4f0 adds a second, non-syscall caller (bprm_apply_policy_object()), so 'landlock_restrict_self()' no longer enforces this invariant nor provides the only path that can create and drop a domain.
Behaviour is unaffected, but should these references be updated to reflect the new code structure?
Quoted 4 lines
security/landlock/cred.c +4
-0+ + restriction->domain = new_dom; +return 0 ; +}
[ ... ]
Quoted 12 lines
security/landlock/syscalls.c +0
-1@@ -9,7 +9,6 @@#include <asm/current.h> #include <linux/anon_inodes.h> -#include <linux/bitops.h> #include <linux/build_bug.h> #include <linux/capability.h> #include <linux/cleanup.h>
[ ... ]
security/landlock/syscalls.c SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, · line 546
Quoted 4 lines
security/landlock/syscalls.c +0
-0@@ -546,10 +544,9 @@ SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, struct landlock_ruleset *ruleset __free(landlock_put_ruleset) = NULL;struct landlock_domain *new_dom =NULL ;struct cred *new_cred;
This isn't a bug, but is the = NULL initializer still needed here? Before the refactor, new_dom was only assigned inside the if (ruleset) block, so the initializer was load-bearing for the later if (ruleset) trace_landlock_enforce_domain(new_dom, ...) call. After the split, new_dom is assigned unconditionally from restriction.domain, and every path between the declaration and that assignment is a plain return. It is the only = NULL pointer initializer left in the file apart from the __free() declaration on the line above.
security/landlock/syscalls.c SYSCALL_DEFINE2(landlock_restrict_self, const int, ruleset_fd, const __u32, · line 549
Quoted 6 lines
security/landlock/syscalls.c +2
-3-struct landlock_cred_security *new_llcred; +struct landlock_restriction restriction;bool process_wide; -bool __maybe_unused log_same_exec, log_new_exec, log_subdomains, - prev_log_subdomains; +int err;
[ ... ]
---
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/34398515657
PATCH v3 12/15 landlock: Free rulesets after an RCU grace period
In reply to Justin SuessDefer every ruleset free behind an RCU grace period, and keep the fields that stay readable while a free is pending out of the union that overlays the deferred-free work item.
The policy_object_get LSM hook lets a caller holding only an RCU-protected pointer to a ruleset (e.g. loaded from a BPF map kptr field under rcu_read_lock()) race a refcount_inc_not_zero() against the drop of the last reference. For that to be sound, the ruleset's memory, and its reference count in particular, must remain valid until every RCU reader that could still observe the pointer is done:
free the ruleset through queue_rcu_work(), which waits for a grace period before running the free work.
The work item is overlaid with the fields that no one may touch once @usage reaches zero: @lock, @quiet_masks and @handled_masks. @usage itself stays outside the union so a racing reader observes zero instead of the work item's bytes, and the tracing fields @version and @id stay outside too because the landlock_free_ruleset trace event reads them when the queued work finally runs.
Since queueing the work never sleeps, the might_sleep() annotation is dropped: a following commit releases ruleset references from BPF object destructors that cannot sleep.
---
Notes:
v2->v3:
- No change.
security/landlock/ruleset.c | 24 ++++++++++++++--- security/landlock/ruleset.h | 54 ++++++++++++++++++++++++------------- 2 files changed, 57 insertions(+), 21 deletions(-)
security/landlock/ruleset.c +21 -3
@@ -21,6 +21,7 @@#include <linux/refcount.h> #include <linux/slab.h> #include <linux/spinlock.h> +#include <linux/workqueue.h> #include <uapi/linux/landlock.h> #include "access.h"
@@ -346,9 +347,26 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)kfree (ruleset); } +static void free_ruleset_work (struct work_struct *const work) +{ +struct landlock_ruleset *ruleset; + + ruleset = container_of(to_rcu_work(work), struct landlock_ruleset, + work_free); +free_ruleset (ruleset); +} + +/* +* RCU readers (cf. the policy_object_get LSM hook) may call +* refcount_inc_not_zero() on a ruleset they hold no reference to: the memory +* must survive a grace period after the last put. Queueing the free also +* makes this callable from contexts that cannot sleep (cf. the +* policy_object_put LSM hook). +*/ void landlock_put_ruleset (struct landlock_ruleset *const ruleset) { -might_sleep (); -if (ruleset &&refcount_dec_and_test (&ruleset->usage)) -free_ruleset (ruleset); +if (ruleset &&refcount_dec_and_test (&ruleset->usage)) { +INIT_RCU_WORK (&ruleset->work_free, free_ruleset_work); +queue_rcu_work (system_dfl_wq, &ruleset->work_free); + } }
security/landlock/ruleset.h +36 -18
76 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 12/15] landlock: Free rulesets after an RCU grace period
Deferring the free moves the landlock_free_ruleset tracepoint out of the caller's context: trace_landlock_free_ruleset() is emitted from free_ruleset(), which after this patch only runs from…
Quoted 4 lines
security/landlock/ruleset.c +0
-0
[ ... ]
Quoted 22 lines
security/landlock/ruleset.c +15
-2@@ -346,9 +347,26 @@ static void free_ruleset(struct landlock_ruleset *const ruleset)kfree (ruleset); } -void landlock_put_ruleset (struct landlock_ruleset *const ruleset) +static void free_ruleset_work (struct work_struct *const work) { -might_sleep (); +struct rcu_work *const rcu_work =to_rcu_work (work); +struct landlock_ruleset *const ruleset = + container_of(rcu_work, struct landlock_ruleset, work_free); + +free_ruleset (ruleset); +} + +/* +* landlock_put_ruleset - Release ruleset reference +* +* @ruleset: Ruleset to release, or NULL. +* +* Release a reference. When the last reference is dropped, the ruleset is +* freed after an RCU grace period.
security/landlock/ruleset.c static void free_ruleset(struct landlock_ruleset *const ruleset)
Quoted 10 lines
security/landlock/ruleset.c +5
-2+*/ +void landlock_put_ruleset (struct landlock_ruleset *const ruleset) +{if (ruleset &&refcount_dec_and_test (&ruleset->usage)) { -trace_landlock_free_ruleset (ruleset); -free_ruleset (ruleset); +INIT_RCU_WORK (&ruleset->work_free, free_ruleset_work); +queue_rcu_work (system_dfl_wq, &ruleset->work_free); } }
Deferring the free moves the landlock_free_ruleset tracepoint out of the caller's context: trace_landlock_free_ruleset() is emitted from free_ruleset(), which after this patch only runs from free_ruleset_work() on a kworker, an RCU grace period after the last put.
That changes an observable, in-tree-tested property of the tracepoint, and the patch does not update the test that asserts the old behaviour, so TEST_F(trace, free_ruleset_on_close) in tools/testing/selftests/landlock/trace_test.c now fails deterministically for three independent reasons:
1. PID filtering drops the event entirely. The fixture calls tracefs_set_pid_filter(getpid()), which writes set_event_pid. ftrace records an event only when the *current* task's pid is in that list. The freeing kworker is a kthread forked from kthreadd, so neither set_event_pid nor options/event-fork ever adds it, and the landlock_free_ruleset record is discarded. The existing free_domain tests handle exactly this by clearing the PID filter before triggering the deferred free.
2. No wait for the grace period + workqueue. The test reads the buffer synchronously right after close(ruleset_fd):
close(ruleset_fd);
buf = tracefs_read_buf();The free_domain tests instead poll (for (int retry = 0; retry < 10; retry++) { usleep(100000); ... }) precisely because the event now arrives later.
3. Wrong task regex. The assertion uses REGEX_FREE_RULESET(TRACE_TASK), but the record, if it were captured at all, carries a kworker name. trace.h already defines KWORKER_TASK for exactly this case.
Net effect: EXPECT_EQ(1, tracefs_count_matches(buf, REGEX_FREE_RULESET(TRACE_TASK))) sees 0. The selftest needs the same treatment the free_domain tests already got (clear the PID filter, poll, match KWORKER_TASK), otherwise the landlock kselftest suite regresses with this commit.
---
The tracepoint documentation for landlock_free_ruleset is now stale and actively misleading for tracer authors. include/trace/events/landlock.h still says the event is "Emitted when a ruleset's last reference is dropped (typically when the creating process closes the ruleset file descriptor)", which after this patch is no longer the emission point: the last put only queues rcu_work, and the event fires from a kworker one RCU grace period later. Documentation/trace/events-landlock.rst likewise lists only "``landlock_free_ruleset``: a ruleset is freed".
The tree already establishes the convention of spelling this out for the sibling event: the landlock_free_domain kernel-doc states "A committed domain is freed from a kworker via landlock_put_domain_deferred() ... so the current task is not the sandboxed task that triggered the free", and Documentation/trace/events-landlock.rst repeats it with a kworker example line. landlock_free_ruleset now has the same property and neither document was updated, so consumers keep assuming the event is attributable to the task that closed the fd (which is precisely the assumption the in-tree selftest encodes).
Quoted 4 lines
security/landlock/ruleset.h +0
-0
[ ... ]
Quoted 47 lines
security/landlock/ruleset.h +26
-15@@ -175,22 +174,41 @@ struct landlock_ruleset {/** * @usage: Number of alive &struct landlock_cred pointing to this * ruleset and also the reference counter for user space. This must -* remain valid while the object can be retrieved from an RCU -* protected pointer. +* remain readable while the object can be retrieved from an RCU +* protected pointer. */ refcount_t usage; -/** -* @lock: Guards modifications to the ruleset. Must not be taken under -* rcu_read_lock, but after refcount_inc_not_zero has succeeded. An -* RCU reader that needs the lock should bail (e.g., return -EAGAIN to -* user space) instead of trying to take it. -*/ -struct mutex lock; -/** -* @quiet_masks: Access rights to skip logging for, per layer level. -*/ -struct access_masks quiet_masks; -/** @handled_masks: Access rights supported by the ruleset. */ -struct access_masks handled_masks; +union { +/** +* @work_free: Scheduled via queue_rcu_work when @usage +* drops to zero. By the time this work item runs, every +* RCU reader that could still observe a pointer to the +* containing struct landlock_ruleset is guaranteed to be +* done. (INIT_RCU_WORK initializes both the inner +* work_struct and the surrounding rcu_work). +* The fields @lock, @quiet_masks and @handled_masks are +* then unused, so overlay this work item with them to +* keep sizeof(struct landlock_ruleset) down. +*/ +struct rcu_work work_free; +struct { +/** +* @lock: Guards modifications to the ruleset. Must not +* be taken under rcu_read_lock, but after +* refcount_inc_not_zero has succeeded. An RCU reader +* that needs the lock should bail (e.g., return -EAGAIN +* to user space) instead of trying to take it. +*/ +struct mutex lock; +/** +* @quiet_masks: Access rights to skip logging for, per
security/landlock/ruleset.h struct landlock_ruleset { · line 214
Quoted 10 lines
security/landlock/ruleset.h +10
-0+* layer level. +*/ +struct access_masks quiet_masks; +/** +* @handled_masks: Access rights supported by the +* ruleset. +*/ +struct access_masks handled_masks; + }; + };
The union makes @lock, @quiet_masks and @handled_masks alias the queued rcu_work, and trace_landlock_free_ruleset() is emitted from free_ruleset() *while that work item is executing*, so at trace time those three fields hold workqueue bookkeeping bytes, not ruleset state.
That matters because include/trace/events/landlock.h explicitly advertises reading the traced pointer's fields beyond TP_STRUCT__entry:
"eBPF programs read these pointers via BTF for richer introspection than the TP_STRUCT__entry fields". A tracer that pairs landlock_create_ruleset (whose TP_STRUCT__entry reads handled_masks.fs/net/scope) with landlock_free_ruleset and reads ruleset->handled_masks or ruleset->quiet_masks via BTF gets rcu_work contents. The commit message and the @work_free kernel-doc justify the overlay with "The fields @lock, @quiet_masks and @handled_masks are then unused", which holds for in-kernel code but not for the BTF field access the header sanctions.
There is no in-tree precedent that would make this acceptable by analogy:
struct landlock_domain uses the same overlay trick but free_domain() emits no tracepoint on the unioned object -- trace_landlock_free_domain() is passed a struct landlock_hierarchy, which has no union. Either the overlay should exclude the fields the trace interface exposes, or the "Pointer access" documentation should state that only @usage, @version and @id are valid at landlock_free_ruleset time.
---
Quoted 22 lines
landlock: Free rulesets after an RCU grace period
Defer every ruleset free behind an RCU grace period, and keep the fields that stay readable while a free is pending out of the union that overlays the deferred-free work item.
Thepolicy_object_getLSM hook lets a caller holding only an RCU-protected pointer to a ruleset (e.g. loaded from a BPF map kptr field underrcu_read_lock()) race arefcount_inc_not_zero()against the drop of the last reference. For that to be sound, the ruleset's memory, and its reference count in particular, must remain valid until every RCU reader that could still observe the pointer is done:
free the ruleset throughqueue_rcu_work(), which waits for a grace period before running the free work.
The work item is overlaid with the fields that no one may touch once @usage reaches zero: @lock, @quiet_masks and @handled_masks. @usage itself stays outside the union so a racing reader observes zero instead of the work item's bytes, and the tracing fields @version and @id stay outside too because thelandlock_free_rulesettrace event reads them when the queued work finally runs.
Since queueing the work never sleeps, themight_sleep()annotation is dropped: a following commit releases ruleset references from BPF object destructors that cannot sleep.
Quoted 2 lines
Cc Mickaël Salaün <[email protected]>Signed-off-by Justin Suess <[email protected]>
This looks like a bug fix for stack argument support. Should this include:
---
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/34398515657
PATCH v3 13/15 landlock: Implement the LSM policy object hooks
In reply to Justin SuessImplement the generic LSM hooks exposing Landlock rulesets as policy objects to BPF. The new code is gated on CONFIG_BPF_LSM, the only configuration where the kfuncs calling the hooks exist.
struct lsm_policy_object is embedded in struct landlock_ruleset and tagged with LSM_ID_LANDLOCK and LANDLOCK_POLICY_TYPE_RULESET at ruleset creation; the hooks resolve the ruleset with container_of() and no Landlock type crosses the LSM boundary. The type namespace is private to Landlock and routes the container_of() resolution once several policy object types exist: the consuming hooks reject an object of an unexpected type with -EINVAL (the put hook, which cannot fail, warns instead), while from_fd only produces objects and needs no check. The embedded object sits outside the union overlaying the deferred-free work item: an RCU reader may still read its identity while a queued free waits out the grace period.
- policy_object_from_fd() translates a ruleset fd, created with landlock_create_ruleset(2) and populated with landlock_add_rule(2), into an owned ruleset reference, validated the same way as for the Landlock syscalls (ruleset file type, FMODE_CAN_READ). A fd referring to a file that is not a Landlock ruleset is declined with -EOPNOTSUPP instead of the syscall's -EBADFD: it may be another LSM's policy object, and the decline lets the framework offer it to the LSM it belongs to.
- policy_object_put() releases such a reference. The hook may be reached from BPF object destructors that cannot sleep, which is
fine: ruleset puts queue the free as RCU work.
- policy_object_get() acquires an additional reference for a caller that only holds an RCU-protected pointer, e.g. loaded from a BPF map kptr field under rcu_read_lock(). The reference is taken with refcount_inc_not_zero(); the racing reader's access to the ruleset memory is safe because rulesets are freed after an RCU grace period.
- bprm_apply_policy_object() shares the landlock_restrict_self(2)
path: it calls landlock_prepare_restriction() on the credentials prepared in the binprm and stages the computed restriction (the merged struct landlock_domain) in their Landlock blob. The flags are the landlock_restrict_self(2) flags; only LANDLOCK_RESTRICT_SELF_TSYNC is rejected with -EINVAL because the restriction targets the execution, not the calling threads.
LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS is honored, not ignored: the syscall implements it directly in landlock_restrict_self(), so the staged path reads it back from the staged restriction's flags and sets no_new_privs on the executing task at application time. The executed program then starts with no_new_privs set, binding it and all its descendants; the current execution's
privilege computation is unaffected, as the bprm credentials (including any setuid elevation) were computed before bprm_committing_creds().
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: an execution either starts confined by the domain or, if it fails earlier, leaves the calling task untouched. The applied layer is accounted in domain_exec so the LOG_SAME_EXEC and LOG_NEW_EXEC audit 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 application emits the landlock_enforce_domain trace event, keeping the event's invariant that every enforcement falls between the domain's create_domain and free_domain events. The process is single-threaded past de_thread(), so the single event concludes the operation with complete == 1 and process_wide == 1. no_new_privs reports the post-flag state; on this path a value of 0 carries no authorization meaning and only tells the observer that the confined program can still elevate through future setuid execs, which the event's documentation now spells out.
The staged restriction's lifetime is fully covered: a second bprm_apply_policy_object() 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(), and the application clears the staged domain so committed task credentials never carry one. landlock_cred_copy(), now also used by hook_cred_transfer(), upholds that invariant by never copying a staged restriction.
---
Notes:
v2->v3:
- No change.
include/trace/events/landlock.h | 15 +++- security/landlock/Makefile | 2 + security/landlock/bpf.c | 152 ++++++++++++++++++++++++++++++++ security/landlock/bpf.h | 21 +++++ security/landlock/cred.c | 16 ++-- security/landlock/cred.h | 16 ++++ security/landlock/limits.h | 4 + security/landlock/ruleset.c | 6 ++ security/landlock/ruleset.h | 22 +++++ security/landlock/setup.c | 2 + 10 files changed, 245 insertions(+), 11 deletions(-) create mode 100644 security/landlock/bpf.c create mode 100644 security/landlock/bpf.h
include/trace/events/landlock.h +12 -3
@@ -500,13 +500,22 @@ TRACE_EVENT(landlock_create_domain,* enforcement time: 1 if set (by a prior * :manpage:`prctl(2)` %PR_SET_NO_NEW_PRIVS or by * %LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS), 0 if the domain -* was enforced with %CAP_SYS_ADMIN instead. +* was enforced with %CAP_SYS_ADMIN instead, or without +* either precondition on the BPF exec path (see below). * * Emitted for each thread sys_landlock_restrict_self() enforces the * domain on, in that thread's own context, right after its * commit_creds(), so it fires only once the thread is irreversibly -* enforcing the domain (aborted operations emit none). Not -* balanced; every enforcement falls between the domain's +* enforcing the domain (aborted operations emit none). Also emitted +* at execve(2)'s point of no return when a BPF-staged policy object +* is applied to the execution (see bpf_lsm_policy_apply_bprm()): the +* process is single-threaded after de_thread(), so the single event +* has @complete == 1 and @process_wide == 1. On that path, +* @no_new_privs == 0 carries no authorization meaning (the authority +* is the privilege to attach the BPF program, not +* no_new_privs/%CAP_SYS_ADMIN); it only tells the observer that the +* confined program can still elevate through future setuid execs. +* Not balanced; every enforcement falls between the domain's * landlock_create_domain and landlock_free_domain events. * * @complete == 1 && @process_wide == 1 means the whole process is
security/landlock/Makefile +2 -0
@@ -20,3 +20,5 @@ landlock-$(CONFIG_SECURITY_LANDLOCK_LOG) += \ landlock-$(CONFIG_AUDIT) += audit.o landlock-$(CONFIG_TRACEPOINTS) += trace.o + +landlock-$(CONFIG_BPF_LSM) += bpf.o
security/landlock/bpf.c new file +152 -0
152 lines not on this page · raw message
security/landlock/bpf.h new file +21 -0
@@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Landlock - LSM policy object 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 +8 -8
@@ -151,11 +151,7 @@ void landlock_apply_restriction(struct landlock_cred_security *const llcred,static void hook_cred_transfer (struct cred *const new,const struct cred *const old) { -const struct landlock_cred_security *const old_llcred = -landlock_cred (old); - -landlock_get_domain (old_llcred->domain); - *landlock_cred (new) = *old_llcred; +landlock_cred_copy (landlock_cred (new),landlock_cred (old)); } static int hook_cred_prepare(struct cred *const new,
@@ -167,10 +163,14 @@ static int hook_cred_prepare(struct cred *const new,static void hook_cred_free (struct cred *const cred) { -struct landlock_domain *const dom =landlock_cred (cred)->domain; +struct landlock_cred_security *const llcred =landlock_cred (cred); + +landlock_put_domain_deferred (llcred->domain); -if (dom) -landlock_put_domain_deferred (dom); +#ifdef CONFIG_BPF_LSM +/* Releases a restriction staged for an aborted execution. */ +landlock_put_domain_deferred (llcred->staged.domain); +#endif /* CONFIG_BPF_LSM */ }#ifdef CONFIG_SECURITY_LANDLOCK_LOG
security/landlock/cred.h +16 -0
@@ -59,6 +59,16 @@ struct landlock_cred_security {*/ struct landlock_domain *domain; +#ifdef CONFIG_BPF_LSM +/** +* @staged: Restriction staged by the bprm_apply_policy_object() hook, +* owning its domain reference, applied at bprm_committing_creds(). +* Only ever set on credentials prepared for an execution; committed +* task credentials never carry a staged restriction. +*/ +struct landlock_restriction staged; +#endif /* CONFIG_BPF_LSM */ +#ifdef CONFIG_SECURITY_LANDLOCK_LOG /** * @domain_exec: Bitmask identifying the domain layers that were enforced by
@@ -99,6 +109,12 @@ static inline void landlock_cred_copy(struct landlock_cred_security *dst, *dst = *src;landlock_get_domain (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_domain *landlock_get_current_domain (void )
security/landlock/limits.h +4 -0
@@ -37,6 +37,10 @@#define LANDLOCK_LAST_RESTRICT_SELF LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS#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 +6 -0
@@ -23,6 +23,7 @@#include <linux/spinlock.h> #include <linux/workqueue.h> #include <uapi/linux/landlock.h> +#include <uapi/linux/lsm.h> #include "access.h" #include "id.h"
@@ -51,6 +52,11 @@ landlock_create_ruleset(const access_mask_t fs_access_mask,mutex_init (&new_ruleset->lock); new_ruleset->rules.root_inode =RB_ROOT ; +#ifdef CONFIG_BPF_LSM + new_ruleset->policy_object.lsmid =LSM_ID_LANDLOCK ; + new_ruleset->policy_object.type =LANDLOCK_POLICY_TYPE_RULESET ; +#endif /* CONFIG_BPF_LSM */ +#if IS_ENABLED (CONFIG_INET ) new_ruleset->rules.root_net_port =RB_ROOT ;#endif /* IS_ENABLED(CONFIG_INET) */
security/landlock/ruleset.h +22 -0
@@ -15,6 +15,7 @@#include <linux/mutex.h> #include <linux/rbtree.h> #include <linux/refcount.h> +#include <linux/security.h> #include <linux/workqueue.h> #include "access.h"
@@ -146,6 +147,16 @@ struct landlock_rules {u32 num_rules; }; +#ifdef CONFIG_BPF_LSM +/* +* Landlock's lsm_policy_object types. The namespace is private to +* Landlock; 0 stays reserved as "unset". +*/ +enum landlock_policy_type { +LANDLOCK_POLICY_TYPE_RULESET =1 , +}; +#endif /* CONFIG_BPF_LSM */ +/** * struct landlock_ruleset - Landlock ruleset *
@@ -157,6 +168,17 @@ struct landlock_ruleset {* @rules: Red-black tree storage for rules. */ struct landlock_rules rules; + +#ifdef CONFIG_BPF_LSM +/** +* @policy_object: Identity under which the ruleset is handed out +* to BPF programs as a referenced kptr: the LSM policy kfuncs +* dispatch back to Landlock through its lsmid. Kept outside the +* union with @work_free: RCU readers may read its lsmid while a +* queued free waits out the grace period. +*/ +struct lsm_policy_object policy_object; +#endif /* CONFIG_BPF_LSM */ /** * @usage: Number of file descriptors referencing this ruleset. Kept * outside the union with @work_free: RCU readers may still call
security/landlock/setup.c +2 -0
@@ -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.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 13/15] landlock: Implement the LSM policy object hooks
Does adding the staged field here grow struct landlock_file_security for every open file?
Quoted 9 lines
security/landlock/cred.h +1
-0@@ -59,6 +59,16 @@ struct landlock_cred_security {*/ struct landlock_domain *domain; +#ifdef CONFIG_BPF_LSM
security/landlock/cred.h struct landlock_cred_security { · line 72
Quoted 10 lines
security/landlock/cred.h +9
-0+/** +* @staged: Restriction staged by the bprm_apply_policy_object() hook, +* owning its domain reference, applied at bprm_committing_creds(). +* Only ever set on credentials prepared for an execution; committed +* task credentials never carry a staged restriction. +*/ +struct landlock_restriction staged; +#endif /* CONFIG_BPF_LSM */ +#ifdef CONFIG_SECURITY_LANDLOCK_LOG
Does adding the staged field here grow struct landlock_file_security for every open file?
struct landlock_cred_security is embedded by value in the file blob:
security/landlock/fs.h:88-90:
struct landlock_cred_security fown_subject;
and security/landlock/setup.c sets .lbs_file = sizeof(struct landlock_file_security), so that blob is allocated for every struct file.
struct landlock_restriction is 16 bytes (domain pointer + u32 flags + tail padding), so adding it grows landlock_cred_security from 11 bytes (8+2+1) to 27 bytes (8+16+2+1), making struct landlock_file_security grow from 32 to 48 bytes (+50%) when CONFIG_BPF_LSM=y.
The struct's kernel-doc says "This structure is packed to minimize the size of struct landlock_file_security," but the new field is documented as "Only ever set on credentials prepared for an execution; committed task credentials never carry a staged restriction," so it is always zero in the file blob.
hook_file_set_fowner() at security/landlock/fs.c copies this blob with raw struct assignments, so the 16 exec-only bytes are copied on every fcntl(F_SETOWN) and socket-owner set. hook_file_free_security() only touches fown_subject.domain, never staged.
CONFIG_BPF_LSM=y is enabled by mainstream distributions alongside CONFIG_SECURITY_LANDLOCK, making this the common configuration. Could the exec-only staging state live in a separate structure that is not embedded in the per-file blob?
Quoted 4 lines
security/landlock/ruleset.h +0
-0
[ ... ]
Quoted 25 lines
security/landlock/ruleset.h +14
-0@@ -146,6 +147,16 @@ struct landlock_rules {u32 num_rules; }; +#ifdef CONFIG_BPF_LSM +/* +* Landlock's lsm_policy_object types. The namespace is private to +* Landlock; 0 stays reserved as "unset". +*/ +enum landlock_policy_type { +LANDLOCK_POLICY_TYPE_RULESET =1 , +}; +#endif /* CONFIG_BPF_LSM */ +/** * struct landlock_ruleset - Landlock ruleset * @@ -157,6 +168,17 @@ struct landlock_ruleset {* @rules: Red-black tree storage for rules. */ struct landlock_rules rules; + +#ifdef CONFIG_BPF_LSM +/** +* @policy_object: Identity under which the ruleset is handed out
security/landlock/ruleset.h struct landlock_ruleset { · line 184
Quoted 10 lines
security/landlock/ruleset.h +7
-0+* to BPF programs as a referenced kptr: the LSM policy kfuncs +* dispatch back to Landlock through its lsmid. Kept outside the +* union with @work_free: RCU readers may read its lsmid while a +* queued free waits out the grace period. +*/ +struct lsm_policy_object policy_object; +#endif /* CONFIG_BPF_LSM */ /** * @usage: Number of file descriptors referencing this ruleset. Kept * outside the union with @work_free: RCU readers may still call
Should the @usage documentation be updated?
The comment says "Number of file descriptors referencing this ruleset," but after this patch a reference can be held with no file descriptor involved at all.
hook_policy_object_from_fd() calls landlock_get_ruleset_from_fd() to increment @usage, then the BPF program parks that owned reference in a map kptr field with bpf_kptr_xchg(). Userspace can close the ruleset fd afterwards, leaving @usage at 1 with zero file descriptors referencing the ruleset. hook_policy_object_get() also increments the refcount for an RCU reader holding no fd.
The rest of this struct's documentation is maintained precisely (three members carry "Kept outside the union with @work_free" rationales), so the stale enumeration stands out.
---
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/34398515657
PATCH v3 14/15 selftests/bpf: Test the LSM policy object kfuncs with Landlock
In reply to Justin SuessExercise the policy object kfuncs against an LSM actually providing policy objects, complementing the LSM-independent tests of the verifier-side and from_fd contracts.
The programs mirror the intended usage: a syscall program acquires a Landlock ruleset with bpf_lsm_policy_from_fd() and parks it in a map kptr field; an LSM program on bprm_creds_for_exec() loads the field under bpf_rcu_read_lock(), takes its own reference with bpf_lsm_policy_acquire(), and applies the ruleset with bpf_lsm_policy_apply_bprm(). The prog_tests runner checks that:
- a monitored execution starts confined (a handled-but-not-allowed write fails) while an unmonitored one is untouched,
- two concurrent monitored executions are both restricted from the one shared map slot,
- the landlock_restrict_self(2) log flags are accepted while LANDLOCK_RESTRICT_SELF_TSYNC is rejected with -EINVAL and leaves the execution unrestricted,
- LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS makes the executed program start with no_new_privs set, while an execution without it stays non-nnp,
- a second apply call on the same execution replaces the staged restriction rather than failing,
- an execution failing past the bprm hook (ENOEXEC) discards the staged restriction and leaves the caller unconfined,
- the object's identity is BTF-readable off the trusted kptr: the LSM-private type tag is nonzero, and the lsmid is LSM_ID_LANDLOCK with the fd alone having routed the translation,
- the bprm application emits a single landlock_enforce_domain event, observed by a tp_btf program: complete == 1, process_wide == 1, and no_new_privs reporting the post-flag state.
---
Notes:
v2->v3:
- Fix a pipe fd leak on the fork() error path of test_restrict_binprm_discard().
tools/testing/selftests/bpf/config | 1 + tools/testing/selftests/bpf/config.x86_64 | 2 +- .../bpf/prog_tests/lsm_policy_landlock.c | 525 ++++++++++++++++++ .../selftests/bpf/progs/lsm_policy_landlock.c | 142 +++++ 4 files changed, 669 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_policy_landlock.c
tools/testing/selftests/bpf/config +1 -0
@@ -122,6 +122,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
@@ -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_landlock.c new file +525 -0
525 lines not on this page · raw message
tools/testing/selftests/bpf/progs/lsm_policy_landlock.c new file +142 -0
142 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH bpf-next v3 14/15] selftests/bpf: Test the LSM policy object kfuncs with Landlock
A selftests pattern flags this as potentially concerning: skipping with no message, so the output carries no hint about which prerequisite is absent.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c new file +0
-0@@ -0,0 +1,525 @@
[ ... ]
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c line 106
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c new file +13
-0+static int setup_env (struct policy_test_env *env) +{ + env->skel =NULL ; + env->ruleset_fd =-1 ; +strcpy (env->tmp_path,"/tmp/lsm_policy_landlock_XXXXXX" ); + env->tmp_fd =mkstemp (env->tmp_path); +if (!ASSERT_GE (env->tmp_fd,0 ,"mkstemp" )) +return -1 ; + + env->ruleset_fd =create_ruleset (); +if (env->ruleset_fd <0 ) { +if (errno ==EOPNOTSUPP || errno ==ENOSYS ) +test__skip ();
^^^^^^^^^^^^
A selftests pattern flags this as potentially concerning: skipping with no message, so the output carries no hint about which prerequisite is absent.
All five subtests funnel through this path, and both errno values look classified correctly here - EOPNOTSUPP when Landlock is built but not listed in CONFIG_LSM or in lsm= at boot, ENOSYS when CONFIG_SECURITY_LANDLOCK=n. What is not visible in the output is which of the two happened; a reader sees five skips and cannot tell a config ordering problem from the feature not being built at all.
Against that, test__skip() in the bpf framework takes no message argument, and a bare test__skip() is the usual idiom across prog_tests/, so carrying a reason means adding a separate print rather than passing one to an existing API.
Would a one-line print naming the missing prerequisite before test__skip() be worth adding?
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c new file +9
-0+else +ASSERT_GE (env->ruleset_fd,0 , +"landlock_create_ruleset" ); +return -1 ; + } + + env->skel =lsm_policy_landlock__open_and_load (); +if (!ASSERT_OK_PTR (env->skel,"skel_open_and_load" )) +return -1 ;
[ ... ]
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c line 216
Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c new file +12
-0+/* +* Exit codes: 4 = unexpected write outcome, 0 = everything as +* expected. +*/ +static void format_child_cmd (char *cmd,size_t len,bool expect_write_ok, +const char *tmp_path) +{ +if (expect_write_ok) +snprintf (cmd, len,"echo x > %s || exit 4; exit 0" , tmp_path); +else + snprintf(cmd, len, + "if echo x > %s 2>/dev/null; then exit 4; fi; exit 0",
^^^^^^^^^^^^^
Is the redirect order here inverted? A shell applies redirections left to right, so the failure of "> %s" is reported before 2>/dev/null has been applied, and the diagnostic goes to the inherited fd 2:
$ sh -c 'if echo x > /nodir123/f 2>/dev/null; then exit 4; fi; exit 0'
sh: 1: cannot create /nodir123/f: Directory nonexistent $ sh -c 'if echo x 2>/dev/null > /nodir123/f; then exit 4; fi; exit 0'
(no output)bash behaves the same way, reporting "/nodir123/f: No such file or directory" for the first form.
Does that message then land in the selftest output? test_progs.c only reassigns the stdout FILE*:
tools/testing/selftests/bpf/test_progs.c:stdio_hijack_init() {
...
fflush(stdout);
fflush(stderr);
stdout = open_memstream(log_buf, log_cnt);
...
}and never redirects fd 1 or fd 2 themselves, while the writer here is a forked child that inherited the real fd 2. So each restricted execution would print
sh: 1: cannot create /tmp/lsm_policy_landlock_XXXXXX: Permission denied
which is seven executions across test_restrict_binprm(), test_restrict_binprm_concurrent() and test_restrict_binprm_trace(). The child's exit status is still 0, so the assertions are unaffected and this is stray output rather than a false failure.
Would "echo x 2>/dev/null > %s" do what this was written to do?
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c line 218
Quoted 2 lines
tools/testing/selftests/bpf/prog_tests/lsm_policy_landlock.c new file +2
-0+ tmp_path); +}
[ ... ]
---
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/34398515657
PATCH v3 15/15 landlock: Document the BPF policy interface
In reply to Justin SuessDescribe how the generic LSM policy kfuncs apply to Landlock
rulesets: the program-side flow, the staged restriction and its trace event, the LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS semantics on an execution, and why the bprm path carries no no_new_privs/ CAP_SYS_ADMIN precondition. Note the new emission point in the trace events overview.
---
Notes:
v2->v3:
- No change.
Documentation/security/landlock.rst | 38 +++++++++++++++++++++++++ Documentation/trace/events-landlock.rst | 5 +++- 2 files changed, 42 insertions(+), 1 deletion(-)
Documentation/security/landlock.rst +38 -0
@@ -130,6 +130,44 @@ 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, through the generic LSM policy kfuncs (see +Documentation/security/lsm-development.rst). A syscall program +(``BPF_PROG_TYPE_SYSCALL``), running in the context of the process +that set the ruleset up, acquires the ruleset with +``bpf_lsm_policy_from_fd()`` 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 with ``bpf_lsm_policy_apply_bprm()``. + +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 +commitment emits the ``landlock_enforce_domain`` trace event (see +Documentation/trace/events-landlock.rst). The kfunc flags take the +``landlock_restrict_self(2)`` flags with their usual semantics, with +the exception of ``LANDLOCK_RESTRICT_SELF_TSYNC``, which is rejected: +the restriction targets the execution, not the calling threads. + +``LANDLOCK_RESTRICT_SELF_NO_NEW_PRIVS`` makes the executed program +start with no_new_privs set, binding it and all its descendants. It +does not affect the current execution's privilege computation: the +bprm credentials, including any setuid elevation, are computed before +the flag is set. + +Unlike ``landlock_restrict_self(2)``, the bprm path has no +no_new_privs/``CAP_SYS_ADMIN`` precondition: a task that is not +no_new_privs can carry a BPF-applied domain, and its setuid +executions still elevate while confined. This is sound because +attaching the BPF program is itself privileged, granting the same +power as the syscall's ``CAP_SYS_ADMIN`` carve-out. + +Executions may run concurrently: each takes its own reference on +the shared ruleset with ``bpf_lsm_policy_acquire()``. + Tests =====
Documentation/trace/events-landlock.rst +4 -1
@@ -34,7 +34,10 @@ Landlock trace events are organized in four categories: - ``landlock_add_rule_fs``: a filesystem rule is added to a ruleset - ``landlock_add_rule_net``: a network port rule is added to a ruleset - ``landlock_create_domain``: a new domain is created from a ruleset -- ``landlock_enforce_domain``: a domain is enforced on a thread +- ``landlock_enforce_domain``: a domain is enforced on a thread. Also + emitted at ``execve(2)``'s point of no return when a BPF program has + staged a policy on the execution (see the BPF kfuncs section of + Documentation/security/landlock.rst) **Denial events** are emitted when an access is denied:
Signature · 1 line
2.55.0