Skip to contentSkip to conversation

btrfs: tree-checker: reject file extents with oversized disk_num_bytes

PATCH In review Cheng Yongkang 2 participants 2 messages Nov 17 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 btrfs: tree-checker: reject file extents with oversized disk_num_bytes

check_extent_data_item() only checks that disk_num_bytes of a regular or preallocated file extent is sector aligned. A crafted image can have a compressed extent far larger than anything btrfs writes, and reading it goes wrong in different ways:

- With disk_num_bytes == 4GiB, btrfs_submit_compressed_read() truncates it to a zero compressed_len and submits an empty bio:

    assertion failed: IS_ALIGNED(logical, blocksize) &&
    IS_ALIGNED(length, blocksize) && length != 0, in fs/btrfs/bio.c:895

- With a 4MiB zlib extent whose num_bytes and ram_bytes are above 4GiB, bvec_offset + bv_len wraps in btrfs_decompress_buf2page() and memcpy() reads past the zlib workspace buffer:

    BUG: KASAN: slab-out-of-bounds in btrfs_decompress_buf2page+0x3e0/0x650

Compression and encoded writes never create a compressed extent larger than BTRFS_MAX_COMPRESSED on disk or BTRFS_MAX_UNCOMPRESSED in memory, and its offset + num_bytes always fits in ram_bytes. Uncompressed extents can be larger than 256MiB, for example preallocated extents from kernels before v3.9 or extents from direct I/O writes, but they should never get close to U32_MAX.

Reject a file extent whose disk_num_bytes is above BTRFS_MAX_COMPRESSED if it is compressed, or above U32_MAX otherwise. For an extent with data on disk, also reject:

- a zero disk_num_bytes, which makes btrfs_submit_compressed_read() submit an empty bio the same way;
- a ram_bytes above BTRFS_MAX_UNCOMPRESSED if it is compressed, or above U32_MAX otherwise;
- an offset + num_bytes beyond ram_bytes if it is compressed, as the compressed read path takes the decompressed range from offset and num_bytes.

Holes are skipped, since without NO_HOLES a hole extent has ram_bytes set to the size of the hole.

---
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 | 55 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
fs/btrfs/tree-checker.c +55 -0
index f021ac14babf..e7e0bb6be76f 100644
@@ -244,7 +244,10 @@ 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 ram_end;
 	u8 fi_type;
 
 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
@@ -377,6 +380,58 @@ static int check_extent_data_item(struct extent_buffer *leaf,
 		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
 		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) > max_disk_num_bytes)) {
+		file_extent_err(leaf, slot,
+		"invalid disk_num_bytes for file extent, have %llu expect <= %u",
+				btrfs_file_extent_disk_num_bytes(leaf, fi),
+				max_disk_num_bytes);
+		return -EUCLEAN;
+	}
+
+	/*
+	 * The remaining checks only apply to extents with data on disk. Holes
+	 * are skipped, as without NO_HOLES their ram_bytes is the hole size.
+	 */
+	if (btrfs_file_extent_disk_bytenr(leaf, fi)) {
+		if (unlikely(btrfs_file_extent_disk_num_bytes(leaf, fi) == 0)) {
+			file_extent_err(leaf, slot,
+		"invalid zero disk_num_bytes for file extent with disk_bytenr %llu",
+					btrfs_file_extent_disk_bytenr(leaf, fi));
+			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;
+		}
+		/* Reads of compressed extents use offset and num_bytes in ram_bytes. */
+		if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE &&
+		    unlikely(check_add_overflow(btrfs_file_extent_offset(leaf, fi),
+						btrfs_file_extent_num_bytes(leaf, fi),
+						&ram_end) ||
+			     ram_end > btrfs_file_extent_ram_bytes(leaf, fi))) {
+			file_extent_err(leaf, slot,
+	"invalid offset + num_bytes for compressed file extent, have %llu + %llu expect <= %llu",
+					btrfs_file_extent_offset(leaf, fi),
+					btrfs_file_extent_num_bytes(leaf, fi),
+					btrfs_file_extent_ram_bytes(leaf, fi));
+			return -EUCLEAN;
+		}
+	}
+
 	/* Catch extent end overflow */
 	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
 					key->offset, &extent_end))) {

base-commit: d45cbbab23d3ca231771e4bff996a6a8118162cf

Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Cheng Yongkang

check_extent_data_item() only checks that disk_num_bytes of a regular or preallocated file extent is sector aligned. A crafted image can have a compressed extent far larger than anything btrfs writes, and reading it goes wrong in different ways:

- With disk_num_bytes == 4GiB, btrfs_submit_compressed_read() truncates it to a zero compressed_len and submits an empty bio:

Quoted 8 lines
     assertion failed: IS_ALIGNED(logical, blocksize) &&
     IS_ALIGNED(length, blocksize) && length != 0, in fs/btrfs/bio.c:895

- With a 4MiB zlib extent whose num_bytes and ram_bytes are above 4GiB, bvec_offset + bv_len wraps in btrfs_decompress_buf2page() and memcpy() reads past the zlib workspace buffer:

     BUG: KASAN: slab-out-of-bounds in btrfs_decompress_buf2page+0x3e0/0x650

With all those enhance checks, I do not think the two reports add any value now.

The patch is really hardening the tree-checker, and that's all what we need to know.

Quoted 85 lines

Compression and encoded writes never create a compressed extent larger than BTRFS_MAX_COMPRESSED on disk or BTRFS_MAX_UNCOMPRESSED in memory, and its offset + num_bytes always fits in ram_bytes. Uncompressed extents can be larger than 256MiB, for example preallocated extents from kernels before v3.9 or extents from direct I/O writes, but they should never get close to U32_MAX.

Reject a file extent whose disk_num_bytes is above BTRFS_MAX_COMPRESSED if it is compressed, or above U32_MAX otherwise. For an extent with data on disk, also reject:

- a zero disk_num_bytes, which makes btrfs_submit_compressed_read() submit an empty bio the same way;
- a ram_bytes above BTRFS_MAX_UNCOMPRESSED if it is compressed, or above U32_MAX otherwise;
- an offset + num_bytes beyond ram_bytes if it is compressed, as the compressed read path takes the decompressed range from offset and num_bytes.

Holes are skipped, since without NO_HOLES a hole extent has ram_bytes set to the size of the hole.

---
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 | 55 +++++++++++++++++++++++++++++++++++++++++
  1 file changed, 55 insertions(+)
fs/btrfs/tree-checker.c +17 -0
index f021ac14babf..e7e0bb6be76f 100644
@@ -244,7 +244,10 @@ 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 ram_end;
  	u8 fi_type;
 
  	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
@@ -377,6 +380,58 @@ static int check_extent_data_item(struct extent_buffer *leaf,
  		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
  		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) > max_disk_num_bytes)) {
+		file_extent_err(leaf, slot,
+		"invalid disk_num_bytes for file extent, have %llu expect <= %u",

fs/btrfs/tree-checker.c static int check_extent_data_item(struct extent_buffer *leaf, · line 406

Quoted 10 lines
fs/btrfs/tree-checker.c +10 -0
+				btrfs_file_extent_disk_num_bytes(leaf, fi),
+				max_disk_num_bytes);
+		return -EUCLEAN;
+	}
+
+	/*
+	 * The remaining checks only apply to extents with data on disk. Holes
+	 * are skipped, as without NO_HOLES their ram_bytes is the hole size.
+	 */
+	if (btrfs_file_extent_disk_bytenr(leaf, fi)) {

Sashiko mentioned a situation where a dedicated hole can by-pass all those checks:

https://sashiko.dev/#/patchset/20261117064613.771336-1-teel4res%40gmail.com

I think a dedicated explicit hole check would be safer, rejecting obvious incorrect things like an explicit hole but with compression set.

Quoted 9 lines
fs/btrfs/tree-checker.c +9 -0
+		if (unlikely(btrfs_file_extent_disk_num_bytes(leaf, fi) == 0)) {
+			file_extent_err(leaf, slot,
+		"invalid zero disk_num_bytes for file extent with disk_bytenr %llu",
+					btrfs_file_extent_disk_bytenr(leaf, fi));
+			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",

fs/btrfs/tree-checker.c static int check_extent_data_item(struct extent_buffer *leaf, · line 425

Quoted 10 lines
fs/btrfs/tree-checker.c +10 -0
+					btrfs_file_extent_ram_bytes(leaf, fi),
+					max_ram_bytes);
+			return -EUCLEAN;
+		}
+		/* Reads of compressed extents use offset and num_bytes in ram_bytes. */
+		if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE &&
+		    unlikely(check_add_overflow(btrfs_file_extent_offset(leaf, fi),
+						btrfs_file_extent_num_bytes(leaf, fi),
+						&ram_end) ||
+			     ram_end > btrfs_file_extent_ram_bytes(leaf, fi))) {

This overflow check should apply to both compressed and uncompressed extents. This is not limited to compressed extents AFAIK.

Quoted 14 lines
fs/btrfs/tree-checker.c +9 -0
+			file_extent_err(leaf, slot,
+	"invalid offset + num_bytes for compressed file extent, have %llu + %llu expect <= %llu",
+					btrfs_file_extent_offset(leaf, fi),
+					btrfs_file_extent_num_bytes(leaf, fi),
+					btrfs_file_extent_ram_bytes(leaf, fi));
+			return -EUCLEAN;
+		}
+	}
+
  	/* Catch extent end overflow */
  	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
  					key->offset, &extent_end))) {

base-commit: d45cbbab23d3ca231771e4bff996a6a8118162cf