feat: created initial stack smash protector

This commit is contained in:
2021-03-15 19:21:46 +01:00
parent 3d08828e8d
commit 513693189e
5 changed files with 40 additions and 3 deletions

View File

@@ -10,7 +10,7 @@ SET(CMAKE_ASM_COMPILER ${COMPILER_RT}/i686-elf-gcc)
#SET(CMAKE_VERBOSE_MAKEFILE ON) #SET(CMAKE_VERBOSE_MAKEFILE ON)
# Set compile flags # 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_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") SET(CMAKE_EXE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/linker.ld -lgcc -ffreestanding -nostdlib -no-pie")

View File

@@ -15,7 +15,7 @@
#error "Userspace not implemented" #error "Userspace not implemented"
#endif #endif
void abort(); void __attribute__((__noreturn__)) abort();
int atexit(void (*)(void)); int atexit(void (*)(void));

View File

@@ -54,6 +54,8 @@ void explode(const char *args);
void exec_self_test(const char *args); void exec_self_test(const char *args);
void smash(const char *args);
#endif #endif
cmd_handler cmd_handlers[] = { cmd_handler cmd_handlers[] = {
@@ -65,10 +67,16 @@ cmd_handler cmd_handlers[] = {
{"explode", explode}, {"explode", explode},
#ifdef ENABLE_SELF_TEST #ifdef ENABLE_SELF_TEST
{"self-test", exec_self_test}, {"self-test", exec_self_test},
{"smash", smash},
#endif #endif
{NULL, NULL}, {NULL, NULL},
}; };
void smash(const char* args) {
char data[16];
memset(data, 'A', 32);
}
void exec_self_test(const char *args) { void exec_self_test(const char *args) {
self_test(); self_test();
} }

View File

@@ -84,7 +84,7 @@ void test_string() {
assert_array_all_entries(array_b, 0, 8); assert_array_all_entries(array_b, 0, 8);
assert_array_all_entries(array_b + 8, 1, 8); assert_array_all_entries(array_b + 8, 1, 8);
memcpy(array_b + 8, array_a, 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 // strlen
assert_int(3, strlen("abc")); assert_int(3, strlen("abc"));

29
kernel/util/ssp.c Normal file
View File

@@ -0,0 +1,29 @@
//
// Created by rick on 15-03-21.
//
// stack smashing protector
#include <stdint.h>
#if __STDC_HOSTED__
#include <stdlib.h>
#else
#include <myke/libk/libk.h>
#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
}