regulator: core: fix regulator_register() error paths to properly release rdev
PATCH 1/1 regulator: core: fix regulator_register() error paths to properly release rdev
There are several issues with the error handling code of the regulator_register() function:
ret = device_register(&rdev->dev);
if (ret != 0) {
put_device(&rdev->dev); --> rdev released
goto unset_supplies;
}
...
unset_supplies:
...
unset_regulator_supplies(rdev); --> use-after-free
...
clean:
if (dangling_of_gpiod)
gpiod_put(config->ena_gpiod);
kfree(rdev); --> double freeWe add a variable to record the failure of device_register() and move put_device() down a bit to avoid the above issues.
--- drivers/regulator/core.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/regulator/core.c +6 -2
@@ -5002,6 +5002,7 @@ struct regulator_dev *struct regulator_dev *rdev;bool dangling_cfg_gpiod = false;bool dangling_of_gpiod = false; +bool reg_device_fail = false;struct device *dev;int ret, i;
@@ -5187,7 +5188,7 @@ struct regulator_dev *dev_set_drvdata (&rdev->dev, rdev); ret =device_register (&rdev->dev);if (ret !=0 ) { -put_device (&rdev->dev); + reg_device_fail = true; goto unset_supplies; }
@@ -5218,7 +5219,10 @@ struct regulator_dev * clean:if (dangling_of_gpiod)gpiod_put (config->ena_gpiod); -kfree (rdev); +if (reg_device_fail) +put_device (&rdev->dev); +else +kfree (rdev);kfree (config); rinse:if (dangling_cfg_gpiod)
Signature · 1 line
1.8.3.1
Mark Brown
maintainerApplied "regulator: core: fix regulator_register() error paths to properly release rdev" to the regulator tree
The patch
regulator: core: fix regulator_register() error paths to properly release rdev
has been applied to the regulator tree at
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-5.5
All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying to this mail.
Thanks,
Mark
From a3cde9534ebdafe18a9bbab208df724c57e6c8e8 Mon Sep 17 00:00:00 2001
From: Wen Yang <[email protected]>
Date: Sun, 1 Dec 2019 11:02:50 +0800
Subject: [PATCH] regulator: core: fix regulator_register() error paths to
properly release rdev
There are several issues with the error handling code of the regulator_register() function:
ret = device_register(&rdev->dev);
if (ret != 0) {
put_device(&rdev->dev); --> rdev released
goto unset_supplies;
}
...
unset_supplies:
...
unset_regulator_supplies(rdev); --> use-after-free
...
clean:
if (dangling_of_gpiod)
gpiod_put(config->ena_gpiod);
kfree(rdev); --> double freeWe add a variable to record the failure of device_register() and move put_device() down a bit to avoid the above issues.
--- drivers/regulator/core.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/regulator/core.c +6 -2
@@ -4998,6 +4998,7 @@ regulator_register(const struct regulator_desc *regulator_desc,struct regulator_dev *rdev;bool dangling_cfg_gpiod = false;bool dangling_of_gpiod = false; +bool reg_device_fail = false;struct device *dev;int ret, i;
@@ -5183,7 +5184,7 @@ regulator_register(const struct regulator_desc *regulator_desc,dev_set_drvdata (&rdev->dev, rdev); ret =device_register (&rdev->dev);if (ret !=0 ) { -put_device (&rdev->dev); + reg_device_fail = true; goto unset_supplies; }
@@ -5213,7 +5214,10 @@ regulator_register(const struct regulator_desc *regulator_desc, clean:if (dangling_of_gpiod)gpiod_put (config->ena_gpiod); -kfree (rdev); +if (reg_device_fail) +put_device (&rdev->dev); +else +kfree (rdev);kfree (config); rinse:if (dangling_cfg_gpiod)
Signature · 1 line
2.20.1