Received: by 2002:a25:b323:0:0:0:0:0 with SMTP id l35csp942739ybj; Fri, 20 Sep 2019 02:30:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqwU8S/5h+AT+6xspuLbyQ4GGdcvsFa669HAXaTyTEUYiBA1UrdT/UyGi/x6EdQoGsNM6iv3 X-Received: by 2002:a17:906:f13:: with SMTP id z19mr18622993eji.209.1568971845696; Fri, 20 Sep 2019 02:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568971845; cv=none; d=google.com; s=arc-20160816; b=n9x+328wTmVh8jAeu02k1BjnldzNCOk2XpIfiZzbrh/viGpKbj4J6TZXY34Rv6j9Nf ZbN6oxeqfIRT1oaxJAih3lkpDAKPLxNX184BnL19ZA6BtfDLurGAe8+Y5yjY2/cWby2A 44wPDR6yR8hISkQQ+t6wTelFyy97ZlqzYaI4j73hqNSD/nfKSTnopgIyJRJNbDoI1SRe SKIcZZVTmM/IA/vhmvOhNhmiaFJ4d+IyStNLwbkbryxuJkH24sMTqyicp62/nvaTI8FF gcqlBpg5L6gBNbn3sX1Kp1FRljvDfz2MVITmS34kHGDvlGWYpsaUfHA1KWDLOoxwPOSZ sy3w== 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=A3kG53KDbpNfnSpj9kCDXa/PjRpH+K8vjxBOdFEHXGE=; b=oQw4/Nmcj79IEjAMunicorlKGSRExLNymgg4c+gHpA4NhA7OV6e7iRmi/Q6nZ7r+ZR HPFyHp41K7z5D27Xw6HNloF/wFNsCFiAKduM3GrBel62X+zz2bilDDM56ibLb21arqI2 X6MOzHSdIrZQNcIwGm1cAr00BrDAxdtT0aRBX7/Vo2r6mTnCqksp+hIBuPT7i94Hrcol oYMZzxTuwt7SKfztdszPKjbruI/EtFKDIcvaoVguJNobZ7Zv/5PVFR7kuHDhVroJdM7B 1uE2AWGhh7Gm7y874FJAmE0/m/4kalxsIdmk/UMvF7diyk9GPQM26kpCoO+c0xwmlckU 5TCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Eenn43Dh; 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 a8si983276edn.332.2019.09.20.02.30.22; Fri, 20 Sep 2019 02:30:45 -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=Eenn43Dh; 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 S2405947AbfISWMQ (ORCPT + 99 others); Thu, 19 Sep 2019 18:12:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:50976 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405915AbfISWMH (ORCPT ); Thu, 19 Sep 2019 18:12:07 -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 8ABF121924; Thu, 19 Sep 2019 22:12:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1568931127; bh=ouKyXAVyj6EMO2IS6VD8fdjtqEOD8VGdbxLgDFJDtEY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Eenn43DhNO20kXvSGl13yA33TPV+RUhEzB+eB3QrD1IR18yEhnhWdwUxAXVwSAF4w LbFZ/cp45UP4vkSFocpNmcPA40I19zH/nf2VPhnFajP9srutUAREzo1G8J0+yA4xh4 ar9SJhgVZ3zPnsophrG1Os3Pz6pXdmpuJi2mnUfw= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Matt Delco , Jim Mattson , syzbot+983c866c3dd6efa3662a@syzkaller.appspotmail.com, Paolo Bonzini Subject: [PATCH 4.19 13/79] KVM: coalesced_mmio: add bounds checking Date: Fri, 20 Sep 2019 00:02:58 +0200 Message-Id: <20190919214808.978553296@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190919214807.612593061@linuxfoundation.org> References: <20190919214807.612593061@linuxfoundation.org> User-Agent: quilt/0.66 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 From: Matt Delco commit b60fe990c6b07ef6d4df67bc0530c7c90a62623a upstream. The first/last indexes are typically shared with a user app. The app can change the 'last' index that the kernel uses to store the next result. This change sanity checks the index before using it for writing to a potentially arbitrary address. This fixes CVE-2019-14821. Cc: stable@vger.kernel.org Fixes: 5f94c1741bdc ("KVM: Add coalesced MMIO support (common part)") Signed-off-by: Matt Delco Signed-off-by: Jim Mattson Reported-by: syzbot+983c866c3dd6efa3662a@syzkaller.appspotmail.com [Use READ_ONCE. - Paolo] Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- virt/kvm/coalesced_mmio.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) --- a/virt/kvm/coalesced_mmio.c +++ b/virt/kvm/coalesced_mmio.c @@ -40,7 +40,7 @@ static int coalesced_mmio_in_range(struc return 1; } -static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev) +static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev, u32 last) { struct kvm_coalesced_mmio_ring *ring; unsigned avail; @@ -52,7 +52,7 @@ static int coalesced_mmio_has_room(struc * there is always one unused entry in the buffer */ ring = dev->kvm->coalesced_mmio_ring; - avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX; + avail = (ring->first - last - 1) % KVM_COALESCED_MMIO_MAX; if (avail == 0) { /* full */ return 0; @@ -67,24 +67,27 @@ static int coalesced_mmio_write(struct k { struct kvm_coalesced_mmio_dev *dev = to_mmio(this); struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; + __u32 insert; if (!coalesced_mmio_in_range(dev, addr, len)) return -EOPNOTSUPP; spin_lock(&dev->kvm->ring_lock); - if (!coalesced_mmio_has_room(dev)) { + insert = READ_ONCE(ring->last); + if (!coalesced_mmio_has_room(dev, insert) || + insert >= KVM_COALESCED_MMIO_MAX) { spin_unlock(&dev->kvm->ring_lock); return -EOPNOTSUPP; } /* copy data in first free entry of the ring */ - ring->coalesced_mmio[ring->last].phys_addr = addr; - ring->coalesced_mmio[ring->last].len = len; - memcpy(ring->coalesced_mmio[ring->last].data, val, len); + ring->coalesced_mmio[insert].phys_addr = addr; + ring->coalesced_mmio[insert].len = len; + memcpy(ring->coalesced_mmio[insert].data, val, len); smp_wmb(); - ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX; + ring->last = (insert + 1) % KVM_COALESCED_MMIO_MAX; spin_unlock(&dev->kvm->ring_lock); return 0; }