Skip to content

btrfs: lzo: Harden decompression callers to avoid kernel memory corruption.

Qu Wenruo linux-btrfs 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest4 patches 5 repliesIn review
  2. v24 patches 6 repliesSuperseded
  3. v14 patches 7 repliesSuperseded

Changes in v3 from the cover letter

Fix comment error for inlined lzo compressed extent. (Still has header), thanks David for pointing this out.
Add example ascii graph as an example.
Enhance inlined extent check, as header length must match with segment header length + LZO_LEN * 2.

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4btrfs: compression: Add linux/sizes.h for compression.hfs/btrfs/compression.h+1 -00 replies
2/4btrfs: lzo: Add comment about the how btrfs records its lzo compressed datafs/btrfs/lzo.c+35 -00 replies
3/4btrfs: lzo: Add header length check to avoid slab out of bounds accessfs/btrfs/lzo.c+34 -17 replies
4/4btrfs: lzo: Harden inline lzo compressed extent decompressionfs/btrfs/lzo.c+10 -10 replies

Reviews trailers per patch

2 of 4 patches have a Reviewed-by or Acked-by.

PatchNikolay BorisovMisono Tomohiro
1/4R-b
2/4
3/4R-b
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 0/4] btrfs: lzo: Harden decompression callers to avoid kernel memory corruption.

This patchset can be fetched from github:
https://github.com/adam900710/linux/tree/lzo_corruption
Which is based on v4.17-rc5.

James Harvey reported pretty strange kernel misbehavior where after reading certain btrfs compressed data, kernel crash with unrelated calltrace.
(https://bugzilla.kernel.org/show_bug.cgi?id=199707 and
https://www.spinics.net/lists/linux-btrfs/msg77971.html)

Thanks for his comprehensive debug help, we located the problem to:

1) Bad lzo decompression verification check
The direct cause.
The lzo decompression code involves memory copy for compressed data who crosses page boundary.
And if corrupted segment header contains invalid length, btrfs will do memory copy and then corrupt kernel memory.

Fix it by add header length check for each segment, and just in case, also add uncompressed data length check. (Patch 3)
The full explanation and kasan output can also be found in that patch.

2) Compressed extent created for NODATASUM inode
The root cause.
Will be addressed in another patchset.

Other patches are mostly cosmetic, like adding extra include for compression.h, to avoid later compiling error. (Patch 1)
Or adding comment of how btrfs organise its compressed data (Patch 2) And adding extra check for inlined compressed data even it's not really possible to happen (Patch 4)

Changelog:
v2:
Cosmetic updates. Mostly for comment and gramma. Comment and gramma fixes suggested by Nikolay. One 4 bytes <-> LE32 convert fixes in comment. Added reviewed-by tag for the 1st patch.

v3:
Fix comment error for inlined lzo compressed extent. (Still has header), thanks David for pointing this out.
Add example ascii graph as an example.
Enhance inlined extent check, as header length must match with segment header length + LZO_LEN * 2.

Qu Wenruo (4):
btrfs: compression: Add linux/sizes.h for compression.h
btrfs: lzo: Add comment about the how btrfs records its lzo compressed
data
btrfs: lzo: Add header length check to avoid slab out of bounds access
btrfs: lzo: Harden inline lzo compressed extent decompression

 fs/btrfs/compression.h |  1 +
 fs/btrfs/lzo.c         | 81 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 80 insertions(+), 2 deletions(-)
Signature · 1 line
2.17.0