Skip to contentSkip to conversation

parser: tcpopt: fix tcp option parsing with NUM + length field

PATCH In review Florian Westphal 2 participants 2 messages Dec 6, 2023 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH v3 1/1 parser: tcpopt: fix tcp option parsing with NUM + length field

tcp option 254 length ge 4

... will segfault.
The crash bug is that tcpopt_expr_alloc() can return NULL if we cannot find a suitable template for the requested kind + field combination, so add the needed error handling in the bison parser.

However, we can handle this. NOP and EOL have templates, all other options (known or unknown) must also have a length field.

So also add a fallback template to handle both kind and length, even if only a numeric option is given that nft doesn't recognize.

Don't bother with output, above will be printed via raw syntax, i.e. tcp option @254,8,8 >= 4.

Fixes 24d8da308342 ("tcpopt: allow to check for presence of any tcp option")

---
v3: same as v2 but added a helper to set the template instead of the 'goto'.

 src/parser_bison.y                            |  4 ++
 src/tcpopt.c                                  | 44 ++++++++++++++-----
 .../packetpath/dumps/tcp_options.nft          | 14 ++++++
 tests/shell/testcases/packetpath/tcp_options  | 39 ++++++++++++++++
 4 files changed, 91 insertions(+), 10 deletions(-)
 create mode 100644 tests/shell/testcases/packetpath/dumps/tcp_options.nft
 create mode 100755 tests/shell/testcases/packetpath/tcp_options
src/parser_bison.y +4 -0
index ee7e9e14c1f2..1a3d64f794cb 100644
@@ -5828,6 +5828,10 @@ tcp_hdr_expr		:	TCP	tcp_hdr_field
 			|	TCP	OPTION	tcp_hdr_option_kind_and_field
 			{
 				$$ = tcpopt_expr_alloc(&@$, $3.kind, $3.field);
+				if ($$ == NULL) {
+					erec_queue(error(&@1, "Could not find a tcp option template"), state->msgs);
+					YYERROR;
+				}
 			}
 			|	TCP	OPTION	AT	close_scope_at	tcp_hdr_option_type	COMMA	NUM	COMMA	NUM
 			{
src/tcpopt.c +34 -10

75 lines not on this page · raw message

tests/shell/testcases/packetpath/dumps/tcp_options.nft new file +14 -0
new file mode 100644
index 000000000000..03e50a56e8c9
@@ -0,0 +1,14 @@
+table inet t {
+	chain c {
+		type filter hook output priority filter; policy accept;
+		tcp dport != 22345 accept
+		tcp flags syn / fin,syn,rst,ack tcp option @254,8,8 >= 4 counter packets 0 bytes 0 drop
+		tcp flags syn / fin,syn,rst,ack tcp option fastopen length >= 2 reset tcp option fastopen counter packets 0 bytes 0
+		tcp flags syn / fin,syn,rst,ack tcp option sack-perm missing counter packets 0 bytes 0 drop
+		tcp flags syn / fin,syn,rst,ack tcp option sack-perm exists counter packets 1 bytes 60
+		tcp flags syn / fin,syn,rst,ack tcp option maxseg size > 1400 counter packets 1 bytes 60
+		tcp flags syn / fin,syn,rst,ack tcp option nop missing counter packets 0 bytes 0
+		tcp flags syn / fin,syn,rst,ack tcp option nop exists counter packets 1 bytes 60
+		tcp flags syn / fin,syn,rst,ack drop
+	}
+}
tests/shell/testcases/packetpath/tcp_options new file +39 -0
new file mode 100755
index 000000000000..0f1ca2644655
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+have_socat="no"
+socat -h > /dev/null && have_socat="yes"
+
+ip link set lo up
+
+$NFT -f /dev/stdin <<EOF
+table inet t {
+	chain c {
+		type filter hook output priority 0;
+		tcp dport != 22345 accept
+		tcp flags syn / fin,syn,rst,ack tcp option 254  length ge 4 counter drop
+		tcp flags syn / fin,syn,rst,ack tcp option fastopen length ge 2 reset tcp option fastopen counter
+		tcp flags syn / fin,syn,rst,ack tcp option sack-perm missing counter drop
+		tcp flags syn / fin,syn,rst,ack tcp option sack-perm exists counter
+		tcp flags syn / fin,syn,rst,ack tcp option maxseg size gt 1400 counter
+		tcp flags syn / fin,syn,rst,ack tcp option nop missing counter
+		tcp flags syn / fin,syn,rst,ack tcp option nop exists counter
+		tcp flags syn / fin,syn,rst,ack drop
+	}
+}
+EOF
+
+if [ $? -ne 0 ]; then
+	exit 1
+fi
+
+if [ $have_socat != "yes" ]; then
+	echo "Ran partial test, socat not available (skipped)"
+	exit 77
+fi
+
+# This will fail (drop in output -> connect fails with eperm)
+socat -t 3 -u STDIN TCP:127.0.0.1:22345,connect-timeout=1 < /dev/null > /dev/null
+
+# Indicate success, dump file has incremented packet counter where its
+# expected to match.
+exit 0
Signature · 1 line
2.41.0
#1 lore raw
Quoted 7 lines · Florian Westphal

tcp option 254 length ge 4

... will segfault.
The crash bug is that tcpopt_expr_alloc() can return NULL if we cannot find a suitable template for the requested kind + field combination, so add the needed error handling in the bison parser.

Quoted 8 lines

However, we can handle this. NOP and EOL have templates, all other options (known or unknown) must also have a length field.

So also add a fallback template to handle both kind and length, even if only a numeric option is given that nft doesn't recognize.

Don't bother with output, above will be printed via raw syntax, i.e. tcp option @254,8,8 >= 4.

Patch LGTM, please push it out.

Thanks.