ide: atapi: check logical block address and read size (CVE-2020-29443)
PATCH v2 1/1 ide: atapi: check logical block address and read size (CVE-2020-29443)
From: Prasad J Pandit <[email protected]>
While processing ATAPI cmd_read/cmd_read_cd commands, Logical Block Address (LBA) maybe invalid OR closer to the last block, leading to an OOB access issues. Add range check to avoid it.
--- hw/ide/atapi.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)
Update v2: range check lba value early in cmd_read[_cd] routines
-> https://lists.gnu.org/archive/html/qemu-devel/2020-12/msg00151.html
hw/ide/atapi.c +24 -6
@@ -322,6 +322,8 @@ static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)static void ide_atapi_cmd_read_pio (IDEState *s,int lba,int nb_sectors,int sector_size) { +assert (0 <= lba && lba < (s->nb_sectors >>2 )); + s->lba = lba; s->packet_transfer_size = nb_sectors * sector_size; s->elementary_transfer_size =0 ;
@@ -420,6 +422,8 @@ eot:static void ide_atapi_cmd_read_dma (IDEState *s,int lba,int nb_sectors,int sector_size) { +assert (0 <= lba && lba < (s->nb_sectors >>2 )); + s->lba = lba; s->packet_transfer_size = nb_sectors * sector_size; s->io_buffer_size =0 ;
@@ -973,35 +977,49 @@ static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)static void cmd_read (IDEState *s,uint8_t * buf) { -int nb_sectors, lba; +unsigned int nb_sectors, lba; + +/* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */ +uint64_t total_sectors = s->nb_sectors >>2 ;if (buf[0 ] ==GPCMD_READ_10 ) { nb_sectors =lduw_be_p (buf +7 ); }else { nb_sectors =ldl_be_p (buf +6 ); } - - lba =ldl_be_p (buf +2 );if (nb_sectors ==0 ) {ide_atapi_cmd_ok (s);return ; } + lba =ldl_be_p (buf +2 ); +if (lba >= total_sectors || lba + nb_sectors -1 >= total_sectors) { +ide_atapi_cmd_error (s,ILLEGAL_REQUEST ,ASC_LOGICAL_BLOCK_OOR ); +return ; + } +ide_atapi_cmd_read (s, lba, nb_sectors,2048 ); }static void cmd_read_cd (IDEState *s,uint8_t * buf) { -int nb_sectors, lba, transfer_request; +unsigned int nb_sectors, lba, transfer_request; + +/* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */ +uint64_t total_sectors = s->nb_sectors >>2 ; nb_sectors = (buf[6 ] <<16 ) | (buf[7 ] <<8 ) | buf[8 ]; - lba =ldl_be_p (buf +2 ); -if (nb_sectors ==0 ) {ide_atapi_cmd_ok (s);return ; } + lba =ldl_be_p (buf +2 ); +if (lba >= total_sectors || lba + nb_sectors -1 >= total_sectors) { +ide_atapi_cmd_error (s,ILLEGAL_REQUEST ,ASC_LOGICAL_BLOCK_OOR ); +return ; + } + transfer_request = buf[9 ] &0xf8 ;if (transfer_request ==0x00 ) {/* nothing */
--
2.29.2
Patchew URL: https://patchew.org/QEMU/[email protected]/
Patchew URL: https://patchew.org/QEMU/[email protected]/
Hi,
This series seems to have some coding style problems. See output below for more information:
Type: series
Message-id: [email protected]
Subject: [PATCH v2] ide: atapi: check logical block address and read size (CVE-2020-29443)
=== TEST SCRIPT BEGIN ===
#!/bin/bash
git rev-parse base > /dev/null || exit 0
git config --local diff.renamelimit 0
git config --local diff.renames True
git config --local diff.algorithm histogram./scripts/checkpatch.pl --mailback base..
=== TEST SCRIPT END ===
Updating 3c8cf5a9c21ff8782164d1def7f44bd888713384
From https://github.com/patchew-project/qemu
* [new tag] patchew/[email protected] -> patchew/[email protected]
Switched to a new branch 'test'
8f56049 ide: atapi: check logical block address and read size (CVE-2020-29443)
=== OUTPUT BEGIN ===
ERROR: space prohibited between function name and open parenthesis '(' #25: FILE: hw/ide/atapi.c:325:
+ assert (0 <= lba && lba < (s->nb_sectors >> 2));
ERROR: space prohibited between function name and open parenthesis '(' #34: FILE: hw/ide/atapi.c:425:
+ assert (0 <= lba && lba < (s->nb_sectors >> 2));
total: 2 errors, 0 warnings, 71 lines checked
Commit 8f560492c204 (ide: atapi: check logical block address and read size (CVE-2020-29443)) has style problems, please review. If any of these errors are false positives report them to the maintainer, see
CHECKPATCH in MAINTAINERS.
=== OUTPUT END ===
Test command exited with code: 1
The full log is available at
http://patchew.org/logs/[email protected]/testing.checkpatch/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]
Quoted 10 lines · P J P
From: Prasad J Pandit <[email protected]>
While processing ATAPI cmd_read/cmd_read_cd commands, Logical Block Address (LBA) maybe invalid OR closer to the last block, leading to an OOB access issues. Add range check to avoid it.
Fixes CVE-2020-29443Reported-by Wenxiang Qian <[email protected]>Fix-suggested-by Paolo Bonzini <[email protected]>Signed-off-by Prasad J Pandit <[email protected]>
Thank you! This looks great.
With the small spacing fix suggested by checkpatch,
You may add a small patch on top to clamp s->nb_sectors at (uint64_t)INT_MAX << 2, just to be super safe.
Paolo
Quoted 88 lines
--- hw/ide/atapi.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)Update v2: range check lba value early in cmd_read[_cd] routines
-> https://lists.gnu.org/archive/html/qemu-devel/2020-12/msg00151.htmlhw/ide/atapi.c +24
-7@@ -322,6 +322,8 @@ static void ide_atapi_cmd_reply(IDEState *s, int size, int max_size)static void ide_atapi_cmd_read_pio (IDEState *s,int lba,int nb_sectors,int sector_size) { +assert (0 <= lba && lba < (s->nb_sectors >>2 )); + s->lba = lba; s->packet_transfer_size = nb_sectors * sector_size; s->elementary_transfer_size =0 ;@@ -420,6 +422,8 @@ eot:static void ide_atapi_cmd_read_dma (IDEState *s,int lba,int nb_sectors,int sector_size) { +assert (0 <= lba && lba < (s->nb_sectors >>2 )); + s->lba = lba; s->packet_transfer_size = nb_sectors * sector_size; s->io_buffer_size =0 ;@@ -973,35 +977,49 @@ static void cmd_prevent_allow_medium_removal(IDEState *s, uint8_t* buf)static void cmd_read (IDEState *s,uint8_t * buf) { -int nb_sectors, lba; +unsigned int nb_sectors, lba; + +/* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */ +uint64_t total_sectors = s->nb_sectors >>2 ;if (buf[0 ] ==GPCMD_READ_10 ) { nb_sectors =lduw_be_p (buf +7 ); }else { nb_sectors =ldl_be_p (buf +6 ); } - - lba =ldl_be_p (buf +2 );if (nb_sectors ==0 ) {ide_atapi_cmd_ok (s);return ; } + lba =ldl_be_p (buf +2 ); +if (lba >= total_sectors || lba + nb_sectors -1 >= total_sectors) { +ide_atapi_cmd_error (s,ILLEGAL_REQUEST ,ASC_LOGICAL_BLOCK_OOR ); +return ; + } +ide_atapi_cmd_read (s, lba, nb_sectors,2048 ); }static void cmd_read_cd (IDEState *s,uint8_t * buf) { -int nb_sectors, lba, transfer_request; +unsigned int nb_sectors, lba, transfer_request; + +/* Total logical sectors of ATAPI_SECTOR_SIZE(=2048) bytes */ +uint64_t total_sectors = s->nb_sectors >>2 ; nb_sectors = (buf[6 ] <<16 ) | (buf[7 ] <<8 ) | buf[8 ]; - lba =ldl_be_p (buf +2 ); -if (nb_sectors ==0 ) {ide_atapi_cmd_ok (s);return ; } + lba =ldl_be_p (buf +2 ); +if (lba >= total_sectors || lba + nb_sectors -1 >= total_sectors) { +ide_atapi_cmd_error (s,ILLEGAL_REQUEST ,ASC_LOGICAL_BLOCK_OOR ); +return ; + } + transfer_request = buf[9 ] &0xf8 ;if (transfer_request ==0x00 ) {/* nothing */ --2.29.2
+-- On Mon, 18 Jan 2021, Paolo Bonzini wrote --+ | Thank you! This looks great. | With the small spacing fix suggested by checkpatch, | Reviewed-by: Paolo Bonzini <[email protected]>
Thank you. Will send patch v3 with space typo fix.
| You may add a small patch on top to clamp s->nb_sectors at (uint64_t)INT_MAX | << 2, just to be super safe.
To confirm:
* (uint64_t)INT_MAX << 2 is => 8589934588 ~= 8.5G sectors ?
Media size would be:
8.5G * 512B(sector) => ~4TB
8.5G * 4096B(sector) => ~32TB* We are limiting IDE media size to ~4TB/~32TB ?
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D
Quoted 8 lines · P J P
To confirm:
* (uint64_t)INT_MAX << 2 is => 8589934588 ~= 8.5G sectors ?
Media size would be: 8.5G * 512B(sector) => ~4TB 8.5G * 4096B(sector) => ~32TB* We are limiting IDE media size to ~4TB/~32TB ?
s->nb_sectors is in units of 512B, so the limit would be 4TB. The purpose is to limit the lba and nb_sectors arguments (which are in 2048B units) of ide_atapi_cmd_read_{dma,pio} to INT_MAX.
Paolo
+-- On Mon, 18 Jan 2021, Paolo Bonzini wrote --+
| s->nb_sectors is in units of 512B, so the limit would be 4TB. The purpose
| is to limit the lba and nb_sectors arguments (which are in 2048B units) of
| ide_atapi_cmd_read_{dma,pio} to INT_MAX.* If it's for IDE_CD type, does the patch below look okay?
===
hw/ide/core.c +0 -0
@@ -1169,7 +1169,7 @@ static void ide_cd_change_cb(void *opaque, bool load,
Error **errp)
s->tray_open = !load;
blk_get_geometry(s->blk, &nb_sectors);- s->nb_sectors = nb_sectors;
+ s->nb_sectors = nb_sectors & (uint64_t)INT_MAX << 2;
/*
* First indicate to the guest that a CD has been removed. That's
@@ -2530,6 +2530,7 @@ int ide_init_drive(IDEState *s, BlockBackend *blk, IDEDriveKind kind,
s->smart_errors = 0;
s->smart_selftest_count = 0;
if (kind == IDE_CD) {
+ s->nb_sectors &= (uint64_t)INT_MAX << 2;
blk_set_dev_ops(blk, &ide_cd_block_ops, s);
blk_set_guest_block_size(blk, 2048);===
* Isn't 4TB limit more for IDE_CD type? Maybe UINT32_MAX?
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D
Quoted 6 lines · P J P
+-- On Mon, 18 Jan 2021, Paolo Bonzini wrote --+ | s->nb_sectors is in units of 512B, so the limit would be 4TB. The purpose | is to limit the lba and nb_sectors arguments (which are in 2048B units) of | ide_atapi_cmd_read_{dma,pio} to INT_MAX.* If it's for
IDE_CDtype, does the patch below look okay?
Not an &, but rather a MIN.
There is also ide_resize_cb, so perhaps a new function ide_set_nb_sectors in hw/ide/core.c would be better.
Quoted 25 lines
===
hw/ide/core.c +1
-1@@ -1169,7 +1169,7 @@ static void ide_cd_change_cb(void *opaque, bool load, Error **errp) s->tray_open = !load;blk_get_geometry (s->blk, &nb_sectors); - s->nb_sectors = nb_sectors; + s->nb_sectors = nb_sectors & (uint64_t )INT_MAX <<2 ;/* * First indicate to the guest that a CD has been removed. That's
hw/ide/core.c +1
-0@@ -2530,6 +2530,7 @@ int ide_init_drive(IDEState *s, BlockBackend *blk,IDEDriveKind kind, s->smart_errors = 0; s->smart_selftest_count =0 ;if (kind ==IDE_CD ) { + s->nb_sectors &= (uint64_t )INT_MAX <<2 ;blk_set_dev_ops (blk, &ide_cd_block_ops, s);blk_set_guest_block_size (blk,2048 );===
Quoted 1 line
* Isn't 4TB limit more for
IDE_CDtype? MaybeUINT32_MAX?
Yes it's a lot more than the practical limit but it doesn't hurt either to have INT_MAX << 2. The point is to have a limit that makes sense as far as the atapi.c code is concerned, i.e. to ensure the size in 2048-byte sectors fits an "int".
Paolo
+-- On Mon, 18 Jan 2021, Paolo Bonzini wrote --+
| On 18/01/21 13:29, P J P wrote:
| > + s->nb_sectors = nb_sectors & (uint64_t)INT_MAX << 2;
| > if (kind == IDE_CD) {
| > + s->nb_sectors &= (uint64_t)INT_MAX << 2;
|
| Not an &, but rather a MIN.
|
| There is also ide_resize_cb, so perhaps a new function ide_set_nb_sectors in
| hw/ide/core.c would be better.
|
| ... it doesn't hurt either to have INT_MAX << 2.Okay, will do.
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D
Quoted 9 lines · P J P
From: Prasad J Pandit <[email protected]>
While processing ATAPI cmd_read/cmd_read_cd commands, Logical Block Address (LBA) maybe invalid OR closer to the last block, leading to an OOB access issues. Add range check to avoid it.
Fixes CVE-2020-29443Reported-by Wenxiang Qian <[email protected]>Fix-suggested-by Paolo Bonzini <[email protected]>
"Suggested-by"
Quoted 4 lines
Signed-off-by Prasad J Pandit <[email protected]>--- hw/ide/atapi.c | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-)