From patchwork Tue Jun 16 15:35:45 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: 188065 Delivered-To: patch@linaro.org Received: by 2002:a92:cf06:0:0:0:0:0 with SMTP id c6csp3774404ilo; Tue, 16 Jun 2020 08:57:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJa2rIzM3ff31g3FY1q10IHnqbcYpzna3eIMKROoWnK3Ez7+aa0yDC71MII+BextvZoFrZ X-Received: by 2002:a17:907:35c2:: with SMTP id ap2mr3227694ejc.530.1592323036750; Tue, 16 Jun 2020 08:57:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592323036; cv=none; d=google.com; s=arc-20160816; b=wmGgtqqQ84hzFdsqQK7/jQa2GJE1ETSJwZ+IvRpbKTBvVkvOvm6f4lKWEH9xI6bXPh nr4bB6Bmpj3ir8BjULHcAiw2zYz2oT4b7ISVeQ6w80fvOOReA0X4HgtsgQB4N6kHzlw8 EldvhFS6thZ3ynD4c/mJGJdOG0H70Pr0EbxmrVTtY3DB9I++VkHWzbT1ky6lJGKC/kR3 6l8tG3WTlQaIgtJUtnMere80Ebo3Gksg5DALCI5JsnypuNA26icaIJJRfQLxKhxST7eH EgRUxsa1nYAF7KlAtGjPS9AhCJUHDbeIF/lyOwFusANgVPpuey6PIIK6crja3dfBECiV pLdg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=SvMZwzbLcFTmXjcesSRfaUcj7qgJ9aLr5XNntI/9P28=; b=GDUMGSGoy2UI9R28KZnzXxd2Zs2S16dBDalfqcHCmWHVgQfzrZlXb94L68zxvAO78c QBAnNOAlH883RoVkEnf0UlxGNDFcZFzLJtds8ToCi4CEqLVjmgla9/RuhB0+PB+m4CQe pcyMt5i7t5oY0AaECbqLnH7OP1dzvtcKw9HvR0ltSVX2wpbGXJfMDVVTifach8ZxUbmE kCVDqyX4/v8aXkOtvq0GZSiYbDIW4EN+/hxHYpx2oASaTTskvfrmChuR3LEJb+2YxHiE 5NvryuhIsgYCxldhay38zHx1M7pAHfwYA8ST5RIW4lG6YLxGwYVjY+a6i+35VuutluFZ iWxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DQAnkUuN; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i24si10755585eds.397.2020.06.16.08.57.16; Tue, 16 Jun 2020 08:57:16 -0700 (PDT) Received-SPF: pass (google.com: domain of stable-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=DQAnkUuN; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732744AbgFPP5P (ORCPT + 15 others); Tue, 16 Jun 2020 11:57:15 -0400 Received: from mail.kernel.org ([198.145.29.99]:55404 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732745AbgFPPzW (ORCPT ); Tue, 16 Jun 2020 11:55:22 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 E7F7521527; Tue, 16 Jun 2020 15:55:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1592322921; bh=7iKMm7KSRQUJ5Y+WeKndqu7co8tjv7zaxZdV9VUdtYA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DQAnkUuNOpzQg+iuUcLdTRFuKnhFqOYJhnwXrDRzWRLdgDVpo7o+3MRUPn7CiC5sz cp0B5ot9EkEFsq5uQzOh7oQsaRX3pzhrrEIXEr8vxLHrJmSSkf11P1p5fVhz6b7uy+ tc4QpsZBK8VUez0UEF7eELDJJZaAySliu/AmBvj4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ulf Hansson Subject: [PATCH 5.6 155/161] mmc: sdio: Fix several potential memory leaks in mmc_sdio_init_card() Date: Tue, 16 Jun 2020 17:35:45 +0200 Message-Id: <20200616153113.728946582@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200616153106.402291280@linuxfoundation.org> References: <20200616153106.402291280@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: Ulf Hansson commit a94a59f43749b4f8cd81b8be87c95f9ef898d19d upstream. Over the years, the code in mmc_sdio_init_card() has grown to become quite messy. Unfortunate this has also lead to that several paths are leaking memory in form of an allocated struct mmc_card, which includes additional data, such as initialized struct device for example. Unfortunate, it's a too complex task find each offending commit. Therefore, this change fixes all memory leaks at once. Cc: Signed-off-by: Ulf Hansson Link: https://lore.kernel.org/r/20200430091640.455-3-ulf.hansson@linaro.org Signed-off-by: Greg Kroah-Hartman --- drivers/mmc/core/sdio.c | 58 ++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) --- a/drivers/mmc/core/sdio.c +++ b/drivers/mmc/core/sdio.c @@ -584,7 +584,7 @@ try_again: */ err = mmc_send_io_op_cond(host, ocr, &rocr); if (err) - goto err; + return err; /* * For SPI, enable CRC as appropriate. @@ -592,17 +592,15 @@ try_again: if (mmc_host_is_spi(host)) { err = mmc_spi_set_crc(host, use_spi_crc); if (err) - goto err; + return err; } /* * Allocate card structure. */ card = mmc_alloc_card(host, NULL); - if (IS_ERR(card)) { - err = PTR_ERR(card); - goto err; - } + if (IS_ERR(card)) + return PTR_ERR(card); if ((rocr & R4_MEMORY_PRESENT) && mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) { @@ -610,19 +608,15 @@ try_again: if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { - mmc_remove_card(card); - pr_debug("%s: Perhaps the card was replaced\n", - mmc_hostname(host)); - return -ENOENT; + err = -ENOENT; + goto mismatch; } } else { card->type = MMC_TYPE_SDIO; if (oldcard && oldcard->type != MMC_TYPE_SDIO) { - mmc_remove_card(card); - pr_debug("%s: Perhaps the card was replaced\n", - mmc_hostname(host)); - return -ENOENT; + err = -ENOENT; + goto mismatch; } } @@ -677,7 +671,7 @@ try_again: if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { err = mmc_sd_get_csd(host, card); if (err) - return err; + goto remove; mmc_decode_cid(card); } @@ -704,7 +698,12 @@ try_again: mmc_set_timing(card->host, MMC_TIMING_SD_HS); } - goto finish; + if (oldcard) + mmc_remove_card(card); + else + host->card = card; + + return 0; } /* @@ -730,16 +729,14 @@ try_again: goto remove; if (oldcard) { - int same = (card->cis.vendor == oldcard->cis.vendor && - card->cis.device == oldcard->cis.device); - mmc_remove_card(card); - if (!same) { - pr_debug("%s: Perhaps the card was replaced\n", - mmc_hostname(host)); - return -ENOENT; + if (card->cis.vendor == oldcard->cis.vendor && + card->cis.device == oldcard->cis.device) { + mmc_remove_card(card); + card = oldcard; + } else { + err = -ENOENT; + goto mismatch; } - - card = oldcard; } card->ocr = ocr_card; mmc_fixup_device(card, sdio_fixup_methods); @@ -800,16 +797,15 @@ try_again: err = -EINVAL; goto remove; } -finish: - if (!oldcard) - host->card = card; + + host->card = card; return 0; +mismatch: + pr_debug("%s: Perhaps the card was replaced\n", mmc_hostname(host)); remove: - if (!oldcard) + if (oldcard != card) mmc_remove_card(card); - -err: return err; }