netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH net] net: skbuff: add overflow debug check to pull/push helpers
@ 2024-02-16 11:36 Florian Westphal
  2024-02-19 15:16 ` Simon Horman
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Florian Westphal @ 2024-02-16 11:36 UTC (permalink / raw)
  To: netdev; +Cc: Florian Westphal

syzbot managed to trigger following splat:
BUG: KASAN: use-after-free in __skb_flow_dissect+0x4a3b/0x5e50
Read of size 1 at addr ffff888208a4000e by task a.out/2313
[..]
  __skb_flow_dissect+0x4a3b/0x5e50
  __skb_get_hash+0xb4/0x400
  ip_tunnel_xmit+0x77e/0x26f0
  ipip_tunnel_xmit+0x298/0x410
  ..

Analysis shows that the skb has a valid ->head, but bogus ->data
pointer.

skb->data gets its bogus value via the neigh layer, which does:

1556    __skb_pull(skb, skb_network_offset(skb));

... and the skb was already dodgy at this point:

skb_network_offset(skb) returns a negative value due to an
earlier overflow of skb->network_header (u16).  __skb_pull thus
"adjusts" skb->data by a huge offset, pointing outside skb->head
area.

Allow debug builds to splat when we try to pull/push more than
INT_MAX bytes.

After this, the syzkaller reproducer yields a more precise splat
before the flow dissector attempts to read off skb->data memory:

WARNING: CPU: 5 PID: 2313 at include/linux/skbuff.h:2653 neigh_connected_output+0x28e/0x400
  ip_finish_output2+0xb25/0xed0
  iptunnel_xmit+0x4ff/0x870
  ipgre_xmit+0x78e/0xbb0

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 A fix to prevent network_header overflow will follow shortly.

 include/linux/skbuff.h | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 2dde34c29203..fd9198fcc3c3 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -2642,6 +2642,8 @@ static inline void skb_put_u8(struct sk_buff *skb, u8 val)
 void *skb_push(struct sk_buff *skb, unsigned int len);
 static inline void *__skb_push(struct sk_buff *skb, unsigned int len)
 {
+	DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
+
 	skb->data -= len;
 	skb->len  += len;
 	return skb->data;
@@ -2650,6 +2652,8 @@ static inline void *__skb_push(struct sk_buff *skb, unsigned int len)
 void *skb_pull(struct sk_buff *skb, unsigned int len);
 static inline void *__skb_pull(struct sk_buff *skb, unsigned int len)
 {
+	DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
+
 	skb->len -= len;
 	if (unlikely(skb->len < skb->data_len)) {
 #if defined(CONFIG_DEBUG_NET)
@@ -2674,6 +2678,8 @@ void *__pskb_pull_tail(struct sk_buff *skb, int delta);
 static inline enum skb_drop_reason
 pskb_may_pull_reason(struct sk_buff *skb, unsigned int len)
 {
+	DEBUG_NET_WARN_ON_ONCE(len > INT_MAX);
+
 	if (likely(len <= skb_headlen(skb)))
 		return SKB_NOT_DROPPED_YET;
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: skbuff: add overflow debug check to pull/push helpers
  2024-02-16 11:36 [PATCH net] net: skbuff: add overflow debug check to pull/push helpers Florian Westphal
@ 2024-02-19 15:16 ` Simon Horman
  2024-02-20 11:07 ` Paolo Abeni
  2024-02-20 11:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: Simon Horman @ 2024-02-19 15:16 UTC (permalink / raw)
  To: Florian Westphal; +Cc: netdev

On Fri, Feb 16, 2024 at 12:36:57PM +0100, Florian Westphal wrote:
> syzbot managed to trigger following splat:
> BUG: KASAN: use-after-free in __skb_flow_dissect+0x4a3b/0x5e50
> Read of size 1 at addr ffff888208a4000e by task a.out/2313
> [..]
>   __skb_flow_dissect+0x4a3b/0x5e50
>   __skb_get_hash+0xb4/0x400
>   ip_tunnel_xmit+0x77e/0x26f0
>   ipip_tunnel_xmit+0x298/0x410
>   ..
> 
> Analysis shows that the skb has a valid ->head, but bogus ->data
> pointer.
> 
> skb->data gets its bogus value via the neigh layer, which does:
> 
> 1556    __skb_pull(skb, skb_network_offset(skb));
> 
> ... and the skb was already dodgy at this point:
> 
> skb_network_offset(skb) returns a negative value due to an
> earlier overflow of skb->network_header (u16).  __skb_pull thus
> "adjusts" skb->data by a huge offset, pointing outside skb->head
> area.
> 
> Allow debug builds to splat when we try to pull/push more than
> INT_MAX bytes.
> 
> After this, the syzkaller reproducer yields a more precise splat
> before the flow dissector attempts to read off skb->data memory:
> 
> WARNING: CPU: 5 PID: 2313 at include/linux/skbuff.h:2653 neigh_connected_output+0x28e/0x400
>   ip_finish_output2+0xb25/0xed0
>   iptunnel_xmit+0x4ff/0x870
>   ipgre_xmit+0x78e/0xbb0
> 
> Signed-off-by: Florian Westphal <fw@strlen.de>

Reviewed-by: Simon Horman <horms@kernel.org>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: skbuff: add overflow debug check to pull/push helpers
  2024-02-16 11:36 [PATCH net] net: skbuff: add overflow debug check to pull/push helpers Florian Westphal
  2024-02-19 15:16 ` Simon Horman
@ 2024-02-20 11:07 ` Paolo Abeni
  2024-02-20 11:18   ` Florian Westphal
  2024-02-20 11:30 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Paolo Abeni @ 2024-02-20 11:07 UTC (permalink / raw)
  To: Florian Westphal, netdev

On Fri, 2024-02-16 at 12:36 +0100, Florian Westphal wrote:
> syzbot managed to trigger following splat:
> BUG: KASAN: use-after-free in __skb_flow_dissect+0x4a3b/0x5e50
> Read of size 1 at addr ffff888208a4000e by task a.out/2313
> [..]
>   __skb_flow_dissect+0x4a3b/0x5e50
>   __skb_get_hash+0xb4/0x400
>   ip_tunnel_xmit+0x77e/0x26f0
>   ipip_tunnel_xmit+0x298/0x410
>   ..
> 
> Analysis shows that the skb has a valid ->head, but bogus ->data
> pointer.
> 
> skb->data gets its bogus value via the neigh layer, which does:
> 
> 1556    __skb_pull(skb, skb_network_offset(skb));
> 
> ... and the skb was already dodgy at this point:
> 
> skb_network_offset(skb) returns a negative value due to an
> earlier overflow of skb->network_header (u16).  __skb_pull thus
> "adjusts" skb->data by a huge offset, pointing outside skb->head
> area.
> 
> Allow debug builds to splat when we try to pull/push more than
> INT_MAX bytes.
> 
> After this, the syzkaller reproducer yields a more precise splat
> before the flow dissector attempts to read off skb->data memory:
> 
> WARNING: CPU: 5 PID: 2313 at include/linux/skbuff.h:2653 neigh_connected_output+0x28e/0x400
>   ip_finish_output2+0xb25/0xed0
>   iptunnel_xmit+0x4ff/0x870
>   ipgre_xmit+0x78e/0xbb0
> 
> Signed-off-by: Florian Westphal <fw@strlen.de>

This is targeting 'net', but IMHO looks more like 'net-next' material.
Any objections applying the patch there?

Thanks!

Paolo


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: skbuff: add overflow debug check to pull/push helpers
  2024-02-20 11:07 ` Paolo Abeni
@ 2024-02-20 11:18   ` Florian Westphal
  0 siblings, 0 replies; 5+ messages in thread
From: Florian Westphal @ 2024-02-20 11:18 UTC (permalink / raw)
  To: Paolo Abeni; +Cc: Florian Westphal, netdev

Paolo Abeni <pabeni@redhat.com> wrote:
> This is targeting 'net', but IMHO looks more like 'net-next' material.
> Any objections applying the patch there?

No.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net] net: skbuff: add overflow debug check to pull/push helpers
  2024-02-16 11:36 [PATCH net] net: skbuff: add overflow debug check to pull/push helpers Florian Westphal
  2024-02-19 15:16 ` Simon Horman
  2024-02-20 11:07 ` Paolo Abeni
@ 2024-02-20 11:30 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-02-20 11:30 UTC (permalink / raw)
  To: Florian Westphal; +Cc: netdev

Hello:

This patch was applied to netdev/net-next.git (main)
by Paolo Abeni <pabeni@redhat.com>:

On Fri, 16 Feb 2024 12:36:57 +0100 you wrote:
> syzbot managed to trigger following splat:
> BUG: KASAN: use-after-free in __skb_flow_dissect+0x4a3b/0x5e50
> Read of size 1 at addr ffff888208a4000e by task a.out/2313
> [..]
>   __skb_flow_dissect+0x4a3b/0x5e50
>   __skb_get_hash+0xb4/0x400
>   ip_tunnel_xmit+0x77e/0x26f0
>   ipip_tunnel_xmit+0x298/0x410
>   ..
> 
> [...]

Here is the summary with links:
  - [net] net: skbuff: add overflow debug check to pull/push helpers
    https://git.kernel.org/netdev/net-next/c/219eee9c0d16

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-02-20 11:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-16 11:36 [PATCH net] net: skbuff: add overflow debug check to pull/push helpers Florian Westphal
2024-02-19 15:16 ` Simon Horman
2024-02-20 11:07 ` Paolo Abeni
2024-02-20 11:18   ` Florian Westphal
2024-02-20 11:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).