Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796245484; b=yLt6hJYq4SRMeRRkQuMOOL6p6VbnNbUT2miMTr9MQtuVcuSyoOQzigAnIEfLgoi49pw5 Sw5ajGsVisP5oKi6ppC/UHJ7fczSbyqMy1v52wbcB/JijHN4r09Ah3gvevMeaYjJWeu0c l8358O3aroXo5V/1dEegNvShivwM+79pIg+Q3fhZRq/RoMEqkLa2yG+4TfhcvB3geitEo mUA2Swp3WObsN9QzCrGgxMZ/QP7oL8QVEmRy9190oPjeakR70Dk0Co7Bd88+g61X94T8i aSgyTyyEu6NseZDkx2Jt7jExczfVku4n7KZl793Q7EaRxk/DrmhZR8LF9xIswPcaXbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796245484; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=NWeGMx8JwRICZbLhCDvD/uhWGMjI8IONuafq0FP66IY=; b=pO0fttV9WQm8/VsgCBzhWwO61Vrt9IsxmC9p976YfkkWkHw0GLZwnKFH9WwtgLjIAlvJ lfBWzmRzZChO1WjULJUAJ4NYfwPUByMx+51f1dG/S32Pm00+vP6UNO/WyOBiHJu04QVoX GzmuBK+3PKVq5KCLNpn6NdHd/WKg7mM9zpJcQW0dcLVmW2/4X/y41VxT2EIWj7ho5XtYX UzN6igoeV8ZBh+NHR0RF40NAWRJ0l6idXyZ/bgws3LDylNhEW8pl53n7ujfD8jSL3LeWu qQVwMW/KILpYXeecI85FxMh2oUeagqpyjcfO9nTtGdn3jUu+hacGo/OFcNh+E3A7NaQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--bgardon.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="cB//YBtr"; arc=none smtp.remote-ip=209.85.167.197 Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4F5560A3831 for ; Wed, 2 Dec 2026 21:04:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A4F5560A3831 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--bgardon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=cB//YBtr Received: by mail-oi1-f197.google.com with SMTP id 5614622812f47-4c467e19391so19136415b6e.3 for ; Wed, 02 Dec 2026 13:04:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796245483; x=1796850283; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NWeGMx8JwRICZbLhCDvD/uhWGMjI8IONuafq0FP66IY=; b=cB//YBtrEilzM6EZlSsu7vIl3S61HIIuFf4dA6+5j4XZPWLdt8LhFoUZR6nD81aPcB dPD4t+hFZooS+yobUHh8TE7URzfeCXjrEfgIbPtArBIxH+YKX/SmmK1qGCErilFomV5M i+LznvUGCjygnh/HbYEwkuqSxurjSSEYyzgYS5ceNXxqdM1IFG5m+KiYaPSiMAQ5nYi4 S6bXLQUq3CR1XxtJGkk917hkTDVJCRghfbgejT4Kjz5JcdLe8BymanZlsBEp04qdD6vS dTl681GVC9yu3Qa69RuilMdoe8WyB3dU0bU3/BoNOLtYWcpicvR7pqKnfyYHm4uYJeEA /dqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796245483; x=1796850283; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NWeGMx8JwRICZbLhCDvD/uhWGMjI8IONuafq0FP66IY=; b=yUhz6S/omF3cdZVnlNg8PwdYAZxIPT9iBHZUzenGor6Rp6Fy+l3sFnsmwhxi4vfevi 4Y2AwkFh5NkVKrgIkeNXvh5AClMMJyOqQY/xH8EBhF0sCsx4mBiu8myJnapD5Yig4TBG Ca3/pu88UspUBfGkdrs3iHtbedfhUwi8ot55dIOB7XEVvalgkuRfBTivQ9DMBlX+4Ove FHvX89gHhpMD6Puj0DshN7fgU3BNGa3DeXR55S2Ulptkjdj19U5Yfe/iNox5YJbEjsqM aZmH321YPfygZKncgnRva0Vw3AVRuLukHeuZvQ9S+N79bpAyBtOWGBktikQDSwfVYRck iRag== X-Forwarded-Encrypted: i=1; AL+hErGrJMQEim2pSkFUm3Vb3wtmG48kg32/80nHq/DPq0aGVi0AvxRf/W25vfa2M4uPlCE9e24=@vger.kernel.org X-Gm-Message-State: APuU4Z+0QZdidHPIeeehmVVWxCoXE0mdxM1ySs3hHzgeJtAxcPoS10bd j7otUJUDDFdWf0z+dQdwaTssvtAwCm08h7GTSD4VutOfPg3pn98YSRo/jJ3ggE1scJNHIsXsz3E HXSIp3VsSBg== X-Received: from jagp27-n2.prod.google.com ([2002:a05:6638:9b:20b0:61c:b6b7:f372]) (user=bgardon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:16a1:b0:572:d0fb:ae64 with SMTP id 5614622812f47-57a0bb6f16fmr40889340b6e.54.1796245482398; Wed, 02 Dec 2026 13:04:42 -0800 (PST) Date: Wed, 2 Dec 2026 13:04:04 -0800 In-Reply-To: <20261202210415.517651-1-bgardon@google.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202210415.517651-1-bgardon@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202210415.517651-15-bgardon@google.com> Subject: [PATCH bpf-next v4 14/17] selftests/bpf: Add negative and verifier tests for BPF OOM kfuncs From: Ben Gardon To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Andrew Morton , Johannes Weiner , Michal Hocko , Shakeel Butt , Roman Gushchin , Tejun Heo Cc: JP Kobryn , Eduard Zingerman , Kumar Kartikeya Dwivedi , Martin KaFai Lau , Song Liu , Yonghong Song , Jiri Olsa , Emil Tsalapatis , Ihor Solodrai , David Rientjes , Suren Baghdasaryan , Josh Don , Sweet Tea Dorminy , Chuyi Zhou , Yafang Shao , benbwgardon@gmail.com, bpf@vger.kernel.org, linux-mm@kvack.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, Ben Gardon , Shuah Khan , linux-kselftest@vger.kernel.org Content-Type: text/plain; charset="UTF-8" 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 --- .../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 +#include +#include +#include + +#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 +#include +#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 +#include +#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 +#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