Commit 9ecc4d85 authored by Cong Wang's avatar Cong Wang Committed by Daniel Borkmann
Browse files

bpf: Check negative offsets in __bpf_skb_min_len()



skb_network_offset() and skb_transport_offset() can be negative when
they are called after we pull the transport header, for example, when
we use eBPF sockmap at the point of ->sk_data_ready().

__bpf_skb_min_len() uses an unsigned int to get these offsets, this
leads to a very large number which then causes bpf_skb_change_tail()
failed unexpectedly.

Fix this by using a signed int to get these offsets and ensure the
minimum is at least zero.

Fixes: 5293efe6 ("bpf: add bpf_skb_change_tail helper")
Signed-off-by: default avatarCong Wang <cong.wang@bytedance.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarJohn Fastabend <john.fastabend@gmail.com>
Link: https://lore.kernel.org/bpf/20241213034057.246437-2-xiyou.wangcong@gmail.com
parent 5153a75e
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -3734,13 +3734,22 @@ static const struct bpf_func_proto bpf_skb_adjust_room_proto = {

static u32 __bpf_skb_min_len(const struct sk_buff *skb)
{
	u32 min_len = skb_network_offset(skb);
	int offset = skb_network_offset(skb);
	u32 min_len = 0;

	if (skb_transport_header_was_set(skb))
		min_len = skb_transport_offset(skb);
	if (skb->ip_summed == CHECKSUM_PARTIAL)
		min_len = skb_checksum_start_offset(skb) +
	if (offset > 0)
		min_len = offset;
	if (skb_transport_header_was_set(skb)) {
		offset = skb_transport_offset(skb);
		if (offset > 0)
			min_len = offset;
	}
	if (skb->ip_summed == CHECKSUM_PARTIAL) {
		offset = skb_checksum_start_offset(skb) +
			 skb->csum_offset + sizeof(__sum16);
		if (offset > 0)
			min_len = offset;
	}
	return min_len;
}