Skip to content

Commit 62e4445

Browse files
committed
Python: Port py/command-line-injection to use proper source/sink customization
1 parent 7f53781 commit 62e4445

File tree

3 files changed

+119
-46
lines changed

3 files changed

+119
-46
lines changed

python/ql/src/Security/CWE-078/CommandInjection.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import python
1919
import semmle.python.security.dataflow.CommandInjection
2020
import DataFlow::PathGraph
2121

22-
from CommandInjectionConfiguration config, DataFlow::PathNode source, DataFlow::PathNode sink
22+
from CommandInjection::Configuration config, DataFlow::PathNode source, DataFlow::PathNode sink
2323
where config.hasFlowPath(source, sink)
2424
select sink.getNode(), source, sink, "This command depends on $@.", source.getNode(),
2525
"a user-provided value"
Lines changed: 31 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,42 @@
11
/**
2-
* Provides a taint-tracking configuration for detecting command injection
3-
* vulnerabilities.
2+
* Provides a taint-tracking configuration for detecting "command injection" vulnerabilities.
3+
*
4+
* Note, for performance reasons: only import this file if
5+
* `CommandInjection::Configuration` is needed, otherwise
6+
* `CommandInjectionCustomizations` should be imported instead.
47
*/
58

6-
import python
9+
private import python
710
import semmle.python.dataflow.new.DataFlow
811
import semmle.python.dataflow.new.TaintTracking
9-
import semmle.python.Concepts
10-
import semmle.python.dataflow.new.RemoteFlowSources
11-
import semmle.python.dataflow.new.BarrierGuards
1212

1313
/**
14-
* A taint-tracking configuration for detecting command injection vulnerabilities.
14+
* Provides a taint-tracking configuration for detecting "command injection" vulnerabilities.
1515
*/
16-
class CommandInjectionConfiguration extends TaintTracking::Configuration {
17-
CommandInjectionConfiguration() { this = "CommandInjectionConfiguration" }
18-
19-
override predicate isSource(DataFlow::Node source) { source instanceof RemoteFlowSource }
20-
21-
override predicate isSink(DataFlow::Node sink) {
22-
sink = any(SystemCommandExecution e).getCommand() and
23-
// Since the implementation of standard library functions such `os.popen` looks like
24-
// ```py
25-
// def popen(cmd, mode="r", buffering=-1):
26-
// ...
27-
// proc = subprocess.Popen(cmd, ...)
28-
// ```
29-
// any time we would report flow to the `os.popen` sink, we can ALSO report the flow
30-
// from the `cmd` parameter to the `subprocess.Popen` sink -- obviously we don't
31-
// want that.
32-
//
33-
// However, simply removing taint edges out of a sink is not a good enough solution,
34-
// since we would only flag one of the `os.system` calls in the following example
35-
// due to use-use flow
36-
// ```py
37-
// os.system(cmd)
38-
// os.system(cmd)
39-
// ```
40-
//
41-
// Best solution I could come up with is to exclude all sinks inside the modules of
42-
// known sinks. This does have a downside: If we have overlooked a function in any
43-
// of these, that internally runs a command, we no longer give an alert :| -- and we
44-
// need to keep them updated (which is hard to remember)
45-
//
46-
// This does not only affect `os.popen`, but also the helper functions in
47-
// `subprocess`. See:
48-
// https://github.com/python/cpython/blob/fa7ce080175f65d678a7d5756c94f82887fc9803/Lib/os.py#L974
49-
// https://github.com/python/cpython/blob/fa7ce080175f65d678a7d5756c94f82887fc9803/Lib/subprocess.py#L341
50-
not sink.getScope().getEnclosingModule().getName() in ["os", "subprocess", "platform", "popen2"]
51-
}
16+
module CommandInjection {
17+
import CommandInjectionCustomizations::CommandInjection
18+
19+
/**
20+
* A taint-tracking configuration for detecting "command injection" vulnerabilities.
21+
*/
22+
class Configuration extends TaintTracking::Configuration {
23+
Configuration() { this = "CommandInjection" }
24+
25+
override predicate isSource(DataFlow::Node source) { source instanceof Source }
26+
27+
override predicate isSink(DataFlow::Node sink) { sink instanceof Sink }
5228

53-
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
54-
guard instanceof StringConstCompare
29+
override predicate isSanitizer(DataFlow::Node node) { node instanceof Sanitizer }
30+
31+
override predicate isSanitizerGuard(DataFlow::BarrierGuard guard) {
32+
guard instanceof SanitizerGuard
33+
}
5534
}
5635
}
36+
37+
/**
38+
* DEPRECATED: Don't extend this class for customization, since this will lead to bad
39+
* performance, instead use the new `CommandInjectionCustomizations.qll` file, and extend
40+
* its' classes.
41+
*/
42+
deprecated class CommandInjectionConfiguration = CommandInjection::Configuration;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Provides default sources, sinks and sanitizers for detecting
3+
* "command injection"
4+
* vulnerabilities, as well as extension points for adding your own.
5+
*/
6+
7+
private import python
8+
private import semmle.python.dataflow.new.DataFlow
9+
private import semmle.python.Concepts
10+
private import semmle.python.dataflow.new.RemoteFlowSources
11+
private import semmle.python.dataflow.new.BarrierGuards
12+
13+
/**
14+
* Provides default sources, sinks and sanitizers for detecting
15+
* "command injection"
16+
* vulnerabilities, as well as extension points for adding your own.
17+
*/
18+
module CommandInjection {
19+
/**
20+
* A data flow source for "command injection" vulnerabilities.
21+
*/
22+
abstract class Source extends DataFlow::Node { }
23+
24+
/**
25+
* A data flow sink for "command injection" vulnerabilities.
26+
*/
27+
abstract class Sink extends DataFlow::Node { }
28+
29+
/**
30+
* A sanitizer for "command injection" vulnerabilities.
31+
*/
32+
abstract class Sanitizer extends DataFlow::Node { }
33+
34+
/**
35+
* A sanitizer guard for "command injection" vulnerabilities.
36+
*/
37+
abstract class SanitizerGuard extends DataFlow::BarrierGuard { }
38+
39+
/**
40+
* A source of remote user input, considered as a flow source.
41+
*/
42+
class RemoteFlowSourceAsSource extends Source, RemoteFlowSource { }
43+
44+
/**
45+
* A command execution, considered as a flow sink.
46+
*/
47+
class CommandExecutionAsSink extends Sink {
48+
CommandExecutionAsSink() {
49+
this = any(SystemCommandExecution e).getCommand() and
50+
// Since the implementation of standard library functions such `os.popen` looks like
51+
// ```py
52+
// def popen(cmd, mode="r", buffering=-1):
53+
// ...
54+
// proc = subprocess.Popen(cmd, ...)
55+
// ```
56+
// any time we would report flow to the `os.popen` sink, we can ALSO report the flow
57+
// from the `cmd` parameter to the `subprocess.Popen` sink -- obviously we don't
58+
// want that.
59+
//
60+
// However, simply removing taint edges out of a sink is not a good enough solution,
61+
// since we would only flag one of the `os.system` calls in the following example
62+
// due to use-use flow
63+
// ```py
64+
// os.system(cmd)
65+
// os.system(cmd)
66+
// ```
67+
//
68+
// Best solution I could come up with is to exclude all sinks inside the modules of
69+
// known sinks. This does have a downside: If we have overlooked a function in any
70+
// of these, that internally runs a command, we no longer give an alert :| -- and we
71+
// need to keep them updated (which is hard to remember)
72+
//
73+
// This does not only affect `os.popen`, but also the helper functions in
74+
// `subprocess`. See:
75+
// https://github.com/python/cpython/blob/fa7ce080175f65d678a7d5756c94f82887fc9803/Lib/os.py#L974
76+
// https://github.com/python/cpython/blob/fa7ce080175f65d678a7d5756c94f82887fc9803/Lib/subprocess.py#L341
77+
not this.getScope().getEnclosingModule().getName() in [
78+
"os", "subprocess", "platform", "popen2"
79+
]
80+
}
81+
}
82+
83+
/**
84+
* A comparison with a constant string, considered as a sanitizer-guard.
85+
*/
86+
class StringConstCompareAsSanitizerGuard extends SanitizerGuard, StringConstCompare { }
87+
}

0 commit comments

Comments
 (0)