-
Notifications
You must be signed in to change notification settings - Fork 37
IrqlFloatStateMismatch: CodeQL port of C28111 #161
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a763d0b
WIP port of C28111
jacob-ronstadt 308d817
add sarif diff file
jacob-ronstadt ffa7244
Merge branch 'development' into jacob-ronstadt/c28111
jacob-ronstadt b3fe25c
Codeql port of C28111
jacob-ronstadt 075845e
updates from review
jacob-ronstadt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/drivers/general/queries/IrqlFloatStateMismatch/IrqlFloatStateMismatch.qhelp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| <!DOCTYPE qhelp PUBLIC "-//Semmle//qhelp//EN" "qhelp.dtd"> | ||
| <qhelp> | ||
| <overview> | ||
| <p> | ||
| TODO overview | ||
| </p> | ||
| </overview> | ||
| <recommendation> | ||
| <p> | ||
| TODO recommendation | ||
| </p> | ||
| </recommendation> | ||
| <example> | ||
| <p> | ||
| Example of incorrect code. Floating point state was saved at APC_LEVEL but restored at PASSIVE_LEVEL | ||
| </p> | ||
| <sample language="c"> <![CDATA[ | ||
| _IRQL_requires_(PASSIVE_LEVEL) | ||
| void driver_utility_bad(void) | ||
| { | ||
| KIRQL oldIRQL; | ||
| KeRaiseIrql(APC_LEVEL, &oldIRQL); | ||
| // running at APC level | ||
| KFLOATING_SAVE FloatBuf; | ||
| if (KeSaveFloatingPointState(&FloatBuf)) | ||
| { | ||
| KeLowerIrql(oldIRQL); // lower back to PASSIVE_LEVEL | ||
| // ... | ||
| KeRestoreFloatingPointState(&FloatBuf); | ||
| } | ||
| } | ||
|
|
||
| }]]> | ||
| </sample> | ||
| <p> | ||
| Correct example | ||
| </p> | ||
| <sample language="c"> <![CDATA[ | ||
| _IRQL_requires_(PASSIVE_LEVEL) | ||
| void driver_utility_good(void) | ||
| { | ||
| // running at APC level | ||
| KFLOATING_SAVE FloatBuf; | ||
| KIRQL oldIRQL; | ||
| KeRaiseIrql(APC_LEVEL, &oldIRQL); | ||
|
|
||
| if (KeSaveFloatingPointState(&FloatBuf)) | ||
| { | ||
| KeLowerIrql(oldIRQL); | ||
| // ... | ||
| KeRaiseIrql(APC_LEVEL, &oldIRQL); | ||
| KeRestoreFloatingPointState(&FloatBuf); | ||
| } | ||
| } | ||
| }]]> | ||
| </sample> | ||
| </example> | ||
| <semmleNotes> | ||
| <p> | ||
| TODO notes | ||
| </p> | ||
| </semmleNotes> | ||
| <references> | ||
| <li> | ||
| <a href="example.com"> | ||
| Example link | ||
| </a> | ||
| </li> | ||
| </references> | ||
| </qhelp> | ||
52 changes: 52 additions & 0 deletions
52
src/drivers/general/queries/IrqlFloatStateMismatch/IrqlFloatStateMismatch.ql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| /** | ||
| * @id cpp/drivers/irql-float-state-mismatch | ||
| * @kind problem | ||
| * @name Irql Float State Mismatch | ||
| * @description The IRQL where the floating-point state was saved does not match the current IRQL (for this restore operation). | ||
| * @platform Desktop | ||
| * @feature.area Multiple | ||
| * @impact Insecure Coding Practice | ||
| * @repro.text The IRQL at which the driver is executing when it restores a floating-point state is different than the IRQL at which it was executing when it saved the floating-point state. | ||
| * Because the IRQL at which the driver runs determines how the floating-point state is saved, the driver must be executing at the same IRQL when it calls the functions to save and to restore the floating-point state. | ||
| * @owner.email: [email protected] | ||
| * @opaqueid CQLD-C28111 | ||
| * @problem.severity warning | ||
| * @precision medium | ||
| * @tags correctness | ||
| * @scope domainspecific | ||
| * @query-version v1 | ||
| */ | ||
|
|
||
| import cpp | ||
| import drivers.libraries.Irql | ||
| import semmle.code.cpp.dataflow.new.DataFlow | ||
|
|
||
| module FloatStateFlowConfig implements DataFlow::ConfigSig { | ||
| predicate isSource(DataFlow::Node source) { | ||
| exists(FunctionCall fc | | ||
| fc.getTarget().getName().matches("KeSaveFloatingPointState") and | ||
| source.asIndirectExpr() = fc.getArgument(0) | ||
| ) | ||
| } | ||
|
|
||
| predicate isSink(DataFlow::Node sink) { | ||
| exists(FunctionCall fc | | ||
| fc.getTarget().getName().matches("KeRestoreFloatingPointState") and | ||
| sink.asIndirectExpr() = fc.getArgument(0) | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| module FloatStateFlow = DataFlow::Global<FloatStateFlowConfig>; | ||
|
|
||
| from DataFlow::Node source, DataFlow::Node sink, int irqlSink, int irqlSource | ||
| where | ||
| FloatStateFlow::flow(source, sink) and | ||
| irqlSource = getPotentialExitIrqlAtCfn(source.asIndirectExpr()) and | ||
| irqlSink = getPotentialExitIrqlAtCfn(sink.asIndirectExpr()) and | ||
| irqlSink != irqlSource | ||
| select sink.asIndirectExpr(), | ||
| "The irql level where the floating-point state was saved (" + irqlSource + | ||
| ") does not match the irql level for the restore operation (" + irqlSink + ")." |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.