diff --git a/Core/StackGuard.c b/Core/StackGuard.cpp similarity index 59% rename from Core/StackGuard.c rename to Core/StackGuard.cpp index 5bc2a27..555978b 100644 --- a/Core/StackGuard.c +++ b/Core/StackGuard.cpp @@ -1,36 +1,34 @@ #include #include +#include #include "../kernel.h" -#ifndef STACK_CHK_GUARD_VALUE -#if UINTPTR_MAX == UINT32_MAX -#define STACK_CHK_GUARD_VALUE 0xDEAD57AC -#else -#define STACK_CHK_GUARD_VALUE 0xDEAD57AC00000000 -#endif -#endif +EXTERNC __attribute__((weak)) uintptr_t __stack_chk_guard = 0; -__attribute__((weak)) uintptr_t __stack_chk_guard = 0; - -__attribute__((weak, no_stack_protector)) uintptr_t __stack_chk_guard_init(void) +EXTERNC __attribute__((weak, no_stack_protector)) uintptr_t __stack_chk_guard_init(void) { - return STACK_CHK_GUARD_VALUE; +#if UINTPTR_MAX == UINT32_MAX + return Random::rand32(); +#else + return Random::rand64(); +#endif } -extern __attribute__((constructor, no_stack_protector)) void __guard_setup(void) +EXTERNC __attribute__((constructor, no_stack_protector)) void __guard_setup(void) { debug("StackGuard: __guard_setup"); if (__stack_chk_guard == 0) __stack_chk_guard = __stack_chk_guard_init(); + debug("Stack guard value: %ld", __stack_chk_guard); } -__attribute__((weak, noreturn, no_stack_protector)) void __stack_chk_fail(void) +EXTERNC __attribute__((weak, noreturn, no_stack_protector)) void __stack_chk_fail(void) { TaskingPanic(); for (short i = 0; i < 10; i++) error("Stack smashing detected!"); - debug("%#lx", __stack_chk_guard); + debug("Current stack check guard value: %#lx", __stack_chk_guard); KPrint("\eFF0000Stack smashing detected!"); #if defined(__amd64__) || defined(__i386__) while (1) @@ -41,7 +39,7 @@ __attribute__((weak, noreturn, no_stack_protector)) void __stack_chk_fail(void) } // https://github.com/gcc-mirror/gcc/blob/master/libssp/ssp.c -__attribute__((weak, noreturn, no_stack_protector)) void __chk_fail(void) +EXTERNC __attribute__((weak, noreturn, no_stack_protector)) void __chk_fail(void) { TaskingPanic(); for (short i = 0; i < 10; i++) diff --git a/Library/Convert.c b/Library/Convert.c index 9cb2fd2..33b65dc 100644 --- a/Library/Convert.c +++ b/Library/Convert.c @@ -870,29 +870,25 @@ __no_stack_protector void *__memcpy_chk(void *dest, const void *src, size_t len, if (unlikely(dest == NULL)) { error("dest is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(src == NULL)) { error("src is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(len == 0)) { error("len is 0"); - while (1) - ; + __chk_fail(); } if (unlikely(slen == 0)) { error("slen is 0"); - while (1) - ; + __chk_fail(); } if (unlikely(len > slen)) @@ -908,22 +904,19 @@ __no_stack_protector void *__memset_chk(void *dest, int val, size_t len, size_t if (unlikely(dest == NULL)) { error("dest is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(len == 0)) { error("len is 0"); - while (1) - ; + __chk_fail(); } if (unlikely(slen == 0)) { error("slen is 0"); - while (1) - ; + __chk_fail(); } if (unlikely(len > slen)) @@ -939,29 +932,25 @@ __no_stack_protector void *__memmove_chk(void *dest, const void *src, size_t len if (unlikely(dest == NULL)) { error("dest is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(src == NULL)) { error("src is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(len == 0)) { error("len is 0"); - while (1) - ; + __chk_fail(); } if (unlikely(slen == 0)) { error("slen is 0"); - while (1) - ; + __chk_fail(); } if (unlikely(len > slen)) @@ -977,22 +966,19 @@ __no_stack_protector char *__strcat_chk(char *dest, const char *src, size_t slen if (unlikely(dest == NULL)) { error("dest is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(src == NULL)) { error("src is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(slen == 0)) { error("slen is 0"); - while (1) - ; + __chk_fail(); } size_t dest_len = strlen(dest); @@ -1009,22 +995,19 @@ __no_stack_protector char *__strcpy_chk(char *dest, const char *src, size_t slen if (unlikely(dest == NULL)) { error("dest is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(src == NULL)) { error("src is NULL"); - while (1) - ; + __chk_fail(); } if (unlikely(slen == 0)) { error("slen is 0"); - while (1) - ; + __chk_fail(); } size_t len = strlen(src);