Skip to contentSkip to conversation

net: skb: check the boundrary of skb drop reason

PATCH Superseded [email protected] 3 participants 9 messages May 12, 2022 – May 13, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: skb: check the boundrary of skb drop reason

From: Menglong Dong <[email protected]>

In the commit 1330b6ef3313 ("skb: make drop reason booleanable"), SKB_NOT_DROPPED_YET is added to the enum skb_drop_reason, which makes the invalid drop reason SKB_NOT_DROPPED_YET can leak to the kfree_skb tracepoint. Once this happen (it happened, as 4th patch says), it can cause NULL pointer in drop monitor and result in kernel panic.

Therefore, check the boundrary of drop reason in both kfree_skb_reason (2th patch) and drop monitor (1th patch) to prevent such case happens again.

Meanwhile, fix the invalid drop reason passed to kfree_skb_reason() in tcp_v4_rcv() and tcp_v6_rcv().

Changes since v1:
- consider tcp_v6_rcv() in the 4th patch

Menglong Dong (4):
net: dm: check the boundary of skb drop reasons
net: skb: check the boundrary of drop reason in kfree_skb_reason()
net: skb: change the definition SKB_DR_SET()
net: tcp: reset 'drop_reason' to NOT_SPCIFIED in tcp_v{4,6}_rcv()

 include/linux/skbuff.h  | 3 ++-
 net/core/drop_monitor.c | 2 +-
 net/core/skbuff.c       | 5 +++++
 net/ipv4/tcp_ipv4.c     | 1 +
 net/ipv6/tcp_ipv6.c     | 1 +
 5 files changed, 10 insertions(+), 2 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 1/4 net: dm: check the boundary of skb drop reasons

From: Menglong Dong <[email protected]>

The 'reason' will be set to 'SKB_DROP_REASON_NOT_SPECIFIED' if it not small that SKB_DROP_REASON_MAX in net_dm_packet_trace_kfree_skb_hit(), but it can't avoid it to be 0, which is invalid and can cause NULL pointer in drop_reasons.

Therefore, reset it to SKB_DROP_REASON_NOT_SPECIFIED when 'reason <= 0'.

---
 net/core/drop_monitor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/drop_monitor.c +1 -1
index b89e3e95bffc..41cac0e4834e 100644
@@ -517,7 +517,7 @@ static void net_dm_packet_trace_kfree_skb_hit(void *ignore,
 	if (!nskb)
 		return;
 
-	if ((unsigned int)reason >= SKB_DROP_REASON_MAX)
+	if (unlikely(reason >= SKB_DROP_REASON_MAX || reason <= 0))
 		reason = SKB_DROP_REASON_NOT_SPECIFIED;
 	cb = NET_DM_SKB_CB(nskb);
 	cb->reason = reason;
Signature · 1 line
2.36.1
#2 lore raw

PATCH v2 2/4 net: skb: check the boundrary of drop reason in kfree_skb_reason()

In reply to [email protected]

From: Menglong Dong <[email protected]>

Sometimes, we may forget to reset skb drop reason to NOT_SPECIFIED after we make it the return value of the functions with return type of enum skb_drop_reason, such as tcp_inbound_md5_hash. Therefore, its value can be SKB_NOT_DROPPED_YET(0), which is invalid for kfree_skb_reason().

So we check the range of drop reason in kfree_skb_reason() and reset it to NOT_SPECIFIED and print a warning with DEBUG_NET_WARN_ON_ONCE() if it is invalid.

---
 net/core/skbuff.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/skbuff.c +5 -0
index 15f7b6f99a8f..e49e43d4c34d 100644
@@ -771,6 +771,11 @@ void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason)
 	if (!skb_unref(skb))
 		return;
 
+	if (unlikely(reason <= 0 || reason >= SKB_DROP_REASON_MAX)) {
+		DEBUG_NET_WARN_ON_ONCE(1);
+		reason = SKB_DROP_REASON_NOT_SPECIFIED;
+	}
+
 	trace_kfree_skb(skb, __builtin_return_address(0), reason);
 	__kfree_skb(skb);
 }
Signature · 1 line
2.36.1
maintainer
#3 lore raw

Re: [PATCH net-next v2 2/4] net: skb: check the boundrary of drop reason in kfree_skb_reason()

net/core/skbuff.c void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) · line 777

Quoted 4 lines · On Thu, 12 May 2022 20:33:11 +0800 [email protected] wrote:
net/core/skbuff.c +4 -0
+	if (unlikely(reason <= 0 || reason >= SKB_DROP_REASON_MAX)) {
+		DEBUG_NET_WARN_ON_ONCE(1);
+		reason = SKB_DROP_REASON_NOT_SPECIFIED;
+	}

With drop_monitor fixes sending an invalid reason to the tracepoint should be a minor bug, right?

Can we just have a:

	DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX);

and avoid having this branch on non-debug builds?

#4 lore raw

Re: [PATCH net-next v2 2/4] net: skb: check the boundrary of drop reason in kfree_skb_reason()

Quoted 7 lines · Jakub Kicinski

net/core/skbuff.c void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) · line 777

Quoted 4 lines · On Thu, 12 May 2022 20:33:11 +0800 [email protected] wrote:
net/core/skbuff.c +4 -0
+     if (unlikely(reason <= 0 || reason >= SKB_DROP_REASON_MAX)) {
+             DEBUG_NET_WARN_ON_ONCE(1);
+             reason = SKB_DROP_REASON_NOT_SPECIFIED;
+     }
Quoted 8 lines

With drop_monitor fixes sending an invalid reason to the tracepoint should be a minor bug, right?

Can we just have a:

        DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX);

and avoid having this branch on non-debug builds?

Exactly what I was going to say.

#5 lore raw

Re: [PATCH net-next v2 2/4] net: skb: check the boundrary of drop reason in kfree_skb_reason()

In reply to Jakub Kicinski
Quoted 7 lines · Jakub Kicinski

net/core/skbuff.c void kfree_skb_reason(struct sk_buff *skb, enum skb_drop_reason reason) · line 777

Quoted 4 lines · On Thu, 12 May 2022 20:33:11 +0800 [email protected] wrote:
net/core/skbuff.c +4 -0
+     if (unlikely(reason <= 0 || reason >= SKB_DROP_REASON_MAX)) {
+             DEBUG_NET_WARN_ON_ONCE(1);
+             reason = SKB_DROP_REASON_NOT_SPECIFIED;
+     }
Quoted 8 lines

With drop_monitor fixes sending an invalid reason to the tracepoint should be a minor bug, right?

Can we just have a:

        DEBUG_NET_WARN_ON_ONCE(reason <= 0 || reason >= SKB_DROP_REASON_MAX);

and avoid having this branch on non-debug builds?

Yeah, it seems this way is more logical. I'll change it in the V3.

#6 lore raw

PATCH v2 3/4 net: skb: change the definition SKB_DR_SET()

In reply to [email protected]

From: Menglong Dong <[email protected]>

The SKB_DR_OR() is used to set the drop reason to a value when it is not set or specified yet. SKB_NOT_DROPPED_YET should also be considered as not set.

---
 include/linux/skbuff.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/skbuff.h +2 -1
index b91d225fdc13..4db3f4a33580 100644
@@ -510,7 +510,8 @@ enum skb_drop_reason {
 	(name = SKB_DROP_REASON_##reason)
 #define SKB_DR_OR(name, reason)					\
 	do {							\
-		if (name == SKB_DROP_REASON_NOT_SPECIFIED)	\
+		if (name == SKB_DROP_REASON_NOT_SPECIFIED ||	\
+		    name == SKB_NOT_DROPPED_YET)		\
 			SKB_DR_SET(name, reason);		\
 	} while (0)
 
Signature · 1 line
2.36.1
#7 lore raw

PATCH v2 4/4 net: tcp: reset 'drop_reason' to NOT_SPCIFIED in tcp_v{4,6}_rcv()

In reply to [email protected]

From: Menglong Dong <[email protected]>

The 'drop_reason' that passed to kfree_skb_reason() in tcp_v4_rcv() and tcp_v6_rcv() can be SKB_NOT_DROPPED_YET(0), as it is used as the return value of tcp_inbound_md5_hash().

And it can panic the kernel with NULL pointer in net_dm_packet_report_size() if the reason is 0, as drop_reasons[0] is NULL.

Fixes 1330b6ef3313 ("skb: make drop reason booleanable")

---
v2:
- consider tcp_v6_rcv()

---
 net/ipv4/tcp_ipv4.c | 1 +
 net/ipv6/tcp_ipv6.c | 1 +
 2 files changed, 2 insertions(+)
net/ipv4/tcp_ipv4.c +1 -0
index 918816ec5dd4..24eb42497a71 100644
@@ -2101,6 +2101,7 @@ int tcp_v4_rcv(struct sk_buff *skb)
 	}
 
 discard_it:
+	SKB_DR_OR(drop_reason, NOT_SPECIFIED);
 	/* Discard frame. */
 	kfree_skb_reason(skb, drop_reason);
 	return 0;
net/ipv6/tcp_ipv6.c +1 -0
index 60bdec257ba7..636ed23d9af0 100644
@@ -1509,6 +1509,7 @@ int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
 discard:
 	if (opt_skb)
 		__kfree_skb(opt_skb);
+	SKB_DR_OR(reason, NOT_SPECIFIED);
 	kfree_skb_reason(skb, reason);
 	return 0;
 csum_err:
Signature · 1 line
2.36.1