Skip to content

Commit 3656376

Browse files
authored
Merge pull request #16064 from github/max-schaefer/fix-unexploitable-types
Automodel: Filter unexploitable types in application mode.
2 parents 5253c96 + deb78b2 commit 3656376

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

java/ql/automodel/src/AutomodelApplicationModeCharacteristics.qll

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,27 @@ newtype TApplicationModeEndpoint =
2828
AutomodelJavaUtil::isFromSource(call) and
2929
exists(Argument argExpr |
3030
arg.asExpr() = argExpr and call = argExpr.getCall() and not argExpr.isVararg()
31-
)
31+
) and
32+
not AutomodelJavaUtil::isUnexploitableType(arg.getType())
3233
} or
3334
TInstanceArgument(Call call, DataFlow::Node arg) {
3435
AutomodelJavaUtil::isFromSource(call) and
3536
arg = DataFlow::getInstanceArgument(call) and
36-
not call instanceof ConstructorCall
37+
not call instanceof ConstructorCall and
38+
not AutomodelJavaUtil::isUnexploitableType(arg.getType())
3739
} or
3840
TImplicitVarargsArray(Call call, DataFlow::ImplicitVarargsArray arg, int idx) {
3941
AutomodelJavaUtil::isFromSource(call) and
4042
call = arg.getCall() and
41-
idx = call.getCallee().getVaragsParameterIndex()
43+
idx = call.getCallee().getVaragsParameterIndex() and
44+
not AutomodelJavaUtil::isUnexploitableType(arg.getType())
4245
} or
43-
TMethodReturnValue(Call call) {
46+
TMethodReturnValue(MethodCall call) {
4447
AutomodelJavaUtil::isFromSource(call) and
45-
not call instanceof ConstructorCall
48+
not AutomodelJavaUtil::isUnexploitableType(call.getType())
4649
} or
4750
TOverriddenParameter(Parameter p, Method overriddenMethod) {
4851
AutomodelJavaUtil::isFromSource(p) and
49-
not p.getCallable().callsConstructor(_) and
5052
p.getCallable().(Method).overrides(overriddenMethod)
5153
}
5254

@@ -163,7 +165,7 @@ class ImplicitVarargsArray extends CallArgument, TImplicitVarargsArray {
163165
* may be a source.
164166
*/
165167
class MethodReturnValue extends ApplicationModeEndpoint, TMethodReturnValue {
166-
Call call;
168+
MethodCall call;
167169

168170
MethodReturnValue() { this = TMethodReturnValue(call) }
169171

java/ql/automodel/test/AutomodelApplicationModeExtraction/Test.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ public static void main(String[] args) throws Exception {
1919
AtomicReference<String> reference = new AtomicReference<>(); // uninteresting (parameterless constructor)
2020
reference.set( // $ sinkModelCandidate=set(Object):Argument[this]
2121
args[0] // $ negativeSinkExample=set(Object):Argument[0] // modeled as a flow step
22-
); // $ negativeSourceExample=set(Object):ReturnValue // return type is void
22+
); // not a source candidate (return type is void)
2323
}
2424

2525
public static void callSupplier(Supplier<String> supplier) {
26-
supplier.get(); // $ sourceModelCandidate=get():ReturnValue
26+
supplier.get(); // not a source candidate (lambda flow)
2727
}
2828

2929
public static void copyFiles(Path source, Path target, CopyOption option) throws Exception {
@@ -52,7 +52,7 @@ public static InputStream getInputStream(String openPath, String otherPath) thro
5252
public static int compareFiles(File f1, File f2) {
5353
return f1.compareTo( // $ negativeSinkExample=compareTo(File):Argument[this]
5454
f2 // $ negativeSinkExample=compareTo(File):Argument[0] // modeled as not a sink
55-
); // $ negativeSourceExample=compareTo(File):ReturnValue // return type is int
55+
); // not a source candidate (return type is int)
5656
}
5757

5858
public static void FilesWalkExample(Path p, FileVisitOption o) throws Exception {
@@ -66,6 +66,7 @@ public static void FilesWalkExample(Path p, FileVisitOption o) throws Exception
6666

6767
public static void WebSocketExample(URLConnection c) throws Exception {
6868
c.getInputStream(); // $ sinkModelCandidate=getInputStream():Argument[this] positiveSourceExample=getInputStream():ReturnValue(remote) // not a source candidate (manual modeling)
69+
c.connect(); // $ sinkModelCandidate=connect():Argument[this] // not a source candidate (return type is void)
6970
}
7071

7172
public static void fileFilterExample(File f, FileFilter ff) {
@@ -102,10 +103,10 @@ public static void FilesListExample(Path p) throws Exception {
102103

103104
Files.delete(
104105
p // $ sinkModelCandidate=delete(Path):Argument[0] positiveSinkExample=delete(Path):Argument[0](path-injection)
105-
); // $ negativeSourceExample=delete(Path):ReturnValue // return type is void
106+
); // not a source candidate (return type is void)
106107

107108
Files.deleteIfExists(
108109
p // $ sinkModelCandidate=deleteIfExists(Path):Argument[0] positiveSinkExample=deleteIfExists(Path):Argument[0](path-injection)
109-
); // $ negativeSourceExample=deleteIfExists(Path):ReturnValue // return type is boolean
110+
); // not a source candidate (return type is boolean)
110111
}
111112
}

java/ql/automodel/test/AutomodelFrameworkModeExtraction/com/github/codeql/test/MyWriter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class MyWriter extends java.io.Writer {
44
@Override
5-
public void write(char[] cbuf, int off, int len) { // $ sinkModelCandidate=write(char[],int,int):Argument[this] sourceModelCandidate=write(char[],int,int):Parameter[this] sourceModelCandidate=write(char[],int,int):Parameter[0]
5+
public void write(char[] cbuf, int off, int len) { // $ sinkModelCandidate=write(char[],int,int):Argument[this] positiveSinkExample=write(char[],int,int):Argument[0](file-content-store) sourceModelCandidate=write(char[],int,int):Parameter[this] sourceModelCandidate=write(char[],int,int):Parameter[0]
66
}
77

88
@Override

0 commit comments

Comments
 (0)