Skip to content

Commit 9fc4476

Browse files
kkdwivediAlexei Starovoitov
authored and
Alexei Starovoitov
committed
selftests/bpf: Test for writes to map key from BPF helpers
When invoking bpf_for_each_map_elem callback, we are passed a PTR_TO_MAP_KEY, previously writes to this through helper may be allowed, but the fix in previous patches is meant to prevent that case. The test case tries to pass it as writable memory to helper, and fails test if it succeeds to pass the verifier. Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 7cb29b1 commit 9fc4476

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <network_helpers.h>
55
#include "for_each_hash_map_elem.skel.h"
66
#include "for_each_array_map_elem.skel.h"
7+
#include "for_each_map_elem_write_key.skel.h"
78

89
static unsigned int duration;
910

@@ -129,10 +130,21 @@ static void test_array_map(void)
129130
for_each_array_map_elem__destroy(skel);
130131
}
131132

133+
static void test_write_map_key(void)
134+
{
135+
struct for_each_map_elem_write_key *skel;
136+
137+
skel = for_each_map_elem_write_key__open_and_load();
138+
if (!ASSERT_ERR_PTR(skel, "for_each_map_elem_write_key__open_and_load"))
139+
for_each_map_elem_write_key__destroy(skel);
140+
}
141+
132142
void test_for_each(void)
133143
{
134144
if (test__start_subtest("hash_map"))
135145
test_hash_map();
136146
if (test__start_subtest("array_map"))
137147
test_array_map();
148+
if (test__start_subtest("write_map_key"))
149+
test_write_map_key();
138150
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <vmlinux.h>
3+
#include <bpf/bpf_helpers.h>
4+
5+
struct {
6+
__uint(type, BPF_MAP_TYPE_ARRAY);
7+
__uint(max_entries, 1);
8+
__type(key, __u32);
9+
__type(value, __u64);
10+
} array_map SEC(".maps");
11+
12+
static __u64
13+
check_array_elem(struct bpf_map *map, __u32 *key, __u64 *val,
14+
void *data)
15+
{
16+
bpf_get_current_comm(key, sizeof(*key));
17+
return 0;
18+
}
19+
20+
SEC("raw_tp/sys_enter")
21+
int test_map_key_write(const void *ctx)
22+
{
23+
bpf_for_each_map_elem(&array_map, check_array_elem, NULL, 0);
24+
return 0;
25+
}
26+
27+
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)