Skip to contentSkip to conversation

netfilter: nft_dynset: restore set element counter when failing to update

PATCH In review Pablo Neira Ayuso 1 participant 2 messages Jun 22, 2022 – Jun 28, 2022

Subsystems (2)
lore
Share & tools
lore

Pablo Neira Ayuso

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nft_dynset: restore set element counter when failing to update

This patch fixes a race condition.

nft_rhash_update() might fail for two reasons:

- Element already exists in the hashtable.
- Another packet won race to insert an entry in the hashtable.

In both cases, new() has already bumped the counter via atomic_add_unless(). Decrement the set element counter in this case.

Fixes 22fe54d5fefc ("netfilter: nf_tables: add support for dynamic set updates")
---
 net/netfilter/nft_set_hash.c | 2 ++
 1 file changed, 2 insertions(+)
net/netfilter/nft_set_hash.c +2 -0
index df40314de21f..23a5cfbe3855 100644
@@ -143,6 +143,7 @@ static bool nft_rhash_update(struct nft_set *set, const u32 *key,
 	/* Another cpu may race to insert the element with the same key */
 	if (prev) {
 		nft_set_elem_destroy(set, he, true);
+		atomic_dec(&set->nelems);
 		he = prev;
 	}
 
@@ -151,6 +152,7 @@ static bool nft_rhash_update(struct nft_set *set, const u32 *key,
 	return true;
 
 err2:
+	atomic_dec(&set->nelems);
 	nft_set_elem_destroy(set, he, true);
 err1:
 	return false;
Signature · 1 line
2.30.2
author maintainer
#1 lore raw
Quoted 9 lines · Pablo Neira Ayuso

This patch fixes a race condition.

nft_rhash_update() might fail for two reasons:

- Element already exists in the hashtable.
- Another packet won race to insert an entry in the hashtable.

In both cases, new() has already bumped the counter via atomic_add_unless(). Decrement the set element counter in this case.

For the record: I'll place this in nf.git.