Skip to content

Commit be5d5e9

Browse files
committed
Create VERIFY_STACKGUARD to prevent stackoverflow in kernel
1 parent 7cafe35 commit be5d5e9

File tree

7 files changed

+66
-3
lines changed

7 files changed

+66
-3
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ binaries: $(bt_stage1) $(bt_stage2) $(kernel_core) $(rm_static)
102102
SECTOR_COUNT_BT_STAGE1 = 1
103103
SECTOR_COUNT_SHARED_LIBRARY = 1
104104
SECTOR_COUNT_BT_STAGE2 = 11
105-
SECTOR_COUNT_KERNEL = 47
105+
SECTOR_COUNT_KERNEL = 48
106106

107107
SECTOR_START_BT_STAGE1 = 0
108108
SECTOR_START_SHARED_LIBRARY = $(shell expr $(SECTOR_START_BT_STAGE1) + $(SECTOR_COUNT_BT_STAGE1) )
@@ -141,6 +141,7 @@ include $(SRC_DRIVERS)/pic/Makefile.mk
141141

142142
include $(SRC_DIR)/fs/Makefile.mk
143143
include $(SRC_DIR)/memmgr/tables/Makefile.mk
144+
include $(SRC_DIR)/memmgr/stackguard/Makefile.mk
144145

145146
include $(SRC_LIB)/app/Makefile.mk
146147
include $(SRC_LIB_DS)/Makefile.mk
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#define __MACRO_TO_STRING_INTERNAL(x) #x
4+
#define MACRO_TO_STRING(x) __MACRO_TO_STRING_INTERNAL(x)
5+
#define __STR__LINE__ MACRO_TO_STRING(__LINE__)
6+
7+
#define VERIFY_STACKGUARD() (verify_stack_guard(__FILE__ "[" __STR__LINE__ "]; verify_stack_guard failed"))
8+
9+
void verify_stack_guard(char err_message[]);

src/kernel/Makefile.mk

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ $(SELF_BUILD_DIR)/%_asm.o: $(SELF_SRC_DIR)/%.asm
1313
mkdir -p $(dir $@)
1414
$(NASM) -o $@ -i $(SRC_REALMODE)/ $<
1515

16-
$(kernel_core).elf: $(SELF_BUILD_DIR)/core_asm.o $(SELF_BUILD_DIR)/panic_asm.o $(SELF_BUILD_ALL_C) \
16+
$(kernel_core).elf: $(SELF_BUILD_DIR)/core_asm.o \
17+
$(SELF_BUILD_DIR)/panic_asm.o \
18+
$(SELF_BUILD_ALL_C) \
1719
$(BUILD_KERNEL)/interrupts/libinterrupts \
1820
$(BUILD_KERNEL)/syscall/libsyscall \
1921
$(BUILD_KERNEL)/process/libprocess \
@@ -26,7 +28,9 @@ $(kernel_core).elf: $(SELF_BUILD_DIR)/core_asm.o $(SELF_BUILD_DIR)/panic_asm.o $
2628
$(BUILD_LIB_DS)/libds \
2729
$(BUILD_DRIVERS)/disk/libdisk \
2830
$(BUILD_DIR)/real_mode/librealmodeclient \
29-
$(BUILD_USR_LIB)/libfuzzyc
31+
$(BUILD_USR_LIB)/libfuzzyc \
32+
$(BUILD_DIR)/memmgr/stackguard/libstackguard # stackguard must be the last one
33+
3034
mkdir -p $(dir $@)
3135
$(KERNEL_LD) -o $@ $^
3236

src/kernel/core.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <fuzzy/kernel/interrupts/interrupts.h>
44
#include <fuzzy/kernel/interrupts/timer.h>
55
#include <fuzzy/kernel/process/process.h>
6+
#include <fuzzy/memmgr/stackguard/stackguard.h>
67

78
#include <string.h>
89
#include <process.h>
@@ -50,6 +51,7 @@ void kernel_core_entry() {
5051

5152
clrscr();
5253

54+
VERIFY_STACKGUARD();
5355
int init_pid = spawnl(INIT_APPNAME, INIT_APPNAME, NULL);
5456
print_log("init process got created: %d", init_pid);
5557

src/memmgr/stackguard/Makefile.mk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
$(SELF_BUILD_DIR)/%.o: $(SELF_SRC_DIR)/%.c $(BUILD_USR_INCLUDE_ALL)
2+
mkdir -p $(dir $@)
3+
$(KERNEL_CC) -c -o $@ $<
4+
5+
$(SELF_BUILD_DIR)/%_asm.o: $(SELF_SRC_DIR)/%.asm
6+
mkdir -p $(dir $@)
7+
nasm -o $@ -f elf32 $<
8+
9+
$(SELF_BUILD_DIR)/libstackguard: $(SELF_BUILD_ALL_C) $(SELF_BUILD_ALL_ASM)
10+
ar rc $@ $^
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
; Corresponding object file should be linked at the tail of the
2+
; instructions.
3+
; Assumes .data gets linked after .text
4+
5+
[BITS 32]
6+
7+
global check_stack_guard
8+
9+
; it would be nice if magic number is present at head of
10+
; instruction but this should be enough for now.
11+
STACK_GUARD_MAGIC_NUMBER EQU 0x4C10A5C7
12+
13+
[SECTION .text]
14+
check_stack_guard:
15+
; return 0 is stack is good
16+
push ebp
17+
mov ebp, esp
18+
19+
mov eax, [stack_guard_pointer]
20+
xor eax, STACK_GUARD_MAGIC_NUMBER
21+
22+
pop ebp
23+
ret
24+
25+
[SECTION .data]
26+
stack_guard_pointer dd STACK_GUARD_MAGIC_NUMBER

src/memmgr/stackguard/stackguard.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <fuzzy/kernel/panic.h>
2+
#include <fuzzy/memmgr/stackguard/stackguard.h>
3+
4+
extern int check_stack_guard();
5+
6+
void verify_stack_guard(char err_message[]) {
7+
int err = check_stack_guard();
8+
if (err != 0) {
9+
PANIC(0, err_message);
10+
}
11+
}

0 commit comments

Comments
 (0)