Skip to content
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

ARROW-11559: [C++] Use smarter Flatbuffers verification parameters #9447

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cpp/src/arrow/ipc/metadata_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,22 @@ Status GetSparseTensorMetadata(const Buffer& metadata, std::shared_ptr<DataType>
Status GetKeyValueMetadata(const KVVector* fb_metadata,
std::shared_ptr<KeyValueMetadata>* out);

template <typename RootType>
bool VerifyFlatbuffers(const uint8_t* data, int64_t size) {
// Heuristic: tables in a Arrow flatbuffers buffer must take at least 1 bit
// each in average (ARROW-11559).
// Especially, the only recursive table (the `Field` table in Schema.fbs)
// must have a non-empty `type` member.
flatbuffers::Verifier verifier(
data, static_cast<size_t>(size),
/*max_depth=*/128,
/*max_tables=*/static_cast<flatbuffers::uoffset_t>(8 * size));
return verifier.VerifyBuffer<RootType>(nullptr);
}

static inline Status VerifyMessage(const uint8_t* data, int64_t size,
const flatbuf::Message** out) {
flatbuffers::Verifier verifier(data, size, /*max_depth=*/128);
if (!flatbuf::VerifyMessageBuffer(verifier)) {
if (!VerifyFlatbuffers<flatbuf::Message>(data, size)) {
return Status::IOError("Invalid flatbuffers message.");
}
*out = flatbuf::GetMessage(data);
Expand Down
8 changes: 3 additions & 5 deletions cpp/src/arrow/ipc/reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1052,11 +1052,9 @@ class RecordBatchFileReaderImpl : public RecordBatchFileReader {
footer_buffer_,
file_->ReadAt(footer_offset_ - footer_length - file_end_size, footer_length));

auto data = footer_buffer_->data();
flatbuffers::Verifier verifier(data, footer_buffer_->size(), /*max_depth=*/128,
/*max_tables=*/UINT_MAX);

if (!flatbuf::VerifyFooterBuffer(verifier)) {
const auto data = footer_buffer_->data();
const auto size = footer_buffer_->size();
if (!internal::VerifyFlatbuffers<flatbuf::Footer>(data, size)) {
return Status::IOError("Verification of flatbuffer-encoded Footer failed.");
}
footer_ = flatbuf::GetFooter(data);
Expand Down