Skip to content

Commit 3eee1f7

Browse files
committed
bpf: fix bpf_skb_load_bytes_relative pkt length check
The len > skb_headlen(skb) cannot be used as a maximum upper bound for the packet length since it does not have any relation to the full linear packet length when filtering is used from upper layers (e.g. in case of reuseport BPF programs) as by then skb->data, skb->len already got mangled through __skb_pull() and others. Fixes: 4e1ec56 ("bpf: add skb_load_bytes_relative helper") Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Martin KaFai Lau <[email protected]>
1 parent b611da4 commit 3eee1f7

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

net/core/filter.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,24 +1712,26 @@ static const struct bpf_func_proto bpf_skb_load_bytes_proto = {
17121712
BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
17131713
u32, offset, void *, to, u32, len, u32, start_header)
17141714
{
1715+
u8 *end = skb_tail_pointer(skb);
1716+
u8 *net = skb_network_header(skb);
1717+
u8 *mac = skb_mac_header(skb);
17151718
u8 *ptr;
17161719

1717-
if (unlikely(offset > 0xffff || len > skb_headlen(skb)))
1720+
if (unlikely(offset > 0xffff || len > (end - mac)))
17181721
goto err_clear;
17191722

17201723
switch (start_header) {
17211724
case BPF_HDR_START_MAC:
1722-
ptr = skb_mac_header(skb) + offset;
1725+
ptr = mac + offset;
17231726
break;
17241727
case BPF_HDR_START_NET:
1725-
ptr = skb_network_header(skb) + offset;
1728+
ptr = net + offset;
17261729
break;
17271730
default:
17281731
goto err_clear;
17291732
}
17301733

1731-
if (likely(ptr >= skb_mac_header(skb) &&
1732-
ptr + len <= skb_tail_pointer(skb))) {
1734+
if (likely(ptr >= mac && ptr + len <= end)) {
17331735
memcpy(to, ptr, len);
17341736
return 0;
17351737
}

0 commit comments

Comments
 (0)