Skip to content

Commit 0f5d82f

Browse files
zhuyifei1999borkmann
authored andcommitted
net/filter: Permit reading NET in load_bytes_relative when MAC not set
Added a check in the switch case on start_header that checks for the existence of the header, and in the case that MAC is not set and the caller requests for MAC, -EFAULT. If the caller requests for NET then MAC's existence is completely ignored. There is no function to check NET header's existence and as far as cgroup_skb/egress is concerned it should always be set. Removed for ptr >= the start of header, considering offset is bounded unsigned and should always be true. len <= end - mac is redundant to ptr + len <= end. Fixes: 3eee1f7 ("bpf: fix bpf_skb_load_bytes_relative pkt length check") Signed-off-by: YiFei Zhu <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Stanislav Fomichev <[email protected]> Link: https://lore.kernel.org/bpf/76bb820ddb6a95f59a772ecbd8c8a336f646b362.1591812755.git.zhuyifei@google.com
1 parent 47f6bc4 commit 0f5d82f

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

net/core/filter.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,25 +1755,27 @@ BPF_CALL_5(bpf_skb_load_bytes_relative, const struct sk_buff *, skb,
17551755
u32, offset, void *, to, u32, len, u32, start_header)
17561756
{
17571757
u8 *end = skb_tail_pointer(skb);
1758-
u8 *net = skb_network_header(skb);
1759-
u8 *mac = skb_mac_header(skb);
1760-
u8 *ptr;
1758+
u8 *start, *ptr;
17611759

1762-
if (unlikely(offset > 0xffff || len > (end - mac)))
1760+
if (unlikely(offset > 0xffff))
17631761
goto err_clear;
17641762

17651763
switch (start_header) {
17661764
case BPF_HDR_START_MAC:
1767-
ptr = mac + offset;
1765+
if (unlikely(!skb_mac_header_was_set(skb)))
1766+
goto err_clear;
1767+
start = skb_mac_header(skb);
17681768
break;
17691769
case BPF_HDR_START_NET:
1770-
ptr = net + offset;
1770+
start = skb_network_header(skb);
17711771
break;
17721772
default:
17731773
goto err_clear;
17741774
}
17751775

1776-
if (likely(ptr >= mac && ptr + len <= end)) {
1776+
ptr = start + offset;
1777+
1778+
if (likely(ptr + len <= end)) {
17771779
memcpy(to, ptr, len);
17781780
return 0;
17791781
}

0 commit comments

Comments
 (0)