Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role Types Correctly Used Query #94

Merged
merged 87 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 79 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
7c51892
Initial work at IRQL-checking
NateD-MSFT Mar 21, 2023
40d0a8c
Significant extra IRQL work.
NateD-MSFT Mar 21, 2023
c7e73e9
Merge branch 'main' into nated-msft/c28124-c28150
NateD-MSFT Aug 17, 2023
a79ab5b
Merge branch 'development' into nated-msft/c28124-c28150
NateD-MSFT Aug 24, 2023
8f96a77
In-progress work
NateD-MSFT Aug 25, 2023
7479c24
More puttering around with IRQL
NateD-MSFT Sep 13, 2023
9d96c89
Update to CodeQL 2.14.4
NateD-MSFT Sep 13, 2023
24dc7df
Merge branch 'nated-msft/codeql-2144-update' into nated-msft/c28124-c…
NateD-MSFT Sep 13, 2023
1cbca7e
Commit more IRQL code. Needs cleanup.
NateD-MSFT Sep 29, 2023
564d095
Some cleanup and minor fixes to entry IRQL evaluation.
NateD-MSFT Sep 29, 2023
6861e39
Replace old Irql high/low checks with new version and update library.
NateD-MSFT Oct 4, 2023
80a70de
Irql.qll cleanup
NateD-MSFT Oct 5, 2023
ebb570d
Get rid of old prototype version of IrqlTooLow
NateD-MSFT Oct 5, 2023
ee4b80c
Update README.md
NateD-MSFT Oct 5, 2023
890e8e6
Merge branch 'nated-msft/codeql-2144-update' into nated-msft/c28124-c…
NateD-MSFT Oct 5, 2023
e1ddb6a
Clean up file names
NateD-MSFT Oct 5, 2023
6dd4a61
Clean up queries.
NateD-MSFT Oct 5, 2023
c9dc15e
Update test script for IRQL queries.
NateD-MSFT Oct 5, 2023
d964175
Update build-codeql.yaml
NateD-MSFT Oct 5, 2023
9aaa8ff
Update ported_driver_ca_checks.qls
NateD-MSFT Oct 5, 2023
5d5adaa
Merge branch 'nated-msft/codeql-2144-update' into nated-msft/c28120-c…
NateD-MSFT Oct 5, 2023
bb06b89
Add IrqlSetTooHigh/IrqlSetTooLow queries.
NateD-MSFT Oct 10, 2023
4842fd4
Bugfix for IrqlTooHigh/IrqlTooLow
NateD-MSFT Oct 10, 2023
d27b6cd
Fix test issues for several IRQL checks.
NateD-MSFT Oct 10, 2023
23ec263
WIP unit tests for IrqlSetTooHigh and IrqlSetTooLow queries
jacob-ronstadt Oct 10, 2023
b8256c9
WIP unit tests for IrqlSetTooHigh and IrqlSetTooLow queries
jacob-ronstadt Oct 10, 2023
ed5d9d5
WIP more tests and comments
jacob-ronstadt Oct 11, 2023
48a157a
bug fixes
jacob-ronstadt Oct 11, 2023
671cd9c
WIP updates to tests
jacob-ronstadt Oct 11, 2023
a042bd8
WIP update tests
jacob-ronstadt Oct 11, 2023
495add6
remove bad tests. Fix run script to run all tests again. run script n…
jacob-ronstadt Oct 11, 2023
a49921c
update tests for IrqlSetTooHigh
jacob-ronstadt Oct 11, 2023
718c480
Merge branch 'nated-msft/c28124-c28150' into jacob-ronstadt/IrqlSet_t…
NateD-MSFT Oct 11, 2023
3208b0b
WIP IrqlSetTooLow tests
jacob-ronstadt Oct 11, 2023
11b8ad4
Merge branch 'jacob-ronstadt/IrqlSet_tests' of https://github.com/mic…
NateD-MSFT Oct 11, 2023
fbbf360
Fix typo in Irql.qll
NateD-MSFT Oct 12, 2023
6ee7ddd
irqlSetTooHigh tests remove calls to KeGetCurrentIRQL as they are not…
jacob-ronstadt Oct 12, 2023
a698823
update IrqlSetTooLow tests
jacob-ronstadt Oct 12, 2023
4250f6f
update tests. line 90 should be a failling test but isnt
jacob-ronstadt Oct 12, 2023
fd9084b
fix IrqlLowerWithFunctionCall1 to call IrqlMinDispatchLowerIrql_fail1
jacob-ronstadt Oct 13, 2023
f0fe56e
Revert"fix IrqlLowerWithFunctionCall1 to call IrqlMinDispatchLowerIrq…
jacob-ronstadt Oct 13, 2023
a35c1c2
fix IrqlLowerWithFunctionCall1 to call IrqlMinDispatchLowerIrql_fail1
jacob-ronstadt Oct 13, 2023
7333100
add WDF function class and structure class
jacob-ronstadt Oct 13, 2023
1997dc4
add kmdf callback functions
jacob-ronstadt Oct 16, 2023
5897744
syntax updates
jacob-ronstadt Oct 16, 2023
1790774
add comments
jacob-ronstadt Oct 19, 2023
6552bfa
Expand WdmDrivers.qll. Add missing WDM Callback Routine types and Rol…
jacob-ronstadt Oct 31, 2023
a6f8cea
WIP query to check role types
jacob-ronstadt Oct 31, 2023
37e417e
queries for implicit and explicit role types
jacob-ronstadt Nov 2, 2023
c8f58fd
move implicit role type query to library as a class
jacob-ronstadt Nov 2, 2023
8950923
update wdmdrivers.qll
jacob-ronstadt Nov 2, 2023
4dd8096
RoleTypeCorrectlyUsed query
jacob-ronstadt Nov 2, 2023
cd21984
WIP
jacob-ronstadt Nov 6, 2023
ef62692
update query
jacob-ronstadt Nov 6, 2023
e3b67d6
WIP updating wdmdrivers.qll to better define role type functions
jacob-ronstadt Nov 7, 2023
855397c
update query message
jacob-ronstadt Nov 7, 2023
4e20294
merge in kmdf library chnages
jacob-ronstadt Nov 8, 2023
c0de485
adding KMDF support for role types
jacob-ronstadt Nov 8, 2023
3de6873
updates to role type query for kmdf and wdm
jacob-ronstadt Nov 8, 2023
4eeb6ec
move wdm role type query to wdm folder
jacob-ronstadt Nov 8, 2023
485cb0b
WIP library for NDIS
jacob-ronstadt Nov 9, 2023
08a9220
WIP NDIS role type query
jacob-ronstadt Nov 9, 2023
33c252c
WIP storport library
jacob-ronstadt Nov 9, 2023
b507dd5
WIP storport query
jacob-ronstadt Nov 9, 2023
d0da7cf
add irql level to wdm role types
jacob-ronstadt Nov 9, 2023
0cebb56
create generic RoleTypes library to include wdm, kmdf, ndis, and stor…
jacob-ronstadt Nov 9, 2023
ea785dc
generic role type query
jacob-ronstadt Nov 9, 2023
56d4548
add function annotation role types. Add extra role types from wdfrole…
jacob-ronstadt Nov 10, 2023
ab50708
ignore c++ functions for now until codeql issue figured out
jacob-ronstadt Nov 21, 2023
c05041d
update role type query to remove double results
jacob-ronstadt Nov 28, 2023
89c5bbb
update role type query
jacob-ronstadt Nov 28, 2023
63dce02
update sarif results, diff, and test script
jacob-ronstadt Nov 28, 2023
157eafd
merge in development branch
jacob-ronstadt Nov 29, 2023
49e45c7
fix conflict in WdmDrivers.qll
jacob-ronstadt Nov 29, 2023
0407f61
remove irql level from role type library due to conflict
jacob-ronstadt Nov 30, 2023
258e20c
fix WdmDrivers.qll
jacob-ronstadt Dec 1, 2023
5e33cf5
fix wdmdrivers.qll
jacob-ronstadt Dec 1, 2023
dd4234f
remove irql library from wdmdrivers.qll
jacob-ronstadt Dec 1, 2023
c68da06
remove old/unused role type queries
jacob-ronstadt Dec 1, 2023
1315362
Update src/drivers/storport/libraries/StorportDrivers.qll
jacob-ronstadt Dec 4, 2023
1916a8c
Update src/drivers/storport/libraries/StorportDrivers.qll
jacob-ronstadt Dec 4, 2023
421f360
Update src/drivers/storport/libraries/StorportDrivers.qll
jacob-ronstadt Dec 4, 2023
79cd025
Update src/drivers/storport/libraries/StorportDrivers.qll
jacob-ronstadt Dec 4, 2023
688db58
Update src/drivers/storport/libraries/StorportDrivers.qll
jacob-ronstadt Dec 4, 2023
d35bf26
fix issue causing problems with diffs
jacob-ronstadt Dec 5, 2023
fd617e8
changes for pull request
jacob-ronstadt Dec 5, 2023
20e3594
merge development in and fix conflicts
jacob-ronstadt Dec 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @id cpp/drivers/role-type-correctly-used
* @kind problem
* @name Incorrect Role Type Use
* @description A function is declared with a role type but used as an argument in a function that expects a different role type for that argument.
* @platform Desktop
* @feature.area Multiple
* @impact Insecure Coding Practice
* @repro.text
* @owner.email: [email protected]
* @opaqueid CQLD-C28147e
* @problem.severity warning
* @precision medium
* @tags correctness
* @scope domainspecific
* @query-version v1
*/

import cpp
import drivers.libraries.RoleTypes
import semmle.code.cpp.TypedefType

from ImplicitRoleTypeFunction irtf, Function f, string rtActual, string rtExpected
where
irtf.getActualRoleTypeString() != irtf.getExpectedRoleTypeString() and
f = irtf.getFunctionUse().getTarget() and
(
if f instanceof RoleTypeFunction
then rtActual = f.(RoleTypeFunction).getRoleTypeString()
else rtActual = "<NO_ROLE_TYPE>"
) and
rtExpected = irtf.getExpectedRoleTypeString() and
not isEqualRoleTypes(rtExpected, rtActual)
select irtf.getFunctionUse(),
"Function " + f.toString() + " declared with role type " + rtActual + " but role type " + rtExpected +
" is expected."

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd">
<qhelp>
<overview>
<p>
Driver entry point functions should be declared with a function role type.
</p>
</overview>
<recommendation>
<p>
Make sure the role type of the function being used matches the expected role type.
</p>
</recommendation>
<example>
<sample src="driver_snippet.c" />
</example>
<semmleNotes>
<p>
C++ functions not currently supported. See https://github.com/github/codeql/issues/14869
</p>
</semmleNotes>
<references>
<li>
<a href="https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/declaring-functions-using-function-role-types-for-wdm-drivers">
C28158 warning - Windows Drivers
</a>
</li>
</references>
</qhelp>
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
{
"$schema" : "https://json.schemastore.org/sarif-2.1.0.json",
"version" : "2.1.0",
"runs" : [ {
"tool" : {
"driver" : {
"name" : "CodeQL",
"organization" : "GitHub",
"semanticVersion" : "2.14.6",
"notifications" : [ {
"id" : "cpp/baseline/expected-extracted-files",
"name" : "cpp/baseline/expected-extracted-files",
"shortDescription" : {
"text" : "Expected extracted files"
},
"fullDescription" : {
"text" : "Files appearing in the source archive that are expected to be extracted."
},
"defaultConfiguration" : {
"enabled" : true
},
"properties" : {
"tags" : [ "expected-extracted-files", "telemetry" ]
}
} ],
"rules" : [ {
"id" : "cpp/drivers/role-type-correctly-used",
"name" : "cpp/drivers/role-type-correctly-used",
"shortDescription" : {
"text" : "Incorrect Role Type Use"
},
"fullDescription" : {
"text" : "A function is declared with a role type but used as an argument in a function that expects a different role type for that argument."
},
"defaultConfiguration" : {
"enabled" : true,
"level" : "warning"
},
"properties" : {
"tags" : [ "correctness" ],
"description" : "A function is declared with a role type but used as an argument in a function that expects a different role type for that argument.",
"feature.area" : "Multiple",
"id" : "cpp/drivers/role-type-correctly-used",
"impact" : "Insecure Coding Practice",
"kind" : "problem",
"name" : "Incorrect Role Type Use",
"opaqueid" : "CQLD-C28147e",
"owner.email:" : "[email protected]",
"platform" : "Desktop",
"precision" : "medium",
"problem.severity" : "warning",
"query-version" : "v1",
"repro.text" : "",
"scope" : "domainspecific"
}
} ]
},
"extensions" : [ {
"name" : "microsoft/windows-drivers",
"semanticVersion" : "0.2.0+89c5bbb932add8b9f527d104a47ab0828cb93370",
"locations" : [ {
"uri" : "file:///C:/codeql-home/Windows-Driver-Developer-Supplemental-Tools/src/",
"description" : {
"text" : "The QL pack root directory."
}
}, {
"uri" : "file:///C:/codeql-home/Windows-Driver-Developer-Supplemental-Tools/src/qlpack.yml",
"description" : {
"text" : "The QL pack definition file."
}
} ]
} ]
},
"invocations" : [ {
"toolExecutionNotifications" : [ {
"locations" : [ {
"physicalLocation" : {
"artifactLocation" : {
"uri" : "driver/driver_snippet.c",
"uriBaseId" : "%SRCROOT%",
"index" : 1
}
}
} ],
"message" : {
"text" : ""
},
"level" : "none",
"descriptor" : {
"id" : "cpp/baseline/expected-extracted-files",
"index" : 0
},
"properties" : {
"formattedMessage" : {
"text" : ""
}
}
}, {
"locations" : [ {
"physicalLocation" : {
"artifactLocation" : {
"uri" : "driver/fail_driver1.c",
"uriBaseId" : "%SRCROOT%",
"index" : 0
}
}
} ],
"message" : {
"text" : ""
},
"level" : "none",
"descriptor" : {
"id" : "cpp/baseline/expected-extracted-files",
"index" : 0
},
"properties" : {
"formattedMessage" : {
"text" : ""
}
}
}, {
"locations" : [ {
"physicalLocation" : {
"artifactLocation" : {
"uri" : "driver/fail_driver1.h",
"uriBaseId" : "%SRCROOT%",
"index" : 2
}
}
} ],
"message" : {
"text" : ""
},
"level" : "none",
"descriptor" : {
"id" : "cpp/baseline/expected-extracted-files",
"index" : 0
},
"properties" : {
"formattedMessage" : {
"text" : ""
}
}
} ],
"executionSuccessful" : true
} ],
"artifacts" : [ {
"location" : {
"uri" : "driver/fail_driver1.c",
"uriBaseId" : "%SRCROOT%",
"index" : 0
}
}, {
"location" : {
"uri" : "driver/driver_snippet.c",
"uriBaseId" : "%SRCROOT%",
"index" : 1
}
}, {
"location" : {
"uri" : "driver/fail_driver1.h",
"uriBaseId" : "%SRCROOT%",
"index" : 2
}
} ],
"results" : [ {
"ruleId" : "cpp/drivers/role-type-correctly-used",
"ruleIndex" : 0,
"rule" : {
"id" : "cpp/drivers/role-type-correctly-used",
"index" : 0
},
"message" : {
"text" : "Function DriverUnload declared with role type <NO_ROLE_TYPE> but role type DRIVER_UNLOAD is expected."
},
"locations" : [ {
"physicalLocation" : {
"artifactLocation" : {
"uri" : "driver/fail_driver1.c",
"uriBaseId" : "%SRCROOT%",
"index" : 0
},
"region" : {
"startLine" : 83,
"startColumn" : 60,
"endColumn" : 72
}
}
} ],
"partialFingerprints" : {
"primaryLocationLineHash" : "b54b2cfbe2573eb8:1",
"primaryLocationStartColumnFingerprint" : "55"
}
} ],
"columnKind" : "utf16CodeUnits",
"properties" : {
"semmle.formatSpecifier" : "sarifv2.1.0"
}
} ]
}
10 changes: 10 additions & 0 deletions src/drivers/general/queries/RoleTypeCorrectlyUsed/driver_snippet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//Macros to enable or disable a code section that may or maynot conflict with this test.
#define SET_DISPATCH 1

//Template function. Not used for this test.
void top_level_call(){
}

Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,4 @@
"semmle.formatSpecifier" : "sarifv2.1.0"
}
} ]
}
}
Loading
Loading