diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f7e6f8..6a12ff8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ SET(CMAKE_ASM_COMPILER ${COMPILER_RT}/i686-elf-gcc) #SET(CMAKE_VERBOSE_MAKEFILE ON) # Set compile flags -SET(CMAKE_C_FLAGS "-g -ffreestanding -Wall -Wextra -fno-exceptions -fno-stack-protector -fno-pie -m32") +SET(CMAKE_C_FLAGS "-g -ffreestanding -Wall -Wextra -fno-exceptions -fstack-protector -fno-pie -m32") SET(CMAKE_ASM_FLAGS "${CFLAGS} -m32 -x assembler-with-cpp") SET(CMAKE_EXE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/linker.ld -lgcc -ffreestanding -nostdlib -no-pie") diff --git a/include/stdlib.h b/include/stdlib.h index 2d25a7b..30701b4 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -15,7 +15,7 @@ #error "Userspace not implemented" #endif -void abort(); +void __attribute__((__noreturn__)) abort(); int atexit(void (*)(void)); diff --git a/kernel/command.c b/kernel/command.c index 18105c2..444ae8c 100644 --- a/kernel/command.c +++ b/kernel/command.c @@ -54,6 +54,8 @@ void explode(const char *args); void exec_self_test(const char *args); +void smash(const char *args); + #endif cmd_handler cmd_handlers[] = { @@ -65,10 +67,16 @@ cmd_handler cmd_handlers[] = { {"explode", explode}, #ifdef ENABLE_SELF_TEST {"self-test", exec_self_test}, + {"smash", smash}, #endif {NULL, NULL}, }; +void smash(const char* args) { + char data[16]; + memset(data, 'A', 32); +} + void exec_self_test(const char *args) { self_test(); } diff --git a/kernel/debug/self_test.c b/kernel/debug/self_test.c index be21bfd..102245c 100644 --- a/kernel/debug/self_test.c +++ b/kernel/debug/self_test.c @@ -84,7 +84,7 @@ void test_string() { assert_array_all_entries(array_b, 0, 8); assert_array_all_entries(array_b + 8, 1, 8); memcpy(array_b + 8, array_a, 8); - assert_array_all_entries(array_b + 8, 0, 16); + assert_array_all_entries(array_b, 0, 16); // strlen assert_int(3, strlen("abc")); diff --git a/kernel/util/ssp.c b/kernel/util/ssp.c new file mode 100644 index 0000000..7fe1cd0 --- /dev/null +++ b/kernel/util/ssp.c @@ -0,0 +1,29 @@ +// +// Created by rick on 15-03-21. +// +// stack smashing protector + +#include +#if __STDC_HOSTED__ +#include +#else +#include +#endif + +#if UINT32_MAX == UINTPTR_MAX +#define STACK_CHK_GUARD 0xe2dee396 +#else +#define STACK_CHK_GUARD 0x595e9fbd94fda766 +#endif + +// todo this value should be unique every time the kernel starts +uintptr_t __stack_chk_guard = STACK_CHK_GUARD; + +void __attribute__((__noreturn__)) __stack_chk_fail(void) { +#if __STDC_HOSTED__ +#error "UNKNONONO" + abort(); +#else + k_panics("Stack Smashed!"); +#endif +}