Skip to content

Apache Lucene open-source search software

Avoid overflow in index input slices invariant checks (#14126)

Chris Hegarty7e20d5b3aad1
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index c2b37a94f89..39d7403a11c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -84,6 +84,9 @@ Bug Fixes
* GITHUB#14123: SortingCodecReader NPE when segment has no (points, vectors, etc...) (Mike Sokolov)
+* GITHUB#14126: Avoid overflow in index input slices invariant checks
+ (Chris Hegarty)
+
Other
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java b/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java
index 1738259fa2f..cd47500a2df 100644
--- a/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/BufferedIndexInput.java
@@ -401,7 +401,7 @@ public abstract class BufferedIndexInput extends IndexInput implements RandomAcc
? base.toString()
: (base.toString() + " [slice=" + sliceDescription + "]"),
BufferedIndexInput.BUFFER_SIZE);
- if (offset < 0 || length < 0 || offset + length > base.length()) {
+ if ((length | offset) < 0 || length > base.length() - offset) {
throw new IllegalArgumentException(
"slice() " + sliceDescription + " out of bounds: " + base);
}
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java
index 39e92061620..dee5c8e3a73 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataInput.java
@@ -424,7 +424,7 @@ public final class ByteBuffersDataInput extends DataInput
}
public ByteBuffersDataInput slice(long offset, long length) {
- if (offset < 0 || length < 0 || offset + length > this.length) {
+ if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
String.format(
Locale.ROOT,
diff --git a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
index c9c92db91b4..b05652789cf 100644
--- a/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/NIOFSDirectory.java
@@ -139,7 +139,7 @@ public class NIOFSDirectory extends FSDirectory {
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- if (offset < 0 || length < 0 || offset + length > this.length()) {
+ if ((length | offset) < 0 || length > this.length() - offset) {
throw new IllegalArgumentException(
"slice() "
+ sliceDescription
diff --git a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java
index 74594be5ec9..800a66a2167 100644
--- a/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java
+++ b/lucene/core/src/java21/org/apache/lucene/store/MemorySegmentIndexInput.java
@@ -597,7 +597,7 @@ abstract class MemorySegmentIndexInput extends IndexInput
*/
@Override
public final MemorySegmentIndexInput slice(String sliceDescription, long offset, long length) {
- if (offset < 0 || length < 0 || offset + length > this.length) {
+ if ((length | offset) < 0 || length > this.length - offset) {
throw new IllegalArgumentException(
"slice() "
+ sliceDescription
diff --git a/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java b/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java
index 420d6d40d6d..cd90db9abbe 100644
--- a/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java
+++ b/lucene/misc/src/java/org/apache/lucene/misc/store/RAFDirectory.java
@@ -136,7 +136,7 @@ public class RAFDirectory extends FSDirectory {
@Override
public IndexInput slice(String sliceDescription, long offset, long length) throws IOException {
- if (offset < 0 || length < 0 || offset + length > this.length()) {
+ if ((length | offset) < 0 || length > this.length() - offset) {
throw new IllegalArgumentException(
"slice() " + sliceDescription + " out of bounds: " + this);
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java
index 6defa5eb8c7..88223a4abeb 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/BaseDirectoryTestCase.java
@@ -771,6 +771,12 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {
slice.slice("slice3sub", 1, len / 2);
});
+ expectThrows(
+ IllegalArgumentException.class,
+ () -> {
+ i.slice("slice4", Long.MAX_VALUE - 1, 10);
+ });
+
i.close();
}
}
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
index 1b4234c3d79..4d3c233257c 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/store/SerialIOCountingDirectory.java
@@ -194,7 +194,7 @@ public class SerialIOCountingDirectory extends FilterDirectory {
public IndexInput slice(
String sliceDescription, long offset, long length, ReadAdvice readAdvice)
throws IOException {
- if (offset < 0 || offset + length > sliceLength) {
+ if ((length | offset) < 0 || length > sliceLength - offset) {
throw new IllegalArgumentException();
}
IndexInput clone = in.clone();