Received: by 10.192.165.148 with SMTP id m20csp2545451imm; Sun, 22 Apr 2018 09:15:47 -0700 (PDT) X-Google-Smtp-Source: AIpwx49fj23G4hoGCJQaaGrLvKNYBtxoeerLCYOJdOIQFbYXkfHiK7xR7akplGOV5sx8ijbpyLoc X-Received: by 2002:a17:902:8501:: with SMTP id bj1-v6mr17470194plb.239.1524413747488; Sun, 22 Apr 2018 09:15:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524413747; cv=none; d=google.com; s=arc-20160816; b=CRnKcr2uEcSh8v+IEqbOJAKBOO99261PRQwbqHQ+0+r5WpFkfd7EJYwHzyLaURp98x uSONSCU6orGL5kN6GWbQLV3v0aKJbX9kfF/lqhK3SeqdznyYW2S+3jV4e90GNE9fFxRX YeEVfjPehM58IaFE7TOdtfq2673RUB5OML+M7jW/r22EqfqCb+e0iUmUQ762FUl8VVp+ 2IrUFJEM8ZngAX45g8riQjMZGh9jO2X+aUPdXEYkMDQuqFeYMct6AC5kMpx3RD4zF210 Ey6DNP/KYT0+s5EcJ/idzSRRbBmMbOIBSOVetAiwKTxTObHhXT9x3kMX0hu8Srj5kJVz Q8pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=1lYg5E+5VWNasRe7isAByzuB4vMmFUGD+xwfes83xWg=; b=RpiaGtxdfd5M/R6qgLH3AnjEU36ckIsL2c9jxlRlpkEJnJbWC/ylKuo/lLsBZkP0l2 d0Jf6g2IXjnSMHgDaWc0H4MY8sB9KqYRmZh8S9baBr7+7enUD/xILufABYtXXxsvyWq2 BcEfE6gJ7GT+jsy9OD3d3+bjNMex+3/KL4i/QG3W2J/ij3WAACG04yXM3jv4k5Rdt3l2 EQkklY4SXq7V/IoXQyL8t23PrBgoCcGe51R0lfbt9jqa7aVwe0YfElgRnUT76aHIPUfD YaCl53u/Ik5WwTr3a+IEEawi5zjzXou308UN0HIMOSVob8j9K9dBXpaDQDPxuVFJkkVs WaAg== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m198si8060773pga.107.2018.04.22.09.15.33; Sun, 22 Apr 2018 09:15:47 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753486AbeDVNzS (ORCPT + 99 others); Sun, 22 Apr 2018 09:55:18 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:44492 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753399AbeDVNzM (ORCPT ); Sun, 22 Apr 2018 09:55:12 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id D452B87A; Sun, 22 Apr 2018 13:55:11 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andre Przywara , Eric Auger , Marc Zyngier Subject: [PATCH 4.16 021/196] KVM: arm/arm64: vgic-its: Fix potential overrun in vgic_copy_lpi_list Date: Sun, 22 Apr 2018 15:50:41 +0200 Message-Id: <20180422135105.224574864@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180422135104.278511750@linuxfoundation.org> References: <20180422135104.278511750@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Marc Zyngier commit 7d8b44c54e0c7c8f688e3a07f17e6083f849f01f upstream. vgic_copy_lpi_list() parses the LPI list and picks LPIs targeting a given vcpu. We allocate the array containing the intids before taking the lpi_list_lock, which means we can have an array size that is not equal to the number of LPIs. This is particularly obvious when looking at the path coming from vgic_enable_lpis, which is not a command, and thus can run in parallel with commands: vcpu 0: vcpu 1: vgic_enable_lpis its_sync_lpi_pending_table vgic_copy_lpi_list intids = kmalloc_array(irq_count) MAPI(lpi targeting vcpu 0) list_for_each_entry(lpi_list_head) intids[i++] = irq->intid; At that stage, we will happily overrun the intids array. Boo. An easy fix is is to break once the array is full. The MAPI command will update the config anyway, and we won't miss a thing. We also make sure that lpi_list_count is read exactly once, so that further updates of that value will not affect the array bound check. Cc: stable@vger.kernel.org Fixes: ccb1d791ab9e ("KVM: arm64: vgic-its: Fix pending table sync") Reviewed-by: Andre Przywara Reviewed-by: Eric Auger Signed-off-by: Marc Zyngier Signed-off-by: Greg Kroah-Hartman --- virt/kvm/arm/vgic/vgic-its.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) --- a/virt/kvm/arm/vgic/vgic-its.c +++ b/virt/kvm/arm/vgic/vgic-its.c @@ -316,21 +316,24 @@ static int vgic_copy_lpi_list(struct kvm struct vgic_dist *dist = &vcpu->kvm->arch.vgic; struct vgic_irq *irq; u32 *intids; - int irq_count = dist->lpi_list_count, i = 0; + int irq_count, i = 0; /* - * We use the current value of the list length, which may change - * after the kmalloc. We don't care, because the guest shouldn't - * change anything while the command handling is still running, - * and in the worst case we would miss a new IRQ, which one wouldn't - * expect to be covered by this command anyway. + * There is an obvious race between allocating the array and LPIs + * being mapped/unmapped. If we ended up here as a result of a + * command, we're safe (locks are held, preventing another + * command). If coming from another path (such as enabling LPIs), + * we must be careful not to overrun the array. */ + irq_count = READ_ONCE(dist->lpi_list_count); intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL); if (!intids) return -ENOMEM; spin_lock(&dist->lpi_list_lock); list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { + if (i == irq_count) + break; /* We don't need to "get" the IRQ, as we hold the list lock. */ if (irq->target_vcpu != vcpu) continue;