proc_read_vec: bound trailing NUL trim to the current string table - #557
Merged
Merged
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.
proc_read_vec tests strtab - *bufp >= 2 before trimming the extra trailing NUL, but the string table it just filled starts at *bufp + off rather than at *bufp. The environ read is handed a non-zero off, so when the tracee exec'd with an empty environment and nothing landed in this table, the test looks at the last two bytes of the argv pointer vector written by the previous call; those are its NULL terminator, so the trim always fires and strtab drops below the start of the table. strtab_len then underflows to SIZE_MAX and strtab_to_vec evaluates strtab + SIZE_MAX, which ubsan reports as a pointer overflow; an envp holding one empty string trips the same test and is miscounted as zero entries, so ptrace_verify_post_exec compares the wrong envc.
Compare against *bufp + off, which is what the strtab_len calculation two lines down already uses. The bound belongs next to the table it describes rather than to the whole allocation, so a caller chaining a second read into the same buffer does not have to know the trim exists. Reads where at least two bytes arrive behave exactly as before.