From patchwork Tue Sep 1 15:09:46 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: 264512 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 BEFDEC433E2 for ; Tue, 1 Sep 2020 16:56:47 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8F93E206C0 for ; Tue, 1 Sep 2020 16:56:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598979407; bh=9GAM1JHzIGSIwGdTlGYAZyH2tlkRvjrRR++SfcBfyMk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=BLUW7UsPUtX2J6Az+rRSqMUBMdOyp3NBPupQYe0ihtHT2S+L8xOYDFt5GLaEY36Tn jsuITvNQD7DIbPf9VkCe6lo950Qd8cyAPhncbyRBjYndl2aJYM1+vqBhj9vF3p/UV8 XoI4C21O8dWlKREDDKFlcmWYTO7kLNJdVt4nvStY= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729838AbgIAPXp (ORCPT ); Tue, 1 Sep 2020 11:23:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:46798 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728772AbgIAPXj (ORCPT ); Tue, 1 Sep 2020 11:23:39 -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 7E4822100A; Tue, 1 Sep 2020 15:23:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1598973817; bh=9GAM1JHzIGSIwGdTlGYAZyH2tlkRvjrRR++SfcBfyMk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pTij4m6ut12vw7XsqfAwv3QPhq8j6n/d0o7OfgJsOr8xvqUECMWXbrKfmBw7xH2T4 L+2+ZBkKyhoO5XWDjP1V5iyr1z+oECu6VPe1TiMvONmeecB224iyGg1QKUXB04r2nb drdA9Eu1myHGMdIyul+syShxB7CwKSlQ18X97dl0= 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 4.19 031/125] PCI: Fix pci_create_slot() reference count leak Date: Tue, 1 Sep 2020 17:09:46 +0200 Message-Id: <20200901150936.086735627@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200901150934.576210879@linuxfoundation.org> References: <20200901150934.576210879@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 a32897f83ee51..fb7478b6c4f9d 100644 --- a/drivers/pci/slot.c +++ b/drivers/pci/slot.c @@ -303,13 +303,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); @@ -328,7 +331,6 @@ out: mutex_unlock(&pci_slot_mutex); return slot; err: - kfree(slot); slot = ERR_PTR(err); goto out; }