Skip to contentSkip to conversation

netfilter: nf_tables: skip expired catchall elements in dedup walk

PATCH In review Aohan Mei 2 participants 3 messages Sep 8

Subsystems (3)
lore
Share & tools
lore

Aohan Mei

author
#0 lore raw

PATCH 1/1 netfilter: nf_tables: skip expired catchall elements in dedup walk

From: Aohan Mei <[email protected]>

nft_setelem_catchall_insert() looks up duplicates with nft_set_elem_active() only, while nft_set_catchall_lookup() and the dump path additionally skip expired and dead elements.

Once a catchall element with a timeout expires, this predicate drift makes it invisible to userspace dumps, yet it still blocks
re-insertion: with NLM_F_EXCL the request fails with -EEXIST, and without it the request reports success but silently inserts nothing. The stale entry only goes away when the (user-tunable) gc interval elapses, so the catchall rule may silently stop matching for an arbitrarily long time after its first expiration.

Align the dedup walk with the lookup and dump predicates: only an element that is active, not expired and not dead counts as a duplicate.

Fixes aaa31047a6d2 ("netfilter: nftables: add catch-all set element support")
Assisted-by CodeBuddy:Kimi-K3
---
 net/netfilter/nf_tables_api.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +3 -1
index 765a92fa90d6..6458ee26dcd9 100644
@@ -6995,7 +6995,9 @@ static int nft_setelem_catchall_insert(const struct net *net,
 
 	list_for_each_entry(catchall, &set->catchall_list, list) {
 		ext = nft_set_elem_ext(set, catchall->elem);
-		if (nft_set_elem_active(ext, genmask)) {
+		if (nft_set_elem_active(ext, genmask) &&
+		    !nft_set_elem_expired(ext) &&
+		    !nft_set_elem_is_dead(ext)) {
 			*priv = catchall->elem;
 			return -EEXIST;
 		}
Signature · 1 line
2.43.7
#1 lore raw
Quoted 32 lines · Aohan Mei

From: Aohan Mei <[email protected]>

nft_setelem_catchall_insert() looks up duplicates with nft_set_elem_active() only, while nft_set_catchall_lookup() and the dump path additionally skip expired and dead elements.

Once a catchall element with a timeout expires, this predicate drift makes it invisible to userspace dumps, yet it still blocks
re-insertion: with NLM_F_EXCL the request fails with -EEXIST, and without it the request reports success but silently inserts nothing. The stale entry only goes away when the (user-tunable) gc interval elapses, so the catchall rule may silently stop matching for an arbitrarily long time after its first expiration.

Align the dedup walk with the lookup and dump predicates: only an element that is active, not expired and not dead counts as a duplicate.

Fixes aaa31047a6d2 ("netfilter: nftables: add catch-all set element support")
Assisted-by CodeBuddy:Kimi-K3
---
 net/netfilter/nf_tables_api.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +0 -0
index 765a92fa90d6..6458ee26dcd9 100644

net/netfilter/nf_tables_api.c static int nft_setelem_catchall_insert(const struct net *net, · line 6999

Quoted 6 lines
net/netfilter/nf_tables_api.c +2 -1
@@ -6995,7 +6995,9 @@ static int nft_setelem_catchall_insert(const struct net *net,
 
 	list_for_each_entry(catchall, &set->catchall_list, list) {
 		ext = nft_set_elem_ext(set, catchall->elem);
-		if (nft_set_elem_active(ext, genmask)) {
+		if (nft_set_elem_active(ext, genmask) &&
+		    !nft_set_elem_expired(ext) &&

This should be:
__nft_set_elem_expired(ext, tstamp)

net/netfilter/nf_tables_api.c static int nft_setelem_catchall_insert(const struct net *net, · line 7000

Quoted 1 line
net/netfilter/nf_tables_api.c +1 -0
+		    !nft_set_elem_is_dead(ext)) {

I don't think dead flag is set on for catchall elements?

Quoted 7 lines
net/netfilter/nf_tables_api.c +0 -0
 			*priv = catchall->elem;
 			return -EEXIST;
 		}
Signature · 1 line
2.43.7
#2 lore raw
Quoted 34 lines · Pablo Neira Ayuso
Quoted 33 lines · Aohan Mei

From: Aohan Mei <[email protected]>

nft_setelem_catchall_insert() looks up duplicates with nft_set_elem_active() only, while nft_set_catchall_lookup() and the dump path additionally skip expired and dead elements.

Once a catchall element with a timeout expires, this predicate drift makes it invisible to userspace dumps, yet it still blocks
re-insertion: with NLM_F_EXCL the request fails with -EEXIST, and without it the request reports success but silently inserts nothing. The stale entry only goes away when the (user-tunable) gc interval elapses, so the catchall rule may silently stop matching for an arbitrarily long time after its first expiration.

Align the dedup walk with the lookup and dump predicates: only an element that is active, not expired and not dead counts as a duplicate.

Fixes aaa31047a6d2 ("netfilter: nftables: add catch-all set element support")
Assisted-by CodeBuddy:Kimi-K3
---
 net/netfilter/nf_tables_api.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +0 -0
index 765a92fa90d6..6458ee26dcd9 100644
@@ -6995,7 +6995,9 @@ static int nft_setelem_catchall_insert(const struct net *net,
 
Quoted 8 lines

net/netfilter/nf_tables_api.c static int nft_setelem_catchall_insert(const struct net *net, · line 6999

Quoted 5 lines
net/netfilter/nf_tables_api.c +2 -1
 	list_for_each_entry(catchall, &set->catchall_list, list) {
 		ext = nft_set_elem_ext(set, catchall->elem);
-		if (nft_set_elem_active(ext, genmask)) {
+		if (nft_set_elem_active(ext, genmask) &&
+		    !nft_set_elem_expired(ext) &&

This should be:
__nft_set_elem_expired(ext, tstamp)

Delete also need this this expired check.

Quoted 3 lines

net/netfilter/nf_tables_api.c static int nft_setelem_catchall_insert(const struct net *net, · line 7000

Quoted 1 line
net/netfilter/nf_tables_api.c +1 -0
+		    !nft_set_elem_is_dead(ext)) {

I don't think dead flag is set on for catchall elements?

It does indeed use it, but I think this check does not belong here and the check for expired is sufficient.

Quoted 7 lines
Quoted 7 lines
net/netfilter/nf_tables_api.c +0 -0
 			*priv = catchall->elem;
 			return -EEXIST;
 		}
Signature · 1 line
2.43.7