Skip to contentSkip to conversation

mmc: core: fix possible use after free of host

PATCH Pan Bian 3 participants 3 messages Apr 17, 2019 – Apr 29, 2019 MMC, SD and SDIO

lore
Share & tools
lore

Pan Bian

author
#0 lore raw

In the function mmc_alloc_host, the function put_device is called to release allocated resources when mmc_gpio_alloc fails. Finally, the function pointed by host->class_dev.class->dev_release (i.e., mmc_host_classdev_release) is used to release resources including the host structure. However, after put_device, host is used and released again. Resulting in a use-after-free bug.

Fixes 1ed21719448("mmc: core: fix error path in mmc_host_alloc")
---
 drivers/mmc/core/host.c | 2 --
 1 file changed, 2 deletions(-)
drivers/mmc/core/host.c +0 -2
index 3a4402a..f8ac567 100644
@@ -429,8 +429,6 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 
 	if (mmc_gpio_alloc(host)) {
 		put_device(&host->class_dev);
-		ida_simple_remove(&mmc_host_ida, host->index);
-		kfree(host);
 		return NULL;
 	}
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 9 lines · Pan Bian

In the function mmc_alloc_host, the function put_device is called to release allocated resources when mmc_gpio_alloc fails. Finally, the function pointed by host->class_dev.class->dev_release (i.e., mmc_host_classdev_release) is used to release resources including the host structure. However, after put_device, host is used and released again. Resulting in a use-after-free bug.

Fixes 1ed21719448("mmc: core: fix error path in mmc_host_alloc")

The original commit 1ed21719448 looks correct to me (see mmc_host_classdev_release).

Quoted 22 lines
---
 drivers/mmc/core/host.c | 2 --
 1 file changed, 2 deletions(-)
drivers/mmc/core/host.c +0 -3
index 3a4402a..f8ac567 100644
@@ -429,8 +429,6 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 
        if (mmc_gpio_alloc(host)) {
                put_device(&host->class_dev);
-               ida_simple_remove(&mmc_host_ida, host->index);
-               kfree(host);
                return NULL;
        }
 
--

2.7.4

#2 lore raw
In reply to Pan Bian
Quoted 10 lines · Pan Bian

In the function mmc_alloc_host, the function put_device is called to release allocated resources when mmc_gpio_alloc fails. Finally, the function pointed by host->class_dev.class->dev_release (i.e., mmc_host_classdev_release) is used to release resources including the host structure. However, after put_device, host is used and released again. Resulting in a use-after-free bug.

Fixes 1ed21719448("mmc: core: fix error path in mmc_host_alloc")

Applied for next, thanks!

Kind regards
Uffe

Quoted 21 lines
---
 drivers/mmc/core/host.c | 2 --
 1 file changed, 2 deletions(-)
drivers/mmc/core/host.c +0 -3
index 3a4402a..f8ac567 100644
@@ -429,8 +429,6 @@ struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
 
        if (mmc_gpio_alloc(host)) {
                put_device(&host->class_dev);
-               ida_simple_remove(&mmc_host_ida, host->index);
-               kfree(host);
                return NULL;
        }
 
--

2.7.4