Эх сурвалжийг харах

feat(stack-protector): protect stack overflowing

greatbridf 2 жил өмнө
parent
commit
5d0c569a04

+ 2 - 2
CMakeLists.txt

@@ -32,8 +32,8 @@ add_custom_command(OUTPUT extracted_bootloader
 
 project(kernel_main)
 
-set(CMAKE_C_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-builtin-declaration-mismatch -Wno-format -Werror=implicit-int -Werror=implicit-function-declaration -Werror=strict-aliasing -fverbose-asm -fno-exceptions -fno-pic -fno-stack-protector")
-set(CMAKE_CXX_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-builtin-declaration-mismatch -Wno-format -fverbose-asm -fno-use-cxa-atexit -fno-exceptions -fno-pic -fno-stack-protector -fno-rtti")
+set(CMAKE_C_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-builtin-declaration-mismatch -Wno-format -Werror=implicit-int -Werror=implicit-function-declaration -Werror=strict-aliasing -fverbose-asm -fno-exceptions -fno-pic -ffreestanding -mstack-protector-guard=global")
+set(CMAKE_CXX_FLAGS "-nostdinc -m32 -nostdlib -W -Wall -Wextra -Wno-builtin-declaration-mismatch -Wno-format -fverbose-asm -fno-use-cxa-atexit -fno-exceptions -fno-pic -ffreestanding -fno-rtti -mstack-protector-guard=global")
 set(CMAKE_CXX_STANDARD 20)
 
 if (CMAKE_BUILD_TYPE STREQUAL "Debug")

+ 3 - 0
src/kernel.ld

@@ -38,6 +38,9 @@ SECTIONS
         KEEP(*(SORT_BY_INIT_PRIORITY(.ctors*)));
         end_ctors = .;
 
+        __stack_chk_guard = .;
+        LONG(0x19198101);
+
         *(.data)
         *(.data*)
         __kernel_text_and_data_end = .;

+ 9 - 0
src/kernel_main.c

@@ -201,3 +201,12 @@ void NORETURN kernel_main(void)
     printkf("switching execution to the scheduler...\n");
     init_scheduler(&tss);
 }
+
+void NORETURN __stack_chk_fail(void)
+{
+    tty_print(console, "***** stack smashing detected! *****\nhalting\n");
+    for (;;) {
+        asm_cli();
+        asm_hlt();
+    }
+}

+ 1 - 1
user-space-program/Makefile.src

@@ -4,7 +4,7 @@ LD=$(CROSS_COMPILE)ld
 OBJCOPY=$(CROSS_COMPILE)objcopy
 XXD=xxd
 
-CFLAGS=-nostdinc -nostdlib -static -g -m32 -W -Wall -Wextra -Werror
+CFLAGS=-nostdinc -nostdlib -static -g -m32 -W -Wall -Wextra -Werror -mstack-protector-guard=global
 
 OBJS=hello-world.out interrupt-test.out stack-test.out init.out
 SYMS=init.sym

+ 3 - 0
user-space-program/script.ld

@@ -13,6 +13,9 @@ SECTIONS
     {
         *(.text)
         *(.text*)
+
+        __stack_chk_guard = .;
+        LONG(0x11451419);
     } > MEM
 
     /DISCARD/ :