diff options
| author | Kui-Feng Lee <[email protected]> | 2024-02-21 18:11:05 -0800 | 
|---|---|---|
| committer | Martin KaFai Lau <[email protected]> | 2024-02-22 12:26:41 -0800 | 
| commit | e9bbda13a7b876451285ab15fb600b809e5e2290 (patch) | |
| tree | 5c5951094ff327b9e52343e25707d5cc944f2fdb /tools/testing/selftests/bpf/prog_tests | |
| parent | 3e0008336ae3153fb89b1a15bb877ddd38680fe6 (diff) | |
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 <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Martin KaFai Lau <[email protected]>
Diffstat (limited to 'tools/testing/selftests/bpf/prog_tests')
| -rw-r--r-- | tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c | 35 | 
1 files changed, 35 insertions, 0 deletions
| diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c new file mode 100644 index 000000000000..106ea447965a --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_no_cfi.c @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */ +#include <test_progs.h> +#include <testing_helpers.h> + +static void load_bpf_test_no_cfi(void) +{ +	int fd; +	int err; + +	fd = open("bpf_test_no_cfi.ko", O_RDONLY); +	if (!ASSERT_GE(fd, 0, "open")) +		return; + +	/* The module will try to register a struct_ops type without +	 * cfi_stubs and with cfi_stubs. +	 * +	 * The one without cfi_stub should fail. The module will be loaded +	 * successfully only if the result of the registration is as +	 * expected, or it fails. +	 */ +	err = finit_module(fd, "", 0); +	close(fd); +	if (!ASSERT_OK(err, "finit_module")) +		return; + +	err = delete_module("bpf_test_no_cfi", 0); +	ASSERT_OK(err, "delete_module"); +} + +void test_struct_ops_no_cfi(void) +{ +	if (test__start_subtest("load_bpf_test_no_cfi")) +		load_bpf_test_no_cfi(); +} |