Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp600270imm; Wed, 29 Aug 2018 07:38:43 -0700 (PDT) X-Google-Smtp-Source: ANB0VdapllkDGk5qBjsbV8KUKiQzQ5LbxdE0BlRSaYoT6bsIEVWdlu0FiX7G8oz6hFxebVsophpI X-Received: by 2002:a17:902:5381:: with SMTP id c1-v6mr6211531pli.328.1535553523186; Wed, 29 Aug 2018 07:38:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535553523; cv=none; d=google.com; s=arc-20160816; b=WVhNwYezFlbMBYVwYp+rB6abX6lI7D//5FmQZsmLtm8SoBCsgPi4RlzJ+luG/oE8Yx 5uhyhOA4G1wT/GV2siMTCU0WzF9lsBekNjvQ4nN8xlSG9ikYS4rC7F6/o2/lkB+ZI24J 3PAuPWZSLOKMltIciMj9I0ObdvlVMHQ5wfLs3Gf/RoiLV3iP11Xs1lQLaTPI4UoPDZWC VKuOCusUHFHamwEFlOqsm9S6nKg4FhAjGRevY/QiriY5mpNXfLx4WzJMbcpMrc81wCA+ NjnfC/uz4anIT+z2wvEpScgaPxyGDjUjZqEI4j4tHN/m2Num2LWVClkT+9xTZmtJAK/0 jh/w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=GS3utXi9Gpq7VWQYgi+riEsCf4DXgOReyTxtIoUSl9w=; b=ib5zONkqhegFsxa6M0O00Vh99lDhBCknNvJ3l2PPUEFgeD0j5ExC8VNQq+Eetl39jo r0MRF2/U2tvQeiOjsHlsGTwr4AsLic85NvLA9FjdKkOnRn6kgx4adHfxDvObp91LMaQD WxoSBPe0YtjfE2lV83oOTIno8pAexBYTyX55V31d5hDRPnz7icCQ5qTZ5ZtVqje6Lbbp P2hA6rCuJ/VQe+rWC5IpBPkFCwHlVgpbuPaQi9jZdtDzZftssUK8Iaim8s1e/qKfP51H 5HTUNKWHDI4/rqyvU5+DFntYNlgLUVpOyPHcZ3R9IBQzgE9nnvZ/6oY0P0/p6Moc1NS4 n94g== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u7-v6si4095510pfb.227.2018.08.29.07.38.28; Wed, 29 Aug 2018 07:38:43 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728750AbeH2SeP (ORCPT + 99 others); Wed, 29 Aug 2018 14:34:15 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:59902 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727537AbeH2SeP (ORCPT ); Wed, 29 Aug 2018 14:34:15 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B162740241EA; Wed, 29 Aug 2018 14:36:59 +0000 (UTC) Received: from flask (unknown [10.40.205.185]) by smtp.corp.redhat.com (Postfix) with SMTP id 65F28621BD; Wed, 29 Aug 2018 14:36:56 +0000 (UTC) Received: by flask (sSMTP sendmail emulation); Wed, 29 Aug 2018 16:36:55 +0200 Date: Wed, 29 Aug 2018 16:36:55 +0200 From: Radim Krcmar To: Liran Alon Cc: Dan Carpenter , Wanpeng Li , LKML , kvm , Paolo Bonzini Subject: Re: [PATCH] KVM: LAPIC: Fix pv ipis out-of-bounds access Message-ID: <20180829143653.GB15829@flask> References: <1535521943-5547-1-git-send-email-wanpengli@tencent.com> <20180829101205.jsp53e2wq7fc6ukd@mwanda> <20180829101822.qo3u7lsmghs3kcuf@mwanda> <20180829102910.rkyato47chayt22s@mwanda> <485CA660-9423-45B7-848F-49E3D13D5CBD@oracle.com> <20180829135539.GA15829@flask> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180829135539.GA15829@flask> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 29 Aug 2018 14:36:59 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.7]); Wed, 29 Aug 2018 14:36:59 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'rkrcmar@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2018-08-29 15:55+0200, Radim Krcmar: > 2018-08-29 13:43+0300, Liran Alon: > > Why is “min” defined as “int” instead of “unsigned int”? > > It represents the lowest APIC ID in bitmap so it can’t be negative… > > Right, > > I think the code would look better as something like (untested): > > diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c > index 0cefba28c864..24fc84eb97d2 100644 > --- a/arch/x86/kvm/lapic.c > +++ b/arch/x86/kvm/lapic.c > @@ -548,7 +548,7 @@ int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, > } > > int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, > - unsigned long ipi_bitmap_high, int min, > + unsigned long ipi_bitmap_high, u32 min, > unsigned long icr, int op_64_bit) > { > int i; > @@ -557,6 +557,7 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, > struct kvm_lapic_irq irq = {0}; > int cluster_size = op_64_bit ? 64 : 32; > int count = 0; > + unsigned long ipi_bitmap[2] = {ipi_bitmap_low, ipi_bitmap_high}; The patch is wrong, I missed the 32/64 bit cluster size. It's salvageable with something like if (op_64_bit) { ipi_bitmap[0] = ipi_bitmap_low; ipi_bitmap[1] = ipi_bitmap_high; ipi_bitmap_size = 128; } else { ipi_bitmap[0] = (u32)ipi_bitmap_low | ipi_bitmap_high << 32; ipi_bitmap_size = 64; } > > irq.vector = icr & APIC_VECTOR_MASK; > irq.delivery_mode = icr & APIC_MODE_MASK; > @@ -571,16 +572,14 @@ int kvm_pv_send_ipi(struct kvm *kvm, unsigned long ipi_bitmap_low, > rcu_read_lock(); > map = rcu_dereference(kvm->arch.apic_map); > > - /* Bits above cluster_size are masked in the caller. */ > - for_each_set_bit(i, &ipi_bitmap_low, BITS_PER_LONG) { > - vcpu = map->phys_map[min + i]->vcpu; > - count += kvm_apic_set_irq(vcpu, &irq, NULL); > - } > + if (min <= map->max_apic_id) { > + size_t ipi_bitmap_size = MIN(sizeof(ipi_bitmap) * 8, > + map->max_apic_id - min + 1); > - min += cluster_size; > - for_each_set_bit(i, &ipi_bitmap_high, BITS_PER_LONG) { > - vcpu = map->phys_map[min + i]->vcpu; > - count += kvm_apic_set_irq(vcpu, &irq, NULL); > + for_each_set_bit(i, ipi_bitmap, ipi_bitmap_size) { and ... MIN(ipi_bitmap_size, map->max_apic_id - min + 1) ... Not good, but could still be nicer than the alternatives. > + vcpu = map->phys_map[min + i]->vcpu; > + count += kvm_apic_set_irq(vcpu, &irq, NULL); > + } > } > > rcu_read_unlock();