|
| 1 | +/** |
| 2 | + * Provides classes and predicates for reasoning about command injection |
| 3 | + * vulnerabilities (CWE-078). |
| 4 | + */ |
| 5 | + |
| 6 | +import rust |
| 7 | +private import codeql.rust.dataflow.DataFlow |
| 8 | +private import codeql.rust.dataflow.FlowSink |
| 9 | +private import codeql.rust.dataflow.FlowBarrier |
| 10 | +private import codeql.rust.Concepts |
| 11 | +private import codeql.rust.security.Barriers as Barriers |
| 12 | + |
| 13 | +/** |
| 14 | + * Provides default sources, sinks and barriers for detecting command injection |
| 15 | + * vulnerabilities, as well as extension points for adding your own. |
| 16 | + */ |
| 17 | +module CommandInjection { |
| 18 | + /** |
| 19 | + * A data flow source for command injection vulnerabilities. |
| 20 | + */ |
| 21 | + abstract class Source extends DataFlow::Node { } |
| 22 | + |
| 23 | + /** |
| 24 | + * A data flow sink for command injection vulnerabilities. |
| 25 | + */ |
| 26 | + abstract class Sink extends QuerySink::Range { |
| 27 | + override string getSinkType() { result = "CommandInjection" } |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * A barrier for command injection vulnerabilities. |
| 32 | + */ |
| 33 | + abstract class Barrier extends DataFlow::Node { } |
| 34 | + |
| 35 | + /** |
| 36 | + * An active threat-model source, considered as a flow source. |
| 37 | + */ |
| 38 | + private class ActiveThreatModelSourceAsSource extends Source, ActiveThreatModelSource { } |
| 39 | + |
| 40 | + /** |
| 41 | + * A sink for command injection from model data. |
| 42 | + */ |
| 43 | + private class ModelsAsDataSink extends Sink { |
| 44 | + ModelsAsDataSink() { sinkNode(this, "command-injection") } |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * A barrier for command injection from model data. |
| 49 | + */ |
| 50 | + private class ModelsAsDataBarrier extends Barrier { |
| 51 | + ModelsAsDataBarrier() { barrierNode(this, "command-injection") } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * A barrier for command injection vulnerabilities for nodes whose type is a |
| 56 | + * numeric type, which is unlikely to expose any vulnerability. |
| 57 | + */ |
| 58 | + private class NumericTypeBarrier extends Barrier instanceof Barriers::NumericTypeBarrier { } |
| 59 | + |
| 60 | + private class BooleanTypeBarrier extends Barrier instanceof Barriers::BooleanTypeBarrier { } |
| 61 | + |
| 62 | + private class FieldlessEnumTypeBarrier extends Barrier instanceof Barriers::FieldlessEnumTypeBarrier |
| 63 | + { } |
| 64 | + |
| 65 | + /** |
| 66 | + * A sanitizer guard for command injection vulnerabilities. |
| 67 | + */ |
| 68 | + class SanitizerGuard extends Barrier { |
| 69 | + SanitizerGuard() { this = DataFlow::BarrierGuard<sanitizerGuard/3>::getABarrierNode() } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +private predicate sanitizerGuard(AstNode g, Expr e, boolean branch) { |
| 74 | + g.(SanitizerGuard::Range).checks(e, branch) |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Provides a class for modeling new command injection safety checks. |
| 79 | + */ |
| 80 | +module SanitizerGuard { |
| 81 | + /** |
| 82 | + * A data-flow node that checks whether a command is safe. |
| 83 | + */ |
| 84 | + abstract class Range extends AstNode { |
| 85 | + /** |
| 86 | + * Holds if this guard validates `e` upon evaluating to `branch`. |
| 87 | + */ |
| 88 | + abstract predicate checks(Expr e, boolean branch); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * A successful membership check against a (presumed) command allowlist. For example: |
| 94 | + * ``` |
| 95 | + * if allowlist.contains(&commmand) { ... } |
| 96 | + * ``` |
| 97 | + */ |
| 98 | +private class AllowlistContainsCheck extends SanitizerGuard::Range, MethodCall { |
| 99 | + AllowlistContainsCheck() { this.getStaticTarget().getName().getText() = "contains" } |
| 100 | + |
| 101 | + override predicate checks(Expr e, boolean branch) { |
| 102 | + e.getParentNode*() = this.getPositionalArgument(0) and |
| 103 | + branch = true |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * An equality check against a (presumed) allowed command value. For example: |
| 109 | + * ``` |
| 110 | + * if command == "ls" { ... } |
| 111 | + * ``` |
| 112 | + */ |
| 113 | +private class AllowlistEqualityCheck extends SanitizerGuard::Range, EqualityOperation { |
| 114 | + override predicate checks(Expr e, boolean branch) { |
| 115 | + e = this.getAnOperand() and |
| 116 | + ( |
| 117 | + this instanceof EqualsOperation and branch = true |
| 118 | + or |
| 119 | + this instanceof NotEqualsOperation and branch = false |
| 120 | + ) |
| 121 | + } |
| 122 | +} |
0 commit comments