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 — Merge commit from fork · yawkat/lz4-java@bb83dd1

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • java/net/jpountz/lz4
      • LZ4BlockInputStream.java
    • test/net/jpountz/lz4
      • LZ4BlockStreamingTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/java/net/jpountz/lz4/LZ4BlockInputStream.java‎

Copy file name to clipboardExpand all lines: src/java/net/jpountz/lz4/LZ4BlockInputStream.java
+36-8Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939
* {@link InputStream} implementation to decode data written with
4040
* {@link LZ4BlockOutputStream}. This class is not thread-safe and does not
4141
* 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>
4246
* @see LZ4BlockOutputStream
4347
*/
4448
public class LZ4BlockInputStream extends FilterInputStream {
@@ -47,6 +51,7 @@ public class LZ4BlockInputStream extends FilterInputStream {
4751
private final LZ4SafeDecompressor safeDecompressor;
4852
private final Checksum checksum;
4953
private final boolean stopOnEmptyBlock;
54+
private final boolean acceptOversizedBlocks;
5055
private byte[] buffer;
5156
private byte[] compressedBuffer;
5257
private int originalLen;
@@ -67,7 +72,7 @@ public class LZ4BlockInputStream extends FilterInputStream {
6772
*/
6873
@Deprecated
6974
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);
7176
}
7277

7378
/**
@@ -85,7 +90,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor,
8590
*/
8691
@Deprecated
8792
public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, Checksum checksum) {
88-
this(in, fastDecompressor, checksum, true);
93+
this(in, fastDecompressor, null, checksum, true, false);
8994
}
9095

9196
/**
@@ -101,7 +106,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor,
101106
*/
102107
@Deprecated
103108
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);
105110
}
106111

107112
/**
@@ -117,7 +122,7 @@ public LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor)
117122
*/
118123
@Deprecated
119124
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);
121126
}
122127

123128
/**
@@ -131,7 +136,7 @@ public LZ4BlockInputStream(InputStream in, boolean stopOnEmptyBlock) {
131136
*/
132137
@Deprecated
133138
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);
135140
}
136141

137142
/**
@@ -147,15 +152,19 @@ public LZ4BlockInputStream(InputStream in) {
147152
* equivalent to the instance which has been used to
148153
* write the stream
149154
* @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
150158
*/
151159
private LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor, LZ4SafeDecompressor safeDecompressor,
152-
Checksum checksum, boolean stopOnEmptyBlock) {
160+
Checksum checksum, boolean stopOnEmptyBlock, boolean acceptOversizedBlocks) {
153161
super(in);
154162

155163
this.fastDecompressor = fastDecompressor;
156164
this.safeDecompressor = safeDecompressor;
157165
this.checksum = checksum;
158166
this.stopOnEmptyBlock = stopOnEmptyBlock;
167+
this.acceptOversizedBlocks = acceptOversizedBlocks;
159168
this.buffer = new byte[0];
160169
this.compressedBuffer = new byte[HEADER_LENGTH];
161170
o = originalLen = 0;
@@ -168,6 +177,7 @@ private LZ4BlockInputStream(InputStream in, LZ4FastDecompressor fastDecompressor
168177
* <li> decompressor - {@code LZ4Factory.fastestInstance().safeDecompressor()} </li>
169178
* <li> checksum - {@link XXHash32} </li>
170179
* <li> stopOnEmptyBlock - {@code true} </li>
180+
* <li> acceptOversizedBlocks - {@code false} </li>
171181
* </ul>
172182
* @return new instance of {@link Builder} to be used to configure and build new LZ4 input stream
173183
*/
@@ -262,7 +272,8 @@ private void refill() throws IOException {
262272
|| compressedLen < 0
263273
|| (originalLen == 0 && compressedLen != 0)
264274
|| (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)) {
266277
throw new IOException("Stream is corrupted");
267278
}
268279
if (originalLen == 0 && compressedLen == 0) {
@@ -365,6 +376,7 @@ public String toString() {
365376
*/
366377
public static final class Builder {
367378
private boolean stopOnEmptyBlock = true;
379+
private boolean acceptOversizedBlocks = false;
368380
private LZ4FastDecompressor fastDecompressor;
369381
private LZ4SafeDecompressor safeDecompressor;
370382
private Checksum checksum;
@@ -383,6 +395,21 @@ public Builder withStopOnEmptyBlock(boolean stopOnEmptyBlock) {
383395
return this;
384396
}
385397

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+
386413
/**
387414
* Registers {@link LZ4FastDecompressor} to be used by the builder as a decompressor. Overrides one set by
388415
* {@link #withDecompressor(LZ4SafeDecompressor)}
@@ -433,6 +460,7 @@ public Builder withChecksum(Checksum checksum) {
433460
* @see #withDecompressor(LZ4FastDecompressor)
434461
* @see #withDecompressor(LZ4SafeDecompressor)
435462
* @see #withStopOnEmptyBlock(boolean)
463+
* @see #withAcceptOversizedBlocks(boolean)
436464
*/
437465
public LZ4BlockInputStream build(InputStream in) {
438466
Checksum checksum = this.checksum;
@@ -445,7 +473,7 @@ public LZ4BlockInputStream build(InputStream in) {
445473
if (fastDecompressor == null && safeDecompressor == null) {
446474
safeDecompressor = LZ4Factory.fastestInstance().safeDecompressor();
447475
}
448-
return new LZ4BlockInputStream(in, fastDecompressor, safeDecompressor, checksum, stopOnEmptyBlock);
476+
return new LZ4BlockInputStream(in, fastDecompressor, safeDecompressor, checksum, stopOnEmptyBlock, acceptOversizedBlocks);
449477
}
450478
}
451479
}
Collapse file

‎src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java‎

Copy file name to clipboardExpand all lines: src/test/net/jpountz/lz4/LZ4BlockStreamingTest.java
+82Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,86 @@ public void testCorruptedStream() {
403403
e = assertThrows(IOException.class, () -> lz4BlockInputStreamBuilder().build(new ByteArrayInputStream(bytesWrongDecompressed)).readAllBytes());
404404
assertEquals("Stream is corrupted", e.getMessage());
405405
}
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+
}
406488
}

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.