All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: virtio_scsi: Fix a NULL pointer dereference in virtscsi_rescan_hotunplug()
@ 2021-11-30 17:19 Zhou Qingyang
  2021-11-30 17:35   ` Paolo Bonzini
  0 siblings, 1 reply; 3+ messages in thread
From: Zhou Qingyang @ 2021-11-30 17:19 UTC (permalink / raw)
  To: zhou1615
  Cc: kjlu, Michael S. Tsirkin, Jason Wang, Paolo Bonzini,
	Stefan Hajnoczi, James E.J. Bottomley, Martin K. Petersen,
	Matt Lupfer, virtualization, linux-scsi, linux-kernel

In virtscsi_rescan_hotunplug(), kmalloc() is directly used in memset(),
which could lead to a NULL pointer dereference on failure of
kmalloc().

Fix this bug by adding a check of inq_result.

This bug was found by a static analyzer. The analysis employs
differential checking to identify inconsistent security operations
(e.g., checks or kfrees) between two code paths and confirms that the
inconsistent operations are not recovered in the current function or
the callers, so they constitute bugs.

Note that, as a bug found by static analysis, it can be a false
positive or hard to trigger. Multiple researchers have cross-reviewed
the bug.

Builds with CONFIG_SCSI_VIRTIO=m show no new warnings,
and our static analyzer no longer warns about this code.

Fixes: 5ff843721467 ("scsi: virtio_scsi: unplug LUNs when events missed")
Signed-off-by: Zhou Qingyang <zhou1615@umn.edu>
---
 drivers/scsi/virtio_scsi.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index 28e1d98ae102..5309f2a3a4cb 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -337,7 +337,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
 	int result, inquiry_len, inq_result_len = 256;
 	char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
-
+	if (!inq_result) {
+		pr_err("%s:no enough memory for inq_result\n",
+			__func__);
+		return;
+	}
 	shost_for_each_device(sdev, shost) {
 		inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
 
-- 
2.25.1


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

* Re: [PATCH] scsi: virtio_scsi: Fix a NULL pointer dereference in virtscsi_rescan_hotunplug()
  2021-11-30 17:19 [PATCH] scsi: virtio_scsi: Fix a NULL pointer dereference in virtscsi_rescan_hotunplug() Zhou Qingyang
@ 2021-11-30 17:35   ` Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2021-11-30 17:35 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: kjlu, Michael S. Tsirkin, Jason Wang, Stefan Hajnoczi,
	James E.J. Bottomley, Martin K. Petersen, Matt Lupfer,
	virtualization, linux-scsi, linux-kernel

On 11/30/21 18:19, Zhou Qingyang wrote:
> --- a/drivers/scsi/virtio_scsi.c
> +++ b/drivers/scsi/virtio_scsi.c
> @@ -337,7 +337,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>   	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
>   	int result, inquiry_len, inq_result_len = 256;
>   	char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
> -
> +	if (!inq_result) {
> +		pr_err("%s:no enough memory for inq_result\n",
> +			__func__);
> +		return;
> +	}
>   	shost_for_each_device(sdev, shost) {
>   		inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
>   

In practice this will never happen, since the kmalloc is very small, so 
I think it's easier to just return early without a printk.  On the other 
hand, if the out-of-memory really could happen, this should be a 
pr_err_ratelimited.

Paolo

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

* Re: [PATCH] scsi: virtio_scsi: Fix a NULL pointer dereference in virtscsi_rescan_hotunplug()
@ 2021-11-30 17:35   ` Paolo Bonzini
  0 siblings, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2021-11-30 17:35 UTC (permalink / raw)
  To: Zhou Qingyang
  Cc: linux-scsi, Martin K. Petersen, Michael S. Tsirkin, kjlu,
	linux-kernel, virtualization, Stefan Hajnoczi, Matt Lupfer,
	James E.J. Bottomley

On 11/30/21 18:19, Zhou Qingyang wrote:
> --- a/drivers/scsi/virtio_scsi.c
> +++ b/drivers/scsi/virtio_scsi.c
> @@ -337,7 +337,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>   	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
>   	int result, inquiry_len, inq_result_len = 256;
>   	char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
> -
> +	if (!inq_result) {
> +		pr_err("%s:no enough memory for inq_result\n",
> +			__func__);
> +		return;
> +	}
>   	shost_for_each_device(sdev, shost) {
>   		inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
>   

In practice this will never happen, since the kmalloc is very small, so 
I think it's easier to just return early without a printk.  On the other 
hand, if the out-of-memory really could happen, this should be a 
pr_err_ratelimited.

Paolo
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

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

end of thread, other threads:[~2021-11-30 17:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-30 17:19 [PATCH] scsi: virtio_scsi: Fix a NULL pointer dereference in virtscsi_rescan_hotunplug() Zhou Qingyang
2021-11-30 17:35 ` Paolo Bonzini
2021-11-30 17:35   ` Paolo Bonzini

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.