Fix off-by-one heap-buffer-overflow in get_or_create_temp_file()#493
Open
xroche wants to merge 1 commit intoDataDog:mainfrom
Open
Fix off-by-one heap-buffer-overflow in get_or_create_temp_file()#493xroche wants to merge 1 commit intoDataDog:mainfrom
xroche wants to merge 1 commit intoDataDog:mainfrom
Conversation
The malloc size calculation in get_or_create_temp_file() allocates
strlen(tmp_dir) + strlen(prefix) + strlen(data.digest) + 2 bytes,
but the constructed path "tmp_dir/prefix-digest\0" requires +3 bytes
(for '/', '-', and the NUL terminator), not +2.
This causes a 1-byte heap-buffer-overflow when strcat writes the NUL
terminator of data.digest past the end of the allocated buffer.
Make the arithmetic explicit by writing each component's length
separately (+1 for each separator and the terminator), matching the
four strcat calls that follow.
The sister function create_temp_file() is not affected: its format
"tmp_dir/prefix.XXXXXX" only has one separator ('/'), and ".XXXXXX"
is already measured by strlen(".XXXXXX").
Fixes DataDog#492
Collaborator
|
Thank you! We will look at why we did not pick this up on asan builds. |
Author
@r1viollet We detected several other issues; what would be the most efficient way to address them ?
Thanks! |
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 does this PR do?
Fix off-by-one heap-buffer-overflow in
get_or_create_temp_file()insrc/lib/loader.c.The
mallocallocatesstrlen(tmp_dir) + strlen(prefix) + strlen(data.digest) + 2, but the pathtmp_dir/prefix-digest\0needs+3(for/,-, and\0). The NUL terminator overflows by 1 byte.Motivation
Detected via AddressSanitizer when
dlopeninglibdd_profiling.so(v0.23.0 release):The sister function
create_temp_file()is correct (only 1 separator).Additional Notes
In practice, the 1-byte NUL overflow is benign on most allocators — it only crashes under ASAN.
How to test the change?
dlopenlibdd_profiling.sounder AddressSanitizer.Classification
Fixes #492