-
Notifications
You must be signed in to change notification settings - Fork 4.1k
MINOR: [C++][Parquet] Reject too-short statistics values in FormatStatValue #50025
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: main
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 |
|---|---|---|
|
|
@@ -184,6 +184,43 @@ std::string FormatFloat16Value(::std::string_view val) { | |
|
|
||
| std::string FormatStatValue(Type::type parquet_type, ::std::string_view val, | ||
| const std::shared_ptr<const LogicalType>& logical_type) { | ||
| // Statistics blobs come from the file's Thrift-encoded metadata and may have | ||
| // arbitrary length under a malicious writer. Reject any value that is shorter | ||
| // than what the physical (and, for FLOAT16, logical) type requires so the | ||
| // memcpy/byte loads below cannot run past the buffer. | ||
| size_t required = 0; | ||
| switch (parquet_type) { | ||
| case Type::BOOLEAN: | ||
| required = sizeof(bool); | ||
| break; | ||
| case Type::INT32: | ||
| case Type::FLOAT: | ||
| required = 4; | ||
| break; | ||
| case Type::INT64: | ||
| case Type::DOUBLE: | ||
| required = 8; | ||
| break; | ||
| case Type::INT96: | ||
| required = 3 * sizeof(int32_t); | ||
| break; | ||
| case Type::FIXED_LEN_BYTE_ARRAY: | ||
| if (logical_type != nullptr && logical_type->is_float16()) { | ||
| required = 2; | ||
| } | ||
| break; | ||
| case Type::BYTE_ARRAY: | ||
| case Type::UNDEFINED: | ||
| default: | ||
| break; | ||
| } | ||
| if (val.size() < required) { | ||
|
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. I'm a little hesitant to complicate
Author
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. Fair, it's mostly the debug/printer path. The thing is val is taken verbatim from the file's Thrift stats, so a truncated min/max makes the BOOLEAN/INT96/numeric memcpys read past the end of the buffer even there. The guard is just a size check in front of the existing loads, so I tried to keep it minimal. Happy to trim it further if it still feels too heavy. |
||
| std::stringstream ss; | ||
| ss << "Statistics value of " << val.size() << " bytes is too small for " | ||
| << TypeToString(parquet_type); | ||
| throw ParquetException(ss.str()); | ||
| } | ||
|
|
||
| std::stringstream result; | ||
| const char* bytes = val.data(); | ||
| switch (parquet_type) { | ||
|
|
||
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.
Why not dealing with the length of
FIXED_LEN_BYTE_ARRAY?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.
FormatStatValue doesn't receive the column's type_length, so it can't validate the full FLBA width here. The only fixed-size read for FLBA is the 2-byte float16 load, which is what this case guards. The decimal, string and hex paths all key off val.size() and stay in bounds.
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.
I admit that it is better than nothing to check types with known sizes. It still looks confusing with this partial support. If we do want to add the check, I think we need to support all types. We can either pass the type instance instead of only the enum or pass the type length to
FormatStatValue. The challenge is thatFormatStatValueis a public API so we cannot changing its signature without breaking backward compatibility.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.
BTW, we need to create an issue for changes like this.