Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Dennis Reußwig" <dennis@reusswig.codes>
Cc: kvm@vger.kernel.org
Subject: Re: [PATCH 04/10] KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit
Date: Fri, 04 Dec 2026 00:16:13 +0000	[thread overview]
Message-ID: <sashiko-outbox-248811@kernel.org> (raw)
In-Reply-To: <20261204-nested-sss-v1-4-57de06f58b2b@reusswig.codes>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] KVM: x86/mmu: Direct shadow page aliasing leaks EPT SSS
  permission
- [High] KVM: x86/mmu: omit shadow_mmu_writable_mask on SSS SPTEs skips
  TLB flushes
- [High] KVM: x86/mmu: SSS read faults livelock when make_spte() strips
  bit 60
- [Medium] KVM: x86/mmu: Unconditional second LOCK BTR in
  __rmap_clear_dirty()
--

commit 60a373f236eec5c8d97428ecb5498541f1b762a3
Author: Dennis Reußwig <dennis@reusswig.codes>

KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit

This commit introduces ACC_EPT_SSS_MASK and updates the x86 MMU to treat
EPT bit 60 (supervisor shadow-stack) like the writable bit during SPTE
creation, write-protection, dirty logging, and TLB flushing.

> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
> index f89e9f8a6cc2f..d2aa576f97cdf 100644
> --- a/arch/x86/kvm/mmu/mmu.c
> +++ b/arch/x86/kvm/mmu/mmu.c

[ ... ]

> @@ -697,7 +697,12 @@ static void kvm_mmu_page_set_translation(struct kvm_mmu_page *sp, int index,
>  		return;
>  	}
>  
> -	WARN_ONCE(access != kvm_mmu_page_get_access(sp, index),
> +	/*
> +	 * role.access cannot hold ACC_EPT_SSS_MASK.  Direct SPs that shadow a
> +	 * guest huge page carry the supervisor shadow-stack grant only in
> +	 * their SPTEs, which inherit it on splits.
> +	 */
> +	WARN_ONCE((access & ~ACC_EPT_SSS_MASK) != kvm_mmu_page_get_access(sp, index),
>  	          "access mismatch under %s page %llx (expected %u, got %u)\n",
>  	          sp->role.passthrough ? "passthrough" : "direct",
>  	          sp->gfn, kvm_mmu_page_get_access(sp, index), access);

[Severity: High]
Can excluding ACC_EPT_SSS_MASK from role.access cause direct shadow pages to
alias across SSS=1 and SSS=0 guest huge pages?

When L1 maps the same 2MiB or 1GiB GFN at two L2 GPAs with matching R/W/X/XU
bits, one with VMX_EPT_SSS_MASK set and one with it clear,
shadow_mmu_get_sp_for_split() passes access to kvm_mmu_child_role(), where
role.access = access truncates bit 4.  Both huge pages then hash to the same
(gfn, role.word) key and share a single direct struct kvm_mmu_page.

If the SSS=1 huge page is split first, shadow_mmu_split_huge_page() populates
sp->spt[] with VMX_EPT_SSS_MASK set; splitting the SSS=0 huge page reuses
that SP, skips the already-present SPTEs, and links them under the SSS=0
parent.  L2 then gets supervisor shadow-stack write access on an L2 GPA where
L1 cleared bit 60.

Separately, direct_pte_prefetch_many() reads only sp->role.access, so
prefetching sibling 4KiB SPTEs under a direct SP shadowing an SSS=1 huge page
silently drops ACC_EPT_SSS_MASK.

Storing the SSS permission in union kvm_mmu_page_role (which still has spare
padding bits) would keep SSS=1 and SSS=0 direct SPs distinct and preserve the
bit during prefetch.

[ ... ]

> @@ -1424,11 +1429,14 @@ static bool __rmap_clear_dirty(struct kvm *kvm, struct kvm_rmap_head *rmap_head,
>  	bool flush = false;
>  
>  	for_each_rmap_spte(rmap_head, &iter, sptep) {
> -		if (spte_ad_need_write_protect(*sptep))
> +		if (spte_ad_need_write_protect(*sptep)) {
>  			flush |= test_and_clear_bit(PT_WRITABLE_SHIFT,
>  						    (unsigned long *)sptep);
> -		else
> +			flush |= test_and_clear_bit(SPTE_EPT_SSS_SHIFT,
> +						    (unsigned long *)sptep);
> +		} else {
>  			flush |= spte_clear_dirty(sptep);
> +		}
>  	}
>  
>  	return flush;

[Severity: Medium]
Does this unconditionally issue a second locked atomic RMW on every
write-protect-mode SPTE even when VMX_EPT_SSS_MASK is clear?

__rmap_clear_dirty() runs under exclusive write_lock(&kvm->mmu_lock) across
every 4KiB SPTE in a memslot during dirty-logging setup and dirty-log
clearing (kvm_mmu_slot_leaf_clear_dirty() and
kvm_mmu_clear_dirty_pt_masked()).  Doubling the serializing bit-clear
operations per SPTE extends mmu_lock hold time and stalls concurrent vCPU
page faults even when EPT SSS is unused.

Since VMX_EPT_SSS_MASK is never set by hardware or locklessly in
fast_page_fault(), loading spte = *sptep once and guarding the second
test_and_clear_bit() with is_sss_spte(spte) avoids the extra atomic on
non-SSS SPTEs.

[ ... ]

> diff --git a/arch/x86/kvm/mmu/spte.c b/arch/x86/kvm/mmu/spte.c
> index 5fc27e9733b38..5251d03841815 100644
> --- a/arch/x86/kvm/mmu/spte.c
> +++ b/arch/x86/kvm/mmu/spte.c

[ ... ]

> @@ -249,14 +251,18 @@ bool make_spte(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
>  	if (host_writable)
>  		spte |= shadow_host_writable_mask;
>  	else
> -		pte_access &= ~ACC_WRITE_MASK;
> +		pte_access &= ~(ACC_WRITE_MASK | ACC_EPT_SSS_MASK);

[Severity: High]
Can clearing ACC_EPT_SSS_MASK when !host_writable livelock L2 on supervisor
shadow-stack read faults?

With EPTP bit 7 set, EPT leaf bit 60 is required for supervisor shadow-stack
reads (such as RET or RSTORSSP), which raise an EPT violation with
EPT_VIOLATION_SHADOW_STACK set and EPT_VIOLATION_ACC_WRITE clear.
__vt_handle_ept_violation() leaves fault->write false, so
__kvm_mmu_faultin_pfn() omits FOLL_WRITE and fails to break COW/KSM/zero
pages.  make_spte() then strips ACC_EPT_SSS_MASK and installs an SPTE without
bit 60, causing L2 to re-fault forever.

The same livelock happens on a write-tracked GFN when
mmu_try_to_unsync_pages() fails and make_spte() sets wrprot = true: both
page_fault_handle_page_track() and mmu_set_spte() gate on fault->write, so
KVM returns RET_PF_FIXED or RET_PF_SPURIOUS instead of
RET_PF_WRITE_PROTECTED.

[ ... ]

> -		if ((!is_last_spte(old_spte, level) || !is_writable_pte(old_spte)) &&
> -		    mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, synchronizing, prefetch))
> +		if ((!is_last_spte(old_spte, level) ||
> +		     !(is_writable_pte(old_spte) ||
> +		       (is_shadow_present_pte(old_spte) && is_sss_spte(old_spte)))) &&
> +		    mmu_try_to_unsync_pages(vcpu->kvm, slot, gfn, synchronizing, prefetch)) {
>  			wrprot = true;
> -		else
> -			spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask |
> -				shadow_dirty_mask;
> +		} else {
> +			if (pte_access & ACC_WRITE_MASK)
> +				spte |= PT_WRITABLE_MASK | shadow_mmu_writable_mask;
> +			if (pte_access & ACC_EPT_SSS_MASK)
> +				spte |= VMX_EPT_SSS_MASK;
> +
> +			spte |= shadow_dirty_mask;
> +		}
>  	}

[Severity: High]
Can omitting shadow_mmu_writable_mask on SSS-only SPTEs cause
write-protection to skip a required remote TLB flush?

For a read-only supervisor shadow-stack leaf (ACC_EPT_SSS_MASK without
ACC_WRITE_MASK), make_spte() sets VMX_EPT_SSS_MASK while leaving
shadow_mmu_writable_mask clear.  Dirty-logging paths clear VMX_EPT_SSS_MASK
under mmu_lock and drop the lock before flushing remote TLBs:

CPU0 (dirty logging)              CPU1 (pt_protect)
kvm_mmu_slot_apply_flags()
  kvm_mmu_slot_leaf_clear_dirty()
    write_lock(&kvm->mmu_lock)
    __rmap_clear_dirty()
      clear VMX_EPT_SSS_MASK
    write_unlock(&kvm->mmu_lock)
                                  account_shadowed()
                                    write_lock(&kvm->mmu_lock)
                                    spte_write_protect(..., true)
                                      returns false  <-- skips TLB flush
                                    write_unlock(&kvm->mmu_lock)
  kvm_flush_remote_tlbs_memslot()

Because PT_WRITABLE_MASK, VMX_EPT_SSS_MASK, and shadow_mmu_writable_mask are
all clear in that window, spte_write_protect() returns false and
account_shadowed() skips kvm_flush_remote_tlbs_gfn().  A remote vCPU can
still hold a stale TLB entry with bit 60 set and write directly into a
shadowed guest page table via shadow-stack accesses.

FNAME(sync_spte) -> leaf_spte_change_needs_tlb_flush() misses the flush in
the same window, and check_spte_writable_invariants() does not check that
is_sss_spte() entries are MMU-writable and host-writable.

Setting shadow_mmu_writable_mask whenever ACC_EPT_SSS_MASK is granted
preserves KVM's MMU-writable TLB flush invariant.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-nested-sss-v1-0-57de06f58b2b@reusswig.codes?part=4

  reply	other threads:[~2026-12-04  0:16 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 23:49 [PATCH 00/10] KVM: nVMX: Supervisor shadow-stack control for nested EPT and CET with eVMCS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 01/10] KVM: nVMX: Allow CET entry/exit controls with enlightened VMCS Dennis Reußwig
2026-12-03 23:49 ` [PATCH 02/10] KVM: x86/mmu: Move TDP A/D tracking type to SPTE bits 56-57 Dennis Reußwig
2026-12-03 23:49 ` [PATCH 03/10] KVM: VMX: Report shadow-stack accesses in EPT violations as PFERR_SS Dennis Reußwig
2026-12-03 23:59   ` sashiko-bot
2026-12-03 23:49 ` [PATCH 04/10] KVM: x86/mmu: Handle the EPT supervisor shadow-stack bit like a write bit Dennis Reußwig
2026-12-04  0:16   ` sashiko-bot [this message]
2026-12-03 23:49 ` [PATCH 05/10] KVM: nVMX: Shadow supervisor shadow-stack control of nested EPT Dennis Reußwig
2026-12-04  0:17   ` sashiko-bot
2026-12-03 23:49 ` [PATCH 06/10] KVM: nVMX: Advertise supervisor shadow-stack control for " Dennis Reußwig
2026-12-04  0:13   ` sashiko-bot
2026-12-03 23:49 ` [PATCH 07/10] KVM: selftests: Add helpers for nested EPT shadow-stack and MBEC tests Dennis Reußwig
2026-12-03 23:49 ` [PATCH 08/10] KVM: selftests: Add nested EPT supervisor shadow-stack test Dennis Reußwig
2026-12-04  0:00   ` sashiko-bot
2026-12-03 23:49 ` [PATCH 09/10] KVM: selftests: Add nested EPT mode-based execute control test Dennis Reußwig
2026-12-04  0:07   ` sashiko-bot
2026-12-03 23:49 ` [PATCH 10/10] KVM: selftests: Check VMX capabilities for CET in L2 with MBEC Dennis Reußwig
2026-12-04  0:10   ` sashiko-bot

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=sashiko-outbox-248811@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dennis@reusswig.codes \
    --cc=kvm@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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