Skip to content

Commit 5ae5eb1

Browse files
porting Clickhouse #37993, Strip less aggressively to make the embedded hash survive
1 parent 814cd0a commit 5ae5eb1

File tree

7 files changed

+20
-15
lines changed

7 files changed

+20
-15
lines changed

CMakeLists.txt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,18 @@ endif ()
234234
# Add a section with the hash of the compiled machine code for integrity checks.
235235
# Only for official builds, because adding a section can be time consuming (rewrite of several GB).
236236
# And cross compiled binaries are not supported (since you cannot execute clickhouse hash-binary)
237-
if (OBJCOPY_PATH AND YANDEX_OFFICIAL_BUILD AND (NOT CMAKE_TOOLCHAIN_FILE))
238-
set (USE_BINARY_HASH 1)
237+
if (YANDEX_OFFICIAL_BUILD AND (NOT CMAKE_TOOLCHAIN_FILE OR CMAKE_TOOLCHAIN_FILE MATCHES "linux/toolchain-x86_64.cmake$"))
238+
message(STATUS "Official build: A checksum hash will be added to the clickhouse executable")
239+
set (USE_BINARY_HASH 1 CACHE STRING "Calculate binary hash and store it in the separate section")
240+
else ()
241+
message(STATUS "No official build: A checksum hash will not be added to the clickhouse executable")
239242
endif ()
240243

241-
# Allows to build stripped binary in a separate directory
242-
if (OBJCOPY_PATH AND READELF_PATH)
243-
option(INSTALL_STRIPPED_BINARIES "Build stripped binaries with debug info in separate directory" OFF)
244-
if (INSTALL_STRIPPED_BINARIES)
245-
set(STRIPPED_BINARIES_OUTPUT "stripped" CACHE STRING "A separate directory for stripped information")
246-
endif()
244+
# Optionally split binaries and debug symbols.
245+
option(INSTALL_STRIPPED_BINARIES "Split binaries and debug symbols" OFF)
246+
if (INSTALL_STRIPPED_BINARIES)
247+
message(STATUS "Will split binaries and debug symbols")
248+
set(STRIPPED_BINARIES_OUTPUT "stripped" CACHE STRING "A separate directory for stripped information")
247249
endif()
248250

249251
cmake_host_system_information(RESULT AVAILABLE_PHYSICAL_MEMORY QUERY AVAILABLE_PHYSICAL_MEMORY) # Not available under freebsd

cmake/strip_binary.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ macro(proton_strip_binary)
1919
COMMAND mkdir -p "${STRIP_DESTINATION_DIR}/lib/debug/bin"
2020
COMMAND mkdir -p "${STRIP_DESTINATION_DIR}/bin"
2121
COMMAND cp "${STRIP_BINARY_PATH}" "${STRIP_DESTINATION_DIR}/bin/${STRIP_TARGET}"
22+
# Splits debug symbols into separate file, leaves the binary untouched:
2223
COMMAND "${OBJCOPY_PATH}" --only-keep-debug --compress-debug-sections "${STRIP_DESTINATION_DIR}/bin/${STRIP_TARGET}" "${STRIP_DESTINATION_DIR}/lib/debug/bin/${STRIP_TARGET}.debug"
2324
COMMAND chmod 0644 "${STRIP_DESTINATION_DIR}/lib/debug/bin/${STRIP_TARGET}.debug"
24-
COMMAND "${STRIP_PATH}" --remove-section=.comment --remove-section=.note "${STRIP_DESTINATION_DIR}/bin/${STRIP_TARGET}"
25+
# Strips binary, sections '.note' & '.comment' are removed in line with Debian's stripping policy: www.debian.org/doc/debian-policy/ch-files.html, section '.clickhouse.hash' is needed for integrity check:
26+
COMMAND "${STRIP_PATH}" --remove-section=.comment --remove-section=.note --keep-section=.clickhouse.hash "${STRIP_DESTINATION_DIR}/bin/${STRIP_TARGET}"
27+
# Associate stripped binary with debug symbols:
2528
COMMAND "${OBJCOPY_PATH}" --add-gnu-debuglink "${STRIP_DESTINATION_DIR}/lib/debug/bin/${STRIP_TARGET}.debug" "${STRIP_DESTINATION_DIR}/bin/${STRIP_TARGET}"
2629
COMMENT "Stripping proton binary" VERBATIM
2730
)

programs/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ else ()
380380
endif()
381381

382382
if (USE_BINARY_HASH)
383-
add_custom_command(TARGET proton POST_BUILD COMMAND ./proton hash-binary > hash && ${OBJCOPY_PATH} --add-section .note.proton .hash=hash proton COMMENT "Adding .note.proton.hash to proton" VERBATIM)
383+
add_custom_command(TARGET clickhouse POST_BUILD COMMAND ./clickhouse hash-binary > hash && ${OBJCOPY_PATH} --add-section .clickhouse.hash=hash clickhouse COMMENT "Adding section '.clickhouse.hash' to clickhouse binary" VERBATIM)
384384
endif()
385385

386386
if (INSTALL_STRIPPED_BINARIES)

programs/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int mainRestart(int argc, char ** argv);
7070
int mainHashBinary(int, char **)
7171
{
7272
/// Intentionally without newline. So you can run:
73-
/// objcopy --add-section .note.ClickHouse.hash=<(./clickhouse hash-binary) clickhouse
73+
/// objcopy --add-section .clickhouse.hash=<(./clickhouse hash-binary) clickhouse
7474
std::cout << getHashOfLoadedBinaryHex();
7575
return 0;
7676
}

src/Common/Elf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ String Elf::getBuildID(const char * nhdr_pos, size_t size)
176176
#endif // OS_SUNOS
177177

178178

179-
String Elf::getBinaryHash() const
179+
String Elf::getStoredBinaryHash() const
180180
{
181-
if (auto section = findSectionByName(".note.ClickHouse.hash"))
181+
if (auto section = findSectionByName(".clickhouse.hash"))
182182
return {section->begin(), section->end()};
183183
else
184184
return {};

src/Common/Elf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class Elf final
6161
static String getBuildID(const char * nhdr_pos, size_t size);
6262

6363
/// Hash of the binary for integrity checks.
64-
String getBinaryHash() const;
64+
String getStoredBinaryHash() const;
6565

6666
private:
6767
MMapReadBufferFromFile in;

src/Daemon/BaseDaemon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ void BaseDaemon::initializeTerminationAndSignalProcessing()
891891
std::string executable_path = getExecutablePath();
892892

893893
if (!executable_path.empty())
894-
stored_binary_hash = DB::Elf(executable_path).getBinaryHash();
894+
stored_binary_hash = DB::Elf(executable_path).getStoredBinaryHash();
895895
#endif
896896
}
897897

0 commit comments

Comments
 (0)