Copilot commented on code in PR #55671:
URL: https://github.com/apache/arrow/pull/55671#discussion_r4628441520


##########
cpp/src/arrow/ipc/options.cc:
##########
@@ -26,6 +26,42 @@ IpcWriteOptions IpcWriteOptions::Defaults() { return 
IpcWriteOptions(); }
 
 IpcReadOptions IpcReadOptions::Defaults() { return IpcReadOptions(); }
 
+Status IpcReadOptions::Validate() const {
+  if (max_recursion_depth < 0) {
+    return Status::Invalid("max_recursion_depth must be non-negative, got ",
+                           max_recursion_depth);
+  }
+  if (max_metadata_length < -1) {
+    return Status::Invalid("max_metadata_length must be -1 or non-negative, 
got ",
+                           max_metadata_length);
+  }
+  if (max_body_length < -1) {
+    return Status::Invalid("max_body_length must be -1 or non-negative, got ",
+                           max_body_length);
+  }
+  if (max_decompression_ratio < -1) {
+    return Status::Invalid("max_decompression_ratio must be -1 or 
non-negative, got ",
+                           max_decompression_ratio);
+  }
+  return Status::OK();
+}
+
+Status IpcReadOptions::CheckMetadataLength(int64_t length, const char* prefix) 
const {
+  if (max_metadata_length >= 0 && length > max_metadata_length) {
+    return Status::Invalid(prefix, length, " exceeds maximum allowed metadata 
length ",
+                           max_metadata_length);
+  }
+  return Status::OK();
+}
+
+Status IpcReadOptions::CheckBodyLength(int64_t length) const {
+  if (max_body_length >= 0 && length > max_body_length) {
+    return Status::Invalid("IPC body length ", length,
+                           " exceeds maximum allowed body length ", 
max_body_length);
+  }
+  return Status::OK();
+}

Review Comment:
   `CheckBodyLength` only enforces the upper limit, so a negative `body_length` 
is accepted. The new public file-reading overloads call this helper before 
computing `metadata_length + body_length`; in particular, `ReadMessageAsync` 
can therefore perform a signed-underflowed/negative-size read for a 
caller-supplied negative length. Reject negative lengths here before applying 
the maximum.



##########
cpp/src/arrow/ipc/metadata_internal.cc:
##########
@@ -1290,6 +1290,7 @@ Result<std::shared_ptr<KeyValueMetadata>> 
GetKeyValueMetadata(
 
   metadata->reserve(fb_metadata->size());
   for (const auto pair : *fb_metadata) {
+    CHECK_FLATBUFFERS_NOT_NULL(pair, "custom_metadata.pair");

Review Comment:
   The new null checks run after `metadata->reserve(fb_metadata->size())`, so 
they do not prevent an attacker-controlled custom-metadata vector from causing 
a large allocation before any pair is inspected. A verified FlatBuffers buffer 
can still contain a very large number of tiny pairs, amplifying the untrusted 
metadata size into the `vector<shared_ptr<...>>` capacity. Please validate/cap 
the pair count against the available metadata budget or avoid this 
unconditional reserve.



##########
cpp/src/arrow/ipc/reader.cc:
##########
@@ -1038,6 +1073,10 @@ class StreamDecoderInternal : public 
MessageDecoderListener {
         swap_endian_(false) {}
 
   Status OnMessageDecoded(std::unique_ptr<Message> message) override {
+    if (ARROW_PREDICT_TRUE(message != nullptr)) {
+      RETURN_NOT_OK(options_.CheckMetadataLength(message->metadata()->size()));
+      RETURN_NOT_OK(options_.CheckBodyLength(message->body_length()));

Review Comment:
   These checks only cover messages delivered through `StreamDecoderInternal`. 
The public `ReadRecordBatch(const Buffer&, ..., options, file)` and 
`ReadRecordBatch(const Message&, ..., options)` entry points below call 
`ReadRecordBatchInternal` directly, which never checks the metadata size or 
advertised body length against these options. Thus a caller can configure 
`max_metadata_length`/`max_body_length` and still bypass both limits through 
the documented direct-reading APIs. Move the option validation and length 
checks into the shared `ReadRecordBatchInternal` path (using the FlatBuffers 
bodyLength) so all entry points enforce the same limits.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to