Commit 219f97f
Reduce false positives compared to SDV for driver tests (#217)
* UserModeMemoryReadMultipleTimes: Remove MdlOrigin.originCanWrite
The double-fetch query flags pairs of dereferences through memory
origins where originCanWrite() is true. MdlOrigin had this set
unconditionally, so every pair of accesses through an MDL-mapped pointer
was flagged, even pure writes to an output buffer:
void *Buf = MmGetSystemAddressForMdlSafe(Mdl, NormalPagePriority);
((MY_IOCTL *)Buf)->Flags = 0; // flagged
((MY_IOCTL *)Buf)->Count = 0; // flagged
While usermode does retain write access to MDL-locked pages through its
original VA, flagging all of these is not useful in practice, since
virtually all direct I/O drivers access the buffer more than once.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* IllegalFieldAccess2: Follow call chains from DriverEntry
The query only allowed MajorFunction access directly inside a
DriverEntry function. Drivers that split initialization across helper
functions were falsely flagged:
VOID InitDispatch(DRIVER_OBJECT *DriverObject) {
DriverObject->MajorFunction[IRP_MJ_CREATE] = MyCreate;
}
NTSTATUS DriverEntry(DRIVER_OBJECT *DriverObject, ...) {
InitDispatch(DriverObject);
}
Follow call chains transitively from DriverEntry so that helpers are
recognized.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* Irql: Evaluate _When_ conditions at call sites
KeSetEvent is annotated _When_(Wait==1, _IRQL_requires_max_(APC_LEVEL)).
When called with Wait=FALSE, the restriction does not apply and
DISPATCH_LEVEL is fine. The query unconditionally applied the annotation
regardless of argument values:
KeAcquireInStackQueuedSpinLock(&Lock, &Handle); // raises to DISPATCH
KeSetEvent(&Event, IO_NO_INCREMENT, FALSE); // flagged
Evaluate _When_ conditions against compile-time argument values and skip
annotations whose conditions are demonstrably false.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* IrqlAnnotationIssue: Skip the -1 parse-failure sentinel
getIrqlLevel() returns -1 when it cannot parse the annotation, such as
_IRQL_saves_ or _When_ conditionals with complex expressions. The query
flagged all of these as invalid annotations:
_IRQL_saves_
VOID KeAcquireSpinLock(PKSPIN_LOCK Lock, PKIRQL OldIrql); // flagged
Filter out the -1 sentinel since these are valid annotations beyond the
analyzer's current parsing capability.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* InvalidFunctionClassTypedef: Allow _PAGED variants
DRIVER_DISPATCH_PAGED is a valid paged variant of DRIVER_DISPATCH for
dispatch routines that only run at PASSIVE_LEVEL. The query flagged the
mismatch between function class and typedef:
_Dispatch_type_(IRP_MJ_DEVICE_CONTROL)
static DRIVER_DISPATCH_PAGED MyDispatch; // flagged
Recognize _PAGED suffixed typedefs as compatible with their unsuffixed
base function class.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* OpaqueMdlUse, OpaqueMdlWrite: Exclude local MDL variables
Drivers sometimes construct synthetic MDLs on the stack for zero-copy
operations. Direct field access is the only way to initialize these:
MDL Mdl = { 0 };
Mdl.MappedSystemVa = Buffer;
Mdl.ByteCount = Len;
Mdl.MdlFlags = MDL_MAPPED_TO_SYSTEM_VA;
Exclude accesses on locally-declared MDL struct variables. Accesses
through MDL pointers are not excluded, since those typically reference
system-provided MDLs.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* UnguardedNullReturnDereference: Exclude calls to _In_opt_ parameters
When a possibly-null return value is passed to a function whose
parameter is annotated _In_opt_, the function explicitly handles null.
The query was flagging these as unguarded dereferences:
OBJ *Obj = LookupObject(...);
PutObject(Obj); // flagged, but PutObject takes _In_opt_ OBJ *
Exclude calls where the argument's corresponding parameter carries an
_opt_ SAL annotation.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
* UnguardedNullReturnDereference: Honor _Analysis_assume_
_Analysis_assume_(Expr) tells the analyzer to treat Expr as true. MSVC
compiles __assume() into an empty statement with no AST node, but the
EmptyStmt remains in the control flow graph at the macro invocation
site. The query was not recognizing this as a null guard:
NBL *Nbl = DequeueNbl(&Queue);
_Analysis_assume_(Nbl);
NET_BUFFER_LIST_STATUS(Nbl) = NDIS_STATUS_FAILURE; // flagged
Match the EmptyStmt at the _Analysis_assume_ location as a barrier by
correlating it with the macro invocation that names the guarded
variable. Also match AssumeExpr directly for compilers that emit it.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---------
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Co-authored-by: NateD-MSFT <34494373+NateD-MSFT@users.noreply.github.com>1 parent d6ed965 commit 219f97f
10 files changed
Lines changed: 106 additions & 29 deletions
File tree
- src
- drivers
- general/queries
- InvalidFunctionClassTypedef
- IrqlAnnotationIssue
- IrqlTooHigh
- IrqlTooLow
- libraries
- wdm/queries
- IllegalFieldAccess2
- OpaqueMdlUse
- OpaqueMdlWrite
- microsoft
- Likely Bugs
- code/cpp/public/windows/kernel
Lines changed: 6 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
76 | 81 | | |
77 | 82 | | |
78 | 83 | | |
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
208 | 208 | | |
209 | 209 | | |
210 | 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 | + | |
211 | 241 | | |
212 | 242 | | |
213 | 243 | | |
| |||
Lines changed: 17 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
90 | 103 | | |
91 | 104 | | |
92 | 105 | | |
| |||
119 | 132 | | |
120 | 133 | | |
121 | 134 | | |
122 | | - | |
123 | | - | |
124 | | - | |
| 135 | + | |
| 136 | + | |
125 | 137 | | |
126 | 138 | | |
127 | 139 | | |
128 | 140 | | |
129 | 141 | | |
130 | | - | |
| 142 | + | |
131 | 143 | | |
132 | 144 | | |
133 | 145 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
45 | 49 | | |
46 | 50 | | |
47 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
46 | 50 | | |
47 | 51 | | |
48 | 52 | | |
| |||
Lines changed: 21 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
| |||
132 | 139 | | |
133 | 140 | | |
134 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
135 | 155 | | |
136 | 156 | | |
137 | 157 | | |
| |||
Lines changed: 0 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
177 | 177 | | |
178 | 178 | | |
179 | 179 | | |
180 | | - | |
181 | | - | |
182 | 180 | | |
183 | 181 | | |
184 | 182 | | |
| |||
0 commit comments