GeezerVison is showing github.com — FastLz: Guard decompression against truncated input (#17000) · netty/netty@775ad71
File treeExpand file treeCollapse file tree Open diff view settings main/java/io/netty/handler/codec/compression test/java/io/netty/handler/codec/compression
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/FastLz.java +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -408,6 +408,10 @@ static int compress(final ByteBuf input, final int inOffset, final int inLength, | 408 | 408 | */ | 409 | 409 | static int decompress(final ByteBuf input, final int inOffset, final int inLength, | 410 | 410 | final ByteBuf output, final int outOffset, final int outLength) { | | 411 | + if (inLength == 0) { | | 412 | + return 0; | | 413 | + } | | 414 | + | 411 | 415 | //int level = ((*(const flzuint8*)input) >> 5) + 1; | 412 | 416 | final int level = (input.getByte(inOffset) >> 5) + 1; | 413 | 417 | if (level != LEVEL_1 && level != LEVEL_2) { | @@ -440,26 +444,41 @@ static int decompress(final ByteBuf input, final int inOffset, final int inLengt | 440 | 444 | int code; | 441 | 445 | if (len == 6) { | 442 | 446 | if (level == LEVEL_1) { | | 447 | + if (ip >= inLength) { | | 448 | + return 0; | | 449 | + } | 443 | 450 | // len += *ip++; | 444 | 451 | len += input.getUnsignedByte(inOffset + ip++); | 445 | 452 | } else { | 446 | 453 | do { | | 454 | + if (ip >= inLength) { | | 455 | + return 0; | | 456 | + } | 447 | 457 | code = input.getUnsignedByte(inOffset + ip++); | 448 | 458 | len += code; | 449 | 459 | } while (code == 255); | 450 | 460 | } | 451 | 461 | } | 452 | 462 | if (level == LEVEL_1) { | | 463 | + if (ip >= inLength) { | | 464 | + return 0; | | 465 | + } | 453 | 466 | // ref -= *ip++; | 454 | 467 | ref -= input.getUnsignedByte(inOffset + ip++); | 455 | 468 | } else { | | 469 | + if (ip >= inLength) { | | 470 | + return 0; | | 471 | + } | 456 | 472 | code = input.getUnsignedByte(inOffset + ip++); | 457 | 473 | ref -= code; | 458 | 474 |
| 459 | 475 | /* match from 16-bit distance */ | 460 | 476 | // if(FASTLZ_UNEXPECT_CONDITIONAL(code==255)) | 461 | 477 | // if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8))) | 462 | 478 | if (code == 255 && ofs == 31 << 8) { | | 479 | + if (ip + 2 > inLength) { | | 480 | + return 0; | | 481 | + } | 463 | 482 | ofs = input.getUnsignedByte(inOffset + ip++) << 8; | 464 | 483 | ofs += input.getUnsignedByte(inOffset + ip++); | 465 | 484 |
| |
Collapse file Copy file name to clipboard +55Lines changed: 55 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* | | 2 | + * Copyright 2026 The Netty Project | | 3 | + * | | 4 | + * The Netty Project licenses this file to you under the Apache License, | | 5 | + * version 2.0 (the "License"); you may not use this file except in compliance | | 6 | + * with the License. You may obtain a copy of the License at: | | 7 | + * | | 8 | + * https://www.apache.org/licenses/LICENSE-2.0 | | 9 | + * | | 10 | + * Unless required by applicable law or agreed to in writing, software | | 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | | 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | | 13 | + * License for the specific language governing permissions and limitations | | 14 | + * under the License. | | 15 | + */ | | 16 | +package io.netty.handler.codec.compression; | | 17 | + | | 18 | +import io.netty.buffer.Unpooled; | | 19 | +import io.netty.channel.embedded.EmbeddedChannel; | | 20 | +import org.junit.jupiter.api.Test; | | 21 | + | | 22 | +import static org.junit.jupiter.api.Assertions.assertThrows; | | 23 | + | | 24 | +public class FastLzFrameDecoderTest { | | 25 | + | | 26 | + @Test | | 27 | + public void testCompressedBlockWithEmptyPayload() { | | 28 | + assertDecompressionException(new byte[] { | | 29 | + 'F', 'L', 'Z', | | 30 | + 0x01, | | 31 | + 0x00, 0x00, | | 32 | + 0x00, 0x01 | | 33 | + }); | | 34 | + } | | 35 | + | | 36 | + @Test | | 37 | + public void testCompressedBlockWithTruncatedMatch() { | | 38 | + assertDecompressionException(new byte[] { | | 39 | + 'F', 'L', 'Z', | | 40 | + 0x01, | | 41 | + 0x00, 0x03, | | 42 | + 0x00, 0x04, | | 43 | + 0x00, 'A', 0x20 | | 44 | + }); | | 45 | + } | | 46 | + | | 47 | + private static void assertDecompressionException(byte[] input) { | | 48 | + EmbeddedChannel channel = new EmbeddedChannel(new FastLzFrameDecoder()); | | 49 | + try { | | 50 | + assertThrows(DecompressionException.class, () -> channel.writeInbound(Unpooled.wrappedBuffer(input))); | | 51 | + } finally { | | 52 | + channel.finishAndReleaseAll(); | | 53 | + } | | 54 | + } | | 55 | +} |
|