linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand
@ 2020-12-08 13:19 Xiaohui Zhang
  2020-12-08 16:30 ` [EXTERNAL] " KY Srinivasan
  2020-12-08 17:52 ` Michael Kelley
  0 siblings, 2 replies; 4+ messages in thread
From: Xiaohui Zhang @ 2020-12-08 13:19 UTC (permalink / raw)
  To: Xiaohui Zhang, K. Y. Srinivasan, Haiyang Zhang,
	Stephen Hemminger, Wei Liu, James E.J. Bottomley,
	Martin K. Petersen, linux-hyperv, linux-scsi, linux-kernel

From: Zhang Xiaohui <ruc_zhangxiaohui@163.com>

storvsc_queuecommand() calls memcpy() without checking
the destination size may trigger a buffer overflower,
which a local user could use to cause denial of service
or the execution of arbitrary code.
Fix it by putting the length check before calling memcpy().

Signed-off-by: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
---
 drivers/scsi/storvsc_drv.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c
index 0c65fbd41..09b60a4c0 100644
--- a/drivers/scsi/storvsc_drv.c
+++ b/drivers/scsi/storvsc_drv.c
@@ -1729,6 +1729,8 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *scmnd)
 
 	vm_srb->cdb_length = scmnd->cmd_len;
 
+	if (vm_srb->cdb_length > STORVSC_MAX_CMD_LEN)
+		vm_srb->cdb_length = STORVSC_MAX_CMD_LEN;
 	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
 
 	sgl = (struct scatterlist *)scsi_sglist(scmnd);
-- 
2.17.1


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

* RE: [EXTERNAL] [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand
  2020-12-08 13:19 [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand Xiaohui Zhang
@ 2020-12-08 16:30 ` KY Srinivasan
  2020-12-08 17:52 ` Michael Kelley
  1 sibling, 0 replies; 4+ messages in thread
From: KY Srinivasan @ 2020-12-08 16:30 UTC (permalink / raw)
  To: Xiaohui Zhang, Haiyang Zhang, Stephen Hemminger, Wei Liu,
	James E.J. Bottomley, Martin K. Petersen, linux-hyperv,
	linux-scsi, linux-kernel



> -----Original Message-----
> From: Xiaohui Zhang <ruc_zhangxiaohui@163.com>
> Sent: Tuesday, December 8, 2020 5:19 AM
> To: Xiaohui Zhang <ruc_zhangxiaohui@163.com>; KY Srinivasan
> <kys@microsoft.com>; Haiyang Zhang <haiyangz@microsoft.com>; Stephen
> Hemminger <sthemmin@microsoft.com>; Wei Liu <wei.liu@kernel.org>;
> James E.J. Bottomley <jejb@linux.ibm.com>; Martin K. Petersen
> <martin.petersen@oracle.com>; linux-hyperv@vger.kernel.org; linux-
> scsi@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: [EXTERNAL] [PATCH 1/1] scsi: Fix possible buffer overflows in
> storvsc_queuecommand
> 
> From: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
> 
> storvsc_queuecommand() calls memcpy() without checking the destination size
> may trigger a buffer overflower, which a local user could use to cause denial of
> service or the execution of arbitrary code.
> Fix it by putting the length check before calling memcpy().
> 
> Signed-off-by: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
> ---
>  drivers/scsi/storvsc_drv.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c index
> 0c65fbd41..09b60a4c0 100644
> --- a/drivers/scsi/storvsc_drv.c
> +++ b/drivers/scsi/storvsc_drv.c
> @@ -1729,6 +1729,8 @@ static int storvsc_queuecommand(struct Scsi_Host
> *host, struct scsi_cmnd *scmnd)
> 
>  	vm_srb->cdb_length = scmnd->cmd_len;
> 
> +	if (vm_srb->cdb_length > STORVSC_MAX_CMD_LEN)
> +		vm_srb->cdb_length = STORVSC_MAX_CMD_LEN;
>  	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);

The data  structure is sized correctly to handle the max command length. Besides
your check is bogus - you cannot truncate the command!

K. Y
> 
>  	sgl = (struct scatterlist *)scsi_sglist(scmnd);
> --
> 2.17.1


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

* RE: [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand
  2020-12-08 13:19 [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand Xiaohui Zhang
  2020-12-08 16:30 ` [EXTERNAL] " KY Srinivasan
@ 2020-12-08 17:52 ` Michael Kelley
       [not found]   ` <56cde2c6.bb9.176451a57d7.Coremail.ruc_zhangxiaohui@163.com>
  1 sibling, 1 reply; 4+ messages in thread
From: Michael Kelley @ 2020-12-08 17:52 UTC (permalink / raw)
  To: Xiaohui Zhang, KY Srinivasan, Haiyang Zhang, Stephen Hemminger,
	Wei Liu, James E.J. Bottomley, Martin K. Petersen, linux-hyperv,
	linux-scsi, linux-kernel

From: Xiaohui Zhang <ruc_zhangxiaohui@163.com>  Sent: Tuesday, December 8, 2020 5:19 AM
> 
> From: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
> 
> storvsc_queuecommand() calls memcpy() without checking
> the destination size may trigger a buffer overflower,
> which a local user could use to cause denial of service
> or the execution of arbitrary code.
> Fix it by putting the length check before calling memcpy().
> 
> Signed-off-by: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
> ---
>  drivers/scsi/storvsc_drv.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c
> index 0c65fbd41..09b60a4c0 100644
> --- a/drivers/scsi/storvsc_drv.c
> +++ b/drivers/scsi/storvsc_drv.c
> @@ -1729,6 +1729,8 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct
> scsi_cmnd *scmnd)
> 
>  	vm_srb->cdb_length = scmnd->cmd_len;
> 
> +	if (vm_srb->cdb_length > STORVSC_MAX_CMD_LEN)
> +		vm_srb->cdb_length = STORVSC_MAX_CMD_LEN;
>  	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
> 
>  	sgl = (struct scatterlist *)scsi_sglist(scmnd);
> --
> 2.17.1

At first glance, this new test isn't necessary.  storvsc_queuecommand() gets
called from scsi_dispatch_cmd(), where just before the queuecommand function
is called, the cmd_len field is checked against the maximum command length
defined for the SCSI controller.  In the case of storvsc, that maximum command
length is STORVSC_MAX_CMD_LEN as set in storvsc_probe().  There's a comment
in scsi_dispatch_cmd() that covers this exact case.

You are correct that we need to make sure there's no buffer overflow.  Are
you seeing any other path where storvsc_queuecommand() could be called
without the cmd_len being checked?

Michael

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

* Re: [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand
       [not found]   ` <56cde2c6.bb9.176451a57d7.Coremail.ruc_zhangxiaohui@163.com>
@ 2020-12-09 13:31     ` Wei Liu
  0 siblings, 0 replies; 4+ messages in thread
From: Wei Liu @ 2020-12-09 13:31 UTC (permalink / raw)
  To: Xiaohui Zhang
  Cc: Michael Kelley, KY Srinivasan, Haiyang Zhang, Stephen Hemminger,
	Wei Liu, James E.J. Bottomley, Martin K. Petersen, linux-hyperv,
	linux-scsi, linux-kernel

On Wed, Dec 09, 2020 at 09:25:23AM +0800, Xiaohui Zhang wrote:
> 
> 
> 
> At 2020-12-09 01:52:42, "Michael Kelley" <mikelley@microsoft.com> wrote:
> >From: Xiaohui Zhang <ruc_zhangxiaohui@163.com>  Sent: Tuesday, December 8, 2020 5:19 AM
> >> 
> >> From: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
> >> 
> >> storvsc_queuecommand() calls memcpy() without checking
> >> the destination size may trigger a buffer overflower,
> >> which a local user could use to cause denial of service
> >> or the execution of arbitrary code.
> >> Fix it by putting the length check before calling memcpy().
> >> 
> >> Signed-off-by: Zhang Xiaohui <ruc_zhangxiaohui@163.com>
> >> ---
> >>  drivers/scsi/storvsc_drv.c | 2 ++
> >>  1 file changed, 2 insertions(+)
> >> 
> >> diff --git a/drivers/scsi/storvsc_drv.c b/drivers/scsi/storvsc_drv.c
> >> index 0c65fbd41..09b60a4c0 100644
> >> --- a/drivers/scsi/storvsc_drv.c
> >> +++ b/drivers/scsi/storvsc_drv.c
> >> @@ -1729,6 +1729,8 @@ static int storvsc_queuecommand(struct Scsi_Host *host, struct
> >> scsi_cmnd *scmnd)
> >> 
> >>  	vm_srb->cdb_length = scmnd->cmd_len;
> >> 
> >> +	if (vm_srb->cdb_length > STORVSC_MAX_CMD_LEN)
> >> +		vm_srb->cdb_length = STORVSC_MAX_CMD_LEN;
> >>  	memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length);
> >> 
> >>  	sgl = (struct scatterlist *)scsi_sglist(scmnd);
> >> --
> >> 2.17.1
> >
> >At first glance, this new test isn't necessary.  storvsc_queuecommand() gets
> >called from scsi_dispatch_cmd(), where just before the queuecommand function
> >is called, the cmd_len field is checked against the maximum command length
> >defined for the SCSI controller.  In the case of storvsc, that maximum command
> >length is STORVSC_MAX_CMD_LEN as set in storvsc_probe().  There's a comment
> >in scsi_dispatch_cmd() that covers this exact case.
> >
> >You are correct that we need to make sure there's no buffer overflow.  Are
> >you seeing any other path where storvsc_queuecommand() could be called
> 
> >without the cmd_len being checked?
> 
> 
> Hello, maybe storvsc_queuecommand() could be called without the cmd_len 
> being checked in scsi_send_eh_cmnd().

In that case, a better approach is to fix the SCSI layer instead of
individual drivers, right? Storvsc can't be the only one that is
affected by this issue.

Truncating the command solves the buffer overflow issue but it doesn't
make sense to issue a truncated command to the controller.

Wei.

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

end of thread, other threads:[~2020-12-09 13:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-08 13:19 [PATCH 1/1] scsi: Fix possible buffer overflows in storvsc_queuecommand Xiaohui Zhang
2020-12-08 16:30 ` [EXTERNAL] " KY Srinivasan
2020-12-08 17:52 ` Michael Kelley
     [not found]   ` <56cde2c6.bb9.176451a57d7.Coremail.ruc_zhangxiaohui@163.com>
2020-12-09 13:31     ` Wei Liu

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