Skip to content

Commit 6291495

Browse files
whamesre
authored andcommitted
power: supply: pm8916_bms_vm: Fix use-after-free in power_supply_changed()
Using the `devm_` variant for requesting IRQ _before_ the `devm_` variant for allocating/registering the `power_supply` handle, means that the `power_supply` handle will be deallocated/unregistered _before_ the interrupt handler (since `devm_` naturally deallocates in reverse allocation order). This means that during removal, there is a race condition where an interrupt can fire just _after_ the `power_supply` handle has been freed, *but* just _before_ the corresponding unregistration of the IRQ handler has run. This will lead to the IRQ handler calling `power_supply_changed()` with a freed `power_supply` handle. Which usually crashes the system or otherwise silently corrupts the memory... Note that there is a similar situation which can also happen during `probe()`; the possibility of an interrupt firing _before_ registering the `power_supply` handle. This would then lead to the nasty situation of using the `power_supply` handle *uninitialized* in `power_supply_changed()`. Fix this racy use-after-free by making sure the IRQ is requested _after_ the registration of the `power_supply` handle. Fixes: 098bce1 ("power: supply: Add pm8916 VM-BMS support") Signed-off-by: Waqar Hameed <waqar.hameed@axis.com> Reviewed-by: Nikita Travkin <nikita@trvn.ru> Link: https://patch.msgid.link/2749c09ff81fcac87ae48147e216135450d8c067.1766268280.git.waqar.hameed@axis.com Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
1 parent 838767f commit 6291495

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

drivers/power/supply/pm8916_bms_vm.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,6 @@ static int pm8916_bms_vm_battery_probe(struct platform_device *pdev)
167167
if (ret < 0)
168168
return -EINVAL;
169169

170-
irq = platform_get_irq_byname(pdev, "fifo");
171-
if (irq < 0)
172-
return irq;
173-
174-
ret = devm_request_threaded_irq(dev, irq, NULL, pm8916_bms_vm_fifo_update_done_irq,
175-
IRQF_ONESHOT, "pm8916_vm_bms", bat);
176-
if (ret)
177-
return ret;
178-
179170
ret = regmap_bulk_read(bat->regmap, bat->reg + PM8916_PERPH_TYPE, &tmp, 2);
180171
if (ret)
181172
goto comm_error;
@@ -220,6 +211,15 @@ static int pm8916_bms_vm_battery_probe(struct platform_device *pdev)
220211
if (ret)
221212
return dev_err_probe(dev, ret, "Unable to get battery info\n");
222213

214+
irq = platform_get_irq_byname(pdev, "fifo");
215+
if (irq < 0)
216+
return irq;
217+
218+
ret = devm_request_threaded_irq(dev, irq, NULL, pm8916_bms_vm_fifo_update_done_irq,
219+
IRQF_ONESHOT, "pm8916_vm_bms", bat);
220+
if (ret)
221+
return ret;
222+
223223
platform_set_drvdata(pdev, bat);
224224

225225
return 0;

0 commit comments

Comments
 (0)