ReadXM: fix out-of-bounds reads from unvalidated dwMemPos (integer overflow) - #104
Open
e-c-hansen wants to merge 1 commit into
Open
ReadXM: fix out-of-bounds reads from unvalidated dwMemPos (integer overflow)#104e-c-hansen wants to merge 1 commit into
e-c-hansen wants to merge 1 commit into
Conversation
In CSoundFile::ReadXM the instrument-sample loop advances dwMemPos by the
attacker-controlled samplesize[] values with no overflow guard, so on exit
dwMemPos can be far past dwMemLength (e.g. 0xFFFFFFF8). The tail parsers then
trust it with overflow-prone checks:
- "TEXT" block: (dwMemPos + 8 < dwMemLength) wraps to 0 < dwMemLength and
reads *(DWORD*)(lpStream + dwMemPos) ~4 GB out of bounds
- "MIDI" block: same shape
- mix plugins: LoadMixPlugins(lpStream + dwMemPos, ...)
A crafted .xm file triggers a SIGSEGV out-of-bounds read (confirmed with
AddressSanitizer and on stock builds). Guarding a single site just moves the
crash to the next, so clamp dwMemPos to dwMemLength once, after the sample
loop. Each tail parser already compares dwMemPos against dwMemLength and only
misbehaved when dwMemPos was allowed to exceed it.
Tested: crashing input now parses cleanly under ASan; valid .xm/.mod/.s3m/.it
files are unaffected.
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
CSoundFile::ReadXMcan perform out-of-bounds reads on a crafted.xmfile,causing a SIGSEGV (denial of service). Confirmed on current
master(
d1b97ed) and on the version shipped by Debian (libmodplug1 0.8.9.0).Root cause
The instrument-sample loop advances
dwMemPosby the attacker-controlledsamplesize[]values with no overflow guard:On exit
dwMemPoscan be far pastdwMemLength(e.g.0xFFFFFFF8). The tailparsers then trust it, each with an overflow-prone check:
load_xm.cpp:522):dwMemPos + 8 < dwMemLength— withdwMemPos = 0xFFFFFFF8,dwMemPos + 8wraps to0, so0 < dwMemLengthpasses and
*(DWORD*)(lpStream + dwMemPos)reads ~4 GB out of bounds.load_xm.cpp:538): same shape.load_xm.cpp:587):LoadMixPlugins(lpStream + dwMemPos, …).Because it is a cluster, guarding a single site just moves the crash to the
next (patching only TEXT relocates the SEGV into
LoadMixPlugins).Fix
Clamp
dwMemPostodwMemLengthonce, immediately after the sample loop.Each tail parser already compares
dwMemPosagainstdwMemLengthand onlymisbehaved when
dwMemPoswas allowed to exceed it, so a single root-causeclamp neutralizes all three without touching each check:
Testing
.xm,.mod,.s3m, and.itfiles parse unchanged.Found via coverage-guided fuzzing (AFL++) with an ASan build. A 719-byte
minimized reproducer is available on request.