|
| 1 | +/** |
| 2 | + * Provides modeling for the `posix-spawn` gem. |
| 3 | + * Version: 0.3.15 |
| 4 | + */ |
| 5 | + |
| 6 | +private import codeql.ruby.Concepts |
| 7 | +private import codeql.ruby.ApiGraphs |
| 8 | +private import codeql.ruby.DataFlow |
| 9 | +private import codeql.ruby.controlflow.CfgNodes |
| 10 | + |
| 11 | +/** |
| 12 | + * Provides modeling for the `posix-spawn` gem. |
| 13 | + * Version: 0.3.15 |
| 14 | + */ |
| 15 | +module PosixSpawn { |
| 16 | + private API::Node posixSpawnModule() { |
| 17 | + result = API::getTopLevelMember("POSIX").getMember("Spawn") |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * A call to `POSIX::Spawn::Child.new` or `POSIX::Spawn::Child.build`. |
| 22 | + */ |
| 23 | + class ChildCall extends SystemCommandExecution::Range, DataFlow::CallNode { |
| 24 | + ChildCall() { |
| 25 | + this = |
| 26 | + [ |
| 27 | + posixSpawnModule().getMember("Child").getAMethodCall("build"), |
| 28 | + posixSpawnModule().getMember("Child").getAnInstantiation() |
| 29 | + ] |
| 30 | + } |
| 31 | + |
| 32 | + override DataFlow::Node getAnArgument() { |
| 33 | + result = this.getArgument(_) and not result.asExpr() instanceof ExprNodes::PairCfgNode |
| 34 | + } |
| 35 | + |
| 36 | + override predicate isShellInterpreted(DataFlow::Node arg) { none() } |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * A call to `POSIX::Spawn.spawn` or a related method. |
| 41 | + */ |
| 42 | + class SystemCall extends SystemCommandExecution::Range, DataFlow::CallNode { |
| 43 | + SystemCall() { |
| 44 | + exists(API::Node spawn | spawn = API::getTopLevelMember("POSIX").getMember("Spawn") | |
| 45 | + this = |
| 46 | + posixSpawnModule() |
| 47 | + .getAMethodCall(["spawn", "fspawn", "popen4", "pspawn", "system", "_pspawn", "`"]) |
| 48 | + ) |
| 49 | + } |
| 50 | + |
| 51 | + override DataFlow::Node getAnArgument() { this.argument(result, _) } |
| 52 | + |
| 53 | + // From the docs: |
| 54 | + // When only command is given and includes a space character, the command |
| 55 | + // text is executed by the system shell interpreter. |
| 56 | + // This means the following signatures are shell interpreted: |
| 57 | + // |
| 58 | + // spawn(cmd) |
| 59 | + // spawn(cmd, opts) |
| 60 | + // spawn(env, cmd) |
| 61 | + // spawn(env, cmd, opts) |
| 62 | + // |
| 63 | + // env and opts will be hashes. We over-approximate by assuming the argument |
| 64 | + // is shell interpreted unless there is another argument with a string |
| 65 | + // constant value. |
| 66 | + override predicate isShellInterpreted(DataFlow::Node arg) { |
| 67 | + exists(int n, int m, DataFlow::Node otherArg | |
| 68 | + this.argument(arg, n) and |
| 69 | + this.argument(otherArg, m) and |
| 70 | + otherArg.asExpr().getConstantValue().isString(_) |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + private predicate argument(DataFlow::Node arg, int n) { |
| 75 | + arg = this.getArgument(n) and |
| 76 | + not arg.asExpr() instanceof ExprNodes::HashLiteralCfgNode and |
| 77 | + not arg.asExpr() instanceof ExprNodes::ArrayLiteralCfgNode and |
| 78 | + not arg.asExpr() instanceof ExprNodes::PairCfgNode |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments