Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4584296pxj; Wed, 12 May 2021 08:41:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwYGn2XUhh8PFdSDppeO4724JQyG10DoEWGq1OKDsDSbbQ/9VEhe/DP3sV176KODLbw/7dK X-Received: by 2002:a17:906:3e97:: with SMTP id a23mr38717240ejj.440.1620834075879; Wed, 12 May 2021 08:41:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620834075; cv=none; d=google.com; s=arc-20160816; b=aoi4sTZPShbtCLY5UTyH+0SZZsaoUx6+QPHmL6aIehLbR8ncUKcvfVoSvD4cdXlDAO eTd+SwNCD2St95z5qferEUISCQmYzxnpgi2Ox7D40Om7C8jxOWzRM47cxm9yt7K8fRuW 8stv1GCeYtbV/ILzwtngZWsUQxwjedVhOOHthjHqbSfsIUd1JL1+Omp/YnuV3izrOIr6 DOwdfsSh6qj97hmPbSrYS0DQ5nPx1f5G+ylG2f1Nsl547GfGi/kxgOakIUWCAY4ScTP1 X6NFDB2cay8zLayp5KVHiUG9ecdtdmdHoiK4CNyPriaQqAz3J8mEPAq7ksnk9/pRhgcr qXuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=KEcUysVOBdqYnOgO84bOK2UUnUl27tHGTKjYY+vuJ9c=; b=bxVzvBqvz0SIidN5Z3ufB8OYnsqe+Dr6ov9OFmTcejRGazwYrX0ceN/2kxxNL2DHBs gBSG5cypkJ7UkUXFW319i1kSii7jVny+OYsBiKIX8QugOJCDfz+cFG8OSLlyh3E2GXAR cmObPB8vtqOtB0bSCcIG/RjuhD6kca8gvCx3KobOWs/IUhVja1/spliR61Gw84jI/q1Y yucg6j2bcISg53Bi6GzcMQZFPEmcN8HMzb0qh9XU3tHuoeNKbDMai09/SFFw7OIiCqx+ hyN6yMPqNm/p7/EyJceN74VOpB5m7OASU1gbKiHVzA31og3issDnqkZEDdYrg1e6O9Q3 9dJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AD9HdzyN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z6si213029ejf.385.2021.05.12.08.40.52; Wed, 12 May 2021 08:41:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=AD9HdzyN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230501AbhELPiH (ORCPT + 99 others); Wed, 12 May 2021 11:38:07 -0400 Received: from mail.kernel.org ([198.145.29.99]:50550 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234411AbhELPQs (ORCPT ); Wed, 12 May 2021 11:16:48 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A55E861955; Wed, 12 May 2021 15:06:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620832004; bh=/E1tmdReGz+1h0t+MJ+ZpO0Bryrlag9Cmbo86gm/fPc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AD9HdzyNXPec9vaFNCZYewlDiMK2MDUMaKU80tpNpQwHr43wEtiYJuifKVvWxTmny ccc8L5mrhKtPCH2WeQfHYJ0QR7Qw35rAasZmNvdVbbLdG7t53j51E+vmC3jZ4setmF gB7OEZ6Vv7fuZV10yghqtNMMXjvMJ4ziwgrdJyUk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Sean Christopherson , Paolo Bonzini Subject: [PATCH 5.10 101/530] KVM: Destroy I/O bus devices on unregister failure _after_ syncing SRCU Date: Wed, 12 May 2021 16:43:31 +0200 Message-Id: <20210512144823.120673628@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144819.664462530@linuxfoundation.org> References: <20210512144819.664462530@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson commit 2ee3757424be7c1cd1d0bbfa6db29a7edd82a250 upstream. If allocating a new instance of an I/O bus fails when unregistering a device, wait to destroy the device until after all readers are guaranteed to see the new null bus. Destroying devices before the bus is nullified could lead to use-after-free since readers expect the devices on their reference of the bus to remain valid. Fixes: f65886606c2d ("KVM: fix memory leak in kvm_io_bus_unregister_dev()") Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Message-Id: <20210412222050.876100-2-seanjc@google.com> Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- virt/kvm/kvm_main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4367,7 +4367,13 @@ void kvm_io_bus_unregister_dev(struct kv new_bus->dev_count--; memcpy(new_bus->range + i, bus->range + i + 1, flex_array_size(new_bus, range, new_bus->dev_count - i)); - } else { + } + + rcu_assign_pointer(kvm->buses[bus_idx], new_bus); + synchronize_srcu_expedited(&kvm->srcu); + + /* Destroy the old bus _after_ installing the (null) bus. */ + if (!new_bus) { pr_err("kvm: failed to shrink bus, removing it completely\n"); for (j = 0; j < bus->dev_count; j++) { if (j == i) @@ -4376,8 +4382,6 @@ void kvm_io_bus_unregister_dev(struct kv } } - rcu_assign_pointer(kvm->buses[bus_idx], new_bus); - synchronize_srcu_expedited(&kvm->srcu); kfree(bus); return; }