Skip to content

Commit 943c604

Browse files
xerialclaude
andauthored
Validate buffer bounds and declared lengths before calling native code (#739)
The JNI layer writes through raw pointers without any bounds check, so the Java API must validate every range it hands to native code. - compress/rawCompress: require maxCompressedLength(inputLength) bytes of output space and an in-range input (#732, #717) - uncompress/rawUncompress: require the declared uncompressed length to fit in the output buffer (#728, CVE-2026-90559, #717) - uncompress*Array: reject uncompressed lengths that are not a multiple of the element size, which under-allocated the result array (#729) - uncompressedLength: reject negative lengths and lengths no valid Snappy stream of the given size can produce (> 64/3 of the compressed size), preventing huge allocations from a few bytes of input (#625, #730) - SnappyFramedInputStream: skip consecutive skippable chunks iteratively instead of recursing once per 4-byte chunk (#731) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b68e18d commit 943c604

3 files changed

Lines changed: 490 additions & 14 deletions

File tree

‎src/main/java/org/xerial/snappy/Snappy.java‎

Lines changed: 140 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public static int compress(byte[] input, int inputOffset, int inputLength, byte[
133133
* @param compressed output of the compressed data. Uses range [pos()..].
134134
* @return byte size of the compressed data.
135135
* @throws SnappyError when the input is not a direct buffer
136+
* @throws IllegalArgumentException when the output buffer has less than {@link #maxCompressedLength(int)} bytes
137+
* remaining
136138
*/
137139
public static int compress(ByteBuffer uncompressed, ByteBuffer compressed)
138140
throws IOException
@@ -150,6 +152,7 @@ public static int compress(ByteBuffer uncompressed, ByteBuffer compressed)
150152
int uPos = uncompressed.position();
151153
int uLen = uncompressed.remaining();
152154
int cPos = compressed.position();
155+
checkOutputSpace(compressed.remaining(), 0, requiredCompressedLength(uLen));
153156
int compressedSize = impl.rawCompress(uncompressed, uPos, uLen, compressed,
154157
cPos);
155158

@@ -354,6 +357,7 @@ public static boolean isValidCompressedBuffer(byte[] input, int offset, int leng
354357
if (input == null) {
355358
throw new NullPointerException("input is null");
356359
}
360+
checkArrayRange(input, offset, length, "input");
357361
return impl.isValidCompressedBuffer(input, offset, length);
358362
}
359363

@@ -446,7 +450,11 @@ public static long rawUncompress(long inputAddr, long inputSize, long destAddr)
446450
public static byte[] rawCompress(Object data, int byteSize)
447451
throws IOException
448452
{
449-
byte[] buf = new byte[Snappy.maxCompressedLength(byteSize)];
453+
if (data == null) {
454+
throw new NullPointerException("input is null");
455+
}
456+
checkArrayRange(data, 0, byteSize, "input");
457+
byte[] buf = new byte[requiredCompressedLength(byteSize)];
450458
int compressedByteSize = impl.rawCompress(data, 0, byteSize, buf, 0);
451459
byte[] result = new byte[compressedByteSize];
452460
System.arraycopy(buf, 0, result, 0, compressedByteSize);
@@ -464,13 +472,17 @@ public static byte[] rawCompress(Object data, int byteSize)
464472
* @param outputOffset byte offset at the output array
465473
* @return byte size of the compressed data
466474
* @throws IOException
475+
* @throws IllegalArgumentException if the input range is out of bounds, or the output array does not have
476+
* {@link #maxCompressedLength(int)} bytes available after outputOffset
467477
*/
468478
public static int rawCompress(Object input, int inputOffset, int inputLength, byte[] output, int outputOffset)
469479
throws IOException
470480
{
471481
if (input == null || output == null) {
472482
throw new NullPointerException("input or output is null");
473483
}
484+
checkArrayRange(input, inputOffset, inputLength, "input");
485+
checkOutputSpace(output.length, outputOffset, requiredCompressedLength(inputLength));
474486

475487
int compressedSize = impl
476488
.rawCompress(input, inputOffset, inputLength, output, outputOffset);
@@ -494,16 +506,127 @@ public static int rawCompress(Object input, int inputOffset, int inputLength, by
494506
* @param outputOffset byte offset in the output buffer
495507
* @return the byte size of the uncompressed data
496508
* @throws IOException when failed to uncompress the input data
509+
* @throws IllegalArgumentException if the input range is out of bounds, or the output array does not have
510+
* enough space after outputOffset for the uncompressed data
497511
*/
498512
public static int rawUncompress(byte[] input, int inputOffset, int inputLength, Object output, int outputOffset)
499513
throws IOException
500514
{
501515
if (input == null || output == null) {
502516
throw new NullPointerException("input or output is null");
503517
}
518+
// uncompressedLength validates the input range
519+
int requiredSize = uncompressedLength(input, inputOffset, inputLength);
520+
checkOutputSpace(byteLengthOf(output), outputOffset, requiredSize);
504521
return impl.rawUncompress(input, inputOffset, inputLength, output, outputOffset);
505522
}
506523

524+
/**
525+
* Returns the byte size of the given primitive array
526+
*/
527+
private static long byteLengthOf(Object array)
528+
{
529+
if (array instanceof byte[]) {
530+
return ((byte[]) array).length;
531+
}
532+
if (array instanceof boolean[]) {
533+
return ((boolean[]) array).length;
534+
}
535+
if (array instanceof char[]) {
536+
return 2L * ((char[]) array).length;
537+
}
538+
if (array instanceof short[]) {
539+
return 2L * ((short[]) array).length;
540+
}
541+
if (array instanceof int[]) {
542+
return 4L * ((int[]) array).length;
543+
}
544+
if (array instanceof float[]) {
545+
return 4L * ((float[]) array).length;
546+
}
547+
if (array instanceof long[]) {
548+
return 8L * ((long[]) array).length;
549+
}
550+
if (array instanceof double[]) {
551+
return 8L * ((double[]) array).length;
552+
}
553+
throw new IllegalArgumentException("not a primitive array: " + array.getClass().getName());
554+
}
555+
556+
/**
557+
* Ensures the byte range [offset, offset+length) lies within the given primitive array, since the native code
558+
* accesses the array through a raw pointer without any bounds check.
559+
*/
560+
private static void checkArrayRange(Object array, int offset, int length, String name)
561+
{
562+
long size = byteLengthOf(array);
563+
if (offset < 0 || length < 0 || (long) offset + length > size) {
564+
throw new IllegalArgumentException(String.format(
565+
"%s range [offset:%,d, length:%,d] is out of bounds for %s of %,d bytes",
566+
name, offset, length, name, size));
567+
}
568+
}
569+
570+
/**
571+
* Ensures the output has at least requiredSize bytes available after outputOffset, since the native code writes
572+
* the output through a raw pointer without any bounds check.
573+
*/
574+
private static void checkOutputSpace(long outputSize, long outputOffset, long requiredSize)
575+
{
576+
if (outputOffset < 0 || outputOffset > outputSize) {
577+
throw new IllegalArgumentException(String.format(
578+
"output offset %,d is out of bounds for output of %,d bytes", outputOffset, outputSize));
579+
}
580+
if (outputSize - outputOffset < requiredSize) {
581+
throw new IllegalArgumentException(String.format(
582+
"not enough space for output: need %,d bytes, but only %,d remaining",
583+
requiredSize, outputSize - outputOffset));
584+
}
585+
}
586+
587+
/**
588+
* Returns the output buffer size required by the native compressor for the given input size.
589+
*/
590+
private static int requiredCompressedLength(int inputLength)
591+
{
592+
int maxCompressedLength = maxCompressedLength(inputLength);
593+
// maxCompressedLength overflows int for inputs close to 2GB
594+
if (maxCompressedLength < 0) {
595+
throw new SnappyError(SnappyErrorCode.TOO_LARGE_INPUT, "input is too large to compress: " + inputLength);
596+
}
597+
return maxCompressedLength;
598+
}
599+
600+
/**
601+
* Rejects an uncompressed length declared in a compressed stream header that no valid Snappy stream of the
602+
* given compressed size can produce. The densest Snappy element is a 3-byte copy that expands to 64 bytes, so
603+
* a valid stream never declares more than 64/3 times its compressed size. This prevents allocating huge (or
604+
* negative-size) buffers from a few bytes of crafted input before decompression detects the corruption.
605+
*/
606+
private static long checkUncompressedLength(long uncompressedLength, long compressedLength)
607+
throws IOException
608+
{
609+
if (uncompressedLength < 0 || uncompressedLength > compressedLength * 64 / 3) {
610+
throw new SnappyIOException(SnappyErrorCode.PARSING_ERROR, String.format(
611+
"invalid uncompressed length %,d declared in %,d bytes of compressed data",
612+
uncompressedLength, compressedLength));
613+
}
614+
return uncompressedLength;
615+
}
616+
617+
/**
618+
* Rejects an uncompressed length that is not a multiple of the element size of the typed array to produce.
619+
*/
620+
private static void checkElementAlignment(int uncompressedLength, int elementSize)
621+
throws IOException
622+
{
623+
if (uncompressedLength % elementSize != 0) {
624+
throw new SnappyIOException(SnappyErrorCode.FAILED_TO_UNCOMPRESS, String.format(
625+
"uncompressed length %,d is not a multiple of the element size %d",
626+
uncompressedLength, elementSize));
627+
}
628+
}
629+
507630
/**
508631
* High-level API for uncompressing the input byte array.
509632
*
@@ -557,6 +680,8 @@ public static int uncompress(byte[] input, int inputOffset, int inputLength, byt
557680
* @return uncompressed data size
558681
* @throws IOException when failed to uncompress the given input
559682
* @throws SnappyError when the input is not a direct buffer
683+
* @throws IllegalArgumentException when the output buffer does not have enough space remaining for the
684+
* uncompressed data
560685
*/
561686
public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed)
562687
throws IOException
@@ -572,6 +697,7 @@ public static int uncompress(ByteBuffer compressed, ByteBuffer uncompressed)
572697
int cPos = compressed.position();
573698
int cLen = compressed.remaining();
574699
int uPos = uncompressed.position();
700+
checkOutputSpace(uncompressed.remaining(), 0, uncompressedLength(compressed));
575701

576702
// pos limit
577703
// [ ......UUUUUU.........]
@@ -608,6 +734,7 @@ public static char[] uncompressCharArray(byte[] input, int offset, int length)
608734
throws IOException
609735
{
610736
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
737+
checkElementAlignment(uncompressedLength, 2);
611738
char[] result = new char[uncompressedLength / 2];
612739
impl.rawUncompress(input, offset, length, result, 0);
613740
return result;
@@ -639,6 +766,7 @@ public static double[] uncompressDoubleArray(byte[] input, int offset, int lengt
639766
throws IOException
640767
{
641768
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
769+
checkElementAlignment(uncompressedLength, 8);
642770
double[] result = new double[uncompressedLength / 8];
643771
impl.rawUncompress(input, offset, length, result, 0);
644772
return result;
@@ -656,7 +784,7 @@ public static double[] uncompressDoubleArray(byte[] input, int offset, int lengt
656784
public static int uncompressedLength(byte[] input)
657785
throws IOException
658786
{
659-
return impl.uncompressedLength(input, 0, input.length);
787+
return uncompressedLength(input, 0, input.length);
660788
}
661789

662790
/**
@@ -676,8 +804,9 @@ public static int uncompressedLength(byte[] input, int offset, int length)
676804
if (input == null) {
677805
throw new NullPointerException("input is null");
678806
}
807+
checkArrayRange(input, offset, length, "input");
679808

680-
return impl.uncompressedLength(input, offset, length);
809+
return (int) checkUncompressedLength(impl.uncompressedLength(input, offset, length), length);
681810
}
682811

683812
/**
@@ -697,7 +826,9 @@ public static int uncompressedLength(ByteBuffer compressed)
697826
throw new SnappyError(SnappyErrorCode.NOT_A_DIRECT_BUFFER, "input is not a direct buffer");
698827
}
699828

700-
return impl.uncompressedLength(compressed, compressed.position(), compressed.remaining());
829+
return (int) checkUncompressedLength(
830+
impl.uncompressedLength(compressed, compressed.position(), compressed.remaining()),
831+
compressed.remaining());
701832
}
702833

703834
/**
@@ -712,7 +843,7 @@ public static int uncompressedLength(ByteBuffer compressed)
712843
public static long uncompressedLength(long inputAddr, long len)
713844
throws IOException
714845
{
715-
return impl.uncompressedLength(inputAddr, len);
846+
return checkUncompressedLength(impl.uncompressedLength(inputAddr, len), len);
716847
}
717848

718849
/**
@@ -741,6 +872,7 @@ public static float[] uncompressFloatArray(byte[] input, int offset, int length)
741872
throws IOException
742873
{
743874
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
875+
checkElementAlignment(uncompressedLength, 4);
744876
float[] result = new float[uncompressedLength / 4];
745877
impl.rawUncompress(input, offset, length, result, 0);
746878
return result;
@@ -772,6 +904,7 @@ public static int[] uncompressIntArray(byte[] input, int offset, int length)
772904
throws IOException
773905
{
774906
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
907+
checkElementAlignment(uncompressedLength, 4);
775908
int[] result = new int[uncompressedLength / 4];
776909
impl.rawUncompress(input, offset, length, result, 0);
777910
return result;
@@ -803,6 +936,7 @@ public static long[] uncompressLongArray(byte[] input, int offset, int length)
803936
throws IOException
804937
{
805938
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
939+
checkElementAlignment(uncompressedLength, 8);
806940
long[] result = new long[uncompressedLength / 8];
807941
impl.rawUncompress(input, offset, length, result, 0);
808942
return result;
@@ -834,6 +968,7 @@ public static short[] uncompressShortArray(byte[] input, int offset, int length)
834968
throws IOException
835969
{
836970
int uncompressedLength = Snappy.uncompressedLength(input, offset, length);
971+
checkElementAlignment(uncompressedLength, 2);
837972
short[] result = new short[uncompressedLength / 2];
838973
impl.rawUncompress(input, offset, length, result, 0);
839974
return result;

‎src/main/java/org/xerial/snappy/SnappyFramedInputStream.java‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -517,18 +517,22 @@ private boolean ensureBuffer()
517517
return false;
518518
}
519519

520-
if (!readBlockHeader()) {
521-
eof = true;
522-
return false;
523-
}
524-
525-
// get action based on header
526-
final FrameMetaData frameMetaData = getFrameMetaData(frameHeader);
520+
// Skip chunks in a loop rather than recursively: a zero-length skippable chunk is only 4 bytes, so
521+
// recursing per chunk lets a small input exhaust the stack
522+
FrameMetaData frameMetaData;
523+
while (true) {
524+
if (!readBlockHeader()) {
525+
eof = true;
526+
return false;
527+
}
527528

528-
if (FrameAction.SKIP == frameMetaData.frameAction) {
529+
// get action based on header
530+
frameMetaData = getFrameMetaData(frameHeader);
531+
if (FrameAction.SKIP != frameMetaData.frameAction) {
532+
break;
533+
}
529534
SnappyFramed.skip(rbc, frameMetaData.length,
530535
ByteBuffer.wrap(buffer));
531-
return ensureBuffer();
532536
}
533537

534538
if (frameMetaData.length > input.capacity()) {
@@ -550,6 +554,8 @@ private boolean ensureBuffer()
550554

551555
input.position(frameData.offset);
552556

557+
// uncompressedLength rejects lengths that cannot be produced from the frame size, so the buffers
558+
// allocated below stay proportional to the frame actually read
553559
final int uncompressedLength = Snappy.uncompressedLength(input);
554560

555561
if (uncompressedLength > uncompressedDirect.capacity()) {

0 commit comments

Comments
 (0)