Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 free

We add a variable to record the failure of device_register() and
move put_device() down a bit to avoid the above issues.

Fixes: c438b9d01736 ("regulator: core: Move registration of regulator device")
Signed-off-by: Wen Yang <wenyang@linux.alibaba.com>
Cc: Liam Girdwood <lgirdwood@gmail.com>
Cc: Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org
Link: https://lore.kernel.org/r/20191201030250.38074-1-wenyang@linux.alibaba.com
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Wen Yang and committed by
Mark Brown
a3cde953 4affd79a

+6 -2
+6 -2
drivers/regulator/core.c
··· 4998 4998 struct regulator_dev *rdev; 4999 4999 bool dangling_cfg_gpiod = false; 5000 5000 bool dangling_of_gpiod = false; 5001 + bool reg_device_fail = false; 5001 5002 struct device *dev; 5002 5003 int ret, i; 5003 5004 ··· 5184 5183 dev_set_drvdata(&rdev->dev, rdev); 5185 5184 ret = device_register(&rdev->dev); 5186 5185 if (ret != 0) { 5187 - put_device(&rdev->dev); 5186 + reg_device_fail = true; 5188 5187 goto unset_supplies; 5189 5188 } 5190 5189 ··· 5214 5213 clean: 5215 5214 if (dangling_of_gpiod) 5216 5215 gpiod_put(config->ena_gpiod); 5217 - kfree(rdev); 5216 + if (reg_device_fail) 5217 + put_device(&rdev->dev); 5218 + else 5219 + kfree(rdev); 5218 5220 kfree(config); 5219 5221 rinse: 5220 5222 if (dangling_cfg_gpiod)