netfilter: nf_tables: Fix for deleting base chains with payload
Subsystems (2)
Phil Sutter
reviewerPATCH 1/1 netfilter: nf_tables: Fix for deleting base chains with payload
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the NFTA_CHAIN_HOOK attribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself.
Detect the situation by checking if the chain's hook list becomes empty after removing all submitted hooks from it. A base chain without hooks is pointless, so revert back to deleting the chain.
Note the 'goto err_chain_del_hook', error path takes care of undoing the hook_list modification and releasing the unused chain_hook.
--- net/netfilter/nf_tables_api.c | 5 +++++ 1 file changed, 5 insertions(+)
net/netfilter/nf_tables_api.c +5 -0
@@ -2699,6 +2699,11 @@ static int nft_delchain_hook(struct nft_ctx *ctx, struct nft_chain *chain,list_move (&hook->list, &chain_del_list); } +if (list_empty (&basechain->hook_list)) { + err =nft_delchain (ctx); + goto err_chain_del_hook; + } + trans =nft_trans_alloc (ctx,NFT_MSG_DELCHAIN ,sizeof (struct nft_trans_chain ));if (!trans) {
Signature · 1 line
2.40.0
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 11 lines · Phil Sutter
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the
NFTA_CHAIN_HOOKattribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself.Detect the situation by checking if the chain's hook list becomes empty after removing all submitted hooks from it. A base chain without hooks is pointless, so revert back to deleting the chain.
Note the 'goto
err_chain_del_hook', error path takes care of undoing thehook_listmodification and releasing the unusedchain_hook.
Could you give a try to this alternative patch?
Thanks.
net/netfilter/nf_tables_api.c +9 -7
@@ -2811,21 +2811,18 @@ static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,return nf_tables_addchain (&ctx, family, genmask, policy, flags, extack); } -static int nft_delchain_hook (struct nft_ctx *ctx,struct nft_chain *chain, +static int nft_delchain_hook (struct nft_ctx *ctx, +struct nft_base_chain *basechain,struct netlink_ext_ack *extack) { +const struct nft_chain *chain = &basechain->chain;const struct nlattr *const *nla = ctx->nla;struct nft_chain_hook chain_hook = {}; -struct nft_base_chain *basechain;struct nft_hook *this, *hook;LIST_HEAD (chain_del_list);struct nft_trans *trans;int err; -if (!nft_is_base_chain (chain)) -return -EOPNOTSUPP ; - - basechain =nft_base_chain (chain); err =nft_chain_parse_hook (ctx->net, basechain, nla, &chain_hook, ctx->family, chain->flags, extack);if (err <0 )
@@ -2915,7 +2912,12 @@ static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,if (chain->flags &NFT_CHAIN_HW_OFFLOAD )return -EOPNOTSUPP ; -return nft_delchain_hook (&ctx, chain, extack); +if (nft_is_base_chain (chain)) { +struct nft_base_chain *basechain =nft_base_chain (chain); + +if (nft_base_chain_netdev (table->family, basechain->ops.hooknum)) +return nft_delchain_hook (&ctx, basechain, extack); + } } if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
Pablo Neira Ayuso
maintainerQuoted 8 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 5 lines · Phil Sutter
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the
NFTA_CHAIN_HOOKattribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself.
Quoted 8 lines
Quoted 6 lines
Detect the situation by checking if the chain's hook list becomes empty after removing all submitted hooks from it. A base chain without hooks is pointless, so revert back to deleting the chain.
Note the 'goto
err_chain_del_hook', error path takes care of undoing thehook_listmodification and releasing the unusedchain_hook.Could you give a try to this alternative patch?
This is the full patch.
From 6d09ac39a91bffb91d6cdc08b97c03fc4f23594e Mon Sep 17 00:00:00 2001
From: Phil Sutter <[email protected]>
Date: Fri, 16 Jun 2023 17:56:11 +0200
Subject: [PATCH nf] netfilter: nf_tables: Fix for deleting base chains with
payload
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the NFTA_CHAIN_HOOK attribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself.
Detect the situation by checking if the chain's hook list becomes empty after removing all submitted hooks from it. A base chain without hooks is pointless, so revert back to deleting the chain.
Note the 'goto err_chain_del_hook', error path takes care of undoing the hook_list modification and releasing the unused chain_hook.
--- net/netfilter/nf_tables_api.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +9 -7
@@ -2811,21 +2811,18 @@ static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,return nf_tables_addchain (&ctx, family, genmask, policy, flags, extack); } -static int nft_delchain_hook (struct nft_ctx *ctx,struct nft_chain *chain, +static int nft_delchain_hook (struct nft_ctx *ctx, +struct nft_base_chain *basechain,struct netlink_ext_ack *extack) { +const struct nft_chain *chain = &basechain->chain;const struct nlattr *const *nla = ctx->nla;struct nft_chain_hook chain_hook = {}; -struct nft_base_chain *basechain;struct nft_hook *this, *hook;LIST_HEAD (chain_del_list);struct nft_trans *trans;int err; -if (!nft_is_base_chain (chain)) -return -EOPNOTSUPP ; - - basechain =nft_base_chain (chain); err =nft_chain_parse_hook (ctx->net, basechain, nla, &chain_hook, ctx->family, chain->flags, extack);if (err <0 )
@@ -2910,7 +2907,12 @@ static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,if (chain->flags &NFT_CHAIN_HW_OFFLOAD )return -EOPNOTSUPP ; -return nft_delchain_hook (&ctx, chain, extack); +if (nft_is_base_chain (chain)) { +struct nft_base_chain *basechain =nft_base_chain (chain); + +if (nft_base_chain_netdev (table->family, basechain->ops.hooknum)) +return nft_delchain_hook (&ctx, basechain, extack); + } } if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerQuoted 13 lines · Pablo Neira Ayuso
Quoted 12 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 9 lines · Phil Sutter
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the
NFTA_CHAIN_HOOKattribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself.Detect the situation by checking if the chain's hook list becomes empty after removing all submitted hooks from it. A base chain without hooks is pointless, so revert back to deleting the chain.
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Note the 'goto
err_chain_del_hook', error path takes care of undoing thehook_listmodification and releasing the unusedchain_hook.Could you give a try to this alternative patch?
This is the full patch.
I forgot to mangle the patch description describing the new approach.
From 3da13f15a02e065e12080f2d66f81289aa6ebd69 Mon Sep 17 00:00:00 2001
From: Phil Sutter <[email protected]>
Date: Fri, 16 Jun 2023 17:56:11 +0200
Subject: [PATCH] netfilter: nf_tables: Fix for deleting base chains with
payload
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the NFTA_CHAIN_HOOK attribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself. Detect the situation by checking if the chain is either the netdev or inet/ingress type.
--- net/netfilter/nf_tables_api.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
net/netfilter/nf_tables_api.c +9 -7
@@ -2811,21 +2811,18 @@ static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,return nf_tables_addchain (&ctx, family, genmask, policy, flags, extack); } -static int nft_delchain_hook (struct nft_ctx *ctx,struct nft_chain *chain, +static int nft_delchain_hook (struct nft_ctx *ctx, +struct nft_base_chain *basechain,struct netlink_ext_ack *extack) { +const struct nft_chain *chain = &basechain->chain;const struct nlattr *const *nla = ctx->nla;struct nft_chain_hook chain_hook = {}; -struct nft_base_chain *basechain;struct nft_hook *this, *hook;LIST_HEAD (chain_del_list);struct nft_trans *trans;int err; -if (!nft_is_base_chain (chain)) -return -EOPNOTSUPP ; - - basechain =nft_base_chain (chain); err =nft_chain_parse_hook (ctx->net, basechain, nla, &chain_hook, ctx->family, chain->flags, extack);if (err <0 )
@@ -2910,7 +2907,12 @@ static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,if (chain->flags &NFT_CHAIN_HW_OFFLOAD )return -EOPNOTSUPP ; -return nft_delchain_hook (&ctx, chain, extack); +if (nft_is_base_chain (chain)) { +struct nft_base_chain *basechain =nft_base_chain (chain); + +if (nft_base_chain_netdev (table->family, basechain->ops.hooknum)) +return nft_delchain_hook (&ctx, basechain, extack); + } } if (info->nlh->nlmsg_flags & NLM_F_NONREC &&
Signature · 1 line
2.30.2
Phil Sutter
reviewerQuoted 10 lines · Pablo Neira Ayuso
Quoted 9 lines · Pablo Neira Ayuso
Quoted 8 lines · Pablo Neira Ayuso
Hi Phil,
Quoted 5 lines · Phil Sutter
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the
NFTA_CHAIN_HOOKattribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines
Detect the situation by checking if the chain's hook list becomes empty after removing all submitted hooks from it. A base chain without hooks is pointless, so revert back to deleting the chain.
Note the 'goto
err_chain_del_hook', error path takes care of undoing thehook_listmodification and releasing the unusedchain_hook.Could you give a try to this alternative patch?
Ah, I guess my fix breaks temporary removal of a single interface from a netdev chain?
Quoted 1 line
Quoted 1 line
This is the full patch.
Works fine, iptables testsuite is back to normal again. :)
Quoted 8 lines
I forgot to mangle the patch description describing the new approach.
From
3da13f15a02e065e12080f2d66f81289aa6ebd69Mon Sep 17 00:00:00 2001
From: Phil Sutter <[email protected]>
Date: Fri, 16 Jun 2023 17:56:11 +0200
Subject: [PATCH] netfilter:nf_tables: Fix for deleting base chains with
payload
Quoted 5 lines
When deleting a base chain, iptables-nft simply submits the whole chain to the kernel, including the
NFTA_CHAIN_HOOKattribute. The new code added by fixed commit then turned this into a chain update, destroying the hook but not the chain itself. Detect the situation by checking if the chain is either the netdev or inet/ingress type.
I'd change the last sentence to "Avoid the situation by applying the new logic to base chains in netdev family or inet family ingress hook." But feel free to push as-is, I'm just nit-picking here. :)
Thanks, Phil