Apache Lucene open-source search software
Reject LZ4 match offsets that point before the start of the output (#16478)
Serhiy Bzhezytskyy2a38fffef52a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 5ed6c32c88e..e016c993584 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -589,6 +589,10 @@ Bug Fixeswhich made the join set degenerate to the whole graph on hub-and-spoke shaped HNSW graphs.(Mayya Sharipova)+* GITHUB#16478: Reject LZ4 match offsets that point before the start of the output in+ LZ4#decompress. Corrupt input previously threw ArrayIndexOutOfBoundsException instead of+ a checked IOException. (Serhiy Bzhezytskyy)+* GITHUB#16350: Disable bulk-scoring in monitor queries. (Alan Woodward)* GITHUB#16378: Accumulate join Total/Avg scores in double precision indiff --git a/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java b/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.javaindex df9c5e6fc25..ae9f126b8a2 100644--- a/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java+++ b/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java@@ -114,6 +114,11 @@ public final class LZ4 {if (matchDec == 0) {throw new IOException("offset 0 is invalid");}+ if (matchDec > dOff) {+ // The match would reference bytes before dest[0], which this call never wrote.+ throw new IOException(+ "match offset " + matchDec + " is invalid, only " + dOff + " bytes are available");+ }int matchLen = token & 0x0F;if (matchLen == 0x0F) {diff --git a/lucene/core/src/test/org/apache/lucene/util/compress/TestDecompressLZ4.java b/lucene/core/src/test/org/apache/lucene/util/compress/TestDecompressLZ4.javaindex 9e4aeff5002..8cd63baf1b7 100644--- a/lucene/core/src/test/org/apache/lucene/util/compress/TestDecompressLZ4.java+++ b/lucene/core/src/test/org/apache/lucene/util/compress/TestDecompressLZ4.java@@ -50,4 +50,70 @@ public class TestDecompressLZ4 extends LuceneTestCase {() -> LZ4.decompress(new ByteArrayDataInput(input), output.length, output, 0));assertEquals("offset 0 is invalid", e.getMessage());}++ public void testDecompressOffsetBeyondOutput() {+ // A match offset must not point before the start of the output, otherwise the match would+ // reference bytes that this call never decompressed.+ byte[] input =+ new byte[] {+ // token: 0 literals, match length 4 (MIN_MATCH)+ 0x0,+ // offset 8, which is greater than the 0 bytes decompressed so far+ 8,+ 0,+ // last literals+ // token+ 7 << 4,+ // literals+ 0,+ 0,+ 0,+ 0,+ 0,+ 0,+ 0+ };++ byte[] output = new byte[18];++ var e =+ assertThrows(+ IOException.class,+ () -> LZ4.decompress(new ByteArrayDataInput(input), output.length, output, 0));+ assertEquals("match offset 8 is invalid, only 0 bytes are available", e.getMessage());+ }++ public void testDecompressOffsetBeyondOutputWithDictionary() {+ // With a preset dictionary the match offset may reach into the dictionary that the caller+ // placed in dest[dOff-dictLen:dOff], so the bound is dOff, not the number of bytes that this+ // call decompressed.+ byte[] input =+ new byte[] {+ // token: 0 literals, match length 4 (MIN_MATCH)+ 0x0,+ // offset 5, one byte past the 4-byte dictionary+ 5,+ 0,+ // last literals+ // token+ 7 << 4,+ // literals+ 0,+ 0,+ 0,+ 0,+ 0,+ 0,+ 0+ };++ byte[] output = new byte[22];+ final int dictLen = 4;++ var e =+ assertThrows(+ IOException.class,+ () -> LZ4.decompress(new ByteArrayDataInput(input), 18, output, dictLen));+ assertEquals("match offset 5 is invalid, only 4 bytes are available", e.getMessage());+ }}