Skip to contentSkip to conversation

lib/decompress_bunzip2: fix off-by-one in run-length bounds check

PATCH Applied Matt Turner 2 participants 8 messages Sep 12 – Sep 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/decompress_bunzip2: fix off-by-one in run-length bounds check

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

Fixes bc22c17e12c1 ("bzip2/lzma: library support for gzip, bzip2 and lzma decompression")

---
lib/decompress_bunzip2: fix off-by-one in run-length bounds check

---
 lib/decompress_bunzip2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/decompress_bunzip2.c +1 -1
index 1288f146661f..aaa75404250e 100644
@@ -439,7 +439,7 @@ static int INIT get_next_block(struct bunzip_data *bd)
 		   array.) */
 		if (runPos) {
 			runPos = 0;
-			if (dbufCount+t >= dbufSize)
+			if (dbufCount+t > dbufSize)
 				return RETVAL_DATA_ERROR;
 
 			uc = symToByte[mtfSymbol[0]];

---
base-commit: 893e11787f78e43b534e252249ac3fff4d1333f8
change-id: 20260912-b4-bunzip2-blocksize-fix-7333376abd40

Best regards,

Signature · 1 line
Matt Turner <[email protected]>
maintainer
#1 lore raw
Quoted 6 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

Quoted 8 lines

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

Thanks.

Quoted 2 lines
Fixes bc22c17e12c1 ("bzip2/lzma: library support for gzip, bzip2 and lzma decompression")

Why is a backport proposed? Hopefully because downstream users need this change, but the changelog doesn't tell anyone the reasons why.

#2 lore raw
Quoted 18 lines · Andrew Morton
Quoted 6 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

Quoted 8 lines

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

Quoted 7 lines

Thanks.

Quoted 2 lines
Fixes bc22c17e12c1 ("bzip2/lzma: library support for gzip, bzip2 and lzma decompression")

Why is a backport proposed? Hopefully because downstream users need this change, but the changelog doesn't tell anyone the reasons why.

I ran into this because my system uses lbzip2 as /bin/bzip2 -- a common thing on Gentoo I believe. As far as I can tell, anyone using lbzip2 as their system bzip2 would run into this and it's only because it's very uncommon these days to compress a kernel with bzip2 that no one has noticed. I only noticed because I was adding support for various compression formats on alpha.

No strong preference for a backport from me. Just thought it would be desirable.

maintainer
#3 lore raw
Quoted 28 lines · On Sun, 13 Sep 2026 22:16:16 -0400 Matt Turner <[email protected]> wrote:
Quoted 18 lines · Andrew Morton
Quoted 6 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

Quoted 8 lines

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

Quoted 7 lines

Thanks.

Quoted 2 lines
Fixes bc22c17e12c1 ("bzip2/lzma: library support for gzip, bzip2 and lzma decompression")

Why is a backport proposed? Hopefully because downstream users need this change, but the changelog doesn't tell anyone the reasons why.

Quoted 6 lines

I ran into this because my system uses lbzip2 as /bin/bzip2 -- a common thing on Gentoo I believe. As far as I can tell, anyone using lbzip2 as their system bzip2 would run into this and it's only because it's very uncommon these days to compress a kernel with bzip2 that no one has noticed. I only noticed because I was adding support for various compression formats on alpha.

OK, thanks. A real-world i-hit-this is compelling.

Quoted 1 line

No strong preference for a backport from me. Just thought it would be desirable.

np, I;ll retain the cc:stable.

maintainer
#4 lore raw
In reply to Matt Turner
Quoted 18 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

...

lib/decompress_bunzip2.c static int INIT get_next_block(struct bunzip_data *bd) · line 442

Quoted 2 lines
lib/decompress_bunzip2.c +1 -1
-			if (dbufCount+t >= dbufSize)
+			if (dbufCount+t > dbufSize)

Sashiko thinks there's also a potential overflow here which could be addressed in this patch.

	https://sashiko.dev/#/patchset/[email protected]
#5 lore raw
Quoted 21 lines · Andrew Morton
Quoted 18 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

...

Quoted 7 lines

lib/decompress_bunzip2.c static int INIT get_next_block(struct bunzip_data *bd) · line 442

Quoted 2 lines
lib/decompress_bunzip2.c +1 -1
-                     if (dbufCount+t >= dbufSize)
+                     if (dbufCount+t > dbufSize)

Sashiko thinks there's also a potential overflow here which could be addressed in this patch.

        https://sashiko.dev/#/patchset/[email protected]

What's the appropriate flow to/from busybox (from which this code originates)? Apparently we don't sync from busybox, but do we want to avoid divergences?

maintainer
#6 lore raw
Quoted 26 lines · On Sun, 13 Sep 2026 22:18:07 -0400 Matt Turner <[email protected]> wrote:
Quoted 24 lines · Andrew Morton
Quoted 20 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

...

lib/decompress_bunzip2.c +1 -1
-                     if (dbufCount+t >= dbufSize)
+                     if (dbufCount+t > dbufSize)
Quoted 8 lines
Quoted 4 lines

Sashiko thinks there's also a potential overflow here which could be addressed in this patch.

        https://sashiko.dev/#/patchset/[email protected]

What's the appropriate flow to/from busybox (from which this code originates)? Apparently we don't sync from busybox, but do we want to avoid divergences?

I'm not aware of any such process for any of the lib/ material which mirrors some userspace project. So I guess it's an ad-hoc "send them an email" thing. There's clearly risk that if we accept a new drop from upstream, such kernel-first fixes will get lost :(

#7 lore raw
Quoted 34 lines · Andrew Morton
Quoted 31 lines · On Sun, 13 Sep 2026 22:18:07 -0400 Matt Turner <[email protected]> wrote:
Quoted 28 lines · Andrew Morton
Quoted 18 lines · On Sat, 12 Sep 2026 14:17:35 -0400 Matt Turner <[email protected]> wrote:

The run-length path rejects a block when dbufCount+t equals dbufSize, but the loop that follows writes exactly t bytes starting at dbufCount, so a block that fills the buffer exactly is legal. bzip2 allows it too:
its decompressor bounds a block at 100000 * blockSize100k and checks that limit per byte appended. Use > instead of >=.

bzip2's encoder stops filling a block 19 bytes early, so nothing it produces ever reaches the limit and the bug stays hidden. Compressors that use the full block size do reach it: an lbzip2 -9 image whose block ends on a run fails to decode, and a self-extracting kernel built that way does not boot.

This code came from busybox, which fixed the same line in 2013 in commit 932e233a491b ("bunzip2: fix off-by-one check").

...

lib/decompress_bunzip2.c static int INIT get_next_block(struct bunzip_data *bd) · line 442

Quoted 2 lines
lib/decompress_bunzip2.c +1 -1
-                     if (dbufCount+t >= dbufSize)
+                     if (dbufCount+t > dbufSize)

Sashiko thinks there's also a potential overflow here which could be addressed in this patch.

        https://sashiko.dev/#/patchset/[email protected]
Quoted 8 lines
Quoted 3 lines

What's the appropriate flow to/from busybox (from which this code originates)? Apparently we don't sync from busybox, but do we want to avoid divergences?

I'm not aware of any such process for any of the lib/ material which mirrors some userspace project. So I guess it's an ad-hoc "send them an email" thing. There's clearly risk that if we accept a new drop from upstream, such kernel-first fixes will get lost :(

Okay, thanks. I'll investigate the Sashiko report and follow up with busybox upstream and then circle back to LKML if there's something we need to address here.