Skip to content

Commit 40dde93

Browse files
committed
C++: Fix FP and accept test changes.
1 parent 23a7cd9 commit 40dde93

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

cpp/ql/src/Critical/DoubleFree.ql

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@ import DoubleFree::PathGraph
1818

1919
predicate isFree(DataFlow::Node n, Expr e) { isFree(n, e, _) }
2020

21-
/**
22-
* Holds if `fc` is a function call that is the result of expanding
23-
* the `ExFreePool` macro.
24-
*/
25-
predicate isExFreePoolCall(FunctionCall fc) {
26-
exists(MacroInvocation mi |
27-
mi.getMacroName() = "ExFreePool" and
28-
mi.getExpr() = fc
29-
)
30-
or
31-
fc.getTarget().hasGlobalName("ExFreePool")
32-
}
33-
3421
/**
3522
* `dealloc1` is a deallocation expression and `e` is an expression such
3623
* that is deallocated by a deallocation expression, and the `(dealloc1, e)` pair
@@ -46,7 +33,7 @@ predicate isExcludeFreePair(DeallocationExpr dealloc1, Expr e) {
4633
// From https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-mmfreepagesfrommdl:
4734
// "After calling MmFreePagesFromMdl, the caller must also call ExFreePool
4835
// to release the memory that was allocated for the MDL structure."
49-
isExFreePoolCall(dealloc2)
36+
isExFreePoolCall(dealloc2, _)
5037
)
5138
}
5239

cpp/ql/src/Critical/FlowAfterFree.qll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,19 @@ predicate isFree(DataFlow::Node n, Expr e, DeallocationExpr dealloc) {
111111
// Ignore realloc functions
112112
not exists(dealloc.(FunctionCall).getTarget().(AllocationFunction).getReallocPtrArg())
113113
}
114+
115+
/**
116+
* Holds if `fc` is a function call that is the result of expanding
117+
* the `ExFreePool` macro.
118+
*/
119+
predicate isExFreePoolCall(FunctionCall fc, Expr e) {
120+
e = fc.getArgument(0) and
121+
(
122+
exists(MacroInvocation mi |
123+
mi.getMacroName() = "ExFreePool" and
124+
mi.getExpr() = fc
125+
)
126+
or
127+
fc.getTarget().hasGlobalName("ExFreePool")
128+
)
129+
}

cpp/ql/src/Critical/UseAfterFree.ql

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,20 @@ predicate isUse(DataFlow::Node n, Expr e) {
146146
)
147147
}
148148

149-
predicate excludeNothing(DeallocationExpr dealloc, Expr e) { none() }
149+
/**
150+
* `dealloc1` is a deallocation expression, `e` is an expression that dereferences a
151+
* pointer, and the `(dealloc1, e)` pair should be excluded by the `FlowFromFree` library.
152+
*/
153+
bindingset[dealloc1, e]
154+
predicate isExcludeFreeUsePair(DeallocationExpr dealloc1, Expr e) {
155+
// From https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-mmfreepagesfrommdl:
156+
// "After calling MmFreePagesFromMdl, the caller must also call ExFreePool
157+
// to release the memory that was allocated for the MDL structure."
158+
dealloc1.(FunctionCall).getTarget().hasGlobalName("MmFreePagesFromMdl") and
159+
isExFreePoolCall(_, e)
160+
}
150161

151-
module UseAfterFree = FlowFromFree<isUse/2, excludeNothing/2>;
162+
module UseAfterFree = FlowFromFree<isUse/2, isExcludeFreeUsePair/2>;
152163

153164
from UseAfterFree::PathNode source, UseAfterFree::PathNode sink, DeallocationExpr dealloc
154165
where

cpp/ql/test/query-tests/Critical/MemoryFreed/UseAfterFree.expected

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ edges
1515
| test_free.cpp:101:10:101:10 | a | test_free.cpp:102:23:102:23 | a |
1616
| test_free.cpp:152:27:152:27 | a | test_free.cpp:153:5:153:5 | a |
1717
| test_free.cpp:152:27:152:27 | a | test_free.cpp:153:5:153:5 | a |
18-
| test_free.cpp:227:24:227:45 | memory_descriptor_list | test_free.cpp:228:16:228:37 | memory_descriptor_list |
1918
nodes
2019
| test_free.cpp:11:10:11:10 | a | semmle.label | a |
2120
| test_free.cpp:11:10:11:10 | a | semmle.label | a |
@@ -40,8 +39,6 @@ nodes
4039
| test_free.cpp:152:27:152:27 | a | semmle.label | a |
4140
| test_free.cpp:152:27:152:27 | a | semmle.label | a |
4241
| test_free.cpp:153:5:153:5 | a | semmle.label | a |
43-
| test_free.cpp:227:24:227:45 | memory_descriptor_list | semmle.label | memory_descriptor_list |
44-
| test_free.cpp:228:16:228:37 | memory_descriptor_list | semmle.label | memory_descriptor_list |
4542
subpaths
4643
#select
4744
| test_free.cpp:12:5:12:5 | a | test_free.cpp:11:10:11:10 | a | test_free.cpp:12:5:12:5 | a | Memory may have been previously freed by $@. | test_free.cpp:11:5:11:8 | call to free | call to free |
@@ -60,4 +57,3 @@ subpaths
6057
| test_free.cpp:102:23:102:23 | a | test_free.cpp:101:10:101:10 | a | test_free.cpp:102:23:102:23 | a | Memory may have been previously freed by $@. | test_free.cpp:101:5:101:8 | call to free | call to free |
6158
| test_free.cpp:153:5:153:5 | a | test_free.cpp:152:27:152:27 | a | test_free.cpp:153:5:153:5 | a | Memory may have been previously freed by $@. | test_free.cpp:152:22:152:25 | call to free | call to free |
6259
| test_free.cpp:153:5:153:5 | a | test_free.cpp:152:27:152:27 | a | test_free.cpp:153:5:153:5 | a | Memory may have been previously freed by $@. | test_free.cpp:152:22:152:25 | call to free | call to free |
63-
| test_free.cpp:228:16:228:37 | memory_descriptor_list | test_free.cpp:227:24:227:45 | memory_descriptor_list | test_free.cpp:228:16:228:37 | memory_descriptor_list | Memory may have been previously freed by $@. | test_free.cpp:227:5:227:22 | call to MmFreePagesFromMdl | call to MmFreePagesFromMdl |

cpp/ql/test/query-tests/Critical/MemoryFreed/test_free.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,5 +225,5 @@ void MmFreePagesFromMdl(void*);
225225
void ExFreePool(void*);
226226
void test_ms_free(void * memory_descriptor_list) {
227227
MmFreePagesFromMdl(memory_descriptor_list); //GOOD
228-
ExFreePool(memory_descriptor_list); // GOOD [FALSE POSITIVE for cpp/use-after-free]
228+
ExFreePool(memory_descriptor_list); // GOOD
229229
}

0 commit comments

Comments
 (0)