CFArray: fix heap overflows in mutable-array growth and copy#45
Open
DTW-Thalion wants to merge 1 commit into
Open
CFArray: fix heap overflows in mutable-array growth and copy#45DTW-Thalion wants to merge 1 commit into
DTW-Thalion wants to merge 1 commit into
Conversation
Three related out-of-bounds heap writes, all reachable from the public
API and all confirmed with AddressSanitizer:
* CFArrayCheckCapacityAndGrow() always grew the backing store by exactly
DEFAULT_ARRAY_CAPACITY (16), ignoring the requested capacity. An
operation needing more than 16 additional slots at once therefore left
the array too small and the subsequent insert wrote past the end (e.g.
CFArrayAppendArray() of 40 elements).
* CFArrayReplaceValues() computed the new size and the move count from
array->_count *after* it had already subtracted range.length, so the
grow request was too small and the memmove copied range.length too
many elements (an out-of-bounds read on removal). It also kept using
the pre-grow "start" pointer, writing into the old buffer after a
reallocation.
* CFArrayCreateMutableCopy() wrote the source contents straight into the
new array's buffer but, on the non-Objective-C path, never enlarged
the requested capacity to the source count (the Objective-C path does)
-- so copying 20 elements with a requested capacity of 0 overran the
16-slot default buffer.
Grow to at least the requested capacity; capture the original count and
recompute "start" after the grow in CFArrayReplaceValues so the size, the
move count, and the destination pointer are all correct; and clamp the
mutable-copy capacity to the source count. Adds regression tests.
Contributor
Author
|
Some extra evidence for this one, in case it is useful for review. Minimal repro — append past the initial 16-slot capacity of a mutable array: #include <CoreFoundation/CFArray.h>
#include <stdio.h>
int main (void)
{
CFMutableArrayRef a = CFArrayCreateMutable (NULL, 0, NULL);
CFIndex i;
for (i = 0; i < 40; i++)
CFArrayAppendValue (a, (const void *)(CFIndex)(i + 1));
printf ("count=%ld last=%ld\n",
(long)CFArrayGetCount (a),
(long)(CFIndex)CFArrayGetValueAtIndex (a, 39));
CFRelease (a);
return 0;
}Built and run against an AddressSanitizer build of the library. On master it aborts: That is exactly the case this PR addresses: With this PR applied, the same repro is clean: |
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.
Three related out-of-bounds heap writes, all reachable from the public
API and all confirmed with AddressSanitizer:
CFArrayCheckCapacityAndGrow() always grew the backing store by exactly
DEFAULT_ARRAY_CAPACITY (16), ignoring the requested capacity. An
operation needing more than 16 additional slots at once therefore left
the array too small and the subsequent insert wrote past the end (e.g.
CFArrayAppendArray() of 40 elements).
CFArrayReplaceValues() computed the new size and the move count from
array->_count after it had already subtracted range.length, so the
grow request was too small and the memmove copied range.length too
many elements (an out-of-bounds read on removal). It also kept using
the pre-grow "start" pointer, writing into the old buffer after a
reallocation.
CFArrayCreateMutableCopy() wrote the source contents straight into the
new array's buffer but, on the non-Objective-C path, never enlarged
the requested capacity to the source count (the Objective-C path does)
-- so copying 20 elements with a requested capacity of 0 overran the
16-slot default buffer.
Grow to at least the requested capacity; capture the original count and
recompute "start" after the grow in CFArrayReplaceValues so the size, the
move count, and the destination pointer are all correct; and clamp the
mutable-copy capacity to the source count. Adds regression tests.