[PATCH v1 0/3] target/i386/tcg: Fix 64-bit RSP truncation in far call/return

NickSampanis posted 3 patches 4 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261025231803.870-1-nicksampanis@gmail.com
Maintainers: Paolo Bonzini <pbonzini@redhat.com>, Richard Henderson <richard.henderson@linaro.org>
target/i386/tcg/seg_helper.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
[PATCH v1 0/3] target/i386/tcg: Fix 64-bit RSP truncation in far call/return
Posted by NickSampanis 4 weeks ago
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
Re: [PATCH v1 0/3] target/i386/tcg: Fix 64-bit RSP truncation in far call/return
Posted by Paolo Bonzini 1 week, 6 days ago
On 10/26/26 00:18, NickSampanis wrote:
> 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

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
Re: [PATCH v1 0/3] target/i386/tcg: Fix 64-bit RSP truncation in far call/return
Posted by NickSampanis 1 week, 3 days ago
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.
Re: [PATCH v1 0/3] target/i386/tcg: Fix 64-bit RSP truncation in far call/return
Posted by Sampanis 1 week, 6 days ago
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

On 11/9/2026 8:56 PM, Paolo Bonzini wrote:
> On 10/26/26 00:18, NickSampanis wrote:
>> 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
>
> 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