Skip to contentSkip to conversation

netfilter: ipset: skip inline gc during hash resize

PATCH New Jun Yang 2 participants 2 messages Nov 17 – Nov 21

Subsystems (3)
lore
Share & tools
lore

Jun Yang

author
#0 lore raw

PATCH 1/1 netfilter: ipset: skip inline gc during hash resize

From: Jun Yang <[email protected]>

When the xtables SET target adds an element to a full hash region, mtype_add() runs garbage collection on that region. If mtype_resize() has already copied a live element, both tables point to the same comment. If the element then expires, inline GC can free the comment in the old table. Destroying the set frees it again in the new table.

Take h->gc.lock and check t->resizing before running inline GC, as the periodic GC worker does. Resize sets the flag under the same lock before it starts copying elements.

Fixes cffcf57bf03c ("netfilter: ipset: exclude gc when resize is in progress")

---
A KASAN reproducer for this issue is available if requested.

 net/netfilter/ipset/ip_set_hash_gen.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
net/netfilter/ipset/ip_set_hash_gen.h +6 -2
index 80ca523f304bb..70f768ba7b443 100644
@@ -1009,8 +1009,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	maxelem = t->maxelem;
 	if (elements >= maxelem) {
 		u32 e;
-		if (SET_WITH_TIMEOUT(set))
-			mtype_gc_do(set, h, t, r);
+		if (SET_WITH_TIMEOUT(set)) {
+			spin_lock_bh(&h->gc.lock);
+			if (!t->resizing)
+				mtype_gc_do(set, h, t, r);
+			spin_unlock_bh(&h->gc.lock);
+		}
 		maxelem = h->maxelem;
 		elements = 0;
 		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 0 (1) · Low: 1 (1)

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The fix is incomplete.
- [Low] Undocumented behaviour change.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a double free of the same kind.
- [Medium] Memory leak (pre-existing).
- [Low] Stale comments (pre-existing).

--

netfilter: ipset: skip inline gc during hash resize

This patch takes h->gc.lock and checks t->resizing before mtype_add() runs inline garbage collection on a full hash region. The goal is to avoid freeing a comment extension in the old table after mtype_resize() has shallow-copied it into the new table, since the new table would later free it again.

Quoted 6 lines
net/netfilter/ipset/ip_set_hash_gen.h +0 -0
index 80ca523f304bb..70f768ba7b443 100644
@@ -1009,8 +1009,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	maxelem = t->maxelem;

net/netfilter/ipset/ip_set_hash_gen.h mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext, · line 1017

Quoted 10 lines
net/netfilter/ipset/ip_set_hash_gen.h +6 -2
 	if (elements >= maxelem) {
 		u32 e;
-		if (SET_WITH_TIMEOUT(set))
-			mtype_gc_do(set, h, t, r);
+		if (SET_WITH_TIMEOUT(set)) {
+			spin_lock_bh(&h->gc.lock);
+			if (!t->resizing)
+				mtype_gc_do(set, h, t, r);
+			spin_unlock_bh(&h->gc.lock);
+		}

[Severity: High]
Does this fully close the double free described in the commit message?

Skipping mtype_gc_do() leaves the expired element in the old table. The same mtype_add() call can then free its extensions a few lines later through the reuse branch:

net/netfilter/ipset/ip_set_hash_gen.h:mtype_add() {
    ...
		/* Reuse first timed out entry */
		if (SET_ELEM_EXPIRED(set, data) && j == -1) {
			j = i;
			reuse = true;
		}
	}
	if (reuse || forceadd) {
		if (j == -1)
			j = 0;
		data = ahash_data(n, j, set->dsize);
		if (!deleted) {
			mtype_del_cidr_all(set, h, data);
			ip_set_ext_destroy(set, data);
			t->hregion[r].elements--;
		}
		goto copy_data;
	}
    ...
}

Hash types set region_lock, so ip_set_add() from the SET target takes no set->lock. mtype_resize() copies elements under rcu_read_lock_bh() only. That seems to allow this sequence:

1. mtype_resize() memcpy()s a live element E, including its comment pointer C, into the new table.
2. E expires.
3. A SET target add of a different value hashes to E's bucket in the old table. It finds E expired and sets reuse, with deleted still false.
4. ip_set_ext_destroy()->ip_set_comment_free() calls kfree_rcu(C) and clears only the old table's pointer.
5. The new table's copy still holds C. Periodic GC, mtype_flush(), or mtype_destroy()->mtype_ahash_destroy(set, t, true)-> mtype_ext_cleanup() then frees C again, after reading strlen(c->str) from freed memory.

The forceadd case also looks easier to reach now. With GC skipped, elements stays at or above maxelem, so SET_WITH_FORCEADD sets get:

		if (elements >= maxelem && SET_WITH_FORCEADD(set))
			forceadd = true;

The reuse/forceadd branch then evicts slot 0. That slot may hold a live element that resize has already copied.

Guarding only mtype_gc_do() doesn't seem to be enough. Would the fix need to block every old table extension free while t->resizing is set (this branch and mtype_del() too), or transfer ownership of the comment instead?

net/netfilter/ipset/ip_set_hash_gen.h mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext, · line 1020

Quoted 3 lines
net/netfilter/ipset/ip_set_hash_gen.h +0 -0
 		maxelem = h->maxelem;
 		elements = 0;
 		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)

[Severity: Low]
Is this behavior change intended, and should the commit message mention it? While t->resizing is set, inline GC is skipped, so the elements total recomputed here still counts expired entries.

Suppose that total is >= h->maxelem, forceadd is not set, and the target bucket has no expired or deleted slot to reuse. mtype_add() now reaches:

	if (elements >= maxelem)
		goto set_full;

or the set_full path for an empty bucket. The set_full path skips the
resize: block, which is where kernel side adds get queued for replay:

	if (t->resizing && ext && ext->target) {
		/* Resize is in process and kernel side add, save values */

The add returns -IPSET_ERR_HASH_FULL and may log the ratelimited "Set %s is full" warning. It is never replayed into the new table, which would have had room because resize does not copy expired entries.

Before this patch, inline GC lowered the counts, so the add succeeded and was queued.

[Severity: High]
This isn't a bug introduced by this patch, but a SET target delete (--del-set) that runs during mtype_resize() causes a similar double free.

mtype_del() destroys the extension on the old table element and then queues the delete for replay:

net/netfilter/ipset/ip_set_hash_gen.h:mtype_del() {
    ...
		t->hregion[r].elements--;
		mtype_del_cidr_all(set, h, d);
		ip_set_ext_destroy(set, data);

		if (t->resizing && ext && ext->target) {
			/* Resize is in process and kernel side del,
			 * save values
			 */
			x = kzalloc_obj(struct mtype_resize_ad, GFP_ATOMIC);
    ...
}

After the table switch and synchronize_rcu(), mtype_resize() replays the delete on the new table:

		} else {
			mtype_del(set, &x->d, NULL, NULL, 0);
		}

Suppose element E was already copied into the new table as E'. The replay finds E' and calls ip_set_ext_destroy() on the same comment C a second time. ip_set_comment_free() then reads strlen(c->str) from freed memory and calls kfree_rcu(c) again.

If E' has expired by the time of the replay, the replay skips it. A later GC, flush or destroy of the new table still frees C again. No expiry is needed: the delete only has to land after the element's region was copied and before the table switch.

Could this be fixed together with the add side?

[Severity: Low]
This is a pre-existing issue, but the file header comment in ip_set_hash_gen.h says:

* are serialized by the nfnl mutex. During resizing the set is
* read-locked, so the only possible concurrent operations are
* the kernel side readers. Those must be protected by proper RCU locking.

That contradicts the comment in mtype_resize():

	/* There can't be another parallel resizing,
	 * but dumping and kernel side add/del are possible
	 */

It also contradicts the t->resizing replay handling in mtype_add() and mtype_del().

The comment above ip_set_comment_free() in ip_set_core.c says:

/* Called from uadd/udel, flush or the garbage collectors protected
* by the set spinlock.

However, mtype_gc_do() never holds set->lock, including at the call site changed here. It holds only the region lock, plus h->gc.lock after this patch.

Both comments state locking assumptions that the double free cases above show to be wrong. Could they be updated?

[Severity: Medium]
This is a pre-existing issue, but does mtype_resize() leak the comment of each expired element it skips? The copy loop does:

				if (SET_ELEM_EXPIRED(set, data))
					continue;

It never calls ip_set_ext_destroy() on these elements. The old table is then released with:

		mtype_ahash_destroy(set, orig, false);

With ext_destroy false, mtype_ahash_destroy() skips mtype_ext_cleanup():

		if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
			mtype_ext_cleanup(set, n);

So each skipped element's ip_set_comment_rcu is never freed, and set->ext_size is never decremented for it. The last uref paths in mtype_add(), mtype_del() and mtype_gc() release the old table the same way.

This patch skips inline GC while resizing, so it can leave a few more expired entries in the old table for resize to drop.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117110619.46590-1-juny24602%40gmail.com