Skip to content

Commit c0aaeee

Browse files
authored
Merge pull request #250 from CoreTrace/feat/dynamic-import-sinks
feat(code-injection): report dynamic imports and runpy reached by input
2 parents 501030a + 2641b57 commit c0aaeee

12 files changed

Lines changed: 527 additions & 28 deletions

File tree

‎docs/usage.md‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ variable is not treated that way yet.
121121
| Rule | Reached sink |
122122
|---|---|
123123
| `command-injection` | Shell or process execution (`os.system`, `subprocess` with a string, …), judged on what the input controls in the command. A string a shell runs, a string naming the program, the program itself, or an operand the program runs as a command (after the host for `ssh`, the program of `sudo`, `env`, `xargs`, `timeout`…, any argument of a batch file) is `high`. The script an interpreter runs (`bash run.sh`, `python tool.py`, its first operand when no option such as `-c` gives the code), or the module of `python -m`, is `high` and kept for review: execution is possible, and its exploitability depends on the file the input chooses. The arguments after a script or code are the script's own: options of unknown meaning, reported `medium` (a hotspot after an option of the script), refuted behind a constant prefix. Otherwise an element of an argument list run without a shell can only be read as an option: `medium`, or `high` when a documented option of the program runs a command it chooses (`git push --receive-pack=…`, `ssh -o ProxyCommand=…`). A value a documented option takes is judged by what the option does with it, whatever its prefix: run it (`sh -c`, `cmd /c`, `python -c`: `high`), keep it as data (`git commit -m`: refuted). After an option the rule does not know, the finding stays a hotspot to review. A constant prefix that keeps a standalone input from being an option (`f"v{version}"`), or an operand after `--` for a program that honours it, is refuted. Every case keeps the rule id `command-injection`, so suppressions and baselines match whatever the case. When the rule reads the command element by element and establishes what the input is there, the finding's `injection` metadata (in the JSON report, and as the only property of its SARIF result) says so: `command` when the input runs or chooses what runs (a shell string, the program, an operand run as a command, the value of an option that runs it); `option` when it can only reach the program as an option or as the value of an option not established to run it, and the message is then titled `Option injection`. When that cannot be established (an option whose effect is to review, an unknown program or element before the input, a command that is not a list or tuple display, a sink such as `os.system` the rule does not read element by element), the metadata is absent and the title stays `Command injection`. |
124-
| `code-injection` | The code `eval` or `exec` runs; the globals and locals passed with it are data. `dangerous-eval` reports every such call, this rule the ones attacker input reaches. |
124+
| `code-injection` | The code `eval` or `exec` runs; the globals and locals passed with it are data. `dangerous-eval` reports every such call, this rule the ones attacker input reaches. Also the module `importlib.import_module`, `__import__` or `runpy.run_module` imports, and the file `runpy.run_path` runs, by position or by keyword (`name`, `mod_name`, `path_name`); the package of a relative name, the globals and the run name are data. An import runs the top-level code of a module the input chooses among those that can be found, a script path the file it chooses: execution is possible, and how far it goes depends on what the input can reach, so these findings are titled `Untrusted module import` and `Untrusted script execution`. A constant package prefix (`"app.plugins." + name`) confines an import to that package: a hotspot. The environment and the command line reach these sinks as they reach `eval`. |
125125
| `sql-injection` | A database statement (`cursor.execute`, SQLAlchemy `text`, Django `raw`, …). Parameters of a parameterised query are not statements. |
126126
| `nosql-injection` | A NoSQL query, such as a MongoDB filter, given attacker input that may hold a structure: a JSON body giving `{"$ne": null}` matches any value. The input that may hold a structure is JSON, the raw bodies a `json.loads` decodes, files, WebSocket messages and standard input. Text cannot hold an operator: form fields, query parameters, headers, cookies, URL parameters, the command line, the environment, and an entry-point parameter annotated with scalars (`str`, `int`, `Optional[str]`, `Annotated[str, Query()]`, `list[str]`, …), which FastAPI validates. A Django, REST framework, aiohttp or tornado view reads a request object: its text attributes (`GET`, `POST`, `headers`, `query_params`, `query`, `match_info`, `arguments`, …) and the URL parameters after it are text, its body, files and `data` are not. Limits: a helper function the request is passed to reads it as a whole; the keys of a dict are not followed, so a string concatenated into `$where` is not reported. The analyzer ships no NoSQL sink: model plugins declare them. |
127127
| `path-traversal` | A file system path (`open`, `send_file`, `os.remove`, …). |
@@ -135,7 +135,12 @@ variable is not treated that way yet.
135135
Each flow is judged before it is reported. A dominating guard that proves the value
136136
safe (`isdigit()`, membership in a constant allowlist, equality with a constant, a
137137
numeric value proven by `int()`, `len()` or bounded arithmetic, a validator such as
138-
`re.fullmatch`) refutes it and nothing is reported. A hotspot is reported at medium
138+
`re.fullmatch`) refutes it and nothing is reported. A sink that selects code by name,
139+
the module of `importlib.import_module`, `__import__` or `runpy.run_module` and the file
140+
of `runpy.run_path`, is proven only by a check that limits the value to constants:
141+
membership in a constant allowlist or equality with a constant. A check of its shape
142+
(`isidentifier()`, `isalnum()`, `re.fullmatch`) limits its characters, not the modules
143+
or files it can name, and leaves a hotspot. A hotspot is reported at medium
139144
confidence. Two things make a flow a hotspot:
140145

141146
- A guard that examines the data without proving it safe. That covers a test of the value

‎src/coretrace_python/bundled/models/python_stdlib/python_stdlib.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ class PythonStdlibModels(ModelPlugin):
6262
# The code is the first argument; the globals and locals it runs with are data.
6363
Sink(_sym("builtins.eval"), TaintKind.CODE, ((TaintKind.CODE, (0,)),)),
6464
Sink(_sym("builtins.exec"), TaintKind.CODE, ((TaintKind.CODE, (0,)),)),
65+
# The module or the file the call imports or runs; its globals, its run name and
66+
# the package of a relative name are data (#240).
67+
*(
68+
Sink(_sym(symbol), TaintKind.CODE, ((TaintKind.CODE, (0,)),), ((TaintKind.CODE, (keyword,)),), selects=True)
69+
for symbol, keyword in (
70+
("importlib.import_module", "name"),
71+
("builtins.__import__", "name"),
72+
("importlib.__import__", "name"),
73+
("runpy.run_module", "mod_name"),
74+
("runpy.run_path", "path_name"),
75+
)
76+
),
6577
Sink(_sym("builtins.open"), TaintKind.PATH),
6678
Sink(_sym("os.remove"), TaintKind.PATH),
6779
Sink(_sym("os.unlink"), TaintKind.PATH),
Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
"""Code injection: attacker-controlled input reaching a CODE sink, such as the code
2-
``eval`` or ``exec`` runs. ``dangerous-eval`` reports every such call; this rule reports
3-
the ones attacker input reaches."""
2+
``eval`` or ``exec`` runs, the module ``importlib.import_module``, ``__import__`` or
3+
``runpy.run_module`` imports, or the file ``runpy.run_path`` runs. ``dangerous-eval``
4+
reports every ``eval`` or ``exec``; this rule reports the calls attacker input reaches.
5+
6+
An import runs the top-level code of a module the input chooses among those that can be
7+
found, and a script path runs the file the input chooses: execution is possible, and
8+
how far it goes depends on what the input can reach. Their findings are titled for
9+
that. A constant package prefix (``"app.plugins." + name``) confines an import to the
10+
modules of that package: a hotspot."""
411

512
from __future__ import annotations
613

714
from typing import ClassVar
815

16+
from coretrace_python.abstract.strings import ModuleStringsAnalysis, leading_text
17+
from coretrace_python.analysis import AnyAnalysis
918
from coretrace_python.findings import Severity
10-
from coretrace_python.plugins import TaintDetector
11-
from coretrace_python.taint import TaintKind
19+
from coretrace_python.findings.refutation import Status, Verdict
20+
from coretrace_python.hir import nodes
21+
from coretrace_python.ir.ssa import SSAAnalysis
22+
from coretrace_python.plugins import Assessment, PluginContext, TaintDetector
23+
from coretrace_python.semantic.symbols import SymbolId
24+
from coretrace_python.taint import TaintFlow, TaintKind
25+
26+
_IMPORTS = frozenset(
27+
SymbolId(f"python.{name}")
28+
for name in ("importlib.import_module", "builtins.__import__", "importlib.__import__", "runpy.run_module")
29+
)
30+
_RUN_PATH = SymbolId("python.runpy.run_path")
1231

1332

1433
class CodeInjectionPlugin(TaintDetector):
@@ -17,3 +36,23 @@ class CodeInjectionPlugin(TaintDetector):
1736
kind: ClassVar[TaintKind] = TaintKind.CODE
1837
severity: ClassVar[Severity] = Severity.HIGH
1938
title: ClassVar[str] = "Code injection"
39+
requires: ClassVar[frozenset[AnyAnalysis]] = TaintDetector.requires | {SSAAnalysis, ModuleStringsAnalysis}
40+
41+
def assess(
42+
self, ctx: PluginContext, function: nodes.Function, flow: TaintFlow, verdict: Verdict
43+
) -> Assessment:
44+
symbol = flow.sink.symbol
45+
if symbol == _RUN_PATH:
46+
return Assessment(verdict, self.severity, "Untrusted script execution")
47+
if symbol not in _IMPORTS:
48+
return Assessment(verdict, self.severity)
49+
if verdict.status is Status.VULNERABILITY and flow.through is None:
50+
ssa = ctx.get(SSAAnalysis, function)
51+
defs = {i.result: i for block in ssa.blocks for i in block.instructions if i.result is not None}
52+
prefix, whole = leading_text(flow.argument, defs, ctx.get(ModuleStringsAnalysis))
53+
package = prefix.rpartition(".")[0]
54+
if not whole and package and not prefix.startswith("."):
55+
verdict = Verdict(
56+
flow, Status.HOTSPOT, f"the package is fixed by {prefix!r}, but the input chooses a module in it"
57+
)
58+
return Assessment(verdict, self.severity, "Untrusted module import")

‎src/coretrace_python/bundled/security/code_injection/plugin.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "code-injection"
22
version = "1.0.0"
33
plugin_api = ">=1,<2"
4-
requires = ["taint.flows", "findings.refutation"]
4+
requires = ["taint.flows", "findings.refutation", "ir.ssa", "abstract.module_strings"]
55
provides = ["vulnerability.code-injection"]
66

77
[entrypoint]

‎src/coretrace_python/findings/refutation.py‎

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,16 @@ def guards(self, sink: BlockId) -> list[_Guard]:
247247

248248
def interpret(
249249
self, condition: Value, truth: bool | None
250-
) -> tuple[dict[Value, tuple[str, TaintKind]], set[Value]]:
251-
"""Values the condition proves safe (with the reason and the kinds it proves them
252-
safe for), and values it mentions without proving anything. ``truth`` is ``None``
253-
when it is not fixed at the sink.
250+
) -> tuple[dict[Value, tuple[str, TaintKind, bool]], set[Value]]:
251+
"""Values the condition proves safe (with the reason, the kinds it proves them
252+
safe for, and whether it limits them to constants), and values it mentions without
253+
proving anything. ``truth`` is ``None`` when it is not fixed at the sink.
254254
255255
A recognised check evaluated the wrong way (``isdigit()`` known false) yields
256256
nothing: it is neither a proof nor a reassuring guard."""
257257

258258
definition = self.defs.get(condition)
259-
proven: dict[Value, tuple[str, TaintKind]] = {}
259+
proven: dict[Value, tuple[str, TaintKind, bool]] = {}
260260
mentioned: set[Value] = set()
261261
if isinstance(definition, UnaryOp) and definition.operator == "not":
262262
return self.interpret(definition.operand, None if truth is None else not truth)
@@ -270,29 +270,37 @@ def interpret(
270270
recognised = self.recognise(definition)
271271
if recognised is None:
272272
return proven, set(self.closure(condition)) | {condition}
273-
tested, reason, when_true, kinds = recognised
273+
tested, reason, when_true, kinds, exact = recognised
274274
if truth is None:
275275
mentioned = set(self.closure(condition)) | {condition}
276276
elif truth == when_true:
277277
# The check proves ``tested``; whatever else it reads is merely mentioned.
278-
proven[tested] = (reason, kinds)
278+
proven[tested] = (reason, kinds, exact)
279279
mentioned = set(self.closure(condition)) | {condition}
280280
return proven, mentioned
281281

282-
def recognise(self, definition: Instruction | None) -> tuple[Value, str, bool, TaintKind] | None:
283-
"""``(validated value, reason, truth that validates, kinds it proves)`` for known
284-
check shapes. A string check, a constant allowlist or an equality with a constant
285-
proves every kind; a ``Validator`` only the kinds it declares."""
282+
def recognise(self, definition: Instruction | None) -> tuple[Value, str, bool, TaintKind, bool] | None:
283+
"""``(validated value, reason, truth that validates, kinds it proves, whether it
284+
limits the value to constants)`` for known check shapes. A string check, a constant
285+
allowlist or an equality with a constant proves every kind; a ``Validator`` only
286+
the kinds it declares. Only the allowlist and the equality limit the value to
287+
constants: a string check or a validator checks its shape."""
286288

287289
if isinstance(definition, Call) and not definition.arguments:
288290
callee = self.defs.get(definition.callee)
289291
if isinstance(callee, GetAttr) and callee.attribute in VALIDATORS:
290-
return callee.object, f"guarded by {callee.attribute}()", True, TaintKind.ALL
292+
return callee.object, f"guarded by {callee.attribute}()", True, TaintKind.ALL, False
291293
if isinstance(definition, Call):
292294
symbol = self.symbols.get(definition.callee)
293295
validator = self.models.validator(symbol) if symbol is not None else None
294296
if validator is not None and validator.argument < len(definition.arguments):
295-
return definition.arguments[validator.argument], f"validated by {symbol}", True, validator.kinds
297+
return (
298+
definition.arguments[validator.argument],
299+
f"validated by {symbol}",
300+
True,
301+
validator.kinds,
302+
False,
303+
)
296304
if isinstance(definition, Compare):
297305
left, right = definition.left, definition.right
298306
if definition.operator in ("in", "not_in") and self.is_constant_collection(right):
@@ -301,11 +309,12 @@ def recognise(self, definition: Instruction | None) -> tuple[Value, str, bool, T
301309
"allowlisted by a membership check on constants",
302310
definition.operator == "in",
303311
TaintKind.ALL,
312+
True,
304313
)
305314
if definition.operator in ("eq", "not_eq"):
306315
for tested, other in ((left, right), (right, left)):
307316
if isinstance(self.defs.get(other), Constant):
308-
return tested, "equals a constant", definition.operator == "eq", TaintKind.ALL
317+
return tested, "equals a constant", definition.operator == "eq", TaintKind.ALL, True
309318
return None
310319

311320
def examined(
@@ -458,10 +467,12 @@ def judge(self, flow: TaintFlow, reachable: bool) -> Verdict:
458467
return Verdict(flow, Status.REFUTED, "sink unreachable by constant propagation")
459468
origins = self.origins(flow.argument)
460469
chain = self.closure(flow.argument) | {flow.argument}
470+
# A sink that selects code by name is proven only by constants (``Sink.selects``).
471+
selects = flow.sink.selects
461472
proven: dict[Value, str] = {
462473
value: f"numeric value within {interval}"
463474
for value, interval in self.ranges.at(sink).items()
464-
if value in chain
475+
if value in chain and not selects
465476
}
466477
mentions: dict[Value, int] = {}
467478
authorization: str | None = (
@@ -471,9 +482,9 @@ def judge(self, flow: TaintFlow, reachable: bool) -> Verdict:
471482
)
472483
for guard in self.guards(sink):
473484
found, mentioned = self.interpret(guard.condition, guard.truth)
474-
for value, (reason, kinds) in found.items():
485+
for value, (reason, kinds, exact) in found.items():
475486
# A check proving some kinds only leaves the others reaching the sink.
476-
if value in chain and not flow.kinds & ~kinds:
487+
if value in chain and not flow.kinds & ~kinds and (exact or not selects):
477488
proven.setdefault(value, reason)
478489
examined = self.examined(guard.condition, mentioned, flow.argument, chain)
479490
for origin in origins:

‎src/coretrace_python/semantic/symbols.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
from coretrace_python.semantic.imports import ImportAnalysis, ImportTable
1515
from coretrace_python.semantic.scopes import ResolutionKind, ScopeAnalysis, ScopeId, ScopeTable
1616

17-
BUILTIN_NAMES = frozenset(name for name in dir(builtins) if not name.startswith("_"))
17+
# ``__import__`` is the one dunder of the builtins a program calls by name (#240).
18+
BUILTIN_NAMES = frozenset(name for name in dir(builtins) if not name.startswith("_")) | {"__import__"}
1819

1920

2021
@dataclass(frozen=True)

‎src/coretrace_python/taint/models.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,16 @@ class Sink:
6565
"""A callable whose arguments must not carry the given taint kinds. ``positions`` and
6666
``keywords`` restrict some kinds to the arguments they list: a SQL statement is the
6767
first argument of ``execute``, its parameter tuple is not a statement; the target of
68-
``requests.get`` is its first argument or ``url=``, its body is no destination."""
68+
``requests.get`` is its first argument or ``url=``, its body is no destination.
69+
``selects`` marks a sink whose argument chooses by name the code that runs, a module
70+
or a file: only a check that limits the value to constants proves it, not a check of
71+
its shape (#240)."""
6972

7073
symbol: SymbolId
7174
kinds: TaintKind
7275
positions: tuple[tuple[TaintKind, tuple[int, ...]], ...] = ()
7376
keywords: tuple[tuple[TaintKind, tuple[str, ...]], ...] = ()
77+
selects: bool = False
7478

7579
def kinds_at(self, position: int | None, keyword: str | None = None) -> TaintKind:
7680
"""The kinds that must not reach the argument at ``position`` or passed as
@@ -90,13 +94,15 @@ def kinds_at(self, position: int | None, keyword: str | None = None) -> TaintKin
9094
return self.kinds & ~(restricted & ~allowed)
9195

9296
def merged(self, other: Sink) -> Sink:
93-
"""This sink with ``other``'s kinds, positions and keywords added."""
97+
"""This sink with ``other``'s kinds, positions and keywords added; it selects code
98+
by name when either does, whatever the order the declarations merge in."""
9499

95100
return Sink(
96101
self.symbol,
97102
self.kinds | other.kinds,
98103
self.positions + other.positions,
99104
self.keywords + other.keywords,
105+
self.selects or other.selects,
100106
)
101107

102108

‎tests/test_detectors.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,15 @@ def test_shipped_plugins_load_with_their_manifests() -> None:
202202
}
203203
for rule in ("sql-injection", "command-injection", "code-injection", "nosql-injection", "path-traversal", "ssrf", "xss"):
204204
assert by_name[rule].provides == (f"vulnerability.{rule}",)
205-
for rule in ("sql-injection", "code-injection", "nosql-injection", "path-traversal", "xss"):
205+
for rule in ("sql-injection", "nosql-injection", "path-traversal", "xss"):
206206
assert by_name[rule].requires == ("taint.flows", "findings.refutation")
207+
# ``code-injection`` also reads how the name a dynamic import receives is built (#240).
208+
assert by_name["code-injection"].requires == (
209+
"taint.flows",
210+
"findings.refutation",
211+
"ir.ssa",
212+
"abstract.module_strings",
213+
)
207214
# ``ssrf`` also reads how the URL of each flow is built.
208215
assert by_name["ssrf"].requires == ("taint.flows", "findings.refutation", "ir.ssa", "abstract.module_strings")
209216
# ``command-injection`` also reads the command each flow reaches, element by element.

0 commit comments

Comments
 (0)