Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp601488img; Fri, 22 Mar 2019 04:57:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqzsv7sGHWfKnLRlgM77Zf2emWnw9M/mnkn+sVYm/SGDV3xkKfN0uPpbGdndefPt9xyIcdrL X-Received: by 2002:aa7:920b:: with SMTP id 11mr8497016pfo.3.1553255878629; Fri, 22 Mar 2019 04:57:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553255878; cv=none; d=google.com; s=arc-20160816; b=0wHFaOC1dGppyVZ/WdLhb3IGIFnucy3heX7zBZo3WvJQgCZvUcSEg0Yv4cRkJgmZEb o4URAaqu+REGJiCTjvv3NPcakGZvbjFhbnWuEQ2DE+p9ztLXufJElh7wEk4eiAAguXuW U6liYhw7aaai+KD+1MayBq7A3YHPltm3PIaegFfqOBt+ii/ZAcVaUqfCOB/85BlypW5X hqIzjdVxwkoojBaMe8Vt4FUYAtfU/Q96lLpwPMGAJcHiFggZrW25Rd8lCClCML+72i5H PFj5EP/9huvzHzdbKpV8IsnlkOnj9hrVwYAITOfvL46pB/vmBX4byJUK0yt/cjPBEDvC L4Jw== 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=uv4vPsMe4q9nPB/d0+aHBswvxE8i3xUGVPzEFAFoJjs=; b=OpOZ5A87LWG9O1f2TCL1p/rmOtjyJqr3kZzw2hXY0wmPW0U/2QQN9LAlZ4ZoQ05c8W nj9Bpf2oFowOuD0hldRb0FcAryKJSIJ7CTUvq0kHURuZr8j3FUwXPImHRF2C2cT8YVOu ynG0LwBx1iRprppZI+zCCcmfPxQkq/TP094gnMhG7aAvLdD4CogOjX8Rjo8mxxPxEngw VfiJEyWp5VyFYNH8M4c/krTrcofxvLCAWgr7KhOzCnJC8/6y++RlKCL9YFvNSLSmp8YW tVRGFj1vqwlwsRgsJAOEvf3Vh8eQY8LlG75PaRGjrNiCfCYAU6xN7hBUD4xnd6tFS1mo lPOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0nI8MHUd; 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 r15si6179531pgv.389.2019.03.22.04.57.43; Fri, 22 Mar 2019 04:57:58 -0700 (PDT) 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=0nI8MHUd; 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 S2387460AbfCVLzz (ORCPT + 99 others); Fri, 22 Mar 2019 07:55:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:60468 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387455AbfCVLzw (ORCPT ); Fri, 22 Mar 2019 07:55:52 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 AF06420850; Fri, 22 Mar 2019 11:55:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553255751; bh=OnmJRvAz8acHbCR5rhtzqT1Wj0i4mGPSwGj01sCgjKg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0nI8MHUdElu9nJth7Sw6VYPhKlgVqYEFnST2hocYZJvha71HQHSE7K70KPdbvghnf zIJ+hPNRCNitXqlxMZbxiNqvwDDyC/HM2X3wDipMh6qsWVdfjcsnMZL1AwU0gOQ7gm UMWkmuTj7lb57zp4mVx17Z+SHhPny4+grT4M8dgQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini Subject: [PATCH 4.14 180/183] KVM: nVMX: Sign extend displacements of VMX instrs mem operands Date: Fri, 22 Mar 2019 12:16:48 +0100 Message-Id: <20190322111254.928862343@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190322111241.819468003@linuxfoundation.org> References: <20190322111241.819468003@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Sean Christopherson commit 946c522b603f281195af1df91837a1d4d1eb3bc9 upstream. The VMCS.EXIT_QUALIFCATION field reports the displacements of memory operands for various instructions, including VMX instructions, as a naturally sized unsigned value, but masks the value by the addr size, e.g. given a ModRM encoded as -0x28(%ebp), the -0x28 displacement is reported as 0xffffffd8 for a 32-bit address size. Despite some weird wording regarding sign extension, the SDM explicitly states that bits beyond the instructions address size are undefined: In all cases, bits of this field beyond the instruction’s address size are undefined. Failure to sign extend the displacement results in KVM incorrectly treating a negative displacement as a large positive displacement when the address size of the VMX instruction is smaller than KVM's native size, e.g. a 32-bit address size on a 64-bit KVM. The very original decoding, added by commit 064aea774768 ("KVM: nVMX: Decoding memory operands of VMX instructions"), sort of modeled sign extension by truncating the final virtual/linear address for a 32-bit address size. I.e. it messed up the effective address but made it work by adjusting the final address. When segmentation checks were added, the truncation logic was kept as-is and no sign extension logic was introduced. In other words, it kept calculating the wrong effective address while mostly generating the correct virtual/linear address. As the effective address is what's used in the segment limit checks, this results in KVM incorreclty injecting #GP/#SS faults due to non-existent segment violations when a nested VMM uses negative displacements with an address size smaller than KVM's native address size. Using the -0x28(%ebp) example, an EBP value of 0x1000 will result in KVM using 0x100000fd8 as the effective address when checking for a segment limit violation. This causes a 100% failure rate when running a 32-bit KVM build as L1 on top of a 64-bit KVM L0. Fixes: f9eb4af67c9d ("KVM: nVMX: VMX instructions: add checks for #GP/#SS exceptions") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/vmx.c | 4 ++++ 1 file changed, 4 insertions(+) --- a/arch/x86/kvm/vmx.c +++ b/arch/x86/kvm/vmx.c @@ -7446,6 +7446,10 @@ static int get_vmx_mem_address(struct kv /* Addr = segment_base + offset */ /* offset = base + [index * scale] + displacement */ off = exit_qualification; /* holds the displacement */ + if (addr_size == 1) + off = (gva_t)sign_extend64(off, 31); + else if (addr_size == 0) + off = (gva_t)sign_extend64(off, 15); if (base_is_valid) off += kvm_register_read(vcpu, base_reg); if (index_is_valid)