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
---
* The `isIndirectCommandArgument` predicate now recognizes shell commands launched through `env` or `nice`.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
result = argumentList(sys, DataFlow::TypeBackTracker::end())
}

/** Gets an argument list without following array transformations that can change positions. */
private DataFlow::SourceNode positionalArgumentList(
SystemCommandExecution sys, DataFlow::TypeBackTracker t
) {
t.start() and
result = sys.getArgumentList().getALocalSource()
or
exists(DataFlow::TypeBackTracker t2 | result = positionalArgumentList(sys, t2).backtrack(t2, t))
}

/**
* Gets a data-flow node whose value ends up being interpreted as an element of the argument list
* `args` after a flow summarised by `t`.
Expand All @@ -83,10 +93,244 @@
result = argumentListElement(args, DataFlow::TypeBackTracker::end())
}

/** Holds if the first `n` elements are recognized `env` options. */
private predicate envOptions(DataFlow::ArrayCreationNode args, int n) {
n = 0 and args.getSize() >= 0
or
envOptions(args, n - 1) and
args.getElement(n - 1).getALocalSource().getStringValue() = ["-i", "--ignore-environment"]
or
envOptions(args, n - 1) and
args.getElement(n - 1).getALocalSource().getStringValue().regexpMatch("--unset=.+")
or
envOptions(args, n - 2) and
args.getElement(n - 2).getALocalSource().getStringValue() = ["-u", "--unset"] and
exists(args.getElement(n - 1)) and
not args.getElement(n - 1).asExpr() instanceof SpreadElement
}

/** Holds if `element` is an environment assignment, possibly with a dynamic value. */
private predicate isEnvAssignment(DataFlow::Node element) {
exists(string value |
value = element.getStringValue() and
value.regexpMatch("[A-Za-z_][A-Za-z0-9_]*=.*")
)
or
exists(AddExpr add |
add = element.asExpr() and
add.getLeftOperand().getStringValue().regexpMatch("[A-Za-z_][A-Za-z0-9_]*=")
)
or
exists(DataFlow::Node prefix |
prefix = StringConcatenation::getFirstOperand(element) and
prefix.getStringValue().regexpMatch("[A-Za-z_][A-Za-z0-9_]*=")
)
Comment on lines +114 to +127
}

/** Holds if the first `n` elements are recognized `env` options, delimiter, and assignments. */
private predicate envPrefix(DataFlow::ArrayCreationNode args, int n) {
envOptions(args, n)
or
envOptions(args, n - 1) and
args.getElement(n - 1).getALocalSource().getStringValue() = ["--", "-"]
or
envOptions(args, n - 1) and
(
isEnvAssignment(args.getElement(n - 1))
or
isEnvAssignment(args.getElement(n - 1).getALocalSource())
)
or
envPrefix(args, n - 1) and
(
isEnvAssignment(args.getElement(n - 1))
or
isEnvAssignment(args.getElement(n - 1).getALocalSource())
)
}

/** Holds if the first `n` elements are recognized `nice` options. */
private predicate nicePrefix(DataFlow::ArrayCreationNode args, int n) {
n = 0 and args.getSize() >= 0
or
nicePrefix(args, n - 1) and
args.getElement(n - 1)
.getALocalSource()
.getStringValue()
.regexpMatch("(-[0-9]+|-n[+-]?[0-9]+|--adjustment=[+-]?[0-9]+)")
or
nicePrefix(args, n - 2) and
args.getElement(n - 2).getALocalSource().getStringValue() = ["-n", "--adjustment"] and
exists(args.getElement(n - 1)) and
not args.getElement(n - 1).asExpr() instanceof SpreadElement
}

/** Holds if the first `n` elements after a shell are supported shell options. */
private predicate shellOptions(DataFlow::ArrayCreationNode args, int shellIndex, int n) {
n = 0 and
exists(args.getElement(shellIndex))
or
shellOptions(args, shellIndex, n - 1) and
args.getElement(shellIndex + n).getALocalSource().getStringValue() =
["-a", "-b", "-C", "-e", "-f", "-h", "-k", "-l", "-m", "-n", "-p", "-t", "-u", "-v", "-x"]
}

/** Holds if a launcher runs the shell at index `i` in its argument array. */
private predicate launcherShell(SystemCommandExecution sys, DataFlow::ArrayCreationNode args, int i) {
exists(string launcher | launcher = commandArgument(sys).getStringValue() |
launcher = ["env", "/usr/bin/env", "/bin/env"] and envPrefix(args, i)
or
launcher = ["nice", "/usr/bin/nice", "/bin/nice"] and
(
nicePrefix(args, i)
or
nicePrefix(args, i - 1) and
args.getElement(i - 1).getALocalSource().getStringValue() = "--"
)
)
}

/** Holds if the array can reach the call without its elements being modified. */
private predicate positionalArrayReaches(
DataFlow::ArrayCreationNode args, ControlFlowNode node, SystemCommandExecution sys
) {
args = positionalArgumentList(sys, DataFlow::TypeBackTracker::end()) and
exists(int i | launcherShell(sys, args, i)) and
(
node = sys.asExpr()
or
not exists(DataFlow::PropWrite write |
write = args.getAPropertyWrite() and
write.getWriteNode() = node and
(
not exists(write.getPropertyName())
or
write.getPropertyName().regexpMatch("([0-9]+|length)")
)
) and
not exists(DeleteExpr del, DataFlow::PropRef ref |
del.getOperand().flow() = ref and
args.flowsTo(ref.getBase()) and
del = node and
(
not exists(ref.getPropertyName())
or
ref.getPropertyName().regexpMatch("[0-9]+")
)
) and
not exists(DataFlow::MethodCallNode call |
call =
args.getAMethodCall([
"copyWithin", "fill", "pop", "push", "reverse", "shift", "sort", "splice", "unshift"
]) and
node = call.asExpr()
) and
positionalArrayReaches(args, node.getASuccessor(), sys)
)
}

/** Holds if an array reaches `use` without its positional elements being modified. */
private predicate positionalArrayReachesUse(
DataFlow::SourceNode args, ControlFlowNode node, ControlFlowNode use
) {
node = use and args.getFile() = use.getFile()
or
not exists(DataFlow::PropWrite write |
write = args.getAPropertyWrite() and
write.getWriteNode() = node and
(
not exists(write.getPropertyName())
or
write.getPropertyName().regexpMatch("([0-9]+|length)")
)
) and
not exists(DeleteExpr del, DataFlow::PropRef ref |
del.getOperand().flow() = ref and
args.flowsTo(ref.getBase()) and
del = node and
(
not exists(ref.getPropertyName())
or
ref.getPropertyName().regexpMatch("[0-9]+")
)
) and
not exists(DataFlow::MethodCallNode call |
call =
args.getAMethodCall([
"copyWithin", "fill", "pop", "push", "reverse", "shift", "sort", "splice", "unshift"
]) and
node = call.asExpr()
) and
positionalArrayReachesUse(args, node.getASuccessor(), use)
}

/** Holds if a launcher call uses a wrapper's array parameter. */
private predicate positionalWrapperArray(
SystemCommandExecution sys, DataFlow::ArrayCreationNode args, DataFlow::InvokeNode wrapperCall,
Function wrapper
) {
exists(int argsIndex |
sys.asExpr().getContainer() = wrapper and
wrapperCall.getACallee() = wrapper and
args = wrapperCall.getArgument(argsIndex).getALocalSource() and
positionalArrayReachesUse(args, args.asExpr().getASuccessor(), wrapperCall.asExpr()) and
positionalArrayReachesUse(args, wrapper.getStart(), sys.asExpr()) and
exists(DataFlow::ParameterNode argsParam, VarAccess access |
argsParam.getParameter() = wrapper.getParameter(argsIndex) and
access = sys.getArgumentList().asExpr() and
access.getVariable() = argsParam.getParameter().getAVariable() and
positionalArrayReachesUse(argsParam, wrapper.getStart(), sys.asExpr())
)
Comment on lines +278 to +283
)
}

/** Holds if one wrapper call launches `env` or `nice` with `args`. */
private predicate wrappedLauncherShell(
SystemCommandExecution sys, DataFlow::ArrayCreationNode args, DataFlow::InvokeNode wrapperCall,
Function wrapper, int i
) {
exists(
DataFlow::ParameterNode launcherParam, DataFlow::ParameterNode argsParam, string launcher,
int launcherIndex, int argsIndex
|
wrapperCall.getACallee() = wrapper and
sys.asExpr().getContainer() = wrapper and
launcherParam = sys.getACommandArgument().getALocalSource() and
argsParam = sys.getArgumentList().getALocalSource() and
launcherParam.getParameter() = wrapper.getParameter(launcherIndex) and
argsParam.getParameter() = wrapper.getParameter(argsIndex) and
args = wrapperCall.getArgument(argsIndex).getALocalSource() and
launcher = wrapperCall.getInvokeExpr().getArgument(launcherIndex).getStringValue()
|
launcher = ["env", "/usr/bin/env", "/bin/env"] and envPrefix(args, i)
or
launcher = ["nice", "/usr/bin/nice", "/bin/nice"] and
(
nicePrefix(args, i)
or
nicePrefix(args, i - 1) and
args.getElement(i - 1).getALocalSource().getStringValue() = "--"
)
)
or
exists(string launcher | launcher = sys.getACommandArgument().getStringValue() |
Comment on lines +303 to +316
launcher = ["env", "/usr/bin/env", "/bin/env"] and envPrefix(args, i)
or
launcher = ["nice", "/usr/bin/nice", "/bin/nice"] and
(
nicePrefix(args, i)
or
nicePrefix(args, i - 1) and
args.getElement(i - 1).getALocalSource().getStringValue() = "--"
)
)
Comment on lines +292 to +326
}

/**
* Holds if `source` contributes to the arguments of an indirect command execution `sys`.
*
* An indirect command execution is a system execution command that starts with `sh -c`, `cmd.exe /c`, or similar.
* An indirect command execution is a system execution command that starts with `sh -c`, `cmd.exe /c`, or similar,
* optionally through an `env` or `nice` launcher.
*
* For example, `getCommand()` is `source`, and the call to `childProcess.spawn` is `sys` in the following example:
*
Expand Down Expand Up @@ -114,4 +358,40 @@
else source = argsSource
)
)
or
exists(DataFlow::ArrayCreationNode args, int i, string dashC |
args = positionalArgumentList(sys, DataFlow::TypeBackTracker::end()) and
not exists(VarAccess access |
access = sys.getACommandArgument().asExpr() and access.getVariable().isParameter()
) and
not exists(DataFlow::ParameterNode parameter |
parameter = sys.getACommandArgument().getALocalSource()
) and
Comment on lines +367 to +369
Comment on lines +364 to +369
positionalArrayReaches(args, args.asExpr().getASuccessor(), sys) and
launcherShell(sys, args, i) and
Comment thread
hvitved marked this conversation as resolved.
Resolved
shellCmd(args.getElement(i).getALocalSource().asExpr(), dashC) and
exists(int optionCount, int cIndex |
shellOptions(args, i, optionCount) and
cIndex = i + optionCount + 1 and
args.getElement(cIndex).getALocalSource().getStringValue() = dashC and
source = args.getElement(cIndex + 1)
) and
not source.asExpr() instanceof SpreadElement
)
or
exists(
DataFlow::ArrayCreationNode args, int i, string dashC, DataFlow::InvokeNode wrapperCall,
Function wrapper
|
positionalWrapperArray(sys, args, wrapperCall, wrapper) and
wrappedLauncherShell(sys, args, wrapperCall, wrapper, i) and
shellCmd(args.getElement(i).getALocalSource().asExpr(), dashC) and
exists(int optionCount, int cIndex |
shellOptions(args, i, optionCount) and
cIndex = i + optionCount + 1 and
args.getElement(cIndex).getALocalSource().getStringValue() = dashC and
Comment on lines +376 to +392
source = args.getElement(cIndex + 1)
) and
not source.asExpr() instanceof SpreadElement
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The command injection queries now recognize shell commands launched through `env` or `nice`.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
| form-parsers.js:41:10:41:31 | "touch ... ds.name | form-parsers.js:40:26:40:31 | fields | form-parsers.js:41:10:41:31 | "touch ... ds.name | This command line depends on a $@. | form-parsers.js:40:26:40:31 | fields | user-provided value |
| form-parsers.js:53:10:53:31 | "touch ... ds.name | form-parsers.js:52:34:52:39 | fields | form-parsers.js:53:10:53:31 | "touch ... ds.name | This command line depends on a $@. | form-parsers.js:52:34:52:39 | fields | user-provided value |
| form-parsers.js:59:10:59:33 | "touch ... ilename | form-parsers.js:58:30:58:33 | part | form-parsers.js:59:10:59:33 | "touch ... ilename | This command line depends on a $@. | form-parsers.js:58:30:58:33 | part | user-provided value |
| launchers.js:8:3:8:49 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:8:41:8:47 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:9:3:9:45 | cp.exec ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:9:37:9:43 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:10:3:10:41 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:10:33:10:39 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:11:3:11:72 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:11:64:11:70 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:12:3:12:82 | cp.exec ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:12:74:12:80 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:13:3:13:57 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:13:49:13:55 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:14:3:14:55 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:14:47:14:53 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:15:3:15:65 | cp.spaw ... mmand]) | launchers.js:5:17:5:23 | req.url | launchers.js:15:57:15:63 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:18:3:18:23 | cp.spaw ... , args) | launchers.js:5:17:5:23 | req.url | launchers.js:17:30:17:36 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:44:3:44:30 | cp.spaw ... Change) | launchers.js:5:17:5:23 | req.url | launchers.js:43:36:43:42 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:49:3:49:30 | cp.spaw ... tional) | launchers.js:5:17:5:23 | req.url | launchers.js:47:36:47:42 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| launchers.js:53:3:53:23 | cp.spaw ... , args) | launchers.js:5:17:5:23 | req.url | launchers.js:19:32:19:38 | command | This command line depends on a $@. | launchers.js:5:17:5:23 | req.url | user-provided value |
| other.js:7:33:7:35 | cmd | other.js:5:25:5:31 | req.url | other.js:7:33:7:35 | cmd | This command line depends on a $@. | other.js:5:25:5:31 | req.url | user-provided value |
| other.js:8:28:8:30 | cmd | other.js:5:25:5:31 | req.url | other.js:8:28:8:30 | cmd | This command line depends on a $@. | other.js:5:25:5:31 | req.url | user-provided value |
| other.js:9:32:9:34 | cmd | other.js:5:25:5:31 | req.url | other.js:9:32:9:34 | cmd | This command line depends on a $@. | other.js:5:25:5:31 | req.url | user-provided value |
Expand Down Expand Up @@ -407,6 +419,21 @@ edges
| form-parsers.js:53:21:53:26 | fields | form-parsers.js:53:10:53:31 | "touch ... ds.name | provenance | |
| form-parsers.js:58:30:58:33 | part | form-parsers.js:59:21:59:24 | part | provenance | |
| form-parsers.js:59:21:59:24 | part | form-parsers.js:59:10:59:33 | "touch ... ilename | provenance | |
| launchers.js:5:9:5:13 | input | launchers.js:6:27:6:31 | input | provenance | |
| launchers.js:5:17:5:23 | req.url | launchers.js:5:9:5:13 | input | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:8:41:8:47 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:9:37:9:43 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:10:33:10:39 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:11:64:11:70 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:12:74:12:80 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:13:49:13:55 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:14:47:14:53 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:15:57:15:63 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:17:30:17:36 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:19:32:19:38 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:43:36:43:42 | command | provenance | |
| launchers.js:6:9:6:15 | command | launchers.js:47:36:47:42 | command | provenance | |
| launchers.js:6:27:6:31 | input | launchers.js:6:9:6:15 | command | provenance | |
| other.js:5:9:5:11 | cmd | other.js:7:33:7:35 | cmd | provenance | |
| other.js:5:9:5:11 | cmd | other.js:8:28:8:30 | cmd | provenance | |
| other.js:5:9:5:11 | cmd | other.js:9:32:9:34 | cmd | provenance | |
Expand Down Expand Up @@ -727,6 +754,22 @@ nodes
| form-parsers.js:58:30:58:33 | part | semmle.label | part |
| form-parsers.js:59:10:59:33 | "touch ... ilename | semmle.label | "touch ... ilename |
| form-parsers.js:59:21:59:24 | part | semmle.label | part |
| launchers.js:5:9:5:13 | input | semmle.label | input |
| launchers.js:5:17:5:23 | req.url | semmle.label | req.url |
| launchers.js:6:9:6:15 | command | semmle.label | command |
| launchers.js:6:27:6:31 | input | semmle.label | input |
| launchers.js:8:41:8:47 | command | semmle.label | command |
| launchers.js:9:37:9:43 | command | semmle.label | command |
| launchers.js:10:33:10:39 | command | semmle.label | command |
| launchers.js:11:64:11:70 | command | semmle.label | command |
| launchers.js:12:74:12:80 | command | semmle.label | command |
| launchers.js:13:49:13:55 | command | semmle.label | command |
| launchers.js:14:47:14:53 | command | semmle.label | command |
| launchers.js:15:57:15:63 | command | semmle.label | command |
| launchers.js:17:30:17:36 | command | semmle.label | command |
| launchers.js:19:32:19:38 | command | semmle.label | command |
| launchers.js:43:36:43:42 | command | semmle.label | command |
| launchers.js:47:36:47:42 | command | semmle.label | command |
| other.js:5:9:5:11 | cmd | semmle.label | cmd |
| other.js:5:15:5:38 | url.par ... , true) | semmle.label | url.par ... , true) |
| other.js:5:25:5:31 | req.url | semmle.label | req.url |
Expand Down
Loading
Loading