Skip to contentSkip to conversation

bpf: check negative offsets in __bpf_skb_min_len()

PATCH In review Cong Wang 2 participants 4 messages Oct 8, 2024 – Oct 28, 2024

Subsystems (4)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 bpf: check negative offsets in __bpf_skb_min_len()

From: Cong Wang <[email protected]>

skb_transport_offset() and skb_transport_offset() can be negative when they are called after we pull the transport header, for example, when we use eBPF sockmap (aka at the point of ->sk_data_ready()).

__bpf_skb_min_len() uses an unsigned int to get these offsets, this leads to a very large number which causes bpf_skb_change_tail() failed unexpectedly.

Fix this by using a signed int to get these offsets and test them against zero.

Fixes 5293efe62df8 ("bpf: add bpf_skb_change_tail helper")
Cc Daniel Borkmann <[email protected]>
---
 net/core/filter.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
net/core/filter.c +15 -6
index 4e3f42cc6611..10ef27639a5d 100644
@@ -3737,13 +3737,22 @@ static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
 
 static u32 __bpf_skb_min_len(const struct sk_buff *skb)
 {
-	u32 min_len = skb_network_offset(skb);
+	int offset = skb_network_offset(skb);
+	u32 min_len = 0;
 
-	if (skb_transport_header_was_set(skb))
-		min_len = skb_transport_offset(skb);
-	if (skb->ip_summed == CHECKSUM_PARTIAL)
-		min_len = skb_checksum_start_offset(skb) +
-			  skb->csum_offset + sizeof(__sum16);
+	if (offset > 0)
+		min_len = offset;
+	if (skb_transport_header_was_set(skb)) {
+		offset = skb_transport_offset(skb);
+		if (offset > 0)
+			min_len = offset;
+	}
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		offset = skb_checksum_start_offset(skb) +
+			 skb->csum_offset + sizeof(__sum16);
+		if (offset > 0)
+			min_len = offset;
+	}
 	return min_len;
 }
 
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 3 lines · Cong Wang

From: Cong Wang <[email protected]>

skb_transport_offset() and skb_transport_offset() can be negative when

nit: I presume the 2nd one is skb_network_offset?

Quoted 7 lines

they are called after we pull the transport header, for example, when we use eBPF sockmap (aka at the point of ->sk_data_ready()).

__bpf_skb_min_len() uses an unsigned int to get these offsets, this leads to a very large number which causes bpf_skb_change_tail() failed unexpectedly.

Quoted 6 lines

Fix this by using a signed int to get these offsets and test them against zero.

Fixes 5293efe62df8 ("bpf: add bpf_skb_change_tail helper")
Cc Daniel Borkmann <[email protected]>

Is there any chance you could also extend the sockmap BPF selftest with this case you're hitting so that BPF CI can run this regularly?

Quoted 25 lines
---
  net/core/filter.c | 21 +++++++++++++++------
  1 file changed, 15 insertions(+), 6 deletions(-)
net/core/filter.c +6 -6
index 4e3f42cc6611..10ef27639a5d 100644
@@ -3737,13 +3737,22 @@ static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
 
  static u32 __bpf_skb_min_len(const struct sk_buff *skb)
  {
-	u32 min_len = skb_network_offset(skb);
+	int offset = skb_network_offset(skb);
+	u32 min_len = 0;
 
-	if (skb_transport_header_was_set(skb))
-		min_len = skb_transport_offset(skb);
-	if (skb->ip_summed == CHECKSUM_PARTIAL)
-		min_len = skb_checksum_start_offset(skb) +
-			  skb->csum_offset + sizeof(__sum16);
+	if (offset > 0)
+		min_len = offset;
+	if (skb_transport_header_was_set(skb)) {
+		offset = skb_transport_offset(skb);

net/core/filter.c static const struct bpf_func_proto bpf_skb_adjust_room_proto = { · line 3756

Quoted 10 lines
net/core/filter.c +9 -0
+		if (offset > 0)
+			min_len = offset;
+	}
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		offset = skb_checksum_start_offset(skb) +
+			 skb->csum_offset + sizeof(__sum16);
+		if (offset > 0)
+			min_len = offset;
+	}
  	return min_len;

I'll let John chime in, but does this mean in case of sockmap min_len always ends up at 0? I just wonder whether we should pass a custom __bpf_skb_min_len to __bpf_skb_change_tail for bpf_skb_change_tail vs sk_skb_change_tail assuming the compiler is able to inlining all this (instead of indirect call).

author
#2 lore raw
Quoted 17 lines · Daniel Borkmann
Quoted 3 lines · Cong Wang

From: Cong Wang <[email protected]>

skb_transport_offset() and skb_transport_offset() can be negative when

nit: I presume the 2nd one is skb_network_offset?

Quoted 10 lines

they are called after we pull the transport header, for example, when we use eBPF sockmap (aka at the point of ->sk_data_ready()).

__bpf_skb_min_len() uses an unsigned int to get these offsets, this leads to a very large number which causes bpf_skb_change_tail() failed unexpectedly.

Fix this by using a signed int to get these offsets and test them against zero.

Quoted 6 lines
Quoted 3 lines
Fixes 5293efe62df8 ("bpf: add bpf_skb_change_tail helper")
Cc Daniel Borkmann <[email protected]>

Is there any chance you could also extend the sockmap BPF selftest with this case you're hitting so that BPF CI can run this regularly?

Yes, my colleague Zijian (Cc'ed) is working on a selftest to cover this case.

Please let me know if you prefer to send it together with the selftest, technically it would make backporting this fix harder, but I am open to any suggestion here.

Quoted 31 lines
Quoted 31 lines
---
  net/core/filter.c | 21 +++++++++++++++------
  1 file changed, 15 insertions(+), 6 deletions(-)
net/core/filter.c +13 -6
index 4e3f42cc6611..10ef27639a5d 100644
@@ -3737,13 +3737,22 @@ static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
  static u32 __bpf_skb_min_len(const struct sk_buff *skb)
  {
-	u32 min_len = skb_network_offset(skb);
+	int offset = skb_network_offset(skb);
+	u32 min_len = 0;
-	if (skb_transport_header_was_set(skb))
-		min_len = skb_transport_offset(skb);
-	if (skb->ip_summed == CHECKSUM_PARTIAL)
-		min_len = skb_checksum_start_offset(skb) +
-			  skb->csum_offset + sizeof(__sum16);
+	if (offset > 0)
+		min_len = offset;
+	if (skb_transport_header_was_set(skb)) {
+		offset = skb_transport_offset(skb);
+		if (offset > 0)
+			min_len = offset;
+	}
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		offset = skb_checksum_start_offset(skb) +
+			 skb->csum_offset + sizeof(__sum16);
+		if (offset > 0)
Quoted 8 lines

net/core/filter.c static const struct bpf_func_proto bpf_skb_adjust_room_proto = { · line 3756

Quoted 3 lines
net/core/filter.c +2 -0
+			min_len = offset;
+	}
  	return min_len;

I'll let John chime in, but does this mean in case of sockmap min_len always ends up at 0? I just wonder whether we should pass a custom __bpf_skb_min_len to __bpf_skb_change_tail for bpf_skb_change_tail vs sk_skb_change_tail assuming the compiler is able to inlining all this (instead of indirect call).

Yes, in case of sockmap skb->data is already past TCP header, so all the offsets here are negative. And since the 'new_len' of bpf_skb_change_tail() is unsigned (too late to change), min_len should be zero.

Thanks.

maintainer
#3 lore raw
Quoted 22 lines · Cong Wang
Quoted 21 lines · Daniel Borkmann
Quoted 3 lines · Cong Wang

From: Cong Wang <[email protected]>

skb_transport_offset() and skb_transport_offset() can be negative when

nit: I presume the 2nd one is skb_network_offset?

Quoted 13 lines

they are called after we pull the transport header, for example, when we use eBPF sockmap (aka at the point of ->sk_data_ready()).

__bpf_skb_min_len() uses an unsigned int to get these offsets, this leads to a very large number which causes bpf_skb_change_tail() failed unexpectedly.

Fix this by using a signed int to get these offsets and test them against zero.

Fixes 5293efe62df8 ("bpf: add bpf_skb_change_tail helper")
Cc Daniel Borkmann <[email protected]>
Quoted 8 lines
Quoted 2 lines

Is there any chance you could also extend the sockmap BPF selftest with this case you're hitting so that BPF CI can run this regularly?

Yes, my colleague Zijian (Cc'ed) is working on a selftest to cover this case.

Please let me know if you prefer to send it together with the selftest, technically it would make backporting this fix harder, but I am open to any suggestion here.

Ideally this is a 2-patch series, first one is the fix itself and the second one contains the BPF selftest so that CI can run it too, and this way it also does not hurt backporting the fix.

Quoted 38 lines
Quoted 29 lines
Quoted 29 lines
   net/core/filter.c | 21 +++++++++++++++------
   1 file changed, 15 insertions(+), 6 deletions(-)
net/core/filter.c +13 -6
index 4e3f42cc6611..10ef27639a5d 100644
@@ -3737,13 +3737,22 @@ static const struct bpf_func_proto bpf_skb_adjust_room_proto = {
   static u32 __bpf_skb_min_len(const struct sk_buff *skb)
   {
-	u32 min_len = skb_network_offset(skb);
+	int offset = skb_network_offset(skb);
+	u32 min_len = 0;
-	if (skb_transport_header_was_set(skb))
-		min_len = skb_transport_offset(skb);
-	if (skb->ip_summed == CHECKSUM_PARTIAL)
-		min_len = skb_checksum_start_offset(skb) +
-			  skb->csum_offset + sizeof(__sum16);
+	if (offset > 0)
+		min_len = offset;
+	if (skb_transport_header_was_set(skb)) {
+		offset = skb_transport_offset(skb);
+		if (offset > 0)
+			min_len = offset;
+	}
+	if (skb->ip_summed == CHECKSUM_PARTIAL) {
+		offset = skb_checksum_start_offset(skb) +
+			 skb->csum_offset + sizeof(__sum16);
+		if (offset > 0)
Quoted 8 lines

net/core/filter.c static const struct bpf_func_proto bpf_skb_adjust_room_proto = { · line 3756

Quoted 3 lines
net/core/filter.c +2 -0
+			min_len = offset;
+	}
   	return min_len;

I'll let John chime in, but does this mean in case of sockmap min_len always ends up at 0? I just wonder whether we should pass a custom __bpf_skb_min_len to __bpf_skb_change_tail for bpf_skb_change_tail vs sk_skb_change_tail assuming the compiler is able to inlining all this (instead of indirect call).

Quoted 3 lines

Yes, in case of sockmap skb->data is already past TCP header, so all the offsets here are negative. And since the 'new_len' of bpf_skb_change_tail() is unsigned (too late to change), min_len should be zero.

Ok, so hopefully this can be further cleaned up/simplified a bit more then.

Thanks,
Daniel