From 68c0f034d507d104a3aff2ed46f01bd800a0af6a Mon Sep 17 00:00:00 2001 From: David Mann Date: Mon, 26 Oct 2026 14:30:56 +0100 Subject: [PATCH] Plug-ins: refuse a code section with no functions but many bytes instead of panicking (#9679) wasmi 2.0's strict limits divide the code section's size by its function count before validating it, so a module whose code section declares 0 functions but more than 1000 bytes (crafted, truncated inside the section, or a duplicate code section) panicked in Plugin::load. Check every code section for that one case before handing the bytes to wasmi and return Error::Module; everything else is still left to wasmi. Co-Authored-By: Claude Opus 5.5 (1M context) --- crates/plugins/src/runtime.rs | 37 +++++++++++++++++++++++++++++++++++ crates/plugins/tests/abi.rs | 19 ++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/crates/plugins/src/runtime.rs b/crates/plugins/src/runtime.rs index c82171a5ac5..9f66b545ba6 100644 --- a/crates/plugins/src/runtime.rs +++ b/crates/plugins/src/runtime.rs @@ -115,6 +115,42 @@ fn wasm_err(e: wasmi::Error) -> Error { if e.as_trap_code() == Some(wasmi::TrapCode::OutOfFuel) { Error::Limit("instruction budget".into()) } else { Error::Trap(e.to_string()) } } +/// Reads an unsigned LEB128 `u32` at the start of `bytes`: the value and its length. +fn leb_u32(bytes: &[u8]) -> Option<(u32, usize)> { + let mut value = 0u32; + for (i, &b) in bytes.iter().take(5).enumerate() { + value |= u32::from(b & 0x7f).checked_shl(7 * i as u32)?; + if b & 0x80 == 0 { + return Some((value, i + 1)); + } + } + None +} + +/// Refuses a code section that declares no functions but more bytes than that count. The spec +/// makes the count the whole section then; wasmi 2.0's strict limits divide the section size by +/// the function count before validating it, so such a module (crafted or truncated) panicked. +/// Every code section is checked, since a duplicate one also reaches that division before +/// validation refuses it. Anything else, malformed or not, is left to wasmi. +fn check_code_section(bytes: &[u8]) -> Result<()> { + if bytes.get(4..8) != Some(&[1, 0, 0, 0][..]) { + return Ok(()); + } + let mut pos = 8usize; + while let Some(&id) = bytes.get(pos) { + let Some((size, len)) = bytes.get(pos + 1..).and_then(leb_u32) else { return Ok(()) }; + let start = pos + 1 + len; + if id == 10 + && let Some((0, count_len)) = bytes.get(start..).and_then(leb_u32) + && size as usize > count_len + { + return Err(Error::Module(format!("the code section declares no functions but {size} bytes"))); + } + pos = start.saturating_add(size as usize); + } + Ok(()) +} + impl Plugin { /// Validates and loads a module: checks the ABI version and reads the manifest. pub fn load(bytes: &[u8], limits: Limits) -> Result { @@ -124,6 +160,7 @@ impl Plugin { if !bytes.starts_with(b"\0asm") { return Err(Error::Module("not a WebAssembly binary (missing \\0asm header)".into())); } + check_code_section(bytes)?; let mut config = Config::default(); config.consume_fuel(true).enforced_limits(EnforcedLimits::strict()).set_max_recursion_depth(limits.max_recursion_depth.max(16)); let engine = Engine::new(&config); diff --git a/crates/plugins/tests/abi.rs b/crates/plugins/tests/abi.rs index 61ee32791fd..17e352982e3 100644 --- a/crates/plugins/tests/abi.rs +++ b/crates/plugins/tests/abi.rs @@ -219,6 +219,25 @@ fn malformed_and_oversized_modules_are_refused() { assert!(matches!(e, Error::Abi(_)), "{e}"); } +#[test] +fn a_code_section_claiming_no_functions_but_many_bytes_is_refused() { + // Header, then a code section (id 10) declaring 2000 bytes whose function count is 0, cut off + // after the count (a truncated download) or padded to its full size. The spec makes the + // count's own byte the whole section; wasmi's strict limits divided the size by the count. + let truncated = b"\0asm\x01\0\0\0\x0a\xd0\x0f\x00".to_vec(); + let mut padded = truncated.clone(); + padded.resize(padded.len() + 1999, 0); + // The same section after a valid empty one: a duplicate reaches the division before validation. + let mut second = b"\0asm\x01\0\0\0\x0a\x01\x00".to_vec(); + second.extend_from_slice(&padded[8..]); + for (name, bytes) in [("truncated", truncated), ("padded", padded), ("second", second)] { + assert!(matches!(Plugin::load(&bytes, Limits::default()), Err(Error::Module(_))), "{name}"); + } + // An empty code section (count 0, nothing after it) is valid; it fails only for the missing ABI exports. + let empty = b"\0asm\x01\0\0\0\x0a\x01\x00"; + assert!(matches!(Plugin::load(empty, Limits::default()), Err(Error::Abi(_)))); +} + #[test] fn budgets_stop_runaway_plugins() { let spin = ok_module("(loop $l (br $l)) (i32.const 0)");