CVE-2022-21675: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: Bytecode Viewer (BCV) is a Java/Android reverse engineering suite. Versions of the package prior to 2.11.0 are vulnerable to Arbitrary File Write via Archive Extraction (AKA "Zip Slip"). The vulnerability is exploited using a specially crafted archive that holds directory traversal filenames (e.g. ../../evil.exe). The Zip Slip vulnerability can affect numerous archive formats, including zip, jar, tar, war, cpio, apk, rar and 7z. The attacker can then overwrite executable files and either invoke them remotely or wait for the system or user to call them, thus achieving remote command execution on the victim’s machine. The impact of a Zip Slip vulnerability would allow an attacker to create or overwrite existing files on the filesystem. In the context of a web application, a web shell could be placed within the application directory to achieve code execution. All users should upgrade to BCV v2.11.0 when possible to receive a patch. There are no recommended workarounds aside from upgrading.
CWE description: The product uses external input to construct a pathname that is intended to identify a file or directory that is located underneath a restricted parent directory, but the product does not properly neutralize special elements within the pathname that can cause the pathname to resolve to a location that is outside of the restricted directory.
CVSS v3 base score: 7.8
Published: 2022-01-12T19:15Z
Repository: https://github.com/Konloch/bytecode-viewer
Language: Java
Files changed: src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java
Commit date: 2022-01-07 21:52:01 +0100
Commit message: Improve Zip Slip detection


Diff: diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java
index f7e9a648..9b28af92 100644
--- a/src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java
+++ b/src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java
@@ -42,10 +42,15 @@ public final class ZipUtils {
      * @param jarPath        the zip file name
      * @param destinationDir the file extract path
      * @throws IOException Signals that an I/O exception has occurred.
      */
     public static void unzipFilesToPath(String jarPath, String destinationDir) throws IOException {
+        String canonicalDestDir = new File(destinationDir).getCanonicalPath();
+        if (!canonicalDestDir.endsWith(File.separator)) {
+            canonicalDestDir += File.separator;
+        }
+
         File file = new File(jarPath);
         try (JarFile jar = new JarFile(file)) {
 
             // fist get all directories,
             // then make those directory on the destination Path
@@ -66,11 +71,11 @@ public final class ZipUtils {
                 JarEntry entry = enums.nextElement();
 
                 String fileName = destinationDir + File.separator + entry.getName();
                 File f = new File(fileName);
 
-                if (!f.getCanonicalPath().startsWith(destinationDir)) {
+                if (!f.getCanonicalPath().startsWith(canonicalDestDir)) {
                     System.out.println("Zip Slip exploit detected. Skipping entry " + entry.getName());
                     continue;
                 }
 
                 File parent = f.getParentFile();

Vulnerable code:                 if (!f.getCanonicalPath().startsWith(destinationDir)) {
Fixed code:         String canonicalDestDir = new File(destinationDir).getCanonicalPath();
        if (!canonicalDestDir.endsWith(File.separator)) {
            canonicalDestDir += File.separator;
        }

                if (!f.getCanonicalPath().startsWith(canonicalDestDir)) {