btrfs: tree-checker: reject file extents with oversized disk_num_bytes
PATCH v2 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. An uncompressed extent is at most BTRFS_MAX_EXTENT_SIZE for a regular write and 256MiB for preallocation.
Reject a file extent whose disk_num_bytes is above BTRFS_MAX_COMPRESSED if it is compressed, or above 256MiB otherwise.
---
Changes in v2:
- Use SZ_256M directly instead of adding a new macro, and leave inode.c alone.
- Shorten the comment.
Note that kernels before v3.9, without 24542bf7ea5e ("btrfs: limit fallocate extent reservation to 256MB"), could create preallocated extents larger than 256MiB. If such old extents should still be accepted, the uncompressed limit needs to be looser.
v1: https://lore.kernel.org/linux-btrfs/[email protected]/
fs/btrfs/tree-checker.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
fs/btrfs/tree-checker.c +14 -0
@@ -244,6 +244,7 @@ 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;u64 extent_end;u8 fi_type;
@@ -377,6 +378,19 @@ 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 on disk, preallocated ones 256M. */ +if (btrfs_file_extent_compression (leaf, fi) !=BTRFS_COMPRESS_NONE ) + max_disk_num_bytes =BTRFS_MAX_COMPRESSED ; +else + max_disk_num_bytes =SZ_256M ; +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 ; + } +/* Catch extent end overflow */ if (unlikely (check_add_overflow (btrfs_file_extent_num_bytes (leaf, fi), key->offset, &extent_end))) {
base-commit: 3aefd67f08f8ed59ed0fa90619b25856df415031
Signature · 1 line
2.34.1
Quoted 32 lines · Cheng Yongkang
check_extent_data_item()only checks thatdisk_num_bytesof 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 zerocompressed_lenand 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_bytesandram_bytesare above 4GiB,bvec_offset+bv_lenwraps inbtrfs_decompress_buf2page()andmemcpy()reads past the zlib workspace buffer:BUG: KASAN: slab-out-of-bounds in btrfs_decompress_buf2page+0x3e0/0x650Compression and encoded writes never create a compressed extent larger than
BTRFS_MAX_COMPRESSED. An uncompressed extent is at mostBTRFS_MAX_EXTENT_SIZEfor a regular write and 256MiB for preallocation.Reject a file extent whose
disk_num_bytesis aboveBTRFS_MAX_COMPRESSEDif it is compressed, or above 256MiB otherwise.Suggested-by Qu Wenruo <[email protected]>Signed-off-by Cheng Yongkang <[email protected]>---
Changes in v2:
- UseSZ_256Mdirectly instead of adding a new macro, and leaveinode.calone.
- Shorten the comment.
Quoted 4 lines
Note that kernels before v3.9, without
24542bf7ea5e("btrfs: limit fallocate extent reservation to 256MB"), could create preallocated extents larger than 256MiB. If such old extents should still be accepted, the uncompressed limit needs to be looser.
This is indeed a problem. I think you can limit the disk_num_bytes check for uncompressed extents to U32_MAX, just to be extra safe. The U32_MAX limit should never be reached, but we can still catch obviously too large members.
On the other hand, you can add extra checks against the ram bytes too for compressed extents. That ram bytes should not exceed the same limits.
Quoted 40 lines
v1: https://lore.kernel.org/linux-btrfs/[email protected]/
fs/btrfs/tree-checker.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)fs/btrfs/tree-checker.c +14
-0@@ -244,6 +244,7 @@ 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;u64 extent_end;u8 fi_type;@@ -377,6 +378,19 @@ 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 on disk, preallocated ones 256M. */ +if (btrfs_file_extent_compression (leaf, fi) !=BTRFS_COMPRESS_NONE ) + max_disk_num_bytes =BTRFS_MAX_COMPRESSED ; +else + max_disk_num_bytes =SZ_256M ; +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 ; + } +/* Catch extent end overflow */ if (unlikely (check_add_overflow (btrfs_file_extent_num_bytes (leaf, fi), key->offset, &extent_end))) {base-commit:
3aefd67f08f8ed59ed0fa90619b25856df415031