cmake: CMake variable handling is a black box
What changed, and why it matters
This commit fixes how the build system handles RUSTFLAGS, a set of flags passed to the Rust compiler. Previously, if the environment variable RUSTFLAGS was not set, the CMake variable could be left undefined, causing later commands that append text to it to behave unpredictably. The change ensures RUSTFLAGS is always initialized to an empty string if not provided, and uses a safer method to combine multiple flags. This is primarily a build robustness and reproducibility fix, not a direct security vulnerability in the device firmware itself.
Treat as a minor build-hygiene improvement. No urgent security action required. Reviewers may want to verify that RUSTFLAGS is now correctly formed in all build environments, especially when the environment variable is unset, to ensure reproducible builds are not affected.
Security signals we found
Build system variable initialization hardened
Potential for malformed compiler flags reduced
No direct memory safety, cryptographic, or authentication issue in diff
Evidence from the diff
The patch modifies CMakeLists.txt and src/CMakeLists.txt. It ensures RUSTFLAGS is always defined: if the environment variable RUSTFLAGS is set, it is quoted and assigned; otherwise it is set to an empty string. It then replaces string(APPEND RUSTFLAGS …) with string(JOIN ” ” RUSTFLAGS …), which avoids creating malformed flag strings when RUSTFLAGS is empty or already contains spaces. It also quotes the test flags assignment and removes redundant quoting in the add_custom_command environment variable. The risk is that an undefined RUSTFLAGS could lead to incorrect Rust compiler flags, potentially affecting build reproducibility or, in worst case, causing the compiler to misinterpret flags. There is no direct exploit or runtime vulnerability shown in the diff.
Changed components
CMake build configurationRust compilation flags handlingFirmware reproducible build pipelineInspect captured patch +7 / −5
diff --git a/CMakeLists.txt b/CMakeLists.txt
index bc172c3..0fa7cdc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -244,7 +244,9 @@ set(CMAKE_C_FLAGS_DEBUG "-Os -ggdb")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-Os -ggdb -DNDEBUG")
if(DEFINED ENV{RUSTFLAGS})
- set(RUSTFLAGS $ENV{RUSTFLAGS})
+ set(RUSTFLAGS "$ENV{RUSTFLAGS}")
+else()
+ set(RUSTFLAGS "")
endif()
#-----------------------------------------------------------------------------
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 1ea605d..254529c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -230,12 +230,12 @@ else()
set(RUST_PROFILE "release")
set(RUST_CARGO_FLAGS "--release")
# For binary reproducibility, strip path prefixes that can be different depending on environment (e.g. /home/<user>, etc.).
- string(APPEND RUSTFLAGS " --remap-path-prefix=${CMAKE_CURRENT_SOURCE_DIR}/rust=src --remap-path-prefix=$ENV{HOME}=")
+ string(JOIN " " RUSTFLAGS "${RUSTFLAGS}" "--remap-path-prefix=${CMAKE_CURRENT_SOURCE_DIR}/rust=src" "--remap-path-prefix=$ENV{HOME}=")
endif()
# Use libsecp256k1 that we link ourselves.
# See https://github.com/rust-bitcoin/rust-secp256k1/tree/7c8270a8506e31731e540fab7ee1abde1f48314e/secp256k1-sys#linking-to-external-symbols
-string(APPEND RUSTFLAGS " --cfg=rust_secp_no_symbol_renaming")
+string(JOIN " " RUSTFLAGS "${RUSTFLAGS}" "--cfg=rust_secp_no_symbol_renaming")
if(CMAKE_CROSSCOMPILING)
set(RUST_TARGET_ARCH thumbv7em-none-eabi)
@@ -284,7 +284,7 @@ add_custom_target(rust-cbindgen
# Test rust crates that contain business logic. Avoid testing crates that depend on hardware.
if(NOT CMAKE_CROSSCOMPILING)
- set(RUSTFLAGS_TESTS ${RUSTFLAGS} -L${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} -lsecp256k1 -lstatic=fatfs)
+ set(RUSTFLAGS_TESTS "${RUSTFLAGS} -L${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} -lsecp256k1 -lstatic=fatfs")
# Since we build with all features we need to use a separate build directory.
# Otherwise we invalidate the result from the normal compilation that uses a
@@ -295,7 +295,7 @@ if(NOT CMAKE_CROSSCOMPILING)
CMAKE_SYSROOT=${CMAKE_SYSROOT}
CMAKE_CURRENT_BINARY_DIR=${CMAKE_CURRENT_BINARY_DIR}
FIRMWARE_VERSION_SHORT=${FIRMWARE_VERSION}
- RUSTFLAGS="${RUSTFLAGS_TESTS}"
+ RUSTFLAGS=${RUSTFLAGS_TESTS}
# only one test thread because of unsafe concurrent access to `SafeData`, `mock_sd()` and `mock_memory()`. Using mutexes instead leads to mutex poisoning and very messy output in case of a unit test failure.
${CARGO}
test
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.