Skip to content

Commit 40c41d5

Browse files
authored
Fix several issues reported by oss-fuzz (bytecodealliance#3526)
- possible integer overflow in adjust_table_max_size: unsigned integer overflow: 2684354559 * 2 cannot be represented in type 'uint32' - limit max memory size in wasm_runtime_malloc - add more checks in aot loader - adjust compilation options
1 parent 42ad472 commit 40c41d5

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

core/config.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,13 +667,11 @@
667667
#define WASM_ENABLE_FUZZ_TEST 0
668668
#endif
669669

670-
#ifndef WASM_MEM_ALLOC_MAX_SIZE
671670
#if WASM_ENABLE_FUZZ_TEST != 0
671+
#ifndef WASM_MEM_ALLOC_MAX_SIZE
672672
/* In oss-fuzz, the maximum RAM is ~2.5G */
673673
#define WASM_MEM_ALLOC_MAX_SIZE (2U * 1024 * 1024 * 1024)
674-
#else
675-
#define WASM_MEM_ALLOC_MAX_SIZE UINT32_MAX
676-
#endif
677674
#endif
675+
#endif /* WASM_ENABLE_FUZZ_TEST != 0 */
678676

679677
#endif /* end of _CONFIG_H_ */

core/iwasm/aot/aot_loader.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
367367
break;
368368
case E_MACHINE_ARM:
369369
case E_MACHINE_AARCH64:
370+
/* TODO: this will make following `strncmp()` ~L392 unnecessary.
371+
* Use const strings here */
370372
machine_type = target_info->arch;
371373
break;
372374
case E_MACHINE_MIPS:
@@ -501,6 +503,11 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end,
501503
read_uint64(p, p_end, target_info.reserved);
502504
read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
503505

506+
if (target_info.arch[sizeof(target_info.arch) - 1] != '\0') {
507+
set_error_buf(error_buf, error_buf_size, "invalid arch string");
508+
return false;
509+
}
510+
504511
if (p != buf_end) {
505512
set_error_buf(error_buf, error_buf_size, "invalid section size");
506513
return false;
@@ -1033,7 +1040,8 @@ load_memory_info(const uint8 **p_buf, const uint8 *buf_end, AOTModule *module,
10331040

10341041
read_uint32(buf, buf_end, module->import_memory_count);
10351042
/* We don't support import_memory_count > 0 currently */
1036-
bh_assert(module->import_memory_count == 0);
1043+
if (module->import_memory_count > 0)
1044+
return false;
10371045

10381046
read_uint32(buf, buf_end, module->memory_count);
10391047
total_size = sizeof(AOTMemory) * (uint64)module->memory_count;

core/iwasm/common/wasm_memory.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ wasm_runtime_malloc(unsigned int size)
284284
#endif
285285
}
286286

287+
#if WASM_ENABLE_FUZZ_TEST != 0
288+
if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
289+
LOG_WARNING("warning: wasm_runtime_malloc with too large size\n");
290+
return NULL;
291+
}
292+
#endif
293+
287294
return wasm_runtime_malloc_internal(size);
288295
}
289296

core/iwasm/interpreter/wasm_loader.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,7 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
380380
{
381381
void *mem;
382382

383-
if (size >= WASM_MEM_ALLOC_MAX_SIZE
384-
|| !(mem = wasm_runtime_malloc((uint32)size))) {
383+
if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
385384
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
386385
return NULL;
387386
}
@@ -2255,9 +2254,15 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
22552254
static void
22562255
adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
22572256
{
2258-
uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
2259-
? init_size * 2
2260-
: WASM_TABLE_MAX_SIZE;
2257+
uint32 default_max_size;
2258+
2259+
if (UINT32_MAX / 2 > init_size)
2260+
default_max_size = init_size * 2;
2261+
else
2262+
default_max_size = UINT32_MAX;
2263+
2264+
if (default_max_size < WASM_TABLE_MAX_SIZE)
2265+
default_max_size = WASM_TABLE_MAX_SIZE;
22612266

22622267
if (max_size_flag) {
22632268
/* module defines the table limitation */

tests/fuzz/wasm-mutator-fuzz/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,20 @@ string(FIND "${CFLAGS_ENV}" "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION" IN_OSS_
131131
if (IN_OSS_FUZZ EQUAL -1)
132132
message("[ceith]:Enable ASan and UBSan in non-oss-fuzz environment")
133133
add_compile_options(
134-
-fsanitize=signed-integer-overflow
135134
-fprofile-instr-generate -fcoverage-mapping
135+
-fno-sanitize-recover=all
136136
-fsanitize=address,undefined
137+
# reference: https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html
138+
# -fsanitize=undefined: All of the checks listed above other than float-divide-by-zero,
139+
# unsigned-integer-overflow, implicit-conversion, local-bounds and
140+
# the nullability-* group of checks.
141+
#
142+
# for now, we disable below from UBSan
143+
# -alignment
144+
# -implicit-conversion
145+
#
146+
-fsanitize=float-divide-by-zero,unsigned-integer-overflow,local-bounds,nullability
147+
-fno-sanitize=alignment
137148
)
138149
add_link_options(-fsanitize=address -fprofile-instr-generate)
139150
endif ()

0 commit comments

Comments
 (0)