Skip to content
/ linux Public

Commit 8618315

Browse files
whameSasha Levin
authored andcommitted
power: supply: wm97xx: Fix NULL pointer dereference in power_supply_changed()
[ Upstream commit 39fe0ea ] In `probe()`, `request_irq()` is called before allocating/registering a `power_supply` handle. If an interrupt is fired between the call to `request_irq()` and `power_supply_register()`, the `power_supply` handle will be used uninitialized in `power_supply_changed()` in `wm97xx_bat_update()` (triggered from the interrupt handler). This will lead to a `NULL` pointer dereference since Fix this racy `NULL` pointer dereference by making sure the IRQ is requested _after_ the registration of the `power_supply` handle. Since the IRQ is the last thing requests in the `probe()` now, remove the error path for freeing it. Instead add one for unregistering the `power_supply` handle when IRQ request fails. Fixes: 7c87942 ("wm97xx_battery: Use irq to detect charger state") Signed-off-by: Waqar Hameed <waqar.hameed@axis.com> Link: https://patch.msgid.link/97b55f0479a932eea7213844bf66f28a974e27a2.1766270196.git.waqar.hameed@axis.com Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 3af85f2 commit 8618315

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

drivers/power/supply/wm97xx_battery.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,6 @@ static int wm97xx_bat_probe(struct platform_device *dev)
178178
"failed to get charge GPIO\n");
179179
if (charge_gpiod) {
180180
gpiod_set_consumer_name(charge_gpiod, "BATT CHRG");
181-
ret = request_irq(gpiod_to_irq(charge_gpiod),
182-
wm97xx_chrg_irq, 0,
183-
"AC Detect", dev);
184-
if (ret)
185-
return dev_err_probe(&dev->dev, ret,
186-
"failed to request GPIO irq\n");
187181
props++; /* POWER_SUPPLY_PROP_STATUS */
188182
}
189183

@@ -199,10 +193,8 @@ static int wm97xx_bat_probe(struct platform_device *dev)
199193
props++; /* POWER_SUPPLY_PROP_VOLTAGE_MIN */
200194

201195
prop = kcalloc(props, sizeof(*prop), GFP_KERNEL);
202-
if (!prop) {
203-
ret = -ENOMEM;
204-
goto err3;
205-
}
196+
if (!prop)
197+
return -ENOMEM;
206198

207199
prop[i++] = POWER_SUPPLY_PROP_PRESENT;
208200
if (charge_gpiod)
@@ -236,15 +228,27 @@ static int wm97xx_bat_probe(struct platform_device *dev)
236228
schedule_work(&bat_work);
237229
} else {
238230
ret = PTR_ERR(bat_psy);
239-
goto err4;
231+
goto free;
232+
}
233+
234+
if (charge_gpiod) {
235+
ret = request_irq(gpiod_to_irq(charge_gpiod), wm97xx_chrg_irq,
236+
0, "AC Detect", dev);
237+
if (ret) {
238+
dev_err_probe(&dev->dev, ret,
239+
"failed to request GPIO irq\n");
240+
goto unregister;
241+
}
240242
}
241243

242244
return 0;
243-
err4:
245+
246+
unregister:
247+
power_supply_unregister(bat_psy);
248+
249+
free:
244250
kfree(prop);
245-
err3:
246-
if (charge_gpiod)
247-
free_irq(gpiod_to_irq(charge_gpiod), dev);
251+
248252
return ret;
249253
}
250254

0 commit comments

Comments
 (0)