Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp824810pxv; Fri, 9 Jul 2021 09:51:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwGffZCo7Uavut4mYo64/+JLecolxcU0a4l1TGK2dsFWcNynCIMNgbumPxivAIa7TirWhjR X-Received: by 2002:a17:906:6d0e:: with SMTP id m14mr3570662ejr.551.1625849481248; Fri, 09 Jul 2021 09:51:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625849481; cv=none; d=google.com; s=arc-20160816; b=Pl2UJla0jsbLBqdz8hUvL/rmG3NaCRiUhLnE8RRO+nKmeY70Bay+uXrwAJ4XLlZG5R dtoZZXy6UCL19W6YHeZG32Q0chhlhSrtZjvUrpnHBuLIZ7MYccHIN6ywa5MWsEbaMq02 EupYuKz3YFjT1lRqNeCLZbTlWrNoVLmDvUma/v0/ubfT45PO0VtWWK0OfACnJezHSBmu VXDJcMjuIcReXZYPHgwLFOPDiyxJqfv8Bs7D4KSoz9kdR2pPpldXU6WZABWrK3LHP7yR EQ5t8jl8im2jUGLIyjbPe6WcBpP/WIC6COQSelQkamKVdrZMF8hg+cbQxad96rOAHyMH 4hzQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=y8ksef0vKPdeGZiNhS+5SAqzB66QJi7/d7HDlmfKEi0=; b=haX8v8+QVQ+7PvFG92qRFQ1ULnjfJ318Gq9q0cGWrMyspuBcyoCu9I0dKExWQY99Ph im055B0xjYvAMTgCKRk632ZspVzFHj2qqLxn+jvlJnetrHhlNI1idIwyiK4zQwU5uUtd 853v45KFI3Cfj7DjOpoG3254tnV1S5n9PTQiad2sPZf3+PHOE3jLdxgPbQG98+lXRco8 clVooU9V8Wg+vqKPXb/82Yp346EjNvE9Ys9DsC9RH/1bBGWGiWpVyEhb8Bu5qBRrDW3U Z0Dd5pxoZaDgQlhFQxN2TPZvjTZyQhJ81npx347EXR0M39QvhNG3+JYoHgkVkiKsg5EQ T5VQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FcsaLTHB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s10si239714edx.308.2021.07.09.09.50.55; Fri, 09 Jul 2021 09:51:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FcsaLTHB; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229546AbhGIQwa (ORCPT + 99 others); Fri, 9 Jul 2021 12:52:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229459AbhGIQw3 (ORCPT ); Fri, 9 Jul 2021 12:52:29 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6032CC0613E5 for ; Fri, 9 Jul 2021 09:49:45 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id h1so5316845plf.6 for ; Fri, 09 Jul 2021 09:49:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=y8ksef0vKPdeGZiNhS+5SAqzB66QJi7/d7HDlmfKEi0=; b=FcsaLTHBk7svPb76TOzEFx6WM9ZjHU1kpCMOBH2/zRIGGeIrdUHC+MC6r7To6OMsTg TLoPCKGbLNt9Ifig3FcGfc9HtaOOWRd1Gk+erZ/U1A4K+10CshBvNH9Zgf4/GZ6j3mGc kIK3MqKa0bayr1N3tvspTxbXqq1ODv9FIW845O1E5JFnKBjJXr93YOUMaXsCWKvcdAOx T4ZqvW+tmSdkL6IYQEu+sT912VPb2Tw8kpq2L7blv+L4ZClgMDlidsEf8DvNknuxR+v4 5bhwjGU6qIJdMOC5xuRPkJTKGYdhIzDV/VDr3gZGHrC2vEXm7yb1cm+f57iqoed28F3K 2zAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=y8ksef0vKPdeGZiNhS+5SAqzB66QJi7/d7HDlmfKEi0=; b=RMTWxajj/84KsRYfyAXx89+xabJwVagMQFCQ/aeMlB+MFCZvRCNCW8OV4uEjHctLKM CfWGQN23VpfbMEhwyDbzsfOLWYkIJeIYMaVZFQp0ym1QuPJQ5yGyugdg7wU1jydgzs95 BcFB0WQWBhDwHV7Cjz+5WTBKHixet0XJhn99ACEQbulzuPAsSr3Z2dVXmwFbO0OukvQJ NmLjvptzI4cRGOoMTjQ3gTucKZxaPmOAsik3REI1oKUAYZGf+6XWc2RsVh6PxNJT8J8J FZJ8t/TCBh8LQzsNY6LXUczvU7D7kSQvz1OZFOhUpTQXyiyAlMSzr5V0dnuaIHwgAZJ0 Tzaw== X-Gm-Message-State: AOAM532QgVqz67WcQooBbZzpWdp9dq2Hdvtm7N+etFQfJGN2qtIgClIg nRlWQS1IUiQk6Z+QR349ReGU7g== X-Received: by 2002:a17:902:fe0a:b029:11d:81c9:3adf with SMTP id g10-20020a170902fe0ab029011d81c93adfmr31730627plj.0.1625849384437; Fri, 09 Jul 2021 09:49:44 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id i27sm7936474pgl.78.2021.07.09.09.49.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Jul 2021 09:49:43 -0700 (PDT) Date: Fri, 9 Jul 2021 16:49:40 +0000 From: Sean Christopherson To: Paolo Bonzini Cc: Lai Jiangshan , linux-kernel@vger.kernel.org, Lai Jiangshan , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , kvm@vger.kernel.org Subject: Re: [PATCH] KVM: X86: Also reload the debug registers before kvm_x86->run() when the host is using them Message-ID: References: <20210628172632.81029-1-jiangshanlai@gmail.com> <46e0aaf1-b7cd-288f-e4be-ac59aa04908f@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46e0aaf1-b7cd-288f-e4be-ac59aa04908f@redhat.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 08, 2021, Paolo Bonzini wrote: > On 28/06/21 19:26, Lai Jiangshan wrote: > > From: Lai Jiangshan > > > > When the host is using debug registers but the guest is not using them > > nor is the guest in guest-debug state, the kvm code does not reset > > the host debug registers before kvm_x86->run(). Rather, it relies on > > the hardware vmentry instruction to automatically reset the dr7 registers > > which ensures that the host breakpoints do not affect the guest. > > > > But there are still problems: > > o The addresses of the host breakpoints can leak into the guest > > and the guest may use these information to attack the host. > > I don't think this is true, because DRn reads would exit (if they don't, > switch_db_regs would be nonzero). But otherwise it makes sense to do at > least the DR7 write, and we might as well do all of them. > > > o It violates the non-instrumentable nature around VM entry and > > exit. For example, when a host breakpoint is set on > > vcpu->arch.cr2, #DB will hit aftr kvm_guest_enter_irqoff(). > > > > Beside the problems, the logic is not consistent either. When the guest > > debug registers are active, the host breakpoints are reset before > > kvm_x86->run(). But when the guest debug registers are inactive, the > > host breakpoints are delayed to be disabled. The host tracing tools may > > see different results depending on there is any guest running or not. > > More precisely, the host tracing tools may see different results depending > on what the guest is doing. > > Queued (with fixed commit message), thanks! > > Paolo > > > To fix the problems, we also reload the debug registers before > > kvm_x86->run() when the host is using them whenever the guest is using > > them or not. > > > > Signed-off-by: Lai Jiangshan > > --- > > arch/x86/kvm/x86.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index b594275d49b5..cce316655d3c 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -9320,7 +9320,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) > > if (test_thread_flag(TIF_NEED_FPU_LOAD)) > > switch_fpu_return(); > > - if (unlikely(vcpu->arch.switch_db_regs)) { > > + if (unlikely(vcpu->arch.switch_db_regs || hw_breakpoint_active())) { > > set_debugreg(0, 7); I would prefer zero only dr7, e.g. if (unlikely(vcpu->arch.switch_db_regs)) { ... } else if (hw_breakpoint_active()) { set_debugreg(0, 7); } Stuffing all DRs isn't a bug because hw_breakpoint_restore() will restore all DRs, but loading stale state into DRs is weird. > > set_debugreg(vcpu->arch.eff_db[0], 0); > > set_debugreg(vcpu->arch.eff_db[1], 1); > > >