[PATCH bpf-next v4 14/17] selftests/bpf: Add negative and verifier tests for BPF OOM kfuncs

From: Ben Gardon

Date: Wed Dec 02 2026 - 16:06:08 EST


Add selftest coverage for BPF OOM kfunc safety boundaries:
- Verifier rejection of bpf_oom_kill_process() from SEC("syscall"), and
rejection of bpf_out_of_memory() / bpf_oom_proactive_kill() from
bpf_oom_ops struct_ops callbacks (oom_lock deadlock guard).
- Runtime rejection of init (pid 1, -EPERM) and unknown flags (-EINVAL)
in bpf_oom_proactive_kill().
- Global (origin__nullable == NULL) proactive termination.

Signed-off-by: Ben Gardon <bgardon@xxxxxxxxxx>
---
.../bpf/prog_tests/test_oom_kfunc_neg.c | 79 +++++++++++++++++++
.../bpf/progs/test_oom_fail_ops_oom.c | 21 +++++
.../bpf/progs/test_oom_fail_ops_proactive.c | 23 ++++++
.../bpf/progs/test_oom_fail_syscall.c | 15 ++++
4 files changed, 138 insertions(+)
create mode 100644 tools/testing/selftests/bpf/prog_tests/test_oom_kfunc_neg.c
create mode 100644 tools/testing/selftests/bpf/progs/test_oom_fail_ops_oom.c
create mode 100644 tools/testing/selftests/bpf/progs/test_oom_fail_ops_proactive.c
create mode 100644 tools/testing/selftests/bpf/progs/test_oom_fail_syscall.c

diff --git a/tools/testing/selftests/bpf/prog_tests/test_oom_kfunc_neg.c b/tools/testing/selftests/bpf/prog_tests/test_oom_kfunc_neg.c
new file mode 100644
index 000000000000..8cfe9e3e85bb
--- /dev/null
+++ b/tools/testing/selftests/bpf/prog_tests/test_oom_kfunc_neg.c
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <errno.h>
+#include <sys/wait.h>
+#include <test_progs.h>
+#include <bpf/bpf.h>
+
+#include "memory_holder.h"
+#include "progs/test_oom_proactive_kill.h"
+#include "test_oom_proactive_kill.skel.h"
+#include "test_oom_fail_syscall.skel.h"
+#include "test_oom_fail_ops_oom.skel.h"
+#include "test_oom_fail_ops_proactive.skel.h"
+
+/*
+ * Serialized because the global proactive kill subtest acquires oom_lock and
+ * invokes the OOM killer, which can collide with other OOM tests under -j.
+ */
+void serial_test_oom_kfunc_neg(void)
+{
+ struct test_oom_proactive_kill *skel = NULL;
+ struct proactive_kill_args args = {};
+ struct memory_holder worker;
+ int prog_fd, status = 0;
+ LIBBPF_OPTS(bpf_test_run_opts, topts,
+ .ctx_in = &args,
+ .ctx_size_in = sizeof(args),
+ );
+
+ memory_holder_init(&worker);
+
+ /* Verifier rejection checks for kfunc context restrictions */
+ RUN_TESTS(test_oom_fail_syscall);
+ RUN_TESTS(test_oom_fail_ops_oom);
+ RUN_TESTS(test_oom_fail_ops_proactive);
+
+ skel = test_oom_proactive_kill__open_and_load();
+ if (!ASSERT_OK_PTR(skel, "open_and_load"))
+ return;
+
+ prog_fd = bpf_program__fd(skel->progs.proactive_kill_prog);
+
+ /* Init (pid 1) must be rejected with -EPERM via oom_unkillable_task() */
+ args.cgroup_id = 0;
+ args.pid = 1;
+ args.wait_on_oom_lock = 1;
+ if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_init"))
+ ASSERT_EQ((int)topts.retval, -EPERM, "reject_init");
+
+ /* Invalid flags must be rejected with -EINVAL */
+ if (!ASSERT_OK(memory_holder_spawn(&worker, 0, 0, 0, false), "spawn_child") ||
+ !ASSERT_OK(memory_holder_start(&worker), "start_child") ||
+ !ASSERT_OK(memory_holder_wait_ready(&worker), "wait_child"))
+ goto cleanup;
+
+ args.cgroup_id = 0;
+ args.pid = worker.pid;
+ args.flags = ~0ULL;
+ args.wait_on_oom_lock = 0;
+ if (ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_bad_flags"))
+ ASSERT_EQ((int)topts.retval, -EINVAL, "reject_bad_flags");
+
+ /* Global proactive kill (origin__nullable == NULL) must succeed */
+ args.cgroup_id = 0;
+ args.pid = worker.pid;
+ args.flags = 0;
+ args.wait_on_oom_lock = 1;
+ if (!ASSERT_OK(bpf_prog_test_run_opts(prog_fd, &topts), "run_global") ||
+ !ASSERT_OK((int)topts.retval, "global_retval"))
+ goto cleanup;
+
+ ASSERT_EQ(waitpid(worker.pid, &status, 0), worker.pid, "waitpid_global");
+ worker.pid = -1;
+ ASSERT_TRUE(WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL,
+ "global_sigkill");
+
+cleanup:
+ memory_holder_cleanup(&worker);
+ test_oom_proactive_kill__destroy(skel);
+}
diff --git a/tools/testing/selftests/bpf/progs/test_oom_fail_ops_oom.c b/tools/testing/selftests/bpf/progs/test_oom_fail_ops_oom.c
new file mode 100644
index 000000000000..ba6b15c6e407
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_oom_fail_ops_oom.c
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops.s/handle_out_of_memory")
+__failure __msg("calling kernel function bpf_out_of_memory is not allowed")
+int BPF_PROG(ops_call_out_of_memory, struct oom_control *oc,
+ struct cgroup *cgrp)
+{
+ return bpf_out_of_memory(oc->memcg, 0, 0);
+}
+
+SEC(".struct_ops.link")
+struct bpf_oom_ops fail_oom_ops = {
+ .name = "fail_ops_oom",
+ .handle_out_of_memory = (void *)ops_call_out_of_memory,
+};
diff --git a/tools/testing/selftests/bpf/progs/test_oom_fail_ops_proactive.c b/tools/testing/selftests/bpf/progs/test_oom_fail_ops_proactive.c
new file mode 100644
index 000000000000..24ba2bac1d85
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_oom_fail_ops_proactive.c
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("struct_ops.s/handle_out_of_memory")
+__failure __msg("calling kernel function bpf_oom_proactive_kill is not allowed")
+int BPF_PROG(ops_call_proactive_kill, struct oom_control *oc,
+ struct cgroup *cgrp)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+
+ return bpf_oom_proactive_kill(oc->memcg, task, "disallowed", 0);
+}
+
+SEC(".struct_ops.link")
+struct bpf_oom_ops fail_proactive_ops = {
+ .name = "fail_ops_proactive",
+ .handle_out_of_memory = (void *)ops_call_proactive_kill,
+};
diff --git a/tools/testing/selftests/bpf/progs/test_oom_fail_syscall.c b/tools/testing/selftests/bpf/progs/test_oom_fail_syscall.c
new file mode 100644
index 000000000000..e6aa7017f500
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/test_oom_fail_syscall.c
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include "bpf_misc.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("syscall")
+__failure __msg("calling kernel function bpf_oom_kill_process is not allowed")
+int syscall_call_oom_kill_process(void *ctx)
+{
+ struct task_struct *task = bpf_get_current_task_btf();
+
+ return bpf_oom_kill_process(NULL, task, "disallowed");
+}
--
2.56.1.911.g883d8bcb20-goog