Skip to contentSkip to conversation

softmmu: Always initialize xlat in address_space_translate_for_iotlb

PATCH Superseded Richard Henderson 2 participants 4 messages Jun 15, 2022 – Jun 21, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 softmmu: Always initialize xlat in address_space_translate_for_iotlb

The bug is an uninitialized memory read, along the translate_fail path, which results in garbage being read from iotlb_to_section, which can lead to a crash in io_readx/io_writex.

The bug may be fixed by writing any value with zero
in ~TARGET_PAGE_MASK, so that the call to iotlb_to_section using the xlat'ed address returns io_mem_unassigned, as desired by the translate_fail path.

It is most useful to record the original physical page address, which will eventually be logged by memory_region_access_valid when the access is rejected by unassigned_mem_accepts.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1065

---
 softmmu/physmem.c | 3 +++
 1 file changed, 3 insertions(+)
softmmu/physmem.c +3 -0
index 657841eed0..fb0f0709b5 100644
@@ -681,6 +681,9 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
     AddressSpaceDispatch *d =
         qatomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch);
 
+    /* Record the original phys page for use by the translate_fail path. */
+    *xlat = addr;
+
     for (;;) {
         section = address_space_translate_internal(d, addr, &addr, plen, false);
 
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 lines · Richard Henderson

The bug is an uninitialized memory read, along the translate_fail path, which results in garbage being read from iotlb_to_section, which can lead to a crash in io_readx/io_writex.

The bug may be fixed by writing any value with zero
in ~TARGET_PAGE_MASK, so that the call to iotlb_to_section using the xlat'ed address returns io_mem_unassigned, as desired by the translate_fail path.

It is most useful to record the original physical page address, which will eventually be logged by memory_region_access_valid when the access is rejected by unassigned_mem_accepts.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1065

---
 softmmu/physmem.c | 3 +++
 1 file changed, 3 insertions(+)
softmmu/physmem.c +0 -0
index 657841eed0..fb0f0709b5 100644

softmmu/physmem.c address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr, · line 685

Quoted 5 lines
softmmu/physmem.c +2 -0
@@ -681,6 +681,9 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
     AddressSpaceDispatch *d =
         qatomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch);
 
+    /* Record the original phys page for use by the translate_fail path. */
+    *xlat = addr;

There's no doc comment for address_space_translate_for_iotlb(), so there's nothing that says explicitly that addr is obliged to be page aligned, although it happens that its only caller does pass a page-aligned address. Were we already implicitly requiring a page-aligned address here, or does not masking addr before assigning to *xlat impose a new requirement ?

thanks
-- PMM

#2 lore raw
Quoted 30 lines · Peter Maydell
Quoted 28 lines · Richard Henderson

The bug is an uninitialized memory read, along the translate_fail path, which results in garbage being read from iotlb_to_section, which can lead to a crash in io_readx/io_writex.

The bug may be fixed by writing any value with zero
in ~TARGET_PAGE_MASK, so that the call to iotlb_to_section using the xlat'ed address returns io_mem_unassigned, as desired by the translate_fail path.

It is most useful to record the original physical page address, which will eventually be logged by memory_region_access_valid when the access is rejected by unassigned_mem_accepts.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1065

---
  softmmu/physmem.c | 3 +++
  1 file changed, 3 insertions(+)
softmmu/physmem.c +0 -0
index 657841eed0..fb0f0709b5 100644
@@ -681,6 +681,9 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
      AddressSpaceDispatch *d =
          qatomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch);
 
Quoted 9 lines

softmmu/physmem.c address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr, · line 685

Quoted 2 lines
softmmu/physmem.c +2 -0
+    /* Record the original phys page for use by the translate_fail path. */
+    *xlat = addr;

There's no doc comment for address_space_translate_for_iotlb(), so there's nothing that says explicitly that addr is obliged to be page aligned, although it happens that its only caller does pass a page-aligned address. Were we already implicitly requiring a page-aligned address here, or does not masking addr before assigning to *xlat impose a new requirement ?

I have no idea. The whole lookup process is both undocumented and twistedly complex. I'm willing to add an extra masking operation here, if it seems necessary?

r~

#3 lore raw
Quoted 42 lines · Richard Henderson
Quoted 30 lines · Peter Maydell
Quoted 28 lines · Richard Henderson

The bug is an uninitialized memory read, along the translate_fail path, which results in garbage being read from iotlb_to_section, which can lead to a crash in io_readx/io_writex.

The bug may be fixed by writing any value with zero
in ~TARGET_PAGE_MASK, so that the call to iotlb_to_section using the xlat'ed address returns io_mem_unassigned, as desired by the translate_fail path.

It is most useful to record the original physical page address, which will eventually be logged by memory_region_access_valid when the access is rejected by unassigned_mem_accepts.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1065

---
  softmmu/physmem.c | 3 +++
  1 file changed, 3 insertions(+)
softmmu/physmem.c +0 -0
index 657841eed0..fb0f0709b5 100644
@@ -681,6 +681,9 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr,
      AddressSpaceDispatch *d =
          qatomic_rcu_read(&cpu->cpu_ases[asidx].memory_dispatch);
 
Quoted 9 lines

softmmu/physmem.c address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr addr, · line 685

Quoted 2 lines
softmmu/physmem.c +2 -0
+    /* Record the original phys page for use by the translate_fail path. */
+    *xlat = addr;

There's no doc comment for address_space_translate_for_iotlb(), so there's nothing that says explicitly that addr is obliged to be page aligned, although it happens that its only caller does pass a page-aligned address. Were we already implicitly requiring a page-aligned address here, or does not masking addr before assigning to *xlat impose a new requirement ?

Quoted 2 lines

I have no idea. The whole lookup process is both undocumented and twistedly complex. I'm willing to add an extra masking operation here, if it seems necessary?

I think we should do one of:
* document that we assume the address is page-aligned
* assert that the address is page-aligned
* mask to force it to page-alignedness

but I much don't care which one of those we do. Maybe we should assert((*xlat & ~TARGET_PAGE_MASK) == 0) at the translate_fail label, with a suitable comment ?

thanks
-- PMM