From patchwork Tue Sep 1 15:08:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 310367 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, URIBL_BLOCKED, USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EB2AAC433E6 for ; Tue, 1 Sep 2020 16:11:49 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BE1AB2067C for ; Tue, 1 Sep 2020 16:11:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598976709; bh=I2d3MEcSO9Kgotp4Tjh60//FRQ6OJp3HSdHsGzGpsO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=SiIIJMgr6IcNs4kzKK6eszEYzrzdYwGtuU0mKZ4fB1cZRY8leMiSDAOiUO6ljB9s0 7iqDvQIQxAzXpI8NMpYSVIeqQJHAQ0AUFCkDMLUXIOBfatVz06U9B3UkdfaddwJpwq /cX4ozVfdS/kSg5IzsL9rMmIgYr1frNKEjvXS0mo= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728152AbgIAQLn (ORCPT ); Tue, 1 Sep 2020 12:11:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:45464 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731168AbgIAPh0 (ORCPT ); Tue, 1 Sep 2020 11:37:26 -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 13BCD21534; Tue, 1 Sep 2020 15:37:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598974645; bh=I2d3MEcSO9Kgotp4Tjh60//FRQ6OJp3HSdHsGzGpsO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sWe4D5B21o9f9Rwo4hjX+eKzQDKvWDErMzqkPmIzt7qL8FYLtHFYCOZbJHpmm50ED hsoaOjGVYK4AanTz9cKKac7JRJhBE1gaBrg4m7fTCBosgjW6WxKGv14DWv4Drebjmk 5HhSlRc3VTB4R/vl07Y08BR5/A5ptlhHGeBN+DQc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Qiushi Wu , Bjorn Helgaas , Sasha Levin Subject: [PATCH 5.8 040/255] PCI: Fix pci_create_slot() reference count leak Date: Tue, 1 Sep 2020 17:08:16 +0200 Message-Id: <20200901151002.671774167@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200901151000.800754757@linuxfoundation.org> References: <20200901151000.800754757@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Qiushi Wu [ Upstream commit 8a94644b440eef5a7b9c104ac8aa7a7f413e35e5 ] kobject_init_and_add() takes a reference even when it fails. If it returns an error, kobject_put() must be called to clean up the memory associated with the object. When kobject_init_and_add() fails, call kobject_put() instead of kfree(). b8eb718348b8 ("net-sysfs: Fix reference count leak in rx|netdev_queue_add_kobject") fixed a similar problem. Link: https://lore.kernel.org/r/20200528021322.1984-1-wu000273@umn.edu Signed-off-by: Qiushi Wu Signed-off-by: Bjorn Helgaas Signed-off-by: Sasha Levin --- drivers/pci/slot.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/pci/slot.c b/drivers/pci/slot.c index cc386ef2fa122..3861505741e6d 100644 --- a/drivers/pci/slot.c +++ b/drivers/pci/slot.c @@ -268,13 +268,16 @@ placeholder: slot_name = make_slot_name(name); if (!slot_name) { err = -ENOMEM; + kfree(slot); goto err; } err = kobject_init_and_add(&slot->kobj, &pci_slot_ktype, NULL, "%s", slot_name); - if (err) + if (err) { + kobject_put(&slot->kobj); goto err; + } INIT_LIST_HEAD(&slot->list); list_add(&slot->list, &parent->slots); @@ -293,7 +296,6 @@ out: mutex_unlock(&pci_slot_mutex); return slot; err: - kfree(slot); slot = ERR_PTR(err); goto out; }