protect stack canary from leak via read-as-string by zeroing second byte
[musl] / src / env / __stack_chk_fail.c
1 #include <string.h>
2 #include <stdint.h>
3 #include "pthread_impl.h"
4
5 uintptr_t __stack_chk_guard;
6
7 void __init_ssp(void *entropy)
8 {
9         if (entropy) memcpy(&__stack_chk_guard, entropy, sizeof(uintptr_t));
10         else __stack_chk_guard = (uintptr_t)&__stack_chk_guard * 1103515245;
11
12 #if UINTPTR_MAX >= 0xffffffffffffffff
13         /* Sacrifice 8 bits of entropy on 64bit to prevent leaking/
14          * overwriting the canary via string-manipulation functions.
15          * The NULL byte is on the second byte so that off-by-ones can
16          * still be detected. Endianness is taken care of
17          * automatically. */
18         ((char *)&__stack_chk_guard)[1] = 0;
19 #endif
20
21         __pthread_self()->canary = __stack_chk_guard;
22 }
23
24 void __stack_chk_fail(void)
25 {
26         a_crash();
27 }
28
29 hidden void __stack_chk_fail_local(void);
30
31 weak_alias(__stack_chk_fail, __stack_chk_fail_local);