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 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. Reported-by: xelra Fixes: 8623772af0610 ("scanner: don't pop active flex scanner scope") Signed-off-by: Phil Sutter --- 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(-) diff --git a/src/scanner.l b/src/scanner.l index b35dddd94eb4a..aa83d8590f5e1 100644 --- a/src/scanner.l +++ b/src/scanner.l @@ -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); } diff --git a/tests/shell/testcases/parsing/dumps/exclusive_start_cond.json-nft b/tests/shell/testcases/parsing/dumps/exclusive_start_cond.json-nft index 1de5007c6fc9d..092a5278cf708 100644 --- a/tests/shell/testcases/parsing/dumps/exclusive_start_cond.json-nft +++ b/tests/shell/testcases/parsing/dumps/exclusive_start_cond.json-nft @@ -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", diff --git a/tests/shell/testcases/parsing/dumps/exclusive_start_cond.nft b/tests/shell/testcases/parsing/dumps/exclusive_start_cond.nft index 1b9b369e67640..a1a6e44faf6ed 100644 --- a/tests/shell/testcases/parsing/dumps/exclusive_start_cond.nft +++ b/tests/shell/testcases/parsing/dumps/exclusive_start_cond.nft @@ -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 diff --git a/tests/shell/testcases/parsing/exclusive_start_cond b/tests/shell/testcases/parsing/exclusive_start_cond index 1fce6826f7cca..b4b34b23d428e 100755 --- a/tests/shell/testcases/parsing/exclusive_start_cond +++ b/tests/shell/testcases/parsing/exclusive_start_cond @@ -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 - <