Repository navigation
cast to unsigned char before ctype calls on schema names - #4046
Open
ArjunD3456 wants to merge 1 commit into
Open
ArjunD3456 wants to merge 1 commit into
ArjunD3456 wants to merge 1 commit into
Conversation
is_avro_id() validates schema names with isalpha()/isdigit() applied to a plain char. Where char is signed, a name byte >= 0x80 becomes a negative int, which is undefined behavior for the ctype functions and an out-of-bounds ctype-table read on libc implementations whose table is not offset for negative indices. Schema names come from the avro.schema JSON of a container file header, so the bytes are untrusted. avropipe's print_bytes_value() calls isprint() on a char the same way. Cast the argument to unsigned char at each site; dump.c already masks with 0x7f and is unaffected.
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 is the purpose of the change
is_avro_idinlang/c/src/schema.cvalidates a schema's name by callingisalpha/isdigiton a plainchar. Wherecharis signed (the default on x86-64 and ARM), a name byte of0x80or higher is sign extended to a negativeint. Passing a negative value other thanEOFto the ctype functions is undefined behavior (C11 7.4p1 requires the argument to be representable asunsigned charor equalEOF), and on a libc whose ctype table is not offset for negative indices it reads out of bounds. Schema names reachis_avro_idstraight from theavro.schemaJSON in a container file header, so the bytes are attacker controlled.print_bytes_valueinlang/c/src/avropipe.chas the same shape, callingisprinton achartaken from a decoded bytes, string, fixed, or enum value. Both now cast tounsigned charat the call, which is the standard way to use these functions.lang/c/src/dump.calready masks with0x7fso it is unaffected.Verifying this change
This change is a code cleanup without new test coverage. Behavior is unchanged for valid input: an ASCII name validates as before, and a name with a byte >= 0x80 is still rejected since it is not alpha, digit, or underscore. The existing
schema_testscases continue to exercise name validation. To confirm the out-of-bounds read I modeled theis_avro_idtable lookup in a standalone program and built it with AddressSanitizer: a name whose first byte is0xC3produced aglobal-buffer-overflowread on the table index before the cast, and the build is clean after it.Documentation