Skip to content

Commit e6317b4

Browse files
authored
Limit the element count of fixed-length lists (#2537)
Fixed-length lists were only checked for zero elements, with no upper bound, so a list of up to 4294967295 elements passed validation. The count then flows to consumers and overflows their canonical ABI size calculations. Cap the element count at 1 Gi, the limit suggested on the issue. Closes #2416.
1 parent d66d436 commit e6317b4

4 files changed

Lines changed: 38 additions & 6 deletions

File tree

crates/wasmparser/src/limits.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ mod component_limits {
7272
pub const MAX_WASM_RECORD_FIELDS: usize = 10_000;
7373
pub const MAX_WASM_VARIANT_CASES: usize = 10_000;
7474
pub const MAX_WASM_TUPLE_TYPES: usize = 10_000;
75+
/// Fixed-length lists don't contribute their length to the component
76+
/// type-size budget, so the element count gets a separate limit of 1 Gi.
77+
pub const MAX_WASM_FIXED_LENGTH_LIST_ELEMENTS: usize = 1_073_741_824;
7578
pub const MAX_WASM_FLAG_NAMES: usize = 1_000;
7679
pub const MAX_WASM_ENUM_CASES: usize = 10_000;
7780
pub const MAX_WASM_INSTANTIATION_EXPORTS: usize = 100_000;

crates/wasmparser/src/validator/component.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4069,6 +4069,13 @@ impl ComponentState {
40694069
"Fixed-length lists must have more than zero elements"
40704070
)
40714071
}
4072+
check_max(
4073+
0,
4074+
elements,
4075+
MAX_WASM_FIXED_LENGTH_LIST_ELEMENTS,
4076+
"fixed-length list element",
4077+
offset,
4078+
)?;
40724079
Ok(ComponentDefinedType::FixedLengthList(
40734080
self.create_component_val_type(ty, offset)?,
40744081
elements,

tests/cli/component-model/fixed-length-lists.wast

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@
3838
"Fixed-length lists must have more than zero elements (at offset 0x54)"
3939
)
4040

41+
(assert_invalid
42+
(component
43+
(core module $m
44+
(memory (export "memory") 1)
45+
(func (export "ret-list") (result i32) unreachable)
46+
)
47+
(core instance $i (instantiate $m))
48+
49+
(func (export "ret-list") (result (list u32 4294967295))
50+
(canon lift (core func $i "ret-list") (memory $i "memory"))
51+
)
52+
)
53+
"fixed-length list element count exceeds limit of 1073741824 (at offset 0x54)"
54+
)
55+
4156
(assert_malformed
4257
(component quote
4358
"(core module $m"

tests/snapshots/cli/component-model/fixed-length-lists.wast.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,30 @@
2121
"text": "Fixed-length lists must have more than zero elements (at offset 0x54)"
2222
},
2323
{
24-
"type": "assert_malformed",
24+
"type": "assert_invalid",
2525
"line": 42,
26-
"filename": "fixed-length-lists.3.wat",
26+
"filename": "fixed-length-lists.3.wasm",
27+
"module_type": "binary",
28+
"text": "fixed-length list element count exceeds limit of 1073741824 (at offset 0x54)"
29+
},
30+
{
31+
"type": "assert_malformed",
32+
"line": 57,
33+
"filename": "fixed-length-lists.4.wat",
2734
"module_type": "text",
2835
"text": "invalid u32 number: constant out of range"
2936
},
3037
{
3138
"type": "assert_invalid",
32-
"line": 57,
33-
"filename": "fixed-length-lists.4.wasm",
39+
"line": 72,
40+
"filename": "fixed-length-lists.5.wasm",
3441
"module_type": "binary",
3542
"text": "type mismatch for import `x`"
3643
},
3744
{
3845
"type": "assert_invalid",
39-
"line": 70,
40-
"filename": "fixed-length-lists.5.wasm",
46+
"line": 85,
47+
"filename": "fixed-length-lists.6.wasm",
4148
"module_type": "binary",
4249
"text": "type mismatch for import `x`"
4350
}

0 commit comments

Comments
 (0)