Skip to content

Commit f7924bd

Browse files
authored
Merge pull request github#13099 from MathiasVP/heuristic-allocation-for-overrun-write
C++: Use heuristic allocation functions in `cpp/overrun-write`
2 parents d045160 + 363514e commit f7924bd

File tree

4 files changed

+12
-2
lines changed

4 files changed

+12
-2
lines changed

cpp/ql/lib/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ private module HeuristicAllocation {
414414
int sizeArg;
415415

416416
HeuristicAllocationFunctionByName() {
417-
Function.super.getName().matches("%alloc%") and
417+
Function.super.getName().matches(["%alloc%", "%Alloc%"]) and
418418
Function.super.getUnspecifiedType() instanceof PointerType and
419419
sizeArg = unique( | | getAnUnsignedParameter(this))
420420
}

cpp/ql/src/experimental/Likely Bugs/OverrunWriteProductFlow.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ VariableAccess getAVariableAccess(Expr e) { e.getAChild*() = result }
4747
* Holds if `(n, state)` pair represents the source of flow for the size
4848
* expression associated with `alloc`.
4949
*/
50-
predicate hasSize(AllocationExpr alloc, DataFlow::Node n, int state) {
50+
predicate hasSize(HeuristicAllocationExpr alloc, DataFlow::Node n, int state) {
5151
exists(VariableAccess va, Expr size, int delta |
5252
size = alloc.getSizeExpr() and
5353
// Get the unique variable in a size expression like `x` in `malloc(x + 1)`.

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-119/OverrunWriteProductFlow.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ edges
222222
| test.cpp:243:12:243:14 | str indirection [string] | test.cpp:243:12:243:21 | string |
223223
| test.cpp:243:12:243:14 | str indirection [string] | test.cpp:243:16:243:21 | string indirection |
224224
| test.cpp:243:16:243:21 | string indirection | test.cpp:243:12:243:21 | string |
225+
| test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p |
225226
nodes
226227
| test.cpp:16:11:16:21 | mk_string_t indirection [string] | semmle.label | mk_string_t indirection [string] |
227228
| test.cpp:18:5:18:30 | ... = ... | semmle.label | ... = ... |
@@ -402,6 +403,8 @@ nodes
402403
| test.cpp:243:12:243:14 | str indirection [string] | semmle.label | str indirection [string] |
403404
| test.cpp:243:12:243:21 | string | semmle.label | string |
404405
| test.cpp:243:16:243:21 | string indirection | semmle.label | string indirection |
406+
| test.cpp:249:20:249:27 | call to my_alloc | semmle.label | call to my_alloc |
407+
| test.cpp:250:12:250:12 | p | semmle.label | p |
405408
subpaths
406409
| test.cpp:242:22:242:27 | buffer | test.cpp:235:40:235:45 | buffer | test.cpp:236:12:236:17 | p_str indirection [post update] [string] | test.cpp:242:16:242:19 | set_string output argument [string] |
407410
#select
@@ -422,3 +425,4 @@ subpaths
422425
| test.cpp:207:9:207:15 | call to strncpy | test.cpp:147:19:147:24 | call to malloc | test.cpp:207:22:207:27 | string | This write may overflow $@ by 3 elements. | test.cpp:207:22:207:27 | string | string |
423426
| test.cpp:232:3:232:8 | call to memset | test.cpp:228:43:228:48 | call to malloc | test.cpp:232:10:232:15 | buffer | This write may overflow $@ by 32 elements. | test.cpp:232:10:232:15 | buffer | buffer |
424427
| test.cpp:243:5:243:10 | call to memset | test.cpp:241:27:241:32 | call to malloc | test.cpp:243:12:243:21 | string | This write may overflow $@ by 1 element. | test.cpp:243:16:243:21 | string | string |
428+
| test.cpp:250:5:250:10 | call to memset | test.cpp:249:20:249:27 | call to my_alloc | test.cpp:250:12:250:12 | p | This write may overflow $@ by 1 element. | test.cpp:250:12:250:12 | p | p |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-119/test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,9 @@ void test_flow_through_setter(unsigned size) {
243243
memset(str.string, 0, size + 1); // BAD
244244
}
245245

246+
void* my_alloc(unsigned size);
247+
248+
void foo(unsigned size) {
249+
int* p = (int*)my_alloc(size); // BAD
250+
memset(p, 0, size + 1);
251+
}

0 commit comments

Comments
 (0)