]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
net: add skb_header_pointer_careful() helper
authorEric Dumazet <edumazet@google.com>
Wed, 28 Jan 2026 14:15:38 +0000 (14:15 +0000)
committerJakub Kicinski <kuba@kernel.org>
Fri, 30 Jan 2026 02:25:22 +0000 (18:25 -0800)
This variant of skb_header_pointer() should be used in contexts
where @offset argument is user-controlled and could be negative.

Negative offsets are supported, as long as the zone starts
between skb->head and skb->data.

Signed-off-by: Eric Dumazet <edumazet@google.com>
Link: https://patch.msgid.link/20260128141539.3404400-2-edumazet@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/linux/skbuff.h

index 86737076101d4a8452e90fe78adcdcfdefb79169..112e48970338fa8b24bb001e00c30631f4fab814 100644 (file)
@@ -4301,6 +4301,18 @@ skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer)
                                    skb_headlen(skb), buffer);
 }
 
+/* Variant of skb_header_pointer() where @offset is user-controlled
+ * and potentially negative.
+ */
+static inline void * __must_check
+skb_header_pointer_careful(const struct sk_buff *skb, int offset,
+                          int len, void *buffer)
+{
+       if (unlikely(offset < 0 && -offset > skb_headroom(skb)))
+               return NULL;
+       return skb_header_pointer(skb, offset, len, buffer);
+}
+
 static inline void * __must_check
 skb_pointer_if_linear(const struct sk_buff *skb, int offset, int len)
 {