Skip to contentSkip to conversation

target/i386/tcg: Fix 64-bit RSP truncation in far call/return

PATCH In review NickSampanis 2 participants 7 messages Oct 25 – Nov 12

lore
Share & tools
lore
#0 lore raw

cover 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

#1 lore raw

PATCH 1/3 target/i386/tcg: Fix 64-bit RSP truncation on retf in 64-bit mode

When executing retf in 64-bit mode with a 16- or 32-bit operand size, helper_ret_protected() is invoked with a shift of 0 or 1. While in long mode the stack uses the full RSP and the SS attributes are ignored. This caused the code to fall back to get_sp_mask(SS.flags). With SS.B clear, this applied a 16-bit mask (0xffff); with SS.B set, it applied a 32-bit mask (0xffffffff).

As a result, a 64-bit RSP pointing above 4 GiB was truncated. As observed in testing, the truncated stack pop read the wrong address, loading corrupted values for the new CS and RIP which resulted in a #GP fault and then a triple fault.

Fix this by ensuring that helper_ret_protected() uses the full 64-bit stack pointer with no mask applied whenever the active code segment (CS) is running in 64-bit mode.

---
 target/i386/tcg/seg_helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
target/i386/tcg/seg_helper.c +1 -1
index 45bffcd738..af18aec694 100644
@@ -2056,7 +2056,7 @@ static inline void helper_ret_protected(CPUX86State *env, int shift,
     sa.mmu_index = x86_mmu_index_pl(env, cpl);
 
 #ifdef TARGET_X86_64
-    if (shift == 2) {
+    if (shift == 2 || (env->hflags & HF_CS64_MASK)) {
         sa.sp_mask = -1;
     } else
 #endif
Signature · 1 line
2.51.0.windows.2
#2 lore raw

PATCH 2/3 target/i386/tcg: Fix 64-bit RSP truncation on far call in 64-bit mode

In reply to NickSampanis

When executing a direct far call to a code segment (no call gate involved) in 64-bit mode with a 16- or 32-bit operand size, lcall_protected falls back to get_sp_mask(SS.flags). This gives a 16- or 32-bit mask, but in 64-bit mode the stack always uses the full RSP. As a result, the pushes of the return address and the stack pointer update (SET_ESP) truncate RSP. This is the same bug fixed for retf in the previous patch.

Use the full stack pointer, with no mask, for a direct far call when CS is 64-bit.

---
 target/i386/tcg/seg_helper.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/i386/tcg/seg_helper.c +5 -1
index af18aec694..87492a2f64 100644
@@ -1731,7 +1731,11 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
 #endif
         {
             sa.sp = env->regs[R_ESP];
-            sa.sp_mask = get_sp_mask(env->segs[R_SS].flags);
+            if (env->hflags & HF_CS64_MASK) {
+                sa.sp_mask = -1;
+            } else {
+                sa.sp_mask = get_sp_mask(env->segs[R_SS].flags);
+            }
             sa.ss_base = env->segs[R_SS].base;
             if (shift) {
                 pushl(&sa, env->segs[R_CS].selector);
Signature · 1 line
2.51.0.windows.2
#3 lore raw

PATCH 3/3 target/i386/tcg: Fix 64-bit RSP truncation on far call through a call gate

In reply to NickSampanis

In long mode, a call gate is 16 bytes and always has a 64-bit target. For a same-privilege call through a call gate, which can also be reached by a compatibility-mode caller, lcall_protected uses get_sp_mask(SS.flags) even though the pushes are 8 bytes. Consequently, the pushes and the stack pointer update (SET_ESP) truncate RSP. This is the same bug as in the previous two patches.

Use the full stack pointer when shift == 2, the same as the inner-privilege branch. This condition tests the gate's own size rather than the caller's mode, which is why it checks shift == 2 instead of HF_CS64_MASK.

---
 target/i386/tcg/seg_helper.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
target/i386/tcg/seg_helper.c +6 -1
index 87492a2f64..f11b55d136 100644
@@ -1925,7 +1925,12 @@ void HELPER(lcall_protected)(CPUX86State *env, int new_cs, target_ulong new_eip,
             /* to same privilege */
             sa.mmu_index = x86_mmu_index_pl(env, cpl);
             sa.sp = env->regs[R_ESP];
-            sa.sp_mask = get_sp_mask(env->segs[R_SS].flags);
+#ifdef TARGET_X86_64
+            if (shift == 2) {
+                sa.sp_mask = -1;
+            } else
+#endif
+                sa.sp_mask = get_sp_mask(env->segs[R_SS].flags);
             sa.ss_base = env->segs[R_SS].base;
             /* push_size = (4 << shift); */
             new_stack = 0;
Signature · 1 line
2.51.0.windows.2
#4 lore raw
In reply to NickSampanis
Quoted 31 lines · NickSampanis

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:

Quoted 8 lines

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

Interesting. And really complicated... The gist is that in 64-bit mode, the operation size only governs how many bytes are popped, not the stack size. And for far CALL, in addition, your patch only touches the same-stack case.

Is that correct?

Paolo

author
#5 lore raw

Hi Paolo,

That is correct.

On RETF(with 32bit operand) when we are in long mode(LMA=1), the operand size(32) should be popped from the RSP even if it points at a 64bit address. Furthermore, get_sp_mask() tries to calculate the mask of RSP, based on attributes of SS segment, which is incorrect when we are in long mode(AMD manual 4.5.3). Maybe the fix should be the correct calculation of sp_mask in get_sp_mask() while we are in long mode. I've seen the use of setting sp_mask equal to -1 in the same function, when it does if (env->hflags & HF_CS64_MASK) sa.sp_mask = -1;  before calling SET_ESP(sa.sp, sa.sp_mask);. And I thought that I should just use that.

The other two bugs have the same root cause.

Best,

Nick

Quoted 48 lines · Paolo Bonzini
Quoted 33 lines · NickSampanis

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:

Quoted 8 lines

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

Interesting.  And really complicated...  The gist is that in 64-bit mode, the operation size only governs how many bytes are popped, not the stack size.  And for far CALL, in addition, your patch only touches the same-stack case.

Is that correct?

Paolo

#6 lore raw
In reply to Paolo Bonzini

I answered the previous email in a hurry. Now that I've had time to review it, I realize I didn't answer the question about stack switching.

The only case where we switch stacks is through a call gate to a more privileged level. When we are doing this with EFER.LMA=1, lcall can have any operand size (16, 32, or 64), but the CPU ignores it when going through a gate. Because the gate is 64bit, the pushes are always 8 bytes, and there is no path that pushes onto a 16bit or 32bit stack. With LMA=1, only type 12 is accepted and read as a 64bit gate, whereas a 16-bit (type 4) gate raises a #GP fault.
https://github.com/qemu/qemu/blob/d73149283205/target/i386/tcg/seg_helper.c#L1762

As a result the the path that is switching stack(fetched from TSS) was already correct and there is no case that we can execute the other path with LMA=1.
https://github.com/qemu/qemu/blob/d73149283205/target/i386/tcg/seg_helper.c#L1849

Of course the other path of call gate is the same privilege path, that is using get_sp_mask(), without checking if the shift is 2
or LMA=1. That again ignores correct stack mask for long and compatibility mode (Vol. 2A, the SAME-PRIVILEGE, CallGateSize = 64 branch).
https://github.com/qemu/qemu/blob/d73149283205/target/i386/tcg/seg_helper.c#L1924
That edge case makes the suggestion that I made of just using get_sp_mask with SS, CS, LMA as arguments, really difficult. Because in RETF case we wanted the truncation not to happen only when LMA=1 and CS.L=1, while in LCALL gate case we want the truncation no to happen when LMA=1 regardless of CS.