]> git.proxmox.com Git - wasi-libc.git/commitdiff
Add `-fstack-protector` support to wasi-libc (#351)
authorYuta Saito <kateinoigakukun@gmail.com>
Tue, 6 Dec 2022 17:19:49 +0000 (02:19 +0900)
committerGitHub <noreply@github.com>
Tue, 6 Dec 2022 17:19:49 +0000 (09:19 -0800)
Inlcude `__stack_chk_fail.c` and initialize `__stack_chk_guard` in ctor.

```
$ cat main.c
char input[] = "0123456789012345";
int main(void) {
    char buf[8];

    for (char *sp = input, *dp = buf; *sp != '\0'; sp++, dp++) {
        *dp = *sp;
    }
    return 0;
}

$ clang main.c -fstack-protector
$ wasmtime ./a.out
Error: failed to run main module `./a.out`

Caused by:
    0: failed to invoke command default
    1: wasm trap: wasm `unreachable` instruction executed
       wasm backtrace:
           0:  0x258 - <unknown>!__stack_chk_fail
           1:  0x21e - <unknown>!__original_main
           2:   0xca - <unknown>!_start
```

Makefile
expected/wasm32-wasi/posix/defined-symbols.txt
expected/wasm32-wasi/single/defined-symbols.txt
libc-top-half/musl/src/env/__stack_chk_fail.c

index aa12cf19202d8513be47c320b00b2664b6111376..37551cb51e0b5a91e097c974dbe8c2ef08bdecfc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -141,6 +141,7 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
         fcntl/creat.c \
         dirent/alphasort.c \
         dirent/versionsort.c \
+        env/__stack_chk_fail.c \
         env/clearenv.c \
         env/getenv.c \
         env/putenv.c \
index 34aa694333a259e5791330646b8813992897888b..6e565dc4413260b1b31b752e931c133213d9aed0 100644 (file)
@@ -91,6 +91,7 @@ __getopt_msg
 __gmtime_r
 __hwcap
 __inet_aton
+__init_ssp
 __init_tp
 __intscan
 __invtrigl_R
@@ -236,6 +237,9 @@ __sin
 __sindf
 __sinl
 __small_printf
+__stack_chk_fail
+__stack_chk_fail_local
+__stack_chk_guard
 __stderr_FILE
 __stderr_used
 __stdin_FILE
index 2e890a049b843fab22c0fe944366060ec75f0125..a692632165fe17fef0c6625c32b77f340c59f5ab 100644 (file)
@@ -81,6 +81,7 @@ __getopt_msg
 __gmtime_r
 __hwcap
 __inet_aton
+__init_ssp
 __intscan
 __invtrigl_R
 __isalnum_l
@@ -193,6 +194,9 @@ __sin
 __sindf
 __sinl
 __small_printf
+__stack_chk_fail
+__stack_chk_fail_local
+__stack_chk_guard
 __stderr_FILE
 __stderr_used
 __stdin_FILE
index e53526020f751d34542f07c6b30d94c5dc08bf72..cb7a3f391191ca2808550cacaf4a3a5447224925 100644 (file)
@@ -1,6 +1,11 @@
 #include <string.h>
 #include <stdint.h>
+#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
 #include "pthread_impl.h"
+#else
+// In non-_REENTRANT, include it for `a_crash`
+# include "atomic.h"
+#endif
 
 uintptr_t __stack_chk_guard;
 
@@ -18,7 +23,9 @@ void __init_ssp(void *entropy)
        ((char *)&__stack_chk_guard)[1] = 0;
 #endif
 
+#if defined(__wasilibc_unmodified_upstream) || defined(_REENTRANT)
        __pthread_self()->canary = __stack_chk_guard;
+#endif
 }
 
 void __stack_chk_fail(void)
@@ -29,3 +36,14 @@ void __stack_chk_fail(void)
 hidden void __stack_chk_fail_local(void);
 
 weak_alias(__stack_chk_fail, __stack_chk_fail_local);
+
+#ifndef __wasilibc_unmodified_upstream
+# include <wasi/api.h>
+
+__attribute__((constructor(60)))
+static void __wasilibc_init_ssp(void) {
+       uintptr_t entropy;
+       int r = __wasi_random_get((uint8_t *)&entropy, sizeof(uintptr_t));
+       __init_ssp(r ? NULL : &entropy);
+}
+#endif