netfilter: nf_tables: use RCU-safe list insertion when replacing rules
Subsystems (2)
Pablo Neira Ayuso
maintainerPATCH 1/4 netfilter: nf_tables: use RCU-safe list insertion when replacing rules
The patch 5e94846 ("netfilter: nf_tables: add insert operation") did not include RCU-safe list insertion when replacing rules.
--- net/netfilter/nf_tables_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -1796,7 +1796,7 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb, goto err2; }nft_rule_disactivate_next (net, old_rule); -list_add_tail (&rule->list, &old_rule->list); +list_add_tail_rcu (&rule->list, &old_rule->list); }else { err = -ENOENT ; goto err2;
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/4 netfilter: nf_tables: use u32 for chain use counter
Since 4fefee5 ("netfilter: nf_tables: allow to delete several objects from a batch"), every new rule bumps the chain use counter. However, this is limited to 16 bits, which means that it will overrun after 2^16 rules.
Use a u32 chain counter and check for overflows (just like we do for table objects).
--- include/net/netfilter/nf_tables.h | 6 +++--- net/netfilter/nf_tables_api.c | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +3 -3
@@ -503,9 +503,9 @@ enum nft_chain_flags {* @net: net namespace that this chain belongs to * @table: table that this chain belongs to * @handle: chain handle -* @flags: bitmask of enum nft_chain_flags * @use: number of jump references to this chain * @level: length of longest path to this chain +* @flags: bitmask of enum nft_chain_flags * @name: name of the chain */ struct nft_chain {
@@ -514,9 +514,9 @@ struct nft_chain {struct net *net;struct nft_table *table;u64 handle; -u8 flags; -u16 use; +u32 use;u16 level; +u8 flags;char name[NFT_CHAIN_MAXNAMELEN ]; };
net/netfilter/nf_tables_api.c +3 -0
@@ -1730,6 +1730,9 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,if (!create || nlh->nlmsg_flags &NLM_F_REPLACE )return -EINVAL ; handle =nf_tables_alloc_handle (table); + +if (chain->use ==UINT_MAX ) +return -EOVERFLOW ; }if (nla[NFTA_RULE_POSITION ]) {
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 3/4 netfilter: nf_tables: decrement chain use counter when replacing rules
In reply to Pablo Neira AyusoThus, the chain use counter remains with the same value after the rule replacement.
--- net/netfilter/nf_tables_api.c | 2 ++ 1 file changed, 2 insertions(+)
net/netfilter/nf_tables_api.c +2 -0
@@ -1799,6 +1799,7 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb, goto err2; }nft_rule_disactivate_next (net, old_rule); + chain->use--;list_add_tail_rcu (&rule->list, &old_rule->list); }else { err = -ENOENT ;
@@ -1829,6 +1830,7 @@ err3:list_del_rcu (&nft_trans_rule (trans)->list);nft_rule_clear (net,nft_trans_rule (trans));nft_trans_destroy (trans); + chain->use++; } err2:nf_tables_rule_destroy (&ctx, rule);
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 4/4 netfilter: nf_tables: fix wrong type in transaction when replacing rules
In reply to Pablo Neira AyusoIn b380e5c ("netfilter: nf_tables: add message type to transactions"), I used the wrong message type in the rule replacement case. The rule that is replaced needs to be handled as a deleted rule.
--- net/netfilter/nf_tables_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -1792,7 +1792,7 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,if (nlh->nlmsg_flags &NLM_F_REPLACE ) {if (nft_rule_is_active_next (net, old_rule)) { - trans =nft_trans_rule_add (&ctx,NFT_MSG_NEWRULE , + trans =nft_trans_rule_add (&ctx,NFT_MSG_DELRULE , old_rule);if (trans ==NULL ) { err = -ENOMEM ;
Signature · 1 line
1.7.10.4