Skip to contentSkip to conversation

btrfs: tree-checker: Check item size before reading file extent type

PATCH In review Qu Wenruo 2 participants 2 messages Sep 2, 2019 – Oct 24, 2019 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

PATCH 1/1 btrfs: tree-checker: Check item size before reading file extent type

In check_extent_data_item(), we read file extent type without verifying if the item size is valid.

Add such check to ensure the file extent type we read is correct.

The check is not as accurate as we need to cover both inline and regular extents, so it only checks if the item size is larger or equal to inline header.
So the existing size checks on inline/regular extents are still needed.

---
 fs/btrfs/tree-checker.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
fs/btrfs/tree-checker.c +11 -0
index 15d1aa7cef1f..22e6474f9d4e 100644
@@ -143,6 +143,17 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 
 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
 
+	/*
+	 * Make sure the item contains at least inline header, so the file
+	 * extent type is not some garbage.
+	 */
+	if (item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START) {
+		file_extent_err(leaf, slot,
+		"invalid item size, have %u expect [%lu, %u)",
+				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
+				SZ_4K);
+		return -EUCLEAN;
+	}
 	if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
 		file_extent_err(leaf, slot,
 		"invalid type for file extent, have %u expect range [0, %u]",
Signature · 1 line
2.23.0
#1 lore raw
Quoted 11 lines · Qu Wenruo

In check_extent_data_item(), we read file extent type without verifying if the item size is valid.

Add such check to ensure the file extent type we read is correct.

The check is not as accurate as we need to cover both inline and regular extents, so it only checks if the item size is larger or equal to inline header.
So the existing size checks on inline/regular extents are still needed.

I lost track of this patch, now added to for-next. Thanks.