Skip to contentSkip to conversation

netfilter: ipset: fix list type element drift bug

PATCH In review Florian Westphal 3 participants 6 messages Aug 6 – Aug 10

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: ipset: fix list type element drift bug

If list_set_uadd() calls list_set_replace() to swap an expired entry, the element count remains the same, therefore the increment must be elided.

Fixes 702b71e7c666 ("netfilter: ipset: Add element count to all set types header")
---
 net/netfilter/ipset/ip_set_list_set.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/ipset/ip_set_list_set.c +4 -1
index ca3ef9479e83..56626f4943a9 100644
@@ -301,8 +301,11 @@ list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	e->set = set;
 	INIT_LIST_HEAD(&e->list);
 	list_set_init_extensions(set, ext, e);
-	if (n)
+	if (n) {
 		list_set_replace(set, e, n);
+		return 0;
+	}
+
 	else if (next)
 		list_add_tail_rcu(&e->list, &next->list);
 	else if (prev)
Signature · 1 line
2.54.0
author maintainer
#1 lore raw

Hi Jozsef, hi Pablo

TL;DR, more unrelated drive-by findings, probably even wrong.

Quoted 6 lines · Florian Westphal

If list_set_uadd() calls list_set_replace() to swap an expired entry, the element count remains the same, therefore the increment must be elided.

Fixes 702b71e7c666 ("netfilter: ipset: Add element count to all set types header")

Jozsef, could you please have a look at the LLM sauce below?
https://sashiko.dev/#/patchset/20260806135341.8896-1-fw%40strlen.de
-- LLM sauce follows --
When inserting before a reference element, does this mistakenly identify the element after the reference as the candidate for replacement? Similarly, when inserting after, does it replace the element before the reference?
Could this lead to incorrect rule ordering by placing the new element on the opposite side of the reference element than requested?
-- LLM end --

I had a look, even if its correct, this change is super old:
00590fdd5be0 ("netfilter: ipset: Introduce RCU locking in list type")

It is been in RHEL7 for a long time as well. So even if its correct, I'm reluctant to make changes.

-- LLM sauce follows --
This is a pre-existing issue, but does this code fail to enforce the set size limit?
Just below this hunk in list_set_uadd(), the element is added and the counter is incremented without validation:

	else if (prev)
		list_add_rcu(&e->list, &prev->list);
	else
		list_add_tail_rcu(&e->list, &map->members);
	set->elements++;

Since the list:set type accepts a size parameter during creation, should this code verify that set->elements is strictly less than map->size before adding the new element to prevent unbounded memory allocation?
-- LLM end --

ipset man page says size parameter is ignored nowadays, so this looks intentional.

So the only complaint about this patch that is valid is this:

+ return 0;
+ }
+
else if (next)

Should better be:
+ return 0;
+ }
+
+ if (next)
- else if (next)

.. as its more readable.

#2 lore raw

Hi Florian,

Quoted 5 lines · Florian Westphal

TL;DR, more unrelated drive-by findings, probably even wrong.

Quoted 2 lines · Florian Westphal

If list_set_uadd() calls list_set_replace() to swap an expired entry, the element count remains the same, therefore the increment must be elided.

Yes, that is correct and your patch "netfilter: ipset: fix list type element drift bug" fixes it properly.

Quoted 4 lines
Quoted 3 lines
Fixes 702b71e7c666 ("netfilter: ipset: Add element count to all set types header")
Quoted 10 lines

Jozsef, could you please have a look at the LLM sauce below?
https://sashiko.dev/#/patchset/20260806135341.8896-1-fw%40strlen.de
-- LLM sauce follows --
When inserting before a reference element, does this mistakenly identify the element after the reference as the candidate for replacement? Similarly, when inserting after, does it replace the element before the reference?
Could this lead to incorrect rule ordering by placing the new element on the opposite side of the reference element than requested?
-- LLM end --

I can't really make sense of it. There's no replacement here, the code tries to identify where to insert the element: before/after the reference one, if it was given. The only replacement happens when the same element is re-added and the extensions may change, but then in that case there's no before-after.

Quoted 5 lines

I had a look, even if its correct, this change is super old:
00590fdd5be0 ("netfilter: ipset: Introduce RCU locking in list type")

It is been in RHEL7 for a long time as well. So even if its correct, I'm reluctant to make changes.

The testsuite contains tests to verify add/del/test before/after functionalities. So I don't know what to fix here.

Quoted 10 lines

-- LLM sauce follows --
This is a pre-existing issue, but does this code fail to enforce the set size limit?
Just below this hunk in list_set_uadd(), the element is added and the counter is incremented without validation:

	else if (prev)
		list_add_rcu(&e->list, &prev->list);
	else
		list_add_tail_rcu(&e->list, &map->members);
	set->elements++;
Quoted 7 lines

Since the list:set type accepts a size parameter during creation, should this code verify that set->elements is strictly less than map->size before adding the new element to prevent unbounded memory allocation?
-- LLM end --

ipset man page says size parameter is ignored nowadays, so this looks intentional.

Yes, exactly. The parameter is kept for backward compatibility reason only.

Quoted 14 lines

So the only complaint about this patch that is valid is this:

net/netfilter/ipset/ip_set_list_set.c +3 -0
+		return 0;
+       }
+
        else if (next)

Should better be:

net/netfilter/ipset/ip_set_list_set.c +4 -1
+		return 0;
+       }
+
+       if (next)
-       else if (next)
Quoted 2 lines

.. as its more readable.

Best regards,
Jozsef

Signature · 3 lines
E-mail : [email protected], [email protected], [email protected]
Address: Wigner Research Centre for Physics
         H-1525 Budapest 114, POB. 49, Hungary
Acked-by on v1 1/1
#3 lore raw
In reply to Florian Westphal
Quoted 2 lines · Florian Westphal

If list_set_uadd() calls list_set_replace() to swap an expired entry, the element count remains the same, therefore the increment must be elided.

Yes, well-spotted!

Quoted 28 lines
Fixes 702b71e7c666 ("netfilter: ipset: Add element count to all set types header")
---
 net/netfilter/ipset/ip_set_list_set.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/netfilter/ipset/ip_set_list_set.c +4 -1
index ca3ef9479e83..56626f4943a9 100644
@@ -301,8 +301,11 @@ list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	e->set = set;
 	INIT_LIST_HEAD(&e->list);
 	list_set_init_extensions(set, ext, e);
-	if (n)
+	if (n) {
 		list_set_replace(set, e, n);
+		return 0;
+	}
+
 	else if (next)
 		list_add_tail_rcu(&e->list, &next->list);
 	else if (prev)
Signature · 1 line
2.54.0
Signature · 3 lines
E-mail : [email protected], [email protected], [email protected]
Address: Wigner Research Centre for Physics
         H-1525 Budapest 114, POB. 49, Hungary
author maintainer
#4 lore raw
Quoted 1 line · Jozsef Kadlecsik

Thanks for reviewing!

Quoted 7 lines

net/netfilter/ipset/ip_set_list_set.c list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext, · line 310

Quoted 7 lines
net/netfilter/ipset/ip_set_list_set.c +4 -0
+	if (n) {
 		list_set_replace(set, e, n);
+		return 0;
+	}
+
 	else if (next)
 		list_add_tail_rcu(&e->list, &next->list);

While it makes no difference from correctness, the
above looks very weird, so I propose to turn the else if into plain if (next).

(Did not spot it before sending...).

#5 lore raw
Quoted 16 lines · Florian Westphal
Quoted 1 line · Jozsef Kadlecsik

Thanks for reviewing!

Quoted 7 lines

net/netfilter/ipset/ip_set_list_set.c list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext, · line 310

Quoted 7 lines
net/netfilter/ipset/ip_set_list_set.c +4 -0
+	if (n) {
 		list_set_replace(set, e, n);
+		return 0;
+	}
+
 	else if (next)
 		list_add_tail_rcu(&e->list, &next->list);

While it makes no difference from correctness, the
above looks very weird, so I propose to turn the else if into plain if (next).

Quoted 1 line

(Did not spot it before sending...).

For the record: I mangled the patch to remove this else when applying this. No need to resend.