Skip to content

Commit fd5cfe4

Browse files
committed
Follow-up to PR bytecodealliance#4300: prevent potential overflow
PR bytecodealliance#4300 introduced the rationale for validating heap_type. This patch moves the validation before the computation of type1 to prevent potential overflow.
1 parent 0343aaf commit fd5cfe4

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

core/iwasm/interpreter/wasm_loader.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,12 +843,28 @@ load_init_expr(WASMModule *module, const uint8 **p_buf, const uint8 *buf_end,
843843
#else
844844
int32 heap_type;
845845
read_leb_int32(p, p_end, heap_type);
846+
847+
/* Validate heap_type before computing type1 to prevent
848+
* potential overflow. */
849+
if (heap_type >= 0) {
850+
if (!check_type_index(module, module->type_count, heap_type,
851+
error_buf, error_buf_size)) {
852+
goto fail;
853+
}
854+
}
855+
else {
856+
if (!wasm_is_valid_heap_type(heap_type)) {
857+
set_error_buf(error_buf, error_buf_size,
858+
"unknown type");
859+
goto fail;
860+
}
861+
}
862+
846863
type1 = (uint8)((int32)0x80 + heap_type);
847864

848865
cur_value.gc_obj = NULL_REF;
849866

850867
if (!is_byte_a_type(type1)
851-
|| !wasm_is_valid_heap_type(heap_type)
852868
|| wasm_is_type_multi_byte_type(type1)) {
853869
p--;
854870
read_leb_uint32(p, p_end, type_idx);

0 commit comments

Comments
 (0)