|
| 1 | +/** |
| 2 | + * Provides default sources, sinks and sanitizers for reasoning about |
| 3 | + * shell command constructed from library input vulnerabilities, as |
| 4 | + * well as extension points for adding your own. |
| 5 | + */ |
| 6 | + |
| 7 | +private import python |
| 8 | +import semmle.python.dataflow.new.DataFlow |
| 9 | +import semmle.python.dataflow.new.TaintTracking |
| 10 | +import CommandInjectionCustomizations::CommandInjection as CommandInjection |
| 11 | +private import semmle.python.Concepts as Concepts |
| 12 | + |
| 13 | +/** |
| 14 | + * Module containing sources, sinks, and sanitizers for shell command constructed from library input. |
| 15 | + */ |
| 16 | +module UnsafeShellCommandConstruction { |
| 17 | + /** A source for shell command constructed from library input vulnerabilities. */ |
| 18 | + abstract class Source extends DataFlow::Node { } |
| 19 | + |
| 20 | + /** An input parameter to a gem seen as a source. */ |
| 21 | + private class LibraryInputAsSource extends Source instanceof DataFlow::ParameterNode { |
| 22 | + LibraryInputAsSource() { |
| 23 | + none() // TODO: Do something here, put it in a shared library. |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + /** A sink for shell command constructed from library input vulnerabilities. */ |
| 28 | + abstract class Sink extends DataFlow::Node { |
| 29 | + /** Gets a description of how the string in this sink was constructed. */ |
| 30 | + abstract string describe(); |
| 31 | + |
| 32 | + /** Gets the dataflow node where the string is constructed. */ |
| 33 | + DataFlow::Node getStringConstruction() { result = this } |
| 34 | + |
| 35 | + /** Gets the dataflow node that executed the string as a shell command. */ |
| 36 | + abstract DataFlow::Node getCommandExecution(); |
| 37 | + } |
| 38 | + |
| 39 | + /** Holds if the string constructed at `source` is executed at `shellExec` */ |
| 40 | + predicate isUsedAsShellCommand(DataFlow::Node source, Concepts::SystemCommandExecution shellExec) { |
| 41 | + source = backtrackShellExec(TypeTracker::TypeBackTracker::end(), shellExec) |
| 42 | + } |
| 43 | + |
| 44 | + import semmle.python.dataflow.new.TypeTracker as TypeTracker |
| 45 | + |
| 46 | + private DataFlow::LocalSourceNode backtrackShellExec( |
| 47 | + TypeTracker::TypeBackTracker t, Concepts::SystemCommandExecution shellExec |
| 48 | + ) { |
| 49 | + t.start() and |
| 50 | + result = any(DataFlow::Node n | shellExec.isShellInterpreted(n)).getALocalSource() |
| 51 | + or |
| 52 | + exists(TypeTracker::TypeBackTracker t2 | |
| 53 | + result = backtrackShellExec(t2, shellExec).backtrack(t2, t) |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * A string constructed from a string-literal (e.g. `f'foo {sink}'`), |
| 59 | + * where the resulting string ends up being executed as a shell command. |
| 60 | + */ |
| 61 | + class StringInterpolationAsSink extends Sink { |
| 62 | + // TODO: Add test. |
| 63 | + Concepts::SystemCommandExecution s; |
| 64 | + Fstring fstring; |
| 65 | + |
| 66 | + StringInterpolationAsSink() { |
| 67 | + isUsedAsShellCommand(any(DataFlow::Node n | n.asExpr() = fstring), s) and |
| 68 | + this.asExpr() = fstring.getASubExpression() |
| 69 | + } |
| 70 | + |
| 71 | + override string describe() { result = "string construction" } |
| 72 | + |
| 73 | + override DataFlow::Node getCommandExecution() { result = s } |
| 74 | + |
| 75 | + override DataFlow::Node getStringConstruction() { result.asExpr() = fstring } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * A component of a string-concatenation (e.g. `"foo " + sink`), |
| 80 | + * where the resulting string ends up being executed as a shell command. |
| 81 | + */ |
| 82 | + class StringConcatAsSink extends Sink { |
| 83 | + // TODO: Add test. |
| 84 | + Concepts::SystemCommandExecution s; |
| 85 | + BinaryExpr add; |
| 86 | + |
| 87 | + StringConcatAsSink() { |
| 88 | + add.getOp() instanceof Add and |
| 89 | + isUsedAsShellCommand(any(DataFlow::Node n | n.asExpr() = add), s) and |
| 90 | + this.asExpr() = add.getASubExpression() |
| 91 | + } |
| 92 | + |
| 93 | + override DataFlow::Node getCommandExecution() { result = s } |
| 94 | + |
| 95 | + override string describe() { result = "string concatenation" } |
| 96 | + |
| 97 | + override DataFlow::Node getStringConstruction() { result.asExpr() = add } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * A string constructed using a `.join(" ")` call, where the resulting string ends up being executed as a shell command. |
| 102 | + */ |
| 103 | + class ArrayJoin extends Sink { |
| 104 | + // TODO: Add test. |
| 105 | + Concepts::SystemCommandExecution s; |
| 106 | + DataFlow::MethodCallNode call; |
| 107 | + |
| 108 | + ArrayJoin() { |
| 109 | + call.getMethodName() = "join" and |
| 110 | + unique( | | call.getArg(_)).asExpr().(Str).getText() = " " and |
| 111 | + isUsedAsShellCommand(call, s) and |
| 112 | + ( |
| 113 | + this = call.getObject() and |
| 114 | + not call.getObject().asExpr() instanceof List |
| 115 | + or |
| 116 | + this.asExpr() = call.getObject().asExpr().(List).getASubExpression() |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + override string describe() { result = "array" } |
| 121 | + |
| 122 | + override DataFlow::Node getCommandExecution() { result = s } |
| 123 | + |
| 124 | + override DataFlow::Node getStringConstruction() { result = call } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * A string constructed from a format call, |
| 129 | + * where the resulting string ends up being executed as a shell command. |
| 130 | + * Either a call to `.format(..)` or a string-interpolation with a `%` operator. |
| 131 | + */ |
| 132 | + class TaintedFormatStringAsSink extends Sink { |
| 133 | + // TODO: Test |
| 134 | + Concepts::SystemCommandExecution s; |
| 135 | + DataFlow::Node formatCall; |
| 136 | + |
| 137 | + TaintedFormatStringAsSink() { |
| 138 | + ( |
| 139 | + formatCall.asExpr().(BinaryExpr).getOp() instanceof Mod and |
| 140 | + this.asExpr() = formatCall.asExpr().(BinaryExpr).getASubExpression() |
| 141 | + or |
| 142 | + formatCall.(DataFlow::MethodCallNode).getMethodName() = "format" and |
| 143 | + this = |
| 144 | + [ |
| 145 | + formatCall.(DataFlow::MethodCallNode).getArg(_), |
| 146 | + formatCall.(DataFlow::MethodCallNode).getObject() |
| 147 | + ] |
| 148 | + ) and |
| 149 | + isUsedAsShellCommand(formatCall, s) |
| 150 | + } |
| 151 | + |
| 152 | + override string describe() { result = "formatted string" } |
| 153 | + |
| 154 | + override DataFlow::Node getCommandExecution() { result = s } |
| 155 | + |
| 156 | + override DataFlow::Node getStringConstruction() { result = formatCall } |
| 157 | + } |
| 158 | +} |
0 commit comments