Re: [PATCH] power: supply: bq24257: fix use-after-free on remove

Sebastian Reichel <[email protected]> Fri, 31 Jul 2026 23:29:56 +0200
Newsgroups org.kernel.vger.linux-pm,org.kernel.vger.linux-kernel,org.kernel.vger.stable
Message-ID <am0ShrO_o3lJ_V8L@venus>
Hi,

On Fri, Jul 31, 2026 at 02:35:54PM +0000, Fan Wu wrote:
> The STAT-pin interrupt is devm-managed, so it stays armed until the devm
> cleanup that runs after remove() returns. remove() cancels
> bq->iilimit_setup_work while the handler can still fire; the threaded
> handler reschedules that work and dereferences bq, so the work runs
> against freed memory once devm frees bq.
> 
> Call devm_free_irq() before canceling the work so the handler is quiesced
> and cannot reschedule it after the cancel.
> 
> Found by static analysis.
> 
> Fixes: 2219a935963e ("power_supply: Add TI BQ24257 charger driver")
> Cc: [email protected]
> Assisted-by: Codex:gpt-5.6
> Signed-off-by: Fan Wu <[email protected]>
> ---

Instead of this, which renders the device managed part of the IRQ
request pointless, just make the delayed work also device
managed and thus ordered correctly:

1. Move the INIT_DELAYED_WORK() block directly before the IRQ registration
2. Replace INIT_DELAYED_WORK() with devm_delayed_work_autocancel()
3. Drop the now pointless cancel_delayed_work_sync() from remove function

Greetings,

-- Sebastian

>  drivers/power/supply/bq24257_charger.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/power/supply/bq24257_charger.c b/drivers/power/supply/bq24257_charger.c
> index 72f1bfea8d..b9110d5fe2 100644
> --- a/drivers/power/supply/bq24257_charger.c
> +++ b/drivers/power/supply/bq24257_charger.c
> @@ -1064,6 +1064,7 @@ static void bq24257_remove(struct i2c_client *client)
>  {
>  	struct bq24257_device *bq = i2c_get_clientdata(client);
>  
> +	devm_free_irq(&client->dev, client->irq, bq);
>  	if (bq->iilimit_autoset_enable)
>  		cancel_delayed_work_sync(&bq->iilimit_setup_work);
>  
> -- 
> 2.34.1
>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEE72YNB0Y/i3JqeVQT2O7X88g7+poFAmptE8oACgkQ2O7X88g7
+prahQ//dIC78ynMsXKstM6WhszwzzvQfMvNG05wWON+xJUuQQoRp79HvPmeZFrk
9HbOn+qL9dx9vq6amfE6zaKFfV8+U0zvC/AAnbkjU8bcsodcWPqHuaNnhT+MW138
+iGlvge7xhlLC8GLE/xWM6M30Vk5+9yDIlm0DxaQ876ub+TenJgploiF9zhIYaiL
t0DHaDZ1qiVIj1aCa+Ue+hX0QkyZac0/nKF7s75iy5dHwL1+iGFd+eAm8smoiJA8
D0jJBwaPrK/uGPKpWAFfeCsLR/jYjwVUZolf+Rpi9dVCZNBTBtFt/InT0hX5N8vS
lB7SROz9sIv3ICcbwvGNjsS30xrYi+kQFt5I7PGTHCMfMFKcglfBIq/xHRPZQugd
8v+d3lVVQdHV+TyhPJIMDHC65+eNnBgUup2OANGeWq/wq8DHwgCBrD3uco0HS+58
/MFvcL7gy5u2DUWZRQ4NhlH4wBMR3WCIQ4uWMQKtfu6kM7iZYtAMRAmg+dBaIfa6
YorgGQvvhZ7HnNdxZ1MX2yuRFNMnX6OyztKZ6h02ZaCcGoMfHfWO8HiPFidSPqkk
GwN9/c0SzNgGoIc9H9ZcYPDy1mE5W/Dsi2iXF7fmSvtW0nBL9n7os6Fs6SYWX7tP
8ZXHO3o0B6ZgrAN+oWZo5wJc3RfBkQnOjnnLOXISLZIrf5av4NY=
=/mKT
-----END PGP SIGNATURE-----