Skip to content

Commit 3bfca68

Browse files
committed
add validation for struct field type
1 parent 5d15f8f commit 3bfca68

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
17871787
read_uint32(buf, buf_end, j);
17881788
#if WASM_ENABLE_AOT_VALIDATOR != 0
17891789
/* an equivalence type should be before the type it refers to */
1790-
if (j > i) {
1790+
if (j >= i) {
17911791
set_error_buf(error_buf, error_buf_size, "invalid type index");
17921792
goto fail;
17931793
}
@@ -1964,6 +1964,13 @@ load_types(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
19641964

19651965
read_uint8(buf, buf_end, struct_type->fields[j].field_flags);
19661966
read_uint8(buf, buf_end, field_type);
1967+
#if WASM_ENABLE_AOT_VALIDATOR != 0
1968+
if (!is_valid_field_type(field_type)) {
1969+
set_error_buf(error_buf, error_buf_size,
1970+
"invalid field type");
1971+
goto fail;
1972+
}
1973+
#endif
19671974
struct_type->fields[j].field_type = field_type;
19681975
struct_type->fields[j].field_size = field_size =
19691976
(uint8)wasm_reftype_size(field_type);

core/iwasm/common/wasm_loader_common.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@ is_valid_func_type(const WASMFuncType *func_type)
179179
return true;
180180
}
181181

182+
bool
183+
is_valid_packed_type(uint8 packed_type)
184+
{
185+
return packed_type == PACKED_TYPE_I8 || packed_type == PACKED_TYPE_I16;
186+
}
187+
188+
bool
189+
is_valid_field_type(uint8 field_type)
190+
{
191+
if (is_valid_value_type(field_type) || is_valid_packed_type(field_type))
192+
return true;
193+
return false;
194+
}
195+
182196
/*
183197
* Indices are represented as a u32.
184198
*/

core/iwasm/common/wasm_loader_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ is_valid_value_type_for_interpreter(uint8 value_tpye);
3838
bool
3939
is_valid_func_type(const WASMFuncType *func_type);
4040

41+
bool
42+
is_valid_packed_type(uint8 packed_type);
43+
44+
bool
45+
is_valid_field_type(uint8 field_type);
46+
4147
bool
4248
is_indices_overflow(uint32 import, uint32 other, char *error_buf,
4349
uint32 error_buf_size);

core/iwasm/interpreter/wasm_loader.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,6 +1961,10 @@ resolve_struct_type(const uint8 **p_buf, const uint8 *buf_end,
19611961
error_buf_size)) {
19621962
goto fail;
19631963
}
1964+
if (!is_valid_field_type(ref_type.ref_type)) {
1965+
set_error_buf(error_buf, error_buf_size, "invalid field type");
1966+
goto fail;
1967+
}
19641968
type->fields[i].field_type = ref_type.ref_type;
19651969
if (need_ref_type_map) {
19661970
type->ref_type_maps[j].index = i;

0 commit comments

Comments
 (0)