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

regulator: gpio: Fix the out-of-bounds access to drvdata::gpiods

drvdata::gpiods is supposed to hold an array of 'gpio_desc' pointers. But
the memory is allocated for only one pointer. This will lead to
out-of-bounds access later in the code if 'config::ngpios' is > 1. So
fix the code to allocate enough memory to hold 'config::ngpios' of GPIO
descriptors.

While at it, also move the check for memory allocation failure to be below
the allocation to make it more readable.

Cc: stable@vger.kernel.org # 5.0
Fixes: d6cd33ad7102 ("regulator: gpio: Convert to use descriptors")
Signed-off-by: Manivannan Sadhasivam <mani@kernel.org>
Link: https://patch.msgid.link/20250703103549.16558-1-mani@kernel.org
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Manivannan Sadhasivam and committed by
Mark Brown
c9764fd8 6729c134

+4 -4
+4 -4
drivers/regulator/gpio-regulator.c
··· 260 260 return -ENOMEM; 261 261 } 262 262 263 - drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *), 264 - GFP_KERNEL); 263 + drvdata->gpiods = devm_kcalloc(dev, config->ngpios, 264 + sizeof(struct gpio_desc *), GFP_KERNEL); 265 + if (!drvdata->gpiods) 266 + return -ENOMEM; 265 267 266 268 if (config->input_supply) { 267 269 drvdata->desc.supply_name = devm_kstrdup(&pdev->dev, ··· 276 274 } 277 275 } 278 276 279 - if (!drvdata->gpiods) 280 - return -ENOMEM; 281 277 for (i = 0; i < config->ngpios; i++) { 282 278 drvdata->gpiods[i] = devm_gpiod_get_index(dev, 283 279 NULL,