|
6 | 6 | * @kind problem
|
7 | 7 | * @id cpp/return-stack-allocated-memory
|
8 | 8 | * @problem.severity warning
|
| 9 | + * @precision high |
9 | 10 | * @tags reliability
|
| 11 | + * external/cwe/cwe-825 |
10 | 12 | */
|
11 | 13 |
|
12 | 14 | import cpp
|
| 15 | +import semmle.code.cpp.dataflow.EscapesTree |
| 16 | +import semmle.code.cpp.dataflow.DataFlow |
13 | 17 |
|
14 |
| -// an expression is possibly stack allocated if it is an aggregate literal |
15 |
| -// or accesses a possibly stack allocated local variables |
16 |
| -predicate exprMaybeStackAllocated(Expr e) { |
17 |
| - e instanceof AggregateLiteral or |
18 |
| - varMaybeStackAllocated(e.(VariableAccess).getTarget()) or |
19 |
| - exprMayPointToStack(e.(ArrayExpr).getArrayBase()) |
20 |
| -} |
21 |
| - |
22 |
| -// a local variable is possibly stack allocated if it is not static and |
23 |
| -// is initialized to/assigned a possibly stack allocated expression |
24 |
| -predicate varMaybeStackAllocated(LocalVariable lv) { |
25 |
| - not lv.isStatic() and |
26 |
| - not lv.getType() instanceof ReferenceType |
| 18 | +/** |
| 19 | + * Holds if `n1` may flow to `n2`, ignoring flow through fields because these |
| 20 | + * are currently modeled as an overapproximation that assumes all objects may |
| 21 | + * alias. |
| 22 | + */ |
| 23 | +predicate conservativeDataFlowStep(DataFlow::Node n1, DataFlow::Node n2) { |
| 24 | + DataFlow::localFlowStep(n1, n2) and |
| 25 | + not n2.asExpr() instanceof FieldAccess |
27 | 26 | }
|
28 | 27 |
|
29 |
| -// an expression possibly points to the stack if it takes the address of |
30 |
| -// a possibly stack allocated expression, if it is a reference to a local variable |
31 |
| -// that possibly points to the stack, or if it is a possibly stack allocated array |
32 |
| -// that is converted (implicitly or explicitly) to a pointer |
33 |
| -predicate exprMayPointToStack(Expr e) { |
34 |
| - exprMaybeStackAllocated(e.(AddressOfExpr).getAnOperand()) |
35 |
| - or |
36 |
| - varMayPointToStack(e.(VariableAccess).getTarget()) |
37 |
| - or |
| 28 | +from LocalScopeVariable var, VariableAccess va, ReturnStmt r |
| 29 | +where |
| 30 | + not var.isStatic() and |
| 31 | + not var.getType().getUnspecifiedType() instanceof ReferenceType and |
| 32 | + not r.isFromUninstantiatedTemplate(_) and |
| 33 | + va = var.getAnAccess() and |
38 | 34 | (
|
39 |
| - exprMaybeStackAllocated(e) and |
40 |
| - e.getType() instanceof ArrayType and |
41 |
| - e.getFullyConverted().getType() instanceof PointerType |
| 35 | + // To check if the address escapes directly from `e` in `return e`, we need |
| 36 | + // to check the fully-converted `e` in case there are implicit |
| 37 | + // array-to-pointer conversions or reference conversions. |
| 38 | + variableAddressEscapesTree(va, r.getExpr().getFullyConverted()) |
| 39 | + or |
| 40 | + // The data flow library doesn't support conversions, so here we check that |
| 41 | + // the address escapes into some expression `pointerToLocal`, which flows |
| 42 | + // in a non-trivial way (one or more steps) to a returned expression. |
| 43 | + exists(Expr pointerToLocal | |
| 44 | + variableAddressEscapesTree(va, pointerToLocal.getFullyConverted()) and |
| 45 | + conservativeDataFlowStep+( |
| 46 | + DataFlow::exprNode(pointerToLocal), |
| 47 | + DataFlow::exprNode(r.getExpr()) |
| 48 | + ) |
| 49 | + ) |
42 | 50 | )
|
43 |
| -} |
44 |
| - |
45 |
| -// a local variable possibly points to the stack if it is initialized to/assigned to |
46 |
| -// an expression that possibly points to the stack |
47 |
| -predicate varMayPointToStack(LocalVariable lv) { |
48 |
| - exprMayPointToStack(lv.getInitializer().getExpr()) |
49 |
| - or |
50 |
| - exists(AssignExpr a | |
51 |
| - a.getLValue().(VariableAccess).getTarget() = lv and |
52 |
| - exprMayPointToStack(a.getRValue()) |
53 |
| - ) |
54 |
| -} |
55 |
| - |
56 |
| -from ReturnStmt r |
57 |
| -where exprMayPointToStack(r.getExpr()) |
58 |
| -select r, "May return stack-allocated memory." |
| 51 | +select r, "May return stack-allocated memory from $@.", va, va.toString() |
0 commit comments