nftables: don't crash in 'list ruleset' if policy is not set
PATCH 1/1 nftables: don't crash in 'list ruleset' if policy is not set
Minimal reproducer:
```
$ cat nft.ruleset
# filters
table inet filter {
chain prerouting {
type filter hook prerouting priority -50
}
}
# dump new state
list ruleset
$ nft -c -f ./nft.ruleset
table inet filter {
chain prerouting {
Segmentation fault (core dumped)
```The crash happens in chain_print_declaration():
```
if (chain->flags & CHAIN_F_BASECHAIN) {
mpz_export_data(&policy, chain->policy->value,
BYTEORDER_HOST_ENDIAN, sizeof(int));
```Here chain->policy is NULL (as textual rule does not mention it).
The change is not to print the policy if it's not set (similar to chain_evaluate() handling).
--- src/rule.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
src/rule.c +9 -5
@@ -1107,17 +1107,21 @@ static void chain_print_declaration(const struct chain *chain,nft_print (octx," # handle %" PRIu64, chain->handle.handle.id);nft_print (octx,"\n" );if (chain->flags &CHAIN_F_BASECHAIN ) { -mpz_export_data (&policy, chain->policy->value, -BYTEORDER_HOST_ENDIAN ,sizeof (int ));nft_print (octx,"\t\ttype %s hook %s" , chain->type,hooknum2str (chain->handle.family, chain->hooknum));if (chain->dev !=NULL )nft_print (octx," device \"%s\"" , chain->dev); -nft_print (octx," priority %s; policy %s;\n" , +nft_print (octx," priority %s;" ,prio2str (octx, priobuf,sizeof (priobuf), chain->handle.family, chain->hooknum, - chain->priority.expr), -chain_policy2str (policy)); + chain->priority.expr)); +if (chain->policy) { +mpz_export_data (&policy, chain->policy->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); +nft_print (octx," policy %s;" , +chain_policy2str (policy)); + } +nft_print (octx,"\n" ); } }
Signature · 1 line
2.23.0
Hi Sergei,
Quoted 28 lines · Sergei Trofimovich
Minimal reproducer:
``` $ cat nft.ruleset # filters table inet filter { chain prerouting { type filter hook prerouting priority -50 } } # dump new state list ruleset $ nft -c -f ./nft.ruleset table inet filter { chain prerouting { Segmentation fault (core dumped) ```The crash happens in
chain_print_declaration():``` if (chain->flags & CHAIN_F_BASECHAIN) { mpz_export_data(&policy, chain->policy->value, BYTEORDER_HOST_ENDIAN, sizeof(int)); ```
Quoted 4 lines
Here
chain->policyisNULL(as textual rule does not mention it).The change is not to print the policy if it's not set (similar to
chain_evaluate()handling).
Thanks for fixing that. Sorry I missed that we could have a base chain without policy.
Quoted 41 lines
CC Florian Westphal <[email protected]>CC Pablo Neira Ayuso <[email protected]>Signed-off-by Sergei Trofimovich <[email protected]>--- src/rule.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)src/rule.c +9
-5@@ -1107,17 +1107,21 @@ static void chain_print_declaration(const struct chain *chain,nft_print (octx," # handle %" PRIu64, chain->handle.handle.id);nft_print (octx,"\n" );if (chain->flags &CHAIN_F_BASECHAIN ) { -mpz_export_data (&policy, chain->policy->value, -BYTEORDER_HOST_ENDIAN ,sizeof (int ));nft_print (octx,"\t\ttype %s hook %s" , chain->type,hooknum2str (chain->handle.family, chain->hooknum));if (chain->dev !=NULL )nft_print (octx," device \"%s\"" , chain->dev); -nft_print (octx," priority %s; policy %s;\n" , +nft_print (octx," priority %s;" ,prio2str (octx, priobuf,sizeof (priobuf), chain->handle.family, chain->hooknum, - chain->priority.expr), -chain_policy2str (policy)); + chain->priority.expr)); +if (chain->policy) { +mpz_export_data (&policy, chain->policy->value, +BYTEORDER_HOST_ENDIAN ,sizeof (int )); +nft_print (octx," policy %s;" , +chain_policy2str (policy)); + } +nft_print (octx,"\n" ); } }
Quoted 33 lines · Fernando Fernandez Mancera
Hi Sergei,
Quoted 30 lines · Sergei Trofimovich
Minimal reproducer:
``` $ cat nft.ruleset # filters table inet filter { chain prerouting { type filter hook prerouting priority -50 } } # dump new state list ruleset $ nft -c -f ./nft.ruleset table inet filter { chain prerouting { Segmentation fault (core dumped) ```The crash happens in
chain_print_declaration():``` if (chain->flags & CHAIN_F_BASECHAIN) { mpz_export_data(&policy, chain->policy->value, BYTEORDER_HOST_ENDIAN, sizeof(int)); ```Here
chain->policyisNULL(as textual rule does not mention it).
Quoted 7 lines
Quoted 2 lines
The change is not to print the policy if it's not set (similar to
chain_evaluate()handling).Thanks for fixing that. Sorry I missed that we could have a base chain without policy.
Applied, thanks.