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
まだ誰も着手していません。
評価
調査の方向性
src/lib.rs の641〜665行目付近にある as_slice/as_slice_mut から始め、SampleFormat::sample_size を読んで、フォーマットがバイト幅にどう対応づけられるかを確認してください。確認すべき呼び出し元は src/traits.rs の型付きビルダーで(261〜286行目付近と318〜343行目付近)、これらは T: SizedSample をそのまま渡しています。両方の関数に size_of::<T>() == self.sample_format.sample_size() の条件を追加し、幅が一致しない SizedSample 実装をカバーするテストを残して、cargo test を実行してください。ガード条件だけで十分か、それとも SizedSample を seal すべきかは、メンテナーと決めてください。
索引モデルが 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時間
- マージ済み PR(30日)
- 1
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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
-
WebAudio: after a main-thread stall, playback runs at 2x speed until the time cursor catches upオープンhost - webaudio platform - web
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
ci: migrate pinned macos-15 runners to macos-26 and macos-26-intel対応中かも @hawkingrei が今日担当しました。 オープンenhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
nowledge-co/hawdb#2224 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
eclipse-score/coverage_tool#95 ·
メンテナーはふだん 2 日以内に返信
-
bug nvim
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100