[CMAKE] Fix GCC -fstack-protector usage

This commit is contained in:
Jérôme Gardou 2020-12-31 17:56:39 +01:00
parent a853102a7b
commit c8d07514c8
15 changed files with 121 additions and 35 deletions

View file

@ -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 $<TARGET_OBJECTS:gcc_ssp_nt>)
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 $<TARGET_OBJECTS:gcc_ssp_nt>)
endif()
if(RUNTIME_CHECKS)

View file

@ -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_OBJECTS:gcc_ssp_nt>)
target_sources(freeldr_pe_dbg PRIVATE $<TARGET_OBJECTS:gcc_ssp_nt>)
endif()
if(RUNTIME_CHECKS)

View file

@ -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)

View file

@ -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 $<TARGET_OBJECTS:gcc_ssp_nt>)
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 $<TARGET_OBJECTS:gcc_ssp_nt>)
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)

View file

@ -7,6 +7,11 @@ target_compile_definitions(libmsvcrt INTERFACE _DLL __USE_CRTIMP)
# Embed msvcrtex into libmsvcrt
target_sources(libmsvcrt PRIVATE $<TARGET_OBJECTS:msvcrtex>)
# Embed RTC libs
if (STACK_PROTECTOR)
target_sources(libmsvcrt PRIVATE $<TARGET_OBJECTS:gcc_ssp_msvcrt>)
target_link_libraries(libmsvcrt INTERFACE libkernel32) # For OutputDebugStringA
endif()
add_definitions(
-DUSE_MSVCRT_PREFIX

View file

@ -1,6 +1,11 @@
spec2def(scsiport.sys scsiport.spec ADD_IMPORTLIB)
# Embed RTC libs
if (STACK_PROTECTOR)
target_sources(libscsiport PRIVATE $<TARGET_OBJECTS:gcc_ssp_scsiport>)
endif()
list(APPEND SOURCE
fdo.c
ioctl.c

View file

@ -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()

View file

@ -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 $<TARGET_OBJECTS:gcc_ssp_nt>)
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 $<TARGET_OBJECTS:memcmp>)
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 $<TARGET_OBJECTS:gcc_ssp_nt>)
elseif(RUNTIME_CHECKS)
target_link_libraries(ntoskrnl runtmchk)
endif()

View file

@ -39,7 +39,7 @@ endif()
# dynamic analysis switches
if(STACK_PROTECTOR)
target_link_libraries(ntkrnlmp gcc_ssp)
target_sources(ntkrnlmp PRIVATE $<TARGET_OBJECTS:gcc_ssp_nt>)
endif()
if(RUNTIME_CHECKS)

View file

@ -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)

View file

@ -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)

View file

@ -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_)

View file

@ -1,23 +1,65 @@
#define FAST_FAIL_STACK_COOKIE_CHECK_FAILURE 2
#ifdef _GCC_SSP_MSVCRT_
#include <windef.h>
#include <winbase.h>
#include <stdio.h>
#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 <windef.h>
#include <wingdi.h>
#include <winddi.h>
#include <stdarg.h>
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 <ntddk.h>
#include <srb.h>
#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 <ntdef.h>
#include <miniport.h>
#include <video.h>
#define print_caller() VideoPortDebugPrint(0, "STACK PROTECTOR FAULT AT %p\n", __builtin_extract_return_addr(__builtin_return_address(0)))
#else
#include <ntdef.h>
#include <debug.h>
#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");
}

View file

@ -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 $<TARGET_OBJECTS:gcc_ssp_win32k>)
endif()
include_directories(
.
include

View file

@ -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 $<TARGET_OBJECTS:gcc_ssp_videoprt>)
endif()
list(APPEND SOURCE
agp.c
child.c