|
| 1 | +/** Definitions for reasoning about loop variable capture issues. */ |
| 2 | + |
| 3 | +import python |
| 4 | +import semmle.python.dataflow.new.DataFlow |
| 5 | + |
| 6 | +/** A looping construct. */ |
| 7 | +abstract class Loop extends AstNode { |
| 8 | + /** |
| 9 | + * Gets a loop variable of this loop. |
| 10 | + * For example, `x` and `y` in `for x,y in pairs: print(x+y)` |
| 11 | + */ |
| 12 | + abstract Variable getALoopVariable(); |
| 13 | +} |
| 14 | + |
| 15 | +/** A `for` loop. */ |
| 16 | +private class ForLoop extends Loop, For { |
| 17 | + override Variable getALoopVariable() { |
| 18 | + this.getTarget() = result.getAnAccess().getParentNode*() and |
| 19 | + result.getScope() = this.getScope() |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +/** Holds if the callable `capturing` captures the variable `var` from the loop `loop`. */ |
| 24 | +predicate capturesLoopVariable(CallableExpr capturing, Loop loop, Variable var) { |
| 25 | + var.getAnAccess().getScope() = capturing.getInnerScope() and |
| 26 | + capturing.getParentNode+() = loop and |
| 27 | + var = loop.getALoopVariable() |
| 28 | +} |
| 29 | + |
| 30 | +/** Dataflow configuration for reasoning about callables that capture a loop variable and then may escape from the loop. */ |
| 31 | +module EscapingCaptureFlowConfig implements DataFlow::ConfigSig { |
| 32 | + predicate isSource(DataFlow::Node node) { capturesLoopVariable(node.asExpr(), _, _) } |
| 33 | + |
| 34 | + predicate isSink(DataFlow::Node node) { |
| 35 | + // Stored in a dict/list. |
| 36 | + exists(Assign assign, Subscript sub | |
| 37 | + sub = assign.getATarget() and node.asExpr() = assign.getValue() |
| 38 | + ) |
| 39 | + or |
| 40 | + // Stored in a list. |
| 41 | + exists(DataFlow::MethodCallNode mc | mc.calls(_, "append") and node = mc.getArg(0)) |
| 42 | + or |
| 43 | + // Used in a yield statement, likely included in a collection. |
| 44 | + // The element of comprehension expressions desugar to involve a yield statement internally. |
| 45 | + exists(Yield y | node.asExpr() = y.getValue()) |
| 46 | + // Checks for storing in a field leads to false positives, so are omitted. |
| 47 | + } |
| 48 | + |
| 49 | + predicate isBarrierOut(DataFlow::Node node) { isSink(node) } |
| 50 | + |
| 51 | + predicate isBarrier(DataFlow::Node node) { |
| 52 | + // Incorrect virtual dispatch to __call__ methods is a source of FPs. |
| 53 | + exists(Function call | |
| 54 | + call.getName() = "__call__" and |
| 55 | + call.getArg(0) = node.(DataFlow::ParameterNode).getParameter() |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + predicate allowImplicitRead(DataFlow::Node node, DataFlow::ContentSet cs) { |
| 60 | + isSink(node) and |
| 61 | + ( |
| 62 | + cs instanceof DataFlow::TupleElementContent or |
| 63 | + cs instanceof DataFlow::ListElementContent or |
| 64 | + cs instanceof DataFlow::SetElementContent or |
| 65 | + cs instanceof DataFlow::DictionaryElementAnyContent |
| 66 | + ) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** Dataflow for reasoning about callables that capture a loop variable and then escape from the loop. */ |
| 71 | +module EscapingCaptureFlow = DataFlow::Global<EscapingCaptureFlowConfig>; |
| 72 | + |
| 73 | +/** Holds if `capturing` is a callable that captures the variable `var` of the loop `loop`, and then may escape the loop via a flow path from `source` to `sink`. */ |
| 74 | +predicate escapingCapture( |
| 75 | + CallableExpr capturing, Loop loop, Variable var, EscapingCaptureFlow::PathNode source, |
| 76 | + EscapingCaptureFlow::PathNode sink |
| 77 | +) { |
| 78 | + capturesLoopVariable(capturing, loop, var) and |
| 79 | + capturing = source.getNode().asExpr() and |
| 80 | + EscapingCaptureFlow::flowPath(source, sink) |
| 81 | +} |
0 commit comments