]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
rule: fix sym refcount assertion
authorFlorian Westphal <fw@strlen.de>
Mon, 15 Jan 2024 13:27:15 +0000 (14:27 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Wed, 22 Jan 2025 23:41:54 +0000 (00:41 +0100)
commit b73298405cda74b3a87a1818bb92f53298d34170 upstream.

Scope release must happen last.
afl provided a reproducer where policy is a define, because
scope is released too early we get:
nft: src/rule.c:559: scope_release: Assertion `sym->refcnt == 1' failed.

... because chain->policy is EXPR_SYMBOL.

Fixes: 627c451b2351 ("src: allow variables in the chain priority specification")
Signed-off-by: Florian Westphal <fw@strlen.de>
src/rule.c
tests/shell/testcases/bogons/nft-f/define_policy_assert [new file with mode: 0644]

index 56066387c51943e7303363ed7612c189b886ce21..9917359b1df53abf73e4b99c81e8b3d775c5cf0e 100644 (file)
@@ -727,7 +727,6 @@ void chain_free(struct chain *chain)
        list_for_each_entry_safe(rule, next, &chain->rules, list)
                rule_free(rule);
        handle_free(&chain->handle);
-       scope_release(&chain->scope);
        xfree(chain->type.str);
        expr_free(chain->dev_expr);
        for (i = 0; i < chain->dev_array_len; i++)
@@ -736,6 +735,11 @@ void chain_free(struct chain *chain)
        expr_free(chain->priority.expr);
        expr_free(chain->policy);
        xfree(chain->comment);
+
+       /* MUST be released after all expressions, they could
+        * hold refcounts.
+        */
+       scope_release(&chain->scope);
        xfree(chain);
 }
 
diff --git a/tests/shell/testcases/bogons/nft-f/define_policy_assert b/tests/shell/testcases/bogons/nft-f/define_policy_assert
new file mode 100644 (file)
index 0000000..f1e58b5
--- /dev/null
@@ -0,0 +1,3 @@
+chain y x { priority filter
+define p = foo
+policy $p