Skip to content

Commit 3777fba

Browse files
committed
wasmtime: harden wasm_interpret_header func
Signed-off-by: Maximilian Hüter <[email protected]>
1 parent 2170c02 commit 3777fba

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

src/libcrun/handlers/handler-utils.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,14 @@ wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err
7272
}
7373

7474
wasm_encoding_t
75-
wasm_interpete_header (const char *header)
75+
wasm_interpret_header (const char *header, const size_t len)
7676
{
77+
if (len < 8)
78+
return WASM_ENC_INVALID;
79+
7780
// Check for the WebAssembly magic bytes
7881
// See: https://webassembly.github.io/spec/core/binary/modules.html#binary-module
79-
if (strncmp (header, "\0asm", 4))
82+
if (memcmp (header, "\0asm", 4))
8083
return WASM_ENC_INVALID;
8184

8285
/* The next four bytes are the WebAssembly version.

src/libcrun/handlers/handler-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ typedef enum
3030

3131
int wasm_can_handle_container (libcrun_container_t *container, libcrun_error_t *err);
3232

33-
wasm_encoding_t wasm_interpete_header (const char *header);
33+
wasm_encoding_t wasm_interpret_header (const char *header, const size_t len);
3434

3535
#endif

src/libcrun/handlers/wasmtime.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@
4444
#endif
4545

4646
#if HAVE_DLOPEN && HAVE_WASMTIME
47+
static void *
48+
load_libwasmtime_symbol (void *handle, const char *name)
49+
{
50+
void *sym = dlsym (handle, name);
51+
if (sym == NULL)
52+
error (EXIT_FAILURE, 0, "could not find symbol in `libwasmtime.so`: %.*s", (int) strlen (name), name);
53+
54+
return sym;
55+
}
56+
57+
# define COMMON_WASM_SYMBOLS
58+
4759
static void
4860
libwasmtime_run_module (void *cookie, char *const argv[], wasm_engine_t *engine, wasm_byte_vec_t *wasm);
4961

@@ -117,7 +129,7 @@ libwasmtime_exec (void *cookie, libcrun_container_t *container arg_unused,
117129
wasm = wasm_bytes;
118130
}
119131

120-
wasm_encoding_t wasm_enc = wasm_interpete_header (wasm.data);
132+
wasm_encoding_t wasm_enc = wasm_interpret_header (wasm.data, wasm.size);
121133
if (wasm_enc == WASM_ENC_INVALID)
122134
error (EXIT_FAILURE, 0, "invalid wasm binary header");
123135

0 commit comments

Comments
 (0)