From: sashiko-bot@kernel.org
To: "David Woodhouse" <dwmw2@infradead.org>, kvm@vger.kernel.org
Cc: linux-hyperv@vger.kernel.org
Subject: Re: [PATCH v3 10/15] KVM: x86: Filter emulated CR0 and CR4 writes
Date: Fri, 27 Nov 2026 15:48:43 +0000 [thread overview]
Message-ID: <sashiko-outbox-237783@kernel.org> (raw)
In-Reply-To: <20261127152730.99319-11-dwmw2@infradead.org>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] KVM: x86: 32-bit truncation of CR0/CR4 writes in
emulator_filter_cr_write()
--
commit 08515d18b5f74a7d7b3ddcb78d650f9907df852f
Author: David Woodhouse <dwmw@amazon.co.uk>
KVM: x86: Filter emulated CR0 and CR4 writes
This commit adds register write filter checks to the x86 emulator's MOV to CR,
CLTS, and LMSW instruction handlers so filtered CR0 and CR4 writes exit to
userspace with KVM_EXIT_X86_REG_WRITE.
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index b559c7880e546..4e6f0cb80288c 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -5846,6 +5846,42 @@ static int emulator_filter_xcr_write(struct x86_emulate_ctxt *ctxt, u32 index,
> vcpu->arch.xcr0, xcr);
> }
>
> +/*
> + * Divert an emulated CR0/CR4 write into a KVM_EXIT_X86_REG_WRITE exit when
> + * it hits a write filter. Called by the instruction handlers (MOV to CR,
> + * CLTS, LMSW) before any architectural effect; state loads that reach
> + * ->set_cr() on other instructions' behalf (RSM, task switches) are not
> + * guest register writes and stay unfiltered.
> + */
> +static int emulator_filter_cr_write(struct x86_emulate_ctxt *ctxt, int cr,
> + ulong val)
> +{
> + struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
> + u64 old_value, new_value;
> + u32 reg;
> +
> + switch (cr) {
> + case 0:
> + reg = KVM_X86_REG_FILTER_CR0;
> + old_value = kvm_read_cr0(vcpu);
> + break;
> + case 4:
> + reg = KVM_X86_REG_FILTER_CR4;
> + old_value = kvm_read_cr4(vcpu);
> + break;
> + default:
> + return X86EMUL_CONTINUE;
> + }
> +
> + /* Match the merge emulator_set_cr() will do with the same value. */
> + new_value = mk_cr_64(old_value, val);
> +
> + if (!kvm_reg_write_filtered(vcpu, reg, old_value, new_value))
> + return X86EMUL_CONTINUE;
> +
> + return emulator_reg_write_exit(ctxt, reg, old_value, new_value);
> +}
[Severity: Medium]
Does calling mk_cr_64() here truncate the upper 32 bits of val on 64-bit
MOV to CR0/CR4?
arch/x86/kvm/x86.c:mk_cr_64() {
static u64 mk_cr_64(u64 curr_cr, u32 new_val)
{
return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
}
}
Because mk_cr_64() takes a u32 new_val, passing the 64-bit operand val
discards bits 63:32 and replaces them with old_value[63:32] (which are zero
for CR0 and CR4). When a 64-bit guest executes an emulated MOV to CR0 or
CR4 with any upper bits set, kvm_reg_write_filtered() cannot match filters
on bits 63:32, and vcpu->run->reg_write.new_value strips fault-inducing
reserved upper bits that userspace needs to see in order to reject the
write with KVM_INSN_GP.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127152730.99319-1-dwmw2@infradead.org?part=10
next prev parent reply other threads:[~2026-11-27 15:48 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 15:27 [PATCH v3 00/15] KVM: x86: Exit handling for Hyper-V hcalls, register intercept David Woodhouse
2026-11-27 15:27 ` [PATCH v3 01/15] KVM: selftests: Make the x86 SSE register helpers volatile David Woodhouse
2026-11-27 15:27 ` [PATCH v3 02/15] KVM: Define what completing an exit does with its instruction David Woodhouse
2026-11-27 15:55 ` sashiko-bot
2026-11-27 15:27 ` [PATCH v3 03/15] KVM: x86: Let userspace leave an MSR access alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 04/15] KVM: selftests: Test MSR accesses which userspace leaves alone David Woodhouse
2026-11-27 15:27 ` [PATCH v3 05/15] KVM: x86: hyper-v: Add KVM_CAP_HYPERV_FORWARD_HCALL David Woodhouse
2026-11-27 15:27 ` [PATCH v3 06/15] KVM: x86: hyper-v: Let userspace leave a hypercall uncompleted David Woodhouse
2026-11-27 15:27 ` [PATCH v3 07/15] KVM: x86: hyper-v: Advertise XMM fast hypercall output David Woodhouse
2026-11-27 15:27 ` [PATCH v3 08/15] KVM: selftests: Add Hyper-V hypercall forwarding test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 09/15] KVM: x86: Add a per-vCPU register write filter David Woodhouse
2026-11-27 15:27 ` [PATCH v3 10/15] KVM: x86: Filter emulated CR0 and CR4 writes David Woodhouse
2026-11-27 15:48 ` sashiko-bot [this message]
2026-11-27 15:27 ` [PATCH v3 11/15] KVM: VMX: Support CR0 and CR4 write filters David Woodhouse
2026-11-27 15:27 ` [PATCH v3 12/15] KVM: SVM: " David Woodhouse
2026-11-27 15:49 ` sashiko-bot
2026-11-27 15:27 ` [PATCH v3 13/15] KVM: x86: Add descriptor-table registers to the write filter David Woodhouse
2026-11-27 15:52 ` sashiko-bot
2026-11-27 15:27 ` [PATCH v3 14/15] KVM: selftests: Add x86 register write filter test David Woodhouse
2026-11-27 15:27 ` [PATCH v3 15/15] KVM: selftests: Test the register write filter with a nested guest David Woodhouse
2026-11-27 15:47 ` 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-237783@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=dwmw2@infradead.org \
--cc=kvm@vger.kernel.org \
--cc=linux-hyperv@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