net: fix potential crash in net/sched/cls_u32.c
Subsystems (3)
cover net: fix potential crash in net/sched/cls_u32.c
GangMin Kim provided a report and a repro fooling u32_classify().
Add skb_header_pointer_careful() variant of skb_header_pointer() and use it in net/sched/cls_u32.c.
Later we can also use it in net/sched/act_pedit.c
Eric Dumazet (2):
net: add skb_header_pointer_careful() helper net/sched: cls_u32: use skb_header_pointer_careful()
include/linux/skbuff.h | 12 ++++++++++++ net/sched/cls_u32.c | 13 ++++++------- 2 files changed, 18 insertions(+), 7 deletions(-)
Signature · 1 line
2.53.0.rc1.217.geba53bf80e-goog
PATCH 1/2 net: add skb_header_pointer_careful() helper
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.
--- include/linux/skbuff.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/linux/skbuff.h +12 -0
@@ -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_headlen (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) {
Signature · 1 line
2.53.0.rc1.217.geba53bf80e-goog
PATCH 2/2 net/sched: cls_u32: use skb_header_pointer_careful()
In reply to Eric Dumazetskb_header_pointer() does not fully validate negative @offset values.
Use skb_header_pointer_careful() instead.
GangMin Kim provided a report and a repro fooling u32_classify():
BUG: KASAN: slab-out-of-bounds in u32_classify+0x1180/0x11b0
net/sched/cls_u32.c:221
Many many thanks to GangMin!
--- net/sched/cls_u32.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
net/sched/cls_u32.c +6 -7
@@ -161,10 +161,8 @@ TC_INDIRECT_SCOPE int u32_classify(struct sk_buff *skb,int toff = off + key->off + (off2 & key->offmask);__be32 *data, hdata; -if (skb_headroom (skb) + toff >INT_MAX ) - goto out; - - data =skb_header_pointer (skb, toff,4 , &hdata); + data =skb_header_pointer_careful (skb, toff,4 , + &hdata);if (!data) goto out;if ((*data ^ key->val) & key->mask) {
@@ -214,8 +212,9 @@ TC_INDIRECT_SCOPE int u32_classify(struct sk_buff *skb,if (ht->divisor) {__be32 *data, hdata; - data =skb_header_pointer (skb, off + n->sel.hoff,4 , - &hdata); + data =skb_header_pointer_careful (skb, + off + n->sel.hoff, +4 , &hdata);if (!data) goto out; sel = ht->divisor & u32_hash_fold(*data, &n->sel,
@@ -229,7 +228,7 @@ TC_INDIRECT_SCOPE int u32_classify(struct sk_buff *skb,if (n->sel.flags &TC_U32_VAROFFSET ) {__be16 *data, hdata; - data =skb_header_pointer (skb, + data =skb_header_pointer_careful (skb, off + n->sel.offoff,2 , &hdata);if (!data)
Signature · 1 line
2.53.0.rc1.217.geba53bf80e-goog
Jakub Kicinski
maintainerRe: [net,1/2] net: add skb_header_pointer_careful() helper
In reply to Eric DumazetThis is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
net: add skb_header_pointer_careful() helper
This patch adds a new helper to validate negative offsets before calling skb_header_pointer(). The commit message states that negative offsets are supported "as long as the zone starts between skb->head and skb->data."
Quoted 7 lines
include/linux/skbuff.h +0
-0@@ -4301,6 +4301,18 @@ skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) skb_headlen(skb), buffer); }
include/linux/skbuff.h skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) · line 4312
Quoted 10 lines
include/linux/skbuff.h +9
-0+/* 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_headlen (skb))) +return NULL ;
Should this check use skb_headroom() instead of skb_headlen()?
The commit message says negative offsets should be valid when the zone is "between skb->head and skb->data" - that describes the headroom, not the headlen. For a negative offset, skb_header_pointer() returns skb->data + offset, which points into the headroom area.
skb_headlen() returns the linear data length after skb->data (skb->len minus skb->data_len), while skb_headroom() returns the space before skb->data (skb->data minus skb->head).
These values are quite different in practice - headroom is typically small (2-128 bytes), while headlen can be up to MTU size (~1500 bytes). With the current check, an offset like -100 would pass validation when skb_headlen is 1500, but could read before skb->head if skb_headroom is only 2 bytes.
The existing offset_valid() function in net/sched/act_pedit.c uses skb_headroom() for this same type of check:
if (offset < 0 && -offset > skb_headroom(skb))
return false;Quoted 2 lines
include/linux/skbuff.h +2
-0+return skb_header_pointer (skb, offset, len, buffer); +}
Signature · 1 line
pw-bot: cr
Re: [net,1/2] net: add skb_header_pointer_careful() helper
Quoted 48 lines · Jakub Kicinski
This is an AI-generated review of your patch. The human sending this email has considered the AI review valid, or at least plausible.
For local reproduction steps see https://netdev-ai.bots.linux.dev/ai-local.html
---
net: addskb_header_pointer_careful()helperThis patch adds a new helper to validate negative offsets before calling
skb_header_pointer(). The commit message states that negative offsets are supported "as long as the zone starts betweenskb->headandskb->data."Quoted 7 lines
include/linux/skbuff.h +0
-0@@ -4301,6 +4301,18 @@ skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) skb_headlen(skb), buffer); }include/linux/skbuff.h skb_header_pointer(const struct sk_buff *skb, int offset, int len, void *buffer) · line 4312
Quoted 10 lines
include/linux/skbuff.h +9
-0+/* 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_headlen (skb))) +return NULL ;Should this check use
skb_headroom()instead ofskb_headlen()?The commit message says negative offsets should be valid when the zone is "between
skb->headandskb->data" - that describes the headroom, not the headlen. For a negative offset,skb_header_pointer()returnsskb->data+ offset, which points into the headroom area.
skb_headlen()returns the linear data length afterskb->data(skb->lenminusskb->data_len), whileskb_headroom()returns the space beforeskb->data(skb->dataminusskb->head).These values are quite different in practice - headroom is typically small (2-128 bytes), while headlen can be up to MTU size (~1500 bytes). With the current check, an offset like -100 would pass validation when
skb_headlenis 1500, but could read beforeskb->headifskb_headroomis only 2 bytes.
Quoted 8 lines
The existing
offset_valid()function innet/sched/act_pedit.cusesskb_headroom()for this same type of check:if (offset < 0 && -offset > skb_headroom(skb)) return false;Quoted 2 lines
include/linux/skbuff.h +2
-0+return skb_header_pointer (skb, offset, len, buffer); +}
Yes, of course, silly error on my side.