Re: [PATCH] power: supply: Fix info use-after-free

From: Vincent Whitchurch
Date: Tue Sep 19 2023 - 03:33:09 EST


On Mon, 2023-09-18 at 17:04 +0200, Sebastian Reichel wrote:
> diff --git a/drivers/power/supply/power_supply_sysfs.c b/drivers/power/supply/power_supply_sysfs.c
> index 06e5b6b0e255..d483a81560ab 100644
> --- a/drivers/power/supply/power_supply_sysfs.c
> +++ b/drivers/power/supply/power_supply_sysfs.c
> @@ -482,6 +482,13 @@ int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env)
>         if (ret)
>                 return ret;
>  
>
> + /*
> + * Kernel generates KOBJ_REMOVE uevent in device removal path, after
> + * resources have been freed. Exit early to avoid use-after-free.
> + */
> + if (psy->removing)
> + return 0;
> +
>         prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
>         if (!prop_buf)
>                 return -ENOMEM;
>
> That would also cover the no-parent-device part and avoid the
> device(s) being queried for data at device removal time, which
> wouldn't work if the device removal happens due to a hot-unplug.

Works for me.

Tested-by: Vincent Whitchurch <vincent.whitchurch@xxxxxxxx>