Fix for exclusive start cond corner cases
Phil Sutter
reviewercover Fix for exclusive start cond corner cases
Patch 2 fixes the issue xelra reported in [1], patch 1 fixes a related issue and patch 3 extends scanner debugging output a bit which helped when analyzing the bug.
Phil Sutter (3):
parser_bison: Fix for comment following exclusive start cond
scanner: Fix delayed start condition pop logic
scanner: Add start condition debugging
src/scanner.l | 21 +++++--- .../dumps/exclusive_start_cond.json-nft | 52 +++++++++++++++++++ .../parsing/dumps/exclusive_start_cond.nft | 2 + .../testcases/parsing/exclusive_start_cond | 6 +++ 4 files changed, 73 insertions(+), 8 deletions(-)
Signature · 1 line
2.54.0
Phil Sutter
reviewerPATCH 1/3 parser_bison: Fix for comment following exclusive start cond
Fixed commit missed "comment" as yet another global scope keyword which may immediately follow the limit statement, probably because rule comments may not appear at arbitrary positions but strictly at rule end. Therefore they are defined at an easily overlooked spot in parser_bison.y.
--- src/scanner.l | 2 +- .../dumps/exclusive_start_cond.json-nft | 18 ++++++++++++++++++ .../parsing/dumps/exclusive_start_cond.nft | 1 + .../testcases/parsing/exclusive_start_cond | 3 +++ 4 files changed, 23 insertions(+), 1 deletion(-)
src/scanner.l +1 -1
@@ -378,7 +378,7 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "position" { return POSITION; } "index" { return INDEX; } -"comment" { return COMMENT; } +<*>"comment" { return COMMENT; } "constant" { return CONSTANT; } "interval" { return INTERVAL; }
tests/shell/testcases/parsing/dumps/exclusive_start_cond.json-nft +18 -0
@@ -2003,6 +2003,24 @@ ] } }, + { + "rule": { + "family": "ip", + "table": "t", + "chain": "c", + "handle": 0, + "comment": "foobar", + "expr": [ + { + "limit": { + "rate": 1, + "burst": 5, + "per": "second" + } + } + ] + } + }, { "rule": { "family": "ip",
tests/shell/testcases/parsing/dumps/exclusive_start_cond.nft +1 -0
@@ -83,6 +83,7 @@ table ip t { limit rate 1/second burst 5 packets fib daddr . iif check exists limit rate 1/second burst 5 packets osf name "foo" limit rate 1/second burst 5 packets ipsec in spi 0 + limit rate 1/second burst 5 packets comment "foobar" meta l4proto 6 limit rate 1/second burst 5 packets tproxy to 0.0.0.1 meta l4proto 17 limit rate 1/second burst 5 packets vxlan vni 0 meta l4proto 17 limit rate 1/second burst 5 packets geneve vni 0
tests/shell/testcases/parsing/exclusive_start_cond +3 -0
@@ -95,6 +95,9 @@ stmts+=("osf name foo") # xfrm_expr stmts+=("ipsec in spi 0") +# comment_spec +stmts+=("comment foobar") + $NFT -f - <<EOF table t { flowtable ft {
Signature · 1 line
2.54.0
Phil Sutter
reviewerPATCH 2/3 scanner: Fix delayed start condition pop logic
In reply to Phil SutterIf scanner_pop_start_cond() is called and the passed start condition is currently active, it is supposed to pop it and all delayed states identified by a zero field value in state->startcond_active. The counter state->flex_state_pop reflects the number of delayed start conditions waiting to be popped.
The implementation mixed up popping of the currently active state with delayed ones though and therefore decremented flex_state_pop for the currently active (i.e., not delayed) state as well. As a consequence, the first delayed state remained active after the function returned, causing problems in corner-cases.
--- src/scanner.l | 11 +++--- .../dumps/exclusive_start_cond.json-nft | 34 +++++++++++++++++++ .../parsing/dumps/exclusive_start_cond.nft | 1 + .../testcases/parsing/exclusive_start_cond | 3 ++ 4 files changed, 42 insertions(+), 7 deletions(-)
src/scanner.l +4 -7
@@ -1404,17 +1404,14 @@ void scanner_pop_start_cond(void *scanner, enum startcond_type t) return; /* Can't pop just yet! */ } - while (state->flex_state_pop) { + t = state->startcond_type = yy_top_state(scanner); + yy_pop_state(scanner); + + while (state->flex_state_pop && !state->startcond_active[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); - - yy_pop_state(scanner); }
tests/shell/testcases/parsing/dumps/exclusive_start_cond.json-nft +34 -0
@@ -2021,6 +2021,40 @@ ] } }, + { + "rule": { + "family": "ip", + "table": "t", + "chain": "c", + "handle": 0, + "comment": "x", + "expr": [ + { + "match": { + "op": "==", + "left": { + "meta": { + "key": "l4proto" + } + }, + "right": 6 + } + }, + { + "limit": { + "rate": 1, + "burst": 5, + "per": "second" + } + }, + { + "reject": { + "type": "tcp reset" + } + } + ] + } + }, { "rule": { "family": "ip",
tests/shell/testcases/parsing/dumps/exclusive_start_cond.nft +1 -0
@@ -84,6 +84,7 @@ table ip t { limit rate 1/second burst 5 packets osf name "foo" limit rate 1/second burst 5 packets ipsec in spi 0 limit rate 1/second burst 5 packets comment "foobar" + meta l4proto 6 limit rate 1/second burst 5 packets reject with tcp reset comment "x" meta l4proto 6 limit rate 1/second burst 5 packets tproxy to 0.0.0.1 meta l4proto 17 limit rate 1/second burst 5 packets vxlan vni 0 meta l4proto 17 limit rate 1/second burst 5 packets geneve vni 0
tests/shell/testcases/parsing/exclusive_start_cond +3 -0
@@ -98,6 +98,9 @@ stmts+=("ipsec in spi 0") # comment_spec stmts+=("comment foobar") +# this revealed a bug in scanner_pop_start_cond() code +stmts+=("reject with tcp reset comment x") + $NFT -f - <<EOF table t { flowtable ft {
Signature · 1 line
2.54.0
Florian Westphal
maintainerRe: [nft PATCH 2/3] scanner: Fix delayed start condition pop logic
Quoted 11 lines · Phil Sutter
If
scanner_pop_start_cond()is called and the passed start condition is currently active, it is supposed to pop it and all delayed states identified by a zero field value instate->startcond_active. The counterstate->flex_state_popreflects the number of delayed start conditions waiting to be popped.The implementation mixed up popping of the currently active state with delayed ones though and therefore decremented
flex_state_popfor the currently active (i.e., not delayed) state as well. As a consequence, the first delayed state remained active after the function returned, causing problems in corner-cases.
Right, it doesn't matter for inclusive states.
Thanks for fixing this up.
Phil Sutter
reviewerPATCH 3/3 scanner: Add start condition debugging
In reply to Phil SutterReport start condition push/pop actions when called with --debug=scanner.
--- src/scanner.l | 8 ++++++++ 1 file changed, 8 insertions(+)
src/scanner.l +8 -0
@@ -1383,10 +1383,13 @@ void scanner_destroy(struct nft_ctx *nft) yylex_destroy(nft->scanner); } +#define sc_dbg(...) if (nft_get_debug(scanner)) fprintf(stderr, __VA_ARGS__) + static void scanner_push_start_cond(void *scanner, enum startcond_type type) { struct parser_state *state = yyget_extra(scanner); + sc_dbg("--pushing start condition %d\n", type); state->startcond_type = type; state->startcond_active[type]++;
@@ -1401,14 +1404,19 @@ void scanner_pop_start_cond(void *scanner, enum startcond_type t) if (state->startcond_type != t) { state->flex_state_pop++; + sc_dbg("--delay popping start condition %d\n", t); return; /* Can't pop just yet! */ } + sc_dbg("--popping start condition %d\n", state->startcond_type); + t = state->startcond_type = yy_top_state(scanner); yy_pop_state(scanner); while (state->flex_state_pop && !state->startcond_active[t]) { state->flex_state_pop--; + sc_dbg("--popping delayed start condition %d\n", + state->startcond_type); state->startcond_type = yy_top_state(scanner); yy_pop_state(scanner);
Signature · 1 line
2.54.0
Phil Sutter
reviewerQuoted 10 lines · Phil Sutter
Patch 2 fixes the issue xelra reported in [1], patch 1 fixes a related issue and patch 3 extends scanner debugging output a bit which helped when analyzing the bug.
Phil Sutter (3):
parser_bison: Fix for comment following exclusive start cond
scanner: Fix delayed start condition pop logic
scanner: Add start condition debugging
Series applied.