Skip to content

Commit

Permalink
Unit test for unitialized memory afer bpf_xdp_adjust_head
Browse files Browse the repository at this point in the history
Reviewed By: frankfeir

Differential Revision: D51536069

fbshipit-source-id: a9d3405a8c7da45300e8368b4e7785350143024f
  • Loading branch information
avasylev authored and facebook-github-bot committed Nov 29, 2023
1 parent 26b7273 commit e5a32cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
31 changes: 31 additions & 0 deletions katran/lib/bpf/balancer_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,35 @@ decrement_ttl(void* data, void* data_end, int offset, bool is_ipv6) {
return FURTHER_PROCESSING;
}

__attribute__((__always_inline__)) static inline long test_bpf_xdp_adjust_head(
struct xdp_md* xdp_md,
int delta) {
long ret = bpf_xdp_adjust_head(xdp_md, delta);
if (ret) {
return ret;
}
if (delta >= 0) {
return ret;
}
// we've prepended "new" memory, initialize it

void* data = (void*)(long)xdp_md->data;
void* data_end = (void*)(long)xdp_md->data_end;
int offset = 0 - delta;
// extra check to make bpf verifier happy
if (data + offset > data_end) {
return -1;
}
// intentionally set all bits 1, as bpf tester zeros allocated memory, unlike
// xdp stack
memset(data, 0xFF, offset);
return ret;
}

#ifdef KATRAN_TEST_MODE
#define XDP_ADJUST_HEAD_FUNC test_bpf_xdp_adjust_head
#else
#define XDP_ADJUST_HEAD_FUNC bpf_xdp_adjust_head
#endif

#endif // __BALANCER_HELPERS
4 changes: 2 additions & 2 deletions katran/lib/bpf/handle_icmp.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ __attribute__((__always_inline__)) static inline int send_icmp6_reply(
__attribute__((__always_inline__)) static inline int send_icmp4_too_big(
struct xdp_md* xdp) {
int headroom = (int)sizeof(struct iphdr) + (int)sizeof(struct icmphdr);
if (bpf_xdp_adjust_head(xdp, 0 - headroom)) {
if (XDP_ADJUST_HEAD_FUNC(xdp, 0 - headroom)) {
return XDP_DROP;
}
void* data = (void*)(long)xdp->data;
Expand Down Expand Up @@ -169,7 +169,7 @@ __attribute__((__always_inline__)) static inline int send_icmp4_too_big(
__attribute__((__always_inline__)) static inline int send_icmp6_too_big(
struct xdp_md* xdp) {
int headroom = (int)sizeof(struct ipv6hdr) + (int)sizeof(struct icmp6hdr);
if (bpf_xdp_adjust_head(xdp, 0 - headroom)) {
if (XDP_ADJUST_HEAD_FUNC(xdp, 0 - headroom)) {
return XDP_DROP;
}
void* data = (void*)(long)xdp->data;
Expand Down
16 changes: 8 additions & 8 deletions katran/lib/bpf/pckt_encap.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ __attribute__((__always_inline__)) static inline bool encap_v6(
__u32 saddr[4];
__u8 proto;
// ip(6)ip6 encap
if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct ipv6hdr))) {
if (XDP_ADJUST_HEAD_FUNC(xdp, 0 - (int)sizeof(struct ipv6hdr))) {
return false;
}
data = (void*)(long)xdp->data;
Expand Down Expand Up @@ -107,7 +107,7 @@ __attribute__((__always_inline__)) static inline bool encap_v4(
ip_suffix ^= pckt->flow.src;
__u64 csum = 0;
// ipip encap
if (bpf_xdp_adjust_head(xdp, 0 - (int)sizeof(struct iphdr))) {
if (XDP_ADJUST_HEAD_FUNC(xdp, 0 - (int)sizeof(struct iphdr))) {
return false;
}
data = (void*)(long)xdp->data;
Expand Down Expand Up @@ -148,7 +148,7 @@ decap_v6(struct xdp_md* xdp, void** data, void** data_end, bool inner_v4) {
} else {
new_eth->h_proto = BE_ETH_P_IPV6;
}
if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct ipv6hdr))) {
if (XDP_ADJUST_HEAD_FUNC(xdp, (int)sizeof(struct ipv6hdr))) {
return false;
}
*data = (void*)(long)xdp->data;
Expand All @@ -165,7 +165,7 @@ decap_v4(struct xdp_md* xdp, void** data, void** data_end) {
memcpy(new_eth->h_source, old_eth->h_source, 6);
memcpy(new_eth->h_dest, old_eth->h_dest, 6);
new_eth->h_proto = BE_ETH_P_IP;
if (bpf_xdp_adjust_head(xdp, (int)sizeof(struct iphdr))) {
if (XDP_ADJUST_HEAD_FUNC(xdp, (int)sizeof(struct iphdr))) {
return false;
}
*data = (void*)(long)xdp->data;
Expand Down Expand Up @@ -274,7 +274,7 @@ __attribute__((__always_inline__)) static inline bool gue_encap_v4(

sport ^= ((pckt->flow.src >> 16) & 0xFFFF);

if (bpf_xdp_adjust_head(
if (XDP_ADJUST_HEAD_FUNC(
xdp, 0 - ((int)sizeof(struct iphdr) + (int)sizeof(struct udphdr)))) {
return false;
}
Expand Down Expand Up @@ -330,7 +330,7 @@ __attribute__((__always_inline__)) static inline bool gue_encap_v6(
return false;
}

if (bpf_xdp_adjust_head(
if (XDP_ADJUST_HEAD_FUNC(
xdp,
0 - ((int)sizeof(struct ipv6hdr) + (int)sizeof(struct udphdr)))) {
return false;
Expand Down Expand Up @@ -380,7 +380,7 @@ gue_decap_v4(struct xdp_md* xdp, void** data, void** data_end) {
memcpy(new_eth->h_source, old_eth->h_source, 6);
memcpy(new_eth->h_dest, old_eth->h_dest, 6);
new_eth->h_proto = BE_ETH_P_IP;
if (bpf_xdp_adjust_head(
if (XDP_ADJUST_HEAD_FUNC(
xdp, (int)(sizeof(struct iphdr) + sizeof(struct udphdr)))) {
return false;
}
Expand All @@ -403,7 +403,7 @@ gue_decap_v6(struct xdp_md* xdp, void** data, void** data_end, bool inner_v4) {
} else {
new_eth->h_proto = BE_ETH_P_IPV6;
}
if (bpf_xdp_adjust_head(
if (XDP_ADJUST_HEAD_FUNC(
xdp, (int)(sizeof(struct ipv6hdr) + sizeof(struct udphdr)))) {
return false;
}
Expand Down

0 comments on commit e5a32cc

Please sign in to comment.