Skip to content

Commit a41f28e

Browse files
joefarebrotheratorralba
authored andcommitted
Use more file openning methods
1 parent 58fba20 commit a41f28e

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

java/ql/lib/semmle/code/java/frameworks/android/ExternalStorage.qll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/** Provides definitions for working with uses of Android external storage */
22

33
import java
4+
private import semmle.code.java.security.FileReadWrite
45
private import semmle.code.java.dataflow.DataFlow
56
private import semmle.code.java.dataflow.ExternalFlow
67

@@ -41,10 +42,9 @@ private predicate externalStorageFlow(DataFlow::Node node1, DataFlow::Node node2
4142
* This is controlable by third-party applications, so is treated as a remote flow source.
4243
*/
4344
predicate androidExternalStorageSource(DataFlow::Node n) {
44-
exists(ConstructorCall fInp, DataFlow::Node externalDir |
45-
fInp.getConstructedType().hasQualifiedName("java.io", "FileInputStream") and
46-
n.asExpr() = fInp and
45+
exists(DataFlow::Node externalDir, DirectFileReadExpr read |
4746
sourceNode(externalDir, "android-external-storage-dir") and
48-
externalStorageFlow(externalDir, DataFlow::exprNode(fInp.getArgument(0)))
47+
n.asExpr() = read and
48+
externalStorageFlow(externalDir, DataFlow::exprNode(read.getFileExpr()))
4949
)
5050
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import java
22

33
/**
4-
* Holds if `fileAccess` is used in the `fileReadingExpr` to read the represented file.
4+
* Holds if `fileAccess` is directly used in the `fileReadingExpr` to read the represented file.
55
*/
6-
private predicate fileRead(VarAccess fileAccess, Expr fileReadingExpr) {
6+
predicate directFileRead(Expr fileAccess, Expr fileReadingExpr) {
77
// `fileAccess` used to construct a class that reads a file.
88
exists(ClassInstanceExpr cie |
99
cie = fileReadingExpr and
@@ -28,6 +28,13 @@ private predicate fileRead(VarAccess fileAccess, Expr fileReadingExpr) {
2828
])
2929
)
3030
)
31+
}
32+
33+
/**
34+
* Holds if `fileAccess` is used in the `fileReadingExpr` to read the represented file.
35+
*/
36+
private predicate fileRead(VarAccess fileAccess, Expr fileReadingExpr) {
37+
directFileRead(fileAccess, fileReadingExpr)
3138
or
3239
// The `fileAccess` is used in a call which directly or indirectly accesses the file.
3340
exists(Call call, int parameterPos, VarAccess nestedFileAccess, Expr nestedFileReadingExpr |
@@ -49,3 +56,15 @@ class FileReadExpr extends Expr {
4956
*/
5057
VarAccess getFileVarAccess() { fileRead(result, this) }
5158
}
59+
60+
/**
61+
* An expression that directly reads from a file and returns its contents.
62+
*/
63+
class DirectFileReadExpr extends Expr {
64+
DirectFileReadExpr() { directFileRead(_, this) }
65+
66+
/**
67+
* Gets the `Expr` representing the file that is read
68+
*/
69+
Expr getFileExpr() { directFileRead(result, this) }
70+
}

java/ql/test/library-tests/frameworks/android/external-storage/Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import java.io.InputStream;
33
import java.io.FileInputStream;
44
import java.io.IOException;
5+
import java.io.FileReader;
6+
import java.io.RandomAccessFile;
57
import android.content.Context;
68
import android.os.Environment;
79

@@ -48,4 +50,10 @@ void test7(Context ctx) throws IOException {
4850
File f = new File(dir, "file.txt");
4951
sink(new FileInputStream(f)); // $ hasTaintFlow
5052
}
53+
54+
void test8() throws IOException {
55+
File f = new File(dir, "file.txt");
56+
sink(new FileReader(f)); // $ hasTaintFlow
57+
sink(new RandomAccessFile(f, "r")); // $ hasTaintFlow
58+
}
5159
}

0 commit comments

Comments
 (0)