From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 207DE326D53 for ; Thu, 13 Nov 2025 22:56:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763074600; cv=none; b=D9DXna2armcEfoLhv88lyODnnSULt2dRTXn5F9kDssCeSZG80wzgINWOhL2ReSWhBcWFQ3raXODV81c9/RQeUAWZfEYeniuOu1SuSDYkbnA4q2H6FRWmZu+mdEeYAA9lzvHtfu0Tdru/BvIj2tU7E3fVMvS2Uu5JZL3ChmV0fJo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763074600; c=relaxed/simple; bh=FrLYypfXkWMF/B8MfR9jtaa3rG5AmOBlRcsStUH+QyM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sXtFukvqNq0Y2nCIM4F2PASnpyvnqU9tK6gTdUqiEn7U71TFhJpmOTS6BvVWUKtYTT3yZJx7Tmk+WTP+Mll7NCN5+A2bEGMKzUsDeg1ZbQeRBuB5tEptEz4r6oBUJTW/nudWs0/nM7PS1VQsZkYOcwfZ+QUARVX/AN1XPdOKDVU= 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=nsxT1APJ; arc=none smtp.client-ip=209.85.210.201 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="nsxT1APJ" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-7ae3e3e0d06so1124552b3a.0 for ; Thu, 13 Nov 2025 14:56:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1763074598; x=1763679398; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Ib2v7Ui/fjE5+JdRUXnMPJd5ZodcfqQQWSBde2U7kt4=; b=nsxT1APJPbPY9mIax9OUsn18Tm0M9Ngn/se9QKH9CwWg7uWFE1s044R9j41eZdSwtH 4K1vczGCXRdmY90Djpew9ZCOKCMpRsuLuEbL6CHKB/gzAK1RFfsCovMiTP3p1tj0fpc3 HdRUwo49eAtMPz7GvL8NguTftXnpbVXMr4ziutJM0/H6iCLB9gUkKiGts5hYfWWjGZLT 2sKF27+hpsryMIHN/oYTKan8BbwvA/VPSIJvgmWRjLsee1NylUA/BQbzap85bnnr/V/2 Asd1OwGLcndnvQGlpNGDfBTDt37XDuhcGtbpaHXHl05hO/DDYc5Y214P7HbfBBD915yh HQGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763074598; x=1763679398; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Ib2v7Ui/fjE5+JdRUXnMPJd5ZodcfqQQWSBde2U7kt4=; b=sIT3LFxuSoMXMH8qFpbTy/glfxPa67djph5jiQT/TiXvKAGd2475yNLGSrtmZv/bNa FdoNC0uIkZT3iBjffQ7PGesFosqjuwSbFeWcAVAunsvrPQ+LCNGrt6IsPO0v4LL5CgGi gXhnIIGPzAwdQhJQEw4GXjLAt3GMrE3qHqrJTn84uh5A+WK7L7vO+/8cfj4vvE6jGNlv wc+b5DPsYnJMnflz+IJt0GQ+IPmJk4k5CDVD4CT02JguoOENqobrlzEvQzX1HXQ+LgsW YbLWMVtB+z1fUv81jS77y3h3x2848YTP58Crfm7o4t1FOcTGKYAeQ3SJ1kyGo+Gx6HZN 1ebQ== X-Forwarded-Encrypted: i=1; AJvYcCWU0mcPKDfs0YRJWiOzHqtiNtj5bTLr2w6y7M1iWAhQlrwMwbg4n2Z9o7rnFGMd+j9rt/ZqnhU10p52mOg=@vger.kernel.org X-Gm-Message-State: AOJu0YxHqD+nWrMz8eL7z5BnWsR7n8lwpohmCrl/plnin5QAJh76BZAb VfoevuyybZsRk1DGLXjJv4dm+nXbxGcSZPGvsyhoS8nkarIa8vrEKXOTb/oWMgxwbfoLc7Nj3cd bUA7Lcw== X-Google-Smtp-Source: AGHT+IH8IA3FWekt1ULXu2lSks3XBHqEUS4FhRpMQVG9SUdbxMomw9Nt9TFp3kJLPvjiS9a6PESnoKZm6yQ= X-Received: from pgcv11.prod.google.com ([2002:a05:6a02:530b:b0:bac:a20:5ef9]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6300:50b:20b0:35b:cc61:680 with SMTP id adf61e73a8af0-35bcc610d4emr550460637.9.1763074598407; Thu, 13 Nov 2025 14:56:38 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 13 Nov 2025 14:56:18 -0800 In-Reply-To: <20251113225621.1688428-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251113225621.1688428-1-seanjc@google.com> X-Mailer: git-send-email 2.52.0.rc1.455.g30608eb744-goog Message-ID: <20251113225621.1688428-7-seanjc@google.com> Subject: [PATCH 6/9] KVM: SVM: Filter out 64-bit exit codes when invoking exit handlers on bare metal From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov , "K. Y. Srinivasan" , Haiyang Zhang , Wei Liu , Dexuan Cui Cc: kvm@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, Jim Mattson , Yosry Ahmed Content-Type: text/plain; charset="UTF-8" Explicitly filter out 64-bit exit codes when invoking exit handlers, as svm_exit_handlers[] will never be sized with entries that use bits 63:32. Processing the non-failing exit code as a 32-bit value will allow tracking exit_code as a single 64-bit value (which it is, architecturally). This will also allow hardening KVM against Spectre-like attacks without needing to do silly things to avoid build failures on 32-bit kernels (array_index_nospec() rightly asserts that the index fits in an "unsigned long"). Omit the check when running as a VM, as KVM has historically failed to set bits 63:32 appropriately when synthesizing VM-Exits, i.e. KVM could get false positives when running as a VM on an older, broken KVM/kernel. From a functional perspective, omitting the check is "fine", as any unwanted collision between e.g. VMEXIT_INVALID and a 32-bit exit code will be fatal to KVM-on-KVM regardless of what KVM-as-L1 does. Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/svm.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 202a4d8088a2..3b05476296d0 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3433,8 +3433,22 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) sev_free_decrypted_vmsa(vcpu, save); } -int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) +int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 __exit_code) { + u32 exit_code = __exit_code; + + /* + * SVM uses negative values, i.e. 64-bit values, to indicate that VMRUN + * failed. Report all such errors to userspace (note, VMEXIT_INVALID, + * a.k.a. SVM_EXIT_ERR, is special cased by svm_handle_exit()). Skip + * the check when running as a VM, as KVM has historically left garbage + * in bits 63:32, i.e. running KVM-on-KVM would hit false positives if + * the underlying kernel is buggy. + */ + if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR) && + (u64)exit_code != __exit_code) + goto unexpected_vmexit; + #ifdef CONFIG_MITIGATION_RETPOLINE if (exit_code == SVM_EXIT_MSR) return msr_interception(vcpu); @@ -3461,7 +3475,7 @@ int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code) unexpected_vmexit: dump_vmcb(vcpu); - kvm_prepare_unexpected_reason_exit(vcpu, exit_code); + kvm_prepare_unexpected_reason_exit(vcpu, __exit_code); return 0; } -- 2.52.0.rc1.455.g30608eb744-goog