mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 11:38:08 +00:00
fix(audio): reject overflowing DSD chunk sizes and invalid factors
This commit is contained in:
@@ -131,3 +131,7 @@ required-features = ["decode"]
|
||||
[[example]]
|
||||
name = "encode"
|
||||
required-features = ["lossless-encode", "aac-encode"]
|
||||
|
||||
[[test]]
|
||||
name = "dsd_bounds"
|
||||
required-features = ["dsd"]
|
||||
|
||||
@@ -53,12 +53,13 @@ pub fn parse_dsdiff_header(bytes: &[u8]) -> Result<(Metadata, usize, usize), Err
|
||||
}
|
||||
|
||||
let total_file_data = bytes.len().saturating_sub(12);
|
||||
if (form_size as usize) > total_file_data {
|
||||
let form_size = usize::try_from(form_size).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if form_size > total_file_data {
|
||||
return Err(Error::UnexpectedEof("FRM8 chunk extends beyond file size"));
|
||||
}
|
||||
|
||||
let mut pos = 16;
|
||||
let end = (12 + form_size as usize).min(bytes.len());
|
||||
let end = 12 + form_size;
|
||||
|
||||
let mut sample_rate = None;
|
||||
let mut channel_names = Vec::new();
|
||||
@@ -71,10 +72,11 @@ pub fn parse_dsdiff_header(bytes: &[u8]) -> Result<(Metadata, usize, usize), Err
|
||||
|
||||
while pos + 12 <= end {
|
||||
let chunk_id: [u8; 4] = bytes[pos..pos + 4].try_into().map_err(|_| Error::UnexpectedEof("chunk ID"))?;
|
||||
let chunk_size = u64::from_be_bytes(bytes[pos + 4..pos + 12].try_into().map_err(|_| Error::UnexpectedEof("chunk size"))?) as usize;
|
||||
let chunk_size = usize::try_from(u64::from_be_bytes(bytes[pos + 4..pos + 12].try_into().map_err(|_| Error::UnexpectedEof("chunk size"))?))
|
||||
.map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
pos += 12;
|
||||
|
||||
if pos + chunk_size > end {
|
||||
if chunk_size > end.saturating_sub(pos) {
|
||||
return Err(Error::UnexpectedEof("subchunk extends beyond container bounds"));
|
||||
}
|
||||
|
||||
@@ -93,12 +95,13 @@ pub fn parse_dsdiff_header(bytes: &[u8]) -> Result<(Metadata, usize, usize), Err
|
||||
let mut prop_pos = 4;
|
||||
while prop_pos + 12 <= chunk_data.len() {
|
||||
let sub_id: [u8; 4] = chunk_data[prop_pos..prop_pos + 4].try_into().map_err(|_| Error::UnexpectedEof("PROP subchunk ID"))?;
|
||||
let sub_size = u64::from_be_bytes(
|
||||
let sub_size = usize::try_from(u64::from_be_bytes(
|
||||
chunk_data[prop_pos + 4..prop_pos + 12].try_into().map_err(|_| Error::UnexpectedEof("PROP subchunk size"))?,
|
||||
) as usize;
|
||||
))
|
||||
.map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
prop_pos += 12;
|
||||
|
||||
if prop_pos + sub_size > chunk_data.len() {
|
||||
if sub_size > chunk_data.len().saturating_sub(prop_pos) {
|
||||
return Err(Error::UnexpectedEof("PROP subchunk extends beyond bounds"));
|
||||
}
|
||||
|
||||
@@ -147,11 +150,12 @@ pub fn parse_dsdiff_header(bytes: &[u8]) -> Result<(Metadata, usize, usize), Err
|
||||
let mut diin_pos = 0;
|
||||
while diin_pos + 12 <= chunk_data.len() {
|
||||
let sub_id = &chunk_data[diin_pos..diin_pos + 4];
|
||||
let sub_size = u64::from_be_bytes(
|
||||
let sub_size = usize::try_from(u64::from_be_bytes(
|
||||
chunk_data[diin_pos + 4..diin_pos + 12].try_into().map_err(|_| Error::UnexpectedEof("DIIN subchunk size"))?,
|
||||
) as usize;
|
||||
))
|
||||
.map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
diin_pos += 12;
|
||||
if diin_pos + sub_size > chunk_data.len() {
|
||||
if sub_size > chunk_data.len().saturating_sub(diin_pos) {
|
||||
break;
|
||||
}
|
||||
let sub_data = &chunk_data[diin_pos..diin_pos + sub_size];
|
||||
|
||||
@@ -55,7 +55,7 @@ fn parse_id3v2(bytes: &[u8], offset: usize) -> (Option<String>, Option<String>)
|
||||
}
|
||||
let frame_size = u32::from_be_bytes([id3[pos + 4], id3[pos + 5], id3[pos + 6], id3[pos + 7]]) as usize;
|
||||
pos += 10;
|
||||
if pos + frame_size > end {
|
||||
if frame_size > end.saturating_sub(pos) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ pub fn parse_dsf_header(bytes: &[u8]) -> Result<(Metadata, usize, u32), Error> {
|
||||
return Err(Error::InvalidHeader("invalid 'fmt ' chunk size (must be at least 52)"));
|
||||
}
|
||||
let fmt_size = usize::try_from(fmt_chunk_size).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if bytes.len() < fmt_offset + fmt_size + 12 {
|
||||
if fmt_size > bytes.len().saturating_sub(fmt_offset + 12) {
|
||||
return Err(Error::UnexpectedEof("fmt chunk extends beyond file bounds"));
|
||||
}
|
||||
|
||||
@@ -176,8 +176,8 @@ pub fn parse_dsf_header(bytes: &[u8]) -> Result<(Metadata, usize, u32), Error> {
|
||||
}
|
||||
|
||||
let payload_offset = data_offset + 12;
|
||||
let payload_size = (data_chunk_size - 12) as usize;
|
||||
if bytes.len() < payload_offset + payload_size {
|
||||
let payload_size = usize::try_from(data_chunk_size - 12).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if payload_size > bytes.len().saturating_sub(payload_offset) {
|
||||
return Err(Error::UnexpectedEof("data chunk payload extends beyond file bounds"));
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ pub fn parse_dsf(bytes: &[u8]) -> Result<RawAudio, Error> {
|
||||
let block_size = block_size as usize;
|
||||
let channels = metadata.channels as usize;
|
||||
|
||||
let target_bytes_per_channel = metadata.sample_count.div_ceil(8) as usize;
|
||||
let target_bytes_per_channel = usize::try_from(metadata.sample_count.div_ceil(8)).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if target_bytes_per_channel.saturating_mul(channels) > MAX_DECODE_BYTES {
|
||||
return Err(Error::MemoryLimitExceeded);
|
||||
}
|
||||
|
||||
@@ -106,6 +106,10 @@ impl RawAudio {
|
||||
|
||||
/// Decimates the raw 1-bit DSD audio to multi-bit PCM `f32` samples with the given factor (8, 16, 32, or 64).
|
||||
pub fn to_pcm_with_factor(&self, factor: u32) -> Result<DecodedAudio, Error> {
|
||||
// Validate before iterating: public RawAudio may contain no channels.
|
||||
if !matches!(factor, 8 | 16 | 32 | 64) {
|
||||
return Err(Error::Unsupported("decimation factor must be 8, 16, 32, or 64"));
|
||||
}
|
||||
let mut channels = Vec::with_capacity(self.channel_bytes.len());
|
||||
for ch_bytes in &self.channel_bytes {
|
||||
let pcm = filter::decimate_channel(ch_bytes, self.bit_order, factor)?;
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
//! Hostile container sizes must return errors, never wrap offsets or panic.
|
||||
use craft_audio::dsd::{BitOrder, Error, RawAudio, encode_dsdiff, encode_dsf, read_metadata, read_raw};
|
||||
|
||||
#[test]
|
||||
fn dsf_untrusted_sizes_are_checked() {
|
||||
let original = encode_dsf(2_822_400, &[&[0x69; 8]], 64, 8).unwrap();
|
||||
for offset in [32, 84] {
|
||||
// fmt and data chunk sizes
|
||||
for size in [u64::MAX, u64::MAX - 31, 1 << 32] {
|
||||
let mut bytes = original.clone();
|
||||
bytes[offset..offset + 8].copy_from_slice(&size.to_le_bytes());
|
||||
assert!(read_metadata(&bytes).is_err());
|
||||
assert!(read_raw(&bytes).is_err());
|
||||
}
|
||||
}
|
||||
let mut bytes = original;
|
||||
bytes[64..72].copy_from_slice(&u64::MAX.to_le_bytes());
|
||||
assert_eq!(read_raw(&bytes), Err(Error::MemoryLimitExceeded));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dsdiff_untrusted_top_level_and_property_sizes_are_checked() {
|
||||
let original = encode_dsdiff(2_822_400, &[&[0xaa; 8]], 64).unwrap();
|
||||
let prop = original.windows(4).position(|w| w == b"PROP").unwrap();
|
||||
for offset in [4, 20, prop + 20] {
|
||||
// FRM8, FVER, nested FS
|
||||
for size in [u64::MAX, u64::MAX - 31, 1 << 32] {
|
||||
let mut bytes = original.clone();
|
||||
bytes[offset..offset + 8].copy_from_slice(&size.to_be_bytes());
|
||||
assert!(read_metadata(&bytes).is_err());
|
||||
assert!(read_raw(&bytes).is_err());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dsdiff_invalid_optional_disc_info_is_ignored_without_overflow() {
|
||||
let mut bytes = encode_dsdiff(2_822_400, &[&[0xaa; 8]], 64).unwrap();
|
||||
let expected = read_metadata(&bytes).unwrap();
|
||||
bytes.extend_from_slice(b"DIIN");
|
||||
bytes.extend_from_slice(&12u64.to_be_bytes());
|
||||
bytes.extend_from_slice(b"DITI");
|
||||
bytes.extend_from_slice(&u64::MAX.to_be_bytes());
|
||||
let size = (bytes.len() - 12) as u64;
|
||||
bytes[4..12].copy_from_slice(&size.to_be_bytes());
|
||||
// Optional malformed text is ignored on 64-bit; unrepresentable sizes may
|
||||
// be rejected on narrower targets, but must never wrap or panic.
|
||||
match read_metadata(&bytes) {
|
||||
Ok(actual) => assert_eq!(actual, expected),
|
||||
Err(Error::MemoryLimitExceeded) => {}
|
||||
other => panic!("unexpected result: {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_raw_audio_rejects_invalid_decimation_factors() {
|
||||
let bytes = encode_dsf(2_822_400, &[&[]], 0, 8).unwrap();
|
||||
let raw = RawAudio { metadata: read_metadata(&bytes).unwrap(), bit_order: BitOrder::LsbFirst, channel_bytes: Vec::new() };
|
||||
for factor in [0, 1, 7, 9, u32::MAX] {
|
||||
assert!(matches!(raw.to_pcm_with_factor(factor), Err(Error::Unsupported(_))));
|
||||
}
|
||||
for factor in [8, 16, 32, 64] {
|
||||
assert_eq!(raw.to_pcm_with_factor(factor).unwrap().sample_rate, 2_822_400 / factor);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user