btrfs-progs: subvol list: fix the wrong timestamp and UUID check for root items

[BUG]
Since commit d729048be6 ("btrfs-progs: stop using
btrfs_root_item_v0"), "btrfs subvolume list -u" not longer correctly
reports UUID nor timestamp, while older (btrfs-progs v6.0.2) still works
correctly:

 v6.0.2:
 # btrfs subv list -u  /mnt/btrfs/
 ID 256 gen 12 top level 5 uuid ed4af580-d512-2644-b392-2a71aaeeb99e path subv1
 ID 257 gen 13 top level 5 uuid a22ccba7-0a0a-a94f-af4b-5116ab58bb61 path subv2

 v6.1:
 # ./btrfs subv list -u /mnt/btrfs/
 ID 256 gen 12 top level 5 uuid -                                    path subv1
 ID 257 gen 13 top level 5 uuid -                                    path subv2

[CAUSE]
Commit d729048be6 ("btrfs-progs: stop using btrfs_root_item_v0")
removed old btrfs_root_item_v0, but incorrectly changed the check for
v0 root item.

Now we will treat v0 root items as latest root items, causing possible
out-of-bound access, while treating current root items as older v0 root
items, ignoring the UUID nor timestamp.

[FIX]
Fix the bug by using correct checks, and add extra comments on the
branches.

Issue: #562
Fixes: d729048be6 ("btrfs-progs: stop using btrfs_root_item_v0")
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Reviewed-by: Neal Gompa <neal@gompa.dev>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Qu Wenruo 2022-12-27 13:55:07 +08:00 committed by David Sterba
parent cde5faa678
commit b7e3acd615

View file

@ -870,14 +870,21 @@ static int list_subvol_search(int fd, struct rb_root *root_lookup)
ri = (struct btrfs_root_item *)(args.buf + off); ri = (struct btrfs_root_item *)(args.buf + off);
gen = btrfs_root_generation(ri); gen = btrfs_root_generation(ri);
flags = btrfs_root_flags(ri); flags = btrfs_root_flags(ri);
if(sh.len < if(sh.len >= sizeof(struct btrfs_root_item)) {
sizeof(struct btrfs_root_item)) { /*
* The new full btrfs_root_item with
* timestamp and UUID.
*/
otime = btrfs_stack_timespec_sec(&ri->otime); otime = btrfs_stack_timespec_sec(&ri->otime);
ogen = btrfs_root_otransid(ri); ogen = btrfs_root_otransid(ri);
memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE); memcpy(uuid, ri->uuid, BTRFS_UUID_SIZE);
memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE); memcpy(puuid, ri->parent_uuid, BTRFS_UUID_SIZE);
memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE); memcpy(ruuid, ri->received_uuid, BTRFS_UUID_SIZE);
} else { } else {
/*
* The old v0 root item, which doesn't
* have timestamp nor UUID.
*/
otime = 0; otime = 0;
ogen = 0; ogen = 0;
memset(uuid, 0, BTRFS_UUID_SIZE); memset(uuid, 0, BTRFS_UUID_SIZE);