Skip to contentSkip to conversation

Bugfix: TCG cross-page overflow for 32 bit guest

PATCH In review ~percival_foss 2 participants 4 messages Apr 29, 2025 – Apr 30, 2025 Arm/Amlogic Meson SoC support

lore
Share & tools
lore
#0 lore raw

v2 cover Bugfix: TCG cross-page overflow for 32 bit guest

Since v1:
-Updated git commits to have proper signed off by tags/messages

Our company found and fixed a bug in TCG that caused 32 bit guests running on 64 bit hosts to crash. If a 32 bit host has a page translation that crosses beyond the last page. This causes a invalid translation to be added to the translation table causing a crash.

Since this is our first patch we tried following the wiki guidelines and broke the patch into a fix and test. More info about each is given in their respective commits. Any feedback is appreciated!

Signed off by: Percival Engineering <[email protected]>

Percival Foss (2):
accel/tcg: Fixed cross-page overflow for 32 bit guest tests/functional: Added cross page overflow test

 accel/tcg/cputlb.c                    |  7 +++
 tests/functional/meson.build          |  1 +
 tests/functional/test_ppc_pegasos2.py | 69 +++++++++++++++++++++++++++
 3 files changed, 77 insertions(+)
 create mode 100755 tests/functional/test_ppc_pegasos2.py
Signature · 1 line
2.45.3
#1 lore raw

PATCH v2 1/2 accel/tcg: Fixed cross-page overflow for 32 bit guest

From: Percival Foss <[email protected]>

The bug being resolved is that the current code in mmu_lookup() assumes a valid 64-bit address space. If a guest has a 32-bit address space, a page translation that crosses beyond the last page in the address space will overflow out of the allocated guest virtual memory space in the QEMU application and cause it to crash. In this case the first page will be the last of the 32-bit address space (for example 0xFFFFF000 for 4K page sizes) and the second page will overflow to a page beyond the 32-bit address space (0x100000000 in the very same example). An invalid translation will be added to the cpu translation table from the second page. Thought the translation will be for page address 0x100000000, checks in other parts of the codebase actually enforce using only 32 bits, and will match this translation. Part of the stored translation is the effective address, and another part is the addend to be used to offset into the QEMU process's virtual memory space. The addend will incorporate the 0x100000000 and offset into likely invalid virtual address space.

The fix in the diff checks if the target is 32 bits and wraps the second page address to the beginning of the memory space.

Signed off by: Percival Engineering <[email protected]>

---
 accel/tcg/cputlb.c | 7 +++++++
 1 file changed, 7 insertions(+)
accel/tcg/cputlb.c +7 -0
index fb22048876..457b3f8ec7 100644
@@ -1767,6 +1767,13 @@ static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
         l->page[1].size = l->page[0].size - size0;
         l->page[0].size = size0;
 
+        /* check for wrapping address space on page crossing if target is 32 bit */
+        #if TARGET_LONG_BITS == 32
+        if (l->page[1].addr >= (1UL << TARGET_LONG_BITS)) {
+            l->page[1].addr %= (1UL << TARGET_LONG_BITS);
+        }
+        # endif
+
         /*
          * Lookup both pages, recognizing exceptions from either.  If the
          * second lookup potentially resized, refresh first CPUTLBEntryFull.
Signature · 1 line
2.45.3
#2 lore raw

Re: [PATCH qemu v2 1/2] accel/tcg: Fixed cross-page overflow for 32 bit guest

Quoted 32 lines · ~percival_foss

From: Percival Foss <[email protected]>

The bug being resolved is that the current code in mmu_lookup() assumes a valid 64-bit address space. If a guest has a 32-bit address space, a page translation that crosses beyond the last page in the address space will overflow out of the allocated guest virtual memory space in the QEMU application and cause it to crash. In this case the first page will be the last of the 32-bit address space (for example 0xFFFFF000 for 4K page sizes) and the second page will overflow to a page beyond the 32-bit address space (0x100000000 in the very same example). An invalid translation will be added to the cpu translation table from the second page. Thought the translation will be for page address 0x100000000, checks in other parts of the codebase actually enforce using only 32 bits, and will match this translation. Part of the stored translation is the effective address, and another part is the addend to be used to offset into the QEMU process's virtual memory space. The addend will incorporate the 0x100000000 and offset into likely invalid virtual address space.

The fix in the diff checks if the target is 32 bits and wraps the second page address to the beginning of the memory space.

Signed off by: Percival Engineering <[email protected]>

---
  accel/tcg/cputlb.c | 7 +++++++
  1 file changed, 7 insertions(+)
accel/tcg/cputlb.c +0 -0
index fb22048876..457b3f8ec7 100644

accel/tcg/cputlb.c static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi, · line 1775

Quoted 9 lines
accel/tcg/cputlb.c +6 -0
@@ -1767,6 +1767,13 @@ static bool mmu_lookup(CPUState *cpu, vaddr addr, MemOpIdx oi,
          l->page[1].size = l->page[0].size - size0;
          l->page[0].size = size0;
 
+        /* check for wrapping address space on page crossing if target is 32 bit */
+        #if TARGET_LONG_BITS == 32
+        if (l->page[1].addr >= (1UL << TARGET_LONG_BITS)) {
+            l->page[1].addr %= (1UL << TARGET_LONG_BITS);
+        }
+        # endif

I agree something needs doing, but this isn't it.

This needs some sort of per-mmu mask, set when the cpu changes modes. For instance, you test ppc32, but the same thing can happen with ppc64 when MSR[SF] is clear.

There are a fair number of other targets which can see the same issue, some more complicated than this. For instance, s390x in 24-bit mode or RISC-V with the pointer masking extension.

This needs some sort of extension to CPUTLBDesc, and a new variation on tlb_flush_by_mmuidx in order for the target to clear the tlb while changing the mask whenever the target cpu changes modes.

r~

#3 lore raw

PATCH v2 2/2 tests/functional: Added cross page overflow test

In reply to ~percival_foss

From: Percival Foss <[email protected]>

This test utilizes pegasos2 to show the cross page overflow bug on 32 bit systems. We patch the firmware image to cause the overflow. The instructions to do this are as follows:
li r3, 0
li r4, -1
lwz r5, 0x0(r4)
lwz r5, 0x0(r3)

This test will add an invalid translation when ran and then crash QEMU. If the fix is applied then pegasos2 will start properly and not crash.

Signed off by:Percival Engineering <[email protected]>

---
 tests/functional/meson.build          |  1 +
 tests/functional/test_ppc_pegasos2.py | 69 +++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100755 tests/functional/test_ppc_pegasos2.py
tests/functional/meson.build +1 -0
index 0f8be30fe2..6641b878c3 100644
@@ -213,6 +213,7 @@ tests_ppc_system_thorough = [
   'ppc_bamboo',
   'ppc_mac',
   'ppc_mpc8544ds',
+  'ppc_pegasos2',
   'ppc_replay',
   'ppc_sam460ex',
   'ppc_tuxrun',
tests/functional/test_ppc_pegasos2.py new file +69 -0

69 lines not on this page · raw message

Signature · 1 line
2.45.3