mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:22:39 +00:00
craft-audio: validate DSF container bounds and DSDIFF channel counts (#1787)
This commit is contained in:
@@ -142,20 +142,36 @@ pub fn parse_dsdiff_header(bytes: &[u8]) -> Result<(Metadata, usize, usize), Err
|
||||
|
||||
match &sub_id {
|
||||
b"FS " => {
|
||||
if sub_data.len() >= 4 {
|
||||
sample_rate = Some(u32::from_be_bytes([sub_data[0], sub_data[1], sub_data[2], sub_data[3]]));
|
||||
if sub_data.len() < 4 {
|
||||
return Err(Error::UnexpectedEof("truncated FS chunk"));
|
||||
}
|
||||
if sample_rate.is_some() {
|
||||
return Err(Error::InvalidHeader("duplicate FS chunk"));
|
||||
}
|
||||
let sr = u32::from_be_bytes([sub_data[0], sub_data[1], sub_data[2], sub_data[3]]);
|
||||
if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sr) {
|
||||
return Err(Error::InvalidHeader("sampling frequency out of range"));
|
||||
}
|
||||
sample_rate = Some(sr);
|
||||
}
|
||||
b"CHNL" => {
|
||||
if sub_data.len() >= 2 {
|
||||
let n = u16::from_be_bytes([sub_data[0], sub_data[1]]);
|
||||
num_channels = Some(n);
|
||||
let mut ch_pos = 2;
|
||||
while ch_pos + 4 <= sub_data.len() && channel_names.len() < n as usize {
|
||||
let id: [u8; 4] = [sub_data[ch_pos], sub_data[ch_pos + 1], sub_data[ch_pos + 2], sub_data[ch_pos + 3]];
|
||||
channel_names.push(channel_id_to_name(&id));
|
||||
ch_pos += 4;
|
||||
}
|
||||
if sub_data.len() < 2 {
|
||||
return Err(Error::UnexpectedEof("truncated CHNL chunk"));
|
||||
}
|
||||
if num_channels.is_some() {
|
||||
return Err(Error::InvalidHeader("duplicate CHNL chunk"));
|
||||
}
|
||||
let n = u16::from_be_bytes([sub_data[0], sub_data[1]]);
|
||||
if n == 0 || n > MAX_CHANNELS as u16 {
|
||||
return Err(Error::Unsupported("channel count must be between 1 and 8"));
|
||||
}
|
||||
num_channels = Some(n);
|
||||
channel_names.clear();
|
||||
let mut ch_pos = 2;
|
||||
while ch_pos + 4 <= sub_data.len() && channel_names.len() < n as usize {
|
||||
let id: [u8; 4] = [sub_data[ch_pos], sub_data[ch_pos + 1], sub_data[ch_pos + 2], sub_data[ch_pos + 3]];
|
||||
channel_names.push(channel_id_to_name(&id));
|
||||
ch_pos += 4;
|
||||
}
|
||||
}
|
||||
b"CMPR" if sub_data.len() >= 4 => {
|
||||
|
||||
+42
-18
@@ -137,79 +137,103 @@ fn parse_dsf_header_bounded(bytes: &[u8]) -> Result<(Metadata, usize, usize, u32
|
||||
if dsd_chunk_size != 28 {
|
||||
return Err(Error::InvalidHeader("invalid 'DSD ' chunk size (must be 28)"));
|
||||
}
|
||||
let _file_size = u64::from_le_bytes(bytes[12..20].try_into().map_err(|_| Error::UnexpectedEof("file size"))?);
|
||||
let file_size = u64::from_le_bytes(bytes[12..20].try_into().map_err(|_| Error::UnexpectedEof("file size"))?);
|
||||
let metadata_offset = u64::from_le_bytes(bytes[20..28].try_into().map_err(|_| Error::UnexpectedEof("metadata offset"))?);
|
||||
|
||||
if file_size > bytes.len() as u64 {
|
||||
return Err(Error::UnexpectedEof("declared file size extends beyond buffer"));
|
||||
}
|
||||
let file_len = usize::try_from(file_size).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if file_len < 28 + 52 + 12 {
|
||||
return Err(Error::InvalidHeader("declared file size is smaller than required headers"));
|
||||
}
|
||||
let file_bytes = &bytes[..file_len];
|
||||
|
||||
// 2. 'fmt ' chunk
|
||||
let fmt_offset = 28;
|
||||
if &bytes[fmt_offset..fmt_offset + 4] != b"fmt " {
|
||||
if &file_bytes[fmt_offset..fmt_offset + 4] != b"fmt " {
|
||||
return Err(Error::InvalidHeader("missing 'fmt ' chunk header"));
|
||||
}
|
||||
let fmt_chunk_size = u64::from_le_bytes(bytes[fmt_offset + 4..fmt_offset + 12].try_into().map_err(|_| Error::UnexpectedEof("fmt chunk size"))?);
|
||||
let fmt_chunk_size =
|
||||
u64::from_le_bytes(file_bytes[fmt_offset + 4..fmt_offset + 12].try_into().map_err(|_| Error::UnexpectedEof("fmt chunk size"))?);
|
||||
if fmt_chunk_size < 52 {
|
||||
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 fmt_size > bytes.len().saturating_sub(fmt_offset + 12) {
|
||||
if fmt_size > file_bytes.len().saturating_sub(fmt_offset) {
|
||||
return Err(Error::UnexpectedEof("fmt chunk extends beyond file bounds"));
|
||||
}
|
||||
|
||||
let format_version = u32::from_le_bytes(bytes[fmt_offset + 12..fmt_offset + 16].try_into().map_err(|_| Error::UnexpectedEof("format version"))?);
|
||||
let format_version =
|
||||
u32::from_le_bytes(file_bytes[fmt_offset + 12..fmt_offset + 16].try_into().map_err(|_| Error::UnexpectedEof("format version"))?);
|
||||
if format_version != 1 {
|
||||
return Err(Error::Unsupported("unsupported DSF format version (only version 1 is supported)"));
|
||||
}
|
||||
|
||||
let format_id = u32::from_le_bytes(bytes[fmt_offset + 16..fmt_offset + 20].try_into().map_err(|_| Error::UnexpectedEof("format id"))?);
|
||||
let format_id = u32::from_le_bytes(file_bytes[fmt_offset + 16..fmt_offset + 20].try_into().map_err(|_| Error::UnexpectedEof("format id"))?);
|
||||
if format_id != 0 {
|
||||
return Err(Error::Unsupported("unsupported DSF format id (only raw DSD id=0 is supported)"));
|
||||
}
|
||||
|
||||
let channel_type = u32::from_le_bytes(bytes[fmt_offset + 20..fmt_offset + 24].try_into().map_err(|_| Error::UnexpectedEof("channel type"))?);
|
||||
let channel_count = u32::from_le_bytes(bytes[fmt_offset + 24..fmt_offset + 28].try_into().map_err(|_| Error::UnexpectedEof("channel count"))?);
|
||||
let channel_type = u32::from_le_bytes(file_bytes[fmt_offset + 20..fmt_offset + 24].try_into().map_err(|_| Error::UnexpectedEof("channel type"))?);
|
||||
let channel_count =
|
||||
u32::from_le_bytes(file_bytes[fmt_offset + 24..fmt_offset + 28].try_into().map_err(|_| Error::UnexpectedEof("channel count"))?);
|
||||
if channel_count == 0 || channel_count > MAX_CHANNELS as u32 {
|
||||
return Err(Error::Unsupported("DSF channel count must be between 1 and 8"));
|
||||
}
|
||||
let channels = channel_count as u16;
|
||||
|
||||
let sample_rate = u32::from_le_bytes(bytes[fmt_offset + 28..fmt_offset + 32].try_into().map_err(|_| Error::UnexpectedEof("sampling frequency"))?);
|
||||
let sample_rate =
|
||||
u32::from_le_bytes(file_bytes[fmt_offset + 28..fmt_offset + 32].try_into().map_err(|_| Error::UnexpectedEof("sampling frequency"))?);
|
||||
if !(MIN_SAMPLE_RATE..=MAX_SAMPLE_RATE).contains(&sample_rate) {
|
||||
return Err(Error::InvalidHeader("DSF sampling frequency out of valid range"));
|
||||
}
|
||||
|
||||
let bits_per_sample =
|
||||
u32::from_le_bytes(bytes[fmt_offset + 32..fmt_offset + 36].try_into().map_err(|_| Error::UnexpectedEof("bits per sample"))?);
|
||||
u32::from_le_bytes(file_bytes[fmt_offset + 32..fmt_offset + 36].try_into().map_err(|_| Error::UnexpectedEof("bits per sample"))?);
|
||||
if bits_per_sample != 1 {
|
||||
return Err(Error::Unsupported("DSF bits per sample must be 1"));
|
||||
}
|
||||
|
||||
let sample_count = u64::from_le_bytes(bytes[fmt_offset + 36..fmt_offset + 44].try_into().map_err(|_| Error::UnexpectedEof("sample count"))?);
|
||||
let sample_count = u64::from_le_bytes(file_bytes[fmt_offset + 36..fmt_offset + 44].try_into().map_err(|_| Error::UnexpectedEof("sample count"))?);
|
||||
|
||||
let block_size_per_channel =
|
||||
u32::from_le_bytes(bytes[fmt_offset + 44..fmt_offset + 48].try_into().map_err(|_| Error::UnexpectedEof("block size per channel"))?);
|
||||
u32::from_le_bytes(file_bytes[fmt_offset + 44..fmt_offset + 48].try_into().map_err(|_| Error::UnexpectedEof("block size per channel"))?);
|
||||
if block_size_per_channel == 0 || block_size_per_channel > MAX_BLOCK_SIZE {
|
||||
return Err(Error::InvalidHeader("DSF block size per channel is invalid or too large"));
|
||||
}
|
||||
|
||||
// 3. 'data' chunk
|
||||
let data_offset = fmt_offset + fmt_size;
|
||||
if &bytes[data_offset..data_offset + 4] != b"data" {
|
||||
let data_offset = fmt_offset.saturating_add(fmt_size);
|
||||
if data_offset.saturating_add(12) > file_bytes.len() {
|
||||
return Err(Error::UnexpectedEof("missing 'data' chunk header"));
|
||||
}
|
||||
if &file_bytes[data_offset..data_offset + 4] != b"data" {
|
||||
return Err(Error::InvalidHeader("missing 'data' chunk header"));
|
||||
}
|
||||
let data_chunk_size =
|
||||
u64::from_le_bytes(bytes[data_offset + 4..data_offset + 12].try_into().map_err(|_| Error::UnexpectedEof("data chunk size"))?);
|
||||
u64::from_le_bytes(file_bytes[data_offset + 4..data_offset + 12].try_into().map_err(|_| Error::UnexpectedEof("data chunk size"))?);
|
||||
if data_chunk_size < 12 {
|
||||
return Err(Error::InvalidHeader("invalid 'data' chunk size (must be at least 12)"));
|
||||
}
|
||||
|
||||
let payload_offset = data_offset + 12;
|
||||
let payload_size = usize::try_from(data_chunk_size - 12).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if payload_size > bytes.len().saturating_sub(payload_offset) {
|
||||
if payload_size > file_bytes.len().saturating_sub(payload_offset) {
|
||||
return Err(Error::UnexpectedEof("data chunk payload extends beyond file bounds"));
|
||||
}
|
||||
let data_end = payload_offset.saturating_add(payload_size);
|
||||
|
||||
// Parse ID3v2 metadata if pointer is valid.
|
||||
let (title, artist) =
|
||||
if metadata_offset > 0 && metadata_offset < bytes.len() as u64 { parse_id3v2(bytes, metadata_offset as usize) } else { (None, None) };
|
||||
let (title, artist) = if metadata_offset == 0 {
|
||||
(None, None)
|
||||
} else {
|
||||
let meta_offset = usize::try_from(metadata_offset).map_err(|_| Error::MemoryLimitExceeded)?;
|
||||
if meta_offset < data_end || meta_offset >= file_len {
|
||||
return Err(Error::InvalidHeader("DSF metadata offset outside declared file bounds"));
|
||||
}
|
||||
parse_id3v2(file_bytes, meta_offset)
|
||||
};
|
||||
|
||||
let channel_names = dsf_channel_names(channel_type, channels);
|
||||
|
||||
|
||||
@@ -127,6 +127,78 @@ fn dsdiff_odd_payload_receives_pad_byte() {
|
||||
assert_eq!(raw.channel_bytes[0].len(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dsf_declared_file_size_and_metadata_pointer_bounds() {
|
||||
let valid = encode_dsf(2_822_400, &[&[0x69; 8]], 64, 8).unwrap();
|
||||
|
||||
// Undersized file size declaration (e.g. 28)
|
||||
let mut undersized = valid.clone();
|
||||
undersized[12..20].copy_from_slice(&28u64.to_le_bytes());
|
||||
assert!(read_metadata(&undersized).is_err());
|
||||
assert!(read_raw(&undersized).is_err());
|
||||
|
||||
// Oversized file size declaration beyond buffer
|
||||
let mut oversized = valid.clone();
|
||||
let big_size = valid.len() as u64 + 100;
|
||||
oversized[12..20].copy_from_slice(&big_size.to_le_bytes());
|
||||
assert!(read_metadata(&oversized).is_err());
|
||||
assert!(read_raw(&oversized).is_err());
|
||||
|
||||
// Metadata pointer outside the declared file bounds
|
||||
let mut outside_meta = valid.clone();
|
||||
let outside_offset = valid.len() as u64 + 10;
|
||||
outside_meta[20..28].copy_from_slice(&outside_offset.to_le_bytes());
|
||||
assert!(read_metadata(&outside_meta).is_err());
|
||||
assert!(read_raw(&outside_meta).is_err());
|
||||
|
||||
// Metadata pointer overlapping data chunk
|
||||
let mut overlap_meta = valid.clone();
|
||||
overlap_meta[20..28].copy_from_slice(&32u64.to_le_bytes());
|
||||
assert!(read_metadata(&overlap_meta).is_err());
|
||||
assert!(read_raw(&overlap_meta).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dsdiff_channel_count_and_duplicate_chunks_are_validated() {
|
||||
let base = encode_dsdiff(2_822_400, &[&[0xaa; 8]], 64).unwrap();
|
||||
|
||||
// Locate PROP chunk payload and CHNL subchunk
|
||||
let prop_idx = base.windows(4).position(|w| w == b"PROP").unwrap();
|
||||
let chnl_idx = base.windows(4).position(|w| w == b"CHNL").unwrap();
|
||||
|
||||
// 1. Oversized channel count (e.g. 65535 or 9)
|
||||
let mut bad_count = base.clone();
|
||||
bad_count[chnl_idx + 12..chnl_idx + 14].copy_from_slice(&65535u16.to_be_bytes());
|
||||
assert!(matches!(read_metadata(&bad_count), Err(Error::Unsupported(_))));
|
||||
assert!(matches!(read_raw(&bad_count), Err(Error::Unsupported(_))));
|
||||
|
||||
let mut zero_count = base.clone();
|
||||
zero_count[chnl_idx + 12..chnl_idx + 14].copy_from_slice(&0u16.to_be_bytes());
|
||||
assert!(matches!(read_metadata(&zero_count), Err(Error::Unsupported(_))));
|
||||
|
||||
// 2. Conflicting duplicate CHNL chunk in PROP
|
||||
let mut dup_chnl = base.clone();
|
||||
let chnl_chunk = dup_chnl[chnl_idx..chnl_idx + 18].to_vec();
|
||||
dup_chnl.splice(chnl_idx..chnl_idx, chnl_chunk);
|
||||
// Adjust PROP size and FRM8 size
|
||||
let prop_size = u64::from_be_bytes(dup_chnl[prop_idx + 4..prop_idx + 12].try_into().unwrap()) + 18;
|
||||
dup_chnl[prop_idx + 4..prop_idx + 12].copy_from_slice(&prop_size.to_be_bytes());
|
||||
let frm8_size = (dup_chnl.len() - 12) as u64;
|
||||
dup_chnl[4..12].copy_from_slice(&frm8_size.to_be_bytes());
|
||||
assert_eq!(read_metadata(&dup_chnl), Err(Error::InvalidHeader("duplicate CHNL chunk")));
|
||||
|
||||
// 3. Duplicate FS chunk in PROP
|
||||
let fs_idx = base.windows(4).position(|w| w == b"FS ").unwrap();
|
||||
let mut dup_fs = base.clone();
|
||||
let fs_chunk = dup_fs[fs_idx..fs_idx + 16].to_vec();
|
||||
dup_fs.splice(fs_idx..fs_idx, fs_chunk);
|
||||
let prop_size = u64::from_be_bytes(dup_fs[prop_idx + 4..prop_idx + 12].try_into().unwrap()) + 16;
|
||||
dup_fs[prop_idx + 4..prop_idx + 12].copy_from_slice(&prop_size.to_be_bytes());
|
||||
let frm8_size = (dup_fs.len() - 12) as u64;
|
||||
dup_fs[4..12].copy_from_slice(&frm8_size.to_be_bytes());
|
||||
assert_eq!(read_metadata(&dup_fs), Err(Error::InvalidHeader("duplicate FS chunk")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(target_pointer_width = "32")]
|
||||
fn dsdiff_32bit_padding_overflow_is_rejected() {
|
||||
|
||||
@@ -113,6 +113,8 @@ fn dsf_with_tag(tag: &[u8]) -> Vec<u8> {
|
||||
let offset = bytes.len() as u64;
|
||||
bytes[20..28].copy_from_slice(&offset.to_le_bytes());
|
||||
bytes.extend_from_slice(tag);
|
||||
let file_size = bytes.len() as u64;
|
||||
bytes[12..20].copy_from_slice(&file_size.to_le_bytes());
|
||||
bytes
|
||||
}
|
||||
|
||||
|
||||
@@ -221,7 +221,9 @@ fn id3v2_tag_in_dsf() {
|
||||
id3.extend_from_slice(&frames);
|
||||
|
||||
dsf.extend_from_slice(&id3);
|
||||
// Update metadata_offset at offset 20 of 'DSD ' chunk
|
||||
// Update file size and metadata_offset of 'DSD ' chunk
|
||||
let file_size = dsf.len() as u64;
|
||||
dsf[12..20].copy_from_slice(&file_size.to_le_bytes());
|
||||
dsf[20..28].copy_from_slice(&metadata_offset.to_le_bytes());
|
||||
|
||||
let meta = read_metadata(&dsf).expect("read metadata with ID3");
|
||||
|
||||
Reference in New Issue
Block a user