Skip to contentSkip to conversation

virtio-blk: limit seg_max to a safe value

PATCH In review Stefan Hajnoczi 3 participants 4 messages May 24, 2021 – Jun 3, 2021

Subsystems (3)
lore
Share & tools
lore

Stefan Hajnoczi

author reviewer
#0 lore raw

PATCH v2 1/1 virtio-blk: limit seg_max to a safe value

The struct virtio_blk_config seg_max value is read from the device and incremented by 2 to account for the request header and status byte descriptors added by the driver.

In preparation for supporting untrusted virtio-blk devices, protect against integer overflow and limit the value to a safe maximum.

---
v2:
* Limit to a virtio-specific value instead of using SG_MAX_SEGMENTS [Christoph]

---
 drivers/block/virtio_blk.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +7 -1
index b9fa3ef5b57c..1635d4289202 100644
@@ -21,6 +21,9 @@
 #define VQ_NAME_LEN 16
 #define MAX_DISCARD_SEGMENTS 256u
 
+/* The maximum number of sg elements that fit into a virtqueue */
+#define VIRTIO_BLK_MAX_SG_ELEMS 32768
+
 static int major;
 static DEFINE_IDA(vd_index_ida);
 
@@ -728,7 +731,10 @@ static int virtblk_probe(struct virtio_device *vdev)
 	if (err || !sg_elems)
 		sg_elems = 1;
 
-	/* We need an extra sg elements at head and tail. */
+	/* Prevent integer overflows and honor max vq size */
+	sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
+
+	/* We need extra sg elements at head and tail. */
 	sg_elems += 2;
 	vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
 	if (!vblk) {
Signature · 1 line
2.31.1
Reviewed-by on v2 1/1
#1 lore raw
Quoted 8 lines · Stefan Hajnoczi

The struct virtio_blk_config seg_max value is read from the device and incremented by 2 to account for the request header and status byte descriptors added by the driver.

In preparation for supporting untrusted virtio-blk devices, protect against integer overflow and limit the value to a safe maximum.

Looks good,

#2 lore raw
In reply to Stefan Hajnoczi
Quoted 21 lines · Stefan Hajnoczi

The struct virtio_blk_config seg_max value is read from the device and incremented by 2 to account for the request header and status byte descriptors added by the driver.

In preparation for supporting untrusted virtio-blk devices, protect against integer overflow and limit the value to a safe maximum.

---
v2:
* Limit to a virtio-specific value instead of using SG_MAX_SEGMENTS [Christoph]

---
 drivers/block/virtio_blk.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +0 -0
index b9fa3ef5b57c..1635d4289202 100644

drivers/block/virtio_blk.c line 25

Quoted 5 lines
drivers/block/virtio_blk.c +2 -0
@@ -21,6 +21,9 @@
 #define VQ_NAME_LEN 16
 #define MAX_DISCARD_SEGMENTS 256u
 
+/* The maximum number of sg elements that fit into a virtqueue */
+#define VIRTIO_BLK_MAX_SG_ELEMS 32768

32768 is what it says for split queues but for packed queues it says 2^15.

So 0x8000 then?

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

Quoted 9 lines
drivers/block/virtio_blk.c +1 -1
+
 static int major;
 static DEFINE_IDA(vd_index_ida);
 
@@ -728,7 +731,10 @@ static int virtblk_probe(struct virtio_device *vdev)
 	if (err || !sg_elems)
 		sg_elems = 1;
 
-	/* We need an extra sg elements at head and tail. */

s/an extra/extra/

Quoted 10 lines
drivers/block/virtio_blk.c +4 -0
+	/* Prevent integer overflows and honor max vq size */
+	sg_elems = min_t(u32, sg_elems, VIRTIO_BLK_MAX_SG_ELEMS - 2);
+
+	/* We need extra sg elements at head and tail. */
 	sg_elems += 2;
 	vdev->priv = vblk = kmalloc(sizeof(*vblk), GFP_KERNEL);
 	if (!vblk) {
Signature · 1 line
2.31.1
author reviewer
#3 lore raw
Quoted 25 lines · Michael S. Tsirkin
Quoted 24 lines · Stefan Hajnoczi

The struct virtio_blk_config seg_max value is read from the device and incremented by 2 to account for the request header and status byte descriptors added by the driver.

In preparation for supporting untrusted virtio-blk devices, protect against integer overflow and limit the value to a safe maximum.

---
v2:
* Limit to a virtio-specific value instead of using SG_MAX_SEGMENTS [Christoph]

---
 drivers/block/virtio_blk.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +0 -0
index b9fa3ef5b57c..1635d4289202 100644
@@ -21,6 +21,9 @@
 #define VQ_NAME_LEN 16
 #define MAX_DISCARD_SEGMENTS 256u
 
Quoted 7 lines

drivers/block/virtio_blk.c line 25

Quoted 2 lines
drivers/block/virtio_blk.c +2 -0
+/* The maximum number of sg elements that fit into a virtqueue */
+#define VIRTIO_BLK_MAX_SG_ELEMS 32768

32768 is what it says for split queues but for packed queues it says 2^15.

So 0x8000 then?

Yes.

2^15 = 0x8000 = 32768

Quoted 11 lines

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

Quoted 9 lines
drivers/block/virtio_blk.c +1 -1
+
 static int major;
 static DEFINE_IDA(vd_index_ida);
 
@@ -728,7 +731,10 @@ static int virtblk_probe(struct virtio_device *vdev)
 	if (err || !sg_elems)
 		sg_elems = 1;
 
-	/* We need an extra sg elements at head and tail. */

s/an extra/extra/

That line is being removed by this patch :).