13
13
import java
14
14
import TempDirUtils
15
15
import DataFlow:: PathGraph
16
+ import semmle.code.java.dataflow.TaintTracking2
16
17
17
- private class MethodFileSystemFileCreation extends Method {
18
- MethodFileSystemFileCreation ( ) {
19
- this .getDeclaringType ( ) instanceof TypeFile and
20
- this .hasName ( [ "mkdir" , "mkdirs" , "createNewFile" ] )
21
- }
18
+ abstract private class MethodFileSystemFileCreation extends Method {
19
+ MethodFileSystemFileCreation ( ) { this .getDeclaringType ( ) instanceof TypeFile }
20
+ }
21
+
22
+ private class MethodFileDirectoryCreation extends MethodFileSystemFileCreation {
23
+ MethodFileDirectoryCreation ( ) { this .hasName ( [ "mkdir" , "mkdirs" ] ) }
24
+ }
25
+
26
+ private class MethodFileFileCreation extends MethodFileSystemFileCreation {
27
+ MethodFileFileCreation ( ) { this .hasName ( [ "createNewFile" ] ) }
22
28
}
23
29
24
30
abstract private class FileCreationSink extends DataFlow:: Node { }
@@ -113,7 +119,10 @@ private class TempDirSystemGetPropertyToCreateConfig extends TaintTracking::Conf
113
119
isAdditionalFileTaintStep ( node1 , node2 )
114
120
}
115
121
116
- override predicate isSink ( DataFlow:: Node sink ) { sink instanceof FileCreationSink }
122
+ override predicate isSink ( DataFlow:: Node sink ) {
123
+ sink instanceof FileCreationSink and
124
+ exists ( TempDirSystemGetPropertyDirectlyToMkdirConfig config | not config .hasFlowTo ( sink ) )
125
+ }
117
126
118
127
override predicate isSanitizer ( DataFlow:: Node sanitizer ) {
119
128
exists ( FilesSanitizingCreationMethodAccess sanitisingMethodAccess |
@@ -122,6 +131,42 @@ private class TempDirSystemGetPropertyToCreateConfig extends TaintTracking::Conf
122
131
}
123
132
}
124
133
134
+ /**
135
+ * Configuration that tracks calls to to `mkdir` or `mkdirs` that are are directly on the temp directory system property.
136
+ * Examples:
137
+ * - `File tempDir = new File(System.getProperty("java.io.tmpdir")); tempDir.mkdir();`
138
+ * - `File tempDir = new File(System.getProperty("java.io.tmpdir")); tempDir.mkdirs();`
139
+ *
140
+ * These are examples of code that is simply verifying that the temp directory exists.
141
+ * As such, this code pattern is filtered out as an explicit vulnerability in
142
+ * `TempDirSystemGetPropertyToCreateConfig::isSink`.
143
+ */
144
+ private class TempDirSystemGetPropertyDirectlyToMkdirConfig extends TaintTracking2:: Configuration {
145
+ TempDirSystemGetPropertyDirectlyToMkdirConfig ( ) {
146
+ this = "TempDirSystemGetPropertyDirectlyToMkdirConfig"
147
+ }
148
+
149
+ override predicate isSource ( DataFlow:: Node node ) {
150
+ exists (
151
+ MethodAccessSystemGetPropertyTempDirTainted propertyGetMethodAccess , DataFlow:: Node callSite
152
+ |
153
+ DataFlow:: localFlow ( DataFlow:: exprNode ( propertyGetMethodAccess ) , callSite )
154
+ |
155
+ isFileConstructorArgument ( callSite .asExpr ( ) , node .asExpr ( ) , 1 )
156
+ )
157
+ }
158
+
159
+ override predicate isSink ( DataFlow:: Node node ) {
160
+ exists ( MethodAccess ma | ma .getMethod ( ) instanceof MethodFileDirectoryCreation |
161
+ ma .getQualifier ( ) = node .asExpr ( )
162
+ )
163
+ }
164
+
165
+ override predicate isSanitizer ( DataFlow:: Node sanitizer ) {
166
+ isFileConstructorArgument ( sanitizer .asExpr ( ) , _, _)
167
+ }
168
+ }
169
+
125
170
//
126
171
// Begin configuration for tracking single-method calls that are vulnerable.
127
172
//
0 commit comments