[PATCH 8/8] regulator: aat2870: Duplicate regulator data per device

From: Lee Jones

Date: Fri Nov 27 2026 - 13:37:26 EST


aat2870_get_regulator() returns a pointer into the static global
aat2870_regulators array and mutates its fields in place, after which
aat2870_regulator_probe() stores the parent MFD pointer into that shared
entry. Probing a second AAT2870 device overwrites the first device's
state and leads to misrouted operations or use-after-free on removal.

Mark aat2870_regulators const and duplicate the matching entry per
device with devm_kmemdup() before registering the regulator.

Fixes: f7eb6c5e8e5e ("regulator: aat2870: Add AAT2870 regulator driver")
Closes: https://sashiko.dev/#/bug/linux-6f356dcb-fbbc-487c-8fb3-0f765ac5a4d6
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/regulator/aat2870-regulator.c | 30 +++++++++++++++++----------
1 file changed, 19 insertions(+), 11 deletions(-)

diff --git a/drivers/regulator/aat2870-regulator.c b/drivers/regulator/aat2870-regulator.c
index 52c08e6c4803..326886371e85 100644
--- a/drivers/regulator/aat2870-regulator.c
+++ b/drivers/regulator/aat2870-regulator.c
@@ -114,26 +114,33 @@ static const unsigned int aat2870_ldo_voltages[] = {
}, \
}

-static struct aat2870_regulator aat2870_regulators[] = {
+static const struct aat2870_regulator aat2870_regulators[] = {
AAT2870_LDO(LDOA),
AAT2870_LDO(LDOB),
AAT2870_LDO(LDOC),
AAT2870_LDO(LDOD),
};

-static struct aat2870_regulator *aat2870_get_regulator(int id)
+static struct aat2870_regulator *aat2870_get_regulator(struct device *dev,
+ int id)
{
- struct aat2870_regulator *ri = NULL;
+ const struct aat2870_regulator *template = NULL;
+ struct aat2870_regulator *ri;
int i;

for (i = 0; i < ARRAY_SIZE(aat2870_regulators); i++) {
- ri = &aat2870_regulators[i];
- if (ri->desc.id == id)
+ if (aat2870_regulators[i].desc.id == id) {
+ template = &aat2870_regulators[i];
break;
+ }
}

- if (i == ARRAY_SIZE(aat2870_regulators))
- return NULL;
+ if (!template)
+ return ERR_PTR(-EINVAL);
+
+ ri = devm_kmemdup(dev, template, sizeof(*ri), GFP_KERNEL);
+ if (!ri)
+ return ERR_PTR(-ENOMEM);

ri->enable_addr = AAT2870_LDO_EN;
ri->enable_shift = id - AAT2870_ID_LDOA;
@@ -153,10 +160,11 @@ static int aat2870_regulator_probe(struct platform_device *pdev)
struct regulator_config config = { };
struct regulator_dev *rdev;

- ri = aat2870_get_regulator(pdev->id);
- if (!ri) {
- dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
- return -EINVAL;
+ ri = aat2870_get_regulator(&pdev->dev, pdev->id);
+ if (IS_ERR(ri)) {
+ if (PTR_ERR(ri) == -EINVAL)
+ dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
+ return PTR_ERR(ri);
}
ri->aat2870 = dev_get_drvdata(pdev->dev.parent);

--
2.56.1.839.g22a8290db9-goog