Skip to contentSkip to conversation

btrfs: tree-checker: add more checks for file extent items

PATCH New Cheng Yongkang 1 participant 1 message Nov 25 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 btrfs: tree-checker: add more checks for file extent items

check_extent_data_item() only checks the alignment of most members of a regular or preallocated file extent. Add the following checks, based on what btrfs writes:

- An explicit hole (disk_bytenr == 0) must be an uncompressed regular extent with zero disk_num_bytes. Its offset is not checked, as btrfs_drop_extents() can trim the front of a hole extent.

- A preallocated extent must not be compressed.

- An extent with data on disk must have a non-zero disk_num_bytes. If it is compressed, disk_num_bytes must not exceed BTRFS_MAX_COMPRESSED and ram_bytes must not exceed BTRFS_MAX_UNCOMPRESSED, as compression and encoded writes never create larger ones. Otherwise both must not exceed U32_MAX. Uncompressed extents can be larger than 256MiB, for example preallocated extents from kernels before v3.9, but they should never get close to U32_MAX.

- offset + num_bytes of an extent with data on disk must not exceed its ram_bytes if it is compressed, or its disk_num_bytes otherwise, as btrfs_extent_item_to_extent_map() uses disk_num_bytes as the ram_bytes of uncompressed extents.

---
Note that before v3.9, __btrfs_prealloc_file_range() reserved the whole fallocate range at once, so on a RAID0 or RAID10 filesystem with five or more data stripes a preallocated extent can be up to the 10G data chunk limit, above U32_MAX. Such a filesystem would fail the new check. If it should still be accepted, BTRFS_MAX_DATA_CHUNK_SIZE could be used as the uncompressed limit instead, as a data extent never crosses a chunk.

Changes in v6:
- Check offset + num_bytes of uncompressed extents against disk_num_bytes instead of ram_bytes, as pointed out by Sashiko on v5. btrfs_extent_item_to_extent_map() ignores the ram_bytes of uncompressed extents, so disk_num_bytes is what reads are bound by. This also keeps accepting uncompressed extents from older kernels with ram_bytes smaller than disk_num_bytes.

Changes in v5:
- Drop the crash reports from the commit message, the patch is a tree-checker hardening.
- Add a dedicated check for explicit holes: they must be uncompressed regular extents with zero disk_num_bytes, as Sashiko pointed out on v4 that a hole bypassed all the new checks.
- Reject compressed preallocated extents, also pointed out by Sashiko.
- Check offset + num_bytes against ram_bytes for uncompressed extents too.
- Only use preallocated extents from kernels before v3.9 as the example of uncompressed extents larger than 256MiB. A direct I/O write reserves its whole range, but each bio gets its own ordered extent, so its file extents are only as large as its bios.
- Change the subject, as the patch no longer only checks disk_num_bytes.

Changes in v4:
- Also check ram_bytes of uncompressed extents against U32_MAX, with max_ram_bytes picked the same way as max_disk_num_bytes.
- Reject a zero disk_num_bytes for extents with data on disk, and an offset + num_bytes beyond ram_bytes for compressed extents, as Sashiko pointed out on v2 and v3.
- Skip holes for the new checks, as their ram_bytes is the hole size without NO_HOLES.
- Correct the commit message: direct I/O writes can also create uncompressed extents larger than 256MiB.
- Rebase onto misc-next.

Changes in v3:
- Relax the uncompressed disk_num_bytes limit from 256MiB to U32_MAX, as preallocated extents created before v3.9 can be larger.
- Also check ram_bytes of compressed extents against BTRFS_MAX_UNCOMPRESSED.

Changes in v2:
- Use SZ_256M directly instead of adding a new macro, and leave inode.c alone.
- Shorten the comment.

 fs/btrfs/tree-checker.c | 72 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 72 insertions(+)
fs/btrfs/tree-checker.c +72 -0
index f021ac14babf..1a8e5b997493 100644
@@ -244,7 +244,11 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 	struct btrfs_file_extent_item *fi;
 	u32 sectorsize = fs_info->sectorsize;
 	u32 item_size = btrfs_item_size(leaf, slot);
+	u32 max_disk_num_bytes;
+	u32 max_ram_bytes;
 	u64 extent_end;
+	u64 data_end;
+	u64 data_len;
 	u8 fi_type;
 
 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
@@ -377,6 +381,74 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
 		return -EUCLEAN;
 
+	if (!btrfs_file_extent_disk_bytenr(leaf, fi)) {
+		if (unlikely(fi_type != BTRFS_FILE_EXTENT_REG ||
+			     btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE ||
+			     btrfs_file_extent_disk_num_bytes(leaf, fi))) {
+			file_extent_err(leaf, slot,
+"invalid hole file extent, have type %u compression %u disk_num_bytes %llu expect type %u compression 0 disk_num_bytes 0",
+					fi_type, btrfs_file_extent_compression(leaf, fi),
+					btrfs_file_extent_disk_num_bytes(leaf, fi),
+					BTRFS_FILE_EXTENT_REG);
+			return -EUCLEAN;
+		}
+	} else {
+		if (unlikely(fi_type == BTRFS_FILE_EXTENT_PREALLOC &&
+			     btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE)) {
+			file_extent_err(leaf, slot,
+		"invalid compression for preallocated file extent, have %u expect 0",
+					btrfs_file_extent_compression(leaf, fi));
+			return -EUCLEAN;
+		}
+
+		/*
+		 * Compressed extents are at most 128K both on disk and
+		 * uncompressed. Uncompressed extents should never get close to
+		 * U32_MAX.
+		 */
+		if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
+			max_disk_num_bytes = BTRFS_MAX_COMPRESSED;
+			max_ram_bytes = BTRFS_MAX_UNCOMPRESSED;
+		} else {
+			max_disk_num_bytes = U32_MAX;
+			max_ram_bytes = U32_MAX;
+		}
+		if (unlikely(btrfs_file_extent_disk_num_bytes(leaf, fi) == 0 ||
+			     btrfs_file_extent_disk_num_bytes(leaf, fi) > max_disk_num_bytes)) {
+			file_extent_err(leaf, slot,
+		"invalid disk_num_bytes for file extent, have %llu expect (0, %u]",
+					btrfs_file_extent_disk_num_bytes(leaf, fi),
+					max_disk_num_bytes);
+			return -EUCLEAN;
+		}
+		if (unlikely(btrfs_file_extent_ram_bytes(leaf, fi) > max_ram_bytes)) {
+			file_extent_err(leaf, slot,
+		"invalid ram_bytes for file extent, have %llu expect <= %u",
+					btrfs_file_extent_ram_bytes(leaf, fi),
+					max_ram_bytes);
+			return -EUCLEAN;
+		}
+		/*
+		 * btrfs_extent_item_to_extent_map() uses disk_num_bytes as the
+		 * ram_bytes of uncompressed extents.
+		 */
+		if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE)
+			data_len = btrfs_file_extent_ram_bytes(leaf, fi);
+		else
+			data_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
+		if (unlikely(check_add_overflow(btrfs_file_extent_offset(leaf, fi),
+						btrfs_file_extent_num_bytes(leaf, fi),
+						&data_end) ||
+			     data_end > data_len)) {
+			file_extent_err(leaf, slot,
+	"invalid offset + num_bytes for file extent, have %llu + %llu expect <= %llu",
+					btrfs_file_extent_offset(leaf, fi),
+					btrfs_file_extent_num_bytes(leaf, fi),
+					data_len);
+			return -EUCLEAN;
+		}
+	}
+
 	/* Catch extent end overflow */
 	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
 					key->offset, &extent_end))) {

base-commit: 10b0e58070dc350f3e47a87bb4a584050f32ef18

Signature · 1 line
2.34.1