|
| 1 | +/** |
| 2 | + * @name Errors When Double Free |
| 3 | + * @description Double freeing of a previously allocated resource can lead to various vulnerabilities in the program |
| 4 | + * and requires the attention of the developer. |
| 5 | + * @kind problem |
| 6 | + * @id cpp/errors-when-double-free |
| 7 | + * @problem.severity warning |
| 8 | + * @precision medium |
| 9 | + * @tags security |
| 10 | + * external/cwe/cwe-415 |
| 11 | + */ |
| 12 | + |
| 13 | +import cpp |
| 14 | + |
| 15 | +/** |
| 16 | + * The function allows `getASuccessor` to be called recursively. |
| 17 | + * This provides a stop in situations of possible influence on the pointer. |
| 18 | + */ |
| 19 | +ControlFlowNode recursASuccessor(FunctionCall fc, LocalScopeVariable v) { |
| 20 | + result = fc |
| 21 | + or |
| 22 | + exists(ControlFlowNode mid | |
| 23 | + mid = recursASuccessor(fc, v) and |
| 24 | + result = mid.getASuccessor() and |
| 25 | + not result = v.getAnAssignedValue() and |
| 26 | + not result.(AddressOfExpr).getOperand() = v.getAnAccess() and |
| 27 | + not ( |
| 28 | + not result instanceof DeallocationExpr and |
| 29 | + result.(FunctionCall).getAnArgument().(VariableAccess).getTarget() = v |
| 30 | + ) and |
| 31 | + ( |
| 32 | + fc.getTarget().hasGlobalOrStdName("realloc") and |
| 33 | + ( |
| 34 | + not fc.getParent*() instanceof IfStmt and |
| 35 | + not result instanceof IfStmt |
| 36 | + ) |
| 37 | + or |
| 38 | + not fc.getTarget().hasGlobalOrStdName("realloc") |
| 39 | + ) |
| 40 | + ) |
| 41 | +} |
| 42 | + |
| 43 | +from FunctionCall fc |
| 44 | +where |
| 45 | + exists(FunctionCall fc2, LocalScopeVariable v | |
| 46 | + freeCall(fc, v.getAnAccess()) and |
| 47 | + freeCall(fc2, v.getAnAccess()) and |
| 48 | + fc != fc2 and |
| 49 | + recursASuccessor(fc, v) = fc2 |
| 50 | + ) |
| 51 | +select fc.getArgument(0), "This pointer may be cleared again later." |
0 commit comments