GeezerVison is showing github.com — Merge commit from fork · yawkat/lz4-java@bb83dd1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/java/net/jpountz/lz4/LZ4BlockInputStream.java +36-8Lines changed: 36 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 39 | 39 | * {@link InputStream} implementation to decode data written with | 40 | 40 | * {@link LZ4BlockOutputStream}. This class is not thread-safe and does not | 41 | 41 | * support {@link #mark(int)}/{@link #reset()}. | | 42 | + * <p>Use {@link Builder#withAcceptOversizedBlocks(boolean)} only for trusted | | 43 | + * inputs that may contain noncanonical legacy LZ4 blocks. Enabling it restores | | 44 | + * acceptance of blocks whose compressed length is greater than or equal to the | | 45 | + * original length, which may permit large attacker-controlled allocations.</p> | 42 | 46 | * @see LZ4BlockOutputStream | 43 | 47 | */ | 44 | 48 | public class LZ4BlockInputStream extends FilterInputStream { | @@ -47,6 +51,7 @@ public class LZ4BlockInputStream extends FilterInputStream { | 47 | 51 | private final LZ4SafeDecompressor safeDecompressor; | 48 | 52 | private final Checksum checksum; | 49 | 53 | private final boolean stopOnEmptyBlock; | | 54 | + private final boolean acceptOversizedBlocks; | 50 | 55 | private byte[] buffer; | 51 | 56 | private byte[] compressedBuffer; | 52 | 57 | private int originalLen; | @@ -67,7 +72,7 @@ public class LZ4BlockInputStream extends FilterInputStream { | 67 | 72 | */ | 68 | 73 | @Deprecated | 69 | 74 | public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, Checksum checksum, boolean stopOnEmptyBlock) { | 70 | | - this(in, fastDecompressor, null, checksum, stopOnEmptyBlock); | | 75 | + this(in, fastDecompressor, null, checksum, stopOnEmptyBlock, false); | 71 | 76 | } | 72 | 77 |
| 73 | 78 | /** | @@ -85,7 +90,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, | 85 | 90 | */ | 86 | 91 | @Deprecated | 87 | 92 | public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, Checksum checksum) { | 88 | | - this(in, fastDecompressor, checksum, true); | | 93 | + this(in, fastDecompressor, null, checksum, true, false); | 89 | 94 | } | 90 | 95 |
| 91 | 96 | /** | @@ -101,7 +106,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, | 101 | 106 | */ | 102 | 107 | @Deprecated | 103 | 108 | public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor) { | 104 | | - this(in, fastDecompressor, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), true); | | 109 | + this(in, fastDecompressor, null, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), true, false); | 105 | 110 | } | 106 | 111 |
| 107 | 112 | /** | @@ -117,7 +122,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor) | 117 | 122 | */ | 118 | 123 | @Deprecated | 119 | 124 | public LZ4BlockInputStream(InputStream in, boolean stopOnEmptyBlock) { | 120 | | - this(in, LZ4Factory.fastestInstance().fastDecompressor(), XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), stopOnEmptyBlock); | | 125 | + this(in, LZ4Factory.fastestInstance().fastDecompressor(), null, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), stopOnEmptyBlock, false); | 121 | 126 | } | 122 | 127 |
| 123 | 128 | /** | @@ -131,7 +136,7 @@ public LZ4BlockInputStream(InputStream in, boolean stopOnEmptyBlock) { | 131 | 136 | */ | 132 | 137 | @Deprecated | 133 | 138 | public LZ4BlockInputStream(InputStream in) { | 134 | | - this(in, LZ4Factory.fastestInstance().fastDecompressor()); | | 139 | + this(in, LZ4Factory.fastestInstance().fastDecompressor(), null, XXHashFactory.fastestInstance().newStreamingHash32(DEFAULT_SEED).asChecksum(), true, false); | 135 | 140 | } | 136 | 141 |
| 137 | 142 | /** | @@ -147,15 +152,19 @@ public LZ4BlockInputStream(InputStream in) { | 147 | 152 | * equivalent to the instance which has been used to | 148 | 153 | * write the stream | 149 | 154 | * @param stopOnEmptyBlock whether read is stopped on an empty block | | 155 | + * @param acceptOversizedBlocks whether noncanonical legacy LZ4 blocks with | | 156 | + * compressed length greater than or equal to | | 157 | + * original length are accepted | 150 | 158 | */ | 151 | 159 | private LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, LZ4SafeDecompressor safeDecompressor, | 152 | | - Checksum checksum, boolean stopOnEmptyBlock) { | | 160 | + Checksum checksum, boolean stopOnEmptyBlock, boolean acceptOversizedBlocks) { | 153 | 161 | super(in); | 154 | 162 |
| 155 | 163 | this.fastDecompressor = fastDecompressor; | 156 | 164 | this.safeDecompressor = safeDecompressor; | 157 | 165 | this.checksum = checksum; | 158 | 166 | this.stopOnEmptyBlock = stopOnEmptyBlock; | | 167 | + this.acceptOversizedBlocks = acceptOversizedBlocks; | 159 | 168 | this.buffer = new byte[0]; | 160 | 169 | this.compressedBuffer = new byte[HEADER_LENGTH]; | 161 | 170 | o = originalLen = 0; | @@ -168,6 +177,7 @@ private LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor | 168 | 177 | * <li> decompressor - {@code LZ4Factory.fastestInstance().safeDecompressor()} </li> | 169 | 178 | * <li> checksum - {@link XXHash32} </li> | 170 | 179 | * <li> stopOnEmptyBlock - {@code true} </li> | | 180 | + * <li> acceptOversizedBlocks - {@code false} </li> | 171 | 181 | * </ul> | 172 | 182 | * @return new instance of {@link Builder} to be used to configure and build new LZ4 input stream | 173 | 183 | */ | @@ -262,7 +272,8 @@ private void refill() throws IOException { | 262 | 272 | || compressedLen < 0 | 263 | 273 | || (originalLen == 0 && compressedLen != 0) | 264 | 274 | || (originalLen != 0 && compressedLen == 0) | 265 | | - || (compressionMethod == COMPRESSION_METHOD_RAW && originalLen != compressedLen)) { | | 275 | + || (compressionMethod == COMPRESSION_METHOD_RAW && originalLen != compressedLen) | | 276 | + || (compressionMethod == COMPRESSION_METHOD_LZ4 && !acceptOversizedBlocks && compressedLen >= originalLen)) { | 266 | 277 | throw new IOException("Stream is corrupted"); | 267 | 278 | } | 268 | 279 | if (originalLen == 0 && compressedLen == 0) { | @@ -365,6 +376,7 @@ public String toString() { | 365 | 376 | */ | 366 | 377 | public static final class Builder { | 367 | 378 | private boolean stopOnEmptyBlock = true; | | 379 | + private boolean acceptOversizedBlocks = false; | 368 | 380 | private LZ4FastDecompressor fastDecompressor; | 369 | 381 | private LZ4SafeDecompressor safeDecompressor; | 370 | 382 | private Checksum checksum; | @@ -383,6 +395,21 @@ public Builder withStopOnEmptyBlock(boolean stopOnEmptyBlock) { | 383 | 395 | return this; | 384 | 396 | } | 385 | 397 |
| | 398 | + /** | | 399 | + * Registers whether the builder should accept oversized blocks whose | | 400 | + * compressed length is greater than or equal to the original length. | | 401 | + * <p>Only enable this for trusted inputs. Accepting such blocks may permit | | 402 | + * large attacker-controlled allocations.</p> | | 403 | + * | | 404 | + * @param acceptOversizedBlocks whether oversized blocks should be accepted | | 405 | + * @return current builder instance | | 406 | + * @since 1.11.2 | | 407 | + */ | | 408 | + public Builder withAcceptOversizedBlocks(boolean acceptOversizedBlocks) { | | 409 | + this.acceptOversizedBlocks = acceptOversizedBlocks; | | 410 | + return this; | | 411 | + } | | 412 | + | 386 | 413 | /** | 387 | 414 | * Registers {@link LZ4FastDecompressor} to be used by the builder as a decompressor. Overrides one set by | 388 | 415 | * {@link #withDecompressor(LZ4SafeDecompressor)} | @@ -433,6 +460,7 @@ public Builder withChecksum(Checksum checksum) { | 433 | 460 | * @see #withDecompressor(LZ4FastDecompressor) | 434 | 461 | * @see #withDecompressor(LZ4SafeDecompressor) | 435 | 462 | * @see #withStopOnEmptyBlock(boolean) | | 463 | + * @see #withAcceptOversizedBlocks(boolean) | 436 | 464 | */ | 437 | 465 | public LZ4BlockInputStream build(InputStream in) { | 438 | 466 | Checksum checksum = this.checksum; | @@ -445,7 +473,7 @@ public LZ4BlockInputStream build(InputStream in) { | 445 | 473 | if (fastDecompressor == null && safeDecompressor == null) { | 446 | 474 | safeDecompressor = LZ4Factory.fastestInstance().safeDecompressor(); | 447 | 475 | } | 448 | | - return new LZ4BlockInputStream(in, fastDecompressor, safeDecompressor, checksum, stopOnEmptyBlock); | | 476 | + return new LZ4BlockInputStream(in, fastDecompressor, safeDecompressor, checksum, stopOnEmptyBlock, acceptOversizedBlocks); | 449 | 477 | } | 450 | 478 | } | 451 | 479 | } |
Collapse file Copy file name to clipboardExpand all lines: src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java +82Lines changed: 82 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -403,4 +403,86 @@ public void testCorruptedStream() { | 403 | 403 | e = assertThrows(IOException.class, () -> lz4BlockInputStreamBuilder().build(new ByteArrayInputStream(bytesWrongDecompressed)).readAllBytes()); | 404 | 404 | assertEquals("Stream is corrupted", e.getMessage()); | 405 | 405 | } | | 406 | + | | 407 | + @Test | | 408 | + public void testRejectsInvalidLz4CompressedLength() { | | 409 | + byte[] bytesCompressedEqOriginal = { | | 410 | + 76, 90, 52, 66, 108, 111, 99, 107, 32, | | 411 | + // Compressed length | | 412 | + 16, 0, 0, 0, | | 413 | + // Decompressed length | | 414 | + 16, 0, 0, 0, | | 415 | + // Header checksum | | 416 | + -53, 21, -62, 6, | | 417 | + // Payload | | 418 | + 64, 1, 2, 3, 4, 4, 0, -128, 5, 6, 7, 8, 9, 10, 11, 12, | | 419 | + // Terminator | | 420 | + 76, 90, 52, 66, 108, 111, 99, 107, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | | 421 | + }; | | 422 | + var e = assertThrows(IOException.class, () -> lz4BlockInputStreamBuilder().build(new ByteArrayInputStream(bytesCompressedEqOriginal)).readAllBytes()); | | 423 | + assertEquals("Stream is corrupted", e.getMessage()); | | 424 | + | | 425 | + byte[] bytesCompressedGtOriginal = { | | 426 | + 76, 90, 52, 66, 108, 111, 99, 107, 32, | | 427 | + // Compressed length | | 428 | + 9, 0, 0, 0, | | 429 | + // Decompressed length | | 430 | + 8, 0, 0, 0, | | 431 | + // Header checksum | | 432 | + -91, -16, 50, 4, | | 433 | + // Payload | | 434 | + -128, 1, 2, 3, 4, 5, 6, 7, 8, | | 435 | + // Terminator | | 436 | + 76, 90, 52, 66, 108, 111, 99, 107, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | | 437 | + }; | | 438 | + e = assertThrows(IOException.class, () -> lz4BlockInputStreamBuilder().build(new ByteArrayInputStream(bytesCompressedGtOriginal)).readAllBytes()); | | 439 | + assertEquals("Stream is corrupted", e.getMessage()); | | 440 | + } | | 441 | + | | 442 | + @Test | | 443 | + public void testAcceptsInvalidLz4CompressedLengthInOversizedBlocksMode() throws IOException { | | 444 | + byte[] bytesCompressedEqOriginal = { | | 445 | + 76, 90, 52, 66, 108, 111, 99, 107, 32, | | 446 | + // Compressed length | | 447 | + 16, 0, 0, 0, | | 448 | + // Decompressed length | | 449 | + 16, 0, 0, 0, | | 450 | + // Header checksum | | 451 | + -53, 21, -62, 6, | | 452 | + // Payload | | 453 | + 64, 1, 2, 3, 4, 4, 0, -128, 5, 6, 7, 8, 9, 10, 11, 12, | | 454 | + // Terminator | | 455 | + 76, 90, 52, 66, 108, 111, 99, 107, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | | 456 | + }; | | 457 | + byte[] expectedCompressedEqOriginal = { | | 458 | + 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 | | 459 | + }; | | 460 | + LZ4BlockInputStream in = lz4BlockInputStreamBuilder() | | 461 | + .withAcceptOversizedBlocks(true) | | 462 | + .build(new ByteArrayInputStream(bytesCompressedEqOriginal)); | | 463 | + assertArrayEquals(expectedCompressedEqOriginal, in.readAllBytes()); | | 464 | + in.close(); | | 465 | + | | 466 | + byte[] bytesCompressedGtOriginal = { | | 467 | + 76, 90, 52, 66, 108, 111, 99, 107, 32, | | 468 | + // Compressed length | | 469 | + 9, 0, 0, 0, | | 470 | + // Decompressed length | | 471 | + 8, 0, 0, 0, | | 472 | + // Header checksum | | 473 | + -91, -16, 50, 4, | | 474 | + // Payload | | 475 | + -128, 1, 2, 3, 4, 5, 6, 7, 8, | | 476 | + // Terminator | | 477 | + 76, 90, 52, 66, 108, 111, 99, 107, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | | 478 | + }; | | 479 | + byte[] expectedCompressedGtOriginal = { | | 480 | + 1, 2, 3, 4, 5, 6, 7, 8 | | 481 | + }; | | 482 | + in = lz4BlockInputStreamBuilder() | | 483 | + .withAcceptOversizedBlocks(true) | | 484 | + .build(new ByteArrayInputStream(bytesCompressedGtOriginal)); | | 485 | + assertArrayEquals(expectedCompressedGtOriginal, in.readAllBytes()); | | 486 | + in.close(); | | 487 | + } | 406 | 488 | } |
|