awc_opus: reject music descriptors that overrun the data tag

music_channels() trusted the descriptor's block count and the chunk records' subblock counts. A descriptor declaring more blocks or subblocks than its 0x55 data tag holds read past the tag: into a sibling stream's payload, whose bytes were silently exported as music Opus packets, or past the end of the bank, where struct.error made main() skip the whole bank and lose its valid streams (exit 0).

Reject the descriptor ([]) when blocks * chunk exceeds the data tag size, or when a chunk's aligned header plus its subblock run does not fit the tag bytes remaining at that chunk -- the same refusal the existing descriptor checks use. Valid banks are unaffected, including single-block descriptors whose chunk field names only the record-table stride (the awc_manifest/awc_shorts fixtures).

Tests: MalformedMusicGeometryTest in tests/test_awc_opus.py (4 malformed cases + 1 valid-bank control). Negative control on main e7584c5: 4 of the 5 fail (struct.error, garbage packet export, whole-bank skip); with the fix all 24 pass. test_awc_manifest (6), test_awc_shorts (10), test_audio_stage_resume (10), test_audio_config (21) pass; compileall clean. Synthetic banks only, no game files.
This commit is contained in:
qerdavo-dot
2026-11-22 12:58:30 +03:00
parent c0ac5fd651
commit 944961c586
2 changed files with 120 additions and 1 deletions

View File

@ -144,6 +144,43 @@ def build_music_bank(block_tocs, samples, rate=48000):
return bytes(out + desc + data)
def build_music_pcm_bank(blocks=1, count=1, pcm=None):
"""One streamed-music descriptor (one mono Opus group) plus one PCM16 stream.
The music data tag holds exactly one real chunk: a 2048-byte header subblock and one
2048-byte data subblock with a single CELT 20 ms mono packet. The PCM payload follows the
music data directly. `blocks` and `count` are what the descriptor and the chunk record
declare, so a test can declare more than the data tag holds.
"""
chunk = 4096
desc = bytearray(12 + 16)
struct.pack_into("<III", desc, 0, blocks, chunk, 1)
struct.pack_into("<II", desc, 12, 0x100, 960)
struct.pack_into("<H", desc, 22, 48000)
desc[24] = 0x0B
music_data = bytearray(chunk)
struct.pack_into("<I", music_data, 4, count)
struct.pack_into("<HB", music_data, 2048, 1, 0x78)
if pcm is None:
pcm = struct.pack("<4h", 11, 22, 33, 44)
fmt = format_block(len(pcm) // 2, 48000, 0, 1)
header_size = 16 + 2 * 4 + 4 * 8
desc_at = header_size
mdata_at = desc_at + len(desc)
pcm_at = mdata_at + len(music_data)
fmt_at = pcm_at + len(pcm)
out = bytearray(b"ADAT" + struct.pack("<III", 0, 2, header_size))
out += struct.pack("<I", 0x20 | (2 << 29)) # music stream: tags 0x48 + 0x55
out += struct.pack("<I", 0x21 | (2 << 29)) # PCM stream: tags 0x76 + 0x55
out += struct.pack("<Q", tag(0x48, desc_at, len(desc)))
out += struct.pack("<Q", tag(0x55, mdata_at, len(music_data)))
out += struct.pack("<Q", tag(0x76, fmt_at, len(fmt)))
out += struct.pack("<Q", tag(0x55, pcm_at, len(pcm)))
if len(out) != header_size:
raise AssertionError("stream table/tag layout does not match header_size")
return bytes(out + desc + music_data + pcm + fmt)
class WriteWavTest(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
@ -435,6 +472,82 @@ class StreamedMusicTest(unittest.TestCase):
self.assertEqual((info[0][1], info[0][4], info[0][5]), (960, [], 2))
class MalformedMusicGeometryTest(unittest.TestCase):
"""A music descriptor that declares more blocks or subblocks than its data tag holds is
rejected, instead of reading past the tag into a sibling stream or past the end of the bank."""
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.addCleanup(self.tmp.cleanup)
self.out = os.path.join(self.tmp.name, "out")
def write_bank(self, **kw):
path = os.path.join(self.tmp.name, "streamed.awc")
with open(path, "wb") as f:
f.write(build_music_pcm_bank(**kw))
return path
def test_blocks_beyond_data_tag_keep_sibling_streams(self):
# Before the fix: struct.error out of music_channels, so main() skipped the whole
# bank and the valid PCM16 stream was never exported.
path = self.write_bank(blocks=3)
with open(path, "rb") as f:
d = f.read()
_, music = awc.parse_awc(d)
self.assertEqual(awc.music_channels(d, *music[0]), [])
self.assertEqual(awc.music_channels(d, *music[0], collect_packets=False), [])
counts = awc.export_bank(path, self.out)
self.assertEqual(counts, {"opus": 0, "pcm": 1, "other": 0, "music": 0})
od = os.path.join(self.out, "streamed")
self.assertTrue(os.path.isfile(os.path.join(od, "00000021.wav")))
self.assertFalse(os.path.exists(os.path.join(od, "00000100.ogg")))
def test_overread_into_sibling_payload_exports_no_music(self):
# The bytes right after the one real chunk are the PCM stream's payload, crafted to
# look like a second chunk. Before the fix its 'packet' was exported as music audio.
pcm = bytearray(4096)
struct.pack_into("<hh", pcm, 0, 11, 22)
struct.pack_into("<I", pcm, 4, 1)
struct.pack_into("<HB", pcm, 2048, 1, 0x78)
path = self.write_bank(blocks=2, pcm=bytes(pcm))
counts = awc.export_bank(path, self.out)
self.assertEqual(counts, {"opus": 0, "pcm": 1, "other": 0, "music": 0})
od = os.path.join(self.out, "streamed")
self.assertFalse(os.path.exists(os.path.join(od, "00000100.ogg")))
self.assertEqual(read_wav(os.path.join(od, "00000021.wav"))["pcm"], bytes(pcm))
def test_subblock_run_beyond_chunk_rejected(self):
# count=2 needs a 2048-byte header plus 2 data subblocks, but the data tag holds
# only 4096 bytes. Before the fix the second subblock was read from the PCM payload.
path = self.write_bank(count=2)
with open(path, "rb") as f:
d = f.read()
_, music = awc.parse_awc(d)
self.assertEqual(awc.music_channels(d, *music[0]), [])
counts = awc.export_bank(path, self.out)
self.assertEqual(counts, {"opus": 0, "pcm": 1, "other": 0, "music": 0})
def test_cli_exports_the_sibling_stream_without_skipping_the_bank(self):
path = self.write_bank(blocks=3)
result = subprocess.run([sys.executable, "-I", awc.__file__, path, self.out, "--jobs", "1"],
capture_output=True, text=True)
self.assertEqual(result.returncode, 0)
self.assertIn("1 WAV", result.stdout)
self.assertNotIn("skip", result.stdout)
self.assertTrue(os.path.isfile(os.path.join(self.out, "streamed", "00000021.wav")))
def test_valid_combined_bank_still_exports_music(self):
path = self.write_bank()
counts = awc.export_bank(path, self.out)
self.assertEqual(counts, {"opus": 0, "pcm": 1, "other": 0, "music": 1})
od = os.path.join(self.out, "streamed")
with open(os.path.join(od, "00000100.ogg"), "rb") as f:
pages = parse_ogg_pages(f.read())
self.assertEqual(pages[-1]["granule"], 960)
self.assertEqual(pages[-1]["body"], bytes([0x78]))
self.assertTrue(os.path.isfile(os.path.join(od, "00000021.wav")))
class MalformedBankTest(unittest.TestCase):
def test_truncated_pcm_bank_creates_no_outputs(self):
pcm = b"\x01\x00" * 4

View File

@ -166,11 +166,15 @@ def music_channels(d: bytes, mus, data, collect_packets: bool = True):
divided by the packet channel count when the Opus packets are stereo. `packet channels` is read from the
Opus TOC stereo bit (bit 2) and defaults to 1 when no packet is present.
collect_packets=False keeps the packet lists empty for metadata-only callers (awc_manifest.py)."""
collect_packets=False keeps the packet lists empty for metadata-only callers (awc_manifest.py).
A descriptor whose declared blocks or subblock runs do not fit its data tag is rejected ([]), so a
corrupt descriptor cannot read past the tag into a sibling stream or past the end of the bank."""
(m_at, m_size), (d_at, d_size) = mus, data
blocks, chunk, channels = struct.unpack_from("<III", d, m_at)
if not (1 <= channels <= 32 and 1 <= blocks and chunk >= 2048 and m_size == 12 + 16 * channels):
return []
if blocks * chunk > d_size:
return []
info = []
for c in range(channels):
base = m_at + 12 + 16 * c
@ -183,6 +187,8 @@ def music_channels(d: bytes, mus, data, collect_packets: bool = True):
recs = [struct.unpack_from("<III", d, at + 24 * c + 4)[:2] for c in range(channels)] # (count, skip)
total = sum(r[0] for r in recs)
header = (channels * 24 + total * 4 + 2047) & ~2047
if header + total * 2048 > d_size - b * chunk:
return []
before = 0
for c, (count, skip) in enumerate(recs):
start = at + header + before * 2048