@@ -47,60 +47,49 @@ macro(os_set_flags)
4747 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread" )
4848 endif ()
4949
50- ###############
51- # According to SDLE we need to add the following flags for additional security:
52- # Debug & Release:
53- # -Wformat: Checks for format string vulnerabilities.
54- # -Wformat-security: Ensures format strings are not vulnerable to attacks.
55- # -fPIC: Generates position-independent code (PIC) suitable for shared libraries.
56- # -fPIE: Generates position-independent executable (PIE) code.
57- # -pie: Links the output as a position-independent executable.
58- # -D_FORTIFY_SOURCE=2: Adds extra checks for buffer overflows.
59- # -mfunction-return=thunk: Mitigates return-oriented programming (ROP) attacks. (Added flag -fcf-protection=none to allow it)
60- # -mindirect-branch=thunk: Mitigates indirect branch attacks.
61- # -mindirect-branch-register: Uses registers for indirect branches to mitigate attacks.
62- # -fstack-protector: Adds stack protection to detect buffer overflows.
50+
51+ if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" )
52+ # Due to security reasons we need to add the following flags for additional security:
53+ # Debug & Release:
54+ # -Wformat: Checks for format string vulnerabilities.
55+ # -Wformat-security: Ensures format strings are not vulnerable to attacks.
56+ # -fPIC: Generates position-independent code during the compilation phase.
57+ # -fPIE: Generates position-independent executables during the compilation phase.
58+ # -D_FORTIFY_SOURCE=2: Adds extra checks for buffer overflows.
59+ # -fstack-protector: Adds stack protection to detect buffer overflows.
6360
64- # Release only
65- # -Werror: Treats all warnings as errors.
66- # -Werror=format-security: Treats format security warnings as errors.
67- # -z noexecstack: Marks the stack as non-executable to prevent certain types of attacks.
68- # -Wl,-z,relro,-z,now: Enables read-only relocations and immediate binding for security.
69- # -fstack-protector-strong: Provides stronger stack protection than -fstack-protector.
70-
71- # see https://readthedocs.intel.com/SecureCodingStandards/2023.Q2.0/compiler/c-cpp/ for more details
61+ # Release only
62+ # -Werror: Treats all warnings as errors.
63+ # -Werror=format-security: Treats format security warnings as errors.
64+ # -z noexecstack: Marks the stack as non-executable to prevent certain types of attacks.
65+ # -Wl,-z,relro,-z,now: Enables read-only relocations and immediate binding for security.
66+ # -fstack-protector-strong: Provides stronger stack protection than -fstack-protector.
67+
68+ # Linker flags
69+ # -pie: Produces position-independent executables during the linking phase.
70+
71+ # see https://readthedocs.intel.com/SecureCodingStandards/2023.Q2.0/compiler/c-cpp/ for more details
7272
73- if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|armv7l" OR APPLE OR # Some flags are not recognized or some systems / gcc versions
74- (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0" )) #
75- set (ADDITIONAL_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -fstack-protector" )
76- else ()
77- #‘-mfunction-return’ and ‘-fcf-protection’ are not compatible, so specifing -fcf-protection=none
78- set (ADDITIONAL_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -fcf-protection=none -mfunction-return=thunk -mindirect-branch=thunk -mindirect-branch-register -fstack-protector" )
79- endif ()
80- set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -pie" )
81-
82- set (ADDITIONAL_COMPILER_FLAGS "${ADDITIONAL_COMPILER_FLAGS} -Wno-error=stringop-overflow" )
73+ set (SECURITY_COMPILER_FLAGS "-Wformat -Wformat-security -fPIC -fstack-protector -Wno-error=stringop-overflow" )
8374
84- string (FIND "${CMAKE_CXX_FLAGS} " "-D_FORTIFY_SOURCE" _index)
85- if (${_index} EQUAL -1) # Define D_FORTIFY_SOURCE is undefined
86- set (ADDITIONAL_COMPILER_FLAGS "${ADDITIONAL_COMPILER_FLAGS } -D_FORTIFY_SOURCE=2" )
87- endif ()
75+ string (FIND "${CMAKE_CXX_FLAGS} " "-D_FORTIFY_SOURCE" _index)
76+ if (${_index} EQUAL -1) # Define D_FORTIFY_SOURCE if undefined
77+ set (SECURITY_COMPILER_FLAGS "${SECURITY_COMPILER_FLAGS } -D_FORTIFY_SOURCE=2" )
78+ endif ()
8879
89- if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
90- message (STATUS "Configuring for Debug build" )
91- else () # Release, RelWithDebInfo, or multi configuration generator is being used (aka not specifing build type, or building with VS)
92- message (STATUS "Configuring for Release build" )
93- set (ADDITIONAL_COMPILER_FLAGS "${ADDITIONAL_COMPILER_FLAGS} -Werror -z noexecstack -Wl,-z,relro,-z,now -fstack-protector-strong" )
94- endif ()
95-
96- set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ADDITIONAL_COMPILER_FLAGS} " )
97- set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ADDITIONAL_COMPILER_FLAGS} " )
98-
99-
100- set_directory_properties (PROPERTIES DIRECTORY third-party/ COMPILE_OPTIONS "-w" )
101- set_source_files_properties (third-party/*.* PROPERTIES COMPILE_OPTIONS "-w" )
80+ if (CMAKE_BUILD_TYPE STREQUAL "Debug" )
81+ message (STATUS "Configuring for Debug build" )
82+ else () # Release, RelWithDebInfo, or multi configuration generator is being used (aka not specifing build type, or building with VS)
83+ message (STATUS "Configuring for Release build" )
84+ set (SECURITY_COMPILER_FLAGS "${SECURITY_COMPILER_FLAGS} -Werror -z noexecstack -Wl,-z,relro,-z,now -fstack-protector-strong" )
85+ endif ()
86+
87+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${SECURITY_COMPILER_FLAGS} " )
88+ set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${SECURITY_COMPILER_FLAGS} " )
89+
90+ set (CMAKE_LINKER_FLAGS "${CMAKE_LINKER_FLAGS} -pie" )
10291
103- #################
92+ endif ()
10493
10594 if (APPLE )
10695 set (FORCE_RSUSB_BACKEND ON )
0 commit comments