stable.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] block: Add a helper to validate the block size
@ 2022-03-30 11:01 Lee Jones
  2022-03-30 11:01 ` [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate " Lee Jones
  2022-03-30 11:08 ` [PATCH 1/2] block: Add a helper to validate the " Lee Jones
  0 siblings, 2 replies; 5+ messages in thread
From: Lee Jones @ 2022-03-30 11:01 UTC (permalink / raw)
  To: lee.jones; +Cc: stable, Xie Yongji, Jens Axboe

From: Xie Yongji <xieyongji@bytedance.com>

[ Upstream commit 570b1cac477643cbf01a45fa5d018430a1fddbce ]

There are some duplicated codes to validate the block
size in block drivers. This limitation actually comes
from block layer, so this patch tries to add a new block
layer helper for that.

Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
Link: https://lore.kernel.org/r/20211026144015.188-2-xieyongji@bytedance.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
---
 include/linux/blkdev.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index d5338b9ee5502..8cc766743270f 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -59,6 +59,14 @@ struct blk_stat_callback;
  */
 #define BLKCG_MAX_POLS		5
 
+static inline int blk_validate_block_size(unsigned int bsize)
+{
+	if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
+		return -EINVAL;
+
+	return 0;
+}
+
 typedef void (rq_end_io_fn)(struct request *, blk_status_t);
 
 /*
-- 
2.35.1.1021.g381101b075-goog


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

* [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate block size
  2022-03-30 11:01 [PATCH 1/2] block: Add a helper to validate the block size Lee Jones
@ 2022-03-30 11:01 ` Lee Jones
  2022-03-30 11:48   ` Greg KH
  2022-03-30 11:08 ` [PATCH 1/2] block: Add a helper to validate the " Lee Jones
  1 sibling, 1 reply; 5+ messages in thread
From: Lee Jones @ 2022-03-30 11:01 UTC (permalink / raw)
  To: lee.jones; +Cc: stable, Xie Yongji, Michael S . Tsirkin, Jens Axboe

From: Xie Yongji <xieyongji@bytedance.com>

[ Upstream commit 57a13a5b8157d9a8606490aaa1b805bafe6c37e1 ]

The block layer can't support a block size larger than
page size yet. And a block size that's too small or
not a power of two won't work either. If a misconfigured
device presents an invalid block size in configuration space,
it will result in the kernel crash something like below:

[  506.154324] BUG: kernel NULL pointer dereference, address: 0000000000000008
[  506.160416] RIP: 0010:create_empty_buffers+0x24/0x100
[  506.174302] Call Trace:
[  506.174651]  create_page_buffers+0x4d/0x60
[  506.175207]  block_read_full_page+0x50/0x380
[  506.175798]  ? __mod_lruvec_page_state+0x60/0xa0
[  506.176412]  ? __add_to_page_cache_locked+0x1b2/0x390
[  506.177085]  ? blkdev_direct_IO+0x4a0/0x4a0
[  506.177644]  ? scan_shadow_nodes+0x30/0x30
[  506.178206]  ? lru_cache_add+0x42/0x60
[  506.178716]  do_read_cache_page+0x695/0x740
[  506.179278]  ? read_part_sector+0xe0/0xe0
[  506.179821]  read_part_sector+0x36/0xe0
[  506.180337]  adfspart_check_ICS+0x32/0x320
[  506.180890]  ? snprintf+0x45/0x70
[  506.181350]  ? read_part_sector+0xe0/0xe0
[  506.181906]  bdev_disk_changed+0x229/0x5c0
[  506.182483]  blkdev_get_whole+0x6d/0x90
[  506.183013]  blkdev_get_by_dev+0x122/0x2d0
[  506.183562]  device_add_disk+0x39e/0x3c0
[  506.184472]  virtblk_probe+0x3f8/0x79b [virtio_blk]
[  506.185461]  virtio_dev_probe+0x15e/0x1d0 [virtio]

So let's use a block layer helper to validate the block size.

Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
Link: https://lore.kernel.org/r/20211026144015.188-5-xieyongji@bytedance.com
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
---
 drivers/block/virtio_blk.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 4b3645e648ee9..2af9d7c7d45cd 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -936,9 +936,17 @@ 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) {
+		err = blk_validate_block_size(blk_size);
+		if (err) {
+			dev_err(&vdev->dev,
+				"virtio_blk: invalid block size: 0x%x\n",
+				blk_size);
+			goto out_cleanup_disk;
+		}
+
 		blk_queue_logical_block_size(q, blk_size);
-	else
+	} else
 		blk_size = queue_logical_block_size(q);
 
 	/* Use topology information if available */
-- 
2.35.1.1021.g381101b075-goog


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

* Re: [PATCH 1/2] block: Add a helper to validate the block size
  2022-03-30 11:01 [PATCH 1/2] block: Add a helper to validate the block size Lee Jones
  2022-03-30 11:01 ` [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate " Lee Jones
@ 2022-03-30 11:08 ` Lee Jones
  1 sibling, 0 replies; 5+ messages in thread
From: Lee Jones @ 2022-03-30 11:08 UTC (permalink / raw)
  To: stable, Xie Yongji, Jens Axboe

On Wed, 30 Mar 2022, Lee Jones wrote:

> From: Xie Yongji <xieyongji@bytedance.com>
> 
> [ Upstream commit 570b1cac477643cbf01a45fa5d018430a1fddbce ]
> 
> There are some duplicated codes to validate the block
> size in block drivers. This limitation actually comes
> from block layer, so this patch tries to add a new block
> layer helper for that.
> 
> Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
> Link: https://lore.kernel.org/r/20211026144015.188-2-xieyongji@bytedance.com
> Signed-off-by: Jens Axboe <axboe@kernel.dk>
> Signed-off-by: Lee Jones <lee.jones@linaro.org>
> ---
>  include/linux/blkdev.h | 8 ++++++++
>  1 file changed, 8 insertions(+)
> 
> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> index d5338b9ee5502..8cc766743270f 100644
> --- a/include/linux/blkdev.h
> +++ b/include/linux/blkdev.h
> @@ -59,6 +59,14 @@ struct blk_stat_callback;
>   */
>  #define BLKCG_MAX_POLS		5
>  
> +static inline int blk_validate_block_size(unsigned int bsize)
> +{
> +	if (bsize < 512 || bsize > PAGE_SIZE || !is_power_of_2(bsize))
> +		return -EINVAL;
> +
> +	return 0;
> +}
> +
>  typedef void (rq_end_io_fn)(struct request *, blk_status_t);
>  
>  /*

These 2 are for linux-5.4.y.

-- 
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

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

* Re: [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate block size
  2022-03-30 11:01 ` [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate " Lee Jones
@ 2022-03-30 11:48   ` Greg KH
  2022-03-30 11:58     ` Lee Jones
  0 siblings, 1 reply; 5+ messages in thread
From: Greg KH @ 2022-03-30 11:48 UTC (permalink / raw)
  To: Lee Jones; +Cc: stable, Xie Yongji, Michael S . Tsirkin, Jens Axboe

On Wed, Mar 30, 2022 at 12:01:07PM +0100, Lee Jones wrote:
> From: Xie Yongji <xieyongji@bytedance.com>
> 
> [ Upstream commit 57a13a5b8157d9a8606490aaa1b805bafe6c37e1 ]
> 
> The block layer can't support a block size larger than
> page size yet. And a block size that's too small or
> not a power of two won't work either. If a misconfigured
> device presents an invalid block size in configuration space,
> it will result in the kernel crash something like below:
> 
> [  506.154324] BUG: kernel NULL pointer dereference, address: 0000000000000008
> [  506.160416] RIP: 0010:create_empty_buffers+0x24/0x100
> [  506.174302] Call Trace:
> [  506.174651]  create_page_buffers+0x4d/0x60
> [  506.175207]  block_read_full_page+0x50/0x380
> [  506.175798]  ? __mod_lruvec_page_state+0x60/0xa0
> [  506.176412]  ? __add_to_page_cache_locked+0x1b2/0x390
> [  506.177085]  ? blkdev_direct_IO+0x4a0/0x4a0
> [  506.177644]  ? scan_shadow_nodes+0x30/0x30
> [  506.178206]  ? lru_cache_add+0x42/0x60
> [  506.178716]  do_read_cache_page+0x695/0x740
> [  506.179278]  ? read_part_sector+0xe0/0xe0
> [  506.179821]  read_part_sector+0x36/0xe0
> [  506.180337]  adfspart_check_ICS+0x32/0x320
> [  506.180890]  ? snprintf+0x45/0x70
> [  506.181350]  ? read_part_sector+0xe0/0xe0
> [  506.181906]  bdev_disk_changed+0x229/0x5c0
> [  506.182483]  blkdev_get_whole+0x6d/0x90
> [  506.183013]  blkdev_get_by_dev+0x122/0x2d0
> [  506.183562]  device_add_disk+0x39e/0x3c0
> [  506.184472]  virtblk_probe+0x3f8/0x79b [virtio_blk]
> [  506.185461]  virtio_dev_probe+0x15e/0x1d0 [virtio]
> 
> So let's use a block layer helper to validate the block size.
> 
> Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
> Acked-by: Michael S. Tsirkin <mst@redhat.com>
> Link: https://lore.kernel.org/r/20211026144015.188-5-xieyongji@bytedance.com
> Signed-off-by: Jens Axboe <axboe@kernel.dk>
> Signed-off-by: Lee Jones <lee.jones@linaro.org>
> ---
>  drivers/block/virtio_blk.c | 12 ++++++++++--
>  1 file changed, 10 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
> index 4b3645e648ee9..2af9d7c7d45cd 100644
> --- a/drivers/block/virtio_blk.c
> +++ b/drivers/block/virtio_blk.c
> @@ -936,9 +936,17 @@ 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) {
> +		err = blk_validate_block_size(blk_size);
> +		if (err) {
> +			dev_err(&vdev->dev,
> +				"virtio_blk: invalid block size: 0x%x\n",
> +				blk_size);
> +			goto out_cleanup_disk;
> +		}
> +
>  		blk_queue_logical_block_size(q, blk_size);
> -	else
> +	} else
>  		blk_size = queue_logical_block_size(q);
>  
>  	/* Use topology information if available */
> -- 
> 2.35.1.1021.g381101b075-goog
> 

You didn't build this one either :(

{sigh}

Care to start over?  It's in the 5.15 queue.  Please submit tested
patches for any/all other branches you want this in.

thanks,

greg k-h

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

* Re: [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate block size
  2022-03-30 11:48   ` Greg KH
@ 2022-03-30 11:58     ` Lee Jones
  0 siblings, 0 replies; 5+ messages in thread
From: Lee Jones @ 2022-03-30 11:58 UTC (permalink / raw)
  To: Greg KH; +Cc: stable, Xie Yongji, Michael S . Tsirkin, Jens Axboe

On Wed, 30 Mar 2022, Greg KH wrote:

> On Wed, Mar 30, 2022 at 12:01:07PM +0100, Lee Jones wrote:
> > From: Xie Yongji <xieyongji@bytedance.com>
> > 
> > [ Upstream commit 57a13a5b8157d9a8606490aaa1b805bafe6c37e1 ]
> > 
> > The block layer can't support a block size larger than
> > page size yet. And a block size that's too small or
> > not a power of two won't work either. If a misconfigured
> > device presents an invalid block size in configuration space,
> > it will result in the kernel crash something like below:
> > 
> > [  506.154324] BUG: kernel NULL pointer dereference, address: 0000000000000008
> > [  506.160416] RIP: 0010:create_empty_buffers+0x24/0x100
> > [  506.174302] Call Trace:
> > [  506.174651]  create_page_buffers+0x4d/0x60
> > [  506.175207]  block_read_full_page+0x50/0x380
> > [  506.175798]  ? __mod_lruvec_page_state+0x60/0xa0
> > [  506.176412]  ? __add_to_page_cache_locked+0x1b2/0x390
> > [  506.177085]  ? blkdev_direct_IO+0x4a0/0x4a0
> > [  506.177644]  ? scan_shadow_nodes+0x30/0x30
> > [  506.178206]  ? lru_cache_add+0x42/0x60
> > [  506.178716]  do_read_cache_page+0x695/0x740
> > [  506.179278]  ? read_part_sector+0xe0/0xe0
> > [  506.179821]  read_part_sector+0x36/0xe0
> > [  506.180337]  adfspart_check_ICS+0x32/0x320
> > [  506.180890]  ? snprintf+0x45/0x70
> > [  506.181350]  ? read_part_sector+0xe0/0xe0
> > [  506.181906]  bdev_disk_changed+0x229/0x5c0
> > [  506.182483]  blkdev_get_whole+0x6d/0x90
> > [  506.183013]  blkdev_get_by_dev+0x122/0x2d0
> > [  506.183562]  device_add_disk+0x39e/0x3c0
> > [  506.184472]  virtblk_probe+0x3f8/0x79b [virtio_blk]
> > [  506.185461]  virtio_dev_probe+0x15e/0x1d0 [virtio]
> > 
> > So let's use a block layer helper to validate the block size.
> > 
> > Signed-off-by: Xie Yongji <xieyongji@bytedance.com>
> > Acked-by: Michael S. Tsirkin <mst@redhat.com>
> > Link: https://lore.kernel.org/r/20211026144015.188-5-xieyongji@bytedance.com
> > Signed-off-by: Jens Axboe <axboe@kernel.dk>
> > Signed-off-by: Lee Jones <lee.jones@linaro.org>
> > ---
> >  drivers/block/virtio_blk.c | 12 ++++++++++--
> >  1 file changed, 10 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
> > index 4b3645e648ee9..2af9d7c7d45cd 100644
> > --- a/drivers/block/virtio_blk.c
> > +++ b/drivers/block/virtio_blk.c
> > @@ -936,9 +936,17 @@ 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) {
> > +		err = blk_validate_block_size(blk_size);
> > +		if (err) {
> > +			dev_err(&vdev->dev,
> > +				"virtio_blk: invalid block size: 0x%x\n",
> > +				blk_size);
> > +			goto out_cleanup_disk;
> > +		}
> > +
> >  		blk_queue_logical_block_size(q, blk_size);
> > -	else
> > +	} else
> >  		blk_size = queue_logical_block_size(q);
> >  
> >  	/* Use topology information if available */
> 
> You didn't build this one either :(
> 
> {sigh}
> 
> Care to start over?

Yes, I think that's a good idea!  :|

</reset>

-- 
Lee Jones [李琼斯]
Principal Technical Lead - Developer Services
Linaro.org │ Open source software for Arm SoCs
Follow Linaro: Facebook | Twitter | Blog

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

end of thread, other threads:[~2022-03-30 12:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-30 11:01 [PATCH 1/2] block: Add a helper to validate the block size Lee Jones
2022-03-30 11:01 ` [PATCH 2/2] virtio-blk: Use blk_validate_block_size() to validate " Lee Jones
2022-03-30 11:48   ` Greg KH
2022-03-30 11:58     ` Lee Jones
2022-03-30 11:08 ` [PATCH 1/2] block: Add a helper to validate the " Lee Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).