Skip to content

Commit 709649e

Browse files
committed
Model replace and putIfAbsent
1 parent 1544330 commit 709649e

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

java/ql/lib/semmle/code/java/security/TaintedEnvironmentVariableQuery.qll

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ private import semmle.code.java.dataflow.FlowSources
55
private import semmle.code.java.Maps
66
private import semmle.code.java.JDK
77

8-
private class MapPutOrRemove extends MethodCall {
9-
MapPutOrRemove() {
10-
this.getMethod() instanceof MapMutator and
11-
this.getMethod().getName().matches(["put%", "remove"])
8+
private class MapUpdateWithKeyOrValue extends MethodCall {
9+
MapUpdateWithKeyOrValue() {
10+
this.getMethod() instanceof MapMethod and
11+
this.getMethod().getName().matches(["put%", "remove", "replace"])
1212
}
1313
}
1414

@@ -19,7 +19,9 @@ private module ProcessBuilderEnvironmentConfig implements DataFlow::ConfigSig {
1919
)
2020
}
2121

22-
predicate isSink(DataFlow::Node sink) { sink.asExpr() = any(MapPutOrRemove mm).getQualifier() }
22+
predicate isSink(DataFlow::Node sink) {
23+
sink.asExpr() = any(MapUpdateWithKeyOrValue mm).getQualifier()
24+
}
2325
}
2426

2527
private module ProcessBuilderEnvironmentFlow = DataFlow::Global<ProcessBuilderEnvironmentConfig>;
@@ -41,7 +43,7 @@ module ExecTaintedEnvironmentConfig implements DataFlow::ConfigSig {
4143
sinkNode(sink, "environment-injection")
4244
or
4345
// sink is a key or value added to a `ProcessBuilder::environment` map.
44-
exists(MapPutOrRemove mm | mm.getAnArgument() = sink.asExpr() |
46+
exists(MapUpdateWithKeyOrValue mm | mm.getAnArgument() = sink.asExpr() |
4547
ProcessBuilderEnvironmentFlow::flowToExpr(mm.getQualifier())
4648
)
4749
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| TaintedEnvironment.java:28:35:28:55 | new String[] | Command with a relative path 'ls' is executed. |
1+
| TaintedEnvironment.java:39:35:39:55 | new String[] | Command with a relative path 'ls' is executed. |
22
| Test.java:50:46:50:49 | "ls" | Command with a relative path 'ls' is executed. |

java/ql/test/query-tests/security/CWE-078/TaintedEnvironment.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ public void buildProcess() throws java.io.IOException {
1515

1616
pb.environment().put(s, "foo"); // $hasTaintFlow
1717

18+
Map<String, String> extra = Map.of("USER", s);
19+
20+
pb.environment().putAll(extra); // $hasTaintFlow
21+
22+
pb.environment().putIfAbsent("foo", s); // $hasTaintFlow
23+
pb.environment().putIfAbsent(s, "foo"); // $hasTaintFlow
24+
25+
pb.environment().replace("foo", s); // $hasTaintFlow
26+
pb.environment().replace(s, "foo"); // $hasTaintFlow
27+
pb.environment().replace("foo", "bar", s); // $hasTaintFlow
28+
1829
Map<String, String> env = pb.environment();
1930

2031
env.put("foo", s); // $hasTaintFlow

0 commit comments

Comments
 (0)