netfilter: nfnetlink: Fix for interrupted hook dumps
Subsystems (2)
Phil Sutter
reviewerPATCH 1/1 netfilter: nfnetlink: Fix for interrupted hook dumps
Handling of concurrent hook changes with a dump in progress was problematic in nfnl_hook_dump and entirely broken in nfnl_hook_dump_nat. Address all issues in a single patch to please the review LLM.
Introduce sequence numbers in struct netns_nf to replace pointer value-based modification detection which may fail due to memory buffer reuse. This also eliminates the need for most manual cb->seq updates and excessive array index value checks.
Since nat hook updates are protected by a different mutex than others, they have to bump their own sequence number. nfnl_hook_dump_nat therefore open-codes what nl_dump_check_consistent does but bumps cb->seq instead of setting NLM_F_DUMP_INTR flag.
Dumps of many nat hooks was entirely broken since the array index was not (re)stored. Use cb->args[1] for that and make sure it is reset upon completion as the same dump may loop over multiple arrays of nat hooks.
In nfnl_hook_dump, don't signal NLM_F_DUMP_INTR if nfnl_hook_entries_head returns error: This is a permanent condition which does not change during a dump. It is already caught by nfnl_hook_dump_start though, so should not happen anyway.
In general, access ops array pointer values using READ_ONCE since they are assigned to using WRITE_ONCE.
Avoid setting NLM_F_DUMP_INTR flag in garbage memory by calling nl_dump_check_consistent only for non-empty skbs. If not a single netlink message was created, nfnetlink code will take care of setting the flag.
---
This patch replaces the previous series with subject "Address Sashiko review of NAT hook dump code" which addressed nf-next. Since the problematic commit b010e2a4a9ac is in Linus' git meanwhile, turn this into a stable update.
--- include/net/netns/netfilter.h | 2 + net/netfilter/core.c | 13 ++++++ net/netfilter/nf_nat_core.c | 14 ++++++- net/netfilter/nfnetlink_hook.c | 73 ++++++++++++++++++++-------------- 4 files changed, 71 insertions(+), 31 deletions(-)
include/net/netns/netfilter.h +2 -0
@@ -33,5 +33,7 @@ struct netns_nf {#if IS_ENABLED (CONFIG_NF_DEFRAG_IPV6 )unsigned int defrag_ipv6_users;#endif +unsigned int hook_base_seq; + unsigned int nat_hook_base_seq; };#endif
net/netfilter/core.c +13 -0
@@ -386,6 +386,15 @@ static void nf_static_key_dec(const struct nf_hook_ops *reg, int pf)#endif } +static void bump_hook_base_seq (struct net *net) +{ +unsigned int base_seq =READ_ONCE (net->nf.hook_base_seq); + +while (++base_seq ==0 ) + ; +smp_store_release (&net->nf.hook_base_seq, base_seq); +} +static int __nf_register_net_hook (struct net *net,int pf,const struct nf_hook_ops *reg) {
@@ -428,6 +437,7 @@ static int __nf_register_net_hook(struct net *net, int pf, new_hooks =nf_hook_entries_grow (p, reg);if (!IS_ERR (new_hooks)) { +bump_hook_base_seq (net);hooks_validate (new_hooks);rcu_assign_pointer (*pp, new_hooks); }
@@ -506,6 +516,7 @@ static void __nf_unregister_net_hook(struct net *net, int pf,net_dec_egress_queue ();#endif nf_static_key_dec (reg, pf); +bump_hook_base_seq (net); }else {WARN_ONCE (1 ,"hook not found, pf %d num %d" , pf, reg->hooknum); }
@@ -784,6 +795,8 @@ static int __net_init netfilter_net_init(struct net *net) return -ENOMEM; }#endif + net->nf.hook_base_seq =1 ; + net->nf.nat_hook_base_seq =1 ;return 0 ; }
net/netfilter/nf_nat_core.c +13 -1
@@ -1160,6 +1160,15 @@ nfnetlink_parse_nat_setup(struct nf_conn *ct, }#endif +static void bump_nat_hook_base_seq (struct net *net) +{ +unsigned int base_seq =READ_ONCE (net->nf.nat_hook_base_seq); + +while (++base_seq ==0 ) + ; +smp_store_release (&net->nf.nat_hook_base_seq, base_seq); +} +static struct nf_ct_helper_expectfn follow_master_nat = { .name ="nat-follow-master" , .expectfn = nf_nat_follow_master,
@@ -1245,8 +1254,10 @@ int nf_nat_register_fn(struct net *net, u8 pf, const struct nf_hook_ops *ops, } ret =nf_hook_entries_insert_raw (&priv->entries, ops); -if (ret ==0 ) +if (ret ==0 ) { +bump_nat_hook_base_seq (net); nat_proto_net->users++; + }mutex_unlock (&nf_nat_proto_mutex);return ret;
@@ -1284,6 +1295,7 @@ void nf_nat_unregister_fn(struct net *net, u8 pf, const struct nf_hook_ops *ops, goto unlock; priv = nat_ops[hooknum].priv;nf_hook_entries_delete_raw (&priv->entries, ops); +bump_nat_hook_base_seq (net);if (nat_proto_net->users ==0 ) {nf_unregister_net_hooks (net, nat_ops, ops_count);
net/netfilter/nfnetlink_hook.c +43 -30
122 lines not on this page · raw message
Signature · 1 line
2.54.0