From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795918483; b=hCiUSJBvOZM0f7BLPL1jXd5z9CSihcZ0Yr0UEMCjjy16lUV3+GGZHRxR2qYtSLOIdRFS AqkoNoQOLtAwC1IgbfV4e74FBIaW4neoJNuwUKl3ThQoBfgzoptFjMSPQl8GWC2Duvez1 kPLOWv+71+EFSviDK4TmSdF4vPR6h/XN5b50jyY3AmMWT0hhrfELt4qotihSLo932dg9P hqJuRoAhLuuXZIA5NUbo077fGxaUYi8chvSFO5Uby0pZIq638an+U3Cb2+71HWMO2nrA+ Sx8pkFXKBi8jefRZvW8Mj01K0jAJXmtwOqQainvDb6fJysxHJn4XfnINlMh1ZBKFaFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795918483; h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID: In-Reply-To:References:X-sashiko-severity:Content-Type: Content-Transfer-Encoding:MIME-Version; bh=iJDvne977J9hGYVcc2MQbJ+XSNWIZUtVo5KuGGHm0cM=; b=aJepsuxSJSQRVXHaDqM9MbFvoj995su678Qc7UmLTitQ0Ug6FZr7TB/xv8Hs+JI9Hlv1 lwRjZ7W+7Nw1dA7ev4E8YsO+5t3X1Z2X5z7mWaCBxL/pcwqdXNO5gxhypeN7KNyUr5X3L qNCAljbsMCcY7MoiQ6hrANP9bFIw1Nv9iWiyKMDYP0unxRLpb4H6amRaB41/g7tj9xifW ZYGqhYpN/YdCqb+3SgLWKjlrvAFGdm6SS6gK8XMcuHh6DcZlr9Z1vvjI/1M8eGZmNaG4S uwPfNKr8ouqbPfCZVv3Cte3FpHhBNda1Y9fhcHn4+DCT0uOxn4ubDT6Tc/KBo49NzYA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aOaqE1AE; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AFB23606A33F; Sun, 29 Nov 2026 02:14:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=aOaqE1AE Received: by smtp.kernel.org (Postfix) with ESMTPSA id D52651F0089A; Sun, 29 Nov 2026 02:14:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795918483; bh=iJDvne977J9hGYVcc2MQbJ+XSNWIZUtVo5KuGGHm0cM=; h=Subject:From:To:Cc:Date:In-Reply-To:References; b=aOaqE1AEkOqzBJBKh16Ug5vM70de357aRrBq2upfcYaXoi0OlqDtTXL7/wCTDop6D eZLJ2kI6UUY5fZwDNAPvwHdD4PMaTJxvamu+kAQ5DB4FZ55oRtlVMYctPmWYjagLTz CFOzTbIjGYVdeeQ+qK7eF9sOs1+Z7R4BWbRVHXqQ6MusBBLKPMb2nquvlGSPQE3Jlu 2chFZ6y0p1YO42LC2Gz5n/z8QlXI1CVB4lDSK6deTZtOk9W73TTT55f5v9dA+x95lH 1dw6/ejkMuEQozA6N7uvtsO7Q+S8Na2+VL5h9NveVrxz2hG5a/LFDkpUKKnOtbSGv/ 7JthrdMi0O2SQ== Subject: Re: [PATCH ipsec 3/4] xfrm: Wait for an RCU grace period before freeing device state From: netdev-bot+sashiko@kernel.org To: jianbol@nvidia.com Cc: netdev@vger.kernel.org, ipsec@lists.linux.dev, steffen.klassert@secunet.com, herbert@gondor.apana.org.au, davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, fw@strlen.de, linux-kernel@vger.kernel.org, cjubran@nvidia.com Date: Sun, 29 Nov 2026 02:14:42 +0000 Message-ID: <179591848241.3135.5652486461526198891@kernel.org> In-Reply-To: <20261125013412.1123866-4-jianbol@nvidia.com> References: <20261125013412.1123866-4-jianbol@nvidia.com> X-sashiko-severity: Medium Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider. Critical: 0 · High: 0 (1) · Medium: 1 · Low: 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). - [Medium] The patch removes the synchronous xfrm_dev_state_free() after xfrm_state_delete() in xfrm_dev_state_flush() (net/xfrm/xfrm_state.c,… - [Low] The new synchronize_rcu() in xfrm_dev_state_flush() (net/xfrm/xfrm_state.c:1041) runs with RTNL held. Pre-existing issues: - [High] This problem wasn't introduced by this patch, but the patch leaves a second caller of the same use-after-free. -- xfrm: Wait for an RCU grace period before freeing device state This patch removes the xfrm_dev_state_free() call that ran right after xfrm_state_delete() in xfrm_dev_state_flush(). Device state freeing now depends on the dev GC list collection pass or on the state GC. It also adds a synchronize_rcu() between collecting the states and calling xfrm_dev_state_free(), so that a grace period separates xdo_dev_state_delete() and xdo_dev_state_free(). > diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c > index 8465ae0647427..9883fb3545f8f 100644 > --- a/net/xfrm/xfrm_state.c > +++ b/net/xfrm/xfrm_state.c > @@ -1000,7 +1000,6 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali > spin_unlock_bh(&net->xfrm.xfrm_state_lock); > > err = xfrm_state_delete(x); > - xfrm_dev_state_free(x); > > xfrm_audit_state_delete(x, err ? 0 : 1, > task_valid); [Severity: Medium] With the synchronous free gone, can a state deleted by the flush end up neither on flush_list nor visible to xfrm_flush_gc()? The commit message covers two cases. Either the collection pass finds the state with a reference left, or the flush dropped the last reference. What happens when some other holder (an skb secpath, a dst bundle, a netlink handler) drops the last reference after the flush loop's xfrm_state_put() but before the collection pass? xfrm_state_put()->__xfrm_state_destroy() does: spin_lock_bh(&xfrm_state_gc_lock); hlist_add_head(&x->gclist, &xfrm_state_gc_list); spin_unlock_bh(&xfrm_state_gc_lock); schedule_work(&xfrm_state_gc_work); spin_unlock_bh() can run pending softirqs or act as a preemption point before schedule_work() is reached. If the collection pass in xfrm_dev_state_flush() runs during that gap, it sees a zero refcount and skips the state: if (!refcount_inc_not_zero(&x->refcnt)) continue; flush_list may then be empty, so synchronize_rcu() is skipped. xfrm_flush_gc()->flush_work() also returns at once, because the work is not queued yet. Would xfrm_dev_state_flush() then return for NETDEV_DOWN or NETDEV_UNREGISTER with xso->dev still set, the netdev reference still held and xdo_dev_state_free() not yet called? The state GC would free it later, but only after the device is down. On unregister, netdev_wait_allref would wait for it. On mlx5, operations such as mlx5_eswitch_block_ipsec could fail for a while because the SA is still there. Before this patch, the flush loop freed the states it deleted while it still held a reference. The earlier "xfrm: Serialize device state teardown" patch in this series added the zero-refcount skip, and this patch now applies it to the states the flush deletes. [ ... ] > @@ -1038,6 +1037,9 @@ int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_vali > spin_unlock_bh(&xfrm_state_dev_gc_lock); > mutex_unlock(&xfrm_state_gc_mutex); > > + if (!hlist_empty(&flush_list)) > + synchronize_rcu(); > + [Severity: High] This is a pre-existing issue, not one introduced by this patch. The commit message says the required ordering is xdo_dev_state_delete() -> synchronize_rcu() -> xdo_dev_state_free(), but the grace period is only added here. Does bond_ipsec_del_sa_all() in drivers/net/bonding/bond_main.c have the same use-after-free? It runs when the active slave changes, via bond_change_active_slave(). For each offloaded SA it calls delete and then free back to back on the old slave: drivers/net/bonding/bond_main.c:bond_ipsec_del_sa_all() { ... if (ipsec->xs->km.state != XFRM_STATE_DEAD) real_dev->xfrmdev_ops->xdo_dev_state_delete(real_dev, ipsec->xs); spin_unlock_bh(&ipsec->xs->lock); if (real_dev->xfrmdev_ops->xdo_dev_state_free) real_dev->xfrmdev_ops->xdo_dev_state_free(real_dev, ipsec->xs); ... } With an mlx5 slave, mlx5e_xfrm_del_state() removes sa_entry with xa_erase_bh(&ipsec->sadb, ...). mlx5e_xfrm_free_state() then calls kfree(sa_entry). At the same time, the RX path on the old slave can be in: mlx5e_ipsec_offload_handle_rx_skb() rcu_read_lock(); sa_entry = xa_load(&ipsec->sadb, sa_handle); ... xfrm_state_hold(sa_entry->x); rcu_read_unlock(); If xa_load() ran before the erase, can xfrm_state_hold(sa_entry->x) read freed memory after the kfree()? The hardware RX rule is only removed later, inside free_state, by mlx5e_accel_ipsec_fs_del_rule(). Tagged packets can keep arriving during this window. The commit message doesn't mention this path. Should it also get a grace period between delete and free? [Severity: Low] xfrm_dev_state_flush() is only called from xfrm_dev_down() and xfrm_dev_unregister() in net/xfrm/xfrm_device.c. Both are netdev notifier handlers that run under RTNL. Would synchronize_net() be a better fit here? synchronize_net() switches to synchronize_rcu_expedited() when rtnl_is_locked(). With synchronize_rcu(), each device with offloaded SAs adds a full normal grace period to RTNL hold time. During batched unregister or netns dismantle, these grace periods run back to back. In the same call, xfrm_flush_gc() may also wait for xfrm_state_gc_task(), which runs its own synchronize_rcu(). [ ... ] -- Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125013412.1123866-1-jianbol%40nvidia.com