From 842716a0b5eceb8db31416cd643720c1037032b2 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 20 Dec 2019 20:51:50 +0100 Subject: [PATCH] Kernel+LibC: Build with basic -fstack-protector support Use simple stack cookies to try to provoke an assertion failure on stack overflow. This is far from perfect, since we use a constant cookie instead of generating a random one on startup, but it can still help us catch bugs, which is the primary concern right now. :^) --- Kernel/StdLib.cpp | 8 ++++++++ Libraries/LibC/crt0.cpp | 9 +++++++++ Makefile.common | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index a0b033cd71e..6c4bcf8a638 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -183,4 +183,12 @@ void free(void* p) { return kfree(p); } + +extern u32 __stack_chk_guard; +u32 __stack_chk_guard = (u32)0xc0000c13; + +[[noreturn]] void __stack_chk_fail() +{ + ASSERT_NOT_REACHED(); +} } diff --git a/Libraries/LibC/crt0.cpp b/Libraries/LibC/crt0.cpp index 8466abefdf3..d8f6da41efb 100644 --- a/Libraries/LibC/crt0.cpp +++ b/Libraries/LibC/crt0.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -51,4 +52,12 @@ int _start(int argc, char** argv, char** env) void __cxa_atexit() { } + +extern u32 __stack_chk_guard; +u32 __stack_chk_guard = (u32)0xc0000c13; + +[[noreturn]] void __stack_chk_fail() +{ + ASSERT_NOT_REACHED(); +} } diff --git a/Makefile.common b/Makefile.common index 4b0c4e7a518..b02681564ab 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1,7 +1,7 @@ ARCH_FLAGS = STANDARD_FLAGS = -std=c++17 -Wno-sized-deallocation -fno-sized-deallocation WARNING_FLAGS = -Werror -Wextra -Wall -Wno-nonnull-compare -Wno-deprecated-copy -Wno-address-of-packed-member -Wundef -Wcast-qual -Wwrite-strings -Wimplicit-fallthrough -Wno-expansion-to-defined -FLAVOR_FLAGS = -fno-exceptions -fno-rtti +FLAVOR_FLAGS = -fno-exceptions -fno-rtti -fstack-protector OPTIMIZATION_FLAGS = -Os MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))