Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3700068ybv; Mon, 10 Feb 2020 04:58:11 -0800 (PST) X-Google-Smtp-Source: APXvYqy4gw8btIhGpRHj1+TjZCQ5W/ggsRlgUWn3O+IAL+XXulZAHB7WOYFL0aBHePqNl5MCTInE X-Received: by 2002:aca:c256:: with SMTP id s83mr752504oif.57.1581339491321; Mon, 10 Feb 2020 04:58:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339491; cv=none; d=google.com; s=arc-20160816; b=ec90BZBHp/uCOJVo9fwsZsvvW+7ecqcWK0S62KutFQgHTqdMa+02kuwKG9av42655H ABpc8Cl0Xo+VZlvnXly1/96l6nZXNhAoTPJZyQLWbt2qeW+4x5f0YgAsqBZwtGaPHwpv 3QCaiB4xt+NqfIPedbqA2N52NjAj78kgHn7aK6IYeIf/Bn9i9eKp9Phg6b+I+hkVjxQ4 iRila5UKY9J8DznGJXRWj0YqLZ7ifHBk0bEk7jDnixr/FgwTFSc3M44wY8MtmSBWvl/u BzbtfbFqynDz0i+BjP3YmdTw0nqI6bjCUVPecpyXzZ053JMPdUFf9/8K+Z6eSJfPnxhZ VkRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=5+QmbFsd29eiaqn/svjnaUowo0tMUhim2AcEnFYYqRQ=; b=LKgegDAr0fPpJFQ/8EexwL14rhPb49ZoiGDg86jQDWXV3dqJQlr8rruCOExTahERWM v84CqStQaa6vD3jGbA1Y3GTQHTyDz71I7TxzgCY9RfHvT+F+fu1hyQTaQcXesPl0cSVQ EcVU/u73XvtYhY69anl9HJBWcvMDlFhJkEQEbiLzgQV3oNRPICbXaJdWCa47TN/eoX4t 5dG7Hv69JEEG+3n17tdXpHknmHt+a1nzVHdKGaLioboPLhlLttnx6JOR7NH0ck+auV8b EKKfrLltadlD8gD4jHdCsXeyUeCwtn7OsIZ6poXSEAi/rmwjY8GdAuHGOAUuRfxh8lxl acuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KkqUcIy6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z7si145902oth.181.2020.02.10.04.57.59; Mon, 10 Feb 2020 04:58:11 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KkqUcIy6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730121AbgBJM5q (ORCPT + 99 others); Mon, 10 Feb 2020 07:57:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:43642 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729984AbgBJMlZ (ORCPT ); Mon, 10 Feb 2020 07:41:25 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0AB5A21569; Mon, 10 Feb 2020 12:41:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338485; bh=+5KaXQ8TOffhLQARMIx5vFz0N/clqkg6YMZZF3j0SMk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KkqUcIy6/pGtoDdf5lImq+ji3Q0EGmaGMvbzLSycblMa3Z8ZjmmA4ReJ0HSiqIbWg xGdyfSiA0aUCKKr3j3me/fem88hpG7swgaqDJ1zjmWTt9XTVORL0CcjCaiCUb6KlpF NqGCcgqZgmNhZSDiq2jOG/auO76yLnSy/K93NQfQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Boris Ostrovsky , Joao Martins , Paolo Bonzini Subject: [PATCH 5.5 247/367] x86/KVM: Make sure KVM_VCPU_FLUSH_TLB flag is not missed Date: Mon, 10 Feb 2020 04:32:40 -0800 Message-Id: <20200210122446.990481051@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Boris Ostrovsky commit b043138246a41064527cf019a3d51d9f015e9796 upstream. There is a potential race in record_steal_time() between setting host-local vcpu->arch.st.steal.preempted to zero (i.e. clearing KVM_VCPU_PREEMPTED) and propagating this value to the guest with kvm_write_guest_cached(). Between those two events the guest may still see KVM_VCPU_PREEMPTED in its copy of kvm_steal_time, set KVM_VCPU_FLUSH_TLB and assume that hypervisor will do the right thing. Which it won't. Instad of copying, we should map kvm_steal_time and that will guarantee atomicity of accesses to @preempted. This is part of CVE-2019-3016. Signed-off-by: Boris Ostrovsky Reviewed-by: Joao Martins Cc: stable@vger.kernel.org Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/x86.c | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2588,45 +2588,47 @@ static void kvm_vcpu_flush_tlb(struct kv static void record_steal_time(struct kvm_vcpu *vcpu) { + struct kvm_host_map map; + struct kvm_steal_time *st; + if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) return; - if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime, - &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)))) + /* -EAGAIN is returned in atomic context so we can just return. */ + if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, + &map, &vcpu->arch.st.cache, false)) return; + st = map.hva + + offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); + /* * Doing a TLB flush here, on the guest's behalf, can avoid * expensive IPIs. */ trace_kvm_pv_tlb_flush(vcpu->vcpu_id, - vcpu->arch.st.steal.preempted & KVM_VCPU_FLUSH_TLB); - if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB) + st->preempted & KVM_VCPU_FLUSH_TLB); + if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) kvm_vcpu_flush_tlb(vcpu, false); - if (vcpu->arch.st.steal.version & 1) - vcpu->arch.st.steal.version += 1; /* first time write, random junk */ + vcpu->arch.st.steal.preempted = 0; - vcpu->arch.st.steal.version += 1; + if (st->version & 1) + st->version += 1; /* first time write, random junk */ - kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime, - &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)); + st->version += 1; smp_wmb(); - vcpu->arch.st.steal.steal += current->sched_info.run_delay - + st->steal += current->sched_info.run_delay - vcpu->arch.st.last_steal; vcpu->arch.st.last_steal = current->sched_info.run_delay; - kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime, - &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)); - smp_wmb(); - vcpu->arch.st.steal.version += 1; + st->version += 1; - kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime, - &vcpu->arch.st.steal, sizeof(struct kvm_steal_time)); + kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false); } int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) @@ -3511,18 +3513,25 @@ void kvm_arch_vcpu_load(struct kvm_vcpu static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) { + struct kvm_host_map map; + struct kvm_steal_time *st; + if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) return; if (vcpu->arch.st.steal.preempted) return; - vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED; + if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map, + &vcpu->arch.st.cache, true)) + return; + + st = map.hva + + offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); + + st->preempted = vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED; - kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime, - &vcpu->arch.st.steal.preempted, - offsetof(struct kvm_steal_time, preempted), - sizeof(vcpu->arch.st.steal.preempted)); + kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true); } void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)