Skip to content

Panic in Opus properties reader for channel count 0 or >8 (expect("Channel count is valid")) #710

Closed

Description

@chipshort

Summary

Probe::open(...).read() panics on Opus files whose OpusHead channel count is 0, or greater than 8 with a channel mapping family other than 0/1 (e.g. family 255). A parser must never panic on input data — and the latter case is even spec-valid.

Panic location (lofty/src/ogg/opus/properties.rs, in read_properties):

properties.channel_mask =
    ChannelMask::from_opus_channels(properties.channels).expect("Channel count is valid");

Confirmed present in 0.22.4, 0.25.1 (latest release), and current main.

Root cause

The validation guard directly above only rejects two cases:

if (channel_mapping_family == 0 && properties.channels > 2)
    || (channel_mapping_family == 1 && properties.channels > 8)
{
    decode_err!(@BAIL Opus, "invalid channel count for mapping family");
}

but ChannelMask::from_opus_channels() returns None for 0 and for >8, so the expect fires for:

  • channels == 0 (any mapping family). C=0 is invalid per RFC 7845 §5.1.1 ("This value MUST NOT be zero") — but invalid input should produce a LoftyError, not a panic.
  • channels > 8 with mapping family ≥ 2. This one is spec-valid: RFC 7845 §5.1.1.3 allows 1...255 channels for family 255 ("Allowed numbers of channels: 1...255"). Ambisonic/many-channel content (mapping families 2/3 in the IANA/draft-terriberry lineage) can also exceed 8 channels.

Impact

Denial of service for any application that reads properties of untrusted Opus files: one such file aborts the calling thread (in async servers: the blocking task, surfacing as a request failure). We hit this in production with Opus files served by a music-download service.

Reproduction

Minimal crafted Opus (two Ogg pages: OpusHead + empty OpusTags) with channels = 0:

use lofty::probe::Probe;

fn ogg_crc(page: &[u8]) -> u32 {
    // Ogg CRC-32: poly 0x04C11DB7, MSB-first, init/xorout 0
    let mut crc = 0u32;
    for &b in page {
        crc ^= (b as u32) << 24;
        for _ in 0..8 {
            crc = if crc & 0x8000_0000 != 0 { (crc << 1) ^ 0x04C1_1DB7 } else { crc << 1 };
        }
    }
    crc
}

fn page(serial: u32, seq: u32, bos: bool, packet: &[u8]) -> Vec<u8> {
    let mut p = b"OggS".to_vec();
    p.push(0); // version
    p.push(if bos { 2 } else { 0 });
    p.extend_from_slice(&0u64.to_le_bytes()); // granule
    p.extend_from_slice(&serial.to_le_bytes());
    p.extend_from_slice(&seq.to_le_bytes());
    p.extend_from_slice(&0u32.to_le_bytes()); // crc placeholder
    p.push(1); // segment count
    p.push(packet.len() as u8); // single segment
    p.extend_from_slice(packet);
    let crc = ogg_crc(&p);
    p[22..26].copy_from_slice(&crc.to_le_bytes());
    p
}

fn main() {
    let mut head = b"OpusHead".to_vec();
    head.push(1); // version
    head.push(0); // channel count = 0  (also panics: e.g. 9 with family 255)
    head.extend_from_slice(&312u16.to_le_bytes()); // pre-skip
    head.extend_from_slice(&48_000u32.to_le_bytes()); // input rate
    head.extend_from_slice(&0u16.to_le_bytes()); // output gain
    head.push(0); // mapping family

    let mut tags = b"OpusTags".to_vec();
    tags.extend_from_slice(&4u32.to_le_bytes());
    tags.extend_from_slice(b"test");
    tags.extend_from_slice(&0u32.to_le_bytes()); // 0 comments

    let mut opus = page(1, 0, true, &head);
    opus.extend_from_slice(&page(1, 1, false, &tags));

    std::fs::write("/tmp/panic.opus", &opus).unwrap();
    let _ = Probe::open("/tmp/panic.opus").unwrap().read(); // panics
}

Output (lofty 0.25.1):

thread 'main' panicked at lofty-0.25.1/src/ogg/opus/properties.rs:125:56:
Channel count is valid

channels = 2, family = 0 parses fine; channels = 0, family = 0, channels = 9, family = 255, and channels = 255, family = 255 all panic.

Suggested fix

Return a decoding error instead of panicking, e.g.:

properties.channel_mask = ChannelMask::from_opus_channels(properties.channels)
    .ok_or_else(|| /* LoftyError: invalid channel count for Opus */)?;

(and/or explicitly reject channels == 0 in the guard above). Alternatively, store the mask as Option<ChannelMask> for unknown layouts — family 255 files have no defined channel meanings anyway.

Generated by AI (Kimi K3), but I think this is a valid bug.

Activity

  1. added this to the 0.25.2 milestone on Sep 13, 2026
  2. Serial-ATA commented on Sep 13, 2026

    @Serial-ATA
    Owner

    Oops, yeah I'll just make this an error for now. I'll make the channel mask optional in 0.26.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions