* [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all()
@ 2026-09-30 16:13 Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 1/2] regulator: of: fill in supply names in of_regulator_bulk_get_all() Alexey Charkov
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Alexey Charkov @ 2026-09-30 16:13 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Corentin Labbe
Cc: Alexey Charkov, linux-kernel, stable, Sashiko
of_regulator_bulk_get_all() only fills in the .consumer part of the
struct regulator_bulk_data for the regulators it gets, leaving .supply as
garbage, while the rest of the regulator core expects a meaningful name
there (e.g. for error prints), causing uninitialized memory access in any
failure paths in the core.
It also allocates the supplies array and hands it to callers, but never
says the callers need to free it (which they don't currently), causing
leaks.
Fix the former and document the latter in the kerneldoc to help any new
users avoid the leak.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
Changes in v2:
- Split by subsystem for easier application, per Mark's feedback
- Link to v1: https://patch.msgid.link/20260929-regulator-get-all-v1-0-e887c66a47f1@flipper.net
To: Liam Girdwood <lgirdwood@gmail.com>
To: Mark Brown <broonie@kernel.org>
To: Corentin Labbe <clabbe@baylibre.com>
Cc: linux-kernel@vger.kernel.org
---
Alexey Charkov (2):
regulator: of: fill in supply names in of_regulator_bulk_get_all()
regulator: of: state who owns the array from of_regulator_bulk_get_all()
drivers/regulator/of_regulator.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
---
base-commit: 6c2cb8b8b843d216ab549b678a0d8831c43153e0
change-id: 20260929-regulator-get-all-a553e73ff1ce
Best regards,
--
Alexey Charkov <alchark@flipper.net>
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2] regulator: of: fill in supply names in of_regulator_bulk_get_all()
2026-09-30 16:13 [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Alexey Charkov
@ 2026-09-30 16:13 ` Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 2/2] regulator: of: state who owns the array from of_regulator_bulk_get_all() Alexey Charkov
2026-10-04 16:29 ` [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Mark Brown
2 siblings, 0 replies; 4+ messages in thread
From: Alexey Charkov @ 2026-09-30 16:13 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Corentin Labbe
Cc: Alexey Charkov, linux-kernel, stable, Sashiko
of_regulator_bulk_get_all() returns an array it allocated itself, and
fills in only the consumer of each entry. Every other way of getting a
bulk array has the supply name set, because the caller provides it, and
the core expects it to be there: regulator_bulk_enable() prints it when
a supply fails to enable, so a caller that hands such an array to it
dereferences uninitialised memory on that path.
Copy each name into the array's own allocation, right behind the
entries, so that it shares the array's lifetime and callers still have
nothing extra to free. That also retires the fixed 64 byte stack buffer
the names were assembled in, which is_supply_name() never bounded the
copy against.
Cc: stable@vger.kernel.org
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/patchset/20260928-b4-rk3576-reboot-mode-v1-0-65486b03bd41@flipper.net?part=3
Fixes: 27b9ecc7a9ba ("regulator: Add of_regulator_bulk_get_all")
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/regulator/of_regulator.c | 24 +++++++++++++++++-------
1 file changed, 17 insertions(+), 7 deletions(-)
diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
index c0cc6cc0afd8..785b7a11dfc6 100644
--- a/drivers/regulator/of_regulator.c
+++ b/drivers/regulator/of_regulator.c
@@ -935,15 +935,15 @@ static int is_supply_name(const char *name)
int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
struct regulator_bulk_data **consumers)
{
- int num_consumers = 0;
+ int num_consumers = 0, names_len = 0;
struct regulator *tmp;
struct regulator_bulk_data *_consumers = NULL;
struct property *prop;
+ char *names;
int i, n = 0, ret;
- char name[64];
/*
- * first pass: get numbers of xxx-supply
+ * first pass: get numbers of xxx-supply and the room their names take
* second pass: fill consumers
*/
restart:
@@ -953,16 +953,19 @@ int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
continue;
if (!_consumers) {
num_consumers++;
+ names_len += i + 1;
continue;
} else {
- memcpy(name, prop->name, i);
- name[i] = '\0';
- tmp = regulator_get(dev, name);
+ memcpy(names, prop->name, i);
+ names[i] = '\0';
+ tmp = regulator_get(dev, names);
if (IS_ERR(tmp)) {
ret = PTR_ERR(tmp);
goto error;
}
+ _consumers[n].supply = names;
_consumers[n].consumer = tmp;
+ names += i + 1;
n++;
continue;
}
@@ -973,9 +976,16 @@ int of_regulator_bulk_get_all(struct device *dev, struct device_node *np,
}
if (num_consumers == 0)
return 0;
- _consumers = kmalloc_objs(struct regulator_bulk_data, num_consumers);
+ /*
+ * The supply names are kept in the same allocation as the array, so
+ * that they share its lifetime and the caller has nothing extra to
+ * free.
+ */
+ _consumers = kzalloc(size_add(size_mul(num_consumers, sizeof(*_consumers)),
+ names_len), GFP_KERNEL);
if (!_consumers)
return -ENOMEM;
+ names = (char *)(_consumers + num_consumers);
goto restart;
error:
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] regulator: of: state who owns the array from of_regulator_bulk_get_all()
2026-09-30 16:13 [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 1/2] regulator: of: fill in supply names in of_regulator_bulk_get_all() Alexey Charkov
@ 2026-09-30 16:13 ` Alexey Charkov
2026-10-04 16:29 ` [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Mark Brown
2 siblings, 0 replies; 4+ messages in thread
From: Alexey Charkov @ 2026-09-30 16:13 UTC (permalink / raw)
To: Liam Girdwood, Mark Brown, Corentin Labbe; +Cc: Alexey Charkov, linux-kernel
of_regulator_bulk_get_all() allocates the consumer array and hands it to
the caller, but never said so. Its own error path frees the array, while
regulator_bulk_free() only puts the regulators, so both callers release
the regulators and leak the array.
Spell the contract out in the kerneldoc.
Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
drivers/regulator/of_regulator.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/drivers/regulator/of_regulator.c b/drivers/regulator/of_regulator.c
index 785b7a11dfc6..09e26febfc83 100644
--- a/drivers/regulator/of_regulator.c
+++ b/drivers/regulator/of_regulator.c
@@ -929,6 +929,11 @@ static int is_supply_name(const char *name)
* before returning to the caller, and @consumers will not be
* changed.
*
+ * On success the array is allocated here and handed to the caller, which
+ * owns it from then on: release the regulators with regulator_bulk_free()
+ * and free the array itself with kfree(). The supply names live in the
+ * same allocation, so they are gone once the array is freed.
+ *
* Return: Number of regulators on success, or a negative error number
* on failure.
*/
--
2.55.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all()
2026-09-30 16:13 [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 1/2] regulator: of: fill in supply names in of_regulator_bulk_get_all() Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 2/2] regulator: of: state who owns the array from of_regulator_bulk_get_all() Alexey Charkov
@ 2026-10-04 16:29 ` Mark Brown
2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2026-10-04 16:29 UTC (permalink / raw)
To: Liam Girdwood, Corentin Labbe, Alexey Charkov
Cc: linux-kernel, stable, Sashiko
On Wed, 30 Sep 2026 20:13:02 +0400, Alexey Charkov wrote:
> regulator: of: Fixes to of_regulator_bulk_get_all()
>
> of_regulator_bulk_get_all() only fills in the .consumer part of the
> struct regulator_bulk_data for the regulators it gets, leaving .supply as
> garbage, while the rest of the regulator core expects a meaningful name
> there (e.g. for error prints), causing uninitialized memory access in any
> failure paths in the core.
>
> [...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/regulator.git for-7.3
Thanks!
[1/2] regulator: of: fill in supply names in of_regulator_bulk_get_all()
https://git.kernel.org/broonie/regulator/c/e2151c29cec7
[2/2] regulator: of: state who owns the array from of_regulator_bulk_get_all()
https://git.kernel.org/broonie/regulator/c/b35ff7bd7dd5
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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-10-04 19:17 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-30 16:13 [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 1/2] regulator: of: fill in supply names in of_regulator_bulk_get_all() Alexey Charkov
2026-09-30 16:13 ` [PATCH v2 2/2] regulator: of: state who owns the array from of_regulator_bulk_get_all() Alexey Charkov
2026-10-04 16:29 ` [PATCH v2 0/2] regulator: of: Fixes to of_regulator_bulk_get_all() Mark Brown
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®