From patchwork Thu Oct 20 09:30:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 6100 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp19wrr; Thu, 20 Oct 2022 02:39:20 -0700 (PDT) X-Google-Smtp-Source: AMsMyM48zHe0ZPAFeokJ/5RhlGtK5AxOobIo6EHDzaJb12CSLIDiK8J+G8GYVG349sjaXHlUJHUi X-Received: by 2002:a17:90b:224d:b0:20d:8828:3051 with SMTP id hk13-20020a17090b224d00b0020d88283051mr14622671pjb.89.1666258759417; Thu, 20 Oct 2022 02:39:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666258759; cv=none; d=google.com; s=arc-20160816; b=PyRKD5ZrwTyzMhYM+EDAqrTTnKFm7eGNlJqhhCIWIknhTFNPpt24j+dmCuBx+vRahw 4Lk9RA5QGiVA3rudY3gJLFE6jCUiklyUhNo1HoKYWwhTBQlPJAPrVVjrDVJ+8BNzqD4B M+BKvDFG5E1gHTwVR0iVQ9pzWZ/JFNNznaK1v0JmyjzOMwTpww3qtacSLyK/dwPi4IYG MhrzCKs+C4ZQV4iXy53gmc+T43tCqZUIf+wFW0/xicdbzaTud3OXVhp3fPN+PNgs6dM2 aAsRPH/Yt8VMBburZk5WBbknfznS4P0tVDM+hk17dJb0Ww1Ik6k+XSKdD7eTShir6zcS FaIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IBgj4Fw1P4eekn5EviwqvOJ2K/YwxD/QOnBlpto11Zg=; b=0gJxopfNTc6wAmfcJy3FC2X5OZYpdYlnTHaUZbEgmb6fltXttZ3ES+6d1wzVVCsNp+ D4p+KtAgVkLzRbSSrBBrHSMwZjB1eRp9knfmy/3AX95P2OHSaHxL3GELOk6HNBTDv/1y UWQRu4vy+pa9XrT0Mty8e5agvmkG8bH9sZxOq+8jZkOefULUJO1pLeh8AKB7YqmwgpcG jUC96wzFMj7dyqk6cDt+EYVABZbCQKatQQWTeEQX94/XSVHwaXZJpUhSjrFgGKdpEghE C5f/LGaD31zZx5ij8Pz7Dnlv5xRWPL4OJmbWhgfv6JYo134KUob/rUQG2L6vhVhsLB3R df2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O5ryYTQq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u4-20020a170902e5c400b001846e0e0511si26656529plf.226.2022.10.20.02.39.05; Thu, 20 Oct 2022 02:39:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O5ryYTQq; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230161AbiJTJbY (ORCPT + 99 others); Thu, 20 Oct 2022 05:31:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56964 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230307AbiJTJbQ (ORCPT ); Thu, 20 Oct 2022 05:31:16 -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 ESMTPS id 5B5C5156244 for ; Thu, 20 Oct 2022 02:31:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666258271; 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=IBgj4Fw1P4eekn5EviwqvOJ2K/YwxD/QOnBlpto11Zg=; b=O5ryYTQqQFBNfUMm10Pk3t3HoJjEeHge5NrUTwo0znTPDz8j/EAYzNRkTohd1LCxhWowq9 qvMG6V6PJUgq1J3ugEi2luCC1P6f9isK2V6BZRYXv59NMeGtNXGcH23RerVdL3tOOI3dhN GDQSJna7TA9/bw0vI22y8TWlDNFCmxw= 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-610-_oUs1GGWOj61jsTPq0Yptw-1; Thu, 20 Oct 2022 05:31:08 -0400 X-MC-Unique: _oUs1GGWOj61jsTPq0Yptw-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 0EA5885A583; Thu, 20 Oct 2022 09:31:08 +0000 (UTC) Received: from localhost.localdomain (ovpn-192-51.brq.redhat.com [10.40.192.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 09EC149BB60; Thu, 20 Oct 2022 09:31:04 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Paolo Bonzini , Borislav Petkov , Ingo Molnar , Sean Christopherson , x86@kernel.org, Thomas Gleixner , Dave Hansen , "H. Peter Anvin" , Maxim Levitsky , stable@vger.kernel.org Subject: [PATCH 1/4] KVM: x86: nSVM: leave nested mode on vCPU free Date: Thu, 20 Oct 2022 12:30:52 +0300 Message-Id: <20221020093055.224317-2-mlevitsk@redhat.com> In-Reply-To: <20221020093055.224317-1-mlevitsk@redhat.com> References: <20221020093055.224317-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747198944844364478?= X-GMAIL-MSGID: =?utf-8?q?1747198944844364478?= If the VM was terminated while nested, we free the nested state while the vCPU still is in nested mode. Soon a warning will be added for this condition. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/svm.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 58f0077d935799..958faa8807f52b 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1439,6 +1439,7 @@ static void svm_vcpu_free(struct kvm_vcpu *vcpu) */ svm_clear_current_vmcb(svm->vmcb); + svm_leave_nested(vcpu); svm_free_nested(svm); sev_free_vcpu(vcpu); From patchwork Thu Oct 20 09:30:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 6098 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4f06:0:0:0:0:0 with SMTP id c6csp175721wru; Thu, 20 Oct 2022 02:38:59 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4MYGQGN0skCjsEWS2jsJB3ncSrMDfVjiTeT2mgDSvlRt7wfVcMnexAWa9N1lGKh1feNhQo X-Received: by 2002:a17:906:591:b0:73d:c3ef:84ae with SMTP id 17-20020a170906059100b0073dc3ef84aemr10281129ejn.155.1666258739172; Thu, 20 Oct 2022 02:38:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666258739; cv=none; d=google.com; s=arc-20160816; b=tSaSSQDeVzQzxE63DTdC+NgMvuyvsf6TfDx2GtDunifqCWvVrOgxXveCA3UI1J0/gg sNquUtGCF2X9QkMEhYZqH3UQ7UdImrhvGE/5WCQRmCmcm4K8ZUdtPGe9DiyoPEhfEtsm yEcyjCSzUcVPVOwtfLr9O7ioWkgmGQ3zJny48ItQXOWgLUdQVu5JyoBETfPTrh5IKwht YvQ38PAR+deygZDxkcoo4pgC0uZVw8nweAqfpN2uK+01KXaEphc+zXMiACXE5/kV/r0Z i1yR4LwHX5CrorDAqL+YRT5s6vzKAqCxo4cqF/ees1tGWJVA/Dwxmuvhs6JZkc1ITQ84 5pPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=NO7cWLyVclWMKKTiYZzmBycykjfaB0P86+4KIdCX/tM=; b=08EQd0pFQ0Inx0xbJLvAGcrKgXsFhSEAKNeInKM7eKP4qupC2EAW6QSQYBCqxaSbxO Fp0G0j3WKJmnc9cR+wlMNwl1W4yHC7mLlDkRvSK1CtP5n4iTBKfX08H+rwImG8uoL9fX qe3Gy1W9LtdRzf+Uma1toUaQoNWk5CP68oMQri9dKzLcsuTMwx2z+4LA21+GD4k81PvG bmrRedIciZ1V5owplwv8UqGvu6TRM28fTPhZIyw4nATExLImpgaiFyOP2eEbw6Rni0CA Y0NxkF6gMnOHzN+ysI3FuqhKCcUCJxewLqApgAw0ezS6blW+hjD2l6MQKEnNBGL62kEv oThw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Mv+5h66M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bt4-20020a0564020a4400b0045ca3642c4esi219064edb.145.2022.10.20.02.38.33; Thu, 20 Oct 2022 02:38:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Mv+5h66M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230173AbiJTJbi (ORCPT + 99 others); Thu, 20 Oct 2022 05:31:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57580 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230335AbiJTJbW (ORCPT ); Thu, 20 Oct 2022 05:31:22 -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 ESMTPS id BA3E0155DB2 for ; Thu, 20 Oct 2022 02:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666258277; 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=NO7cWLyVclWMKKTiYZzmBycykjfaB0P86+4KIdCX/tM=; b=Mv+5h66M3hORBahuXEiyOg1SJdRNH2XzNc5UICdsEqcO8HDWmp9FsEbUr2ac8vU1tP+prB YOPH26A3aE/pCRKjWRdF0FQTQNwuwpcJsxbqvsjXkOvpPjP9fZrSpW4QT9CltFJxO4BxDm 7IqtjW1ZieUE9cfNqXmszl2TaRRZW7w= 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-201-si_bzrwCPsqHWe6dAUaoPQ-1; Thu, 20 Oct 2022 05:31:11 -0400 X-MC-Unique: si_bzrwCPsqHWe6dAUaoPQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 3B30138012DD; Thu, 20 Oct 2022 09:31:11 +0000 (UTC) Received: from localhost.localdomain (ovpn-192-51.brq.redhat.com [10.40.192.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6D32649BB60; Thu, 20 Oct 2022 09:31:08 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Paolo Bonzini , Borislav Petkov , Ingo Molnar , Sean Christopherson , x86@kernel.org, Thomas Gleixner , Dave Hansen , "H. Peter Anvin" , Maxim Levitsky , stable@vger.kernel.org Subject: [PATCH 2/4] KVM: x86: nSVM: harden svm_free_nested against freeing vmcb02 while still in use Date: Thu, 20 Oct 2022 12:30:53 +0300 Message-Id: <20221020093055.224317-3-mlevitsk@redhat.com> In-Reply-To: <20221020093055.224317-1-mlevitsk@redhat.com> References: <20221020093055.224317-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747198923659432225?= X-GMAIL-MSGID: =?utf-8?q?1747198923659432225?= Make sure that KVM uses vmcb01 before freeing nested state, and warn if that is not the case. This is a minimal fix for CVE-2022-3344 making the kernel print a warning instead of a kernel panic. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/nested.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index 4c620999d230a5..b02a3a1792f194 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1125,6 +1125,9 @@ void svm_free_nested(struct vcpu_svm *svm) if (!svm->nested.initialized) return; + if (WARN_ON_ONCE(svm->vmcb != svm->vmcb01.ptr)) + svm_switch_vmcb(svm, &svm->vmcb01); + svm_vcpu_free_msrpm(svm->nested.msrpm); svm->nested.msrpm = NULL; From patchwork Thu Oct 20 09:30:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 6096 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4f06:0:0:0:0:0 with SMTP id c6csp175480wru; Thu, 20 Oct 2022 02:38:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4ZrLMjGBzW23UGYnFrXmAkiC2zC2PZR9GNXySdJuX1oLAWmgAuxrKBummR5O7P7ayW0CLR X-Received: by 2002:a17:902:f60c:b0:17c:163f:c0a8 with SMTP id n12-20020a170902f60c00b0017c163fc0a8mr12690399plg.38.1666258688498; Thu, 20 Oct 2022 02:38:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666258688; cv=none; d=google.com; s=arc-20160816; b=cvRBcmff8gvYL9FGMgsltl3c0SOsPsQr6x6X5/I5d2JqY/D/gx+r5CG6l6XcIJuJxA +zIYtfSf5Z22zVvJL59xxaFhnaQsWKGLeMw0tGs5MMoUm2Yspmv4S4yahlL9EdJbXiC8 Fbq54RyZtYqo97z5mxMcGVJCR4FdqalDCMetBn3HUc5tbiBsOZZ4pX0ACQwt2yzh4KP9 Qa/x4gQZeXrAQNwwVu41/jGUsp2xpJQZPCXPFDqlXy8+egHBlqRcyQXTC+PV0r0do9ux rfbVY0ImqYyE3FAj25v62Pi9YXsP6ZcIhGjMqjUo4ZVY5Ygx1qxgZ+Yt9D4dqpqR76TD nLIQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=P73JFXQ3AWZGy0kFIbXH5/4fwtY8KoEObX2sf4YZ6rg=; b=L3h/HS0vfv0ZJaI6uG4uk/AF/zIdRmrk3TIERtZwd2LSF7qBNAAZbjEO/1vDG30nWj GO+l8rsEXT3Y/YGWpleT9Q3Lggm7eblj2/iQyIr79li3bxSJuvH8PdIGX0j8coaz3gIu MUxcP5UDSrB8Ni9l8aFyQEStmOWpcSvl/L1/TsjZnaCLZu7Sh2+XWPjFWGfbmjylWrwS GX0zCnKHePDidL7cHh5DqmOztk4Gialax7K4QYohjXrW0CnGb5yTLfIgBuSqgOHFUFPf R1d2G1JewzAP4rwwrbkeoTCwg0KVgeG7RcMy8I9aNKuBL7LFdSJKo33hfN000KvEJ2cR hUTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=L2f8vb7V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id m22-20020a056a00081600b00557eeeb099csi22115611pfk.339.2022.10.20.02.37.55; Thu, 20 Oct 2022 02:38:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=L2f8vb7V; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230423AbiJTJbl (ORCPT + 99 others); Thu, 20 Oct 2022 05:31:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57854 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230372AbiJTJb0 (ORCPT ); Thu, 20 Oct 2022 05:31: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 ESMTPS id B0955147D00 for ; Thu, 20 Oct 2022 02:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666258280; 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=P73JFXQ3AWZGy0kFIbXH5/4fwtY8KoEObX2sf4YZ6rg=; b=L2f8vb7V/tZcbsO/sunLqpi/4sl0vrE2m74VYIPf/IvpoSHy+P32seDeHxELbD5dyO7MIM foFCoUGl+LhKwf35GnvyrFVp+DkMgHrUf8JzE3+dL86lntsDA8Iq508o5+qB/dk0EL7uvN UXAZ4tXQ/rsgzx/TDDd4F2znShVna8c= 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-9-w3fq1VFrN-yCpp6SElqxpQ-1; Thu, 20 Oct 2022 05:31:15 -0400 X-MC-Unique: w3fq1VFrN-yCpp6SElqxpQ-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 6654085A583; Thu, 20 Oct 2022 09:31:14 +0000 (UTC) Received: from localhost.localdomain (ovpn-192-51.brq.redhat.com [10.40.192.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 99B2249BB60; Thu, 20 Oct 2022 09:31:11 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Paolo Bonzini , Borislav Petkov , Ingo Molnar , Sean Christopherson , x86@kernel.org, Thomas Gleixner , Dave Hansen , "H. Peter Anvin" , Maxim Levitsky , stable@vger.kernel.org Subject: [PATCH 3/4] KVM: x86: add kvm_leave_nested Date: Thu, 20 Oct 2022 12:30:54 +0300 Message-Id: <20221020093055.224317-4-mlevitsk@redhat.com> In-Reply-To: <20221020093055.224317-1-mlevitsk@redhat.com> References: <20221020093055.224317-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747198870313403458?= X-GMAIL-MSGID: =?utf-8?q?1747198870313403458?= Wrap a call to nested_ops->leave_nested into a function. Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky --- arch/x86/kvm/svm/nested.c | 3 --- arch/x86/kvm/vmx/nested.c | 3 --- arch/x86/kvm/x86.c | 8 +++++++- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c index b02a3a1792f194..7354f0035a691d 100644 --- a/arch/x86/kvm/svm/nested.c +++ b/arch/x86/kvm/svm/nested.c @@ -1146,9 +1146,6 @@ void svm_free_nested(struct vcpu_svm *svm) svm->nested.initialized = false; } -/* - * Forcibly leave nested mode in order to be able to reset the VCPU later on. - */ void svm_leave_nested(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index 8f67a9c4a28706..a66c5e276ab408 100644 --- a/arch/x86/kvm/vmx/nested.c +++ b/arch/x86/kvm/vmx/nested.c @@ -6440,9 +6440,6 @@ static int vmx_get_nested_state(struct kvm_vcpu *vcpu, return kvm_state.size; } -/* - * Forcibly leave nested mode in order to be able to reset the VCPU later on. - */ void vmx_leave_nested(struct kvm_vcpu *vcpu) { if (is_guest_mode(vcpu)) { diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4bd5f8a751de91..d86a8aae1471d3 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -628,6 +628,12 @@ static void kvm_queue_exception_vmexit(struct kvm_vcpu *vcpu, unsigned int vecto ex->payload = payload; } +/* Forcibly leave the nested mode in cases like a vCPU reset */ +static void kvm_leave_nested(struct kvm_vcpu *vcpu) +{ + kvm_x86_ops.nested_ops->leave_nested(vcpu); +} + static void kvm_multiple_exception(struct kvm_vcpu *vcpu, unsigned nr, bool has_error, u32 error_code, bool has_payload, unsigned long payload, bool reinject) @@ -5200,7 +5206,7 @@ static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, if (events->flags & KVM_VCPUEVENT_VALID_SMM) { if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { - kvm_x86_ops.nested_ops->leave_nested(vcpu); + kvm_leave_nested(vcpu); kvm_smm_changed(vcpu, events->smi.smm); } From patchwork Thu Oct 20 09:30:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maxim Levitsky X-Patchwork-Id: 6099 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4f06:0:0:0:0:0 with SMTP id c6csp175734wru; Thu, 20 Oct 2022 02:39:00 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7SAJOAp4FLlQDCZb1PWZn0Gvmi69bFYDIwj0+zMn/nLyc5it7n7OBFZmT+f0TnwkfEtZCs X-Received: by 2002:a17:902:7c8a:b0:17b:6eaa:5da3 with SMTP id y10-20020a1709027c8a00b0017b6eaa5da3mr12803027pll.33.1666258740633; Thu, 20 Oct 2022 02:39:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666258740; cv=none; d=google.com; s=arc-20160816; b=bWANayvH/Vc83MsVUgLd8/YnPoRKANJxJieKm5J6pjsCl+XM2Hcz+894H5uSEzn7J6 D1QGAtLETKB8nUISmlZ9i5Eh83M7UVe8GivyGgKD97Bys2g1leqczckdsM59N1H4/yCo xKRV6FnXb1s9nSuK4KHpzMIWQNUdeuAuy+rPLpVOIbg3PAgF1g8DQ9PYVsJQak6FhiDb tIOF4FkQpVCiTxqwefWKM060GwYbtw8iPLGGbrq4oOdsUE0pXJxMI/c+eZi9zQ2Ddars n8TbqHuEELKDHcDwIfHliIQFb0MMOutnnSsOFop9yNK4QtsfWM8Bvln2fLgk3Dt/zcO+ kpJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=nm86sopHALiAvOTB3Mb1uaCRxtNMK3Osw7jYg3LC+M0=; b=HK3dxwDPusaobO1KUYgvxJWQyZQ5bR63D3CwEc42Cfl6xAASvfZyoo1dd+xVwODPHe 3T6V4wD7Z+q7c3k0VHl4W70tjwz2IwSN80IGenpBebUjw/FW8oOKUIsqAstlk1QGr8dy DvoP7Phj7CECPa+XV/EQGz1nKAHr0waXrStDgeavD4xa/kCApxa1FGfq69elE2mcRRp2 C9TsPnmXqZBwQS2rs1qXJz57JcCyhdCUh4e8fe+n9rbcEkRirlxtocoDn3DjlknohOXD kM8rzVANjHwyD1+Q2d6rAV9a2ypYkqIkd5uaKMlRudIOJUUPdUss9VXrdjq2e6HwUSgM BPDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PrBzeCYA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s19-20020a63f053000000b0044b412c1a77si20001757pgj.362.2022.10.20.02.38.46; Thu, 20 Oct 2022 02:39:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=PrBzeCYA; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230453AbiJTJbp (ORCPT + 99 others); Thu, 20 Oct 2022 05:31:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57578 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230439AbiJTJb3 (ORCPT ); Thu, 20 Oct 2022 05:31:29 -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 ESMTPS id 0A6CC157F6F for ; Thu, 20 Oct 2022 02:31:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666258283; 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=nm86sopHALiAvOTB3Mb1uaCRxtNMK3Osw7jYg3LC+M0=; b=PrBzeCYAoQuMxz23urWDMNkIm5te+d7ccZd/Ny3fT2+GBPnEdZQzALrJUB0+ZWxImEOq0R FxlTuhZwN9aiV2wCuoOqdZKlnGjeOvZ3/+qd0nm8GSV8P1te/gh8W9+vapaOfVD/6YmAEk iJ9eu6619IYI28GShB35+XZwssdMIUc= 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-423-MgE-q4rgP1SE4fwskpXN-A-1; Thu, 20 Oct 2022 05:31:18 -0400 X-MC-Unique: MgE-q4rgP1SE4fwskpXN-A-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9305729324B0; Thu, 20 Oct 2022 09:31:17 +0000 (UTC) Received: from localhost.localdomain (ovpn-192-51.brq.redhat.com [10.40.192.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id C4AAC49BB60; Thu, 20 Oct 2022 09:31:14 +0000 (UTC) From: Maxim Levitsky To: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Paolo Bonzini , Borislav Petkov , Ingo Molnar , Sean Christopherson , x86@kernel.org, Thomas Gleixner , Dave Hansen , "H. Peter Anvin" , Maxim Levitsky , stable@vger.kernel.org Subject: [PATCH 4/4] KVM: x86: forcibly leave nested mode on vCPU reset Date: Thu, 20 Oct 2022 12:30:55 +0300 Message-Id: <20221020093055.224317-5-mlevitsk@redhat.com> In-Reply-To: <20221020093055.224317-1-mlevitsk@redhat.com> References: <20221020093055.224317-1-mlevitsk@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747198924972366287?= X-GMAIL-MSGID: =?utf-8?q?1747198924972366287?= While not obivous, kvm_vcpu_reset leaves the nested mode by clearing 'vcpu->arch.hflags' but it does so without all the required housekeeping. This makes SVM and VMX continue to use vmcs02/vmcb02 while the cpu is not in nested mode. In particular, in SVM code, it makes the 'svm_free_nested' free the vmcb02, while still in use, which later triggers use after free and a kernel crash. This issue is assigned CVE-2022-3344 Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky --- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index d86a8aae1471d3..313c4a6dc65e45 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -11931,6 +11931,7 @@ void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) WARN_ON_ONCE(!init_event && (old_cr0 || kvm_read_cr3(vcpu) || kvm_read_cr4(vcpu))); + kvm_leave_nested(vcpu); kvm_lapic_reset(vcpu, init_event); vcpu->arch.hflags = 0;