[C++][Parquet] Saturate ApplicationVersion components instead of atoi UB#50013
[C++][Parquet] Saturate ApplicationVersion components instead of atoi UB#50013rootvector2 wants to merge 1 commit into
Conversation
The version components in a Parquet file's `created_by` string are attacker-controlled. std::atoi exhibits undefined behavior when the parsed value overflows int (see [c.strtoint]). Replace the three atoi calls in ApplicationVersionParser with a saturating helper that uses std::strtoul and clamps to INT_MAX when the parsed value would not fit in int. Add a regression test exercising overflow inputs.
|
Thanks for opening a pull request! If this is not a minor PR. Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose Opening GitHub issues ahead of time contributes to the Openness of the Apache Arrow project. Then could you also rename the pull request title in the following format? or See also: |
There was a problem hiding this comment.
Pull request overview
This PR hardens Parquet created_by parsing in C++ by removing undefined behavior when version components overflow int, preventing attacker-controlled metadata from producing garbage version values that could affect reader decisions (e.g., statistics trust).
Changes:
- Replaced
std::atoiparsing of major/minor/patch with a helper that usesstd::strtouland saturates toINT_MAXon overflow. - Added a regression test covering very large version components and
INT_MAXboundary conditions.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| cpp/src/parquet/metadata.cc | Adds a safe, saturating parser for version components and wires it into ApplicationVersion parsing. |
| cpp/src/parquet/metadata_test.cc | Adds a new test ensuring overflow inputs clamp to INT_MAX and checks boundary behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Boundary cases: INT_MAX is representable, INT_MAX+1 saturates. | ||
| ApplicationVersion at_max("parquet-mr version 2147483647.2147483647.2147483647"); | ||
| ASSERT_EQ(std::numeric_limits<int>::max(), at_max.version.major); | ||
| ASSERT_EQ(std::numeric_limits<int>::max(), at_max.version.minor); | ||
| ASSERT_EQ(std::numeric_limits<int>::max(), at_max.version.patch); | ||
|
|
||
| ApplicationVersion just_over("parquet-mr version 2147483648.2147483648.2147483648"); | ||
| ASSERT_EQ(std::numeric_limits<int>::max(), just_over.version.major); |
What
When reading a Parquet file,
ApplicationVersion::ApplicationVersion(const std::string& created_by)parses the writer-suppliedcreated_bymetadata string into three integer fields (version.major,version.minor,version.patch). The current implementation incpp/src/parquet/metadata.ccdoes this withstd::atoi:The substrings here are already filtered to be digit-only, but their length is not bounded. The
created_byfield is fully attacker-controlled in any untrusted Parquet file, so an input likereaches
atoiwith a value far outside the range ofint. Per the C++ standard,std::atoihas undefined behavior when the result cannot be represented as anint([c.strtoint]). In practice the value is unspecified, and the negative or otherwise garbage value that comes out then drivesVersionLt/HasCorrectStatistics, controlling whether the reader chooses to trust the column statistics block of the file.How to observe
A small reproducer in the same C++17 mode as Arrow:
UBSan flags the overflow inside the underlying
strtol. The existing tests inmetadata_test.cconly cover small inputs, which is why this has not been caught.The fix
Replace the three
atoicall sites with a smallParseUnsignedVersionComponenthelper inside the anonymous namespace inmetadata.cc. The helper usesstd::strtoul(which is defined to seterrno = ERANGEand returnULONG_MAXon overflow rather than invoking UB) and saturates tostd::numeric_limits<int>::max()when the parsed value would not fit inint. The fix is local to the callee – callers (the constructor ofApplicationVersion, called fromFileMetaData's thrift deserializer) are unchanged.A new
ApplicationVersion.VersionComponentOverflowtest exercises:INT_MAX),INT_MAX(representable),INT_MAX + 1(saturates).Build/test evidence
Built
parquet_objlibandparquet-internals-testlocally; all 18ApplicationVersion.*tests pass, including the new overflow case: