Skip to content

Commit 2fa0e82

Browse files
Handle out-of-bounds component sections (#8323) (#8338)
* Handle out-of-bounds component sections Fixes #8322 * Add a test that trancated component binaries don't cause panics Co-authored-by: Nick Fitzgerald <fitzgen@gmail.com>
1 parent 5dab110 commit 2fa0e82

2 files changed

Lines changed: 56 additions & 1 deletion

File tree

crates/environ/src/component/translate.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
EntityIndex, ModuleEnvironment, ModuleTranslation, ModuleTypesBuilder, PrimaryMap, Tunables,
55
TypeConvert, WasmHeapType, WasmValType,
66
};
7+
use anyhow::anyhow;
78
use anyhow::{bail, Result};
89
use indexmap::IndexMap;
910
use std::collections::HashMap;
@@ -534,7 +535,18 @@ impl<'a, 'data> Translator<'a, 'data> {
534535
self.validator,
535536
self.types.module_types_builder(),
536537
)
537-
.translate(parser, &component[range.start..range.end])?;
538+
.translate(
539+
parser,
540+
component.get(range.start..range.end).ok_or_else(|| {
541+
anyhow!(
542+
"section range {}..{} is out of bounds (bound = {})",
543+
range.start,
544+
range.end,
545+
component.len()
546+
)
547+
.context("wasm component contains an invalid module section")
548+
})?,
549+
)?;
538550
let static_idx = self.static_modules.push(translation);
539551
self.result
540552
.initializers

tests/all/component_model/aot.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,46 @@ fn detect_precompiled() -> Result<()> {
133133
);
134134
Ok(())
135135
}
136+
137+
#[test]
138+
#[cfg_attr(miri, ignore)]
139+
fn truncated_component_binaries_dont_panic() -> Result<()> {
140+
let engine = super::engine();
141+
142+
let binary = wat::parse_str(
143+
r#"
144+
(component
145+
(import "a" (core module $m0
146+
(import "" "" (func))
147+
))
148+
149+
(core module $m1
150+
(func (export ""))
151+
)
152+
(core instance $i1 (instantiate (module $m1)))
153+
(func $f (canon lift (core func $i1 "f")))
154+
155+
(component $c1
156+
(import "f" (func))
157+
(core module $m2
158+
(func (export "g"))
159+
)
160+
(core instance $i2 (instantiate $m2))
161+
(func (export "g")
162+
(canon lift (core func $i2 "g"))
163+
)
164+
)
165+
(instance $i3 (instantiate $c1 (with "f" (func $f))))
166+
(func (export "g") (alias export $i3 "g"))
167+
)
168+
"#,
169+
)?;
170+
171+
// Check that if we feed each truncation of the component binary into
172+
// `Component::new` we don't get any panics.
173+
for i in 1..binary.len() - 1 {
174+
let _ = Component::from_binary(&engine, &binary[0..i]);
175+
}
176+
177+
Ok(())
178+
}

0 commit comments

Comments
 (0)