summaryrefslogtreecommitdiffstats
path: root/drivers/serial
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2021-09-16 11:34:58 +0200
committerSascha Hauer <s.hauer@pengutronix.de>2021-10-02 11:16:23 +0200
commit1c8625aac0d204e567fcee5a8b4e9d0145f011c0 (patch)
treed02216c2b2e22f9a8817dc2e016636e397c33e9d /drivers/serial
parent41023724b2b09ec1c8555bb32c929a82ebc84359 (diff)
downloadbarebox-1c8625aac0d204e567fcee5a8b4e9d0145f011c0.tar.gz
barebox-1c8625aac0d204e567fcee5a8b4e9d0145f011c0.tar.xz
virtio: implement remove callbacks
virtio parent device drivers (e.g. PCI and MMIO) create child devices and free them on remove. The virtio drivers for the child devices (e.g. block and console) however don't unregister with their respective subsystems in the remove callbacks. So these subsystems may have stale pointers pointing at removed devices. This is especially problematic for the console driver, because the virtio console device_d will be removed, but the console itself remains registered leading to a use-after-free as soon as printf is invoked for the previously active console. This leads to a crash when typing reset in https://www.barebox.org/jsbarebox/?graphic=0 Fix this for all virtio drivers. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> Link: https://lore.barebox.org/20210916093458.21102-1-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'drivers/serial')
-rw-r--r--drivers/serial/virtio_console.c14
1 files changed, 14 insertions, 0 deletions
diff --git a/drivers/serial/virtio_console.c b/drivers/serial/virtio_console.c
index a1331035d9..a4adb77610 100644
--- a/drivers/serial/virtio_console.c
+++ b/drivers/serial/virtio_console.c
@@ -134,6 +134,8 @@ static int virtcons_probe(struct virtio_device *vdev)
virtcons = xzalloc(sizeof(*virtcons));
+ vdev->priv = virtcons;
+
virtcons->in_vq = vqs[0];
virtcons->out_vq = vqs[1];
@@ -150,6 +152,17 @@ static int virtcons_probe(struct virtio_device *vdev)
return console_register(&virtcons->cdev);
}
+static void virtcons_remove(struct virtio_device *vdev)
+{
+ struct virtio_console *virtcons = vdev->priv;
+
+ vdev->config->reset(vdev);
+ console_unregister(&virtcons->cdev);
+ vdev->config->del_vqs(vdev);
+
+ free(virtcons);
+}
+
static struct virtio_device_id id_table[] = {
{ VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
{ 0 },
@@ -159,6 +172,7 @@ static struct virtio_driver virtio_console = {
.driver.name = "virtio_console",
.id_table = id_table,
.probe = virtcons_probe,
+ .remove = virtcons_remove,
};
device_virtio_driver(virtio_console);