From b426c0f2ee9a4ab8c15f8cf0e3f55de16572976d Mon Sep 17 00:00:00 2001 From: bumbread Date: Mon, 8 Aug 2022 23:39:43 +1100 Subject: [PATCH] Support for -fstack-protector --- src/_win/cookie.c | 15 +++++++++++++++ src/_win/entry.c | 2 ++ src/_win/win.h | 1 + src/ciabatta.c | 1 + test.cmd | 2 +- test/alloca.c | 10 ++++++++++ test/helloworld.c | 7 +++++++ 7 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/_win/cookie.c create mode 100644 test/alloca.c create mode 100644 test/helloworld.c diff --git a/src/_win/cookie.c b/src/_win/cookie.c new file mode 100644 index 0000000..1f3b009 --- /dev/null +++ b/src/_win/cookie.c @@ -0,0 +1,15 @@ + +u64 __security_cookie; + +void __security_init_cookie() { + // They say it's a random number so I generated + // one using numbergenerator.org + __security_cookie = 0xb26e04cc62ba48a; +} + +void __security_check_cookie(u64 retrieved) { + if(__security_cookie != retrieved) { + printf("Bro you've got a buffer overrun\n"); + abort(); + } +} diff --git a/src/_win/entry.c b/src/_win/entry.c index bd2b406..2b679a4 100644 --- a/src/_win/entry.c +++ b/src/_win/entry.c @@ -12,6 +12,7 @@ _Noreturn void mainCRTStartup() { _setup_heap(); _setup_timer(); _setup_io(); + __security_init_cookie(); srand(0); setlocale(LC_ALL, "C"); @@ -28,6 +29,7 @@ _Noreturn void WinMainCRTStartup() { _setup_heap(); _setup_timer(); _setup_io(); + __security_init_cookie(); srand(0); setlocale(LC_ALL, "C"); diff --git a/src/_win/win.h b/src/_win/win.h index a1c5d7f..9daefb1 100644 --- a/src/_win/win.h +++ b/src/_win/win.h @@ -12,3 +12,4 @@ void _setup_eh(); void _setup_heap(); void _setup_io(); void _close_io(); +void __security_init_cookie(); \ No newline at end of file diff --git a/src/ciabatta.c b/src/ciabatta.c index fd4f780..fef13f1 100644 --- a/src/ciabatta.c +++ b/src/ciabatta.c @@ -66,6 +66,7 @@ // Windows stuff #if defined(CIABATTA_WIN) #include "_win/win.h" + #include "_win/cookie.c" #include "_win/assert.c" #include "_win/cmdline.c" #include "_win/entry.c" diff --git a/test.cmd b/test.cmd index a1c4583..c543659 100644 --- a/test.cmd +++ b/test.cmd @@ -1,2 +1,2 @@ -clang -std=c11 test\%1 utf8.obj -Iinc -g -lciabatta.lib -nostdlib \ No newline at end of file +clang -std=c11 test\%1 utf8.obj -Iinc -g -lciabatta.lib -nostdlib -fstack-protector \ No newline at end of file diff --git a/test/alloca.c b/test/alloca.c new file mode 100644 index 0000000..39b7d53 --- /dev/null +++ b/test/alloca.c @@ -0,0 +1,10 @@ +#include +#include +int main() { + uint8_t *sp = _alloca(512); + for(int i = 0; i != 550; ++i) { + sp[i] = 0; + } + printf("%p\n", sp); + return 0; +} diff --git a/test/helloworld.c b/test/helloworld.c new file mode 100644 index 0000000..f1f0b40 --- /dev/null +++ b/test/helloworld.c @@ -0,0 +1,7 @@ + +#include + +int main() { + printf("Hello, World!\n"); + return 0; +}