All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] virtio-blk: limit seg_max to a safe value
@ 2021-05-24 15:40 Stefan Hajnoczi
  2021-05-24 15:47 ` Christoph Hellwig
  2021-05-24 20:47 ` Michael S. Tsirkin
  0 siblings, 2 replies; 4+ messages in thread
From: Stefan Hajnoczi @ 2021-05-24 15:40 UTC (permalink / raw)
  To: linux-block
  Cc: Xie Yongji, Christoph Hellwig, Michael S. Tsirkin, jasowang,
	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.

Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
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(-)

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index b9fa3ef5b57c..1635d4289202 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -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) {
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] virtio-blk: limit seg_max to a safe value
  2021-05-24 15:40 [PATCH v2] virtio-blk: limit seg_max to a safe value Stefan Hajnoczi
@ 2021-05-24 15:47 ` Christoph Hellwig
  2021-05-24 20:47 ` Michael S. Tsirkin
  1 sibling, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2021-05-24 15:47 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: linux-block, Xie Yongji, Christoph Hellwig, Michael S. Tsirkin, jasowang

On Mon, May 24, 2021 at 04:40:20PM +0100, Stefan Hajnoczi wrote:
> 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.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] virtio-blk: limit seg_max to a safe value
  2021-05-24 15:40 [PATCH v2] virtio-blk: limit seg_max to a safe value Stefan Hajnoczi
  2021-05-24 15:47 ` Christoph Hellwig
@ 2021-05-24 20:47 ` Michael S. Tsirkin
  2021-06-03 14:59   ` Stefan Hajnoczi
  1 sibling, 1 reply; 4+ messages in thread
From: Michael S. Tsirkin @ 2021-05-24 20:47 UTC (permalink / raw)
  To: Stefan Hajnoczi; +Cc: linux-block, Xie Yongji, Christoph Hellwig, jasowang

On Mon, May 24, 2021 at 04:40:20PM +0100, Stefan Hajnoczi wrote:
> 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.
> 
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
> 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(-)
> 
> diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
> index b9fa3ef5b57c..1635d4289202 100644
> --- a/drivers/block/virtio_blk.c
> +++ b/drivers/block/virtio_blk.c
> @@ -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?

> +
>  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/

> +	/* 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) {
> -- 
> 2.31.1
> 


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v2] virtio-blk: limit seg_max to a safe value
  2021-05-24 20:47 ` Michael S. Tsirkin
@ 2021-06-03 14:59   ` Stefan Hajnoczi
  0 siblings, 0 replies; 4+ messages in thread
From: Stefan Hajnoczi @ 2021-06-03 14:59 UTC (permalink / raw)
  To: Michael S. Tsirkin; +Cc: linux-block, Xie Yongji, Christoph Hellwig, jasowang

[-- Attachment #1: Type: text/plain, Size: 1661 bytes --]

On Mon, May 24, 2021 at 04:47:37PM -0400, Michael S. Tsirkin wrote:
> On Mon, May 24, 2021 at 04:40:20PM +0100, Stefan Hajnoczi wrote:
> > 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.
> > 
> > Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> > ---
> > 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(-)
> > 
> > diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
> > index b9fa3ef5b57c..1635d4289202 100644
> > --- a/drivers/block/virtio_blk.c
> > +++ b/drivers/block/virtio_blk.c
> > @@ -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?

Yes.

2^15 = 0x8000 = 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. */
> 
> s/an extra/extra/

That line is being removed by this patch :).

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-06-03 14:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-24 15:40 [PATCH v2] virtio-blk: limit seg_max to a safe value Stefan Hajnoczi
2021-05-24 15:47 ` Christoph Hellwig
2021-05-24 20:47 ` Michael S. Tsirkin
2021-06-03 14:59   ` Stefan Hajnoczi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.