|
| 1 | +/** |
| 2 | + * @name Recursive functions |
| 3 | + * @id tob/java/unbounded-recursion |
| 4 | + * @description Detects possibly unbounded recursive calls |
| 5 | + * @kind path-problem |
| 6 | + * @tags security |
| 7 | + * @precision low |
| 8 | + * @problem.severity warning |
| 9 | + * @security-severity 3.0 |
| 10 | + * @group security |
| 11 | + */ |
| 12 | + |
| 13 | +import java |
| 14 | +import semmle.code.java.dataflow.DataFlow |
| 15 | + |
| 16 | +predicate isTestPackage(RefType referenceType) { |
| 17 | + referenceType.getPackage().getName().toLowerCase().matches("%test%") or |
| 18 | + referenceType.getPackage().getName().toLowerCase().matches("%benchmark%") or |
| 19 | + referenceType.getName().toLowerCase().matches("%test%") |
| 20 | +} |
| 21 | + |
| 22 | +class RecursionSource extends Method { |
| 23 | + RecursionSource() { |
| 24 | + not isTestPackage(this.getDeclaringType()) and |
| 25 | + this.calls+(this) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Check if the Expr uses directly an argument of the enclosing function |
| 31 | + */ |
| 32 | +class ParameterOperation extends Expr { |
| 33 | + ParameterOperation() { |
| 34 | + (this instanceof BinaryExpr or this instanceof UnaryAssignExpr) and |
| 35 | + exists(VarAccess va | va.getVariable() = this.getEnclosingCallable().getAParameter() | |
| 36 | + this.getAChildExpr+() = va |
| 37 | + ) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +module RecursiveConfig implements DataFlow::StateConfigSig { |
| 42 | + class FlowState = Method; |
| 43 | + |
| 44 | + predicate isSource(DataFlow::Node node, FlowState firstMethod) { |
| 45 | + node.asExpr().(MethodCall).getCallee() instanceof RecursionSource and |
| 46 | + firstMethod = node.asExpr().(MethodCall).getCallee() |
| 47 | + } |
| 48 | + |
| 49 | + predicate isSink(DataFlow::Node node, FlowState firstMethod) { |
| 50 | + node.asExpr().(MethodCall).getCallee().calls(firstMethod) and |
| 51 | + firstMethod.calls+(node.asExpr().(MethodCall).getCaller()) |
| 52 | + } |
| 53 | + |
| 54 | + predicate isBarrier(DataFlow::Node node) { |
| 55 | + exists(MethodCall ma | |
| 56 | + ma = node.asExpr() and |
| 57 | + exists(Expr e | e = ma.getAnArgument() and e instanceof ParameterOperation) |
| 58 | + ) |
| 59 | + } |
| 60 | + // /** |
| 61 | + // * Weird but useful deduplication logic |
| 62 | + // */ |
| 63 | + // predicate isBarrierOut(DataFlow::Node node, FlowState state) { |
| 64 | + // node.asExpr().(MethodCall).getCallee().getName() > state.getName() |
| 65 | + // } |
| 66 | +} |
| 67 | + |
| 68 | +module RecursiveFlow = DataFlow::GlobalWithState<RecursiveConfig>; |
| 69 | + |
| 70 | +import RecursiveFlow::PathGraph |
| 71 | + |
| 72 | +/* |
| 73 | + * TODO: This query could be improved with the following ideas: |
| 74 | + * - limit results to methods that take any user input |
| 75 | + * - do not return methods that have calls to self (or unbounded recursion) that are conditional |
| 76 | + * - gather and print whole call graph (list of calls from recursiveMethod to itself) |
| 77 | + */ |
| 78 | + |
| 79 | +from RecursiveFlow::PathNode source, RecursiveFlow::PathNode sink |
| 80 | +where RecursiveFlow::flowPath(source, sink) |
| 81 | +select sink.getNode(), source, sink, "Found a recursion: " |
0 commit comments