Skip to content

Fix for CVE-2026-111271 - OOB read in CSoundFile::ReadAMS2() - #107

Merged
Konstanty merged 1 commit into
Konstanty:masterfrom
kavol:fix-ams2-pattern-bounds
Oct 23, 2026
Merged

Konstanty merged 1 commit into
Konstanty:masterfrom
kavol:fix-ams2-pattern-bounds

Conversation

@kavol

@kavol kavol commented Oct 21, 2026

Copy link
Copy Markdown

The pattern unpack loop in CSoundFile::ReadAMS2() tests "pos < packedlen"
only at the top of each iteration, but the body advances pos up to five
times. The inner effect loop is the worst of them:

while (byte2 & 0x80)
{
    byte2 = psrc[pos++];
    ...
    UINT param = psrc[pos++];

It runs for as long as every byte it fetches has bit 7 set, with no bound
of its own, so a pattern whose effect bytes all keep bit 7 set walks pos
past packedlen and on past the end of the input buffer, reading adjacent
heap memory until it finds a byte without bit 7 or hits an unmapped page.
Check pos against packedlen at each read instead. packedlen already
satisfies packedlen < dwMemLength - dwMemPos, so staying within it keeps
every read inside the buffer.

This is the v2 counterpart of upstream 70d73b9, which added exactly this
bound to the four reads in the ReadAMS() v1 decoder and left ReadAMS2()
alone. The "pos+1 > packedlen" spelling is upstream's from that commit.

The two byte note/instrument read just above has the same problem on a
smaller scale and is bounded here too.

The first hunk fixes a problem not described in the CVE: the pattern name
memcpy reads patnamlen bytes from dwMemPos+3, with patnamlen up to
MAX_PATTERNNAME-1, but at that point only dwMemPos+4 is known to be inside
the buffer, so a pattern at the end of the file reads up to 30 bytes past
it. The dwMemPos checks added previously do not cover this: they all sit
in the instrument and sample loops, which finish before the pattern section
starts.

Co-Authored-By: Claude Opus 5 noreply@anthropic.com

The pattern unpack loop in CSoundFile::ReadAMS2() tests "pos < packedlen"
only at the top of each iteration, but the body advances pos up to five
times. The inner effect loop is the worst of them:

    while (byte2 & 0x80)
    {
        byte2 = psrc[pos++];
        ...
        UINT param = psrc[pos++];

It runs for as long as every byte it fetches has bit 7 set, with no bound
of its own, so a pattern whose effect bytes all keep bit 7 set walks pos
past packedlen and on past the end of the input buffer, reading adjacent
heap memory until it finds a byte without bit 7 or hits an unmapped page.
Check pos against packedlen at each read instead. packedlen already
satisfies packedlen < dwMemLength - dwMemPos, so staying within it keeps
every read inside the buffer.

This is the v2 counterpart of upstream 70d73b9, which added exactly this
bound to the four reads in the ReadAMS() v1 decoder and left ReadAMS2()
alone. The "pos+1 > packedlen" spelling is upstream's from that commit.

The two byte note/instrument read just above has the same problem on a
smaller scale and is bounded here too.

The first hunk fixes a problem not described in the CVE: the pattern name
memcpy reads patnamlen bytes from dwMemPos+3, with patnamlen up to
MAX_PATTERNNAME-1, but at that point only dwMemPos+4 is known to be inside
the buffer, so a pattern at the end of the file reads up to 30 bytes past
it. The dwMemPos checks added previously do not cover this: they all sit
in the instrument and sample loops, which finish before the pattern section
starts.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@Konstanty
Konstanty merged commit 84b80da into Konstanty:master Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants