From 6854ad489f287987889513f19ab16316a15b964d Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Wed, 19 Feb 2025 19:12:27 +0100 Subject: [PATCH] Use gcc stack protector Trick was to use the stack-protector-guard option otherwise gcc expect the canary value to be stored on the Thread Local Storage of the current thread. That does not work the same way for us, so trying to access TLS at %gs (the canonical place for the TLS), where gcc expect the canary to be storeg, lead to system reboot as it is not setup. --- Makefile | 4 ++-- core/assert.h | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 1ebe675..6af38e5 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,9 @@ CPPFLAGS = -MMD AS=nasm ASFLAGS += -f elf32 LDFLAGS += -m elf_i386 -CFLAGS += -m32 -pipe -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-pie -fno-stack-protector -fno-tree-vectorize -D__KERNEL__ +CFLAGS += -m32 -pipe -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-pie -fno-tree-vectorize -D__KERNEL__ -fstack-protector-all -mstack-protector-guard=global #keep .i and .s -#CFLAGS += -save-temps +#CFLAGS += -save-temps -fverbose-asm #CFLAGS += -fanalyzer -Wno-analyzer-malloc-leak -Wno-analyzer-out-of-bounds CXXFLAGS += -m32 -Wall -Wextra -Werror -ffreestanding -fno-exceptions -fno-rtti -fno-pie DEBUG_FLAGS += -g -Og -DDEBUG -fno-omit-frame-pointer -fno-inline diff --git a/core/assert.h b/core/assert.h index 5bafdd8..d42c602 100644 --- a/core/assert.h +++ b/core/assert.h @@ -22,3 +22,14 @@ } \ } \ } while (0) + +#define panic(fmt, args...) \ + do { \ + asm volatile("cli"); \ + printf("PANIC at %s:%d\n " fmt "", __FILE__, __LINE__); \ + printf("PANIC: " fmt "\n", ##args); \ + printStackTrace(3); \ + while (1) \ + asm volatile("hlt"); \ + __builtin_unreachable(); \ + } while (0)