Skip to content

Commit db5bfaf

Browse files
liuhangbinkernel-patches-bot
authored andcommitted
selftests/bpf: Add verifier tests for bpf arg ARG_CONST_MAP_PTR_OR_NULL
Use helper bpf_redirect_map() and bpf_redirect_map_multi() to test bpf arg ARG_CONST_MAP_PTR and ARG_CONST_MAP_PTR_OR_NULL. Make sure the map arg could be verified correctly when it is NULL or valid map pointer. Add devmap and devmap_hash in struct bpf_test due to bpf_redirect_{map, map_multi} limit. Test result: ]# ./test_verifier 713 716 #713/p ARG_CONST_MAP_PTR: null pointer OK #714/p ARG_CONST_MAP_PTR: valid map pointer OK #715/p ARG_CONST_MAP_PTR_OR_NULL: null pointer for ex_map OK #716/p ARG_CONST_MAP_PTR_OR_NULL: valid map pointer for ex_map OK Summary: 4 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
1 parent f2f7cfa commit db5bfaf

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
#define MAX_INSNS BPF_MAXINSNS
5151
#define MAX_TEST_INSNS 1000000
5252
#define MAX_FIXUPS 8
53-
#define MAX_NR_MAPS 20
53+
#define MAX_NR_MAPS 22
5454
#define MAX_TEST_RUNS 8
5555
#define POINTER_VALUE 0xcafe4all
5656
#define TEST_DATA_LEN 64
@@ -87,6 +87,8 @@ struct bpf_test {
8787
int fixup_sk_storage_map[MAX_FIXUPS];
8888
int fixup_map_event_output[MAX_FIXUPS];
8989
int fixup_map_reuseport_array[MAX_FIXUPS];
90+
int fixup_map_devmap[MAX_FIXUPS];
91+
int fixup_map_devmap_hash[MAX_FIXUPS];
9092
const char *errstr;
9193
const char *errstr_unpriv;
9294
uint32_t insn_processed;
@@ -640,6 +642,8 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
640642
int *fixup_sk_storage_map = test->fixup_sk_storage_map;
641643
int *fixup_map_event_output = test->fixup_map_event_output;
642644
int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;
645+
int *fixup_map_devmap = test->fixup_map_devmap;
646+
int *fixup_map_devmap_hash = test->fixup_map_devmap_hash;
643647

644648
if (test->fill_helper) {
645649
test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn));
@@ -817,6 +821,22 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
817821
fixup_map_reuseport_array++;
818822
} while (*fixup_map_reuseport_array);
819823
}
824+
if (*fixup_map_devmap) {
825+
map_fds[20] = __create_map(BPF_MAP_TYPE_DEVMAP,
826+
sizeof(u32), sizeof(u32), 1, 0);
827+
do {
828+
prog[*fixup_map_devmap].imm = map_fds[20];
829+
fixup_map_devmap++;
830+
} while (*fixup_map_devmap);
831+
}
832+
if (*fixup_map_devmap_hash) {
833+
map_fds[21] = __create_map(BPF_MAP_TYPE_DEVMAP_HASH,
834+
sizeof(u32), sizeof(u32), 1, 0);
835+
do {
836+
prog[*fixup_map_devmap_hash].imm = map_fds[21];
837+
fixup_map_devmap_hash++;
838+
} while (*fixup_map_devmap_hash);
839+
}
820840
}
821841

822842
struct libcap {

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

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,73 @@
9393
.fixup_map_hash_16b = { 4 },
9494
.result = ACCEPT,
9595
},
96+
{
97+
"ARG_CONST_MAP_PTR: null pointer",
98+
.insns = {
99+
/* bpf_redirect_map arg1 (map) */
100+
BPF_MOV64_IMM(BPF_REG_1, 0),
101+
/* bpf_redirect_map arg2 (ifindex) */
102+
BPF_MOV64_IMM(BPF_REG_2, 0),
103+
/* bpf_redirect_map arg3 (flags) */
104+
BPF_MOV64_IMM(BPF_REG_3, 0),
105+
BPF_EMIT_CALL(BPF_FUNC_redirect_map),
106+
BPF_EXIT_INSN(),
107+
},
108+
.result = REJECT,
109+
.prog_type = BPF_PROG_TYPE_XDP,
110+
.errstr = "R1 type=inv expected=map_ptr",
111+
},
112+
{
113+
"ARG_CONST_MAP_PTR: valid map pointer",
114+
.insns = {
115+
BPF_MOV64_IMM(BPF_REG_1, 0),
116+
/* bpf_redirect_map arg1 (map) */
117+
BPF_LD_MAP_FD(BPF_REG_1, 0),
118+
/* bpf_redirect_map arg2 (ifindex) */
119+
BPF_MOV64_IMM(BPF_REG_2, 0),
120+
/* bpf_redirect_map arg3 (flags) */
121+
BPF_MOV64_IMM(BPF_REG_3, 0),
122+
BPF_EMIT_CALL(BPF_FUNC_redirect_map),
123+
BPF_EXIT_INSN(),
124+
},
125+
.fixup_map_devmap = { 1 },
126+
.result = ACCEPT,
127+
.prog_type = BPF_PROG_TYPE_XDP,
128+
},
129+
{
130+
"ARG_CONST_MAP_PTR_OR_NULL: null pointer for ex_map",
131+
.insns = {
132+
BPF_MOV64_IMM(BPF_REG_1, 0),
133+
/* bpf_redirect_map_multi arg1 (in_map) */
134+
BPF_LD_MAP_FD(BPF_REG_1, 0),
135+
/* bpf_redirect_map_multi arg2 (ex_map) */
136+
BPF_MOV64_IMM(BPF_REG_2, 0),
137+
/* bpf_redirect_map_multi arg3 (flags) */
138+
BPF_MOV64_IMM(BPF_REG_3, 0),
139+
BPF_EMIT_CALL(BPF_FUNC_redirect_map_multi),
140+
BPF_EXIT_INSN(),
141+
},
142+
.fixup_map_devmap = { 1 },
143+
.result = ACCEPT,
144+
.prog_type = BPF_PROG_TYPE_XDP,
145+
.retval = 4,
146+
},
147+
{
148+
"ARG_CONST_MAP_PTR_OR_NULL: valid map pointer for ex_map",
149+
.insns = {
150+
BPF_MOV64_IMM(BPF_REG_1, 0),
151+
/* bpf_redirect_map_multi arg1 (in_map) */
152+
BPF_LD_MAP_FD(BPF_REG_1, 0),
153+
/* bpf_redirect_map_multi arg2 (ex_map) */
154+
BPF_LD_MAP_FD(BPF_REG_2, 1),
155+
/* bpf_redirect_map_multi arg3 (flags) */
156+
BPF_MOV64_IMM(BPF_REG_3, 0),
157+
BPF_EMIT_CALL(BPF_FUNC_redirect_map_multi),
158+
BPF_EXIT_INSN(),
159+
},
160+
.fixup_map_devmap = { 1 },
161+
.fixup_map_devmap_hash = { 3 },
162+
.result = ACCEPT,
163+
.prog_type = BPF_PROG_TYPE_XDP,
164+
.retval = 4,
165+
},

0 commit comments

Comments
 (0)