From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D48837E31F for ; Mon, 23 Feb 2026 23:15:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771888521; cv=none; b=jw5O4JEGLaRTJprdzFhXar2EEaJzr0xkcPCSTqrIXbZm4w4f6cxrfx/uvlrjBUg2sR22sKw2egYOGzBsr2Z+aVKygDpRFJ98IgWgCEt5ItQ9h/xA3hEwjknkgFiTMWI5GVVmNtkOZuTNCCUW3/y9G+3krlvmc+V2UC0vV48hX+U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771888521; c=relaxed/simple; bh=B9rEHO/Alu+wvKNgWIpdOvdWR+itIxpOX4WOHVwRT4c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FVTbgpUZVyu7xczdOzuSMWNNa/AWBZlFioHC4EsmeFyWndzOUJKMJLoMiqz0n4TjZ8KrisTRxjSSM7hCsVDR9pLgqSL6Oon3hS0FZhkxNPsVaC1ePWKYNxfc7lybcoAKgn1M4mHdQNMFtLdN8np7+emLSBwjkk7ajMplegbYFpg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=S7vrsr8x; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="S7vrsr8x" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-35301003062so37329032a91.2 for ; Mon, 23 Feb 2026 15:15:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771888519; x=1772493319; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+b2NA5qyQrO16m+c2pRd+nTlMHqOSFWpDC57i2Qp01k=; b=S7vrsr8xHyShFbhghyIfbu7QpMTWI08iA1X5M5KmdF0Y6su11MNUtlwJyPMAxQXST6 35xr+gNeXziFOQKBTUbHo/OHgt9rKqdmNX1RfIRdozNk9U5zQGQ/ueM5pj/ExsOcdGUr mq5W2VvhKMjvN5Lj8g9JXepJHNUCl4rPy0xRw/5632QyHJWYBHEaspEWe0vsmdCAYaYT LW7Bl6vM6OGJhokYLU7qIwskXAyrVWdp9annTjaoJSCmQ6RtxFLIb2kY8HS1v1ESguKB 5z3eqlt4JVWhtxsoVt2TVVOx3PkIDKLi2SIHEfVtdKlkshs6UTfUPhiS3upuWnmYiqhJ IsZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771888519; x=1772493319; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+b2NA5qyQrO16m+c2pRd+nTlMHqOSFWpDC57i2Qp01k=; b=R0afKl99d0fIg2mV4B4LATfzBV47wIezENAFADbdMPNvhhG8aqy9YKX6VvhexcNaOI thN9tGgTiS15vOAHHVkkoaQOX1ZCaDwVIiCWlVs4h9UX1B5cMTkd+37i0hYqCtsCGJb1 2KsvRjt8OPU723l/y7Lm3JcQJeVFFDCQSlNLhAjytB4r9kCcUVX6fzrkDNjFPVKf55Db kvw8lL7aBGWkf0+e6cSSaFquCoiVGYoWK7X7raEIwN4yhV4LNq+qmREH9z1ivZPMayor INSBv6F0eF/xMqHO1Ed+jP+KGPh//t4xoMfGzJj6gnDc1pPnikGFv+5+zajYCmwJ1TAy Tc2Q== X-Forwarded-Encrypted: i=1; AJvYcCXzh5Zr/4v0pdl6htPOSgvUsEbxGtxmAPkDh+tk5VsYDeA8AP/nLOKUX0RVU+e0J1WiXfPThDG11muJubc=@vger.kernel.org X-Gm-Message-State: AOJu0YyR9GA8/xgxasCdEInoFcKfSTls7evQN7F7XUwIX1D9GjQrFHe8 coclPR1AGV9JWm8jni4jUKDlyWFm43UBbCkD/OJ6XFfIcVmRAHVxMIyY+Ok2HAWbZeF/XWFkYPk a4iwRYQ== X-Received: from pjug8.prod.google.com ([2002:a17:90a:ce88:b0:354:7c11:76e1]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3d50:b0:356:2eac:b650 with SMTP id 98e67ed59e1d1-358ae7c3731mr8524476a91.3.1771888518929; Mon, 23 Feb 2026 15:15:18 -0800 (PST) Date: Mon, 23 Feb 2026 15:15:17 -0800 In-Reply-To: <20260206190851.860662-26-yosry.ahmed@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260206190851.860662-1-yosry.ahmed@linux.dev> <20260206190851.860662-26-yosry.ahmed@linux.dev> Message-ID: Subject: Re: [PATCH v5 25/26] KVM: nSVM: Sanitize control fields copied from VMCB12 From: Sean Christopherson To: Yosry Ahmed Cc: Paolo Bonzini , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson Content-Type: text/plain; charset="us-ascii" On Fri, Feb 06, 2026, Yosry Ahmed wrote: > Make sure all fields used from VMCB12 in creating the VMCB02 are > sanitized, such that no unhandled or reserved bits end up in the VMCB02. > > The following control fields are read from VMCB12 and have bits that are > either reserved or not handled/advertised by KVM: tlb_ctl, int_ctl, > int_state, int_vector, event_inj, misc_ctl, and misc_ctl2. > > The following fields do not require any extra sanitizing: > - int_ctl: bits from VMCB12 are copied bit-by-bit as needed. > - misc_ctl: only used in consistency checks (particularly NP_ENABLE). > - misc_ctl2: bits from VMCB12 are copied bit-by-bit as needed. > > For the remaining fields, make sure only defined bits are copied from > L1's VMCB12 into KVM'cache by defining appropriate masks where needed. > The only exception is tlb_ctl, which is unused, so remove it. > > Opportunistically cleanup ignoring the lower bits of {io/msr}pm_base_pa > in __nested_copy_vmcb_control_to_cache() by using PAGE_MASK. Also, move > the ASID copying ahead with other special cases, and expand the comment > about the ASID being copied only for consistency checks. Stop. Bundling. Changes. This is not a hypothetical situation, bundling small changes like this is quite literally making review take 3-4x longer than it should. The interrupt changes are trivial to review. The I/O and MSR bitmap changes are also easy enough, but I wanted to double that PAGE_MASK does indeed equal ~0x0fffULL (__PHYSICAL_MASK is the one that can be dynamic). I disagree the the tlb_ctl change. Moving the ASID handling is _completely_ superfluous. Combining any two of those is annoying to deal with. Combining all of them wastes a non-trivial amount of time. What should have taken me ~5 minutes to review is dragging into 20+ minutes, because I keep having to cross-reference the changelog with the code to understand WTF is going on. Just stop doing it, please. > Suggested-by: Jim Mattson > Signed-off-by: Yosry Ahmed > --- > arch/x86/include/asm/svm.h | 5 +++++ > arch/x86/kvm/svm/nested.c | 28 +++++++++++++++------------- > arch/x86/kvm/svm/svm.h | 1 - > 3 files changed, 20 insertions(+), 14 deletions(-) > > diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h > index c169256c415f..fe3b6d9cea31 100644 > --- a/arch/x86/include/asm/svm.h > +++ b/arch/x86/include/asm/svm.h > @@ -222,6 +222,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { > #define X2APIC_MODE_SHIFT 30 > #define X2APIC_MODE_MASK (1 << X2APIC_MODE_SHIFT) > > +#define SVM_INT_VECTOR_MASK GENMASK(7, 0) > + > #define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0) > #define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1) > > @@ -635,6 +637,9 @@ static inline void __unused_size_checks(void) > #define SVM_EVTINJ_VALID (1 << 31) > #define SVM_EVTINJ_VALID_ERR (1 << 11) > > +#define SVM_EVTINJ_RESERVED_BITS ~(SVM_EVTINJ_VEC_MASK | SVM_EVTINJ_TYPE_MASK | \ > + SVM_EVTINJ_VALID_ERR | SVM_EVTINJ_VALID) > + > #define SVM_EXITINTINFO_VEC_MASK SVM_EVTINJ_VEC_MASK > #define SVM_EXITINTINFO_TYPE_MASK SVM_EVTINJ_TYPE_MASK > > diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c > index 0a7bb01f5404..c87738962970 100644 > --- a/arch/x86/kvm/svm/nested.c > +++ b/arch/x86/kvm/svm/nested.c > @@ -499,32 +499,35 @@ void __nested_copy_vmcb_control_to_cache(struct kvm_vcpu *vcpu, > if (!guest_cpu_cap_has(vcpu, X86_FEATURE_NPT)) > to->misc_ctl &= ~SVM_MISC_ENABLE_NP; > > - to->iopm_base_pa = from->iopm_base_pa; > - to->msrpm_base_pa = from->msrpm_base_pa; > + /* > + * Copy the ASID here because nested_vmcb_check_controls() will check > + * it. The ASID could be invalid, or conflict with another VM's ASID , Spurious space before the command. > + * so it should never be used directly to run L2. > + */ > + to->asid = from->asid; > + > + /* Lower bits of IOPM_BASE_PA and MSRPM_BASE_PA are ignored */ > + to->iopm_base_pa = from->iopm_base_pa & PAGE_MASK; > + to->msrpm_base_pa = from->msrpm_base_pa & PAGE_MASK; >> to->tsc_offset = from->tsc_offset; > - to->tlb_ctl = from->tlb_ctl; I don't think we should completely drop tlb_ctl. KVM doesn't do anything with vmcb12's tlb_ctl only because we haven't addressed the TODO list in nested_svm_transition_tlb_flush(). I think I would rather update this code to sanitize the field now, as opposed to waiting until we address that TODO. KVM advertises X86_FEATURE_FLUSHBYASID, so I think we can do the right thing without having to speculate on what the future will bring. Alternatively, we could add a TODO here or update the one in nested_svm_transition_tlb_flush(), but that seems like more overall work than just hardening the code.