From 2ba039f945c54637fb15a6527e56f559b87b35d8 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 4 Nov 2022 15:08:47 +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 --- .../boot/src/main/java/org/wildfly/core/jar/boot/Main.java | 4 ++++ .../main/java/org/jboss/as/cli/handlers/ArchiveHandler.java | 3 +++ patching/src/main/java/org/jboss/as/patching/ZipUtils.java | 2 +- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bootable-jar/boot/src/main/java/org/wildfly/core/jar/boot/Main.java b/bootable-jar/boot/src/main/java/org/wildfly/core/jar/boot/Main.java index e0ae16c91e7..492aa535b41 100644 --- a/bootable-jar/boot/src/main/java/org/wildfly/core/jar/boot/Main.java +++ b/bootable-jar/boot/src/main/java/org/wildfly/core/jar/boot/Main.java @@ -18,6 +18,7 @@ import java.io.BufferedReader; import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.lang.reflect.Method; @@ -210,6 +211,9 @@ private static void unzip(InputStream wf, Path dir) throws Exception { while (ze != null) { String fileName = ze.getName(); Path newFile = dir.resolve(fileName); + if (!newFile.normalize().startsWith(dir.normalize())) { + throw new IOException("Bad zip entry"); + } if (ze.isDirectory()) { Files.createDirectories(newFile); } else { diff --git a/cli/src/main/java/org/jboss/as/cli/handlers/ArchiveHandler.java b/cli/src/main/java/org/jboss/as/cli/handlers/ArchiveHandler.java index 13c33d71b05..ba731b95ef4 100644 --- a/cli/src/main/java/org/jboss/as/cli/handlers/ArchiveHandler.java +++ b/cli/src/main/java/org/jboss/as/cli/handlers/ArchiveHandler.java @@ -163,6 +163,9 @@ private File extractArchive(File archive) throws IOException { for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) { JarEntry entry = entries.nextElement(); File file = new File(tempDir, entry.getName()); + if (!file.toPath().normalize().startsWith(tempDir.toPath().normalize())) { + throw new IOException("Bad zip entry"); + } if (entry.isDirectory()) { file.mkdir(); continue; diff --git a/patching/src/main/java/org/jboss/as/patching/ZipUtils.java b/patching/src/main/java/org/jboss/as/patching/ZipUtils.java index 722b00c6ce5..6ed5833caf3 100644 --- a/patching/src/main/java/org/jboss/as/patching/ZipUtils.java +++ b/patching/src/main/java/org/jboss/as/patching/ZipUtils.java @@ -119,7 +119,7 @@ private static void unzip(final ZipFile zip, final File patchDir) throws IOExcep final String name = entry.getName(); final File current = new File(patchDir, name); final String canonicalDestinationPath = current.getCanonicalPath(); - if (! canonicalDestinationPath.startsWith(patchDir.getCanonicalPath())) { + if (! current.getCanonicalFile().toPath().startsWith(patchDir.getCanonicalFile().toPath())) { throw PatchLogger.ROOT_LOGGER.entryOutsideOfPatchDirectory(canonicalDestinationPath); } if (entry.isDirectory()) {