Skip to contentSkip to conversation

Fix out of bounds when parsing TCP options

PATCH Applied Maxim Mikityanskiy 5 participants 9 messages Jun 10, 2021 – Jun 11, 2021

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

v2 cover Fix out of bounds when parsing TCP options

This series fixes out-of-bounds access in various places in the kernel where parsing of TCP options takes place. Fortunately, many more occurrences don't have this bug.

v2 changes:

synproxy: Added an early return when length < 0 to avoid calling skb_header_pointer with negative length.

sch_cake: Added doff validation to avoid parsing garbage.

Maxim Mikityanskiy (3):
netfilter: synproxy: Fix out of bounds when parsing TCP options
mptcp: Fix out of bounds when parsing TCP options
sch_cake: Fix out of bounds when parsing TCP options and header

 net/mptcp/options.c              | 2 ++
 net/netfilter/nf_synproxy_core.c | 5 +++++
 net/sched/sch_cake.c             | 6 +++++-
 3 files changed, 12 insertions(+), 1 deletion(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v2 1/3 netfilter: synproxy: Fix out of bounds when parsing TCP options

The TCP option parser in synproxy (synproxy_parse_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

v2 changes:

Added an early return when length < 0 to avoid calling skb_header_pointer with negative length.

Cc Young Xiao <[email protected]>
Fixes 48b1de4c110a ("netfilter: add SYNPROXY core/target")
---
 net/netfilter/nf_synproxy_core.c | 5 +++++
 1 file changed, 5 insertions(+)
net/netfilter/nf_synproxy_core.c +5 -0
index b100c04a0e43..3d6d49420db8 100644
@@ -31,6 +31,9 @@ synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
 	int length = (th->doff * 4) - sizeof(*th);
 	u8 buf[40], *ptr;
 
+	if (unlikely(length < 0))
+		return false;
+
 	ptr = skb_header_pointer(skb, doff + sizeof(*th), length, buf);
 	if (ptr == NULL)
 		return false;
@@ -47,6 +50,8 @@ synproxy_parse_options(const struct sk_buff *skb, unsigned int doff,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return true;
 			opsize = *ptr++;
 			if (opsize < 2)
 				return true;
Signature · 1 line
2.25.1
maintainer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH net v2 1/3] netfilter: synproxy: Fix out of bounds when parsing TCP options

Quoted 8 lines · Maxim Mikityanskiy

The TCP option parser in synproxy (synproxy_parse_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

#3 lore raw

PATCH v2 2/3 mptcp: Fix out of bounds when parsing TCP options

In reply to Maxim Mikityanskiy

The TCP option parser in mptcp (mptcp_get_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

Cc Young Xiao <[email protected]>
Fixes cec37a6e41aa ("mptcp: Handle MP_CAPABLE options for outgoing connections")
---
 net/mptcp/options.c | 2 ++
 1 file changed, 2 insertions(+)
net/mptcp/options.c +2 -0
index 6b825fb3fa83..9b263f27ce9b 100644
@@ -356,6 +356,8 @@ void mptcp_get_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH net v2 2/3] mptcp: Fix out of bounds when parsing TCP options

Quoted 32 lines · Maxim Mikityanskiy

The TCP option parser in mptcp (mptcp_get_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

Cc Young Xiao <[email protected]>
Fixes cec37a6e41aa ("mptcp: Handle MP_CAPABLE options for outgoing connections")

---
net/mptcp/options.c | 2 ++

1 file changed, 2 insertions(+)
net/mptcp/options.c +2 -0
index 6b825fb3fa83..9b263f27ce9b 100644
@@ -356,6 +356,8 @@ void mptcp_get_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
Signature · 1 line
2.25.1

Matthieu -

Could you apply this in mptcp_net-next so it's easier to track when the patch finds its way to net-next?

(MPTCP patchwork status set to "queued")

Thanks!

--
Mat Martineau
Intel

#5 lore raw

Re: [PATCH net v2 2/3] mptcp: Fix out of bounds when parsing TCP options

Quoted 37 lines · Mat Martineau
Quoted 32 lines · Maxim Mikityanskiy

The TCP option parser in mptcp (mptcp_get_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

Cc Young Xiao <[email protected]>
Fixes cec37a6e41aa ("mptcp: Handle MP_CAPABLE options for outgoing connections")

---
net/mptcp/options.c | 2 ++

1 file changed, 2 insertions(+)
net/mptcp/options.c +2 -0
index 6b825fb3fa83..9b263f27ce9b 100644
@@ -356,6 +356,8 @@ void mptcp_get_options(const struct sk_buff *skb,
 			length--;
 			continue;
 		default:
+			if (length < 2)
+				return;
 			opsize = *ptr++;
 			if (opsize < 2) /* "silly options" */
 				return;
Signature · 1 line
2.25.1

Matthieu -

Quoted 3 lines

Could you apply this in mptcp_net-next so it's easier to track when the patch finds its way to net-next?

(MPTCP patchwork status set to "queued")

And to clarify for Maxim: we have a separate MPTCP git tree where we stage MPTCP changes before submitting to net- or net-next. For your patch set that has related changes across a few networking subsystems, sending directly to netdev is the best thing to do.

We do have other queued changes for netdev so I think it would work well to include this patch in our tree now, and it will automatically disappear once it is fully merged upstream. Sometimes it takes a couple of weeks for the net/master branch to get merged in to net-next/master.

--
Mat Martineau
Intel

#6 lore raw

Re: [PATCH net v2 2/3] mptcp: Fix out of bounds when parsing TCP options

In reply to Mat Martineau

Hi Mat,

Quoted 17 lines · Mat Martineau
Quoted 6 lines · Maxim Mikityanskiy

The TCP option parser in mptcp (mptcp_get_options) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

Quoted 8 lines

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

Cc Young Xiao <[email protected]>
Fixes cec37a6e41aa ("mptcp: Handle MP_CAPABLE options for outgoing

connections")

Quoted 4 lines

Matthieu -

Could you apply this in mptcp_net-next so it's easier to track when the patch finds its way to net-next?

Sure, good idea!
Thank you for the patch and the review!

Just applied in our tree:

- fdd037a5564a: mptcp: Fix out of bounds when parsing TCP options
- Results: 083034593cec..1ba3eb627299

Builds and tests are now in progress:

https://cirrus-ci.com/github/multipath-tcp/mptcp_net-next/export/20210611T142646
https://github.com/multipath-tcp/mptcp_net-next/actions/workflows/build-validation.yml?query=branch:export/20210611T142646

Cheers,
Matt

Signature · 2 lines
Tessares | Belgium | Hybrid Access Solutions
www.tessares.net
#7 lore raw

PATCH v2 3/3 sch_cake: Fix out of bounds when parsing TCP options and header

In reply to Maxim Mikityanskiy

The TCP option parser in cake qdisc (cake_get_tcpopt and cake_tcph_may_drop) could read one byte out of bounds. When the length is 1, the execution flow gets into the loop, reads one byte of the opcode, and if the opcode is neither TCPOPT_EOL nor TCPOPT_NOP, it reads one more byte, which exceeds the length of 1.

This fix is inspired by commit 9609dad263f8 ("ipv4: tcp_input: fix stack out of bounds when parsing TCP options.").

v2 changes:

Added doff validation in cake_get_tcphdr to avoid parsing garbage as TCP header. Although it wasn't strictly an out-of-bounds access (memory was allocated), garbage values could be read where CAKE expected the TCP header if doff was smaller than 5.

Cc Young Xiao <[email protected]>
Fixes 8b7138814f29 ("sch_cake: Add optional ACK filter")
---
 net/sched/sch_cake.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
net/sched/sch_cake.c +5 -1
index 7d37638ee1c7..5c15968b5155 100644
@@ -943,7 +943,7 @@ static struct tcphdr *cake_get_tcphdr(const struct sk_buff *skb,
 	}
 
 	tcph = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
-	if (!tcph)
+	if (!tcph || tcph->doff < 5)
 		return NULL;
 
 	return skb_header_pointer(skb, offset,
@@ -967,6 +967,8 @@ static const void *cake_get_tcpopt(const struct tcphdr *tcph,
 			length--;
 			continue;
 		}
+		if (length < 2)
+			break;
 		opsize = *ptr++;
 		if (opsize < 2 || opsize > length)
 			break;
@@ -1104,6 +1106,8 @@ static bool cake_tcph_may_drop(const struct tcphdr *tcph,
 			length--;
 			continue;
 		}
+		if (length < 2)
+			break;
 		opsize = *ptr++;
 		if (opsize < 2 || opsize > length)
 			break;
Signature · 1 line
2.25.1
#8 lore raw
In reply to Maxim Mikityanskiy
This series was applied to netdev/net.git (refs/heads/master):

Hello:

This series was applied to netdev/net.git (refs/heads/master):

Quoted 10 lines · On Thu, 10 Jun 2021 19:40:28 +0300 you wrote:

This series fixes out-of-bounds access in various places in the kernel where parsing of TCP options takes place. Fortunately, many more occurrences don't have this bug.

v2 changes:

synproxy: Added an early return when length < 0 to avoid calling skb_header_pointer with negative length.

[...]

Here is the summary with links:
- [net,v2,1/3] netfilter: synproxy: Fix out of bounds when parsing TCP options

    https://git.kernel.org/netdev/net/c/5fc177ab7594
  - [net,v2,2/3] mptcp: Fix out of bounds when parsing TCP options
    https://git.kernel.org/netdev/net/c/07718be26568
  - [net,v2,3/3] sch_cake: Fix out of bounds when parsing TCP options and header
    https://git.kernel.org/netdev/net/c/ba91c49dedbd

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