From nobody Thu Apr 18 19:16:45 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8316C433F5 for ; Fri, 25 Mar 2022 13:21:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353443AbiCYNX0 (ORCPT ); Fri, 25 Mar 2022 09:23:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49938 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353945AbiCYNXW (ORCPT ); Fri, 25 Mar 2022 09:23:22 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6E435D110E for ; Fri, 25 Mar 2022 06:21:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648214507; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F/zMX3vMpimh1Q2Dt4VlHRposb+rNApFkADJ1V2LHvc=; b=ar0n9IyyeipX04SANk041sAC8us6qdUld9pJNzEH/3Pi2sH5A5fbbvQxrVxuzt6TgpqhOz 7NwY0UkHeM4HO8qMH4xNH+m7sMyjb3tJUznEw6xkin/DrEIxdS87wSrHeZ+cRSVS1MNFno bjT3pJUiPjh+ArCP4iwJeG8wGxk3+hw= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-96-Qts0xWIXPZib_4VTGFShrQ-1; Fri, 25 Mar 2022 09:21:46 -0400 X-MC-Unique: Qts0xWIXPZib_4VTGFShrQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id CC5D73C14CCF; Fri, 25 Mar 2022 13:21:45 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.40.194.62]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF3CC2166B16; Fri, 25 Mar 2022 13:21:43 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, Paolo Bonzini Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , linux-kernel@vger.kernel.org Subject: [PATCH 1/3] KVM: x86: Check lapic_in_kernel() before attempting to set a SynIC irq Date: Fri, 25 Mar 2022 14:21:38 +0100 Message-Id: <20220325132140.25650-2-vkuznets@redhat.com> In-Reply-To: <20220325132140.25650-1-vkuznets@redhat.com> References: <20220325132140.25650-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When KVM_CAP_HYPERV_SYNIC{,2} is activated, KVM already checks for irqchip_in_kernel() so normally SynIC irqs should never be set. It is, however, possible for a misbehaving VMM to write to SYNIC/STIMER MSRs causing erroneous behavior. The immediate issue being fixed is that kvm_irq_delivery_to_apic() (kvm_irq_delivery_to_apic_fast()) crashes when called with 'irq.shorthand =3D APIC_DEST_SELF' and 'src =3D=3D NULL'. Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/hyperv.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 7480e3562d30..a79a094c1266 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -453,6 +453,9 @@ static int synic_set_irq(struct kvm_vcpu_hv_synic *syni= c, u32 sint) struct kvm_lapic_irq irq; int ret, vector; =20 + if (KVM_BUG_ON(!lapic_in_kernel(vcpu), vcpu->kvm)) + return -EINVAL; + if (sint >=3D ARRAY_SIZE(synic->sint)) return -EINVAL; =20 --=20 2.35.1 From nobody Thu Apr 18 19:16:45 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D3CAFC433EF for ; Fri, 25 Mar 2022 13:21:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1355210AbiCYNXa (ORCPT ); Fri, 25 Mar 2022 09:23:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50004 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1354331AbiCYNX0 (ORCPT ); Fri, 25 Mar 2022 09:23:26 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 93E90D111E for ; Fri, 25 Mar 2022 06:21:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648214511; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gMv4e7wT/AWnoTRe9UY/uzLmQ9YuuE85ZSgP+0jK/+I=; b=BbnIn9mcdYgoxMrIUk7U1g3I+iOYntTrabRTwOviXQO3QzLX7XR6wq7tmb5Hr9fFdE9Dnb bOQNrBY0CICUp0ltt9mGO9Mo9r9o+LszdWFjb3vI73KWa7C+BBfF6SdiDwXyWDVW/rHyXO TDcQQb6K7esZUsRZxe/iOukvr40p2NU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-271-Nnh9jeBKNfOyJ6zmm8o7GQ-1; Fri, 25 Mar 2022 09:21:48 -0400 X-MC-Unique: Nnh9jeBKNfOyJ6zmm8o7GQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D83F9866DF8; Fri, 25 Mar 2022 13:21:47 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.40.194.62]) by smtp.corp.redhat.com (Postfix) with ESMTP id 38E1A2166B16; Fri, 25 Mar 2022 13:21:46 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, Paolo Bonzini Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , linux-kernel@vger.kernel.org Subject: [PATCH 2/3] KVM: x86: Avoid theoretical NULL pointer dereference in kvm_irq_delivery_to_apic_fast() Date: Fri, 25 Mar 2022 14:21:39 +0100 Message-Id: <20220325132140.25650-3-vkuznets@redhat.com> In-Reply-To: <20220325132140.25650-1-vkuznets@redhat.com> References: <20220325132140.25650-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When kvm_irq_delivery_to_apic_fast() is called with APIC_DEST_SELF shorthand, 'src' must not be NULL. Crash the VM with KVM_BUG_ON() instead of crashing the host. Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/lapic.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 80a2020c4db4..66b0eb0bda94 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1024,6 +1024,10 @@ bool kvm_irq_delivery_to_apic_fast(struct kvm *kvm, = struct kvm_lapic *src, *r =3D -1; =20 if (irq->shorthand =3D=3D APIC_DEST_SELF) { + if (KVM_BUG_ON(!src, kvm)) { + *r =3D 0; + return true; + } *r =3D kvm_apic_set_irq(src->vcpu, irq, dest_map); return true; } --=20 2.35.1 From nobody Thu Apr 18 19:16:45 2024 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 25556C433F5 for ; Fri, 25 Mar 2022 13:22:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353490AbiCYNXe (ORCPT ); Fri, 25 Mar 2022 09:23:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1355495AbiCYNXb (ORCPT ); Fri, 25 Mar 2022 09:23:31 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E97B1D1CD0 for ; Fri, 25 Mar 2022 06:21:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1648214516; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=aGcphh+FSCJkCtquixHZ+Ppqf8dAW6dwu2JqKDu/J40=; b=bQdIqX7d+6k/7g+4sPEb25j8DVrbCt55pZ3l1jlVoaUMjsH7iKLZHAFrf8THaeyNEikCI4 vlJ5NqI3KIMYXB4K/FxpHNrBMa5jLpj2GVeK/SK/5tLUTJz+rhgpOpCFzqCYoiQRiCKOxB bVLvGS7R78hU3N7y3GgJ8TdyGjMFBhE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-456-AWhCHrn_OLuKCMj93ui8aw-1; Fri, 25 Mar 2022 09:21:50 -0400 X-MC-Unique: AWhCHrn_OLuKCMj93ui8aw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E40802A59540; Fri, 25 Mar 2022 13:21:49 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.40.194.62]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3BED52166B16; Fri, 25 Mar 2022 13:21:48 +0000 (UTC) From: Vitaly Kuznetsov To: kvm@vger.kernel.org, Paolo Bonzini Cc: Sean Christopherson , Wanpeng Li , Jim Mattson , linux-kernel@vger.kernel.org Subject: [PATCH 3/3] KVM: x86: Forbid VMM to set SYNIC/STIMER MSRs when SynIC wasn't activated Date: Fri, 25 Mar 2022 14:21:40 +0100 Message-Id: <20220325132140.25650-4-vkuznets@redhat.com> In-Reply-To: <20220325132140.25650-1-vkuznets@redhat.com> References: <20220325132140.25650-1-vkuznets@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Setting non-zero values to SYNIC/STIMER MSRs activates certain features, this should not happen when KVM_CAP_HYPERV_SYNIC{,2} was not activated. Note, it would've been better to forbid writing anything to SYNIC/STIMER MSRs, including zeroes, however, at least QEMU tries clearing HV_X64_MSR_STIMER0_CONFIG without SynIC. HV_X64_MSR_EOM MSR is somewhat 'special' as writing zero there triggers an action, this also should not happen when SynIC wasn't activated. Signed-off-by: Vitaly Kuznetsov --- arch/x86/kvm/hyperv.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index a79a094c1266..123b677111c5 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -243,7 +243,7 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *syni= c, struct kvm_vcpu *vcpu =3D hv_synic_to_vcpu(synic); int ret; =20 - if (!synic->active && !host) + if (!synic->active && (!host || data)) return 1; =20 trace_kvm_hv_synic_set_msr(vcpu->vcpu_id, msr, data, host); @@ -289,6 +289,9 @@ static int synic_set_msr(struct kvm_vcpu_hv_synic *syni= c, case HV_X64_MSR_EOM: { int i; =20 + if (!synic->active) + break; + for (i =3D 0; i < ARRAY_SIZE(synic->sint); i++) kvm_hv_notify_acked_sint(vcpu, i); break; @@ -668,7 +671,7 @@ static int stimer_set_config(struct kvm_vcpu_hv_stimer = *stimer, u64 config, struct kvm_vcpu_hv *hv_vcpu =3D to_hv_vcpu(vcpu); struct kvm_vcpu_hv_synic *synic =3D to_hv_synic(vcpu); =20 - if (!synic->active && !host) + if (!synic->active && (!host || config)) return 1; =20 if (unlikely(!host && hv_vcpu->enforce_cpuid && new_config.direct_mode && @@ -697,7 +700,7 @@ static int stimer_set_count(struct kvm_vcpu_hv_stimer *= stimer, u64 count, struct kvm_vcpu *vcpu =3D hv_stimer_to_vcpu(stimer); struct kvm_vcpu_hv_synic *synic =3D to_hv_synic(vcpu); =20 - if (!synic->active && !host) + if (!synic->active && (!host || count)) return 1; =20 trace_kvm_hv_stimer_set_count(hv_stimer_to_vcpu(stimer)->vcpu_id, --=20 2.35.1