Safe custom WitMap length can cause generated Rust bindings to write past a heap allocation

Aperta
#1,790 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
22/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust, wasm

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

gen-rust

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
Lingua principale
Rust
Stelle
1.5k
Fork
306
Merge medio
1g 4h
PR unite (30g)
21

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

  • Nessun Dockerfile né file Docker Compose
  • Nessun modello di pull request
  • Nessuna guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bytecodealliance/wit-bindgen

Tutte le issue di bytecodealliance/wit-bindgen

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.