Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp431560imm; Wed, 29 Aug 2018 03:45:16 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbMKBStBctgduLDRCrpsHpukrE0ovhWw81og3tqrQsTzYk63t5i44AKw2rGqUBHElK25SV2 X-Received: by 2002:a17:902:7683:: with SMTP id m3-v6mr5490821pll.255.1535539516221; Wed, 29 Aug 2018 03:45:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535539516; cv=none; d=google.com; s=arc-20160816; b=RE/mbECAWE/RmfM25i+fzehIF9WiXfC2iJ+zzJsB/rT+Waj6ouMjsBIhkxmuRrdSEO He3UwEnQjQ7ioPFgoAZ22am3XJTaOeXpblh+vEyHsVWA8LX5e3ci9pkoCH1wxNBSLj+b 9CHn0gnFYbFNad+p5gm4wUDaF7AB9kx0L2ebXOWAL5OBrsT7TVcU9y5Y7/kB0LdZcgFV Cp6/Pdgt/YTEDyXZpIdwDmhiLALJiw3FuhvMTinU/wlPxgoCADwE4ONpojnNO9fBsCBO 9TMsUjF0K5fMK4kMGL7mfl7Z/vDMLzxgfMeaFQmuBpr21p1x3EM4BMvpPkTe3joInWot 0FIw== 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:arc-authentication-results; bh=RDpOnvCDyWDT788rVRzX/abois3l1Ut7PyjWTdR2uNc=; b=Wd7NCFuteqvJaPQmd08wfXUjOimOLhRP2+jgI+MEv8sXJ436b3sER+bwF/QHK3hBlE Em7XOcFEAhnRnJ9G8+vluoo0TLvE5gggD64LsTEZHxgC3rrrpYJcyCY0S7/r2LSlzjwr UPhIEpRs1+1wZxT45VIqXeRwtB3yf+3wPi0SM7a4g4+bl/y8iUAEhQ/Qyu3vSSJU6LH6 IWXa9tcykxr3/tPj6GCcQL3EQ39cjeEAwB/mjQmzUjjYKjduaT6EqRZQPPpvWiEPuSZR At89Ozh/YEI0OW4U1s2yvGfXoVngZNNtuDW2K31Z6uo3XEgcx6oVaFumgIcPcT32XK7k F3hQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2018-07-02 header.b=SjcjKomH; 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 e67-v6si3324849plb.272.2018.08.29.03.45.00; Wed, 29 Aug 2018 03:45:16 -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=@oracle.com header.s=corp-2018-07-02 header.b=SjcjKomH; 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 S1728217AbeH2Ojx (ORCPT + 99 others); Wed, 29 Aug 2018 10:39:53 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:47024 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726858AbeH2Ojx (ORCPT ); Wed, 29 Aug 2018 10:39:53 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w7TAhU67025773; Wed, 29 Aug 2018 10:43:32 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=RDpOnvCDyWDT788rVRzX/abois3l1Ut7PyjWTdR2uNc=; b=SjcjKomHEOsbNvOStZ5rZZHcpUhn8e79UpxrigVz5k8KFjp8LMzfO5wkX/wDvY71EkYm eBozdOqe8tmS1D8UMe7OlAaf93I9y5Wd2H5SUKWxWBav8+o8Q+M22ghEuwSw1yv6kqVD LRYvV7RQav98EdSI1pw/OOZt++8q8qbVSf+8+PNEKKrJlIj0/qY1TC75RXQ+LYVhWQry OuIJsFpOt4krM7YHCjVhdBjW7D45DdJnh4FQW5dRFvtvAW4wdogSjp+lmc/nZD8/YMKf uX2lMOHU6QodxVB+Ryuc7DL8PxgDdH1iDl9dVFx73BUjcogQbKPXmi170cVxZSx+lzya 7w== Received: from userv0022.oracle.com (userv0022.oracle.com [156.151.31.74]) by aserp2120.oracle.com with ESMTP id 2m2y2ph0tf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 29 Aug 2018 10:43:32 +0000 Received: from userv0122.oracle.com (userv0122.oracle.com [156.151.31.75]) by userv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w7TAhQwl005758 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 29 Aug 2018 10:43:26 GMT Received: from abhmp0005.oracle.com (abhmp0005.oracle.com [141.146.116.11]) by userv0122.oracle.com (8.14.4/8.14.4) with ESMTP id w7TAhQOF019862; Wed, 29 Aug 2018 10:43:26 GMT Received: from lirans-mbp.ravello.local (/213.57.127.2) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 29 Aug 2018 03:43:25 -0700 Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 11.1 \(3445.4.7\)) Subject: Re: [PATCH] KVM: LAPIC: Fix pv ipis out-of-bounds access From: Liran Alon In-Reply-To: <20180829102910.rkyato47chayt22s@mwanda> Date: Wed, 29 Aug 2018 13:43:21 +0300 Cc: Wanpeng Li , LKML , kvm , Paolo Bonzini , Radim Krcmar Content-Transfer-Encoding: quoted-printable Message-Id: <485CA660-9423-45B7-848F-49E3D13D5CBD@oracle.com> References: <1535521943-5547-1-git-send-email-wanpengli@tencent.com> <20180829101205.jsp53e2wq7fc6ukd@mwanda> <20180829101822.qo3u7lsmghs3kcuf@mwanda> <20180829102910.rkyato47chayt22s@mwanda> To: Dan Carpenter X-Mailer: Apple Mail (2.3445.4.7) X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=8999 signatures=668708 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=783 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1808290118 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > On 29 Aug 2018, at 13:29, Dan Carpenter = wrote: >=20 > On Wed, Aug 29, 2018 at 06:23:08PM +0800, Wanpeng Li wrote: >> On Wed, 29 Aug 2018 at 18:18, Dan Carpenter = wrote: >>>=20 >>> On Wed, Aug 29, 2018 at 01:12:05PM +0300, Dan Carpenter wrote: >>>> On Wed, Aug 29, 2018 at 12:05:06PM +0300, Liran Alon wrote: >>>>>> arch/x86/kvm/lapic.c | 17 +++++++++++++---- >>>>>> 1 file changed, 13 insertions(+), 4 deletions(-) >>>>>>=20 >>>>>> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c >>>>>> index 0cefba2..86e933c 100644 >>>>>> --- a/arch/x86/kvm/lapic.c >>>>>> +++ b/arch/x86/kvm/lapic.c >>>>>> @@ -571,18 +571,27 @@ int kvm_pv_send_ipi(struct kvm *kvm, = unsigned long ipi_bitmap_low, >>>>>> rcu_read_lock(); >>>>>> map =3D rcu_dereference(kvm->arch.apic_map); >>>>>>=20 >>>>>> + if (unlikely((s32)(map->max_apic_id - __fls(ipi_bitmap_low)) < = min)) >>>>>> + goto out; >>>>>=20 >>>>> I personally think =E2=80=9Cif ((min + __fls(ipi_bitmap_low)) > = map->max_apic_id)=E2=80=9D is more readable. >>>>> But that=E2=80=99s just a matter of taste :) >>>>=20 >>>> That's an integer overflow. >>>>=20 >>>> But I do prefer to put the variable on the left. The truth is that = some >>>> Smatch checks just ignore code which is backwards written because >>>> otherwise you have to write duplicate code and the most code is = written >>>> with the variable on the left. >>>>=20 >>>> if (min > (s32)(map->max_apic_id - __fls(ipi_bitmap_low)) >>>=20 >>> Wait, the (s32) cast doesn't make sense. We want negative min = values to >>> be treated as invalid. >>=20 >> In v2, how about: >>=20 >> if (unlikely(min > map->max_apic_id || (min + __fls(ipi_bitmap_low)) = > >> map->max_apic_id)) >> goto out; >=20 > That works, too. It still has the off by one and we should set > "count =3D -KVM_EINVAL;". >=20 > Is the unlikely() really required? I don't know what the fast paths = are > in KVM, so I don't know. >=20 > regards, > dan carpenter Why is =E2=80=9Cmin=E2=80=9D defined as =E2=80=9Cint=E2=80=9D instead of = =E2=80=9Cunsigned int=E2=80=9D? It represents the lowest APIC ID in bitmap so it can=E2=80=99t be = negative=E2=80=A6 "if (unlikely(min > map->max_apic_id || (min + __fls(ipi_bitmap_low)) > = map->max_apic_id))=E2=80=9D should indeed be ok. -Liran