1313import java
1414import TempDirUtils
1515import DataFlow:: PathGraph
16+ import semmle.code.java.dataflow.TaintTracking2
1617
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" ] ) }
2228}
2329
2430abstract private class FileCreationSink extends DataFlow:: Node { }
@@ -113,7 +119,10 @@ private class TempDirSystemGetPropertyToCreateConfig extends TaintTracking::Conf
113119 isAdditionalFileTaintStep ( node1 , node2 )
114120 }
115121
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+ }
117126
118127 override predicate isSanitizer ( DataFlow:: Node sanitizer ) {
119128 exists ( FilesSanitizingCreationMethodAccess sanitisingMethodAccess |
@@ -122,6 +131,42 @@ private class TempDirSystemGetPropertyToCreateConfig extends TaintTracking::Conf
122131 }
123132}
124133
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+
125170//
126171// Begin configuration for tracking single-method calls that are vulnerable.
127172//
0 commit comments