linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in  virtscsi_rescan_hotunplug
@ 2023-02-02  6:41 Zheng Wang
  2023-02-02  6:43 ` Zheng Hacker
  2023-02-13 12:09 ` Michael S. Tsirkin
  0 siblings, 2 replies; 6+ messages in thread
From: Zheng Wang @ 2023-02-02  6:41 UTC (permalink / raw)
  To: mst
  Cc: hackerzheng666, jasowang, pbonzini, stefanha, jejb,
	martin.petersen, virtualization, linux-scsi, linux-kernel,
	security, alex000young, Zheng Wang

There is no check about the return value of kmalloc in
virtscsi_rescan_hotunplug. Add the check to avoid use
of null pointer 'inq_result' in case of the failure
of kmalloc.

Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
---
v2:
- add kfree to avoid memory leak
---
 drivers/scsi/virtio_scsi.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
index d07d24c06b54..a66d8815d738 100644
--- a/drivers/scsi/virtio_scsi.c
+++ b/drivers/scsi/virtio_scsi.c
@@ -330,7 +330,7 @@ static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
 	scsi_device_put(sdev);
 }
 
-static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
+static int virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
 {
 	struct scsi_device *sdev;
 	struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
@@ -338,6 +338,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
 	int result, inquiry_len, inq_result_len = 256;
 	char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
 
+	if (!inq_result) {
+		kfree(inq_result);
+		return -ENOMEM;
+	}
+
 	shost_for_each_device(sdev, shost) {
 		inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
 
@@ -366,6 +371,7 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
 	}
 
 	kfree(inq_result);
+	return 0;
 }
 
 static void virtscsi_handle_event(struct work_struct *work)
@@ -374,12 +380,15 @@ static void virtscsi_handle_event(struct work_struct *work)
 		container_of(work, struct virtio_scsi_event_node, work);
 	struct virtio_scsi *vscsi = event_node->vscsi;
 	struct virtio_scsi_event *event = &event_node->event;
+	int ret = 0;
 
 	if (event->event &
 	    cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
 		event->event &= ~cpu_to_virtio32(vscsi->vdev,
 						   VIRTIO_SCSI_T_EVENTS_MISSED);
-		virtscsi_rescan_hotunplug(vscsi);
+		ret = virtscsi_rescan_hotunplug(vscsi);
+		if (ret)
+			return;
 		scsi_scan_host(virtio_scsi_host(vscsi->vdev));
 	}
 
-- 
2.25.1


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

* Re: [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in virtscsi_rescan_hotunplug
  2023-02-02  6:41 [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in virtscsi_rescan_hotunplug Zheng Wang
@ 2023-02-02  6:43 ` Zheng Hacker
  2023-02-13 12:09 ` Michael S. Tsirkin
  1 sibling, 0 replies; 6+ messages in thread
From: Zheng Hacker @ 2023-02-02  6:43 UTC (permalink / raw)
  To: Zheng Wang
  Cc: mst, jasowang, pbonzini, stefanha, jejb, martin.petersen,
	virtualization, linux-scsi, linux-kernel, security, alex000young

After writing the patch, I found there is an old patch about this issue.
The link is : https://lore.kernel.org/all/ecb2f3c6-af8c-dd43-1dcf-0b5e8a9d8848@redhat.com/

Best regards,
Zheng Wang

Zheng Wang <zyytlz.wz@163.com> 于2023年2月2日周四 14:42写道:
>
> There is no check about the return value of kmalloc in
> virtscsi_rescan_hotunplug. Add the check to avoid use
> of null pointer 'inq_result' in case of the failure
> of kmalloc.
>
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---
> v2:
> - add kfree to avoid memory leak
> ---
>  drivers/scsi/virtio_scsi.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
> index d07d24c06b54..a66d8815d738 100644
> --- a/drivers/scsi/virtio_scsi.c
> +++ b/drivers/scsi/virtio_scsi.c
> @@ -330,7 +330,7 @@ static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
>         scsi_device_put(sdev);
>  }
>
> -static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
> +static int virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>  {
>         struct scsi_device *sdev;
>         struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
> @@ -338,6 +338,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>         int result, inquiry_len, inq_result_len = 256;
>         char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
>
> +       if (!inq_result) {
> +               kfree(inq_result);
> +               return -ENOMEM;
> +       }
> +
>         shost_for_each_device(sdev, shost) {
>                 inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
>
> @@ -366,6 +371,7 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>         }
>
>         kfree(inq_result);
> +       return 0;
>  }
>
>  static void virtscsi_handle_event(struct work_struct *work)
> @@ -374,12 +380,15 @@ static void virtscsi_handle_event(struct work_struct *work)
>                 container_of(work, struct virtio_scsi_event_node, work);
>         struct virtio_scsi *vscsi = event_node->vscsi;
>         struct virtio_scsi_event *event = &event_node->event;
> +       int ret = 0;
>
>         if (event->event &
>             cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
>                 event->event &= ~cpu_to_virtio32(vscsi->vdev,
>                                                    VIRTIO_SCSI_T_EVENTS_MISSED);
> -               virtscsi_rescan_hotunplug(vscsi);
> +               ret = virtscsi_rescan_hotunplug(vscsi);
> +               if (ret)
> +                       return;
>                 scsi_scan_host(virtio_scsi_host(vscsi->vdev));
>         }
>
> --
> 2.25.1
>

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

* Re: [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in  virtscsi_rescan_hotunplug
  2023-02-02  6:41 [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in virtscsi_rescan_hotunplug Zheng Wang
  2023-02-02  6:43 ` Zheng Hacker
@ 2023-02-13 12:09 ` Michael S. Tsirkin
  2023-02-14  2:36   ` Zheng Hacker
  2023-02-14 16:41   ` Stefan Hajnoczi
  1 sibling, 2 replies; 6+ messages in thread
From: Michael S. Tsirkin @ 2023-02-13 12:09 UTC (permalink / raw)
  To: Zheng Wang
  Cc: hackerzheng666, jasowang, pbonzini, stefanha, jejb,
	martin.petersen, virtualization, linux-scsi, linux-kernel,
	security, alex000young

On Thu, Feb 02, 2023 at 02:41:24PM +0800, Zheng Wang wrote:
> There is no check about the return value of kmalloc in
> virtscsi_rescan_hotunplug. Add the check to avoid use
> of null pointer 'inq_result' in case of the failure
> of kmalloc.
> 
> Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> ---

I fixed a typo in subject and tweaked the patch a bit

> v2:
> - add kfree to avoid memory leak
> ---
>  drivers/scsi/virtio_scsi.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
> index d07d24c06b54..a66d8815d738 100644
> --- a/drivers/scsi/virtio_scsi.c
> +++ b/drivers/scsi/virtio_scsi.c
> @@ -330,7 +330,7 @@ static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
>  	scsi_device_put(sdev);
>  }
>  
> -static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
> +static int virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>  {
>  	struct scsi_device *sdev;
>  	struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
> @@ -338,6 +338,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>  	int result, inquiry_len, inq_result_len = 256;
>  	char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
>  
> +	if (!inq_result) {
> +		kfree(inq_result);
> +		return -ENOMEM;
> +	}
> +
>  	shost_for_each_device(sdev, shost) {
>  		inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
>  
> @@ -366,6 +371,7 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
>  	}
>  
>  	kfree(inq_result);
> +	return 0;
>  }
>  
>  static void virtscsi_handle_event(struct work_struct *work)
> @@ -374,12 +380,15 @@ static void virtscsi_handle_event(struct work_struct *work)
>  		container_of(work, struct virtio_scsi_event_node, work);
>  	struct virtio_scsi *vscsi = event_node->vscsi;
>  	struct virtio_scsi_event *event = &event_node->event;
> +	int ret = 0;
>

dropped = 0 here
  
>  	if (event->event &
>  	    cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {

and moved declaration here.

>  		event->event &= ~cpu_to_virtio32(vscsi->vdev,
>  						   VIRTIO_SCSI_T_EVENTS_MISSED);
> -		virtscsi_rescan_hotunplug(vscsi);
> +		ret = virtscsi_rescan_hotunplug(vscsi);
> +		if (ret)
> +			return;
>  		scsi_scan_host(virtio_scsi_host(vscsi->vdev));
>  	}
>  
> -- 
> 2.25.1
> 
> 


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

* Re: [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in virtscsi_rescan_hotunplug
  2023-02-13 12:09 ` Michael S. Tsirkin
@ 2023-02-14  2:36   ` Zheng Hacker
  2023-02-14 16:41   ` Stefan Hajnoczi
  1 sibling, 0 replies; 6+ messages in thread
From: Zheng Hacker @ 2023-02-14  2:36 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: Zheng Wang, jasowang, pbonzini, stefanha, jejb, martin.petersen,
	virtualization, linux-scsi, linux-kernel, security, alex000young

Michael S. Tsirkin <mst@redhat.com> 于2023年2月13日周一 20:09写道:
>
> On Thu, Feb 02, 2023 at 02:41:24PM +0800, Zheng Wang wrote:
> > There is no check about the return value of kmalloc in
> > virtscsi_rescan_hotunplug. Add the check to avoid use
> > of null pointer 'inq_result' in case of the failure
> > of kmalloc.
> >
> > Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> > ---
>
> I fixed a typo in subject and tweaked the patch a bit

Thanks for your kindness ^^

>
> > v2:
> > - add kfree to avoid memory leak
> > ---
> >  drivers/scsi/virtio_scsi.c | 13 +++++++++++--
> >  1 file changed, 11 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/scsi/virtio_scsi.c b/drivers/scsi/virtio_scsi.c
> > index d07d24c06b54..a66d8815d738 100644
> > --- a/drivers/scsi/virtio_scsi.c
> > +++ b/drivers/scsi/virtio_scsi.c
> > @@ -330,7 +330,7 @@ static void virtscsi_handle_param_change(struct virtio_scsi *vscsi,
> >       scsi_device_put(sdev);
> >  }
> >
> > -static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
> > +static int virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
> >  {
> >       struct scsi_device *sdev;
> >       struct Scsi_Host *shost = virtio_scsi_host(vscsi->vdev);
> > @@ -338,6 +338,11 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
> >       int result, inquiry_len, inq_result_len = 256;
> >       char *inq_result = kmalloc(inq_result_len, GFP_KERNEL);
> >
> > +     if (!inq_result) {
> > +             kfree(inq_result);
> > +             return -ENOMEM;
> > +     }
> > +
> >       shost_for_each_device(sdev, shost) {
> >               inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
> >
> > @@ -366,6 +371,7 @@ static void virtscsi_rescan_hotunplug(struct virtio_scsi *vscsi)
> >       }
> >
> >       kfree(inq_result);
> > +     return 0;
> >  }
> >
> >  static void virtscsi_handle_event(struct work_struct *work)
> > @@ -374,12 +380,15 @@ static void virtscsi_handle_event(struct work_struct *work)
> >               container_of(work, struct virtio_scsi_event_node, work);
> >       struct virtio_scsi *vscsi = event_node->vscsi;
> >       struct virtio_scsi_event *event = &event_node->event;
> > +     int ret = 0;
> >
>
> dropped = 0 here

Get it.

>
> >       if (event->event &
> >           cpu_to_virtio32(vscsi->vdev, VIRTIO_SCSI_T_EVENTS_MISSED)) {
>
> and moved declaration here.

Will do in the next version of patch.
>
> >               event->event &= ~cpu_to_virtio32(vscsi->vdev,
> >                                                  VIRTIO_SCSI_T_EVENTS_MISSED);
> > -             virtscsi_rescan_hotunplug(vscsi);
> > +             ret = virtscsi_rescan_hotunplug(vscsi);
> > +             if (ret)
> > +                     return;
> >               scsi_scan_host(virtio_scsi_host(vscsi->vdev));
> >       }
> >
> > --
> > 2.25.1
> >
> >
>

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

* Re: [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in  virtscsi_rescan_hotunplug
  2023-02-13 12:09 ` Michael S. Tsirkin
  2023-02-14  2:36   ` Zheng Hacker
@ 2023-02-14 16:41   ` Stefan Hajnoczi
  2023-02-17  9:12     ` Zheng Hacker
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Hajnoczi @ 2023-02-14 16:41 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: Zheng Wang, hackerzheng666, jasowang, pbonzini, jejb,
	martin.petersen, virtualization, linux-scsi, linux-kernel,
	security, alex000young

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

On Mon, Feb 13, 2023 at 07:09:50AM -0500, Michael S. Tsirkin wrote:
> On Thu, Feb 02, 2023 at 02:41:24PM +0800, Zheng Wang wrote:
> > There is no check about the return value of kmalloc in
> > virtscsi_rescan_hotunplug. Add the check to avoid use
> > of null pointer 'inq_result' in case of the failure
> > of kmalloc.
> > 
> > Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> > ---
> 
> I fixed a typo in subject and tweaked the patch a bit

Thanks for picking this patch up.
https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/commit/?h=linux-next&id=4a8fc6e3ac68ce4c355d1f4473ef1d8468b23bdc

Stefan

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

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

* Re: [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in virtscsi_rescan_hotunplug
  2023-02-14 16:41   ` Stefan Hajnoczi
@ 2023-02-17  9:12     ` Zheng Hacker
  0 siblings, 0 replies; 6+ messages in thread
From: Zheng Hacker @ 2023-02-17  9:12 UTC (permalink / raw)
  To: Stefan Hajnoczi
  Cc: Michael S. Tsirkin, Zheng Wang, jasowang, pbonzini, jejb,
	martin.petersen, virtualization, linux-scsi, linux-kernel,
	security, alex000young

Get it ^^

Thanks,
Zheng

Stefan Hajnoczi <stefanha@redhat.com> 于2023年2月15日周三 00:42写道:
>
> On Mon, Feb 13, 2023 at 07:09:50AM -0500, Michael S. Tsirkin wrote:
> > On Thu, Feb 02, 2023 at 02:41:24PM +0800, Zheng Wang wrote:
> > > There is no check about the return value of kmalloc in
> > > virtscsi_rescan_hotunplug. Add the check to avoid use
> > > of null pointer 'inq_result' in case of the failure
> > > of kmalloc.
> > >
> > > Signed-off-by: Zheng Wang <zyytlz.wz@163.com>
> > > ---
> >
> > I fixed a typo in subject and tweaked the patch a bit
>
> Thanks for picking this patch up.
> https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git/commit/?h=linux-next&id=4a8fc6e3ac68ce4c355d1f4473ef1d8468b23bdc
>
> Stefan

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

end of thread, other threads:[~2023-02-17  9:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-02  6:41 [PATCH v2] scsi: virtio_scsi: Fix poential NULL pointer dereference in virtscsi_rescan_hotunplug Zheng Wang
2023-02-02  6:43 ` Zheng Hacker
2023-02-13 12:09 ` Michael S. Tsirkin
2023-02-14  2:36   ` Zheng Hacker
2023-02-14 16:41   ` Stefan Hajnoczi
2023-02-17  9:12     ` Zheng Hacker

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).