Skip to content

Commit 778e971

Browse files
pchaignoKernel Patches Daemon
authored andcommitted
selftests/bpf: Test direct packet access on non-linear skbs
This patch adds new selftests in the direct packet access suite, to cover the non-linear case. The three first tests cover the behavior of the bounds check with a non-linear skb (first two with min. linear size, third with long enough linear size). The last test adds a call to bpf_skb_pull_data() to be able to access the packet. Signed-off-by: Paul Chaignon <[email protected]>
1 parent 5756086 commit 778e971

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,4 +801,57 @@ l0_%=: /* exit(0) */ \
801801
: __clobber_all);
802802
}
803803

804+
#define access_test_non_linear(name, desc, retval, linear_sz) \
805+
SEC("tc") \
806+
__description("direct packet access: " #name " (non-linear, " desc ")") \
807+
__success __retval(retval) \
808+
__linear_size(linear_sz) \
809+
__naked void access_##name(void) \
810+
{ \
811+
asm volatile (" \
812+
r2 = *(u32*)(r1 + %[skb_data]); \
813+
r3 = *(u32*)(r1 + %[skb_data_end]); \
814+
r0 = r2; \
815+
r0 += 22; \
816+
if r0 > r3 goto l0_%=; \
817+
r0 = *(u8*)(r0 - 1); \
818+
exit; \
819+
l0_%=: r0 = 1; \
820+
exit; \
821+
" : \
822+
: __imm_const(skb_data, offsetof(struct __sk_buff, data)), \
823+
__imm_const(skb_data_end, offsetof(struct __sk_buff, data_end)) \
824+
: __clobber_all); \
825+
}
826+
827+
access_test_non_linear(test31, "too short eth", 1, ETH_HLEN);
828+
access_test_non_linear(test32, "too short 1", 1, 1);
829+
access_test_non_linear(test33, "long enough", 0, 22);
830+
831+
SEC("tc")
832+
__description("direct packet access: test34 (non-linear, linearized)")
833+
__success __retval(0)
834+
__linear_size(ETH_HLEN)
835+
__naked void access_test34_non_linear_linearized(void)
836+
{
837+
asm volatile (" \
838+
r6 = r1; \
839+
r2 = 22; \
840+
call %[bpf_skb_pull_data]; \
841+
r2 = *(u32*)(r6 + %[skb_data]); \
842+
r3 = *(u32*)(r6 + %[skb_data_end]); \
843+
r0 = r2; \
844+
r0 += 22; \
845+
if r0 > r3 goto l0_%=; \
846+
r0 = *(u8*)(r0 - 1); \
847+
exit; \
848+
l0_%=: r0 = 1; \
849+
exit; \
850+
" :
851+
: __imm(bpf_skb_pull_data),
852+
__imm_const(skb_data, offsetof(struct __sk_buff, data)),
853+
__imm_const(skb_data_end, offsetof(struct __sk_buff, data_end))
854+
: __clobber_all);
855+
}
856+
804857
char _license[] SEC("license") = "GPL";

0 commit comments

Comments
 (0)