Re: [PATCH] firewire: core: defer old_rom assignment until kmemdup() succeeds
From: Takashi Sakamoto
Date: Thu Dec 03 2026 - 08:37:10 EST
On Wed, Dec 02, 2026 at 07:02:50PM +0800, Ziheng Wei wrote:
> In read_config_rom(), old_rom is declared with __free(kfree) and assigned
> device->config_rom before calling kmemdup(). If kmemdup() fails and returns
> -ENOMEM during fw_device_refresh(), the cleanup handler frees old_rom while
> device->config_rom still points to the freed buffer. Subsequent config ROM
> reads (via sysfs or refresh retry) then hit a use-after-free, and
> fw_device_release() double-frees device->config_rom.
>
> Defer the assignment of old_rom until after kmemdup() succeeds, inside the
> fw_device_rwsem write lock where device->config_rom is replaced.
>
> Found by LLM-assisted static analysis. Compile-tested on x86_64.
>
> Fixes: b330f98ff238 ("firewire: core: use cleanup function to release cached configuration ROM")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Ziheng Wei <zihengw03@xxxxxxx>
> ---
> drivers/firewire/core-device.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
Good catch. Applied to for-linus branch.
> diff --git a/drivers/firewire/core-device.c b/drivers/firewire/core-device.c
> index a68100d3d..130c15b07 100644
> --- a/drivers/firewire/core-device.c
> +++ b/drivers/firewire/core-device.c
> @@ -797,12 +797,12 @@ static int read_config_rom(struct fw_device *device, int generation)
>
> device->max_speed = speed;
>
> - old_rom = device->config_rom;
> new_rom = kmemdup(rom, length * 4, GFP_KERNEL);
> if (new_rom == NULL)
> return -ENOMEM;
>
> scoped_guard(rwsem_write, &fw_device_rwsem) {
> + old_rom = device->config_rom;
> device->config_rom = new_rom;
> device->config_rom_length = length;
> }
>
> base-commit: 97d73c8251226cf27cc6656b4c542869bb8c69fa
> --
> 2.43.0
Thanks
Takashi Sakamoto