CVE-2022-4583: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal') (CWE-22)
value: A vulnerability was found in jLEMS. It has been declared as critical. Affected by this vulnerability is the function unpackJar of the file src/main/java/org/lemsml/jlems/io/util/JUtil.java. The manipulation leads to path traversal. The attack can be launched remotely. The name of the patch is 8c224637d7d561076364a9e3c2c375daeaf463dc. It is recommended to apply a patch to fix this issue. The identifier VDB-216169 was assigned to this vulnerability.
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: 8.8
Published: 2022-12-17T13:15Z
Repository: https://github.com/LEMS/jLEMS
Language: Java
Files changed: src/main/java/org/lemsml/jlems/io/util/JUtil.java
Commit date: 2022-07-30 01:39:58 +0000
Commit message: 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 <Jonathan.Leitschuh@gmail.com>
Signed-off-by: Jonathan Leitschuh <Jonathan.Leitschuh@gmail.com>

Bug-tracker: https://github.com/JLLeitschuh/security-research/issues/16

Co-authored-by: Moderne <team@moderne.io>
Diff: diff --git a/src/main/java/org/lemsml/jlems/io/util/JUtil.java b/src/main/java/org/lemsml/jlems/io/util/JUtil.java
index 8cff03f..44eceff 100644
--- a/src/main/java/org/lemsml/jlems/io/util/JUtil.java
+++ b/src/main/java/org/lemsml/jlems/io/util/JUtil.java
@@ -203,10 +203,13 @@ public final class JUtil {
       Enumeration<JarEntry> en = jf.entries();
 
       while (en.hasMoreElements()) {
          JarEntry je = en.nextElement();
          java.io.File f = new File(fout,  je.getName());
+							if (!f.toPath().normalize().startsWith(fout.toPath().normalize())) {
+								throw new RuntimeException("Bad zip entry");
+							}
          if (je.isDirectory()) {
             f.mkdirs();
             continue;
 
          } else {

Fixed code: 							if (!f.toPath().normalize().startsWith(fout.toPath().normalize())) {
								throw new RuntimeException("Bad zip entry");
							}