From nobody Fri Jul 24 22:18:15 2026 Received: from mail-pl1-f171.google.com (mail-pl1-f171.google.com [209.85.214.171]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4F4D43F4B9 for ; Wed, 22 Jul 2026 22:38:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784759932; cv=none; b=Ft+qF0FpUfUrGucxMlDHPVbtlsffowm/EHgzsWoNhFW/l0KohXpjfhBNaXhwZbay8N6eokRWxkn2WD3nRx5NiTWQnKfIRZLLrYH1z/Vu8bYAWIhVnHRKLj1FdAJU01zLhG6Z0GN7gcDxOCknqvO+vdkX0TgoIzliB8e79ndMjhc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784759932; c=relaxed/simple; bh=ErJpjEQWDeZx+5Ukr3howqPNeHRsrbWj24/d7PrQaQk=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=BRjawjGkhkLTGEYBsW/H2nDQW6aUKKhLcjSWQQwJ46FO+NWBIPSZP7QvWvU77UOE7Lw2UWp+WHV1L/bptHNzzT/qBQRxvNbrl3awA0n0RWCM85cWCGVpx9mQfoEhSYXNrz36buWXRhVfyDdNVQwRhCiQLe8MtowEMUDXinsTjHM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=asu.edu; spf=pass smtp.mailfrom=asu.edu; dkim=pass (2048-bit key) header.d=asu.edu header.i=@asu.edu header.b=Rq6hTw6Z; arc=none smtp.client-ip=209.85.214.171 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=asu.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=asu.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=asu.edu header.i=@asu.edu header.b="Rq6hTw6Z" Received: by mail-pl1-f171.google.com with SMTP id d9443c01a7336-2cc73e322dbso220365ad.1 for ; Wed, 22 Jul 2026 15:38:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=asu.edu; s=google; t=1784759929; x=1785364729; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=0yGVB60xf7Pehiss9PEglYAbbqwJonda8iGQt4O0k6M=; b=Rq6hTw6Z4f3OGzg0TZXmkGROhRK5wFWsfUUUHFbXycFpb3awolcFqFwM7MeFs7n2i9 e9w28x9dy/GagegvrqgtJsTLMndJojHnV3udZNQXO+WXvGN4fURaVHsLmdD3XnC+gT4e M7NmAjxjgcLmEBhTf5iwZZykIOm7mjOGNhxfDltD1seAZvH6KMA1cyPHweqhJo8bNaiF UyCRMzP/IaLd0kDyGCEh2a7QzBkfE6aEDWQSqRRukoTjlHBKZubtWi9NnLcXh+FcMRiX AsAFxDSh0/iTUbVqLBkFonlI6xZlwaS99lJD6gkAykHZib7o2IuuWd2rXA3zA9wmRHmJ v/FA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784759929; x=1785364729; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=0yGVB60xf7Pehiss9PEglYAbbqwJonda8iGQt4O0k6M=; b=FWwx3WvuQqpz0xAqDTgZ/kBlzblOCVns4SAlmF6nKEyuhv0N3U3GVp0D5cF88UvJTW Ipb5QxaHyvKWFHMbc6cUQVig16U00BTrmj6bnQLRPQUyYLi9zx2/Cmnyei7HU/QDq91w imU1sDRNpC9zxxBGFv0DQ5EkxmIQqVZ10IXFhXPLVnNGTSlAnsDE8ElX649Xc5892FsR DLH1j4m5o0yp3LpbdcbgLb9pB4Vjwnwhv+TvTk80VyWnwnoiBsiH8QRJ4obUqwopnC1J EC99a7MewKHMSfHmrqOLx42xwgt7X4msBokmE7hdIZHBTIgW2ViVWfiAT5eb6T8U0EAo 3Xig== X-Forwarded-Encrypted: i=1; AHgh+RrOHFJkDUKuxypxy4ONxXQ/UcPfCt53eyQOnF/b/Bbee5sqyd+KubjsrjXF8cIB2GrOgMhlSKTPuEF0juI=@vger.kernel.org X-Gm-Message-State: AOJu0YyUfu3WP3gGaZ+K4gWl3GpKohGEekEAEhxZSdbvG7UztzGGGgC9 CloJl+ifdSrKJPsEBZFsC3Aqaoj8dyC6AtSyOHS1ZIIw2p5xMzcxA4ZBL2sxbTW+KOgiCwG+mqn zj04= X-Gm-Gg: AR+sD13Rckqt9AhfLE7GLWgxgB0O6C/XoF7fzgEEhoGCzkXeWkYlE1kfroJCYypOoFS /lhlwl6q9/xkd8NFuDJenZ76d/l4l2grttsZkJeeltJiha763qcz1u6+PI9G5GngJoMwJgyn2GN PucVVMdiBx1TSd/cP+EffFbdmYK+BHhZS8Dz120yV9ljBFVYccqHX39f6QW2wmYfEWCrWGsdFPp 0pb2t9psx3IhMdggnC9U2DL+DoBum89SUyuhqSXhX7oPkfVysBbOhQm0upcHBECwUsFULf5h924 JoCJlQFhtzqwSX4LM6txIzuJd9stnY2VttYR/wT8Xj/kP3GiAwzsZ25rcd5EpFnQLHEMFgr9jwt e8pBm3krW5QhieNOUVKvZrkWFCRN7ryOYVV7R8F5abKIyvWW/koCkv2uE+gEvwLT88VUbyOJcwP GyLhyQiXY= X-Received: by 2002:a05:6a20:9398:b0:3c3:bbe6:95de with SMTP id adf61e73a8af0-3c44afcd580mr443511637.6.1784759928377; Wed, 22 Jul 2026 15:38:48 -0700 (PDT) Received: from xiang.tailc0aff1.ts.net ([20.171.14.70]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-3147e1cf8fasm13254741eec.31.2026.07.22.15.38.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jul 2026 15:38:48 -0700 (PDT) From: "Xiang Mei (Microsoft)" To: Pablo Neira Ayuso , Florian Westphal , Phil Sutter Cc: "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Jozsef Kadlecsik , netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, AutonomousCodeSecurity@microsoft.com, tgopinath@linux.microsoft.com, kys@microsoft.com, "Xiang Mei (Microsoft)" Subject: [PATCH nf] netfilter: ipset: fix refcount race between list:set GC and swap Date: Wed, 22 Jul 2026 22:38:32 +0000 Message-ID: <20260722223832.3524006-1-xmei5@asu.edu> X-Mailer: git-send-email 2.43.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __ip_set_put_byindex() resolved the index to a set pointer under RCU, then took ip_set_ref_lock in __ip_set_put() to decrement set->ref. ip_set_swap() holds that same lock while swapping both the ip_set_list slots and the two sets' ref counters, so it can interleave between the dereference and the lock acquisition, leaving the caller to decrement a set whose reference already moved to the other index and hit BUG_ON(set->ref =3D=3D 0). list_set_gc() reaches this from timer softirq, which the nfnl mutex does not serialize against swap: an expiring list:set member calls list_set_del() -> ip_set_put_byindex() while IPSET_CMD_SWAP runs on the referenced sets. Resolve the index and decrement under ip_set_ref_lock, as ip_set_swap() already does, keeping the refcount tied to the index rather than to a stale set pointer. kernel BUG at net/netfilter/ipset/ip_set_core.c:685! Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI RIP: 0010:ip_set_put_byindex (net/netfilter/ipset/ip_set_core.c:870) Call Trace: list_set_del (net/netfilter/ipset/ip_set_list_set.c:159) set_cleanup_entries (net/netfilter/ipset/ip_set_list_set.c:181) list_set_gc (net/netfilter/ipset/ip_set_list_set.c:578) call_timer_fn (kernel/time/timer.c:1748) __run_timers (kernel/time/timer.c:1799 kernel/time/timer.c:2374) run_timer_softirq (kernel/time/timer.c:2405) Kernel panic - not syncing: Fatal exception in interrupt Fixes: 9076aea76538 ("netfilter: ipset: Increase the number of maximal sets= automatically") Reported-by: AutonomousCodeSecurity@microsoft.com Signed-off-by: Xiang Mei (Microsoft) Acked-by: Jozsef Kadlecsik --- net/netfilter/ipset/ip_set_core.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/net/netfilter/ipset/ip_set_core.c b/net/netfilter/ipset/ip_set= _core.c index 6cfad152d7d1..16d33c3e28cf 100644 --- a/net/netfilter/ipset/ip_set_core.c +++ b/net/netfilter/ipset/ip_set_core.c @@ -679,11 +679,18 @@ __ip_set_get(struct ip_set *set) } =20 static void -__ip_set_put(struct ip_set *set) +__ip_set_put_locked(struct ip_set *set) { - write_lock_bh(&ip_set_ref_lock); + lockdep_assert_held(&ip_set_ref_lock); BUG_ON(set->ref =3D=3D 0); set->ref--; +} + +static void +__ip_set_put(struct ip_set *set) +{ + write_lock_bh(&ip_set_ref_lock); + __ip_set_put_locked(set); write_unlock_bh(&ip_set_ref_lock); } =20 @@ -854,11 +861,11 @@ __ip_set_put_byindex(struct ip_set_net *inst, ip_set_= id_t index) { struct ip_set *set; =20 - rcu_read_lock(); - set =3D rcu_dereference(inst->ip_set_list)[index]; + write_lock_bh(&ip_set_ref_lock); + set =3D ip_set(inst, index); if (set) - __ip_set_put(set); - rcu_read_unlock(); + __ip_set_put_locked(set); + write_unlock_bh(&ip_set_ref_lock); } =20 void --=20 2.43.0