Skip to content

Commit 3515020

Browse files
eddyz87Alexei Starovoitov
authored andcommitted
selftests/bpf: verifier/prevent_map_lookup converted to inline assembly
Test verifier/prevent_map_lookup automatically converted to use inline assembly. This was a part of a series [1] but could not be applied becuase another patch from a series had to be witheld. [1] https://lore.kernel.org/bpf/[email protected]/ Signed-off-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 6d26d98 commit 3515020

File tree

3 files changed

+63
-29
lines changed

3 files changed

+63
-29
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "verifier_meta_access.skel.h"
4343
#include "verifier_netfilter_ctx.skel.h"
4444
#include "verifier_netfilter_retcode.skel.h"
45+
#include "verifier_prevent_map_lookup.skel.h"
4546
#include "verifier_raw_stack.skel.h"
4647
#include "verifier_raw_tp_writable.skel.h"
4748
#include "verifier_reg_equal.skel.h"
@@ -140,6 +141,7 @@ void test_verifier_masking(void) { RUN(verifier_masking); }
140141
void test_verifier_meta_access(void) { RUN(verifier_meta_access); }
141142
void test_verifier_netfilter_ctx(void) { RUN(verifier_netfilter_ctx); }
142143
void test_verifier_netfilter_retcode(void) { RUN(verifier_netfilter_retcode); }
144+
void test_verifier_prevent_map_lookup(void) { RUN(verifier_prevent_map_lookup); }
143145
void test_verifier_raw_stack(void) { RUN(verifier_raw_stack); }
144146
void test_verifier_raw_tp_writable(void) { RUN(verifier_raw_tp_writable); }
145147
void test_verifier_reg_equal(void) { RUN(verifier_reg_equal); }
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/* Converted from tools/testing/selftests/bpf/verifier/prevent_map_lookup.c */
3+
4+
#include <linux/bpf.h>
5+
#include <bpf/bpf_helpers.h>
6+
#include "bpf_misc.h"
7+
8+
struct {
9+
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
10+
__uint(max_entries, 1);
11+
__type(key, __u32);
12+
__type(value, __u64);
13+
} map_stacktrace SEC(".maps");
14+
15+
struct {
16+
__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
17+
__uint(max_entries, 8);
18+
__uint(key_size, sizeof(int));
19+
__array(values, void (void));
20+
} map_prog2_socket SEC(".maps");
21+
22+
SEC("perf_event")
23+
__description("prevent map lookup in stack trace")
24+
__failure __msg("cannot pass map_type 7 into func bpf_map_lookup_elem")
25+
__naked void map_lookup_in_stack_trace(void)
26+
{
27+
asm volatile (" \
28+
r1 = 0; \
29+
*(u64*)(r10 - 8) = r1; \
30+
r2 = r10; \
31+
r2 += -8; \
32+
r1 = %[map_stacktrace] ll; \
33+
call %[bpf_map_lookup_elem]; \
34+
exit; \
35+
" :
36+
: __imm(bpf_map_lookup_elem),
37+
__imm_addr(map_stacktrace)
38+
: __clobber_all);
39+
}
40+
41+
SEC("socket")
42+
__description("prevent map lookup in prog array")
43+
__failure __msg("cannot pass map_type 3 into func bpf_map_lookup_elem")
44+
__failure_unpriv
45+
__naked void map_lookup_in_prog_array(void)
46+
{
47+
asm volatile (" \
48+
r1 = 0; \
49+
*(u64*)(r10 - 8) = r1; \
50+
r2 = r10; \
51+
r2 += -8; \
52+
r1 = %[map_prog2_socket] ll; \
53+
call %[bpf_map_lookup_elem]; \
54+
exit; \
55+
" :
56+
: __imm(bpf_map_lookup_elem),
57+
__imm_addr(map_prog2_socket)
58+
: __clobber_all);
59+
}
60+
61+
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/verifier/prevent_map_lookup.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)