Skip to contentSkip to conversation

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

PATCH Superseded Florian Westphal 4 participants 18 messages Dec 5, 2023 – Dec 6, 2023 netfilter

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH v2 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")

---
v2: MUST bump exthdr.offset, else this continues to check for 'kind', even if 'length' was asked for.
Also fix the dump file, it was not correct (254,0,8 instead of 254,8,8).

 src/parser_bison.y                            |  4 ++
 src/tcpopt.c                                  | 28 +++++++++----
 .../packetpath/dumps/tcp_options.nft          | 14 +++++++
 tests/shell/testcases/packetpath/tcp_options  | 39 +++++++++++++++++++
 4 files changed, 78 insertions(+), 7 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 +21 -7
index 3fcb2731ae73..93b08c8cc0f2 100644
@@ -118,6 +118,13 @@ static const struct exthdr_desc tcpopt_mptcp = {
 		[TCPOPT_MPTCP_SUBTYPE]  = PHT("subtype", 16, 4),
 	},
 };
+
+static const struct exthdr_desc tcpopt_fallback = {
+	.templates	= {
+		[TCPOPT_COMMON_KIND]	= PHT("kind",   0, 8),
+		[TCPOPT_COMMON_LENGTH]	= PHT("length", 8, 8),
+	},
+};
 #undef PHT
 
 const struct exthdr_desc *tcpopt_protocols[] = {
@@ -182,19 +189,24 @@ struct expr *tcpopt_expr_alloc(const struct location *loc,
 		desc = tcpopt_protocols[kind];
 
 	if (!desc) {
-		if (field != TCPOPT_COMMON_KIND || kind > 255)
+		if (kind > 255)
 			return NULL;
 
+		switch (field) {
+		case TCPOPT_COMMON_KIND:
+		case TCPOPT_COMMON_LENGTH:
+			break;
+		default:
+			return NULL;
+		}
+
 		expr = expr_alloc(loc, EXPR_EXTHDR, &integer_type,
 				  BYTEORDER_BIG_ENDIAN, 8);
 
-		desc = tcpopt_protocols[TCPOPT_NOP];
+		desc = &tcpopt_fallback;
 		tmpl = &desc->templates[field];
-		expr->exthdr.desc   = desc;
-		expr->exthdr.tmpl   = tmpl;
-		expr->exthdr.op = NFT_EXTHDR_OP_TCPOPT;
 		expr->exthdr.raw_type = kind;
-		return expr;
+		goto out_finalize;
 	}
 
 	tmpl = &desc->templates[field];
@@ -203,10 +215,12 @@ struct expr *tcpopt_expr_alloc(const struct location *loc,
 
 	expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype,
 			  BYTEORDER_BIG_ENDIAN, tmpl->len);
+
+	expr->exthdr.raw_type = desc->type;
+out_finalize:
 	expr->exthdr.desc   = desc;
 	expr->exthdr.tmpl   = tmpl;
 	expr->exthdr.op     = NFT_EXTHDR_OP_TCPOPT;
-	expr->exthdr.raw_type = desc->type;
 	expr->exthdr.offset = tmpl->offset;
 
 	return expr;
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 66 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.

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")

---
v2: MUST bump exthdr.offset, else this continues to check for 'kind', even if 'length' was asked for.
Also fix the dump file, it was not correct (254,0,8 instead of 254,8,8).

 src/parser_bison.y                            |  4 ++
 src/tcpopt.c                                  | 28 +++++++++----
 .../packetpath/dumps/tcp_options.nft          | 14 +++++++
 tests/shell/testcases/packetpath/tcp_options  | 39 +++++++++++++++++++
 4 files changed, 78 insertions(+), 7 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 +7 -0
index 3fcb2731ae73..93b08c8cc0f2 100644
@@ -118,6 +118,13 @@ static const struct exthdr_desc tcpopt_mptcp = {
 		[TCPOPT_MPTCP_SUBTYPE]  = PHT("subtype", 16, 4),
 	},
 };
+
+static const struct exthdr_desc tcpopt_fallback = {
+	.templates	= {
+		[TCPOPT_COMMON_KIND]	= PHT("kind",   0, 8),
+		[TCPOPT_COMMON_LENGTH]	= PHT("length", 8, 8),
+	},
+};
 #undef PHT
 
 const struct exthdr_desc *tcpopt_protocols[] = {

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 193

Quoted 6 lines
src/tcpopt.c +1 -1
@@ -182,19 +189,24 @@ struct expr *tcpopt_expr_alloc(const struct location *loc,
 		desc = tcpopt_protocols[kind];
 
 	if (!desc) {
-		if (field != TCPOPT_COMMON_KIND || kind > 255)
+		if (kind > 255)
 			return NULL;

Another suggestion: Remove this NULL, it leaves lhs as NULL in the relational. kind > 255 cannot ever happen, parser rejects numbers over 255.

ruleset.nft:1:30-32: Error: value too large
add rule inet x y tcp option 256 length 4

                             ^^^

you could turn it into assert().

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 200

Quoted 6 lines
src/tcpopt.c +6 -0
+		switch (field) {
+		case TCPOPT_COMMON_KIND:
+		case TCPOPT_COMMON_LENGTH:
+			break;
+		default:
+			return NULL;

Probably instead of this code above:

+ desc = &tcpopt_fallback;
+ if (field > TCPOPT_COMMON_LENGTH)
+ tmpl = &tcpopt_unknown_template;
+ else
+ tmpl = &desc->templates[field];

so no NULL is ever returned and fall back to the unknown template.

Quoted 4 lines
src/tcpopt.c +2 -0
+		}
+
 		expr = expr_alloc(loc, EXPR_EXTHDR, &integer_type,
 				  BYTEORDER_BIG_ENDIAN, 8);

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 209

Quoted 10 lines
src/tcpopt.c +2 -5
 
-		desc = tcpopt_protocols[TCPOPT_NOP];
+		desc = &tcpopt_fallback;
 		tmpl = &desc->templates[field];
-		expr->exthdr.desc   = desc;
-		expr->exthdr.tmpl   = tmpl;
-		expr->exthdr.op = NFT_EXTHDR_OP_TCPOPT;
 		expr->exthdr.raw_type = kind;
-		return expr;
+		goto out_finalize;

Maybe add a helper function to set up expr->exthdr to avoid the goto trick. But not a deal breaker, it is OK if you prefer it this way.

Thanks!

Quoted 86 lines
src/tcpopt.c +3 -1
 	}
 
 	tmpl = &desc->templates[field];
@@ -203,10 +215,12 @@ struct expr *tcpopt_expr_alloc(const struct location *loc,
 
 	expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype,
 			  BYTEORDER_BIG_ENDIAN, tmpl->len);
+
+	expr->exthdr.raw_type = desc->type;
+out_finalize:
 	expr->exthdr.desc   = desc;
 	expr->exthdr.tmpl   = tmpl;
 	expr->exthdr.op     = NFT_EXTHDR_OP_TCPOPT;
-	expr->exthdr.raw_type = desc->type;
 	expr->exthdr.offset = tmpl->offset;
 
 	return expr;
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
author maintainer
#2 lore raw
Quoted 8 lines · Pablo Neira Ayuso

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 193

Quoted 4 lines
src/tcpopt.c +1 -1
 	if (!desc) {
-		if (field != TCPOPT_COMMON_KIND || kind > 255)
+		if (kind > 255)
 			return NULL;

Another suggestion: Remove this NULL, it leaves lhs as NULL in the relational. kind > 255 cannot ever happen, parser rejects numbers over 255.

We can also feed this via input from udata (typeof). So I'd rather not assert() or rely on bison checks.

#3 lore raw
Quoted 10 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 193

Quoted 4 lines
src/tcpopt.c +1 -1
 	if (!desc) {
-		if (field != TCPOPT_COMMON_KIND || kind > 255)
+		if (kind > 255)
 			return NULL;

Another suggestion: Remove this NULL, it leaves lhs as NULL in the relational. kind > 255 cannot ever happen, parser rejects numbers over 255.

Quoted 2 lines

We can also feed this via input from udata (typeof). So I'd rather not assert() or rely on bison checks.

OK, but then NULL does not help either, that will crash on evaluation too.

You could narrow down kind and field in tcpopt_expr_alloc() to uint8_t.

author maintainer
#4 lore raw
Quoted 11 lines · Pablo Neira Ayuso
Quoted 10 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 193

Quoted 4 lines
src/tcpopt.c +1 -1
 	if (!desc) {
-		if (field != TCPOPT_COMMON_KIND || kind > 255)
+		if (kind > 255)
 			return NULL;

Another suggestion: Remove this NULL, it leaves lhs as NULL in the relational. kind > 255 cannot ever happen, parser rejects numbers over 255.

Quoted 6 lines
Quoted 2 lines

We can also feed this via input from udata (typeof). So I'd rather not assert() or rely on bison checks.

OK, but then NULL does not help either, that will crash on evaluation too.

You could narrow down kind and field in tcpopt_expr_alloc() to uint8_t.

Unfortunately, no. 'kind' is overloaded, SACK blocks 1/2/3/4 use values gt 255, see TCPOPT_KIND_SACK3 at end of enum tcpopt_kind.

#5 lore raw
Quoted 15 lines · Florian Westphal
Quoted 14 lines · Pablo Neira Ayuso
Quoted 12 lines · Florian Westphal
Quoted 8 lines · Pablo Neira Ayuso

src/tcpopt.c struct expr *tcpopt_expr_alloc(const struct location *loc, · line 193

Quoted 4 lines
src/tcpopt.c +1 -1
 	if (!desc) {
-		if (field != TCPOPT_COMMON_KIND || kind > 255)
+		if (kind > 255)
 			return NULL;

Another suggestion: Remove this NULL, it leaves lhs as NULL in the relational. kind > 255 cannot ever happen, parser rejects numbers over 255.

We can also feed this via input from udata (typeof). So I'd rather not assert() or rely on bison checks.

Quoted 6 lines
Quoted 3 lines

OK, but then NULL does not help either, that will crash on evaluation too.

You could narrow down kind and field in tcpopt_expr_alloc() to uint8_t.

Unfortunately, no. 'kind' is overloaded, SACK blocks 1/2/3/4 use values gt 255, see TCPOPT_KIND_SACK3 at end of enum tcpopt_kind.

OK, patch is fine then.

#6 lore raw
In reply to Florian Westphal

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

is there a reason not to also generate a .json-nft file?

Thomas

author maintainer
#7 lore raw
Quoted 6 lines · Thomas Haller

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

is there a reason not to also generate a .json-nft file?

Yes, I am not adding more one-line monsters.

I'll add one once there is a solution in place that has human readable json dumps that don't fail validation because of identical but differently formatted output.

#8 lore raw
Quoted 6 lines · Florian Westphal
Quoted 5 lines · Thomas Haller

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

Quoted 9 lines
Quoted 1 line

is there a reason not to also generate a .json-nft file?

Yes, I am not adding more one-line monsters.

I'll add one once there is a solution in place that has human readable
json dumps that don't fail validation because of identical but differently formatted output.

What about the "[PATCH nft 0/2] pretty print .json-nft files" patches?

Thomas

author maintainer
#9 lore raw
Quoted 11 lines · Thomas Haller
Quoted 10 lines · Florian Westphal
Quoted 6 lines · Thomas Haller

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

is there a reason not to also generate a .json-nft file?

Yes, I am not adding more one-line monsters.

Quoted 7 lines
Quoted 5 lines

I'll add one once there is a solution in place that has human readable
json dumps that don't fail validation because of identical but differently formatted output.

What about the "[PATCH nft 0/2] pretty print .json-nft files" patches?

I'm fine with that. Phil? Pablo? This is re:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

author maintainer
#10 lore raw
Quoted 18 lines · Florian Westphal
Quoted 17 lines · Thomas Haller
Quoted 15 lines · Florian Westphal
Quoted 6 lines · Thomas Haller

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

is there a reason not to also generate a .json-nft file?

Yes, I am not adding more one-line monsters.

I'll add one once there is a solution in place that has human readable
json dumps that don't fail validation because of identical but differently formatted output.

Quoted 5 lines
Quoted 1 line

What about the "[PATCH nft 0/2] pretty print .json-nft files" patches?

I'm fine with that. Phil? Pablo? This is re:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

What about making it so we NEVER compare json-nft at all?

Instead, feed the json-nft file to nft, then do a normal list-ruleset, then compare that vs. normal .nft file.

This avoids any and all formatting issues and also avoids breakage when the json-nft file is formatted differently.

Eg. postprocessing via json_pp won't match what this patch above expects.

#11 lore raw
Quoted 24 lines · Florian Westphal
Quoted 23 lines · Florian Westphal
Quoted 11 lines · Thomas Haller
Quoted 10 lines · Florian Westphal
Quoted 6 lines · Thomas Haller

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

is there a reason not to also generate a .json-nft file?

Yes, I am not adding more one-line monsters.

Quoted 8 lines
Quoted 5 lines

I'll add one once there is a solution in place that has human readable
json dumps that don't fail validation because of identical but differently formatted output.

What about the "[PATCH nft 0/2] pretty print .json-nft files" patches?

I'm fine with that. Phil? Pablo? This is re:

Quoted 7 lines
Quoted 1 line

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

What about making it so we NEVER compare json-nft at all?

Instead, feed the json-nft file to nft, then do a normal list- ruleset,
then compare that vs. normal .nft file.

The .nft and .json-nft files are all fed back into nft --check -f. So that is happening too.

It will also comparing the raw files (after sanitize+prettify), which is closer to the original thing that is supposed to be tested. That is why it's done.

Quoted 8 lines

This avoids any and all formatting issues and also avoids breakage when
the json-nft file is formatted differently.

Eg. postprocessing via json_pp won't match what this patch above expects.

What issues do you mean? I don't see any. Did you test/review the two patches?

Thomas

author maintainer
#12 lore raw
Quoted 6 lines · Thomas Haller
Quoted 3 lines

Instead, feed the json-nft file to nft, then do a normal list- ruleset,
then compare that vs. normal .nft file.

The .nft and .json-nft files are all fed back into nft --check -f. So that is happening too.

Not really, this checks that the parser eats the input.

Quoted 3 lines

It will also comparing the raw files (after sanitize+prettify), which is closer to the original thing that is supposed to be tested. That is why it's done.

"metainfo": {
- "json_schema_version": 1,
+ "version": "VERSION",
"release_name": "RELEASE_NAME",
- "version": "VERSION"
+ "json_schema_version": 1
}
},

i.e. it fails validation because the on-record file has a different layout/ordering than what is expected.

But if you feed it into nft, nft list ruleset will generate the expected (non-json) output.

Quoted 2 lines

What issues do you mean? I don't see any. Did you test/review the two patches?

The first one is applied. The second one I applied locally.

But its still picky about the formatting.

#13 lore raw
Quoted 26 lines · Florian Westphal
Quoted 7 lines · Thomas Haller
Quoted 3 lines

Instead, feed the json-nft file to nft, then do a normal list- ruleset,
then compare that vs. normal .nft file.

The .nft and .json-nft files are all fed back into nft --check - f. So
that is happening too.

Not really, this checks that the parser eats the input.

Quoted 5 lines

It will also comparing the raw files (after sanitize+prettify), which
is closer to the original thing that is supposed to be tested. That is
why it's done.

"metainfo": {
-        "json_schema_version": 1,
+        "version": "VERSION",
"release_name": "RELEASE_NAME",
-        "version": "VERSION"
+        "json_schema_version": 1
}
},

Quoted 2 lines

i.e. it fails validation because the on-record file has a different layout/ordering than what is expected.

Does this mean all tests on master have this problem?

Quoted 4 lines

But if you feed it into nft, nft list ruleset will generate the expected
(non-json) output.

where do you encounter that? How to reproduce this?

Is this an old libjansson? Since 2.8 (2016), JSON_PRESERVE_ORDER is implied. Maybe libnftables needs to set JSON_PRESERVE_ORDER flag at a few places.

Quoted 8 lines
Quoted 4 lines

What issues do you mean? I don't see any. Did you test/review the two
patches?

The first one is applied.  The second one I applied locally.

But its still picky about the formatting.

That's a problem...

author maintainer
#14 lore raw
Quoted 9 lines · Thomas Haller
Quoted 9 lines

"metainfo": {
-        "json_schema_version": 1,
+        "version": "VERSION",
"release_name": "RELEASE_NAME",
-        "version": "VERSION"
+        "json_schema_version": 1
}
},

Quoted 4 lines
Quoted 2 lines

i.e. it fails validation because the on-record file has a different layout/ordering than what is expected.

Does this mean all tests on master have this problem?

No, those are all raw binary-like oneline dumps.

Quoted 9 lines
Quoted 3 lines

But if you feed it into nft, nft list ruleset will generate the expected
(non-json) output.

where do you encounter that? How to reproduce this?

Is this an old libjansson? Since 2.8 (2016), JSON_PRESERVE_ORDER is implied. Maybe libnftables needs to set JSON_PRESERVE_ORDER flag at a few places.

Just format any existing json dump file with a different formatting tool, e.g. json_pp.

#15 lore raw
Quoted 10 lines · Florian Westphal
Quoted 9 lines · Thomas Haller
Quoted 9 lines

"metainfo": {
-        "json_schema_version": 1,
+        "version": "VERSION",
"release_name": "RELEASE_NAME",
-        "version": "VERSION"
+        "json_schema_version": 1
}
},

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

i.e. it fails validation because the on-record file has a different
layout/ordering than what is expected.

Does this mean all tests on master have this problem?

No, those are all raw binary-like oneline dumps.

sorry, I don't follow.

The patch takes the "raw binary-like oneline dump", and prettifies it. It does so via json-pretty.sh script. That script, does of course not mess up the order of dictionary keys.

Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

But if you feed it into nft, nft list ruleset will generate the expected
(non-json) output.

where do you encounter that? How to reproduce this?

Quoted 7 lines
Quoted 4 lines

Is this an old libjansson? Since 2.8 (2016), JSON_PRESERVE_ORDER is implied. Maybe libnftables needs to set JSON_PRESERVE_ORDER flag at a
few places.

Just format any existing json dump file with a different formatting tool, e.g. json_pp.

The patch has a way for prettifying JSON (json-pretty.sh). Sure, it could also use json_pp instead, but what problem would that solve?

Thomas

reviewer
#16 lore raw
In reply to Florian Westphal
Quoted 19 lines · Florian Westphal
Quoted 18 lines · Florian Westphal
Quoted 17 lines · Thomas Haller
Quoted 15 lines · Florian Westphal
Quoted 6 lines · Thomas Haller

Hi Florian,

Quoted 1 line · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14 +++++++

is there a reason not to also generate a .json-nft file?

Yes, I am not adding more one-line monsters.

I'll add one once there is a solution in place that has human readable
json dumps that don't fail validation because of identical but differently formatted output.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

What about the "[PATCH nft 0/2] pretty print .json-nft files" patches?

I'm fine with that. Phil? Pablo? This is re:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

What I don't like is that we'll still get these huge patches/mails if the dumps are converted. Those that remain are still hard to handle in case of errors.

Quoted 7 lines

What about making it so we NEVER compare json-nft at all?

Instead, feed the json-nft file to nft, then do a normal list-ruleset, then compare that vs. normal .nft file.

This avoids any and all formatting issues and also avoids breakage when the json-nft file is formatted differently.

We may hide problems because nft might inadvertently sanitize the input. Also, conversion from standard syntax to JSON may be symmetrically broken, so standard->JSON->standard won't detect the problem.

Quoted 2 lines

Eg. postprocessing via json_pp won't match what this patch above expects.

Python natively supports JSON. Converting stuff into comparable strings (which also look pretty when printed) is a simple matter of:

| import json
| 
| json.dumps(json.loads(<dump as string>), \
| 	   sort_keys = True, indent = 4, \
| 	   separators = (',', ': '))

We rely upon Python for the testsuite already, so I don't see why there's all the fuss. JSON dump create, load and compare have not been a problem in the 5 years tests/py does it.

Cheers, Phil

#17 lore raw
Quoted 25 lines · Phil Sutter
Quoted 24 lines · Florian Westphal
Quoted 23 lines · Florian Westphal
Quoted 21 lines · Thomas Haller
Quoted 9 lines · Florian Westphal
Quoted 7 lines · Thomas Haller

Hi Florian,

Quoted 2 lines · Florian Westphal

 .../packetpath/dumps/tcp_options.nft          | 14
+++++++

is there a reason not to also generate a .json-nft file?

Quoted 8 lines

Yes, I am not adding more one-line monsters.

I'll add one once there is a solution in place that has human readable
json dumps that don't fail validation because of identical but
differently formatted output.

What about the "[PATCH nft 0/2] pretty print .json-nft files" patches?

Quoted 8 lines
Quoted 3 lines
Quoted 3 lines

I'm fine with that. Phil? Pablo? This is re:

https://patchwork.ozlabs.org/project/netfilter-devel/patch/[email protected]/

What I don't like is that we'll still get these huge patches/mails if the dumps are converted. Those that remain are still hard to handle in
case of errors.

We can of course do a one-time commit to convert all .json-nft to multi-line. The patch makes an effort to not require re-generating the existing files unless necessary.

The only question is which one is preferable.

Regenerating all .json-nft files once, also makes the second patch slightly simpler (but not so much, that it would be an argument for doing that).

Quoted 25 lines
Quoted 10 lines

What about making it so we NEVER compare json-nft at all?

Instead, feed the json-nft file to nft, then do a normal list- ruleset,
then compare that vs. normal .nft file.

This avoids any and all formatting issues and also avoids breakage when
the json-nft file is formatted differently.

We may hide problems because nft might inadvertently sanitize the input.
Also, conversion from standard syntax to JSON may be symmetrically broken, so standard->JSON->standard won't detect the problem.

Quoted 2 lines

Eg. postprocessing via json_pp won't match what this patch above expects.

Python natively supports JSON. Converting stuff into comparable strings
(which also look pretty when printed) is a simple matter of:

Quoted 2 lines

import json

Quoted 8 lines
Quoted 3 lines
json.dumps(json.loads(<dump as string>), \
	   sort_keys = True, indent = 4, \
	   separators = (',', ': '))

We rely upon Python for the testsuite already, so I don't see why there's all the fuss. JSON dump create, load and compare have not been a
problem in the 5 years tests/py does it.

The current patches intentionally don't try to sort keys. I claim, it's also not necessary to sort them. I can easily be convinced otherwise, by showing a counter-example/reproducer.

There is no fuzz, aside you and Florian bringing this topic up.

Thomas