Skip to content

Commit 6f5a630

Browse files
author
Alexei Starovoitov
committed
bpf, net: Introduce skb_pointer_if_linear().
Network drivers always call skb_header_pointer() with non-null buffer. Remove !buffer check to prevent accidental misuse of skb_header_pointer(). Introduce skb_pointer_if_linear() instead. Reported-by: Jakub Kicinski <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 41ee014 commit 6f5a630

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

include/linux/skbuff.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4023,7 +4023,7 @@ __skb_header_pointer(const struct sk_buff *skb, int offset, int len,
40234023
if (likely(hlen - offset >= len))
40244024
return (void *)data + offset;
40254025

4026-
if (!skb || !buffer || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0))
4026+
if (!skb || unlikely(skb_copy_bits(skb, offset, buffer, len) < 0))
40274027
return NULL;
40284028

40294029
return buffer;
@@ -4036,6 +4036,14 @@ skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
40364036
skb_headlen(skb), buffer);
40374037
}
40384038

4039+
static inline void * __must_check
4040+
skb_pointer_if_linear(const struct sk_buff *skb, int offset, int len)
4041+
{
4042+
if (likely(skb_headlen(skb) - offset >= len))
4043+
return skb->data + offset;
4044+
return NULL;
4045+
}
4046+
40394047
/**
40404048
* skb_needs_linearize - check if we need to linearize a given skb
40414049
* depending on the given device features.

kernel/bpf/helpers.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,10 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
22632263
case BPF_DYNPTR_TYPE_RINGBUF:
22642264
return ptr->data + ptr->offset + offset;
22652265
case BPF_DYNPTR_TYPE_SKB:
2266-
return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer__opt);
2266+
if (buffer__opt)
2267+
return skb_header_pointer(ptr->data, ptr->offset + offset, len, buffer__opt);
2268+
else
2269+
return skb_pointer_if_linear(ptr->data, ptr->offset + offset, len);
22672270
case BPF_DYNPTR_TYPE_XDP:
22682271
{
22692272
void *xdp_ptr = bpf_xdp_pointer(ptr->data, ptr->offset + offset, len);

0 commit comments

Comments
 (0)