* [PATCH v3] btrfs: tree-checker: reject file extents with oversized disk_num_bytes
@ 2026-11-17 2:38 Cheng Yongkang
2026-11-17 4:31 ` Qu Wenruo
0 siblings, 1 reply; 4+ messages in thread
From: Cheng Yongkang @ 2026-11-17 2:38 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 on disk or BTRFS_MAX_UNCOMPRESSED in memory.
Uncompressed extents are limited to 256MiB since v3.9, but older kernels
could create larger preallocated extents.
Reject a compressed file extent whose ram_bytes is above
BTRFS_MAX_UNCOMPRESSED or whose disk_num_bytes is above
BTRFS_MAX_COMPRESSED. For an uncompressed extent, only reject a
disk_num_bytes above U32_MAX, which should never be reached.
Suggested-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
---
Changes in v3:
- Relax the uncompressed limit from 256MiB to U32_MAX, so preallocated
extents created before v3.9 are still accepted.
- Also check ram_bytes of compressed extents against
BTRFS_MAX_UNCOMPRESSED.
v2: https://lore.kernel.org/linux-btrfs/20261116050922.310476-1-teel4res@gmail.com/
v1: https://lore.kernel.org/linux-btrfs/20261116044543.282187-1-teel4res@gmail.com/
fs/btrfs/tree-checker.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index f021ac14babf..e5d17bf4051b 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,30 @@ 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) {
+ if (unlikely(btrfs_file_extent_ram_bytes(leaf, fi) > BTRFS_MAX_UNCOMPRESSED)) {
+ file_extent_err(leaf, slot,
+ "invalid ram_bytes for compressed file extent, have %llu expect <= %u",
+ btrfs_file_extent_ram_bytes(leaf, fi),
+ BTRFS_MAX_UNCOMPRESSED);
+ return -EUCLEAN;
+ }
+ max_disk_num_bytes = BTRFS_MAX_COMPRESSED;
+ } else {
+ max_disk_num_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;
+ }
+
/* 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 related [flat|nested] 4+ messages in thread
* Re: [PATCH v3] btrfs: tree-checker: reject file extents with oversized disk_num_bytes
2026-11-17 2:38 [PATCH v3] btrfs: tree-checker: reject file extents with oversized disk_num_bytes Cheng Yongkang
@ 2026-11-17 4:31 ` Qu Wenruo
2026-11-17 4:57 ` Cheng Yongkang
0 siblings, 1 reply; 4+ messages in thread
From: Qu Wenruo @ 2026-11-17 4:31 UTC (permalink / raw)
To: Cheng Yongkang, David Sterba; +Cc: Chris Mason, linux-btrfs, linux-kernel
在 2026/11/17 13:08, 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:
>
> 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.
> Uncompressed extents are limited to 256MiB since v3.9, but older kernels
> could create larger preallocated extents.
>
> Reject a compressed file extent whose ram_bytes is above
> BTRFS_MAX_UNCOMPRESSED or whose disk_num_bytes is above
> BTRFS_MAX_COMPRESSED. For an uncompressed extent, only reject a
> disk_num_bytes above U32_MAX, which should never be reached.
>
> Suggested-by: Qu Wenruo <wqu@suse.com>
> Signed-off-by: Cheng Yongkang <teel4res@gmail.com>
> ---
> Changes in v3:
> - Relax the uncompressed limit from 256MiB to U32_MAX, so preallocated
> extents created before v3.9 are still accepted.
> - Also check ram_bytes of compressed extents against
> BTRFS_MAX_UNCOMPRESSED.
>
> v2: https://lore.kernel.org/linux-btrfs/20261116050922.310476-1-teel4res@gmail.com/
> v1: https://lore.kernel.org/linux-btrfs/20261116044543.282187-1-teel4res@gmail.com/
I don't know which agent you're using, but I have seen several ones (not
only from you) doing this linking-to-older-versions behavior.
It doesn't make much sense.
Let your agent to learn from patches of known developers of each
involved subsystem, other than follow whatever weird built-in prompts.
And the full changelog would make more sense, other than letting the
reviewer to jump to your previous version for history.
E.g:
https://lore.kernel.org/linux-btrfs/036f030792d3b497db41f1e7e376e085e094d006.1794588939.git.fdmanana@suse.com/
>
> fs/btrfs/tree-checker.c | 25 +++++++++++++++++++++++++
> 1 file changed, 25 insertions(+)
>
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index f021ac14babf..e5d17bf4051b 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,30 @@ 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) {
> + if (unlikely(btrfs_file_extent_ram_bytes(leaf, fi) > BTRFS_MAX_UNCOMPRESSED)) {
I think the ram bytes check should apply to both compressed and
uncompressed extents.
For compressed, it's MAX_UNCOMPRESSED, and for uncompressed it's U32_MAX.
> + file_extent_err(leaf, slot,
> + "invalid ram_bytes for compressed file extent, have %llu expect <= %u",
> + btrfs_file_extent_ram_bytes(leaf, fi),
> + BTRFS_MAX_UNCOMPRESSED);
> + return -EUCLEAN;
> + }
> + max_disk_num_bytes = BTRFS_MAX_COMPRESSED;
> + } else {
> + max_disk_num_bytes = U32_MAX;
> + }
> + if (unlikely(btrfs_file_extent_disk_num_bytes(leaf, fi) > max_disk_num_bytes)) {
Just like what you did for 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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] btrfs: tree-checker: reject file extents with oversized disk_num_bytes
2026-11-17 4:31 ` Qu Wenruo
@ 2026-11-17 4:57 ` Cheng Yongkang
2026-11-17 5:38 ` Qu Wenruo
0 siblings, 1 reply; 4+ messages in thread
From: Cheng Yongkang @ 2026-11-17 4:57 UTC (permalink / raw)
To: Qu Wenruo; +Cc: David Sterba, Chris Mason, linux-btrfs, linux-kernel
On 2026/11/17 15:01, Qu Wenruo wrote:
> I don't know which agent you're using, but I have seen several ones (not
> only from you) doing this linking-to-older-versions behavior.
>
> It doesn't make much sense.
[...]
> And the full changelog would make more sense, other than letting the
> reviewer to jump to your previous version for history.
The links came from Documentation/process/submitting-patches.rst, which
recommends adding lore links to previous versions after the "---" line.
Its example also lists the changes of every version, though, and I only
listed the v3 changes. That was my mistake.
I'll follow the btrfs style from now on: the full changelog of all
versions, without links to the older ones.
> I think the ram bytes check should apply to both compressed and
> uncompressed extents.
>
> For compressed, it's MAX_UNCOMPRESSED, and for uncompressed it's U32_MAX.
Will do in v4, with max_ram_bytes picked the same way as
max_disk_num_bytes.
Thanks,
Cheng Yongkang
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v3] btrfs: tree-checker: reject file extents with oversized disk_num_bytes
2026-11-17 4:57 ` Cheng Yongkang
@ 2026-11-17 5:38 ` Qu Wenruo
0 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-11-17 5:38 UTC (permalink / raw)
To: Cheng Yongkang; +Cc: David Sterba, Chris Mason, linux-btrfs, linux-kernel
在 2026/11/17 15:27, Cheng Yongkang 写道:
> On 2026/11/17 15:01, Qu Wenruo wrote:
>> I don't know which agent you're using, but I have seen several ones (not
>> only from you) doing this linking-to-older-versions behavior.
>>
>> It doesn't make much sense.
> [...]
>> And the full changelog would make more sense, other than letting the
>> reviewer to jump to your previous version for history.
>
> The links came from Documentation/process/submitting-patches.rst, which
> recommends adding lore links to previous versions after the "---" line.
> Its example also lists the changes of every version, though, and I only
> listed the v3 changes. That was my mistake.
>
> I'll follow the btrfs style from now on: the full changelog of all
> versions, without links to the older ones.
>
>> I think the ram bytes check should apply to both compressed and
>> uncompressed extents.
>>
>> For compressed, it's MAX_UNCOMPRESSED, and for uncompressed it's U32_MAX.
>
> Will do in v4, with max_ram_bytes picked the same way as
> max_disk_num_bytes.
And Sashiko has some reviews too:
https://sashiko.dev/#/patchset/20261117023841.467366-1-teel4res%40gmail.com
>
> Thanks,
> Cheng Yongkang
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-17 5:38 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 2:38 [PATCH v3] btrfs: tree-checker: reject file extents with oversized disk_num_bytes Cheng Yongkang
2026-11-17 4:31 ` Qu Wenruo
2026-11-17 4:57 ` Cheng Yongkang
2026-11-17 5:38 ` Qu Wenruo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox