Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3088336imu; Wed, 7 Nov 2018 04:59:43 -0800 (PST) X-Google-Smtp-Source: AJdET5fIMGaAmMXIrFyEc0s6hhxdeMrkZ1RuoNmEz9SS/+uvancbFy99v7o3LXKa8yLu5Z6txc6F X-Received: by 2002:a63:5153:: with SMTP id r19mr73461pgl.281.1541595583250; Wed, 07 Nov 2018 04:59:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541595583; cv=none; d=google.com; s=arc-20160816; b=hHv6LsZVA/b69RAouoA0/LzNOh3mkLFS+pPF46WsCEl6LNoZgmTTM2qZIUo/FxwupF pwBFneg87MnXh87S0ZSawhWdljklTAHC6W3VgBGYLPBsd6N+IAcd0F1HH1ejFUGE9bP3 yOXxVj1DYjQyYQNNd+09fbLsVIyTFDscH0Cn77Ai0BuRw71GchSmZMreu5KXFIxR8p1K 9OHTb8XcK5ItKxs4u7BHlfTqJTM9ifs496wEJDDPNnLzdOrLfOEGPKt6IAd7wHdPEtm0 oAUJP10F9+4yOK6dLZelgJBveeiSj18nENn+Dq8W2vjHEZzEY/eYgTNFPj8QiXXYO0OE 5lEw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:references:message-id :content-transfer-encoding:cc:date:in-reply-to:from:subject :mime-version:dkim-signature; bh=nKud3UATwc76gkx5/Pz33XLfjBqEZFTNvU/O8gvjEOQ=; b=Sx0EzWvLej0Uael4qj4tTmpbylrtN4wM1mCTQCtMV/nFp5S6qwLidXvL/y5u1o1Vgd KMSprlp94/vJASsbc3XP1VQsaonJaQJsDk6EtL5J/R5x2YAVniFvP6zO5yvrR7fw2uUU nfjHfacdz9YlBMdxQwYGKunvEwcaRuft4W97HoZPbfaAhhmepfOiOofxUAI0lVsLm2zi zn91ve3oY5m28PSVNG+fUqyNWKoOoExmgvSkPUxH2ZjuaBdW2e2Ofu79nasNSUsHG6xp lCA4H8OJcu1qgf4R9RLUxQM4sWxyyoVfaEJwip/a4qGNZlZ4Aaq0ZpGZ41Aub/8JFdGS Yggg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=zBm7EwhQ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g6-v6si523390plt.212.2018.11.07.04.59.27; Wed, 07 Nov 2018 04:59:43 -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=@oracle.com header.s=corp-2018-07-02 header.b=zBm7EwhQ; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727178AbeKGW3W (ORCPT + 99 others); Wed, 7 Nov 2018 17:29:22 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:49502 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726411AbeKGW3W (ORCPT ); Wed, 7 Nov 2018 17:29:22 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id wA7CmdVe167337; Wed, 7 Nov 2018 12:58:57 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=content-type : mime-version : subject : from : in-reply-to : date : cc : content-transfer-encoding : message-id : references : to; s=corp-2018-07-02; bh=nKud3UATwc76gkx5/Pz33XLfjBqEZFTNvU/O8gvjEOQ=; b=zBm7EwhQY6oTeA+kQSlM6G+js3byfTyZ5Fao4YFUdk5u64yxeBGCSJ3l+s+n6v5WZyjc oKoVVcy3SqS1XDY/SVdEQKMz6SZz0BNRuv/JFLcbEwOI2cpevIueuE1++k+QKLO/5ozP jDuYPhvWEjxSVCxzmN3BzH+0GyOolVWnGAckPYdwPzz2tkGREWyYkjVqd26E9HsvI7xk f2aEpkziwlb1UBzH2JaXM8P/slOYYnLW4Wo7BABjeUFb1ntdn0zXFiXMImPXT+4xRG0X MLLK844fuj3IHvTr8t7xi9dt7zDO3LTRT5sH4Yw+wvDqbXQpPyonBOU5DPRnq9VzXoCn zg== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by userp2120.oracle.com with ESMTP id 2nh4aqu667-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 07 Nov 2018 12:58:57 +0000 Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id wA7CwtVu019637 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 7 Nov 2018 12:58:56 GMT Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id wA7Cwtvp026430; Wed, 7 Nov 2018 12:58:55 GMT Received: from lirans-mbp.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 07 Nov 2018 04:58:55 -0800 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.1 \(3445.4.7\)) Subject: Re: KMSAN: kernel-infoleak in kvm_vcpu_write_guest_page From: Liran Alon In-Reply-To: <9d2e26fb-1d2a-248f-5451-ee95d8a6c017@redhat.com> Date: Wed, 7 Nov 2018 14:58:52 +0200 Cc: Alexander Potapenko , syzbot+ded1696f6b50b615b630@syzkaller.appspotmail.com, kvm@vger.kernel.org, LKML , rkrcmar@redhat.com, syzkaller-bugs@googlegroups.com Content-Transfer-Encoding: quoted-printable Message-Id: <5A114BC0-96E7-41AF-A975-EC3B87A5A60D@oracle.com> References: <0000000000005de8da057a092ba2@google.com> <9d2e26fb-1d2a-248f-5451-ee95d8a6c017@redhat.com> To: Paolo Bonzini X-Mailer: Apple Mail (2.3445.4.7) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9069 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1811070117 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On 7 Nov 2018, at 14:47, Paolo Bonzini wrote: >=20 > On 07/11/2018 13:10, Alexander Potapenko wrote: >> This appears to be a real bug in KVM. >> Please see a simplified reproducer attached. >=20 > Thanks, I agree it's a reael bug. The basic issue is that the > kvm_state->size member is too small (1040) in the KVM_SET_NESTED_STATE > ioctl, aka 0x4080aebf. >=20 > One way to fix it would be to just change kmalloc to kzalloc when > allocating cached_vmcs12 and cached_shadow_vmcs12, but really the = ioctl > is wrong and should be rejected. And the case where a shadow VMCS has > to be loaded is even more wrong, and we have to fix it anyway, so I > don't really like the idea of papering over the bug in the allocation. >=20 > I'll test this patch and submit it formally: >=20 > diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c > index c645f777b425..c546f0b1f3e0 100644 > --- a/arch/x86/kvm/vmx.c > +++ b/arch/x86/kvm/vmx.c > @@ -14888,10 +14888,13 @@ static int vmx_set_nested_state(struct > kvm_vcpu *vcpu, > if (ret) > return ret; >=20 > - /* Empty 'VMXON' state is permitted */ > - if (kvm_state->size < sizeof(kvm_state) + sizeof(*vmcs12)) > + /* Empty 'VMXON' state is permitted. A partial VMCS12 is not. = */ > + if (kvm_state->size =3D=3D sizeof(kvm_state)) > return 0; >=20 > + if (kvm_state->size < sizeof(kvm_state) + VMCS12_SIZE) > + return -EINVAL; > + I don=E2=80=99t think that this test is sufficient to fully resolve = issue. What if malicious userspace supplies valid size but pages containing = nested_state->vmcs12 is unmapped? This will result in vmx_set_nested_state() still calling = set_current_vmptr() but failing on copy_from_user() which still leaks cached_vmcs12 on next VMPTRLD of guest. Therefore, I think that the correct patch should be to change = vmx_set_nested_state() to first gather all relevant information from userspace and validate it, and only then start applying it to KVM=E2=80=99s internal vCPU state. > if (kvm_state->vmx.vmcs_pa !=3D -1ull) { > if (kvm_state->vmx.vmcs_pa =3D=3D = kvm_state->vmx.vmxon_pa || > !page_address_valid(vcpu, kvm_state->vmx.vmcs_pa)) > @@ -14917,6 +14920,7 @@ static int vmx_set_nested_state(struct = kvm_vcpu > *vcpu, > } >=20 > vmcs12 =3D get_vmcs12(vcpu); > + BUILD_BUG_ON(sizeof(*vmcs12) > VMCS12_SIZE); Why put this BUILD_BUG_ON() specifically here? There are many places which assumes cached_vmcs12 is of size = VMCS12_SIZE. (Such as nested_release_vmcs12() and handle_vmptrld()). > if (copy_from_user(vmcs12, user_kvm_nested_state->data, = sizeof(*vmcs12))) > return -EFAULT; >=20 > @@ -14932,7 +14936,7 @@ static int vmx_set_nested_state(struct = kvm_vcpu > *vcpu, > if (nested_cpu_has_shadow_vmcs(vmcs12) && > vmcs12->vmcs_link_pointer !=3D -1ull) { > struct vmcs12 *shadow_vmcs12 =3D = get_shadow_vmcs12(vcpu); > - if (kvm_state->size < sizeof(kvm_state) + 2 * = sizeof(*vmcs12)) > + if (kvm_state->size < sizeof(kvm_state) + 2 * = VMCS12_SIZE) > return -EINVAL; >=20 > if (copy_from_user(shadow_vmcs12, >=20 > Paolo -Liran