Skip to content

Commit f118492

Browse files
authored
Add integer overflow check for some indices in wasm/aot loader (bytecodealliance#3579)
Check whether the indices overflow UINT32_MAX or not for: - import function count + function count - import global count + global count - import tag count + tag count This PR fixes the issue reported by Oss-fuzz test (#69920).
1 parent 3d4d8e6 commit f118492

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,9 @@ load_global_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
22172217
const uint8 *buf = *p_buf;
22182218

22192219
read_uint32(buf, buf_end, module->global_count);
2220+
if (is_indices_overflow(module->import_global_count, module->global_count,
2221+
error_buf, error_buf_size))
2222+
return false;
22202223

22212224
/* load globals */
22222225
if (module->global_count > 0
@@ -2481,6 +2484,10 @@ load_init_data_section(const uint8 *buf, const uint8 *buf_end,
24812484

24822485
/* load function count and start function index */
24832486
read_uint32(p, p_end, module->func_count);
2487+
if (is_indices_overflow(module->import_func_count, module->func_count,
2488+
error_buf, error_buf_size))
2489+
return false;
2490+
24842491
read_uint32(p, p_end, module->start_func_index);
24852492

24862493
/* check start function index */

core/iwasm/common/wasm_loader_common.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,20 @@ is_valid_func_type(const WASMFuncType *func_type)
9696

9797
return true;
9898
}
99+
100+
/*
101+
* Indices are represented as a u32.
102+
*/
103+
bool
104+
is_indices_overflow(uint32 import, uint32 other, char *error_buf,
105+
uint32 error_buf_size)
106+
{
107+
if (import > UINT32_MAX - other) {
108+
snprintf(error_buf, error_buf_size,
109+
"too many items in the index space(%" PRIu32 "+%" PRIu32 ").",
110+
import, other);
111+
return true;
112+
}
113+
114+
return false;
115+
}

core/iwasm/common/wasm_loader_common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ is_valid_value_type(uint8 value_tpye);
2323
bool
2424
is_valid_func_type(const WASMFuncType *func_type);
2525

26+
bool
27+
is_indices_overflow(uint32 import, uint32 other, char *error_buf,
28+
uint32 error_buf_size);
29+
2630
#ifdef __cplusplus
2731
}
2832
#endif

core/iwasm/interpreter/wasm_loader.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3627,6 +3627,10 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
36273627
return false;
36283628
}
36293629

3630+
if (is_indices_overflow(module->import_function_count, func_count,
3631+
error_buf, error_buf_size))
3632+
return false;
3633+
36303634
if (func_count) {
36313635
module->function_count = func_count;
36323636
total_size = sizeof(WASMFunction *) * (uint64)func_count;
@@ -4022,6 +4026,9 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
40224026
#endif
40234027

40244028
read_leb_uint32(p, p_end, global_count);
4029+
if (is_indices_overflow(module->import_global_count, global_count,
4030+
error_buf, error_buf_size))
4031+
return false;
40254032

40264033
module->global_count = 0;
40274034
if (global_count) {
@@ -4921,6 +4928,10 @@ load_tag_section(const uint8 *buf, const uint8 *buf_end, const uint8 *buf_code,
49214928

49224929
/* get tag count */
49234930
read_leb_uint32(p, p_end, section_tag_count);
4931+
if (is_indices_overflow(module->import_tag_count, section_tag_count,
4932+
error_buf, error_buf_size))
4933+
return false;
4934+
49244935
module->tag_count = section_tag_count;
49254936

49264937
if (section_tag_count) {

core/iwasm/interpreter/wasm_mini_loader.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,8 @@ load_function_section(const uint8 *buf, const uint8 *buf_end,
11391139

11401140
bh_assert(func_count == code_count);
11411141

1142+
bh_assert(module->import_function_count <= UINT32_MAX - func_count);
1143+
11421144
if (func_count) {
11431145
module->function_count = func_count;
11441146
total_size = sizeof(WASMFunction *) * (uint64)func_count;
@@ -1321,6 +1323,8 @@ load_global_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
13211323

13221324
read_leb_uint32(p, p_end, global_count);
13231325

1326+
bh_assert(module->import_global_count <= UINT32_MAX - global_count);
1327+
13241328
module->global_count = 0;
13251329
if (global_count) {
13261330
total_size = sizeof(WASMGlobal) * (uint64)global_count;

0 commit comments

Comments
 (0)