Changeset View
Changeset View
Standalone View
Standalone View
head/lib/libc/secure/stack_protector.c
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
void __stack_chk_fail(void); | void __stack_chk_fail(void); | ||||
void __chk_fail(void); | void __chk_fail(void); | ||||
/*LINTED used*/ | /*LINTED used*/ | ||||
static void | static void | ||||
__guard_setup(void) | __guard_setup(void) | ||||
{ | { | ||||
static const int mib[2] = { CTL_KERN, KERN_ARND }; | static const int mib[2] = { CTL_KERN, KERN_ARND }; | ||||
volatile long tmp_stack_chk_guard[nitems(__stack_chk_guard)]; | |||||
size_t len; | size_t len; | ||||
int error; | int error, idx; | ||||
if (__stack_chk_guard[0] != 0) | if (__stack_chk_guard[0] != 0) | ||||
return; | return; | ||||
error = _elf_aux_info(AT_CANARY, __stack_chk_guard, | /* | ||||
sizeof(__stack_chk_guard)); | * Avoid using functions which might have stack protection | ||||
if (error == 0 && __stack_chk_guard[0] != 0) | * enabled, to update the __stack_chk_guard. First fetch the | ||||
* data into a temporal array, then do manual volatile copy to | |||||
* not allow optimizer to call memcpy() behind us. | |||||
*/ | |||||
error = _elf_aux_info(AT_CANARY, (void *)tmp_stack_chk_guard, | |||||
sizeof(tmp_stack_chk_guard)); | |||||
if (error == 0 && tmp_stack_chk_guard[0] != 0) { | |||||
for (idx = 0; idx < nitems(__stack_chk_guard); idx++) { | |||||
__stack_chk_guard[idx] = tmp_stack_chk_guard[idx]; | |||||
tmp_stack_chk_guard[idx] = 0; | |||||
} | |||||
return; | return; | ||||
} | |||||
len = sizeof(__stack_chk_guard); | len = sizeof(__stack_chk_guard); | ||||
if (__sysctl(mib, nitems(mib), __stack_chk_guard, &len, NULL, 0) == | if (__sysctl(mib, nitems(mib), __stack_chk_guard, &len, NULL, 0) == | ||||
-1 || len != sizeof(__stack_chk_guard)) { | -1 || len != sizeof(__stack_chk_guard)) { | ||||
/* If sysctl was unsuccessful, use the "terminator canary". */ | /* If sysctl was unsuccessful, use the "terminator canary". */ | ||||
((unsigned char *)(void *)__stack_chk_guard)[0] = 0; | ((unsigned char *)(void *)__stack_chk_guard)[0] = 0; | ||||
((unsigned char *)(void *)__stack_chk_guard)[1] = 0; | ((unsigned char *)(void *)__stack_chk_guard)[1] = 0; | ||||
((unsigned char *)(void *)__stack_chk_guard)[2] = '\n'; | ((unsigned char *)(void *)__stack_chk_guard)[2] = '\n'; | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |