From patchwork Thu Apr 16 11:30:44 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marcelo Tosatti X-Patchwork-Id: 18505 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n3GBV1w5014927 for ; Thu, 16 Apr 2009 11:31:02 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753779AbZDPLbA (ORCPT ); Thu, 16 Apr 2009 07:31:00 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753723AbZDPLbA (ORCPT ); Thu, 16 Apr 2009 07:31:00 -0400 Received: from mx2.redhat.com ([66.187.237.31]:49353 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753212AbZDPLa7 (ORCPT ); Thu, 16 Apr 2009 07:30:59 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n3GBUxV3013458 for ; Thu, 16 Apr 2009 07:30:59 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n3GBUwiE009953; Thu, 16 Apr 2009 07:30:58 -0400 Received: from amt.cnet (vpn-10-194.str.redhat.com [10.32.10.194]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n3GBUunp026693; Thu, 16 Apr 2009 07:30:57 -0400 Received: from amt.cnet (amt.cnet [127.0.0.1]) by amt.cnet (Postfix) with ESMTP id 26BC5681244; Thu, 16 Apr 2009 08:30:46 -0300 (BRT) Received: (from marcelo@localhost) by amt.cnet (8.14.3/8.14.3/Submit) id n3GBUixA011214; Thu, 16 Apr 2009 08:30:44 -0300 Date: Thu, 16 Apr 2009 08:30:44 -0300 From: Marcelo Tosatti To: Avi Kivity Cc: kvm Subject: KVM: x86: check for cr3 validity in ioctl_set_sregs Message-ID: <20090416113044.GA11200@amt.cnet> References: <20090415221042.GA20127@amt.cnet> <49E6F2AF.9050300@redhat.com> <20090416091042.GA6709@amt.cnet> <49E6F92C.2000202@redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <49E6F92C.2000202@redhat.com> User-Agent: Mutt/1.5.18 (2008-05-17) X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Matt T. Yourst notes that kvm_arch_vcpu_ioctl_set_sregs lacks validity checking for the new cr3 value: "Userspace callers of KVM_SET_SREGS can pass a bogus value of cr3 to the kernel. This will trigger a NULL pointer access in gfn_to_rmap() when userspace next tries to call KVM_RUN on the affected VCPU and kvm attempts to activate the new non-existent page table root. This happens since kvm only validates that cr3 points to a valid guest physical memory page when code *inside* the guest sets cr3. However, kvm currently trusts the userspace caller (e.g. QEMU) on the host machine to always supply a valid page table root, rather than properly validating it along with the rest of the reloaded guest state." http://sourceforge.net/tracker/?func=detail&atid=893831&aid=2687641&group_id=180599 Check for a valid cr3 address in kvm_arch_vcpu_ioctl_set_sregs, triple fault in case of failure. Signed-off-by: Marcelo Tosatti --- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: kvm/arch/x86/kvm/x86.c =================================================================== --- kvm.orig/arch/x86/kvm/x86.c +++ kvm/arch/x86/kvm/x86.c @@ -3986,7 +3986,13 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct vcpu->arch.cr2 = sregs->cr2; mmu_reset_needed |= vcpu->arch.cr3 != sregs->cr3; - vcpu->arch.cr3 = sregs->cr3; + + down_read(&vcpu->kvm->slots_lock); + if (gfn_to_memslot(vcpu->kvm, sregs->cr3 >> PAGE_SHIFT)) + vcpu->arch.cr3 = sregs->cr3; + else + set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests); + up_read(&vcpu->kvm->slots_lock); kvm_set_cr8(vcpu, sregs->cr8);