-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathFloatHardwareStateProtection.ql
More file actions
112 lines (100 loc) · 3.96 KB
/
FloatHardwareStateProtection.ql
File metadata and controls
112 lines (100 loc) · 3.96 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @id cpp/drivers/float-hardware-state-protection
* @kind problem
* @name Float Hardware State Protection
* @description Drivers must protect floating-point hardware state.
* @platform Desktop
* @feature.area Multiple
* @impact Insecure Coding Practice
* @repro.text This warning is only applicable in kernel mode. The driver is attempting to use a variable or constant of a float type when the code is not protected by KeSaveFloatingPointState and KeRestoreFloatingPointState, or EngSaveFloatingPointState and EngRestoreFloatingPointState.
* @owner.email sdat@microsoft.com
* @opaqueid CQLD-C28110
* @problem.severity warning
* @precision medium
* @tags correctness
* ca_ported
* @scope domainspecific
* @query-version v1
*/
import cpp
import semmle.code.cpp.dataflow.new.DataFlow
import drivers.libraries.SAL
import drivers.kmdf.libraries.KmdfDrivers
import semmle.code.cpp.Specifier
class KernelFloatFunctionAnnotation extends SALAnnotation {
KernelFloatFunctionAnnotation() { this.getMacroName().matches(["_Kernel_float_used_"]) }
}
class KernelFloatAnnotatedTypedef extends TypedefType {
KernelFloatFunctionAnnotation kernelFloatAnnotation;
KernelFloatAnnotatedTypedef() { kernelFloatAnnotation.getTypedefDeclarations() = this }
KernelFloatFunctionAnnotation getKernelFloatAnnotation() { result = kernelFloatAnnotation }
}
class KernelFloatAnnotatedFunction extends Function {
KernelFloatFunctionAnnotation kernelFloatAnnotation;
KernelFloatAnnotatedFunction() {
(
// this.hasCLinkage() and
exists(
FunctionDeclarationEntry fde // actual function declarations
|
fde = this.getADeclarationEntry() and
fde.getFile().getAnIncludedFile().getBaseName().matches("%wdf.h") and
kernelFloatAnnotation.getDeclarationEntry() = fde
)
or
exists(
FunctionDeclarationEntry fde // typedefs
|
fde.getFunction() = this and
fde.getTypedefType().(KernelFloatAnnotatedTypedef).getKernelFloatAnnotation() =
kernelFloatAnnotation
)
)
}
}
class FuncWithSafeFloatAccess extends Function {
FuncWithSafeFloatAccess() {
exists(FunctionCall funcCallThatUsesFloat, FunctionCall saveFloat, ControlFlowNode saveFloatBB, Function funcThatUsesFloat, VariableAccess floatAccess|
(
saveFloat.getTarget().getName() = ("KeSaveFloatingPointState") or
saveFloat.getTarget().getName() = ("EngSaveFloatingPointState")
)
and saveFloatBB = saveFloat.getBasicBlock()
and floatAccess.getTarget().getType() instanceof FloatingPointType
and funcThatUsesFloat = floatAccess.getEnclosingFunction()
and funcCallThatUsesFloat.getTarget() = funcThatUsesFloat
and funcCallThatUsesFloat.getBasicBlock().getAPredecessor*() = saveFloatBB
and this.calls*(funcThatUsesFloat)
// this function can call a function that uses float
)
}
}
class SafeFloatAccess extends VariableAccess {
SafeFloatAccess() {
this.getType() instanceof FloatingPointType and
exists(FunctionCall saveFloat, ControlFlowNode saveFloatBB |
(
saveFloat.getTarget().getName() = ("KeSaveFloatingPointState") or
saveFloat.getTarget().getName() = ("EngSaveFloatingPointState")
) and
saveFloatBB = saveFloat.getBasicBlock() and
(
this.getBasicBlock().getAPredecessor*() = saveFloatBB or
saveFloatBB.getASuccessor*() = this.getBasicBlock() or
this.getBasicBlock() = saveFloatBB
)
)
or
this.getTarget().getEnclosingElement() instanceof KernelFloatAnnotatedFunction
}
}
from VariableAccess floatAccess
where
floatAccess.getTarget().getType() instanceof FloatingPointType and
not floatAccess instanceof SafeFloatAccess and
not exists(FuncWithSafeFloatAccess safeFunc |
safeFunc = floatAccess.getEnclosingFunction())
select floatAccess,
"Use of float detected without protecting floating-point hardware state"