Skip to content

Commit 962465f

Browse files
committed
add array-taint-steps to unsafe-shell-command-construction
1 parent a4c42aa commit 962465f

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

ruby/ql/lib/codeql/ruby/security/UnsafeShellCommandConstructionQuery.qll

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import UnsafeShellCommandConstructionCustomizations::UnsafeShellCommandConstruct
1111
private import codeql.ruby.TaintTracking
1212
private import CommandInjectionCustomizations::CommandInjection as CommandInjection
1313
private import codeql.ruby.dataflow.BarrierGuards
14+
private import codeql.ruby.frameworks.core.Array
1415

1516
/**
1617
* A taint-tracking configuration for detecting shell command constructed from library input vulnerabilities.
@@ -32,4 +33,9 @@ class Configuration extends TaintTracking::Configuration {
3233
override DataFlow::FlowFeature getAFeature() {
3334
result instanceof DataFlow::FeatureHasSourceCallContext
3435
}
36+
37+
override predicate isAdditionalTaintStep(DataFlow::Node pred, DataFlow::Node succ) {
38+
// if an array element gets tainted, then we treat the entire array as tainted
39+
Array::taintedArrayObjectSteps(pred, succ)
40+
}
3541
}

ruby/ql/test/query-tests/security/cwe-078/UnsafeShellCommandConstruction/UnsafeShellCommandConstruction.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ edges
1212
| impl/unsafeShell.rb:51:17:51:17 | x : | impl/unsafeShell.rb:52:14:52:14 | x |
1313
| impl/unsafeShell.rb:51:17:51:17 | x : | impl/unsafeShell.rb:54:29:54:29 | x |
1414
| impl/unsafeShell.rb:57:21:57:21 | x : | impl/unsafeShell.rb:58:23:58:23 | x |
15+
| impl/unsafeShell.rb:61:20:61:20 | x : | impl/unsafeShell.rb:64:14:64:16 | arr |
16+
| impl/unsafeShell.rb:61:20:61:20 | x : | impl/unsafeShell.rb:68:14:68:16 | arr |
1517
nodes
1618
| impl/sub/notImported.rb:2:12:2:17 | target : | semmle.label | target : |
1719
| impl/sub/notImported.rb:3:19:3:27 | #{...} | semmle.label | #{...} |
@@ -38,6 +40,9 @@ nodes
3840
| impl/unsafeShell.rb:54:29:54:29 | x | semmle.label | x |
3941
| impl/unsafeShell.rb:57:21:57:21 | x : | semmle.label | x : |
4042
| impl/unsafeShell.rb:58:23:58:23 | x | semmle.label | x |
43+
| impl/unsafeShell.rb:61:20:61:20 | x : | semmle.label | x : |
44+
| impl/unsafeShell.rb:64:14:64:16 | arr | semmle.label | arr |
45+
| impl/unsafeShell.rb:68:14:68:16 | arr | semmle.label | arr |
4146
subpaths
4247
#select
4348
| impl/sub/notImported.rb:3:14:3:28 | "cat #{...}" | impl/sub/notImported.rb:2:12:2:17 | target : | impl/sub/notImported.rb:3:19:3:27 | #{...} | This string construction which depends on $@ is later used in a $@. | impl/sub/notImported.rb:2:12:2:17 | target | library input | impl/sub/notImported.rb:3:5:3:34 | call to popen | shell command |
@@ -53,3 +58,5 @@ subpaths
5358
| impl/unsafeShell.rb:52:14:52:24 | call to join | impl/unsafeShell.rb:51:17:51:17 | x : | impl/unsafeShell.rb:52:14:52:14 | x | This array which depends on $@ is later used in a $@. | impl/unsafeShell.rb:51:17:51:17 | x | library input | impl/unsafeShell.rb:52:5:52:30 | call to popen | shell command |
5459
| impl/unsafeShell.rb:54:14:54:40 | call to join | impl/unsafeShell.rb:51:17:51:17 | x : | impl/unsafeShell.rb:54:29:54:29 | x | This array which depends on $@ is later used in a $@. | impl/unsafeShell.rb:51:17:51:17 | x | library input | impl/unsafeShell.rb:54:5:54:46 | call to popen | shell command |
5560
| impl/unsafeShell.rb:58:14:58:23 | ... + ... | impl/unsafeShell.rb:57:21:57:21 | x : | impl/unsafeShell.rb:58:23:58:23 | x | This string concatenation which depends on $@ is later used in a $@. | impl/unsafeShell.rb:57:21:57:21 | x | library input | impl/unsafeShell.rb:58:5:58:29 | call to popen | shell command |
61+
| impl/unsafeShell.rb:64:14:64:26 | call to join | impl/unsafeShell.rb:61:20:61:20 | x : | impl/unsafeShell.rb:64:14:64:16 | arr | This array which depends on $@ is later used in a $@. | impl/unsafeShell.rb:61:20:61:20 | x | library input | impl/unsafeShell.rb:64:5:64:32 | call to popen | shell command |
62+
| impl/unsafeShell.rb:68:14:68:26 | call to join | impl/unsafeShell.rb:61:20:61:20 | x : | impl/unsafeShell.rb:68:14:68:16 | arr | This array which depends on $@ is later used in a $@. | impl/unsafeShell.rb:61:20:61:20 | x | library input | impl/unsafeShell.rb:68:5:68:32 | call to popen | shell command |

ruby/ql/test/query-tests/security/cwe-078/UnsafeShellCommandConstruction/impl/unsafeShell.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,14 @@ def arrayJoin(x)
5757
def string_concat(x)
5858
IO.popen("cat " + x, "w") # NOT OK
5959
end
60+
61+
def array_taint (x, y)
62+
arr = ["cat"]
63+
arr.push(x)
64+
IO.popen(arr.join(' '), "w") # NOT OK
65+
66+
arr2 = ["cat"]
67+
arr2 << y
68+
IO.popen(arr.join(' '), "w") # NOT OK
69+
end
6070
end

0 commit comments

Comments
 (0)