Skip to content

Commit fea9f5f

Browse files
authored
Merge pull request github#5746 from owen-mc/java/refactor-exec-tainted
Make ExecTainted easier to extend
2 parents a7cc9f9 + 8a01799 commit fea9f5f

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

java/ql/src/semmle/code/java/JDK.qll

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
import Member
6+
import semmle.code.java.security.ExternalProcess
67

78
// --- Standard types ---
89
/** The class `java.lang.Object`. */
@@ -176,24 +177,37 @@ class TypeFile extends Class {
176177
}
177178

178179
// --- Standard methods ---
180+
/**
181+
* Any constructor of class `java.lang.ProcessBuilder`.
182+
*/
183+
class ProcessBuilderConstructor extends Constructor, ExecCallable {
184+
ProcessBuilderConstructor() { this.getDeclaringType() instanceof TypeProcessBuilder }
185+
186+
override int getAnExecutedArgument() { result = 0 }
187+
}
188+
179189
/**
180190
* Any of the methods named `command` on class `java.lang.ProcessBuilder`.
181191
*/
182-
class MethodProcessBuilderCommand extends Method {
192+
class MethodProcessBuilderCommand extends Method, ExecCallable {
183193
MethodProcessBuilderCommand() {
184194
hasName("command") and
185195
getDeclaringType() instanceof TypeProcessBuilder
186196
}
197+
198+
override int getAnExecutedArgument() { result = 0 }
187199
}
188200

189201
/**
190202
* Any method named `exec` on class `java.lang.Runtime`.
191203
*/
192-
class MethodRuntimeExec extends Method {
204+
class MethodRuntimeExec extends Method, ExecCallable {
193205
MethodRuntimeExec() {
194206
hasName("exec") and
195207
getDeclaringType() instanceof TypeRuntime
196208
}
209+
210+
override int getAnExecutedArgument() { result = 0 }
197211
}
198212

199213
/**
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
/* Definitions related to the Apache Commons Exec library. */
22
import semmle.code.java.Type
3+
import semmle.code.java.security.ExternalProcess
34

4-
library class TypeCommandLine extends Class {
5+
/** The class `org.apache.commons.exec.CommandLine`. */
6+
private class TypeCommandLine extends Class {
57
TypeCommandLine() { hasQualifiedName("org.apache.commons.exec", "CommandLine") }
68
}
79

8-
library class MethodCommandLineParse extends Method {
10+
/** The `parse()` method of the class `org.apache.commons.exec.CommandLine`. */
11+
private class MethodCommandLineParse extends Method, ExecCallable {
912
MethodCommandLineParse() {
1013
getDeclaringType() instanceof TypeCommandLine and
1114
hasName("parse")
1215
}
16+
17+
override int getAnExecutedArgument() { result = 0 }
1318
}
1419

15-
library class MethodCommandLineAddArguments extends Method {
20+
/** The `addArguments()` method of the class `org.apache.commons.exec.CommandLine`. */
21+
private class MethodCommandLineAddArguments extends Method, ExecCallable {
1622
MethodCommandLineAddArguments() {
1723
getDeclaringType() instanceof TypeCommandLine and
1824
hasName("addArguments")
1925
}
26+
27+
override int getAnExecutedArgument() { result = 0 }
2028
}

java/ql/src/semmle/code/java/security/ExternalProcess.qll

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
/* Definitions related to external processes. */
22
import semmle.code.java.Member
3-
import semmle.code.java.JDK
4-
import semmle.code.java.frameworks.apache.Exec
3+
4+
private module Instances {
5+
private import semmle.code.java.JDK
6+
private import semmle.code.java.frameworks.apache.Exec
7+
}
8+
9+
/**
10+
* A callable that executes a command.
11+
*/
12+
abstract class ExecCallable extends Callable {
13+
/**
14+
* Gets the index of an argument that will be part of the command that is executed.
15+
*/
16+
abstract int getAnExecutedArgument();
17+
}
518

619
/**
720
* An expression used as an argument to a call that executes an external command. For calls to
@@ -10,21 +23,10 @@ import semmle.code.java.frameworks.apache.Exec
1023
*/
1124
class ArgumentToExec extends Expr {
1225
ArgumentToExec() {
13-
exists(MethodAccess execCall, Method method |
14-
execCall.getArgument(0) = this and
15-
method = execCall.getMethod() and
16-
(
17-
method instanceof MethodRuntimeExec or
18-
method instanceof MethodProcessBuilderCommand or
19-
method instanceof MethodCommandLineParse or
20-
method instanceof MethodCommandLineAddArguments
21-
)
22-
)
23-
or
24-
exists(ConstructorCall expr, Constructor cons |
25-
expr.getConstructor() = cons and
26-
cons.getDeclaringType().hasQualifiedName("java.lang", "ProcessBuilder") and
27-
expr.getArgument(0) = this
26+
exists(Call execCall, ExecCallable execCallable, int i |
27+
execCall.getArgument(pragma[only_bind_into](i)) = this and
28+
execCallable = execCall.getCallee() and
29+
i = execCallable.getAnExecutedArgument()
2830
)
2931
}
3032
}

0 commit comments

Comments
 (0)