Data::as_slice/as_slice_mut validate only T::FORMAT, not the byte width; a safe SizedSample impl with a different size yields OOB reads and writes in the typed stream callbacks
还没有人认领这个 Issue。
评估
调研方向
从 src/lib.rs 中 641-665 行左右的 as_slice/as_slice_mut 开始,阅读 SampleFormat::sample_size,了解格式如何映射为字节宽度。需要核对的调用方是 src/traits.rs 中的类型化 builder(约第 261-286 行和 318-343 行),它们直接传递 T: SizedSample。在两个函数中都添加条件 size_of::<T>() == self.sample_format.sample_size(),保留一个测试,覆盖宽度不匹配的 SizedSample 实现,并运行 cargo test;与维护者商定仅靠该守卫是否足够,还是应当将 SizedSample 设为密封(sealed)。
由索引模型根据 Issue 内容生成。
描述
Data::as_slice/as_slice_mut validate only T::FORMAT, not the byte width; a safe SizedSample impl with a different size yields OOB reads and writes in the typed stream callbacks
Summary
Data::as_slice<T> and as_slice_mut<T> (src/lib.rs:641-665) check only
T::FORMAT == self.sample_format and then build a slice with
slice::from_raw_parts(self.data as *const T, self.len). They never compare size_of::<T>()
with the host buffer's sample width.
SizedSample is a public, safe, unsealed trait. A downstream type can set
const FORMAT: SampleFormat = SampleFormat::F32 while being 16 bytes instead of 4. The guard
passes, and the returned &[T]/&mut [T] spans len * size_of::<T>() bytes over a
len * 4-byte host buffer. The typed stream builders (build_input_stream::<T> /
build_output_stream::<T>, src/traits.rs:261-286 / :318-343) forward their T: SizedSample to
exactly these functions inside the callback, so the victim path is 100% safe Rust.
Reproduction
use cpal::{Data, Sample, SampleFormat, SizedSample, FromSample};
/// 16-byte type whose safe SizedSample impl claims F32.
#[derive(Copy, Clone)]
struct FatF32 { pad: [u8; 16] }
impl Sample for FatF32 {
const EQUILIBRIUM: Self = FatF32 { pad: [0; 16] };
type Signed = i8;
type Float = f32;
}
impl FromSample<FatF32> for i8 { fn from_sample_(_: FatF32) -> i8 { 0 } }
impl FromSample<i8> for FatF32 { fn from_sample_(_: i8) -> FatF32 { FatF32 { pad: [0; 16] } } }
impl FromSample<FatF32> for f32 { fn from_sample_(_: FatF32) -> f32 { 0.0 } }
impl FromSample<f32> for FatF32 { fn from_sample_(_: f32) -> FatF32 { FatF32 { pad: [0; 16] } } }
impl SizedSample for FatF32 { const FORMAT: SampleFormat = SampleFormat::F32; }
// In a typed input-stream callback (safe API):
// let samples: &[FatF32] = data.as_slice::<FatF32>().unwrap();
// let _ = samples[samples.len() - 1].pad; // reads 16 bytes at offset 112..128 of a 32-byte buffer
The full probe implements a safe DeviceTrait whose driver delivers a genuine 8-sample f32
buffer (32 bytes) via the public Data::from_parts (host side satisfies that constructor's
contract). The victim is device.build_input_stream::<FatF32, _, _>(...).
Evidence (cpal 0.18.2):
ASan input heap: heap-buffer-overflow READ of size 16 at cpal-0.18.2/src/traits.rs:277
ASan output heap: heap-buffer-overflow WRITE of size 16 at cpal-0.18.2/src/traits.rs:334
guard-page input: SIGSEGV (exit 139)
guard-page output: SIGSEGV (exit 139)
honest f32 controls (heap input/output): exit 0, ASan clean
Direct Data::as_slice::<FatF32>() on the same f32 buffer reports Some(len=8) with a
128-byte span over the 32-byte allocation (4x over-read); a 1-byte impl passes the same guard
and under-reads.
Root cause
src/lib.rs:641-652:
pub fn as_slice<T>(&self) -> Option<&[T]>
where
T: SizedSample,
{
if T::FORMAT == self.sample_format {
unsafe { Some(std::slice::from_raw_parts(self.data as *const T, self.len)) }
} else {
None
}
}
T::FORMAT names the format, not size_of::<T>(); the trait does not bind the two, and safe
downstream impls can violate the correspondence.
Suggested fix
Add the byte-width check before constructing the slice:
if T::FORMAT == self.sample_format
&& std::mem::size_of::<T>() == self.sample_format.sample_size()
{
...
}
(or seal SizedSample / make it an unsafe trait and document the size requirement as a safety
contract). The same check is needed in as_slice_mut.
Scope note
Verified on cpal 0.18.2 (latest release); the FORMAT-only guard is unchanged on master
44ba7ad (2026-11-21). The typed builders are the recommended API, so the victim path needs no
unsafe code. No open issue matches the SizedSample width hole.
- 主要语言
- Rust
- 星标
- 4k
- 派生
- 554
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 1
环境准备
- 提供 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
RustAudio/cpal 的其他 Issue
-
API design
难度 2/5 1-3 小时 新手友好度 64/100
-
aaudio: dynamic buffer tuning never grows the buffer (set_buffer_size_in_frames reports Err on success)可能已有人在做 @dannyward630 于 39 天前认领。 未关闭host - aaudio platform - android
难度 2/5 1-3 小时 新手友好度 79/100
-
WebAudio wasm build fails with wasm-bindgen panic=unwind closures可能已有人在做 @bogusyogi 于 48 天前认领。 未关闭host - webaudio platform - web
难度 2/5 1-3 小时 新手友好度 75/100
-
PipeWire: dropping callback captures on the realtime worker can terminate the process with SIGXCPU未关闭host - pipewire platform - linux
难度 4/5 3-5 天 新手友好度 45/100
-
host - webaudio platform - web
难度 3/5 1-2 天 新手友好度 72/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 83/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
laravel/rust-framework#864 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
eclipse-score/kyron#249 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 68/100
维护者通常 1 天内回复