target/riscv: fix stale ptshift and base on page walk restart
PATCH 1/2 target/riscv: fix stale ptshift and base on page walk restart
When the atomic compare-and-swap for updating A/D bits in the page table entry fails due to a concurrent PTE modification by another vCPU, get_physical_address() jumps to the 'restart' label to re-walk the page table from the root.
However, neither 'ptshift' nor 'base' are re-initialized before the restart. After the walk completes, ptshift has been decremented to its final value and base has been overwritten with an inner PTE PPN. On goto restart, the for loop resets i=0 but ptshift and base remain stale, causing the restarted walk to compute incorrect PTE addresses.
In an SMP guest with MTTCG and Svadu active, this can result in incorrect physical address mappings or guest crashes.
Fix by saving the root base address and re-initializing both ptshift and base on each restart.
--- target/riscv/cpu_helper.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
target/riscv/cpu_helper.c +4 -1
@@ -1316,12 +1316,15 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, adue = adue && (env->henvcfg &HENVCFG_ADUE ); } -int ptshift = (levels -1 ) * ptidxbits; +int ptshift;target_ulong pte;hwaddr pte_addr; +const hwaddr base_root = base;int i; restart: + ptshift = (levels -1 ) * ptidxbits; + base = base_root;for (i =0 ; i < levels; i++, ptshift -= ptidxbits) {target_ulong idx;if (i ==0 ) {
Signature · 1 line
2.43.0
PATCH 2/2 hw/intc: fix heap OOB in ACLINT MTIMER multi-socket
The MMIO read/write handlers index timecmp[] with the absolute hartid (hartid_base + offset) but the array is allocated with num_harts elements. In multi-socket configurations with hartid_base > 0 this causes heap OOB access in the QEMU process.
Fix by using the relative offset for array indexing.
--- hw/intc/riscv_aclint.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
hw/intc/riscv_aclint.c +6 -4
@@ -131,6 +131,7 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr, addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {size_t hartid = mtimer->hartid_base + ((addr - mtimer->timecmp_base) >>3 ); +size_t hartid_offset = hartid - mtimer->hartid_base;CPUState *cpu =cpu_by_arch_id (hartid);CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ;if (!env) {
@@ -138,11 +139,11 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr, "aclint-mtimer: invalid hartid: %zu", hartid); }else if ((addr &0x7 ) ==0 ) {/* timecmp_lo for RV32/RV64 or timecmp for RV64 */ -uint64_t timecmp = mtimer->timecmp[hartid]; +uint64_t timecmp = mtimer->timecmp[hartid_offset];return (size ==4 ) ? (timecmp &0xFFFFFFFF ) : timecmp; }else if ((addr &0x7 ) ==4 ) {/* timecmp_hi */ -uint64_t timecmp = mtimer->timecmp[hartid]; +uint64_t timecmp = mtimer->timecmp[hartid_offset];return (timecmp >>32 ) &0xFFFFFFFF ; }else { qemu_log_mask(LOG_UNIMP,
@@ -174,6 +175,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {size_t hartid = mtimer->hartid_base + ((addr - mtimer->timecmp_base) >>3 ); +size_t hartid_offset = hartid - mtimer->hartid_base;CPUState *cpu =cpu_by_arch_id (hartid);CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ;if (!env) {
@@ -182,7 +184,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, }else if ((addr &0x7 ) ==0 ) {if (size ==4 ) {/* timecmp_lo for RV32/RV64 */ -uint64_t timecmp_hi = mtimer->timecmp[hartid] >>32 ; +uint64_t timecmp_hi = mtimer->timecmp[hartid_offset] >>32 ;riscv_aclint_mtimer_write_timecmp (mtimer,RISCV_CPU (cpu), hartid, timecmp_hi <<32 | (value &0xFFFFFFFF )); }else {
@@ -193,7 +195,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, }else if ((addr &0x7 ) ==4 ) {if (size ==4 ) {/* timecmp_hi for RV32/RV64 */ -uint64_t timecmp_lo = mtimer->timecmp[hartid]; +uint64_t timecmp_lo = mtimer->timecmp[hartid_offset];riscv_aclint_mtimer_write_timecmp (mtimer,RISCV_CPU (cpu), hartid, value <<32 | (timecmp_lo &0xFFFFFFFF )); }else {
Signature · 1 line
2.43.0
Re: [PATCH 2/2] hw/intc: fix heap OOB in ACLINT MTIMER multi-socket
Quoted 10 lines · Sebastián Alba Vives
The MMIO read/write handlers index timecmp[] with the absolute hartid (
hartid_base+ offset) but the array is allocated withnum_hartselements. In multi-socket configurations withhartid_base> 0 this causes heap OOB access in the QEMU process.Fix by using the relative offset for array indexing.
Signed-off-by Sebastián Alba Vives <[email protected]>
Alistair
Quoted 60 lines
--- hw/intc/riscv_aclint.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)hw/intc/riscv_aclint.c +6
-5@@ -131,6 +131,7 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr, addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {size_t hartid = mtimer->hartid_base + ((addr - mtimer->timecmp_base) >>3 ); +size_t hartid_offset = hartid - mtimer->hartid_base;CPUState *cpu =cpu_by_arch_id (hartid);CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ;if (!env) {@@ -138,11 +139,11 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr, "aclint-mtimer: invalid hartid: %zu", hartid); }else if ((addr &0x7 ) ==0 ) {/* timecmp_lo for RV32/RV64 or timecmp for RV64 */ -uint64_t timecmp = mtimer->timecmp[hartid]; +uint64_t timecmp = mtimer->timecmp[hartid_offset];return (size ==4 ) ? (timecmp &0xFFFFFFFF ) : timecmp; }else if ((addr &0x7 ) ==4 ) {/* timecmp_hi */ -uint64_t timecmp = mtimer->timecmp[hartid]; +uint64_t timecmp = mtimer->timecmp[hartid_offset];return (timecmp >>32 ) &0xFFFFFFFF ; }else { qemu_log_mask(LOG_UNIMP,@@ -174,6 +175,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {size_t hartid = mtimer->hartid_base + ((addr - mtimer->timecmp_base) >>3 ); +size_t hartid_offset = hartid - mtimer->hartid_base;CPUState *cpu =cpu_by_arch_id (hartid);CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ;if (!env) {@@ -182,7 +184,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, }else if ((addr &0x7 ) ==0 ) {if (size ==4 ) {/* timecmp_lo for RV32/RV64 */ -uint64_t timecmp_hi = mtimer->timecmp[hartid] >>32 ; +uint64_t timecmp_hi = mtimer->timecmp[hartid_offset] >>32 ;riscv_aclint_mtimer_write_timecmp (mtimer,RISCV_CPU (cpu), hartid, timecmp_hi <<32 | (value &0xFFFFFFFF )); }else {@@ -193,7 +195,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, }else if ((addr &0x7 ) ==4 ) {if (size ==4 ) {/* timecmp_hi for RV32/RV64 */ -uint64_t timecmp_lo = mtimer->timecmp[hartid]; +uint64_t timecmp_lo = mtimer->timecmp[hartid_offset];riscv_aclint_mtimer_write_timecmp (mtimer,RISCV_CPU (cpu), hartid, value <<32 | (timecmp_lo &0xFFFFFFFF )); }else { --2.43.0
Alistair Francis
AppliedRe: [PATCH 2/2] hw/intc: fix heap OOB in ACLINT MTIMER multi-socket
In reply to Sebastián Alba VivesQuoted 10 lines · Sebastián Alba Vives
The MMIO read/write handlers index timecmp[] with the absolute hartid (
hartid_base+ offset) but the array is allocated withnum_hartselements. In multi-socket configurations withhartid_base> 0 this causes heap OOB access in the QEMU process.Fix by using the relative offset for array indexing.
Signed-off-by Sebastián Alba Vives <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair
Quoted 60 lines
--- hw/intc/riscv_aclint.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)hw/intc/riscv_aclint.c +6
-5@@ -131,6 +131,7 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr, addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {size_t hartid = mtimer->hartid_base + ((addr - mtimer->timecmp_base) >>3 ); +size_t hartid_offset = hartid - mtimer->hartid_base;CPUState *cpu =cpu_by_arch_id (hartid);CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ;if (!env) {@@ -138,11 +139,11 @@ static uint64_t riscv_aclint_mtimer_read(void *opaque, hwaddr addr, "aclint-mtimer: invalid hartid: %zu", hartid); }else if ((addr &0x7 ) ==0 ) {/* timecmp_lo for RV32/RV64 or timecmp for RV64 */ -uint64_t timecmp = mtimer->timecmp[hartid]; +uint64_t timecmp = mtimer->timecmp[hartid_offset];return (size ==4 ) ? (timecmp &0xFFFFFFFF ) : timecmp; }else if ((addr &0x7 ) ==4 ) {/* timecmp_hi */ -uint64_t timecmp = mtimer->timecmp[hartid]; +uint64_t timecmp = mtimer->timecmp[hartid_offset];return (timecmp >>32 ) &0xFFFFFFFF ; }else { qemu_log_mask(LOG_UNIMP,@@ -174,6 +175,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, addr < (mtimer->timecmp_base + (mtimer->num_harts << 3))) {size_t hartid = mtimer->hartid_base + ((addr - mtimer->timecmp_base) >>3 ); +size_t hartid_offset = hartid - mtimer->hartid_base;CPUState *cpu =cpu_by_arch_id (hartid);CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ;if (!env) {@@ -182,7 +184,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, }else if ((addr &0x7 ) ==0 ) {if (size ==4 ) {/* timecmp_lo for RV32/RV64 */ -uint64_t timecmp_hi = mtimer->timecmp[hartid] >>32 ; +uint64_t timecmp_hi = mtimer->timecmp[hartid_offset] >>32 ;riscv_aclint_mtimer_write_timecmp (mtimer,RISCV_CPU (cpu), hartid, timecmp_hi <<32 | (value &0xFFFFFFFF )); }else {@@ -193,7 +195,7 @@ static void riscv_aclint_mtimer_write(void *opaque, hwaddr addr, }else if ((addr &0x7 ) ==4 ) {if (size ==4 ) {/* timecmp_hi for RV32/RV64 */ -uint64_t timecmp_lo = mtimer->timecmp[hartid]; +uint64_t timecmp_lo = mtimer->timecmp[hartid_offset];riscv_aclint_mtimer_write_timecmp (mtimer,RISCV_CPU (cpu), hartid, value <<32 | (timecmp_lo &0xFFFFFFFF )); }else { --2.43.0
Re: [PATCH 2/2] hw/intc: fix heap OOB in ACLINT MTIMER multi-socket
In reply to Sebastián Alba VivesQuoted 5 lines · Sebastián Alba Vives
The MMIO read/write handlers index timecmp[] with the absolute hartid (
hartid_base+ offset) but the array is allocated withnum_hartselements. In multi-socket configurations withhartid_base> 0 this causes heap OOB access in the QEMU process.
Quoted 7 lines
Fix by using the relative offset for array indexing.
Signed-off-by Sebastián Alba Vives <[email protected]>--- hw/intc/riscv_aclint.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
This one too, feels like it should be applied to the current stable series. I'm picking it up, please let me know if I shouldn't.
Thanks,
/mjt
Quoted 12 lines · Sebastián Alba Vives
When the atomic compare-and-swap for updating A/D bits in the page table entry fails due to a concurrent PTE modification by another vCPU,
get_physical_address()jumps to the 'restart' label to re-walk the page table from the root.However, neither 'ptshift' nor 'base' are re-initialized before the restart. After the walk completes, ptshift has been decremented to its final value and base has been overwritten with an inner PTE PPN. On goto restart, the for loop resets i=0 but ptshift and base remain stale, causing the restarted walk to compute incorrect PTE addresses.
Quoted 8 lines
In an SMP guest with MTTCG and Svadu active, this can result in incorrect physical address mappings or guest crashes.
Fix by saving the root base address and re-initializing both ptshift and base on each restart.
Fixes 0c3e702aca ("RISC-V CPU Helpers")Signed-off-by Sebastián Alba Vives <[email protected]>
Alistair
Quoted 29 lines
--- target/riscv/cpu_helper.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)target/riscv/cpu_helper.c +4
-2@@ -1316,12 +1316,15 @@ static int get_physical_address(CPURISCVState *env, hwaddr *physical, adue = adue && (env->henvcfg &HENVCFG_ADUE ); } -int ptshift = (levels -1 ) * ptidxbits; +int ptshift;target_ulong pte;hwaddr pte_addr; +const hwaddr base_root = base;int i; restart: + ptshift = (levels -1 ) * ptidxbits; + base = base_root;for (i =0 ; i < levels; i++, ptshift -= ptidxbits) {target_ulong idx;if (i ==0 ) { --2.43.0
Quoted 11 lines · Sebastián Alba Vives
When the atomic compare-and-swap for updating A/D bits in the page table entry fails due to a concurrent PTE modification by another vCPU,
get_physical_address()jumps to the 'restart' label to re-walk the page table from the root.However, neither 'ptshift' nor 'base' are re-initialized before the restart. After the walk completes, ptshift has been decremented to its final value and base has been overwritten with an inner PTE PPN. On goto restart, the for loop resets i=0 but ptshift and base remain stale, causing the restarted walk to compute incorrect PTE addresses.
Quoted 8 lines
In an SMP guest with MTTCG and Svadu active, this can result in incorrect physical address mappings or guest crashes.
Fix by saving the root base address and re-initializing both ptshift and base on each restart.
Fixes 0c3e702aca ("RISC-V CPU Helpers")Signed-off-by Sebastián Alba Vives <[email protected]>
Shouldn't this change be in stable qemu series too? I'm picking it up, please let me know if I shouldn't.
Thanks,
/mjt
Quoted 16 lines · Michael Tokarev
Quoted 14 lines · Sebastián Alba Vives
When the atomic compare-and-swap for updating A/D bits in the page table entry fails due to a concurrent PTE modification by another vCPU,
get_physical_address()jumps to the 'restart' label to re-walk the page table from the root.However, neither 'ptshift' nor 'base' are re-initialized before the restart. After the walk completes, ptshift has been decremented to its final value and base has been overwritten with an inner PTE PPN. On goto restart, the for loop resets i=0 but ptshift and base remain stale, causing the restarted walk to compute incorrect PTE addresses.
In an SMP guest with MTTCG and Svadu active, this can result in incorrect physical address mappings or guest crashes.
Quoted 7 lines
Quoted 5 lines
Fix by saving the root base address and re-initializing both ptshift and base on each restart.
Fixes 0c3e702aca ("RISC-V CPU Helpers")Signed-off-by Sebastián Alba Vives <[email protected]>Shouldn't this change be in stable qemu series too?
Yes, it should.
I thought the Fixes tags were picked up, should it also CC qemu-stable?
Alistair
On 01.05.2026 04:32, Alistair Francis wrote:
..
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Fixes 0c3e702aca ("RISC-V CPU Helpers")Signed-off-by Sebastián Alba Vives <[email protected]>Shouldn't this change be in stable qemu series too?
Yes, it should.
I thought the
Fixestags were picked up, should it also CC qemu-stable?
The "Fixes" tag does not necessary mean the change is for stable series. Many are, but definitely not all of them. Especially when the talk's about anything related to migration - even more serious bugs might not be fixed in stable because the fix introduces a change in migration stream.
So far I do watch for Fixes: tags and either pick stuff up like this one, with a notification, or as for confirmation if I'm not sure.
You can also Cc: qemu-stable in regular email when discussing the changes, not necessary add Cc: tags.
Either way works.
Thank you!
/mjt