Skip to content

Commit 2048aed

Browse files
committed
Review feedback and improve temp dir vulnerable/safe code sugestion
1 parent 76964d5 commit 2048aed

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

java/ql/src/Security/CWE/CWE-200/TempDirUsageSafe.java

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.io.File;
2+
import java.io.IOException;
13
import java.nio.file.Files;
24
import java.nio.file.attribute.PosixFilePermission;
35
import java.nio.file.attribute.PosixFilePermissions;
@@ -9,11 +11,90 @@ void exampleSafe() throws IOException {
911

1012
Path temp2 = Files.createTempDirectory("random-directory"); // GOOD: File has permissions `drwx------`
1113

12-
File tempDirChildFile = new File(System.getProperty("java.io.tmpdir"), "/child-create-file.txt");
14+
// Creating a temporary file with a non-randomly generated name
15+
File tempChildFile = new File(System.getProperty("java.io.tmpdir"), "/child-create-file.txt");
16+
// Warning: This will fail on windows as it doesn't support PosixFilePermissions.
17+
// See `exampleSafeWithWindowsSupportFile` if your code needs to support windows and unix-like systems.
1318
Files.createFile(
14-
tempDirChildFile.toPath(),
15-
tempDirChild.toPath(),
19+
tempChildFile.toPath(),
1620
PosixFilePermissions.asFileAttribute(EnumSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE))
1721
); // GOOD: Good has permissions `-rw-------`
1822
}
23+
24+
/*
25+
* An example of a safe use of createFile or createDirectory if your code must support windows and unix-like systems.
26+
*/
27+
void exampleSafeWithWindowsSupportFile() {
28+
// Creating a temporary file with a non-randomly generated name
29+
File tempChildFile = new File(System.getProperty("java.io.tmpdir"), "/child-create-file.txt");
30+
createTempFile(tempChildFile.toPath()); // GOOD: Good has permissions `-rw-------`
31+
}
32+
33+
static void createTempFile(Path tempDir) {
34+
try {
35+
if (tempDirChild.getFileSystem().supportedFileAttributeViews().contains("posix")) {
36+
// Explicit permissions setting is only required on unix-like systems because
37+
// the temporary directory is shared between all users.
38+
// This is not necessary on Windows, each user has their own temp directory
39+
final EnumSet<PosixFilePermission> posixFilePermissions =
40+
EnumSet.of(
41+
PosixFilePermission.OWNER_READ,
42+
PosixFilePermission.OWNER_WRITE
43+
);
44+
if (!Files.exists(tempDirChild)) {
45+
Files.createFile(
46+
tempDirChild,
47+
PosixFilePermissions.asFileAttribute(posixFilePermissions)
48+
); // GOOD: Directory has permissions `-rw-------`
49+
} else {
50+
Files.setPosixFilePermissions(
51+
tempDirChild,
52+
posixFilePermissions
53+
); // GOOD: Good has permissions `-rw-------`, or will throw an exception if this fails
54+
}
55+
} else if (!Files.exists(tempDirChild)) {
56+
// On Windows, we still need to create the directory, when it doesn't already exist.
57+
Files.createDirectory(tempDirChild); // GOOD: Windows doesn't share the temp directory between users
58+
}
59+
} catch (IOException exception) {
60+
throw new UncheckedIOException("Failed to create temp file", exception);
61+
}
62+
}
63+
64+
void exampleSafeWithWindowsSupportDirectory() {
65+
File tempDirChildDir = new File(System.getProperty("java.io.tmpdir"), "/child-dir");
66+
createTempDirectories(tempDirChildDir.toPath()); // GOOD: Directory has permissions `drwx------`
67+
}
68+
69+
static void createTempDirectories(Path tempDirChild) {
70+
try {
71+
if (tempDirChild.getFileSystem().supportedFileAttributeViews().contains("posix")) {
72+
// Explicit permissions setting is only required on unix-like systems because
73+
// the temporary directory is shared between all users.
74+
// This is not necessary on Windows, each user has their own temp directory
75+
final EnumSet<PosixFilePermission> posixFilePermissions =
76+
EnumSet.of(
77+
PosixFilePermission.OWNER_READ,
78+
PosixFilePermission.OWNER_WRITE,
79+
PosixFilePermission.OWNER_EXECUTE
80+
);
81+
if (!Files.exists(tempDirChild)) {
82+
Files.createDirectories(
83+
tempDirChild,
84+
PosixFilePermissions.asFileAttribute(posixFilePermissions)
85+
); // GOOD: Directory has permissions `drwx------`
86+
} else {
87+
Files.setPosixFilePermissions(
88+
tempDirChild,
89+
posixFilePermissions
90+
); // GOOD: Good has permissions `drwx------`, or will throw an exception if this fails
91+
}
92+
} else if (!Files.exists(tempDirChild)) {
93+
// On Windows, we still need to create the directory, when it doesn't already exist.
94+
Files.createDirectories(tempDirChild); // GOOD: Windows doesn't share the temp directory between users
95+
}
96+
} catch (IOException exception) {
97+
throw new UncheckedIOException("Failed to create temp dir", exception);
98+
}
99+
}
19100
}

java/ql/src/Security/CWE/CWE-200/TempDirUsageVulnerable.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ void exampleVulnerable() {
1515

1616
File tempDirChildFile = new File(System.getProperty("java.io.tmpdir"), "/child-create-file.txt");
1717
Files.createFile(tempDirChildFile.toPath()); // BAD: File has permissions `-rw-r--r--`
18+
19+
File tempDirChildDir = new File(System.getProperty("java.io.tmpdir"), "/child-dir");
20+
tempDirChildDir.mkdir(); // BAD: Directory has permissions `drwxr-xr-x`
21+
Files.createDirectory(tempDirChildDir.toPath()); // BAD: Directory has permissions `drwxr-xr-x`
1822
}
1923
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
category: newQuery
33
---
4-
* A new query titled "Temporary directory Local information disclosure" (`java/local-temp-file-or-directory-information-disclosure`) has been added.
4+
* A new query titled "Local information disclosure in a temporary directory" (`java/local-temp-file-or-directory-information-disclosure`) has been added.
55
This query finds uses of APIs that leak potentially sensitive information to other local users via the system temporary directory.
66
This query was originally [submitted as query by @JLLeitschuh](https://github.com/github/codeql/pull/4388).

0 commit comments

Comments
 (0)