From 29a3beb1f83488763085af5dd4a592f05828c63e Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Tue, 4 Oct 2022 00:21:02 +0000 Subject: [PATCH] vuln-fix: Temporary Directory Hijacking or Information Disclosure This fixes either Temporary Directory Hijacking, or Temporary Directory Local Information Disclosure. Weakness: CWE-379: Creation of Temporary File in Directory with Insecure Permissions Severity: High CVSSS: 7.3 Detection: CodeQL & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.UseFilesCreateTempDirectory) Reported-by: Jonathan Leitschuh Signed-off-by: Jonathan Leitschuh Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/10 Co-authored-by: Moderne --- src/main/java/org/bridj/Platform.java | 5 +++-- src/test/java/org/bridj/NativeLibraryTest.java | 5 ++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/bridj/Platform.java b/src/main/java/org/bridj/Platform.java index 16ad0a6d..651f5e97 100644 --- a/src/main/java/org/bridj/Platform.java +++ b/src/main/java/org/bridj/Platform.java @@ -40,6 +40,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; @@ -710,8 +711,8 @@ static File extractEmbeddedLibraryResource(String name) throws IOException { static File createTempDir(String prefix) throws IOException { File dir; for (int i = 0; i < maxTempFileAttempts; i++) { - dir = File.createTempFile(prefix, ""); - if (dir.delete() && dir.mkdirs()) { + dir = Files.createTempDirectory(prefix).toFile(); + if (true) { return dir; } } diff --git a/src/test/java/org/bridj/NativeLibraryTest.java b/src/test/java/org/bridj/NativeLibraryTest.java index 099c142e..494c41c8 100644 --- a/src/test/java/org/bridj/NativeLibraryTest.java +++ b/src/test/java/org/bridj/NativeLibraryTest.java @@ -35,6 +35,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.file.Files; import org.bridj.ann.Library; import org.bridj.ann.Runtime; @@ -46,9 +47,7 @@ public class NativeLibraryTest { static File tempDir() throws IOException { - File f = File.createTempFile("bridj", "natlibtest"); - f.delete(); - f.mkdir(); + File f = Files.createTempDirectory("bridj" + "natlibtest").toFile(); f.deleteOnExit(); return f; }