-
Notifications
You must be signed in to change notification settings - Fork 848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enables Conditional Compiler Protections #6743
base: main
Are you sure you want to change the base?
Changes from 2 commits
24288a4
24360a3
951bec6
f914add
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -274,8 +274,28 @@ if(CMAKE_C_COMPILER_ID MATCHES "GNU|AppleClang|Clang") | |||||||||||||||||||||||||||||||||
"The compiler ${CMAKE_C_COMPILER_ID} does not support -fvisibility=hidden" | ||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||
endif(NOT CC_SUPPORTS_VISIBILITY_HIDDEN) | ||||||||||||||||||||||||||||||||||
# security options go here - these are not supported by all compilers | ||||||||||||||||||||||||||||||||||
# first and foremost, do we have LTO? | ||||||||||||||||||||||||||||||||||
check_c_compiler_flag(-flto CC_SUPPORTS_LTO) | ||||||||||||||||||||||||||||||||||
# if we are building on a GLIBC environment, FORTIFY is available | ||||||||||||||||||||||||||||||||||
check_c_compiler_flag(-D_FORTIFY_SOURCE CC_SUPPORTS_FORTIFY) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this ever fail? Since you're just setting the You probably need to construct a small program that should generate warning, compile it, and set this flag if the program emits a warning. Something like this:
Suggested change
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if(CC_SUPPORTS_FORTIFY) | ||||||||||||||||||||||||||||||||||
add_compile_options(-D_FORTIFY_SOURCE=2) | ||||||||||||||||||||||||||||||||||
if(CC_SUPPORTS_LTO) | ||||||||||||||||||||||||||||||||||
add_compile_options(-flto) | ||||||||||||||||||||||||||||||||||
endif() | ||||||||||||||||||||||||||||||||||
else() | ||||||||||||||||||||||||||||||||||
message(STATUS "Compiler ${CMAKE_C_COMPILER_ID} does not support -D_FORTIFY_SOURCE=1") | ||||||||||||||||||||||||||||||||||
endif() | ||||||||||||||||||||||||||||||||||
# CFI is currently supported by clang | ||||||||||||||||||||||||||||||||||
check_c_compiler_flag(-CFI CC_SUPPORTS_CFI) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using CLang it does not have any
|
||||||||||||||||||||||||||||||||||
if(CC_SUPPORTS_CFI AND CC_SUPPORTS_LTO) | ||||||||||||||||||||||||||||||||||
add_compile_options(-flto -fsanitize=cfi) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need the flag mats@fury:~/lang/cc/examples$ clang -flto -fsanitize=cfi struct.c
clang: error: invalid argument '-fsanitize=cfi' only allowed with '-fvisibility='
mats@fury:~/lang/cc/examples$ clang -flto -fvisibility=hidden -fsanitize=cfi struct.c
mats@fury:~/lang/cc/examples$ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mkindahl which platform you compiled on? Fedora 39/gcc/x86_64 was not able to reproduce There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am using Ubuntu 22.04 LTS.
|
||||||||||||||||||||||||||||||||||
else() | ||||||||||||||||||||||||||||||||||
message(STATUS "Compiler ${CMAKE_C_COMPILER_ID} does not support CFI") | ||||||||||||||||||||||||||||||||||
endif() | ||||||||||||||||||||||||||||||||||
endif() | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
# On Windows, default to only include Release builds so MSBuild.exe 'just works' | ||||||||||||||||||||||||||||||||||
if(WIN32 AND NOT CMAKE_CONFIGURATION_TYPES) | ||||||||||||||||||||||||||||||||||
set(CMAKE_CONFIGURATION_TYPES | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can be pretty sure that if the
-fsanitize=cfi
works,-flto
will work as well, so no need to check this separately unless you want to provide a very specific error message saying what is wrong (and you do not have such a message here).