From: Chao Gao <chao.gao@intel.com>
To: Sean Christopherson <seanjc@google.com>
Cc: Mathias Krause <minipli@grsecurity.net>,
Paolo Bonzini <pbonzini@redhat.com>, <kvm@vger.kernel.org>
Subject: Re: [kvm-unit-tests PATCH v2 13/13] x86: Avoid top-most page for vmalloc on x86-64
Date: Sun, 14 Sep 2025 19:05:34 +0800 [thread overview]
Message-ID: <aMahfvF1r39Xq6zK@intel.com> (raw)
In-Reply-To: <aMQwQnGpNzutdr-q@google.com>
On Fri, Sep 12, 2025 at 07:37:54AM -0700, Sean Christopherson wrote:
>On Thu, Jun 26, 2025, Mathias Krause wrote:
>> The x86-64 implementation if setup_mmu() doesn't initialize 'vfree_top'
>> and leaves it at its zero-value. This isn't wrong per se, however, it
>> leads to odd configurations when the first vmalloc/vmap page gets
>> allocated. It'll be the very last page in the virtual address space --
>> which is an interesting corner case -- but its boundary will probably
>> wrap. It does so, for CET's shadow stack, at least, which loads the
>> shadow stack pointer with the base address of the mapped page plus its
>> size, i.e. 0xffffffff_fffff000 + 4096, which wraps to 0x0.
>>
>> The CPU seems to handle such configurations just fine. However, it feels
>> odd to set the shadow stack pointer to "NULL".
>>
>> To avoid the wrapping, ignore the top most page by initializing
>> 'vfree_top' to just one page below.
>>
>> Reviewed-by: Chao Gao <chao.gao@intel.com>
>> Signed-off-by: Mathias Krause <minipli@grsecurity.net>
>> ---
>> v2:
>> - change comment in x86/lam.c too
>>
>> lib/x86/vm.c | 2 ++
>> x86/lam.c | 10 +++++-----
>> 2 files changed, 7 insertions(+), 5 deletions(-)
>>
>> diff --git a/lib/x86/vm.c b/lib/x86/vm.c
>> index 90f73fbb2dfd..27e7bb4004ef 100644
>> --- a/lib/x86/vm.c
>> +++ b/lib/x86/vm.c
>> @@ -191,6 +191,8 @@ void *setup_mmu(phys_addr_t end_of_memory, void *opt_mask)
>> end_of_memory = (1ul << 32); /* map mmio 1:1 */
>>
>> setup_mmu_range(cr3, 0, end_of_memory);
>> + /* skip the last page for out-of-bound and wrap-around reasons */
>> + init_alloc_vpage((void *)(~(PAGE_SIZE - 1)));
>
>This breaks the eventinj test (leads to SHUTDOWN). I haven't spent any time
>trying to figure out why.
do_iret in eventinj.c is buggy. When crafting an IRET frame, it just sets
RFLAGS, CS and RIP properly, leaving SS and RSP as 0. So after IRET, RSP
becomes 0. Then, a nested NMI is injected right after IRET and accesses the
stack. If the stack (i.e., the page starts from 0xffffffff_fffff000) isn't
mapped, the NMI will cause TRIPLE FAULT.
The issue goes unnoticed because w/o this patch, the handle_irq()->malloc() at
the beginning of eventinj.c:main() allocates and maps the page 0xffffffff_fffff000
The issue can be fixed with below diff:
index 6fbb2d0f..945f7d11 100644
--- a/x86/eventinj.c
+++ b/x86/eventinj.c
@@ -132,7 +132,7 @@ static void nested_nmi_iret_isr(struct ex_regs *r)
{
printf("Nested NMI isr running rip=%lx\n", r->rip);
- if (r->rip == iret_stack[-3])
+ if (r->rip == iret_stack[-4])
test_count++;
}
@@ -152,6 +152,7 @@ asm("do_iret:"
"mov 8(%esp), %edx \n\t" // virt_stack
#endif
"xchg %"R "dx, %"R "sp \n\t" // point to new stack
+ "push"W" %"R "dx \n\t"
"pushf"W" \n\t"
"mov %cs, %ecx \n\t"
"push"W" %"R "cx \n\t"
prev parent reply other threads:[~2025-09-14 11:05 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-06-26 7:34 [kvm-unit-tests PATCH v2 00/13] Improve CET tests Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 01/13] x86: cet: Pass virtual addresses to invlpg Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 02/13] x86: cet: Remove unnecessary memory zeroing for shadow stack Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 03/13] x86: cet: Directly check for #CP exception in run_in_user() Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 04/13] x86: cet: Validate #CP error code Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 05/13] x86: cet: Use report_skip() Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 06/13] x86: cet: Drop unnecessary casting Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 07/13] x86: cet: Validate writing unaligned values to SSP MSR causes #GP Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 08/13] x86: cet: Validate CET states during VMX transitions Mathias Krause
2025-07-11 8:51 ` Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 09/13] x86: cet: Make shadow stack less fragile Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 10/13] x86: cet: Simplify IBT test Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 11/13] x86: cet: Use symbolic values for the #CP error codes Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 12/13] x86: cet: Test far returns too Mathias Krause
2025-06-26 7:34 ` [kvm-unit-tests PATCH v2 13/13] x86: Avoid top-most page for vmalloc on x86-64 Mathias Krause
2025-09-12 14:37 ` Sean Christopherson
2025-09-14 11:05 ` Chao Gao [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aMahfvF1r39Xq6zK@intel.com \
--to=chao.gao@intel.com \
--cc=kvm@vger.kernel.org \
--cc=minipli@grsecurity.net \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox