Skip to content

Apache Lucene open-source search software

Disallow invalid offset 0 for LZ4 decompression (#15570)

Marcono1234473dd7b58655
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 8cbc9023c2a..8c7663743b9 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -246,6 +246,8 @@ Improvements
* GITHUB#15479: Add NumericDocValueRangeQuery base class to add accessor methods for field, lower and upper values (Alan Woodward)
+* GITHUB#15570: Disallow invalid offset 0 for LZ4 decompression (Marcono1234)
+
Optimizations
---------------------
* GITHUB#15140: Optimize TopScoreDocCollector with TernaryLongHeap for improved performance over Binary-LongHeap. (Ramakrishna Chilaka)
diff --git a/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java b/lucene/core/src/java/org/apache/lucene/util/compress/LZ4.java
index 9033226a297..25a6f025dc5 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
@@ -111,7 +111,9 @@ public final class LZ4 {
// matchs
final int matchDec = compressed.readShort() & 0xFFFF;
- assert matchDec > 0;
+ if (matchDec == 0) {
+ throw new IOException("offset 0 is invalid");
+ }
int matchLen = token & 0x0F;
if (matchLen == 0x0F) {
diff --git a/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java b/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java
index ddb556822d4..a4b9fbb5524 100644
--- a/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java
+++ b/lucene/core/src/test/org/apache/lucene/util/compress/LZ4TestCase.java
@@ -143,7 +143,8 @@ public abstract class LZ4TestCase extends LuceneTestCase {
// Now restore and compare bytes
byte[] restored = new byte[length + random().nextInt(10)];
- LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, 0);
+ int restoreOffsetEnd = LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, 0);
+ assertEquals(length, restoreOffsetEnd);
assertArrayEquals(
ArrayUtil.copyOfSubArray(data, offset, offset + length),
ArrayUtil.copyOfSubArray(restored, 0, length));
@@ -151,7 +152,9 @@ public abstract class LZ4TestCase extends LuceneTestCase {
// Now restore with an offset
int restoreOffset = TestUtil.nextInt(random(), 1, 10);
restored = new byte[restoreOffset + length + random().nextInt(10)];
- LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, restoreOffset);
+ restoreOffsetEnd =
+ LZ4.decompress(new ByteArrayDataInput(compressed), length, restored, restoreOffset);
+ assertEquals(length, restoreOffsetEnd - restoreOffset);
assertArrayEquals(
ArrayUtil.copyOfSubArray(data, offset, offset + length),
ArrayUtil.copyOfSubArray(restored, restoreOffset, restoreOffset + length));
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.java
new file mode 100644
index 00000000000..9e4aeff5002
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/util/compress/TestDecompressLZ4.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.util.compress;
+
+import java.io.IOException;
+import org.apache.lucene.store.ByteArrayDataInput;
+import org.apache.lucene.tests.util.LuceneTestCase;
+
+public class TestDecompressLZ4 extends LuceneTestCase {
+ public void testDecompressOffset0() {
+ byte[] input =
+ new byte[] {
+ // token
+ 0xE,
+ // offset 0 (invalid)
+ 0,
+ 0,
+ // last literal
+ // token
+ 7 << 4,
+ // literal
+ 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("offset 0 is invalid", e.getMessage());
+ }
+}