Fix out-of-bounds array write in POR missing-value reader#376
Open
aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
Open
Fix out-of-bounds array write in POR missing-value reader#376aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
aaranmcguire wants to merge 1 commit intoWizardMac:devfrom
Conversation
The bounds check on missing_double_values[3] was performed after the write, not before. When n_missing_values == 3, the code wrote to missing_double_values[3] — one element past the end of the array — before detecting the error. The struct layout (spss/readstat_spss.h): double missing_double_values[3]; /* bytes 0-23 */ char missing_string_values[3][9]; /* bytes 24-50 */ So the fourth write (8 bytes at offset 24) overwrites the first 8 bytes of missing_string_values[0], corrupting string-missing storage. Trigger: a POR file with a variable declaring 4 discrete missing values (malformed, but trivially crafted; any fuzzer finds this immediately). Fix: move the n_missing_values >= 3 check before the array write.
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.
PR 5: Fix out-of-bounds array write in POR missing-value reader
Summary
In
spss/readstat_por_read.c, the bounds check on themissing_double_values[3]array is performed after the write, not beforeit. When
n_missing_valuesequals 3, the code writes tomissing_double_values[3]— one slot past the end of the array — beforedetecting the error.
Vulnerable code
The struct declaration (
spss/readstat_spss.h):Why it is a real bug
SPSS allows up to 3 discrete missing values per variable. The POR reader
calls
read_missing_value_recordonce per missing-value token in the file.After the third call,
varinfo->n_missing_values == 3.On the fourth call (a malformed file that encodes a fourth missing value):
varinfo->missing_double_values[3]is written — one element past the3-element array.
if (n_missing_values > 2)check fires — but too late.The write lands in whatever field follows
missing_double_valuesinspss_varinfo_t. Looking at the struct:So the fourth
doublewrite (8 bytes) overwrites the first 8 bytes ofmissing_string_values[0]— corrupting the string-missing storage. Whetherthis matters in practice depends on what the code reads from
missing_string_valuesafterward, but the memory corruption is unconditional.Reproduction
With AddressSanitizer:
Fix
Move the bounds check before the write:
The condition changes from
> 2(post-write, after index 3 has been used) to>= 3(pre-write, rejecting index 3 before it is accessed).