Skip to content

Commit 0746640

Browse files
linear0211lum1n0us
andauthored
Ensure --addr-pool mask accepts numbers only (#4619)
* Ensure --addr-pool mask accepts numbers only * Add mask validation * Replace mask assignment position * Use a thread-safe function and free allocated memory Co-authored-by: liang.he <[email protected]>
1 parent e78317a commit 0746640

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

core/iwasm/common/wasm_runtime_common.c

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3898,7 +3898,8 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
38983898

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

39043905
cp = bh_strdup(addr_pool[i]);
@@ -3908,18 +3909,40 @@ wasm_runtime_init_wasi(WASMModuleInstanceCommon *module_inst,
39083909
goto fail;
39093910
}
39103911

3911-
address = strtok(cp, "/");
3912-
mask = strtok(NULL, "/");
3912+
#ifdef BH_PLATFORM_WINDOWS
3913+
address = strtok_s(cp, "/", &nextptr);
3914+
mask = strtok_s(NULL, "/", &nextptr);
3915+
#else
3916+
address = strtok_r(cp, "/", &nextptr);
3917+
mask = strtok_r(NULL, "/", &nextptr);
3918+
#endif
39133919

39143920
if (!mask) {
39153921
snprintf(error_buf, error_buf_size,
39163922
"Invalid address pool entry: %s, must be in the format of "
39173923
"ADDRESS/MASK",
39183924
addr_pool[i]);
3925+
wasm_runtime_free(cp);
3926+
goto fail;
3927+
}
3928+
3929+
errno = 0;
3930+
mask_val = strtol(mask, &endptr, 10);
3931+
3932+
if (mask == endptr || *endptr != '\0') {
3933+
snprintf(error_buf, error_buf_size,
3934+
"Invalid address pool entry: mask must be a number");
3935+
wasm_runtime_free(cp);
3936+
goto fail;
3937+
}
3938+
if (errno != 0 || mask_val < 0) {
3939+
snprintf(error_buf, error_buf_size,
3940+
"Init wasi environment failed: invalid mask number");
3941+
wasm_runtime_free(cp);
39193942
goto fail;
39203943
}
39213944

3922-
ret = addr_pool_insert(apool, address, (uint8)atoi(mask));
3945+
ret = addr_pool_insert(apool, address, (uint8)mask_val);
39233946
wasm_runtime_free(cp);
39243947
if (!ret) {
39253948
set_error_buf(error_buf, error_buf_size,

core/iwasm/libraries/libc-wasi/sandboxed-system-primitives/src/posix.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3105,7 +3105,6 @@ addr_pool_insert(struct addr_pool *addr_pool, const char *addr, uint8 mask)
31053105
}
31063106

31073107
next->next = NULL;
3108-
next->mask = mask;
31093108

31103109
if (os_socket_inet_network(true, addr, &target) != BHT_OK) {
31113110
// If parsing IPv4 fails, try IPv6
@@ -3116,10 +3115,20 @@ addr_pool_insert(struct addr_pool *addr_pool, const char *addr, uint8 mask)
31163115
next->type = IPv6;
31173116
bh_memcpy_s(next->addr.ip6, sizeof(next->addr.ip6), target.ipv6,
31183117
sizeof(target.ipv6));
3118+
if (mask > 128) {
3119+
wasm_runtime_free(next);
3120+
return false;
3121+
}
3122+
next->mask = mask;
31193123
}
31203124
else {
31213125
next->type = IPv4;
31223126
next->addr.ip4 = target.ipv4;
3127+
if (mask > 32) {
3128+
wasm_runtime_free(next);
3129+
return false;
3130+
}
3131+
next->mask = mask;
31233132
}
31243133

31253134
/* attach with */

0 commit comments

Comments
 (0)