virtio-blk: handle block_device_operations callbacks after hot unplug
Subsystems (3)
Stefan Hajnoczi
reviewerPATCH 1/1 virtio-blk: handle block_device_operations callbacks after hot unplug
A virtio_blk block device can still be referenced after hot unplug by userspace processes that hold the file descriptor. In this case virtblk_getgeo() can be invoked after virtblk_remove() was called. For example, a program that has /dev/vdb open can call ioctl(HDIO_GETGEO) after hot unplug.
Fix this by clearing vblk->disk->private_data and checking that the virtio_blk driver instance is still around in virtblk_getgeo().
Note that the virtblk_getgeo() function itself is guaranteed to remain in memory after hot unplug because the virtio_blk module refcount is still held while a block device reference exists.
--- drivers/block/virtio_blk.c | 5 +++++ 1 file changed, 5 insertions(+)
drivers/block/virtio_blk.c +5 -0
@@ -300,6 +300,10 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) {struct virtio_blk *vblk = bd->bd_disk->private_data; +/* Driver instance has been removed */ +if (!vblk) +return -ENOTTY ; +/* see if the host passed in geometry config */ if (virtio_has_feature (vblk->vdev,VIRTIO_BLK_F_GEOMETRY )) { virtio_cread(vblk->vdev, struct virtio_blk_config,
@@ -835,6 +839,7 @@ static void virtblk_remove(struct virtio_device *vdev) vdev->config->reset (vdev); refc =kref_read (&disk_to_dev (vblk->disk)->kobj.kref); + vblk->disk->private_data =NULL ;put_disk (vblk->disk); vdev->config->del_vqs (vdev);kfree (vblk->vqs);
Signature · 1 line
2.25.1
Michael S. Tsirkin
maintainerQuoted 24 lines · Stefan Hajnoczi
A
virtio_blkblock device can still be referenced after hot unplug by userspace processes that hold the file descriptor. In this casevirtblk_getgeo()can be invoked aftervirtblk_remove()was called. For example, a program that has/dev/vdbopen can callioctl(HDIO_GETGEO)after hot unplug.Fix this by clearing
vblk->disk->private_dataand checking that thevirtio_blkdriver instance is still around invirtblk_getgeo().Note that the
virtblk_getgeo()function itself is guaranteed to remain in memory after hot unplug because thevirtio_blkmodule refcount is still held while a block device reference exists.Originally-by Lance Digby <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- drivers/block/virtio_blk.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/block/virtio_blk.c +0
-0
drivers/block/virtio_blk.c static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) · line 309
Quoted 10 lines
drivers/block/virtio_blk.c +4
-0@@ -300,6 +300,10 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) {struct virtio_blk *vblk = bd->bd_disk->private_data; +/* Driver instance has been removed */ +if (!vblk) +return -ENOTTY ; +/* see if the host passed in geometry config */ if (virtio_has_feature (vblk->vdev,VIRTIO_BLK_F_GEOMETRY )) { virtio_cread(vblk->vdev, struct virtio_blk_config,
Just so I understand, what serializes this access? See below for what looks like a race condition ...
drivers/block/virtio_blk.c static void virtblk_remove(struct virtio_device *vdev) · line 841
Quoted 4 lines
drivers/block/virtio_blk.c +0
-0@@ -835,6 +839,7 @@ static void virtblk_remove(struct virtio_device *vdev) vdev->config->reset (vdev); refc =kref_read (&disk_to_dev (vblk->disk)->kobj.kref);
So what if private_data is tested at this time ...
drivers/block/virtio_blk.c static void virtblk_remove(struct virtio_device *vdev) · line 845
Quoted 4 lines
drivers/block/virtio_blk.c +1
-0+ vblk->disk->private_data =NULL ;put_disk (vblk->disk); vdev->config->del_vqs (vdev);kfree (vblk->vqs);
... and then used at this time?
What prevents this?
Quoted 3 lines
Signature · 1 line
2.25.1
Quoted 24 lines · Stefan Hajnoczi
A
virtio_blkblock device can still be referenced after hot unplug by userspace processes that hold the file descriptor. In this casevirtblk_getgeo()can be invoked aftervirtblk_remove()was called. For example, a program that has/dev/vdbopen can callioctl(HDIO_GETGEO)after hot unplug.Fix this by clearing
vblk->disk->private_dataand checking that thevirtio_blkdriver instance is still around invirtblk_getgeo().Note that the
virtblk_getgeo()function itself is guaranteed to remain in memory after hot unplug because thevirtio_blkmodule refcount is still held while a block device reference exists.Originally-by Lance Digby <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- drivers/block/virtio_blk.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/block/virtio_blk.c +0
-0
drivers/block/virtio_blk.c static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) · line 305
Quoted 6 lines
drivers/block/virtio_blk.c +3
-0@@ -300,6 +300,10 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) {struct virtio_blk *vblk = bd->bd_disk->private_data; +/* Driver instance has been removed */ +if (!vblk) +return -ENOTTY ;
If this ever hits you have a nasty race condition and this is not going to fix it, as it could be removed just after this check as well.
Quoted 35 lines · Stefan Hajnoczi
A
virtio_blkblock device can still be referenced after hot unplug by userspace processes that hold the file descriptor. In this casevirtblk_getgeo()can be invoked aftervirtblk_remove()was called. For example, a program that has/dev/vdbopen can callioctl(HDIO_GETGEO)after hot unplug.Fix this by clearing
vblk->disk->private_dataand checking that thevirtio_blkdriver instance is still around invirtblk_getgeo().Note that the
virtblk_getgeo()function itself is guaranteed to remain in memory after hot unplug because thevirtio_blkmodule refcount is still held while a block device reference exists.Originally-by Lance Digby <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- drivers/block/virtio_blk.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/block/virtio_blk.c +4
-0@@ -300,6 +300,10 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) {struct virtio_blk *vblk = bd->bd_disk->private_data; +/* Driver instance has been removed */ +if (!vblk) +return -ENOTTY ; +/* see if the host passed in geometry config */ if (virtio_has_feature (vblk->vdev,VIRTIO_BLK_F_GEOMETRY )) { virtio_cread(vblk->vdev, struct virtio_blk_config,
drivers/block/virtio_blk.c static void virtblk_remove(struct virtio_device *vdev) · line 845
Quoted 7 lines
drivers/block/virtio_blk.c +1
-0@@ -835,6 +839,7 @@ static void virtblk_remove(struct virtio_device *vdev) vdev->config->reset (vdev); refc =kref_read (&disk_to_dev (vblk->disk)->kobj.kref); + vblk->disk->private_data =NULL ;put_disk (vblk->disk); vdev->config->del_vqs (vdev);kfree (vblk->vqs);
As pointed out, can be a race. We had a very similar issue in virtio-vsock, and we solved using RCU to assign and get the pointer [1], maybe the same solution can work here.
Cheers,
Stefano
[1] 0deab087b16a vsock/virtio: use RCU to avoid use-after-free on the_virtio_vsock
Stefan Hajnoczi
reviewerQuoted 28 lines · Christoph Hellwig
Quoted 27 lines · Stefan Hajnoczi
A
virtio_blkblock device can still be referenced after hot unplug by userspace processes that hold the file descriptor. In this casevirtblk_getgeo()can be invoked aftervirtblk_remove()was called. For example, a program that has/dev/vdbopen can callioctl(HDIO_GETGEO)after hot unplug.Fix this by clearing
vblk->disk->private_dataand checking that thevirtio_blkdriver instance is still around invirtblk_getgeo().Note that the
virtblk_getgeo()function itself is guaranteed to remain in memory after hot unplug because thevirtio_blkmodule refcount is still held while a block device reference exists.Originally-by Lance Digby <[email protected]>Signed-off-by Stefan Hajnoczi <[email protected]>--- drivers/block/virtio_blk.c | 5 +++++ 1 file changed, 5 insertions(+)drivers/block/virtio_blk.c +0
-0@@ -300,6 +300,10 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) {struct virtio_blk *vblk = bd->bd_disk->private_data;
Quoted 6 lines
drivers/block/virtio_blk.c static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) · line 305
Quoted 3 lines
drivers/block/virtio_blk.c +3
-0+/* Driver instance has been removed */ +if (!vblk) +return -ENOTTY ;If this ever hits you have a nasty race condition and this is not going to fix it, as it could be removed just after this check as well.
Perhaps a better fix is to keep a refcount in struct virtio_blk and implement block_device_operations->release() to decrement the refcount.
That way the struct virtio_blk stays around until all block device references are dropped. This is similar to nbd_put() in drivers/block/nbd.c.
Stefan