Netdev List
 help / color / mirror / Atom feed
From: Tariq Toukan <tariqt@nvidia.com>
To: Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, <netdev@vger.kernel.org>,
	Paolo Abeni <pabeni@redhat.com>,
	Willem de Bruijn <willemb@google.com>
Cc: Andrea Parri <parri.andrea@gmail.com>,
	Boris Pismenny <borisp@nvidia.com>,
	Cosmin Ratiu <cratiu@nvidia.com>,
	Elliot Edmunds <eedmunds@nvidia.com>,
	"Gal Pressman" <gal@nvidia.com>,
	Jeroen Massar <jmassar@nvidia.com>,
	Jianbo Liu <jianbol@nvidia.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Leon Romanovsky <leon@kernel.org>, <linux-rdma@vger.kernel.org>,
	Mark Bloch <mbloch@nvidia.com>,
	Patrisious Haddad <phaddad@nvidia.com>,
	Raed Salem <raeds@nvidia.com>, Saeed Mahameed <saeedm@nvidia.com>,
	Simon Horman <horms@kernel.org>,
	Sridhar Samudrala <sridhar.samudrala@intel.com>,
	"Steffen Klassert" <steffen.klassert@secunet.com>,
	Tariq Toukan <tariqt@nvidia.com>
Subject: [PATCH net 04/10] net/mlx5e: Drain pending hardware event work before freeing IPsec SA
Date: Thu, 26 Nov 2026 23:56:08 +0200	[thread overview]
Message-ID: <20261126215614.792598-5-tariqt@nvidia.com> (raw)
In-Reply-To: <20261126215614.792598-1-tariqt@nvidia.com>

From: Jianbo Liu <jianbol@nvidia.com>

mlx5e_ipsec_event() queues a per-event work item. This is untracked by
the SA, so mlx5e_xfrm_free_state() cannot cancel it, potentially
resulting in a use-after-free.

Embed the work in sa_entry so SA destruction can cancel it. The event
handler looks the SA up without a lock and a device flush frees the SA
right after deleting it, so wait for a running handler before
cancelling the work.

Discovered by manual code inspection. Reproduced on ConnectX-7 (fw
28.48.1000) under KASAN, with a 3 s delay added at the start of
mlx5e_ipsec_handle_event() and the SAs flushed right after a soft
limit event:

  BUG: KASAN: slab-use-after-free in mlx5e_ipsec_handle_event

With the fix the flush waits for the handler, no report.

Fixes: 8c582ddfbb47 ("net/mlx5e: Handle hardware IPsec limits events")
Signed-off-by: Jianbo Liu <jianbol@nvidia.com>
Reviewed-by: Cosmin Ratiu <cratiu@nvidia.com>
Signed-off-by: Tariq Toukan <tariqt@nvidia.com>
---
 .../mellanox/mlx5/core/en_accel/ipsec.c       | 34 ++++++++++++++-----
 .../mellanox/mlx5/core/en_accel/ipsec.h       |  5 +++
 .../mlx5/core/en_accel/ipsec_offload.c        | 25 ++++----------
 3 files changed, 37 insertions(+), 27 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
index a549851c8382..ea7a1ae2056f 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.c
@@ -42,6 +42,7 @@
 #include "ipsec.h"
 #include "ipsec_rxtx.h"
 #include "en_rep.h"
+#include "lib/eq.h"
 
 #define MLX5_IPSEC_RESCHED msecs_to_jiffies(1000)
 #define MLX5E_IPSEC_TUNNEL_SA XA_MARK_1
@@ -869,6 +870,8 @@ static int mlx5e_xfrm_add_state(struct net_device *dev,
 	if (err)
 		goto err_hw_ctx;
 
+	INIT_WORK(&sa_entry->event_work, mlx5e_ipsec_handle_event);
+
 	/* We use *_bh() variant because xfrm_timer_handler(), which runs
 	 * in softirq context, can reach our state delete logic and we need
 	 * xa_erase_bh() there.
@@ -944,6 +947,9 @@ static void mlx5e_xfrm_free_state(struct net_device *dev, struct xfrm_state *x)
 	if (x->xso.flags & XFRM_DEV_OFFLOAD_FLAG_ACQ)
 		goto sa_entry_free;
 
+	mlx5_eq_synchronize_async_irq(ipsec->mdev);
+	cancel_work_sync(&sa_entry->event_work);
+
 	if (sa_entry->work)
 		cancel_work_sync(&sa_entry->work->work);
 
@@ -1002,10 +1008,13 @@ static int mlx5e_ipsec_netevent_event(struct notifier_block *nb,
 
 void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 {
+	u32 caps = mlx5_ipsec_device_caps(priv->mdev);
+	bool pkt_offload = caps & MLX5_IPSEC_CAP_PACKET_OFFLOAD;
+	bool tunnel = caps & MLX5_IPSEC_CAP_TUNNEL;
 	struct mlx5e_ipsec *ipsec;
 	int ret = -ENOMEM;
 
-	if (!mlx5_ipsec_device_caps(priv->mdev)) {
+	if (!caps) {
 		netdev_dbg(priv->netdev, "Not an IPSec offload device\n");
 		return;
 	}
@@ -1022,18 +1031,22 @@ void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 	if (!ipsec->wq)
 		goto err_wq;
 
-	if (mlx5_ipsec_device_caps(priv->mdev) &
-	    MLX5_IPSEC_CAP_PACKET_OFFLOAD) {
+	if (pkt_offload) {
 		ret = mlx5e_ipsec_aso_init(ipsec);
 		if (ret)
 			goto err_aso;
+
+		ipsec->nb.notifier_call = mlx5e_ipsec_event;
+		ret = mlx5_notifier_register(priv->mdev, &ipsec->nb);
+		if (ret)
+			goto clear_aso;
 	}
 
-	if (mlx5_ipsec_device_caps(priv->mdev) & MLX5_IPSEC_CAP_TUNNEL) {
+	if (tunnel) {
 		ipsec->netevent_nb.notifier_call = mlx5e_ipsec_netevent_event;
 		ret = register_netevent_notifier(&ipsec->netevent_nb);
 		if (ret)
-			goto clear_aso;
+			goto err_notifier;
 	}
 
 	ipsec->is_uplink_rep = mlx5e_is_uplink_rep(priv);
@@ -1047,10 +1060,13 @@ void mlx5e_ipsec_init(struct mlx5e_priv *priv)
 	return;
 
 err_fs_init:
-	if (mlx5_ipsec_device_caps(priv->mdev) & MLX5_IPSEC_CAP_TUNNEL)
+	if (tunnel)
 		unregister_netevent_notifier(&ipsec->netevent_nb);
+err_notifier:
+	if (pkt_offload)
+		mlx5_notifier_unregister(priv->mdev, &ipsec->nb);
 clear_aso:
-	if (mlx5_ipsec_device_caps(priv->mdev) & MLX5_IPSEC_CAP_PACKET_OFFLOAD)
+	if (pkt_offload)
 		mlx5e_ipsec_aso_cleanup(ipsec);
 err_aso:
 	destroy_workqueue(ipsec->wq);
@@ -1073,8 +1089,10 @@ void mlx5e_ipsec_cleanup(struct mlx5e_priv *priv)
 		ipsec->netevent_nb.notifier_call = NULL;
 	}
 	if (ipsec->aso)
-		mlx5e_ipsec_aso_cleanup(ipsec);
+		mlx5_notifier_unregister(ipsec->mdev, &ipsec->nb);
 	destroy_workqueue(ipsec->wq);
+	if (ipsec->aso)
+		mlx5e_ipsec_aso_cleanup(ipsec);
 	kfree(ipsec);
 	priv->ipsec = NULL;
 }
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
index 9bbcd9e4c319..6c17bd997a0c 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec.h
@@ -37,6 +37,7 @@
 #include <linux/mlx5/device.h>
 #include <net/xfrm.h>
 #include <linux/idr.h>
+#include <linux/workqueue.h>
 #include "lib/aso.h"
 #include "lib/devcom.h"
 
@@ -286,6 +287,7 @@ struct mlx5e_ipsec_sa_entry {
 	struct mlx5e_ipsec_rule ipsec_rule;
 	struct mlx5e_ipsec_work *work;
 	struct mlx5e_ipsec_dwork *dwork;
+	struct work_struct event_work;
 	struct mlx5e_ipsec_limits limits;
 	u8 ctx[MLX5_ST_SZ_BYTES(ipsec_aso)];
 };
@@ -332,6 +334,9 @@ void mlx5_accel_esp_modify_xfrm(struct mlx5e_ipsec_sa_entry *sa_entry,
 
 int mlx5e_ipsec_aso_init(struct mlx5e_ipsec *ipsec);
 void mlx5e_ipsec_aso_cleanup(struct mlx5e_ipsec *ipsec);
+void mlx5e_ipsec_handle_event(struct work_struct *work);
+int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
+		      void *data);
 
 int mlx5e_ipsec_aso_query(struct mlx5e_ipsec_sa_entry *sa_entry,
 			  struct mlx5_wqe_aso_ctrl_seg *data);
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
index 145677ce9640..1f03465762e0 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_accel/ipsec_offload.c
@@ -438,11 +438,10 @@ static void mlx5e_ipsec_handle_limits(struct mlx5e_ipsec_sa_entry *sa_entry)
 	}
 }
 
-static void mlx5e_ipsec_handle_event(struct work_struct *_work)
+void mlx5e_ipsec_handle_event(struct work_struct *work)
 {
-	struct mlx5e_ipsec_work *work =
-		container_of(_work, struct mlx5e_ipsec_work, work);
-	struct mlx5e_ipsec_sa_entry *sa_entry = work->data;
+	struct mlx5e_ipsec_sa_entry *sa_entry =
+		container_of(work, struct mlx5e_ipsec_sa_entry, event_work);
 	struct mlx5_accel_esp_xfrm_attrs tmp = {};
 	struct mlx5_accel_esp_xfrm_attrs *attrs;
 	bool need_modify = false;
@@ -471,16 +470,14 @@ static void mlx5e_ipsec_handle_event(struct work_struct *_work)
 	spin_unlock_bh(&sa_entry->x->lock);
 	if (need_modify)
 		mlx5_accel_esp_modify_xfrm(sa_entry, &tmp);
-	kfree(work);
 }
 
-static int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
-			     void *data)
+int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
+		      void *data)
 {
 	struct mlx5e_ipsec *ipsec = container_of(nb, struct mlx5e_ipsec, nb);
 	struct mlx5e_ipsec_sa_entry *sa_entry;
 	struct mlx5_eqe_obj_change *object;
-	struct mlx5e_ipsec_work *work;
 	struct mlx5_eqe *eqe = data;
 	u16 type;
 
@@ -497,14 +494,7 @@ static int mlx5e_ipsec_event(struct notifier_block *nb, unsigned long event,
 	if (!sa_entry)
 		return NOTIFY_DONE;
 
-	work = kmalloc_obj(*work, GFP_ATOMIC);
-	if (!work)
-		return NOTIFY_DONE;
-
-	INIT_WORK(&work->work, mlx5e_ipsec_handle_event);
-	work->data = sa_entry;
-
-	queue_work(ipsec->wq, &work->work);
+	queue_work(ipsec->wq, &sa_entry->event_work);
 	return NOTIFY_OK;
 }
 
@@ -536,8 +526,6 @@ int mlx5e_ipsec_aso_init(struct mlx5e_ipsec *ipsec)
 	}
 
 	spin_lock_init(&aso->lock);
-	ipsec->nb.notifier_call = mlx5e_ipsec_event;
-	mlx5_notifier_register(mdev, &ipsec->nb);
 
 	ipsec->aso = aso;
 	return 0;
@@ -559,7 +547,6 @@ void mlx5e_ipsec_aso_cleanup(struct mlx5e_ipsec *ipsec)
 	aso = ipsec->aso;
 	pdev = mlx5_core_dma_dev(mdev);
 
-	mlx5_notifier_unregister(mdev, &ipsec->nb);
 	mlx5_aso_destroy(aso->aso);
 	dma_unmap_single(pdev, aso->dma_addr, sizeof(aso->ctx),
 			 DMA_BIDIRECTIONAL);
-- 
2.44.0


  parent reply	other threads:[~2026-11-26 21:57 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-26 21:56 [PATCH net 00/10] net/mlx5e: Fix IPsec SA races and neighbour handling Tariq Toukan
2026-11-26 21:56 ` [PATCH net 01/10] net/mlx5e: Initialize IPsec XArrays with XA_FLAGS_LOCK_BH Tariq Toukan
2026-11-26 21:56 ` [PATCH net 02/10] net/mlx5e: Stop counting packets dropped by IPsec RX SA rules Tariq Toukan
2026-11-26 21:56 ` [PATCH net 03/10] net/mlx5e: Fix IPsec SA modify/stats update race Tariq Toukan
2026-11-26 21:56 ` Tariq Toukan [this message]
2026-11-26 21:56 ` [PATCH net 05/10] net/mlx5e: Drop IPsec tunnel traffic if the neigh cannot be created Tariq Toukan
2026-11-26 21:56 ` [PATCH net 07/10] net/mlx5e: Copy back only replay_esn after an IPsec ESN update Tariq Toukan
2026-11-26 21:56 ` [PATCH net 08/10] net/mlx5e: Validate neighbour events before updating IPsec SAs Tariq Toukan
2026-11-26 21:56 ` [PATCH net 09/10] net/mlx5e: Serialize IPsec netevent MAC snapshots Tariq Toukan
2026-11-26 21:56 ` [PATCH net 10/10] net/mlx5e: Create IPsec flow counters once per SA Tariq Toukan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261126215614.792598-5-tariqt@nvidia.com \
    --to=tariqt@nvidia.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=borisp@nvidia.com \
    --cc=cratiu@nvidia.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=eedmunds@nvidia.com \
    --cc=gal@nvidia.com \
    --cc=horms@kernel.org \
    --cc=jianbol@nvidia.com \
    --cc=jmassar@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=leon@kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=mbloch@nvidia.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=parri.andrea@gmail.com \
    --cc=phaddad@nvidia.com \
    --cc=raeds@nvidia.com \
    --cc=saeedm@nvidia.com \
    --cc=sridhar.samudrala@intel.com \
    --cc=steffen.klassert@secunet.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox