Skip to content

Commit 6fab274

Browse files
committed
Control-flow Enforcement Technology (CET), published by Intel, introduces
indirect branch tracking(IBT) feature aiming to ensure the target address of an indirect jump/call is not tampered. When IBT is enabled, each function or target of any indirect jump/call will start with an 'endbr32/64' instruction otherwise the program will crash during execution. To build an application with CET enabled. we need to ensure: 1. build the source code with "-fcf-protection=full" 2. all the libraries linked with .o files must be CET enabled too This patch aims to enable CET for compiler-rt builtins library, we add an option "COMPILER_RT_ENABLE_CET" whose default value is OFF to enable CET for compiler-rt in building time and when this option is "ON", "-fcf-protection=full" is added to BUILTINS_CFLAG and the "endbr32/64" will be placed in the beginning of each assembly function. We also enabled CET for crtbegin, crtend object files in this patch. Reviewed by: MaskRay, compnerd, manojgupta, efriedma Differential Revision: https://reviews.llvm.org/D109811 Signed-off-by: jinge90 <ge.jin@intel.com>
1 parent 9b70dda commit 6fab274

File tree

7 files changed

+41
-0
lines changed

7 files changed

+41
-0
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ option(COMPILER_RT_BUILD_ORC "Build ORC runtime" ON)
5454
mark_as_advanced(COMPILER_RT_BUILD_ORC)
5555
option(COMPILER_RT_BUILD_GWP_ASAN "Build GWP-ASan, and link it into SCUDO" ON)
5656
mark_as_advanced(COMPILER_RT_BUILD_GWP_ASAN)
57+
option(COMPILER_RT_ENABLE_CET "Build Compiler RT with CET enabled" OFF)
5758

5859
if(FUCHSIA)
5960
set(COMPILER_RT_HWASAN_WITH_INTERCEPTORS_DEFAULT OFF)
@@ -244,6 +245,14 @@ include(config-ix)
244245
# Setup Compiler Flags
245246
#================================
246247

248+
# fcf-protection is a gcc/clang option for CET support on Linux platforms.
249+
# We need to handle MSVC CET option on Windows platforms.
250+
if (NOT MSVC)
251+
if (COMPILER_RT_ENABLE_CET AND NOT COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
252+
message(FATAL_ERROR "Compiler used to build compiler-rt doesn't support CET!")
253+
endif()
254+
endif()
255+
247256
if(MSVC)
248257
# Override any existing /W flags with /W4. This is what LLVM does. Failing to
249258
# remove other /W[0-4] flags will result in a warning about overriding a

compiler-rt/cmake/config-ix.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ endif ()
6464
check_c_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG)
6565
check_c_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG)
6666
check_c_compiler_flag(-std=c11 COMPILER_RT_HAS_STD_C11_FLAG)
67+
check_c_compiler_flag(-fcf-protection=full COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
6768
check_cxx_compiler_flag(-fPIC COMPILER_RT_HAS_FPIC_FLAG)
6869
check_cxx_compiler_flag(-fPIE COMPILER_RT_HAS_FPIE_FLAG)
6970
check_cxx_compiler_flag(-fno-builtin COMPILER_RT_HAS_FNO_BUILTIN_FLAG)

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,10 @@ if (APPLE)
678678
else ()
679679
set(BUILTIN_CFLAGS "")
680680

681+
if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
682+
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full BUILTIN_CFLAGS)
683+
endif()
684+
681685
append_list_if(COMPILER_RT_HAS_FLOAT16 -DCOMPILER_RT_HAS_FLOAT16 BUILTIN_CFLAGS)
682686

683687
append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 BUILTIN_CFLAGS)

compiler-rt/lib/builtins/assembly.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
#ifndef COMPILERRT_ASSEMBLY_H
1515
#define COMPILERRT_ASSEMBLY_H
1616

17+
#if defined(__linux__) && defined(__CET__)
18+
#if __has_include(<cet.h>)
19+
#include <cet.h>
20+
#endif
21+
#endif
22+
1723
#if defined(__APPLE__) && defined(__aarch64__)
1824
#define SEPARATOR %%
1925
#else

compiler-rt/lib/crt/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAG
100100
append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
101101
append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
102102
append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
103+
if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
104+
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
105+
endif()
103106

104107
foreach(arch ${CRT_SUPPORTED_ARCH})
105108
add_compiler_rt_runtime(clang_rt.crtbegin

compiler-rt/test/builtins/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ foreach(arch ${BUILTIN_TEST_ARCH})
4949
string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
5050
endif()
5151

52+
if(COMPILER_RT_ENABLE_CET)
53+
if(NOT arch MATCHES "i?86|x86_64|AMD64")
54+
message(SEND_ERROR "${arch} does not support CET")
55+
endif()
56+
if(COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
57+
list(APPEND BUILTINS_TEST_TARGET_CFLAGS -fcf-protection=full)
58+
string(REPLACE ";" " " BUILTINS_TEST_TARGET_CFLAGS "${BUILTINS_TEST_TARGET_CFLAGS}")
59+
endif()
60+
endif()
61+
5262
# Compute builtins available in library and add them as lit features.
5363
if(APPLE)
5464
# TODO: Support other Apple platforms.

compiler-rt/test/crt/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ if (COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT)
2121
string(TOUPPER ${arch} ARCH_UPPER_CASE)
2222
set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
2323

24+
if (COMPILER_RT_ENABLE_CET)
25+
if (${arch} MATCHES "i386|x86_64")
26+
list(APPEND CRT_TEST_TARGET_CFLAGS -fcf-protection=full)
27+
string(REPLACE ";" " " CRT_TEST_TARGET_CFLAGS "${CRT_TEST_TARGET_CFLAGS}")
28+
else()
29+
message(FATAL_ERROR "The target arch ${arch} doesn't support CET")
30+
endif()
31+
endif()
2432
configure_lit_site_cfg(
2533
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
2634
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)

0 commit comments

Comments
 (0)