[GCC/CMAKE]
authorJérôme Gardou <jerome.gardou@reactos.org>
Sat, 4 Oct 2014 20:26:14 +0000 (20:26 +0000)
committerJérôme Gardou <jerome.gardou@reactos.org>
Sat, 4 Oct 2014 20:26:14 +0000 (20:26 +0000)
 - Add a way to conditionally use the GCC stack protector.
Just add -DSTACK_PROTECTOR:BOOL=TRUE as argument to configure.cmd/sh

svn path=/trunk/; revision=64527

reactos/boot/freeldr/freeldr/CMakeLists.txt
reactos/cmake/config.cmake
reactos/cmake/gcc.cmake
reactos/lib/CMakeLists.txt
reactos/lib/gcc_ssp/CMakeLists.txt [new file with mode: 0644]
reactos/lib/gcc_ssp/gcc_ssp.c [new file with mode: 0644]
reactos/lib/sdk/crt/msvcrtex.cmake
reactos/ntoskrnl/CMakeLists.txt
reactos/ntoskrnl/ntkrnlmp/CMakeLists.txt

index 4551eb9..9e8c355 100644 (file)
@@ -210,6 +210,11 @@ endif()
 target_link_libraries(freeldr_pe freeldr_common cportlib cmlib rtl libcntpr)
 target_link_libraries(freeldr_pe_dbg freeldr_common cportlib cmlib rtl libcntpr)
 
+if (STACK_PROTECTOR)
+    target_link_libraries(freeldr_pe gcc_ssp)
+    target_link_libraries(freeldr_pe_dbg gcc_ssp)
+endif()
+
 add_dependencies(freeldr_pe asm)
 add_dependencies(freeldr_pe_dbg asm)
 
@@ -266,6 +271,11 @@ endif()
 target_link_libraries(setupldr_pe freeldr_common cportlib cmlib rtl libcntpr)
 target_link_libraries(setupldr_pe_dbg freeldr_common cportlib cmlib rtl libcntpr)
 
+if (STACK_PROTECTOR)
+    target_link_libraries(setupldr_pe gcc_ssp)
+    target_link_libraries(setupldr_pe_dbg gcc_ssp)
+endif()
+
 add_dependencies(setupldr_pe asm)
 add_dependencies(setupldr_pe_dbg asm)
 
index 4b04be8..1374cab 100644 (file)
@@ -75,6 +75,9 @@ set(_PREFAST_ FALSE CACHE BOOL
 "Whether to enable PREFAST while compiling.")
 set(_VS_ANALYZE_ FALSE CACHE BOOL
 "Whether to enable static analysis while compiling.")
+else()
+set(STACK_PROTECTOR FALSE CACHE BOOL
+"Whether to enbable the GCC stack checker while compiling")
 endif()
 
 set(USE_DUMMY_PSEH FALSE CACHE BOOL
index bcfb0c7..6ed8fe5 100644 (file)
@@ -30,6 +30,10 @@ if(USE_DUMMY_PSEH)
     add_definitions(-D_USE_DUMMY_PSEH=1)
 endif()
 
+if(STACK_PROTECTOR)
+    add_compile_flags(${MODULE} "-fstack-protector-all")
+endif()
+
 # Compiler Core
 add_compile_flags("-pipe -fms-extensions -fno-strict-aliasing")
 if(GCC_VERSION VERSION_GREATER 4.7)
@@ -281,6 +285,10 @@ function(set_module_type_toolchain MODULE TYPE)
             add_target_link_flags(${MODULE} "-Wl,--wdmdriver")
         endif()
     endif()
+    
+    if(STACK_PROTECTOR)
+        target_link_libraries(${MODULE} gcc_ssp)
+    endif()
 endfunction()
 
 function(add_delay_importlibs _module)
index a030ad1..b75a58c 100644 (file)
@@ -13,6 +13,9 @@ add_subdirectory(drivers)
 add_subdirectory(epsapi)
 add_subdirectory(fast486)
 add_subdirectory(fslib)
+if (STACK_PROTECTOR)
+    add_subdirectory(gcc_ssp)
+endif()
 add_subdirectory(lsalib)
 add_subdirectory(ppcmmu)
 add_subdirectory(pseh)
diff --git a/reactos/lib/gcc_ssp/CMakeLists.txt b/reactos/lib/gcc_ssp/CMakeLists.txt
new file mode 100644 (file)
index 0000000..389e629
--- /dev/null
@@ -0,0 +1,2 @@
+
+add_library(gcc_ssp STATIC gcc_ssp.c)
\ No newline at end of file
diff --git a/reactos/lib/gcc_ssp/gcc_ssp.c b/reactos/lib/gcc_ssp/gcc_ssp.c
new file mode 100644 (file)
index 0000000..58e662c
--- /dev/null
@@ -0,0 +1,23 @@
+
+#define FAST_FAIL_STACK_COOKIE_CHECK_FAILURE 2
+
+/* Should be random :-/ */
+void * __stack_chk_guard = (void*)0xf00df00d;
+
+#if 0
+void __stack_chk_guard_setup()
+{
+    unsigned char * p;
+    p = (unsigned char *)&__stack_chk_guard; // *** Notice that this takes the address of __stack_chk_guard  ***
+
+    /* If you have the ability to generate random numbers in your kernel then use them,
+       otherwise for 32-bit code: */
+    *p =  0x00000aff; // *** p is &__stack_chk_guard so *p writes to __stack_chk_guard rather than *__stack_chk_guard ***
+}
+#endif
+
+void __stack_chk_fail()
+{
+    /* Like __fastfail */
+    __asm__("int $0x29" : : "c"(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE) : "memory");
+}
index e9277cc..5eef28f 100644 (file)
@@ -85,4 +85,8 @@ if(NOT MSVC)
     target_link_libraries(msvcrtex oldnames)
 endif()
 
+if(STACK_PROTECTOR)
+    target_link_libraries(msvcrtex gcc_ssp)
+endif()
+
 add_dependencies(msvcrtex psdk asm)
index cd0ad3b..7f5a1d8 100644 (file)
@@ -44,6 +44,10 @@ target_link_libraries(ntoskrnl
     wdmguid
     ioevent)
 
+if(STACK_PROTECTOR)
+    target_link_libraries(ntoskrnl gcc_ssp)
+endif()
+
 add_importlibs(ntoskrnl hal kdcom bootvid)
 add_pch(ntoskrnl ${REACTOS_SOURCE_DIR}/ntoskrnl/include/ntoskrnl.h NTOSKRNL_SOURCE)
 add_dependencies(ntoskrnl psdk bugcodes asm)
index f70ea2e..350019d 100644 (file)
@@ -30,6 +30,10 @@ else()
     set_image_base(ntkrnlmp 0x80800000)
 endif()
 
+if(STACK_PROTECTOR)
+    target_link_libraries(ntkrnlmp gcc_ssp)
+endif()
+
 target_link_libraries(ntkrnlmp
     cportlib
     csq