You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
WaveFormat::parse reads a WAVEFORMATEXTENSIBLE through a &WAVEFORMATEX (safe fn, out-of-bounds read) #65
WaveFormat::parse is a safe function that takes &WAVEFORMATEX (18 bytes), but when wFormatTag == WAVE_FORMAT_EXTENSIBLE and cbSize >= 22 it does
let waveformatextensible:WAVEFORMATEXTENSIBLE = unsafe{
std::ptr::read(std::ptr::from_ref(waveformatex).cast::<WAVEFORMATEXTENSIBLE>())};
i.e. it reads 40 bytes through a reference that only guarantees 18. The SAFETY comment holds for a header that really sits in front of a WAVEFORMATEXTENSIBLE buffer, but the function signature cannot express that, so safe code can trigger an out-of-bounds read:
use wasapi::WaveFormat;use windows::Win32::Media::Audio::{WAVEFORMATEX,WAVE_FORMAT_EXTENSIBLE};let header = WAVEFORMATEX{wFormatTag:WAVE_FORMAT_EXTENSIBLEasu16,nChannels:2,nSamplesPerSec:48000,nAvgBytesPerSec:384000,nBlockAlign:8,wBitsPerSample:32,cbSize:22,};// Reads 22 bytes past the end of `header` (UB in safe code).let _ = WaveFormat::parse(&header);
(wasapi 0.24.0, src/waveformat.rs, parse.)
Possible fixes, in the order I would prefer them:
Make it pub unsafe fn parse(waveformatex: *const WAVEFORMATEX) with the safety contract spelled out (the pointer must address a buffer of at least sizeof(WAVEFORMATEX) + cbSize bytes), and keep the safe entry points as parse_from_blob_bytes(&[u8]) (already there) plus perhaps From<WAVEFORMATEXTENSIBLE>.
Or keep a safe parse that only reads the WAVEFORMATEX it was given and returns Err(UnsupportedFormat) for the extensible tag, pointing callers at the byte-slice parser.
For what it is worth, Device::from_raw already carries unsafe in 0.24, so this is the one remaining place where a safe signature hides a pointer-width assumption. I am not using parse in my own code (I go through the byte-slice path), so there is no urgency on my side. This report was drafted with AI assistance and checked against the 0.24.0 source by hand.
I'm planning to file an informational (unsound) advisory for this in the RustSec advisory-db. It isn't a vulnerability advisory: it records that the safe WaveFormat::parse in 0.20.0 to 0.24.0 was unsound and lists 0.25.0 as the fixed version, so users still on older versions get a warning from cargo audit. Thanks for the quick fix! If you have any concerns or would like anything worded differently, just let me know.
Activity
HEnquist commentedon Sep 21, 2026
Thanks for reporting this. Making the function unsafe is the correct way to handle this. I'll fix it in the next release.
HEnquist commentedon Oct 1, 2026
Fixed in #64 and released in 0.25.0,
parseis now unsafe and takes a raw pointer.115dkk commentedon Oct 7, 2026
I'm planning to file an informational (unsound) advisory for this in the RustSec advisory-db. It isn't a vulnerability advisory: it records that the safe
WaveFormat::parsein 0.20.0 to 0.24.0 was unsound and lists 0.25.0 as the fixed version, so users still on older versions get a warning fromcargo audit. Thanks for the quick fix! If you have any concerns or would like anything worded differently, just let me know.