|
| 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 | + * The `Kernel` module is included by the `Object` class, so its methods are available |
| 10 | + * in every Ruby object. In addition, its module methods can be called by |
| 11 | + * providing a specific receiver as in `Kernel.exit`. |
| 12 | + */ |
| 13 | +class KernelMethodCall extends MethodCall { |
| 14 | + KernelMethodCall() { |
| 15 | + this = API::getTopLevelMember("Kernel").getAMethodCall(_).asExpr().getExpr() |
| 16 | + or |
| 17 | + // we assume that if there's no obvious target for this method call |
| 18 | + // and the method name matches a Kernel method, then it is a Kernel method call. |
| 19 | + // TODO: ApiGraphs should ideally handle this case |
| 20 | + not exists(DataFlowCallable method, DataFlowCall call | |
| 21 | + viableCallable(call) = method and call.getExpr() = this |
| 22 | + ) and |
| 23 | + ( |
| 24 | + this.getReceiver() instanceof Self and isPrivateKernelMethod(this.getMethodName()) |
| 25 | + or |
| 26 | + isPublicKernelMethod(this.getMethodName()) |
| 27 | + ) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Public methods in the `Kernel` module. These can be invoked on any object via the usual dot syntax. |
| 33 | + * ```ruby |
| 34 | + * arr = [] |
| 35 | + * arr.send("push", 5) # => [5] |
| 36 | + * ``` |
| 37 | + */ |
| 38 | +private predicate isPublicKernelMethod(string method) { |
| 39 | + method in ["class", "clone", "frozen?", "tap", "then", "yield_self", "send"] |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Private methods in the `Kernel` module. |
| 44 | + * These can be be invoked on `self`, on `Kernel`, or using a low-level primitive like `send` or `instance_eval`. |
| 45 | + * ```ruby |
| 46 | + * puts "hello world" |
| 47 | + * Kernel.puts "hello world" |
| 48 | + * 5.instance_eval { puts "hello world" } |
| 49 | + * 5.send("puts", "hello world") |
| 50 | + * ``` |
| 51 | + */ |
| 52 | +private predicate isPrivateKernelMethod(string method) { |
| 53 | + method in [ |
| 54 | + "Array", "Complex", "Float", "Hash", "Integer", "Rational", "String", "__callee__", "__dir__", |
| 55 | + "__method__", "`", "abort", "at_exit", "autoload", "autoload?", "binding", "block_given?", |
| 56 | + "callcc", "caller", "caller_locations", "catch", "chomp", "chop", "eval", "exec", "exit", |
| 57 | + "exit!", "fail", "fork", "format", "gets", "global_variables", "gsub", "iterator?", "lambda", |
| 58 | + "load", "local_variables", "loop", "open", "p", "pp", "print", "printf", "proc", "putc", |
| 59 | + "puts", "raise", "rand", "readline", "readlines", "require", "require_relative", "select", |
| 60 | + "set_trace_func", "sleep", "spawn", "sprintf", "srand", "sub", "syscall", "system", "test", |
| 61 | + "throw", "trace_var", "trap", "untrace_var", "warn" |
| 62 | + ] |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * A system command executed via subshell literal syntax. |
| 67 | + * E.g. |
| 68 | + * ```ruby |
| 69 | + * `cat foo.txt` |
| 70 | + * %x(cat foo.txt) |
| 71 | + * %x[cat foo.txt] |
| 72 | + * %x{cat foo.txt} |
| 73 | + * %x/cat foo.txt/ |
| 74 | + * ``` |
| 75 | + */ |
| 76 | +class SubshellLiteralExecution extends SystemCommandExecution::Range { |
| 77 | + SubshellLiteral literal; |
| 78 | + |
| 79 | + SubshellLiteralExecution() { this.asExpr().getExpr() = literal } |
| 80 | + |
| 81 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = literal.getComponent(_) } |
| 82 | + |
| 83 | + override predicate isShellInterpreted(DataFlow::Node arg) { arg = getAnArgument() } |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * A system command executed via shell heredoc syntax. |
| 88 | + * E.g. |
| 89 | + * ```ruby |
| 90 | + * <<`EOF` |
| 91 | + * cat foo.text |
| 92 | + * EOF |
| 93 | + * ``` |
| 94 | + */ |
| 95 | +class SubshellHeredocExecution extends SystemCommandExecution::Range { |
| 96 | + HereDoc heredoc; |
| 97 | + |
| 98 | + SubshellHeredocExecution() { this.asExpr().getExpr() = heredoc and heredoc.isSubShell() } |
| 99 | + |
| 100 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = heredoc.getComponent(_) } |
| 101 | + |
| 102 | + override predicate isShellInterpreted(DataFlow::Node arg) { arg = getAnArgument() } |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * A system command executed via the `Kernel.system` method. |
| 107 | + * `Kernel.system` accepts three argument forms: |
| 108 | + * - A single string. If it contains no shell meta characters, keywords or |
| 109 | + * builtins, it is executed directly in a subprocess. |
| 110 | + * Otherwise, it is executed in a subshell. |
| 111 | + * ```ruby |
| 112 | + * system("cat foo.txt | tail") |
| 113 | + * ``` |
| 114 | + * - A command and one or more arguments. |
| 115 | + * The command is executed in a subprocess. |
| 116 | + * ```ruby |
| 117 | + * system("cat", "foo.txt") |
| 118 | + * ``` |
| 119 | + * - An array containing the command name and argv[0], followed by zero or more arguments. |
| 120 | + * The command is executed in a subprocess. |
| 121 | + * ```ruby |
| 122 | + * system(["cat", "cat"], "foo.txt") |
| 123 | + * ``` |
| 124 | + * In addition, `Kernel.system` accepts an optional environment hash as the |
| 125 | + * first argument and an optional options hash as the last argument. |
| 126 | + * We don't yet distinguish between these arguments and the command arguments. |
| 127 | + * ```ruby |
| 128 | + * system({"FOO" => "BAR"}, "cat foo.txt | tail", {unsetenv_others: true}) |
| 129 | + * ``` |
| 130 | + * Ruby documentation: https://docs.ruby-lang.org/en/3.0.0/Kernel.html#method-i-system |
| 131 | + */ |
| 132 | +class KernelSystemCall extends SystemCommandExecution::Range { |
| 133 | + KernelMethodCall methodCall; |
| 134 | + |
| 135 | + KernelSystemCall() { |
| 136 | + methodCall.getMethodName() = "system" and |
| 137 | + this.asExpr().getExpr() = methodCall |
| 138 | + } |
| 139 | + |
| 140 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 141 | + |
| 142 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 143 | + // Kernel.system invokes a subshell if you provide a single string as argument |
| 144 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +/** |
| 149 | + * A system command executed via the `Kernel.exec` method. |
| 150 | + * `Kernel.exec` takes the same argument forms as `Kernel.system`. See `KernelSystemCall` for details. |
| 151 | + * Ruby documentation: https://docs.ruby-lang.org/en/3.0.0/Kernel.html#method-i-exec |
| 152 | + */ |
| 153 | +class KernelExecCall extends SystemCommandExecution::Range { |
| 154 | + KernelMethodCall methodCall; |
| 155 | + |
| 156 | + KernelExecCall() { |
| 157 | + methodCall.getMethodName() = "exec" and |
| 158 | + this.asExpr().getExpr() = methodCall |
| 159 | + } |
| 160 | + |
| 161 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 162 | + |
| 163 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 164 | + // Kernel.exec invokes a subshell if you provide a single string as argument |
| 165 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * A system command executed via the `Kernel.spawn` method. |
| 171 | + * `Kernel.spawn` takes the same argument forms as `Kernel.system`. |
| 172 | + * See `KernelSystemCall` for details. |
| 173 | + * Ruby documentation: https://docs.ruby-lang.org/en/3.0.0/Kernel.html#method-i-spawn |
| 174 | + * TODO: document and handle the env and option arguments. |
| 175 | + * ``` |
| 176 | + * spawn([env,] command... [,options]) → pid |
| 177 | + * ``` |
| 178 | + */ |
| 179 | +class KernelSpawnCall extends SystemCommandExecution::Range { |
| 180 | + KernelMethodCall methodCall; |
| 181 | + |
| 182 | + KernelSpawnCall() { |
| 183 | + methodCall.getMethodName() = "spawn" and |
| 184 | + this.asExpr().getExpr() = methodCall |
| 185 | + } |
| 186 | + |
| 187 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 188 | + |
| 189 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 190 | + // Kernel.spawn invokes a subshell if you provide a single string as argument |
| 191 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +/** |
| 196 | + * A system command executed via one of the `Open3` methods. |
| 197 | + * These methods take the same argument forms as `Kernel.system`. |
| 198 | + * See `KernelSystemCall` for details. |
| 199 | + */ |
| 200 | +class Open3Call extends SystemCommandExecution::Range { |
| 201 | + MethodCall methodCall; |
| 202 | + |
| 203 | + Open3Call() { |
| 204 | + this.asExpr().getExpr() = methodCall and |
| 205 | + this = |
| 206 | + API::getTopLevelMember("Open3") |
| 207 | + .getAMethodCall(["popen3", "popen2", "popen2e", "capture3", "capture2", "capture2e"]) |
| 208 | + } |
| 209 | + |
| 210 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 211 | + |
| 212 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 213 | + // These Open3 methods invoke a subshell if you provide a single string as argument |
| 214 | + methodCall.getNumberOfArguments() = 1 and arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 215 | + } |
| 216 | +} |
| 217 | + |
| 218 | +/** |
| 219 | + * A pipeline of system commands constructed via one of the `Open3` methods. |
| 220 | + * These methods accept a variable argument list of commands. |
| 221 | + * Commands can be in any form supported by `Kernel.system`. See `KernelSystemCall` for details. |
| 222 | + * ```ruby |
| 223 | + * Open3.pipeline("cat foo.txt", "tail") |
| 224 | + * Open3.pipeline(["cat", "foo.txt"], "tail") |
| 225 | + * Open3.pipeline([{}, "cat", "foo.txt"], "tail") |
| 226 | + * Open3.pipeline([["cat", "cat"], "foo.txt"], "tail") |
| 227 | + */ |
| 228 | +class Open3PipelineCall extends SystemCommandExecution::Range { |
| 229 | + MethodCall methodCall; |
| 230 | + |
| 231 | + Open3PipelineCall() { |
| 232 | + this.asExpr().getExpr() = methodCall and |
| 233 | + this = |
| 234 | + API::getTopLevelMember("Open3") |
| 235 | + .getAMethodCall(["pipeline_rw", "pipeline_r", "pipeline_w", "pipeline_start", "pipeline"]) |
| 236 | + } |
| 237 | + |
| 238 | + override DataFlow::Node getAnArgument() { result.asExpr().getExpr() = methodCall.getAnArgument() } |
| 239 | + |
| 240 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 241 | + // A command in the pipeline is executed in a subshell if it is given as a single string argument. |
| 242 | + arg.asExpr().getExpr() instanceof StringlikeLiteral and |
| 243 | + arg.asExpr().getExpr() = methodCall.getAnArgument() |
| 244 | + } |
| 245 | +} |
0 commit comments