From patchwork Tue Sep 1 15:08:40 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: 310293 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, 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 E451BC43461 for ; Tue, 1 Sep 2020 16:41:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B4EC120767 for ; Tue, 1 Sep 2020 16:41:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598978488; bh=eXkRyPhcUiQGNLNWpcl4I2tLXNoV/g7UbdgnwcGdQnU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=C7UochKprRJUOKwAItFhLWzYoMe+jajihtz4hyUly5EUzR5Jd5we0ep3psS/oa9Lj Rwdl4/gUBs0iGwuEddQP2CUzNygKTsAUJhA9zewMBR6ziGYZPR0CJbceE8VQK7nkGq zREMgQ9rdNMJ3jkdUyZRGTGVvTxELSII255PC2Ac= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730395AbgIAQlN (ORCPT ); Tue, 1 Sep 2020 12:41:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:56234 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730345AbgIAP2J (ORCPT ); Tue, 1 Sep 2020 11:28:09 -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 8A324207D3; Tue, 1 Sep 2020 15:28:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598974089; bh=eXkRyPhcUiQGNLNWpcl4I2tLXNoV/g7UbdgnwcGdQnU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t5ieOUoyPCR6lAMQC/13kqnv+BiHgZ8Ian9dp8Xjp9mV6Zs9nzWvYCVQTEZUrVzER 13tcOF5aannJxSQI2ka9Hj3684oLL8Wae+XZRZEIz6MwilWj4zIRjUic8+Z5OYyz3B qGbyjc5+XNF3l1zHqaMNRHHowd63Ipq+ehtIpENw= 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.4 040/214] PCI: Fix pci_create_slot() reference count leak Date: Tue, 1 Sep 2020 17:08:40 +0200 Message-Id: <20200901150954.885546013@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200901150952.963606936@linuxfoundation.org> References: <20200901150952.963606936@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 ae4aa0e1f2f42..1f087746b7bb0 100644 --- a/drivers/pci/slot.c +++ b/drivers/pci/slot.c @@ -304,13 +304,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); @@ -329,7 +332,6 @@ out: mutex_unlock(&pci_slot_mutex); return slot; err: - kfree(slot); slot = ERR_PTR(err); goto out; }