linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] usb: storage: sddr55: Fix a possible null-pointer dereference in sddr55_transport()
@ 2019-07-29 11:49 Jia-Ju Bai
  2019-07-29 11:51 ` Jia-Ju Bai
  2019-07-29 13:44 ` Alan Stern
  0 siblings, 2 replies; 3+ messages in thread
From: Jia-Ju Bai @ 2019-07-29 11:49 UTC (permalink / raw)
  To: stern, gregkh; +Cc: linux-usb, usb-storage, linux-kernel, Jia-Ju Bai

In sddr55_transport(), there is an if statement on line 836 to check
whether info->lba_to_pba is NULL:
    if (info->lba_to_pba == NULL || ...)

When info->lba_to_pba is NULL, it is used on line 948:
    pba = info->lba_to_pba[lba];

Thus, a possible null-pointer dereference may occur.

To fix this bug, info->lba_to_pba is checked before being used.

This bug is found by a static analysis tool STCheck written by us.

Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
---
v2:
* Avoid uninitialized access of pba.
  Thank Oliver for helpful advice.

---
 drivers/usb/storage/sddr55.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
index b8527c55335b..d23aff16091e 100644
--- a/drivers/usb/storage/sddr55.c
+++ b/drivers/usb/storage/sddr55.c
@@ -945,7 +945,7 @@ static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
 			return USB_STOR_TRANSPORT_FAILED;
 		}
 
-		pba = info->lba_to_pba[lba];
+		pba = info->lba_to_pba ? info->lba_to_pba[lba] : 0;
 
 		if (srb->cmnd[0] == WRITE_10) {
 			usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
-- 
2.17.0


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

* Re: [PATCH v2] usb: storage: sddr55: Fix a possible null-pointer dereference in sddr55_transport()
  2019-07-29 11:49 [PATCH v2] usb: storage: sddr55: Fix a possible null-pointer dereference in sddr55_transport() Jia-Ju Bai
@ 2019-07-29 11:51 ` Jia-Ju Bai
  2019-07-29 13:44 ` Alan Stern
  1 sibling, 0 replies; 3+ messages in thread
From: Jia-Ju Bai @ 2019-07-29 11:51 UTC (permalink / raw)
  To: stern, gregkh, Oliver Neukum; +Cc: linux-usb, usb-storage, linux-kernel

Sorry, I forgot to send to Oliver, so send it again.

On 2019/7/29 19:49, Jia-Ju Bai wrote:
> In sddr55_transport(), there is an if statement on line 836 to check
> whether info->lba_to_pba is NULL:
>      if (info->lba_to_pba == NULL || ...)
>
> When info->lba_to_pba is NULL, it is used on line 948:
>      pba = info->lba_to_pba[lba];
>
> Thus, a possible null-pointer dereference may occur.
>
> To fix this bug, info->lba_to_pba is checked before being used.
>
> This bug is found by a static analysis tool STCheck written by us.
>
> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
> v2:
> * Avoid uninitialized access of pba.
>    Thank Oliver for helpful advice.
>
> ---
>   drivers/usb/storage/sddr55.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
> index b8527c55335b..d23aff16091e 100644
> --- a/drivers/usb/storage/sddr55.c
> +++ b/drivers/usb/storage/sddr55.c
> @@ -945,7 +945,7 @@ static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
>   			return USB_STOR_TRANSPORT_FAILED;
>   		}
>   
> -		pba = info->lba_to_pba[lba];
> +		pba = info->lba_to_pba ? info->lba_to_pba[lba] : 0;
>   
>   		if (srb->cmnd[0] == WRITE_10) {
>   			usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",


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

* Re: [PATCH v2] usb: storage: sddr55: Fix a possible null-pointer dereference in sddr55_transport()
  2019-07-29 11:49 [PATCH v2] usb: storage: sddr55: Fix a possible null-pointer dereference in sddr55_transport() Jia-Ju Bai
  2019-07-29 11:51 ` Jia-Ju Bai
@ 2019-07-29 13:44 ` Alan Stern
  1 sibling, 0 replies; 3+ messages in thread
From: Alan Stern @ 2019-07-29 13:44 UTC (permalink / raw)
  To: Jia-Ju Bai; +Cc: gregkh, USB list, USB Storage list, Kernel development list

On Mon, 29 Jul 2019, Jia-Ju Bai wrote:

> In sddr55_transport(), there is an if statement on line 836 to check
> whether info->lba_to_pba is NULL:
>     if (info->lba_to_pba == NULL || ...)
> 
> When info->lba_to_pba is NULL, it is used on line 948:
>     pba = info->lba_to_pba[lba];
> 
> Thus, a possible null-pointer dereference may occur.
> 
> To fix this bug, info->lba_to_pba is checked before being used.
> 
> This bug is found by a static analysis tool STCheck written by us.

This is not the right way to fix the bug.

The code already contains a test on line 938:

		if (lba >= info->max_log_blks) {

If this test fails, the driver doesn't try to dereference 
info->lba_to_pba.

The problem is that info->max_log_blks may be set even though 
info->lba_to_pba is NULL, because the READ_CAPACITY case in 
sddr55_transport() doesn't check the return code from 
sddr55_read_map().  _That_ is what needs to be fixed.

Alan Stern

> Signed-off-by: Jia-Ju Bai <baijiaju1990@gmail.com>
> ---
> v2:
> * Avoid uninitialized access of pba.
>   Thank Oliver for helpful advice.
> 
> ---
>  drivers/usb/storage/sddr55.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/usb/storage/sddr55.c b/drivers/usb/storage/sddr55.c
> index b8527c55335b..d23aff16091e 100644
> --- a/drivers/usb/storage/sddr55.c
> +++ b/drivers/usb/storage/sddr55.c
> @@ -945,7 +945,7 @@ static int sddr55_transport(struct scsi_cmnd *srb, struct us_data *us)
>  			return USB_STOR_TRANSPORT_FAILED;
>  		}
>  
> -		pba = info->lba_to_pba[lba];
> +		pba = info->lba_to_pba ? info->lba_to_pba[lba] : 0;
>  
>  		if (srb->cmnd[0] == WRITE_10) {
>  			usb_stor_dbg(us, "WRITE_10: write block %04X (LBA %04X) page %01X pages %d\n",
> 


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

end of thread, other threads:[~2019-07-29 13:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-29 11:49 [PATCH v2] usb: storage: sddr55: Fix a possible null-pointer dereference in sddr55_transport() Jia-Ju Bai
2019-07-29 11:51 ` Jia-Ju Bai
2019-07-29 13:44 ` Alan Stern

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