Skip to content

Commit 7732912

Browse files
authored
Merge pull request #1843 from shimat/fix/hardening-security-flags
Hardening security with compiler switches - /GS and /HIGHENTROPYVA
2 parents 3ea3668 + bc42e87 commit 7732912

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

.github/workflows/manylinux.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ jobs:
210210
with:
211211
path: |
212212
${{ github.workspace }}/opencv_artifacts_slim/include
213-
${{ github.workspace }}/opencv_artifacts_slim/lib
213+
${{ github.workspace }}/opencv_artifacts_slim/lib64
214214
key: opencv-${{ env.OPENCV_VERSION }}-manylinux2_28-slim-${{ hashFiles('cmake/opencv_build_options_slim.cmake') }}
215215

216216
- name: Configure OpenCV (slim)
@@ -238,7 +238,7 @@ jobs:
238238
with:
239239
path: |
240240
${{ github.workspace }}/opencv_artifacts_slim/include
241-
${{ github.workspace }}/opencv_artifacts_slim/lib
241+
${{ github.workspace }}/opencv_artifacts_slim/lib64
242242
key: opencv-${{ env.OPENCV_VERSION }}-manylinux2_28-slim-${{ hashFiles('cmake/opencv_build_options_slim.cmake') }}
243243

244244
- name: Build OpenCvSharpExtern (slim)
@@ -248,6 +248,7 @@ jobs:
248248
-S src \
249249
-B src/build-slim \
250250
-D CMAKE_BUILD_TYPE=Release \
251+
-D OpenCV_DIR=${GITHUB_WORKSPACE}/opencv_artifacts_slim/lib64/cmake/opencv4 \
251252
-D CMAKE_PREFIX_PATH=${GITHUB_WORKSPACE}/opencv_artifacts_slim \
252253
-D NO_CONTRIB=ON \
253254
-D NO_VIDEOIO=ON \

.github/workflows/windows.yml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
name: Windows Server 2025
22

33
on:
4+
workflow_dispatch:
5+
inputs:
6+
run_binskim:
7+
description: 'Run BinSkim security hardening check'
8+
type: boolean
9+
default: true
410
pull_request:
511
types: [synchronize, opened]
612
push:
@@ -173,6 +179,43 @@ jobs:
173179
cd ${env:GITHUB_WORKSPACE}\test\OpenCvSharp.Tests.Windows
174180
dotnet test -c Release -f net48 --runtime win-x64
175181
182+
- name: Verify security hardening with BinSkim
183+
if: inputs.run_binskim == true
184+
shell: powershell
185+
run: |
186+
# Official install: download .nupkg from NuGet, unzip, run exe from tools/
187+
$dest = "$env:TEMP\binskim"
188+
Invoke-WebRequest "https://www.nuget.org/api/v2/package/Microsoft.CodeAnalysis.BinSkim" -OutFile "$dest.zip"
189+
Expand-Archive "$dest.zip" $dest -Force
190+
$binskimExe = Get-ChildItem "$dest\tools\net*\win-x64\binskim.exe" | Select-Object -Last 1 -ExpandProperty FullName
191+
if (-not $binskimExe) { throw "binskim.exe not found" }
192+
193+
# Scan OpenCvSharpExtern.dll only.
194+
# opencv_videoio_ffmpeg*.dll is a pre-built binary from opencv_3rdparty
195+
# and cannot be recompiled with different flags (see opencv/3rdparty/ffmpeg/ffmpeg.cmake).
196+
$dll = "$env:GITHUB_WORKSPACE\src\build\OpenCvSharpExtern\Release\OpenCvSharpExtern.dll"
197+
if (-not (Test-Path $dll)) { throw "OpenCvSharpExtern.dll not found: $dll" }
198+
199+
$sarifFile = "$env:TEMP\binskim.sarif"
200+
& $binskimExe analyze $dll --output $sarifFile
201+
# Ignore BinSkim's own exit code (it returns 1 on PDB load failures etc.);
202+
# rely solely on SARIF result filtering below.
203+
$LASTEXITCODE = 0
204+
205+
# Ignore findings we cannot address:
206+
# ERR997 - PDB unavailable (Release builds have no PDB by default)
207+
# BA2007 - compiler warning level violations in vcpkg static libs (tesseract, tiff, etc.)
208+
if (-not (Test-Path $sarifFile)) { Write-Warning "No SARIF output produced."; exit 0 }
209+
$failures = (Get-Content $sarifFile | ConvertFrom-Json).runs[0].results | Where-Object {
210+
$ruleId = ($_.ruleId -split "/")[0]
211+
$_.level -eq "error" -and $ruleId -notmatch "^ERR997" -and $ruleId -ne "BA2007"
212+
}
213+
if ($failures) {
214+
$failures | ForEach-Object { Write-Host "FAIL $($_.ruleId): $(($_.message.text -split "`n")[0])" }
215+
exit 1
216+
}
217+
Write-Host "Security hardening verified: no actionable failures."
218+
176219
- name: Pack NuGet packages
177220
shell: powershell
178221
run: |
@@ -208,4 +251,4 @@ jobs:
208251
uses: actions/upload-artifact@v6
209252
with:
210253
name: packages_windows
211-
path: ${{ github.workspace }}\artifacts
254+
path: ${{ github.workspace }}\artifacts

cmake/opencv_build_options.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,12 @@ if(WIN32)
5858
set(BUILD_JPEG OFF CACHE BOOL "" FORCE)
5959
set(BUILD_PNG OFF CACHE BOOL "" FORCE)
6060
set(BUILD_WEBP OFF CACHE BOOL "" FORCE)
61+
62+
# Enable security hardening flags (/GS, /sdl, /guard:cf, /DYNAMICBASE, etc.)
63+
# to satisfy security audit requirements (issue #1841).
64+
# Note: opencv_videoio_ffmpeg*.dll is a pre-built binary downloaded from
65+
# https://github.com/opencv/opencv_3rdparty and is not affected by this flag.
66+
# Its security hardening status depends entirely on the OpenCV project's build pipeline.
67+
set(ENABLE_BUILD_HARDENING ON CACHE BOOL "" FORCE)
6168
endif()
6269

src/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ option(NO_HIGHGUI "Disable highgui bindings" OFF)
2525
option(NO_VIDEOIO "Disable videoio bindings" OFF)
2626
option(NO_INSTALL_TO_TEST "Skip copying built DLL to the test project directory (Windows only)" OFF)
2727

28+
# Enable MSVC security hardening flags (issue #1841)
29+
if(MSVC)
30+
add_compile_options(/guard:cf /GS)
31+
# /HIGHENTROPYVA: enable high-entropy 64-bit ASLR
32+
add_link_options(/guard:cf /DYNAMICBASE /HIGHENTROPYVA)
33+
endif()
34+
2835
# Convert options to compile definitions so C++ preprocessor can see them
2936
if(NO_CONTRIB)
3037
add_compile_definitions(NO_CONTRIB)

0 commit comments

Comments
 (0)