Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40C5BC282DA for ; Fri, 5 Apr 2019 22:43:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 045302171F for ; Fri, 5 Apr 2019 22:43:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="iwQvh5P4" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726461AbfDEWny (ORCPT ); Fri, 5 Apr 2019 18:43:54 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:44266 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726435AbfDEWnx (ORCPT ); Fri, 5 Apr 2019 18:43:53 -0400 Received: by mail-wr1-f67.google.com with SMTP id y7so9668810wrn.11 for ; Fri, 05 Apr 2019 15:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=oxotopg+hHSHdBIPFxdfnmb6wtC0k81SaBA16OBj0H0=; b=iwQvh5P4AzkB/3yQTTKEAU43cxi2F+ObYXgJxtsw+BEHuX/SdTL4gRCoGVLyadzSQd y5yVFH3nnSfyWgHlQ58Kn7Jxgy3tpJyBKqUkq5v9yIRbxGLMlI2ksStYoDi7QeG3rMEL XLwwCxmbHuoHQR6ddCPzw/Q1TDqyt0TpK8fSkAMojvei/Fjj8IolM+Vb+AzqSeK0uusr 4AOs29ACMVZ+ANHlfnFWqBfdJMQ83oDdDHF0iC5h4lHT67z4TDKblY4vzAhFuxxLPq0z vaoJ/9fZbPW/Vj1RpMgSanoPa6JzlWDPGd6YoFU/cZgX/CHULbkjHIeNHnPJZiH45Qzk wPyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=oxotopg+hHSHdBIPFxdfnmb6wtC0k81SaBA16OBj0H0=; b=uVEVeETcW8ZmWat4lUBNA/gdVNJOHs+1YdtXOtBqcLsi6IioJ/n5VsfVKAW4MVsxZR gficm0VMPoUFpsg+57SGEEYwBEWXje0QurYnsKbcN/NqgcQosFkXuiFKsIaJBCqX5els tlH9cWAKU+pkEElqvGHfJDXWZ1inXxBzFQlyO/dOZRzo44T5kkQoR+w4e0sAhhEA8R4R 5oh03crXRNMZnP7eAtWX0K1m2RsT+7uhiTUwTWVBG6CBWIp3rTNuFjWE2pdJyIqGPeQ9 q8TSi9snsSE03MUqmkloGBUuhfAfFSLxN3HoUGUqwebu6A7oXSD/MDowdrWhJBnVQ0bz wUew== X-Gm-Message-State: APjAAAUlQ6vz+2xgC6MAsH1dn55uRs336JTZkConDxciO25VtL7qB2Qw FhRurlnlaDMq7J7OU5hIYz7BfJfA X-Google-Smtp-Source: APXvYqx7M9adqP1wN4IQGHThjMY09euQxUS19B8QM605FM2ZJmKATBP7gOmEpisJjRmlPxuM3kAdGA== X-Received: by 2002:adf:e4c2:: with SMTP id v2mr9860222wrm.124.1554504231724; Fri, 05 Apr 2019 15:43:51 -0700 (PDT) Received: from 640k.lan ([93.56.166.5]) by smtp.gmail.com with ESMTPSA id h2sm42821876wro.11.2019.04.05.15.43.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 05 Apr 2019 15:43:51 -0700 (PDT) From: Paolo Bonzini To: kvm@vger.kernel.org Cc: Marc Orr Subject: [PATCH kvm-unit-tests 6/6] Add leak scenario to virt_x2apic_mode_test Date: Sat, 6 Apr 2019 00:43:43 +0200 Message-Id: <1554504223-7919-7-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1554504223-7919-1-git-send-email-pbonzini@redhat.com> References: <1554504223-7919-1-git-send-email-pbonzini@redhat.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Marc Orr While working on virt_x2apic_mode_test, we noticed that KVM allows for a buggy or malicious L1 to get at L0's x2APIC MSRs via nested. The issue is in KVM's nested_vmx_prepare_msr_bitmap() function. Specifically, an L1 can execute the following sequence: 1. WRMSR(IA32_SPEC_CTRL, 1), which causes the spec_ctrl variable, in nested_vmx_prepare_msr_bitmap() to become true. 2. Clear "virtualize x2APIC mode" 3. Set "APIC-register virualization" Then, KVM will copy L1's MSR bitmap for the x2APIC MSR range into L0, and run L2 with "Virtualize x2APIC mode" disabled, which gives L2 unfettered access to L0's x2APIC msrs. Thus, this patch extends virt_x2apic_mode_test with a test case for this scenario. Note, this patch was used to discover and fix the issue described in the KVM patch titled "KVM: x86: nVMX: close leak of L0's x2APIC MSRs". Signed-off-by: Marc Orr Reviewed-by: Jim Mattson Signed-off-by: Paolo Bonzini --- lib/x86/processor.h | 5 +++++ x86/vmexit.c | 5 ----- x86/vmx_tests.c | 31 +++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/x86/processor.h b/lib/x86/processor.h index 6f77148..a4ef315 100644 --- a/lib/x86/processor.h +++ b/lib/x86/processor.h @@ -487,4 +487,9 @@ static inline void flush_tlb(void) write_cr4(cr4); } +static inline int has_spec_ctrl(void) +{ + return !!(cpuid_indexed(7,0).d & (1 << 26)); +} + #endif diff --git a/x86/vmexit.c b/x86/vmexit.c index c12dd24..f094f53 100644 --- a/x86/vmexit.c +++ b/x86/vmexit.c @@ -417,11 +417,6 @@ static void tscdeadline(void) while (x == 0) barrier(); } -static int has_spec_ctrl(void) -{ - return !!(cpuid_indexed(7,0).d & (1 << 26)); -} - static void wr_ibrs_msr(void) { wrmsr(MSR_IA32_SPEC_CTRL, 1); diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c index 12d80df..868f284 100644 --- a/x86/vmx_tests.c +++ b/x86/vmx_tests.c @@ -6106,6 +6106,27 @@ struct virt_x2apic_mode_test_case virt_x2apic_mode_tests[] = { }, }, }, + + /* + * Disable "Virtualize x2APIC mode", disable x2APIC MSR intercepts, and + * enable "APIC-register virtualization" --> L2 gets L1's x2APIC MSRs. + */ + { + .name = "Baseline", + .virt_x2apic_mode_config = { + .virtual_interrupt_delivery = true, + .use_msr_bitmaps = true, + .disable_x2apic_msr_intercepts = true, + .disable_x2apic = false, + .apic_reg_virt_config = { + .apic_register_virtualization = true, + .use_tpr_shadow = true, + .virtualize_apic_accesses = false, + .virtualize_x2apic_mode = false, + .activate_secondary_controls = true, + }, + }, + }, }; enum X2apic_op { @@ -6423,6 +6444,16 @@ static void virt_x2apic_mode_test(void) struct virt_x2apic_mode_guest_args *args = &virt_x2apic_mode_guest_args; /* + * This is to exercise an issue in KVM's logic to merge L0's and L1's + * MSR bitmaps. Previously, an L1 could get at L0's x2APIC MSRs by + * writing the IA32_SPEC_CTRL MSR or the IA32_PRED_CMD MSRs. KVM would + * then proceed to manipulate the MSR bitmaps, as if VMCS12 had the + * "Virtualize x2APIC mod" control set, even when it didn't. + */ + if (has_spec_ctrl()) + wrmsr(MSR_IA32_SPEC_CTRL, 1); + + /* * Check that VMCS12 supports: * - "Virtual-APIC address", indicated by "use TPR shadow" * - "MSR-bitmap address", indicated by "use MSR bitmaps" -- 1.8.3.1