Skip to content

Commit 0c906eb

Browse files
committed
Fix several oss-fuzz problems
- #69574 UBsan detected an unsigned int overflow issue: ``` unsigned integer overflow: 2684354559 * 2 cannot be represented in type 'uint32' (aka 'unsigned int') ``` - #69576 ASan detected an stack overflow issue: ``` multi-byte-read-stack-buffer-overflow ``` - #69577 ASan detected an assertions about `load_memory_info()` - #69579 ASan OOM - adjust compilation options
1 parent 42ad472 commit 0c906eb

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

core/iwasm/aot/aot_loader.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ loader_malloc(uint64 size, char *error_buf, uint32 error_buf_size)
285285
{
286286
void *mem;
287287

288-
if (size >= UINT32_MAX || !(mem = wasm_runtime_malloc((uint32)size))) {
288+
if (size >= WASM_MEM_ALLOC_MAX_SIZE
289+
|| !(mem = wasm_runtime_malloc((uint32)size))) {
289290
set_error_buf(error_buf, error_buf_size, "allocate memory failed");
290291
return NULL;
291292
}
@@ -367,6 +368,8 @@ get_aot_file_target(AOTTargetInfo *target_info, char *target_buf,
367368
break;
368369
case E_MACHINE_ARM:
369370
case E_MACHINE_AARCH64:
371+
/* TODO: this will make following `strncmp()` ~L392 unnecessary.
372+
* Use const strings here */
370373
machine_type = target_info->arch;
371374
break;
372375
case E_MACHINE_MIPS:
@@ -501,6 +504,11 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end,
501504
read_uint64(p, p_end, target_info.reserved);
502505
read_byte_array(p, p_end, target_info.arch, sizeof(target_info.arch));
503506

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

10341042
read_uint32(buf, buf_end, module->import_memory_count);
10351043
/* We don't support import_memory_count > 0 currently */
1036-
bh_assert(module->import_memory_count == 0);
1044+
if (module->import_memory_count > 0)
1045+
return false;
10371046

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

core/iwasm/common/wasm_memory.c

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

287+
if (size >= WASM_MEM_ALLOC_MAX_SIZE) {
288+
LOG_WARNING("warning: wasm_runtime_malloc with too large size\n");
289+
return NULL;
290+
}
291+
287292
return wasm_runtime_malloc_internal(size);
288293
}
289294

core/iwasm/interpreter/wasm_loader.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,9 +2255,15 @@ load_type_section(const uint8 *buf, const uint8 *buf_end, WASMModule *module,
22552255
static void
22562256
adjust_table_max_size(uint32 init_size, uint32 max_size_flag, uint32 *max_size)
22572257
{
2258-
uint32 default_max_size = init_size * 2 > WASM_TABLE_MAX_SIZE
2259-
? init_size * 2
2260-
: WASM_TABLE_MAX_SIZE;
2258+
uint32 default_max_size;
2259+
2260+
if (UINT32_MAX / 2 > init_size)
2261+
default_max_size = init_size * 2;
2262+
else
2263+
default_max_size = UINT32_MAX;
2264+
2265+
if (default_max_size < WASM_TABLE_MAX_SIZE)
2266+
default_max_size = WASM_TABLE_MAX_SIZE;
22612267

22622268
if (max_size_flag) {
22632269
/* 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)