[PATCH v2] btrfs: lzo: reject compressed segment that overflows the compressed input

Weiming Shi posted 1 patch 21 hours ago
fs/btrfs/lzo.c | 11 +++++++++++
1 file changed, 11 insertions(+)
[PATCH v2] btrfs: lzo: reject compressed segment that overflows the compressed input
Posted by Weiming Shi 21 hours ago
lzo_decompress_bio() validates each on-disk segment length seg_len only
against the workspace cbuf size, not against the compressed input size
(compressed_len, the total folio bytes of the bio).  A crafted extent can
carry a segment whose seg_len passes the cbuf check but runs past the end
of the bio, so copy_compressed_segment() walks off the last folio:
get_current_folio() then returns the NULL folio from bio_next_folio(), and
with CONFIG_BTRFS_ASSERT disabled (default) folio_size(NULL) faults.

 BUG: KASAN: null-ptr-deref in lzo_decompress_bio (fs/btrfs/lzo.c:383)
 Read of size 8 at addr 0000000000000000 by task kworker/u8:1/29
 Workqueue: btrfs-endio simple_end_io_work
  kasan_report (mm/kasan/report.c:590)
  lzo_decompress_bio (fs/btrfs/lzo.c:383)
  end_bbio_compressed_read (fs/btrfs/compression.c:1065)
  btrfs_bio_end_io (fs/btrfs/bio.c:135)
  btrfs_check_read_bio (fs/btrfs/bio.c:180 fs/btrfs/bio.c:285)
  simple_end_io_work
  process_one_work
  worker_thread

Reject any segment whose payload would extend beyond compressed_len before
copying it, treating it as corruption like the other on-disk validation
failures in this function.

Fixes: a6e66e6f8c1b ("btrfs: rework lzo_decompress_bio() to make it subpage compatible")
Reported-by: Xiang Mei <xmei5@asu.edu>
Assisted-by: Claude:claude-opus-4-8
Signed-off-by: Weiming Shi <bestswngs@gmail.com>
---
v2:
  - Return -EUCLEAN instead of -EIO to match lzo_decompress() (Qu Wenruo).
  - Emit a btrfs_err() message when rejecting the segment (Qu Wenruo).

 fs/btrfs/lzo.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/fs/btrfs/lzo.c b/fs/btrfs/lzo.c
index 2de18c7b5..6e4aa2285 100644
--- a/fs/btrfs/lzo.c
+++ b/fs/btrfs/lzo.c
@@ -491,6 +491,17 @@ int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
 			return -EIO;
 		}
 
+		/* The segment must not extend beyond the compressed input. */
+		if (unlikely(cur_in + seg_len > compressed_len)) {
+			struct btrfs_inode *inode = cb->bbio.inode;
+
+			btrfs_err(fs_info,
+			"lzo segment overflows compressed input, root %llu inode %llu offset %llu cur_in %u len %u compressed len %u",
+				  btrfs_root_id(inode->root), btrfs_ino(inode),
+				  cb->start, cur_in, seg_len, compressed_len);
+			return -EUCLEAN;
+		}
+
 		/* Copy the compressed segment payload into workspace */
 		copy_compressed_segment(cb, &fi, &cur_folio_index, workspace->cbuf,
 					seg_len, &cur_in);
-- 
2.43.0
Re: [PATCH v2] btrfs: lzo: reject compressed segment that overflows the compressed input
Posted by Qu Wenruo 21 hours ago

在 2026/6/7 14:55, Weiming Shi 写道:
> lzo_decompress_bio() validates each on-disk segment length seg_len only
> against the workspace cbuf size, not against the compressed input size
> (compressed_len, the total folio bytes of the bio).  A crafted extent can
> carry a segment whose seg_len passes the cbuf check but runs past the end
> of the bio, so copy_compressed_segment() walks off the last folio:
> get_current_folio() then returns the NULL folio from bio_next_folio(), and
> with CONFIG_BTRFS_ASSERT disabled (default) folio_size(NULL) faults.
> 
>   BUG: KASAN: null-ptr-deref in lzo_decompress_bio (fs/btrfs/lzo.c:383)
>   Read of size 8 at addr 0000000000000000 by task kworker/u8:1/29
>   Workqueue: btrfs-endio simple_end_io_work
>    kasan_report (mm/kasan/report.c:590)
>    lzo_decompress_bio (fs/btrfs/lzo.c:383)
>    end_bbio_compressed_read (fs/btrfs/compression.c:1065)
>    btrfs_bio_end_io (fs/btrfs/bio.c:135)
>    btrfs_check_read_bio (fs/btrfs/bio.c:180 fs/btrfs/bio.c:285)
>    simple_end_io_work
>    process_one_work
>    worker_thread
> 
> Reject any segment whose payload would extend beyond compressed_len before
> copying it, treating it as corruption like the other on-disk validation
> failures in this function.
> 
> Fixes: a6e66e6f8c1b ("btrfs: rework lzo_decompress_bio() to make it subpage compatible")
> Reported-by: Xiang Mei <xmei5@asu.edu>
> Assisted-by: Claude:claude-opus-4-8
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

And pushed to for-next.

Thanks,
Qu

> ---
> v2:
>    - Return -EUCLEAN instead of -EIO to match lzo_decompress() (Qu Wenruo).
>    - Emit a btrfs_err() message when rejecting the segment (Qu Wenruo).
> 
>   fs/btrfs/lzo.c | 11 +++++++++++
>   1 file changed, 11 insertions(+)
> 
> diff --git a/fs/btrfs/lzo.c b/fs/btrfs/lzo.c
> index 2de18c7b5..6e4aa2285 100644
> --- a/fs/btrfs/lzo.c
> +++ b/fs/btrfs/lzo.c
> @@ -491,6 +491,17 @@ int lzo_decompress_bio(struct list_head *ws, struct compressed_bio *cb)
>   			return -EIO;
>   		}
>   
> +		/* The segment must not extend beyond the compressed input. */
> +		if (unlikely(cur_in + seg_len > compressed_len)) {
> +			struct btrfs_inode *inode = cb->bbio.inode;
> +
> +			btrfs_err(fs_info,
> +			"lzo segment overflows compressed input, root %llu inode %llu offset %llu cur_in %u len %u compressed len %u",
> +				  btrfs_root_id(inode->root), btrfs_ino(inode),
> +				  cb->start, cur_in, seg_len, compressed_len);
> +			return -EUCLEAN;
> +		}
> +
>   		/* Copy the compressed segment payload into workspace */
>   		copy_compressed_segment(cb, &fi, &cur_folio_index, workspace->cbuf,
>   					seg_len, &cur_in);