RDMA/mlx5: Add missing error flow in mlx5_ib_set_vport_rep
Subsystems (2)
PATCH 1/1 RDMA/mlx5: Add missing error flow in mlx5_ib_set_vport_rep
From: Shay Drory <[email protected]>
mlx5_ib_set_vport_rep() attaches the representor netdev to the IB device, which takes a netdev reference, and then adds the LAG demux rule. If mlx5_lag_demux_rule_add() fails, the error is returned without detaching the netdev. The eswitch does not call unload() for a representor whose load() failed, so the reference is never dropped and a later unregister_netdevice() of the representor waits forever while holding the eswitch representors lock.
Similarly, if ib_device_set_netdev() fails, the rep pointers assigned at the start of the function are left dangling.
Undo the netdev attach and clear the rep pointers on failure.
--- drivers/infiniband/hw/mlx5/ib_rep.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
drivers/infiniband/hw/mlx5/ib_rep.c +13 -2
@@ -28,7 +28,7 @@ mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev, ret =ib_device_set_netdev (&ibdev->ib_dev, ndev, vport_index +1 );if (ret) -return ret; + goto err_set_netdev;/* Only Vports that are not native to the LAG master eswitch need to add * demux rule.
@@ -36,7 +36,18 @@ mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev,if (mlx5_eswitch_get_total_vports (dev) > vport_index)return 0 ; -return mlx5_lag_demux_rule_add (rep_dev, rep->vport, vport_index); + ret =mlx5_lag_demux_rule_add (rep_dev, rep->vport, vport_index); +if (ret) + goto err_demux; + +return 0 ; + +err_demux: +ib_device_set_netdev (&ibdev->ib_dev,NULL , vport_index +1 ); +err_set_netdev: + rep->rep_data[REP_IB ].priv =NULL ; + ibdev->port[vport_index].rep =NULL ; +return ret; }static void mlx5_ib_register_peer_vport_reps (struct mlx5_core_dev *mdev);
---
base-commit: 978361dd2ccff357d4fe6aac6ce057d1bfae8312
change-id: 20261015-rep-netdev-leak-93431d272bbb
Best regards,
Signature · 1 line
Edward Srouji <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] rdma/mlx5: TOCTOU race in port representor cleanup leading to NULL deref --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rdma/mlx5: TOCTOU race in port representor cleanup leading to NULL deref --
commit 8e906a48ae2be9e11d0c09a6021af6c819f14449
Author: Shay Drory <[email protected]>
RDMA/mlx5: Add missing error flow in mlx5_ib_set_vport_rep
mlx5_ib_set_vport_rep() attaches the representor netdev to the IB device, which takes a netdev reference, and then adds the LAG demux rule. Undo the netdev attach and clear the rep pointers on failure.
Quoted 10 lines
drivers/infiniband/hw/mlx5/ib_rep.c +1
-1@@ -36,7 +36,18 @@ mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev,if (mlx5_eswitch_get_total_vports (dev) > vport_index)return 0 ; -return mlx5_lag_demux_rule_add (rep_dev, rep->vport, vport_index); + ret =mlx5_lag_demux_rule_add (rep_dev, rep->vport, vport_index);
drivers/infiniband/hw/mlx5/ib_rep.c mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev, · line 49
Quoted 10 lines
drivers/infiniband/hw/mlx5/ib_rep.c +10
-0+if (ret) + goto err_demux; + +return 0 ; + +err_demux: +ib_device_set_netdev (&ibdev->ib_dev,NULL , vport_index +1 ); +err_set_netdev: + rep->rep_data[REP_IB ].priv =NULL ; + ibdev->port[vport_index].rep =NULL ;
[Severity: High]
Can this lockless clearing of the pointer cause a NULL pointer dereference if it races with concurrent readers?
In drivers/infiniband/hw/mlx5/counters.c:mlx5_ib_query_q_counters_vport(), the rep pointer is checked and then dereferenced:
if (!dev->port[port_num].rep || dev->port[port_num].rep->vport == MLX5_VPORT_UPLINK) return 0; mdev = mlx5_eswitch_get_core_dev(dev->port[port_num].rep->esw);
Since there is no READ_ONCE() or local caching of the pointer on the read side, and no synchronization with this error path, can the compiler reload the pointer from memory after the safety check?
If the pointer is cleared by the error path here between the check and the dereference in mlx5_ib_query_q_counters_vport(), wouldn't this result in a NULL pointer dereference?
Quoted 2 lines
drivers/infiniband/hw/mlx5/ib_rep.c +1
-0+return ret; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 50 lines · On 10/15/2026 2:55 PM, [email protected] wrote:
External email: Use caution opening links or attachments
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] rdma/mlx5: TOCTOU race in port representor cleanup leading to NULL deref --commit
8e906a48ae2be9e11d0c09a6021af6c819f14449
Author: Shay Drory <[email protected]>RDMA/mlx5: Add missing error flow in
mlx5_ib_set_vport_rep
mlx5_ib_set_vport_rep()attaches the representor netdev to the IB device, which takes a netdev reference, and then adds the LAG demux rule. Undo the netdev attach and clear the rep pointers on failure.Quoted 10 lines
drivers/infiniband/hw/mlx5/ib_rep.c +1
-1@@ -36,7 +36,18 @@ mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev,if (mlx5_eswitch_get_total_vports (dev) > vport_index)return 0 ; -return mlx5_lag_demux_rule_add (rep_dev, rep->vport, vport_index); + ret =mlx5_lag_demux_rule_add (rep_dev, rep->vport, vport_index);drivers/infiniband/hw/mlx5/ib_rep.c mlx5_ib_set_vport_rep(struct mlx5_core_dev *dev, · line 49
Quoted 10 lines
drivers/infiniband/hw/mlx5/ib_rep.c +10
-0+if (ret) + goto err_demux; + +return 0 ; + +err_demux: +ib_device_set_netdev (&ibdev->ib_dev,NULL , vport_index +1 ); +err_set_netdev: + rep->rep_data[REP_IB ].priv =NULL ; + ibdev->port[vport_index].rep =NULL ;[Severity: High]
Can this lockless clearing of the pointer cause a NULL pointer dereference if it races with concurrent readers?In drivers/infiniband/hw/mlx5/counters.c:mlx5_ib_query_q_counters_vport(), the rep pointer is checked and then dereferenced:
if (!dev->port[port_num].rep || dev->port[port_num].rep->vport == MLX5_VPORT_UPLINK) return 0; mdev = mlx5_eswitch_get_core_dev(dev->port[port_num].rep->esw);
Quoted 10 lines
Since there is no
READ_ONCE()or local caching of the pointer on the read side, and no synchronization with this error path, can the compiler reload the pointer from memory after the safety check?If the pointer is cleared by the error path here between the check and the dereference in
mlx5_ib_query_q_counters_vport(), wouldn't this result in a NULL pointer dereference?Quoted 2 lines
drivers/infiniband/hw/mlx5/ib_rep.c +1
-0+return ret; }
Essentially, Sashiko is correct.
But the same pattern already exist, with a wider racy window, in mlx5_ib_vport_rep_unload().
I let the AI to look for readers that have similar patterns, and it found in multiple different places.
Specifically here, the function can be reordered to publish the reps at the end of it, but it's a behavioral change and need to consider what it affects.>
Quoted 2 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1