Skip to content

Commit

Permalink
spi: fix use-after-free of the add_lock mutex
Browse files Browse the repository at this point in the history
Commit 6098475 ("spi: Fix deadlock when adding SPI controllers on
SPI buses") introduced a per-controller mutex. But mutex_unlock() of
said lock is called after the controller is already freed:

  spi_unregister_controller(ctlr)
  -> put_device(&ctlr->dev)
    -> spi_controller_release(dev)
  -> mutex_unlock(&ctrl->add_lock)

Move the put_device() after the mutex_unlock().

Fixes: 6098475 ("spi: Fix deadlock when adding SPI controllers on SPI buses")
Signed-off-by: Michael Walle <michael@walle.cc>
Reviewed-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Reviewed-by: Lukas Wunner <lukas@wunner.de>
Cc: stable@vger.kernel.org # v5.15
Link: https://lore.kernel.org/r/20211111083713.3335171-1-michael@walle.cc
Signed-off-by: Mark Brown <broonie@kernel.org>
  • Loading branch information
Michael Walle authored and Mark Brown committed Nov 12, 2021
1 parent 6532582 commit 6c53b45
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions drivers/spi/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -3058,12 +3058,6 @@ void spi_unregister_controller(struct spi_controller *ctlr)

device_del(&ctlr->dev);

/* Release the last reference on the controller if its driver
* has not yet been converted to devm_spi_alloc_master/slave().
*/
if (!ctlr->devm_allocated)
put_device(&ctlr->dev);

/* free bus id */
mutex_lock(&board_lock);
if (found == ctlr)
Expand All @@ -3072,6 +3066,12 @@ void spi_unregister_controller(struct spi_controller *ctlr)

if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
mutex_unlock(&ctlr->add_lock);

/* Release the last reference on the controller if its driver
* has not yet been converted to devm_spi_alloc_master/slave().
*/
if (!ctlr->devm_allocated)
put_device(&ctlr->dev);
}
EXPORT_SYMBOL_GPL(spi_unregister_controller);

Expand Down

0 comments on commit 6c53b45

Please sign in to comment.