Skip to contentSkip to conversation

parser: fix scope closing with > 1 nested scope

PATCH Applied Florian Westphal 1 participant 5 messages Jun 24, 2022 – Jun 27, 2022 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

cover parser: fix scope closing with > 1 nested scope

This resolves the parsing problem reported by Phil. First patch is his test case, second patch is an (unrelated) fix for SYNPROXY scoping (lack of close).

Last patch fixes closing with nested scopes, where we exited a scope too early.

Florian Westphal (2):
parser: add missing synproxy scope closure
scanner: don't pop active flex scanner scope

Phil Sutter (1):
tests/py: Add a test for failing ipsec after counter

 include/parser.h              |  3 +++
 src/parser_bison.y            |  2 +-
 src/scanner.l                 | 11 +++++++++++
 tests/py/inet/ipsec.t         |  2 ++
 tests/py/inet/ipsec.t.json    | 21 +++++++++++++++++++++
 tests/py/inet/ipsec.t.payload |  6 ++++++
 6 files changed, 44 insertions(+), 1 deletion(-)
Signature · 1 line
2.35.1
author maintainer
#1 lore raw

PATCH 1/3 tests/py: Add a test for failing ipsec after counter

From: Phil Sutter <[email protected]>

This is a bug in parser/scanner due to scoping:

| Error: syntax error, unexpected string, expecting saddr or daddr
| add rule ip ipsec-ip4 ipsec-forw counter ipsec out ip daddr 192.168.1.2
|                                                       ^^^^^
---
 tests/py/inet/ipsec.t         |  2 ++
 tests/py/inet/ipsec.t.json    | 21 +++++++++++++++++++++
 tests/py/inet/ipsec.t.payload |  6 ++++++
 3 files changed, 29 insertions(+)
tests/py/inet/ipsec.t +2 -0
index e924e9bcbdbc..b18df395de6c 100644
@@ -19,3 +19,5 @@ ipsec in ip6 daddr dead::beef;ok
 ipsec out ip6 saddr dead::feed;ok
 
 ipsec in spnum 256 reqid 1;fail
+
+counter ipsec out ip daddr 192.168.1.2;ok
tests/py/inet/ipsec.t.json +21 -0
index d7d3a03c2113..18a64f3533b3 100644
@@ -134,3 +134,24 @@
         }
     }
 ]
+
+# counter ipsec out ip daddr 192.168.1.2
+[
+    {
+        "counter": null
+    },
+    {
+        "match": {
+            "left": {
+                "ipsec": {
+                    "dir": "out",
+                    "family": "ip",
+                    "key": "daddr",
+                    "spnum": 0
+                }
+            },
+            "op": "==",
+            "right": "192.168.1.2"
+        }
+    }
+]
tests/py/inet/ipsec.t.payload +6 -0
index c46a2263f6c0..9648255df02e 100644
@@ -37,3 +37,9 @@ ip ipsec-ip4 ipsec-forw
   [ xfrm load out 0 saddr6 => reg 1 ]
   [ cmp eq reg 1 0x0000adde 0x00000000 0x00000000 0xedfe0000 ]
 
+# counter ipsec out ip daddr 192.168.1.2
+ip ipsec-ip4 ipsec-forw
+  [ counter pkts 0 bytes 0 ]
+  [ xfrm load out 0 daddr4 => reg 1 ]
+  [ cmp eq reg 1 0x0201a8c0 ]
+
Signature · 1 line
2.35.1
author maintainer
#2 lore raw

PATCH 2/3 parser: add missing synproxy scope closure

In reply to Florian Westphal
Fixes 232f2c3287fc ("scanner: synproxy: Move to own scope")
---
 src/parser_bison.y | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/parser_bison.y +1 -1
index 2a0240fb9862..73a2702dcec1 100644
@@ -2015,7 +2015,7 @@ map_block_obj_type	:	COUNTER	close_scope_counter { $$ = NFT_OBJECT_COUNTER; }
 			|	QUOTA	close_scope_quota { $$ = NFT_OBJECT_QUOTA; }
 			|	LIMIT	close_scope_limit { $$ = NFT_OBJECT_LIMIT; }
 			|	SECMARK close_scope_secmark { $$ = NFT_OBJECT_SECMARK; }
-			|	SYNPROXY { $$ = NFT_OBJECT_SYNPROXY; }
+			|	SYNPROXY close_scope_synproxy { $$ = NFT_OBJECT_SYNPROXY; }
 			;
 
 map_block		:	/* empty */	{ $$ = $<set>-1; }
Signature · 1 line
2.35.1
author maintainer
#3 lore raw

PATCH 3/3 scanner: don't pop active flex scanner scope

In reply to Florian Westphal

Currently we can pop a flex scope that is still active, i.e. the scanner_pop_start_cond() for the scope has not been done.

Example:
counter ipsec out ip daddr 192.168.1.2 counter name "ipsec_out"

Here, parser fails because 'daddr' is parsed as STRING, not as DADDR token.

Bug is as follows:
COUNTER changes scope to COUNTER. (COUNTER). Next, IPSEC scope gets pushed, stack is: COUNTER, IPSEC.

Then, the 'COUNTER' scope close happens. Because active scope has changed, we cannot pop (we would pop the 'ipsec' scope in flex).
The pop operation gets delayed accordingly.

Next, IP gets pushed, stack is: COUNTER, IPSEC, IP, plus the information that one scope closure/pop was delayed.

Then, the IP scope is closed. Because a pop operation was delayed, we pop again, which brings us back to COUNTER state.

This is bogus: The pop operation CANNOT be done yet, because the ipsec scope is still open, but the existing code lacks the information to detect this.

After popping the IP scope, we must remain in IPSEC scope until bison parser calls scanner_pop_start_cond(, IPSEC).

This adds a counter per flex scope so that we can detect this case. In above case, after the IP scope gets closed, the "new" (previous) scope (IPSEC) will be treated as active and its close is attempted again on the next call to scanner_pop_start_cond().

After this patch, transition in above rule is:

push counter (COUNTER)
push IPSEC (COUNTER, IPSEC)
pop COUNTER (delayed: COUNTER, IPSEC, pending-pop for COUNTER), push IP (COUNTER, IPSEC, IP, pending-pop for COUNTER) pop IP (COUNTER, IPSEC, pending-pop for COUNTER) parse DADDR (we're in IPSEC scope, its valid token) pop IPSEC (pops all remaining scopes).

We could also resurrect the commit:
"scanner: flags: move to own scope", the test case passes with the new scope closure logic.

Fixes bff106c5b277 ("scanner: add support for scope nesting")
---
 include/parser.h |  3 +++
 src/scanner.l    | 11 +++++++++++
 2 files changed, 14 insertions(+)
include/parser.h +3 -0
index d8d2eb115922..2fb037cb8470 100644
@@ -26,6 +26,7 @@ struct parser_state {
 	unsigned int			flex_state_pop;
 	unsigned int			startcond_type;
 	struct list_head		*cmds;
+	unsigned int			*startcond_active;
 };
 
 enum startcond_type {
@@ -81,6 +82,8 @@ enum startcond_type {
 	PARSER_SC_STMT_REJECT,
 	PARSER_SC_STMT_SYNPROXY,
 	PARSER_SC_STMT_TPROXY,
+
+	__SC_MAX
 };
 
 struct mnl_socket;
src/scanner.l +11 -0
index 7eb74020ef84..5741261a690a 100644
@@ -1144,6 +1144,8 @@ void *scanner_init(struct parser_state *state)
 	yylex_init_extra(state, &scanner);
 	yyset_out(NULL, scanner);
 
+	state->startcond_active = xzalloc_array(__SC_MAX,
+						sizeof(*state->startcond_active));
 	return scanner;
 }
 
@@ -1173,6 +1175,8 @@ void scanner_destroy(struct nft_ctx *nft)
 	struct parser_state *state = yyget_extra(nft->scanner);
 
 	input_descriptor_list_destroy(state);
+	xfree(state->startcond_active);
+
 	yylex_destroy(nft->scanner);
 }
 
@@ -1181,6 +1185,7 @@ static void scanner_push_start_cond(void *scanner, enum startcond_type type)
 	struct parser_state *state = yyget_extra(scanner);
 
 	state->startcond_type = type;
+	state->startcond_active[type]++;
 
 	yy_push_state((int)type, scanner);
 }
@@ -1189,6 +1194,8 @@ void scanner_pop_start_cond(void *scanner, enum startcond_type t)
 {
 	struct parser_state *state = yyget_extra(scanner);
 
+	state->startcond_active[t]--;
+
 	if (state->startcond_type != t) {
 		state->flex_state_pop++;
 		return; /* Can't pop just yet! */
@@ -1198,6 +1205,10 @@ void scanner_pop_start_cond(void *scanner, enum startcond_type t)
 		state->flex_state_pop--;
 		state->startcond_type = yy_top_state(scanner);
 		yy_pop_state(scanner);
+
+		t = state->startcond_type;
+		if (state->startcond_active[t])
+			return;
 	}
 
 	state->startcond_type = yy_top_state(scanner);
Signature · 1 line
2.35.1
author maintainer Applied
#4 lore raw
In reply to Florian Westphal
Quoted 7 lines · Florian Westphal

This resolves the parsing problem reported by Phil. First patch is his test case, second patch is an (unrelated) fix for SYNPROXY scoping (lack of close).

Last patch fixes closing with nested scopes, where we exited a scope too early.

Quoted 6 lines

Florian Westphal (2):
parser: add missing synproxy scope closure
scanner: don't pop active flex scanner scope

Phil Sutter (1):
tests/py: Add a test for failing ipsec after counter

Pushed to nftables.git