Safe custom WitMap length can cause generated Rust bindings to write past a heap allocation
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 22/100
Rechercherichtung
Start with the proof of concept: a custom WitMap whose wit_map_len reports 1 but whose IntoIterator yields two pairs, driven through wit_bindgen::generate! with map_type. Read the generated Rust map-lowering code, where the allocation is sized from wit_map_len and entries are written without a bounds check. Run the PoC under Miri to reproduce the out-of-bounds write. Done means the lowering either checks the yielded count against the reported length or rejects the mismatch safely, with the Miri run clean.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Hi,
I am the Co-Lead of the Cargo Team and was recently hired by the Rust Foundation to help triage AI discovered bugs. This bug was found with AI, namely scrutineer and Astra. Except where specifically labeled this report was written by me. @alexcrichton asked me to file this one as an issue so that it doesn't get lost.
AI text:
Summary
Out-of-bounds heap write in generated Rust bindings with custom WitMap. The safe WitMap customization API allows a custom implementation to report a length inconsistent with its IntoIterator output, but generated Rust map lowering allocates from that reported length and writes every yielded entry without a bounds check. A map reporting one u32/u32 pair and yielding two causes a four-byte write at offset eight of an eight-byte heap allocation, before any host import is called.
Proof of concept
Prerequisites: the Rust nightly toolchain with Miri installed and initialized; the recorded verification and this rerun use nightly-2026-06-24. Save the following two files together. All consumer Rust code is safe.
main.rs:
#[derive(Clone)]
pub struct BadMap<K,V>(Vec<(K,V)>);
impl<K,V> wit_bindgen::rt::WitMap<K,V> for BadMap<K,V> {
fn wit_map_new(_: usize) -> Self { Self(Vec::new()) }
fn wit_map_push(&mut self,k:K,v:V) { self.0.push((k,v)); }
fn wit_map_len(&self) -> usize { 1 }
}
impl<K,V> IntoIterator for BadMap<K,V> {
type Item=(K,V);
type IntoIter=std::vec::IntoIter<(K,V)>;
fn into_iter(self)->Self::IntoIter { self.0.into_iter() }
}
impl<'a,K:Copy,V:Copy> IntoIterator for &'a BadMap<K,V> {
type Item=(K,V);
type IntoIter=std::vec::IntoIter<(K,V)>;
fn into_iter(self)->Self::IntoIter { self.0.clone().into_iter() }
}
wit_bindgen::generate!({
inline: "package test:probe; world probe { import send: func(values: map<u32,u32>); }",
map_type: "crate::BadMap",
});
fn main() {
send(&BadMap(vec![(1,2),(3,4)]));
}
Verbatim diagnostic and top of the Miri stack (other output omitted):
error: Undefined Behavior: memory access failed: attempting to access 4 bytes, but got alloc337+0x8 which is at or beyond the end of the allocation of size 8 bytes
= note: stack backtrace:
0: send
at src/main.rs:18:1: 21:3
1: main
at src/main.rs:23:5: 23:37
- Vorherrschende Sprache
- Rust
- Sterne
- 1.5k
- Forks
- 306
- Ø Merge
- 1 T. 4 Std.
- Gemergte PRs (30 T.)
- 21
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Kein Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus bytecodealliance/wit-bindgen
-
gen-rust
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 25/100
bytecodealliance/wit-bindgen#1791 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-rust
Schwierigkeit 3/5 Ein halber Tag Anfängerfreundlichkeit 55/100
bytecodealliance/wit-bindgen#1787 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-c#
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 22/100
bytecodealliance/wit-bindgen#1759 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Async exports never free the block of indirect parametersEvtl. vergeben @Aditya1404Sal hat das vor 37 Tagen übernommen. Offenasync
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 30/100
bytecodealliance/wit-bindgen#1757 ·
Maintainer antworten meist innerhalb von 1 Tag
-
gen-rust
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 42/100
bytecodealliance/wit-bindgen#1754 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in bytecodealliance/wit-bindgen
Ähnliche Issues
-
lane:collections todo:ticket
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
Maintainer antworten meist innerhalb von 3 Tagen
-
bugfix
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
storytold/designcraft#3662 ·
Maintainer antworten meist innerhalb von 1 Tag
-
documentation
Schwierigkeit 1/5 1-3 Stunden Anfängerfreundlichkeit 88/100
Maintainer antworten meist innerhalb von 1 Tag
-
good first issue tooling
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Nixitov/Riptide-Native#521 ·
Maintainer antworten meist innerhalb von 1 Tag