|
| 1 | +private import codeql.ruby.AST |
| 2 | +private import codeql.ruby.Concepts |
| 3 | +private import codeql.ruby.DataFlow |
| 4 | +private import codeql.ruby.ApiGraphs |
| 5 | +private import codeql.ruby.dataflow.internal.DataFlowDispatch |
| 6 | +private import codeql.ruby.dataflow.internal.DataFlowImplCommon |
| 7 | + |
| 8 | +/** |
| 9 | + * A system command executed via subshell literal syntax. |
| 10 | + * E.g. |
| 11 | + * ```ruby |
| 12 | + * `cat foo.txt` |
| 13 | + * %x(cat foo.txt) |
| 14 | + * %x[cat foo.txt] |
| 15 | + * %x{cat foo.txt} |
| 16 | + * %x/cat foo.txt/ |
| 17 | + * ``` |
| 18 | + */ |
| 19 | +class SubshellLiteralExecution extends SystemCommandExecution::Range { |
| 20 | + SubshellLiteral literal; |
| 21 | + |
| 22 | + SubshellLiteralExecution() { this.asExpr().getExpr() = literal } |
| 23 | + |
| 24 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = literal.getComponent(_) } |
| 25 | + |
| 26 | + override predicate isShellInterpreted(DataFlow::Node arg) { arg = getAnArgument() } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * A system command executed via the `Kernel.system` method. |
| 31 | + * `Kernel.system` accepts three argument forms: |
| 32 | + * - A single string. If it contains no shell meta characters, keywords or builtins, it is executed directly in a subprocess. |
| 33 | + * Otherwise, it is executed in a subshell. |
| 34 | + * ```ruby |
| 35 | + * system("cat foo.txt | tail") |
| 36 | + * ``` |
| 37 | + * - A command and one or more arguments. |
| 38 | + * The command is executed in a subprocess. |
| 39 | + * ```ruby |
| 40 | + * system("cat", "foo.txt") |
| 41 | + * ``` |
| 42 | + * - An array containing the command name and argv[0], followed by zero or more arguments. |
| 43 | + * The command is executed in a subprocess. |
| 44 | + * ```ruby |
| 45 | + * system(["cat", "cat"], "foo.txt") |
| 46 | + * ``` |
| 47 | + * In addition, `Kernel.system` accepts an optional environment hash as the first argument and and optional options hash as the last argument. |
| 48 | + * We don't yet distinguish between these arguments and the command arguments. |
| 49 | + * ```ruby |
| 50 | + * system({"FOO" => "BAR"}, "cat foo.txt | tail", {unsetenv_others: true}) |
| 51 | + * ``` |
| 52 | + * Ruby documentation: https://docs.ruby-lang.org/en/3.0.0/Kernel.html#method-i-system |
| 53 | + */ |
| 54 | +class KernelSystemCall extends SystemCommandExecution::Range { |
| 55 | + MethodCall methodCall; |
| 56 | + |
| 57 | + KernelSystemCall() { |
| 58 | + methodCall.getMethodName() = "system" and |
| 59 | + this.asExpr().getExpr() = methodCall and |
| 60 | + // `Kernel.system` can be reached via `Kernel.system` or just `system` |
| 61 | + // (if there's no other method by the same name in scope). |
| 62 | + ( |
| 63 | + this = API::getTopLevelMember("Kernel").getAMethodCall("system") |
| 64 | + or |
| 65 | + // we assume that if there's no obvious target for this method call, then it must refer to Kernel.system. |
| 66 | + not exists(DataFlowCallable method, DataFlowCall call | |
| 67 | + viableCallable(call) = method and call.getExpr() = methodCall |
| 68 | + ) |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 73 | + |
| 74 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 75 | + // Kernel.system invokes a subshell if you provide a single string as argument |
| 76 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * A system command executed via the `Kernel.exec` method. |
| 82 | + * `Kernel.exec` takes the same argument forms as `Kernel.system`. See `KernelSystemCall` for details. |
| 83 | + * Ruby documentation: https://docs.ruby-lang.org/en/3.0.0/Kernel.html#method-i-exec |
| 84 | + */ |
| 85 | +class KernelExecCall extends SystemCommandExecution::Range { |
| 86 | + MethodCall methodCall; |
| 87 | + |
| 88 | + KernelExecCall() { |
| 89 | + methodCall.getMethodName() = "exec" and |
| 90 | + this.asExpr().getExpr() = methodCall and |
| 91 | + // `Kernel.exec` can be reached via `Kernel.exec`, `Process.exec` or just `exec` |
| 92 | + // (if there's no other method by the same name in scope). |
| 93 | + ( |
| 94 | + this = API::getTopLevelMember("Kernel").getAMethodCall("exec") |
| 95 | + or |
| 96 | + this = API::getTopLevelMember("Process").getAMethodCall("exec") |
| 97 | + or |
| 98 | + // we assume that if there's no obvious target for this method call, then it must refer to Kernel.exec. |
| 99 | + not exists(DataFlowCallable method, DataFlowCall call | |
| 100 | + viableCallable(call) = method and call.getExpr() = methodCall |
| 101 | + ) |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 106 | + |
| 107 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 108 | + // Kernel.exec invokes a subshell if you provide a single string as argument |
| 109 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * A system command executed via the `Kernel.spawn` method. |
| 115 | + * `Kernel.spawn` takes the same argument forms as `Kernel.system`. See `KernelSystemCall` for details. |
| 116 | + * Ruby documentation: https://docs.ruby-lang.org/en/3.0.0/Kernel.html#method-i-spawn |
| 117 | + * TODO: document and handle the env and option arguments. |
| 118 | + * ``` |
| 119 | + * spawn([env,] command... [,options]) → pid |
| 120 | + * ``` |
| 121 | + */ |
| 122 | +class KernelSpawnCall extends SystemCommandExecution::Range { |
| 123 | + MethodCall methodCall; |
| 124 | + |
| 125 | + KernelSpawnCall() { |
| 126 | + methodCall.getMethodName() = "spawn" and |
| 127 | + this.asExpr().getExpr() = methodCall and |
| 128 | + // `Kernel.spawn` can be reached via `Kernel.spawn`, `Process.spawn` or just `spawn` |
| 129 | + // (if there's no other method by the same name in scope). |
| 130 | + ( |
| 131 | + this = API::getTopLevelMember("Kernel").getAMethodCall("spawn") |
| 132 | + or |
| 133 | + this = API::getTopLevelMember("Process").getAMethodCall("spawn") |
| 134 | + or |
| 135 | + not exists(DataFlowCallable method, DataFlowCall call | |
| 136 | + viableCallable(call) = method and call.getExpr() = methodCall |
| 137 | + ) |
| 138 | + ) |
| 139 | + } |
| 140 | + |
| 141 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 142 | + |
| 143 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 144 | + // Kernel.spawn invokes a subshell if you provide a single string as argument |
| 145 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +class Open3Call extends SystemCommandExecution::Range { |
| 150 | + MethodCall methodCall; |
| 151 | + |
| 152 | + Open3Call() { |
| 153 | + this.asExpr().getExpr() = methodCall and |
| 154 | + exists(string methodName | |
| 155 | + methodName in [ |
| 156 | + "popen3", "popen2", "popen2e", "capture3", "capture2", "capture2e", "pipeline_rw", |
| 157 | + "pipeline_r", "pipeline_w", "pipeline_start", "pipeline" |
| 158 | + ] and |
| 159 | + this = API::getTopLevelMember("Open3").getAMethodCall(methodName) |
| 160 | + ) |
| 161 | + } |
| 162 | + |
| 163 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 164 | + |
| 165 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 166 | + // These Open3 methods invoke a subshell if you provide a single string as argument |
| 167 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 168 | + } |
| 169 | +} |
0 commit comments