CFData: fix CFDataReplaceBytes length miscalculation (heap overflow)#54
Open
DTW-Thalion wants to merge 1 commit into
Open
CFData: fix CFDataReplaceBytes length miscalculation (heap overflow)#54DTW-Thalion wants to merge 1 commit into
DTW-Thalion wants to merge 1 commit into
Conversation
7d04fd8 to
cc65658
Compare
CFDataReplaceBytes computed the new buffer length as range.location + newLength, which ignores the bytes following the replaced range. When a replace or delete leaves a non-empty tail, the buffer was grown too small and the tail memmove wrote past the allocation (heap buffer overflow), and the stored length was wrong (the tail was dropped). Compute the length as _length - range.length + newLength, and base the range precondition and the tail-move guard on _length rather than _capacity / the miscomputed length. Add Tests/CFData/replacebytes.m covering a middle grow and a middle delete.
cc65658 to
9ced23d
Compare
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.
Summary
CFDataReplaceBytescomputes the new buffer length asrange.location + newLength, which ignores the bytes after the replacedrange. When a replace or delete leaves a non-empty tail, the buffer is grown
too small and the subsequent tail
memmovewrites past the allocation(heap buffer overflow), and the stored length is wrong (the tail is dropped).
This affects
CFDataReplaceBytes,CFDataDeleteBytes, and any replace thatleaves trailing bytes — ordinary
CFMutableDatausage.Reproduction (AddressSanitizer)
heap-buffer-overflow WRITE of size 15inmemmovevia
CFDataReplaceBytes(the buffer is grown to only 32 bytes inCFDataCheckCapacityAndGrow);CFDataGetLengthreturns 32 instead of 47.Fix
Compute the new length as
_length - range.length + newLength, and base therange precondition (
assert) and the tail-move guard on_lengthrather than_capacity/ the miscomputed length.Test
Tests/CFData/replacebytes.mcovers a middle grow and a middle delete,asserting both the resulting length and that the prefix/tail are preserved.