From 97797de92f909b3b40580438d72f704d7d874ae7 Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Mon, 11 Aug 2025 11:43:20 -0700 Subject: [PATCH 1/2] Port random numbers to use wasip2 methods --- expected/wasm32-wasip2/defined-symbols.txt | 1 + libc-bottom-half/headers/public/wasi/libc.h | 5 +++ libc-bottom-half/sources/__wasilibc_random.c | 25 +++++++++++++++ libc-bottom-half/sources/getentropy.c | 10 ++++-- libc-top-half/musl/src/env/__stack_chk_fail.c | 10 ++++++ test/src/misc/getentropy.c | 31 +++++++++++++++++++ 6 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 libc-bottom-half/sources/__wasilibc_random.c create mode 100644 test/src/misc/getentropy.c diff --git a/expected/wasm32-wasip2/defined-symbols.txt b/expected/wasm32-wasip2/defined-symbols.txt index d5fc8f116..10e55033c 100644 --- a/expected/wasm32-wasip2/defined-symbols.txt +++ b/expected/wasm32-wasip2/defined-symbols.txt @@ -381,6 +381,7 @@ __wasilibc_nocwd_utimensat __wasilibc_open_nomode __wasilibc_populate_preopens __wasilibc_pthread_self +__wasilibc_random __wasilibc_register_preopened_fd __wasilibc_rename_newat __wasilibc_rename_oldat diff --git a/libc-bottom-half/headers/public/wasi/libc.h b/libc-bottom-half/headers/public/wasi/libc.h index bed8f9418..e280f0e42 100644 --- a/libc-bottom-half/headers/public/wasi/libc.h +++ b/libc-bottom-half/headers/public/wasi/libc.h @@ -3,6 +3,7 @@ #include <__typedef_off_t.h> #include <__struct_timespec.h> +#include #ifdef __cplusplus extern "C" { @@ -67,6 +68,10 @@ int __wasilibc_rename_newat(const char *oldpath, int newdirfd, const char *newpa /// Enable busywait in futex on current thread. void __wasilibc_enable_futex_busywait_on_current_thread(void); +/// Fill a buffer with random bytes +int __wasilibc_random(void* buffer, size_t len) + __attribute__((__warn_unused_result__)); + #ifdef __cplusplus } #endif diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c new file mode 100644 index 000000000..eca0c9f1f --- /dev/null +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -0,0 +1,25 @@ +#include +#include +#ifdef __wasilibc_use_wasip2 +#include + +int __wasilibc_random(void *buffer, size_t len) { + + // Set up a WASI byte list to receive the results + wasip2_list_u8_t wasi_list; + + // Get random bytes + random_get_random_bytes(len, &wasi_list); + if (wasi_list.len != len) + return EINVAL; + else { + // Copy the result + memcpy(buffer, wasi_list.ptr, len); + } + + // Free the WASI byte list + wasip2_list_u8_free(&wasi_list); + + return 0; +} +#endif diff --git a/libc-bottom-half/sources/getentropy.c b/libc-bottom-half/sources/getentropy.c index e540e7e31..3d6ff05c7 100644 --- a/libc-bottom-half/sources/getentropy.c +++ b/libc-bottom-half/sources/getentropy.c @@ -1,6 +1,10 @@ #include #include +#ifdef __wasilibc_use_wasip2 +#include +#else #include +#endif int __getentropy(void *buffer, size_t len) { if (len > 256) { @@ -8,13 +12,15 @@ int __getentropy(void *buffer, size_t len) { return -1; } +#ifdef __wasilibc_use_wasip2 + int r = __wasilibc_random(buffer, len); +#else int r = __wasi_random_get(buffer, len); - +#endif if (r != 0) { errno = r; return -1; } - return 0; } weak_alias(__getentropy, getentropy); diff --git a/libc-top-half/musl/src/env/__stack_chk_fail.c b/libc-top-half/musl/src/env/__stack_chk_fail.c index cb7a3f391..7c39c22e6 100644 --- a/libc-top-half/musl/src/env/__stack_chk_fail.c +++ b/libc-top-half/musl/src/env/__stack_chk_fail.c @@ -38,12 +38,22 @@ hidden void __stack_chk_fail_local(void); weak_alias(__stack_chk_fail, __stack_chk_fail_local); #ifndef __wasilibc_unmodified_upstream +#ifdef __wasilibc_use_wasip2 +# include +#else # include +#endif __attribute__((constructor(60))) static void __wasilibc_init_ssp(void) { uintptr_t entropy; +#ifdef __wasilibc_use_wasip2 + int len = sizeof(uintptr_t); + + int r = __wasilibc_random(&entropy, len); +#else int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t)); +#endif __init_ssp(r ? NULL : &entropy); } #endif diff --git a/test/src/misc/getentropy.c b/test/src/misc/getentropy.c new file mode 100644 index 000000000..d77b436aa --- /dev/null +++ b/test/src/misc/getentropy.c @@ -0,0 +1,31 @@ +//! add-flags.py(CFLAGS): -I. +//! add-flags.py(ARGS): foo bar +#include +#include +#include "test.h" + +#define TEST(c, ...) \ + ( (c) || (t_error(#c " failed: " __VA_ARGS__),0) ) + +int main() +{ + size_t len = 256; + uint8_t buffer[len]; + + TEST(getentropy(&buffer, len) == 0, "getentropy() should return 0\n"); + int all_zeroes = 1; + + for (size_t i = 0; i < len; i++) { + if (buffer[i] != 0) { + all_zeroes = 0; + break; + } + } + + TEST(all_zeroes == 0, "getentropy() returned 256 zeroes\n"); + + // More than 256 bytes is an error + TEST(getentropy(&buffer, 257)==-1, "requesting > 256 bytes should be an error\n"); + + return t_status; +} From cdcd442983ccf626199f7555300f90c801b98d7f Mon Sep 17 00:00:00 2001 From: Tim Chevalier Date: Wed, 13 Aug 2025 13:58:24 -0700 Subject: [PATCH 2/2] Exit if requested random bytes != returned bytes --- libc-bottom-half/sources/__wasilibc_random.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/libc-bottom-half/sources/__wasilibc_random.c b/libc-bottom-half/sources/__wasilibc_random.c index eca0c9f1f..8232c44e3 100644 --- a/libc-bottom-half/sources/__wasilibc_random.c +++ b/libc-bottom-half/sources/__wasilibc_random.c @@ -2,6 +2,7 @@ #include #ifdef __wasilibc_use_wasip2 #include +#include int __wasilibc_random(void *buffer, size_t len) { @@ -10,8 +11,11 @@ int __wasilibc_random(void *buffer, size_t len) { // Get random bytes random_get_random_bytes(len, &wasi_list); + + // The spec for get-random-bytes specifies that wasi_list.len + // will be equal to len. if (wasi_list.len != len) - return EINVAL; + _Exit(EX_OSERR); else { // Copy the result memcpy(buffer, wasi_list.ptr, len);