Fix stack-buffer-overflow in FASTCOVER with small samples (#4499)#4600
Fix stack-buffer-overflow in FASTCOVER with small samples (#4499)#4600Nicolas0315 wants to merge 2 commits intofacebook:devfrom
Conversation
FASTCOVER_hashPtrToIndex reads d bytes (6 or 8) from the sample buffer, but FASTCOVER_selectSegment iterated up to 'end' without ensuring d bytes remain, causing an out-of-bounds read when samples are very small (e.g. 8 x 1-byte). Two fixes: 1. In FASTCOVER_selectSegment: limit all hash-reading loops to 'hashEnd = end - d + 1' instead of 'end', preventing reads past the buffer. 2. In FASTCOVER_buildDictionary: clamp epochEnd to the actual training data boundary (ctx->offsets[ctx->nbTrainSamples]) so selectSegment never receives an end position beyond the sample buffer. Tested with AddressSanitizer + UBSan using the PoC from facebook#4499 (8 x 1-byte samples). Before: stack-buffer-overflow. After: no crash, returns a 135-byte dictionary. Fixes facebook#4499
|
Hi @Nicolas0315! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
@Nicolas0315 can you please add that unit test to |
sebres
left a comment
There was a problem hiding this comment.
Shall not the lower limit of sample be 8 bytes?
Related to the docu for ZDICT_trainFromBuffer:
Lines 199 to 200 in c84c8fb
So it doesn't really make sense to train on such short samples.
Probably it shall simply check the size of sample and
ZDICT_trainFromBuffer could simply bypass them (and then fail if everything is too short, exactly how the description says).
Add a unit test in basicUnitTests() that calls ZDICT_trainFromBuffer_fastCover with 8 samples of 1 byte each (d=8, k=16). This is the minimal reproducer from issue facebook#4499; it triggered a stack-buffer-overflow in FASTCOVER_hashPtrToIndex before the fix in this PR. The test verifies the function does not crash regardless of whether it returns a valid dictionary or an error.
Summary
Fix a stack-buffer-overflow (out-of-bounds read) in
FASTCOVER_hashPtrToIndextriggered when training a dictionary with many small samples (e.g. 8 × 1-byte).Root Cause
FASTCOVER_hashPtrToIndexreadsdbytes (6 or 8) from the sample buffer viaZSTD_hash6Ptr/ZSTD_hash8Ptr. However:FASTCOVER_selectSegmentiteratedactiveSegment.endup toendwithout ensuring that at leastdbytes remain at the read position, causing OOB reads near the buffer boundary.FASTCOVER_buildDictionarycomputedepochEnd = epochBegin + epochs.sizewithout clamping to the actual sample buffer size, soendcould exceed the training data boundary.Note:
FASTCOVER_computeFrequencyalready has the correct bound check (start + readLength <= currSampleEnd), so this was a consistency issue only in the segment selection path.Fix
FASTCOVER_selectSegment: addhashEnd = end - d + 1and use it as the loop bound for all three hash-reading loops (main sliding window, cleanup, and frequency zeroing).FASTCOVER_buildDictionary: clampepochEndtoctx->offsets[ctx->nbTrainSamples](the actual end of training data).Testing
Tested with ASAN + UBSAN using the PoC from #4499:
Before:
AddressSanitizer: stack-buffer-overflow in FASTCOVER_hashPtrToIndexAfter: No crash, returns a 135-byte dictionary.
Fixes #4499