On Sat, Nov 7, 2026 at 2:49 AM Mikulas Patocka <[email protected]> wrote:
>
> If __set_snapshot_details fails, the variable td is leaked - this
> prevents any other snapshot with the same id from being created.
>
> Fix this by removing it from the list and freeing it.
>
> Signed-off-by: Mikulas Patocka <[email protected]>
> Assisted-by: Claude:claude-opus-5
> Cc: [email protected]
> Fixes: 991d9fa02da0 ("dm: add thin provisioning target")
>
> ---
>  drivers/md/dm-thin-metadata.c |    5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> Index: linux-2.6/drivers/md/dm-thin-metadata.c
> ===================================================================
> --- linux-2.6.orig/drivers/md/dm-thin-metadata.c
> +++ linux-2.6/drivers/md/dm-thin-metadata.c
> @@ -1225,10 +1225,13 @@ static int __create_snap(struct dm_pool_
>         __close_device(td);
>
>         if (r)
> -               goto bad;
> +               goto bad2;
>
>         return 0;
>
> +bad2:
> +       list_del(&td->list);
> +       kfree(td);
>  bad:
>         dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
>         dm_btree_remove(&pmd->details_info, pmd->details_root,
>
>

The bug is hard to trigger. __set_snapshot_details() fails only when
__open_device(origin) fails. There are three causes: the origin
details are missing, the metadata read fails, or kmalloc() fails. All
three are corner cases. Even so, the patch is still needed. Otherwise
td stays on pmd->thin_devices with changed = 1. On the next metadata
commit, __write_changed_details() writes this invalid device entry
into the details tree, breaking consistency between the mapping tree
and the details tree.

There is a separate issue in the same error path. The final
dm_btree_remove(&pmd->details_info, ...) at the bad: label should be
removed. The snapshot details are not in the details tree during
__create_snap(), so this call always runs on a missing key and returns
-ENODATA. dm_btree_remove() skips the new_root update on -ENODATA, but
create_snap's error path ignores this return code and commits the
undefined metadata anyway. This leaves pmd->details_root dangling and
leaks metadata blocks. I'll send a separate patch to drop the
details_info remove.

Reviewed-by: Ming-Hung Tsai <[email protected]>


Reply via email to