|
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 | | -import cpp |
12 | | - |
13 | | -// an expression is possibly stack allocated if it is an aggregate literal |
14 | | -// or a reference to a possibly stack allocated local variables |
15 | | -predicate exprMaybeStackAllocated(Expr e) { |
16 | | - e instanceof AggregateLiteral |
17 | | - or varMaybeStackAllocated(e.(VariableAccess).getTarget()) |
18 | | -} |
19 | | - |
20 | | -// a local variable is possibly stack allocated if it is not static and |
21 | | -// is initialized to/assigned a possibly stack allocated expression |
22 | | -predicate varMaybeStackAllocated(LocalVariable lv) { |
23 | | - not lv.isStatic() and |
24 | | - ( lv.getType().getUnderlyingType() instanceof ArrayType |
25 | | - or exprMaybeStackAllocated(lv.getInitializer().getExpr()) |
26 | | - or exists(AssignExpr a | a.getLValue().(VariableAccess).getTarget() = lv and |
27 | | - exprMaybeStackAllocated(a.getRValue()))) |
28 | | -} |
29 | 13 |
|
30 | | -// an expression possibly points to the stack if it takes the address of |
31 | | -// a possibly stack allocated expression, if it is a reference to a local variable |
32 | | -// that possibly points to the stack, or if it is a possibly stack allocated array |
33 | | -// that is converted (implicitly or explicitly) to a pointer |
34 | | -predicate exprMayPointToStack(Expr e) { |
35 | | - e instanceof AddressOfExpr and exprMaybeStackAllocated(e.(AddressOfExpr).getAnOperand()) |
36 | | - or varMayPointToStack(e.(VariableAccess).getTarget()) |
37 | | - or exprMaybeStackAllocated(e) and e.getType() instanceof ArrayType and e.getFullyConverted().getType() instanceof PointerType |
38 | | -} |
| 14 | +import cpp |
| 15 | +import semmle.code.cpp.dataflow.EscapesTree |
| 16 | +import semmle.code.cpp.dataflow.DataFlow |
39 | 17 |
|
40 | | -// a local variable possibly points to the stack if it is initialized to/assigned to |
41 | | -// an expression that possibly points to the stack |
42 | | -predicate varMayPointToStack(LocalVariable lv) { |
43 | | - exprMayPointToStack(lv.getInitializer().getExpr()) |
44 | | - or exists(AssignExpr a | a.getLValue().(VariableAccess).getTarget() = lv and |
45 | | - exprMayPointToStack(a.getRValue())) |
| 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 |
46 | 26 | } |
47 | 27 |
|
48 | | -from ReturnStmt r |
49 | | -where exprMayPointToStack(r.getExpr()) |
50 | | -select r, "May return stack-allocated memory." |
| 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 |
| 34 | + ( |
| 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 | + ) |
| 50 | + ) |
| 51 | +select r, "May return stack-allocated memory from $@.", va, va.toString() |
0 commit comments