scsi: sr: fix oob access in get_capabilities

Message ID 1488536272-10509-1-git-send-email-wangkefeng.wang@huawei.com
State New
Headers show

Commit Message

Kefeng Wang March 3, 2017, 10:17 a.m.
'n = header_length + block_descriptor_length' could be greater than 512,
and will lead to oob access, so enlarge transfer buffer to fix it.

-- 
1.7.12.4

Comments

Martin K. Petersen March 16, 2017, 12:07 a.m. | #1
Kefeng Wang <wangkefeng.wang@huawei.com> writes:

Kefeng,

> 'n = header_length + block_descriptor_length' could be greater than 512,

> and will lead to oob access, so enlarge transfer buffer to fix it.


Can you share the output of sg_modes -p 0x2a /dev/srN for the offending
drive?

This mode page is usually much smaller than 512 bytes (typically between
32 and 128 bytes).

-- 
Martin K. Petersen	Oracle Linux Engineering
Kefeng Wang March 16, 2017, 5:21 a.m. | #2
On 2017/3/16 8:07, Martin K. Petersen wrote:
> Kefeng Wang <wangkefeng.wang@huawei.com> writes:

> 

> Kefeng,

> 

>> 'n = header_length + block_descriptor_length' could be greater than 512,

>> and will lead to oob access, so enlarge transfer buffer to fix it.

> 

> Can you share the output of sg_modes -p 0x2a /dev/srN for the offending

> drive?



root@localhost ~]# sg_modes -p 0x2a /dev/sr0
    QEMU      QEMU DVD-ROM      0.15   peripheral_type: cd/dvd [0x5]
Mode parameter header from MODE SENSE(10):
Invalid block descriptor length=512, ignore
  Mode data length=36, medium type=0x70, specific param=0x00, longlba=0
  Block descriptor length=0
>> MM capabilities and mechanical status (obsolete), page_control: current

 00     2a 12 00 00 71 60 29 00  02 c2 00 02 02 00 02 c2
 10     00 00 00 00
Unexpectedly received extra mode page responses, ignore

note: the issue was found in guestos?  maybe some bugs exist in qemu?

Kefeng

> 

> This mode page is usually much smaller than 512 bytes (typically between

> 32 and 128 bytes).

>
Kefeng Wang March 20, 2017, 6 a.m. | #3
On 2017/3/18 7:29, Martin K. Petersen wrote:
> Kefeng Wang <wangkefeng.wang@huawei.com> writes:

> 

> Kefeng,

> 

>> root@localhost ~]# sg_modes -p 0x2a /dev/sr0

>>     QEMU      QEMU DVD-ROM      0.15   peripheral_type: cd/dvd [0x5]

>> Mode parameter header from MODE SENSE(10):

>> Invalid block descriptor length=512, ignore

>>   Mode data length=36, medium type=0x70, specific param=0x00, longlba=0

>>   Block descriptor length=0

>>>> MM capabilities and mechanical status (obsolete), page_control: current

>>  00     2a 12 00 00 71 60 29 00  02 c2 00 02 02 00 02 c2

>>  10     00 00 00 00

>> Unexpectedly received extra mode page responses, ignore

> 

> That looks pretty broken.

> 

> Could you try the following patch?


The issue still exists, the patch return zero in scsi_mode_sense(), but zero means
SAM_STAT_GOOD in scsi_status_is_good(), so n will be still bigger than 512;

Thanks,
Martin K. Petersen March 20, 2017, 2:29 p.m. | #4
Kefeng Wang <wangkefeng.wang@huawei.com> writes:

Kefeng,

> The issue still exists, the patch return zero in scsi_mode_sense(), but zero means

> SAM_STAT_GOOD in scsi_status_is_good(), so n will be still bigger than 512;


OK, I checked the other users of scsi_mode_sense(). So let's keep this
fix local to sr.c for now.

How about the following?


scsi: sr: Sanity check returned mode data
    
Kefeng Wang discovered that old versions of the QEMU CD driver would
return mangled mode data causing us to walk off the end of the buffer in
an attempt to parse it. Sanity check the returned mode sense data.
    
Cc: <stable@vger.kernel.org>
Reported-by: Kefeng Wang <wangkefeng.wang@huawei.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c

index 0b29b9329b1c..a8f630213a1a 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -836,6 +836,7 @@ static void get_capabilities(struct scsi_cd *cd)
 	unsigned char *buffer;
 	struct scsi_mode_data data;
 	struct scsi_sense_hdr sshdr;
+	unsigned int ms_len = 128;
 	int rc, n;
 
 	static const char *loadmech[] =
@@ -862,10 +863,11 @@ static void get_capabilities(struct scsi_cd *cd)
 	scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);
 
 	/* ask for mode page 0x2a */
-	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,
+	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,
 			     SR_TIMEOUT, 3, &data, NULL);
 
-	if (!scsi_status_is_good(rc)) {
+	if (!scsi_status_is_good(rc) || data.length > ms_len ||
+	    data.header_length + data.block_descriptor_length > data.length) {
 		/* failed, drive doesn't have capabilities mode page */
 		cd->cdi.speed = 1;
 		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |

Kefeng Wang March 21, 2017, 2:20 a.m. | #5
On 2017/3/20 22:29, Martin K. Petersen wrote:
> Kefeng Wang <wangkefeng.wang@huawei.com> writes:

> 

> Kefeng,

> 

>> The issue still exists, the patch return zero in scsi_mode_sense(), but zero means

>> SAM_STAT_GOOD in scsi_status_is_good(), so n will be still bigger than 512;

> 

> OK, I checked the other users of scsi_mode_sense(). So let's keep this

> fix local to sr.c for now.

> 

> How about the following?

> 

> 

> scsi: sr: Sanity check returned mode data

>     

> Kefeng Wang discovered that old versions of the QEMU CD driver would

> return mangled mode data causing us to walk off the end of the buffer in

> an attempt to parse it. Sanity check the returned mode sense data.

>     

> Cc: <stable@vger.kernel.org>

> Reported-by: Kefeng Wang <wangkefeng.wang@huawei.com>

> Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>

> 

> diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c

> index 0b29b9329b1c..a8f630213a1a 100644

> --- a/drivers/scsi/sr.c

> +++ b/drivers/scsi/sr.c

> @@ -836,6 +836,7 @@ static void get_capabilities(struct scsi_cd *cd)

>  	unsigned char *buffer;

>  	struct scsi_mode_data data;

>  	struct scsi_sense_hdr sshdr;

> +	unsigned int ms_len = 128;

>  	int rc, n;

>  

>  	static const char *loadmech[] =

> @@ -862,10 +863,11 @@ static void get_capabilities(struct scsi_cd *cd)

>  	scsi_test_unit_ready(cd->device, SR_TIMEOUT, MAX_RETRIES, &sshdr);

>  

>  	/* ask for mode page 0x2a */

> -	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, 128,

> +	rc = scsi_mode_sense(cd->device, 0, 0x2a, buffer, ms_len,

>  			     SR_TIMEOUT, 3, &data, NULL);

>  


move n = data.header_length + data.block_descriptor_length; here,

> -	if (!scsi_status_is_good(rc)) {

> +	if (!scsi_status_is_good(rc) || data.length > ms_len ||

> +	    data.header_length + data.block_descriptor_length > data.length) {


n > data.length

Tested-by: Kefeng Wang <wangkefeng.wang@huawei.com>


Thanks,
Kefeng

>  		/* failed, drive doesn't have capabilities mode page */

>  		cd->cdi.speed = 1;

>  		cd->cdi.mask |= (CDC_CD_R | CDC_CD_RW | CDC_DVD_R |

> 

> 

> 

> .

>

Patch hide | download patch | download mbox

===
BUG: KASAN: slab-out-of-bounds in sr_probe+0x570/0xcc0 at addr ffff88000009020e
Read of size 1 by task kworker/u48:2/188

Signed-off-by: Kefeng Wang <wangkefeng.wang@huawei.com>
---
 drivers/scsi/sr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c
index 0b29b93..5a80aa6 100644
--- a/drivers/scsi/sr.c
+++ b/drivers/scsi/sr.c
@@ -852,7 +852,7 @@  static void get_capabilities(struct scsi_cd *cd)
 
 
 	/* allocate transfer buffer */
-	buffer = kmalloc(512, GFP_KERNEL | GFP_DMA);
+	buffer = kmalloc(1024, GFP_KERNEL | GFP_DMA);
 	if (!buffer) {
 		sr_printk(KERN_ERR, cd, "out of memory.\n");
 		return;