Skip to content

Commit 9ecc4d8

Browse files
Cong Wangborkmann
authored andcommitted
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: 5293efe ("bpf: add bpf_skb_change_tail helper") Signed-off-by: Cong Wang <cong.wang@bytedance.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: John Fastabend <john.fastabend@gmail.com> Link: https://lore.kernel.org/bpf/20241213034057.246437-2-xiyou.wangcong@gmail.com
1 parent 5153a75 commit 9ecc4d8

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

net/core/filter.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3734,13 +3734,22 @@ static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
37343734

37353735
static u32 __bpf_skb_min_len(const struct sk_buff *skb)
37363736
{
3737-
u32 min_len = skb_network_offset(skb);
3737+
int offset = skb_network_offset(skb);
3738+
u32 min_len = 0;
37383739

3739-
if (skb_transport_header_was_set(skb))
3740-
min_len = skb_transport_offset(skb);
3741-
if (skb->ip_summed == CHECKSUM_PARTIAL)
3742-
min_len = skb_checksum_start_offset(skb) +
3743-
skb->csum_offset + sizeof(__sum16);
3740+
if (offset > 0)
3741+
min_len = offset;
3742+
if (skb_transport_header_was_set(skb)) {
3743+
offset = skb_transport_offset(skb);
3744+
if (offset > 0)
3745+
min_len = offset;
3746+
}
3747+
if (skb->ip_summed == CHECKSUM_PARTIAL) {
3748+
offset = skb_checksum_start_offset(skb) +
3749+
skb->csum_offset + sizeof(__sum16);
3750+
if (offset > 0)
3751+
min_len = offset;
3752+
}
37443753
return min_len;
37453754
}
37463755

0 commit comments

Comments
 (0)