This is an automated email from the ASF dual-hosted git repository.

yiguolei pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git


The following commit(s) were added to refs/heads/master by this push:
     new c9811d5c1fc [fix](load) Validate Arrow IPC buffer ranges before 
decoding (#71142)
c9811d5c1fc is described below

commit c9811d5c1fc4300bff145ff8d4d3156750bfb543
Author: Xin Liao <[email protected]>
AuthorDate: Thu Dec 3 15:33:11 2026 +0800

    [fix](load) Validate Arrow IPC buffer ranges before decoding (#71142)
    
    ### Background
    
    Arrow Stream Load reads IPC messages from the load pipe and converts
    decoded record batches into Doris columns.
    
    ### What problem does this PR solve?
    
    Issue Number: None
    Related PR: None
    PR Type: Bugfix
    
    Problem Summary:
    
    Malformed Arrow IPC input can reach array decoding or conversion before
    its buffer ranges and array layout are fully checked. Stream Load now
    rejects invalid ranges before decoding and checks the resulting batch
    before column conversion.
    
    ### Details
    
    - **Frequency:** Triggered by input that fails IPC buffer range or batch
    validation. Production frequency is unknown.
    - **Root cause:** The IPC stream reader can return short buffer slices,
    and the lightweight batch validator checks only the endpoints of
    variable-width offsets.
    - **Why existing tests missed it:**
    `LoadArrowPipeTest.MalformedIpcReturnsAnError` checks invalid framing.
    `test_arrow_stream_load_type_validation` checks type conversions and
    uses structurally valid IPC fixtures.
    - **Fix:** Validate record and dictionary buffer ranges against the
    message body, then fully validate decoded record batches before
    conversion.
    - **Other paths and branches:** The patch covers the BE Arrow Stream
    Load reader on master. Release branches were not verified.
    
    ### Testing
    
    Before rebasing, a local Arrow 24 ASAN/UBSAN smoke check passed with
    compressed and uncompressed streams, dictionary and binary arrays, and
    an empty batch. The local BE unit-test build could not link due to
    missing third-party libraries. The Doris end-to-end regression was not
    run.
    
    ### Release note
    
    Arrow Stream Load rejects invalid IPC buffer ranges and malformed record
    batches.
    
    ### Check List (For Author)
    
    - Behavior changed: No
    - Does this need documentation: No
    - Pre-submission status: Partially verified: Doris BE unit and
    end-to-end tests did not run locally.
    
    ### AI Disclosure
    
    - Tool: Codex
    - Generated: The fix, the Arrow smoke check, and the PR description.
    - Human review: The author identified the decoding path and requested
    IPC buffer validation.
---
 be/src/format_v2/arrow/arrow_stream_reader.cpp | 78 ++++++++++++++++++++++++--
 be/test/format_v2/table/load_reader_test.cpp   | 37 ++++++++++++
 2 files changed, 110 insertions(+), 5 deletions(-)

diff --git a/be/src/format_v2/arrow/arrow_stream_reader.cpp 
b/be/src/format_v2/arrow/arrow_stream_reader.cpp
index ff757d99d15..036e9e2a95c 100644
--- a/be/src/format_v2/arrow/arrow_stream_reader.cpp
+++ b/be/src/format_v2/arrow/arrow_stream_reader.cpp
@@ -22,9 +22,13 @@
 #include <arrow/builder.h>
 #include <arrow/compute/api.h>
 #include <arrow/io/interfaces.h>
+#include <arrow/ipc/message.h>
 #include <arrow/ipc/reader.h>
 #include <arrow/record_batch.h>
 #include <arrow/type.h>
+#include <flatbuffers/array.h>
+#include <flatbuffers/table.h>
+#include <flatbuffers/vector.h>
 
 #include <utility>
 
@@ -51,6 +55,66 @@ namespace doris::format::arrow_stream {
 
 namespace {
 
+class ValidatingIpcMessageReader final : public arrow::ipc::MessageReader {
+public:
+    explicit ValidatingIpcMessageReader(const 
std::shared_ptr<arrow::io::InputStream>& input)
+            : _reader(arrow::ipc::MessageReader::Open(input)) {}
+
+    arrow::Result<std::unique_ptr<arrow::ipc::Message>> ReadNextMessage() 
override {
+        ARROW_ASSIGN_OR_RAISE(auto message, _reader->ReadNextMessage());
+        if (message && (message->type() == 
arrow::ipc::MessageType::RECORD_BATCH ||
+                        message->type() == 
arrow::ipc::MessageType::DICTIONARY_BATCH)) {
+            ARROW_RETURN_NOT_OK(validate_buffer_ranges(*message));
+        }
+        return message;
+    }
+
+private:
+    static arrow::Status validate_buffer_ranges(const arrow::ipc::Message& 
message) {
+        // MessageReader has verified the FlatBuffer. Arrow does not install 
its generated
+        // IPC headers. These are the vtable offsets of DictionaryBatch.data 
and
+        // RecordBatch.buffers in Arrow's IPC schema. A Buffer is two 
consecutive int64s.
+        constexpr flatbuffers::voffset_t dictionary_data = 6;
+        constexpr flatbuffers::voffset_t record_batch_buffers = 8;
+        using BufferRange = flatbuffers::Array<int64_t, 2>;
+        using Buffers = flatbuffers::Vector<const BufferRange*>;
+
+        const auto* batch = static_cast<const 
flatbuffers::Table*>(message.header());
+        // FlatBuffers permits a missing union payload or an empty dictionary 
table.
+        if (batch == nullptr) {
+            return arrow::Status::Invalid("Arrow IPC message is missing its 
batch header");
+        }
+        if (message.type() == arrow::ipc::MessageType::DICTIONARY_BATCH) {
+            batch = batch->GetPointer<const 
flatbuffers::Table*>(dictionary_data);
+            if (batch == nullptr) {
+                return arrow::Status::Invalid("Arrow IPC dictionary is missing 
its record batch");
+            }
+        }
+        const auto* buffers = batch->GetPointer<const 
Buffers*>(record_batch_buffers);
+        // A batch can omit the vector when its schema needs no buffers.
+        if (buffers == nullptr) {
+            return arrow::Status::OK();
+        }
+
+        // BufferReader can return a short slice. Reject invalid ranges before 
Arrow reads,
+        // decompresses or interprets the slices. Subtraction avoids signed 
overflow.
+        const int64_t body_size = message.body()->size();
+        for (flatbuffers::uoffset_t i = 0; i < buffers->size(); ++i) {
+            const auto* buffer = buffers->Get(i);
+            const int64_t offset = buffer->Get(0);
+            const int64_t length = buffer->Get(1);
+            if (offset < 0 || length < 0 || offset > body_size || length > 
body_size - offset) {
+                return arrow::Status::Invalid("Arrow IPC buffer ", i,
+                                              " exceeds the message body 
(offset=", offset,
+                                              ", length=", length, ", body 
size=", body_size, ")");
+            }
+        }
+        return arrow::Status::OK();
+    }
+
+    std::unique_ptr<arrow::ipc::MessageReader> _reader;
+};
+
 class ArrowLoadSchemaConvertor final : public DorisArrowSchemaConvertor {
 public:
     using DorisArrowSchemaConvertor::DorisArrowSchemaConvertor;
@@ -408,8 +472,9 @@ Status ArrowStreamReader::_open_next_stream() {
     options.memory_pool = _arrow_pool.get();
     // Keep IPC decompression on the scanner thread, where the load memory 
tracker is attached.
     options.use_threads = false;
-    RETURN_DORIS_STATUS_IF_RESULT_ERROR(_ipc_reader,
-                                        
arrow::ipc::RecordBatchStreamReader::Open(_input, options));
+    RETURN_DORIS_STATUS_IF_RESULT_ERROR(
+            _ipc_reader, arrow::ipc::RecordBatchStreamReader::Open(
+                                 
std::make_unique<ValidatingIpcMessageReader>(_input), options));
     _arrow_positions.assign(_file_slots.size(), -1);
     for (const auto& [id, position] : _request->local_positions) {
         const auto& name = _file_slots.at(id.value())->col_name();
@@ -489,12 +554,15 @@ Status ArrowStreamReader::_next_batch() {
                 return Status::OK();
             }
         }
-        RETURN_DORIS_STATUS_IF_ERROR(_ipc_reader->ReadNext(&_batch));
-        if (!_batch) {
+        std::shared_ptr<arrow::RecordBatch> batch;
+        RETURN_DORIS_STATUS_IF_ERROR(_ipc_reader->ReadNext(&batch));
+        if (!batch) {
             _ipc_reader.reset();
             continue;
         }
-        RETURN_DORIS_STATUS_IF_ERROR(_batch->Validate());
+        // Validate every array before normalization or materialization reads 
its buffers.
+        RETURN_DORIS_STATUS_IF_ERROR(batch->ValidateFull());
+        _batch = std::move(batch);
         if (_batch->num_rows() != 0) {
             RETURN_IF_ERROR(_prepare_arrays());
         }
diff --git a/be/test/format_v2/table/load_reader_test.cpp 
b/be/test/format_v2/table/load_reader_test.cpp
index f29c3f0dfb7..0ef9db155ed 100644
--- a/be/test/format_v2/table/load_reader_test.cpp
+++ b/be/test/format_v2/table/load_reader_test.cpp
@@ -1261,6 +1261,43 @@ TEST_F(LoadArrowPipeTest, 
Date64RetainsDorisRangeValidation) {
     ASSERT_TRUE(reader.close().ok());
 }
 
+TEST_F(LoadArrowPipeTest, RejectsInvalidDate64InNullList) {
+    set_column_type(make_nullable(
+            
std::make_shared<DataTypeArray>(make_nullable(std::make_shared<DataTypeDateV2>()))));
+
+    // Full Arrow validation checks the DATE64 child before Doris masks the 
NULL list.
+    arrow::Date64Builder dates;
+    ASSERT_TRUE(dates.AppendValues({1, 86400000}).ok());
+    auto values = dates.Finish();
+    ASSERT_TRUE(values.ok());
+    arrow::Int32Builder offsets;
+    ASSERT_TRUE(offsets.AppendValues({0, 1, 2}).ok());
+    auto positions = offsets.Finish();
+    ASSERT_TRUE(positions.ok());
+    arrow::BooleanBuilder validity;
+    ASSERT_TRUE(validity.AppendValues(std::vector<bool> {false, true}).ok());
+    auto valid = validity.Finish();
+    ASSERT_TRUE(valid.ok());
+    auto lists = arrow::ListArray::FromArrays(**positions, **values, 
arrow::default_memory_pool(),
+                                              (*valid)->data()->buffers[1], 1);
+    ASSERT_TRUE(lists.ok());
+    append_batch(arrow::RecordBatch::Make(
+            arrow::schema({arrow::field("raw_value", (*lists)->type())}), 2, 
{*lists}));
+    ASSERT_TRUE(_pipe->finish().ok());
+
+    LoadTableReader reader(source.get(), destination.get());
+    ASSERT_TRUE(prepare(reader, FileFormat::ARROW).ok());
+    Block output(destination->slots(), 0);
+    bool eos = false;
+    const auto status = reader.get_block(&output, &eos);
+    ASSERT_FALSE(status.ok());
+    EXPECT_NE(status.to_string().find("does not represent a whole number of 
days"),
+              std::string::npos)
+            << status;
+    EXPECT_EQ(output.rows(), 0);
+    ASSERT_TRUE(reader.close().ok());
+}
+
 TEST_F(LoadArrowPipeTest, UInt32Ipv4PreservesTheUnsignedAddressBits) {
     set_column_type(make_nullable(std::make_shared<DataTypeIPv4>()));
     arrow::UInt32Builder builder;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to