Skip to content

Truly independent web browser

LibWasm: Limit chains of supertypes to a depth of 63

Andreas Klinga2662dff3709
diff --git a/Libraries/LibWasm/Module/src/validator.rs b/Libraries/LibWasm/Module/src/validator.rs
index 8c29913656c..89037398031 100644
--- a/Libraries/LibWasm/Module/src/validator.rs
+++ b/Libraries/LibWasm/Module/src/validator.rs
@@ -143,6 +143,10 @@ const MEMORY64_MAX_PAGES: u64 = 1 << 48;
/// local takes a slot of the function's machine stack frame, so this bounds the frame size a module can ask for.
const MAX_FUNCTION_LOCALS: usize = 50_000;
+/// The most supertypes a type may have above it, as the JS API's implementation limits say. Runtime types hold all of
+/// their supertypes, and subtype checks walk them.
+const MAX_SUBTYPING_DEPTH: u32 = 63;
+
fn invalid<T>(message: impl Into<String>) -> Result<T> {
Err(Error::invalid(message))
}
@@ -2179,6 +2183,8 @@ fn declared_functions(module: &Module) -> HashSet<u32> {
fn validate_types(module: &Module, identities: &mut Vec<u32>) -> Result<()> {
let mut canonicalizer = TypeCanonicalizer::new();
+ // How many supertypes each type has above it.
+ let mut depths: Vec<u32> = Vec::with_capacity(module.types.len());
for group in &module.recursion_groups {
let end = group.start + group.count;
let sub_types = &module.types[group.start as usize..end as usize];
@@ -2219,10 +2225,15 @@ fn validate_types(module: &Module, identities: &mut Vec<u32>) -> Result<()> {
};
for (offset, sub_type) in sub_types.iter().enumerate() {
let index = group.start + offset as u32;
+ let mut depth = 0;
for &supertype in &sub_type.supertypes {
if supertype >= index {
return invalid("unknown type: supertypes must be declared before their subtypes");
}
+ depth = depths[supertype as usize] + 1;
+ if depth > MAX_SUBTYPING_DEPTH {
+ return invalid("subtyping depth is too large");
+ }
let parent = &module.types[supertype as usize];
if parent.is_final {
return invalid("sub type of a final type");
@@ -2231,6 +2242,7 @@ fn validate_types(module: &Module, identities: &mut Vec<u32>) -> Result<()> {
return invalid("sub type does not match its supertype");
}
}
+ depths.push(depth);
}
}
Ok(())
diff --git a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-subtyping-depth-limit.txt b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-subtyping-depth-limit.txt
index f41a924460d..fe728a452f5 100644
--- a/Tests/LibWeb/Text/expected/Wasm/WebAssembly-subtyping-depth-limit.txt
+++ b/Tests/LibWeb/Text/expected/Wasm/WebAssembly-subtyping-depth-limit.txt
@@ -1,2 +1,2 @@
Subtyping depth 63: true
-Subtyping depth 64: true
+Subtyping depth 64: false