net/mlx5: SF, remove probed SF devices when arming vhca events fails
Subsystems (4)
PATCH 1/1 net/mlx5: SF, remove probed SF devices when arming vhca events fails
mlx5_sf_dev_table_create() queues work that probes the SFs which are already active, and then arms the vhca state events. If arming fails, the arm_err path stops and flushes that work and frees the table, but the SF devices that the work already added stay on the auxiliary bus and in table->devices. dev->priv.sf_dev_table is set to NULL, so mlx5_sf_dev_table_destroy() returns early later and nothing removes them, while they keep pointing at the parent mlx5_core_dev.
Remove them with mlx5_sf_dev_destroy_all() after the work is flushed, as mlx5_sf_dev_table_destroy() does. Move the helper up so that it is defined before the new caller.
---
Found with a static analysis tool we are developing.
Compile-tested only, with x86_64_defconfig plus MLX5_CORE, MLX5_CORE_EN, MLX5_ESWITCH, MLX5_SF and W=1.
.../ethernet/mellanox/mlx5/core/sf/dev/dev.c | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +12 -11
@@ -328,6 +328,17 @@ int mlx5_sf_dev_notifier_init(struct mlx5_core_dev *dev)return mlx5_vhca_event_notifier_register (dev, &dev->priv.sf_dev_nb); } +static void mlx5_sf_dev_destroy_all (struct mlx5_sf_dev_table *table) +{ +struct mlx5_sf_dev *sf_dev; +unsigned long index; + +xa_for_each (&table->devices, index, sf_dev) { +xa_erase (&table->devices, index); +mlx5_sf_dev_remove_aux (table->dev, sf_dev); + } +} +void mlx5_sf_dev_table_create (struct mlx5_core_dev *dev) {struct mlx5_sf_dev_table *table;
@@ -360,6 +371,7 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) arm_err:mlx5_sf_dev_destroy_active_works (table);mlx5_vhca_event_work_queues_flush (dev); +mlx5_sf_dev_destroy_all (table); add_active_err:kfree (table); dev->priv.sf_dev_table =NULL ;
@@ -367,17 +379,6 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev)mlx5_core_err (dev,"SF DEV table create err = %d\n" , err); } -static void mlx5_sf_dev_destroy_all (struct mlx5_sf_dev_table *table) -{ -struct mlx5_sf_dev *sf_dev; -unsigned long index; - -xa_for_each (&table->devices, index, sf_dev) { -xa_erase (&table->devices, index); -mlx5_sf_dev_remove_aux (table->dev, sf_dev); - } -} -void mlx5_sf_dev_notifier_cleanup (struct mlx5_core_dev *dev) {if (mlx5_core_is_sf (dev))
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net/mlx5: race and UAF in mlx5_sf_dev_table_create() arm_err cleanup --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net/mlx5: race and UAF in mlx5_sf_dev_table_create() arm_err cleanup
--
commit a5d70bb82124aa0963c3bb188af6126f523476a3
Author: Changyul Lee <[email protected]>
net/mlx5: SF, remove probed SF devices when arming vhca events fails
This commit calls mlx5_sf_dev_destroy_all() on the arm_err error path in mlx5_sf_dev_table_create() so that SF auxiliary devices probed by the active work are removed if arming VHCA events fails.
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +0
-0
[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) · line 377
Quoted 8 lines
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +1
-0@@ -360,6 +371,7 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) arm_err:mlx5_sf_dev_destroy_active_works (table);mlx5_vhca_event_work_queues_flush (dev); +mlx5_sf_dev_destroy_all (table); add_active_err:kfree (table); dev->priv.sf_dev_table =NULL ;
[Severity: Medium]
Can mlx5_sf_dev_state_change_handler() race with mlx5_sf_dev_destroy_all() and kfree(table) on the arm_err path?
If mlx5_sf_dev_vhca_arm_all() fails after arming one or more SF functions, those functions remain armed in firmware while dev->priv.sf_dev_nb is still registered and dev->priv.sf_dev_table still points to table. Unlike mlx5_sf_dev_table_destroy(), which runs after mlx5_sf_dev_notifier_cleanup() has unregistered the notifier, arm_err runs with the notifier active.
When mlx5_sf_dev_destroy_all() removes an already-probed SF, tearing down its HCA transitions the function from MLX5_VHCA_STATE_IN_USE back to MLX5_VHCA_STATE_ACTIVE, which can queue a new VHCA state event after mlx5_vhca_event_work_queues_flush() has already returned:
CPU0 CPU1 (vhca_events wq)mlx5_sf_dev_table_create()
mlx5_sf_dev_vhca_arm_all()
/* arms SF 0, fails on SF 1 */
mlx5_sf_dev_destroy_active_works()
mlx5_vhca_event_work_queues_flush()
mlx5_sf_dev_destroy_all(table)
xa_erase(&table->devices, 0)
mlx5_sf_dev_remove_aux()
/* SF 0 becomes ACTIVE,
fires VHCA event */
mlx5_sf_dev_state_change_handler()
table = dev->priv.sf_dev_table;
kfree(table)
xa_load(&table->devices, 0)
<-- use-after-free dev->priv.sf_dev_table = NULL;
Even if the handler runs before kfree(table), seeing MLX5_VHCA_STATE_ACTIVE after xa_erase() causes mlx5_sf_dev_add() to re-add the SF auxiliary device that mlx5_sf_dev_destroy_all() just removed, and a state change to ALLOCATED or TEARDOWN_REQUEST can call mlx5_sf_dev_del() concurrently with mlx5_sf_dev_destroy_all().
Clearing dev->priv.sf_dev_table before mlx5_vhca_event_work_queues_flush() (with a barrier or WRITE_ONCE/READ_ONCE pair) would ensure no handler runs against table while mlx5_sf_dev_destroy_all() and kfree(table) execute.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.
Critical: 0 · High: 1 (2) · Medium: 0 · Low: 0
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).
- [High] The patch adds a mlx5_sf_dev_destroy_all() call to the arm_err path of mlx5_sf_dev_table_create().
Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it sits on the path the patch changes.
- [High] This problem wasn't introduced by this patch, but it is the same synchronization gap the patch relies on.
--
net/mlx5: SF, remove probed SF devices when arming vhca events fails
This patch adds a mlx5_sf_dev_destroy_all() call to the arm_err path of mlx5_sf_dev_table_create(). The goal is to remove SF auxiliary devices that the active-SF probe work already added. It also moves the helper above its new caller.
Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +0
-0
[ ... ]
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) · line 374
Quoted 5 lines
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +1
-0@@ -360,6 +371,7 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) arm_err:mlx5_sf_dev_destroy_active_works (table);mlx5_vhca_event_work_queues_flush (dev); +mlx5_sf_dev_destroy_all (table);
[Severity: High]
Is it safe to call mlx5_sf_dev_destroy_all() here while VHCA state change events are still being delivered?
mlx5_load() calls mlx5_vhca_event_start(), which registers the VHCA EQ notifier, before it calls mlx5_sf_dev_table_create(). sf_dev_nb was registered even earlier. The arm_err path unregisters neither of them. mlx5_vhca_event_work_queues_flush() only drains work that is already queued.
mlx5_sf_dev_vhca_arm_all() also returns on the first failure, so the functions armed before it stay armed. mlx5_vhca_event_notify() re-arms a function every time it delivers an event:
mlx5_vhca_event_notify()
mlx5_vhca_event_arm(dev, event->function_id);
...
blocking_notifier_call_chain(&dev->priv.vhca_state_n_head, 0, event);dev->priv.sf_dev_table is still set at this point, so mlx5_sf_dev_state_change_handler() gets past its only check:
if (!table) return 0; ... sf_dev = xa_load(&table->devices, sf_index);
Could the handler's xa_load() and the xa_for_each() in mlx5_sf_dev_destroy_all() both return the same sf_dev before either xa_erase() runs? If so, both would call mlx5_sf_dev_remove_aux(). That runs auxiliary_device_delete() and auxiliary_device_uninit() twice, and the second caller uses sf_dev after mlx5_sf_dev_release() has freed it.
An MLX5_VHCA_STATE_ACTIVE event could also be handled while the loop runs, or after it ends but before kfree(table). In that case mlx5_sf_dev_add() can xa_insert() at an index the loop has already passed. Wouldn't that SF stay on the auxiliary bus with parent_mdev pointing at the parent device? That is the same leak the commit message says this patch fixes. If the insert happens after kfree(table), it writes into a freed xarray.
The commit message says this matches mlx5_sf_dev_table_destroy(), but that function depends on this:
/* Now that event handler is not running, it is safe to destroy * the sf device without race. */
That holds in mlx5_unload() because mlx5_vhca_event_stop() runs first and does both the unregister and the flush. This path only does the flush.
Should the handler be quiesced before the table is walked and freed, for example by stopping event delivery or adding real locking? Setting dev->priv.sf_dev_table to NULL earlier would not be enough on its own, because an mlx5_sf_dev_add() that is already running still uses the table.
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) · line 377
Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +0
-0add_active_err:kfree (table); dev->priv.sf_dev_table =NULL ;
[Severity: High]
This is a pre-existing issue that was not introduced by this patch. Can mlx5_sf_dev_state_change_handler() still use the table after this kfree()?
The handler reads dev->priv.sf_dev_table once, with no lock and no RCU, and only checks it for NULL. If a handler loads the pointer before the NULL store, it can then call xa_load(), xa_erase() or xa_insert() on the freed table->devices. mlx5_sf_dev_add() also reads freed table fields:
sf_dev->bar_base_addr = table->base_address + (sf_index * table->sf_bar_length);
Swapping the kfree() and the NULL store would not close this, because a handler may already hold the old pointer.
This patch also widens the window: the slow mlx5_sf_dev_destroy_all() now runs between the flush and kfree(table).
[Severity: High]
This isn't a bug introduced by this patch, but the mlx5_load() error unwind has the same ordering problem. The err_traps_reg label destroys the table before VHCA events are stopped:
drivers/net/ethernet/mellanox/mlx5/core/main.c:mlx5_load() {
...
err_traps_reg:
mlx5_sf_dev_table_destroy(dev);
mlx5_sriov_detach(dev);
err_sriov:
mlx5_lag_remove_mdev(dev);
mlx5_ec_cleanup(dev);
err_ec:
mlx5_vhca_event_stop(dev);
...
}mlx5_unload() uses the opposite order:
mlx5_vhca_event_stop(dev); mlx5_sf_dev_table_destroy(dev);
Suppose mlx5_devlink_traps_register() or mlx5_data_direct_init() fails after mlx5_sf_dev_table_create() succeeded. Won't mlx5_sf_dev_table_destroy() then run mlx5_sf_dev_destroy_all() and kfree(table) while the EQ notifier is still registered and the vhca event workqueues have not been flushed?
That breaks the "event handler is not running" precondition in the comment in mlx5_sf_dev_table_destroy(). It opens the same races described above:
double removal of an sf_dev, a leaked SF, and use of the freed table.
A mlx5_sf_dev_add_active_work() item that already passed its stop_active_wq check could also still be inside mlx5_sf_dev_add() while mlx5_sf_dev_destroy_all() runs. Nothing on this path flushes the vhca event workqueues first.
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117031141.1720619-1-lcy8047%40gmail.com
Quoted 10 lines · Changyul Lee
External email: Use caution opening links or attachments
mlx5_sf_dev_table_create()queues work that probes the SFs which are already active, and then arms the vhca state events. If arming fails, thearm_errpath stops and flushes that work and frees the table, but the SF devices that the work already added stay on the auxiliary bus and intable->devices.dev->priv.sf_dev_tableis set to NULL, somlx5_sf_dev_table_destroy()returns early later and nothing removes them, while they keep pointing at the parentmlx5_core_dev.
Hi, sorry for the late response...
once an SF is armed, the DPU can activate it and the event handler probes it, so arm_err has needed this since SF aux devices were added. Please re-spin with:
and adjust the commit message accordingly. With that:
P.S.: the race pointed by sashiko is pre-existing and should be handle in different fix
Quoted 69 lines
Remove them with
mlx5_sf_dev_destroy_all()after the work is flushed, asmlx5_sf_dev_table_destroy()does. Move the helper up so that it is defined before the new caller.Fixes 4f57332d6a55 ("net/mlx5: SF: Fix probing active SFs during driver probe phase")Signed-off-by Changyul Lee <[email protected]>---
Found with a static analysis tool we are developing.Compile-tested only, with
x86_64_defconfigplusMLX5_CORE,MLX5_CORE_EN,MLX5_ESWITCH,MLX5_SFand W=1..../ethernet/mellanox/mlx5/core/sf/dev/dev.c | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-)drivers/net/ethernet/mellanox/mlx5/core/sf/dev/dev.c +12
-11@@ -328,6 +328,17 @@ int mlx5_sf_dev_notifier_init(struct mlx5_core_dev *dev)return mlx5_vhca_event_notifier_register (dev, &dev->priv.sf_dev_nb); } +static void mlx5_sf_dev_destroy_all (struct mlx5_sf_dev_table *table) +{ +struct mlx5_sf_dev *sf_dev; +unsigned long index; + +xa_for_each (&table->devices, index, sf_dev) { +xa_erase (&table->devices, index); +mlx5_sf_dev_remove_aux (table->dev, sf_dev); + } +} +void mlx5_sf_dev_table_create (struct mlx5_core_dev *dev) {struct mlx5_sf_dev_table *table;@@ -360,6 +371,7 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev) arm_err:mlx5_sf_dev_destroy_active_works (table);mlx5_vhca_event_work_queues_flush (dev); +mlx5_sf_dev_destroy_all (table); add_active_err:kfree (table); dev->priv.sf_dev_table =NULL ;@@ -367,17 +379,6 @@ void mlx5_sf_dev_table_create(struct mlx5_core_dev *dev)mlx5_core_err (dev,"SF DEV table create err = %d\n" , err); } -static void mlx5_sf_dev_destroy_all (struct mlx5_sf_dev_table *table) -{ -struct mlx5_sf_dev *sf_dev; -unsigned long index; - -xa_for_each (&table->devices, index, sf_dev) { -xa_erase (&table->devices, index); -mlx5_sf_dev_remove_aux (table->dev, sf_dev); - } -} -void mlx5_sf_dev_notifier_cleanup (struct mlx5_core_dev *dev) {if (mlx5_core_is_sf (dev))base-commit:
7475ea8b58616b33c04afb976cf0fbaad9dd63fb--
2.43.0
Tariq Toukan
maintainerQuoted 20 lines · Shay Drori
Quoted 10 lines · Changyul Lee
External email: Use caution opening links or attachments
mlx5_sf_dev_table_create()queues work that probes the SFs which are already active, and then arms the vhca state events. If arming fails, thearm_errpath stops and flushes that work and frees the table, but the SF devices that the work already added stay on the auxiliary bus and intable->devices.dev->priv.sf_dev_tableis set to NULL, somlx5_sf_dev_table_destroy()returns early later and nothing removes them, while they keep pointing at the parentmlx5_core_dev.Hi, sorry for the late response...
once an SF is armed, the DPU can activate it and the event handler probes it, so
arm_errhas needed this since SF aux devices were added. Please re-spin with:
Quoted 8 lines
Fixes 90d010b8634b ("net/mlx5: SF, Add auxiliary device support")and adjust the commit message accordingly. With that:
Reviewed-by Shay Drory <[email protected]>
P.S.: the race pointed by sashiko is pre-existing and should be handle in different fix
pw-bot: cr