mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v2] net: ethernet: mtk_wed: fix double free in mtk_wed_rx_reset
@ 2026-11-27 12:34 Zhi-Jun You
  2026-11-28 12:36 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages in thread
From: Zhi-Jun You @ 2026-11-27 12:34 UTC (permalink / raw)
  To: Felix Fietkau, Lorenzo Bianconi, Andrew Lunn, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Matthias Brugger,
	AngeloGioacchino Del Regno, Sujuan Chen
  Cc: netdev, linux-kernel, linux-arm-kernel, linux-mediatek, Sashiko,
	stable, Zhi-Jun You

mtk_wed_free_rx_buffer already calls mtk_wed_hwrro_free_buffer but
mtk_wed_rx_reset calls both subsequently. This would cause double free
because dma_free_coherent doesn't set freed desc to NULL.

Fix this by setting dev->rx_buf_ring.desc, dev->hw_rro.page and
dev->hw_rro.desc to NULL after freeing so the NULL check can work.

Also remove the duplicated mtk_wed_hwrro_free_buffer call.

hwrro path is compile tested only because I don't own v3 hardware.

Fixes: 1543b8ff02f0 ("net: ethernet: mtk_wed: add wed 3.0 reset support")
Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/map/216156
Cc: stable@vger.kernel.org
Signed-off-by: Zhi-Jun You <hujy652@gmail.com>
---
Changes in v2:
- set dev->rx_buf_ring.desc, dev->hw_rro.page and dev->hw_rro.desc
  to NULL after freeing
- update commit message
- link to v1: https://patch.msgid.link/20261104-double-free-v1-1-efa52326702f@gmail.com
---
Hi Lorenzo, sorry I decided to update this patch instead of adding
another patch.
Hope this is okay.
---
 drivers/net/ethernet/mediatek/mtk_wed.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/mediatek/mtk_wed.c b/drivers/net/ethernet/mediatek/mtk_wed.c
index 0bae25247798..0f215dd3d2d4 100644
--- a/drivers/net/ethernet/mediatek/mtk_wed.c
+++ b/drivers/net/ethernet/mediatek/mtk_wed.c
@@ -880,9 +880,11 @@ mtk_wed_hwrro_free_buffer(struct mtk_wed_device *dev)
 
 	dma_free_coherent(dev->hw->dev, dev->hw_rro.size * sizeof(*desc),
 			  desc, dev->hw_rro.desc_phys);
+	dev->hw_rro.desc = NULL;
 
 free_pagelist:
 	kfree(page_list);
+	dev->hw_rro.pages = NULL;
 }
 
 static void
@@ -896,6 +898,7 @@ mtk_wed_free_rx_buffer(struct mtk_wed_device *dev)
 	dev->wlan.release_rx_buf(dev);
 	dma_free_coherent(dev->hw->dev, dev->rx_buf_ring.size * sizeof(*desc),
 			  desc, dev->rx_buf_ring.desc_phys);
+	dev->rx_buf_ring.desc = NULL;
 
 	mtk_wed_hwrro_free_buffer(dev);
 }
@@ -1722,7 +1725,6 @@ mtk_wed_rx_reset(struct mtk_wed_device *dev)
 				   false);
 	}
 	mtk_wed_free_rx_buffer(dev);
-	mtk_wed_hwrro_free_buffer(dev);
 
 	return 0;
 }

---
base-commit: 28a371228d4eb4857cc31eeb1a0d7d01e9c3ea2f
change-id: 20261115-doublefree-c43a6e0c0149

Best regards,
--  
Zhi-Jun You <hujy652@gmail.com>


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH net v2] net: ethernet: mtk_wed: fix double free in mtk_wed_rx_reset
  2026-11-27 12:34 [PATCH net v2] net: ethernet: mtk_wed: fix double free in mtk_wed_rx_reset Zhi-Jun You
@ 2026-11-28 12:36 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 12:36 UTC (permalink / raw)
  To: hujy652
  Cc: nbd, lorenzo, andrew+netdev, davem, edumazet, kuba, pabeni,
	matthias.bgg, angelogioacchino.delregno, sujuan.chen, netdev,
	linux-kernel, linux-arm-kernel, linux-mediatek, sashiko-bot,
	stable

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

Critical: 0 · High: 0 · Medium: 1 (1) · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] The commit message describes a different bug from the one the
  new `dev->rx_buf_ring.desc = NULL` line fixes, and the only Fixes: tag…

Pre-existing issues:
- [Medium] mtk_wed_rx_reset() frees the RX buffer-manager resources only
  on full success.

--

net: ethernet: mtk_wed: fix double free in mtk_wed_rx_reset

mtk_wed_free_rx_buffer() already calls mtk_wed_hwrro_free_buffer(), so
this patch removes the second mtk_wed_hwrro_free_buffer() call in
mtk_wed_rx_reset(). It also sets dev->rx_buf_ring.desc, dev->hw_rro.desc
and dev->hw_rro.pages to NULL after they are freed, so the existing NULL
checks work.

Link: https://sashiko.dev/#/map/216156

> diff --git a/drivers/net/ethernet/mediatek/mtk_wed.c b/drivers/net/ethernet/mediatek/mtk_wed.c
> index 0bae252477987..0f215dd3d2d44 100644
> --- a/drivers/net/ethernet/mediatek/mtk_wed.c
> +++ b/drivers/net/ethernet/mediatek/mtk_wed.c

[ ... ]

> @@ -896,6 +898,7 @@ mtk_wed_free_rx_buffer(struct mtk_wed_device *dev)
>  	dev->wlan.release_rx_buf(dev);
>  	dma_free_coherent(dev->hw->dev, dev->rx_buf_ring.size * sizeof(*desc),
>  			  desc, dev->rx_buf_ring.desc_phys);
> +	dev->rx_buf_ring.desc = NULL;

[Severity: Medium]
Does this line fix an older bug than the one the Fixes: tag names?

The commit message describes the duplicated mtk_wed_hwrro_free_buffer()
call. Removing that call is enough to fix that bug. The only tag is:

    Fixes: 1543b8ff02f0 ("net: ethernet: mtk_wed: add wed 3.0 reset support")

The stale rx_buf_ring.desc pointer goes back further, to b08134c6e109
("net: ethernet: mtk_wed: add mtk_wed_rx_reset routine"). That commit added
the mtk_wed_free_rx_buffer() call at the end of mtk_wed_rx_reset() and
moved mtk_wed_rx_buffer_alloc() into mtk_wed_start(). It also affects WED
v2 (MT7986/MT7915).

Before this patch, rx_buf_ring.desc stays dangling after a reset until the
next successful mtk_wed_start(). There are two ways the restart can be
skipped.

The first is mt7915_dma_start() with wed_reset=true returning early:

drivers/net/wireless/mediatek/mt76/mt7915/dma.c:mt7915_dma_start() {
    ...
		ret = mt7915_mcu_wed_enable_rx_stats(dev);
		if (ret)
			return ret;

		mtk_wed_device_start(&dev->mt76.mmio.wed, wed_irq_mask);
    ...
}

The second is mtk_wed_start() bailing out when dma_alloc_coherent() fails
in mtk_wed_rx_buffer_alloc().

In either case, a later detach runs the free path again on the stale
pointer:

__mtk_wed_detach()
  mtk_wed_free_rx_rings()
    mtk_wed_free_rx_buffer()
      dev->wlan.release_rx_buf(dev);
      dma_free_coherent(..., desc, ...);

With only 1543b8ff02f0 tagged, would stable trees that have b08134c6e109
but not the hwrro code (such as 6.6.y) miss this part of the fix?

Would it make sense to add a Fixes: tag for b08134c6e109 and describe the
reset->detach double free in the commit message?

Also, the commit message says dev->hw_rro.page, but the field is
dev->hw_rro.pages.

>  
>  	mtk_wed_hwrro_free_buffer(dev);
>  }
> @@ -1722,7 +1725,6 @@ mtk_wed_rx_reset(struct mtk_wed_device *dev)
>  				   false);
>  	}
>  	mtk_wed_free_rx_buffer(dev);
> -	mtk_wed_hwrro_free_buffer(dev);
>  
>  	return 0;
>  }

[Severity: Medium]
This is a pre-existing issue and was not introduced by this patch.
mtk_wed_rx_reset() only reaches mtk_wed_free_rx_buffer() if every step
succeeds. Both mtk_wed_mcu_send_msg() state change calls return early on
error:

	ret = mtk_wed_mcu_send_msg(wo, MTK_WED_MODULE_ID_WO,
				   MTK_WED_WO_CMD_CHANGE_STATE, &val,
				   sizeof(val), true);
	if (ret)
		return ret;

mtk_wed_reset_dma() then ignores the return value:

	if (mtk_wed_get_rx_capa(dev))
		mtk_wed_rx_reset(dev);

After that, the WLAN reset flow calls mtk_wed_device_start() again. On
mt7915 this goes through
mt7915_dma_reset()->mt7915_dma_enable()->mt7915_dma_start().

mtk_wed_start() always calls mtk_wed_rx_buffer_alloc(). That function
overwrites the live pointers without releasing them first:

	dev->rx_buf_ring.desc = desc;
	dev->rx_buf_ring.desc_phys = desc_phys;
	dev->wlan.init_rx_buf(dev, dev->wlan.rx_npkt);

mtk_wed_hwrro_buffer_alloc() overwrites dev->hw_rro.pages and
dev->hw_rro.desc in the same way.

If a firmware message fails during SER recovery, can this leak the old
rx_buf_ring coherent ring? The same question applies to the hw_rro mapped
pages, page_list and coherent ring, and to the RX buffers taken by the
first init_rx_buf() call.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261127-doublefree-v2-1-c0c6d3da3b52%40gmail.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28 12:36 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 12:34 [PATCH net v2] net: ethernet: mtk_wed: fix double free in mtk_wed_rx_reset Zhi-Jun You
2026-11-28 12:36 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®