Skip to contentSkip to conversation

mmc: moxart: fix potential use-after-free on remove path.

PATCH In review Greg Kroah-Hartman 2 participants 7 messages Feb 23 – Mar 9 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: moxart: fix potential use-after-free on remove path.

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

Cc Ulf Hansson <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000
---
 drivers/mmc/host/moxart-mmc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/moxart-mmc.c +1 -1
index 3dd8f232052f..256e16390ef3 100644
@@ -690,12 +690,12 @@ static void moxart_remove(struct platform_device *pdev)
 		dma_release_channel(host->dma_chan_tx);
 	if (!IS_ERR_OR_NULL(host->dma_chan_rx))
 		dma_release_channel(host->dma_chan_rx);
-	mmc_remove_host(mmc);
 
 	writel(0, host->base + REG_INTERRUPT_MASK);
 	writel(0, host->base + REG_POWER_CONTROL);
 	writel(readl(host->base + REG_CLOCK_CONTROL) | CLK_OFF,
 	       host->base + REG_CLOCK_CONTROL);
+	mmc_remove_host(mmc);
 }
 
 static const struct of_device_id moxart_mmc_match[] = {
Signature · 1 line
2.53.0
#1 lore raw
Quoted 5 lines · Greg Kroah-Hartman

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

mmc_remove_host() doesn't actually free the host, but it reverses what mmc_add_host() did during probe.

Since the moxart driver uses devm_mmc_alloc_host() the last reference to the host will be dropped after ->remove() completes, leading to mmc_free_host() to be called for it.

However, improvements can still be made in the ->remove() callback. See below.

Quoted 4 lines
Cc Ulf Hansson <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

What's this?

Quoted 10 lines
---
 drivers/mmc/host/moxart-mmc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/moxart-mmc.c +0 -0
index 3dd8f232052f..256e16390ef3 100644

drivers/mmc/host/moxart-mmc.c static void moxart_remove(struct platform_device *pdev) · line 698

Quoted 10 lines
drivers/mmc/host/moxart-mmc.c +1 -1
@@ -690,12 +690,12 @@ static void moxart_remove(struct platform_device *pdev)
                dma_release_channel(host->dma_chan_tx);
        if (!IS_ERR_OR_NULL(host->dma_chan_rx))
                dma_release_channel(host->dma_chan_rx);
-       mmc_remove_host(mmc);
 
        writel(0, host->base + REG_INTERRUPT_MASK);
        writel(0, host->base + REG_POWER_CONTROL);
        writel(readl(host->base + REG_CLOCK_CONTROL) | CLK_OFF,
               host->base + REG_CLOCK_CONTROL);
+       mmc_remove_host(mmc);

Rather than moving this to the bottom of the function, it would be more correct to move it to the beginning.

This way, we ensure things have been closed down properly before releasing the dma channels.

Quoted 6 lines
drivers/mmc/host/moxart-mmc.c +0 -1
 }
 
 static const struct of_device_id moxart_mmc_match[] = {
--

2.53.0

Kind regards
Uffe

#2 lore raw
Quoted 8 lines · Ulf Hansson
Quoted 5 lines · Greg Kroah-Hartman

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

Quoted 6 lines

mmc_remove_host() doesn't actually free the host, but it reverses what mmc_add_host() did during probe.

Since the moxart driver uses devm_mmc_alloc_host() the last reference to the host will be dropped after ->remove() completes, leading to mmc_free_host() to be called for it.

Then how did commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path") do anything? It really wasn't needed either? And so the CVE related to it should be rejected?

Quoted 8 lines

However, improvements can still be made in the ->remove() callback. See below.

Quoted 4 lines
Cc Ulf Hansson <[email protected]>
Cc stable <[email protected]>
Assisted-by gkh_clanker_2000

What's this?

My assorted hacks of scripts that found this issue.

Quoted 24 lines
Quoted 10 lines
---
 drivers/mmc/host/moxart-mmc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mmc/host/moxart-mmc.c +0 -0
index 3dd8f232052f..256e16390ef3 100644

drivers/mmc/host/moxart-mmc.c static void moxart_remove(struct platform_device *pdev) · line 698

Quoted 10 lines
drivers/mmc/host/moxart-mmc.c +1 -1
@@ -690,12 +690,12 @@ static void moxart_remove(struct platform_device *pdev)
                dma_release_channel(host->dma_chan_tx);
        if (!IS_ERR_OR_NULL(host->dma_chan_rx))
                dma_release_channel(host->dma_chan_rx);
-       mmc_remove_host(mmc);
 
        writel(0, host->base + REG_INTERRUPT_MASK);
        writel(0, host->base + REG_POWER_CONTROL);
        writel(readl(host->base + REG_CLOCK_CONTROL) | CLK_OFF,
               host->base + REG_CLOCK_CONTROL);
+       mmc_remove_host(mmc);

Rather than moving this to the bottom of the function, it would be more correct to move it to the beginning.

Quoted 2 lines

This way, we ensure things have been closed down properly before releasing the dma channels.

Ok, but I was just trying to follow the same pattern in the above mentioned commit. If that pattern was not actually fixing something, then this change also doesn't do anything, so it's not needed either.

thanks,

greg k-h

#3 lore raw
Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Ulf Hansson
Quoted 5 lines · Greg Kroah-Hartman

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

mmc_remove_host() doesn't actually free the host, but it reverses what mmc_add_host() did during probe.

Quoted 7 lines
Quoted 3 lines

Since the moxart driver uses devm_mmc_alloc_host() the last reference to the host will be dropped after ->remove() completes, leading to mmc_free_host() to be called for it.

Then how did commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path") do anything? It really wasn't needed either? And so the CVE related to it should be rejected?

No, commit bd2db32e7c3e is perfectly okay and solves the intended problem.

Before the moxart driver was converted to use devm_mmc_alloc_host() in commit 973aa22b9f1a, it used mmc_alloc_host() during probe.

Calling mmc_free_host() is needed in these cases, but it should typically be the final thing a ->remove() callback does, in order to clean up correctly and prevent use-after-free bugs.

[...]

Kind regards
Uffe

#4 lore raw
Quoted 19 lines · Ulf Hansson
Quoted 17 lines · Greg Kroah-Hartman
Quoted 14 lines · Ulf Hansson
Quoted 5 lines · Greg Kroah-Hartman

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

mmc_remove_host() doesn't actually free the host, but it reverses what mmc_add_host() did during probe.

Since the moxart driver uses devm_mmc_alloc_host() the last reference to the host will be dropped after ->remove() completes, leading to mmc_free_host() to be called for it.

Quoted 8 lines
Quoted 3 lines

Then how did commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path") do anything? It really wasn't needed either? And so the CVE related to it should be rejected?

No, commit bd2db32e7c3e is perfectly okay and solves the intended problem.

Before the moxart driver was converted to use devm_mmc_alloc_host() in commit 973aa22b9f1a, it used mmc_alloc_host() during probe.

Ah, that makes more sense, thanks, I was confused.

Quoted 3 lines

Calling mmc_free_host() is needed in these cases, but it should typically be the final thing a ->remove() callback does, in order to clean up correctly and prevent use-after-free bugs.

I moved the call to be the "final thing" in these patches, so they are ok?

thanks,

greg k-h

#5 lore raw
Quoted 30 lines · Greg Kroah-Hartman
Quoted 19 lines · Ulf Hansson
Quoted 17 lines · Greg Kroah-Hartman
Quoted 14 lines · Ulf Hansson
Quoted 5 lines · Greg Kroah-Hartman

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

mmc_remove_host() doesn't actually free the host, but it reverses what mmc_add_host() did during probe.

Since the moxart driver uses devm_mmc_alloc_host() the last reference to the host will be dropped after ->remove() completes, leading to mmc_free_host() to be called for it.

Quoted 8 lines
Quoted 3 lines

Then how did commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path") do anything? It really wasn't needed either? And so the CVE related to it should be rejected?

No, commit bd2db32e7c3e is perfectly okay and solves the intended problem.

Before the moxart driver was converted to use devm_mmc_alloc_host() in commit 973aa22b9f1a, it used mmc_alloc_host() during probe.

Quoted 8 lines

Ah, that makes more sense, thanks, I was confused.

Quoted 3 lines

Calling mmc_free_host() is needed in these cases, but it should typically be the final thing a ->remove() callback does, in order to clean up correctly and prevent use-after-free bugs.

I moved the call to be the "final thing" in these patches, so they are ok?

No, mmc_free_host() should be in the end (unless the devm* variant is used in probe), while mmc_remove_host() should be in the beginning.

Kind regards
Uffe

#6 lore raw
Quoted 38 lines · Ulf Hansson
Quoted 36 lines · Greg Kroah-Hartman
Quoted 19 lines · Ulf Hansson
Quoted 17 lines · Greg Kroah-Hartman
Quoted 14 lines · Ulf Hansson
Quoted 5 lines · Greg Kroah-Hartman

Just like in commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path"), we should wait until after we are finished writing to the mmc host device before removing it, otherwise it could have been already freed.

mmc_remove_host() doesn't actually free the host, but it reverses what mmc_add_host() did during probe.

Since the moxart driver uses devm_mmc_alloc_host() the last reference to the host will be dropped after ->remove() completes, leading to mmc_free_host() to be called for it.

Quoted 8 lines
Quoted 3 lines

Then how did commit bd2db32e7c3e ("moxart: fix potential use-after-free on remove path") do anything? It really wasn't needed either? And so the CVE related to it should be rejected?

No, commit bd2db32e7c3e is perfectly okay and solves the intended problem.

Before the moxart driver was converted to use devm_mmc_alloc_host() in commit 973aa22b9f1a, it used mmc_alloc_host() during probe.

Ah, that makes more sense, thanks, I was confused.

Quoted 3 lines

Calling mmc_free_host() is needed in these cases, but it should typically be the final thing a ->remove() callback does, in order to clean up correctly and prevent use-after-free bugs.

Quoted 5 lines
Quoted 2 lines

I moved the call to be the "final thing" in these patches, so they are ok?

No, mmc_free_host() should be in the end (unless the devm* variant is used in probe), while mmc_remove_host() should be in the beginning.

Ok, I think I understand. I'll respin these two patches based on that when I get a chance later this week.

thanks for your patience,

greg k-h