Libc: add stack smashing protection, moved kernel panic from abort to panic function in stdlib

This commit is contained in:
g1n 2021-08-06 19:15:42 +03:00
parent e839ac8a3f
commit 99888ea24d
6 changed files with 37 additions and 4 deletions

View File

@ -13,7 +13,7 @@ EXEC_PREFIX?=$(PREFIX)
BOOTDIR?=$(EXEC_PREFIX)/boot
INCLUDEDIR?=$(PREFIX)/include
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra
CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra -fstack-protector-all
CPPFLAGS:=$(CPPFLAGS) -D__is_kernel -Iinclude
LDFLAGS:=$(LDFLAGS)
LIBS:=$(LIBS) -nostdlib -lk -lgcc

View File

@ -33,6 +33,8 @@ stdio/printf.o \
stdio/putchar.o \
stdio/puts.o \
stdlib/abort.o \
stdlib/panic.o \
stdlib/stack_protection.o \
string/memcmp.o \
string/memcpy.o \
string/memmove.o \

View File

@ -9,6 +9,7 @@ extern "C" {
__attribute__((__noreturn__))
void abort(void);
void panic(char *panic_msg);
#ifdef __cplusplus
}

View File

@ -5,11 +5,9 @@ __attribute__((__noreturn__))
void abort(void) {
#if defined(__is_libk)
// TODO: Add proper kernel panic.
printf("kernel: panic: abort()\n");
panic("abort()\n");
#else
// TODO: Abnormally terminate the process as if by SIGABRT.
printf("abort()\n");
#endif
while (1) { }
__builtin_unreachable();
}

12
libc/stdlib/panic.c Normal file
View File

@ -0,0 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
__attribute__((__noreturn__))
void panic(char *panic_msg) {
#if defined(__is_libk)
// TODO: Add proper kernel panic.
printf("kernel: panic: %s\n", panic_msg);
#endif
while (1) { }
__builtin_unreachable();
}

View File

@ -0,0 +1,20 @@
#include <stdint.h>
#include <stdlib.h>
#if UINT32_MAX == UINTPTR_MAX
#define STACK_CHK_GUARD 0xe2dee396
#else
#define STACK_CHK_GUARD 0x595e9fbd94fda766
#endif
uintptr_t __stack_chk_guard = STACK_CHK_GUARD;
__attribute__((noreturn))
void __stack_chk_fail(void)
{
#if __STDC_HOSTED__
abort();
#elif __is_myos_kernel
panic("Stack smashing detected");
#endif
}