mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-31 03:35:32 -04:00
selftests/bpf: Test direct reading/writing read-only percpu_array map
Verify these two cases: 1. Direct reading the data of read-only percpu data's percpu_array map is allowed. 2. Direct writing the data of read-only percpu data's percpu_array map is disallowed. Assisted-by: Codex:gpt-5.5-xhigh Signed-off-by: Leon Hwang <leon.hwang@linux.dev> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20260813152324.97937-9-leon.hwang@linux.dev
This commit is contained in:
committed by
Andrii Nakryiko
parent
4c9241bd73
commit
3993d5beef
@@ -236,6 +236,92 @@ static void test_global_percpu_data_lskel(void)
|
||||
test_global_percpu_data_lskel__destroy(lskel);
|
||||
}
|
||||
|
||||
static int create_rdonly_percpu_array(void)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_map_create_opts, map_opts,
|
||||
.map_flags = BPF_F_RDONLY_PROG,
|
||||
);
|
||||
int key = 0, map_fd, err;
|
||||
__u64 value = 0;
|
||||
|
||||
map_fd = bpf_map_create(BPF_MAP_TYPE_PERCPU_ARRAY, "percpu_ro_map", sizeof(int),
|
||||
sizeof(__u64), 1, &map_opts);
|
||||
if (!ASSERT_GE(map_fd, 0, "bpf_map_create"))
|
||||
return -1;
|
||||
|
||||
err = bpf_map_update_elem(map_fd, &key, &value, BPF_F_ALL_CPUS);
|
||||
if (!ASSERT_OK(err, "bpf_map_update_elem"))
|
||||
goto out;
|
||||
|
||||
err = bpf_map_freeze(map_fd);
|
||||
if (!ASSERT_OK(err, "bpf_map_freeze"))
|
||||
goto out;
|
||||
|
||||
return map_fd;
|
||||
|
||||
out:
|
||||
close(map_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
static void test_global_percpu_data_rdonly_direct_read(void)
|
||||
{
|
||||
/*
|
||||
* Raw instructions with manually prepared rdonly percpu_array map
|
||||
* for testing direct-read global percpu data, because libbpf
|
||||
* doesn't have rdonly internal percpu_array map support for
|
||||
* global percpu data.
|
||||
*/
|
||||
struct bpf_insn insns[] = {
|
||||
BPF_LD_MAP_VALUE(BPF_REG_1, 0, 0),
|
||||
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
};
|
||||
int map_fd, prog_fd;
|
||||
|
||||
map_fd = create_rdonly_percpu_array();
|
||||
if (map_fd < 0)
|
||||
return;
|
||||
|
||||
insns[0].imm = map_fd;
|
||||
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "percpu_ro_prog", "GPL", insns,
|
||||
ARRAY_SIZE(insns), NULL);
|
||||
if (ASSERT_GE(prog_fd, 0, "bpf_prog_load"))
|
||||
close(prog_fd);
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
static void test_global_percpu_data_rdonly_direct_write(void)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_prog_load_opts, prog_opts);
|
||||
/* See the comment in test_global_percpu_data_rdonly_direct_read() */
|
||||
struct bpf_insn insns[] = {
|
||||
BPF_LD_MAP_VALUE(BPF_REG_1, 0, 0),
|
||||
BPF_LDX_MEM(BPF_DW, BPF_REG_0, BPF_REG_1, 0),
|
||||
BPF_ST_MEM(BPF_DW, BPF_REG_1, 0, 0),
|
||||
BPF_EXIT_INSN(),
|
||||
};
|
||||
char log_buf[256] = {};
|
||||
int map_fd, prog_fd;
|
||||
|
||||
prog_opts.log_buf = log_buf;
|
||||
prog_opts.log_size = sizeof(log_buf);
|
||||
prog_opts.log_level = 1;
|
||||
|
||||
map_fd = create_rdonly_percpu_array();
|
||||
if (map_fd < 0)
|
||||
return;
|
||||
|
||||
insns[0].imm = map_fd;
|
||||
prog_fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, "percpu_ro_prog", "GPL", insns,
|
||||
ARRAY_SIZE(insns), &prog_opts);
|
||||
if (!ASSERT_LT(prog_fd, 0, "bpf_prog_load"))
|
||||
close(prog_fd);
|
||||
else
|
||||
ASSERT_HAS_SUBSTR(log_buf, "write into map forbidden", "verifier log");
|
||||
close(map_fd);
|
||||
}
|
||||
|
||||
void test_global_percpu_data(void)
|
||||
{
|
||||
if (!feat_supported(NULL, FEAT_PERCPU_DATA)) {
|
||||
@@ -247,4 +333,8 @@ void test_global_percpu_data(void)
|
||||
test_global_percpu_data_init();
|
||||
if (test__start_subtest("lskel"))
|
||||
test_global_percpu_data_lskel();
|
||||
if (test__start_subtest("rdonly_direct_read"))
|
||||
test_global_percpu_data_rdonly_direct_read();
|
||||
if (test__start_subtest("rdonly_direct_write"))
|
||||
test_global_percpu_data_rdonly_direct_write();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user