From c8d07514c8e0bc235e180f6990f914ee86880f8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Gardou?= Date: Thu, 31 Dec 2020 17:56:39 +0100 Subject: [PATCH] [CMAKE] Fix GCC -fstack-protector usage --- boot/environ/CMakeLists.txt | 4 +- boot/freeldr/freeldr/CMakeLists.txt | 4 +- dll/keyboard/CMakeLists.txt | 2 +- dll/ntdll/CMakeLists.txt | 10 +++ dll/win32/msvcrt/CMakeLists.txt | 5 ++ drivers/storage/port/scsiport/CMakeLists.txt | 5 ++ .../apitests/win32u/win32u_ros/CMakeLists.txt | 6 +- ntoskrnl/CMakeLists.txt | 10 ++- ntoskrnl/ntkrnlmp/CMakeLists.txt | 2 +- sdk/cmake/gcc.cmake | 6 +- sdk/lib/crt/msvcrtex.cmake | 4 - sdk/lib/gcc_ssp/CMakeLists.txt | 14 +++- sdk/lib/gcc_ssp/gcc_ssp.c | 74 +++++++++++++++---- win32ss/CMakeLists.txt | 5 ++ win32ss/drivers/videoprt/CMakeLists.txt | 5 ++ 15 files changed, 121 insertions(+), 35 deletions(-) diff --git a/boot/environ/CMakeLists.txt b/boot/environ/CMakeLists.txt index 4f62a8b111b..05aa5b81ef9 100644 --- a/boot/environ/CMakeLists.txt +++ b/boot/environ/CMakeLists.txt @@ -105,7 +105,7 @@ target_link_libraries(bootmgfw bootlib cportlib cmlib rtl libcntpr) # dynamic analysis switches if(STACK_PROTECTOR) - target_link_libraries(bootmgfw gcc_ssp) + target_sources(bootmgfw PRIVATE $) endif() if(RUNTIME_CHECKS) @@ -153,7 +153,7 @@ target_link_libraries(rosload bootlib cportlib cmlib rtl libcntpr) # dynamic analysis switches if(STACK_PROTECTOR) - target_link_libraries(rosload gcc_ssp) + target_sources(rosload PRIVATE $) endif() if(RUNTIME_CHECKS) diff --git a/boot/freeldr/freeldr/CMakeLists.txt b/boot/freeldr/freeldr/CMakeLists.txt index 87116af5978..95dc2e31224 100644 --- a/boot/freeldr/freeldr/CMakeLists.txt +++ b/boot/freeldr/freeldr/CMakeLists.txt @@ -312,8 +312,8 @@ target_link_libraries(freeldr_pe_dbg freeldr_common cportlib cmlib rtl libcntpr) # dynamic analysis switches if(STACK_PROTECTOR) - target_link_libraries(freeldr_pe gcc_ssp) - target_link_libraries(freeldr_pe_dbg gcc_ssp) + target_sources(freeldr_pe PRIVATE $) + target_sources(freeldr_pe_dbg PRIVATE $) endif() if(RUNTIME_CHECKS) diff --git a/dll/keyboard/CMakeLists.txt b/dll/keyboard/CMakeLists.txt index f728060633c..76cb12e1ea7 100644 --- a/dll/keyboard/CMakeLists.txt +++ b/dll/keyboard/CMakeLists.txt @@ -109,7 +109,7 @@ foreach(_keyboard_layout ${_keyboard_layouts}) # dynamic analysis switches if(STACK_PROTECTOR) - target_link_libraries(${_keyboard_layout} gcc_ssp) + target_compile_options(${_keyboard_layout} PRIVATE -fno-stack-protector) endif() if(RUNTIME_CHECKS) diff --git a/dll/ntdll/CMakeLists.txt b/dll/ntdll/CMakeLists.txt index 09e077cb4e6..ce1a8fcc67b 100644 --- a/dll/ntdll/CMakeLists.txt +++ b/dll/ntdll/CMakeLists.txt @@ -3,6 +3,11 @@ add_subdirectory(nt_0600) spec2def(ntdll.dll def/ntdll.spec ADD_IMPORTLIB) +# Embed RTC libs +if (STACK_PROTECTOR) + target_sources(libntdll PRIVATE $) +endif() + add_definitions( -D__NTDLL__ -D_NTOSKRNL_ @@ -60,6 +65,11 @@ if(MSVC) endif() target_link_libraries(ntdll rtl ntdllsys libcntpr uuid ${PSEH_LIB}) + +if (STACK_PROTECTOR) + target_sources(ntdll PRIVATE $) +endif() + add_pch(ntdll include/ntdll.h "${PCH_SKIP_SOURCE}") add_dependencies(ntdll ntstatus asm) add_cd_file(TARGET ntdll DESTINATION reactos/system32 NO_CAB FOR all) diff --git a/dll/win32/msvcrt/CMakeLists.txt b/dll/win32/msvcrt/CMakeLists.txt index 61ae9432d74..f2f922ed85f 100644 --- a/dll/win32/msvcrt/CMakeLists.txt +++ b/dll/win32/msvcrt/CMakeLists.txt @@ -7,6 +7,11 @@ target_compile_definitions(libmsvcrt INTERFACE _DLL __USE_CRTIMP) # Embed msvcrtex into libmsvcrt target_sources(libmsvcrt PRIVATE $) +# Embed RTC libs +if (STACK_PROTECTOR) + target_sources(libmsvcrt PRIVATE $) + target_link_libraries(libmsvcrt INTERFACE libkernel32) # For OutputDebugStringA +endif() add_definitions( -DUSE_MSVCRT_PREFIX diff --git a/drivers/storage/port/scsiport/CMakeLists.txt b/drivers/storage/port/scsiport/CMakeLists.txt index e0f3ce08a78..f4bbc0b48e8 100644 --- a/drivers/storage/port/scsiport/CMakeLists.txt +++ b/drivers/storage/port/scsiport/CMakeLists.txt @@ -1,6 +1,11 @@ spec2def(scsiport.sys scsiport.spec ADD_IMPORTLIB) +# Embed RTC libs +if (STACK_PROTECTOR) + target_sources(libscsiport PRIVATE $) +endif() + list(APPEND SOURCE fdo.c ioctl.c diff --git a/modules/rostests/apitests/win32u/win32u_ros/CMakeLists.txt b/modules/rostests/apitests/win32u/win32u_ros/CMakeLists.txt index e8a8c03cf24..38bf98e2d50 100644 --- a/modules/rostests/apitests/win32u/win32u_ros/CMakeLists.txt +++ b/modules/rostests/apitests/win32u/win32u_ros/CMakeLists.txt @@ -4,7 +4,7 @@ include_directories(${REACTOS_SOURCE_DIR}/win32ss) spec2def(win32u.dll win32u_ros.spec ADD_IMPORTLIB) add_asm_files(win32u_ros_asm sys-stubs.S) -add_library(win32u MODULE +add_library(win32u MODULE main.c ${win32u_ros_asm} ${CMAKE_CURRENT_BINARY_DIR}/win32u.def) @@ -12,3 +12,7 @@ add_library(win32u MODULE set_module_type(win32u module) add_dependencies(win32u psdk) add_rostests_file(TARGET win32u) + +if (STACK_PROTECTOR) + target_compile_options(win32u PRIVATE -fno-stack-protector) +endif() diff --git a/ntoskrnl/CMakeLists.txt b/ntoskrnl/CMakeLists.txt index fd093c6ca06..8048520ec90 100644 --- a/ntoskrnl/CMakeLists.txt +++ b/ntoskrnl/CMakeLists.txt @@ -10,11 +10,17 @@ set(NTKRNLMP_SOURCE ${SOURCE}) set(NTKRNLMP_ASM_SOURCE ${ASM_SOURCE}) spec2def(ntoskrnl.exe ntoskrnl.spec ADD_IMPORTLIB) + +# Embed RTC libs +if (STACK_PROTECTOR) + target_sources(libntoskrnl PRIVATE $) +endif() + add_asm_files(ntoskrnl_asm ${NTOSKRNL_ASM_SOURCE}) if (GCC AND CLANG) # Clang optimises strcmp calls to memcmp. - target_link_libraries(libntoskrnl INTERFACE memcmp) + target_sources(libntoskrnl PRIVATE $) endif() list(APPEND PCH_SKIP_SOURCE @@ -58,7 +64,7 @@ endif() target_link_libraries(ntoskrnl cportlib csq ${PSEH_LIB} arbiter cmlib ntlsalib rtl ${ROSSYM_LIB} libcntpr wdmguid ioevent) if(STACK_PROTECTOR) - target_link_libraries(ntoskrnl gcc_ssp) + target_sources(ntoskrnl PRIVATE $) elseif(RUNTIME_CHECKS) target_link_libraries(ntoskrnl runtmchk) endif() diff --git a/ntoskrnl/ntkrnlmp/CMakeLists.txt b/ntoskrnl/ntkrnlmp/CMakeLists.txt index eb9610e1719..393fee96409 100644 --- a/ntoskrnl/ntkrnlmp/CMakeLists.txt +++ b/ntoskrnl/ntkrnlmp/CMakeLists.txt @@ -39,7 +39,7 @@ endif() # dynamic analysis switches if(STACK_PROTECTOR) - target_link_libraries(ntkrnlmp gcc_ssp) + target_sources(ntkrnlmp PRIVATE $) endif() if(RUNTIME_CHECKS) diff --git a/sdk/cmake/gcc.cmake b/sdk/cmake/gcc.cmake index bec502d75c7..72c0f5d09e0 100644 --- a/sdk/cmake/gcc.cmake +++ b/sdk/cmake/gcc.cmake @@ -35,7 +35,7 @@ if(USE_DUMMY_PSEH) endif() if(STACK_PROTECTOR) - add_compile_options(-fstack-protector-all) + add_compile_options(-fstack-protector-strong) endif() # Compiler Core @@ -287,10 +287,6 @@ function(set_module_type_toolchain MODULE TYPE) #Disabled due to LD bug: ROSBE-154 #add_linker_script(${MODULE} ${REACTOS_SOURCE_DIR}/sdk/cmake/init-section.lds) endif() - - if(STACK_PROTECTOR) - target_link_libraries(${MODULE} gcc_ssp) - endif() endfunction() function(add_delay_importlibs _module) diff --git a/sdk/lib/crt/msvcrtex.cmake b/sdk/lib/crt/msvcrtex.cmake index f720b977534..d969ba53b47 100644 --- a/sdk/lib/crt/msvcrtex.cmake +++ b/sdk/lib/crt/msvcrtex.cmake @@ -127,8 +127,4 @@ if(NOT MSVC) target_link_libraries(msvcrtex oldnames) endif() -if(STACK_PROTECTOR) - target_link_libraries(msvcrtex gcc_ssp) -endif() - add_dependencies(msvcrtex psdk asm) diff --git a/sdk/lib/gcc_ssp/CMakeLists.txt b/sdk/lib/gcc_ssp/CMakeLists.txt index a7556901e07..2e5a9e5bc0e 100644 --- a/sdk/lib/gcc_ssp/CMakeLists.txt +++ b/sdk/lib/gcc_ssp/CMakeLists.txt @@ -1,2 +1,14 @@ -add_library(gcc_ssp gcc_ssp.c) +add_library(gcc_ssp_nt OBJECT gcc_ssp.c) + +add_library(gcc_ssp_msvcrt OBJECT gcc_ssp.c) +target_compile_definitions(gcc_ssp_msvcrt PRIVATE _GCC_SSP_MSVCRT_) + +add_library(gcc_ssp_win32k OBJECT gcc_ssp.c) +target_compile_definitions(gcc_ssp_win32k PRIVATE _GCC_SSP_WIN32K_) + +add_library(gcc_ssp_videoprt OBJECT gcc_ssp.c) +target_compile_definitions(gcc_ssp_videoprt PRIVATE _GCC_SSP_VIDEOPRT_) + +add_library(gcc_ssp_scsiport OBJECT gcc_ssp.c) +target_compile_definitions(gcc_ssp_scsiport PRIVATE _GCC_SSP_SCSIPORT_) diff --git a/sdk/lib/gcc_ssp/gcc_ssp.c b/sdk/lib/gcc_ssp/gcc_ssp.c index 58e662cc36c..cb3664d1f27 100644 --- a/sdk/lib/gcc_ssp/gcc_ssp.c +++ b/sdk/lib/gcc_ssp/gcc_ssp.c @@ -1,23 +1,65 @@ -#define FAST_FAIL_STACK_COOKIE_CHECK_FAILURE 2 +#ifdef _GCC_SSP_MSVCRT_ + +#include +#include +#include + +#define print_caller() do { \ + char buffer[64]; \ + _snprintf(buffer, sizeof(buffer), "STACK PROTECTOR FAULT AT %p\n", __builtin_extract_return_addr(__builtin_return_address (0))); \ + OutputDebugStringA(buffer); \ +} while(0) + +#elif defined(_GCC_SSP_WIN32K_) + +#include +#include +#include +#include + +static inline +void +print_caller_helper(char* fmt, ...) +{ + va_list args; + + va_start(args, fmt); + EngDebugPrint("", fmt, args); + va_end(args); +} + +#define print_caller() print_caller_helper("STACK PROTECTOR FAULT AT %p\n", __builtin_extract_return_addr(__builtin_return_address(0))) + +#elif defined(_GCC_SSP_SCSIPORT_) + +#include +#include + +#define print_caller() ScsiDebugPrint(0, "STACK PROTECTOR FAULT AT %p\n", __builtin_extract_return_addr(__builtin_return_address(0))) + +#elif defined(_GCC_SSP_VIDEOPRT_) + +#include +#include +#include + +#define print_caller() VideoPortDebugPrint(0, "STACK PROTECTOR FAULT AT %p\n", __builtin_extract_return_addr(__builtin_return_address(0))) + +#else + +#include +#include + +#define print_caller() DbgPrint("STACK PROTECTOR FAULT AT %p\n", __builtin_extract_return_addr(__builtin_return_address(0))) + +#endif /* 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_guard = (void*)0xb00fbeefbaafb00f; void __stack_chk_fail() { - /* Like __fastfail */ - __asm__("int $0x29" : : "c"(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE) : "memory"); + print_caller(); + __asm__("int $3"); } diff --git a/win32ss/CMakeLists.txt b/win32ss/CMakeLists.txt index b74e41f0361..44dc77cd381 100644 --- a/win32ss/CMakeLists.txt +++ b/win32ss/CMakeLists.txt @@ -18,6 +18,11 @@ add_subdirectory(user/winsrv) spec2def(win32k.sys win32k.spec ADD_IMPORTLIB) +# embed RTC libs +if (STACK_PROTECTOR) + target_sources(libwin32k PRIVATE $) +endif() + include_directories( . include diff --git a/win32ss/drivers/videoprt/CMakeLists.txt b/win32ss/drivers/videoprt/CMakeLists.txt index 6f652c9ccff..cc5a174f57f 100644 --- a/win32ss/drivers/videoprt/CMakeLists.txt +++ b/win32ss/drivers/videoprt/CMakeLists.txt @@ -3,6 +3,11 @@ include_directories(${REACTOS_SOURCE_DIR}/ntoskrnl/include) add_definitions(-D_VIDEOPORT_) spec2def(videoprt.sys videoprt.spec ADD_IMPORTLIB) +# Embed RTC libs +if (STACK_PROTECTOR) + target_sources(libvideoprt PRIVATE $) +endif() + list(APPEND SOURCE agp.c child.c