Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1084939pxk; Fri, 25 Sep 2020 06:00:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzaApbpUyeZmnDjtEUNUV27RRBazP4DSaRjVQqP1vJAXWh6B5W0BIaBX3ZDvauR+a6cBUsI X-Received: by 2002:a17:906:9701:: with SMTP id k1mr2759212ejx.0.1601038802090; Fri, 25 Sep 2020 06:00:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1601038802; cv=none; d=google.com; s=arc-20160816; b=j70XJSuscaeAcT8g3X/8su5jqQstYiZNWvvy+/KWhqz9VGfS2FXN0BytlG4PYT2Zwm 8nM2P1W+em8SDgicMzm0kEU+IVx8Bws4zxwyjFPo9Ym0Mbjrk81i1hRWduMn8YHB5PwO 92Q/MQZYjM77ogNCPmnQ4oRcNQPh7k2YvKXazWSAxfDshBLIs2QSZWrZKL/Xi4yH1s/B H6woxExI57xn8xGus14NvqgJ1C8F9d/ALy1aceZuMJb4Ri9Nm3bA4e1HcvDRvZhTyM8o WO20biCrQ1g6OM2b9/QuclDaGy+T6aVo5Eu2ZKobuY6rfvB3kHL8hfoBn6X95umt9vhx 4CDw== 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=AoBNImYebUEXkj8zm3J0Eg9v5uB342O+E2DCd8gsexY=; b=AYFvPfWfqLVDsKaunUb1uDhUiD2T5avx4c70U7GX3BqjkeN6LP8rJngdl9WG30eyXH FeP2iTZQXlkApRfo+9BBC5h/qFjZGGQCTkVH146rF/mCL1u8G1xCltOIJEm1Bc6N+hsc mRlHj4uQWn0CpE69iGl2D9iPH6+XhabB/+HfL+nTeMQvpa/Sii06ZwQxG9b0kVxs0fyM UVcE3+ZIE5dPeYyxu/+Yte+lhsDXZgbZFcoUZFQi7Tqp6TJ+mq7d988Mc/TxGQ7nXSuF 061FK/rmN4DHedn/wtO/pZ0FDGxxKKdwMWfQe5Kyaon8D1Efdn6M3ZYjQtGnN7nTj0vz op0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="g/e+O0NF"; 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=fail (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 wq8si1823476ejb.480.2020.09.25.05.59.39; Fri, 25 Sep 2020 06:00:02 -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=@kernel.org header.s=default header.b="g/e+O0NF"; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729290AbgIYM4M (ORCPT + 99 others); Fri, 25 Sep 2020 08:56:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:59322 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729444AbgIYMxg (ORCPT ); Fri, 25 Sep 2020 08:53:36 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (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 B678A21741; Fri, 25 Sep 2020 12:53:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1601038416; bh=G0cp1vQPLNVOtvJJKq/Ho+5w4LU7UHa+SivaCfjnP+g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g/e+O0NFkeF66/mMBBBSiv14HpAu1kKNY1oo/o3mxbUmFbjkLwysZpApnd0MGvnwR K1q2Jl2plrjsAkZX08FezT0apYdLtpKkk9i9vxtvT1yK4eEJzCmKrE38HX+5yuexa4 dnWKIThRkY7MaF6tXRso6vCYPjnANh+aRxGZzYfg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Rustam Kovhaev , Vitaly Kuznetsov , Paolo Bonzini , Sasha Levin , syzbot+f196caa45793d6374707@syzkaller.appspotmail.com Subject: [PATCH 4.19 02/37] KVM: fix memory leak in kvm_io_bus_unregister_dev() Date: Fri, 25 Sep 2020 14:48:30 +0200 Message-Id: <20200925124721.325359894@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200925124720.972208530@linuxfoundation.org> References: <20200925124720.972208530@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: Rustam Kovhaev [ Upstream commit f65886606c2d3b562716de030706dfe1bea4ed5e ] when kmalloc() fails in kvm_io_bus_unregister_dev(), before removing the bus, we should iterate over all other devices linked to it and call kvm_iodevice_destructor() for them Fixes: 90db10434b16 ("KVM: kvm_io_bus_unregister_dev() should never fail") Cc: stable@vger.kernel.org Reported-and-tested-by: syzbot+f196caa45793d6374707@syzkaller.appspotmail.com Link: https://syzkaller.appspot.com/bug?extid=f196caa45793d6374707 Signed-off-by: Rustam Kovhaev Reviewed-by: Vitaly Kuznetsov Message-Id: <20200907185535.233114-1-rkovhaev@gmail.com> Signed-off-by: Paolo Bonzini Signed-off-by: Sasha Levin --- virt/kvm/kvm_main.c | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2155b52b17eca..6bd01d12df2ec 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3844,7 +3844,7 @@ int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr, void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, struct kvm_io_device *dev) { - int i; + int i, j; struct kvm_io_bus *new_bus, *bus; bus = kvm_get_bus(kvm, bus_idx); @@ -3861,17 +3861,20 @@ void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx, new_bus = kmalloc(sizeof(*bus) + ((bus->dev_count - 1) * sizeof(struct kvm_io_range)), GFP_KERNEL); - if (!new_bus) { + if (new_bus) { + memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range)); + new_bus->dev_count--; + memcpy(new_bus->range + i, bus->range + i + 1, + (new_bus->dev_count - i) * sizeof(struct kvm_io_range)); + } else { pr_err("kvm: failed to shrink bus, removing it completely\n"); - goto broken; + for (j = 0; j < bus->dev_count; j++) { + if (j == i) + continue; + kvm_iodevice_destructor(bus->range[j].dev); + } } - memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range)); - new_bus->dev_count--; - memcpy(new_bus->range + i, bus->range + i + 1, - (new_bus->dev_count - i) * sizeof(struct kvm_io_range)); - -broken: rcu_assign_pointer(kvm->buses[bus_idx], new_bus); synchronize_srcu_expedited(&kvm->srcu); kfree(bus); -- 2.25.1