src: ensure chain policy evaluation when specified
Pablo Neira Ayuso
maintainerPATCH 1/1 src: ensure chain policy evaluation when specified
Set on CHAIN_F_BASECHAIN when policy is specified in chain, otherwise chain priority is not evaluated.
Toggling this flag requires needs two adjustments to work though:
1) chain_evaluate() needs skip evaluation of hook name and priority if not specified to allow for updating the default chain policy, e.g.
chain ip x y { policy accept; }2) update netlink bytecode generation for chain to skip NFTA_CHAIN_HOOK so update path is exercised in the kernel.
Fixes acdfae9c3126 ("src: allow to specify the default policy for base chains")
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- src/evaluate.c | 27 ++++++++++--------- src/mnl.c | 8 ++++-- src/parser_bison.y | 1 + .../bogons/nft-f/basechain_bad_policy | 2 ++ 4 files changed, 24 insertions(+), 14 deletions(-) create mode 100644 tests/shell/testcases/bogons/nft-f/basechain_bad_policy
src/evaluate.c +15 -12
@@ -5772,18 +5772,21 @@ static int chain_evaluate(struct eval_ctx *ctx, struct chain *chain) }if (chain->flags &CHAIN_F_BASECHAIN ) { - chain->hook.num =str2hooknum (chain->handle.family, - chain->hook.name); -if (chain->hook.num ==NF_INET_NUMHOOKS ) -return __stmt_binary_error (ctx, &chain->hook.loc,NULL , -"The %s family does not support this hook" , -family2str (chain->handle.family)); - -if (!evaluate_priority (ctx, &chain->priority, - chain->handle.family, chain->hook.num)) -return __stmt_binary_error (ctx, &chain->priority.loc,NULL , -"invalid priority expression %s in this context." , -expr_name (chain->priority.expr)); +if (chain->hook.name) { + chain->hook.num =str2hooknum (chain->handle.family, + chain->hook.name); +if (chain->hook.num ==NF_INET_NUMHOOKS ) +return __stmt_binary_error (ctx, &chain->hook.loc,NULL , +"The %s family does not support this hook" , +family2str (chain->handle.family)); + } +if (chain->priority.expr) { +if (!evaluate_priority (ctx, &chain->priority, + chain->handle.family, chain->hook.num)) +return __stmt_binary_error (ctx, &chain->priority.loc,NULL , +"invalid priority expression %s in this context." , +expr_name (chain->priority.expr)); + }if (chain->policy) {expr_set_context (&ctx->ectx, &policy_type,NFT_NAME_MAXLEN *BITS_PER_BYTE );
src/mnl.c +6 -2
@@ -897,7 +897,9 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, struct cmd *cmd,mnl_attr_put_strz (nlh,NFTA_CHAIN_TYPE , cmd->chain->type.str); } - nest =mnl_attr_nest_start (nlh,NFTA_CHAIN_HOOK ); +if (cmd->chain->type.str || + (cmd->chain && cmd->chain->dev_expr)) + nest =mnl_attr_nest_start (nlh,NFTA_CHAIN_HOOK );if (cmd->chain->type.str) {mnl_attr_put_u32 (nlh,NFTA_HOOK_HOOKNUM ,htonl (cmd->chain->hook.num));
@@ -909,7 +911,9 @@ int mnl_nft_chain_add(struct netlink_ctx *ctx, struct cmd *cmd,if (cmd->chain && cmd->chain->dev_expr)mnl_nft_chain_devs_build (nlh, cmd); -mnl_attr_nest_end (nlh, nest); +if (cmd->chain->type.str || + (cmd->chain && cmd->chain->dev_expr)) +mnl_attr_nest_end (nlh, nest); }nftnl_chain_free (nlc);
src/parser_bison.y +1 -0
@@ -2834,6 +2834,7 @@ policy_spec : POLICY policy_expr close_scope_policy } $<chain>0->policy = $2; $<chain>0->policy->location = @$; + $<chain>0->flags |= CHAIN_F_BASECHAIN; } ;
tests/shell/testcases/bogons/nft-f/basechain_bad_policy new file +2 -0
@@ -0,0 +1,2 @@ +define MY_POLICY = deny +table T { chain C { policy $MY_POLICY; };};
Signature · 1 line
2.30.2