GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Port 4.1: Validate bzip2 Huffman code lengths before building decode … · netty/netty@f2f1b52

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec/src
    • main/java/io/netty/handler/codec/compression
      • Bzip2Decoder.java
    • test/java/io/netty/handler/codec/compression
      • Bzip2DecoderTest.java
      • Bzip2MalformedStreams.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec/src/main/java/io/netty/handler/codec/compression/Bzip2Decoder.java‎

Copy file name to clipboardExpand all lines: codec/src/main/java/io/netty/handler/codec/compression/Bzip2Decoder.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static io.netty.handler.codec.compression.Bzip2Constants.BLOCK_HEADER_MAGIC_2;
2727
import static io.netty.handler.codec.compression.Bzip2Constants.END_OF_STREAM_MAGIC_1;
2828
import static io.netty.handler.codec.compression.Bzip2Constants.END_OF_STREAM_MAGIC_2;
29+
import static io.netty.handler.codec.compression.Bzip2Constants.HUFFMAN_DECODE_MAX_CODE_LENGTH;
2930
import static io.netty.handler.codec.compression.Bzip2Constants.HUFFMAN_MAXIMUM_TABLES;
3031
import static io.netty.handler.codec.compression.Bzip2Constants.HUFFMAN_MAX_ALPHABET_SIZE;
3132
import static io.netty.handler.codec.compression.Bzip2Constants.HUFFMAN_MINIMUM_TABLES;
@@ -273,6 +274,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
273274
break loop;
274275
}
275276
}
277+
if (currLength < 1 || currLength > HUFFMAN_DECODE_MAX_CODE_LENGTH) {
278+
throw new DecompressionException("Bzip2 block Huffman code length not in range 1.."
279+
+ HUFFMAN_DECODE_MAX_CODE_LENGTH);
280+
}
276281
codeLength[currGroup][currAlpha] = (byte) currLength;
277282
}
278283
currLength = -1;
Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/Bzip2DecoderTest.java
+17Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,23 @@ public void execute() {
322322
});
323323
}
324324

325+
/**
326+
* Regression test: a Huffman code length read in {@code RECEIVE_HUFFMAN_LENGTH} was never
327+
* validated against the 1..23 range before being stored and later used to index the per-table
328+
* code-base array (sized for lengths up to 23), so a crafted stream could trigger an
329+
* {@link ArrayIndexOutOfBoundsException} instead of a clean {@link DecompressionException}.
330+
*/
331+
@Test
332+
public void testCodeLengthOutOfRange() {
333+
final ByteBuf in = Unpooled.wrappedBuffer(Bzip2MalformedStreams.codeLengthOutOfRange());
334+
assertThrows(DecompressionException.class, new Executable() {
335+
@Override
336+
public void execute() {
337+
channel.writeInbound(in);
338+
}
339+
}, "Bzip2 block Huffman code length not in range");
340+
}
341+
325342
@Override
326343
protected byte[] compress(byte[] data) throws Exception {
327344
ByteArrayOutputStream os = new ByteArrayOutputStream();
Collapse file

‎codec/src/test/java/io/netty/handler/codec/compression/Bzip2MalformedStreams.java‎

Copy file name to clipboardExpand all lines: codec/src/test/java/io/netty/handler/codec/compression/Bzip2MalformedStreams.java
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,41 @@ static byte[] selectorIndexOutOfRange() {
5858
return out.toByteArray();
5959
}
6060

61+
/**
62+
* A stream whose Huffman table starts the code-length delta coding at 31, which is outside the
63+
* valid range (1..23). The length is written straight into the per-table code-length array and
64+
* later used to index the code-base array (sized for lengths up to 23), so an unvalidated value
65+
* drives that index out of bounds.
66+
*/
67+
static byte[] codeLengthOutOfRange() {
68+
BitWriter bits = new BitWriter();
69+
bits.writeBit(0); // randomised = false
70+
bits.writeBits(0, 24); // bwtStartPointer
71+
bits.writeBits(0x0001, 16); // huffmanInUse16: only the range 0xF0-0xFF is in use
72+
bits.writeBits(0x8000, 16); // symbol sub-bitmap: only 0xF0 is used -> alphabet size 3
73+
bits.writeBits(2, 3); // totalTables = 2
74+
bits.writeBits(1, 15); // totalSelectors = 1
75+
bits.writeBit(0); // unary-coded selector index = 0 (valid)
76+
// Huffman code lengths for 2 tables of 3 symbols each. Each table starts the delta coding
77+
// at 31 and never alters it, so every code length is 31 (out of the valid 1..23 range).
78+
for (int table = 0; table < 2; table++) {
79+
bits.writeBits(31, 5); // start_huffman_length = 31
80+
for (int symbol = 0; symbol < 3; symbol++) {
81+
bits.writeBit(0); // 0 => commit current length without alteration
82+
}
83+
}
84+
85+
ByteArrayOutputStream out = new ByteArrayOutputStream();
86+
writeMedium(out, MAGIC_NUMBER);
87+
out.write('1'); // block size digit
88+
writeMedium(out, BLOCK_HEADER_MAGIC_1);
89+
writeMedium(out, BLOCK_HEADER_MAGIC_2);
90+
writeInt(out, 0); // block CRC (irrelevant, the fault triggers earlier)
91+
byte[] bitBytes = bits.toByteArray();
92+
out.write(bitBytes, 0, bitBytes.length);
93+
return out.toByteArray();
94+
}
95+
6196
private static void writeMedium(ByteArrayOutputStream out, int value) {
6297
out.write(value >>> 16);
6398
out.write(value >>> 8);

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.