You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enabling Control Flow Guard (CFG) by default for MSVC on Windows, with options to disable CFG. (#7988)
* Enabling Control Flow Guard by default for MSVC on Windows, with options to disable it.
* Fix configuration error for non-MSVC compilers.
* Reviewed and updated configuration for Python build and added comment for CFG.
# Default CFG to ON for MSVC, OFF for other compilers.
397
+
if (CMAKE_CXX_COMPILER_ID STREQUAL"MSVC")
398
+
option(Z3_ENABLE_CFG "Enable Control Flow Guard security checks"ON)
399
+
else()
400
+
option(Z3_ENABLE_CFG "Enable Control Flow Guard security checks"OFF)
401
+
endif()
402
+
403
+
if (Z3_ENABLE_CFG)
404
+
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL"MSVC")
405
+
message(FATAL_ERROR "Z3_ENABLE_CFG is only supported with MSVC compiler. "
406
+
"Current compiler: ${CMAKE_CXX_COMPILER_ID}. "
407
+
"You should remove Z3_ENABLE_CFG or set it to OFF or use MSVC to compile.")
408
+
endif()
409
+
410
+
# Check for incompatible options (handle both / and - forms for robustness)
411
+
string(REGEX MATCH "[-/]ZI" _has_ZI "${CMAKE_CXX_FLAGS}${CMAKE_CXX_FLAGS_DEBUG}${CMAKE_CXX_FLAGS_RELEASE}${CMAKE_CXX_FLAGS_RELWITHDEBINFO}${CMAKE_CXX_FLAGS_MINSIZEREL}")
412
+
string(REGEX MATCH "[-/]clr" _has_clr "${CMAKE_CXX_FLAGS}${CMAKE_CXX_FLAGS_DEBUG}${CMAKE_CXX_FLAGS_RELEASE}${CMAKE_CXX_FLAGS_RELWITHDEBINFO}${CMAKE_CXX_FLAGS_MINSIZEREL}")
413
+
414
+
if(_has_ZI)
415
+
message(WARNING "/guard:cf is incompatible with /ZI (Edit and Continue debug information). "
416
+
"Control Flow Guard will be disabled due to /ZI option.")
417
+
elseif(_has_clr)
418
+
message(WARNING "/guard:cf is incompatible with /clr (Common Language Runtime compilation). "
419
+
"Control Flow Guard will be disabled due to /clr option.")
390
420
else()
391
-
message(FATAL_ERROR "Can't enable control flow integrity for compiler \"${CMAKE_CXX_COMPILER_ID}\"."
392
-
"You should set Z3_ENABLE_CFI to OFF or use Clang or MSVC to compile.")
421
+
# Enable Control Flow Guard if no incompatible options are present
422
+
message(STATUS"Enabling Control Flow Guard (/guard:cf) and ASLR (/DYNAMICBASE) for MSVC")
Copy file name to clipboardExpand all lines: README-CMake.md
+33-1Lines changed: 33 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -365,6 +365,35 @@ build type when invoking ``cmake`` by passing ``-DCMAKE_BUILD_TYPE=<build_type>`
365
365
For multi-configuration generators (e.g. Visual Studio) you don't set the build type
366
366
when invoking CMake and instead set the build type within Visual Studio itself.
367
367
368
+
## MSVC Security Features
369
+
370
+
When building with Microsoft Visual C++ (MSVC), Z3 automatically enables several security features by default:
371
+
372
+
### Control Flow Guard (CFG)
373
+
-**CMake Option**: `Z3_ENABLE_CFG` - Defaults to `ON` for MSVC builds
374
+
-**Compiler flag**: `/guard:cf` - Automatically enabled when `Z3_ENABLE_CFG=ON`
375
+
-**Linker flag**: `/GUARD:CF` - Automatically enabled when `Z3_ENABLE_CFG=ON`
376
+
-**Purpose**: Control Flow Guard analyzes control flow for indirect call targets at compile time and inserts runtime verification code to detect attempts to compromise your code by redirecting control flow to attacker-controlled locations
377
+
-**Note**: Automatically enables `/DYNAMICBASE` as required by `/GUARD:CF`
378
+
379
+
### Address Space Layout Randomization (ASLR)
380
+
-**Linker flag**: `/DYNAMICBASE` - Enabled when Control Flow Guard is active
381
+
-**Purpose**: Randomizes memory layout to make exploitation more difficult
382
+
-**Note**: Required for Control Flow Guard to function properly
383
+
384
+
### Incompatibilities
385
+
Control Flow Guard is incompatible with:
386
+
-`/ZI` (Edit and Continue debug information format)
387
+
-`/clr` (Common Language Runtime compilation)
388
+
389
+
When these incompatible options are detected, Control Flow Guard will be automatically disabled with a warning message.
390
+
391
+
### Disabling Control Flow Guard
392
+
To disable Control Flow Guard, set the CMake option:
393
+
```bash
394
+
cmake -DZ3_ENABLE_CFG=OFF ../
395
+
```
396
+
368
397
## Useful options
369
398
370
399
The following useful options can be passed to CMake whilst configuring.
@@ -404,8 +433,11 @@ The following useful options can be passed to CMake whilst configuring.
404
433
*``Z3_ALWAYS_BUILD_DOCS`` - BOOL. If set to ``TRUE`` and ``Z3_BUILD_DOCUMENTATION`` is ``TRUE`` then documentation for API bindings will always be built.
405
434
Disabling this is useful for faster incremental builds. The documentation can be manually built by invoking the ``api_docs`` target.
406
435
*``Z3_LINK_TIME_OPTIMIZATION`` - BOOL. If set to ``TRUE`` link time optimization will be enabled.
407
-
*``Z3_ENABLE_CFI`` - BOOL. If set to ``TRUE`` will enable Control Flow Integrity security checks. This is only supported by MSVC and Clang and will
436
+
*``Z3_ENABLE_CFI`` - BOOL. If set to ``TRUE`` will enable Control Flow Integrity security checks. This is only supported by Clang and will
408
437
fail on other compilers. This requires Z3_LINK_TIME_OPTIMIZATION to also be enabled.
438
+
*``Z3_ENABLE_CFG`` - BOOL. If set to ``TRUE`` will enable Control Flow Guard security checks. This is only supported by MSVC and will
439
+
fail on other compilers. This does not require link time optimization. Control Flow Guard is enabled by default for MSVC builds.
440
+
Note: Control Flow Guard is incompatible with ``/ZI`` (Edit and Continue debug information) and ``/clr`` (Common Language Runtime compilation).
409
441
*``Z3_API_LOG_SYNC`` - BOOL. If set to ``TRUE`` will enable experimental API log sync feature.
410
442
*``WARNINGS_AS_ERRORS`` - STRING. If set to ``ON`` compiler warnings will be treated as errors. If set to ``OFF`` compiler warnings will not be treated as errors.
411
443
If set to ``SERIOUS_ONLY`` a subset of compiler warnings will be treated as errors.
Copy file name to clipboardExpand all lines: README.md
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,12 @@ cd build
49
49
nmake
50
50
```
51
51
52
-
Z3 uses C++20. The recommended version of Visual Studio is therefore VS2019 or later.
52
+
Z3 uses C++20. The recommended version of Visual Studio is therefore VS2019 or later.
53
+
54
+
**Security Features (MSVC)**: When building with Visual Studio/MSVC, a couple of security features are enabled by default for Z3:
55
+
- Control Flow Guard (`/guard:cf`) - enabled by default to detect attempts to compromise your code by preventing calls to locations other than function entry points, making it more difficult for attackers to execute arbitrary code through control flow redirection
56
+
- Address Space Layout Randomization (`/DYNAMICBASE`) - enabled by default for memory layout randomization, required by the `/GUARD:CF` linker option
57
+
- These can be disabled using `python scripts/mk_make.py --no-guardcf` (Python build) or `cmake -DZ3_ENABLE_CFG=OFF` (CMake build) if needed
"""Validate that Control Flow Guard is compatible with the final compiler options.
843
+
844
+
Args:
845
+
final_cxxflags: The complete CXXFLAGS string that will be used for compilation
846
+
"""
847
+
globalGUARD_CF
848
+
849
+
ifnotGUARD_CFornotIS_WINDOWS:
850
+
return
851
+
852
+
# Check the final compiler flags for incompatible options
853
+
zi_pattern=re.compile(r'[/-]ZI\b')
854
+
ifzi_pattern.search(final_cxxflags):
855
+
raiseMKException("Control Flow Guard (/guard:cf) is incompatible with Edit and Continue debug information (/ZI or -ZI). Disable Control Flow Guard with --no-guardcf.")
856
+
857
+
clr_pattern=re.compile(r'[/-]clr(?::|$|\s)')
858
+
ifclr_pattern.search(final_cxxflags):
859
+
raiseMKException("Control Flow Guard (/guard:cf) is incompatible with Common Language Runtime compilation (/clr or -clr). Disable Control Flow Guard with --no-guardcf when using managed code.")
860
+
861
+
# Note: /Zi or -Zi (Program Database debug info) is compatible with /guard:cf
862
+
ifis_verbose() andGUARD_CF:
863
+
print("Control Flow Guard enabled and compatible with current compiler options.")
864
+
829
865
830
866
# Return a list containing a file names included using '#include' in
0 commit comments