target/i386/tcg: Fix 64-bit RSP truncation in far call/return
Get series with b4
b4 am [email protected]Versions newest first
- v1 Latest3 patches 3 repliesIn review
Patches in v1 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/3 | target/i386/tcg: Fix 64-bit RSP truncation on retf in 64-bit modetarget/ | +1 | 0 replies | |
| 2/3 | target/i386/tcg: Fix 64-bit RSP truncation on far call in 64-bit modetarget/ | +5 | 0 replies | |
| 3/3 | target/i386/tcg: Fix 64-bit RSP truncation on far call through a call gatetarget/ | +6 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH v1 0/3] target/i386/tcg: Fix 64-bit RSP truncation in far call/return
Found the bug while debugging a kernel using qemu-tcg where a retf with a 32-bit operand size executed at an RSP above 4 GiB in long mode, leading to a triple fault. This led me to investigate the retf handler of qemu-tcg, and subsequently find two additional bugs in lcall.
This series fixes three distinct cases where 64-bit stack pointers (RSP) pointing above 4 GiB were incorrectly truncated to 16-bit or 32-bit values during far control-flow instructions in 64-bit/long mode:
target/i386/tcg: Fix 64-bit RSP truncation on retf in 64-bit mode target/i386/tcg: Fix 64-bit RSP truncation on far call in 64-bit mode target/i386/tcg: Fix 64-bit RSP truncation on far call through a call gate
For the first two patches, I used the HF_CS64_MASK flag of env->hflags based on the similar check in the retf handler that sets the sp_mask to -1.
https://github.com/qemu/qemu/blob/d73149283205/target/i386/tcg/seg_helper.c#L2207
For the third patch (far call through a call gate), patch 3 checks shift == 2 instead, since the call-gate path increments shift under EFER.LMA, so shift == 2 for any valid 64-bit gate regardless of the caller's mode.
https://github.com/qemu/qemu/blob/d73149283205/target/i386/tcg/seg_helper.c#L1834
Proof-of-Concept & Testing:
I wrote a proof of concept for each bug. Each PoC is a 512-byte boot sector that sets up a basic 4-level long mode page table mapping the first 2 MiB of memory 1:1, maps a 2 MiB page at the 4 GiB virtual address, points RSP above 4 GiB, and executes the instruction. The PoC source files are available on GitHub Gist:
1) 64-bit RSP truncation on retf in 64-bit mode PoC
https://gist.github.com/NickSampanis/3d67093b5b99a5e9627912e2f8f4b297
2) 64-bit RSP truncation on far call in 64-bit mode PoC
https://gist.github.com/NickSampanis/d4dea9948a34fbdedeed91d77586ff05
3) 64-bit RSP truncation on far call through a call gate PoC
https://gist.github.com/NickSampanis/7e9c6a9c994ef67a1aea2b13814be441
Note that these PoCs are not part of the patch series.
Build:
as --64 -o poc.o poc.S
objcopy -O binary -j .text poc.o poc.img
Run:
qemu-system-x86_64 -accel tcg -drive format=raw,file=poc.img -d int,cpu -debugcon stdio -display none -no-reboot
Output Before the Fix:
The truncated stack pop or push reads/writes the wrong address, loading corrupted values into CS and RIP, which results in a #GP shown in -d int, followed immediately by a triple fault.
Output After the Fix:
The instruction executes using the full 64-bit RSP without any #GP or triple fault.
Tested on TCG only. This series does not include an in-tree test yet, but I can add a test to tests/tcg/x86_64/system.
NickSampanis (3):
target/i386/tcg: Fix 64-bit RSP truncation on retf in 64-bit mode target/i386/tcg: Fix 64-bit RSP truncation on far call in 64-bit mode target/i386/tcg: Fix 64-bit RSP truncation on far call through a call gate
target/i386/tcg/seg_helper.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-)
--
2.51.0.windows.2