Add stack protector

This commit is contained in:
Mathieu Strypsteen 2024-07-09 10:18:44 +02:00
parent 470c208284
commit 1ee034683a
4 changed files with 27 additions and 17 deletions

19
kernel/src/cpu/boot.s Normal file
View file

@ -0,0 +1,19 @@
rdrand:
rdrand %rax
jnc rdrand
ret
.global _start
_start:
mov %rcx, %rdi
mov $stack, %rsp
call rdrand
mov %rax, __stack_chk_guard
call main
.section .bss
.align 8
.global __stack_chk_guard
__stack_chk_guard:
.skip 8
.align 16
.skip 0x100000
stack:

View file

@ -25,20 +25,7 @@ mod sys;
static ALLOC: LockedHeap = LockedHeap::empty();
pub static RSDP_ADDRESS: Once<u64> = Once::new();
global_asm!(
"
.global _start
_start:
mov %rcx, %rdi
mov $stack, %rsp
call main
.section .bss
.align 16
.skip 0x100000
stack:
",
options(att_syntax)
);
global_asm!(include_str!("cpu/boot.s"), options(att_syntax));
#[no_mangle]
extern "C" fn main(temp_loader_struct: *const LoaderStruct) -> ! {
@ -55,11 +42,11 @@ extern "C" fn main(temp_loader_struct: *const LoaderStruct) -> ! {
if loader_struct.magic != LOADER_STRUCT_MAGIC {
panic!("Invalid bootloader struct");
}
RSDP_ADDRESS.call_once(|| loader_struct.rsdp_address);
setup_gdt();
setup_idt();
setup_paging(&loader_struct, loader_struct.phys_kernel_start, loader_struct.phys_heap_start);
disable_pic();
RSDP_ADDRESS.call_once(|| loader_struct.rsdp_address);
loop {
hlt();
}
@ -72,3 +59,7 @@ fn panic(info: &PanicInfo) -> ! {
hlt();
}
}
#[no_mangle]
extern "C" fn __stack_chk_fail() {
panic!("Stack smashing detected");
}

View file

@ -3,7 +3,7 @@ COMPONENTS:=dispatcher events executer hardware namespace parser resources table
CFILES:=$(foreach comp, $(COMPONENTS), $(wildcard ../acpica/source/components/$(comp)/*.c))
OFILES:=$(patsubst %.c, %.o, $(CFILES))
CC:=clang
CFLAGS:=--target=x86_64-elf -ffreestanding -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -O2 -I. -I../acpica/source/include -DACPI_LIBRARY -D__linux__
CFLAGS:=--target=x86_64-elf -ffreestanding -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -fstack-protector-strong -O2 -I. -I../acpica/source/include -DACPI_LIBRARY -D__linux__
libacpica.a: $(OFILES)
llvm-ar rcD $@ $^

View file

@ -2,4 +2,4 @@
# shellcheck disable=SC2068
set -e
./build.sh
qemu-system-x86_64 -M q35 -m 256M -L /usr/share/OVMF -bios OVMF_CODE.fd -drive file=img/os.img,format=raw $@
qemu-system-x86_64 -M q35 -cpu qemu64,+rdrand -m 256M -L /usr/share/OVMF -bios OVMF_CODE.fd -drive file=img/os.img,format=raw $@