Skip to content

Commit 9d66d60

Browse files
anakryikoMartin KaFai Lau
authored andcommitted
selftests/bpf: add another struct_ops callback use case test
Add a test which tests the case that was just fixed. Kernel has full type information about callback, but user explicitly nulls out the reference to declaratively set BPF program reference. Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/r/20240507001335.1445325-4-andrii@kernel.org Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent e18e2e7 commit 9d66d60

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

tools/testing/selftests/bpf/prog_tests/test_struct_ops_module.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <time.h>
55

66
#include "struct_ops_module.skel.h"
7+
#include "struct_ops_nulled_out_cb.skel.h"
78

89
static void check_map_info(struct bpf_map_info *info)
910
{
@@ -174,6 +175,30 @@ static void test_struct_ops_incompatible(void)
174175
struct_ops_module__destroy(skel);
175176
}
176177

178+
/* validate that it's ok to "turn off" callback that kernel supports */
179+
static void test_struct_ops_nulled_out_cb(void)
180+
{
181+
struct struct_ops_nulled_out_cb *skel;
182+
int err;
183+
184+
skel = struct_ops_nulled_out_cb__open();
185+
if (!ASSERT_OK_PTR(skel, "skel_open"))
186+
return;
187+
188+
/* kernel knows about test_1, but we still null it out */
189+
skel->struct_ops.ops->test_1 = NULL;
190+
191+
err = struct_ops_nulled_out_cb__load(skel);
192+
if (!ASSERT_OK(err, "skel_load"))
193+
goto cleanup;
194+
195+
ASSERT_FALSE(bpf_program__autoload(skel->progs.test_1_turn_off), "prog_autoload");
196+
ASSERT_LT(bpf_program__fd(skel->progs.test_1_turn_off), 0, "prog_fd");
197+
198+
cleanup:
199+
struct_ops_nulled_out_cb__destroy(skel);
200+
}
201+
177202
void serial_test_struct_ops_module(void)
178203
{
179204
if (test__start_subtest("test_struct_ops_load"))
@@ -182,5 +207,7 @@ void serial_test_struct_ops_module(void)
182207
test_struct_ops_not_zeroed();
183208
if (test__start_subtest("test_struct_ops_incompatible"))
184209
test_struct_ops_incompatible();
210+
if (test__start_subtest("test_struct_ops_null_out_cb"))
211+
test_struct_ops_nulled_out_cb();
185212
}
186213

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
#include <vmlinux.h>
4+
#include <bpf/bpf_tracing.h>
5+
#include "../bpf_testmod/bpf_testmod.h"
6+
7+
char _license[] SEC("license") = "GPL";
8+
9+
int rand;
10+
int arr[1];
11+
12+
SEC("struct_ops/test_1")
13+
int BPF_PROG(test_1_turn_off)
14+
{
15+
return arr[rand]; /* potentially way out of range access */
16+
}
17+
18+
SEC(".struct_ops.link")
19+
struct bpf_testmod_ops ops = {
20+
.test_1 = (void *)test_1_turn_off,
21+
};
22+

0 commit comments

Comments
 (0)