Skip to content

Commit a94a59f

Browse files
committed
mmc: sdio: Fix several potential memory leaks in mmc_sdio_init_card()
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: <stable@vger.kernel.org> Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org> Link: https://lore.kernel.org/r/20200430091640.455-3-ulf.hansson@linaro.org
1 parent f04086c commit a94a59f

File tree

1 file changed

+27
-31
lines changed

1 file changed

+27
-31
lines changed

drivers/mmc/core/sdio.c

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -584,45 +584,39 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
584584
*/
585585
err = mmc_send_io_op_cond(host, ocr, &rocr);
586586
if (err)
587-
goto err;
587+
return err;
588588

589589
/*
590590
* For SPI, enable CRC as appropriate.
591591
*/
592592
if (mmc_host_is_spi(host)) {
593593
err = mmc_spi_set_crc(host, use_spi_crc);
594594
if (err)
595-
goto err;
595+
return err;
596596
}
597597

598598
/*
599599
* Allocate card structure.
600600
*/
601601
card = mmc_alloc_card(host, NULL);
602-
if (IS_ERR(card)) {
603-
err = PTR_ERR(card);
604-
goto err;
605-
}
602+
if (IS_ERR(card))
603+
return PTR_ERR(card);
606604

607605
if ((rocr & R4_MEMORY_PRESENT) &&
608606
mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
609607
card->type = MMC_TYPE_SD_COMBO;
610608

611609
if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
612610
memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
613-
mmc_remove_card(card);
614-
pr_debug("%s: Perhaps the card was replaced\n",
615-
mmc_hostname(host));
616-
return -ENOENT;
611+
err = -ENOENT;
612+
goto mismatch;
617613
}
618614
} else {
619615
card->type = MMC_TYPE_SDIO;
620616

621617
if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
622-
mmc_remove_card(card);
623-
pr_debug("%s: Perhaps the card was replaced\n",
624-
mmc_hostname(host));
625-
return -ENOENT;
618+
err = -ENOENT;
619+
goto mismatch;
626620
}
627621
}
628622

@@ -677,7 +671,7 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
677671
if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
678672
err = mmc_sd_get_csd(host, card);
679673
if (err)
680-
return err;
674+
goto remove;
681675

682676
mmc_decode_cid(card);
683677
}
@@ -704,7 +698,12 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
704698
mmc_set_timing(card->host, MMC_TIMING_SD_HS);
705699
}
706700

707-
goto finish;
701+
if (oldcard)
702+
mmc_remove_card(card);
703+
else
704+
host->card = card;
705+
706+
return 0;
708707
}
709708

710709
/*
@@ -730,16 +729,14 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
730729
goto remove;
731730

732731
if (oldcard) {
733-
int same = (card->cis.vendor == oldcard->cis.vendor &&
734-
card->cis.device == oldcard->cis.device);
735-
mmc_remove_card(card);
736-
if (!same) {
737-
pr_debug("%s: Perhaps the card was replaced\n",
738-
mmc_hostname(host));
739-
return -ENOENT;
732+
if (card->cis.vendor == oldcard->cis.vendor &&
733+
card->cis.device == oldcard->cis.device) {
734+
mmc_remove_card(card);
735+
card = oldcard;
736+
} else {
737+
err = -ENOENT;
738+
goto mismatch;
740739
}
741-
742-
card = oldcard;
743740
}
744741
card->ocr = ocr_card;
745742
mmc_fixup_device(card, sdio_fixup_methods);
@@ -800,16 +797,15 @@ static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
800797
err = -EINVAL;
801798
goto remove;
802799
}
803-
finish:
804-
if (!oldcard)
805-
host->card = card;
800+
801+
host->card = card;
806802
return 0;
807803

804+
mismatch:
805+
pr_debug("%s: Perhaps the card was replaced\n", mmc_hostname(host));
808806
remove:
809-
if (!oldcard)
807+
if (oldcard != card)
810808
mmc_remove_card(card);
811-
812-
err:
813809
return err;
814810
}
815811

0 commit comments

Comments
 (0)