This is an automated email from the ASF dual-hosted git repository.
JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 865d0526b1 [core] Validate deletion vector checksums (#10070)
865d0526b1 is described below
commit 865d0526b16bd6f7f47694277f1cbf61ad69f814
Author: Akash Reddy Jammula <[email protected]>
AuthorDate: Mon Sep 21 19:42:50 2026 -0700
[core] Validate deletion vector checksums (#10070)
---
.../paimon/deletionvectors/DeletionVector.java | 24 ++++---
.../deletionvectors/DeletionVectorChecksum.java | 52 +++++++++++++++
.../paimon/deletionvectors/DeletionVectorTest.java | 77 ++++++++++++++++++++++
.../DeletionVectorsIndexFileTest.java | 38 +++++++++++
4 files changed, 181 insertions(+), 10 deletions(-)
diff --git
a/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVector.java
b/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVector.java
index 3526ae2a1d..d3f7eed1ce 100644
---
a/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVector.java
+++
b/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVector.java
@@ -101,6 +101,14 @@ public interface DeletionVector extends
DeletionVectorJudger {
static DeletionVector read(DataInputStream dis, @Nullable Long length)
throws IOException {
// read bitmap length
int bitmapLength = dis.readInt();
+ if (bitmapLength < BitmapDeletionVector.MAGIC_NUMBER_SIZE_BYTES) {
+ throw new IOException(
+ "Invalid deletion vector bitmap length: "
+ + bitmapLength
+ + ", expected at least "
+ + BitmapDeletionVector.MAGIC_NUMBER_SIZE_BYTES);
+ }
+
// read magic number
int magicNumber = dis.readInt();
@@ -113,11 +121,9 @@ public interface DeletionVector extends
DeletionVectorJudger {
+ length);
}
- // magic number has been read
- byte[] bytes = new byte[bitmapLength -
BitmapDeletionVector.MAGIC_NUMBER_SIZE_BYTES];
- dis.readFully(bytes);
- dis.skipBytes(4); // skip crc
- return
BitmapDeletionVector.deserializeFromByteBuffer(ByteBuffer.wrap(bytes));
+ byte[] bitmapData =
+ DeletionVectorChecksum.readAndValidate(dis, bitmapLength,
magicNumber);
+ return
BitmapDeletionVector.deserializeFromByteBuffer(ByteBuffer.wrap(bitmapData));
} else if (toLittleEndianInt(magicNumber) ==
Bitmap64DeletionVector.MAGIC_NUMBER) {
if (length != null) {
long expectedBitmapLength =
@@ -133,11 +139,9 @@ public interface DeletionVector extends
DeletionVectorJudger {
}
}
- // magic number have been read
- byte[] bytes = new byte[bitmapLength -
Bitmap64DeletionVector.MAGIC_NUMBER_SIZE_BYTES];
- dis.readFully(bytes);
- dis.skipBytes(4); // skip crc
- return
Bitmap64DeletionVector.deserializeFromBitmapDataBytes(bytes);
+ byte[] bitmapData =
+ DeletionVectorChecksum.readAndValidate(dis, bitmapLength,
magicNumber);
+ return
Bitmap64DeletionVector.deserializeFromBitmapDataBytes(bitmapData);
} else {
throw new RuntimeException(
"Invalid magic number: "
diff --git
a/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorChecksum.java
b/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorChecksum.java
new file mode 100644
index 0000000000..a3d8584cdd
--- /dev/null
+++
b/paimon-core/src/main/java/org/apache/paimon/deletionvectors/DeletionVectorChecksum.java
@@ -0,0 +1,52 @@
+/*
+ * 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.paimon.deletionvectors;
+
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.zip.CRC32;
+
+/** Checksum validation for serialized deletion vectors. */
+class DeletionVectorChecksum {
+
+ static byte[] readAndValidate(DataInputStream in, int bitmapLength, int
magicNumber)
+ throws IOException {
+ CRC32 checksum = new CRC32();
+
checksum.update(ByteBuffer.allocate(Integer.BYTES).putInt(magicNumber).array());
+
+ byte[] bitmapData = new byte[bitmapLength -
BitmapDeletionVector.MAGIC_NUMBER_SIZE_BYTES];
+ in.readFully(bitmapData);
+ checksum.update(bitmapData);
+
+ int expectedChecksum = in.readInt();
+ int actualChecksum = (int) checksum.getValue();
+ if (expectedChecksum != actualChecksum) {
+ throw new IOException(
+ "Invalid deletion vector checksum. Expected "
+ + Integer.toUnsignedLong(expectedChecksum)
+ + " but computed "
+ + Integer.toUnsignedLong(actualChecksum)
+ + ".");
+ }
+ return bitmapData;
+ }
+
+ private DeletionVectorChecksum() {}
+}
diff --git
a/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorTest.java
b/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorTest.java
index a924edeb1f..3f1289116e 100644
---
a/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorTest.java
@@ -25,15 +25,22 @@ import org.apache.paimon.reader.FileRecordIterator;
import org.apache.paimon.reader.FileRecordReader;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
import javax.annotation.Nullable;
+import java.io.ByteArrayInputStream;
+import java.io.DataInputStream;
import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** Test for {@link DeletionVector}. */
public class DeletionVectorTest {
@@ -178,6 +185,58 @@ public class DeletionVectorTest {
}
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testRejectCorruptedChecksum(boolean bitmap64) {
+ byte[] serialized = serializeDeletionVector(bitmap64);
+ serialized[serialized.length - 1] ^= 1;
+
+ assertThatThrownBy(
+ () ->
+ readDeletionVector(
+ serialized,
serializedLength(serialized, bitmap64)))
+ .isInstanceOf(IOException.class)
+ .hasMessageContaining("Invalid deletion vector checksum");
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testRejectCorruptedPayload(boolean bitmap64) {
+ byte[] serialized = serializeDeletionVector(bitmap64);
+ serialized[
+ Bitmap64DeletionVector.LENGTH_SIZE_BYTES
+ +
Bitmap64DeletionVector.MAGIC_NUMBER_SIZE_BYTES] ^=
+ 1;
+
+ assertThatThrownBy(
+ () ->
+ readDeletionVector(
+ serialized,
serializedLength(serialized, bitmap64)))
+ .isInstanceOf(IOException.class)
+ .hasMessageContaining("Invalid deletion vector checksum");
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testRejectTruncatedChecksum(boolean bitmap64) {
+ byte[] serialized = serializeDeletionVector(bitmap64);
+ long length = serializedLength(serialized, bitmap64);
+ byte[] truncated = Arrays.copyOf(serialized, serialized.length - 1);
+
+ assertThatThrownBy(() -> readDeletionVector(truncated, length))
+ .isInstanceOf(IOException.class);
+ }
+
+ @Test
+ public void testRejectInvalidBitmapLength() {
+ byte[] serialized = serializeDeletionVector(false);
+
ByteBuffer.wrap(serialized).putInt(BitmapDeletionVector.MAGIC_NUMBER_SIZE_BYTES
- 1);
+
+ assertThatThrownBy(() -> readDeletionVector(serialized, null))
+ .isInstanceOf(IOException.class)
+ .hasMessageContaining("Invalid deletion vector bitmap length");
+ }
+
@Test
public void testBitmapDeletionVectorTo64() {
HashSet<Integer> toDelete = new HashSet<>();
@@ -214,6 +273,24 @@ public class DeletionVectorTest {
}
}
+ private static byte[] serializeDeletionVector(boolean bitmap64) {
+ DeletionVector deletionVector =
+ bitmap64 ? new Bitmap64DeletionVector() : new
BitmapDeletionVector();
+ deletionVector.delete(1);
+ deletionVector.delete(10);
+ return DeletionVector.serializeToBytes(deletionVector);
+ }
+
+ private static long serializedLength(byte[] serialized, boolean bitmap64) {
+ return bitmap64 ? serialized.length :
ByteBuffer.wrap(serialized).getInt();
+ }
+
+ private static DeletionVector readDeletionVector(byte[] serialized,
@Nullable Long length)
+ throws IOException {
+ return DeletionVector.read(
+ new DataInputStream(new ByteArrayInputStream(serialized)),
length);
+ }
+
private static class TestingFileRecordIterator implements
FileRecordIterator<InternalRow> {
private final int rows;
diff --git
a/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java
b/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java
index 9a243bf8bf..c1a8e2a0fe 100644
---
a/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/deletionvectors/DeletionVectorsIndexFileTest.java
@@ -34,6 +34,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -46,6 +47,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
/** Test for {@link DeletionVectorsIndexFile}. */
public class DeletionVectorsIndexFileTest {
@@ -350,6 +352,42 @@ public class DeletionVectorsIndexFileTest {
assertThat(dv.isDeleted(100)).isTrue();
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ public void testRejectCorruptedDeletionVectorFromIndexFile(boolean
bitmap64)
+ throws IOException {
+ IndexPathFactory pathFactory = getPathFactory();
+ DeletionVectorsIndexFile deletionVectorsIndexFile =
+ deletionVectorsIndexFile(pathFactory, bitmap64);
+
+ DeletionVector deletionVector = createEmptyDV(bitmap64);
+ deletionVector.delete(1);
+ IndexFileMeta indexFileMeta =
+ deletionVectorsIndexFile
+
.writeWithRolling(Collections.singletonMap("file.parquet", deletionVector))
+ .get(0);
+ DeletionVectorMeta deletionVectorMeta =
indexFileMeta.dvRanges().get("file.parquet");
+ Path indexPath = pathFactory.toPath(indexFileMeta);
+ java.nio.file.Path localIndexPath =
java.nio.file.Paths.get(indexPath.toUri());
+ byte[] bytes = Files.readAllBytes(localIndexPath);
+ int checksumEnd =
+ deletionVectorMeta.offset()
+ + deletionVectorMeta.length()
+ + (bitmap64 ? 0 : Integer.BYTES * 2);
+ bytes[checksumEnd - 1] ^= 1;
+ Files.write(localIndexPath, bytes);
+
+ DeletionFile deletionFile =
+ new DeletionFile(
+ indexPath.toString(),
+ deletionVectorMeta.offset(),
+ deletionVectorMeta.length(),
+ deletionVectorMeta.cardinality());
+ assertThatThrownBy(() -> DeletionVector.read(LocalFileIO.create(),
deletionFile))
+ .isInstanceOf(IOException.class)
+ .hasMessageContaining("Invalid deletion vector checksum");
+ }
+
@Test
public void testReadOldDeletionVector32Bit() throws IOException {
try (InputStream inputStream =