Skip to content

Commit

Permalink
build: Set security flags for release builds (#6087)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Christian Menges <[email protected]>
  • Loading branch information
Garfield96 authored Jul 8, 2024
1 parent 4dfc256 commit 32cead0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ option(FLB_RELEASE "Build with release mode (-O2 -g -DNDEBUG)" No)
set(FLB_IPO "ReleaseOnly" CACHE STRING "Build with interprocedural optimization")
set_property(CACHE FLB_IPO PROPERTY STRINGS "On;Off;ReleaseOnly")
option(FLB_SMALL "Optimise for small size" No)
set(FLB_SECURITY "ReleaseOnly" CACHE STRING "Build with security optimizations")
set_property(CACHE FLB_SECURITY PROPERTY STRINGS "On;Off;ReleaseOnly")
option(FLB_COVERAGE "Build with code-coverage" No)
option(FLB_JEMALLOC "Build with Jemalloc support" No)
option(FLB_REGEX "Build with Regex support" Yes)
Expand Down Expand Up @@ -332,6 +334,21 @@ if(FLB_IPO STREQUAL "On" OR (FLB_IPO STREQUAL "ReleaseOnly" AND FLB_RELEASE))
endif()
endif()

# Harden release binary against security vulnerabilities
if(FLB_SECURITY STREQUAL "On" OR (FLB_SECURITY STREQUAL "ReleaseOnly" AND FLB_RELEASE))
if (NOT MSVC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,relro,-z,now")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,noexecstack")
if(NOT FLB_SMALL)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstack-protector")
# Fortify requires optimization
if(CMAKE_BUILD_TYPE STREQUAL "Release" OR CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D_FORTIFY_SOURCE=1")
endif()
endif()
endif()
endif()

if(FLB_PARSER)
FLB_DEFINITION(FLB_HAVE_PARSER)
message(STATUS "Enabling FLB_REGEX since FLB_PARSER requires")
Expand Down

0 comments on commit 32cead0

Please sign in to comment.