Skip to content

Commit 7218750

Browse files
andreimateianakryiko
authored andcommitted
bpf: Add a possibly-zero-sized read test
This patch adds a test for the condition that the previous patch mucked with - illegal zero-sized helper memory access. As opposed to existing tests, this new one uses a size whose lower bound is zero, as opposed to a known-zero one. Signed-off-by: Andrei Matei <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8a021e7 commit 7218750

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

tools/testing/selftests/bpf/progs/verifier_helper_value_access.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,14 @@ l0_%=: exit; \
8989
: __clobber_all);
9090
}
9191

92+
/* Call a function taking a pointer and a size which doesn't allow the size to
93+
* be zero (i.e. bpf_trace_printk() declares the second argument to be
94+
* ARG_CONST_SIZE, not ARG_CONST_SIZE_OR_ZERO). We attempt to pass zero for the
95+
* size and expect to fail.
96+
*/
9297
SEC("tracepoint")
9398
__description("helper access to map: empty range")
94-
__failure __msg("R2 invalid zero-sized read")
99+
__failure __msg("R2 invalid zero-sized read: u64=[0,0]")
95100
__naked void access_to_map_empty_range(void)
96101
{
97102
asm volatile (" \
@@ -113,6 +118,38 @@ l0_%=: exit; \
113118
: __clobber_all);
114119
}
115120

121+
/* Like the test above, but this time the size register is not known to be zero;
122+
* its lower-bound is zero though, which is still unacceptable.
123+
*/
124+
SEC("tracepoint")
125+
__description("helper access to map: possibly-empty ange")
126+
__failure __msg("R2 invalid zero-sized read: u64=[0,4]")
127+
__naked void access_to_map_possibly_empty_range(void)
128+
{
129+
asm volatile (" \
130+
r2 = r10; \
131+
r2 += -8; \
132+
r1 = 0; \
133+
*(u64*)(r2 + 0) = r1; \
134+
r1 = %[map_hash_48b] ll; \
135+
call %[bpf_map_lookup_elem]; \
136+
if r0 == 0 goto l0_%=; \
137+
r1 = r0; \
138+
/* Read an unknown value */ \
139+
r7 = *(u64*)(r0 + 0); \
140+
/* Make it small and positive, to avoid other errors */ \
141+
r7 &= 4; \
142+
r2 = 0; \
143+
r2 += r7; \
144+
call %[bpf_trace_printk]; \
145+
l0_%=: exit; \
146+
" :
147+
: __imm(bpf_map_lookup_elem),
148+
__imm(bpf_trace_printk),
149+
__imm_addr(map_hash_48b)
150+
: __clobber_all);
151+
}
152+
116153
SEC("tracepoint")
117154
__description("helper access to map: out-of-bound range")
118155
__failure __msg("invalid access to map value, value_size=48 off=0 size=56")

0 commit comments

Comments
 (0)