Skip to contentSkip to conversation

virtio-blk: check host supplied logical block size

PATCH In review Maxim Levitsky 3 participants 6 messages Jul 15, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-blk: check host supplied logical block size

Linux kernel only supports logical block sizes which are power of two, at least 512 bytes and no more that PAGE_SIZE.

Check this instead of crashing later on.

Note that there is no need to check physical block size since it is only a hint, and virtio-blk already only supports power of two values.

Bugzilla link: https://bugzilla.redhat.com/show_bug.cgi?id=1664619

---
 drivers/block/virtio_blk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/block/virtio_blk.c +18 -2
index 980df853ee497..36dda31cc4e96 100644
@@ -681,6 +681,12 @@ static const struct blk_mq_ops virtio_mq_ops = {
 static unsigned int virtblk_queue_depth;
 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
 
+
+static bool virtblk_valid_block_size(unsigned int blksize)
+{
+	return blksize >= 512 && blksize <= PAGE_SIZE && is_power_of_2(blksize);
+}
+
 static int virtblk_probe(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk;
@@ -809,9 +815,16 @@ static int virtblk_probe(struct virtio_device *vdev)
 	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
 				   struct virtio_blk_config, blk_size,
 				   &blk_size);
-	if (!err)
+	if (!err) {
+		if (!virtblk_valid_block_size(blk_size)) {
+			dev_err(&vdev->dev,
+				"%s failure: unsupported logical block size %d\n",
+				__func__, blk_size);
+			err = -EINVAL;
+			goto out_cleanup_queue;
+		}
 		blk_queue_logical_block_size(q, blk_size);
-	else
+	} else
 		blk_size = queue_logical_block_size(q);
 
 	/* Use topology information if available */
@@ -872,6 +885,9 @@ static int virtblk_probe(struct virtio_device *vdev)
 	device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
 	return 0;
 
+out_cleanup_queue:
+	blk_cleanup_queue(vblk->disk->queue);
+	vblk->disk->queue = NULL;
 out_free_tags:
 	blk_mq_free_tag_set(&vblk->tag_set);
 out_put_disk:
Signature · 1 line
2.26.2
#1 lore raw
Quoted 20 lines · Maxim Levitsky

Linux kernel only supports logical block sizes which are power of two, at least 512 bytes and no more that PAGE_SIZE.

Check this instead of crashing later on.

Note that there is no need to check physical block size since it is only a hint, and virtio-blk already only supports power of two values.

Bugzilla link: https://bugzilla.redhat.com/show_bug.cgi?id=1664619

---
 drivers/block/virtio_blk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/block/virtio_blk.c +0 -0
index 980df853ee497..36dda31cc4e96 100644

drivers/block/virtio_blk.c static const struct blk_mq_ops virtio_mq_ops = { · line 689

Quoted 9 lines
drivers/block/virtio_blk.c +6 -0
@@ -681,6 +681,12 @@ static const struct blk_mq_ops virtio_mq_ops = {
 static unsigned int virtblk_queue_depth;
 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
 
+
+static bool virtblk_valid_block_size(unsigned int blksize)
+{
+	return blksize >= 512 && blksize <= PAGE_SIZE && is_power_of_2(blksize);
+}
+

Is this a blk core assumption? in that case, does not this belong in blk core?

Quoted 12 lines
drivers/block/virtio_blk.c +4 -1
 static int virtblk_probe(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk;
@@ -809,9 +815,16 @@ static int virtblk_probe(struct virtio_device *vdev)
 	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
 				   struct virtio_blk_config, blk_size,
 				   &blk_size);
-	if (!err)
+	if (!err) {
+		if (!virtblk_valid_block_size(blk_size)) {
+			dev_err(&vdev->dev,
+				"%s failure: unsupported logical block size %d\n",

drivers/block/virtio_blk.c static int virtblk_probe(struct virtio_device *vdev) · line 830

Quoted 10 lines
drivers/block/virtio_blk.c +5 -1
+				__func__, blk_size);
+			err = -EINVAL;
+			goto out_cleanup_queue;
+		}
 		blk_queue_logical_block_size(q, blk_size);
-	else
+	} else
 		blk_size = queue_logical_block_size(q);
 
 	/* Use topology information if available */

OK so if we are doing this pls add {} around blk_size = queue_logical_block_size(q); too.

Quoted 12 lines
drivers/block/virtio_blk.c +3 -0
@@ -872,6 +885,9 @@ static int virtblk_probe(struct virtio_device *vdev)
 	device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
 	return 0;
 
+out_cleanup_queue:
+	blk_cleanup_queue(vblk->disk->queue);
+	vblk->disk->queue = NULL;
 out_free_tags:
 	blk_mq_free_tag_set(&vblk->tag_set);
 out_put_disk:
Signature · 1 line
2.26.2
#2 lore raw
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Maxim Levitsky

Linux kernel only supports logical block sizes which are power of two,
at least 512 bytes and no more that PAGE_SIZE.

Check this instead of crashing later on.

Note that there is no need to check physical block size since it is only a hint, and virtio-blk already only supports power of two values.

Bugzilla link: https://bugzilla.redhat.com/show_bug.cgi?id=1664619

---
 drivers/block/virtio_blk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/block/virtio_blk.c +0 -0
index 980df853ee497..36dda31cc4e96 100644
@@ -681,6 +681,12 @@ static const struct blk_mq_ops virtio_mq_ops =
 {
 static unsigned int virtblk_queue_depth;
 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
 
Quoted 10 lines

drivers/block/virtio_blk.c static const struct blk_mq_ops virtio_mq_ops = { · line 689

Quoted 7 lines
drivers/block/virtio_blk.c +6 -0
+
+static bool virtblk_valid_block_size(unsigned int blksize)
+{
+	return blksize >= 512 && blksize <= PAGE_SIZE &&
 is_power_of_2(blksize);
+}
+

Is this a blk core assumption? in that case, does not this belong in blk core?

It is a blk core assumption.
I had checked other drivers and these that have variable block size all check this manually like that.

I don't mind fixing all of them but I am a bit afraid to create too much mess.

Quoted 24 lines
Quoted 24 lines
drivers/block/virtio_blk.c +0 -0
 static int virtblk_probe(struct virtio_device *vdev)

{

drivers/block/virtio_blk.c +9 -2
 	struct virtio_blk *vblk;
@@ -809,9 +815,16 @@ static int virtblk_probe(struct virtio_device
 *vdev)
 	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
 				   struct virtio_blk_config, blk_size,
 				   &blk_size);
-	if (!err)
+	if (!err) {
+		if (!virtblk_valid_block_size(blk_size)) {
+			dev_err(&vdev->dev,
+				"%s failure: unsupported logical block
 size %d\n",
+				__func__, blk_size);
+			err = -EINVAL;
+			goto out_cleanup_queue;
+		}
 		blk_queue_logical_block_size(q, blk_size);
-	else
+	} else
 		blk_size = queue_logical_block_size(q);
Quoted 5 lines
Quoted 1 line

/* Use topology information if available */

OK so if we are doing this pls add {} around blk_size = queue_logical_block_size(q);
too.

Will do.

Quoted 6 lines
Quoted 6 lines
drivers/block/virtio_blk.c +0 -0
@@ -872,6 +885,9 @@ static int virtblk_probe(struct virtio_device
 *vdev)
 	device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
 	return 0;
 
Quoted 8 lines
Quoted 8 lines
drivers/block/virtio_blk.c +3 -0
+out_cleanup_queue:
+	blk_cleanup_queue(vblk->disk->queue);
+	vblk->disk->queue = NULL;
 out_free_tags:
 	blk_mq_free_tag_set(&vblk->tag_set);

out_put_disk:

Signature · 1 line
2.26.2

Best regards,
Maxim Levitsky

#3 lore raw
Quoted 39 lines · Maxim Levitsky
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Maxim Levitsky

Linux kernel only supports logical block sizes which are power of two,
at least 512 bytes and no more that PAGE_SIZE.

Check this instead of crashing later on.

Note that there is no need to check physical block size since it is only a hint, and virtio-blk already only supports power of two values.

Bugzilla link: https://bugzilla.redhat.com/show_bug.cgi?id=1664619

---
 drivers/block/virtio_blk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/block/virtio_blk.c +0 -0
index 980df853ee497..36dda31cc4e96 100644
@@ -681,6 +681,12 @@ static const struct blk_mq_ops virtio_mq_ops =
 {
 static unsigned int virtblk_queue_depth;
 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
 
Quoted 10 lines

drivers/block/virtio_blk.c static const struct blk_mq_ops virtio_mq_ops = { · line 689

Quoted 7 lines
drivers/block/virtio_blk.c +6 -0
+
+static bool virtblk_valid_block_size(unsigned int blksize)
+{
+	return blksize >= 512 && blksize <= PAGE_SIZE &&
 is_power_of_2(blksize);
+}
+

Is this a blk core assumption? in that case, does not this belong in blk core?

Quoted 6 lines

It is a blk core assumption.
I had checked other drivers and these that have variable block size all check this manually like that.

I don't mind fixing all of them but I am a bit afraid to create too much mess.

You don't have to, start by adding this in blk core and using in virtio. Patches to update all drivers can then come separately.

Quoted 49 lines
Quoted 24 lines
Quoted 24 lines
drivers/block/virtio_blk.c +0 -0
 static int virtblk_probe(struct virtio_device *vdev)

{

drivers/block/virtio_blk.c +9 -2
 	struct virtio_blk *vblk;
@@ -809,9 +815,16 @@ static int virtblk_probe(struct virtio_device
 *vdev)
 	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
 				   struct virtio_blk_config, blk_size,
 				   &blk_size);
-	if (!err)
+	if (!err) {
+		if (!virtblk_valid_block_size(blk_size)) {
+			dev_err(&vdev->dev,
+				"%s failure: unsupported logical block
 size %d\n",
+				__func__, blk_size);
+			err = -EINVAL;
+			goto out_cleanup_queue;
+		}
 		blk_queue_logical_block_size(q, blk_size);
-	else
+	} else
 		blk_size = queue_logical_block_size(q);
Quoted 5 lines
Quoted 1 line

/* Use topology information if available */

OK so if we are doing this pls add {} around blk_size = queue_logical_block_size(q);
too.

Will do.

Quoted 6 lines
Quoted 6 lines
drivers/block/virtio_blk.c +0 -0
@@ -872,6 +885,9 @@ static int virtblk_probe(struct virtio_device
 *vdev)
 	device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
 	return 0;
 
Quoted 8 lines
Quoted 8 lines
drivers/block/virtio_blk.c +3 -0
+out_cleanup_queue:
+	blk_cleanup_queue(vblk->disk->queue);
+	vblk->disk->queue = NULL;
 out_free_tags:
 	blk_mq_free_tag_set(&vblk->tag_set);

out_put_disk:

Signature · 1 line
2.26.2

Best regards,
Maxim Levitsky

#4 lore raw
Quoted 44 lines · Michael S. Tsirkin
Quoted 43 lines · Maxim Levitsky
Quoted 27 lines · Michael S. Tsirkin
Quoted 26 lines · Maxim Levitsky

Linux kernel only supports logical block sizes which are power of two,
at least 512 bytes and no more that PAGE_SIZE.

Check this instead of crashing later on.

Note that there is no need to check physical block size since it is only a hint, and virtio-blk already only supports power of two values.

Bugzilla link: https://bugzilla.redhat.com/show_bug.cgi?id=1664619

---
 drivers/block/virtio_blk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/block/virtio_blk.c +0 -0
index 980df853ee497..36dda31cc4e96 100644
@@ -681,6 +681,12 @@ static const struct blk_mq_ops virtio_mq_ops =
 {
 static unsigned int virtblk_queue_depth;
 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
 
Quoted 10 lines

drivers/block/virtio_blk.c static const struct blk_mq_ops virtio_mq_ops = { · line 689

Quoted 7 lines
drivers/block/virtio_blk.c +6 -0
+
+static bool virtblk_valid_block_size(unsigned int blksize)
+{
+	return blksize >= 512 && blksize <= PAGE_SIZE &&
 is_power_of_2(blksize);
+}
+

Is this a blk core assumption? in that case, does not this belong in blk core?

It is a blk core assumption.
I had checked other drivers and these that have variable block size all check this manually like that.

Quoted 5 lines
Quoted 2 lines

I don't mind fixing all of them but I am a bit afraid to create too much mess.

You don't have to, start by adding this in blk core and using in virtio. Patches to update all drivers can then come separately.

I'll do this then.

Best regards,
Maxim Levitsky

Quoted 47 lines
Quoted 47 lines
Quoted 24 lines
Quoted 24 lines
drivers/block/virtio_blk.c +0 -0
 static int virtblk_probe(struct virtio_device *vdev)

{

drivers/block/virtio_blk.c +9 -2
 	struct virtio_blk *vblk;
@@ -809,9 +815,16 @@ static int virtblk_probe(struct virtio_device
 *vdev)
 	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
 				   struct virtio_blk_config, blk_size,
 				   &blk_size);
-	if (!err)
+	if (!err) {
+		if (!virtblk_valid_block_size(blk_size)) {
+			dev_err(&vdev->dev,
+				"%s failure: unsupported logical block
 size %d\n",
+				__func__, blk_size);
+			err = -EINVAL;
+			goto out_cleanup_queue;
+		}
 		blk_queue_logical_block_size(q, blk_size);
-	else
+	} else
 		blk_size = queue_logical_block_size(q);
Quoted 5 lines
Quoted 1 line

/* Use topology information if available */

OK so if we are doing this pls add {} around blk_size = queue_logical_block_size(q);
too.

Will do.

Quoted 5 lines
Quoted 5 lines
drivers/block/virtio_blk.c +0 -0
@@ -872,6 +885,9 @@ static int virtblk_probe(struct virtio_device
 *vdev)
 	device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
 	return 0;
 
Quoted 8 lines
Quoted 8 lines
drivers/block/virtio_blk.c +3 -0
+out_cleanup_queue:
+	blk_cleanup_queue(vblk->disk->queue);
+	vblk->disk->queue = NULL;
 out_free_tags:
 	blk_mq_free_tag_set(&vblk->tag_set);

out_put_disk:

Signature · 1 line
2.26.2

Best regards,
Maxim Levitsky

Reviewed-by on v1 1/1
#5 lore raw
In reply to Maxim Levitsky
Quoted 65 lines · Maxim Levitsky

Linux kernel only supports logical block sizes which are power of two, at least 512 bytes and no more that PAGE_SIZE.

Check this instead of crashing later on.

Note that there is no need to check physical block size since it is only a hint, and virtio-blk already only supports power of two values.

Bugzilla link: https://bugzilla.redhat.com/show_bug.cgi?id=1664619

---
 drivers/block/virtio_blk.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
drivers/block/virtio_blk.c +18 -3
index 980df853ee497..36dda31cc4e96 100644
@@ -681,6 +681,12 @@ static const struct blk_mq_ops virtio_mq_ops = {
 static unsigned int virtblk_queue_depth;
 module_param_named(queue_depth, virtblk_queue_depth, uint, 0444);
 
+
+static bool virtblk_valid_block_size(unsigned int blksize)
+{
+       return blksize >= 512 && blksize <= PAGE_SIZE && is_power_of_2(blksize);
+}
+
 static int virtblk_probe(struct virtio_device *vdev)
 {
        struct virtio_blk *vblk;
@@ -809,9 +815,16 @@ static int virtblk_probe(struct virtio_device *vdev)
        err = virtio_cread_feature(vdev, VIRTIO_BLK_F_BLK_SIZE,
                                   struct virtio_blk_config, blk_size,
                                   &blk_size);
-       if (!err)
+       if (!err) {
+               if (!virtblk_valid_block_size(blk_size)) {
+                       dev_err(&vdev->dev,
+                               "%s failure: unsupported logical block size %d\n",
+                               __func__, blk_size);
+                       err = -EINVAL;
+                       goto out_cleanup_queue;
+               }
                blk_queue_logical_block_size(q, blk_size);
-       else
+       } else
                blk_size = queue_logical_block_size(q);
 
        /* Use topology information if available */
@@ -872,6 +885,9 @@ static int virtblk_probe(struct virtio_device *vdev)
        device_add_disk(&vdev->dev, vblk->disk, virtblk_attr_groups);
        return 0;
 
+out_cleanup_queue:
+       blk_cleanup_queue(vblk->disk->queue);
+       vblk->disk->queue = NULL;
 out_free_tags:
        blk_mq_free_tag_set(&vblk->tag_set);
 out_put_disk:
--

2.26.2

Quoted 4 lines

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Looks fine,

Signature · 1 line
Ming Lei