Skip to content

cast to unsigned char before ctype calls on schema names - #4046

Open
ArjunD3456 wants to merge 1 commit into
apache:mainfrom
ArjunD3456:ctype-unsigned-char
Open

ArjunD3456 wants to merge 1 commit into
apache:mainfrom
ArjunD3456:ctype-unsigned-char

Conversation

@ArjunD3456

Copy link
Copy Markdown

What is the purpose of the change

is_avro_id in lang/c/src/schema.c validates a schema's name by calling isalpha/isdigit on a plain char. Where char is signed (the default on x86-64 and ARM), a name byte of 0x80 or higher is sign extended to a negative int. Passing a negative value other than EOF to the ctype functions is undefined behavior (C11 7.4p1 requires the argument to be representable as unsigned char or equal EOF), and on a libc whose ctype table is not offset for negative indices it reads out of bounds. Schema names reach is_avro_id straight from the avro.schema JSON in a container file header, so the bytes are attacker controlled. print_bytes_value in lang/c/src/avropipe.c has the same shape, calling isprint on a char taken from a decoded bytes, string, fixed, or enum value. Both now cast to unsigned char at the call, which is the standard way to use these functions. lang/c/src/dump.c already masks with 0x7f so 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_tests cases continue to exercise name validation. To confirm the out-of-bounds read I modeled the is_avro_id table lookup in a standalone program and built it with AddressSanitizer: a name whose first byte is 0xC3 produced a global-buffer-overflow read on the table index before the cast, and the build is clean after it.

Documentation

  • Does this pull request introduce a new feature? no

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.
@github-actions github-actions Bot added the C label Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant