Re: [BUG] Unloading mt7921e module cause use-after-free

From: Mikhail Gavrilov
Date: Fri Jan 12 2024 - 02:31:32 EST


On Fri, Jan 12, 2024 at 10:34 AM Deren Wu (武德仁) <Deren.Wu@xxxxxxxxxxxx> wrote:
>
> Hi Mikhail,
>
> I can reproduce a similar problem when CONFI_DEBUG_SHIRQ is enabled.
>
> Pleaese try this patch in mainline kernel (6.7+)
> ---
> --- a/drivers/net/wireless/mediatek/mt76/mt7921/pci.c
> +++ b/drivers/net/wireless/mediatek/mt76/mt7921/pci.c
> @@ -387,6 +387,7 @@ static void mt7921_pci_remove(struct pci_dev *pdev)
> struct mt792x_dev *dev = container_of(mdev, struct mt792x_dev,
> mt76);
>
> mt7921e_unregister_device(dev);
> + set_bit(MT76_REMOVED, &mdev->phy.state);
> devm_free_irq(&pdev->dev, pdev->irq, dev);
> mt76_free_device(&dev->mt76);
> pci_free_irq_vectors(pdev);
> diff --git a/drivers/net/wireless/mediatek/mt76/mt792x_dma.c
> b/drivers/net/wireless/mediatek/mt76/mt792x_dma.c
> index 488326ce5ed4..3893dbe866fe 100644
> --- a/drivers/net/wireless/mediatek/mt76/mt792x_dma.c
> +++ b/drivers/net/wireless/mediatek/mt76/mt792x_dma.c
> @@ -12,6 +12,8 @@ irqreturn_t mt792x_irq_handler(int irq, void
> *dev_instance)
> {
> struct mt792x_dev *dev = dev_instance;
>
> + if (test_bit(MT76_REMOVED, &dev->mt76.phy.state))
> + return IRQ_NONE;
> mt76_wr(dev, dev->irq_map->host_irq_enable, 0);
>
> if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
>
>

Thanks, this patch looks good to me.
Demonstration: https://youtu.be/nKnA2ftVoXw

Tested-by: Mikhail Gavrilov <mikhail.v.gavrilov@xxxxxxxxx>

--
Best Regards,
Mike Gavrilov.