[py] reject negative enum and union index in binary decoder#3876
Open
arib06 wants to merge 1 commit into
Open
Conversation
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
read_enum,read_unionandskip_unionin the Python binary decoder read the enum symbol index / union branch index from the wire and validate it with an upper-bound-only check (index >= len(...)). A negative index is not caught. Because the index is then used directly to subscriptwriters_schema.symbols/writers_schema.schemas, and Python lists accept negative subscripts, a crafted negative zigzag index passes validation and wraps to an element counted from the end of the list. For an enum this silently returns the wrong symbol; for a union it selects a branch that the writer never declared and decodes the following bytes with that wrong branch schema, which is a decode-time type confusion and desyncs the stream forskip_union. Positive out-of-range indices are already rejected, so this only closes the negative side. The check becomes0 <= index < len(...)at all three sites.Verifying this change
This change added tests and can be verified as follows:
test_negative_enum_indexandtest_negative_union_indexinTestMisc, feeding a single0x01byte (zigzag-1) as the index. Both assertSchemaResolutionException. Against the unpatched decoder the enum read returnssymbols[-1]and the union read decodes viaschemas[-1], so both tests fail before the fix.test_iosuite green (152 tests).Documentation