Skip to content

Commit 9d385d3

Browse files
authored
FEAT: Added Security Compiler Options in CMake (#316)
### Work Item / Issue Reference <!-- IMPORTANT: Please follow the PR template guidelines below. For mssql-python maintainers: Insert your ADO Work Item ID below (e.g. AB#37452) For external contributors: Insert Github Issue number below (e.g. #149) Only one reference is required - either GitHub issue OR ADO Work Item. --> <!-- mssql-python maintainers: ADO Work Item --> > [AB#40193](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/40193) ------------------------------------------------------------------- ### Summary <!-- Insert your summary of changes below. Minimum 10 characters required. --> This pull request improves the build configuration for the `mssql_python/pybind` project, focusing on security and diagnostics for MSVC builds. The main changes enhance security compliance and provide better visibility into the build process. Build diagnostics: * Enabled verbose output in CMake to show actual compiler and linker commands by setting `CMAKE_VERBOSE_MAKEFILE` to `ON`. Security compliance improvements (MSVC builds): * Added security-related compiler options: `/GS` for buffer security checks and `/guard:cf` for Control Flow Guard. * Added security-related linker options: `/DYNAMICBASE` for ASLR, `/NXCOMPAT` for DEP, and `/GUARD:CF` for Control Flow Guard. * Conditionally applied `/SAFESEH` linker option for 32-bit builds to enable Safe Structured Exception Handling; skipped for 64-bit builds. * Added status messages to indicate when security flags are applied and which options are enabled or skipped.
1 parent 00fbc3c commit 9d385d3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

mssql_python/pybind/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,37 @@ project(ddbc_bindings)
55
set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77

8+
# Enable verbose output to see actual compiler/linker commands
9+
set(CMAKE_VERBOSE_MAKEFILE ON CACHE BOOL "Verbose output" FORCE)
10+
811
if (MSVC)
12+
# Security compiler options for OneBranch compliance
13+
message(STATUS "Applying MSVC security compiler options for OneBranch compliance")
14+
15+
add_compile_options(
16+
/GS # Buffer security check - detects buffer overruns
17+
/guard:cf # Control Flow Guard - protects against control flow hijacking
18+
)
19+
20+
add_link_options(
21+
/DYNAMICBASE # ASLR - Address Space Layout Randomization
22+
/NXCOMPAT # DEP - Data Execution Prevention
23+
/GUARD:CF # Control Flow Guard (linker)
24+
)
25+
26+
# SAFESEH only for x86 (32-bit) builds
27+
if(CMAKE_SIZEOF_VOID_P EQUAL 4) # 32-bit
28+
message(STATUS "Applying /SAFESEH for 32-bit build")
29+
add_link_options(/SAFESEH) # Safe Structured Exception Handling
30+
else()
31+
message(STATUS "Skipping /SAFESEH (not applicable for 64-bit builds)")
32+
endif()
33+
934
# Enable PDB generation for all target types
1035
add_compile_options("$<$<CONFIG:Release>:/Zi>")
1136
add_link_options("$<$<CONFIG:Release>:/DEBUG /OPT:REF /OPT:ICF>")
37+
38+
message(STATUS "Security flags applied: /GS /guard:cf /DYNAMICBASE /NXCOMPAT /GUARD:CF")
1239
endif()
1340

1441
# Detect platform

0 commit comments

Comments
 (0)