Skip to contentSkip to conversation

spi: fix use-after-free on managed registration failure

PATCH In review Johan Hovold 3 participants 8 messages Mar 25 – Mar 27 SPI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: fix use-after-free on managed registration failure

The SPI API is asymmetric and the controller is freed as part of deregistration (unless it has been allocated using devm_spi_alloc_host/target()).

A recent change converting the managed registration function to use devm_add_action_or_reset() inadvertently introduced a (mostly theoretical) regression where a non-devres managed controller could be freed as part of failed registration. This in turn would lead to use-after-free in controller driver error paths.

Fix this by taking another reference before calling devm_add_action_or_reset() and not releasing it on errors for non-devres allocated controllers.

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Fixes b6376dbed8e1 ("spi: Simplify devm_spi_*_controller()")
Cc Andy Shevchenko <[email protected]>
---
 drivers/spi/spi.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/spi/spi.c +12 -1
index cb00619864cf..aac378e668a8 100644
@@ -3537,8 +3537,19 @@ int devm_spi_register_controller(struct device *dev,
 	if (ret)
 		return ret;
 
-	return devm_add_action_or_reset(dev, devm_spi_unregister_controller, ctlr);
+	/*
+	 * Prevent controller from being freed by spi_unregister_controller()
+	 * if devm_add_action_or_reset() fails for a non-devres allocated
+	 * controller.
+	 */
+	spi_controller_get(ctlr);
+
+	ret = devm_add_action_or_reset(dev, devm_spi_unregister_controller, ctlr);
 
+	if (ret == 0 || ctlr->devm_allocated)
+		spi_controller_put(ctlr);
+
+	return ret;
 }
 EXPORT_SYMBOL_GPL(devm_spi_register_controller);
 
Signature · 1 line
2.52.0
Acked-by on v1 1/1
#1 lore raw
Quoted 10 lines · Johan Hovold

The SPI API is asymmetric and the controller is freed as part of deregistration (unless it has been allocated using devm_spi_alloc_host/target()).

A recent change converting the managed registration function to use devm_add_action_or_reset() inadvertently introduced a (mostly theoretical) regression where a non-devres managed controller could be freed as part of failed registration. This in turn would lead to use-after-free in controller driver error paths.

Quoted 8 lines

Fix this by taking another reference before calling devm_add_action_or_reset() and not releasing it on errors for non-devres allocated controllers.

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Thank you!
This is good TODO list now.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 20 lines · Andy Shevchenko
Quoted 10 lines · Johan Hovold

The SPI API is asymmetric and the controller is freed as part of deregistration (unless it has been allocated using devm_spi_alloc_host/target()).

A recent change converting the managed registration function to use devm_add_action_or_reset() inadvertently introduced a (mostly theoretical) regression where a non-devres managed controller could be freed as part of failed registration. This in turn would lead to use-after-free in controller driver error paths.

Quoted 8 lines

Fix this by taking another reference before calling devm_add_action_or_reset() and not releasing it on errors for non-devres allocated controllers.

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Quoted 4 lines

Thank you!
This is good TODO list now.

Seems Mark dropped the original change, so this is not needed.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#3 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Johan Hovold

The SPI API is asymmetric and the controller is freed as part of deregistration (unless it has been allocated using devm_spi_alloc_host/target()).

...

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Seems Mark dropped the original change, so this is not needed.

Not intentionally?

#4 lore raw
Quoted 3 lines · Mark Brown
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Wed, Mar 25, 2026 at 03:53:19PM +0100, Johan Hovold wrote:

...

Quoted 10 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Seems Mark dropped the original change, so this is not needed.

Not intentionally?

I don't know, but it allows to start from fixing the users to avoid regressions. I'm fine with the current state of affairs.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 3 lines · Mark Brown
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Wed, Mar 25, 2026 at 03:53:19PM +0100, Johan Hovold wrote:

...

Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Seems Mark dropped the original change, so this is not needed.

Not intentionally?

I don't know, but it allows to start from fixing the users to avoid regressions. I'm fine with the current state of affairs.

The offending commit is already in mainline and hasn't been reverted in Mark's tree. This fix has been merged there though so nothing more is needed for now.

Johan

#6 lore raw
Quoted 5 lines · Johan Hovold
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Mark Brown
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Andy Shevchenko

On Wed, Mar 25, 2026 at 03:53:19PM +0100, Johan Hovold wrote:

...

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

An alternative would be a partial revert of the offending commit, but it is better to handle this explicitly until the API has been fixed (e.g. see 5e844cc37a5c ("spi: Introduce device-managed SPI controller allocation")).

Seems Mark dropped the original change, so this is not needed.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Not intentionally?

I don't know, but it allows to start from fixing the users to avoid regressions. I'm fine with the current state of affairs.

The offending commit is already in mainline and hasn't been reverted in Mark's tree. This fix has been merged there though so nothing more is needed for now.

Ah, I only looked into Linux Next (without looking into the base, which is v7.0-rc5 as of today). Thanks for clarification.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#7 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 7 lines · Johan Hovold
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Mark Brown

Not intentionally?

I don't know, but it allows to start from fixing the users to avoid regressions. I'm fine with the current state of affairs.

Quoted 6 lines
Quoted 3 lines

The offending commit is already in mainline and hasn't been reverted in Mark's tree. This fix has been merged there though so nothing more is needed for now.

Ah, I only looked into Linux Next (without looking into the base, which is v7.0-rc5 as of today). Thanks for clarification.

Oh, that's a relief! I thought I'd managed to throw some commits away by mistake.