Skip to content

Commit 64f50e6

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 Acked-by: Toke Høiland-Jørgensen <toke@redhat.com> Acked-by: John Fastabend <john.fastabend@gmail.com> Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
1 parent 24aa97f commit 64f50e6

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 21
53+
#define MAX_NR_MAPS 23
5454
#define MAX_TEST_RUNS 8
5555
#define POINTER_VALUE 0xcafe4all
5656
#define TEST_DATA_LEN 64
@@ -88,6 +88,8 @@ struct bpf_test {
8888
int fixup_map_event_output[MAX_FIXUPS];
8989
int fixup_map_reuseport_array[MAX_FIXUPS];
9090
int fixup_map_ringbuf[MAX_FIXUPS];
91+
int fixup_map_devmap[MAX_FIXUPS];
92+
int fixup_map_devmap_hash[MAX_FIXUPS];
9193
/* Expected verifier log output for result REJECT or VERBOSE_ACCEPT.
9294
* Can be a tab-separated sequence of expected strings. An empty string
9395
* means no log verification.
@@ -718,6 +720,8 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
718720
int *fixup_map_event_output = test->fixup_map_event_output;
719721
int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;
720722
int *fixup_map_ringbuf = test->fixup_map_ringbuf;
723+
int *fixup_map_devmap = test->fixup_map_devmap;
724+
int *fixup_map_devmap_hash = test->fixup_map_devmap_hash;
721725

722726
if (test->fill_helper) {
723727
test->fill_insns = calloc(MAX_TEST_INSNS, sizeof(struct bpf_insn));
@@ -903,6 +907,22 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,
903907
fixup_map_ringbuf++;
904908
} while (*fixup_map_ringbuf);
905909
}
910+
if (*fixup_map_devmap) {
911+
map_fds[20] = __create_map(BPF_MAP_TYPE_DEVMAP,
912+
sizeof(u32), sizeof(u32), 1, 0);
913+
do {
914+
prog[*fixup_map_devmap].imm = map_fds[20];
915+
fixup_map_devmap++;
916+
} while (*fixup_map_devmap);
917+
}
918+
if (*fixup_map_devmap_hash) {
919+
map_fds[21] = __create_map(BPF_MAP_TYPE_DEVMAP_HASH,
920+
sizeof(u32), sizeof(u32), 1, 0);
921+
do {
922+
prog[*fixup_map_devmap_hash].imm = map_fds[21];
923+
fixup_map_devmap_hash++;
924+
} while (*fixup_map_devmap_hash);
925+
}
906926
}
907927

908928
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)