Skip to content

Commit 0eceed2

Browse files
authored
wasi: avoid user-triggerable 0-sized allocations (#4452)
might fix #4451
1 parent 7d05dbc commit 0eceed2

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ wasi_fd_pread(wasm_exec_env_t exec_env, wasi_fd_t fd, iovec_app_t *iovec_app,
375375
return (wasi_errno_t)-1;
376376

377377
total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
378+
if (total_size == 0) {
379+
total_size = 1; /* avoid user-triggered 0-sized allocation */
380+
}
378381
if (total_size >= UINT32_MAX
379382
|| !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
380383
return (wasi_errno_t)-1;
@@ -430,6 +433,9 @@ wasi_fd_pwrite(wasm_exec_env_t exec_env, wasi_fd_t fd,
430433
return (wasi_errno_t)-1;
431434

432435
total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
436+
if (total_size == 0) {
437+
total_size = 1; /* avoid user-triggered 0-sized allocation */
438+
}
433439
if (total_size >= UINT32_MAX
434440
|| !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
435441
return (wasi_errno_t)-1;
@@ -484,6 +490,9 @@ wasi_fd_read(wasm_exec_env_t exec_env, wasi_fd_t fd,
484490
return (wasi_errno_t)-1;
485491

486492
total_size = sizeof(wasi_iovec_t) * (uint64)iovs_len;
493+
if (total_size == 0) {
494+
total_size = 1; /* avoid user-triggered 0-sized allocation */
495+
}
487496
if (total_size >= UINT32_MAX
488497
|| !(iovec_begin = wasm_runtime_malloc((uint32)total_size)))
489498
return (wasi_errno_t)-1;
@@ -654,6 +663,9 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
654663
return (wasi_errno_t)-1;
655664

656665
total_size = sizeof(wasi_ciovec_t) * (uint64)iovs_len;
666+
if (total_size == 0) {
667+
total_size = 1; /* avoid user-triggered 0-sized allocation */
668+
}
657669
if (total_size >= UINT32_MAX
658670
|| !(ciovec_begin = wasm_runtime_malloc((uint32)total_size)))
659671
return (wasi_errno_t)-1;

0 commit comments

Comments
 (0)