Skip to content

Commit 90b5feb

Browse files
stefanhaRHmstsirkin
authored andcommitted
virtio-blk: handle block_device_operations callbacks after hot unplug
A userspace process holding a file descriptor to a virtio_blk device can still invoke block_device_operations after hot unplug. This leads to a use-after-free accessing vblk->vdev in virtblk_getgeo() when ioctl(HDIO_GETGEO) is invoked: BUG: unable to handle kernel NULL pointer dereference at 0000000000000090 IP: [<ffffffffc00e5450>] virtio_check_driver_offered_feature+0x10/0x90 [virtio] PGD 800000003a92f067 PUD 3a930067 PMD 0 Oops: 0000 [#1] SMP CPU: 0 PID: 1310 Comm: hdio-getgeo Tainted: G OE ------------ 3.10.0-1062.el7.x86_64 #1 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.13.0-0-gf21b5a4aeb02-prebuilt.qemu.org 04/01/2014 task: ffff9be5fbfb8000 ti: ffff9be5fa890000 task.ti: ffff9be5fa890000 RIP: 0010:[<ffffffffc00e5450>] [<ffffffffc00e5450>] virtio_check_driver_offered_feature+0x10/0x90 [virtio] RSP: 0018:ffff9be5fa893dc8 EFLAGS: 00010246 RAX: ffff9be5fc3f3400 RBX: ffff9be5fa893e30 RCX: 0000000000000000 RDX: 0000000000000000 RSI: 0000000000000004 RDI: ffff9be5fbc10b40 RBP: ffff9be5fa893dc8 R08: 0000000000000301 R09: 0000000000000301 R10: 0000000000000000 R11: 0000000000000000 R12: ffff9be5fdc24680 R13: ffff9be5fbc10b40 R14: ffff9be5fbc10480 R15: 0000000000000000 FS: 00007f1bfb968740(0000) GS:ffff9be5ffc00000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 0000000000000090 CR3: 000000003a894000 CR4: 0000000000360ff0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: [<ffffffffc016ac37>] virtblk_getgeo+0x47/0x110 [virtio_blk] [<ffffffff8d3f200d>] ? handle_mm_fault+0x39d/0x9b0 [<ffffffff8d561265>] blkdev_ioctl+0x1f5/0xa20 [<ffffffff8d488771>] block_ioctl+0x41/0x50 [<ffffffff8d45d9e0>] do_vfs_ioctl+0x3a0/0x5a0 [<ffffffff8d45dc81>] SyS_ioctl+0xa1/0xc0 A related problem is that virtblk_remove() leaks the vd_index_ida index when something still holds a reference to vblk->disk during hot unplug. This causes virtio-blk device names to be lost (vda, vdb, etc). Fix these issues by protecting vblk->vdev with a mutex and reference counting vblk so the vd_index_ida index can be removed in all cases. Fixes: 48e4043 ("virtio: add virtio disk geometry feature") Reported-by: Lance Digby <[email protected]> Signed-off-by: Stefan Hajnoczi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Michael S. Tsirkin <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]>
1 parent 6a8b55e commit 90b5feb

File tree

1 file changed

+78
-8
lines changed

1 file changed

+78
-8
lines changed

drivers/block/virtio_blk.c

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ struct virtio_blk_vq {
3333
} ____cacheline_aligned_in_smp;
3434

3535
struct virtio_blk {
36+
/*
37+
* This mutex must be held by anything that may run after
38+
* virtblk_remove() sets vblk->vdev to NULL.
39+
*
40+
* blk-mq, virtqueue processing, and sysfs attribute code paths are
41+
* shut down before vblk->vdev is set to NULL and therefore do not need
42+
* to hold this mutex.
43+
*/
44+
struct mutex vdev_mutex;
3645
struct virtio_device *vdev;
3746

3847
/* The disk structure for the kernel. */
@@ -44,6 +53,13 @@ struct virtio_blk {
4453
/* Process context for config space updates */
4554
struct work_struct config_work;
4655

56+
/*
57+
* Tracks references from block_device_operations open/release and
58+
* virtio_driver probe/remove so this object can be freed once no
59+
* longer in use.
60+
*/
61+
refcount_t refs;
62+
4763
/* What host tells us, plus 2 for header & tailer. */
4864
unsigned int sg_elems;
4965

@@ -295,10 +311,55 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
295311
return err;
296312
}
297313

314+
static void virtblk_get(struct virtio_blk *vblk)
315+
{
316+
refcount_inc(&vblk->refs);
317+
}
318+
319+
static void virtblk_put(struct virtio_blk *vblk)
320+
{
321+
if (refcount_dec_and_test(&vblk->refs)) {
322+
ida_simple_remove(&vd_index_ida, vblk->index);
323+
mutex_destroy(&vblk->vdev_mutex);
324+
kfree(vblk);
325+
}
326+
}
327+
328+
static int virtblk_open(struct block_device *bd, fmode_t mode)
329+
{
330+
struct virtio_blk *vblk = bd->bd_disk->private_data;
331+
int ret = 0;
332+
333+
mutex_lock(&vblk->vdev_mutex);
334+
335+
if (vblk->vdev)
336+
virtblk_get(vblk);
337+
else
338+
ret = -ENXIO;
339+
340+
mutex_unlock(&vblk->vdev_mutex);
341+
return ret;
342+
}
343+
344+
static void virtblk_release(struct gendisk *disk, fmode_t mode)
345+
{
346+
struct virtio_blk *vblk = disk->private_data;
347+
348+
virtblk_put(vblk);
349+
}
350+
298351
/* We provide getgeo only to please some old bootloader/partitioning tools */
299352
static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
300353
{
301354
struct virtio_blk *vblk = bd->bd_disk->private_data;
355+
int ret = 0;
356+
357+
mutex_lock(&vblk->vdev_mutex);
358+
359+
if (!vblk->vdev) {
360+
ret = -ENXIO;
361+
goto out;
362+
}
302363

303364
/* see if the host passed in geometry config */
304365
if (virtio_has_feature(vblk->vdev, VIRTIO_BLK_F_GEOMETRY)) {
@@ -314,11 +375,15 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
314375
geo->sectors = 1 << 5;
315376
geo->cylinders = get_capacity(bd->bd_disk) >> 11;
316377
}
317-
return 0;
378+
out:
379+
mutex_unlock(&vblk->vdev_mutex);
380+
return ret;
318381
}
319382

320383
static const struct block_device_operations virtblk_fops = {
321384
.owner = THIS_MODULE,
385+
.open = virtblk_open,
386+
.release = virtblk_release,
322387
.getgeo = virtblk_getgeo,
323388
};
324389

@@ -655,6 +720,10 @@ static int virtblk_probe(struct virtio_device *vdev)
655720
goto out_free_index;
656721
}
657722

723+
/* This reference is dropped in virtblk_remove(). */
724+
refcount_set(&vblk->refs, 1);
725+
mutex_init(&vblk->vdev_mutex);
726+
658727
vblk->vdev = vdev;
659728
vblk->sg_elems = sg_elems;
660729

@@ -820,8 +889,6 @@ static int virtblk_probe(struct virtio_device *vdev)
820889
static void virtblk_remove(struct virtio_device *vdev)
821890
{
822891
struct virtio_blk *vblk = vdev->priv;
823-
int index = vblk->index;
824-
int refc;
825892

826893
/* Make sure no work handler is accessing the device. */
827894
flush_work(&vblk->config_work);
@@ -831,18 +898,21 @@ static void virtblk_remove(struct virtio_device *vdev)
831898

832899
blk_mq_free_tag_set(&vblk->tag_set);
833900

901+
mutex_lock(&vblk->vdev_mutex);
902+
834903
/* Stop all the virtqueues. */
835904
vdev->config->reset(vdev);
836905

837-
refc = kref_read(&disk_to_dev(vblk->disk)->kobj.kref);
906+
/* Virtqueues are stopped, nothing can use vblk->vdev anymore. */
907+
vblk->vdev = NULL;
908+
838909
put_disk(vblk->disk);
839910
vdev->config->del_vqs(vdev);
840911
kfree(vblk->vqs);
841-
kfree(vblk);
842912

843-
/* Only free device id if we don't have any users */
844-
if (refc == 1)
845-
ida_simple_remove(&vd_index_ida, index);
913+
mutex_unlock(&vblk->vdev_mutex);
914+
915+
virtblk_put(vblk);
846916
}
847917

848918
#ifdef CONFIG_PM_SLEEP

0 commit comments

Comments
 (0)