Skip to contentSkip to conversation

udp: fix segmentation crash for GRO packet without fraglist

PATCH In review [email protected] 5 participants 32 messages Apr 15, 2024 – Apr 29, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 udp: fix segmentation crash for GRO packet without fraglist

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30

The reason that the packet loses its fraglist is that in ingress bpf it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into linear and set the fraglist to null.

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function call. As there is no split flow and app may not decode the merged UDP packet, we should drop the packet without fraglist in skb_segment_list here.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +3 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb,
 	if (err)
 		goto err_linearize;
 
+	if (!list_skb)
+		goto err_linearize;
+
 	skb_shinfo(skb)->frag_list = NULL;
 
 	while (list_skb) {
Signature · 1 line
2.18.0
#1 lore raw
Quoted 12 lines · shiming.cheng@ wrote:

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30
Quoted 6 lines

The reason that the packet loses its fraglist is that in ingress bpf it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into linear and set the fraglist to null.

What is the right behavior from BPF with regard to SKB_GSO_FRAGLIST skbs?

Some, like SCTP, cannot be linearized ever, as the do not have a single gso_size.

Should this BPF operation just fail?

Quoted 18 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function call. As there is no split flow and app may not decode the merged UDP packet, we should drop the packet without fraglist in skb_segment_list here.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4510

Quoted 7 lines
net/core/skbuff.c +3 -0
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb,
 	if (err)
 		goto err_linearize;
 
+	if (!list_skb)
+		goto err_linearize;
+
 	skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

#2 lore raw
Quoted 25 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 12 lines · shiming.cheng@ wrote:

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30
Quoted 1 line

The reason that the packet loses its fraglist is that in ingress

bpf

Quoted 4 lines

it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into

linear

Quoted 1 line

and set the fraglist to null.

Quoted 8 lines

What is the right behavior from BPF with regard to SKB_GSO_FRAGLIST skbs?

Some, like SCTP, cannot be linearized ever, as the do not have a single gso_size.

Should this BPF operation just fail?

In most situation for big gso size packet, it indeed fails but BPF doesn't check the result. It seems the udp GRO packet can't be pulled/ trimed/condensed or else it can't be segmented correctly.

As the BPF function comments it doesn't matter if the data pull failed or pull less. It just does a blind best effort pull.

A patch to modify bpf pull length is upstreamed to Google before and below are part of Google BPF expert maze's reply:
[email protected]<[email protected]> #5Apr 13, 2024 02:30AM
I *think* if that patch fixes anything, then it's really proving that there's a bug in the kernel that needs to be fixed instead. It should be legal to call try_make_writable(skb, X) with *any* value of X.

I add maze in loop and we could start more discussion here.

Quoted 33 lines
Quoted 3 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function

call.

Quoted 1 line

As there is no split flow and app may not decode the merged UDP

packet,

Quoted 1 line

we should drop the packet without fraglist in skb_segment_list

here.

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff *skb,

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4510

Quoted 7 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

#3 lore raw
Quoted 50 lines · Lena Wang (王娜)
Quoted 25 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 12 lines · shiming.cheng@ wrote:

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30
Quoted 1 line

The reason that the packet loses its fraglist is that in ingress

bpf

Quoted 4 lines

it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into

linear

Quoted 1 line

and set the fraglist to null.

Quoted 8 lines

What is the right behavior from BPF with regard to SKB_GSO_FRAGLIST skbs?

Some, like SCTP, cannot be linearized ever, as the do not have a single gso_size.

Should this BPF operation just fail?

In most situation for big gso size packet, it indeed fails but BPF doesn't check the result. It seems the udp GRO packet can't be pulled/ trimed/condensed or else it can't be segmented correctly.

As the BPF function comments it doesn't matter if the data pull failed or pull less. It just does a blind best effort pull.

A patch to modify bpf pull length is upstreamed to Google before and below are part of Google BPF expert maze's reply:
[email protected]<[email protected]> #5Apr 13, 2024 02:30AM
I *think* if that patch fixes anything, then it's really proving that there's a bug in the kernel that needs to be fixed instead. It should be legal to call try_make_writable(skb, X) with *any* value of X.

Quoted 1 line

I add maze in loop and we could start more discussion here.

Personally, I think bpf_skb_pull_data() should have automatically (ie. in kernel code) reduced how much it pulls so that it would pull headers only,
and not packet content.
(This is assuming the rest of the code isn't ready to deal with a longer pull, which I think is the case atm. Pulling too much, and then crashing or forcing the stack to drop packets because of them being malformed seems wrong...)

In general it would be nice if there was a way to just say pull all headers... (or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked at the packet, so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length from bpf code itself.

Quoted 24 lines
Quoted 24 lines
Quoted 2 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function

call.

Quoted 1 line

As there is no split flow and app may not decode the merged UDP

packet,

Quoted 1 line

we should drop the packet without fraglist in skb_segment_list

here.

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff *skb,

Quoted 3 lines
net/core/skbuff.c +0 -0
 if (err)
 goto err_linearize;
 
Quoted 8 lines
Quoted 8 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4510

Quoted 4 lines
net/core/skbuff.c +3 -0
+if (!list_skb)
+goto err_linearize;
+
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#4 lore raw
Quoted 51 lines · Maciej Żenczykowski
Quoted 50 lines · Lena Wang (王娜)
Quoted 25 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 12 lines · shiming.cheng@ wrote:

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30
Quoted 1 line

The reason that the packet loses its fraglist is that in ingress

bpf

Quoted 4 lines

it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into

linear

Quoted 1 line

and set the fraglist to null.

Quoted 8 lines

What is the right behavior from BPF with regard to SKB_GSO_FRAGLIST skbs?

Some, like SCTP, cannot be linearized ever, as the do not have a single gso_size.

Should this BPF operation just fail?

In most situation for big gso size packet, it indeed fails but BPF doesn't check the result. It seems the udp GRO packet can't be pulled/ trimed/condensed or else it can't be segmented correctly.

As the BPF function comments it doesn't matter if the data pull failed or pull less. It just does a blind best effort pull.

A patch to modify bpf pull length is upstreamed to Google before and below are part of Google BPF expert maze's reply:
[email protected]<[email protected]> #5Apr 13, 2024 02:30AM
I *think* if that patch fixes anything, then it's really proving that there's a bug in the kernel that needs to be fixed instead. It should be legal to call try_make_writable(skb, X) with *any* value of X.

Quoted 5 lines
Quoted 1 line

I add maze in loop and we could start more discussion here.

Personally, I think bpf_skb_pull_data() should have automatically (ie. in kernel code) reduced how much it pulls so that it would pull headers only,

That would be a helper that parses headers to discover header length. Parsing is better left to the BPF program.

Quoted 33 lines

and not packet content.
(This is assuming the rest of the code isn't ready to deal with a longer pull, which I think is the case atm. Pulling too much, and then crashing or forcing the stack to drop packets because of them being malformed seems wrong...)

In general it would be nice if there was a way to just say pull all headers... (or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked at the packet, so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length from bpf code itself.

Quoted 21 lines
Quoted 21 lines
Quoted 2 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function

call.

Quoted 1 line

As there is no split flow and app may not decode the merged UDP

packet,

Quoted 1 line

we should drop the packet without fraglist in skb_segment_list

here.

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff *skb,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list is linearized, but not a pskb_may_pull that only pulls in part of the list.

Even with BPF being privileged, the kernel should not crash if BPF pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO packet, I think gso_size is still valid, so if the head_skb length does not match gso_size, it has been messed with and should be dropped.

For a GSO_BY_FRAGS skb, there is no single gso_size, and this pull may be entirely undetectable as long as frag_list != NULL?

Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4510

Quoted 1 line
net/core/skbuff.c +0 -0
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#5 lore raw
Quoted 53 lines · Willem de Bruijn
Quoted 51 lines · Maciej Żenczykowski
Quoted 50 lines · Lena Wang (王娜)
Quoted 25 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 12 lines · shiming.cheng@ wrote:

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30
Quoted 1 line

The reason that the packet loses its fraglist is that in ingress

bpf

Quoted 4 lines

it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into

linear

Quoted 1 line

and set the fraglist to null.

Quoted 8 lines

What is the right behavior from BPF with regard to SKB_GSO_FRAGLIST skbs?

Some, like SCTP, cannot be linearized ever, as the do not have a single gso_size.

Should this BPF operation just fail?

In most situation for big gso size packet, it indeed fails but BPF doesn't check the result. It seems the udp GRO packet can't be pulled/ trimed/condensed or else it can't be segmented correctly.

As the BPF function comments it doesn't matter if the data pull failed or pull less. It just does a blind best effort pull.

A patch to modify bpf pull length is upstreamed to Google before and below are part of Google BPF expert maze's reply:
[email protected]<[email protected]> #5Apr 13, 2024 02:30AM
I *think* if that patch fixes anything, then it's really proving that there's a bug in the kernel that needs to be fixed instead. It should be legal to call try_make_writable(skb, X) with *any* value of X.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I add maze in loop and we could start more discussion here.

Personally, I think bpf_skb_pull_data() should have automatically (ie. in kernel code) reduced how much it pulls so that it would pull headers only,

That would be a helper that parses headers to discover header length.

Does it actually need to? Presumably the bpf pull function could notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and reduce the pull accordingly so that it doesn't pull anything from the non-linear fraglist portion???

I know only the generic overview of what udp gso is, not any details, so I am assuming here that there's some sort of guarantee to how these packets are structured... But I imagine there must be or we wouldn't be hitting these issues deeper in the stack?

Quoted 61 lines

Parsing is better left to the BPF program.

Quoted 33 lines

and not packet content.
(This is assuming the rest of the code isn't ready to deal with a longer pull, which I think is the case atm. Pulling too much, and then crashing or forcing the stack to drop packets because of them being malformed seems wrong...)

In general it would be nice if there was a way to just say pull all headers... (or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked at the packet, so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length from bpf code itself.

Quoted 21 lines
Quoted 21 lines
Quoted 2 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function

call.

Quoted 1 line

As there is no split flow and app may not decode the merged UDP

packet,

Quoted 1 line

we should drop the packet without fraglist in skb_segment_list

here.

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff *skb,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list is linearized, but not a pskb_may_pull that only pulls in part of the list.

Even with BPF being privileged, the kernel should not crash if BPF pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO packet, I think gso_size is still valid, so if the head_skb length does not match gso_size, it has been messed with and should be dropped.

For a GSO_BY_FRAGS skb, there is no single gso_size, and this pull may be entirely undetectable as long as frag_list != NULL?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4510

Quoted 1 line
net/core/skbuff.c +0 -0
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

#6 lore raw
Quoted 70 lines · Maciej Żenczykowski
Quoted 53 lines · Willem de Bruijn
Quoted 51 lines · Maciej Żenczykowski
Quoted 50 lines · Lena Wang (王娜)
Quoted 25 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 12 lines · shiming.cheng@ wrote:

From: Shiming Cheng <[email protected]>

A GRO packet without fraglist is crashed and backtrace is as below:

 [ 1100.812205][    C3] CPU: 3 PID: 0 Comm: swapper/3 Tainted:
G        W  OE      6.6.17-android15-0-g380371ea9bf1 #1
 [ 1100.812317][    C3]  __udp_gso_segment+0x298/0x4d4
 [ 1100.812335][    C3]  __skb_gso_segment+0xc4/0x120
 [ 1100.812339][    C3]  udp_rcv_segment+0x50/0x134
 [ 1100.812344][    C3]  udp_queue_rcv_skb+0x74/0x114
 [ 1100.812348][    C3]  udp_unicast_rcv_skb+0x94/0xac
 [ 1100.812358][    C3]  udp_rcv+0x20/0x30
Quoted 1 line

The reason that the packet loses its fraglist is that in ingress

bpf

Quoted 4 lines

it makes a test pull with to make sure it can read packet headers via direct packet access: In bpf_progs/offload.c try_make_writable -> bpf_skb_pull_data -> pskb_may_pull -> __pskb_pull_tail This operation pull the data in fraglist into

linear

Quoted 1 line

and set the fraglist to null.

Quoted 8 lines

What is the right behavior from BPF with regard to SKB_GSO_FRAGLIST skbs?

Some, like SCTP, cannot be linearized ever, as the do not have a single gso_size.

Should this BPF operation just fail?

In most situation for big gso size packet, it indeed fails but BPF doesn't check the result. It seems the udp GRO packet can't be pulled/ trimed/condensed or else it can't be segmented correctly.

As the BPF function comments it doesn't matter if the data pull failed or pull less. It just does a blind best effort pull.

A patch to modify bpf pull length is upstreamed to Google before and below are part of Google BPF expert maze's reply:
[email protected]<[email protected]> #5Apr 13, 2024 02:30AM
I *think* if that patch fixes anything, then it's really proving that there's a bug in the kernel that needs to be fixed instead. It should be legal to call try_make_writable(skb, X) with *any* value of X.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

I add maze in loop and we could start more discussion here.

Personally, I think bpf_skb_pull_data() should have automatically (ie. in kernel code) reduced how much it pulls so that it would pull headers only,

That would be a helper that parses headers to discover header length.

Does it actually need to? Presumably the bpf pull function could notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and reduce the pull accordingly so that it doesn't pull anything from the non-linear fraglist portion???

Quoted 4 lines

I know only the generic overview of what udp gso is, not any details, so I am assuming here that there's some sort of guarantee to how these packets are structured... But I imagine there must be or we wouldn't be hitting these issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed the headers are in the linear portion,
and the pull should simply be ignored?

Quoted 54 lines
Quoted 54 lines

Parsing is better left to the BPF program.

Quoted 33 lines

and not packet content.
(This is assuming the rest of the code isn't ready to deal with a longer pull, which I think is the case atm. Pulling too much, and then crashing or forcing the stack to drop packets because of them being malformed seems wrong...)

In general it would be nice if there was a way to just say pull all headers... (or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked at the packet, so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length from bpf code itself.

Quoted 21 lines
Quoted 21 lines
Quoted 2 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function

call.

Quoted 1 line

As there is no split flow and app may not decode the merged UDP

packet,

Quoted 1 line

we should drop the packet without fraglist in skb_segment_list

here.

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff *skb,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list is linearized, but not a pskb_may_pull that only pulls in part of the list.

Even with BPF being privileged, the kernel should not crash if BPF pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO packet, I think gso_size is still valid, so if the head_skb length does not match gso_size, it has been messed with and should be dropped.

Quoted 8 lines
Quoted 8 lines

For a GSO_BY_FRAGS skb, there is no single gso_size, and this pull may be entirely undetectable as long as frag_list != NULL?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff *skb, · line 4510

Quoted 1 line
net/core/skbuff.c +0 -0
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#7 lore raw
Quoted 12 lines
Quoted 12 lines
Quoted 5 lines
Quoted 3 lines

Personally, I think bpf_skb_pull_data() should have automatically (ie. in kernel code) reduced how much it pulls so that it would pull headers only,

That would be a helper that parses headers to discover header length.

Does it actually need to? Presumably the bpf pull function could notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and reduce the pull accordingly so that it doesn't pull anything from the non-linear fraglist portion???

Quoted 11 lines
Quoted 4 lines

I know only the generic overview of what udp gso is, not any details, so I am assuming here that there's some sort of guarantee to how these packets are structured... But I imagine there must be or we wouldn't be hitting these issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed the headers are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over having to add then in the net hot path only to protect against dangerous BPF programs.

In this case, it would be detecting this GSO type and failing the operation if exceeding skb_headlen().

Quoted 63 lines
Quoted 52 lines
Quoted 52 lines
Quoted 34 lines

and not packet content.
(This is assuming the rest of the code isn't ready to deal with a longer pull, which I think is the case atm. Pulling too much, and then crashing or forcing the stack to drop packets because of them being malformed seems wrong...)

In general it would be nice if there was a way to just say pull all headers... (or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked at the packet, so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length from bpf code itself.

Quoted 21 lines
Quoted 21 lines
Quoted 2 lines

BPF needs to modify a proper length to do pull data. However kernel should also improve the flow to avoid crash from a bpf function

call.

Quoted 1 line

As there is no split flow and app may not decode the merged UDP

packet,

Quoted 1 line

we should drop the packet without fraglist in skb_segment_list

here.

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff *skb,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list is linearized, but not a pskb_may_pull that only pulls in part of the list.

Even with BPF being privileged, the kernel should not crash if BPF pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO packet, I think gso_size is still valid, so if the head_skb length does not match gso_size, it has been messed with and should be dropped.

Quoted 8 lines
Quoted 8 lines

For a GSO_BY_FRAGS skb, there is no single gso_size, and this pull may be entirely undetectable as long as frag_list != NULL?

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4510

Quoted 1 line
net/core/skbuff.c +0 -0
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is the best we can do indeed, I think.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#8 lore raw
Quoted 33 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so that it

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover header

length.

Quoted 4 lines
Quoted 4 lines

Does it actually need to? Presumably the bpf pull function could notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and reduce

the pull

Quoted 4 lines
Quoted 4 lines

accordingly so that it doesn't pull anything from the non-linear fraglist portion???

I know only the generic overview of what udp gso is, not any

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how these

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we wouldn't be

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed the

headers

Quoted 4 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 1 line
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over having to add then in the net hot path only to protect against dangerous BPF programs.

Is it OK to ignore or decrease pull length for udp gro fraglist packet? It could save the normal packet and sent to user correctly.

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{
+ if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+ (SKB_GSO_UDP |SKB_GSO_UDP_L4)) {
+ return 0;

+ or if (write_len > skb_headlen(skb))
+ write_len = skb_headlen(skb);
+ }

	return skb_ensure_writable(skb, write_len);
}
Quoted 61 lines

In this case, it would be detecting this GSO type and failing the operation if exceeding skb_headlen().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and not packet content.
(This is assuming the rest of the code isn't ready to deal

with a longer pull,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which I think is the case atm. Pulling too much, and then

crashing or forcing

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the stack to drop packets because of them being malformed

seems wrong...)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In general it would be nice if there was a way to just say

pull all headers...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

(or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked

at the packet,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length

from bpf

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

code itself.

Quoted 1 line
Quoted 1 line
Quoted 1 line

BPF needs to modify a proper length to do pull data.

However kernel

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

should also improve the flow to avoid crash from a bpf

function

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

call.

Quoted 1 line

As there is no split flow and app may not decode the

merged UDP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

packet,

Quoted 1 line

we should drop the packet without fraglist in

skb_segment_list

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

here.

Quoted 2 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist

chaining.")

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Shiming Cheng <

[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

net/core/skbuff.c struct sk_buff

Quoted 10 lines
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff

*skb_segment_list(struct

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

sk_buff *skb,

net/core/skbuff.c struct sk_buff · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list is linearized, but not a pskb_may_pull that only pulls in part of

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

list.

Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Even with BPF being privileged, the kernel should not crash if

BPF

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO packet,

I

Quoted 1 line
Quoted 1 line
Quoted 1 line

think gso_size is still valid, so if the head_skb length does

not

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

match gso_size, it has been messed with and should be dropped.

Is it OK as below? Is it OK to add log to record the error for easy checking issue.

In net/core/skbuff.c skb_segment_list +unsigned int mss = skb_shinfo(head_skb)->gso_size; +bool err_len = false;

+if ( mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
+ pr_err("skb is dropped due to messed data. gso size:%d,
+ hdrlen:%d", mss, skb_headlen(head_skb)
+ if (!list_skb)
+ goto err_linearize;
+ else
+ err_len = true;
+}

...
+if (err_len) {
+ goto err_linearize;
+}

skb_get(skb);
...

Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

For a GSO_BY_FRAGS skb, there is no single gso_size, and this

pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

may be entirely undetectable as long as frag_list != NULL?

In function skb_segment_list(), it just handle udp fraglist gro packet. nr_frags will be 0 here.

It records a SKB_GSO_DODGY in gso_type when doing partially eaten for fraglist in __pskb_pull_tail and in skb_segment() it will check and disable NETIF_F_SG.
skb_segment could segment data as gso_size even if it is pulled into hearder skb. I am not sure if it can decode when frag_list is NULL or partially eaten as no BPF pulls illegal length for tcp packet. Our platfrom doesn't meet issues in skb_segment for tcp packet till now.

Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

net/core/skbuff.c struct sk_buff · line 4510

Quoted 1 line
net/core/skbuff.c +0 -0
 skb_shinfo(skb)->frag_list = NULL;

In absense of plugging the issue in BPF, dropping here is

the best

Quoted 4 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

we can do indeed, I think.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#9 lore raw
Quoted 47 lines · Lena Wang (王娜)
Quoted 33 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so that it

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover header

length.

Quoted 4 lines
Quoted 4 lines

Does it actually need to? Presumably the bpf pull function could notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and reduce

the pull

Quoted 4 lines
Quoted 4 lines

accordingly so that it doesn't pull anything from the non-linear fraglist portion???

I know only the generic overview of what udp gso is, not any

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how these

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we wouldn't be

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed the

headers

Quoted 4 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 1 line
Quoted 6 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over having to add then in the net hot path only to protect against dangerous BPF programs.

Is it OK to ignore or decrease pull length for udp gro fraglist packet? It could save the normal packet and sent to user correctly.

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

Review

Quoted 2 lines
(quoted diff) +2 -0
+	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+		(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Review

Quoted 1 line
(quoted diff) +1 -0
+		return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	     or if (write_len > skb_headlen(skb))
+			write_len = skb_headlen(skb);

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Quoted 80 lines
(quoted diff) +1 -0
+	}
	return skb_ensure_writable(skb, write_len);
}
Quoted 61 lines

In this case, it would be detecting this GSO type and failing the operation if exceeding skb_headlen().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and not packet content.
(This is assuming the rest of the code isn't ready to deal

with a longer pull,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which I think is the case atm. Pulling too much, and then

crashing or forcing

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the stack to drop packets because of them being malformed

seems wrong...)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In general it would be nice if there was a way to just say

pull all headers...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

(or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even looked

at the packet,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

so that you can look at the packet,
so it's relatively hard/annoying to pull the correct length

from bpf

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

code itself.

Quoted 1 line
Quoted 1 line
Quoted 1 line

BPF needs to modify a proper length to do pull data.

However kernel

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

should also improve the flow to avoid crash from a bpf

function

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

call.

Quoted 1 line

As there is no split flow and app may not decode the

merged UDP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

packet,

Quoted 1 line

we should drop the packet without fraglist in

skb_segment_list

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

here.

Quoted 2 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist

chaining.")

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Shiming Cheng <

[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

net/core/skbuff.c struct sk_buff

Quoted 10 lines
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff

*skb_segment_list(struct

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

sk_buff *skb,

net/core/skbuff.c struct sk_buff · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list is linearized, but not a pskb_may_pull that only pulls in part of

the

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

list.

Quoted 10 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Even with BPF being privileged, the kernel should not crash if

BPF

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO packet,

I

Quoted 1 line
Quoted 1 line
Quoted 1 line

think gso_size is still valid, so if the head_skb length does

not

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

match gso_size, it has been messed with and should be dropped.

Is it OK as below? Is it OK to add log to record the error for easy checking issue.

In net/core/skbuff.c skb_segment_list

Review

Quoted 6 lines
(quoted diff) +5 -0
+unsigned int mss = skb_shinfo(head_skb)->gso_size;
+bool err_len = false;
 
+if ( mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
+	pr_err("skb is dropped due to messed data. gso size:%d,
+		hdrlen:%d", mss, skb_headlen(head_skb)

Such logs should always be rate limited. But no need to log cases where we well understood how we get there.

I would stick with one approach: either in the BPF func or in segmentation, not both. And then I find BPF preferable, as explained before.

Quoted 13 lines
(quoted diff) +8 -0
+	if (!list_skb)
+		goto err_linearize;
+	else
+		err_len = true;
+}
 
...
+if (err_len) {
+	goto err_linearize;
+}

skb_get(skb);
...

#10 lore raw
Quoted 57 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 8 lines
Quoted 8 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so that it

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf pull function

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from the non-

linear

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso is, not any

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we wouldn't

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed the

Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over having

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against dangerous

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro fraglist

packet?

Quoted 2 lines

It could save the normal packet and sent to user correctly.

Quoted 9 lines

Review

Quoted 6 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO. In udp_offload.c udp4_gro_complete gso_type adds "SKB_GSO_FRAGLIST| SKB_GSO_UDP_L4". Here checking these two flags is to limit the packet as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that may addrive skb_segment_list.

Quoted 8 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);
Quoted 8 lines

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{
+ if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+ SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+ return 0;
+ }

        return skb_ensure_writable(skb, write_len);
}
Quoted 104 lines
Quoted 10 lines

+}
return skb_ensure_writable(skb, write_len);
}

Quoted 5 lines

In this case, it would be detecting this GSO type and failing the operation if exceeding skb_headlen().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and not packet content.
(This is assuming the rest of the code isn't ready to

deal

Quoted 2 lines
Quoted 2 lines

with a longer pull,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which I think is the case atm. Pulling too much, and

then

Quoted 5 lines
Quoted 5 lines

crashing or forcing

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the stack to drop packets because of them being malformed

seems wrong...)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In general it would be nice if there was a way to just

say

Quoted 3 lines
Quoted 3 lines

pull all headers...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

(or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even

looked

Quoted 3 lines
Quoted 3 lines

at the packet,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

so that you can look at the packet,
so it's relatively hard/annoying to pull the correct

length

Quoted 4 lines
Quoted 4 lines

from bpf

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

code itself.

Quoted 1 line
Quoted 1 line
Quoted 1 line

BPF needs to modify a proper length to do pull

data.

Quoted 2 lines
Quoted 2 lines

However kernel

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

should also improve the flow to avoid crash from a

bpf

Quoted 3 lines
Quoted 3 lines

function

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

call.

Quoted 1 line

As there is no split flow and app may not decode

the

Quoted 22 lines
Quoted 22 lines

merged UDP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

packet,

Quoted 1 line

we should drop the packet without fraglist in

skb_segment_list

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

here.

Quoted 2 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist

chaining.")

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Shiming Cheng <

[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

net/core/skbuff.c struct sk_buff

Quoted 10 lines
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff

*skb_segment_list(struct

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sk_buff *skb,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list

is

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

linearized, but not a pskb_may_pull that only pulls in part

of

Quoted 4 lines
Quoted 4 lines

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

list.

Even with BPF being privileged, the kernel should not crash

if

Quoted 4 lines
Quoted 4 lines

BPF

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO

packet,

Quoted 2 lines
Quoted 2 lines

I

Quoted 1 line
Quoted 1 line
Quoted 1 line

think gso_size is still valid, so if the head_skb length

does

Quoted 2 lines
Quoted 2 lines

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

match gso_size, it has been messed with and should be

dropped.

Review

Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Is it OK as below? Is it OK to add log to record the error for easy checking issue.

In net/core/skbuff.c skb_segment_list

(quoted diff) +5 -0
+unsigned int mss = skb_shinfo(head_skb)->gso_size;
+bool err_len = false;
 
+if ( mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
+pr_err("skb is dropped due to messed data. gso size:%d,
+hdrlen:%d", mss, skb_headlen(head_skb)

Such logs should always be rate limited. But no need to log cases where we well understood how we get there.

Quoted 4 lines

I would stick with one approach: either in the BPF func or in segmentation, not both. And then I find BPF preferable, as explained before.

OK, we try make a patch in BPF func.

Quoted 13 lines
Quoted 13 lines
(quoted diff) +8 -0
+if (!list_skb)
+goto err_linearize;
+else
+err_len = true;
+}
 
...
+if (err_len) {
+goto err_linearize;
+}

skb_get(skb);
...

#11 lore raw
Quoted 98 lines · Lena Wang (王娜)
Quoted 57 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 8 lines
Quoted 8 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so that it

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf pull function

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST) and

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from the non-

linear

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso is, not any

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we wouldn't

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed the

Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over having

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against dangerous

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro fraglist

packet?

Quoted 2 lines

It could save the normal packet and sent to user correctly.

Quoted 9 lines

Review

Quoted 6 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO. In udp_offload.c udp4_gro_complete gso_type adds "SKB_GSO_FRAGLIST| SKB_GSO_UDP_L4". Here checking these two flags is to limit the packet as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that may addrive skb_segment_list.

Quoted 8 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);
Quoted 8 lines

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

Review

Quoted 3 lines
(quoted diff) +3 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               return 0;

please limit write_len to skb_headlen() instead of just returning 0

Quoted 120 lines

+ }

        return skb_ensure_writable(skb, write_len);
}
Quoted 104 lines
Quoted 10 lines

+}
return skb_ensure_writable(skb, write_len);
}

Quoted 5 lines

In this case, it would be detecting this GSO type and failing the operation if exceeding skb_headlen().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and not packet content.
(This is assuming the rest of the code isn't ready to

deal

Quoted 2 lines
Quoted 2 lines

with a longer pull,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which I think is the case atm. Pulling too much, and

then

Quoted 5 lines
Quoted 5 lines

crashing or forcing

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the stack to drop packets because of them being malformed

seems wrong...)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In general it would be nice if there was a way to just

say

Quoted 3 lines
Quoted 3 lines

pull all headers...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

(or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've even

looked

Quoted 3 lines
Quoted 3 lines

at the packet,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

so that you can look at the packet,
so it's relatively hard/annoying to pull the correct

length

Quoted 4 lines
Quoted 4 lines

from bpf

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

code itself.

Quoted 1 line
Quoted 1 line
Quoted 1 line

BPF needs to modify a proper length to do pull

data.

Quoted 2 lines
Quoted 2 lines

However kernel

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

should also improve the flow to avoid crash from a

bpf

Quoted 3 lines
Quoted 3 lines

function

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

call.

Quoted 1 line

As there is no split flow and app may not decode

the

Quoted 22 lines
Quoted 22 lines

merged UDP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

packet,

Quoted 1 line

we should drop the packet without fraglist in

skb_segment_list

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

here.

Quoted 2 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist

chaining.")

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Shiming Cheng <

[email protected]>

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

net/core/skbuff.c struct sk_buff

Quoted 10 lines
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..f68f2679b086 100644
@@ -4504,6 +4504,9 @@ struct sk_buff

*skb_segment_list(struct

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sk_buff *skb,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data frag_list

is

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

linearized, but not a pskb_may_pull that only pulls in part

of

Quoted 4 lines
Quoted 4 lines

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

list.

Even with BPF being privileged, the kernel should not crash

if

Quoted 4 lines
Quoted 4 lines

BPF

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO

packet,

Quoted 2 lines
Quoted 2 lines

I

Quoted 1 line
Quoted 1 line
Quoted 1 line

think gso_size is still valid, so if the head_skb length

does

Quoted 2 lines
Quoted 2 lines

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

match gso_size, it has been messed with and should be

dropped.

Review

Quoted 11 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Is it OK as below? Is it OK to add log to record the error for easy checking issue.

In net/core/skbuff.c skb_segment_list

(quoted diff) +5 -0
+unsigned int mss = skb_shinfo(head_skb)->gso_size;
+bool err_len = false;
 
+if ( mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
+pr_err("skb is dropped due to messed data. gso size:%d,
+hdrlen:%d", mss, skb_headlen(head_skb)

Such logs should always be rate limited. But no need to log cases where we well understood how we get there.

Quoted 4 lines

I would stick with one approach: either in the BPF func or in segmentation, not both. And then I find BPF preferable, as explained before.

OK, we try make a patch in BPF func.

Quoted 6 lines
Quoted 6 lines
(quoted diff) +5 -0
+if (!list_skb)
+goto err_linearize;
+else
+err_len = true;
+}
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

...

(quoted diff) +3 -0
+if (err_len) {
+goto err_linearize;
+}

skb_get(skb);
...

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#12 lore raw
Quoted 118 lines · Maciej Żenczykowski

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 4 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 5 lines
Quoted 5 lines

you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open

attachments

Quoted 5 lines
Quoted 5 lines

until

Quoted 4 lines
Quoted 4 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so

that it

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf pull

function

Quoted 3 lines
Quoted 3 lines

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST)

and

Quoted 3 lines
Quoted 3 lines

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from the

non-

Quoted 4 lines
Quoted 4 lines

linear

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso is, not

any

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we

wouldn't

Quoted 5 lines
Quoted 5 lines

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over

having

Quoted 2 lines
Quoted 2 lines

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against

dangerous

Quoted 16 lines
Quoted 7 lines

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro fraglist

packet?

Quoted 2 lines

It could save the normal packet and sent to user correctly.

Quoted 9 lines

Review

Quoted 6 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Quoted 3 lines

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO. In udp_offload.c udp4_gro_complete gso_type adds "SKB_GSO_FRAGLIST| SKB_GSO_UDP_L4". Here checking these two flags is to limit the

packet

Quoted 3 lines

as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that may

addrive

Quoted 24 lines

skb_segment_list.

Quoted 8 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);
Quoted 8 lines

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >
Quoted 5 lines

skb_headlen(skb))) {

Quoted 1 line

+ return 0;

please limit write_len to skb_headlen() instead of just returning 0

Hi Maze & Willem,
Maze's advice is:
In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{
+ if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+ SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+ write_len = skb_headlen(skb);
+ }

        return skb_ensure_writable(skb, write_len);
}

Willem's advice is to "Failing for a pull > skb_headlen is arguably reasonable...". It prefers to return 0 :
+ if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+ SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+ return 0;
+ }

It seems a bit conflict. However I am not sure if my understanding is right and hope to get your further guide.

Thanks
Lena

Quoted 145 lines
Quoted 10 lines

+ }

        return skb_ensure_writable(skb, write_len);
}
Quoted 6 lines
Quoted 6 lines

+}
return skb_ensure_writable(skb, write_len);
}

Quoted 1 line

In this case, it would be detecting this GSO type and failing

the

Quoted 10 lines
Quoted 10 lines
Quoted 4 lines
Quoted 4 lines

operation if exceeding skb_headlen().

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

and not packet content.
(This is assuming the rest of the code isn't ready to

deal

Quoted 2 lines
Quoted 2 lines

with a longer pull,

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

which I think is the case atm. Pulling too much, and

then

Quoted 2 lines
Quoted 2 lines

crashing or forcing

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the stack to drop packets because of them being

malformed

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

seems wrong...)

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

In general it would be nice if there was a way to

just

Quoted 4 lines
Quoted 4 lines

say

Quoted 3 lines
Quoted 3 lines

pull all headers...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

(or possibly all L2/L3/L4 headers)
You in general need to pull stuff *before* you've

even

Quoted 4 lines
Quoted 4 lines

looked

Quoted 3 lines
Quoted 3 lines

at the packet,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

so that you can look at the packet,
so it's relatively hard/annoying to pull the correct

Quoted 8 lines
Quoted 8 lines

length

Quoted 4 lines
Quoted 4 lines

from bpf

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

code itself.

Quoted 1 line
Quoted 1 line
Quoted 1 line

BPF needs to modify a proper length to do pull

data.

Quoted 2 lines
Quoted 2 lines

However kernel

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

should also improve the flow to avoid crash

from a

Quoted 4 lines
Quoted 4 lines

bpf

Quoted 3 lines
Quoted 3 lines

function

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

call.

Quoted 1 line

As there is no split flow and app may not

decode

Quoted 8 lines
Quoted 8 lines

the

Quoted 7 lines
Quoted 7 lines

merged UDP

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

packet,

Quoted 1 line

we should drop the packet without fraglist in

skb_segment_list

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

here.

Quoted 2 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO

fraglist

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

chaining.")

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Shiming Cheng <

[email protected]>

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Signed-off-by Lena Wang <

[email protected]>

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 5 lines
---
 net/core/skbuff.c | 3 +++
 1 file changed, 3 insertions(+)
(quoted diff) +0 -0

b/net/core/skbuff.c

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

net/core/skbuff.c struct sk_buff

Quoted 4 lines

index b99127712e67..f68f2679b086 100644

net/core/skbuff.c +0 -0
@@ -4504,6 +4504,9 @@ struct sk_buff

*skb_segment_list(struct

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

sk_buff *skb,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

net/core/skbuff.c struct sk_buff · line 4509

Quoted 6 lines
net/core/skbuff.c +3 -0
 if (err)
 goto err_linearize;
 
+if (!list_skb)
+goto err_linearize;
+

This would catch the case where the entire data

frag_list

Quoted 2 lines
Quoted 2 lines

is

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

linearized, but not a pskb_may_pull that only pulls in

part

Quoted 5 lines
Quoted 5 lines

of

Quoted 4 lines
Quoted 4 lines

the

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

list.

Even with BPF being privileged, the kernel should not

crash

Quoted 8 lines
Quoted 8 lines

if

Quoted 4 lines
Quoted 4 lines

BPF

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

pulls a FRAGLIST GSO skb.

But the check needs to be refined a bit. For a UDP GSO

packet,

Quoted 2 lines
Quoted 2 lines

I

Quoted 1 line
Quoted 1 line
Quoted 1 line

think gso_size is still valid, so if the head_skb

length

Quoted 6 lines
Quoted 6 lines

does

Quoted 2 lines
Quoted 2 lines

not

Quoted 1 line
Quoted 1 line
Quoted 1 line

match gso_size, it has been messed with and should be

dropped.

Quoted 2 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Is it OK as below? Is it OK to add log to record the error for

easy

Quoted 13 lines
Quoted 13 lines

Review

Quoted 9 lines

checking issue.

In net/core/skbuff.c skb_segment_list

(quoted diff) +5 -0
+unsigned int mss = skb_shinfo(head_skb)->gso_size;
+bool err_len = false;
 
+if ( mss != GSO_BY_FRAGS && mss != skb_headlen(head_skb)) {
+pr_err("skb is dropped due to messed data. gso size:%d,
+hdrlen:%d", mss, skb_headlen(head_skb)

Such logs should always be rate limited. But no need to log cases where we well understood how we get there.

Quoted 2 lines
Quoted 2 lines

I would stick with one approach: either in the BPF func or in segmentation, not both. And then I find BPF preferable, as

explained

Quoted 10 lines
Quoted 2 lines

before.

OK, we try make a patch in BPF func.

Quoted 6 lines
Quoted 6 lines
(quoted diff) +5 -0
+if (!list_skb)
+goto err_linearize;
+else
+err_len = true;
+}
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

...

(quoted diff) +3 -0
+if (err_len) {
+goto err_linearize;
+}

skb_get(skb);
...

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#13 lore raw
Quoted 145 lines · Lena Wang (王娜)
Quoted 118 lines · Maciej Żenczykowski

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 4 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 5 lines
Quoted 5 lines

you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open

attachments

Quoted 5 lines
Quoted 5 lines

until

Quoted 4 lines
Quoted 4 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so

that it

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf pull

function

Quoted 3 lines
Quoted 3 lines

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST)

and

Quoted 3 lines
Quoted 3 lines

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from the

non-

Quoted 4 lines
Quoted 4 lines

linear

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso is, not

any

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we

wouldn't

Quoted 5 lines
Quoted 5 lines

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over

having

Quoted 2 lines
Quoted 2 lines

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against

dangerous

Quoted 16 lines
Quoted 7 lines

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro fraglist

packet?

Quoted 2 lines

It could save the normal packet and sent to user correctly.

Quoted 9 lines

Review

Quoted 6 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Quoted 3 lines

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO. In udp_offload.c udp4_gro_complete gso_type adds "SKB_GSO_FRAGLIST| SKB_GSO_UDP_L4". Here checking these two flags is to limit the

packet

Quoted 3 lines

as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that may

addrive

Quoted 24 lines

skb_segment_list.

Quoted 8 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);
Quoted 8 lines

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >
Quoted 5 lines

skb_headlen(skb))) {

Quoted 1 line

+ return 0;

please limit write_len to skb_headlen() instead of just returning 0

Hi Maze & Willem,
Maze's advice is:
In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +4 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               write_len = skb_headlen(skb);
+       }
        return skb_ensure_writable(skb, write_len);

}

Willem's advice is to "Failing for a pull > skb_headlen is arguably reasonable...". It prefers to return 0 :

(quoted diff) +4 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               return 0;
+       }
Quoted 2 lines

It seems a bit conflict. However I am not sure if my understanding is right and hope to get your further guide.

I did not mean to return 0. But to fail a request that would pull an unsafe amount. The caller must get a clear error signal.

Back to the original report: the issue should already have been fixed by commit 876e8ca83667 ("net: fix NULL pointer in skb_segment_list"). But that commit is in the kernel for which you report the error.

Turns out that the crash is not in skb_segment_list, but later in __udpv4_gso_segment_list_csum. Which unconditionally dereferences udp_hdr(seg).

The above fix also mentions skb pull as the culprit, but does not include a BPF program. If this can be reached in other ways, then we do need a stronger test in skb_segment_list, as you propose.

I don't want to narrowly check whether udp_hdr is safe. Essentially, an SKB_GSO_FRAGLIST skb layout cannot be trusted at all if even one byte would get pulled.

#14 lore raw
Quoted 147 lines · Willem de Bruijn
Quoted 145 lines · Lena Wang (王娜)
Quoted 118 lines · Maciej Żenczykowski

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 4 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 5 lines
Quoted 5 lines

you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open

attachments

Quoted 5 lines
Quoted 5 lines

until

Quoted 4 lines
Quoted 4 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so

that it

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf pull

function

Quoted 3 lines
Quoted 3 lines

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST)

and

Quoted 3 lines
Quoted 3 lines

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from the

non-

Quoted 4 lines
Quoted 4 lines

linear

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso is, not

any

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we

wouldn't

Quoted 5 lines
Quoted 5 lines

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over

having

Quoted 2 lines
Quoted 2 lines

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against

dangerous

Quoted 16 lines
Quoted 7 lines

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro fraglist

packet?

Quoted 2 lines

It could save the normal packet and sent to user correctly.

Quoted 9 lines

Review

Quoted 6 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Quoted 3 lines

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO. In udp_offload.c udp4_gro_complete gso_type adds "SKB_GSO_FRAGLIST| SKB_GSO_UDP_L4". Here checking these two flags is to limit the

packet

Quoted 3 lines

as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that may

addrive

Quoted 24 lines

skb_segment_list.

Quoted 8 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);
Quoted 8 lines

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >
Quoted 5 lines

skb_headlen(skb))) {

Quoted 1 line

+ return 0;

please limit write_len to skb_headlen() instead of just returning 0

Hi Maze & Willem,
Maze's advice is:
In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +4 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               write_len = skb_headlen(skb);
+       }
        return skb_ensure_writable(skb, write_len);

}

Willem's advice is to "Failing for a pull > skb_headlen is arguably reasonable...". It prefers to return 0 :

(quoted diff) +4 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               return 0;
+       }
Quoted 5 lines
Quoted 2 lines

It seems a bit conflict. However I am not sure if my understanding is right and hope to get your further guide.

I did not mean to return 0. But to fail a request that would pull an unsafe amount. The caller must get a clear error signal.

That's hostile on userspace.
Currently the caller doesn't even check the error return... Why would we? We already have to reload all pointers, and have to do and will thus redo checking on those.

What do you expect the caller to do? Subtract -1 and try again? That's hard to do from BPF as it involves looping... and is slow.

We already try to not pull too much:

void try_make_writable(struct __sk_buff* skb, int len) {
if (len > skb->len) len = skb->len;
if (skb->data_end - skb->data < len) bpf_skb_pull_data(skb, len);
}

Is there at least something like skb->len that has the actually pullable length in it?

Or are these skb's structured in such a way that there is never a need to pull anything,
because the headers are already always in the linear portion?

Quoted 8 lines

Back to the original report: the issue should already have been fixed by commit 876e8ca83667 ("net: fix NULL pointer in skb_segment_list"). But that commit is in the kernel for which you report the error.

Turns out that the crash is not in skb_segment_list, but later in __udpv4_gso_segment_list_csum. Which unconditionally dereferences udp_hdr(seg).

Quoted 7 lines

The above fix also mentions skb pull as the culprit, but does not include a BPF program. If this can be reached in other ways, then we do need a stronger test in skb_segment_list, as you propose.

I don't want to narrowly check whether udp_hdr is safe. Essentially, an SKB_GSO_FRAGLIST skb layout cannot be trusted at all if even one byte would get pulled.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#15 lore raw
Quoted 149 lines · Maciej Żenczykowski
Quoted 147 lines · Willem de Bruijn
Quoted 145 lines · Lena Wang (王娜)
Quoted 118 lines · Maciej Żenczykowski

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 4 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 5 lines
Quoted 5 lines

you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open

attachments

Quoted 5 lines
Quoted 5 lines

until

Quoted 4 lines
Quoted 4 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data() should have

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it pulls so

that it

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to discover

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf pull

function

Quoted 3 lines
Quoted 3 lines

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO FRAGLIST)

and

Quoted 3 lines
Quoted 3 lines

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from the

non-

Quoted 4 lines
Quoted 4 lines

linear

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso is, not

any

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee to how

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or we

wouldn't

Quoted 5 lines
Quoted 5 lines

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already guaranteed

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers, over

having

Quoted 2 lines
Quoted 2 lines

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against

dangerous

Quoted 16 lines
Quoted 7 lines

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro fraglist

packet?

Quoted 2 lines

It could save the normal packet and sent to user correctly.

Quoted 9 lines

Review

Quoted 6 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with SKB_GSO_FRAGLIST.

Quoted 3 lines

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO. In udp_offload.c udp4_gro_complete gso_type adds "SKB_GSO_FRAGLIST| SKB_GSO_UDP_L4". Here checking these two flags is to limit the

packet

Quoted 3 lines

as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that may

addrive

Quoted 24 lines

skb_segment_list.

Quoted 8 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a sane workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);
Quoted 8 lines

Truncating requests would be a surprising change of behavior for this function.

Failing for a pull > skb_headlen is arguably reasonable, as the alternative is that we let it go through but have to drop the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

Review

Quoted 2 lines
(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >
Quoted 5 lines

skb_headlen(skb))) {

Quoted 1 line

+ return 0;

please limit write_len to skb_headlen() instead of just returning 0

Hi Maze & Willem,
Maze's advice is:
In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +4 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               write_len = skb_headlen(skb);
+       }
        return skb_ensure_writable(skb, write_len);

}

Willem's advice is to "Failing for a pull > skb_headlen is arguably reasonable...". It prefers to return 0 :

(quoted diff) +4 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len > skb_headlen(skb))) {
+               return 0;
+       }
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

It seems a bit conflict. However I am not sure if my understanding is right and hope to get your further guide.

I did not mean to return 0. But to fail a request that would pull an unsafe amount. The caller must get a clear error signal.

That's hostile on userspace.
Currently the caller doesn't even check the error return...

It can, and probably should.

bpf_skb_pull data returns the error code from bpf_try_make_writable:

   return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
Quoted 8 lines

Why would we? We already have to reload all pointers, and have to do and will thus redo checking on those.

What do you expect the caller to do? Subtract -1 and try again? That's hard to do from BPF as it involves looping... and is slow.

We already try to not pull too much:

Quoted 7 lines

void try_make_writable(struct __sk_buff* skb, int len) {
if (len > skb->len) len = skb->len;
if (skb->data_end - skb->data < len) bpf_skb_pull_data(skb, len);
}

Is there at least something like skb->len that has the actually pullable length in it?

The above snippet shows that it passes skb_headlen if the caller passes 0.

But your BPF program does not even need the data writable, so then it is of little help of course.

Quoted 3 lines

Or are these skb's structured in such a way that there is never a need to pull anything,
because the headers are already always in the linear portion?

That is indeed the case.

So as far as I can see:

A BPF program that just wants to pull the network and transport headers can diligently pull exactly what is needed. And will not even observe any data pulled into linear in practice. This is still advisable rather than trusting that the headers are linear. It may also be required by the validator? Don't know. But check the return value.

Quoted 19 lines
Quoted 9 lines

Back to the original report: the issue should already have been fixed by commit 876e8ca83667 ("net: fix NULL pointer in skb_segment_list"). But that commit is in the kernel for which you report the error.

Turns out that the crash is not in skb_segment_list, but later in __udpv4_gso_segment_list_csum. Which unconditionally dereferences udp_hdr(seg).

Quoted 7 lines

The above fix also mentions skb pull as the culprit, but does not include a BPF program. If this can be reached in other ways, then we do need a stronger test in skb_segment_list, as you propose.

I don't want to narrowly check whether udp_hdr is safe. Essentially, an SKB_GSO_FRAGLIST skb layout cannot be trusted at all if even one byte would get pulled.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#16 lore raw
Quoted 231 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 7 lines · Maciej Żenczykowski
Quoted 5 lines · Willem de Bruijn
Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Maciej Żenczykowski

External email : Please do not click links or open

attachments until

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

you have verified the sender or the content.

Quoted 4 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open

attachments

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

until

Quoted 3 lines
Quoted 3 lines

you have verified the sender or the content.

Quoted 1 line · Lena Wang (王娜)

On Tue, 2024-04-16 at 19:14 -0400, Willem de Bruijn

Quoted 6 lines · wrote:
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

External email : Please do not click links or open

attachments

Quoted 3 lines
Quoted 3 lines

until

Quoted 2 lines
Quoted 2 lines

you have verified the sender or the content.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

Personally, I think bpf_skb_pull_data()

should have

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

automatically

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

(ie. in kernel code) reduced how much it

pulls so

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

that it

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

would pull

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 1 line

headers only,

That would be a helper that parses headers to

discover

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

header

Quoted 3 lines
Quoted 3 lines

length.

Quoted 2 lines
Quoted 2 lines

Does it actually need to? Presumably the bpf

pull

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

function

Quoted 3 lines
Quoted 3 lines

could

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

notice that it is
a packet flagged as being of type X (UDP GSO

FRAGLIST)

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

and

Quoted 3 lines
Quoted 3 lines

reduce

Quoted 2 lines
Quoted 2 lines

the pull

Quoted 1 line
Quoted 1 line

accordingly so that it doesn't pull anything from

the

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

non-

Quoted 4 lines
Quoted 4 lines

linear

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

fraglist portion???

I know only the generic overview of what udp gso

is, not

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

any

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

details, so I am

Quoted 1 line
Quoted 1 line

assuming here that there's some sort of guarantee

to how

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

these

Quoted 2 lines
Quoted 2 lines

packets

Quoted 1 line
Quoted 1 line

are structured... But I imagine there must be or

we

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

wouldn't

Quoted 5 lines
Quoted 5 lines

be

Quoted 4 lines
Quoted 4 lines

hitting these

Quoted 3 lines
Quoted 1 line

issues deeper in the stack?

Perhaps for a packet of this type we're already

guaranteed

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

the

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

headers

Quoted 5 lines

are in the linear portion,
and the pull should simply be ignored?

Quoted 2 lines
Quoted 2 lines

Parsing is better left to the BPF program.

I do prefer adding sanity checks to the BPF helpers,

over

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

having

Quoted 2 lines
Quoted 2 lines

to

Quoted 1 line
Quoted 1 line

add then in the net hot path only to protect against

dangerous

Quoted 4 lines
Quoted 4 lines

BPF

Quoted 3 lines
Quoted 2 lines

programs.

Is it OK to ignore or decrease pull length for udp gro

fraglist

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

packet?

Quoted 1 line

It could save the normal packet and sent to user

correctly.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct

sk_buff *skb,

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

Review

Quoted 4 lines

unsigned int write_len)
{

(quoted diff) +2 -0
+if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+(SKB_GSO_UDP  |SKB_GSO_UDP_L4)) {

The issue is not with SKB_GSO_UDP_L4, but with

SKB_GSO_FRAGLIST.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Current in kernel just UDP uses SKB_GSO_FRAGLIST to do GRO.

In

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

udp_offload.c udp4_gro_complete gso_type adds

"SKB_GSO_FRAGLIST|

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

SKB_GSO_UDP_L4". Here checking these two flags is to limit

the

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

packet

Quoted 3 lines

as "UDP + need GSO + fraglist".

We could remove SKB_GSO_UDP_L4 check for more packet that

may

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

addrive

Quoted 5 lines

skb_segment_list.

Quoted 3 lines
Quoted 1 line

+return 0;

Failing for any pull is a bit excessive. And would kill a

sane

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

workaround of pulling only as many bytes as needed.

Review

Quoted 2 lines
(quoted diff) +2 -0
+     or if (write_len > skb_headlen(skb))
+write_len = skb_headlen(skb);

Truncating requests would be a surprising change of

behavior

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

for this function.

Failing for a pull > skb_headlen is arguably reasonable,

as

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

the alternative is that we let it go through but have to

drop

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 3 lines

the now malformed packets on segmentation.

Is it OK as below?

In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff

*skb,

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

Review

Quoted 4 lines

unsigned int write_len)
{

(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >

skb_headlen(skb))) {

Quoted 1 line

+ return 0;

please limit write_len to skb_headlen() instead of just

returning 0

Quoted 10 lines
Quoted 10 lines

Review

Quoted 10 lines
Quoted 1 line

Hi Maze & Willem,
Maze's advice is:
In common/net/core/filter.c
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{

(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >

skb_headlen(skb))) {

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
(quoted diff) +2 -0
+               write_len = skb_headlen(skb);
+       }
        return skb_ensure_writable(skb, write_len);

}

Willem's advice is to "Failing for a pull > skb_headlen is

arguably

Quoted 3 lines
Quoted 3 lines

Review

Quoted 3 lines

reasonable...". It prefers to return 0 :

(quoted diff) +2 -0
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+               SKB_GSO_FRAGLIST) && (write_len >

skb_headlen(skb))) {

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +2 -0
+               return 0;
+       }

It seems a bit conflict. However I am not sure if my

understanding is

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

right and hope to get your further guide.

I did not mean to return 0. But to fail a request that would pull

an

Quoted 4 lines
Quoted 1 line

unsafe amount. The caller must get a clear error signal.

That's hostile on userspace.
Currently the caller doesn't even check the error return...

It can, and probably should.

bpf_skb_pull data returns the error code from bpf_try_make_writable:

   return bpf_try_make_writable(skb, len ? : skb_headlen(skb));
Quoted 1 line

Why would we? We already have to reload all pointers, and have to

do

Quoted 7 lines

and will thus redo checking on those.

What do you expect the caller to do? Subtract -1 and try again? That's hard to do from BPF as it involves looping... and is slow.

We already try to not pull too much:

Quoted 7 lines

void try_make_writable(struct __sk_buff* skb, int len) {
if (len > skb->len) len = skb->len;
if (skb->data_end - skb->data < len) bpf_skb_pull_data(skb, len);
}

Is there at least something like skb->len that has the actually pullable length in it?

The above snippet shows that it passes skb_headlen if the caller passes 0.

But your BPF program does not even need the data writable, so then it is of little help of course.

Quoted 1 line

Or are these skb's structured in such a way that there is never a

need

Quoted 2 lines

to pull anything,
because the headers are already always in the linear portion?

That is indeed the case.

Quoted 9 lines

So as far as I can see:

A BPF program that just wants to pull the network and transport headers can diligently pull exactly what is needed. And will not even observe any data pulled into linear in practice. This is still advisable rather than trusting that the headers are linear. It may also be required by the validator? Don't know. But check the return value.

Hi Willem,
As the discussion, is it OK for the patch below?

net/core/filter.c +0 -0
index 3a6110ea4009..abc6029c8eef 100644
@@ -1655,6 +1655,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,

bpf_sp);
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{
+ if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+ SKB_GSO_FRAGLIST) && (write_len >
skb_headlen(skb))) {
+ return -ENOMEM;
+ }
+

        return skb_ensure_writable(skb, write_len);

}

net/core/skbuff.c +0 -0
index 73b1e0e53534..2e90534c1a1e 100644
@@ -4036,9 +4036,11 @@ struct sk_buff *skb_segment_list(struct sk_buff

*skb,

        unsigned int tnl_hlen = skb_tnl_header_len(skb);
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        struct sk_buff *tail = NULL;
        struct sk_buff *nskb, *tmp;
        int len_diff, err;

+ bool err_len = false;

        skb_push(skb, -skb_network_offset(skb) + offset);

@@ -4047,6 +4049,14 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb,

        if (err)
                goto err_linearize;

+ if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb)) {
+ if (!list_skb) {
+ goto err_linearize;
+ } else {
+ err_len = true;
+ }
+ }
+

        skb_shinfo(skb)->frag_list = NULL;

        while (list_skb) {
@@ -4109,6 +4119,9 @@ struct sk_buff *skb_segment_list(struct sk_buff
*skb,
            __skb_linearize(skb))
                goto err_linearize;

+ if (err_len)
+ goto err_linearize;
+

        skb_get(skb);

        return skb;
Quoted 26 lines
Quoted 2 lines
Quoted 2 lines

Back to the original report: the issue should already have been

fixed

Quoted 1 line
Quoted 1 line

by commit 876e8ca83667 ("net: fix NULL pointer in

skb_segment_list").

Quoted 8 lines
Quoted 8 lines

But that commit is in the kernel for which you report the error.

Turns out that the crash is not in skb_segment_list, but later in __udpv4_gso_segment_list_csum. Which unconditionally dereferences udp_hdr(seg).

The above fix also mentions skb pull as the culprit, but does not include a BPF program. If this can be reached in other ways, then

we

Quoted 3 lines
Quoted 3 lines

do need a stronger test in skb_segment_list, as you propose.

I don't want to narrowly check whether udp_hdr is safe.

Essentially,

Quoted 1 line
Quoted 1 line

an SKB_GSO_FRAGLIST skb layout cannot be trusted at all if even

one

Quoted 4 lines
Quoted 1 line

byte would get pulled.

--
Maciej Żenczykowski, Kernel Networking Developer @ Google

#17 lore raw
Quoted 2 lines

Hi Willem,
As the discussion, is it OK for the patch below?

Thanks for iterating on this.

I would like the opinion also of the fraglist and UDP GRO experts.

Yes, I think both

- protecting skb_segment_list against clearly illegal fraglist packets, and
- blocking BPF from constructing such packets

are worthwhile stable fixes. I believe they should be two separate patches. Both probably with the same Fixes tag: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining").

Quoted 5 lines
net/core/filter.c +0 -0
index 3a6110ea4009..abc6029c8eef 100644

net/core/filter.c static DEFINE_PER_CPU(struct bpf_scratchpad, · line 1664

Quoted 10 lines
net/core/filter.c +5 -0
@@ -1655,6 +1655,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
                                          unsigned int write_len)
 {
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+                       SKB_GSO_FRAGLIST) && (write_len >
 skb_headlen(skb))) {
+               return -ENOMEM;
+       }
+

Indentation looks off, but I agree with the logic.

    if (skb_is_gso(skb) &&
        (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
         (write_len > skb_headlen(skb)))
Quoted 21 lines
net/core/filter.c +0 -0
        return skb_ensure_writable(skb, write_len);

}

net/core/skbuff.c +2 -0
index 73b1e0e53534..2e90534c1a1e 100644
@@ -4036,9 +4036,11 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
        unsigned int tnl_hlen = skb_tnl_header_len(skb);
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        struct sk_buff *tail = NULL;
        struct sk_buff *nskb, *tmp;
        int len_diff, err;
+       bool err_len = false;
 
        skb_push(skb, -skb_network_offset(skb) + offset);

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff · line 4055

Quoted 7 lines
net/core/skbuff.c +3 -0
@@ -4047,6 +4049,14 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
        if (err)
                goto err_linearize;
 
+       if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb)) {
+               if (!list_skb) {
+                       goto err_linearize;

The label no longer truly covers the meaning.

But that is already true since the above (second) jump was added in commit c329b261afe7 ("net: prevent skb corruption on frag list segmentation").

Neither needs the kfree_skb_list, as skb->next is not assigned to until the loop. Can just return ERR_PTR(-EFAULT)?

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff · line 4060

Quoted 5 lines
net/core/skbuff.c +5 -0
+               } else {
+                       err_len = true;
+               }
+       }
+

Why the branch? Might as well always fail immediately?

Quoted 36 lines
net/core/skbuff.c +0 -0
        skb_shinfo(skb)->frag_list = NULL;
 
        while (list_skb) {
net/core/skbuff.c +3 -0
@@ -4109,6 +4119,9 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
            __skb_linearize(skb))
                goto err_linearize;
 
+       if (err_len)
+               goto err_linearize;
+
        skb_get(skb);
 
        return skb;
Quoted 21 lines
Quoted 2 lines
Quoted 2 lines

Back to the original report: the issue should already have been

fixed

Quoted 1 line
Quoted 1 line

by commit 876e8ca83667 ("net: fix NULL pointer in

skb_segment_list").

Quoted 8 lines
Quoted 8 lines

But that commit is in the kernel for which you report the error.

Turns out that the crash is not in skb_segment_list, but later in __udpv4_gso_segment_list_csum. Which unconditionally dereferences udp_hdr(seg).

The above fix also mentions skb pull as the culprit, but does not include a BPF program. If this can be reached in other ways, then

we

Quoted 3 lines
Quoted 3 lines

do need a stronger test in skb_segment_list, as you propose.

I don't want to narrowly check whether udp_hdr is safe.

Essentially,

Quoted 1 line
Quoted 1 line

an SKB_GSO_FRAGLIST skb layout cannot be trusted at all if even

one

Quoted 1 line
Quoted 1 line

byte would get pulled.

#18 lore raw
Quoted 82 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 2 lines

Hi Willem,
As the discussion, is it OK for the patch below?

Thanks for iterating on this.

I would like the opinion also of the fraglist and UDP GRO experts.

Yes, I think both

- protecting skb_segment_list against clearly illegal fraglist packets, and
- blocking BPF from constructing such packets

are worthwhile stable fixes. I believe they should be two separate patches. Both probably with the same Fixes tag: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining").

Quoted 5 lines
net/core/filter.c +0 -0
index 3a6110ea4009..abc6029c8eef 100644

net/core/filter.c static DEFINE_PER_CPU(struct bpf_scratchpad, · line 1664

Quoted 10 lines
net/core/filter.c +5 -0
@@ -1655,6 +1655,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
                                          unsigned int write_len)
 {
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+                       SKB_GSO_FRAGLIST) && (write_len >
 skb_headlen(skb))) {
+               return -ENOMEM;
+       }
+

Indentation looks off, but I agree with the logic.

    if (skb_is_gso(skb) &&
        (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
         (write_len > skb_headlen(skb)))

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 8 lines
net/core/filter.c +0 -0
        return skb_ensure_writable(skb, write_len);

}

net/core/skbuff.c +0 -0
index 73b1e0e53534..2e90534c1a1e 100644
@@ -4036,9 +4036,11 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
net/core/skbuff.c +2 -0
 *skb,
        unsigned int tnl_hlen = skb_tnl_header_len(skb);
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        struct sk_buff *tail = NULL;
        struct sk_buff *nskb, *tmp;
        int len_diff, err;
+       bool err_len = false;
 
        skb_push(skb, -skb_network_offset(skb) + offset);
@@ -4047,6 +4049,14 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4055

Quoted 7 lines
net/core/skbuff.c +3 -0
 *skb,
        if (err)
                goto err_linearize;
 
+       if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb)) {
+               if (!list_skb) {
+                       goto err_linearize;

The label no longer truly covers the meaning.

But that is already true since the above (second) jump was added in commit c329b261afe7 ("net: prevent skb corruption on frag list segmentation").

Neither needs the kfree_skb_list, as skb->next is not assigned to until the loop. Can just return ERR_PTR(-EFAULT)?

Quoted 8 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4060

Quoted 5 lines
net/core/skbuff.c +5 -0
+               } else {
+                       err_len = true;
+               }
+       }
+

Why the branch? Might as well always fail immediately?

Hi Willem,
Thanks for your guidance.
You are right. There is no need for another branch as fraglist could be freeed in kfree_skb.
Could I git send mail wo patches as below?

From 933237400c0e2fa997470b70ff0e407996fa239c Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pull GROed skb's fraglist

A GROed skb with fraglist can't be pulled data
from its fraglist as it may result a invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pull
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,

bpf_sp);
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{
+ if (skb_is_gso(skb) &&
+ (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+ write_len > skb_headlen(skb)) {
+ return -ENOMEM;
+ }
return skb_ensure_writable(skb, write_len);
}

Signature · 24 lines
2.18.0


From 2d0729b20cf810ba1b31e046952c1cd78f295ca3 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop GROed skb pulled from fraglist

A GROed skb with fraglist maybe pulled by BPF
or other ways. It can't be trusted at all even
if one byte is pulled and should be dropped
on segmentation.

If the gso_size does not match skb_headlen(),
it means to be pulled part of or the entire
fraglsit. It has been messed with and we return
error to free this skb.

Fixes: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
Signed-off-by: Shiming Cheng <[email protected]>
Signed-off-by: Lena Wang <[email protected]>
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..750fbb51b99f 100644
@@ -4493,6 +4493,7 @@ struct sk_buff *skb_segment_list(struct sk_buff

*skb,
unsigned int tnl_hlen = skb_tnl_header_len(skb); unsigned int delta_truesize = 0;
unsigned int delta_len = 0;
+ unsigned int mss = skb_shinfo(skb)->gso_size;
struct sk_buff *tail = NULL;
struct sk_buff *nskb, *tmp;
int len_diff, err;
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb,
if (err)
goto err_linearize;

+ if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+ return ERR_PTR(-EFAULT);
+
skb_shinfo(skb)->frag_list = NULL;

while (list_skb) {

Signature · 1 line
2.18.0
Quoted 46 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 4 lines
net/core/skbuff.c +0 -0
        skb_shinfo(skb)->frag_list = NULL;
 
        while (list_skb) {
net/core/skbuff.c +0 -0
@@ -4109,6 +4119,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 9 lines
net/core/skbuff.c +3 -0
 *skb,
            __skb_linearize(skb))
                goto err_linearize;
 
+       if (err_len)
+               goto err_linearize;
+
        skb_get(skb);
 
Quoted 4 lines
        return skb;
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Back to the original report: the issue should already have

been

Quoted 4 lines
Quoted 4 lines

fixed

Quoted 1 line
Quoted 1 line

by commit 876e8ca83667 ("net: fix NULL pointer in

skb_segment_list").

Quoted 1 line
Quoted 1 line

But that commit is in the kernel for which you report the

error.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Turns out that the crash is not in skb_segment_list, but

later in

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

__udpv4_gso_segment_list_csum. Which unconditionally

dereferences

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

udp_hdr(seg).

The above fix also mentions skb pull as the culprit, but does

not

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

include a BPF program. If this can be reached in other ways,

then

Quoted 6 lines
Quoted 6 lines

we

Quoted 3 lines
Quoted 3 lines

do need a stronger test in skb_segment_list, as you propose.

I don't want to narrowly check whether udp_hdr is safe.

Essentially,

Quoted 1 line
Quoted 1 line

an SKB_GSO_FRAGLIST skb layout cannot be trusted at all if

even

Quoted 2 lines
Quoted 2 lines

one

Quoted 1 line
Quoted 1 line

byte would get pulled.

#19 lore raw
Quoted 97 lines · Lena Wang (王娜)
Quoted 82 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 2 lines

Hi Willem,
As the discussion, is it OK for the patch below?

Thanks for iterating on this.

I would like the opinion also of the fraglist and UDP GRO experts.

Yes, I think both

- protecting skb_segment_list against clearly illegal fraglist packets, and
- blocking BPF from constructing such packets

are worthwhile stable fixes. I believe they should be two separate patches. Both probably with the same Fixes tag: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining").

Quoted 5 lines
net/core/filter.c +0 -0
index 3a6110ea4009..abc6029c8eef 100644

net/core/filter.c static DEFINE_PER_CPU(struct bpf_scratchpad, · line 1664

Quoted 10 lines
net/core/filter.c +5 -0
@@ -1655,6 +1655,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
                                          unsigned int write_len)
 {
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+                       SKB_GSO_FRAGLIST) && (write_len >
 skb_headlen(skb))) {
+               return -ENOMEM;
+       }
+

Indentation looks off, but I agree with the logic.

    if (skb_is_gso(skb) &&
        (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
         (write_len > skb_headlen(skb)))

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 8 lines
net/core/filter.c +0 -0
        return skb_ensure_writable(skb, write_len);

}

net/core/skbuff.c +0 -0
index 73b1e0e53534..2e90534c1a1e 100644
@@ -4036,9 +4036,11 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
net/core/skbuff.c +2 -0
 *skb,
        unsigned int tnl_hlen = skb_tnl_header_len(skb);
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        struct sk_buff *tail = NULL;
        struct sk_buff *nskb, *tmp;
        int len_diff, err;
+       bool err_len = false;
 
        skb_push(skb, -skb_network_offset(skb) + offset);
@@ -4047,6 +4049,14 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4055

Quoted 7 lines
net/core/skbuff.c +3 -0
 *skb,
        if (err)
                goto err_linearize;
 
+       if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb)) {
+               if (!list_skb) {
+                       goto err_linearize;

The label no longer truly covers the meaning.

But that is already true since the above (second) jump was added in commit c329b261afe7 ("net: prevent skb corruption on frag list segmentation").

Neither needs the kfree_skb_list, as skb->next is not assigned to until the loop. Can just return ERR_PTR(-EFAULT)?

Quoted 8 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4060

Quoted 5 lines
net/core/skbuff.c +5 -0
+               } else {
+                       err_len = true;
+               }
+       }
+

Why the branch? Might as well always fail immediately?

Hi Willem,
Thanks for your guidance.
You are right. There is no need for another branch as fraglist could be freeed in kfree_skb.
Could I git send mail wo patches as below?

Quoted 6 lines

From 933237400c0e2fa997470b70ff0e407996fa239c Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pull GROed skb's fraglist

A GROed skb with fraglist can't be pulled data

Please use the specific label: SKB_GSO_FRAGLIST skb

Quoted 44 lines

from its fraglist as it may result a invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pull
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
 					  unsigned int write_len)
 {
+	if (skb_is_gso(skb) &&
+	    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+	     write_len > skb_headlen(skb)) {
+		return -ENOMEM;
+	}
 	return skb_ensure_writable(skb, write_len);
 }
Signature · 13 lines
-- 
2.18.0


From 2d0729b20cf810ba1b31e046952c1cd78f295ca3 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop GROed skb pulled from fraglist

A GROed skb with fraglist maybe pulled by BPF
or other ways. It can't be trusted at all even
if one byte is pulled and should be dropped
on segmentation.

This paraphrases my comment. It is better to spell it out:

An SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is expected to have all segments except the last to be gso_size long. If this does not hold, the skb has been modified and the fraglist gso integrity is lost. Drop the packet, as it cannot be segmented correctly by skb_segment_list.

The skb could be salvaged, though, right? By linearizing, dropping the SKB_GSO_FRAGLIST bit and entering the normal skb_segment path rather than the skb_segment_list path.

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

Quoted 4 lines

If the gso_size does not match skb_headlen(),
it means to be pulled part of or the entire
fraglsit. It has been messed with and we return

fraglist

Quoted 14 lines

error to free this skb.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..750fbb51b99f 100644

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff · line 4497

Quoted 5 lines
net/core/skbuff.c +1 -0
@@ -4493,6 +4493,7 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
 	unsigned int delta_truesize = 0;
 	unsigned int delta_len = 0;
+	unsigned int mss = skb_shinfo(skb)->gso_size;

Reverse christmas tree

net/core/skbuff.c struct sk_buff *skb_segment_list(struct sk_buff · line 4511

Quoted 11 lines
net/core/skbuff.c +0 -0
 	struct sk_buff *tail = NULL;
 	struct sk_buff *nskb, *tmp;
net/core/skbuff.c +3 -0
 	int len_diff, err;
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
 	if (err)
 		goto err_linearize;
 
+	if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+		return ERR_PTR(-EFAULT);
+

Do this precondition integrity check before the skb_unclone path?

Quoted 5 lines
net/core/skbuff.c +0 -0
 	skb_shinfo(skb)->frag_list = NULL;
 

while (list_skb) {

Signature · 1 line
2.18.0
#20 lore raw
Quoted 219 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 7 lines
Quoted 7 lines

you have verified the sender or the content.

Quoted 2 lines

Hi Willem,
As the discussion, is it OK for the patch below?

Thanks for iterating on this.

I would like the opinion also of the fraglist and UDP GRO

experts.

Quoted 8 lines
Quoted 8 lines

Yes, I think both

- protecting skb_segment_list against clearly illegal fraglist packets, and
- blocking BPF from constructing such packets

are worthwhile stable fixes. I believe they should be two

separate

Quoted 8 lines
Quoted 8 lines

patches. Both probably with the same Fixes tag: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining").

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 5 lines
net/core/filter.c +0 -0
index 3a6110ea4009..abc6029c8eef 100644
@@ -1655,6 +1655,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 3 lines
Quoted 3 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1657

Quoted 3 lines
net/core/filter.c +0 -0
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
                                          unsigned int

write_len)

Quoted 41 lines
Quoted 41 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1664

Quoted 7 lines
net/core/filter.c +5 -0
 {
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+                       SKB_GSO_FRAGLIST) && (write_len >
 skb_headlen(skb))) {
+               return -ENOMEM;
+       }
+

Indentation looks off, but I agree with the logic.

    if (skb_is_gso(skb) &&
        (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
         (write_len > skb_headlen(skb)))

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 8 lines
net/core/filter.c +0 -0
        return skb_ensure_writable(skb, write_len);

}

net/core/skbuff.c +0 -0
index 73b1e0e53534..2e90534c1a1e 100644
@@ -4036,9 +4036,11 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
net/core/skbuff.c +2 -0
 *skb,
        unsigned int tnl_hlen = skb_tnl_header_len(skb);
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        struct sk_buff *tail = NULL;
        struct sk_buff *nskb, *tmp;
        int len_diff, err;
+       bool err_len = false;
 
        skb_push(skb, -skb_network_offset(skb) + offset);
@@ -4047,6 +4049,14 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 4 lines
net/core/skbuff.c +0 -0
 *skb,
        if (err)
                goto err_linearize;
 
Quoted 7 lines
Quoted 7 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4055

Quoted 3 lines
net/core/skbuff.c +3 -0
+       if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb)) {
+               if (!list_skb) {
+                       goto err_linearize;

The label no longer truly covers the meaning.

But that is already true since the above (second) jump was added

in

Quoted 14 lines
Quoted 6 lines

commit c329b261afe7 ("net: prevent skb corruption on frag list segmentation").

Neither needs the kfree_skb_list, as skb->next is not assigned to until the loop. Can just return ERR_PTR(-EFAULT)?

Quoted 8 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4060

Quoted 5 lines
net/core/skbuff.c +5 -0
+               } else {
+                       err_len = true;
+               }
+       }
+

Why the branch? Might as well always fail immediately?

Quoted 7 lines

Hi Willem,
Thanks for your guidance.
You are right. There is no need for another branch as fraglist could be freeed in kfree_skb.
Could I git send mail wo patches as below?

From 933237400c0e2fa997470b70ff0e407996fa239c Mon Sep 17 00:00:00

2001

Quoted 5 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pull GROed skb's fraglist

A GROed skb with fraglist can't be pulled data

Please use the specific label: SKB_GSO_FRAGLIST skb

Quoted 36 lines

from its fraglist as it may result a invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pull
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
   unsigned int write_len)
 {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
 return skb_ensure_writable(skb, write_len);
 }
Signature · 5 lines
-- 
2.18.0


From 2d0729b20cf810ba1b31e046952c1cd78f295ca3 Mon Sep 17 00:00:00

2001

Quoted 8 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop GROed skb pulled from fraglist

A GROed skb with fraglist maybe pulled by BPF
or other ways. It can't be trusted at all even
if one byte is pulled and should be dropped
on segmentation.

This paraphrases my comment. It is better to spell it out:

An SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is expected to have all segments except the last to be gso_size long. If this does not hold, the skb has been modified and the fraglist gso integrity is lost. Drop
the packet, as it cannot be segmented correctly by skb_segment_list.

The skb could be salvaged, though, right? By linearizing, dropping the SKB_GSO_FRAGLIST bit and entering the normal skb_segment path rather than the skb_segment_list path.

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

Quoted 4 lines

If the gso_size does not match skb_headlen(),
it means to be pulled part of or the entire
fraglsit. It has been messed with and we return

fraglist

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 14 lines

error to free this skb.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..750fbb51b99f 100644
@@ -4493,6 +4493,7 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4497

Quoted 5 lines
net/core/skbuff.c +1 -0
 *skb,
 unsigned int tnl_hlen = skb_tnl_header_len(skb);
 unsigned int delta_truesize = 0;
 unsigned int delta_len = 0;
+unsigned int mss = skb_shinfo(skb)->gso_size;

Reverse christmas tree

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 4 lines
net/core/skbuff.c +0 -0
 struct sk_buff *tail = NULL;
 struct sk_buff *nskb, *tmp;

int len_diff, err;

net/core/skbuff.c +0 -0
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 4 lines
net/core/skbuff.c +0 -0
 *skb,
 if (err)
 goto err_linearize;
 
Quoted 5 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4511

Quoted 3 lines
net/core/skbuff.c +3 -0
+if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+return ERR_PTR(-EFAULT);
+

Do this precondition integrity check before the skb_unclone path?

After return error, the skb will enter into kfree_skb, not consume_skb. It may meet same crash problem which has been resolved by skb_unclone.

Or kfree_skb could well handle the cloned skb's release?

Other changes are updated as below:

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,

bpf_sp);
static inline int __bpf_try_make_writable(struct sk_buff *skb,
unsigned int write_len)
{
+ if (skb_is_gso(skb) &&
+ (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+ write_len > skb_headlen(skb)) {
+ return -ENOMEM;
+ }
return skb_ensure_writable(skb, write_len);
}

Signature · 36 lines
2.18.0


From 64d55392debbc90ef2e9c33441024d612075bdd7 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop pulled SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is
expected to have all segments except the last
to be gso_size long. If this does not hold, the
skb has been modified and the fraglist gso integrity
is lost. Drop the packet, as it cannot be segmented
correctly by skb_segment_list.

The skb could be salvaged, though, right?
By linearizing, dropping the SKB_GSO_FRAGLIST bit
and entering the normal skb_segment path rather than
the skb_segment_list path.

That choice is currently made in the protocol caller,
__udp_gso_segment. It's not trivial to add such a
backup path here. So let's add this backstop against
kernel crashes.

If the gso_size does not match skb_headlen(),
it means part of or the entire fraglist has been pulled.
It has been messed with and we should return error to
free this skb.

Fixes: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
Signed-off-by: Shiming Cheng <[email protected]>
Signed-off-by: Lena Wang <[email protected]>
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..4777f5fea6c3 100644
@@ -4491,6 +4491,7 @@ struct sk_buff *skb_segment_list(struct sk_buff

*skb,
{
struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; unsigned int tnl_hlen = skb_tnl_header_len(skb);
+ unsigned int mss = skb_shinfo(skb)->gso_size;
unsigned int delta_truesize = 0;
unsigned int delta_len = 0;
struct sk_buff *tail = NULL;
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb,
if (err)
goto err_linearize;

+ if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+ return ERR_PTR(-EFAULT);
+
skb_shinfo(skb)->frag_list = NULL;

while (list_skb) {

Signature · 1 line
2.18.0
#21 lore raw
Quoted 15 lines
Quoted 9 lines

Review struct sk_buff *skb_segment_list(struct

Quoted 4 lines

struct sk_buff *tail = NULL;
struct sk_buff *nskb, *tmp;
int len_diff, err;

(quoted diff) +0 -0
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 4 lines
(quoted diff) +0 -0
 *skb,
 if (err)
 goto err_linearize;
 
Quoted 5 lines

Review struct sk_buff *skb_segment_list(struct · line 4511

Quoted 3 lines
(quoted diff) +3 -0
+if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+return ERR_PTR(-EFAULT);
+

Do this precondition integrity check before the skb_unclone path?

Quoted 4 lines

After return error, the skb will enter into kfree_skb, not consume_skb. It may meet same crash problem which has been resolved by skb_unclone.

Or kfree_skb could well handle the cloned skb's release?

Since this is an error path it should reach kfree_skb rather than consume_skb.

Quoted 60 lines

Other changes are updated as below:

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
 					  unsigned int write_len)
 {
+	if (skb_is_gso(skb) &&
+	    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+	     write_len > skb_headlen(skb)) {
+		return -ENOMEM;
+	}
 	return skb_ensure_writable(skb, write_len);
 }
Signature · 20 lines
-- 
2.18.0


From 64d55392debbc90ef2e9c33441024d612075bdd7 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop pulled SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is
expected to have all segments except the last
to be gso_size long. If this does not hold, the
skb has been modified and the fraglist gso integrity
is lost. Drop the packet, as it cannot be segmented
correctly by skb_segment_list.

The skb could be salvaged, though, right?
By linearizing, dropping the SKB_GSO_FRAGLIST bit
and entering the normal skb_segment path rather than
the skb_segment_list path.

Drop the "though, right?"

Quoted 10 lines

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

If the gso_size does not match skb_headlen(), it means part of or the entire fraglist has been pulled. It has been messed with and we should return error to free this skb.

This paragraph is now duplicative. Drop.

Quoted 34 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +1 -0
index b99127712e67..4777f5fea6c3 100644
@@ -4491,6 +4491,7 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
 {
 	struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
 	unsigned int tnl_hlen = skb_tnl_header_len(skb);
+	unsigned int mss = skb_shinfo(skb)->gso_size;
 	unsigned int delta_truesize = 0;
 	unsigned int delta_len = 0;
net/core/skbuff.c +3 -0
 	struct sk_buff *tail = NULL;
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
 	if (err)
 		goto err_linearize;
 
+	if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+		return ERR_PTR(-EFAULT);
+
 	skb_shinfo(skb)->frag_list = NULL;
 

while (list_skb) {

Signature · 1 line
2.18.0
#22 lore raw
Quoted 20 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 9 lines
Quoted 9 lines

Review struct sk_buff *skb_segment_list(struct

Quoted 4 lines

struct sk_buff *tail = NULL;
struct sk_buff *nskb, *tmp;
int len_diff, err;

(quoted diff) +0 -0
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 4 lines
(quoted diff) +0 -0
 *skb,
 if (err)
 goto err_linearize;
 
Quoted 7 lines
Quoted 5 lines

Review struct sk_buff *skb_segment_list(struct · line 4511

Quoted 3 lines
(quoted diff) +3 -0
+if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+return ERR_PTR(-EFAULT);
+

Do this precondition integrity check before the skb_unclone path?

After return error, the skb will enter into kfree_skb, not

Quoted 9 lines

consume_skb.

Quoted 1 line

It may meet same crash problem which has been resolved by

skb_unclone.

Quoted 2 lines

Or kfree_skb could well handle the cloned skb's release?

Since this is an error path it should reach kfree_skb rather than consume_skb.

So we could keep the check after the skb_unclone path, right?

Quoted 70 lines
Quoted 4 lines

Other changes are updated as below:

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17 00:00:00

2001

Quoted 41 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
   unsigned int write_len)
 {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
 return skb_ensure_writable(skb, write_len);
 }
Signature · 5 lines
-- 
2.18.0


From 64d55392debbc90ef2e9c33441024d612075bdd7 Mon Sep 17 00:00:00

2001

Quoted 11 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop pulled SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is expected to have all segments except the last to be gso_size long. If this does not hold, the skb has been modified and the fraglist gso integrity is lost. Drop the packet, as it cannot be segmented correctly by skb_segment_list.

Quoted 4 lines

The skb could be salvaged, though, right?
By linearizing, dropping the SKB_GSO_FRAGLIST bit and entering the normal skb_segment path rather than the skb_segment_list path.

Drop the "though, right?"

Quoted 6 lines

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

Quoted 6 lines
Quoted 4 lines

If the gso_size does not match skb_headlen(), it means part of or the entire fraglist has been pulled. It has been messed with and we should return error to free this skb.

This paragraph is now duplicative. Drop.

OK, updated as below:

From 59d561adc13d52e3c225c6b8276f6a53324f7d56 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop pulled SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is expected to have all segments except the last to be gso_size long. If this does not hold, the skb has been modified and the fraglist gso integrity is lost. Drop the packet, as it cannot be segmented correctly by skb_segment_list.

The skb could be salvaged. By linearizing, dropping the SKB_GSO_FRAGLIST bit and entering the normal skb_segment path rather than the skb_segment_list path.

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..4777f5fea6c3 100644
@@ -4491,6 +4491,7 @@ struct sk_buff *skb_segment_list(struct sk_buff

*skb,
{
struct sk_buff *list_skb = skb_shinfo(skb)->frag_list; unsigned int tnl_hlen = skb_tnl_header_len(skb);
+ unsigned int mss = skb_shinfo(skb)->gso_size;
unsigned int delta_truesize = 0;
unsigned int delta_len = 0;
struct sk_buff *tail = NULL;
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct sk_buff *skb,
if (err)
goto err_linearize;

+ if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+ return ERR_PTR(-EFAULT);
+
skb_shinfo(skb)->frag_list = NULL;

while (list_skb) {

Signature · 1 line
2.18.0
Quoted 38 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..4777f5fea6c3 100644
@@ -4491,6 +4491,7 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 9 lines
net/core/skbuff.c +1 -0
 *skb,
 {
 struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
 unsigned int tnl_hlen = skb_tnl_header_len(skb);
+unsigned int mss = skb_shinfo(skb)->gso_size;
 unsigned int delta_truesize = 0;
 unsigned int delta_len = 0;

struct sk_buff *tail = NULL;

net/core/skbuff.c +0 -0
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 12 lines
net/core/skbuff.c +3 -0
 *skb,
 if (err)
 goto err_linearize;
 
+if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+return ERR_PTR(-EFAULT);
+
 skb_shinfo(skb)->frag_list = NULL;
 

while (list_skb) {

Signature · 1 line
2.18.0
maintainer
#23 lore raw

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 21 lines
Quoted 21 lines
Quoted 1 line

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17 00:00:00

2001

Quoted 19 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
  net/core/filter.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
  static inline int __bpf_try_make_writable(struct sk_buff *skb,
Quoted 8 lines

net/core/filter.c static DEFINE_PER_CPU(struct bpf_scratchpad, · line 1671

Quoted 8 lines
net/core/filter.c +5 -0
    unsigned int write_len)
  {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
  return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of skb_ensure_writable() internals, presumably that would be inside pskb_may_pull_reason(), or only if we ever see more code instances similar to this?

Thanks,
Daniel

#24 lore raw
Quoted 34 lines · Daniel Borkmann

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 32 lines
Quoted 32 lines
Quoted 1 line

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17 00:00:00

2001

Quoted 30 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
  net/core/filter.c | 5 +++++
  1 file changed, 5 insertions(+)
net/core/filter.c +2 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
  static inline int __bpf_try_make_writable(struct sk_buff *skb,
    unsigned int write_len)
  {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

net/core/filter.c static DEFINE_PER_CPU(struct bpf_scratchpad, · line 1671

Quoted 4 lines
net/core/filter.c +3 -0
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
  return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of skb_ensure_writable() internals, presumably that would be inside pskb_may_pull_reason(), or only if we ever see more code instances similar to this?

Good point. Most callers of skb_ensure_writable correctly pull only headers, so wouldn't cause this problem. But it also adds coverage to things like tc pedit.

#25 lore raw
Quoted 49 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Daniel Borkmann

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 1 line
Quoted 1 line
Quoted 1 line

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17

00:00:00

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2001

Quoted 3 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST

skb

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
  net/core/filter.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 5 lines
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1663

Quoted 2 lines
net/core/filter.c +0 -0
 bpf_sp);
  static inline int __bpf_try_make_writable(struct sk_buff

*skb,

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1671

Quoted 8 lines
net/core/filter.c +5 -0
    unsigned int write_len)
  {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
  return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of

Quoted 8 lines

skb_ensure_writable()

Quoted 1 line

internals, presumably that would be inside pskb_may_pull_reason(),

or only if we ever

Quoted 1 line

see more code instances similar to this?

Good point. Most callers of skb_ensure_writable correctly pull only headers, so wouldn't cause this problem. But it also adds coverage to things like tc pedit.

Updated:

From 3be30b8cf6e629f2615ef4eafe3b2a1c0d68c530 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Sun, 28 Apr 2024 15:03:12 +0800
Subject: [PATCH net] net: prevent pulling SKB_GSO_FRAGLIST skb

BPF or TC callers may pull in a length longer than skb_headlen() for a SKB_GSO_FRAGLIST skb. The data in fraglist will be pulled into the linear space. However it destroys the skb's structure and may result in an invalid segmentation or kernel exception.

So we should add protection to stop the operation and return error to remind callers.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 include/linux/skbuff.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/skbuff.h +0 -0
index 9d24aec064e8..3eef65b3db24 100644
@@ -2740,6 +2740,12 @@ pskb_may_pull_reason(struct sk_buff *skb,

unsigned int len)
if (unlikely(len > skb->len))
return SKB_DROP_REASON_PKT_TOO_SMALL;

+ if (skb_is_gso(skb) &&
+ (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+ write_len > skb_headlen(skb)) {
+ return SKB_DROP_REASON_NOMEM;
+ }
+
if (unlikely(!__pskb_pull_tail(skb, len - skb_headlen(skb))))
return SKB_DROP_REASON_NOMEM;

Signature · 1 line
2.18.0
#26 lore raw
Quoted 86 lines · Lena Wang (王娜)
Quoted 49 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Daniel Borkmann

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 1 line
Quoted 1 line
Quoted 1 line

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17

00:00:00

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2001

Quoted 3 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST

skb

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
  net/core/filter.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 5 lines
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1663

Quoted 2 lines
net/core/filter.c +0 -0
 bpf_sp);
  static inline int __bpf_try_make_writable(struct sk_buff

*skb,

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1671

Quoted 8 lines
net/core/filter.c +5 -0
    unsigned int write_len)
  {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
  return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of

Quoted 8 lines

skb_ensure_writable()

Quoted 1 line

internals, presumably that would be inside pskb_may_pull_reason(),

or only if we ever

Quoted 1 line

see more code instances similar to this?

Good point. Most callers of skb_ensure_writable correctly pull only headers, so wouldn't cause this problem. But it also adds coverage to things like tc pedit.

Updated:

From 3be30b8cf6e629f2615ef4eafe3b2a1c0d68c530 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Sun, 28 Apr 2024 15:03:12 +0800
Subject: [PATCH net] net: prevent pulling SKB_GSO_FRAGLIST skb

BPF or TC callers may pull in a length longer than skb_headlen() for a SKB_GSO_FRAGLIST skb. The data in fraglist will be pulled into the linear space. However it destroys the skb's structure and may result in an invalid segmentation or kernel exception.

So we should add protection to stop the operation and return error to remind callers.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 include/linux/skbuff.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/skbuff.h +0 -0
index 9d24aec064e8..3eef65b3db24 100644

include/linux/skbuff.h pskb_may_pull_reason(struct sk_buff *skb, · line 2740

Quoted 1 line
include/linux/skbuff.h +0 -0
@@ -2740,6 +2740,12 @@ pskb_may_pull_reason(struct sk_buff *skb,
 unsigned int len)

pskb_may_pull is called in far too many locations. Daniel's suggestion was to amend skb_ensure_writable

Please start sending the patches as regular patches. They are close enough to review normally.

Quoted 14 lines
include/linux/skbuff.h +6 -0
 	if (unlikely(len > skb->len))
 		return SKB_DROP_REASON_PKT_TOO_SMALL;
 
+	if (skb_is_gso(skb) &&
+	    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+	     write_len > skb_headlen(skb)) {
+		return SKB_DROP_REASON_NOMEM;
+	}
+
 	if (unlikely(!__pskb_pull_tail(skb, len - skb_headlen(skb))))
 		return SKB_DROP_REASON_NOMEM;
Signature · 2 lines
-- 
2.18.0
maintainer
#27 lore raw
In reply to Lena Wang (王娜)
Quoted 86 lines · Lena Wang (王娜)
Quoted 49 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Daniel Borkmann

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 1 line
Quoted 1 line
Quoted 1 line

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17

00:00:00

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2001

Quoted 3 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST

skb

Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
   net/core/filter.c | 5 +++++
   1 file changed, 5 insertions(+)
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 5 lines
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1663

Quoted 2 lines
net/core/filter.c +0 -0
 bpf_sp);
   static inline int __bpf_try_make_writable(struct sk_buff

*skb,

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1671

Quoted 8 lines
net/core/filter.c +5 -0
     unsigned int write_len)
   {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
   return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of

Quoted 8 lines

skb_ensure_writable()

Quoted 1 line

internals, presumably that would be inside pskb_may_pull_reason(),

or only if we ever

Quoted 1 line

see more code instances similar to this?

Good point. Most callers of skb_ensure_writable correctly pull only headers, so wouldn't cause this problem. But it also adds coverage to things like tc pedit.

Updated:

From 3be30b8cf6e629f2615ef4eafe3b2a1c0d68c530 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Sun, 28 Apr 2024 15:03:12 +0800
Subject: [PATCH net] net: prevent pulling SKB_GSO_FRAGLIST skb

BPF or TC callers may pull in a length longer than skb_headlen() for a SKB_GSO_FRAGLIST skb. The data in fraglist will be pulled into the linear space. However it destroys the skb's structure and may result in an invalid segmentation or kernel exception.

So we should add protection to stop the operation and return error to remind callers.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
  include/linux/skbuff.h | 6 ++++++
  1 file changed, 6 insertions(+)
include/linux/skbuff.h +0 -0
index 9d24aec064e8..3eef65b3db24 100644

include/linux/skbuff.h pskb_may_pull_reason(struct sk_buff *skb, · line 2748

Quoted 9 lines
include/linux/skbuff.h +5 -0
@@ -2740,6 +2740,12 @@ pskb_may_pull_reason(struct sk_buff *skb,
 unsigned int len)
  	if (unlikely(len > skb->len))
  		return SKB_DROP_REASON_PKT_TOO_SMALL;
 
+	if (skb_is_gso(skb) &&
+	    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+	     write_len > skb_headlen(skb)) {
+		return SKB_DROP_REASON_NOMEM;
+	}

The 'write_len > skb_headlen(skb)' test is redundant, no ?

It is covered by the earlier test :

if (likely(len <= skb_headlen(skb)))
return SKB_NOT_DROPPED_YET;

Also, was this patch even compile tested since there is no write_len var ?

Quoted 4 lines
include/linux/skbuff.h +0 -0
  	if (unlikely(!__pskb_pull_tail(skb, len - skb_headlen(skb))))
  		return SKB_DROP_REASON_NOMEM;
#28 lore raw
Quoted 108 lines · Daniel Borkmann

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 17 lines
Quoted 17 lines

you have verified the sender or the content.

Quoted 3 lines · Daniel Borkmann

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 1 line
Quoted 1 line
Quoted 1 line

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17

00:00:00

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2001

Quoted 3 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST

skb

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid segmentation or kernel exception.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

For such structured skb we limit the BPF pulling data length smaller than skb_headlen() and return error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist

chaining.")

Quoted 25 lines
Quoted 25 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 11 lines
---
   net/core/filter.c | 5 +++++
   1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1663

Quoted 2 lines
net/core/filter.c +0 -0
 bpf_sp);
   static inline int __bpf_try_make_writable(struct sk_buff

*skb,

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1671

Quoted 8 lines
net/core/filter.c +5 -0
     unsigned int write_len)
   {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
   return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of

Quoted 2 lines
Quoted 2 lines

skb_ensure_writable()

Quoted 1 line

internals, presumably that would be inside

pskb_may_pull_reason(),

Quoted 4 lines
Quoted 4 lines

or only if we ever

Quoted 1 line

see more code instances similar to this?

Good point. Most callers of skb_ensure_writable correctly pull

only

Quoted 1 line
Quoted 1 line

headers, so wouldn't cause this problem. But it also adds coverage

to

Quoted 5 lines
Quoted 1 line

things like tc pedit.

Updated:

From 3be30b8cf6e629f2615ef4eafe3b2a1c0d68c530 Mon Sep 17 00:00:00

2001

Quoted 24 lines

From: Shiming Cheng <[email protected]>
Date: Sun, 28 Apr 2024 15:03:12 +0800
Subject: [PATCH net] net: prevent pulling SKB_GSO_FRAGLIST skb

BPF or TC callers may pull in a length longer than skb_headlen() for a SKB_GSO_FRAGLIST skb. The data in fraglist will be pulled into the linear space. However it destroys the skb's structure and may result in an invalid segmentation or kernel exception.

So we should add protection to stop the operation and return error to remind callers.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
  include/linux/skbuff.h | 6 ++++++
  1 file changed, 6 insertions(+)
include/linux/skbuff.h +0 -0
index 9d24aec064e8..3eef65b3db24 100644

include/linux/skbuff.h pskb_may_pull_reason(struct sk_buff *skb, · line 2748

Quoted 9 lines
include/linux/skbuff.h +5 -0
@@ -2740,6 +2740,12 @@ pskb_may_pull_reason(struct sk_buff *skb,
 unsigned int len)
  if (unlikely(len > skb->len))
  return SKB_DROP_REASON_PKT_TOO_SMALL;
 
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return SKB_DROP_REASON_NOMEM;
+}

The 'write_len > skb_headlen(skb)' test is redundant, no ?

Quoted 5 lines

It is covered by the earlier test :

if (likely(len <= skb_headlen(skb)))
return SKB_NOT_DROPPED_YET;

Daniel, it is not redundant. The bpf pulls a len between skb_headlen(skb) and skb->len that results in error. Here it will stop this operation. For other skbs(not SKB_GSO_FRAGLIST) it could be a normal behaviour and will continue to do next pulling.

Quoted 3 lines

Also, was this patch even compile tested since there is no write_len var ?

The check is moved into skb_ensure_writable as advice and a new mail loop is created to start upstream:

From 36307cb8706653a63a389faadbb1324cda5c0445 Mon Sep 17 00:00:00 2001 F
rom: Shiming Cheng <[email protected]>
Date: Sun, 28 Apr 2024
21:50:11 +0800
Subject: [PATCH net] net: prevent pulling
SKB_GSO_FRAGLIST skb

BPF or TC callers may pull in a length longer than skb_headlen() for a SKB_GSO_FRAGLIST skb. The data in fraglist will be pulled into the linear space. However it destroys the skb's structure and may result in an invalid segmentation or kernel exception.

So we should add protection to stop the operation and return error to remind callers.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/core/skbuff.c +6 -0
index f68f2679b086..2d35e009e814 100644
@@ -6100,6 +6100,12 @@ EXPORT_SYMBOL(skb_vlan_untag);
 
 int skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
 {
+	if (skb_is_gso(skb) &&
+	    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+	     write_len > skb_headlen(skb)) {
+		return -ENOMEM;
+	}
+
 	if (!pskb_may_pull(skb, write_len))
 		return -ENOMEM;
 
Signature · 1 line
2.18.0
Quoted 5 lines
Quoted 4 lines
include/linux/skbuff.h +0 -0
  if (unlikely(!__pskb_pull_tail(skb, len - skb_headlen(skb))))
  return SKB_DROP_REASON_NOMEM;
maintainer
#29 lore raw
Quoted 111 lines · Lena Wang (王娜)
Quoted 110 lines · Daniel Borkmann

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 17 lines
Quoted 17 lines

you have verified the sender or the content.

Quoted 3 lines · Daniel Borkmann

On 4/26/24 11:52 AM, Lena Wang (王娜) wrote:
[...]

Quoted 1 line
Quoted 1 line
Quoted 1 line
   From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17

00:00:00

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

2001

Quoted 3 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST

skb

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid segmentation or kernel exception.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

For such structured skb we limit the BPF pulling data length smaller than skb_headlen() and return error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist

chaining.")

Quoted 25 lines
Quoted 25 lines
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 11 lines
---
    net/core/filter.c | 5 +++++
    1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1663

Quoted 2 lines
net/core/filter.c +0 -0
 bpf_sp);
    static inline int __bpf_try_make_writable(struct sk_buff

*skb,

Quoted 10 lines
Quoted 8 lines
Quoted 8 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1671

Quoted 8 lines
net/core/filter.c +5 -0
      unsigned int write_len)
    {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
    return skb_ensure_writable(skb, write_len);

Dumb question, but should this guard be more generically part of

Quoted 2 lines
Quoted 2 lines

skb_ensure_writable()

Quoted 1 line

internals, presumably that would be inside

pskb_may_pull_reason(),

Quoted 4 lines
Quoted 4 lines

or only if we ever

Quoted 1 line

see more code instances similar to this?

Good point. Most callers of skb_ensure_writable correctly pull

only

Quoted 1 line
Quoted 1 line

headers, so wouldn't cause this problem. But it also adds coverage

to

Quoted 5 lines
Quoted 1 line

things like tc pedit.

Updated:

From 3be30b8cf6e629f2615ef4eafe3b2a1c0d68c530 Mon Sep 17 00:00:00

2001

Quoted 24 lines

From: Shiming Cheng <[email protected]>
Date: Sun, 28 Apr 2024 15:03:12 +0800
Subject: [PATCH net] net: prevent pulling SKB_GSO_FRAGLIST skb

BPF or TC callers may pull in a length longer than skb_headlen() for a SKB_GSO_FRAGLIST skb. The data in fraglist will be pulled into the linear space. However it destroys the skb's structure and may result in an invalid segmentation or kernel exception.

So we should add protection to stop the operation and return error to remind callers.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
   include/linux/skbuff.h | 6 ++++++
   1 file changed, 6 insertions(+)
include/linux/skbuff.h +0 -0
index 9d24aec064e8..3eef65b3db24 100644

include/linux/skbuff.h pskb_may_pull_reason(struct sk_buff *skb, · line 2748

Quoted 9 lines
include/linux/skbuff.h +5 -0
@@ -2740,6 +2740,12 @@ pskb_may_pull_reason(struct sk_buff *skb,
 unsigned int len)
   if (unlikely(len > skb->len))
   return SKB_DROP_REASON_PKT_TOO_SMALL;
 
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return SKB_DROP_REASON_NOMEM;
+}

The 'write_len > skb_headlen(skb)' test is redundant, no ?

It is covered by the earlier test :

Quoted 7 lines
Quoted 3 lines

if (likely(len <= skb_headlen(skb)))
return SKB_NOT_DROPPED_YET;

Daniel, it is not redundant. The bpf pulls a len between skb_headlen(skb) and skb->len that results in error. Here it will stop this operation. For other skbs(not SKB_GSO_FRAGLIST) it could be a normal behaviour and will continue to do next pulling.

I meant something like the below. The len <= skb_headlen(skb) case you already return earlier with SKB_NOT_DROPPED_YET. Willem, do you see a case where this should not live in pskb_may_pull_reason() but rather specifically in skb_ensure_writable()?

Thanks,
Daniel

include/linux/skbuff.h +9 -5
index 517e546a120a..ef2a0328ff2b 100644
@@ -1687,6 +1687,11 @@ int skb_zerocopy_iter_stream(struct sock *sk, struct sk_buff *skb,
 /* Internal */
 #define skb_shinfo(SKB)	((struct skb_shared_info *)(skb_end_pointer(SKB)))
 
+static inline bool skb_is_gso(const struct sk_buff *skb)
+{
+	return skb_shinfo(skb)->gso_size;
+}
+
 static inline struct skb_shared_hwtstamps *skb_hwtstamps(struct sk_buff *skb)
 {
 	return &skb_shinfo(skb)->hwtstamps;
@@ -2740,6 +2745,10 @@ pskb_may_pull_reason(struct sk_buff *skb, unsigned int len)
 	if (unlikely(len > skb->len))
 		return SKB_DROP_REASON_PKT_TOO_SMALL;
 
+	if (unlikely(skb_is_gso(skb) &&
+		     (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST)))
+		return SKB_DROP_REASON_FRAGLIST_PULL;
+
 	if (unlikely(!__pskb_pull_tail(skb, len - skb_headlen(skb))))
 		return SKB_DROP_REASON_NOMEM;
 
@@ -4953,11 +4962,6 @@ static inline struct sec_path *skb_sec_path(const struct sk_buff *skb)
 #endif
 }
 
-static inline bool skb_is_gso(const struct sk_buff *skb)
-{
-	return skb_shinfo(skb)->gso_size;
-}
-
 /* Note: Should be called only if skb_is_gso(skb) is true */
 static inline bool skb_is_gso_v6(const struct sk_buff *skb)
 {
include/net/dropreason-core.h +6 -0
index 9707ab54fdd5..9d6c97a6b2b6 100644
@@ -418,6 +418,12 @@ enum skb_drop_reason {
 	 * iterations.
 	 */
 	SKB_DROP_REASON_TC_RECLASSIFY_LOOP,
+	/**
+	 * @SKB_DROP_REASON_FRAGLIST_PULL: attempting to pull GSO fraglist
+	 * which destroys the skb's structure and may then result in an
+	 * invalid segmentation or kernel exception.
+	 */
+	SKB_DROP_REASON_FRAGLIST_PULL,
 	/**
 	 * @SKB_DROP_REASON_MAX: the maximum of core drop reasons, which
 	 * shouldn't be used as a real 'reason' - only for tracing code gen
Signature · 1 line
2.21.0
#30 lore raw
Quoted 12 lines
Quoted 11 lines
Quoted 7 lines

The 'write_len > skb_headlen(skb)' test is redundant, no ?

It is covered by the earlier test :

if (likely(len <= skb_headlen(skb)))
return SKB_NOT_DROPPED_YET;

Daniel, it is not redundant. The bpf pulls a len between skb_headlen(skb) and skb->len that results in error. Here it will stop this operation. For other skbs(not SKB_GSO_FRAGLIST) it could be a normal behaviour and will continue to do next pulling.

Quoted 4 lines

I meant something like the below. The len <= skb_headlen(skb) case you already return earlier with SKB_NOT_DROPPED_YET. Willem, do you see a case where this should not live in pskb_may_pull_reason() but rather specifically in skb_ensure_writable()?

Yes. pskb_may_pull is called all over the hot path. All in locations that are known safe, because they only pull header bytes. I prefer to limit the branch to the few (user configurable) locations that are in scope.

#31 lore raw
In reply to Lena Wang (王娜)
Quoted 259 lines · Lena Wang (王娜)
Quoted 219 lines · Willem de Bruijn

External email : Please do not click links or open attachments until you have verified the sender or the content.

Quoted 3 lines · Lena Wang (王娜)
Quoted 2 lines · Willem de Bruijn

External email : Please do not click links or open attachments

until

Quoted 7 lines
Quoted 7 lines

you have verified the sender or the content.

Quoted 2 lines

Hi Willem,
As the discussion, is it OK for the patch below?

Thanks for iterating on this.

I would like the opinion also of the fraglist and UDP GRO

experts.

Quoted 8 lines
Quoted 8 lines

Yes, I think both

- protecting skb_segment_list against clearly illegal fraglist packets, and
- blocking BPF from constructing such packets

are worthwhile stable fixes. I believe they should be two

separate

Quoted 8 lines
Quoted 8 lines

patches. Both probably with the same Fixes tag: 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining").

net/core/filter.c static DEFINE_PER_CPU(struct

Quoted 5 lines
net/core/filter.c +0 -0
index 3a6110ea4009..abc6029c8eef 100644
@@ -1655,6 +1655,11 @@ static DEFINE_PER_CPU(struct

bpf_scratchpad,

Quoted 3 lines
Quoted 3 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1657

Quoted 3 lines
net/core/filter.c +0 -0
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
                                          unsigned int

write_len)

Quoted 41 lines
Quoted 41 lines

net/core/filter.c static DEFINE_PER_CPU(struct · line 1664

Quoted 7 lines
net/core/filter.c +5 -0
 {
+       if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type &
+                       SKB_GSO_FRAGLIST) && (write_len >
 skb_headlen(skb))) {
+               return -ENOMEM;
+       }
+

Indentation looks off, but I agree with the logic.

    if (skb_is_gso(skb) &&
        (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
         (write_len > skb_headlen(skb)))

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 8 lines
net/core/filter.c +0 -0
        return skb_ensure_writable(skb, write_len);

}

net/core/skbuff.c +0 -0
index 73b1e0e53534..2e90534c1a1e 100644
@@ -4036,9 +4036,11 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 13 lines
net/core/skbuff.c +2 -0
 *skb,
        unsigned int tnl_hlen = skb_tnl_header_len(skb);
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        struct sk_buff *tail = NULL;
        struct sk_buff *nskb, *tmp;
        int len_diff, err;
+       bool err_len = false;
 
        skb_push(skb, -skb_network_offset(skb) + offset);
@@ -4047,6 +4049,14 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 4 lines
net/core/skbuff.c +0 -0
 *skb,
        if (err)
                goto err_linearize;
 
Quoted 7 lines
Quoted 7 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4055

Quoted 3 lines
net/core/skbuff.c +3 -0
+       if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb)) {
+               if (!list_skb) {
+                       goto err_linearize;

The label no longer truly covers the meaning.

But that is already true since the above (second) jump was added

in

Quoted 14 lines
Quoted 6 lines

commit c329b261afe7 ("net: prevent skb corruption on frag list segmentation").

Neither needs the kfree_skb_list, as skb->next is not assigned to until the loop. Can just return ERR_PTR(-EFAULT)?

Quoted 8 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4060

Quoted 5 lines
net/core/skbuff.c +5 -0
+               } else {
+                       err_len = true;
+               }
+       }
+

Why the branch? Might as well always fail immediately?

Quoted 7 lines

Hi Willem,
Thanks for your guidance.
You are right. There is no need for another branch as fraglist could be freeed in kfree_skb.
Could I git send mail wo patches as below?

From 933237400c0e2fa997470b70ff0e407996fa239c Mon Sep 17 00:00:00

2001

Quoted 5 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pull GROed skb's fraglist

A GROed skb with fraglist can't be pulled data

Please use the specific label: SKB_GSO_FRAGLIST skb

Quoted 35 lines

from its fraglist as it may result a invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pull
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +5 -1
index 8adf95765cdd..8ed4d5d87167 100644
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
   unsigned int write_len)
 {
+if (skb_is_gso(skb) &&
+    (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+     write_len > skb_headlen(skb)) {
+return -ENOMEM;
+}
 return skb_ensure_writable(skb, write_len);
 }
 
--

2.18.0

Quoted 1 line

From 2d0729b20cf810ba1b31e046952c1cd78f295ca3 Mon Sep 17 00:00:00

2001

Quoted 8 lines

From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop GROed skb pulled from fraglist

A GROed skb with fraglist maybe pulled by BPF
or other ways. It can't be trusted at all even
if one byte is pulled and should be dropped
on segmentation.

This paraphrases my comment. It is better to spell it out:

An SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is expected to have all segments except the last to be gso_size long. If this does not hold, the skb has been modified and the fraglist gso integrity is lost. Drop
the packet, as it cannot be segmented correctly by skb_segment_list.

The skb could be salvaged, though, right? By linearizing, dropping the SKB_GSO_FRAGLIST bit and entering the normal skb_segment path rather than the skb_segment_list path.

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

Quoted 4 lines

If the gso_size does not match skb_headlen(),
it means to be pulled part of or the entire
fraglsit. It has been messed with and we return

fraglist

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 14 lines

error to free this skb.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +0 -0
index b99127712e67..750fbb51b99f 100644
@@ -4493,6 +4493,7 @@ struct sk_buff *skb_segment_list(struct

sk_buff

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4497

Quoted 5 lines
net/core/skbuff.c +1 -0
 *skb,
 unsigned int tnl_hlen = skb_tnl_header_len(skb);
 unsigned int delta_truesize = 0;
 unsigned int delta_len = 0;
+unsigned int mss = skb_shinfo(skb)->gso_size;

Reverse christmas tree

net/core/skbuff.c struct sk_buff *skb_segment_list(struct

Quoted 4 lines
net/core/skbuff.c +0 -0
 struct sk_buff *tail = NULL;
 struct sk_buff *nskb, *tmp;

int len_diff, err;

net/core/skbuff.c +0 -0
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct

sk_buff

Quoted 4 lines
net/core/skbuff.c +0 -0
 *skb,
 if (err)
 goto err_linearize;
 
Quoted 5 lines

net/core/skbuff.c struct sk_buff *skb_segment_list(struct · line 4511

Quoted 3 lines
net/core/skbuff.c +3 -0
+if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+return ERR_PTR(-EFAULT);
+

Do this precondition integrity check before the skb_unclone path?

After return error, the skb will enter into kfree_skb, not consume_skb. It may meet same crash problem which has been resolved by skb_unclone.

Or kfree_skb could well handle the cloned skb's release?

Other changes are updated as below:

From 301da5c9d65652bac6091d4cd64b751b3338f8bb Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 13:42:35 +0800
Subject: [PATCH net] net: prevent BPF pulling SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb can't be pulled data
from its fraglist as it may result an invalid
segmentation or kernel exception.

For such structured skb we limit the BPF pulling
data length smaller than skb_headlen() and return
error if exceeding.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/filter.c | 5 +++++
 1 file changed, 5 insertions(+)
net/core/filter.c +0 -0
index 8adf95765cdd..8ed4d5d87167 100644

net/core/filter.c static DEFINE_PER_CPU(struct bpf_scratchpad, · line 1668

Quoted 7 lines
net/core/filter.c +3 -0
@@ -1662,6 +1662,11 @@ static DEFINE_PER_CPU(struct bpf_scratchpad,
 bpf_sp);
 static inline int __bpf_try_make_writable(struct sk_buff *skb,
                                          unsigned int write_len)
 {
+       if (skb_is_gso(skb) &&
+           (skb_shinfo(skb)->gso_type & SKB_GSO_FRAGLIST) &&
+            write_len > skb_headlen(skb)) {

at least call:
skb_ensure_writable(skb, skb_headlen(skb));
here before you return the error to make sure the already linear header portion is writable.
(the skb could be a clone with a r/o header afaict)

Quoted 67 lines
net/core/filter.c +2 -1
+               return -ENOMEM;
+       }
        return skb_ensure_writable(skb, write_len);
 }
 
--

2.18.0

From 64d55392debbc90ef2e9c33441024d612075bdd7 Mon Sep 17 00:00:00 2001
From: Shiming Cheng <[email protected]>
Date: Wed, 24 Apr 2024 14:43:45 +0800
Subject: [PATCH net] net: drop pulled SKB_GSO_FRAGLIST skb

A SKB_GSO_FRAGLIST skb without GSO_BY_FRAGS is expected to have all segments except the last to be gso_size long. If this does not hold, the skb has been modified and the fraglist gso integrity is lost. Drop the packet, as it cannot be segmented correctly by skb_segment_list.

The skb could be salvaged, though, right?
By linearizing, dropping the SKB_GSO_FRAGLIST bit and entering the normal skb_segment path rather than the skb_segment_list path.

That choice is currently made in the protocol caller, __udp_gso_segment. It's not trivial to add such a backup path here. So let's add this backstop against kernel crashes.

If the gso_size does not match skb_headlen(), it means part of or the entire fraglist has been pulled. It has been messed with and we should return error to free this skb.

Fixes 3a1296a38d0c ("net: Support GRO/GSO fraglist chaining.")
---
 net/core/skbuff.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/skbuff.c +1 -0
index b99127712e67..4777f5fea6c3 100644
@@ -4491,6 +4491,7 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
 {
        struct sk_buff *list_skb = skb_shinfo(skb)->frag_list;
        unsigned int tnl_hlen = skb_tnl_header_len(skb);
+       unsigned int mss = skb_shinfo(skb)->gso_size;
        unsigned int delta_truesize = 0;
        unsigned int delta_len = 0;
        struct sk_buff *tail = NULL;
net/core/skbuff.c +3 -0
@@ -4504,6 +4505,9 @@ struct sk_buff *skb_segment_list(struct sk_buff
 *skb,
        if (err)
                goto err_linearize;
 
+       if (mss != GSO_BY_FRAGS && mss != skb_headlen(skb))
+               return ERR_PTR(-EFAULT);
+
        skb_shinfo(skb)->frag_list = NULL;
 
        while (list_skb) {
Quoted 2 lines

--
2.18.0

--
Maciej Żenczykowski, Kernel Networking Developer @ Google