netfilter: ipset: do not destroy shared extensions on hash resize
Subsystems (3)
PATCH 1/1 netfilter: ipset: do not destroy shared extensions on hash resize
From: Jun Yang <[email protected]>
mtype_resize() copies the elements into the replacement table with a plain memcpy(), so the comment extension is shared between an element and its copy until the resize completes.
Commit cffcf57bf03cb ("netfilter: ipset: exclude gc when resize is in progress") stopped gc from freeing it behind the copy, but the packet path still does: mtype_add() reusing a timed out element and mtype_del() destroy the extension on the old table before queueing the operation for replay. The replay finds the copy in the replacement table and frees the same ip_set_comment_rcu again:
BUG: KASAN: double-free in rcu_free_sheaf+0x1b/0xe0 Free of addr ffff8881056b9480 by task poc/798 kfree+0x12f/0x4e0 hash_ip4_resize+0x1268/0x18e0 [ip_set_hash_ip] call_ad+0x367/0x8e0 [ip_set] Allocated by task 798: ip_set_init_comment+0xbe/0x220 [ip_set] hash_ip4_add+0x19c7/0x22f0 [ip_set_hash_ip]
A replayed add can instead leave the copy holding the stale pointer, because ip_set_init_comment() is skipped for kernel side adds since commit a442c8a89fb9 ("netfilter: ipset: do not update comments from kernel-side adds"), and the second free then happens at set destroy.
Destroy the extension on the old table only when the operation is not queued for replay: the replacement table owns the element from the replay on, and the old table is freed with ext_destroy == false. An element deleted before the copy loop reaches it is not copied, so its comment is leaked instead of freed twice.
---
A KASAN reproducer for this issue is available if requested.
net/netfilter/ipset/ip_set_hash_gen.h | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
net/netfilter/ipset/ip_set_hash_gen.h +10 -5
@@ -1068,7 +1068,11 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext, data =ahash_data (n, j, set->dsize);if (!deleted) {mtype_del_cidr_all (set, h, data); -ip_set_ext_destroy (set, data); +/* The resize copy shares this extension: +* the replayed add destroys it. +*/ +if (!(t->resizing && ext && ext->target)) +ip_set_ext_destroy (set, data); t->hregion[r].elements--; } goto copy_data;
@@ -1223,11 +1227,10 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,smp_store_release (&n->pos, --pos); 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 +/* Resize is in process and kernel side del, save +* values. The resize copy shares the extension: +* the replayed del destroys it. */ x = kzalloc_obj(struct mtype_resize_ad, GFP_ATOMIC);if (x) {
@@ -1236,6 +1239,8 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext, sizeof(struct mtype_elem)); x->flags = flags; } + }else { +ip_set_ext_destroy (set, data); }for (; i < pos; i++) {if (!test_bit (i, n->used))
base-commit: 4d67cf2fd2352f130c42494a933ba9158b925c65
Signature · 1 line
2.56.1