]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: fix a memory leak in __btrfs_map_block() after RST lookup failure
authorQu Wenruo <wqu@suse.com>
Thu, 11 Jun 2026 00:46:41 +0000 (10:16 +0930)
committerDavid Sterba <dsterba@suse.com>
Tue, 14 Jul 2026 21:14:26 +0000 (23:14 +0200)
[BUG]
With all previous memory leaks fixed, now mkfs/030 fails with the
following memory leak:

====== RUN CHECK /home/adam/btrfs-progs/mkfs.btrfs -f -O zoned -d dup -m single /dev/nullb0 /dev/nullb1 /dev/nullb2 /dev/nullb3
WARNING: DUP is not recommended on filesystem with multiple devices
WARNING: DUP may not actually lead to 2 copies on the device, see manual page

=================================================================
==48914==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 144 byte(s) in 1 object(s) allocated from:
    #0 0x7fe9247205dd in calloc /usr/src/debug/gcc/gcc/libsanitizer/asan/asan_malloc_linux.cpp:74
    #1 0x5587da350c9a in __btrfs_map_block kernel-shared/volumes.c:2236
    #2 0x5587da34f4b9 in btrfs_map_block kernel-shared/volumes.c:2002
    #3 0x5587da279470 in queue_discard_logical mkfs/main.c:1355
    #4 0x5587da27a01b in discard_free_space mkfs/main.c:1453
    #5 0x5587da2825a8 in main mkfs/main.c:2587
    #6 0x7fe924227634  (/usr/lib/libc.so.6+0x27634) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
    #7 0x7fe9242276e8 in __libc_start_main (/usr/lib/libc.so.6+0x276e8) (BuildId: 2f722da304c0a508c891285e6840199c35019c8d)
    #8 0x5587da26ffa4 in _start (/home/adam/btrfs-progs/mkfs.btrfs+0x3cfa4) (BuildId: 67b730427c6a4739addff9bd31ecd89b1b5ae11a)

[CAUSE]
In __btrfs_map_block() if the fs has raid-stripe-tree enabled, we need
to do one extra mapping to get the physical address.

However btrfs_stripe_tree_logical_to_physical() can fail, and if that
function failed, we error out without freeing @multi we have allocated,
resulting the above memory leak.

[FIX]
Do the proper error handling before erroring out after
btrfs_stripe_tree_logical_to_physical() failure.

Pull-request: #1131
Fixes: a9fd50f85fdd ("btrfs-progs: read stripe tree when mapping blocks")
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
kernel-shared/volumes.c

index c3d113b6e3b186fef3fb90890a908efd9790411c..f35b59bcc20ee972ae69be9a1e55f80b6858da4f 100644 (file)
@@ -2396,8 +2396,10 @@ again:
 
                        ret = btrfs_stripe_tree_logical_to_physical(fs_info, logical,
                                                                    &multi->stripes[i]);
-                       if (ret)
+                       if (ret) {
+                               kfree(multi);
                                return ret;
+                       }
                } else {
                        multi->stripes[i].physical =
                                map->stripes[stripe_index].physical +