Re: [PATCH 1/6] w1: Free slave structure in w1_slave_release()

From: Krzysztof Kozlowski

Date: Wed Dec 02 2026 - 05:38:17 EST


On 02/12/2026 10:36, Lee Jones wrote:
> w1_unref_slave() calls kfree(sl) immediately after device_unregister(),
> violating the driver model lifetime rules. If another thread holds a
> reference on sl->dev (such as via sysfs or device iteration), the
> embedded struct device is freed while still in use, causing a
> use-after-free when w1_slave_release() later runs. Similarly,
> __w1_attach_slave_device() calls put_device(&sl->dev) on registration
> failure, so freeing sl in w1_slave_release() means
> w1_attach_slave_device() must not touch or free sl afterwards.
>
> Move kfree(sl) and the final atomic_dec(&master->refcnt) into
> w1_slave_release() so both the slave and its master remain valid until
> the final device reference is released. Adjust w1_unref_slave() to
> decrement dev->refcnt only when refcnt remains non-zero, and update the
> error path in w1_attach_slave_device() accordingly.
>
> Fixes: 9fcbbac5ded4 ("w1: process w1 netlink commands in w1_process thread")
> Closes: https://sashiko.dev/#/bug/linux-8d066669-dde3-4c3f-a842-12383edebc99
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
> ---


Reviewed-by: Krzysztof Kozlowski <krzysztof.kozlowski@xxxxxxxxxxxxxxxx>

Best regards,
Krzysztof