Skip to content

Commit ab4ba2e

Browse files
adam900710kdave
authored andcommitted
btrfs: tree-checker: Verify dev item
[BUG] For fuzzed image whose DEV_ITEM has invalid total_bytes as 0, then kernel will just panic: BUG: unable to handle kernel NULL pointer dereference at 0000000000000098 #PF error: [normal kernel read fault] PGD 800000022b2bd067 P4D 800000022b2bd067 PUD 22b2bc067 PMD 0 Oops: 0000 [#1] SMP PTI CPU: 0 PID: 1106 Comm: mount Not tainted 5.0.0-rc8+ #9 RIP: 0010:btrfs_verify_dev_extents+0x2a5/0x5a0 Call Trace: open_ctree+0x160d/0x2149 btrfs_mount_root+0x5b2/0x680 [CAUSE] If device extent verification finds a deivce with 0 total_bytes, then it assumes it's a seed dummy, then search for seed devices. But in this case, there is no seed device at all, causing NULL pointer. [FIX] Since this is caused by fuzzed image, let's go the tree-check way, just add a new verification for device item. Reported-by: Yoon Jungyeon <jungyeon@gatech.edu> Link: https://bugzilla.kernel.org/show_bug.cgi?id=202691 Reviewed-by: Nikolay Borisov <nborisov@suse.com> Signed-off-by: Qu Wenruo <wqu@suse.com> Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 075cb3c commit ab4ba2e

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

fs/btrfs/tree-checker.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,77 @@ int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
600600
return 0;
601601
}
602602

603+
__printf(4, 5)
604+
__cold
605+
static void dev_item_err(const struct btrfs_fs_info *fs_info,
606+
const struct extent_buffer *eb, int slot,
607+
const char *fmt, ...)
608+
{
609+
struct btrfs_key key;
610+
struct va_format vaf;
611+
va_list args;
612+
613+
btrfs_item_key_to_cpu(eb, &key, slot);
614+
va_start(args, fmt);
615+
616+
vaf.fmt = fmt;
617+
vaf.va = &args;
618+
619+
btrfs_crit(fs_info,
620+
"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
621+
btrfs_header_level(eb) == 0 ? "leaf" : "node",
622+
btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
623+
key.objectid, &vaf);
624+
va_end(args);
625+
}
626+
627+
static int check_dev_item(struct btrfs_fs_info *fs_info,
628+
struct extent_buffer *leaf,
629+
struct btrfs_key *key, int slot)
630+
{
631+
struct btrfs_dev_item *ditem;
632+
u64 max_devid = max(BTRFS_MAX_DEVS(fs_info), BTRFS_MAX_DEVS_SYS_CHUNK);
633+
634+
if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
635+
dev_item_err(fs_info, leaf, slot,
636+
"invalid objectid: has=%llu expect=%llu",
637+
key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
638+
return -EUCLEAN;
639+
}
640+
if (key->offset > max_devid) {
641+
dev_item_err(fs_info, leaf, slot,
642+
"invalid devid: has=%llu expect=[0, %llu]",
643+
key->offset, max_devid);
644+
return -EUCLEAN;
645+
}
646+
ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
647+
if (btrfs_device_id(leaf, ditem) != key->offset) {
648+
dev_item_err(fs_info, leaf, slot,
649+
"devid mismatch: key has=%llu item has=%llu",
650+
key->offset, btrfs_device_id(leaf, ditem));
651+
return -EUCLEAN;
652+
}
653+
654+
/*
655+
* For device total_bytes, we don't have reliable way to check it, as
656+
* it can be 0 for device removal. Device size check can only be done
657+
* by dev extents check.
658+
*/
659+
if (btrfs_device_bytes_used(leaf, ditem) >
660+
btrfs_device_total_bytes(leaf, ditem)) {
661+
dev_item_err(fs_info, leaf, slot,
662+
"invalid bytes used: have %llu expect [0, %llu]",
663+
btrfs_device_bytes_used(leaf, ditem),
664+
btrfs_device_total_bytes(leaf, ditem));
665+
return -EUCLEAN;
666+
}
667+
/*
668+
* Remaining members like io_align/type/gen/dev_group aren't really
669+
* utilized. Skip them to make later usage of them easier.
670+
*/
671+
return 0;
672+
}
673+
603674
/*
604675
* Common point to switch the item-specific validation.
605676
*/
@@ -630,6 +701,9 @@ static int check_leaf_item(struct btrfs_fs_info *fs_info,
630701
ret = btrfs_check_chunk_valid(fs_info, leaf, chunk,
631702
key->offset);
632703
break;
704+
case BTRFS_DEV_ITEM_KEY:
705+
ret = check_dev_item(fs_info, leaf, key, slot);
706+
break;
633707
}
634708
return ret;
635709
}

fs/btrfs/volumes.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,15 +4958,6 @@ static void check_raid56_incompat_flag(struct btrfs_fs_info *info, u64 type)
49584958
btrfs_set_fs_incompat(info, RAID56);
49594959
}
49604960

4961-
#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
4962-
- sizeof(struct btrfs_chunk)) \
4963-
/ sizeof(struct btrfs_stripe) + 1)
4964-
4965-
#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
4966-
- 2 * sizeof(struct btrfs_disk_key) \
4967-
- 2 * sizeof(struct btrfs_chunk)) \
4968-
/ sizeof(struct btrfs_stripe) + 1)
4969-
49704961
static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
49714962
u64 start, u64 type)
49724963
{

fs/btrfs/volumes.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ struct btrfs_fs_devices {
258258

259259
#define BTRFS_BIO_INLINE_CSUM_SIZE 64
260260

261+
#define BTRFS_MAX_DEVS(info) ((BTRFS_MAX_ITEM_SIZE(info) \
262+
- sizeof(struct btrfs_chunk)) \
263+
/ sizeof(struct btrfs_stripe) + 1)
264+
265+
#define BTRFS_MAX_DEVS_SYS_CHUNK ((BTRFS_SYSTEM_CHUNK_ARRAY_SIZE \
266+
- 2 * sizeof(struct btrfs_disk_key) \
267+
- 2 * sizeof(struct btrfs_chunk)) \
268+
/ sizeof(struct btrfs_stripe) + 1)
269+
261270
/*
262271
* we need the mirror number and stripe index to be passed around
263272
* the call chain while we are processing end_io (especially errors).

0 commit comments

Comments
 (0)