Skip to content

Commit d853571

Browse files
jacob-ronstadthouha2Elaine HouhaNateD-MSFT
authored
RI Development to main (#175)
* Github actions updates (#139) * Update build-codeql.yaml test codeql action Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml manual download Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml add back yest of build step Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml try all jobs Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml test all jobs Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml dvl tests Signed-off-by: Jacob Ronstadt <[email protected]> * Add files via upload Signed-off-by: Jacob Ronstadt <[email protected]> * Create readme Signed-off-by: Jacob Ronstadt <[email protected]> * restructure * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * add test projects * Update dvl_tests.ps1 Signed-off-by: Jacob Ronstadt <[email protected]> * Update dvl_tests.ps1 Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update dvl_tests.ps1 Signed-off-by: Jacob Ronstadt <[email protected]> * Update dvl_tests.ps1 Signed-off-by: Jacob Ronstadt <[email protected]> * Update dvl_tests.ps1 Signed-off-by: Jacob Ronstadt <[email protected]> * update build_create_analyze_test.py to fail when different results are found * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * change when results get uploaded * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * fix exit code * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * update to use driver_snippet.c instead of other driver projects * add template path variable * fix path * fix path * test * fix template path * test * update template project to have arm configuration * fix msbuild command * fix driver_snippet.c * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * print results location * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * fix sarif copy and compare sarif results * add expected sarif results * Delete src/drivers/test/dvl_tests/readme Signed-off-by: Jacob Ronstadt <[email protected]> --------- Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml to allow publish step to fail for outside PRs Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml (#141) Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml (#142) * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * test secret condition without deleting secret Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml change condition back Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml test env Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml test env Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml fail test-codeql-latest-vs-current if prev job fails Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> --------- Signed-off-by: Jacob Ronstadt <[email protected]> * Initial Batch of Windows Filtering Platform Queries for Static Contracts (#110) * init commit * moved files around added the snippets * I think this fixes the build * update the WFP library to be more robust. Driver_snippets still need some edits * Update the ID path * fixes build issue * attempt 2 * forgot () added them * fix spelling * updated ID path and hopefully fixed the last compile error * I had another typo, this should fix it * changes the id property * lowercase letters * synced the forked made name change to one query * other name edit --------- Co-authored-by: Elaine Houha <[email protected]> * codeql port of C28648. TODO needs tests * Revert "codeql port of C28648. TODO needs tests" This reverts commit c282407. * OperandAssignment: CodeQL Port of C28129 (#121) * WIP C28129 * C28129 * add sarif file and update diff file * update id and move out of experimental * add query to ported ca checks suite --------- Signed-off-by: Jacob Ronstadt <[email protected]> * IoInitializeTimerCall: CodeQL port of C28133 (#123) * C28133 * add query to ported ca checks suite --------- Signed-off-by: Jacob Ronstadt <[email protected]> * IrqlIllegalValue: CodeQL port of c28151 (#144) * codeql port of c28151 * Update IrqlIllegalValue.ql Signed-off-by: Jacob Ronstadt <[email protected]> --------- Signed-off-by: Jacob Ronstadt <[email protected]> * NullPointerCharacterAssignment: CodeQL port of C28730 (#146) codeql port of C28730 * FloatSafeExit: CodeQL port of C28162 (#147) Port of C28162 * FloatUnsafeExit: CodeQL port of C28161 (#148) * WIP C28161 * port of C28161 * move from kmdf to wdm * remove old files * PointerVariableSize: CodeQL port of C28132 (#143) * port of C28132 * update to remove false positives * NtstatusExplicitCast3: CodeQL port of C28716 (#151) * port of C28716 * add other bool types * NtstatusExplicitCast2: CodeQL port of C29715 (#150) * port of C29715 * add other bool types * NtstatusExplicitCast: CodeQL port of C28714 (#149) * port of C28714 * improve query * add other bool types * update name * CurrentFunctionTypeNotCorrect: CodeQL port of C28101 (#124) * C28101 * add query to ported ca checks suite * specifically check for driver_initialize --------- Signed-off-by: Jacob Ronstadt <[email protected]> * InvalidFunctionPointerAnnotation: CodeQL port of C28165 (#145) * codeql port of C28165 * update query to also work with typedef types and _use_decl_annotations_ * update sarif * FloatHardwareStateProtection: CodeQL port of c28110 (#130) * WIP FloatHardwareStateProtection * FloatHardwareStateProtection.ql TODO test on samples * add more control flow * add query to ported ca checks suite * move from kmdf to wdm. add sarif file. * fix output message * resolve todos * DriverEntrySaveBuffer: Port of c28131 (#122) * C28131 * updates to ql * update id and move out of experimental * add query to ported ca checks suite * update to also check for local pointers to structs --------- Signed-off-by: Jacob Ronstadt <[email protected]> * IRPStackEntryCopy: CodeQL port of c28114 (#120) * port of C28114, Copying a whole IRP stack entry * update unit tests * fix ql name * update id and move out of experimental * add query to ported ca checks suite * fix sarif file --------- Signed-off-by: Jacob Ronstadt <[email protected]> * update ported_driver_ca_checks.qls with new queries * fix typo * updates to test script * Driver isolation queries (#152) * WIP driver isolation * WIP driver isolation. add zw* using non-null rootdirectory * add flow config for OK open reg key functions? * query finds Zw* isolation function use where OBJECT_ATTRIBUTES has non-null RootDirectory AND the RootDirectory didn't come from allowed function * WIP null rootdir * WIP null rootdir * WIP split into read/write accesses * Preliminary query * fix zwRead and zwWrite predicates * fix null root dir * bug fixes and add test cases * query improvements and tests * flow update * add tests and small query improvements * add exceptions * add extra barriers to flow * refactor query and fix barriers in dataflow * update qhelp files and queries and tests * fix comment * query improvements * WIP query updates * bug fixes and improvements * fix exception for SERIALCOMM to also match as prefix * allow handles that are relative to handles obtained from approved ddi * add recursive check for allowed handle source * change @kind from path-problem for debugging to problem * update select message * update zw violation 2 * small fix * add allowed handle source to driverisolation.qll and zwviolation2. update zwViolation1 to use lib * improve query * query updates * update query to remove potential true results * WIP wdf isolation * move isolation queries to experimental * update config file * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * remove wdf WIP query for dev * update version of qlpack * update driver_snippet --------- Signed-off-by: Jacob Ronstadt <[email protected]> * update config file to include other isolation queries * update build script * update sarif files for isolation queries * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * github action updates to get latest codeql version (#154) * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> --------- Signed-off-by: Jacob Ronstadt <[email protected]> * minor updates * fix compare issue with backslashes in latest codeql version * Irql.qll updates to expand ability to detect function IRQL requirements (#153) * updates to Irql.qll to add conditional macros within a _When_ macro * Update Irql.qll Signed-off-by: Jacob Ronstadt <[email protected]> * Update Irql.qll Signed-off-by: Jacob Ronstadt <[email protected]> * update IRQL library so that IRQL requirements for functions that are not annotated can be tracked by looking at the the functions implicit role type based on how it's used * Update Irql.qll to remove test code Signed-off-by: Jacob Ronstadt <[email protected]> * irql updates * bug fix in test script * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * bug fix * bug fix --------- Signed-off-by: Jacob Ronstadt <[email protected]> * update uses of dataflow to use latest library (#155) * update uses of dataflow to use latest library * fix dataflow use * bug fix * Update build-codeql.yaml Signed-off-by: Jacob Ronstadt <[email protected]> * update pack versions to fix false positive --------- Signed-off-by: Jacob Ronstadt <[email protected]> * StaticInitializer: CodeQL port of C28651 (#156) * WIP C28651 * Codeql port of C28651 * remove TODOs * Add APIs to ExtendedDeprecatedApis for Codeql port of c28727 (#163) add apis from C28727 * Driver apps framework (#165) * framework for writing queries for apps that interact with drivers * remove extra print * IrqlFunctionNotAnnotated: codeql port of c28167 (#160) codeql port of c28167 * IrqlLoweredImproperly: Codeql port of c28141 (#157) * CodeQL port of C28141 * update sarif file * IrqlFloatStateMismatch: CodeQL port of C28111 (#161) * WIP port of C28111 * add sarif diff file * Codeql port of C28111 * updates from review * IrqlCancelRoutine: CodeQL port of c28144 (#162) * CodeQL port of C28144 * updates from review * IrqlAnnotationIssue: CodeQL port of c28153 (#158) * CodeQL port of C28153 * Remove TODO comment * updates from review * IrqlInconsistentWithRequired: CodeQL port of C28166 (#159) * CodeQL port of C28156 * updates from review * AnnotationSyntax: CodeQL port of c28266 (#164) * WIP c28266 * CodeQL port of C28266 * Remove commented-out code in AnnotationSyntax.ql * updates from review * CodeQL port of C28650 * add build_database_only option to build script * update gitignore * ImportantFunctionCallOptimizedOut: port of c28625 (#167) * Codeql port of C28625 * Update src/drivers/general/queries/ImportantFunctionCallOptimizedOut/driver_snippet.c Co-authored-by: NateD-MSFT <[email protected]> Signed-off-by: Jacob Ronstadt <[email protected]> --------- Signed-off-by: Jacob Ronstadt <[email protected]> Co-authored-by: NateD-MSFT <[email protected]> * InvalidFunctionClassTypedef: Codeql port of c28268 (#170) * CodeQL port of C28268 * Add example to InvalidFunctionClassTypedef.qhelp * StrictTypeMatch: Codeql port of c28139 (#169) * WIP port of C28139 * CodeQL port of C28139 * Update src/drivers/general/queries/StrictTypeMatch/driver_snippet.c Co-authored-by: NateD-MSFT <[email protected]> Signed-off-by: Jacob Ronstadt <[email protected]> * Fix parameter order in code samples --------- Signed-off-by: Jacob Ronstadt <[email protected]> Co-authored-by: NateD-MSFT <[email protected]> * MultipleFunctionClassAnnotations: Port of C28177 (#168) * Port of C28177 * Update src/drivers/general/queries/MultipleFunctionClassAnnotations/driver_snippet.c Co-authored-by: NateD-MSFT <[email protected]> Signed-off-by: Jacob Ronstadt <[email protected]> * Update example description in qhelp file --------- Signed-off-by: Jacob Ronstadt <[email protected]> Co-authored-by: NateD-MSFT <[email protected]> * UnsafeCallInGlobalinit: CodeQL port of C28637 (#166) * CodeQL port of C28637 * move query to app folder * update query --------- Signed-off-by: Jacob Ronstadt <[email protected]> * Update opaqueid in ImproperNotOperatorOnZero.ql Signed-off-by: Jacob Ronstadt <[email protected]> * update build script and repo structure * ported_driver_ca_checks.qls to use tag ca_ported and update ported ca rule queries with tag * update dvl_tests.ps1 * update qlpack version and fix path error * bug fix in AnnotationSyntax.ql * update DriverEntrySaveBuffer.ql to only look at registrypath and not driverobject since that's what C28131 did * remove false positives * remove false positives * remove false positives * Add possible false positive note to DriverEntrySaveBuffer.qhelp * remove non-SAL parameters due to false positives * update message * update test results to match query update * fix query id * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * Query suite updates (#172) * Suite updates to add additional queries * update windows_driver_mustfix.qls * update readme * qlpack version update * update query packs * update github action * update query suites with missing query * update version * sort query suites * add missing queries * Fix opaqueid fields. For some reason opaque-id causes issues with sarif reader. * fix changed file * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * Update codeql-config.yml Signed-off-by: Jacob Ronstadt <[email protected]> * Must run query update (#173) * Revert must-fix to current requirements. Make recommended contain what next mustfix will. Make mustrun suite. * update version * remove old suite files * update dependency versions. update dvl_tests.ps1 for changed mustfix suite name * fix version number in suites * update qlpack version * Jacob ronstadt/mustrun (#174) * Revert must-fix to current requirements. Make recommended contain what next mustfix will. Make mustrun suite. * update version * remove old suite files * Updating documentation for new version * update dependency versions. update dvl_tests.ps1 for changed mustfix suite name * fix version number in suites * update qlpack version * update readme * update qlpack dependency versions --------- Signed-off-by: Jacob Ronstadt <[email protected]> * Update README.md Signed-off-by: Jacob Ronstadt <[email protected]> * Actions azure file storage fix (#176) * fixing azure file upload * testing * fix pack version check * only compare detailed results * bug fix * use more threads with codeql * save latest version if the results are the same * testing * add manual publish workflow * Fix query health action (#177) * suppress breaking change warning. NA because we don't get the output from set-azstoragefilecontent * move float safe/unsafe queries to kmdf folder and fix output strings * move float safe/unsafe queries to kmdf folder and fix output strings * Improve recommendations in ExtendedDeprecatedApis for strlen (#179) * Improve replacement recommendations in ExtendedDeprecatedApis * Bump query version --------- Signed-off-by: Jacob Ronstadt <[email protected]> Co-authored-by: Elaine Houha <[email protected]> Co-authored-by: Elaine Houha <[email protected]> Co-authored-by: NateD-MSFT <[email protected]>
1 parent 44a75ac commit d853571

File tree

303 files changed

+23716
-2688
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

303 files changed

+23716
-2688
lines changed

.github/workflows/build-codeql.yaml

Lines changed: 268 additions & 50 deletions
Large diffs are not rendered by default.

.github/workflows/publish.yml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
name: Build and Publish Windows CodeQL queries
3+
on:
4+
workflow_dispatch:
5+
input:
6+
version:
7+
description: 'CodeQL version to use'
8+
required: true
9+
type: string
10+
jobs:
11+
publish:
12+
runs-on: windows-latest
13+
permissions:
14+
contents: read
15+
packages: write
16+
steps:
17+
- name: Enable long git paths
18+
shell: cmd
19+
run: git config --global core.longpaths true
20+
21+
- name: Clone self (windows-driver-developer-supplemental-tools)
22+
uses: actions/checkout@v4
23+
with:
24+
path: .
25+
fetch-depth: 0
26+
27+
- name: CodeQL Download
28+
run:
29+
Invoke-WebRequest -Uri "https://github.com/github/codeql-cli-binaries/releases/download/v${{ github.event.inputs.version }}/codeql-win64.zip" -OutFile codeql-win64.zip;
30+
Expand-Archive -Path codeql-win64.zip -DestinationPath .\codeql-zip -Force;
31+
Move-Item -Path .\codeql-zip\codeql -Destination .\codeql-cli\
32+
33+
- name: Install CodeQL pack dependencies
34+
shell: cmd
35+
run: |
36+
pushd .\src
37+
..\codeql-cli\codeql.cmd pack install
38+
popd
39+
- name: Publish New CodeQL Pack
40+
shell: pwsh
41+
env:
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
run:
44+
.\codeql-cli\codeql.cmd pack publish ./src;

.gitignore

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# query compilation caches
22
**/*.cache
33

4+
**/*.codeql/
5+
46
# IDE folders
57
.vscode/*
68
**/.vs/*
@@ -13,6 +15,11 @@ src/drivers/test/**/Debug/*
1315
src/drivers/test/TestDB/*
1416
src/drivers/test/working/*
1517
src/drivers/test/AnalysisFiles/*
16-
18+
AnalysisFiles/*
19+
TestDB/*
20+
working/*
1721
#excel files
18-
**/*.xlsx
22+
**/*.xlsx
23+
24+
# Log files
25+
**/*.log

README.md

Lines changed: 71 additions & 85 deletions
Large diffs are not rendered by default.

config/codeql-config.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: "CodeQL config"
2+
disable-default-queries: true
3+
4+
packs:
5+
- microsoft/[email protected]:codeql-suites/cpp-code-scanning.qls
6+
- microsoft/[email protected]+5:windows-driver-suites/recommended.qls
7+
- microsoft/[email protected]+5:drivers\general\queries\experimental\DriverIsolationZwViolation1\DriverIsolationZwViolation1.ql
8+
- microsoft/[email protected]+5:drivers\general\queries\experimental\DriverIsolationZwViolation2\DriverIsolationZwViolation2.ql
9+
- microsoft/[email protected]+5:drivers\general\queries\experimental\DriverIsolationRtlViolation\DriverIsolationRtlViolation.ql

src/codeql-pack.lock.yml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22
lockVersion: 1.0.0
33
dependencies:
44
codeql/cpp-all:
5-
version: 0.12.1
5+
version: 4.2.0
66
codeql/dataflow:
7-
version: 0.1.4
7+
version: 2.0.5
8+
codeql/mad:
9+
version: 1.0.21
810
codeql/rangeanalysis:
9-
version: 0.0.3
11+
version: 1.0.21
1012
codeql/ssa:
11-
version: 0.2.4
13+
version: 1.1.0
14+
codeql/suite-helpers:
15+
version: 1.0.21
1216
codeql/tutorial:
13-
version: 0.2.4
17+
version: 1.0.21
18+
codeql/typeflow:
19+
version: 1.0.21
1420
codeql/typetracking:
15-
version: 0.2.4
21+
version: 2.0.5
1622
codeql/util:
17-
version: 0.2.4
23+
version: 2.0.8
24+
codeql/xml:
25+
version: 1.0.21
26+
microsoft/cpp-queries:
27+
version: 0.0.4
1828
compiled: false
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
2+
<qhelp>
3+
<overview>
4+
<p>
5+
When using a DLL, it is frequently the case that any static construtors are called from DllMain. There are a number of constraints that apply to calling other functions from DllMain. In particular, it is possible to create memory leaks if the DLL is loaded and unloaded dynamically. SysAllocString is an example of a function that, in this case, could cause a memory leak.
6+
</p>
7+
</overview>
8+
<recommendation>
9+
<p>
10+
The ideal DllMain would be just an empty stub. However, given the complexity of many applications, this is generally too restrictive. A good rule of thumb for DllMain is to postpone as much initialization as possible. Lazy initialization increases robustness of the application because this initialization is not performed while the loader lock is held. Also, lazy initialization enables you to safely use much more of the Windows API.
11+
</p>
12+
</recommendation>
13+
<example>
14+
<p>
15+
DLLMain function
16+
</p>
17+
<sample language="c"> <![CDATA[
18+
BOOL WINAPI DllMain(
19+
HINSTANCE hinstDLL, // handle to DLL module
20+
DWORD fdwReason, // reason for calling function
21+
LPVOID lpvReserved ) // reserved
22+
{
23+
// Perform actions based on the reason for calling.
24+
switch( fdwReason )
25+
{
26+
case DLL_PROCESS_ATTACH:
27+
// Initialize once for each new process.
28+
// Return FALSE to fail DLL load.
29+
break;
30+
31+
case DLL_THREAD_ATTACH:
32+
// Do thread-specific initialization.
33+
break;
34+
35+
case DLL_THREAD_DETACH:
36+
// Do thread-specific cleanup.
37+
break;
38+
39+
case DLL_PROCESS_DETACH:
40+
41+
if (lpvReserved != nullptr)
42+
{
43+
break; // do not do cleanup if process termination scenario
44+
}
45+
46+
// Perform any necessary cleanup.
47+
break;
48+
}
49+
return TRUE; // Successful DLL_PROCESS_ATTACH.
50+
}
51+
}]]>
52+
53+
</example>
54+
<semmleNotes>
55+
<p>
56+
57+
</p>
58+
</semmleNotes>
59+
<references>
60+
<li>
61+
<a href="https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/28637-calling-function-in-a-global-initializer-is-unsafe">
62+
C28637
63+
</a>
64+
</li>
65+
</references>
66+
</qhelp>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
/**
4+
* @id cpp/drivers/unsafe-call-in-global-init
5+
* @kind problem
6+
* @name UnsafeCallInGlobalInit
7+
* @description When using a DLL, it is frequently the case that any
8+
* static construtors are called from DllMain.
9+
* There are a number of constraints that apply to calling
10+
* other functions from DllMain. In particular, it is
11+
* possible to create memory leaks if the DLL is loaded
12+
* and unloaded dynamically.
13+
* @platform Desktop
14+
* @feature.area Multiple
15+
* @impact Insecure Coding Practice
16+
* @repro.text
17+
* @owner.email: [email protected]
18+
* @opaqueid CQLD-C28637
19+
* @problem.severity warning
20+
* @precision medium
21+
* @tags correctness
22+
* ca_ported
23+
* @scope domainspecific
24+
* @query-version v1
25+
*/
26+
27+
import cpp
28+
import drivers.libraries.DriverIsolation
29+
30+
from Function f, string msg
31+
where
32+
f.getName().matches("DllMain") and
33+
exists(FunctionCall fc |
34+
fc.getEnclosingFunction() = f
35+
and
36+
(
37+
fc.getTarget()
38+
.getName()
39+
.matches([
40+
"LoadLibrary%", "LoadLibraryEx", "GetStringTypeA", "GetStringTypeEx", "GetStringTypeW",
41+
"CoInitializeEx", "CreateProcess%", "ExitThread%", "CreateThread%", "ShGetFolderPathW"
42+
]) or
43+
fc instanceof RegistryIsolationFunctionCall
44+
)
45+
and
46+
msg = "Unsafe call in DllMain: " + fc.getTarget().getName() + ". "
47+
)
48+
or
49+
exists(Initializer i |
50+
f.getName().matches("DllMain") and
51+
i.getExpr().getEnclosingFunction() = f and
52+
not i.getDeclaration().isStatic() and
53+
i.getExpr().toString().toLowerCase().matches("null") and
54+
msg = "Potential unsafe initialization in DllMain. "
55+
)
56+
select f, msg + " Review Dynamic-Link Library Best Practices."

0 commit comments

Comments
 (0)