Skip to content

Commit c4af8a9

Browse files
whamesre
authored andcommitted
power: supply: ab8500: 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()`. Commit 1c1f13a ("power: supply: ab8500: Move to componentized binding") introduced this issue during a refactorization. Fix this racy use-after-free by making sure the IRQ is requested _after_ the registration of the `power_supply` handle. Fixes: 1c1f13a ("power: supply: ab8500: Move to componentized binding") Signed-off-by: Waqar Hameed <waqar.hameed@axis.com> Reviewed-by: Linus Walleij <linusw@kernel.org> Link: https://patch.msgid.link/ccf83a09942cb8dda3dff70b2682f2c2e9cb97f2.1766268280.git.waqar.hameed@axis.com Signed-off-by: Sebastian Reichel <sebastian.reichel@collabora.com>
1 parent ce3bc84 commit c4af8a9

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

drivers/power/supply/ab8500_charger.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,26 +3466,6 @@ static int ab8500_charger_probe(struct platform_device *pdev)
34663466
return ret;
34673467
}
34683468

3469-
/* Request interrupts */
3470-
for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3471-
irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3472-
if (irq < 0)
3473-
return irq;
3474-
3475-
ret = devm_request_threaded_irq(dev,
3476-
irq, NULL, ab8500_charger_irq[i].isr,
3477-
IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3478-
ab8500_charger_irq[i].name, di);
3479-
3480-
if (ret != 0) {
3481-
dev_err(dev, "failed to request %s IRQ %d: %d\n"
3482-
, ab8500_charger_irq[i].name, irq, ret);
3483-
return ret;
3484-
}
3485-
dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3486-
ab8500_charger_irq[i].name, irq, ret);
3487-
}
3488-
34893469
/* initialize lock */
34903470
spin_lock_init(&di->usb_state.usb_lock);
34913471
mutex_init(&di->usb_ipt_crnt_lock);
@@ -3614,6 +3594,26 @@ static int ab8500_charger_probe(struct platform_device *pdev)
36143594
return PTR_ERR(di->usb_chg.psy);
36153595
}
36163596

3597+
/* Request interrupts */
3598+
for (i = 0; i < ARRAY_SIZE(ab8500_charger_irq); i++) {
3599+
irq = platform_get_irq_byname(pdev, ab8500_charger_irq[i].name);
3600+
if (irq < 0)
3601+
return irq;
3602+
3603+
ret = devm_request_threaded_irq(dev,
3604+
irq, NULL, ab8500_charger_irq[i].isr,
3605+
IRQF_SHARED | IRQF_NO_SUSPEND | IRQF_ONESHOT,
3606+
ab8500_charger_irq[i].name, di);
3607+
3608+
if (ret != 0) {
3609+
dev_err(dev, "failed to request %s IRQ %d: %d\n"
3610+
, ab8500_charger_irq[i].name, irq, ret);
3611+
return ret;
3612+
}
3613+
dev_dbg(dev, "Requested %s IRQ %d: %d\n",
3614+
ab8500_charger_irq[i].name, irq, ret);
3615+
}
3616+
36173617
/*
36183618
* Check what battery we have, since we always have the USB
36193619
* psy, use that as a handle.

0 commit comments

Comments
 (0)