netfilter: ipset: fix list type element drift bug
Subsystems (2)
Florian Westphal
maintainerPATCH 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.
--- 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
@@ -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
Florian Westphal
maintainerHi Jozsef, hi Pablo
TL;DR, more unrelated drive-by findings, probably even wrong.
Quoted 6 lines · Florian Westphal
If
list_set_uadd()callslist_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")Signed-off-by Florian Westphal <[email protected]>
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.
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()callslist_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")Signed-off-by Florian Westphal <[email protected]>
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 inlist_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->elementsis strictly less thanmap->sizebefore 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
Quoted 2 lines · Florian Westphal
If
list_set_uadd()callslist_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")Signed-off-by Florian Westphal <[email protected]>--- 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@@ -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
Florian Westphal
maintainerQuoted 1 line · Jozsef Kadlecsik
Acked-by Jozsef Kadlecsik <[email protected]>
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...).
Pablo Neira Ayuso
maintainerQuoted 16 lines · Florian Westphal
Quoted 1 line · Jozsef Kadlecsik
Acked-by Jozsef Kadlecsik <[email protected]>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.