virtio: check for driver remove callback before calling it
PATCH 1/1 virtio: check for driver remove callback before calling it
The .remove() callback of a driver is normally not mandatory, and the increase of devm_ functions availability makes it even less so. For a virtio_driver it's not marked explicitly as optional, but nothing enforces its presence, so check for its existence before calling it.
The NULL dereference that led to this fix was seen in the wild for the spi-virtio driver, which is currently the only in-tree virtio driver without a remove callback.
---
The behavior of the spi-virtio driver is not conformant to the virtio framework guidelines, but that will be tackled in a different series.
--- drivers/virtio/virtio.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/virtio/virtio.c +2 -1
@@ -374,7 +374,8 @@ static void virtio_dev_remove(struct device *_d)virtio_config_core_disable (dev); - drv->remove (dev); +if (drv->remove) + drv->remove (dev);/* Driver should have reset device. */ WARN_ON_ONCE (dev->config->get_status (dev));
---
base-commit: fa4ad798f6f681031775266044ba1b0cf3c670bd
change-id: 20261108-virtio_drv_remove_fix-5ba8e3123d7e
Best regards,
Signature · 1 line
Francesco Valla <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] virtio: unreset device in virtio_dev_remove() when drv->remove is NULL It also noticed 1 related…
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio: unreset device in virtio_dev_remove() when drv->remove is NULL
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] virtio: use-after-free in virtio_dev_remove() [1] --
commit ccd4078efa3bba738fda1cc44e8e8955495a84fa
Author: Francesco Valla <[email protected]>
virtio: check for driver remove callback before calling it
This commit checks whether drv->remove is non-NULL before calling it in virtio_dev_remove().
Quoted 5 lines
drivers/virtio/virtio.c +0
-0
drivers/virtio/virtio.c static void virtio_dev_remove(struct device *_d) · line 381
Quoted 9 lines
drivers/virtio/virtio.c +2
-1@@ -374,7 +374,8 @@ static void virtio_dev_remove(struct device *_d)virtio_config_core_disable (dev); - drv->remove (dev); +if (drv->remove) + drv->remove (dev);/* Driver should have reset device. */ WARN_ON_ONCE (dev->config->get_status (dev));
[Severity: Medium]
When drv->remove is NULL, will WARN_ON_ONCE(dev->config->get_status(dev)) always fire on unbind or module unload?
__device_release_driver() calls device_remove() (which runs virtio_dev_remove()) before device_unbind_cleanup() runs devres_release_all(). Even when a driver such as spi-virtio registers a devm callback to reset the device and delete its virtqueues, that cleanup has not run yet when virtio_dev_remove() checks get_status() and sets VIRTIO_CONFIG_S_ACKNOWLEDGE. The subsequent devm reset then wipes out VIRTIO_CONFIG_S_ACKNOWLEDGE, or if a driver has no devm reset action at all, the device is never reset and remains active with live virtqueues and DMA after its devm resources are freed.
[Severity: Critical]
This problem wasn't introduced by this patch, but calling of_node_put(dev->dev.of_node) at the end of virtio_dev_remove() causes a refcount underflow and use-after-free [1].
virtio_device_of_init() acquires the dev->dev.of_node reference during register_virtio_device(), not during driver probe, and virtio_dev_remove() drops that reference on driver unbind without clearing dev->dev.of_node. Unbinding and rebinding a driver drops the reference multiple times on the same pointer, while unregistering a device that never bound to a driver leaks the reference.
[1] https://sashiko.dev/#/bug/linux-cf8a6c4a-49fb-4827-8f82-d189cf36ccdd
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 36 lines · On Sun, Nov 08, 2026 at 04:50:38PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] virtio: unreset device invirtio_dev_remove()whendrv->removeis NULL
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] virtio: use-after-free invirtio_dev_remove()[1] --commit
ccd4078efa3bba738fda1cc44e8e8955495a84fa
Author: Francesco Valla <[email protected]>virtio: check for driver remove callback before calling it
This commit checks whether
drv->removeis non-NULL before calling it invirtio_dev_remove().Quoted 5 lines
drivers/virtio/virtio.c +0
-0drivers/virtio/virtio.c static void virtio_dev_remove(struct device *_d) · line 381
Quoted 9 lines
drivers/virtio/virtio.c +2
-1@@ -374,7 +374,8 @@ static void virtio_dev_remove(struct device *_d)virtio_config_core_disable (dev); - drv->remove (dev); +if (drv->remove) + drv->remove (dev);/* Driver should have reset device. */ WARN_ON_ONCE (dev->config->get_status (dev));[Severity: Medium]
Whendrv->removeis NULL, will WARN_ON_ONCE(dev->config->get_status(dev)) always fire on unbind or module unload?
Quoted 10 lines
__device_release_driver()callsdevice_remove()(which runsvirtio_dev_remove()) beforedevice_unbind_cleanup()runsdevres_release_all(). Even when a driver such as spi-virtio registers a devm callback to reset the device and delete its virtqueues, that cleanup has not run yet whenvirtio_dev_remove()checksget_status()and setsVIRTIO_CONFIG_S_ACKNOWLEDGE. The subsequent devm reset then wipes outVIRTIO_CONFIG_S_ACKNOWLEDGE, or if a driver has no devm reset action at all, the device is never reset and remains active with live virtqueues and DMA after its devm resources are freed.
Yes, but that's (IMO) better than a NULL dereference. The spi-virtio driver will be fixed in a different series, as stated.
Quoted 5 lines
[Severity: Critical]
This problem wasn't introduced by this patch, but callingof_node_put(dev->dev.of_node)at the end ofvirtio_dev_remove()causes a refcount underflow and use-after-free [1].
Quoted 9 lines
virtio_device_of_init()acquires thedev->dev.of_nodereference duringregister_virtio_device(), not during driver probe, andvirtio_dev_remove()drops that reference on driver unbind without clearingdev->dev.of_node. Unbinding and rebinding a driver drops the reference multiple times on the same pointer, while unregistering a device that never bound to a driver leaks the reference.[1] https://sashiko.dev/#/bug/linux-cf8a6c4a-49fb-4827-8f82-d189cf36ccdd
That's... interesting. I'll add it to my backlog if no one is already working on it.
Quoted 2 lines
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Regards,
Francesco