Repository navigation
txt: bound schema rows and columns in readstat_parse_txt - #431
Merged
Merged
Conversation
readstat_parse_txt allocates line_lens with rows_per_observation entries and then indexes it with each schema entry's row, storing col + len there. Neither value is checked, and both come from the dictionary file. In a Stata .dct, _line(N) is independent of _lines(), so _line(3) with the default single line writes past line_lens, and _line(0) or a SAS INPUT #0 wraps the row to 0xFFFFFFFF. _column(0) wraps col to UINT32_MAX, so col + len wraps small, the line buffer is sized from that, and txt_parse_fixed_width copies from &line_buffer[col]. Reject rows outside rows_per_observation and col + len that overflows before line_lens is touched.
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.
readstat_parse_txt sizes line_lens from rows_per_observation and then indexes it with each schema entry's row, writing col + len there, but neither value is checked and both come straight from the dictionary file. In a Stata .dct,
_line(3)with the default_lines(1)reads and writesline_lens[2]past an 8-byte allocation (ASan heap-buffer-overflow at readstat_txt_read.c:203), and_line(0)or a SASINPUT #0 name $ 1-5;wraps the uint32_t row to 0xFFFFFFFF._column(0)wraps col to UINT32_MAX, so col + len wraps to a small line length and txt_parse_fixed_width then copies from&line_buffer[4294967295]. The readstat CLI hits this for any.datread with a.dct/.sas/.sps.Reject entries whose row is not below rows_per_observation or whose col + len overflows uint32_t with READSTAT_ERROR_PARSE before line_lens is touched. All three dictionary parsers and hand-built schemas go through this function, so one check covers them. The new test in test_readstat aborts under ASan without the fix and passes with it.