[C++][Parquet] Saturate ApplicationVersion components instead of atoi UB#50013
Open
rootvector2 wants to merge 1 commit into
Open
[C++][Parquet] Saturate ApplicationVersion components instead of atoi UB#50013rootvector2 wants to merge 1 commit into
rootvector2 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: