From f50b903e4afc828bb3943ab09b154c7b2ba20f5a Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 20 Nov 2026 19:25:38 -0800 Subject: [PATCH] Plug-ins: the host refuses a function type past 32 results before validating code Plugin::load validates every function body with wasmparser before wasmi reads the module, and wasmi's EnforcedLimits::strict() limit of 32 parameters and 32 results per function type applied only after that. A module whose type has 1,000 results could make the validator hold 1,000 operand entries for each call to it. check_features now reads the type section first and refuses a function type with more than 32 parameters or results, with wasmi's message, before any function body is validated. --- crates/plugins/src/runtime.rs | 38 +++++++++++++++++++++++++++++++++-- crates/plugins/tests/abi.rs | 17 ++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/crates/plugins/src/runtime.rs b/crates/plugins/src/runtime.rs index 1a25e9e6b0..b4ef0fca25 100644 --- a/crates/plugins/src/runtime.rs +++ b/crates/plugins/src/runtime.rs @@ -4,7 +4,7 @@ use wasmi::{ Config, CustomFuelCosts, EnforcedLimits, Engine, Instance, Memory, Module, Store, StoreLimitsBuilder, TypedFunc, TypedResumableCall, Val, WasmParams, WasmResults, }; -use wasmparser::{Validator, WasmFeatures}; +use wasmparser::{CompositeInnerType, Parser, Payload, Validator, WasmFeatures}; use crate::host::{self, Bridge, Deadline, Host, Report, Run}; use crate::manifest::{MAX_MANIFEST_BYTES, Manifest}; @@ -42,12 +42,46 @@ const WASM_FEATURES: WasmFeatures = WasmFeatures::MUTABLE_GLOBAL .union(WasmFeatures::EXTENDED_CONST) .union(WasmFeatures::FLOATS); +/// The most parameters or results a function type may have: the limit `EnforcedLimits::strict()` +/// sets in wasmi 2.0.0. +const MAX_FUNC_TYPE_VALUES: usize = 32; + /// Validates `bytes` with only [`WASM_FEATURES`] on, before wasmi reads the module. A module that -/// uses any other proposal is refused with the validator's error, which names the proposal. +/// uses any other proposal is refused with the validator's error, which names the proposal. A +/// function type with more than [`MAX_FUNC_TYPE_VALUES`] parameters or results is refused before +/// any function body is validated. fn check_features(bytes: &[u8]) -> Result<()> { + check_func_types(bytes)?; Validator::new_with_features(WASM_FEATURES).validate_all(bytes).map(|_| ()).map_err(|e| Error::Module(e.to_string())) } +/// Refuses a function type in the type section with more than [`MAX_FUNC_TYPE_VALUES`] parameters +/// or results, with wasmi's message. Reading stops at the code section. A section that doesn't +/// parse is left to the validator, which reports it. +fn check_func_types(bytes: &[u8]) -> Result<()> { + for payload in Parser::new(0).parse_all(bytes) { + let types = match payload { + Ok(Payload::TypeSection(types)) => types, + Ok(Payload::CodeSectionStart { .. }) | Err(_) => return Ok(()), + Ok(_) => continue, + }; + for group in types { + let Ok(group) = group else { return Ok(()) }; + for ty in group.types() { + if let CompositeInnerType::Func(f) = &ty.composite_type.inner { + if f.params().len() > MAX_FUNC_TYPE_VALUES { + return Err(Error::Module(format!("a function type exceeds the limit of {MAX_FUNC_TYPE_VALUES} parameters"))); + } + if f.results().len() > MAX_FUNC_TYPE_VALUES { + return Err(Error::Module(format!("a function type exceeds the limit of {MAX_FUNC_TYPE_VALUES} results"))); + } + } + } + } + } + Ok(()) +} + /// Sets each WebAssembly proposal that wasmi's `Config` has a setter for in this build on or off, /// to match [`WASM_FEATURES`]. fn set_features(config: &mut Config) -> &mut Config { diff --git a/crates/plugins/tests/abi.rs b/crates/plugins/tests/abi.rs index ae99846c69..3d359b2a38 100644 --- a/crates/plugins/tests/abi.rs +++ b/crates/plugins/tests/abi.rs @@ -197,6 +197,23 @@ fn a_plugin_may_use_the_listed_webassembly_proposals_and_no_others() { } } +/// A function type with more than 32 parameters or results is refused before any function body is +/// validated: the module's extra function has an invalid body, and the error names the limit. A +/// type with 32 of each loads. +#[test] +fn a_function_type_past_32_values_is_refused_before_any_function_body_is_validated() { + let i32s = |n: usize| vec!["i32"; n].join(" "); + for (word, field) in [("parameters", "param"), ("results", "result")] { + let fits = module(MANIFEST, tpl::ECHO, &format!("(type (func ({field} {})))", i32s(32))); + assert!(Plugin::load(&fits, Limits::default()).is_ok(), "32 {word}"); + let past = module(MANIFEST, tpl::ECHO, &format!("(type (func ({field} {}))) (func (i32.add))", i32s(33))); + match Plugin::load(&past, Limits::default()) { + Err(Error::Module(m)) => assert_eq!(m, format!("a function type exceeds the limit of 32 {word}")), + other => panic!("33 {word}: {:?}", other.err()), + } + } +} + #[test] fn budgets_stop_runaway_plugins() { let spin = ok_module("(loop $l (br $l)) (i64.const 0)");