From 1958ce646e90f2f68ef00ab9a866699bf3b3349b Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 29 Jul 2022 19:16:12 +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 --- src/main/java/fr/lebonq/minecraft/Fabric.java | 9 ++++++--- .../fr/lebonq/minecraft/libraries/LibrarieNative.java | 7 +++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/java/fr/lebonq/minecraft/Fabric.java b/src/main/java/fr/lebonq/minecraft/Fabric.java index a35b45b..1dd978c 100644 --- a/src/main/java/fr/lebonq/minecraft/Fabric.java +++ b/src/main/java/fr/lebonq/minecraft/Fabric.java @@ -56,9 +56,12 @@ public void getZipFabric(String pMcVersion,File pVersionPath,File pMcJar,AppCont ZipEntry vNext = vEntries.nextElement(); AppController.LOGGER.log(Level.INFO,"{}",vNext.getName()); - File vNextFolder = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")) + "/" + vNext.getName().substring(0, vNext.getName().lastIndexOf("/"))); + File vNextFolder = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")), vNext.getName().substring(0, vNext.getName().lastIndexOf("/"))); vNextFolder.mkdirs(); - File vNextFile = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")) + "/" + vNext.getName()); //On cree un fichier temporaire + File vNextFile = new File(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")), vNext.getName()); + if(!vNextFile.toPath().normalize().startsWith(pVersionPath.getAbsolutePath().substring(0, pVersionPath.getAbsolutePath().lastIndexOf("\\")))) { + throw new RuntimeException("Bad zip entry"); + } //On cree un fichier temporaire if(!(vEntries.hasMoreElements())){ if(vNextFile.exists()){ @@ -101,4 +104,4 @@ public File getClientJar() { } -} \ No newline at end of file +} diff --git a/src/main/java/fr/lebonq/minecraft/libraries/LibrarieNative.java b/src/main/java/fr/lebonq/minecraft/libraries/LibrarieNative.java index b966fe8..2b766a3 100644 --- a/src/main/java/fr/lebonq/minecraft/libraries/LibrarieNative.java +++ b/src/main/java/fr/lebonq/minecraft/libraries/LibrarieNative.java @@ -51,10 +51,13 @@ public void extractNative(File pBin) { if(vEntry.isDirectory()){//Si c'est un directory on recupere tout dedans avant d'aller plus loins } else{ - File vFileOut = new File(pBin + "/" +vEntry.getName()); + File vFileOut = new File(pBin, vEntry.getName()); + if(!vFileOut.toPath().normalize().startsWith(pBin.toPath())) { + throw new RuntimeException("Bad zip entry"); + } vFileOut.deleteOnExit(); if(vEntry.getName().lastIndexOf("/") != -1){//Si == -1 alors c'est un fichier sans dossier - File vFileFolder = new File(pBin + "/" + vEntry.getName().substring(0, vEntry.getName().lastIndexOf("/")));//to create folder without file name + File vFileFolder = new File(pBin, vEntry.getName().substring(0, vEntry.getName().lastIndexOf("/")));//to create folder without file name vFileFolder.mkdirs(); vFileFolder.deleteOnExit();//On supprimer le fichier a la du process launcher }