|
| 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 codeql.ruby.DataFlow |
| 8 | +private import codeql.ruby.DataFlow2 |
| 9 | +private import codeql.ruby.ApiGraphs |
| 10 | +private import codeql.ruby.frameworks.core.Gem::Gem as Gem |
| 11 | +private import codeql.ruby.AST as Ast |
| 12 | +private import codeql.ruby.Concepts as Concepts |
| 13 | + |
| 14 | +/** |
| 15 | + * Module containing sources, sinks, and sanitizers for shell command constructed from library input. |
| 16 | + */ |
| 17 | +module UnsafeShellCommandConstruction { |
| 18 | + /** A source for shell command constructed from library input vulnerabilities. */ |
| 19 | + abstract class Source extends DataFlow::Node { } |
| 20 | + |
| 21 | + /** An input parameter to a gem seen as a source. */ |
| 22 | + private class LibraryInputAsSource extends Source instanceof DataFlow::ParameterNode { |
| 23 | + LibraryInputAsSource() { |
| 24 | + this = Gem::getALibraryInput() and |
| 25 | + // we exclude arguments named `cmd` or similar, as they seem to execute commands on purpose |
| 26 | + not exists(string name | name = super.getName() | |
| 27 | + name = ["cmd", "command"] |
| 28 | + or |
| 29 | + name.regexpMatch(".*(Cmd|Command)$") |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + /** A sink for shell command constructed from library input vulnerabilities. */ |
| 35 | + abstract class Sink extends DataFlow::Node { |
| 36 | + /** Gets a description of how the string in this sink was constructed. */ |
| 37 | + abstract string describe(); |
| 38 | + |
| 39 | + /** Gets the dataflow node where the string is constructed. */ |
| 40 | + DataFlow::Node getStringConstruction() { result = this } |
| 41 | + |
| 42 | + /** Gets the dataflow node that executed the string as a shell command. */ |
| 43 | + abstract DataFlow::Node getCommandExecution(); |
| 44 | + } |
| 45 | + |
| 46 | + /** Holds if the string constructed at `source` is executed at `shellExec` */ |
| 47 | + predicate isUsedAsShellCommand(DataFlow::Node source, Concepts::SystemCommandExecution shellExec) { |
| 48 | + source = backtrackShellExec(TypeTracker::TypeBackTracker::end(), shellExec) |
| 49 | + } |
| 50 | + |
| 51 | + import codeql.ruby.typetracking.TypeTracker as TypeTracker |
| 52 | + |
| 53 | + private DataFlow::LocalSourceNode backtrackShellExec( |
| 54 | + TypeTracker::TypeBackTracker t, Concepts::SystemCommandExecution shellExec |
| 55 | + ) { |
| 56 | + t.start() and |
| 57 | + result = any(DataFlow::Node n | shellExec.isShellInterpreted(n)).getALocalSource() |
| 58 | + or |
| 59 | + exists(TypeTracker::TypeBackTracker t2 | |
| 60 | + result = backtrackShellExec(t2, shellExec).backtrack(t2, t) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * A string constructed from a string-literal (e.g. `"foo #{sink}"`), |
| 66 | + * where the resulting string ends up being executed as a shell command. |
| 67 | + */ |
| 68 | + class StringInterpolationAsSink extends Sink { |
| 69 | + Concepts::SystemCommandExecution s; |
| 70 | + Ast::StringLiteral lit; |
| 71 | + |
| 72 | + StringInterpolationAsSink() { |
| 73 | + isUsedAsShellCommand(any(DataFlow::Node n | n.asExpr().getExpr() = lit), s) and |
| 74 | + this.asExpr().getExpr() = lit.getComponent(_) |
| 75 | + } |
| 76 | + |
| 77 | + override string describe() { result = "string construction" } |
| 78 | + |
| 79 | + override DataFlow::Node getCommandExecution() { result = s } |
| 80 | + |
| 81 | + override DataFlow::Node getStringConstruction() { result.asExpr().getExpr() = lit } |
| 82 | + } |
| 83 | + |
| 84 | + import codeql.ruby.security.TaintedFormatStringSpecific as TaintedFormat |
| 85 | + |
| 86 | + /** |
| 87 | + * A string constructed from a printf-style call, |
| 88 | + * where the resulting string ends up being executed as a shell command. |
| 89 | + */ |
| 90 | + class TaintedFormatStringAsSink extends Sink { |
| 91 | + Concepts::SystemCommandExecution s; |
| 92 | + TaintedFormat::PrintfStyleCall call; |
| 93 | + |
| 94 | + TaintedFormatStringAsSink() { |
| 95 | + isUsedAsShellCommand(call, s) and |
| 96 | + this = [call.getFormatArgument(_), call.getFormatString()] |
| 97 | + } |
| 98 | + |
| 99 | + override string describe() { result = "formatted string" } |
| 100 | + |
| 101 | + override DataFlow::Node getCommandExecution() { result = s } |
| 102 | + |
| 103 | + override DataFlow::Node getStringConstruction() { result = call } |
| 104 | + } |
| 105 | +} |
0 commit comments