All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH nf] netfilter: nft_dynset: restore set element counter when failing to update
@ 2022-06-22  9:32 Pablo Neira Ayuso
  2022-06-28 17:02 ` Pablo Neira Ayuso
  0 siblings, 1 reply; 2+ messages in thread
From: Pablo Neira Ayuso @ 2022-06-22  9:32 UTC (permalink / raw)
  To: netfilter-devel

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")
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 net/netfilter/nft_set_hash.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/netfilter/nft_set_hash.c b/net/netfilter/nft_set_hash.c
index df40314de21f..23a5cfbe3855 100644
--- a/net/netfilter/nft_set_hash.c
+++ b/net/netfilter/nft_set_hash.c
@@ -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;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH nf] netfilter: nft_dynset: restore set element counter when failing to update
  2022-06-22  9:32 [PATCH nf] netfilter: nft_dynset: restore set element counter when failing to update Pablo Neira Ayuso
@ 2022-06-28 17:02 ` Pablo Neira Ayuso
  0 siblings, 0 replies; 2+ messages in thread
From: Pablo Neira Ayuso @ 2022-06-28 17:02 UTC (permalink / raw)
  To: netfilter-devel

On Wed, Jun 22, 2022 at 11:32:19AM +0200, Pablo Neira Ayuso wrote:
> 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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-28 17:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-22  9:32 [PATCH nf] netfilter: nft_dynset: restore set element counter when failing to update Pablo Neira Ayuso
2022-06-28 17:02 ` Pablo Neira Ayuso

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.