Skip to content

Add UBSan flags to Clang builds for enhanced runtime safety - #16

Open
assisted-by-ai wants to merge 3 commits into
Whonix:masterfrom
assisted-by-ai:claude/review-compilation-security-c2DdU
Open

Add UBSan flags to Clang builds for enhanced runtime safety#16
assisted-by-ai wants to merge 3 commits into
Whonix:masterfrom
assisted-by-ai:claude/review-compilation-security-c2DdU

Conversation

@assisted-by-ai

Copy link
Copy Markdown
Contributor

Summary

This change enhances the security hardening flags used when compiling with Clang by adding undefined behavior sanitizer (UBSan) instrumentation to catch runtime errors at compile time.

Key Changes

  • Removed commented-out Clang detection code that was no longer needed
  • Added conditional UBSan flags specifically for Clang builds:
    • -fsanitize=undefined: Enables undefined behavior sanitization
    • -fsanitize-minimal-runtime: Uses minimal UBSan runtime overhead
    • -fno-sanitize-recover=all: Causes the program to halt on first UBSan error instead of continuing
  • These flags are now added to FORTIFY_CFLAGS when Clang is detected

Implementation Details

The new Clang-specific flags are conditionally added using ifneq to detect Clang in the compiler version string, ensuring these UBSan options (which may not be supported by GCC in the same way) only apply to Clang builds. This provides additional runtime safety checks for undefined behavior while maintaining compatibility with other compilers.

https://claude.ai/code/session_01E8Jai7NViAgjrdJ6Us48Qd

@ArrayBolt3

Copy link
Copy Markdown
Contributor

Integrated wiki changes. Will add flags where appropriate (including to kloak).

Comment thread Makefile
Comment on lines -45 to +55
#ifeq (,$(findstring clang,$(CC_VERSION))) # if clang
#WARN_CFLAGS += #
#endif

# IMPORTANT: Do NOT remove -ftrapv from the list of flags, it is used to allow
# signed integer arithmetic without explicit overflow checks.
FORTIFY_CFLAGS := -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 \
-fstack-clash-protection -fstack-protector-all \
-fno-delete-null-pointer-checks -fno-strict-overflow -fno-strict-aliasing \
-fstrict-flex-arrays=3 -ftrapv -ftrivial-auto-var-init=pattern

ifneq (,$(findstring clang,$(CC_VERSION))) # if clang
FORTIFY_CFLAGS += -fsanitize=undefined -fsanitize-minimal-runtime -fno-sanitize-recover=all
endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a good idea, but is rejected for now. As per the compiler_hardening wiki page:

Note: It is unclear whether this actually provides any meaningful level of hardening. Omitting these flags may be desirable even with Clang.

adrelanos pushed a commit that referenced this pull request May 11, 2026
Replace secrets: inherit with explicit single-secret map.
claude added 3 commits May 11, 2026 21:33
Replaces the empty commented-out Clang placeholder with working
-fsanitize=undefined -fsanitize-minimal-runtime -fno-sanitize-recover=all
flags, guarded by ifneq so they only apply when building with Clang.
Also fixes the original placeholder's ifeq/ifneq logic inversion.

https://claude.ai/code/session_01E8Jai7NViAgjrdJ6Us48Qd
Add -Wformat-truncation and -fno-strict-overflow to shared section.
Add -fzero-call-used-regs=all to both summary command blocks.
Add -Wformat-truncation and -fno-strict-overflow to both summary
command blocks.

https://claude.ai/code/session_01E8Jai7NViAgjrdJ6Us48Qd
@assisted-by-ai
assisted-by-ai force-pushed the claude/review-compilation-security-c2DdU branch from d20028f to bf07dcd Compare May 11, 2026 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants