hw/intc: sifive_plic: Fix heap-buffer-overflow in SiFive PLIC read operation
PATCH 1/1 hw/intc: sifive_plic: Fix heap-buffer-overflow in SiFive PLIC read operation
The sifive_plic_read function in hw/intc/sifive_plic.c had a potential heap-buffer-overflow issue when reading from the pending_base region. This occurred because the code did not check if the calculated word index was within valid bounds before accessing the pending array.
This fix prevents out-of-bounds memory access, ensuring safer and more robust handling of PLIC reads.
ASAN log:
==78800==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x602000038a14 at pc 0x5baf49d0d6cb bp 0x7ffc2ea4e180 sp 0x7ffc2ea4e178 READ of size 4 at 0x602000038a14 thread T0
#0 0x5baf49d0d6ca in sifive_plic_read hw/intc/sifive_plic.c:151:16
#1 0x5baf49f7f3bb in memory_region_read_accessor system/memory.c:445:11Reproducer:
cat << EOF | qemu-system-riscv64 -display \
none -machine accel=qtest, -m 512M -machine shakti_c -m 2G -qtest stdio readl 0xc001004
EOF
--- hw/intc/sifive_plic.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
hw/intc/sifive_plic.c +8 -1
@@ -147,7 +147,14 @@ static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) (plic->num_sources + 31) >> 3)) {uint32_t word = (addr - plic->pending_base) >>2 ; -return plic->pending[word]; +if (word < plic->bitfield_words) { +return plic->pending[word]; + }else { +qemu_log_mask (LOG_GUEST_ERROR , +"sifive_plic_read: Word out of bounds for pending_base read: word=%u\n" , + word); +return 0 ; + } } else if (addr_between(addr, plic->enable_base, plic->num_addrs * plic->enable_stride)) {uint32_t addrid = (addr - plic->enable_base) / plic->enable_stride;
Signature · 1 line
2.34.1
Quoted 33 lines · Zheyu Ma
The
sifive_plic_readfunction inhw/intc/sifive_plic.chad a potential heap-buffer-overflow issue when reading from thepending_baseregion. This occurred because the code did not check if the calculated word index was within valid bounds before accessing the pending array.This fix prevents out-of-bounds memory access, ensuring safer and more robust handling of PLIC reads.
ASAN log:
==78800==ERROR: AddressSanitizer: heap-buffer-overflow on address0x602000038a14at pc0x5baf49d0d6cbbp0x7ffc2ea4e180sp0x7ffc2ea4e178READ of size 4 at0x602000038a14thread T0#0 0x5baf49d0d6ca in sifive_plic_read hw/intc/sifive_plic.c:151:16 #1 0x5baf49f7f3bb in memory_region_read_accessor system/memory.c:445:11Reproducer:
cat << EOF | qemu-system-riscv64 -display \
none -machine accel=qtest, -m 512M -machineshakti_c-m 2G -qtest stdio readl0xc001004
EOFSigned-off-by Zheyu Ma <[email protected]>--- hw/intc/sifive_plic.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)hw/intc/sifive_plic.c +0
-0@@ -147,7 +147,14 @@ static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) (plic->num_sources + 31) >> 3)) {uint32_t word = (addr - plic->pending_base) >>2 ;
hw/intc/sifive_plic.c static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) · line 157
Quoted 10 lines
hw/intc/sifive_plic.c +8
-1-return plic->pending[word]; +if (word < plic->bitfield_words) { +return plic->pending[word]; + }else { +qemu_log_mask (LOG_GUEST_ERROR , +"sifive_plic_read: Word out of bounds for pending_base read: word=%u\n" , + word); +return 0 ; + }
This seems a bit odd. This part of the code is guarded by
} else if (addr_between(addr, plic->pending_base,
(plic->num_sources + 31) >> 3)) {and we calculate plic->bitfield_words in realize based on plic->num_sources:
s->bitfield_words = (s->num_sources + 31) >> 5;
so presumably the intention was that we put enough words in the bitfield for the number of sources we have, so that the array access wouldn't overrun. Maybe we got the calculation wrong?
thanks
-- PMM
Quoted 51 lines · Peter Maydell
Quoted 33 lines · Zheyu Ma
The
sifive_plic_readfunction inhw/intc/sifive_plic.chad a potential heap-buffer-overflow issue when reading from thepending_baseregion. This occurred because the code did not check if the calculated word index was within valid bounds before accessing the pending array.This fix prevents out-of-bounds memory access, ensuring safer and more robust handling of PLIC reads.
ASAN log:
==78800==ERROR: AddressSanitizer: heap-buffer-overflow on address0x602000038a14at pc0x5baf49d0d6cbbp0x7ffc2ea4e180sp0x7ffc2ea4e178READ of size 4 at0x602000038a14thread T0#0 0x5baf49d0d6ca in sifive_plic_read hw/intc/sifive_plic.c:151:16 #1 0x5baf49f7f3bb in memory_region_read_accessor system/memory.c:445:11Reproducer:
cat << EOF | qemu-system-riscv64 -display \
none -machine accel=qtest, -m 512M -machineshakti_c-m 2G -qtest stdio readl0xc001004
EOFSigned-off-by Zheyu Ma <[email protected]>--- hw/intc/sifive_plic.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)hw/intc/sifive_plic.c +0
-0@@ -147,7 +147,14 @@ static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) (plic->num_sources + 31) >> 3)) {uint32_t word = (addr - plic->pending_base) >>2 ;hw/intc/sifive_plic.c static uint64_t sifive_plic_read(void *opaque, hwaddr addr, unsigned size) · line 157
Quoted 10 lines
hw/intc/sifive_plic.c +8
-1-return plic->pending[word]; +if (word < plic->bitfield_words) { +return plic->pending[word]; + }else { +qemu_log_mask (LOG_GUEST_ERROR , +"sifive_plic_read: Word out of bounds for pending_base read: word=%u\n" , + word); +return 0 ; + }This seems a bit odd. This part of the code is guarded by
} else if (addr_between(addr, plic->pending_base, (plic->num_sources + 31) >> 3)) {
Quoted 8 lines
and we calculate
plic->bitfield_wordsin realize based onplic->num_sources:s->bitfield_words = (s->num_sources + 31) >> 5;so presumably the intention was that we put enough words in the bitfield for the number of sources we have, so that the array access wouldn't overrun. Maybe we got the calculation wrong?
Yeah, the calculation is wrong here.
We have
s->bitfield_words = (s->num_sources + 31) >> 5;
...s->pending = g_new0(uint32_t, s->bitfield_words);
But then the check is
} else if (addr_between(addr, plic->pending_base,
(plic->num_sources + 31) >> 3)) {So when we allocate we shift by 5, but then when we check we only shift by 3.
So that's the bug. It's not immediately obvious what the correct fix is though, do you mind having a look?
Alistair
Quoted 4 lines
thanks
-- PMM