Skip to content

Commit e9bbda1

Browse files
ThinkerYzu1Martin KaFai Lau
authored andcommitted
selftests/bpf: Test case for lacking CFI stub functions.
Ensure struct_ops rejects the registration of struct_ops types without proper CFI stub functions. bpf_test_no_cfi.ko is a module that attempts to register a struct_ops type called "bpf_test_no_cfi_ops" with cfi_stubs of NULL and non-NULL value. The NULL one should fail, and the non-NULL one should succeed. The module can only be loaded successfully if these registrations yield the expected results. Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com> Link: https://lore.kernel.org/r/20240222021105.1180475-3-thinker.li@gmail.com Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
1 parent 3e00083 commit e9bbda1

6 files changed

Lines changed: 151 additions & 3 deletions

File tree

tools/testing/selftests/bpf/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ TEST_GEN_PROGS_EXTENDED = test_sock_addr test_skb_cgroup_id_user \
132132
flow_dissector_load test_flow_dissector test_tcp_check_syncookie_user \
133133
test_lirc_mode2_user xdping test_cpp runqslower bench bpf_testmod.ko \
134134
xskxceiver xdp_redirect_multi xdp_synproxy veristat xdp_hw_metadata \
135-
xdp_features
135+
xdp_features bpf_test_no_cfi.ko
136136

137137
TEST_GEN_FILES += liburandom_read.so urandom_read sign-file uprobe_multi
138138

@@ -254,6 +254,12 @@ $(OUTPUT)/bpf_testmod.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_testmo
254254
$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_testmod
255255
$(Q)cp bpf_testmod/bpf_testmod.ko $@
256256

257+
$(OUTPUT)/bpf_test_no_cfi.ko: $(VMLINUX_BTF) $(RESOLVE_BTFIDS) $(wildcard bpf_test_no_cfi/Makefile bpf_test_no_cfi/*.[ch])
258+
$(call msg,MOD,,$@)
259+
$(Q)$(RM) bpf_test_no_cfi/bpf_test_no_cfi.ko # force re-compilation
260+
$(Q)$(MAKE) $(submake_extras) RESOLVE_BTFIDS=$(RESOLVE_BTFIDS) -C bpf_test_no_cfi
261+
$(Q)cp bpf_test_no_cfi/bpf_test_no_cfi.ko $@
262+
257263
DEFAULT_BPFTOOL := $(HOST_SCRATCH_DIR)/sbin/bpftool
258264
ifneq ($(CROSS_COMPILE),)
259265
CROSS_BPFTOOL := $(SCRATCH_DIR)/sbin/bpftool
@@ -631,6 +637,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
631637
flow_dissector_load.h \
632638
ip_check_defrag_frags.h
633639
TRUNNER_EXTRA_FILES := $(OUTPUT)/urandom_read $(OUTPUT)/bpf_testmod.ko \
640+
$(OUTPUT)/bpf_test_no_cfi.ko \
634641
$(OUTPUT)/liburandom_read.so \
635642
$(OUTPUT)/xdp_synproxy \
636643
$(OUTPUT)/sign-file \
@@ -759,6 +766,7 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
759766
feature bpftool \
760767
$(addprefix $(OUTPUT)/,*.o *.skel.h *.lskel.h *.subskel.h \
761768
no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \
769+
bpf_test_no_cfi.ko \
762770
liburandom_read.so)
763771

764772
.PHONY: docs docs-clean
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
BPF_TEST_NO_CFI_DIR := $(realpath $(dir $(abspath $(lastword $(MAKEFILE_LIST)))))
2+
KDIR ?= $(abspath $(BPF_TEST_NO_CFI_DIR)/../../../../..)
3+
4+
ifeq ($(V),1)
5+
Q =
6+
else
7+
Q = @
8+
endif
9+
10+
MODULES = bpf_test_no_cfi.ko
11+
12+
obj-m += bpf_test_no_cfi.o
13+
14+
all:
15+
+$(Q)make -C $(KDIR) M=$(BPF_TEST_NO_CFI_DIR) modules
16+
17+
clean:
18+
+$(Q)make -C $(KDIR) M=$(BPF_TEST_NO_CFI_DIR) clean
19+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
#include <linux/bpf.h>
4+
#include <linux/btf.h>
5+
#include <linux/init.h>
6+
#include <linux/module.h>
7+
8+
struct bpf_test_no_cfi_ops {
9+
void (*fn_1)(void);
10+
void (*fn_2)(void);
11+
};
12+
13+
static int dummy_init(struct btf *btf)
14+
{
15+
return 0;
16+
}
17+
18+
static int dummy_init_member(const struct btf_type *t,
19+
const struct btf_member *member,
20+
void *kdata, const void *udata)
21+
{
22+
return 0;
23+
}
24+
25+
static int dummy_reg(void *kdata)
26+
{
27+
return 0;
28+
}
29+
30+
static void dummy_unreg(void *kdata)
31+
{
32+
}
33+
34+
static const struct bpf_verifier_ops dummy_verifier_ops;
35+
36+
static void bpf_test_no_cfi_ops__fn_1(void)
37+
{
38+
}
39+
40+
static void bpf_test_no_cfi_ops__fn_2(void)
41+
{
42+
}
43+
44+
static struct bpf_test_no_cfi_ops __test_no_cif_ops = {
45+
.fn_1 = bpf_test_no_cfi_ops__fn_1,
46+
.fn_2 = bpf_test_no_cfi_ops__fn_2,
47+
};
48+
49+
static struct bpf_struct_ops test_no_cif_ops = {
50+
.verifier_ops = &dummy_verifier_ops,
51+
.init = dummy_init,
52+
.init_member = dummy_init_member,
53+
.reg = dummy_reg,
54+
.unreg = dummy_unreg,
55+
.name = "bpf_test_no_cfi_ops",
56+
.owner = THIS_MODULE,
57+
};
58+
59+
static int bpf_test_no_cfi_init(void)
60+
{
61+
int ret;
62+
63+
ret = register_bpf_struct_ops(&test_no_cif_ops,
64+
bpf_test_no_cfi_ops);
65+
if (!ret)
66+
return -EINVAL;
67+
68+
test_no_cif_ops.cfi_stubs = &__test_no_cif_ops;
69+
ret = register_bpf_struct_ops(&test_no_cif_ops,
70+
bpf_test_no_cfi_ops);
71+
return ret;
72+
}
73+
74+
static void bpf_test_no_cfi_exit(void)
75+
{
76+
}
77+
78+
module_init(bpf_test_no_cfi_init);
79+
module_exit(bpf_test_no_cfi_exit);
80+
81+
MODULE_AUTHOR("Kuifeng Lee");
82+
MODULE_DESCRIPTION("BPF no cfi_stubs test module");
83+
MODULE_LICENSE("Dual BSD/GPL");
84+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
3+
#include <test_progs.h>
4+
#include <testing_helpers.h>
5+
6+
static void load_bpf_test_no_cfi(void)
7+
{
8+
int fd;
9+
int err;
10+
11+
fd = open("bpf_test_no_cfi.ko", O_RDONLY);
12+
if (!ASSERT_GE(fd, 0, "open"))
13+
return;
14+
15+
/* The module will try to register a struct_ops type without
16+
* cfi_stubs and with cfi_stubs.
17+
*
18+
* The one without cfi_stub should fail. The module will be loaded
19+
* successfully only if the result of the registration is as
20+
* expected, or it fails.
21+
*/
22+
err = finit_module(fd, "", 0);
23+
close(fd);
24+
if (!ASSERT_OK(err, "finit_module"))
25+
return;
26+
27+
err = delete_module("bpf_test_no_cfi", 0);
28+
ASSERT_OK(err, "delete_module");
29+
}
30+
31+
void test_struct_ops_no_cfi(void)
32+
{
33+
if (test__start_subtest("load_bpf_test_no_cfi"))
34+
load_bpf_test_no_cfi();
35+
}

tools/testing/selftests/bpf/testing_helpers.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,12 @@ __u64 read_perf_max_sample_freq(void)
356356
return sample_freq;
357357
}
358358

359-
static int finit_module(int fd, const char *param_values, int flags)
359+
int finit_module(int fd, const char *param_values, int flags)
360360
{
361361
return syscall(__NR_finit_module, fd, param_values, flags);
362362
}
363363

364-
static int delete_module(const char *name, int flags)
364+
int delete_module(const char *name, int flags)
365365
{
366366
return syscall(__NR_delete_module, name, flags);
367367
}

tools/testing/selftests/bpf/testing_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ __u64 read_perf_max_sample_freq(void);
3636
int load_bpf_testmod(bool verbose);
3737
int unload_bpf_testmod(bool verbose);
3838
int kern_sync_rcu(void);
39+
int finit_module(int fd, const char *param_values, int flags);
40+
int delete_module(const char *name, int flags);
3941

4042
static inline __u64 get_time_ns(void)
4143
{

0 commit comments

Comments
 (0)