Repository navigation
fix: validate V1 header count and value offsets in GenericIndexed #20447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
|
|
||
| package org.apache.druid.segment.data; | ||
|
|
||
| import com.google.common.base.Preconditions; | ||
| import com.google.common.primitives.Ints; | ||
| import org.apache.druid.collections.ResourceHolder; | ||
| import org.apache.druid.common.utils.SerializerUtils; | ||
|
|
@@ -282,9 +283,16 @@ private static final class V1<T> extends GenericIndexed<T> | |
| ) | ||
| { | ||
| super(strategy, allowReverseLookup, buffer.getInt()); | ||
| Preconditions.checkArgument(size >= 0, "size[%s] must be non-negative", size); | ||
| this.theBuffer = buffer; | ||
| this.headerOffset = theBuffer.position(); | ||
| this.valuesOffset = theBuffer.position() + size * Integer.BYTES; | ||
| final long valuesOffsetLong = (long) headerOffset + (long) size * Integer.BYTES; | ||
| Preconditions.checkArgument( | ||
| valuesOffsetLong <= theBuffer.limit(), | ||
| "size[%s] exceeds the available buffer", | ||
| size | ||
| ); | ||
| this.valuesOffset = (int) valuesOffsetLong; | ||
| } | ||
|
|
||
| @Nullable | ||
|
|
@@ -304,13 +312,15 @@ public T get(int index) | |
| startOffset = theBuffer.getInt(headerOffset + headerPosition) + Integer.BYTES; | ||
| endOffset = theBuffer.getInt(headerOffset + headerPosition + Integer.BYTES); | ||
| } | ||
| validateRelativeOffsets(index != 0, startOffset, endOffset); | ||
| return copyBufferAndGet(theBuffer, valuesOffset + startOffset, valuesOffset + endOffset); | ||
| } | ||
|
|
||
| @Override | ||
| public BufferIndexed singleThreaded() | ||
| { | ||
| final ByteBuffer copyBuffer = theBuffer.asReadOnlyBuffer(); | ||
| final int bufferLimit = copyBuffer.limit(); | ||
| return new BufferIndexed() | ||
| { | ||
| @Nullable | ||
|
|
@@ -330,7 +340,8 @@ protected ByteBuffer getByteBuffer(final int index) | |
| startOffset = theBuffer.getInt(headerOffset + headerPosition) + Integer.BYTES; | ||
| endOffset = theBuffer.getInt(headerOffset + headerPosition + Integer.BYTES); | ||
| } | ||
| return bufferedIndexedGetByteBuffer(copyBuffer, valuesOffset + startOffset, valuesOffset + endOffset); | ||
| validateRelativeOffsets(index != 0, startOffset, endOffset); | ||
| return bufferedIndexedGetByteBuffer(copyBuffer, valuesOffset + startOffset, valuesOffset + endOffset, bufferLimit); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -407,6 +418,7 @@ public T get(int index) | |
| startOffset = headerBuffer.getInt(headerPosition) + Integer.BYTES; | ||
| endOffset = headerBuffer.getInt(headerPosition + Integer.BYTES); | ||
| } | ||
| validateRelativeOffsets(relativePositionOfIndex != 0, startOffset, endOffset); | ||
| int fileNum = index >> logBaseTwoOfElementsPerValueFile; | ||
| return copyBufferAndGet(valueBuffers[fileNum], startOffset, endOffset); | ||
| } | ||
|
|
@@ -415,8 +427,10 @@ public T get(int index) | |
| public BufferIndexed singleThreaded() | ||
| { | ||
| final ByteBuffer[] copyValueBuffers = new ByteBuffer[valueBuffers.length]; | ||
| final int[] valueBufferLimits = new int[valueBuffers.length]; | ||
| for (int i = 0; i < valueBuffers.length; i++) { | ||
| copyValueBuffers[i] = valueBuffers[i].asReadOnlyBuffer(); | ||
| valueBufferLimits[i] = copyValueBuffers[i].limit(); | ||
| } | ||
|
|
||
| return new BufferIndexed() | ||
|
|
@@ -440,8 +454,9 @@ protected ByteBuffer getByteBuffer(int index) | |
| startOffset = headerBuffer.getInt(headerPosition) + Integer.BYTES; | ||
| endOffset = headerBuffer.getInt(headerPosition + Integer.BYTES); | ||
| } | ||
| validateRelativeOffsets(relativePositionOfIndex != 0, startOffset, endOffset); | ||
| int fileNum = index >> logBaseTwoOfElementsPerValueFile; | ||
| return bufferedIndexedGetByteBuffer(copyValueBuffers[fileNum], startOffset, endOffset); | ||
| return bufferedIndexedGetByteBuffer(copyValueBuffers[fileNum], startOffset, endOffset, valueBufferLimits[fileNum]); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -559,11 +574,30 @@ public Iterator<T> iterator() | |
| return IndexedIterable.create(this).iterator(); | ||
| } | ||
|
|
||
| private static void validateRelativeOffsets(boolean hasPrevious, int startOffset, int endOffset) | ||
| { | ||
| if (endOffset < startOffset || (hasPrevious && startOffset - Integer.BYTES < Integer.BYTES)) { | ||
| throw new IAE( | ||
| "value offsets out of bounds: startOffset[%s], endOffset[%s]", | ||
| startOffset, | ||
| endOffset | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| protected T copyBufferAndGet(ByteBuffer valueBuffer, int startOffset, int endOffset) | ||
| { | ||
| ByteBuffer copyValueBuffer = valueBuffer.asReadOnlyBuffer(); | ||
| int size = endOffset - startOffset; | ||
| if (startOffset < Integer.BYTES || endOffset < startOffset || endOffset > copyValueBuffer.limit()) { | ||
| throw new IAE( | ||
| "value offsets out of bounds: startOffset[%s], endOffset[%s], limit[%s]", | ||
| startOffset, | ||
| endOffset, | ||
| copyValueBuffer.limit() | ||
| ); | ||
| } | ||
| if (size == 0 && (copyValueBuffer.get(startOffset - Integer.BYTES) == NULL_VALUE_SIZE_MARKER)) { | ||
| return null; | ||
| } | ||
|
|
@@ -599,9 +633,17 @@ public T get(final int index) | |
| } | ||
|
|
||
| @Nullable | ||
| ByteBuffer bufferedIndexedGetByteBuffer(ByteBuffer copyValueBuffer, int startOffset, int endOffset) | ||
| ByteBuffer bufferedIndexedGetByteBuffer(ByteBuffer copyValueBuffer, int startOffset, int endOffset, int bufferLimit) | ||
| { | ||
| int size = endOffset - startOffset; | ||
| if (startOffset < Integer.BYTES || endOffset < startOffset || endOffset > bufferLimit) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Restore the reusable limit before marker lookup Finding: This check uses the stable original limit, but the null/empty marker read still uses copyValueBuffer's current limit, which may have been narrowed by an earlier direct getByteBuffer call. A later in-range zero-length value can therefore throw IndexOutOfBoundsException instead of being returned or rejected descriptively. Suggestion: Read the marker through a view restored to bufferLimit before checking it, and add a successive lookup regression for null or empty values. |
||
| throw new IAE( | ||
| "value offsets out of bounds: startOffset[%s], endOffset[%s], limit[%s]", | ||
| startOffset, | ||
| endOffset, | ||
| bufferLimit | ||
| ); | ||
| } | ||
| if (size == 0 && (copyValueBuffer.get(startOffset - Integer.BYTES) == NULL_VALUE_SIZE_MARKER)) { | ||
| return null; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * 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.druid.segment.data; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.nio.ByteOrder; | ||
|
|
||
| public class GenericIndexedMalformedHeaderTest | ||
| { | ||
| private static ByteBuffer buildV1(int numElements, int[] offsets, byte[] values, int numBytesUsedOverride) | ||
| { | ||
| final int valuesBytes = values.length; | ||
| final int numBytesUsed = numBytesUsedOverride >= 0 ? numBytesUsedOverride : 8 + numElements * 4 + valuesBytes; | ||
| final ByteBuffer buffer = ByteBuffer.allocate(64).order(ByteOrder.BIG_ENDIAN); | ||
| buffer.put((byte) 0x1); // VERSION_ONE | ||
| buffer.put((byte) 0x0); // allowReverseLookup | ||
| buffer.putInt(numBytesUsed); | ||
| buffer.putInt(numElements); | ||
| for (int offset : offsets) { | ||
| buffer.putInt(offset); | ||
| } | ||
| buffer.put(values); | ||
| buffer.flip(); | ||
| buffer.limit(buffer.capacity()); | ||
| return buffer; | ||
| } | ||
|
|
||
| @Test | ||
| public void testNegativeCountRejected() | ||
| { | ||
| final IllegalArgumentException e = Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> GenericIndexed.read(buildV1(-5, new int[]{}, new byte[]{1, 2, 3}, 8), GenericIndexed.STRING_STRATEGY, null) | ||
| ); | ||
| Assertions.assertTrue(e.getMessage().contains("must be non-negative"), e.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testOverflowCountRejected() | ||
| { | ||
| final IllegalArgumentException e = Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> GenericIndexed.read(buildV1(0x40000001, new int[]{}, new byte[]{}, 8), GenericIndexed.STRING_STRATEGY, null) | ||
| ); | ||
| Assertions.assertTrue(e.getMessage().contains("exceeds the available buffer"), e.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testHugeOffsetRejected() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.read( | ||
| buildV1(2, new int[]{6, 1000}, new byte[]{0, 0, 0, 2, 'a', 'b', 0, 0, 0, 2, 'c', 'd'}, -1), | ||
| GenericIndexed.STRING_STRATEGY, | ||
| null | ||
| ); | ||
| Assertions.assertEquals("ab", indexed.get(0)); | ||
| final IllegalArgumentException e = Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> indexed.get(1) | ||
| ); | ||
| Assertions.assertTrue(e.getMessage().contains("out of bounds"), e.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNegativeOffsetRejected() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.read( | ||
| buildV1(2, new int[]{-5, 10}, new byte[]{0, 0, 0, 2, 'x', 'y', 0, 0, 0, 2, 'p', 'q'}, -1), | ||
| GenericIndexed.STRING_STRATEGY, | ||
| null | ||
| ); | ||
| final IllegalArgumentException e = Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> indexed.get(0) | ||
| ); | ||
| Assertions.assertTrue(e.getMessage().contains("out of bounds"), e.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testValidHeaderReads() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.read( | ||
| buildV1(1, new int[]{6}, new byte[]{0, 0, 0, 2, 'h', 'i'}, -1), | ||
| GenericIndexed.STRING_STRATEGY, | ||
| null | ||
| ); | ||
| Assertions.assertEquals(1, indexed.size()); | ||
| Assertions.assertEquals("hi", indexed.get(0)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleThreadedRightwardProbes() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.fromIterable( | ||
| java.util.Arrays.asList("a", "b", "c"), | ||
| GenericIndexed.STRING_STRATEGY | ||
| ); | ||
| GenericIndexed<String>.BufferIndexed bi = indexed.singleThreaded(); | ||
|
|
||
| Assertions.assertDoesNotThrow(() -> bi.getByteBuffer(0)); | ||
| // A rightward binary-search probe must not be rejected by the bounds check | ||
| // after an earlier probe narrowed the reused buffer's limit. | ||
| Assertions.assertDoesNotThrow(() -> bi.getByteBuffer(2)); | ||
| Assertions.assertDoesNotThrow(() -> bi.getByteBuffer(1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleThreadedSuccessiveLookups() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.fromIterable( | ||
| java.util.Arrays.asList("a", "b", "c"), | ||
| GenericIndexed.STRING_STRATEGY | ||
| ); | ||
| GenericIndexed<String>.BufferIndexed bi = indexed.singleThreaded(); | ||
|
|
||
| Assertions.assertDoesNotThrow(() -> bi.getByteBuffer(1)); | ||
| final ByteBuffer second = bi.getByteBuffer(2); | ||
| Assertions.assertEquals(1, second.remaining()); | ||
| Assertions.assertEquals((byte) 'c', second.get()); | ||
| final ByteBuffer first = bi.getByteBuffer(0); | ||
| Assertions.assertEquals(1, first.remaining()); | ||
| Assertions.assertEquals((byte) 'a', first.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testIntermediateOffsetGapRejected() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.read( | ||
| buildV1(2, new int[]{0, 10}, new byte[]{0, 0, 0, 2, 'x', 'y', 0, 0, 0, 2, 'p', 'q'}, -1), | ||
| GenericIndexed.STRING_STRATEGY, | ||
| null | ||
| ); | ||
| // offsets[0] == 0 leaves the first value without its four-byte size marker, | ||
| // so a direct get(1) must reject the header instead of decoding outside the element. | ||
| final IllegalArgumentException e = Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> indexed.get(1) | ||
| ); | ||
| Assertions.assertTrue(e.getMessage().contains("out of bounds"), e.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSingleThreadedIntermediateOffsetGapRejected() | ||
| { | ||
| GenericIndexed<String> indexed = GenericIndexed.read( | ||
| buildV1(2, new int[]{0, 10}, new byte[]{0, 0, 0, 2, 'x', 'y', 0, 0, 0, 2, 'p', 'q'}, -1), | ||
| GenericIndexed.STRING_STRATEGY, | ||
| null | ||
| ); | ||
| final IllegalArgumentException e = Assertions.assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> indexed.singleThreaded().getByteBuffer(1) | ||
| ); | ||
| Assertions.assertTrue(e.getMessage().contains("out of bounds"), e.getMessage()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P2] Validate relative header offsets before adding the V1 base
Finding: This check validates only the absolute range passed to it, but V1 adds valuesOffset to each header-derived offset before calling copyBufferAndGet. A malformed two-entry table such as offsets [0, 10] therefore lets a direct get(1) pass with startOffset[valuesOffset + 4] and endOffset[valuesOffset + 10], even though the prior end offset is below the mandatory four-byte value marker; the method then decodes bytes outside the declared element instead of rejecting the header. The same intermediate-offset gap exists in the shared V2 path, and the current tests only access index 0 for the negative-offset case, so malformed headers can still produce the silent misreads this PR is meant to eliminate.
Suggestion: Validate each header-relative end offset before converting it to an absolute position (or carry the value-region base into the validator), and add direct later-index tests for too-small or negative preceding offsets in both normal and single-threaded access paths.