|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about system |
| 3 | + * commands built from user-controlled sources (that is, command injection |
| 4 | + * vulnerabilities). |
| 5 | + */ |
| 6 | + |
| 7 | +import swift |
| 8 | +import codeql.swift.dataflow.DataFlow |
| 9 | +import codeql.swift.dataflow.ExternalFlow |
| 10 | + |
| 11 | +/** |
| 12 | + * A dataflow sink for command injection vulnerabilities. |
| 13 | + */ |
| 14 | +abstract class CommandInjectionSink extends DataFlow::Node { } |
| 15 | + |
| 16 | +/** |
| 17 | + * A barrier for command injection vulnerabilities. |
| 18 | + */ |
| 19 | +abstract class CommandInjectionBarrier extends DataFlow::Node { } |
| 20 | + |
| 21 | +/** |
| 22 | + * A unit class for adding additional flow steps. |
| 23 | + */ |
| 24 | +class CommandInjectionAdditionalFlowStep extends Unit { |
| 25 | + /** |
| 26 | + * Holds if the step from `node1` to `node2` should be considered a flow |
| 27 | + * step for paths related to command injection vulnerabilities. |
| 28 | + */ |
| 29 | + abstract predicate step(DataFlow::Node nodeFrom, DataFlow::Node nodeTo); |
| 30 | +} |
| 31 | + |
| 32 | +private class ProcessSink2 extends CommandInjectionSink instanceof DataFlow::Node { |
| 33 | + ProcessSink2() { |
| 34 | + exists(AssignExpr assign, ProcessHost s | |
| 35 | + assign.getDest() = s and |
| 36 | + this.asExpr() = assign.getSource() |
| 37 | + ) |
| 38 | + or |
| 39 | + exists(AssignExpr assign, ProcessHost s, ArrayExpr a | |
| 40 | + assign.getDest() = s and |
| 41 | + a = assign.getSource() and |
| 42 | + this.asExpr() = a.getAnElement() |
| 43 | + ) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +private class ProcessHost extends MemberRefExpr { |
| 48 | + ProcessHost() { this.getBase() instanceof ProcessRef } |
| 49 | +} |
| 50 | + |
| 51 | +/** An expression of type `Process`. */ |
| 52 | +private class ProcessRef extends Expr { |
| 53 | + ProcessRef() { |
| 54 | + this.getType() instanceof ProcessType or |
| 55 | + this.getType() = any(OptionalType t | t.getBaseType() instanceof ProcessType) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** The type `Process`. */ |
| 60 | +private class ProcessType extends NominalType { |
| 61 | + ProcessType() { this.getFullName() = "Process" } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * A `DataFlow::Node` that is written into a `Process` object. |
| 66 | + */ |
| 67 | +private class ProcessSink extends CommandInjectionSink instanceof DataFlow::Node { |
| 68 | + ProcessSink() { |
| 69 | + // any write into a class derived from `Process` is a sink. For |
| 70 | + // example in `Process.launchPath = sensitive` the post-update node corresponding |
| 71 | + // with `Process.launchPath` is a sink. |
| 72 | + exists(NominalType t, Expr e | |
| 73 | + t.getABaseType*().getUnderlyingType().getName() = "Process" and |
| 74 | + e.getFullyConverted() = this.asExpr() and |
| 75 | + e.getFullyConverted().getType() = t |
| 76 | + ) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * A sink defined in a CSV model. |
| 82 | + */ |
| 83 | +private class DefaultCommandInjectionSink extends CommandInjectionSink { |
| 84 | + DefaultCommandInjectionSink() { sinkNode(this, "command-injection") } |
| 85 | +} |
0 commit comments