From patchwork Fri Sep 17 12:03:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emanuele Giuseppe Esposito X-Patchwork-Id: 12501701 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=-16.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 D18ADC433EF for ; Fri, 17 Sep 2021 12:03:41 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AAA9F61029 for ; Fri, 17 Sep 2021 12:03:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243998AbhIQMFC (ORCPT ); Fri, 17 Sep 2021 08:05:02 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:35507 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235036AbhIQME7 (ORCPT ); Fri, 17 Sep 2021 08:04:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1631880217; 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; bh=i2+E4iw4IwPmea7ZaJQqlz/wwifxDZQ9dTKq7+3EUpE=; b=bfWUwUG79VqymhPVsawn1pgPdZ+smMqlMT6R86TkXC2hTX97fpusCAKYeyi6mKLP1ruUnv bAAdRnXoYGWB+c20YpxEoTgprCh17KPQIS0/diI8THpmMO7snse3MggN/NBnpkY+rUIoJZ 2IxxPeDar8FRI9+u+weK8MtOlfu6qAA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-48-EfX0AjqQPse9pyRJtacd_g-1; Fri, 17 Sep 2021 08:03:35 -0400 X-MC-Unique: EfX0AjqQPse9pyRJtacd_g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D6D6EDF8A0; Fri, 17 Sep 2021 12:03:33 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 18FEF100164A; Fri, 17 Sep 2021 12:03:32 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Maxim Levitsky , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [PATCH v2 0/4] KVM: nSVM: avoid TOC/TOU race when checking vmcb12 Date: Fri, 17 Sep 2021 08:03:25 -0400 Message-Id: <20210917120329.2013766-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Currently there is a TOC/TOU race between the check of vmcb12's efer, cr0 and cr4 registers and the later save of their values in svm_set_*, because the guest could modify the values in the meanwhile. To solve this issue, this serie introuces and uses svm->nested.save structure in enter_svm_guest_mode to save the current value of efer, cr0 and cr4 and later use these to set the vcpu->arch.* state. Patch 1 just refactor the code to simplify the next two patches, patch 2 introduces svm->nested.save to cache the efer, cr0 and cr4 fields and in patch 3 and 4 we use it to avoid TOC/TOU races. Signed-off-by: Emanuele Giuseppe Esposito --- RFC: * use svm->nested.save instead of local variables. * not dependent anymore from "KVM: nSVM: remove useless kvm_clear_*_queue" * simplified patches, we just use the struct and not move the check nearer to the TOU. v2: * svm->nested.save is a separate struct vmcb_save_area_cached, and not vmcb_save_area. * update also vmcb02->cr3 with svm->nested.save.cr3 Emanuele Giuseppe Esposito (4): KVM: nSVM: move nested_vmcb_check_cr3_cr4 logic in nested_vmcb_valid_sregs nSVM: introduce smv->nested.save to cache save area fields nSVM: use vmcb_save_area_cached in nested_vmcb_valid_sregs() nSVM: use svm->nested.save to load vmcb12 registers and avoid TOC/TOU races arch/x86/kvm/svm/nested.c | 95 +++++++++++++++++++++------------------ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 12 +++++ 3 files changed, 64 insertions(+), 44 deletions(-)