Skip to content

Commit 8dedd34

Browse files
borkmannAlexei Starovoitov
authored andcommitted
selftests/bpf: Test that exclusive maps are rejected as iter targets
Add a subtest to map_excl that creates an exclusive map and verifies a bpf_map_elem iterator cannot be attached to it, which would otherwise let an unrelated program read and overwrite the map's contents through the iterator's writable value buffer. # LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t map_excl [...] ./test_progs -t map_excl [ 1.704382] bpf_testmod: loading out-of-tree module taints kernel. [ 1.706068] bpf_testmod: module verification failed: signature and/or required key missing - tainting kernel #215/1 map_excl/map_excl_allowed:OK #215/2 map_excl/map_excl_denied:OK #215/3 map_excl/map_excl_no_map_in_map:OK #215/4 map_excl/map_excl_no_map_iter:OK #215 map_excl:OK Summary: 1/4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Link: https://lore.kernel.org/r/20260602133052.423725-5-daniel@iogearbox.net Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 082c412 commit 8dedd34

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <bpf/btf.h>
88

99
#include "map_excl.skel.h"
10+
#include "bpf_iter_bpf_array_map.skel.h"
1011

1112
#ifndef SHA256_DIGEST_SIZE
1213
#define SHA256_DIGEST_SIZE 32
@@ -89,6 +90,42 @@ static void test_map_excl_no_map_in_map(void)
8990
close(excl_fd);
9091
}
9192

93+
static void test_map_excl_no_map_iter(void)
94+
{
95+
__u8 hash[SHA256_DIGEST_SIZE] = {};
96+
LIBBPF_OPTS(bpf_map_create_opts, excl_opts,
97+
.excl_prog_hash = hash,
98+
.excl_prog_hash_size = sizeof(hash));
99+
DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts);
100+
struct bpf_iter_bpf_array_map *skel = NULL;
101+
union bpf_iter_link_info linfo;
102+
struct bpf_link *link;
103+
int excl_fd;
104+
105+
excl_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "excl_iter", 4, 8, 3, &excl_opts);
106+
if (!ASSERT_OK_FD(excl_fd, "create exclusive map"))
107+
return;
108+
109+
skel = bpf_iter_bpf_array_map__open_and_load();
110+
if (!ASSERT_OK_PTR(skel, "bpf_iter_bpf_array_map__open_and_load"))
111+
goto out;
112+
113+
memset(&linfo, 0, sizeof(linfo));
114+
linfo.map.map_fd = excl_fd;
115+
opts.link_info = &linfo;
116+
opts.link_info_len = sizeof(linfo);
117+
118+
link = bpf_program__attach_iter(skel->progs.dump_bpf_array_map, &opts);
119+
if (!ASSERT_ERR_PTR(link, "reject exclusive map as iter target")) {
120+
bpf_link__destroy(link);
121+
goto out;
122+
}
123+
ASSERT_EQ(libbpf_get_error(link), -EPERM, "iter attach errno");
124+
out:
125+
bpf_iter_bpf_array_map__destroy(skel);
126+
close(excl_fd);
127+
}
128+
92129
void test_map_excl(void)
93130
{
94131
if (test__start_subtest("map_excl_allowed"))
@@ -97,4 +134,6 @@ void test_map_excl(void)
97134
test_map_excl_denied();
98135
if (test__start_subtest("map_excl_no_map_in_map"))
99136
test_map_excl_no_map_in_map();
137+
if (test__start_subtest("map_excl_no_map_iter"))
138+
test_map_excl_no_map_iter();
100139
}

0 commit comments

Comments
 (0)