Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions core/iwasm/common/wasm_runtime_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3812,7 +3812,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,

/* addr_pool(textual) -> apool */
for (i = 0; i < addr_pool_size; i++) {
char *cp, *address, *mask;
char *cp, *address, *mask, *endptr;
long mask_val;
bool ret = false;

cp = bh_strdup(addr_pool[i]);
Expand All @@ -3833,7 +3834,21 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
goto fail;
}

ret = addr_pool_insert(apool, address, (uint8)atoi(mask));
errno = 0;
mask_val = strtol(mask, &endptr, 10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMU, strtol() can handle "a100" and "0x100", but how does it deal with "+100" and "-100"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel addr_pool_insert is a bit awkward api as it takes addr as a string but mask as an integer. but it's another story.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the Linux man page:

The string may begin with an arbitrary amount of white space (as determined by isspace(3)) followed by a single optional '+' or '-' sign.

So, +100 and -100 are parsed as positive 100 and negative 100.

I’ll also update the code to add stricter strtol checks and enforce mask range validation in addr_pool_insert.

Copy link
Contributor Author

@linear0211 linear0211 Sep 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i feel addr_pool_insert is a bit awkward api as it takes addr as a string but mask as an integer. but it's another story.

@yamt How about changing the addr_pool_insert function's uint8 mask to const char *mask, and letting the function handle string-to-number conversion and mask validation based on the IP type?


if (mask == endptr || *endptr != '\0') {
snprintf(error_buf, error_buf_size,
"Invalid address pool entry: mask must be a number");
goto fail;
}
if (errno != 0 || mask_val < 0 || mask_val > 128) {
snprintf(error_buf, error_buf_size,
"Init wasi environment failed: invalid mask number");
goto fail;
}

ret = addr_pool_insert(apool, address, (uint8)mask_val);
wasm_runtime_free(cp);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that cp is not freed when the execution jumps to the goto fail above.

if (!ret) {
set_error_buf(error_buf, error_buf_size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3116,10 +3116,18 @@ addr_pool_insert(struct addr_pool *addr_pool, const char *addr, uint8 mask)
next->type = IPv6;
bh_memcpy_s(next->addr.ip6, sizeof(next->addr.ip6), target.ipv6,
sizeof(target.ipv6));
if (mask > 128) {
wasm_runtime_free(next);
return false;
}
}
else {
next->type = IPv4;
next->addr.ip4 = target.ipv4;
if (mask > 32) {
wasm_runtime_free(next);
return false;
}
}

/* attach with */
Expand Down
Loading