Skip to content

Commit f409444

Browse files
committed
Merge bitcoin/bitcoin#32071: build: Drop option to disable hardening.
77e553a build: refactor: hardening flags -> core_interface (David Gumberg) 00ba3ba build: Drop option for disabling hardening (David Gumberg) f57db75 build: Use `-z noseparate-code` on NetBSD < 11.0 (David Gumberg) Pull request description: Follow up to #32038 which dropped `NO_HARDEN` from depends builds, this PR drops the `ENABLE_HARDENING` build option since disabling hardening of binaries should not be a supported or maintained use case. With this change, hardening flags are always enabled. Individual hardening flags and options can still be disabled by appending flags, e.g.: ```bash cmake -B build \ -DAPPEND_CPPFLAGS='-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 -fno-stack-protector -fcf-protection=none -fno-stack-clash-protection' \ -DAPPEND_LDFLAGS='-Wl,-z,lazy -Wl,-z,norelro -Wl,-z,noseparate-code' ``` There is an issue with NetBSD 10.0's dynamic linker that makes one of the hardening linker flags, `-z separate-code`, [problematic](bitcoin/bitcoin#28724 (comment)), so this PR also introduces a check to prevent the use of this flag in NetBSD versions < 11.0, (where this issue is [fixed](NetBSD/src@acf7fb3)). The fix for this [might be backported](https://mail-index.netbsd.org/tech-userlevel/2023/01/05/msg013670.html) to NetBSD 10.0. I suggest reviewing the diff with whitespace changes hidden (`git diff -w` or using github's hide whitespace option) ACKs for top commit: hebasto: re-ACK 77e553a. laanwj: re-ACK 77e553a janb84: ACK [77e553a](bitcoin/bitcoin@77e553a) vasild: ACK 77e553a musaHaruna: tested ACK [77e553](bitcoin/bitcoin@77e553a) Tree-SHA512: b149fb0371d12312c140255bf674c2bdc9f5272a5750a5b9ec5f192323364bb2ea8e164af13b9ab981ab3aa7ceb91b7a64785081e7458470e81c2f5228abf7b1
2 parents d62c2d8 + 77e553a commit f409444

File tree

2 files changed

+57
-52
lines changed

2 files changed

+57
-52
lines changed

CMakeLists.txt

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ if(WITH_BDB)
128128
endif()
129129
cmake_dependent_option(BUILD_WALLET_TOOL "Build bitcoin-wallet tool." ${BUILD_TESTS} "ENABLE_WALLET" OFF)
130130

131-
option(ENABLE_HARDENING "Attempt to harden the resulting executables." ON)
132131
option(REDUCE_EXPORTS "Attempt to reduce exported symbols in the resulting executables." OFF)
133132
option(WERROR "Treat compiler warnings as errors." OFF)
134133
option(WITH_CCACHE "Attempt to use ccache for compiling." ON)
@@ -502,63 +501,71 @@ try_append_cxx_flags("-fmacro-prefix-map=A=B" TARGET core_interface SKIP_LINK
502501
# -fstack-reuse=none for all gcc builds. (Only gcc understands this flag).
503502
try_append_cxx_flags("-fstack-reuse=none" TARGET core_interface)
504503

505-
if(ENABLE_HARDENING)
506-
add_library(hardening_interface INTERFACE)
507-
target_link_libraries(core_interface INTERFACE hardening_interface)
508-
if(MSVC)
509-
try_append_linker_flag("/DYNAMICBASE" TARGET hardening_interface)
510-
try_append_linker_flag("/HIGHENTROPYVA" TARGET hardening_interface)
511-
try_append_linker_flag("/NXCOMPAT" TARGET hardening_interface)
512-
else()
504+
if(MSVC)
505+
try_append_linker_flag("/DYNAMICBASE" TARGET core_interface)
506+
try_append_linker_flag("/HIGHENTROPYVA" TARGET core_interface)
507+
try_append_linker_flag("/NXCOMPAT" TARGET core_interface)
508+
else()
513509

514-
# _FORTIFY_SOURCE requires that there is some level of optimization,
515-
# otherwise it does nothing and just creates a compiler warning.
516-
try_append_cxx_flags("-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3"
517-
RESULT_VAR cxx_supports_fortify_source
518-
SOURCE "int main() {
519-
# if !defined __OPTIMIZE__ || __OPTIMIZE__ <= 0
520-
#error
521-
#endif
522-
}"
510+
# _FORTIFY_SOURCE requires that there is some level of optimization,
511+
# otherwise it does nothing and just creates a compiler warning.
512+
try_append_cxx_flags("-U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3"
513+
RESULT_VAR cxx_supports_fortify_source
514+
SOURCE "int main() {
515+
# if !defined __OPTIMIZE__ || __OPTIMIZE__ <= 0
516+
#error
517+
#endif
518+
}"
519+
)
520+
if(cxx_supports_fortify_source)
521+
target_compile_options(core_interface INTERFACE
522+
-U_FORTIFY_SOURCE
523+
-D_FORTIFY_SOURCE=3
523524
)
524-
if(cxx_supports_fortify_source)
525-
target_compile_options(hardening_interface INTERFACE
526-
-U_FORTIFY_SOURCE
527-
-D_FORTIFY_SOURCE=3
528-
)
529-
endif()
530-
unset(cxx_supports_fortify_source)
525+
endif()
526+
unset(cxx_supports_fortify_source)
531527

532-
try_append_cxx_flags("-Wstack-protector" TARGET hardening_interface SKIP_LINK)
533-
try_append_cxx_flags("-fstack-protector-all" TARGET hardening_interface)
534-
try_append_cxx_flags("-fcf-protection=full" TARGET hardening_interface)
528+
try_append_cxx_flags("-Wstack-protector" TARGET core_interface SKIP_LINK)
529+
try_append_cxx_flags("-fstack-protector-all" TARGET core_interface)
530+
try_append_cxx_flags("-fcf-protection=full" TARGET core_interface)
535531

536-
if(MINGW)
537-
# stack-clash-protection is a no-op for Windows.
538-
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458 for more details.
539-
else()
540-
try_append_cxx_flags("-fstack-clash-protection" TARGET hardening_interface)
541-
endif()
542-
543-
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
544-
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
545-
try_append_cxx_flags("-mbranch-protection=bti" TARGET hardening_interface SKIP_LINK)
546-
else()
547-
try_append_cxx_flags("-mbranch-protection=standard" TARGET hardening_interface SKIP_LINK)
548-
endif()
549-
endif()
532+
if(MINGW)
533+
# stack-clash-protection is a no-op for Windows.
534+
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90458 for more details.
535+
else()
536+
try_append_cxx_flags("-fstack-clash-protection" TARGET core_interface)
537+
endif()
550538

551-
try_append_linker_flag("-Wl,--enable-reloc-section" TARGET hardening_interface)
552-
try_append_linker_flag("-Wl,--dynamicbase" TARGET hardening_interface)
553-
try_append_linker_flag("-Wl,--nxcompat" TARGET hardening_interface)
554-
try_append_linker_flag("-Wl,--high-entropy-va" TARGET hardening_interface)
555-
try_append_linker_flag("-Wl,-z,relro" TARGET hardening_interface)
556-
try_append_linker_flag("-Wl,-z,now" TARGET hardening_interface)
557-
try_append_linker_flag("-Wl,-z,separate-code" TARGET hardening_interface)
539+
if(CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64")
558540
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
559-
try_append_linker_flag("-Wl,-fixup_chains" TARGET hardening_interface)
541+
try_append_cxx_flags("-mbranch-protection=bti" TARGET core_interface SKIP_LINK)
542+
else()
543+
try_append_cxx_flags("-mbranch-protection=standard" TARGET core_interface SKIP_LINK)
560544
endif()
561545
endif()
546+
547+
try_append_linker_flag("-Wl,--enable-reloc-section" TARGET core_interface)
548+
try_append_linker_flag("-Wl,--dynamicbase" TARGET core_interface)
549+
try_append_linker_flag("-Wl,--nxcompat" TARGET core_interface)
550+
try_append_linker_flag("-Wl,--high-entropy-va" TARGET core_interface)
551+
try_append_linker_flag("-Wl,-z,relro" TARGET core_interface)
552+
try_append_linker_flag("-Wl,-z,now" TARGET core_interface)
553+
# TODO: This can be dropped once Bitcoin Core no longer supports
554+
# NetBSD 10.0 or if upstream fix is backported.
555+
# NetBSD's dynamic linker ld.elf_so < 11.0 supports exactly 2
556+
# `PT_LOAD` segments and binaries linked with `-z separate-code`
557+
# have 4 `PT_LOAD` segments.
558+
# Relevant discussions:
559+
# - https://github.com/bitcoin/bitcoin/pull/28724#issuecomment-2589347934
560+
# - https://mail-index.netbsd.org/tech-userlevel/2023/01/05/msg013666.html
561+
if(CMAKE_SYSTEM_NAME STREQUAL "NetBSD" AND CMAKE_SYSTEM_VERSION VERSION_LESS 11.0)
562+
try_append_linker_flag("-Wl,-z,noseparate-code" TARGET core_interface)
563+
else()
564+
try_append_linker_flag("-Wl,-z,separate-code" TARGET core_interface)
565+
endif()
566+
if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
567+
try_append_linker_flag("-Wl,-fixup_chains" TARGET core_interface)
568+
endif()
562569
endif()
563570

564571
if(REDUCE_EXPORTS)
@@ -703,7 +710,6 @@ message("Cross compiling ....................... ${cross_status}")
703710
message("C++ compiler .......................... ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}, ${CMAKE_CXX_COMPILER}")
704711
include(FlagsSummary)
705712
flags_summary()
706-
message("Attempt to harden executables ......... ${ENABLE_HARDENING}")
707713
message("Treat compiler warnings as errors ..... ${WERROR}")
708714
message("Use ccache for compiling .............. ${WITH_CCACHE}")
709715
message("\n")

CMakePresets.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@
7777
"BUILD_UTIL_CHAINSTATE": "ON",
7878
"BUILD_WALLET_TOOL": "ON",
7979
"ENABLE_EXTERNAL_SIGNER": "ON",
80-
"ENABLE_HARDENING": "ON",
8180
"ENABLE_WALLET": "ON",
8281
"WARN_INCOMPATIBLE_BDB": "OFF",
8382
"WITH_BDB": "ON",

0 commit comments

Comments
 (0)