Skip to content

Commit 8bd6794

Browse files
lum1n0usyamt
andauthored
libc-wasi: add missing pointer validations to socket functions (#4611) (#4665)
* libc-wasi: add missing pointer validations to socket functions (#4611) cf. #4463 the fix for sock_addr_resolve is incomplete. cf. #4610 * Sync from main branch - wasi_sock_recv doesn't use src_addr - check src_addr before coverting * CI: use windows-2022 image for now (#4633) github is currently rolling out windows-2025 image. for some reasons, the "path_symlink_trailing_slashes" test case in wasi testsuite fails on windows-2025 image. someone familar with windows need to investigate what was the key difference between 2022 and 2025. until that happens, this commit makes our CI use windows-2022 image. cf. #4632 actions/runner-images#12677 --------- Co-authored-by: YAMAMOTO Takashi <[email protected]>
1 parent 6450d87 commit 8bd6794

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed

.github/workflows/build_iwasm_release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ jobs:
127127
working-directory: ${{ inputs.cwd }}
128128

129129
- name: Compress the binary on Windows
130-
if: inputs.runner == 'windows-latest'
130+
if: inputs.runner == 'windows-2022'
131131
run: |
132132
tar -czf iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz iwasm.exe
133133
Compress-Archive -Path iwasm.exe -DestinationPath iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.zip
134134
mv iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.* ../
135135
working-directory: ${{ inputs.cwd }}/build/Release
136136

137137
- name: compress the binary on non-Windows
138-
if: inputs.runner != 'windows-latest'
138+
if: inputs.runner != 'windows-2022'
139139
run: |
140140
# Follow the symlink to the actual binary file
141141
tar --dereference -czf iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz iwasm

.github/workflows/build_llvm_libraries.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ jobs:
118118
key: 0-ccache-${{ inputs.os }}-${{ steps.get_last_commit.outputs.last_commit }}
119119
restore-keys: |
120120
0-ccache-${{ inputs.os }}
121-
if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'windows-latest'
121+
if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'windows-2022'
122122

123123
# Install tools on Windows
124124
- run: choco install -y ccache ninja
125-
if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'windows-latest'
125+
if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true' && inputs.os == 'windows-2022'
126126

127127
- name: Build LLVM libraries
128128
if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true'

.github/workflows/build_wamrc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ jobs:
6363
working-directory: wamr-compiler
6464

6565
- name: Compress the binary on Windows
66-
if: inputs.runner == 'windows-latest' && inputs.release
66+
if: inputs.runner == 'windows-2022' && inputs.release
6767
run: |
6868
tar -czf wamrc-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz wamrc.exe
6969
Compress-Archive -Path wamrc.exe -DestinationPath wamrc-${{ inputs.ver_num }}-${{ inputs.runner }}.zip
7070
mv wamrc-${{ inputs.ver_num }}-${{ inputs.runner }}.* ../
7171
working-directory: wamr-compiler/build/Release
7272

7373
- name: compress the binary on non-Windows
74-
if: inputs.runner != 'windows-latest' && inputs.release
74+
if: inputs.runner != 'windows-2022' && inputs.release
7575
run: |
7676
# Follow the symlink to the actual binary file
7777
tar --dereference -czf wamrc-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz wamrc

.github/workflows/compilation_on_windows.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (C) 2019 Intel Corporation. All rights reserved.
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
name: compilation on windows-latest
4+
name: compilation on windows-2022
55

66
on:
77
# will be triggered on PR events
@@ -63,11 +63,11 @@ jobs:
6363
actions: write
6464
uses: ./.github/workflows/build_llvm_libraries.yml
6565
with:
66-
os: "windows-latest"
66+
os: "windows-2022"
6767
arch: "AArch64 ARM Mips RISCV X86"
6868

6969
build_iwasm:
70-
runs-on: windows-latest
70+
runs-on: windows-2022
7171
strategy:
7272
matrix:
7373
build_options:
@@ -105,7 +105,7 @@ jobs:
105105
strategy:
106106
matrix:
107107
include:
108-
- os: windows-latest
108+
- os: windows-2022
109109
llvm_cache_key: ${{ needs.build_llvm_libraries_on_windows.outputs.cache_key }}
110110
steps:
111111
- name: checkout
@@ -136,7 +136,7 @@ jobs:
136136
working-directory: wamr-compiler
137137

138138
test:
139-
runs-on: windows-latest
139+
runs-on: windows-2022
140140
needs: [build_iwasm, build_wamrc]
141141
strategy:
142142
fail-fast: false

.github/workflows/release_process.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
needs: [create_tag, create_release]
8686
uses: ./.github/workflows/build_llvm_libraries.yml
8787
with:
88-
os: "windows-latest"
88+
os: "windows-2022"
8989
arch: "AArch64 ARM Mips RISCV X86"
9090

9191
#
@@ -122,7 +122,7 @@ jobs:
122122
with:
123123
llvm_cache_key: ${{ needs.build_llvm_libraries_on_windows.outputs.cache_key }}
124124
release: true
125-
runner: windows-latest
125+
runner: windows-2022
126126
upload_url: ${{ needs.create_release.outputs.upload_url }}
127127
ver_num: ${{ needs.create_tag.outputs.new_ver }}
128128

@@ -160,7 +160,7 @@ jobs:
160160
with:
161161
cwd: product-mini/platforms/windows
162162
llvm_cache_key: ${{ needs.build_llvm_libraries_on_windows.outputs.cache_key }}
163-
runner: windows-latest
163+
runner: windows-2022
164164
upload_url: ${{ needs.create_release.outputs.upload_url }}
165165
ver_num: ${{ needs.create_tag.outputs.new_ver}}
166166

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,9 @@ wasi_sock_accept(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_fdflags_t flags,
11611161
if (!wasi_ctx)
11621162
return __WASI_EACCES;
11631163

1164+
if (!validate_native_addr(fd_new, sizeof(*fd_new)))
1165+
return __WASI_EINVAL;
1166+
11641167
curfds = wasi_ctx_get_curfds(wasi_ctx);
11651168

11661169
return wasi_ssp_sock_accept(exec_env, curfds, fd, flags, fd_new);
@@ -1219,6 +1222,19 @@ wasi_sock_addr_resolve(wasm_exec_env_t exec_env, const char *host,
12191222
if (!wasi_ctx)
12201223
return __WASI_EACCES;
12211224

1225+
if (!validate_native_addr(hints, sizeof(*hints)))
1226+
return __WASI_EINVAL;
1227+
1228+
uint64_t addr_info_byte_size = sizeof(*addr_info) * addr_info_size;
1229+
if (addr_info_byte_size / addr_info_size != sizeof(*addr_info))
1230+
return __WASI_EINVAL;
1231+
1232+
if (!validate_native_addr(addr_info, addr_info_byte_size))
1233+
return __WASI_EINVAL;
1234+
1235+
if (!validate_native_addr(max_info_size, sizeof(*max_info_size)))
1236+
return __WASI_EINVAL;
1237+
12221238
curfds = wasi_ctx_get_curfds(wasi_ctx);
12231239
ns_lookup_list = wasi_ctx_get_ns_lookup_list(wasi_ctx);
12241240

@@ -1238,6 +1254,9 @@ wasi_sock_bind(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr)
12381254
if (!wasi_ctx)
12391255
return __WASI_EACCES;
12401256

1257+
if (!validate_native_addr(addr, sizeof(*addr)))
1258+
return __WASI_EINVAL;
1259+
12411260
curfds = wasi_ctx_get_curfds(wasi_ctx);
12421261
addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
12431262

@@ -1264,6 +1283,9 @@ wasi_sock_connect(wasm_exec_env_t exec_env, wasi_fd_t fd, wasi_addr_t *addr)
12641283
if (!wasi_ctx)
12651284
return __WASI_EACCES;
12661285

1286+
if (!validate_native_addr(addr, sizeof(*addr)))
1287+
return __WASI_EINVAL;
1288+
12671289
curfds = wasi_ctx_get_curfds(wasi_ctx);
12681290
addr_pool = wasi_ctx_get_addr_pool(wasi_ctx);
12691291

@@ -1643,6 +1665,9 @@ wasi_sock_open(wasm_exec_env_t exec_env, wasi_fd_t poolfd,
16431665
if (!wasi_ctx)
16441666
return __WASI_EACCES;
16451667

1668+
if (!validate_native_addr(sockfd, sizeof(*sockfd)))
1669+
return __WASI_EINVAL;
1670+
16461671
curfds = wasi_ctx_get_curfds(wasi_ctx);
16471672

16481673
return wasi_ssp_sock_open(exec_env, curfds, poolfd, af, socktype, sockfd);
@@ -2082,6 +2107,10 @@ wasi_sock_recv_from(wasm_exec_env_t exec_env, wasi_fd_t sock,
20822107
return __WASI_EINVAL;
20832108
}
20842109

2110+
/* note: src_addr is NULL when called by wasi_sock_recv */
2111+
if (src_addr != NULL && !validate_native_addr(src_addr, sizeof(*src_addr)))
2112+
return __WASI_EINVAL;
2113+
20852114
if (!validate_native_addr(ro_data_len, (uint64)sizeof(uint32)))
20862115
return __WASI_EINVAL;
20872116

@@ -2118,16 +2147,19 @@ wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
21182147
wasi_roflags_t *ro_flags)
21192148
{
21202149
wasm_module_inst_t module_inst = get_module_inst(exec_env);
2121-
__wasi_addr_t src_addr;
21222150
wasi_errno_t error;
21232151

2152+
if (!validate_native_addr(ro_data_len, sizeof(*ro_data_len)))
2153+
return __WASI_EINVAL;
2154+
21242155
if (!validate_native_addr(ro_flags, (uint64)sizeof(wasi_roflags_t)))
21252156
return __WASI_EINVAL;
21262157

2158+
// We call `recvfrom` with NULL source address as `recv` doesn't
2159+
// return the source address and this parameter is not used.
2160+
*ro_data_len = 0;
21272161
error = wasi_sock_recv_from(exec_env, sock, ri_data, ri_data_len, ri_flags,
2128-
&src_addr, ro_data_len);
2129-
*ro_flags = ri_flags;
2130-
2162+
NULL, ro_data_len);
21312163
return error;
21322164
}
21332165

@@ -2228,6 +2260,9 @@ wasi_sock_send_to(wasm_exec_env_t exec_env, wasi_fd_t sock,
22282260
return __WASI_EINVAL;
22292261
}
22302262

2263+
if (!validate_native_addr((void *)dest_addr, sizeof(*dest_addr)))
2264+
return __WASI_EINVAL;
2265+
22312266
if (!validate_native_addr(so_data_len, (uint64)sizeof(uint32)))
22322267
return __WASI_EINVAL;
22332268

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2854,7 +2854,11 @@ wasmtime_ssp_sock_recv_from(wasm_exec_env_t exec_env, struct fd_table *curfds,
28542854
return convert_errno(errno);
28552855
}
28562856

2857-
bh_sockaddr_to_wasi_addr(&sockaddr, src_addr);
2857+
// If the source address is not NULL, we need to convert the sockaddr
2858+
// back to __wasi_addr_t format.
2859+
if (src_addr != NULL) {
2860+
bh_sockaddr_to_wasi_addr(&sockaddr, src_addr);
2861+
}
28582862

28592863
*recv_len = (size_t)ret;
28602864
return __WASI_ESUCCESS;

0 commit comments

Comments
 (0)