Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* Fixed false positives in the command injection queries when untrusted data is passed as positional parameters (`$0`, `$1`, ...) to `sh -c` or `bash -c` rather than as the command string itself.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
import javascript

/**
* Holds if `shell arg <cmd>` runs `<cmd>` as a shell command.
*
* That is, either `shell` is a Unix shell (`sh` or similar) and
* `arg` is `"-c"`, or `shell` is `cmd.exe` and `arg` is `"/c"`.
* Holds if `shell arg <cmd>` runs `<cmd>` as a shell command, where `isUnix` indicates whether
* `shell` is a POSIX-style shell (`sh` or `bash`) rather than Windows `cmd.exe`.
*/
private predicate shellCmd(Expr shell, string arg) {
private predicate shellCmd(Expr shell, string arg, boolean isUnix) {
exists(string s | s = shell.getStringValue() |
s = ["sh", "bash", "/bin/sh", "/bin/bash"] and
arg = "-c"
arg = "-c" and
isUnix = true
)
or
exists(string s | s = shell.getStringValue().toLowerCase() |
s = ["cmd", "cmd.exe"] and
arg = ["/c", "/C"]
arg = ["/c", "/C"] and
isUnix = false
)
}

Expand Down Expand Up @@ -84,9 +84,160 @@
}

/**
* Holds if `source` contributes to the arguments of an indirect command execution `sys`.
* Gets a data-flow node whose elements form a prefix of the argument list in `sys`
* after a flow summarized by `t`.
*
* Unlike `argumentList`, this only steps through value-preserving flow and `.concat(...)`
* receivers, preserving element indices from index 0 up to the array's length.
*/
private DataFlow::SourceNode argumentListPrefix(
SystemCommandExecution sys, DataFlow::TypeBackTracker t
) {
t.start() and
result = sys.getArgumentList().getALocalSource()
or
exists(DataFlow::TypeBackTracker t2, DataFlow::SourceNode pred |
pred = argumentListPrefix(sys, t2)
|
result = pred.backtrack(t2, t)
or
t = t2.continue() and
pred.(DataFlow::MethodCallNode).getMethodName() = "concat" and
result = pred.(DataFlow::MethodCallNode).getReceiver().getALocalSource()
Comment on lines +102 to +106
)
}

/**
* Gets a data-flow node whose elements form a prefix of the argument list in `sys`.
*/
private DataFlow::SourceNode argumentListPrefix(SystemCommandExecution sys) {
result = argumentListPrefix(sys, DataFlow::TypeBackTracker::end())
}

/**
* Holds if `args` has no mutating operations that could shift or overwrite prefix indices
* dynamically.
*/
private predicate hasUnmodifiedPrefix(DataFlow::ArrayCreationNode args) {
not exists(
args.getAMethodCall([
"pop", "unshift", "shift", "splice", "reverse", "sort", "copyWithin", "fill"
])
) and
not exists(args.getAPropertyWrite("length")) and
not exists(DataFlow::PropWrite pw | pw = args.getAPropertyWrite() |
exists(pw.getPropertyNameExpr()) and
not exists(pw.getPropertyName())
)
}

/**
* Gets a data-flow node written to non-negative integer index `i` of `args`, provided no spread
* element precedes or occupies index `i`.
*/
private DataFlow::Node arrayElementAt(DataFlow::ArrayCreationNode args, int i) {
args = argumentListPrefix(_) and
not exists(int j | j in [0 .. i] | args.getElement(j).asExpr() instanceof SpreadElement) and
(
result = args.getElement(i)
or
exists(string prop |
result = args.getAPropertyWrite(prop).getRhs() and
i = prop.toInt() and
i >= 0 and
prop = i.toString()
)
)
}

/**
* Gets the unique string value that element `i` of `args` may have, if all writes to index `i`
* resolve to that single string constant.
*/
private string uniqueElementString(DataFlow::ArrayCreationNode args, int i) {
forex(DataFlow::Node elem | elem = arrayElementAt(args, i) |
exists(string s | elem.mayHaveStringValue(s))
) and
result = unique(string s | arrayElementAt(args, i).mayHaveStringValue(s))
}
Comment on lines +157 to +162

/**
* Gets the number of subsequent arguments consumed as option-arguments by Unix shell option
* token `opt` (for example `1` for `-o`, `-euo`, `-oe`, or `--rcfile`).
*/
bindingset[opt]
private int shellOptionArgCount(string opt) {
if opt = ["--rcfile", "--init-file"]
then result = 1
else
if opt.regexpMatch("[-+][a-zA-Z]+")
then result = count(int k | opt.charAt(k) = ["o", "O"])
else result = 0
}

/**
* Holds if `i` is the index of a shell option or the first non-option operand in `args`.
*/
private predicate unixOptionScanIndex(DataFlow::ArrayCreationNode args, int i) {
args = argumentListPrefix(_) and
hasUnmodifiedPrefix(args) and
exists(arrayElementAt(args, 0)) and
i = 0
or
exists(int prev, string opt, int argCount |
unixOptionScanIndex(args, prev) and
opt = uniqueElementString(args, prev) and
opt.charAt(0) = ["-", "+"] and
not opt = ["-", "--"] and
argCount = shellOptionArgCount(opt) and
forall(int k | k in [1 .. argCount] | exists(arrayElementAt(args, prev + k))) and
i = prev + 1 + argCount
) and
exists(arrayElementAt(args, i))
}

/**
* Gets the index of the `command_string` operand for a Unix `sh -c` invocation using `args`,
* if it can be determined unambiguously from `args`.
*/
private int unixCommandStringIndex(DataFlow::ArrayCreationNode args) {
exists(int cIdx |
unixOptionScanIndex(args, cIdx) and
uniqueElementString(args, cIdx).regexpMatch("-[a-zA-Z]*c[a-zA-Z]*")
) and
(
exists(int endOpts |
unixOptionScanIndex(args, endOpts) and
uniqueElementString(args, endOpts) = ["-", "--"] and
result = endOpts + 1 and
exists(arrayElementAt(args, result))
)
or
unixOptionScanIndex(args, result) and
not any(string s | arrayElementAt(args, result).mayHaveStringValue(s)).charAt(0) = ["-", "+"]
)
}

/**
* Holds if every origin of the prefix of `sys`'s argument list is an array creation with a
* known Unix `-c` command-string index.
*/
private predicate prefixFixesUnixCommand(SystemCommandExecution sys) {
forex(DataFlow::SourceNode leaf |
leaf = argumentListPrefix(sys) and
not leaf.(DataFlow::MethodCallNode).getMethodName() = "concat" and
not exists(DataFlow::TypeBackTracker t0 | t0.start() and exists(leaf.backtrack(t0, _)))
|
exists(unixCommandStringIndex(leaf))
)
}

/**
* Holds if `source` contributes to the command executed by an indirect command execution `sys`.
*
* An indirect command execution is a system execution command that starts with `sh -c`, `cmd.exe /c`, or similar.
* For POSIX shells (`sh -c`, `bash -c`), only the `command_string` operand is interpreted as shell code when its
* position can be determined; subsequent arguments (`$0`, `$1`, ...) are positional parameters rather than commands.
*
* For example, `getCommand()` is `source`, and the call to `childProcess.spawn` is `sys` in the following example:
*
Expand All @@ -103,15 +254,21 @@
* ```
*/
predicate isIndirectCommandArgument(DataFlow::Node source, SystemCommandExecution sys) {
exists(DataFlow::ArrayCreationNode args, DataFlow::Node shell, string dashC |
shellCmd(shell.asExpr(), dashC) and
exists(DataFlow::ArrayCreationNode args, DataFlow::Node shell, string dashC, boolean isUnix |
shellCmd(shell.asExpr(), dashC, isUnix) and
shell = commandArgument(sys) and
args = argumentList(sys) and
argumentListElement(args).mayHaveStringValue(dashC) and
exists(DataFlow::SourceNode argsSource | argsSource = argumentList(sys) |
if exists(argsSource.getAPropertyWrite())
then source = argsSource.getAPropertyWrite().getRhs()
else source = argsSource
argumentListElement(args).mayHaveStringValue(dashC)
|
if isUnix = true and args = argumentListPrefix(sys) and exists(unixCommandStringIndex(args))
then source = arrayElementAt(args, unixCommandStringIndex(args))
Comment on lines +263 to +264
Comment on lines +263 to +264
Comment on lines +263 to +264
else (
not (isUnix = true and not args = argumentListPrefix(sys) and prefixFixesUnixCommand(sys)) and
exists(DataFlow::SourceNode argsSource | argsSource = argumentList(sys) |
if exists(argsSource.getAPropertyWrite())
then source = argsSource.getAPropertyWrite().getRhs()
else source = argsSource
)
)
)
}
Loading
Loading