hw/gpio/aspeed: Add bounds checking for register table access
PATCH 1/1 hw/gpio/aspeed: Add bounds checking for register table access
Added bounds checking in the aspeed_gpio_read() and aspeed_gpio_write() functions to ensure the index idx is within the valid range of the reg_table array.
The correct size of reg_table is determined dynamically based on whether it is aspeed_3_3v_gpios or aspeed_1_8v_gpios. If idx exceeds the size of reg_table, an error is logged, and the function returns.
AddressSanitizer log indicating the issue:
==2602930==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55a5da29e128 at pc 0x55a5d700dc62 bp 0x7fff096c4e90 sp 0x7fff096c4e88 READ of size 2 at 0x55a5da29e128 thread T0
#0 0x55a5d700dc61 in aspeed_gpio_read hw/gpio/aspeed_gpio.c:564:14
#1 0x55a5d933f3ab in memory_region_read_accessor system/memory.c:445:11
#2 0x55a5d92fba40 in access_with_adjusted_size system/memory.c:573:18
#3 0x55a5d92f842c in memory_region_dispatch_read1 system/memory.c:1426:16
#4 0x55a5d92f7b68 in memory_region_dispatch_read system/memory.c:1459:9
#5 0x55a5d9376ad1 in flatview_read_continue_step system/physmem.c:2836:18
#6 0x55a5d9376399 in flatview_read_continue system/physmem.c:2877:19
#7 0x55a5d93775b8 in flatview_read system/physmem.c:2907:12--- hw/gpio/aspeed_gpio.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
hw/gpio/aspeed_gpio.c +26 -0
@@ -550,6 +550,7 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size)GPIOSets *set;uint32_t value =0 ;uint64_t debounce_value; +uint32_t reg_table_size; idx = offset >>2 ;if (idx >=GPIO_DEBOUNCE_TIME_1 && idx <=GPIO_DEBOUNCE_TIME_3 ) {
@@ -559,6 +560,18 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size)return debounce_value; } +if (agc->reg_table == aspeed_3_3v_gpios) { + reg_table_size =GPIO_3_3V_REG_ARRAY_SIZE ; + }else { + reg_table_size =GPIO_1_8V_REG_ARRAY_SIZE ; + } + +if (idx >= reg_table_size) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: idx 0x%" PRIx64" out of bounds\n" , + __func__, idx); +return 0 ; + } + reg = &agc->reg_table[idx];if (reg->set_idx >= agc->nr_gpio_sets) { qemu_log_mask(LOG_GUEST_ERROR, "%s: no getter for offset 0x%"
@@ -768,6 +781,7 @@ static void aspeed_gpio_write(void *opaque, hwaddr offset, uint64_t data,const AspeedGPIOReg *reg;GPIOSets *set;uint32_t cleared; +uint32_t reg_table_size;trace_aspeed_gpio_write (offset, data);
@@ -785,6 +799,18 @@ static void aspeed_gpio_write(void *opaque, hwaddr offset, uint64_t data,return ; } +if (agc->reg_table == aspeed_3_3v_gpios) { + reg_table_size =GPIO_3_3V_REG_ARRAY_SIZE ; + }else { + reg_table_size =GPIO_1_8V_REG_ARRAY_SIZE ; + } + +if (idx >= reg_table_size) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: idx 0x%" PRIx64" out of bounds\n" , + __func__, idx); +return ; + } + reg = &agc->reg_table[idx];if (reg->set_idx >= agc->nr_gpio_sets) { qemu_log_mask(LOG_GUEST_ERROR, "%s: no setter for offset 0x%"
Signature · 1 line
2.34.1
Andrew Jeffery
maintainerHello Zheyu Ma,
Quoted 12 lines · Zheyu Ma
Added bounds checking in the
aspeed_gpio_read()andaspeed_gpio_write()functions to ensure the index idx is within the valid range of thereg_tablearray.The correct size of
reg_tableis determined dynamically based on whether it isaspeed_3_3v_gpiosoraspeed_1_8v_gpios. If idx exceeds the size ofreg_table, an error is logged, and the function returns.AddressSanitizer log indicating the issue:
==2602930==ERROR: AddressSanitizer: global-buffer-overflow on address
0x55a5da29e128at pc0x55a5d700dc62bp0x7fff096c4e90sp0x7fff096c4e88READ of size 2 at0x55a5da29e128thread T0
Quoted 8 lines
#0 0x55a5d700dc61 in aspeed_gpio_read hw/gpio/aspeed_gpio.c:564:14
#1 0x55a5d933f3ab in memory_region_read_accessor system/memory.c:445:11
#2 0x55a5d92fba40 in access_with_adjusted_size system/memory.c:573:18
#3 0x55a5d92f842c in memory_region_dispatch_read1 system/memory.c:1426:16
#4 0x55a5d92f7b68 in memory_region_dispatch_read system/memory.c:1459:9
#5 0x55a5d9376ad1 in flatview_read_continue_step system/physmem.c:2836:18
#6 0x55a5d9376399 in flatview_read_continue system/physmem.c:2877:19
#7 0x55a5d93775b8 in flatview_read system/physmem.c:2907:12I'm mildly interested in what you were doing to trigger this. Certainly we could do with a guard in the model to prevent it, but I'm curious all the same.
Quoted 19 lines
Signed-off-by Zheyu Ma <[email protected]>--- hw/gpio/aspeed_gpio.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)hw/gpio/aspeed_gpio.c +1
-0@@ -550,6 +550,7 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size)GPIOSets *set;uint32_t value =0 ;uint64_t debounce_value; +uint32_t reg_table_size; idx = offset >>2 ;if (idx >=GPIO_DEBOUNCE_TIME_1 && idx <=GPIO_DEBOUNCE_TIME_3 ) {
hw/gpio/aspeed_gpio.c static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) · line 567
Quoted 8 lines
hw/gpio/aspeed_gpio.c +5
-0@@ -559,6 +560,18 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size)return debounce_value; } +if (agc->reg_table == aspeed_3_3v_gpios) { + reg_table_size =GPIO_3_3V_REG_ARRAY_SIZE ; + }else { + reg_table_size =GPIO_1_8V_REG_ARRAY_SIZE ; + }
I think I'd prefer we add reg_table_size as a member of AspeedGPIOClass and initialise it at the same time as we initialise reg_table. I feel it would help maintain safety in the face of future changes (i.e. if another reg table were introduced). With that approach the hunk above can be dropped.
hw/gpio/aspeed_gpio.c static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) · line 569
Quoted 2 lines
hw/gpio/aspeed_gpio.c +2
-0+ +if (idx >= reg_table_size) {
This condition would then become:
```
if (idx >= agc->reg_table_size) {
```Thoughts?
Andrew
Hi Andrew,
Quoted 24 lines · Andrew Jeffery
Hello Zheyu Ma,
Quoted 11 lines · Zheyu Ma
Added bounds checking in the
aspeed_gpio_read()andaspeed_gpio_write()functions to ensure the index idx is within the valid range of thereg_tablearray.The correct size of
reg_tableis determined dynamically based on whether it isaspeed_3_3v_gpiosoraspeed_1_8v_gpios. If idx exceeds the size ofreg_table, an error is logged, and the function returns.AddressSanitizer log indicating the issue:
==2602930==ERROR: AddressSanitizer: global-buffer-overflow on address
0x55a5da29e128at pc0x55a5d700dc62bp0x7fff096c4e90sp0x7fff096c4e88Quoted 3 lines
READ of size 2 at
0x55a5da29e128thread T0#0 0x55a5d700dc61 in aspeed_gpio_read hw/gpio/aspeed_gpio.c:564:14 #1 0x55a5d933f3ab in memory_region_read_accessor
system/memory.c:445:11Quoted 2 lines
#2 0x55a5d92fba40 in access_with_adjusted_size system/memory.c:573:18 #3 0x55a5d92f842c in memory_region_dispatch_read1
system/memory.c:1426:16Quoted 1 line
#4 0x55a5d92f7b68 in memory_region_dispatch_read
system/memory.c:1459:9
Quoted 9 lines
Quoted 1 line
#5 0x55a5d9376ad1 in flatview_read_continue_step
system/physmem.c:2836:18Quoted 2 lines
#6 0x55a5d9376399 in flatview_read_continue system/physmem.c:2877:19 #7 0x55a5d93775b8 in flatview_read system/physmem.c:2907:12I'm mildly interested in what you were doing to trigger this. Certainly we could do with a guard in the model to prevent it, but I'm curious all the same.
Actually, I'm doing the virtual device fuzzing test and trying to discover bugs.
Quoted 40 lines
hw/gpio/aspeed_gpio.c static uint64_t aspeed_gpio_read(void *opaque,
Quoted 12 lines
Signed-off-by Zheyu Ma <[email protected]>--- hw/gpio/aspeed_gpio.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)hw/gpio/aspeed_gpio.c +0
-0@@ -550,6 +550,7 @@ static uint64_t aspeed_gpio_read(void *opaque,hwaddr offset,
uint32_tsize)hw/gpio/aspeed_gpio.c static uint64_t aspeed_gpio_read(void *opaque,
Quoted 8 lines
hw/gpio/aspeed_gpio.c +1
-0GPIOSets *set;uint32_t value =0 ;uint64_t debounce_value; +uint32_t reg_table_size; idx = offset >>2 ;if (idx >=GPIO_DEBOUNCE_TIME_1 && idx <=GPIO_DEBOUNCE_TIME_3 ) {@@ -559,6 +560,18 @@ static uint64_t aspeed_gpio_read(void *opaque,hwaddr offset,
uint32_tsize)hw/gpio/aspeed_gpio.c static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) · line 567
Quoted 8 lines
hw/gpio/aspeed_gpio.c +5
-0return debounce_value; } +if (agc->reg_table == aspeed_3_3v_gpios) { + reg_table_size =GPIO_3_3V_REG_ARRAY_SIZE ; + }else { + reg_table_size =GPIO_1_8V_REG_ARRAY_SIZE ; + }I think I'd prefer we add
reg_table_sizeas a member of AspeedGPIOClass and initialise it at the same time as we initialisereg_table. I feel it would help maintain safety in the face of future changes (i.e. if another reg table were introduced). With that approach the hunk above can be dropped.hw/gpio/aspeed_gpio.c static uint64_t aspeed_gpio_read(void *opaque, · line 569
Quoted 2 lines
hw/gpio/aspeed_gpio.c +2
-0+ +if (idx >= reg_table_size) {
Quoted 8 lines
This condition would then become:
``` if (idx >= agc->reg_table_size) { ```Thoughts?
I agree with you, adding a new member is a more maintainable way, I'll send a v2 patch, thanks!
Zheyu
Quoted 44 lines · Zheyu Ma
Hi Andrew,
On Wed, Jun 19, 2024 at 1:58 AM Andrew Jeffery <[email protected] <mailto:[email protected]>> wrote:
Hello Zheyu Ma, On Tue, 2024-06-18 at 15:09 +0200, Zheyu Ma wrote: > Added bounds checking in the aspeed_gpio_read() and aspeed_gpio_write() > functions to ensure the index idx is within the valid range of the > reg_table array. > > The correct size of reg_table is determined dynamically based on whether > it is aspeed_3_3v_gpios or aspeed_1_8v_gpios. If idx exceeds the > size of reg_table, an error is logged, and the function returns. > > AddressSanitizer log indicating the issue: > > ==2602930==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55a5da29e128 at pc 0x55a5d700dc62 bp 0x7fff096c4e90 sp 0x7fff096c4e88 > READ of size 2 at 0x55a5da29e128 thread T0 > #0 0x55a5d700dc61 in aspeed_gpio_read hw/gpio/aspeed_gpio.c:564:14 > #1 0x55a5d933f3ab in memory_region_read_accessor system/memory.c:445:11 > #2 0x55a5d92fba40 in access_with_adjusted_size system/memory.c:573:18 > #3 0x55a5d92f842c in memory_region_dispatch_read1 system/memory.c:1426:16 > #4 0x55a5d92f7b68 in memory_region_dispatch_read system/memory.c:1459:9 > #5 0x55a5d9376ad1 in flatview_read_continue_step system/physmem.c:2836:18 > #6 0x55a5d9376399 in flatview_read_continue system/physmem.c:2877:19 > #7 0x55a5d93775b8 in flatview_read system/physmem.c:2907:12 I'm mildly interested in what you were doing to trigger this. Certainly we could do with a guard in the model to prevent it, but I'm curious all the same.
Quoted 1 line
Actually, I'm doing the virtual device fuzzing test and trying to discover bugs.
Could you share the reproducer? (As you did in your other patches, it is very useful to reproduce).
Quoted 54 lines
> > Signed-off-by: Zheyu Ma <[email protected] <mailto:[email protected]>> > --- > hw/gpio/aspeed_gpio.c | 26 ++++++++++++++++++++++++++ > 1 file changed, 26 insertions(+) > > diff --git a/hw/gpio/aspeed_gpio.c b/hw/gpio/aspeed_gpio.c > index c1781e2ba3..1441046f6c 100644 > --- a/hw/gpio/aspeed_gpio.c > +++ b/hw/gpio/aspeed_gpio.c > @@ -550,6 +550,7 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) > GPIOSets *set; > uint32_t value = 0; > uint64_t debounce_value; > + uint32_t reg_table_size; > > idx = offset >> 2; > if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) { > @@ -559,6 +560,18 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) > return debounce_value; > } > > + if (agc->reg_table == aspeed_3_3v_gpios) { > + reg_table_size = GPIO_3_3V_REG_ARRAY_SIZE; > + } else { > + reg_table_size = GPIO_1_8V_REG_ARRAY_SIZE; > + } I think I'd prefer we add reg_table_size as a member of AspeedGPIOClass and initialise it at the same time as we initialise reg_table. I feel it would help maintain safety in the face of future changes (i.e. if another reg table were introduced). With that approach the hunk above can be dropped. > + > + if (idx >= reg_table_size) { This condition would then become: ``` if (idx >= agc->reg_table_size) { ``` Thoughts?I agree with you, adding a new member is a more maintainable way, I'll send a v2 patch, thanks!
Zheyu
Hi Philippe,
Quoted 48 lines · Philippe Mathieu-Daudé
Quoted 4 lines · Zheyu Ma
Hi Andrew,
On Wed, Jun 19, 2024 at 1:58 AM Andrew Jeffery <[email protected] <mailto:[email protected]>>
Quoted 29 lines · wrote:
Hello Zheyu Ma, On Tue, 2024-06-18 at 15:09 +0200, Zheyu Ma wrote: > Added bounds checking in the aspeed_gpio_read() and aspeed_gpio_write() > functions to ensure the index idx is within the valid range of the > reg_table array. > > The correct size of reg_table is determined dynamically based on whether > it is aspeed_3_3v_gpios or aspeed_1_8v_gpios. If idx exceeds the > size of reg_table, an error is logged, and the function returns. > > AddressSanitizer log indicating the issue: > > ==2602930==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55a5da29e128 at pc 0x55a5d700dc62 bp 0x7fff096c4e90 sp 0x7fff096c4e88 > READ of size 2 at 0x55a5da29e128 thread T0 > #0 0x55a5d700dc61 in aspeed_gpio_read hw/gpio/aspeed_gpio.c:564:14 > #1 0x55a5d933f3ab in memory_region_read_accessor system/memory.c:445:11 > #2 0x55a5d92fba40 in access_with_adjusted_size system/memory.c:573:18 > #3 0x55a5d92f842c in memory_region_dispatch_read1 system/memory.c:1426:16 > #4 0x55a5d92f7b68 in memory_region_dispatch_readQuoted 8 lines
system/memory.c:1459:9 > #5 0x55a5d9376ad1 in flatview_read_continue_step system/physmem.c:2836:18 > #6 0x55a5d9376399 in flatview_read_continue system/physmem.c:2877:19 > #7 0x55a5d93775b8 in flatview_read system/physmem.c:2907:12 I'm mildly interested in what you were doing to trigger this.Certainly
Quoted 4 lines
we could do with a guard in the model to prevent it, but I'm curious all the same.
Quoted 6 lines
Quoted 2 lines
Actually, I'm doing the virtual device fuzzing test and trying to discover bugs.
Could you share the reproducer? (As you did in your other patches, it is very useful to reproduce).
Sure, I've sent a v3 patch.
Zheyu
Quoted 59 lines
Quoted 27 lines
> > Signed-off-by: Zheyu Ma <[email protected] <mailto:[email protected]>> > --- > hw/gpio/aspeed_gpio.c | 26 ++++++++++++++++++++++++++ > 1 file changed, 26 insertions(+) > > diff --git a/hw/gpio/aspeed_gpio.c b/hw/gpio/aspeed_gpio.c > index c1781e2ba3..1441046f6c 100644 > --- a/hw/gpio/aspeed_gpio.c > +++ b/hw/gpio/aspeed_gpio.c > @@ -550,6 +550,7 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) > GPIOSets *set; > uint32_t value = 0; > uint64_t debounce_value; > + uint32_t reg_table_size; > > idx = offset >> 2; > if (idx >= GPIO_DEBOUNCE_TIME_1 && idx <= GPIO_DEBOUNCE_TIME_3) { > @@ -559,6 +560,18 @@ static uint64_t aspeed_gpio_read(void *opaque, hwaddr offset, uint32_t size) > return debounce_value; > }Quoted 8 lines
> > + if (agc->reg_table == aspeed_3_3v_gpios) { > + reg_table_size = GPIO_3_3V_REG_ARRAY_SIZE; > + } else { > + reg_table_size = GPIO_1_8V_REG_ARRAY_SIZE; > + } I think I'd prefer we add reg_table_size as a member ofAspeedGPIOClass
Quoted 21 lines
and initialise it at the same time as we initialise
reg_table. I feel it would help maintain safety in the face of future changes (i.e. if another reg table were introduced). With that approach the hunk above can be dropped.> +
> + if (idx >=reg_table_size) {This condition would then become:
```
if (idx >=agc->reg_table_size) {
```Thoughts?
I agree with you, adding a new member is a more maintainable way, I'll send a v2 patch, thanks!
Zheyu