From 51ad537d9bd7688fb0a62bc4f1b9baac57f8b3ff Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Mon, 3 Oct 2022 21:24:29 +0000 Subject: [PATCH] vuln-fix: Zip Slip Vulnerability This fixes a Zip-Slip vulnerability. This change does one of two things. This change either 1. Inserts a guard to protect against Zip Slip. OR 2. Replaces `dir.getCanonicalPath().startsWith(parent.getCanonicalPath())`, which is vulnerable to partial path traversal attacks, with the more secure `dir.getCanonicalFile().toPath().startsWith(parent.getCanonicalFile().toPath())`. For number 2, consider `"/usr/outnot".startsWith("/usr/out")`. The check is bypassed although `/outnot` is not under the `/out` directory. It's important to understand that the terminating slash may be removed when using various `String` representations of the `File` object. For example, on Linux, `println(new File("/var"))` will print `/var`, but `println(new File("/var", "/")` will print `/var/`; however, `println(new File("/var", "/").getCanonicalPath())` will print `/var`. Weakness: CWE-22: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') Severity: High CVSSS: 7.4 Detection: CodeQL (https://codeql.github.com/codeql-query-help/java/java-zipslip/) & OpenRewrite (https://public.moderne.io/recipes/org.openrewrite.java.security.ZipSlip) Reported-by: Jonathan Leitschuh Signed-off-by: Jonathan Leitschuh Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/16 Co-authored-by: Moderne --- .../main/java/com/kumuluz/ee/loader/EeClassLoader.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tools/loader/src/main/java/com/kumuluz/ee/loader/EeClassLoader.java b/tools/loader/src/main/java/com/kumuluz/ee/loader/EeClassLoader.java index e071588e..c6f4d839 100644 --- a/tools/loader/src/main/java/com/kumuluz/ee/loader/EeClassLoader.java +++ b/tools/loader/src/main/java/com/kumuluz/ee/loader/EeClassLoader.java @@ -173,7 +173,7 @@ private File createFile(JarEntry jarEntry, InputStream is) throws EeClassLoaderE File tmpFile = null; try { if (jarEntry.isDirectory()) { - tmpFile = new File(tempDir + File.separator + jarEntry); + tmpFile = new File(tempDir, jarEntry); tmpFile.mkdirs(); tmpFile.deleteOnExit(); chmod777(tmpFile); @@ -184,13 +184,17 @@ private File createFile(JarEntry jarEntry, InputStream is) throws EeClassLoaderE int lastPathIndex = jarEntry.getName().lastIndexOf("/"); if (lastPathIndex > -1) { String dirPath = jarEntry.getName().substring(0, lastPathIndex); - tempDir = new File(tempDir.getPath() + File.separator + dirPath); + tempDir = new File(tempDir.getPath(), dirPath); tempDir.mkdirs(); tempDir.deleteOnExit(); chmod777(tempDir); fileName = fileName.substring(lastPathIndex + 1); } - tmpFile = new File(tempDir + File.separator + fileName); + final File zipEntryFile = new File(tempDir, fileName); + if (!zipEntryFile.toPath().normalize().startsWith(tempDir.toPath().normalize())) { + throw new IOException("Bad zip entry"); + } + tmpFile = zipEntryFile; tmpFile.deleteOnExit(); chmod777(tmpFile); // Unix - allow temp file deletion by any user BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile));