Skip to content

Commit

Permalink
Fixed hardening script. It turned out that position-independent code …
Browse files Browse the repository at this point in the history
…was not enabled, and enabling it breaks build with clang because of a bug in clang. Implemented a workaround to it.
  • Loading branch information
KOLANICH committed Sep 1, 2020
1 parent efac9bf commit 6ffe619
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
45 changes: 33 additions & 12 deletions cmake/Hardening.cmake
Expand Up @@ -8,6 +8,8 @@ include(CheckCXXCompilerFlag)

option(HARDENING_SSE2 "Enable hardening flags requiring at least SSE2 support for target" OFF)

set(CLANG_WORKAROUND_SCRIPT "${CMAKE_CURRENT_LIST_DIR}/clangLinkerWorkaround.sh")

function(determineSupportedHardeningFlags property)
set(FLAGS_HARDENING "")
foreach(flag ${ARGN})
Expand All @@ -22,17 +24,23 @@ function(determineSupportedHardeningFlags property)
endforeach(flag)
string(REPLACE ";" " " FLAGS_HARDENING "${FLAGS_HARDENING}")
#message(STATUS "FLAGS_HARDENING ${FLAGS_HARDENING}")
set(HARDENING_${property} "${FLAGS_HARDENING}" CACHE STRING "Hardening flags")
set(HARDENING_${property} "${FLAGS_HARDENING}" PARENT_SCOPE)
endfunction(determineSupportedHardeningFlags)

function(processFlagsList target property)
function(processFlagsList target property cache)
get_target_property(FLAGS_UNHARDENED ${target} ${property})
if(FLAGS_UNHARDENED MATCHES "FLAGS_UNHARDENED-NOTFOUND")
set(FLAGS_UNHARDENED "")
endif()
#message(STATUS "processFlagsList ${target} ${property} ${FLAGS_UNHARDENED}")
#message(STATUS "HARDENING_${property} ${HARDENING_${property}}")
if(HARDENING_${property})

if(cache)
if(HARDENING_${property})
else()
determineSupportedHardeningFlags(${property} ${ARGN})
set(HARDENING_${property} "${HARDENING_${property}}" CACHE STRING "Hardening flags")
endif()
else()
determineSupportedHardeningFlags(${property} ${ARGN})
endif()
Expand All @@ -45,28 +53,40 @@ function(processFlagsList target property)
endfunction(processFlagsList)

function(setupPIC target)
set_property(TARGET ${target} PROPERTY POSITION_INDEPENDENT_CODE ON) # FUCK, doesn't work
set_property(TARGET ${target} PROPERTY POSITION_INDEPENDENT_CODE ON) # bad, doesn't work
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
get_target_property(type ${target} TYPE)
if(type STREQUAL "EXECUTABLE")
list(APPEND HARDENING_COMPILER_FLAGS
list(APPEND HARDENING_PIC_COMPILE_FLAGS
"-fPIE"
)
else()
list(APPEND HARDENING_COMPILER_FLAGS
list(APPEND HARDENING_PIC_COMPILE_FLAGS
"-fPIC"
)
endif()
list(APPEND HARDENING_LINKER_FLAGS
"-pie"
)
if(type STREQUAL "EXECUTABLE")
# https://mropert.github.io/2018/02/02/pic_pie_sanitizers/
list(APPEND HARDENING_PIC_LINKER_FLAGS
"-Wl,-pie"
)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
message(STATUS "Working around Clang bug https://bugs.llvm.org/show_bug.cgi?id=44594 ...")
list(APPEND HARDENING_PIC_LINKER_FLAGS
"--ld-path=\"${CLANG_WORKAROUND_SCRIPT}\""
)

endif()
endif()
elseif(MSVC)
list(APPEND HARDENING_COMPILER_FLAGS
list(APPEND HARDENING_PIC_COMPILE_FLAGS
"/dynamicbase" "/HIGHENTROPYVA"
)
else()
message(ERROR "The compiler is not supported")
endif()
processFlagsList(${target} COMPILE_FLAGS OFF ${HARDENING_PIC_COMPILE_FLAGS})
processFlagsList(${target} LINK_FLAGS OFF ${HARDENING_PIC_LINKER_FLAGS})
endfunction(setupPIC)

function(harden target)
Expand Down Expand Up @@ -183,6 +203,7 @@ function(harden target)
"-Wl,-z,ibtplt"
"-Wl,-z,ibt"
"-Wl,-z,shstk"
"-Wl,-z,notext" # may be required for PIC
)
endif()
list(APPEND HARDENING_MACRODEFS
Expand All @@ -196,8 +217,8 @@ function(harden target)
message(ERROR "The compiler is not supported")
endif()

processFlagsList(${target} COMPILE_FLAGS ${HARDENING_COMPILER_FLAGS})
processFlagsList(${target} LINK_FLAGS ${HARDENING_LINKER_FLAGS})
processFlagsList(${target} COMPILE_FLAGS ON ${HARDENING_COMPILER_FLAGS})
processFlagsList(${target} LINK_FLAGS ON ${HARDENING_LINKER_FLAGS})

#list(JOIN HARDENING_MACRODEFS " " HARDENING_MACRODEFS) # unneeded, list is needed, not string
set(HARDENING_MACRODEFS "${HARDENING_MACRODEFS}" CACHE STRING "Hardening flags CMake list (not string!)")
Expand Down
31 changes: 31 additions & 0 deletions cmake/clangLinkerWorkaround.sh
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

#This is free and unencumbered software released into the public domain.
#Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
#In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#For more information, please refer to <https://unlicense.org/>

echo "There is a long-ignored bug in CLang. https://bugs.llvm.org/show_bug.cgi?id=44594 . Have to work around."

$(
echo ld
i=0
for el in $@; do
if [ -e "$el" ]; then
fn=$(basename $el);
ext=${fn##*.}
dn=$(dirname $el);
if [[ $dn == *"/lib/gcc/"* ]]; then
if [ "$ext" = "o" ]; then
bn="${fn%.*}";
newFulN="$dn/${bn}S.o";
if [ -e "$newFulN" ]; then
el=$newFulN;
fi;
fi;
fi;
fi;
echo $el;
done;
) | xargs

0 comments on commit 6ffe619

Please sign in to comment.