regmap: ram: fix memory leaks in __regmap_init_ram() on error
Subsystems (2)
PATCH 1/1 regmap: ram: fix memory leaks in __regmap_init_ram() on error
Two allocations in __regmap_init_ram() are not cleaned up on failure.
If the kzalloc_objs() for data->written fails, data->read is returned with no way for the caller to free it.
If __regmap_init() fails, neither data->read nor data->written is freed because its error paths do not call bus->free_context() (which is regmap_ram_free_context() here). Only regmap_exit() does, and that is never reached on an init failure.
Free the allocated arrays before returning any error.
--- drivers/base/regmap/regmap-ram.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/base/regmap/regmap-ram.c +7 -1
@@ -71,11 +71,17 @@ struct regmap *__regmap_init_ram(struct device *dev,return ERR_PTR (-ENOMEM ); data->written =kzalloc_objs (bool, config->max_register +1 ); -if (!data->written) +if (!data->written) { +kfree (data->read);return ERR_PTR (-ENOMEM ); + } map =__regmap_init (dev, ®map_ram, data, config, lock_key, lock_name); +if (IS_ERR (map)) { +kfree (data->read); +kfree (data->written); + }return map; }
Signature · 1 line
2.50.1 (Apple Git-155)
Mark Brown
maintainerQuoted 1 line · Yuho Choi
regmap: ram: fix memory leaks in
__regmap_init_ram()on error
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.1
Thanks!
[1/1] regmap: ram: fix memory leaks in __regmap_init_ram() on error
https://git.kernel.org/broonie/sound/c/7e555fcae40a
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
Dear Mark,
Thanks for applying the patch.
Best regards,
Yuho Choi
Quoted 32 lines · Mark Brown
Quoted 1 line · Yuho Choi
regmap: ram: fix memory leaks in
__regmap_init_ram()on errorApplied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regmap.git for-7.1
Thanks!
[1/1] regmap: ram: fix memory leaks in
__regmap_init_ram()on errorhttps://git.kernel.org/broonie/sound/c/7e555fcae40aAll 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