Skip to content

Commit 63a9936

Browse files
Yonghong Songanakryiko
authored andcommitted
selftests/bpf: Add tests for ldsx of pkt data/data_end/data_meta accesses
The following tests are added to verifier_ldsx.c: - sign extension of data/data_end/data_meta for tcx programs. The actual checking is in bpf_skb_is_valid_access() which is called by sk_filter, cg_skb, lwt, tc(tcx) and sk_skb. - sign extension of data/data_end/data_meta for xdp programs. - sign extension of data/data_end for flow_dissector programs. All newly-added tests have verification failure with message "invalid bpf_context access". Without previous patch, all these tests succeeded verification. Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]>
1 parent 92de360 commit 63a9936

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

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

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,118 @@ __naked void ldsx_s32_range(void)
144144
: __clobber_all);
145145
}
146146

147+
SEC("xdp")
148+
__description("LDSX, xdp s32 xdp_md->data")
149+
__failure __msg("invalid bpf_context access")
150+
__naked void ldsx_ctx_1(void)
151+
{
152+
asm volatile (
153+
"r2 = *(s32 *)(r1 + %[xdp_md_data]);"
154+
"r0 = 0;"
155+
"exit;"
156+
:
157+
: __imm_const(xdp_md_data, offsetof(struct xdp_md, data))
158+
: __clobber_all);
159+
}
160+
161+
SEC("xdp")
162+
__description("LDSX, xdp s32 xdp_md->data_end")
163+
__failure __msg("invalid bpf_context access")
164+
__naked void ldsx_ctx_2(void)
165+
{
166+
asm volatile (
167+
"r2 = *(s32 *)(r1 + %[xdp_md_data_end]);"
168+
"r0 = 0;"
169+
"exit;"
170+
:
171+
: __imm_const(xdp_md_data_end, offsetof(struct xdp_md, data_end))
172+
: __clobber_all);
173+
}
174+
175+
SEC("xdp")
176+
__description("LDSX, xdp s32 xdp_md->data_meta")
177+
__failure __msg("invalid bpf_context access")
178+
__naked void ldsx_ctx_3(void)
179+
{
180+
asm volatile (
181+
"r2 = *(s32 *)(r1 + %[xdp_md_data_meta]);"
182+
"r0 = 0;"
183+
"exit;"
184+
:
185+
: __imm_const(xdp_md_data_meta, offsetof(struct xdp_md, data_meta))
186+
: __clobber_all);
187+
}
188+
189+
SEC("tcx/ingress")
190+
__description("LDSX, tcx s32 __sk_buff->data")
191+
__failure __msg("invalid bpf_context access")
192+
__naked void ldsx_ctx_4(void)
193+
{
194+
asm volatile (
195+
"r2 = *(s32 *)(r1 + %[sk_buff_data]);"
196+
"r0 = 0;"
197+
"exit;"
198+
:
199+
: __imm_const(sk_buff_data, offsetof(struct __sk_buff, data))
200+
: __clobber_all);
201+
}
202+
203+
SEC("tcx/ingress")
204+
__description("LDSX, tcx s32 __sk_buff->data_end")
205+
__failure __msg("invalid bpf_context access")
206+
__naked void ldsx_ctx_5(void)
207+
{
208+
asm volatile (
209+
"r2 = *(s32 *)(r1 + %[sk_buff_data_end]);"
210+
"r0 = 0;"
211+
"exit;"
212+
:
213+
: __imm_const(sk_buff_data_end, offsetof(struct __sk_buff, data_end))
214+
: __clobber_all);
215+
}
216+
217+
SEC("tcx/ingress")
218+
__description("LDSX, tcx s32 __sk_buff->data_meta")
219+
__failure __msg("invalid bpf_context access")
220+
__naked void ldsx_ctx_6(void)
221+
{
222+
asm volatile (
223+
"r2 = *(s32 *)(r1 + %[sk_buff_data_meta]);"
224+
"r0 = 0;"
225+
"exit;"
226+
:
227+
: __imm_const(sk_buff_data_meta, offsetof(struct __sk_buff, data_meta))
228+
: __clobber_all);
229+
}
230+
231+
SEC("flow_dissector")
232+
__description("LDSX, flow_dissector s32 __sk_buff->data")
233+
__failure __msg("invalid bpf_context access")
234+
__naked void ldsx_ctx_7(void)
235+
{
236+
asm volatile (
237+
"r2 = *(s32 *)(r1 + %[sk_buff_data]);"
238+
"r0 = 0;"
239+
"exit;"
240+
:
241+
: __imm_const(sk_buff_data, offsetof(struct __sk_buff, data))
242+
: __clobber_all);
243+
}
244+
245+
SEC("flow_dissector")
246+
__description("LDSX, flow_dissector s32 __sk_buff->data_end")
247+
__failure __msg("invalid bpf_context access")
248+
__naked void ldsx_ctx_8(void)
249+
{
250+
asm volatile (
251+
"r2 = *(s32 *)(r1 + %[sk_buff_data_end]);"
252+
"r0 = 0;"
253+
"exit;"
254+
:
255+
: __imm_const(sk_buff_data_end, offsetof(struct __sk_buff, data_end))
256+
: __clobber_all);
257+
}
258+
147259
#else
148260

149261
SEC("socket")

0 commit comments

Comments
 (0)