Skip to contentSkip to conversation

lsm: give BPF programs a way to query locked_down state

PATCH In review Justin Suess 7 participants 17 messages Aug 15 – Aug 18

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover lsm: give BPF programs a way to query locked_down state

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",

				                 current->comm, lockdown_reasons[what]);

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

Justin

Justin Suess (2):
lsm: add bpf_security_locked_down() kfunc selftests/bpf: Test bpf_security_locked_down kfunc

 security/Makefile                             |  1 +
 security/lsm_kfuncs.c                         | 84 +++++++++++++++++++
 .../selftests/bpf/prog_tests/lsm_kfuncs.c     | 28 +++++++
 .../testing/selftests/bpf/progs/lsm_kfuncs.c  | 34 ++++++++
 .../selftests/bpf/progs/lsm_kfuncs_fail.c     | 26 ++++++
 5 files changed, 173 insertions(+)
 create mode 100644 security/lsm_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs_fail.c

base-commit: d82ebfc685c91e7f5623a8be949da1ddb767420b

Signature · 1 line
2.54.0
#1 lore raw

PATCH 1/2 lsm: add bpf_security_locked_down() kfunc

Add a new kfunc bpf_security_locked_down, which calls security_locked_down and returns the result.

Create a new file security/lsm_kfuncs.c for LSM framework kfuncs.

Reject reasons outside (LOCKDOWN_NONE, LOCKDOWN_CONFIDENTIALITY_MAX) with -EINVAL before dispatching the hook. Limit the kfunc to BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL programs, and refuse it to programs attached to the locked_down hook itself, which would recurse into the dispatch.

---
 security/Makefile     |  1 +
 security/lsm_kfuncs.c | 84 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
 create mode 100644 security/lsm_kfuncs.c
security/Makefile +1 -0
index 4601230ba442..dee8ff218548 100644
@@ -12,6 +12,7 @@ obj-$(CONFIG_MMU)			+= min_addr.o
 
 # Object file lists
 obj-$(CONFIG_SECURITY)			+= security.o lsm_notifier.o lsm_init.o
+obj-$(CONFIG_BPF_SYSCALL)		+= lsm_kfuncs.o
 obj-$(CONFIG_SECURITYFS)		+= inode.o
 obj-$(CONFIG_SECURITY_SELINUX)		+= selinux/
 obj-$(CONFIG_SECURITY_SMACK)		+= smack/
security/lsm_kfuncs.c new file +84 -0

84 lines not on this page · raw message

Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH bpf-next 1/2] lsm: add bpf_security_locked_down() kfunc

This isn't a bug, but could the changelog lead with the use case that motivates exposing security_locked_down() to BPF programs, instead of restating what each hunk does?
Quoted 7 lines

lsm: add bpf_security_locked_down() kfunc

Add a new kfunc bpf_security_locked_down, which calls security_locked_down and returns the result.

Create a new file security/lsm_kfuncs.c for LSM framework kfuncs.

Quoted 7 lines

Reject reasons outside (LOCKDOWN_NONE, LOCKDOWN_CONFIDENTIALITY_MAX) with -EINVAL before dispatching the hook. Limit the kfunc to BPF_PROG_TYPE_LSM and BPF_PROG_TYPE_SYSCALL programs, and refuse it to programs attached to the locked_down hook itself, which would recurse into the dispatch.

This isn't a bug, but could the changelog lead with the use case that motivates exposing security_locked_down() to BPF programs, instead of restating what each hunk does?

---
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/31882515336

#3 lore raw

PATCH 2/2 selftests/bpf: Test bpf_security_locked_down kfunc

In reply to Justin Suess

Test the bpf_security_locked_down() kfunc. An LSM program attached to the locked_down hook denies LOCKDOWN_HIBERNATION, so a syscall program querying the kfunc observes both verdicts deterministically without touching real lockdown state: 0 for LOCKDOWN_KEXEC and -EPERM for LOCKDOWN_HIBERNATION. Out-of-range reasons must return -EINVAL.

Programs in denied calling contexts (a tracing program, and an LSM program attached to the locked_down hook itself) must be rejected at load time by the kfunc filter.

The selftest config guarantees the verdicts are stable: the bpf LSM is in CONFIG_LSM and the lockdown LSM is not, so the kernel cannot already be locked down.

---
 .../selftests/bpf/prog_tests/lsm_kfuncs.c     | 28 +++++++++++++++
 .../testing/selftests/bpf/progs/lsm_kfuncs.c  | 34 +++++++++++++++++++
 .../selftests/bpf/progs/lsm_kfuncs_fail.c     | 26 ++++++++++++++
 3 files changed, 88 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs.c
 create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs_fail.c
tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c new file +28 -0
new file mode 100644
index 000000000000..c836851d8397
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "lsm_kfuncs.skel.h"
+#include "lsm_kfuncs_fail.skel.h"
+
+void test_lsm_kfuncs(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct lsm_kfuncs *skel;
+
+	RUN_TESTS(lsm_kfuncs_fail);
+
+	skel = lsm_kfuncs__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!ASSERT_OK(lsm_kfuncs__attach(skel), "attach"))
+		goto out;
+
+	if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(skel->progs.query),
+					      &opts), "test_run"))
+		goto out;
+	ASSERT_EQ(skel->data->ret_clear, 0, "not locked down");
+	ASSERT_EQ(skel->data->ret_denied, -EPERM, "locked down");
+	ASSERT_EQ(skel->data->ret_invalid_low, -EINVAL, "LOCKDOWN_NONE invalid");
+	ASSERT_EQ(skel->data->ret_invalid_high, -EINVAL, "CONFIDENTIALITY_MAX invalid");
+out:
+	lsm_kfuncs__destroy(skel);
+}
tools/testing/selftests/bpf/progs/lsm_kfuncs.c new file +34 -0
new file mode 100644
index 000000000000..2637b9bc9025
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+extern int bpf_security_locked_down(enum lockdown_reason what) __ksym;
+
+/* Reason nothing in the test environment genuinely queries or locks. */
+#define DENY_REASON LOCKDOWN_HIBERNATION
+#define ALLOW_REASON LOCKDOWN_KEXEC
+
+int ret_clear = 1;
+int ret_denied = 1;
+int ret_invalid_low = 1;
+int ret_invalid_high = 1;
+
+SEC("lsm/locked_down")
+int BPF_PROG(lockdown_hook, enum lockdown_reason what)
+{
+	return what == DENY_REASON ? -EPERM : 0;
+}
+
+SEC("syscall")
+int query(void *ctx)
+{
+	ret_clear = bpf_security_locked_down(ALLOW_REASON);
+	ret_denied = bpf_security_locked_down(DENY_REASON);
+	ret_invalid_low = bpf_security_locked_down(LOCKDOWN_NONE);
+	ret_invalid_high = bpf_security_locked_down(LOCKDOWN_CONFIDENTIALITY_MAX);
+	return 0;
+}
tools/testing/selftests/bpf/progs/lsm_kfuncs_fail.c new file +26 -0
new file mode 100644
index 000000000000..b9861d9c4f16
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+extern int bpf_security_locked_down(enum lockdown_reason what) __ksym;
+
+/* Tracing programs must be rejected by the kfunc filter. */
+SEC("fentry/bpf_fentry_test1")
+__failure __msg("calling kernel function bpf_security_locked_down is not allowed")
+int BPF_PROG(tracing_caller, int a)
+{
+	bpf_security_locked_down(LOCKDOWN_KEXEC);
+	return 0;
+}
+
+/* As must locked_down programs, which would recurse into the dispatch. */
+SEC("lsm/locked_down")
+__failure __msg("calling kernel function bpf_security_locked_down is not allowed")
+int BPF_PROG(recursive_caller, enum lockdown_reason what)
+{
+	return bpf_security_locked_down(what);
+}
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Test bpf_security_locked_down kfunc

Does the selftest config actually guarantee this? Looking at tools/testing/selftests/bpf/config, it sets only CONFIG_BPF_LSM=y, CONFIG_SECURITY=y and CONFIG_SECURITYFS=y. There is no CONFIG_LSM line…
Quoted 3 lines

The selftest config guarantees the verdicts are stable: the bpf LSM is in CONFIG_LSM and the lockdown LSM is not, so the kernel cannot already be locked down.

Does the selftest config actually guarantee this? Looking at tools/testing/selftests/bpf/config, it sets only CONFIG_BPF_LSM=y, CONFIG_SECURITY=y and CONFIG_SECURITYFS=y. There is no CONFIG_LSM line at all in that file.

The only CONFIG_LSM in the bpf selftest configs is tools/testing/selftests/bpf/config.x86_64, which has CONFIG_LSM="selinux,bpf,integrity". On other arch configs (aarch64, s390x, riscv64, ppc64el), CONFIG_LSM falls back to the security/Kconfig default, which does list "lockdown".

What actually prevents the lockdown LSM from being active is that CONFIG_SECURITY_LOCKDOWN_LSM defaults to n, so the lockdown LSM registers no hook. This is a weaker guarantee than the changelog states, and only holds when test_progs is run against the shipped selftest config.

Quoted 18 lines
tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c new file +12 -0
new file mode 100644
index 000000000000..c836851d8397
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+#include "lsm_kfuncs.skel.h"
+#include "lsm_kfuncs_fail.skel.h"
+
+void test_lsm_kfuncs(void)
+{
+	LIBBPF_OPTS(bpf_test_run_opts, opts);
+	struct lsm_kfuncs *skel;
+
+	RUN_TESTS(lsm_kfuncs_fail);
+

tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c line 22

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c new file +10 -0
+	skel = lsm_kfuncs__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "open_and_load"))
+		return;
+	if (!ASSERT_OK(lsm_kfuncs__attach(skel), "attach"))
+		goto out;
+
+	if (!ASSERT_OK(bpf_prog_test_run_opts(bpf_program__fd(skel->progs.query),
+					      &opts), "test_run"))
+		goto out;
+	ASSERT_EQ(skel->data->ret_clear, 0, "not locked down");

Can this assertion produce false failures on a kernel that is already locked down? If CONFIG_SECURITY_LOCKDOWN_LSM=y with lockdown active (lockdown=integrity/confidentiality, or Secure Boot with CONFIG_LOCK_DOWN_KERNEL_FORCE_*), security_locked_down() walks every registered locked_down hook, not just the test's BPF program.

security/lockdown/lockdown.c:
LSM_HOOK_INIT(locked_down, lockdown_is_locked_down)

would return -EPERM for LOCKDOWN_KEXEC (an integrity-level reason), so ret_clear becomes -EPERM and the test reports a hard FAIL rather than a skip.

Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c new file +6 -0
+	ASSERT_EQ(skel->data->ret_denied, -EPERM, "locked down");
+	ASSERT_EQ(skel->data->ret_invalid_low, -EINVAL, "LOCKDOWN_NONE invalid");
+	ASSERT_EQ(skel->data->ret_invalid_high, -EINVAL, "CONFIDENTIALITY_MAX invalid");
+out:
+	lsm_kfuncs__destroy(skel);
+}

[ ... ]

Quoted 25 lines
tools/testing/selftests/bpf/progs/lsm_kfuncs.c new file +19 -0
new file mode 100644
index 000000000000..2637b9bc9025
@@ -0,0 +1,34 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "vmlinux.h"
+#include <errno.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+extern int bpf_security_locked_down(enum lockdown_reason what) __ksym;
+
+/* Reason nothing in the test environment genuinely queries or locks. */
+#define DENY_REASON LOCKDOWN_HIBERNATION
+#define ALLOW_REASON LOCKDOWN_KEXEC
+
+int ret_clear = 1;
+int ret_denied = 1;
+int ret_invalid_low = 1;
+int ret_invalid_high = 1;
+

tools/testing/selftests/bpf/progs/lsm_kfuncs.c line 29

Quoted 10 lines
tools/testing/selftests/bpf/progs/lsm_kfuncs.c new file +10 -0
+SEC("lsm/locked_down")
+int BPF_PROG(lockdown_hook, enum lockdown_reason what)
+{
+	return what == DENY_REASON ? -EPERM : 0;
+}
+
+SEC("syscall")
+int query(void *ctx)
+{
+	ret_clear = bpf_security_locked_down(ALLOW_REASON);

Would it make sense to have the test tolerate or skip when the running kernel is already locked down? The current approach asserts on global host state the test does not control. Consider checking if ret_clear != 0 and skipping with a message, so the test does not produce false regression signals on distro kernels with lockdown active.

tools/testing/selftests/bpf/progs/lsm_kfuncs.c line 34

Quoted 5 lines
tools/testing/selftests/bpf/progs/lsm_kfuncs.c new file +5 -0
+	ret_denied = bpf_security_locked_down(DENY_REASON);
+	ret_invalid_low = bpf_security_locked_down(LOCKDOWN_NONE);
+	ret_invalid_high = bpf_security_locked_down(LOCKDOWN_CONFIDENTIALITY_MAX);
+	return 0;
+}

[ ... ]

---
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/31882515336

#5 lore raw
In reply to Justin Suess

+cc Nicolas

Quoted 8 lines · Justin Suess

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Quoted 5 lines

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Adding the bpf_security_locked_down() kfunc does not actually solve the TOCTOU issue you mentioned. Even after bpf_security_locked_down() is called, userspace can still change the lockdown state via /sys/kernel/ security/lockdown.

I fail to see the necessity for a BPF program to know whether a specific operation is locked down. The test case provided in patch 2 does not clearly demonstrate a scenario where this is required. In my view, the verdict should happen exactly where security_locked_down() is currently invoked.

Furthermore, based on the current implementation of Lockdown, integrity is the prerequisite for confidentiality. It is designed to be coarse- grained and does not support per-operation lockdown. The fact that LSM BPF can already hook into locked_down seems to violate this foundational model, this is is analogous to the bitmap implementation [1], I’m considering whether we should restrict BPF from attaching to the locked_down hook. Nicolas, what are your thoughts on this?

[1] https://lore.kernel.org/all/[email protected]/

Quoted 48 lines

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",

				                 current->comm, lockdown_reasons[what]);

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

Justin

Justin Suess (2):
lsm: add bpf_security_locked_down() kfunc selftests/bpf: Test bpf_security_locked_down kfunc

  security/Makefile                             |  1 +
  security/lsm_kfuncs.c                         | 84 +++++++++++++++++++
  .../selftests/bpf/prog_tests/lsm_kfuncs.c     | 28 +++++++
  .../testing/selftests/bpf/progs/lsm_kfuncs.c  | 34 ++++++++
  .../selftests/bpf/progs/lsm_kfuncs_fail.c     | 26 ++++++
  5 files changed, 173 insertions(+)

create mode 100644 security/lsm_kfuncs.c
create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs_fail.c

base-commit: d82ebfc685c91e7f5623a8be949da1ddb767420b

#6 lore raw
Quoted 17 lines · Xiujianfeng

+cc Nicolas

Quoted 8 lines · Justin Suess

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Quoted 5 lines

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Quoted 5 lines

Adding the bpf_security_locked_down() kfunc does not actually solve the TOCTOU issue you mentioned. Even after bpf_security_locked_down() is called, userspace can still change the lockdown state via /sys/kernel/ security/lockdown.

This would put it at the same raciness level as any kernel caller of security_locked_down.

More importantly, the sysfs file is not equivalent to calling the hook. /sys/kernel/security/lockdown reflects only the lockdown LSM's static level.

security_locked_down() is a generic LSM hook and other implementers of locked_down: including BPF LSM programs attached to that hook (which is already supported) can contribute per-reason, dynamic verdicts that never appear in sysfs. The composite answer is only obtainable by invoking the hook, which is what this kfunc exposes.

Quoted 6 lines

I fail to see the necessity for a BPF program to know whether a specific operation is locked down. The test case provided in patch 2 does not clearly demonstrate a scenario where this is required. In my view, the verdict should happen exactly where security_locked_down() is currently invoked.

Elaborating a bit more on the use cases:

1. Being able to know if features LOCKDOWN_BPF_READ / LOCKDOWN_KPROBES is locked down without a userspace hop / polling an LSM-specific interface for error handling.

2. Use with the BPF LSM implmentations of security_locked_down, without Lockdown even in the picture.

3. A BPF LSM can restrict access to arbritrary sensitive resources based on the security_locked_down verdict.

Quoted 7 lines

Furthermore, based on the current implementation of Lockdown, integrity is the prerequisite for confidentiality. It is designed to be coarse- grained and does not support per-operation lockdown. The fact that LSM BPF can already hook into locked_down seems to violate this foundational model, this is is analogous to the bitmap implementation [1], I’m considering whether we should restrict BPF from attaching to the locked_down hook. Nicolas, what are your thoughts on this?

You describe a Lockdown-specific model, why should other security models / implementations of the same hook be made to have the same semantics?

The point of the LSM hooks are to be generic.

(I have use cases that do rely on that hook; though I'm embarrased to speak on it and it is probably abuse: I use this hook to temporarily disable hibernation in BPF)

Justin

Quoted 53 lines

[1]
https://lore.kernel.org/all/[email protected]/

Quoted 48 lines

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",

				                 current->comm, lockdown_reasons[what]);

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

Justin

Justin Suess (2):
lsm: add bpf_security_locked_down() kfunc selftests/bpf: Test bpf_security_locked_down kfunc

  security/Makefile                             |  1 +
  security/lsm_kfuncs.c                         | 84 +++++++++++++++++++
  .../selftests/bpf/prog_tests/lsm_kfuncs.c     | 28 +++++++
  .../testing/selftests/bpf/progs/lsm_kfuncs.c  | 34 ++++++++
  .../selftests/bpf/progs/lsm_kfuncs_fail.c     | 26 ++++++
  5 files changed, 173 insertions(+)

create mode 100644 security/lsm_kfuncs.c
create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs_fail.c

base-commit: d82ebfc685c91e7f5623a8be949da1ddb767420b

#7 lore raw
Quoted 48 lines · Justin Suess
Quoted 17 lines · Xiujianfeng

+cc Nicolas

Quoted 8 lines · Justin Suess

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Quoted 5 lines

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Quoted 5 lines

Adding the bpf_security_locked_down() kfunc does not actually solve the TOCTOU issue you mentioned. Even after bpf_security_locked_down() is called, userspace can still change the lockdown state via /sys/kernel/ security/lockdown.

This would put it at the same raciness level as any kernel caller of security_locked_down.

More importantly, the sysfs file is not equivalent to calling the hook. /sys/kernel/security/lockdown reflects only the lockdown LSM's static level.

security_locked_down() is a generic LSM hook and other implementers of locked_down: including BPF LSM programs attached to that hook (which is already supported) can contribute per-reason, dynamic verdicts that never appear in sysfs. The composite answer is only obtainable by invoking the hook, which is what this kfunc exposes.

Quoted 6 lines

I fail to see the necessity for a BPF program to know whether a specific operation is locked down. The test case provided in patch 2 does not clearly demonstrate a scenario where this is required. In my view, the verdict should happen exactly where security_locked_down() is currently invoked.

Elaborating a bit more on the use cases:

1. Being able to know if features LOCKDOWN_BPF_READ / LOCKDOWN_KPROBES is locked down without a userspace hop / polling an LSM-specific interface for error handling.

Quoted 6 lines

2. Use with the BPF LSM implmentations of security_locked_down, without Lockdown even in the picture.

3. A BPF LSM can restrict access to arbritrary sensitive resources based on the security_locked_down verdict.

I stand by my view that LSM arbitration should happen right where the action is actually executed. Wrapping it inside BPF kfuncs introduces dangerous nested invocations between LSM and BPF. While your patch
blocks the locked_down hook from calling bpf_security_locked_down, it is
a whack-a-mole workaround and does not alter the fact of nesting itself.

Quoted 71 lines
Quoted 7 lines

Furthermore, based on the current implementation of Lockdown, integrity is the prerequisite for confidentiality. It is designed to be coarse- grained and does not support per-operation lockdown. The fact that LSM BPF can already hook into locked_down seems to violate this foundational model, this is is analogous to the bitmap implementation [1], I’m considering whether we should restrict BPF from attaching to the locked_down hook. Nicolas, what are your thoughts on this?

You describe a Lockdown-specific model, why should other security models / implementations of the same hook be made to have the same semantics?

The point of the LSM hooks are to be generic.

(I have use cases that do rely on that hook; though I'm embarrased to speak on it and it is probably abuse: I use this hook to temporarily disable hibernation in BPF)

Justin

Quoted 53 lines

[1]
https://lore.kernel.org/all/[email protected]/

Quoted 48 lines

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",
current->comm, lockdown_reasons[what]);

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

Justin

Justin Suess (2):
lsm: add bpf_security_locked_down() kfunc selftests/bpf: Test bpf_security_locked_down kfunc

   security/Makefile                             |  1 +
   security/lsm_kfuncs.c                         | 84 +++++++++++++++++++
   .../selftests/bpf/prog_tests/lsm_kfuncs.c     | 28 +++++++
   .../testing/selftests/bpf/progs/lsm_kfuncs.c  | 34 ++++++++
   .../selftests/bpf/progs/lsm_kfuncs_fail.c     | 26 ++++++
   5 files changed, 173 insertions(+)

create mode 100644 security/lsm_kfuncs.c
create mode 100644 tools/testing/selftests/bpf/prog_tests/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs.c create mode 100644 tools/testing/selftests/bpf/progs/lsm_kfuncs_fail.c

base-commit: d82ebfc685c91e7f5623a8be949da1ddb767420b

#8 lore raw
In reply to Xiujianfeng
Quoted 17 lines · Xiujianfeng

+cc Nicolas

Quoted 8 lines · Justin Suess

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Quoted 5 lines

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Quoted 7 lines

Adding the bpf_security_locked_down() kfunc does not actually solve the TOCTOU issue you mentioned. Even after bpf_security_locked_down() is called, userspace can still change the lockdown state via /sys/kernel/ security/lockdown.

I fail to see the necessity for a BPF program to know whether a specific operation is locked down.

I agree with Xiu.

Quoted 6 lines

The test case provided in patch 2 does not
clearly demonstrate a scenario where this is required. In my view, the verdict should happen exactly where security_locked_down() is currently invoked.

Quoted 7 lines

Furthermore, based on the current implementation of Lockdown, integrity is the prerequisite for confidentiality. It is designed to be coarse- grained and does not support per-operation lockdown. The fact that LSM BPF can already hook into locked_down seems to violate this foundational model, this is is analogous to the bitmap implementation [1], I’m considering whether we should restrict BPF from attaching to the locked_down hook. Nicolas, what are your thoughts on this?

I do not see any issue about LSM BPF being able to hook into security_locked_down. If Lockdown is enabled in integrity mode, and a BPF LSM hook authorizes let's say the LOCKDOWN_DEV_MEM reason, Lockdown will deny it anyway.

IMHO, if one wants to disable the Lockdown LSM and implement it through eBPF, if the implementation is bad, it is not our problem. Exactly as for other LSM hooks.

Quoted 34 lines

[1]
https://lore.kernel.org/all/[email protected]/

Quoted 30 lines

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",

				                 current->comm, lockdown_reasons[what]);

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

Justin

Nicolas

#9 lore raw
In reply to Justin Suess
Quoted 33 lines · Justin Suess

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

Quoted 9 lines

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",

				                 current->comm, lockdown_reasons[what]);

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

I don't think there is good enough justification to add this in the discussion. The reason you gave (dynamically disabling hibernation) also doesn't inspire confidence. I think figuring out some other way to achieve that is better.

If you care about knowing the state of lockdown LSM, doing bpf_probe_read_kernel() etc. should allow reading that state from the program. If you have a BPF LSM supplying a dynamic verdict in your environment, I don't see why you cannot use the decision procedure from the same implementation in other places of the LSM. I doubt you have the scenario where BPF LSM is shipped by someone else and your program needs access to its decisions instead.

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

pw-bot: cr

Quoted 1 line

[...]

#10 lore raw
Quoted 40 lines · Kumar Kartikeya Dwivedi
Quoted 39 lines · Justin Suess

Howdy,

BPF programs can attach to the locked_down LSM hook and contribute a verdict, but they have never been able to ask the locked_down question
themselves: there is no way for a program to invoke the hook and learn whether a given operation is locked down. (i.e be a caller of security_locked_down rather than a consumer).

Today the state has to be fed in out of band, for example userspace reading /sys/kernel/security/lockdown and writing the result into a map. That is a time-of-check/time-of-use race: a security_locked_down verdict can be raised at runtime, so the cached answer can be stale by the time the program acts on it.

Add a bpf_security_locked_down() kfunc that calls security_locked_down() and returns its verdict, letting LSM and syscall programs query locked_down state at decision time. Out-of-range reasons are rejected with -EINVAL before dispatching the hook, and the kfunc is refused to programs attached to the locked_down hook itself, which would recurse into the dispatch. (how the obvious recursion issue is addressed).

As this is the first pure-lsm-hook kfunc, add a new file security/lsm_kfuncs.c to host it.

This kfunc has no reliance on / relation to the Lockdown LSM, despite the similar naming. It is an LSM-agnostic caller of security_locked_down, and Lockdown just happens to be the only in-tree subscriber to this hook at the moment.

In fact, the test environment does not rely on CONFIG_SECURITY_LOCKDOWN at all, and uses a BPF implementation of security_locked_down.

This kfunc can cause notices to be printed with kmsg if the Lockdown LSM is enabled due to this line in security/lockdown/lockdown.c:

pr_notice_ratelimited("Lockdown: %s: %s is restricted; see man kernel_lockdown.7\n",

				                 current->comm, lockdown_reasons[what]);
Quoted 8 lines
Quoted 3 lines

Patch 1 adds the kfunc, patch 2 the selftests. This is based on bpf-next/master, but applies cleanly to the lsm tree.

I don't think there is good enough justification to add this in the discussion. The reason you gave (dynamically disabling hibernation) also doesn't inspire confidence. I think figuring out some other way to achieve that is better.

It is really a quite terrible hack isn't it?

I am using it as part of an hid_bpf_ops program to allow disabling hibernation on my Linux smartTV box via their proprietary remote contro.

But you are right using it that way smells off.

I was proud of it anyway...

Quoted 6 lines

If you care about knowing the state of lockdown LSM, doing bpf_probe_read_kernel() etc. should allow reading that state from the program. If you have a BPF LSM supplying a dynamic verdict in your environment, I don't see why you cannot use the decision procedure from the same implementation in other places of the LSM. I doubt you have the scenario where BPF LSM is shipped by someone else and your program needs access to its decisions instead.

This argument persuaded me.

Given this, I don't think the kfunc is the correct way to do what I was trying to do anyway... so I will drop this for now since it was a trivial patch.

Justin

Quoted 8 lines

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

pw-bot: cr

Quoted 1 line

[...]

#11 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 9 lines · Kumar Kartikeya Dwivedi

On Sat Aug 15, 2026 at 1:20 PM CEST, Justin Suess wrote:

If you care about knowing the state of lockdown LSM, doing bpf_probe_read_kernel() etc. should allow reading that state from the program. If you have a BPF LSM supplying a dynamic verdict in your environment, I don't see why you cannot use the decision procedure from the same implementation in other places of the LSM. I doubt you have the scenario where BPF LSM is shipped by someone else and your program needs access to its decisions instead.

(I've found a workaround with tracepoints that removes the need for this series. Thank you anyway)

Quoted 4 lines

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

Without wanting to rehash this issue,

Both LSM and BPF maintainers, some cc'd in the thread, have worked with me as a new contributor through my mistakes and volunteered their time, expertise, and effort. I cannot appreciate it enough.

Clearly, great talent and commitment to making the kernel better lives on both sides of the fence. There's also no question the whole kernel would benefit from better communication between these awesome subsystems.

I don't mean to admonish anyone or assign fault. Debates happen, things get heated, it's what happens. But the fact that multiple series are stalled on this because BPF/LSM don't trust each other enough to have shared code ownership implies the current process is broken. This isn't the first instance of this breakdown either. It hurts new contributors caught in the crossfire, maintainers who have to tiptoe around this drama, and kernel users.

If anyone is willing: I'd like to set the debates aside and discuss in good faith how we can establish a better process for LSM/BPF review, testing, and integration. So we can steer this conversation to the technical side, which is where actual work happens.

That could be an LSM/BPF integration branch, or Documentation/, or something else, or meeting up at a conference and having some beers together.

Kind Regards,
Justin

Quoted 3 lines

pw-bot: cr

Quoted 1 line

[...]

#12 lore raw
In reply to Kumar Kartikeya Dwivedi
Quoted 6 lines · Kumar Kartikeya Dwivedi

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

I'm still going to send v7 of bpf_init_inode_xattr series, even though it's a stalemate. Haven't yet decided where it'll live in v7, I'm inclined to leave it where it was (in fs/) since one side will NAK it wherever it lives. We can record NAKs and take it from there.

Just wanted to state this for least surprise when v7 does come out, which will be soon.

Thanks,
David

maintainer
#13 lore raw
Quoted 9 lines · David Windsor
Quoted 6 lines · Kumar Kartikeya Dwivedi

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

Quoted 4 lines

I'm still going to send v7 of bpf_init_inode_xattr series, even though it's a stalemate. Haven't yet decided where it'll live in v7, I'm inclined to leave it where it was (in fs/) since one side will NAK it wherever it lives. We can record NAKs and take it from there.

As a FYI, I'm not going to ACK any of the LSM changes with the kfunc in security/bpf_lsm_kfuncs.c.

Signature · 1 line
paul-moore.com
#14 lore raw
Quoted 11 lines · Paul Moore
Quoted 9 lines · David Windsor
Quoted 6 lines · Kumar Kartikeya Dwivedi

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

Quoted 8 lines
Quoted 4 lines

I'm still going to send v7 of bpf_init_inode_xattr series, even though it's a stalemate. Haven't yet decided where it'll live in v7, I'm inclined to leave it where it was (in fs/) since one side will NAK it wherever it lives. We can record NAKs and take it from there.

As a FYI, I'm not going to ACK any of the LSM changes with the kfunc in security/bpf_lsm_kfuncs.c.

Hmm, even if it lands in security/ you'd not ACK it? I thought what we'd settled on as acceptable from your end was placing it in security/, then having a broader discussion about the rest of the fs kfuncs? Just trying to understand everyone's position.

maintainer
#15 lore raw
Quoted 16 lines · David Windsor
Quoted 15 lines · Paul Moore
Quoted 13 lines · David Windsor
Quoted 6 lines · Kumar Kartikeya Dwivedi

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

I'm still going to send v7 of bpf_init_inode_xattr series, even though it's a stalemate. Haven't yet decided where it'll live in v7, I'm inclined to leave it where it was (in fs/) since one side will NAK it wherever it lives. We can record NAKs and take it from there.

Quoted 4 lines
Quoted 2 lines

As a FYI, I'm not going to ACK any of the LSM changes with the kfunc in security/bpf_lsm_kfuncs.c.

Hmm, even if it lands in security/ you'd not ACK it?

Bah, a typo on my part, my apologies! What I had meant to say was that I'm not going to ACK any of the LSM changes with the kfunc *not* in security/bpf_lsm_kfuncs.c. Sorry for the confusion.

Signature · 1 line
paul-moore.com
#16 lore raw
Quoted 18 lines · Paul Moore
Quoted 16 lines · David Windsor
Quoted 15 lines · Paul Moore
Quoted 13 lines · David Windsor
Quoted 6 lines · Kumar Kartikeya Dwivedi

Lastly, given the difficulties we've faced from the LSM maintainers, I'm not inclined to waste more time in explaining again why this cannot go under security/.

I'm still going to send v7 of bpf_init_inode_xattr series, even though it's a stalemate. Haven't yet decided where it'll live in v7, I'm inclined to leave it where it was (in fs/) since one side will NAK it wherever it lives. We can record NAKs and take it from there.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

As a FYI, I'm not going to ACK any of the LSM changes with the kfunc in security/bpf_lsm_kfuncs.c.

Hmm, even if it lands in security/ you'd not ACK it?

Bah, a typo on my part, my apologies! What I had meant to say was that I'm not going to ACK any of the LSM changes with the kfunc *not* in security/bpf_lsm_kfuncs.c. Sorry for the confusion.

Makes sense, thanks. I'll send it in the next week most likely.