From 6ec747105bfa8d52aca77bc577268f1944b19585 Mon Sep 17 00:00:00 2001 From: Michal Idziorek Date: Tue, 25 Nov 2014 12:22:35 +0100 Subject: started adding stack shmashing protection --- Makefile | 3 ++- kernel/kernel.c | 21 +++++++++++++++++++++ userspace/Makefile | 1 + userspace/sys/sys.c | 24 ++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3469bce..6806c1b 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,8 @@ CC=i686-foolos-gcc ############ compiler flags ############ CFLAGS= -CFLAGS=-w +CFLAGS+=-fstack-protector-all +CFLAGS+=-w CFLAGS+=-ffreestanding CFLATS+=-Wall CFLAGS+=-Wextra diff --git a/kernel/kernel.c b/kernel/kernel.c index 59ad130..77970c3 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -31,6 +31,27 @@ #include "task.h" +#include +#include + + +// CODE FOR Stack Smashing Protector, TODO: MOVE / and do not duplicate +// with sys.c +// http://wiki.osdev.org/Stack_Smashing_Protector + +#if UINT32_MAX == UINTPTR_MAX +#define STACK_CHK_GUARD 0xe2dee396 +#else +#define STACK_CHK_GUARD 0x595e9fbd94fda766 +#endif + +uintptr_t __stack_chk_guard = STACK_CHK_GUARD; + +__attribute__((noreturn)) +void __stack_chk_fail(void) +{ + panic(FOOLOS_MODULE_NAME,"Stack smashing detected"); +} // mp informs us if this if this is the main processor void kernel_main(uint32_t initial_stack, int mp) diff --git a/userspace/Makefile b/userspace/Makefile index 63310bf..6c6d711 100644 --- a/userspace/Makefile +++ b/userspace/Makefile @@ -1,6 +1,7 @@ CC=i686-foolos-gcc CFLAGS= +#CFLAGS+=-fstack-protector-all CFLAGS+=-I.. CFLAGS+=-w CFLAGS+=-std=gnu11 diff --git a/userspace/sys/sys.c b/userspace/sys/sys.c index ef0d1df..1ab3579 100644 --- a/userspace/sys/sys.c +++ b/userspace/sys/sys.c @@ -3,6 +3,30 @@ #include +// CODE FOR Stack Smashing Protector, TODO: MOVE / and do not duplicate +// with kernel.c +// http://wiki.osdev.org/Stack_Smashing_Protector +#include +#include + +#if UINT32_MAX == UINTPTR_MAX +#define STACK_CHK_GUARD 0xe2dee396 +#else +#define STACK_CHK_GUARD 0x595e9fbd94fda766 +#endif + +uintptr_t __stack_chk_guard = STACK_CHK_GUARD; + +__attribute__((noreturn)) +void __stack_chk_fail(void) +{ + write(1,"stack smashing!\n",16); + exit(99); +} + + +/////////////////// + // required by binutils! long sysconf(int name) -- cgit v1.2.3