Skip to content

Commit b788ad3

Browse files
Wolfram Sanggregkh
authored andcommitted
virtio-mmio: don't break lifecycle of vm_dev
[ Upstream commit 55c91fe ] vm_dev has a separate lifecycle because it has a 'struct device' embedded. Thus, having a release callback for it is correct. Allocating the vm_dev struct with devres totally breaks this protection, though. Instead of waiting for the vm_dev release callback, the memory is freed when the platform_device is removed. Resulting in a use-after-free when finally the callback is to be called. To easily see the problem, compile the kernel with CONFIG_DEBUG_KOBJECT_RELEASE and unbind with sysfs. The fix is easy, don't use devres in this case. Found during my research about object lifetime problems. Fixes: 7eb781b ("virtio_mmio: add cleanup for virtio_mmio_probe") Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Message-Id: <20230629120526.7184-1-wsa+renesas@sang-engineering.com> Signed-off-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent e22a4b7 commit b788ad3

File tree

1 file changed

+2
-3
lines changed

1 file changed

+2
-3
lines changed

drivers/virtio/virtio_mmio.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,8 @@ static void virtio_mmio_release_dev(struct device *_d)
543543
struct virtio_device *vdev =
544544
container_of(_d, struct virtio_device, dev);
545545
struct virtio_mmio_device *vm_dev = to_virtio_mmio_device(vdev);
546-
struct platform_device *pdev = vm_dev->pdev;
547546

548-
devm_kfree(&pdev->dev, vm_dev);
547+
kfree(vm_dev);
549548
}
550549

551550
/* Platform device */
@@ -556,7 +555,7 @@ static int virtio_mmio_probe(struct platform_device *pdev)
556555
unsigned long magic;
557556
int rc;
558557

559-
vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
558+
vm_dev = kzalloc(sizeof(*vm_dev), GFP_KERNEL);
560559
if (!vm_dev)
561560
return -ENOMEM;
562561

0 commit comments

Comments
 (0)