hw/block/onenand: fix out-of-bounds read
cover hw/block/onenand: fix out-of-bounds read
An off-by-one error in a switch case in onenand_read() allowed a misbehaving guest to read off the end of a block of memory. This bug was revealed now that QEMU supports execution from device memory: if started with no guest image loaded, "qemu-system-arm -M n800" will now try to execute by reading from this device's registers until it gets to the offset triggering the segfault. Previously it would have run into the "can't execute from device memory" assert first.
As a followup, patch 2 fixes some reporting so that we don't trigger a hw_error() for reads of unimplemented registers. With the two patches, execution now successfully results in QEMU emulating the guest being off in the weeds...
NB: the onenand device is used only by the "n800" and "n810" machines, which are usable only with TCG, not KVM, so the segfault is not a security issue.
thanks
-- PMM
Peter Maydell (2):
hw/block/onenand: Fix off-by-one error allowing out-of-bounds read hw/block/onenand: use qemu_log_mask() for reporting
hw/block/onenand.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
Signature · 1 line
2.19.1
PATCH 1/2 hw/block/onenand: Fix off-by-one error allowing out-of-bounds read
An off-by-one error in a switch case in onenand_read() allowed a misbehaving guest to read off the end of a block of memory.
NB: the onenand device is used only by the "n800" and "n810" machines, which are usable only with TCG, not KVM, so this is not a security issue.
---
I tweaked RTH's suggested fix to use an 0xbffe offset so we don't overrun on an access to 0xbfff either.
hw/block/onenand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/block/onenand.c +1 -1
@@ -608,7 +608,7 @@ static uint64_t onenand_read(void *opaque, hwaddr addr,int offset = addr >> s->shift;switch (offset) { - case 0x0000 ... 0xc000: + case 0x0000 ... 0xbffe:return lduw_le_p (s->boot[0 ] + addr);case 0xf000 :/* Manufacturer ID */
Signature · 1 line
2.19.1
Re: [Qemu-arm] [Qemu-devel] [PATCH for-3.1 1/2] hw/block/onenand: Fix off-by-one error allowing out-of-bounds read
Quoted 7 lines · Peter Maydell
An off-by-one error in a switch case in
onenand_read()allowed a misbehaving guest to read off the end of a block of memory.NB: the onenand device is used only by the "n800" and "n810" machines, which are usable only with TCG, not KVM, so this is not a security issue.
Quoted 6 lines
Reported-by Thomas Huth <[email protected]>Suggested-by Richard Henderson <[email protected]>Signed-off-by Peter Maydell <[email protected]>---
I tweaked RTH's suggested fix to use an0xbffeoffset so we don't overrun on an access to0xbfffeither.
Correct.
Quoted 10 lines
hw/block/onenand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/block/onenand.c +0
-0
hw/block/onenand.c static uint64_t onenand_read(void *opaque, hwaddr addr, · line 614
Quoted 8 lines
hw/block/onenand.c +1
-1@@ -608,7 +608,7 @@ static uint64_t onenand_read(void *opaque, hwaddr addr,int offset = addr >> s->shift;switch (offset) { - case 0x0000 ... 0xc000: + case 0x0000 ... 0xbffe:return lduw_le_p (s->boot[0 ] + addr);case 0xf000 :/* Manufacturer ID */
Re: [Qemu-arm] [PATCH for-3.1 1/2] hw/block/onenand: Fix off-by-one error allowing out-of-bounds read
In reply to Peter MaydellQuoted 10 lines · Peter Maydell
An off-by-one error in a switch case in
onenand_read()allowed a misbehaving guest to read off the end of a block of memory.NB: the onenand device is used only by the "n800" and "n810" machines, which are usable only with TCG, not KVM, so this is not a security issue.
Reported-by Thomas Huth <[email protected]>Suggested-by Richard Henderson <[email protected]>Signed-off-by Peter Maydell <[email protected]>
Quoted 6 lines
---
I tweaked RTH's suggested fix to use an0xbffeoffset so we don't overrun on an access to0xbfffeither.hw/block/onenand.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
r~
PATCH 2/2 hw/block/onenand: use qemu_log_mask() for reporting
In reply to Peter MaydellUpdate the onenand device to use qemu_log_mask() for reporting guest errors and unimplemented features, rather than plain fprintf() and hw_error().
(We leave the hw_error() in onenand_reset(), as that is triggered by a failure to read the underlying block device for the bootRAM, not by guest action.)
--- hw/block/onenand.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
hw/block/onenand.c +13 -9
@@ -28,6 +28,7 @@#include "exec/memory.h" #include "hw/sysbus.h" #include "qemu/error-report.h" +#include "qemu/log.h" /* 11 for 2kB-page OneNAND ("2nd generation") and 10 for 1kB-page chips */ #define PAGE_SHIFT 11
@@ -594,8 +595,8 @@ static void onenand_command(OneNANDState *s)default : s->status |=ONEN_ERR_CMD ; s->intstatus |=ONEN_INT ; -fprintf (stderr,"%s: unknown OneNAND command %x\n" , - __func__, s->command); +qemu_log_mask (LOG_GUEST_ERROR ,"unknown OneNAND command %x\n" , + s->command); }onenand_intr_update (s);
@@ -657,12 +658,13 @@ static uint64_t onenand_read(void *opaque, hwaddr addr,case 0xff02 :/* ECC Result of spare area data */ case 0xff03 :/* ECC Result of main area data */ case 0xff04 :/* ECC Result of spare area data */ -hw_error ("%s: implement ECC\n" , __func__); +qemu_log_mask (LOG_UNIMP , +"onenand: ECC result registers unimplemented\n" );return 0x0000 ; } -fprintf (stderr,"%s: unknown OneNAND register %x\n" , - __func__, offset); +qemu_log_mask (LOG_GUEST_ERROR ,"read of unknown OneNAND register 0x%x\n" , + offset);return 0 ; }
@@ -706,8 +708,9 @@ static void onenand_write(void *opaque, hwaddr addr,break ;default : -fprintf (stderr,"%s: unknown OneNAND boot command %" PRIx64"\n" , - __func__, value); +qemu_log_mask (LOG_GUEST_ERROR , +"unknown OneNAND boot command %" PRIx64"\n" , + value); }break ;
@@ -757,8 +760,9 @@ static void onenand_write(void *opaque, hwaddr addr,break ;default : -fprintf (stderr,"%s: unknown OneNAND register %x\n" , - __func__, offset); +qemu_log_mask (LOG_GUEST_ERROR , +"write to unknown OneNAND register 0x%x\n" , + offset); } }
Signature · 1 line
2.19.1
Re: [Qemu-arm] [Qemu-devel] [PATCH for-3.1 2/2] hw/block/onenand: use qemu_log_mask() for reporting
Quoted 9 lines · Peter Maydell
Update the onenand device to use
qemu_log_mask()for reporting guest errors and unimplemented features, rather than plainfprintf()andhw_error().(We leave the
hw_error()inonenand_reset(), as that is triggered by a failure to read the underlying block device for the bootRAM, not by guest action.)Signed-off-by Peter Maydell <[email protected]>
Quoted 69 lines
--- hw/block/onenand.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)hw/block/onenand.c +13
-9@@ -28,6 +28,7 @@#include "exec/memory.h" #include "hw/sysbus.h" #include "qemu/error-report.h" +#include "qemu/log.h" /* 11 for 2kB-page OneNAND ("2nd generation") and 10 for 1kB-page chips */ #define PAGE_SHIFT 11@@ -594,8 +595,8 @@ static void onenand_command(OneNANDState *s)default : s->status |=ONEN_ERR_CMD ; s->intstatus |=ONEN_INT ; -fprintf (stderr,"%s: unknown OneNAND command %x\n" , - __func__, s->command); +qemu_log_mask (LOG_GUEST_ERROR ,"unknown OneNAND command %x\n" , + s->command); }onenand_intr_update (s);@@ -657,12 +658,13 @@ static uint64_t onenand_read(void *opaque, hwaddr addr,case 0xff02 :/* ECC Result of spare area data */ case 0xff03 :/* ECC Result of main area data */ case 0xff04 :/* ECC Result of spare area data */ -hw_error ("%s: implement ECC\n" , __func__); +qemu_log_mask (LOG_UNIMP , +"onenand: ECC result registers unimplemented\n" );return 0x0000 ; } -fprintf (stderr,"%s: unknown OneNAND register %x\n" , - __func__, offset); +qemu_log_mask (LOG_GUEST_ERROR ,"read of unknown OneNAND register 0x%x\n" , + offset);return 0 ; }@@ -706,8 +708,9 @@ static void onenand_write(void *opaque, hwaddr addr,break ;default : -fprintf (stderr,"%s: unknown OneNAND boot command %" PRIx64"\n" , - __func__, value); +qemu_log_mask (LOG_GUEST_ERROR , +"unknown OneNAND boot command %" PRIx64"\n" , + value); }break ;@@ -757,8 +760,9 @@ static void onenand_write(void *opaque, hwaddr addr,break ;default : -fprintf (stderr,"%s: unknown OneNAND register %x\n" , - __func__, offset); +qemu_log_mask (LOG_GUEST_ERROR , +"write to unknown OneNAND register 0x%x\n" , + offset); } }
Re: [Qemu-arm] [PATCH for-3.1 2/2] hw/block/onenand: use qemu_log_mask() for reporting
In reply to Peter MaydellQuoted 4 lines · Peter Maydell
Update the onenand device to use
qemu_log_mask()for reporting guest errors and unimplemented features, rather than plainfprintf()andhw_error().
Quoted 8 lines
(We leave the
hw_error()inonenand_reset(), as that is triggered by a failure to read the underlying block device for the bootRAM, not by guest action.)Signed-off-by Peter Maydell <[email protected]>--- hw/block/onenand.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)
r~
Re: [Qemu-arm] [PATCH for-3.1 2/2] hw/block/onenand: use qemu_log_mask() for reporting
In reply to Peter MaydellQuoted 4 lines · Peter Maydell
Update the onenand device to use
qemu_log_mask()for reporting guest errors and unimplemented features, rather than plainfprintf()andhw_error().
Quoted 8 lines
(We leave the
hw_error()inonenand_reset(), as that is triggered by a failure to read the underlying block device for the bootRAM, not by guest action.)Signed-off-by Peter Maydell <[email protected]>--- hw/block/onenand.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-)