Skip to contentSkip to conversation

net/mlx5: Fix HWS and DR steering memory safety bugs

PATCH Applied Tariq Toukan 5 participants 9 messages Nov 18 – Nov 21

Subsystems (4)
lore
Share & tools
lore

Tariq Toukan

author maintainer
#0 lore raw

v2 cover net/mlx5: Fix HWS and DR steering memory safety bugs

Hi,

This series by Yevgeny fixes two memory safety bugs in the mlx5 software and hardware steering code paths.

Both are missing-check or wrong-handling problems that lead to a NULL pointer being inserted into an xarray, or an out-of-bounds write that corrupts the kernel heap.

Regards,
Tariq

V2:
Took patch #2 out of the series.
Patch #1: Remove empty line. Updated commit message. Patch #3 (now #2): Update commit message. Not allocating chunks in error state.
Print error once when putting pool into error state

V1:
https://lore.kernel.org/all/[email protected]/

Yevgeny Kliteynik (2):
net/mlx5: HWS, Handle NULL in vport destination action creation net/mlx5: DR, Fix out-of-bounds write when sync to HW fails

 .../mellanox/mlx5/core/steering/hws/fs_hws.c  |  2 ++
 .../mlx5/core/steering/sws/dr_icm_pool.c      | 32 +++++++++++++++++--
 2 files changed, 31 insertions(+), 3 deletions(-)

base-commit: aa40afa831ebfe45e40a23721711059f2ac584d4

Signature · 1 line
2.44.0
author maintainer
#1 lore raw

PATCH v2 1/2 net/mlx5: HWS, Handle NULL in vport destination action creation

From: Yevgeny Kliteynik <[email protected]>

mlx5hws_action_create_dest_vport() can return NULL. If this happens, xa_insert() will insert a NULL entry into the xarray. The next mlx5hws_action_create_dest_vport() failure will try to insert NULL again, but xa_insert() will return -EBUSY, because we already have NULL entry. In such case the code will go into error flow of action creation, and eventually will cause NULL dereference at hws_action_destroy_hws().

The issue was discovered by code inspection.

Fixes 8e2e08a6d1e0 ("net/mlx5: fs, add support for dest vport HWS action")
---
 drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c +2 -0
index cbcbc2b62809..de22e7279a31 100644
@@ -441,6 +441,8 @@ mlx5_fs_get_dest_action_vport(struct mlx5_fs_hws_context *fs_ctx,
 
 	dest = mlx5hws_action_create_dest_vport(ctx, vport_num,	vhca_id_valid,
 						dest_attr->vport.vhca_id, flags);
+	if (!dest)
+		return NULL;
 
 	err = xa_insert(dests_xa, idx, dest, GFP_KERNEL);
 	if (err) {
Signature · 1 line
2.44.0
#2 lore raw

Re: [PATCH net V2 1/2] net/mlx5: HWS, Handle NULL in vport destination action creation

Sashiko has reviewed this patch and found no issues.
Quoted 10 lines

From: Yevgeny Kliteynik <[email protected]>

mlx5hws_action_create_dest_vport() can return NULL. If this happens, xa_insert() will insert a NULL entry into the xarray. The next mlx5hws_action_create_dest_vport() failure will try to insert NULL again, but xa_insert() will return -EBUSY, because we already have NULL entry. In such case the code will go into error flow of action creation, and eventually will cause NULL dereference at hws_action_destroy_hws().

Quoted 7 lines

The issue was discovered by code inspection.

Fixes 8e2e08a6d1e0 ("net/mlx5: fs, add support for dest vport HWS action")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH net V2 1/2] net/mlx5: HWS, Handle NULL in vport destination action creation

In reply to Tariq Toukan
Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you…

Thank you for your contribution! Sashiko AI review found more severe issue(s) in other patches of this series. This mail is only an FYI, in case you want to address the nit picks below when you respin.

Low priority / nit picks for your consideration:

- [Low] The commit message for "net/mlx5: HWS, Handle NULL in vport destination action creation" describes only part of what the bug did.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but the same caller, mlx5_fs_fte_get_hws_actions() in…

--

net/mlx5: HWS, Handle NULL in vport destination action creation

This patch makes mlx5_fs_get_dest_action_vport() return NULL as soon as mlx5hws_action_create_dest_vport() fails. A NULL entry is therefore never inserted into the vport destination xarray. Before this change, inserting NULL could later cause a NULL dereference in hws_action_destroy_hws().

Quoted 5 lines

The next mlx5hws_action_create_dest_vport() failure will try to insert NULL again, but xa_insert() will return -EBUSY, because we already have NULL entry. In such case the code will go into error flow of action creation, and eventually will cause NULL dereference at hws_action_destroy_hws().

[Severity: Low]
This isn't a bug, but does this describe the whole impact? The text covers a later creation for the same idx failing again. What about a later creation that succeeds?

When the entry is NULL, __xa_insert() stores XA_ZERO_ENTRY instead:

lib/xarray.c:__xa_insert() {
    ...
	if (!entry)
		entry = XA_ZERO_ENTRY;
    ...
}

xa_load() turns that back into NULL. The slot looks empty but stays reserved.

Suppose a later mlx5hws_action_create_dest_vport() call succeeds, for example once a transient kzalloc or STC pool failure has cleared. The old code then appears to loop:

mlx5_fs_get_dest_action_vport()
dest_load:
xa_load() returns NULL mlx5hws_action_create_dest_vport() returns a valid action xa_insert() returns -EBUSY mlx5hws_action_destroy(dest)
goto dest_load

Wouldn't this spin forever and hang rule insertion for that vport?

The fix covers both outcomes, since NULL is never inserted now. Could the commit message also mention the livelock?

drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c mlx5_fs_get_dest_action_vport(struct mlx5_fs_hws_context *fs_ctx, · line 448

Quoted 13 lines
drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c +2 -0
index cbcbc2b62809d..de22e7279a312 100644
@@ -441,6 +441,8 @@ mlx5_fs_get_dest_action_vport(struct mlx5_fs_hws_context *fs_ctx,
 
 	dest = mlx5hws_action_create_dest_vport(ctx, vport_num,	vhca_id_valid,
 						dest_attr->vport.vhca_id, flags);
+	if (!dest)
+		return NULL;
 
 	err = xa_insert(dests_xa, idx, dest, GFP_KERNEL);
 	if (err) {

[Severity: High]
This is a pre-existing issue, but can the same caller, mlx5_fs_fte_get_hws_actions(), still oops on a NULL action in other destination cases?

In three cases the result goes into fs_actions[] and is counted before the NULL check. These are FLOW_TABLE_NUM (when the action has to be created), RANGE and FLOW_SAMPLER:

drivers/net/ethernet/mellanox/mlx5/core/steering/hws/fs_hws.c:mlx5_fs_fte_get_hws_actions() {
    ...
				dest_action = mlx5_fs_create_dest_action_table_num(fs_ctx,
										   dst);
				fs_actions[num_fs_actions++].action = dest_action;
				break;
			case MLX5_FLOW_DESTINATION_TYPE_RANGE:
				dest_action = mlx5_fs_create_dest_action_range(ctx, dst);
				fs_actions[num_fs_actions++].action = dest_action;
				break;
    ...
			case MLX5_FLOW_DESTINATION_TYPE_FLOW_SAMPLER:
				dest_action =
					mlx5_fs_get_dest_action_sampler(fs_ctx,
									dst);
				fs_actions[num_fs_actions].action = dest_action;
				fs_actions[num_fs_actions++].sampler_id =
							dst->dest_attr.sampler_id;
				break;
    ...
			if (!dest_action) {
				err = -ENOMEM;
				goto free_actions;
			}
    ...
}

Each of these can return NULL:

mlx5hws_action_create_dest_table_num(): allocation failure or STC pool exhaustion

mlx5hws_action_create_dest_match_range(): invalid range parameters, or allocation, definer or STC failure

mlx5_fs_get_dest_action_sampler(): GFP_ATOMIC allocation or __xa_insert() failure, or failure to create the sampler action

The free_actions path then calls teardown on every counted entry:

mlx5_fs_destroy_fs_actions()
mlx5_fs_destroy_fs_action()

    mlx5hws_action_get_type(fs_action->action)
      return action->type;   <-- NULL dereference

The other patch in this series, "net/mlx5: DR, Fix out-of-bounds write when sync to HW fails", only touches sws/dr_icm_pool.c and does not fix this.

Could dest_action be checked before it goes into fs_actions[]? Alternatively, mlx5_fs_destroy_fs_actions() could skip NULL entries.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118173227.1957670-1-tariqt%40nvidia.com
author maintainer
#4 lore raw

PATCH v2 2/2 net/mlx5: DR, Fix out-of-bounds write when sync to HW fails

In reply to Tariq Toukan

From: Yevgeny Kliteynik <[email protected]>

mlx5dr_icm_free_chunk() moves a freed chunk to the "hot" chunks array by writing it to hot_chunks_arr and, once the accumulated hot memory crosses a threshold, triggers a sync to HW that flushes the array. The array is large enough to hold the chunks of a single accumulation cycle, and relies on the sync path to flush it.

However, if sync to HW fails, hot chunks array is not cleared, and every subsequent mlx5dr_icm_free_chunk will write past the end of hot_chunks_arr, resulting in an out-of-bounds write and kernel heap corruption.

A failed sync to HW also means it is no longer safe to recycle ICM: a chunk's memory may only be returned to the buddy allocator (and thus be reused) after a sync has confirmed that HW no longer references it.

The issue was discovered by a fuzzer that repeatedly triggered sync failure, at which point the out-of-bounds write was reported by KASAN:

KASAN log excerpt: BUG:

     slab-out-of-bounds in mlx5dr_icm_free_chunk+0x4cd at call 2
     (hot_chunks_num=3 > cap=2).

Fix this by putting the pool into an error state on the first failed sync to HW, reporting the failure once. The sync is not retried, and while the pool is in this state:

- mlx5dr_icm_free_chunk() doesn't queue freed chunks; the segment is left reserved and is reclaimed when the pool is destroyed.

- mlx5dr_icm_alloc_chunk() doesn't serve new chunks.

Fixes 4519fc45beeb ("net/mlx5: DR, Keep track of hot ICM chunks in an array instead of list")
---
 .../mlx5/core/steering/sws/dr_icm_pool.c      | 32 +++++++++++++++++--
 1 file changed, 29 insertions(+), 3 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/steering/sws/dr_icm_pool.c +29 -3
index 1b928b40686e..a906a72601c4 100644
@@ -33,6 +33,11 @@ struct mlx5dr_icm_pool {
 	u64 hot_memory_size;
 	/* hot memory size threshold for triggering sync */
 	u64 th;
+	/* Set when a sync to HW fails - no new chunks are served, freed
+	 * chunks are no longer queued, and their ICM is not returned to
+	 * the buddy allocator.
+	 */
+	bool error_state;
 };
 
 struct mlx5dr_icm_dm {
@@ -368,7 +373,15 @@ static int dr_icm_pool_sync_all_buddy_pools(struct mlx5dr_icm_pool *pool)
 
 	err = mlx5dr_cmd_sync_steering(pool->dmn->mdev);
 	if (err) {
-		mlx5dr_err(pool->dmn, "Failed to sync to HW (err: %d)\n", err);
+		/* The hot chunks could not be flushed. Put the pool in an
+		 * error state so that no further chunks are queued (which
+		 * would overflow hot_chunks_arr), and no new chunks are
+		 * served. The sync is not retried.
+		 */
+		mlx5dr_err(pool->dmn,
+			   "Failed to sync to HW (err: %d), disabling ICM pool\n",
+			   err);
+		pool->error_state = true;
 		return err;
 	}
 
@@ -443,6 +456,10 @@ mlx5dr_icm_alloc_chunk(struct mlx5dr_icm_pool *pool,
 		return NULL;
 
 	mutex_lock(&pool->mutex);
+
+	if (pool->error_state)
+		goto out;
+
 	/* find mem, get back the relevant buddy pool and seg in that mem */
 	ret = dr_icm_handle_buddies_get_mem(pool, chunk_size, &buddy, &seg);
 	if (ret)
@@ -475,6 +492,14 @@ void mlx5dr_icm_free_chunk(struct mlx5dr_icm_chunk *chunk)
 	/* move the chunk to the waiting chunks array, AKA "hot" memory */
 	mutex_lock(&pool->mutex);
 
+	/* If the pool is in an error state, the chunk must not be queued.
+	 * ICM memory is not returned to the buddy allocator - it is
+	 * reclaimed only when the pool is destroyed.
+	 * Just free the chunk's bookkeeping struct.
+	 */
+	if (pool->error_state)
+		goto out;
+
 	pool->hot_memory_size += mlx5dr_icm_pool_get_chunk_byte_size(chunk);
 
 	hot_chunk = &pool->hot_chunks_arr[pool->hot_chunks_num++];
@@ -482,12 +507,13 @@ void mlx5dr_icm_free_chunk(struct mlx5dr_icm_chunk *chunk)
 	hot_chunk->seg = chunk->seg;
 	hot_chunk->size = chunk->size;
 
-	kmem_cache_free(chunks_cache, chunk);
-
 	/* Check if we have chunks that are waiting for sync-ste */
 	if (dr_icm_pool_is_sync_required(pool))
 		dr_icm_pool_sync_all_buddy_pools(pool);
 
+out:
+	kmem_cache_free(chunks_cache, chunk);
+
 	mutex_unlock(&pool->mutex);
 }
 
Signature · 1 line
2.44.0
#5 lore raw

Re: [PATCH net V2 2/2] net/mlx5: DR, Fix out-of-bounds write when sync to HW fails

Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

From: Yevgeny Kliteynik <[email protected]>

mlx5dr_icm_free_chunk() moves a freed chunk to the "hot" chunks array by writing it to hot_chunks_arr and, once the accumulated hot memory crosses a threshold, triggers a sync to HW that flushes the array. The array is large enough to hold the chunks of a single accumulation cycle, and relies on the sync path to flush it.

However, if sync to HW fails, hot chunks array is not cleared, and every subsequent mlx5dr_icm_free_chunk will write past the end of hot_chunks_arr, resulting in an out-of-bounds write and kernel heap corruption.

Quoted 8 lines

A failed sync to HW also means it is no longer safe to recycle ICM: a chunk's memory may only be returned to the buddy allocator (and thus be [ ... ]

Fixes 4519fc45beeb ("net/mlx5: DR, Keep track of hot ICM chunks in an array instead of list")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH net V2 2/2] net/mlx5: DR, Fix out-of-bounds write when sync to HW fails

In reply to Tariq Toukan
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

Critical: 0 · High: 0 · 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] One SYNC_STEERING failure now disables the ICM pool permanently.
- [Low] The 'KASAN log excerpt' in the commit message is not real KASAN output.

--

net/mlx5: DR, Fix out-of-bounds write when sync to HW fails

This patch adds an error_state flag to struct mlx5dr_icm_pool. The flag is set when SYNC_STEERING fails in dr_icm_pool_sync_all_buddy_pools(). While it is set, mlx5dr_icm_free_chunk() stops queuing chunks into hot_chunks_arr, which prevents the out-of-bounds write, and mlx5dr_icm_alloc_chunk() returns NULL.

Quoted 6 lines

The issue was discovered by a fuzzer that repeatedly triggered sync failure, at which point the out-of-bounds write was reported by KASAN:

   KASAN log excerpt: BUG:
     slab-out-of-bounds in mlx5dr_icm_free_chunk+0x4cd at call 2
     (hot_chunks_num=3 > cap=2).

[Severity: Low]
Is this excerpt from an actual KASAN report? A real splat starts with "BUG: KASAN: slab-out-of-bounds in func+0xoff/0xsize". It is followed by "Write of size N at addr ... by task ..." and a stack trace. KASAN doesn't print "at call N" or notes like "(hot_chunks_num=3 > cap=2)".

A capacity of 2 also looks hard to reach on real hardware. mlx5dr_icm_pool_create() sizes the array like this:

	num_of_chunks = DIV_ROUND_UP(max_hot_size, entry_size) + 1;
	pool->th = max_hot_size;

Here max_hot_size is a percentage of the byte size of the largest chunk. A capacity of 2 would need max_hot_size <= entry_size. Real device caps make the array thousands of entries long.

The overflow itself looks real. Before this patch, a failed sync left hot_chunks_num and hot_memory_size unchanged.

Could the commit message include the real KASAN splat? Or could it say plainly that the overflow was found by injecting sync failures in a test harness, and describe how they were injected?

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/steering/sws/dr_icm_pool.c +0 -0
index 1b928b40686e5..a906a72601c4f 100644

[ ... ]

Quoted 4 lines
drivers/net/ethernet/mellanox/mlx5/core/steering/sws/dr_icm_pool.c +0 -0
@@ -368,7 +373,15 @@ static int dr_icm_pool_sync_all_buddy_pools(struct mlx5dr_icm_pool *pool)
 
 	err = mlx5dr_cmd_sync_steering(pool->dmn->mdev);
 	if (err) {

drivers/net/ethernet/mellanox/mlx5/core/steering/sws/dr_icm_pool.c static int dr_icm_pool_sync_all_buddy_pools(struct mlx5dr_icm_pool *pool) · line 384

Quoted 10 lines
drivers/net/ethernet/mellanox/mlx5/core/steering/sws/dr_icm_pool.c +9 -1
-		mlx5dr_err(pool->dmn, "Failed to sync to HW (err: %d)\n", err);
+		/* The hot chunks could not be flushed. Put the pool in an
+		 * error state so that no further chunks are queued (which
+		 * would overflow hot_chunks_arr), and no new chunks are
+		 * served. The sync is not retried.
+		 */
+		mlx5dr_err(pool->dmn,
+			   "Failed to sync to HW (err: %d), disabling ICM pool\n",
+			   err);
+		pool->error_state = true;
                ^^^^

[Severity: Medium]
Does one SYNC_STEERING failure now disable this pool for good? Only mlx5dr_icm_pool_destroy() clears error_state, and the sync is never retried.

After that, every mlx5dr_icm_alloc_chunk() call returns NULL. STE hash table allocation in mlx5dr_ste_htbl_alloc(), modify action allocation and pattern allocation in dr_ptrn.c all fail. No new steering rules can be inserted until the domain is torn down by an eswitch mode change, driver reload or health recovery. Each later mlx5dr_icm_free_chunk() also leaves its segment reserved in the buddy.

mlx5dr_cmd_sync_steering() only skips the device internal error state:

	if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR)
		return 0;
	...
	return mlx5_cmd_exec_in(mdev, sync_steering, in);

That means transient host-side errors also set the flag. For example, the SYNC_STEERING inbox is at most 16 bytes, so alloc_msg() in cmd.c skips the message cache:

	if (in_size <= 16)
		goto cache_miss;

It then allocates with GFP_KERNEL, which can fail with -ENOMEM under memory pressure. Command timeouts and the -ENXIO paths are passed up the same way.

Also, error_state is per pool (STE, MODIFY_ACTION, MODIFY_HDR_PTRN), but the sync is device-wide. A later successful sync from another pool won't re-enable this one.

The commit message says a chunk may only return to the buddy after a sync confirms HW no longer references it. That explains not recycling the queued hot chunks. Does it also justify refusing to allocate from segments that are already in the buddy free lists? Those were either freed after an earlier successful sync or never used.

Would it work to stop queuing only when hot_chunks_num reaches the array capacity, leaving those segments reserved, and to retry the sync on later frees? That avoids the overflow and keeps the no-recycle rule without disabling the pool permanently.

Quoted 3 lines
drivers/net/ethernet/mellanox/mlx5/core/steering/sws/dr_icm_pool.c +0 -0
 		return err;
 	}
 

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261118173227.1957670-1-tariqt%40nvidia.com
#7 lore raw
In reply to Tariq Toukan
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- What hardware the change was tested on. For driver fixes please mention the device (and if relevant firmware version) used for testing, or say that the change was not tested on real hardware.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#8 lore raw
In reply to Tariq Toukan
This series was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 10 lines · On Wed, 18 Nov 2026 19:32:25 +0200 you wrote:

Hi,

This series by Yevgeny fixes two memory safety bugs in the mlx5 software and hardware steering code paths.

Both are missing-check or wrong-handling problems that lead to a NULL pointer being inserted into an xarray, or an out-of-bounds write that corrupts the kernel heap.

[...]

Here is the summary with links:
- [net,V2,1/2] net/mlx5: HWS, Handle NULL in vport destination action creation

    https://git.kernel.org/netdev/net/c/c0ec15a58655
  - [net,V2,2/2] net/mlx5: DR, Fix out-of-bounds write when sync to HW fails
    https://git.kernel.org/netdev/net/c/0e56ff4441ee

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html