forked from osquery/osquery
-
Notifications
You must be signed in to change notification settings - Fork 1
418 lines (324 loc) · 14.3 KB
/
codeql_runner.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
name: "codeql_scan"
on: workflow_dispatch
env:
SUBMODULE_CACHE_VERSION: 2
jobs:
analyze_linux:
name: Analyze
runs-on: ubuntu-22.04
container:
image: osquery/builder18.04:a4961d234
options: --privileged --init -v /var/run/docker.sock:/var/run/docker.sock
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'cpp' ]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Create a build folder
id: build_paths
run: |
build_path=`realpath $GITHUB_WORKSPACE/../build`
ccache_path=`realpath $GITHUB_WORKSPACE/../ccache`
mkdir $build_path
mkdir $ccache_path
echo ::set-output name=BINARY::$build_path
echo ::set-output name=SOURCE::$GITHUB_WORKSPACE
echo ::set-output name=CCACHE::$ccache_path
- name: Select the build job count
shell: bash
id: build_job_count
run: |
echo ::set-output name=VALUE::$(($(nproc) + 1))
- name: Update the cache (ccache)
uses: actions/cache@v3
with:
path: ${{ steps.build_paths.outputs.CCACHE }}
key: |
ccache_ubuntu-18.04_Release_${{ github.sha }}
restore-keys: |
ccache_ubuntu-18.04_Release
- name: Update the cache (git submodules)
uses: actions/cache@v3
with:
path: ${{ steps.build_paths.outputs.SOURCE }}/.git/modules
key: |
gitmodules_ubuntu-18.04_${{env.SUBMODULE_CACHE_VERSION}}_${{ github.sha }}
restore-keys: |
gitmodules_ubuntu-18.04_${{env.SUBMODULE_CACHE_VERSION}}
- name: Update the git submodules
working-directory: ${{ steps.build_paths.outputs.SOURCE }}
run: |
git submodule sync --recursive
- name: Configure the project
working-directory: ${{ steps.build_paths.outputs.BINARY }}
env:
CCACHE_DIR: ${{ steps.build_paths.outputs.CCACHE }}
run: |
cmake -G "Unix Makefiles" \
-DOSQUERY_TOOLCHAIN_SYSROOT:PATH="/usr/local/osquery-toolchain" \
-DCMAKE_BUILD_TYPE:STRING="Release" \
-DOSQUERY_BUILD_TESTS=OFF \
"${{ steps.build_paths.outputs.SOURCE }}"
- name: Build third party libraries
env:
CCACHE_DIR: ${{ steps.build_paths.outputs.CCACHE }}
run: |
cmake --build ${{ steps.build_paths.outputs.BINARY }} -j ${{ steps.build_job_count.outputs.VALUE }} --target thirdparty_libraries
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
config-file: ./tools/ci/codeql/codeql.yml
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
- name: "Build code to analyze"
env:
CCACHE_DISABLE: "true"
run: |
cmake --build ${{ steps.build_paths.outputs.BINARY }} -j ${{ steps.build_job_count.outputs.VALUE }} --target osqueryd
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
upload: False
output: sarif-results
- name: Filter out third party headers
uses: advanced-security/filter-sarif@v1
with:
patterns: |
-**/libraries/cmake/source/**
-**/build/libs/**
-**/build/openssl/**
-**/build/installed_formulas/**
input: sarif-results/cpp.sarif
output: sarif-results/cpp.sarif
- name: Upload Sarif
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: sarif-results/cpp.sarif
analyze_windows:
name: Analyze
runs-on: windows-2019
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: [ 'cpp' ]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup the build paths
shell: powershell
id: build_paths
run: |
$rel_src_path = "w\src"
$rel_build_path = "w\build"
$rel_sccache_path = "w\sccache"
$rel_downloads_path = "w\downloads"
$rel_install_path = "w\install"
New-Item -ItemType Directory -Force -Path $rel_build_path
New-Item -ItemType Directory -Force -Path $rel_sccache_path
New-Item -ItemType Directory -Force -Path $rel_downloads_path
New-Item -ItemType Directory -Force -Path $rel_install_path
$base_dir = (Get-Item .).FullName
echo "SOURCE=$base_dir\$rel_src_path" >> $env:GITHUB_OUTPUT
echo "REL_SOURCE=$rel_src_path" >> $env:GITHUB_OUTPUT
echo "BINARY=$base_dir\$rel_build_path" >> $env:GITHUB_OUTPUT
echo "SCCACHE=$base_dir\$rel_sccache_path" >> $env:GITHUB_OUTPUT
echo "DOWNLOADS=$base_dir\$rel_downloads_path" >> $env:GITHUB_OUTPUT
echo "INSTALL=$base_dir\$rel_install_path" >> $env:GITHUB_OUTPUT
# NOTE: We will only use an already existing cache, and will not save it later,
# just to prevent trashing of cache. Also the cache is only partially updated,
# since we only build third party libraries with the cache on.
- name: Update the cache (ccache)
uses: actions/cache/restore@v3
with:
path: ${{ steps.build_paths.outputs.CCACHE }}
key: |
ccache_ubuntu-18.04_Release_${{ github.sha }}
restore-keys: |
ccache_ubuntu-18.04_Release
- name: Update the cache (git submodules)
uses: actions/cache@v3
with:
path: ${{ steps.build_paths.outputs.SOURCE }}/.git/modules
key: |
gitmodules_ubuntu-18.04_${{env.SUBMODULE_CACHE_VERSION}}_${{ github.sha }}
restore-keys: |
gitmodules_ubuntu-18.04_${{env.SUBMODULE_CACHE_VERSION}}
- name: Update the git submodules
working-directory: ${{ steps.build_paths.outputs.SOURCE }}
run: |
git submodule sync --recursive
- name: Initialize the Python 3 installation
uses: actions/setup-python@v2
with:
python-version: "3.x"
architecture: "x64"
# The runners will likely have both the x86 and x64 versions of
# Python but we always need the 64-bit one regardless of which
# architecture we are building for.
#
# The setup-python action should have put the right Python version
# in the PATH variable for us, so locate the installation directory
# so we can use it as a hint when we configure the project with
# CMake
- name: Locate the Python root directory
id: python_root_directory
shell: powershell
run: |
$python_executable_path = $(Get-Command python.exe | Select-Object -ExpandProperty Definition)
$python_root_directory = (Get-Item $python_executable_path).Directory.FullName
echo "VALUE=$python_root_directory" >> $env:GITHUB_OUTPUT
- name: Install Strawberry Perl
working-directory: ${{ steps.build_paths.outputs.SOURCE }}
shell: powershell
run: |
tools\ci\scripts\install_openssl_formula_dependencies.ps1
- name: Install CMake
working-directory: ${{ steps.build_paths.outputs.DOWNLOADS }}
shell: powershell
run: |
$long_cmake_ver = "3.21.4"
$short_cmake_ver = $($long_cmake_ver.split(".")[0] + "." + $long_cmake_ver.split(".")[1])
$folder_name = $("cmake-" + $long_cmake_ver + "-windows-x86_64")
$archive_name = $($folder_name + ".zip")
$url = $("https://cmake.org/files/v" + $short_cmake_ver + "/" + $archive_name)
(New-Object System.Net.WebClient).DownloadFile($url, $archive_name)
7z x -o${{ steps.build_paths.outputs.INSTALL }} -y $archive_name
echo "${{ steps.build_paths.outputs.INSTALL }}\$folder_name\bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Install sccache
working-directory: ${{ steps.build_paths.outputs.DOWNLOADS }}
shell: powershell
run: |
$long_version = "0.0.1"
$archive_name = $("sccache-" + $long_version + "-windows.7z")
$url = $("https://github.com/osquery/sccache/releases/download/" + $long_version + "-osquery/" + $archive_name)
(New-Object System.Net.WebClient).DownloadFile($url, $archive_name)
7z x -o${{ steps.build_paths.outputs.INSTALL }}\sccache -y $archive_name
echo "${{ steps.build_paths.outputs.INSTALL }}\sccache" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Install Ninja
working-directory: ${{ steps.build_paths.outputs.DOWNLOADS }}
shell: powershell
run: |
$long_version = "1.11.1"
$archive_name = "ninja-win.zip"
$url = $("https://github.com/ninja-build/ninja/releases/download/v" + $long_version + "/" + $archive_name)
(New-Object System.Net.WebClient).DownloadFile($url, $archive_name)
7z x -o${{ steps.build_paths.outputs.INSTALL }}\ninja -y $archive_name
echo "${{ steps.build_paths.outputs.INSTALL }}\ninja" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
- name: Configure the project
shell: cmd
working-directory: ${{ steps.build_paths.outputs.BINARY }}
env:
SCCACHE_DIR: ${{ steps.build_paths.outputs.SCCACHE }}
SCCACHE_CACHE_SIZE: "5G"
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" amd64
@echo on
cmake -G Ninja ^
-DCMAKE_C_COMPILER=cl.exe ^
-DCMAKE_CXX_COMPILER=cl.exe ^
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }} ^
-DOSQUERY_BUILD_TESTS=OFF ^
-DCMAKE_C_COMPILER_LAUNCHER="sccache.exe" ^
-DCMAKE_CXX_COMPILER_LAUNCHER="sccache.exe" ^
-DPython3_ROOT_DIR=${{ steps.python_root_directory.outputs.VALUE }} ^
${{ steps.build_paths.outputs.SOURCE }}
- name: Determine compiler version
id: determine_compiler_version
shell: pwsh
run: |
$compiler = (Get-Content "${{ steps.build_paths.outputs.BINARY }}\CMakeCache.txt" | Select-String -Pattern "CMAKE_CXX_COMPILER:STRING=(.*)").Matches[0].Groups[1].Value
echo "Compiler configured by CMake is $compiler"
if ($compiler -eq $null || $compiler -eq "") {
Write-Error "Could not find the configured compiler" -ErrorAction Stop
}
<#
We run the compiler help option; the compiler will write its version in stderr.
Due to how powershell works, we have to go through some hoops to extract the stderr to a variable
and also avoid it considering the command as failed because stderr contains messages.
The expression runs the compiler in a subshell, discards its stdout, then the stderr of the subshell is redirected
to the stdout of the parent shell.
#>
$ErrorActionPreference = 'Continue'
$erroutput = $( & "$compiler" /? 1>$null ) 2>&1
$ErrorActionPreference = 'Stop'
if ($erroutput -eq $null || $erroutput -eq "") {
Write-Error "Failed to run the compiler at $compiler" -ErrorAction Stop
}
$version = ($erroutput | Select-String -Pattern "Compiler Version (.*) for").Matches[0].Groups[1].Value.Replace(".", "")
if ($version -eq $null || $version -eq "") {
Write-Error "Failed to determine compiler version for $compiler and output $erroutput" -ErrorAction Stop
}
echo "Found compiler version $version"
echo "COMPILER_VERSION=$version" >> $env:GITHUB_OUTPUT
# NOTE: We will only use an already existing cache, and will not save it later,
# just to prevent trashing of cache. Also the cache is only partially updated,
# since we only build third party libraries with the cache on.
- name: Update the cache (sccache)
uses: actions/cache/restore@v3
with:
path: ${{ steps.build_paths.outputs.SCCACHE }}
key: |
sccache_${{ matrix.os }}_64_Release_${{ steps.determine_compiler_version.outputs.COMPILER_VERSION }}_${{ github.sha }}
restore-keys: |
sccache_${{ matrix.os }}_64_Release_${{ steps.determine_compiler_version.outputs.COMPILER_VERSION }}
- name: Build third party libraries
shell: cmd
working-directory: ${{ steps.build_paths.outputs.BINARY }}
env:
SCCACHE_DIR: ${{ steps.build_paths.outputs.SCCACHE }}
SCCACHE_CACHE_SIZE: "5G"
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" ${{ steps.vc_arch.outputs.VC_ARCH }}
@echo on
cmake --build . -j 3 --target thirdparty_libraries
if %errorlevel% neq 0 exit /b %errorlevel%
sccache.exe --stop-server
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
config-file: ./tools/ci/codeql/codeql.yml
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main
- name: "Build code to analyze"
shell: cmd
run: |
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat" ${{ steps.vc_arch.outputs.VC_ARCH }}
@echo on
cmake --build . -j 3 --target osqueryd
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
upload: False
output: sarif-results
- name: Filter out third party headers
uses: advanced-security/filter-sarif@v1
with:
patterns: |
-**/libraries/cmake/source/**
-**/build/libs/**
-**/build/openssl/**
-**/build/installed_formulas/**
input: sarif-results/cpp.sarif
output: sarif-results/cpp.sarif
- name: Upload Sarif
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: sarif-results/cpp.sarif