net: mana: HW channel reliability and hardening fixes
Subsystems (5)
v5 cover net: mana: HW channel reliability and hardening fixes
This series fixes a set of latent bugs and robustness gaps in the MANA Hardware Channel (HWC), the control path the driver uses to talk to the device. The issues range from a use-after-free of completion queues during teardown to buffer mis-sizing, unsafe teardown ordering, missing validation of device-supplied RX metadata, and stale-response handling after a command timeout.
Patch overview:
1 RCU-protect gc->cq_table lookups against concurrent CQ destroy
The EQ interrupt handler dereferences CQ pointers from gc->cq_table while teardown can free them. Put the table under RCU and wait a grace period before freeing, closing the use-after-free.
2 fix HWC RQ/SQ buffer size swap
init_queues() sized the RQ with max_req_msg_size and the SQ with max_resp_msg_size -- backwards. Correct the swap; both sizes are equal in practice, so this is a latent-correctness fix.
3 free HWC comp_buf after destroying the EQ
Reorder teardown so the EQ is destroyed (readers quiesced) before comp_buf and the CQ are freed, preventing a late EQ-handler access to freed memory.
4 validate hardware-supplied values in the HWC RX path
Bounds-check the SGE, verify the recovered slot index and SGE address, and validate response length and msg_id before use, so malformed or hostile DMA metadata cannot cause wrong-slot completion or out-of-bounds access.
5 fix HWC teardown safety with setup_active flag and destroy ordering
Track setup activation explicitly, tear the EQ/CQ down before the TXQ/RXQ, and on an unrecoverable teardown failure leak the HWC resources rather than free memory the device may still DMA into.
6 fix stale HWC response after command timeout
Replace the inflight-slot semaphore with a bitmap + waitqueue and per-slot refcount/lock; latch the channel on timeout so no new slots are handed out, drop duplicate/late responses, and ignore a zero firmware-supplied timeout.
7 keep max_num_cqs immutable once cq_table is allocated
gc->max_num_cqs is set once when cq_table is allocated and never reset, so a spoofed post-init HWC event cannot inflate the bound past the allocation and drive an out-of-bounds cq_table access.
Follow-up feature work (net-next, sent separately):
The original series also contained two patches that are improvements, not fixes:
net: mana: support concurrent HWC requests
net: mana: add dynamic HWC queue depth with reinit path
Per the netdev tree rules, fixes go to 'net' and features/improvements go to 'net-next', and the two must not be combined in a single submission. Those two patches build on the locking and teardown groundwork in this series, so they will be posted as a separate net-next series only after these fixes have propagated from net into net-next through the usual periodic merge.
Changes since v4:
- No code changes. Resending as a standalone thread; v4 was accidentally sent in-reply-to the v3 thread.
- v4: https://lore.kernel.org/netdev/[email protected]/
- v3: https://lore.kernel.org/netdev/[email protected]/
Changes since v3:
Addressed the netdev-ai and sashiko.dev automated reviews of v3.
- New patch 7 ("keep max_num_cqs immutable once cq_table is allocated"): gc->max_num_cqs is set once and never reset, so a spoofed post-init HWC event cannot inflate the bound past the allocation and cause an out-of-bounds cq_table access.
- patch 1: replaced the per-CQ synchronize_rcu() in the netdev teardown paths with a two-pass quiesce/free that takes a single grace period per teardown; snapshot cq->id and max_num_cqs with READ_ONCE() so the same value sizes, bounds and indexes cq_table; corrected the gc->cq_table lifetime comment; rescoped the changelog to the use-after-free fix.
- patch 2: reworded the changelog as a latent-correctness fix (both message sizes are 0x1000, so the swap has no observable overflow) and dropped the incorrect note about hoisting the queue dimensions.
- patch 4: removed the short-response early return so a malformed response reaches verify_resp_msg() -> -EPROTO and completes the sender instead of hanging it; account leaked RX WQEs and trip hwc_timeout on RQ exhaustion; read the device-supplied inline_oob_size_div4 (through its u32 flags word, as it is a bit-field) and sge->address with READ_ONCE() and reject any value other than the one the driver programs; reframed the msg_id check as defense in depth.
- patch 5: arm setup_active immediately after mana_smc_setup_hwc() succeeds; destroy the EQ (IRQ deregister + drain) before the CQ; drop the redundant teardown in mana_hwc_establish_channel() that caused a double hardware timeout and masked the original error.
- patch 6: take both the sender and response-side references up front in mana_hwc_get_msg_index() (refcount initialised to 2, under the lock that publishes the slot) so an early/stale/forged response cannot free the slot before the sender posts; changed caller_ctx::error from u32 to int; reject a zero firmware-supplied HWC timeout in the query path as well as the reconfig path; access hwc_timed_out with READ_ONCE()/ WRITE_ONCE(); comment and changelog fixes.
- Assorted comment and commit-message clarifications throughout.
Changes since v2:
- Per maintainer feedback, split the original combined series: the fixes here target 'net'; the two feature patches now go to 'net-next' and are sent separately (see above). Rebased the fixes onto net.
- Dropped the pcie_flr()-based reset fallback from the teardown path. pcie_flr() resets device config without the save/restore that pci_reset_function() provides, and cannot be used as a drop-in here. On an unrecoverable teardown failure the driver now leaks the HWC resources instead of touching memory the device may still DMA into.
- patch 2: store the HWC queue dimensions before creating the CQ so the RX completion handler can never observe a zero max_resp_msg_size divisor or a stale num_inflight_msg bound.
- Assorted commit-message and comment clarifications.
Long Li (7):
net: mana: RCU-protect gc->cq_table lookups against concurrent CQ
destroy
net: mana: fix HWC RQ/SQ buffer size swap
net: mana: free HWC comp_buf after destroying the EQ
net: mana: validate hardware-supplied values in the HWC RX path
net: mana: fix HWC teardown safety with setup_active flag and destroy
ordering
net: mana: fix stale HWC response after command timeout
net: mana: keep max_num_cqs immutable once cq_table is allocated
drivers/infiniband/hw/mana/cq.c | 46 +- .../net/ethernet/microsoft/mana/gdma_main.c | 48 +- .../net/ethernet/microsoft/mana/hw_channel.c | 452 +++++++++++++++--- drivers/net/ethernet/microsoft/mana/mana_en.c | 136 ++++-- include/net/mana/gdma.h | 40 +- include/net/mana/hw_channel.h | 44 +- 6 files changed, 652 insertions(+), 114 deletions(-)
base-commit: dd057113ac7ba5bdd2aed3d9405305911152f911
Signature · 1 line
2.43.0
PATCH v5 1/7 net: mana: RCU-protect gc->cq_table lookups against concurrent CQ destroy
The EQ interrupt handler (mana_gd_process_eqe) looks up the completing CQ in gc->cq_table[cq_id] and runs its callback, concurrently with CQ teardown on another CPU that clears the slot and frees the CQ. cq_table was a plain pointer array freed with no grace period, so the two race into a use-after-free:
CPU A (mana_gd_intr, hard IRQ) CPU B (CQ destroy)
---------------------------------- ------------------------------
cq = gc->cq_table[cq_id]; // valid
gc->cq_table[id] = NULL;
kfree(cq); // freed
cq->cq.callback(ctx, cq); // use-after-free
The handler's existing rcu_read_lock() only guards the per-IRQ EQ list traversal; cq_table was never under any RCU contract, and a read-side lock is inert unless the freer also defers the free past a grace period.
Put cq_table under RCU: annotate the base pointer and entries __rcu, read with rcu_dereference() in the handler, publish with rcu_assign_pointer(), and on teardown clear the slot then synchronize_rcu() before freeing the CQ. The grace period blocks until every in-flight handler has dropped the old pointer, so the kfree() can no longer race the callback.
This fixes only the CQ lifetime (the use-after-free); it does not make the cq_id bound trustworthy. gc->max_num_cqs is still range-checked outside the published table, and hardening that field against a spoofed device value is a separate change.
netdev teardown destroys a CQ per TX and per RX queue, so one grace period each in mana_gd_destroy_cq() would serialize up to
2 * MANA_MAX_NUM_QUEUES synchronize_rcu() calls under RTNL on every ifdown, MTU change or ring/channel reconfigure. Clear all of a port's CQ slots first and take a single grace period per teardown instead:mana_gd_unpublish_cq() clears a slot without waiting, and mana_gd_destroy_cq() -- which still serves the single-CQ callers -- finds the slot already cleared and skips its own synchronize_rcu().
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- Replaced the per-CQ synchronize_rcu() in the netdev teardown paths with a two-pass quiesce/free that takes one grace period per teardown; mana_gd_unpublish_cq() splits the slot-clear from the grace period.
- Snapshot cq->id and max_num_cqs with READ_ONCE() in mana_hwc_establish_channel() so one value sizes, bounds and indexes cq_table.
- Corrected the gc->cq_table lifetime comment in gdma.h; rescoped the changelog to the use-after-free fix (the bound is patch 7).
drivers/infiniband/hw/mana/cq.c | 46 +++++- .../net/ethernet/microsoft/mana/gdma_main.c | 41 ++++-- .../net/ethernet/microsoft/mana/hw_channel.c | 29 ++-- drivers/net/ethernet/microsoft/mana/mana_en.c | 136 ++++++++++++++---- include/net/mana/gdma.h | 32 ++++- 5 files changed, 234 insertions(+), 50 deletions(-)
drivers/infiniband/hw/mana/cq.c +40 -6
@@ -131,12 +131,20 @@ static void mana_ib_cq_handler(void *ctx, struct gdma_queue *gdma_cq)int mana_ib_install_cq_cb (struct mana_ib_dev *mdev,struct mana_ib_cq *cq) {struct gdma_context *gc =mdev_to_gc (mdev); +struct gdma_queue __rcu **cq_table;struct gdma_queue *gdma_cq; -if (cq->queue.id >= gc->max_num_cqs) +/* No rcu_read_lock(): install/remove run within the IB device +* lifetime, which mana_rdma_remove() (ib_unregister_device) drains +* before the base cq_table can be freed. See gdma_context::cq_table +* in gdma.h for why "true" is sound. +*/ + cq_table =rcu_dereference_protected (gc->cq_table, true); +if (!cq_table || cq->queue.id >= gc->max_num_cqs)return -EINVAL ; +/* Create CQ table entry, sharing a CQ between WQs is not supported */ -if (gc->cq_table[cq->queue.id]) +if (rcu_access_pointer (cq_table[cq->queue.id]))return -EINVAL ;if (cq->queue.kmem) gdma_cq = cq->queue.kmem;
@@ -149,23 +157,49 @@ int mana_ib_install_cq_cb(struct mana_ib_dev *mdev, struct mana_ib_cq *cq) gdma_cq->type =GDMA_CQ ; gdma_cq->cq.callback = mana_ib_cq_handler; gdma_cq->id = cq->queue.id; - gc->cq_table[cq->queue.id] = gdma_cq; +rcu_assign_pointer (cq_table[cq->queue.id], gdma_cq);return 0 ; }void mana_ib_remove_cq_cb (struct mana_ib_dev *mdev,struct mana_ib_cq *cq) {struct gdma_context *gc =mdev_to_gc (mdev); +struct gdma_queue __rcu **cq_table; +struct gdma_queue *gdma_cq; -if (cq->queue.id >= gc->max_num_cqs || cq->queue.id ==INVALID_QUEUE_ID ) +if (cq->queue.id ==INVALID_QUEUE_ID )return ;if (cq->queue.kmem)/* Then it will be cleaned and removed by the mana */ return ; -kfree (gc->cq_table[cq->queue.id]); - gc->cq_table[cq->queue.id] =NULL ; +/* No rcu_read_lock(): like mana_ib_install_cq_cb(), this runs within +* the IB device lifetime that mana_rdma_remove() drains before the +* base cq_table can be freed. See gdma_context::cq_table in gdma.h. +*/ + cq_table =rcu_dereference_protected (gc->cq_table, true); +if (!cq_table || cq->queue.id >= gc->max_num_cqs) +return ; +/* Removers for a given CQ are serialized by the IB core, so the slot +* is read and cleared without rcu_read_lock() or atomicity: a CQ is +* never torn down while a live QP references it (cq->usecnt), nor +* while the QP-create that installed the entry is still running (that +* create holds a reference on the CQ uobject across its error path, +* before usecnt is taken). Any double-remove is therefore sequential +* -- the later caller sees the NULL stored below and returns. +*/ + gdma_cq =rcu_dereference_protected (cq_table[cq->queue.id], true); +if (!gdma_cq) +return ;/* already removed by a prior teardown path */ + +rcu_assign_pointer (cq_table[cq->queue.id],NULL ); + +/* Wait for in-flight EQ handlers that may have loaded the old +* pointer via rcu_dereference() to finish before freeing. +*/ +synchronize_rcu (); +kfree (gdma_cq); }int mana_ib_arm_cq (struct ib_cq *ibcq,enum ib_cq_notify_flags flags)
drivers/net/ethernet/microsoft/mana/gdma_main.c +33 -8
67 lines not on this page · raw message
drivers/net/ethernet/microsoft/mana/hw_channel.c +21 -8
@@ -674,6 +674,7 @@ static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth,struct gdma_queue *sq = hwc->txq->gdma_wq;struct gdma_queue *eq = hwc->cq->gdma_eq;struct gdma_queue *cq = hwc->cq->gdma_cq; +struct gdma_queue __rcu **cq_table;int err;init_completion (&hwc->hwc_init_eqe_comp);
@@ -698,11 +699,15 @@ static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth,if (WARN_ON (cq->id >= gc->max_num_cqs))return -EPROTO ; - gc->cq_table =vcalloc (gc->max_num_cqs,sizeof (struct gdma_queue *)); -if (!gc->cq_table) + cq_table =vcalloc (gc->max_num_cqs,sizeof (*cq_table)); +if (!cq_table)return -ENOMEM ; - gc->cq_table[cq->id] = cq; +rcu_assign_pointer (cq_table[cq->id], cq); +/* Publish the fully-initialised table last; pairs with the +* rcu_dereference(gc->cq_table) in mana_gd_process_eqe(). +*/ +rcu_assign_pointer (gc->cq_table, cq_table);return 0 ; }
@@ -811,6 +816,7 @@ int mana_hwc_create_channel(struct gdma_context *gc)void mana_hwc_destroy_channel (struct gdma_context *gc) {struct hw_channel_context *hwc = gc->hwc.driver_data; +struct gdma_queue __rcu **old_cq_table;if (!hwc)return ;
@@ -818,10 +824,8 @@ void mana_hwc_destroy_channel(struct gdma_context *gc)/* gc->max_num_cqs is set in mana_hwc_init_event_handler(). If it's * non-zero, the HWC worked and we should tear down the HWC here. */ -if (gc->max_num_cqs >0 ) { +if (gc->max_num_cqs >0 )mana_smc_teardown_hwc (&gc->shm_channel, false); - gc->max_num_cqs =0 ; - }if (hwc->txq)mana_hwc_destroy_wq (hwc, hwc->txq);
@@ -832,6 +836,14 @@ void mana_hwc_destroy_channel(struct gdma_context *gc)if (hwc->cq)mana_hwc_destroy_cq (hwc->gdma_dev->gdma_context, hwc->cq); +/* Reset only after mana_hwc_destroy_cq() above has run with a valid +* max_num_cqs so mana_gd_destroy_cq() clears the CQ table slot and +* waits out in-flight EQ handlers (synchronize_rcu) before the CQ is +* freed. Clearing it earlier would make that path early-return and +* skip the slot clear, leaving a dangling cq_table entry. +*/ + gc->max_num_cqs =0 ; +kfree (hwc->caller_ctx); hwc->caller_ctx =NULL ;
@@ -848,8 +860,9 @@ void mana_hwc_destroy_channel(struct gdma_context *gc) gc->hwc.driver_data =NULL ; gc->hwc.gdma_context =NULL ; -vfree (gc->cq_table); - gc->cq_table =NULL ; + old_cq_table =rcu_replace_pointer (gc->cq_table,NULL , true); +synchronize_rcu (); +vfree (old_cq_table); } int mana_hwc_send_request(struct hw_channel_context *hwc, u32 req_len,
drivers/net/ethernet/microsoft/mana/mana_en.c +109 -27
220 lines not on this page · raw message
include/net/mana/gdma.h +31 -1
@@ -418,7 +418,31 @@ struct gdma_context {/* This maps a CQ index to the queue structure. */ unsigned int max_num_cqs; -struct gdma_queue **cq_table; +/* Both the base pointer and each entry are RCU-managed. The fast +* path (mana_gd_process_eqe) reads the base via rcu_dereference() +* under rcu_read_lock(), so the table is freed with +* rcu_assign_pointer(NULL) + synchronize_rcu() and an in-flight +* reader can never observe freed memory. +* +* The slow paths -- mana_gd_destroy_cq() and the CQ install/remove +* callers (mana_create_txq/_rxq, mana_ib_install/remove_cq_cb) -- +* instead read the base with rcu_dereference_protected(cq_table, +* true). The bare "true" asserts teardown/bring-up ordering, not a +* lock: the base table is allocated in mana_hwc_establish_channel() +* and replaced+freed only by mana_hwc_destroy_channel() (via +* mana_gd_cleanup_device()) and the create-time reinit. The reinit +* runs before either consumer is probed, and cleanup_device() runs +* after mana_remove() / mana_rdma_remove() have detached the ports +* under RTNL and drained the IB device, so no install/remove caller +* is running when the base is freed. This is an ordering argument +* about when cleanup_device() runs: suspend and shutdown keep the +* netdev registered, so it does not rely on unregister_netdevice() +* having run on every path. mana_hwc_destroy_channel() itself reads +* cq_table (mana_hwc_destroy_cq()) before it replaces and vfree()s +* the base, so that access is ordered ahead of the free by program +* order. +*/ +struct gdma_queue __rcu *__rcu *cq_table;/* Protect eq_test_event and test_event_eq_id */ struct mutex eq_test_event_mutex;
@@ -496,6 +520,12 @@ int mana_gd_create_mana_wq_cq(struct gdma_dev *gd,void mana_gd_destroy_queue (struct gdma_context *gc,struct gdma_queue *queue); +/* Clear a CQ's cq_table slot without waiting for a grace period. Batched +* teardown paths clear several slots and then take a single synchronize_rcu(); +* single-CQ callers use mana_gd_destroy_cq() instead, which also waits. +*/ +bool mana_gd_unpublish_cq (struct gdma_context *gc,struct gdma_queue *queue); +int mana_gd_poll_cq (struct gdma_queue *cq,struct gdma_comp *comp,int num_cqe);void mana_gd_ring_cq (struct gdma_queue *cq,u8 arm_bit);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net v5 1/7] net: mana: RCU-protect gc->cq_table lookups against concurrent CQ destroy
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Missing pointer verification in `mana_gd_unpublish_cq()` and `mana_ib_remove_cq_cb()` causes them…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Missing pointer verification in mana_gd_unpublish_cq() and mana_ib_remove_cq_cb() causes them to incorrectly clear valid, concurrent CQs (double-unpublish race) and inadvertently unpublish CQ 0 on error paths. --
--- Patch [1]: [PATCH net v5 1/7] net: mana: RCU-protect gc->cq_table lookups against concurrent CQ destroy --- commit 882f7728097f3bfd4b17e15e31d6c14542d507c2
Author: Long Li <[email protected]>
Subject: net: mana: RCU-protect gc->cq_table lookups against concurrent CQ destroy
This patch puts the gc->cq_table under RCU protection to prevent a use-after-free race between the EQ interrupt handler and concurrent CQ teardowns. It also introduces a two-pass batched teardown to optimize synchronize_rcu() usage during network device teardowns.
Quoted 4 lines
drivers/infiniband/hw/mana/cq.c +0
-0
[ ... ]
Quoted 10 lines
drivers/infiniband/hw/mana/cq.c +1
-1@@ -149,23 +157,49 @@ int mana_ib_install_cq_cb(struct mana_ib_dev *mdev, struct mana_ib_cq *cq) gdma_cq->cq.callback = mana_ib_cq_handler; gdma_cq->id = cq->queue.id; - gc->cq_table[cq->queue.id] = gdma_cq; +rcu_assign_pointer (cq_table[cq->queue.id], gdma_cq);return 0 ; }void mana_ib_remove_cq_cb (struct mana_ib_dev *mdev,struct mana_ib_cq *cq) {
[ ... ]
drivers/infiniband/hw/mana/cq.c int mana_ib_install_cq_cb(struct mana_ib_dev *mdev, struct mana_ib_cq *cq) · line 196
Quoted 13 lines
drivers/infiniband/hw/mana/cq.c +13
-0+/* Removers for a given CQ are serialized by the IB core, so the slot +* is read and cleared without rcu_read_lock() or atomicity: a CQ is +* never torn down while a live QP references it (cq->usecnt), nor +* while the QP-create that installed the entry is still running (that +* create holds a reference on the CQ uobject across its error path, +* before usecnt is taken). Any double-remove is therefore sequential +* -- the later caller sees the NULL stored below and returns. +*/ + gdma_cq =rcu_dereference_protected (cq_table[cq->queue.id], true); +if (!gdma_cq) +return ;/* already removed by a prior teardown path */ + +rcu_assign_pointer (cq_table[cq->queue.id],NULL );
[Severity: High]
Does this RDMA cleanup path need to verify that the gdma_cq read from the table actually matches the expected one (such as cq->queue.kmem)?
If a hardware ID was reused by a concurrent operation, could this clear a newly created and published CQ?
Quoted 11 lines
drivers/infiniband/hw/mana/cq.c +6
-0+ +/* Wait for in-flight EQ handlers that may have loaded the old +* pointer via rcu_dereference() to finish before freeing. +*/ +synchronize_rcu (); +kfree (gdma_cq); }drivers/net/ethernet/microsoft/mana/gdma_main.c +0
-0
[ ... ]
Quoted 18 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +7
-4@@ -1050,18 +1052,41 @@ static void mana_gd_create_cq(const struct gdma_queue_spec *spec, queue->cq.callback = spec->cq.callback; } -static void mana_gd_destroy_cq (struct gdma_context *gc, -struct gdma_queue *queue) +bool mana_gd_unpublish_cq (struct gdma_context *gc,struct gdma_queue *queue) { +struct gdma_queue __rcu **cq_table;u32 id = queue->id; -if (id >= gc->max_num_cqs) -return ; +/* No rcu_read_lock() here: unpublish runs only on the +* CQ-destroy/teardown path, where the base cq_table is stable. See +* the lifecycle note on gdma_context::cq_table in gdma.h for why the +* "true" predicate is sound. +*/
drivers/net/ethernet/microsoft/mana/gdma_main.c static void mana_gd_create_cq(const struct gdma_queue_spec *spec, · line 1074
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/gdma_main.c +10
-0+ cq_table =rcu_dereference_protected (gc->cq_table, true); +if (!cq_table || id >= gc->max_num_cqs) +return false; + +if (!rcu_access_pointer (cq_table[id])) +return false; + +rcu_assign_pointer (cq_table[id],NULL ); +return true; +}
[Severity: High]
Should this check if the pointer actually matches the queue being unpublished?
If a hardware ID is 0 on a failure path, could this unintentionally clear the slot for whatever valid CQ currently holds ID 0?
Verifying that rcu_access_pointer(cq_table[id]) equals the queue variable could prevent blindly clearing the wrong CQ.
Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0
-0
[ ... ]
Quoted 16 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10
-2@@ -2448,8 +2454,24 @@ static void mana_destroy_txq(struct mana_port_context *apc) apc->tx_qp[i]->txq.napi_initialized = false; } -if (apc->tx_qp[i]->tx_object !=INVALID_MANA_HANDLE ) -mana_destroy_wq_obj (apc,GDMA_SQ , apc->tx_qp[i]->tx_object); +if (apc->tx_qp[i]->tx_object !=INVALID_MANA_HANDLE ) { +mana_destroy_wq_obj (apc,GDMA_SQ , + apc->tx_qp[i]->tx_object); + apc->tx_qp[i]->tx_object =INVALID_MANA_HANDLE ; + } + +if (apc->tx_qp[i]->tx_cq.gdma_cq) +mana_gd_unpublish_cq (gc, apc->tx_qp[i]->tx_cq.gdma_cq); + } +
drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_destroy_txq(struct mana_port_context *apc) · line 2476
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +8
-0+synchronize_rcu (); + +/* Pass 2: the slots are clear, so mana_gd_destroy_cq() skips its own +* grace period; free the CQ, the TXQ and the queue pair. +*/ +for (i =0 ; i < apc->num_queues; i++) { +if (!apc->tx_qp[i]) +continue ;mana_deinit_cq (apc, &apc->tx_qp[i]->tx_cq);
[Severity: High]
Does this create a race window during the two-pass teardown?
Since mana_deinit_cq() calls mana_gd_destroy_cq(), which in turn calls mana_gd_unpublish_cq() a second time for the same queue, could a newly created RDMA CQ that recycled this ID during the synchronize_rcu() wait be incorrectly destroyed here?
If mana_gd_unpublish_cq() only checks for a non-NULL slot, it seems like it might blindly clear the newly published CQ on this second pass.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v5 2/7 net: mana: fix HWC RQ/SQ buffer size swap
In reply to Long LiThe HWC RQ receives responses and the SQ sends requests, but mana_hwc_init_queues() sized the RQ with max_req_msg_size and the SQ with max_resp_msg_size -- backwards. mana_hwc_rx_event_handler() also recovered the RX slot index by dividing by max_req_msg_size instead of the response size that strides the RQ buffer.
Both mistakes are latent today: the only caller passes HW_CHANNEL_MAX_REQUEST_SIZE and HW_CHANNEL_MAX_RESPONSE_SIZE, which are both 0x1000, and the queues are never re-created with the hardware-reported sizes (those are only used by mana_hwc_test_channel()), so max_req_msg_size == max_resp_msg_size on every path and there is no observable overflow or mis-indexing. The bug would only surface if the two sizes ever diverged.
Size the RQ by max_resp_msg_size and the SQ by max_req_msg_size, store max_resp_msg_size in hw_channel_context, and use it as the RX slot stride so the code is correct regardless of the two sizes.
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- Reworded the changelog as a latent-correctness fix (both message sizes are 0x1000, so the swap has no observable overflow) and dropped the note about hoisting the queue dimensions above mana_hwc_create_cq().
drivers/net/ethernet/microsoft/mana/hw_channel.c | 7 ++++--- include/net/mana/hw_channel.h | 1 + 2 files changed, 5 insertions(+), 3 deletions(-)
drivers/net/ethernet/microsoft/mana/hw_channel.c +4 -3
@@ -263,7 +263,7 @@ static void mana_hwc_rx_event_handler(void *ctx, u32 gdma_rxq_id,/* Select the RX work request for virtual address and for reposting. */ rq_base_addr = hwc_rxq->msg_buf->mem_info.dma_handle; - rx_req_idx = (sge->address - rq_base_addr) / hwc->max_req_msg_size; + rx_req_idx = (sge->address - rq_base_addr) / hwc->max_resp_msg_size;if (rx_req_idx >= hwc_rxq->msg_buf->num_reqs) { dev_err(hwc->dev, "HWC RX: wrong rx_req_idx=%llu, num_reqs=%u\n",
@@ -733,14 +733,14 @@ static int mana_hwc_init_queues(struct hw_channel_context *hwc, u16 q_depth, goto out; } - err =mana_hwc_create_wq (hwc,GDMA_RQ , q_depth, max_req_msg_size, + err =mana_hwc_create_wq (hwc,GDMA_RQ , q_depth, max_resp_msg_size, hwc->cq, &hwc->rxq);if (err) {dev_err (hwc->dev,"Failed to create HWC RQ: %d\n" , err); goto out; } - err =mana_hwc_create_wq (hwc,GDMA_SQ , q_depth, max_resp_msg_size, + err =mana_hwc_create_wq (hwc,GDMA_SQ , q_depth, max_req_msg_size, hwc->cq, &hwc->txq);if (err) {dev_err (hwc->dev,"Failed to create HWC SQ: %d\n" , err);
@@ -749,6 +749,7 @@ static int mana_hwc_init_queues(struct hw_channel_context *hwc, u16 q_depth, hwc->num_inflight_msg = q_depth; hwc->max_req_msg_size = max_req_msg_size; + hwc->max_resp_msg_size = max_resp_msg_size;return 0 ; out:
include/net/mana/hw_channel.h +1 -0
@@ -181,6 +181,7 @@ struct hw_channel_context {u16 num_inflight_msg;u32 max_req_msg_size; +u32 max_resp_msg_size;u16 hwc_init_q_depth_max;u32 hwc_init_max_req_msg_size;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net v5 2/7] net: mana: fix HWC RQ/SQ buffer size swap
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Use-after-free on stack buffer due to missing synchronization between timeout handler and RX completion.
- [High] Teardown sequence in mana_hwc_destroy_channel() frees WQs before quiescing the CQ, causing a UAF if an in-flight EQ handler runs concurrently.
- [High] In a Confidential VM setup, a malicious host can modify the unencrypted DMA memory field inline_oob_size_div4 in the receive queue ring buffer to cause the guest to read past the end of the ring buffer allocation, resulting in a kernel panic. --
--- Patch [2]: [PATCH net v5 2/7] net: mana: fix HWC RQ/SQ buffer size swap --- commit b0ce57145f46d4e8a027e81baea5f74199a24b8e
Author: Long Li <[email protected]>
net: mana: fix HWC RQ/SQ buffer size swap
The HWC RQ receives responses and the SQ sends requests, but mana_hwc_init_queues() sized the RQ with max_req_msg_size and the SQ with max_resp_msg_size -- backwards. mana_hwc_rx_event_handler() also recovered the RX slot index by dividing by max_req_msg_size instead of the response size that strides the RQ buffer.
drivers/net/ethernet/microsoft/mana/hw_channel.c static void mana_hwc_rx_event_handler(void *ctx, u32 gdma_rxq_id, · line 266
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +1
-1@@ -263,7 +263,7 @@ static void mana_hwc_rx_event_handler(void *ctx, u32 gdma_rxq_id,/* Select the RX work request for virtual address and for reposting. */ rq_base_addr = hwc_rxq->msg_buf->mem_info.dma_handle; - rx_req_idx = (sge->address - rq_base_addr) / hwc->max_req_msg_size; + rx_req_idx = (sge->address - rq_base_addr) / hwc->max_resp_msg_size;
[Severity: High]
This isn't a bug introduced by this patch, but does mana_hwc_destroy_channel() in hw_channel.c free the WQs before quiescing the CQ?
In mana_hwc_destroy_channel():
if (hwc->rxq) mana_hwc_destroy_wq(hwc, hwc->rxq); if (hwc->cq) mana_hwc_destroy_cq(hwc->gdma_dev->gdma_context, hwc->cq);
If an EQ interrupt fires concurrently before mana_hwc_destroy_cq() is called, can mana_gd_process_eq_events() look up the still-published CQ, invoking mana_hwc_rx_event_handler() which dereferences the freed hwc->rxq here?
[Severity: High]
This is a pre-existing issue, but can a malicious host cause an out-of-bounds dereference when we read sge->address here?
Earlier in mana_hwc_rx_event_handler():
sge = (struct gdma_sge *)(wqe + 8 + dma_oob->inline_oob_size_div4 * 4);
If this is a Confidential VM, dma_oob resides in unencrypted DMA memory shared with the host. Can the host supply an arbitrarily large inline_oob_size_div4, forcing the guest kernel to dereference unmapped memory when reading sge->address?
[Severity: Critical]
This is a pre-existing issue, but is there a use-after-free on the stack buffer if mana_hwc_send_request() times out?
If mana_hwc_send_request() times out, it clears the inflight bit and returns, leaving ctx->output_buf pointing to an invalid stack frame.
Concurrently, mana_hwc_rx_event_handler() calls mana_hwc_handle_resp() which does a lockless test_bit():
if (!test_bit(msg_id, hwc->inflight_msg_res.map)) return; ... memcpy(ctx->output_buf, resp_msg, resp_len);
If the timeout thread clears the bit and returns just after the test_bit() check in mana_hwc_handle_resp(), will we copy the response data into a popped stack frame?
Quoted 3 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-0if (rx_req_idx >= hwc_rxq->msg_buf->num_reqs) { dev_err(hwc->dev, "HWC RX: wrong rx_req_idx=%llu, num_reqs=%u\n",
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v5 3/7 net: mana: free HWC comp_buf after destroying the EQ
In reply to Long Limana_hwc_destroy_cq() freed hwc_cq->comp_buf and destroyed the CQ before the EQ. That was unsafe while the EQ was still registered: the EQ interrupt handler reaches comp_buf via mana_hwc_comp_event() and the CQ object (hwc->cq->gdma_cq) via mana_hwc_init_event_handler(), so a late EQE dispatched after the free could touch freed memory.
Destroy the EQ first. mana_gd_destroy_queue() on the EQ deregisters its IRQ and waits out in-flight handlers, fencing all EQE dispatch; only then free the CQ and comp_buf.
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- No functional change since v3; the teardown-ordering guarantees this patch relies on are made explicit in patch 5.
.../net/ethernet/microsoft/mana/hw_channel.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
drivers/net/ethernet/microsoft/mana/hw_channel.c +14 -4
@@ -384,14 +384,24 @@ static void mana_hwc_comp_event(void *ctx, struct gdma_queue *q_self)static void mana_hwc_destroy_cq (struct gdma_context *gc,struct hwc_cq *hwc_cq) { -kfree (hwc_cq->comp_buf); +/* Destroy the EQ before the CQ. mana_gd_destroy_queue() on the EQ +* deregisters its IRQ and waits out in-flight handlers, fencing all +* EQE dispatch — both the completion path and HWC init/reconfig +* events. Freeing the CQ first would leave the EQ live and able to +* dispatch an event that dereferences hwc->cq->gdma_cq (e.g. +* mana_hwc_init_event_handler()) after it has been freed. +*/ +if (hwc_cq->gdma_eq) +mana_gd_destroy_queue (gc, hwc_cq->gdma_eq); +/* comp_buf is reached only by mana_hwc_comp_event(), invoked from +* the now-fenced EQ handler, so it is safe to free once the EQ and +* CQ are gone. +*/ if (hwc_cq->gdma_cq)mana_gd_destroy_queue (gc, hwc_cq->gdma_cq); -if (hwc_cq->gdma_eq) -mana_gd_destroy_queue (gc, hwc_cq->gdma_eq); - +kfree (hwc_cq->comp_buf);kfree (hwc_cq); }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net v5 3/7] net: mana: free HWC comp_buf after destroying the EQ
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [Critical] A TOCTOU race condition between the HWC request timeout path and the response interrupt handler allows the handler to perform a memcpy into a deallocated stack frame.
- [High] Pre-existing use-after-free risk in mana_hwc_destroy_channel due to WQs (txq and rxq) being destroyed before the EQ is safely fenced.
- [High] If HWC channel establishment times out, hardware teardown is skipped, allowing the hardware to DMA into freed queue memory.
--
--- Patch [3]: [PATCH net v5 3/7] net: mana: free HWC comp_buf after destroying the EQ --- commit 20a39692a14893099232cb3a9085ec258236f7dc
Author: Long Li <[email protected]>
net: mana: free HWC comp_buf after destroying the EQ
mana_hwc_destroy_cq() freed hwc_cq->comp_buf and destroyed the CQ before the EQ. That was unsafe while the EQ was still registered: the EQ interrupt handler reaches comp_buf via mana_hwc_comp_event() and the CQ object (hwc->cq->gdma_cq) via mana_hwc_init_event_handler(), so a late EQE dispatched after the free could touch freed memory.
Quoted 8 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-0@@ -384,14 +384,24 @@ static void mana_hwc_comp_event(void *ctx, struct gdma_queue *q_self)static void mana_hwc_destroy_cq (struct gdma_context *gc,struct hwc_cq *hwc_cq) {
drivers/net/ethernet/microsoft/mana/hw_channel.c static void mana_hwc_comp_event(void *ctx, struct gdma_queue *q_self) · line 395
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +9
-1-kfree (hwc_cq->comp_buf); +/* Destroy the EQ before the CQ. mana_gd_destroy_queue() on the EQ +* deregisters its IRQ and waits out in-flight handlers, fencing all +* EQE dispatch — both the completion path and HWC init/reconfig +* events. Freeing the CQ first would leave the EQ live and able to +* dispatch an event that dereferences hwc->cq->gdma_cq (e.g. +* mana_hwc_init_event_handler()) after it has been freed. +*/ +if (hwc_cq->gdma_eq) +mana_gd_destroy_queue (gc, hwc_cq->gdma_eq);
[Severity: High]
This isn't a bug introduced by this patch, but while checking the teardown ordering, could there be a similar use-after-free risk for the WQs?
In mana_hwc_destroy_channel(), the WQs are destroyed before the EQ is safely fenced by mana_hwc_destroy_cq():
drivers/net/ethernet/microsoft/mana/hw_channel.c:mana_hwc_destroy_channel() {
...
mana_hwc_destroy_wq(hwc, hwc->rxq);
mana_hwc_destroy_wq(hwc, hwc->txq);
mana_hwc_destroy_cq(hwc->gdma_dev->gdma_context, hwc->cq);
...
}If an in-flight EQ interrupt handler runs during this window, it will invoke mana_hwc_comp_event(), which calls mana_hwc_rx_event_handler(). The handler blindly dereferences the freed hwc->rxq:
drivers/net/ethernet/microsoft/mana/hw_channel.c:mana_hwc_rx_event_handler() {
...
if (WARN_ON_ONCE(hwc_rxq->gdma_wq->id != gdma_rxq_id))
...
}Will this cause a use-after-free if an interrupt arrives after the WQs are freed but before mana_hwc_destroy_cq() fences the EQ?
[Severity: High]
This is a pre-existing issue, but does skipping hardware teardown on a timeout lead to DMA into freed memory?
In mana_hwc_establish_channel(), if hardware initialization times out:
drivers/net/ethernet/microsoft/mana/hw_channel.c:mana_hwc_establish_channel() {
...
if (!wait_for_completion_timeout(&hwc->hwc_init_eqe_comp, 60 * HZ))
return -ETIMEDOUT;
...
}The hardware channel remains active while gc->max_num_cqs remains 0. When the error path calls mana_hwc_destroy_channel(), the teardown is skipped:
drivers/net/ethernet/microsoft/mana/hw_channel.c:mana_hwc_destroy_channel() {
...
if (gc->max_num_cqs > 0)
mana_smc_teardown_hwc(&gc->shm_channel, false);
...
}Since the hardware is still active but the queue memory is subsequently freed, can the device DMA into the freed queue memory?
[Severity: Critical]
This is a pre-existing issue, but could a race condition cause stack corruption if a request times out?
In mana_hwc_send_request(), if wait_for_completion_timeout() times out, the function clears the msg_id and returns, destroying the stack frame containing ctx->output_buf:
drivers/net/ethernet/microsoft/mana/hw_channel.c:mana_hwc_send_request() {
...
if (!wait_for_completion_timeout(&ctx->comp_event, 40 * HZ)) {
...
mana_hwc_put_msg_index(hwc, msg_id);
return err;
}
...
}Concurrently, the interrupt handler does a lockless check of the inflight map:
drivers/net/ethernet/microsoft/mana/hw_channel.c:mana_hwc_handle_resp() {
...
if (!test_bit(msg_id, hwc->inflight_msg_res.map)) {
...
memcpy(ctx->output_buf, resp_msg, resp_len);
...
}If the handler observes the bit as set just before the timeout clears it, it will execute the memcpy(). Since the request function has already returned, doesn't this write arbitrary device-controlled data into a deallocated stack frame?
Quoted 14 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +6
-3+ +/* comp_buf is reached only by mana_hwc_comp_event(), invoked from +* the now-fenced EQ handler, so it is safe to free once the EQ and +* CQ are gone. +*/ if (hwc_cq->gdma_cq)mana_gd_destroy_queue (gc, hwc_cq->gdma_cq); -if (hwc_cq->gdma_eq) -mana_gd_destroy_queue (gc, hwc_cq->gdma_eq); - +kfree (hwc_cq->comp_buf);kfree (hwc_cq); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v5 4/7 net: mana: validate hardware-supplied values in the HWC RX path
In reply to Long Limana_hwc_rx_event_handler() consumed lengths and indices taken straight from device DMA without validation. A buggy firmware or a malicious host (in a confidential VM, where the DMA buffer is shared) could drive a wrong or reused in-flight request to completion or index out of bounds. Validate before use:
- snapshot the device-supplied inline_oob_size_div4 (read once through its u32 flags word with READ_ONCE(), as it is a bit-field) and reject any value other than the one the driver programs (INLINE_OOB_SMALL_SIZE / 4), so a corrupted OOB size cannot move the SGE out of the WQE before it is dereferenced;
- snapshot sge->address with READ_ONCE() and validate and use only the snapshot, so the value that is bounds-checked is the value that is used (the DMA buffer is host-writable in a confidential VM);
- match the SGE address against the address the driver posted for that slot, not just an in-range index -- an in-range but wrong SGE would otherwise truncate onto a neighbouring slot and read a stale response;
- reject a resp_len larger than the RX buffer.
As defence in depth, mana_hwc_handle_resp() also bounds-checks hwc_msg_id before indexing the inflight bitmap and caller_ctx. Its only caller already rejects the same range with the value it passes by value, so this is a guard at the indexing site, not a reachable out-of-bounds.
Repost the RX WQE on every validation early-return that can still identify its slot. The paths that cannot -- an unexpected OOB size, an out-of-range index, or an SGE address matching no posted slot -- intentionally leak a single WQE rather than risk reposting the wrong one. Because the HWC RQ depth is never replenished, count those leaks and, once they exhaust the posted depth, log the terminal state and shorten the command timeout so callers fail fast instead of draining silently.
A short response is no longer rejected in the handler: it reaches mana_hwc_handle_resp(), whose mana_hwc_verify_resp_msg() fails it with -EPROTO and completes the waiting sender, so a single malformed response cannot convert into a channel-wide timeout.
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- Removed the short-response early return so a malformed response reaches verify_resp_msg() -> -EPROTO and completes the sender instead of hanging it.
- Account leaked RX WQEs and trip hwc_timeout on RQ exhaustion.
- Read inline_oob_size_div4 (through its u32 flags word, as it is a bit-field) and sge->address with READ_ONCE() and reject any value other than the one the driver programs.
- Reframed the msg_id check as defense in depth in the changelog.
.../net/ethernet/microsoft/mana/hw_channel.c | 116 ++++++++++++++++-- include/net/mana/hw_channel.h | 6 + 2 files changed, 111 insertions(+), 11 deletions(-)
drivers/net/ethernet/microsoft/mana/hw_channel.c +105 -11
@@ -83,6 +83,19 @@ static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len,struct hwc_caller_ctx *ctx;int err; +/* Defence in depth: the sole caller, mana_hwc_rx_event_handler(), +* already rejects msg_id >= hwc->num_inflight_msg with the value it +* passes here by value, so this cannot be reached out of range. Keep +* the guard at the indexing site so the bitmap and caller_ctx array +* are never indexed without a bound in view. +*/ +if (msg_id >= hwc->num_inflight_msg) { +dev_err (hwc->dev,"hwc_rx: msg_id %u >= max %u\n" , + msg_id, hwc->num_inflight_msg); +mana_hwc_post_rx_wqe (hwc->rxq, rx_req); +return ; + } +if (!test_bit (msg_id, hwc->inflight_msg_res.map)) {dev_err (hwc->dev,"hwc_rx: invalid msg_id = %u\n" , msg_id);mana_hwc_post_rx_wqe (hwc->rxq, rx_req);
@@ -90,6 +103,18 @@ static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len, } ctx = hwc->caller_ctx + msg_id; + +/* Reject responses larger than the RX DMA buffer — the SGE +* limits what hardware can DMA, so an oversized resp_len +* indicates a firmware bug. Fail rather than silently +* truncating. +*/ +if (resp_len > rx_req->buf_len) { +dev_err (hwc->dev,"HWC RX: resp_len %u > buf_len %u\n" , + resp_len, rx_req->buf_len); + resp_len =0 ; + } + err =mana_hwc_verify_resp_msg (ctx, resp_msg, resp_len);if (err) goto out;
@@ -237,18 +262,39 @@ static void mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self, } } +/* An RX WQE whose SGE the handler cannot trust is deliberately not +* reposted: reposting a slot we may have mis-identified could double-post +* a buffer the device still owns. Each such leak permanently lowers the +* RQ's posted depth, so once the whole depth is gone the channel can no +* longer receive responses. Make that terminal state explicit -- log it +* once and shorten the command timeout so callers fail fast -- rather than +* letting every later command drain its full timeout against a dead RQ. +*/ +static void mana_hwc_rx_leak_wqe (struct hw_channel_context *hwc) +{ +if (++hwc->rx_leaked_wqe == hwc->rxq->queue_depth) { +dev_err (hwc->dev, +"HWC RX: RQ exhausted after %u leaked WQEs; channel unusable\n" , + hwc->rx_leaked_wqe); + hwc->hwc_timeout =1 ; + } +} +static void mana_hwc_rx_event_handler (void *ctx,u32 gdma_rxq_id,const struct hwc_rx_oob *rx_oob) {struct hw_channel_context *hwc = ctx;struct hwc_wq *hwc_rxq = hwc->rxq;struct hwc_work_request *rx_req; +struct gdma_wqe oob_snapshot;struct gdma_resp_hdr *resp;struct gdma_wqe *dma_oob;struct gdma_queue *rq;struct gdma_sge *sge;u64 rq_base_addr;u64 rx_req_idx; +u64 sge_addr; +u32 oob_div4;u16 msg_id;u8 *wqe;
@@ -259,28 +305,76 @@ static void mana_hwc_rx_event_handler(void *ctx, u32 gdma_rxq_id, wqe =mana_gd_get_wqe_ptr (rq, rx_oob->wqe_offset /GDMA_WQE_BU_SIZE ); dma_oob = (struct gdma_wqe *)wqe; - sge = (struct gdma_sge *)(wqe +8 + dma_oob->inline_oob_size_div4 *4 ); - -/* Select the RX work request for virtual address and for reposting. */ +/* inline_oob_size_div4 lives in device-accessible RQ memory (shared +* and host-writable in a confidential VM), so snapshot it once and +* validate and use only the snapshot. It is a bit-field, which +* READ_ONCE() cannot take the size of, so read the u32 flags word it +* shares through the union and extract the field from the local copy. +* The driver programs INLINE_OOB_SMALL_SIZE for every HWC RQ WQE via +* mana_gd_post_work_request(), so the only valid value is +* INLINE_OOB_SMALL_SIZE / 4, which puts the SGE at wqe + 16 inside +* this WQE's own BU. Reject anything else -- the slot cannot be +* trusted, so leak this RX WQE rather than repost the wrong one. +*/ + oob_snapshot.flags =READ_ONCE (dma_oob->flags); + oob_div4 = oob_snapshot.inline_oob_size_div4; +if (oob_div4 !=INLINE_OOB_SMALL_SIZE /4 ) { +dev_err (hwc->dev,"HWC RX: unexpected inline_oob_size_div4=%u\n" , + oob_div4); +mana_hwc_rx_leak_wqe (hwc); +return ; + } + sge = (struct gdma_sge *)(wqe +8 + oob_div4 *4 ); + +/* Recover the originating RX slot from the SGE address. Snapshot it +* once, for the same shared-memory reason: of the three terms only +* sge_addr comes from device memory; rq_base_addr and +* max_resp_msg_size are driver-private. An in-range but wrong SGE +* would otherwise truncate onto a neighbouring slot, letting us read +* a stale response that could complete the wrong, reused in-flight +* request. Require the index in range AND the address to exactly +* match the value the driver posted for that slot. +*/ + sge_addr =READ_ONCE (sge->address); rq_base_addr = hwc_rxq->msg_buf->mem_info.dma_handle; - rx_req_idx = (sge->address - rq_base_addr) / hwc->max_resp_msg_size; - -if (rx_req_idx >= hwc_rxq->msg_buf->num_reqs) { -dev_err (hwc->dev,"HWC RX: wrong rx_req_idx=%llu, num_reqs=%u\n" , - rx_req_idx, hwc_rxq->msg_buf->num_reqs); + rx_req_idx = (sge_addr - rq_base_addr) / hwc->max_resp_msg_size; + +if (rx_req_idx >= hwc_rxq->queue_depth) { +/* Cannot identify the slot, so we cannot safely repost this +* WQE; leak it. An out-of-range index means a corrupted SGE +* from hardware or host tampering. +*/ +dev_err (hwc->dev,"HWC RX: SGE idx %llu out of range\n" , + rx_req_idx); +mana_hwc_rx_leak_wqe (hwc);return ; } rx_req = &hwc_rxq->msg_buf->reqs[rx_req_idx]; +if (sge_addr != (u64 )rx_req->buf_sge_addr) { +/* In-range index but the address does not match what the +* driver posted for that slot; the same unrecoverable case, +* so leak this WQE rather than repost the wrong one. +*/ +dev_err (hwc->dev,"HWC RX: invalid SGE address %llx (idx=%llu)\n" , + sge_addr, rx_req_idx); +mana_hwc_rx_leak_wqe (hwc); +return ; + } + resp = (struct gdma_resp_hdr *)rx_req->buf_va; -/* Read msg_id once from DMA buffer to prevent TOCTOU: -* DMA memory is shared/unencrypted in CVMs - host can -* modify it between reads. +/* Read msg_id once from the DMA buffer to prevent TOCTOU: DMA memory +* is shared/unencrypted in CVMs, so the host can modify it between +* reads. A short response is not rejected here; it is handed to +* mana_hwc_handle_resp() below, whose mana_hwc_verify_resp_msg() +* fails it with -EPROTO and completes the waiting sender, so one +* malformed response cannot stall the whole channel. */ msg_id =READ_ONCE (resp->response.hwc_msg_id);if (msg_id >= hwc->num_inflight_msg) {dev_err (hwc->dev,"HWC RX: wrong msg_id=%u\n" , msg_id); +mana_hwc_post_rx_wqe (hwc_rxq, rx_req);return ; }
include/net/mana/hw_channel.h +6 -0
@@ -200,6 +200,12 @@ struct hw_channel_context {u32 pf_dest_vrcq_id;u32 hwc_timeout; +/* Count of RX WQEs deliberately not reposted after an untrusted SGE +* (see mana_hwc_rx_leak_wqe()); once it reaches the RQ depth the +* channel can no longer receive responses. +*/ +u32 rx_leaked_wqe; +struct hwc_caller_ctx *caller_ctx; };
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net v5 4/7] net: mana: validate hardware-supplied values in the HWC RX path
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:
Pre-existing issues:
- [Critical] Race condition between timeout path and RX interrupt handler leading to kernel stack corruption and cross-request data corruption.
- [High] Missing length check before reading DMA response payload causes stale message IDs to incorrectly complete unrelated requests. --
--- Patch [4]: [PATCH net v5 4/7] net: mana: validate hardware-supplied values in the HWC RX path --- commit 74a56502856c8211c5e1df5ca5d7019fd74fb6b6
Author: Long Li <[email protected]>
net: mana: validate hardware-supplied values in the HWC RX path
This commit adds validation for lengths, indices, and sizes taken from device DMA in the HWC RX path. It aims to protect against buggy firmware or malicious hosts by snapshotting values and performing bounds checks. It also drops the early return for short responses, deferring to mana_hwc_handle_resp() to handle them.
Quoted 11 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +3
-0@@ -90,6 +103,18 @@ static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len, } ctx = hwc->caller_ctx + msg_id; + +/* Reject responses larger than the RX DMA buffer — the SGE +* limits what hardware can DMA, so an oversized resp_len
drivers/net/ethernet/microsoft/mana/hw_channel.c static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len, · line 118
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +9
-0+* indicates a firmware bug. Fail rather than silently +* truncating. +*/ +if (resp_len > rx_req->buf_len) { +dev_err (hwc->dev,"HWC RX: resp_len %u > buf_len %u\n" , + resp_len, rx_req->buf_len); + resp_len =0 ; + } + err =mana_hwc_verify_resp_msg (ctx, resp_msg, resp_len);
[Severity: Critical]
This is a pre-existing issue, but is there a race condition between the timeout path and the RX interrupt handler leading to kernel stack corruption?
If a slow hardware response causes wait_for_completion_timeout() to expire in mana_hwc_send_request(), the timeout path frees the msg_id and returns:
mana_hwc_send_request() {
err = wait_for_completion_timeout(...);
if (!err) {
err = -ETIMEDOUT;
goto out;
}
...
out:
mana_hwc_put_msg_index(hwc, msg_id);
return err;
}Returning here pops the stack frame pointed to by ctx->output_buf.
If the interrupt handler is concurrently running and passes the bounds checks, it could then execute memcpy() directly into this deallocated stack frame:
mana_hwc_handle_resp() {
...
memcpy(ctx->output_buf, resp_msg, resp_len);
...
}Can this cause memory corruption, especially if the timed-out msg_id is quickly reallocated to a new request which then gets overwritten with the stale response?
Quoted 2 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-0if (err) goto out;
[ ... ]
Quoted 4 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-1@@ -259,28 +305,76 @@ static void mana_hwc_rx_event_handler(void *ctx, u32 gdma_rxq_id, resp = (struct gdma_resp_hdr *)rx_req->buf_va; -/* Read msg_id once from DMA buffer to prevent TOCTOU:
drivers/net/ethernet/microsoft/mana/hw_channel.c static void mana_hwc_rx_event_handler(void *ctx, u32 gdma_rxq_id, · line 314
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +6
-2-* DMA memory is shared/unencrypted in CVMs - host can -* modify it between reads. +/* Read msg_id once from the DMA buffer to prevent TOCTOU: DMA memory +* is shared/unencrypted in CVMs, so the host can modify it between +* reads. A short response is not rejected here; it is handed to +* mana_hwc_handle_resp() below, whose mana_hwc_verify_resp_msg() +* fails it with -EPROTO and completes the waiting sender, so one +* malformed response cannot stall the whole channel. */ msg_id =READ_ONCE (resp->response.hwc_msg_id);
[Severity: High]
This isn't a bug introduced by this patch, but does reading the DMA response payload without a length check cause stale message IDs to incorrectly complete unrelated requests?
If the hardware or a malicious host provides an RX response with tx_oob_data_size smaller than the offset required to contain the hwc_msg_id field, it will not overwrite the hwc_msg_id field in the persistent DMA ring buffer.
The driver then reads this stale msg_id in mana_hwc_rx_event_handler(), which likely belongs to a previously completed request that has since been re-allocated to a new, unrelated active request.
The driver subsequently fails this unrelated active request with -EPROTO in mana_hwc_handle_resp():
mana_hwc_handle_resp() {
...
err = mana_hwc_verify_resp_msg(ctx, resp_msg, resp_len);
if (err)
goto out;
...
out:
ctx->error = err;
...
complete(&ctx->comp_event);
}Could a malicious host forge short DMA responses to intentionally abort unrelated active requests?
Quoted 2 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-0if (msg_id >= hwc->num_inflight_msg) {dev_err (hwc->dev,"HWC RX: wrong msg_id=%u\n" , msg_id);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v5 5/7 net: mana: fix HWC teardown safety with setup_active flag and destroy ordering
In reply to Long LiThree teardown hazards let the hardware touch memory the driver freed.
First, once mana_smc_setup_hwc() succeeds the device has active MST entries and can DMA into the HWC queue buffers. If a later step in mana_hwc_establish_channel() fails, the caller had no reliable way to know teardown was required and could free those buffers while the mappings were still live -- a DMA-after-free. max_num_cqs was used as a "HWC is up" proxy, but it is only set when the init EQE arrives.
Add a setup_active flag, set the moment setup_hwc activates MST entries. On a later failure establish_channel() just returns the error; the caller's error path (mana_hwc_create_channel() -> destroy_channel()) performs the single teardown, gated on setup_active. Tearing down inline as well would run teardown twice -- doubling the 60s hardware timeout on failure and masking the original error code. max_num_cqs is no longer
reset: it is an immutable bound (see gdma.h) and cq_table == NULL is the sole teardown signal.
Second, destroy_channel() freed the TXQ/RXQ buffers while the HWC EQ was still on the interrupt dispatch list, so an in-flight interrupt could run the handler against freed buffers:
CPU A (mana_gd_intr, hard IRQ) CPU B (destroy_channel)
---------------------------------- ------------------------------
free TXQ/RXQ DMA buffers
handler accesses RQ/TXQ buffers (EQ still registered)
Destroy the CQ first: mana_hwc_destroy_cq() -> mana_gd_deregister_irq() removes the EQ via list_del_rcu() + synchronize_rcu(), after which no handler can reach the queues; only then free the TXQ and RXQ.
Third, if mana_smc_teardown_hwc() itself fails the MST entries stay live, yet destroy_channel() went on to free the CQ/RQ/TXQ buffers the device can still DMA into -- a DMA-after-free on systems without an IOMMU to fault the stale access. Leak the HWC resources on teardown failure instead of freeing memory the hardware can still reach, and keep setup_active set so the failure is not mistaken for a clean teardown.
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- Arm setup_active immediately after mana_smc_setup_hwc() succeeds.
- Destroy the EQ (IRQ deregister + drain) before the CQ.
- Dropped the redundant teardown in mana_hwc_establish_channel() that caused a double hardware timeout and masked the original error code.
.../net/ethernet/microsoft/mana/hw_channel.c | 56 ++++++++++++++----- include/net/mana/gdma.h | 8 ++- include/net/mana/hw_channel.h | 9 +++ 3 files changed, 57 insertions(+), 16 deletions(-)
drivers/net/ethernet/microsoft/mana/hw_channel.c +41 -15
@@ -4,6 +4,7 @@#include <net/mana/gdma.h> #include <net/mana/mana.h> #include <net/mana/hw_channel.h> +#include <linux/pci.h> #include <linux/vmalloc.h> static int mana_hwc_get_msg_index (struct hw_channel_context *hwc,u16 *msg_id)
@@ -792,6 +793,15 @@ static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth,if (err)return err; +/* setup_hwc activated MST entries — hardware can now DMA into our +* queue buffers. Record that in setup_active so the caller's error +* path (mana_hwc_create_channel() -> mana_hwc_destroy_channel()) +* tears the HWC down exactly once. Do not also tear down here: a +* second teardown would double the hardware timeout on failure and +* mask the original error code. +*/ + hwc->setup_active = true; +if (!wait_for_completion_timeout (&hwc->hwc_init_eqe_comp,60 *HZ ))return -ETIMEDOUT ;
@@ -926,11 +936,38 @@ void mana_hwc_destroy_channel(struct gdma_context *gc)if (!hwc)return ; -/* gc->max_num_cqs is set in mana_hwc_init_event_handler(). If it's -* non-zero, the HWC worked and we should tear down the HWC here. +/* Tear down the HWC if setup_hwc previously activated MST entries. +* This is the definitive flag — unlike max_num_cqs which depends +* on the init EQE arriving. +* +* If teardown fails the device may still have active MST entries +* and can DMA into the HWC queue buffers. Freeing them would risk +* memory corruption on systems without an IOMMU to fault the stale +* DMA, so leak the HWC resources instead of handing the pages back +* to the allocator. Keep setup_active set so the failure is not +* mistaken for a clean teardown. +*/ +if (hwc->setup_active) { +int td_err =mana_smc_teardown_hwc (&gc->shm_channel, false); + +if (td_err) { +dev_err (gc->dev, +"HWC teardown failed: %d, leaking resources\n" , + td_err); +return ; + } + + hwc->setup_active = false; + } + +/* Tear down the HWC CQ object first — mana_hwc_destroy_cq() +* both unpublishes the CQ from cq_table (+synchronize_rcu) and +* deregisters the HWC EQ from the interrupt handler list (via +* mana_gd_deregister_irq + synchronize_rcu), guaranteeing no +* interrupt handler can access RQ/TXQ buffers after this point. */ -if (gc->max_num_cqs >0 ) -mana_smc_teardown_hwc (&gc->shm_channel, false); +if (hwc->cq) +mana_hwc_destroy_cq (hwc->gdma_dev->gdma_context, hwc->cq);if (hwc->txq)mana_hwc_destroy_wq (hwc, hwc->txq);
@@ -938,17 +975,6 @@ void mana_hwc_destroy_channel(struct gdma_context *gc)if (hwc->rxq)mana_hwc_destroy_wq (hwc, hwc->rxq); -if (hwc->cq) -mana_hwc_destroy_cq (hwc->gdma_dev->gdma_context, hwc->cq); - -/* Reset only after mana_hwc_destroy_cq() above has run with a valid -* max_num_cqs so mana_gd_destroy_cq() clears the CQ table slot and -* waits out in-flight EQ handlers (synchronize_rcu) before the CQ is -* freed. Clearing it earlier would make that path early-return and -* skip the slot clear, leaving a dangling cq_table entry. -*/ - gc->max_num_cqs =0 ; -kfree (hwc->caller_ctx); hwc->caller_ctx =NULL ;
include/net/mana/gdma.h +7 -1
@@ -416,7 +416,13 @@ struct gdma_context {/* L2 MTU */ u16 adapter_mtu; -/* This maps a CQ index to the queue structure. */ +/* Size of cq_table, i.e. the largest valid CQ index + 1. Set once +* when cq_table is allocated and treated as immutable for the +* table's lifetime (a bound only) -- it is never reset on teardown. +* cq_table == NULL is the sole "table torn down" signal, so every +* cq_table[id] access must guard with both !cq_table (gone) and +* id >= max_num_cqs (out of bounds). +*/ unsigned int max_num_cqs;/* Both the base pointer and each entry are RCU-managed. The fast * path (mana_gd_process_eqe) reads the base via rcu_dereference()
include/net/mana/hw_channel.h +9 -0
@@ -206,6 +206,15 @@ struct hw_channel_context {*/ u32 rx_leaked_wqe; +/* Set after mana_smc_setup_hwc() succeeds (hardware has active +* MST entries). Cleared only after mana_smc_teardown_hwc() +* succeeds, on both the recoverable establish_channel path and the +* terminal destroy_channel path. If teardown fails it stays set: +* establish_channel() skips its retry and destroy_channel() leaks +* the HWC rather than free buffers the device may still DMA into. +*/ +bool setup_active; +struct hwc_caller_ctx *caller_ctx; };
Signature · 1 line
2.43.0
PATCH v5 6/7 net: mana: fix stale HWC response after command timeout
In reply to Long LiThe HWC freed a message slot (mana_hwc_put_msg_index) the instant mana_hwc_send_request() timed out, while the hardware command was still pending and caller_ctx.output_buf still pointed at the caller's response buffer. A late response then raced two ways:
- handle_resp() runs in CQ interrupt context and memcpy()'d into output_buf after the sender had returned and its buffer was gone.
- the freed slot was reused by the next request, so the stale response completed the wrong command with another request's data.
Give each caller_ctx a spinlock, a refcount and an -EINPROGRESS sentinel (and change caller_ctx::error from u32 to int so it holds the negative errno values, including the sentinel, without relying on unsigned wraparound):
- The sender publishes output_buf under the slot lock and NULLs it under the same lock on timeout/exit, so handle_resp() (also under the lock) skips the copy once the sender is gone.
- The slot is released only when both the sender and handle_resp() have dropped their reference, so a msg_id whose response is still outstanding is never handed to a new request.
- Both references are taken up front in mana_hwc_get_msg_index(), under the same lock that publishes the slot, so a stale, duplicate or early response that arrives before the sender posts drops only the response-side reference and cannot release the slot out from under the sender. A per-slot "responded" flag drops the payload of any such extra response.
- On a genuine timeout the channel is marked hwc_timed_out and further mana_hwc_get_msg_index() callers fail with -ETIMEDOUT instead of reusing a slot whose response may still arrive. The flag is read with READ_ONCE() outside the bitmap lock and written with WRITE_ONCE() under it.
Replace the counting semaphore with a waitqueue + bitmap so a slot held past a timeout does not deadlock admission and timed-out waiters can be released.
Because the timeout latch keys off wait_for_completion_timeout() returning immediately, a zero hwc_timeout would time out every command at once and latch the whole channel. Ignore a device-reported zero from both sources that feed hwc_timeout -- the HWC_DATA_CFG_HWC_TIMEOUT reconfig event and the GDMA_QUERY_HWC_TIMEOUT response -- and keep the positive default instead.
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- Take both the sender and response-side references up front in mana_hwc_get_msg_index() (refcount initialised to 2, under the lock that publishes the slot) so an early/stale/forged response cannot free the slot before the sender posts; the pre-post error path latches ->responded to avoid a double drop.
- Changed caller_ctx::error from u32 to int so it holds the negative -EINPROGRESS sentinel and errno values directly.
- Reject a zero firmware-supplied HWC timeout in the query path as well as the reconfig path.
- Access hwc_timed_out with READ_ONCE()/WRITE_ONCE(); comment and changelog fixes.
.../net/ethernet/microsoft/mana/gdma_main.c | 7 +- .../net/ethernet/microsoft/mana/hw_channel.c | 218 +++++++++++++++--- include/net/mana/hw_channel.h | 27 ++- 3 files changed, 214 insertions(+), 38 deletions(-)
drivers/net/ethernet/microsoft/mana/gdma_main.c +6 -1
@@ -310,7 +310,12 @@ static int mana_gd_query_hwc_timeout(struct pci_dev *pdev, u32 *timeout_val)if (err || resp.hdr.status)return err ? err : -EPROTO ; - *timeout_val = resp.timeout_ms; +/* A zero timeout would make every HWC command time out immediately +* and latch the channel (see the HWC_DATA_CFG_HWC_TIMEOUT handler). +* Ignore a zero from the device and keep the caller's positive value. +*/ +if (resp.timeout_ms) + *timeout_val = resp.timeout_ms;return 0 ; }
drivers/net/ethernet/microsoft/mana/hw_channel.c +183 -35
326 lines not on this page · raw message
include/net/mana/hw_channel.h +25 -2
@@ -171,8 +171,25 @@ struct hwc_caller_ctx {void *output_buf;u32 output_buflen; -u32 error;/* Linux error code */ +int error;/* Linux error code (negative errno or 0) */ u32 status_code; + +/* Protects output_buf against concurrent access from +* handle_resp() (CQ interrupt) and the sender timeout path. +*/ +spinlock_t lock; + +/* Tracks sender + handle_resp ownership. The last put +* (refcount reaches 0) releases the bitmap slot. +*/ +refcount_t refcnt; +u16 msg_id; + +/* Set under lock by the first handle_resp() for this slot so a +* duplicate or replayed response is dropped instead of consuming +* the response-side reference a second time. +*/ +bool responded; };struct hw_channel_context {
@@ -193,8 +210,9 @@ struct hw_channel_context {struct hwc_wq *txq;struct hwc_cq *cq; -struct semaphore sema;struct gdma_resource inflight_msg_res; +/* Waitqueue for senders blocked on a full inflight bitmap. */ +wait_queue_head_t msg_waitq;u32 pf_dest_vrq_id;u32 pf_dest_vrcq_id;
@@ -206,6 +224,11 @@ struct hw_channel_context {*/ u32 rx_leaked_wqe; +/* Set on first HWC timeout. Causes get_msg_index() to return +* -ETIMEDOUT instead of waiting, draining all queued senders. +*/ +bool hwc_timed_out; +/* Set after mana_smc_setup_hwc() succeeds (hardware has active * MST entries). Cleared only after mana_smc_teardown_hwc() * succeeds, on both the recoverable establish_channel path and the
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net v5 6/7] net: mana: fix stale HWC response after command timeout
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Logic flaw in handling stale/duplicate responses causes premature request failure and slot reuse.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Logic flaw in handling stale/duplicate responses causes premature request failure and slot reuse.
- [High] Data race: message slot is published to the inflight bitmap before context initialization is complete. --
--- Patch [6]: [PATCH net v5 6/7] net: mana: fix stale HWC response after command timeout --- commit 7b87368dbc534bab9b5a48c46518ecb9efb9318e
Author: Long Li <[email protected]>
net: mana: fix stale HWC response after command timeout
This commit attempts to fix a race condition where an HWC response arrives after a command timeout by giving each caller context a spinlock and a refcount. It prevents reusing slots whose responses are still outstanding.
Quoted 3 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-0
[ ... ]
Quoted 16 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +6
-2@@ -7,25 +7,58 @@static int mana_hwc_get_msg_index (struct hw_channel_context *hwc,u16 *msg_id) {struct gdma_resource *r = &hwc->inflight_msg_res;unsigned long flags;u32 index; -down (&hwc->sema); +for (;;) { +spin_lock_irqsave (&r->lock, flags); -spin_lock_irqsave (&r->lock, flags); +if (hwc->hwc_timed_out) { +spin_unlock_irqrestore (&r->lock, flags); +return -ETIMEDOUT ; + }
drivers/net/ethernet/microsoft/mana/hw_channel.c line 27
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +7
-2- index =find_first_zero_bit (hwc->inflight_msg_res.map, - hwc->inflight_msg_res.size); + index =find_first_zero_bit (r->map, r->size); +if (index < r->size) { +struct hwc_caller_ctx *ctx; + +bitmap_set (r->map, index,1 ); + ctx = &hwc->caller_ctx[index]; +reinit_completion (&ctx->comp_event);
[Severity: High]
Can a concurrent stale response observe the bitmap before the context is fully initialized?
If a stale response arrives for this index on another CPU, mana_hwc_handle_resp() could see the bit set in the inflight bitmap via test_bit() and proceed to locklessly read and modify context fields while they are still being initialized here.
Because ctx->lock is not held during this initialization, mana_hwc_handle_resp() might observe an uninitialized state, such as an empty refcnt. This could cause a refcount underflow when hwc_ctx_put() is called, or race with reinit_completion().
Quoted 2 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +2
-0+/* Take the response-side reference here, under +* r->lock and together with the slot bitmap bit,
[ ... ]
Quoted 39 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +26
-7@@ -116,22 +156,44 @@ static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len, resp_len =0 ; } - err =mana_hwc_verify_resp_msg (ctx, resp_msg, resp_len); -if (err) - goto out; +spin_lock (&ctx->lock); - ctx->status_code = resp_msg->status; +if (ctx->responded) { +/* A response for this slot was already delivered; this is a +* duplicate or replayed one. Drop it so the hwc_ctx_put() +* a first response performs is not done twice, which would +* over-release the slot while the sender still owns it. +*/ +spin_unlock (&ctx->lock); +mana_hwc_post_rx_wqe (hwc->rxq, rx_req); +return ; + } + ctx->responded = true; -memcpy (ctx->output_buf, resp_msg, resp_len); -out: - ctx->error = err; + err =mana_hwc_verify_resp_msg (ctx, resp_msg, resp_len); + +if (!err && ctx->output_buf) { + ctx->status_code = resp_msg->status; +memcpy (ctx->output_buf, resp_msg, resp_len); + ctx->error =0 ; + }else if (ctx->output_buf) { +/* Record the error only while the sender still owns the +* request: a non-NULL output_buf means it is still waiting. +* Once it has timed out (or been force-completed by destroy) +* it clears output_buf and takes its own error, so a late +* response must not write ctx->error or the buffer here. +*/ + ctx->error = err;
drivers/net/ethernet/microsoft/mana/hw_channel.c static void mana_hwc_handle_resp(struct hw_channel_context *hwc, u32 resp_len, · line 193
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +3
-3+ } -/* Must post rx wqe before complete(), otherwise the next rx may -* hit no_wqe error. +/* Post RX WQE before completing — the next response may arrive +* immediately and needs a posted buffer. */ mana_hwc_post_rx_wqe (hwc->rxq, rx_req); -complete (&ctx->comp_event);
[Severity: High]
Could a stale response that arrives before the sender sets output_buf cause the new request to fail prematurely?
If a stale response arrives after a message slot is allocated, but before mana_hwc_send_request() sets output_buf to a non-NULL value, mana_hwc_handle_resp() will execute this path. Because output_buf is NULL, ctx->error remains -EINPROGRESS.
It then sets ctx->responded = true and signals ctx->comp_event.
When mana_hwc_send_request() subsequently sets output_buf, posts the new WQE, and waits on the completion event, wait_for_completion_timeout() will return immediately because it was already completed.
The sender will then read -EINPROGRESS from ctx->error, assume a failure, and free the message slot while its newly posted WQE is still actively in flight. This could potentially lead to data corruption when the real response arrives and corrupts the next request's buffer.
Quoted 4 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +3
-0+spin_unlock (&ctx->lock); + +hwc_ctx_put (hwc, ctx); }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
PATCH v5 7/7 net: mana: keep max_num_cqs immutable once cq_table is allocated
In reply to Long Limana_hwc_init_event_handler() applied every HWC_INIT_DATA_MAX_NUM_CQS event straight to gc->max_num_cqs. That handler stays live for the whole channel lifetime -- it also services runtime reconfig and link events -- so it is not confined to the initial bootstrap.
gc->cq_table is allocated once, sized to the max_num_cqs seen at bootstrap, and every reader (mana_gd_process_eqe(), mana_create_rxq() and mana_create_txq()) bounds-checks a CQ index against gc->max_num_cqs before indexing gc->cq_table. A device -- or a malicious host in a confidential VM -- that sends a later HWC_INIT_DATA_MAX_NUM_CQS with a larger value inflates the bound past the allocation. This includes an event timed to land while mana_hwc_establish_channel() is between reading the count and publishing cq_table. A subsequent out-of-range CQ id then passes the bounds check and indexes cq_table out of bounds: an out-of-bounds read in the EQ fast path, or an out-of-bounds pointer write in mana_create_rxq()/mana_create_txq(), corrupting guest kernel memory.
Stop writing gc->max_num_cqs from the event handler. Store the reported value in hwc_init_max_num_cqs, and let mana_hwc_establish_channel() commit it to gc->max_num_cqs once, from the same snapshot that sizes cq_table. The handler store uses WRITE_ONCE() and the establish-time read uses READ_ONCE(), since the two run concurrently (EQ interrupt vs process context); the single, non-reloadable read is what guarantees the value that sizes cq_table is the same one published as the bound, even across the sleeping vcalloc(). gc->max_num_cqs then always matches the allocation and no later event can change the bound after the table is published, so the existing bounds checks are sufficient.
---
Changes in v5:
- No code changes since v4 (resend as a standalone thread).
Changes in v4:
- New patch in v4, split out of the v3 teardown-safety work in response to review: gc->max_num_cqs is set once when cq_table is allocated and never reset, so a spoofed post-init HWC event cannot inflate the bound past the allocation.
.../net/ethernet/microsoft/mana/hw_channel.c | 34 ++++++++++++++++--- include/net/mana/hw_channel.h | 1 + 2 files changed, 30 insertions(+), 5 deletions(-)
drivers/net/ethernet/microsoft/mana/hw_channel.c +29 -5
@@ -246,7 +246,15 @@ static void mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self,break ;case HWC_INIT_DATA_MAX_NUM_CQS : - gd->gdma_context->max_num_cqs = val; +/* Store, don't apply: mana_hwc_establish_channel() +* commits this to gc->max_num_cqs once, together +* with sizing cq_table, so a spoofed post-init event +* cannot inflate the bound past the allocation. +* WRITE_ONCE() pairs with the READ_ONCE() there: +* this store runs in EQ interrupt context, +* concurrently with that process-context read. +*/ +WRITE_ONCE (hwc->hwc_init_max_num_cqs, val);break ;case HWC_INIT_DATA_PDID :
@@ -852,6 +860,8 @@ static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth,struct gdma_queue *eq = hwc->cq->gdma_eq;struct gdma_queue *cq = hwc->cq->gdma_cq;struct gdma_queue __rcu **cq_table; +u32 num_cqs; +u32 cq_id;int err;init_completion (&hwc->hwc_init_eqe_comp);
@@ -881,15 +891,29 @@ static int mana_hwc_establish_channel(struct gdma_context *gc, u16 *q_depth, *max_req_msg_size = hwc->hwc_init_max_req_msg_size; *max_resp_msg_size = hwc->hwc_init_max_resp_msg_size; -/* Both were set in mana_hwc_init_event_handler(). */ -if (WARN_ON (cq->id >= gc->max_num_cqs)) +/* Snapshot the device-reported CQ count and CQ id into locals and +* use only the locals below, so the same value that sizes cq_table +* also bounds and indexes it -- even across the sleeping vcalloc(). +* Both fields are written by mana_hwc_init_event_handler() from EQ +* interrupt context: hwc_init_max_num_cqs under WRITE_ONCE() (paired +* here), and cq->id as an ordinary store. READ_ONCE() keeps each +* read tear-free and, crucially, non-reloadable, so a spoofed +* post-init event cannot make the WARN_ON() pass against one value +* while the allocation or the index uses another. +*/ + num_cqs =READ_ONCE (hwc->hwc_init_max_num_cqs); + cq_id =READ_ONCE (cq->id); + +if (WARN_ON (cq_id >= num_cqs))return -EPROTO ; - cq_table =vcalloc (gc->max_num_cqs,sizeof (*cq_table)); + cq_table =vcalloc (num_cqs,sizeof (*cq_table));if (!cq_table)return -ENOMEM ; -rcu_assign_pointer (cq_table[cq->id], cq); + gc->max_num_cqs = num_cqs; + +rcu_assign_pointer (cq_table[cq_id], cq);/* Publish the fully-initialised table last; pairs with the * rcu_dereference(gc->cq_table) in mana_gd_process_eqe(). */
include/net/mana/hw_channel.h +1 -0
@@ -203,6 +203,7 @@ struct hw_channel_context {u16 hwc_init_q_depth_max;u32 hwc_init_max_req_msg_size;u32 hwc_init_max_resp_msg_size; +u32 hwc_init_max_num_cqs;struct completion hwc_init_eqe_comp;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH net v5 7/7] net: mana: keep max_num_cqs immutable once cq_table is allocated
Thank 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:
Pre-existing issues:
- [Critical] A spoofed GDMA_EQE_HWC_INIT_EQ_ID_DB event changes gd->doorbell without bounds checking, leading to an out-of-bounds MMIO write. --
--- Patch [7]: [PATCH net v5 7/7] net: mana: keep max_num_cqs immutable once cq_table is allocated --- commit e3524562bdca9a596ad21cc819f86df8b083a5c4
Author: Long Li <[email protected]>
Subject: net: mana: keep max_num_cqs immutable once cq_table is allocated
This commit prevents a spoofed HWC_INIT_DATA_MAX_NUM_CQS event from bypassing allocation bounds checks. It modifies the initialization event handler to store the reported value rather than applying it directly, committing it only once when sizing the table.
Quoted 9 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +0
-1@@ -246,7 +246,15 @@ static void mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self,break ;case HWC_INIT_DATA_MAX_NUM_CQS : - gd->gdma_context->max_num_cqs = val;
drivers/net/ethernet/microsoft/mana/hw_channel.c static void mana_hwc_init_event_handler(void *ctx, struct gdma_queue *q_self, · line 258
Quoted 10 lines
drivers/net/ethernet/microsoft/mana/hw_channel.c +9
-0+/* Store, don't apply: mana_hwc_establish_channel() +* commits this to gc->max_num_cqs once, together +* with sizing cq_table, so a spoofed post-init event +* cannot inflate the bound past the allocation. +* WRITE_ONCE() pairs with the READ_ONCE() there: +* this store runs in EQ interrupt context, +* concurrently with that process-context read. +*/ +WRITE_ONCE (hwc->hwc_init_max_num_cqs, val);break ;
[Severity: Critical]
This is a pre-existing issue, but while this patch protects against spoofed CQ counts from untrusted environments, could a similar out-of-bounds memory access happen with the doorbell index?
Looking earlier in mana_hwc_init_event_handler(), a spoofed doorbell index appears to be accepted blindly:
mana_hwc_init_event_handler() {
...
switch (event->type) {
case GDMA_EQE_HWC_INIT_EQ_ID_DB:
eq_db.as_uint32 = event->details[0];
hwc->cq->gdma_eq->id = eq_db.eq_id;
gd->doorbell = eq_db.doorbell;
break;
...
}If a malicious host sends a GDMA_EQE_HWC_INIT_EQ_ID_DB event with an arbitrarily large doorbell index, it is updated without bounds validation against gc->bar0_size.
Would this lead to an out-of-bounds MMIO write when later ringing the doorbell?
drivers/net/ethernet/microsoft/mana/gdma_main.c:mana_gd_ring_doorbell() {
...
void __iomem *addr = gc->db_page_base + gc->db_page_size * db_index;
...
writeq(e.as_uint64, addr);
}[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7