|
1 | 1 | /** |
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. |
4 | 7 | */ |
5 | 8 |
|
6 | | -import python |
| 9 | +private import python |
7 | 10 | import semmle.python.dataflow.new.DataFlow |
8 | 11 | 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 |
12 | 12 |
|
13 | 13 | /** |
14 | | - * A taint-tracking configuration for detecting command injection vulnerabilities. |
| 14 | + * Provides a taint-tracking configuration for detecting "command injection" vulnerabilities. |
15 | 15 | */ |
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 } |
52 | 28 |
|
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 | + } |
55 | 34 | } |
56 | 35 | } |
| 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; |
0 commit comments