ata: Do not release the host resources twice on probe() failure
Niklas Cassel
maintainerv4 cover ata: Do not release the host resources twice on probe() failure
ata_host_start() registers ata_host_stop() as a devres action as soon as it has succeeded:
if (have_stop) {
start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
...
if (start_dr)
devres_add(host->dev, start_dr);
host->flags |= ATA_HOST_STARTED;From that point on, releasing the host resources is owned by devres: when probe() fails, the driver core calls devres_release_all(), which calls ata_host_stop(), which calls ->port_stop() and ->host_stop().
ata_host_activate() and ahci_host_activate_multi_irqs() can however fail after ata_host_start() has succeeded - devm_kasprintf(), devm_request_irq() (a shared IRQ conflict) and ata_host_register() (scsi_add_host(), ata_tport_add()) can all fail - and they return the error with the devres action still registered. Since the caller cannot tell whether ata_host_start() succeeded, and since it has to release the resources for the failures happening before that, all the ahci-platform drivers release the host resources in their probe() error path, e.g. ahci_probe() calls ahci_platform_disable_resources() while ahci_host_stop() does the same through devres.
The clocks, regulators, resets and PHYs of the host are therefore released twice, which gives refcount underflow warnings from the clk, regulator and phy cores and, for shared resources, can disable resources which are still in use by other devices.
Patch 4 adds ata_host_undo_start(), which stops the ports and drops the devres action without calling ->host_stop(), and calls it from both activation helpers when they fail, so that "on failure, the caller releases what it acquired" holds for all of them. sata_qstor and sata_fsl are the only drivers which implement ->host_stop() while having no error handling at all for the activate host call, so they get some.
Patches 1 to 3 have to come first, as they fix error paths which patch 4 would otherwise turn into resource leaks, and which are worth fixing on their own:
- ahci_st is the only ahci-platform driver whose ->host_stop() does more than its probe() error path, as it also asserts the "pwr-dwn" reset (patch 1).
- sata_fsl releases hcr_base and host_priv in its probe() error path although sata_fsl_host_stop() does that as well, which is a use-after-free that the two device_create_file() calls after ata_host_activate() can trigger today (patch 2).
- ahci_host_activate_multi_irqs() never frees the IRQs which it requested when it fails, so the IRQ handlers stay registered while the caller releases the resources of the host (patch 3).
Patch 5 is an unrelated kdoc fix that I noticed while documenting the above.
Changes since v3:
- New patch 2, which fixes the use-after-free of host_priv in the probe() error path of sata_fsl. Patch 4 would otherwise make sata_fsl leak hcr_base and host_priv when activating the host fails.
- New patch 3, which frees the IRQs requested by ahci_host_activate_multi_irqs() when it fails, like ata_host_activate() already does.
- Reword the "if failed, just free the IRQ and leave ports alone" comment in ata_host_activate(), as the ports are no longer left alone.
Niklas Cassel (5):
ata: ahci_st: Assert the power down reset in the probe() error path
ata: sata_fsl: Fix use-after-free of host_priv on probe() failure
ata: libahci: Free the IRQs when activating a multi-IRQ host fails
ata: libata: Do not leave ata_host_stop() registered when activation
fails
ata: libata-core: Fix the ata_host_register() kdoc
drivers/ata/ahci_st.c | 51 ++++++++++++--------- drivers/ata/libahci.c | 28 +++++++++++- drivers/ata/libahci_platform.c | 4 ++ drivers/ata/libata-core.c | 81 ++++++++++++++++++++++++++++++---- drivers/ata/libata-sff.c | 5 +++ drivers/ata/sata_fsl.c | 24 +++++++--- drivers/ata/sata_qstor.c | 8 +++- include/linux/libata.h | 1 + 8 files changed, 161 insertions(+), 41 deletions(-)
Signature · 1 line
2.55.0
Niklas Cassel
maintainerPATCH v4 1/5 ata: ahci_st: Assert the power down reset in the probe() error path
st_ahci_probe_resets() deasserts the "pwr-dwn" reset, but the probe() error path of st_ahci_probe() only releases the host resources, so the SATA IP is left powered up when probe() fails after st_ahci_probe_resets() has succeeded, e.g. when ahci_platform_enable_resources() fails.
The reset is asserted by st_ahci_host_stop(), however ->host_stop() is only called through the ata_host_stop() devres action registered by ata_host_start(), so it does not cover any failure happening before the host has been started.
Factor the assert out into st_ahci_assert_pwrdwn() and call it from both probe() error paths. The "pwr-dwn" reset control is an exclusive one, so asserting it once more from st_ahci_host_stop() is harmless.
No functional change intended for ->host_stop() and st_ahci_suspend().
--- drivers/ata/ahci_st.c | 51 +++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 21 deletions(-)
drivers/ata/ahci_st.c +30 -21
@@ -83,18 +83,27 @@ static int st_ahci_deassert_resets(struct ahci_host_priv *hpriv,return 0 ; } -static void st_ahci_host_stop (struct ata_host *host) +static int st_ahci_assert_pwrdwn (struct ahci_host_priv *hpriv, +struct device *dev) { -struct ahci_host_priv *hpriv = host->private_data;struct st_ahci_drv_data *drv_data = hpriv->plat_data; -struct device *dev = host->dev;int err; -if (drv_data->pwr) { - err =reset_control_assert (drv_data->pwr); -if (err) -dev_err (dev,"unable to pwrdwn\n" ); - } +if (!drv_data->pwr) +return 0 ; + + err =reset_control_assert (drv_data->pwr); +if (err) +dev_err (dev,"unable to pwrdwn\n" ); + +return err; +} + +static void st_ahci_host_stop (struct ata_host *host) +{ +struct ahci_host_priv *hpriv = host->private_data; + +st_ahci_assert_pwrdwn (hpriv, host->dev);ahci_platform_disable_resources (hpriv); }
@@ -162,38 +171,38 @@ static int st_ahci_probe(struct platform_device *pdev) err =ahci_platform_enable_resources (hpriv);if (err) -return err; + goto assert_pwrdwn;st_ahci_configure_oob (hpriv->mmio); err =ahci_platform_init_host (pdev, hpriv, &st_ahci_port_info, &ahci_platform_sht); -if (err) { -ahci_platform_disable_resources (hpriv); -return err; - } +if (err) + goto disable_resources;return 0 ; + +disable_resources: +ahci_platform_disable_resources (hpriv); +assert_pwrdwn: +st_ahci_assert_pwrdwn (hpriv, &pdev->dev); + +return err; }static int st_ahci_suspend (struct device *dev) {struct ata_host *host =dev_get_drvdata (dev);struct ahci_host_priv *hpriv = host->private_data; -struct st_ahci_drv_data *drv_data = hpriv->plat_data;int err; err =ahci_platform_suspend_host (dev);if (err)return err; -if (drv_data->pwr) { - err =reset_control_assert (drv_data->pwr); -if (err) { -dev_err (dev,"unable to pwrdwn" ); -return err; - } - } + err =st_ahci_assert_pwrdwn (hpriv, dev); +if (err) +return err;ahci_platform_disable_resources (hpriv);
Signature · 1 line
2.55.0
Niklas Cassel
maintainerPATCH v4 2/5 ata: sata_fsl: Fix use-after-free of host_priv on probe() failure
In reply to Niklas Casselsata_fsl_host_stop() releases hcr_base and host_priv:
static void sata_fsl_host_stop(struct ata_host *host)
{
struct sata_fsl_host_priv *host_priv = host->private_data;
iounmap(host_priv->hcr_base);
kfree(host_priv);
}->host_stop() is called by the driver core through the ata_host_stop() devres action which ata_host_start() registers, so it is called both when the device is unbound and when probe() fails after ata_host_activate() has started the host.
The error_exit_with_cleanup label in sata_fsl_probe() releases hcr_base and host_priv as well, and it is reachable from the two device_create_file() calls done after ata_host_activate(). In that case sata_fsl_host_stop() runs on an already freed host_priv, resulting in a use-after-free and a double iounmap()/kfree().
sata_fsl_probe() also ignores the return value of ata_host_activate(), so probe() returns success even if activating the host failed.
Check the return value of ata_host_activate() and, once the host has been activated, leave hcr_base and host_priv to sata_fsl_host_stop().
Note that if ata_host_activate() fails inside ata_host_start(), e.g. if sata_fsl_port_start() fails, ->host_stop() is not registered and hcr_base and host_priv are leaked. Leaking them is preferable to freeing them twice, and this is addressed by a later patch in this series.
--- drivers/ata/sata_fsl.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/ata/sata_fsl.c +17 -7
@@ -1490,8 +1490,10 @@ static int sata_fsl_probe(struct platform_device *ofdev)* device discovery process, invoking our port_start() handler & * error_handler() to execute a dummy Softreset EH session */ -ata_host_activate (host, irq, sata_fsl_interrupt,SATA_FSL_IRQ_FLAG , - &sata_fsl_sht); + retval =ata_host_activate (host, irq, sata_fsl_interrupt, +SATA_FSL_IRQ_FLAG , &sata_fsl_sht); +if (retval) +return retval; host_priv->intr_coalescing.show = fsl_sata_intr_coalescing_show; host_priv->intr_coalescing.store = fsl_sata_intr_coalescing_store;
@@ -1500,7 +1502,7 @@ static int sata_fsl_probe(struct platform_device *ofdev) host_priv->intr_coalescing.attr.mode =S_IRUGO |S_IWUSR ; retval =device_create_file (host->dev, &host_priv->intr_coalescing);if (retval) - goto error_exit_with_cleanup; + goto error_exit_detach; host_priv->rx_watermark.show = fsl_sata_rx_watermark_show; host_priv->rx_watermark.store = fsl_sata_rx_watermark_store;
@@ -1510,16 +1512,24 @@ static int sata_fsl_probe(struct platform_device *ofdev) retval =device_create_file (host->dev, &host_priv->rx_watermark);if (retval) {device_remove_file (&ofdev->dev, &host_priv->intr_coalescing); - goto error_exit_with_cleanup; + goto error_exit_detach; }return 0 ; -error_exit_with_cleanup: +/* +* Once the host has been activated, hcr_base and host_priv are +* released by sata_fsl_host_stop(), which is called by the driver core +* through the ata_host_stop() devres action registered by +* ata_host_start(). Releasing them here as well would result in a +* double iounmap() and a use-after-free. +*/ +error_exit_detach: +ata_host_detach (host); -if (host) -ata_host_detach (host); +return retval; +error_exit_with_cleanup:if (hcr_base)iounmap (hcr_base);kfree (host_priv);
Signature · 1 line
2.55.0
Niklas Cassel
maintainerPATCH v4 3/5 ata: libahci: Free the IRQs when activating a multi-IRQ host fails
In reply to Niklas Casselahci_host_activate_multi_irqs() requests one IRQ per port, but it does not free them when requesting one of them, or when registering the host, fails. The IRQs are only freed by the driver core, when it releases the devres of the device after probe() has returned, while the caller of ahci_host_activate() releases the resources of the host in its probe() error path, e.g. ahci_probe() disables the clocks, regulators, resets and PHYs of the host. An IRQ handler running in that window would access the MMIO of a host which is no longer clocked.
ahci_host_activate_multi_irqs() did free the IRQs until commit 0a142b26921c ("ahci: cleanup ahci_host_activate_multi_irqs"), which removed the explicit free because devm makes it unnecessary. That is true for freeing the IRQs as such, but not for the window described above:
devres is only released after probe() has returned, i.e. after the error path of the caller has released the resources of the host.
Note that this window cannot be hit with the current users: the only user of AHCI_HFLAG_MULTI_MSI is the AHCI PCI driver, which does not release any resource in its probe() error path, and the ports of the host are still frozen, i.e. their interrupts are masked, when ata_host_register() fails.
Free the IRQs explicitly again, like ata_host_activate() does, so that the IRQ handlers cannot run once ahci_host_activate() has failed.
--- drivers/ata/libahci.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
drivers/ata/libahci.c +24 -2
@@ -2723,11 +2723,33 @@ static int ahci_host_activate_multi_irqs(struct ata_host *host, 0, pp->irq_desc, host->ports[i]);if (rc) -return rc; + goto free_irqs;ata_port_desc_misc (host->ports[i], irq); } -return ata_host_register (host, sht); + rc =ata_host_register (host, sht); +if (rc) + goto free_irqs; + +return 0 ; + +free_irqs: +/* +* Free the IRQs which have been requested, so that the handlers can no +* longer access the MMIO of the host once we return, e.g. after the +* caller has disabled the clocks of the host. +*/ +while (--i >=0 ) { +struct ahci_port_priv *pp = host->ports[i]->private_data; + +if (!pp) +continue ; + +devm_free_irq (host->dev, hpriv->get_irq_vector (host, i), + host->ports[i]); + } + +return rc; }/**
Signature · 1 line
2.55.0
Niklas Cassel
maintainerPATCH v4 5/5 ata: libata-core: Fix the ata_host_register() kdoc
In reply to Niklas Casselata_host_register() does not start the ports of the host, it requires them to have been started already:
/* host must have been started */
if (!(host->flags & ATA_HOST_STARTED)) {
dev_err(host->dev, "BUG: trying to register unstarted host\n");
WARN_ON(1);
return -EINVAL;
}Fix the kdoc accordingly, and fix the grammar of the last sentence while at it.
--- drivers/ata/libata-core.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/ata/libata-core.c +3 -3
@@ -6237,9 +6237,9 @@ static void async_port_probe(void *data, async_cookie_t cookie)* @sht: template for SCSI host * * Register initialized ATA host. @host is allocated using -* ata_host_alloc() and fully initialized by LLD. This function -* starts ports, registers @host with ATA and SCSI layers and -* probe registered devices. +* ata_host_alloc(), fully initialized by the LLD and started using +* ata_host_start(). This function registers @host with the ATA and +* SCSI layers and probes the registered devices. * * On failure, the host remains started, i.e. the devres action * registered by ata_host_start() is kept, so ->host_stop() is called by
Signature · 1 line
2.55.0
Niklas Cassel
maintainerPATCH v4 4/5 ata: libata: Do not leave ata_host_stop() registered when activation fails
In reply to Niklas Casselata_host_start() registers ata_host_stop() as a devres action as soon as it has succeeded, which hands the release of the host resources over to
devres: ->port_stop() and ->host_stop() are then called by the driver core when probe() fails.
ata_host_activate() and ahci_host_activate_multi_irqs() can both fail after ata_host_start() has succeeded, e.g. if devm_request_irq() or ata_host_register() fails, and they return the error with the devres action still registered. A driver which releases the host resources in its probe() error path therefore releases them twice: once itself and once through ->host_stop().
All ahci-platform drivers are in that situation, e.g. ahci_probe() calls ahci_platform_disable_resources() while ahci_host_stop() does the same through devres. This gives refcount underflow warnings from the clk, regulator and phy cores and, for shared resources, can disable resources which are still in use by other devices.
Add ata_host_undo_start(), which stops the ports and drops the devres action without calling ->host_stop(), and call it from both activation helpers when they fail. Releasing the host resources on failure is then always left to the caller, which is what all callers having a probe() error path already assume.
This changes the semantics for the drivers which implement ->host_stop() while also completely lacking error handling for the activate host call. Add activate host error handling for sata_qstor and sata_fsl.
Note that ata_pci_sff_activate_host() is deliberately left as is: none of its callers releases the host resources in its probe() error path, they all rely on ->host_stop() being called by devres, including ata_pci_init_one(), which releases the devres group of the host itself.
--- drivers/ata/libahci.c | 2 + drivers/ata/libahci_platform.c | 4 ++ drivers/ata/libata-core.c | 75 +++++++++++++++++++++++++++++++--- drivers/ata/libata-sff.c | 5 +++ drivers/ata/sata_fsl.c | 2 +- drivers/ata/sata_qstor.c | 8 +++- include/linux/libata.h | 1 + 7 files changed, 88 insertions(+), 9 deletions(-)
drivers/ata/libahci.c +2 -0
@@ -2749,6 +2749,8 @@ static int ahci_host_activate_multi_irqs(struct ata_host *host, host->ports[i]); } +ata_host_undo_start (host); +return rc; }
drivers/ata/libahci_platform.c +4 -0
@@ -689,6 +689,10 @@ EXPORT_SYMBOL_GPL(ahci_platform_get_resources);* ahci-platform host, note any necessary resources (ie clks, phys, etc.) * must be initialized / enabled before calling this. * +* On failure, ->host_stop() is not called, so the caller has to release the +* resources it enabled (clocks, regulators, resets, PHYs) in its probe() +* error path. +* * RETURNS: * 0 on success otherwise a negative error code */
drivers/ata/libata-core.c +69 -6
114 lines not on this page · raw message
drivers/ata/libata-sff.c +5 -0
@@ -2267,6 +2267,11 @@ EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);* hosts. This separate helper is necessary because SFF hosts * use two separate interrupts in legacy mode. * +* Note that, unlike ata_host_activate(), the devres action registered +* by ata_host_start() is kept on failure, i.e. ->host_stop() is called +* by the driver core when probe() fails. All callers of this function +* rely on that, none of them releases the host resources itself. +* * LOCKING: * Inherited from calling layer (may sleep). *
drivers/ata/sata_fsl.c +1 -1
@@ -1493,7 +1493,7 @@ static int sata_fsl_probe(struct platform_device *ofdev) retval =ata_host_activate (host, irq, sata_fsl_interrupt,SATA_FSL_IRQ_FLAG , &sata_fsl_sht);if (retval) -return retval; + goto error_exit_with_cleanup; host_priv->intr_coalescing.show = fsl_sata_intr_coalescing_show; host_priv->intr_coalescing.store = fsl_sata_intr_coalescing_store;
drivers/ata/sata_qstor.c +6 -2
@@ -584,8 +584,12 @@ static int qs_ata_init_one(struct pci_dev *pdev,qs_host_init (host, board_idx);pci_set_master (pdev); -return ata_host_activate (host, pdev->irq, qs_intr,IRQF_SHARED , - &qs_ata_sht); + rc =ata_host_activate (host, pdev->irq, qs_intr,IRQF_SHARED , + &qs_ata_sht); +if (rc) +qs_host_stop (host); + +return rc; }module_pci_driver (qs_ata_pci_driver);
include/linux/libata.h +1 -0
@@ -1147,6 +1147,7 @@ extern struct ata_host *ata_host_alloc_pinfo(struct device *dev,extern void ata_host_get (struct ata_host *host);extern void ata_host_put (struct ata_host *host);extern int ata_host_start (struct ata_host *host); +extern void ata_host_undo_start (struct ata_host *host);extern int ata_host_register (struct ata_host *host,const struct scsi_host_template *sht); extern int ata_host_activate(struct ata_host *host, int irq,
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v4 1/5] ata: ahci_st: Assert the power down reset in the probe() error path
In reply to Niklas CasselThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] st_ahci_probe_resets() blindly masks all errors from devm_reset_control_get(), breaking probe deferral and leading to kernel panics. --
commit aedfb0423cffc301a78c793e7a81df6e88c712e0
Author: Niklas Cassel <[email protected]>
ata: ahci_st: Assert the power down reset in the probe() error path
This commit updates the probe error path and host stop functions in the ahci_st driver to properly assert the power down reset. It extracts the reset assertion logic into a helper function and ensures the SATA IP is not left powered up if initialization fails after resets are deasserted.
Quoted 4 lines
drivers/ata/ahci_st.c +0
-0
[ ... ]
drivers/ata/ahci_st.c static int st_ahci_probe(struct platform_device *pdev) · line 174
Quoted 6 lines
drivers/ata/ahci_st.c +1
-1@@ -162,38 +171,38 @@ static int st_ahci_probe(struct platform_device *pdev) err =ahci_platform_enable_resources (hpriv);if (err) -return err; + goto assert_pwrdwn;
[Severity: High]
This isn't a bug introduced by this patch, but while reviewing the probe path here, I noticed a pre-existing issue in st_ahci_probe_resets(), which executes just before ahci_platform_enable_resources().
Does st_ahci_probe_resets() blindly mask all errors from devm_reset_control_get(), breaking probe deferral?
drivers/ata/ahci_st.c:st_ahci_probe_resets() {
...
drv_data->pwr = devm_reset_control_get(dev, "pwr-dwn");
if (IS_ERR(drv_data->pwr)) {
dev_info(dev, "power reset control not defined\n");
drv_data->pwr = NULL;
}
...
}If the reset controller driver probes after the SATA driver, devm_reset_control_get() returns -EPROBE_DEFER. Because the code treats this as a missing optional reset, it continues initialization.
Will this cause an imprecise external abort (kernel panic) when st_ahci_configure_oob() subsequently attempts to access the MMIO registers of a hardware block that is still held in reset?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Niklas Cassel
maintainerRe: [PATCH v4 1/5] ata: ahci_st: Assert the power down reset in the probe() error path
Quoted 4 lines · On Thu, Sep 10, 2026 at 05:29:03PM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High]st_ahci_probe_resets()blindly masks all errors fromdevm_reset_control_get(), breaking probe deferral and leading to kernel panics.
I do not intend to fix this pre-existing issue.
Would be happy to pick up a patch for it though.
Kind regards,
Niklas
Re: [PATCH v4 1/5] ata: ahci_st: Assert the power down reset in the probe() error path
In reply to Niklas CasselQuoted 6 lines · Niklas Cassel
st_ahci_probe_resets()deasserts the "pwr-dwn" reset, but theprobe()error path ofst_ahci_probe()only releases the host resources, so the SATA IP is left powered up whenprobe()fails afterst_ahci_probe_resets()has succeeded, e.g. whenahci_platform_enable_resources()fails.
Quoted 7 lines
The reset is asserted by
st_ahci_host_stop(), however ->host_stop() is only called through theata_host_stop()devres action registered byata_host_start(), so it does not cover any failure happening before the host has been started.Factor the assert out into
st_ahci_assert_pwrdwn()and call it from bothprobe()error paths. The "pwr-dwn" reset control is an exclusive one, so
from both probe() error path *and* ... ?
Quoted 7 lines
asserting it once more from
st_ahci_host_stop()is harmless.No functional change intended for ->host_stop() and
st_ahci_suspend().Fixes 76884cb2f7da ("ahci: st: Add support for ST's SATA IP")Signed-off-by Niklas Cassel <[email protected]>
Other that the nit above, looks good.
Signature · 2 lines
Damien Le Moal Western Digital Research
Damien Le Moal
maintainerRe: [PATCH v4 2/5] ata: sata_fsl: Fix use-after-free of host_priv on probe() failure
In reply to Niklas CasselQuoted 24 lines · Niklas Cassel
sata_fsl_host_stop()releaseshcr_baseandhost_priv:static void sata_fsl_host_stop(struct ata_host *host) { struct sata_fsl_host_priv *host_priv = host->private_data; iounmap(host_priv->hcr_base); kfree(host_priv); }->host_stop() is called by the driver core through the
ata_host_stop()devres action whichata_host_start()registers, so it is called both when the device is unbound and whenprobe()fails afterata_host_activate()has started the host.The
error_exit_with_cleanuplabel insata_fsl_probe()releaseshcr_baseandhost_privas well, and it is reachable from the twodevice_create_file()calls done afterata_host_activate(). In that casesata_fsl_host_stop()runs on an already freedhost_priv, resulting in a use-after-free and a double iounmap()/kfree().
sata_fsl_probe()also ignores the return value ofata_host_activate(), soprobe()returns success even if activating the host failed.
Quoted 7 lines
Check the return value of
ata_host_activate()and, once the host has been activated, leavehcr_baseandhost_privtosata_fsl_host_stop().Note that if
ata_host_activate()fails insideata_host_start(), e.g. ifsata_fsl_port_start()fails, ->host_stop() is not registered andhcr_baseandhost_privare leaked. Leaking them is preferable to freeing them twice, and this is addressed by a later patch in this series.
I would prefer seeing the leak fixed here too so that we have a self contained fix in a single commit.
Or, move the leak fix patch before this one.
Quoted 63 lines
Fixes 6c8ad7e8cf29 ("sata_fsl: fix UAF in sata_fsl_port_stop when rmmod sata_fsl")Signed-off-by Niklas Cassel <[email protected]>--- drivers/ata/sata_fsl.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)drivers/ata/sata_fsl.c +17
-7@@ -1490,8 +1490,10 @@ static int sata_fsl_probe(struct platform_device *ofdev)* device discovery process, invoking our port_start() handler & * error_handler() to execute a dummy Softreset EH session */ -ata_host_activate (host, irq, sata_fsl_interrupt,SATA_FSL_IRQ_FLAG , - &sata_fsl_sht); + retval =ata_host_activate (host, irq, sata_fsl_interrupt, +SATA_FSL_IRQ_FLAG , &sata_fsl_sht); +if (retval) +return retval; host_priv->intr_coalescing.show = fsl_sata_intr_coalescing_show; host_priv->intr_coalescing.store = fsl_sata_intr_coalescing_store;@@ -1500,7 +1502,7 @@ static int sata_fsl_probe(struct platform_device *ofdev) host_priv->intr_coalescing.attr.mode =S_IRUGO |S_IWUSR ; retval =device_create_file (host->dev, &host_priv->intr_coalescing);if (retval) - goto error_exit_with_cleanup; + goto error_exit_detach; host_priv->rx_watermark.show = fsl_sata_rx_watermark_show; host_priv->rx_watermark.store = fsl_sata_rx_watermark_store;@@ -1510,16 +1512,24 @@ static int sata_fsl_probe(struct platform_device *ofdev) retval =device_create_file (host->dev, &host_priv->rx_watermark);if (retval) {device_remove_file (&ofdev->dev, &host_priv->intr_coalescing); - goto error_exit_with_cleanup; + goto error_exit_detach; }return 0 ; -error_exit_with_cleanup: +/* +* Once the host has been activated, hcr_base and host_priv are +* released by sata_fsl_host_stop(), which is called by the driver core +* through the ata_host_stop() devres action registered by +* ata_host_start(). Releasing them here as well would result in a +* double iounmap() and a use-after-free. +*/ +error_exit_detach: +ata_host_detach (host); -if (host) -ata_host_detach (host); +return retval; +error_exit_with_cleanup:if (hcr_base)iounmap (hcr_base);kfree (host_priv);
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v4 3/5] ata: libahci: Free the IRQs when activating a multi-IRQ host fails
In reply to Niklas CasselQuoted 22 lines · Niklas Cassel
ahci_host_activate_multi_irqs()requests one IRQ per port, but it does not free them when requesting one of them, or when registering the host, fails. The IRQs are only freed by the driver core, when it releases the devres of the device afterprobe()has returned, while the caller ofahci_host_activate()releases the resources of the host in itsprobe()error path, e.g.ahci_probe()disables the clocks, regulators, resets and PHYs of the host. An IRQ handler running in that window would access the MMIO of a host which is no longer clocked.
ahci_host_activate_multi_irqs()did free the IRQs until commit0a142b26921c("ahci: cleanupahci_host_activate_multi_irqs"), which removed the explicit free because devm makes it unnecessary. That is true for freeing the IRQs as such, but not for the window described above:
devres is only released afterprobe()has returned, i.e. after the error path of the caller has released the resources of the host.Note that this window cannot be hit with the current users: the only user of
AHCI_HFLAG_MULTI_MSIis the AHCI PCI driver, which does not release any resource in itsprobe()error path, and the ports of the host are still frozen, i.e. their interrupts are masked, whenata_host_register()fails.
Quoted 6 lines
Free the IRQs explicitly again, like
ata_host_activate()does, so that the IRQ handlers cannot run onceahci_host_activate()has failed.Fixes 0a142b26921c ("ahci: cleanup ahci_host_activate_multi_irqs")Signed-off-by Niklas Cassel <[email protected]>
Looks OK to me.
Signature · 2 lines
Damien Le Moal Western Digital Research
Re: [PATCH v4 5/5] ata: libata-core: Fix the ata_host_register() kdoc
In reply to Niklas CasselQuoted 10 lines · Niklas Cassel
ata_host_register()does not start the ports of the host, it requires them to have been started already:/* host must have been started */ if (!(host->flags & ATA_HOST_STARTED)) { dev_err(host->dev, "BUG: trying to register unstarted host\n"); WARN_ON(1); return -EINVAL; }
Quoted 4 lines
Fix the kdoc accordingly, and fix the grammar of the last sentence while at it.
Signed-off-by Niklas Cassel <[email protected]>
Signature · 2 lines
Damien Le Moal Western Digital Research
Niklas Cassel
maintainerRe: [PATCH v4 2/5] ata: sata_fsl: Fix use-after-free of host_priv on probe() failure
In reply to Damien Le MoalQuoted 33 lines · Damien Le Moal
Quoted 24 lines · Niklas Cassel
sata_fsl_host_stop()releaseshcr_baseandhost_priv:static void sata_fsl_host_stop(struct ata_host *host) { struct sata_fsl_host_priv *host_priv = host->private_data; iounmap(host_priv->hcr_base); kfree(host_priv); }->host_stop() is called by the driver core through the
ata_host_stop()devres action whichata_host_start()registers, so it is called both when the device is unbound and whenprobe()fails afterata_host_activate()has started the host.The
error_exit_with_cleanuplabel insata_fsl_probe()releaseshcr_baseandhost_privas well, and it is reachable from the twodevice_create_file()calls done afterata_host_activate(). In that casesata_fsl_host_stop()runs on an already freedhost_priv, resulting in a use-after-free and a double iounmap()/kfree().
sata_fsl_probe()also ignores the return value ofata_host_activate(), soprobe()returns success even if activating the host failed.Quoted 7 lines
Check the return value of
ata_host_activate()and, once the host has been activated, leavehcr_baseandhost_privtosata_fsl_host_stop().Note that if
ata_host_activate()fails insideata_host_start(), e.g. ifsata_fsl_port_start()fails, ->host_stop() is not registered andhcr_baseandhost_privare leaked. Leaking them is preferable to freeing them twice, and this is addressed by a later patch in this series.
Quoted 4 lines
I would prefer seeing the leak fixed here too so that we have a self contained fix in a single commit.
Or, move the leak fix patch before this one.
I took a third option:
This (temporary) leak was only added by this patch, and was later fixed by "ata: libata: Do not leave ata_host_stop() registered when activation fails".
The UAF happened only when the activation succeeded and device_create_file() failed. Thus, this fix does not rely on ata_host_activate() error handling.
Thus, let this patch only fix the UAF, without adding any error handling to ata_host_activate(). That way this patch is self contained.
Patch "ata: libata: Do not leave ata_host_stop() registered when activation fails" will add ata_host_activate() error handling to this driver, and at this point, ata_host_activate() internals has been fixed. Thus, we there will never exist any intermediate step where we might leak resources.
Kind regards,
Niklas