* [PATCH] btrfs: tree-checker: reject file extents with oversized disk_num_bytes
@ 2026-11-16 4:45 Cheng Yongkang
2026-11-16 4:48 ` Cheng Yongkang
0 siblings, 1 reply; 2+ messages in thread
From: Cheng Yongkang @ 2026-11-16 4:45 UTC (permalink / raw)
To: David Sterba; +Cc: Chris Mason, Qu Wenruo, linux-btrfs, linux-kernel
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.
Add BTRFS_MAX_PREALLOC_EXTENT_SIZE for the preallocation limit, and make
the tree checker reject a file extent whose disk_num_bytes is above
BTRFS_MAX_COMPRESSED if it is compressed, or above
BTRFS_MAX_PREALLOC_EXTENT_SIZE otherwise.
Suggested-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
Report: https://lore.kernel.org/linux-btrfs/20261115163724.226922-1-teel4res@gmail.com/
On v7.3 both images from the report are rejected by the tree checker
with this patch, and 256MiB preallocated extents still pass.
fs/btrfs/fs.h | 3 +++
fs/btrfs/inode.c | 2 +-
fs/btrfs/tree-checker.c | 18 ++++++++++++++++++
3 files changed, 22 insertions(+), 1 deletion(-)
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index 2dd167c32d61..c65051a9f018 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -72,6 +72,9 @@ static_assert(BTRFS_MAX_FOLIO_SIZE > PAGE_SIZE);
#define BTRFS_MAX_EXTENT_SIZE SZ_128M
+/* The maximum size of one extent created by btrfs_prealloc_file_range(). */
+#define BTRFS_MAX_PREALLOC_EXTENT_SIZE SZ_256M
+
/*
* Maximum length to trim in a single iteration to avoid holding device list
* mutex for too long.
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 19b1eba2dd84..3f8889378d6a 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -9175,7 +9175,7 @@ int btrfs_prealloc_file_range(struct inode *inode, int mode,
u64 end = start + num_bytes - 1;
while (num_bytes > 0) {
- cur_bytes = min_t(u64, num_bytes, SZ_256M);
+ cur_bytes = min_t(u64, num_bytes, BTRFS_MAX_PREALLOC_EXTENT_SIZE);
cur_bytes = max(cur_bytes, min_size);
/*
* If we are severely fragmented we could end up with really
diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index f021ac14babf..6e57f5969bf9 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -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,23 @@ static int check_extent_data_item(struct extent_buffer *leaf,
CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
return -EUCLEAN;
+ /*
+ * A compressed extent is never larger than BTRFS_MAX_COMPRESSED on
+ * disk. An uncompressed one is at most BTRFS_MAX_EXTENT_SIZE for a
+ * regular write and BTRFS_MAX_PREALLOC_EXTENT_SIZE for preallocation.
+ */
+ if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE)
+ max_disk_num_bytes = BTRFS_MAX_COMPRESSED;
+ else
+ max_disk_num_bytes = BTRFS_MAX_PREALLOC_EXTENT_SIZE;
+ 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
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] btrfs: tree-checker: reject file extents with oversized disk_num_bytes
2026-11-16 4:45 [PATCH] btrfs: tree-checker: reject file extents with oversized disk_num_bytes Cheng Yongkang
@ 2026-11-16 4:48 ` Cheng Yongkang
0 siblings, 0 replies; 2+ messages in thread
From: Cheng Yongkang @ 2026-11-16 4:48 UTC (permalink / raw)
To: David Sterba; +Cc: Chris Mason, Qu Wenruo, linux-btrfs, linux-kernel
Please drop this patch, it has a problem. Sorry for the noise.
Thanks,
Cheng Yongkang
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-16 4:48 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-16 4:45 [PATCH] btrfs: tree-checker: reject file extents with oversized disk_num_bytes Cheng Yongkang
2026-11-16 4:48 ` Cheng Yongkang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®