CVE-2023-48299 (published 2023-11-21T21:15Z)

TorchServe is a tool for serving and scaling PyTorch models in production. Starting in version 0.1.0 and prior to version 0.9.0, using the model/workflow management API, there is a chance of uploading potentially harmful archives that contain files that are extracted to any location on the filesystem that is within the process permissions. Leveraging this issue could aid third-party actors in hiding harmful code in open-source/public models, which can be downloaded from the internet, and take advantage of machines running Torchserve. The ZipSlip issue in TorchServe has been fixed by validating the paths of files contained within a zip archive before extracting them. TorchServe release 0.9.0 includes fixes to address the ZipSlip vulnerability.

CVSS v3 base score: 5.3, MEDIUM (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:L/A:N)

Weakness information:
cwe_id: CWE-22
cwe_name: Improper Limitation of a Pathname to a Restricted Directory ('Path Traversal')
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.
extended_description: 
            Many file operations are intended to take place within a restricted directory. By using special elements such as ".." and "/" separators, attackers can escape outside of the restricted location to access files or directories that are elsewhere on the system. One of the most common special elements is the "../" sequence, which in most modern operating systems is interpreted as the parent directory of the current location. This is referred to as relative path traversal. Path traversal also covers the use of absolute pathnames such as "/usr/local/bin" to access unexpected files. This is referred to as absolute path traversal.
         
         

Fix information:
repo_url: https://github.com/pytorch/serve
commit_details: repo_url: https://github.com/pytorch/serve
author: Mark Saroufim
author_date: 2023-09-29 16:46:40-07:00
msg: fix zip slip error (#2634)
repository: repo_url: https://github.com/pytorch/serve
repo_name: pytorch/serve
description: Serve, optimize and scale PyTorch models in production
repo_language: Java
owner: pytorch
file_changes: filename: ZipUtils.java
change_type: ModificationType.MODIFY
diff: @@ -28,8 +28,15 @@ public final class ZipUtils {
         try (ZipInputStream zis = new ZipInputStream(is)) {
             ZipEntry entry;
             while ((entry = zis.getNextEntry()) != null) {
-                String name = entry.getName();
-                File file = new File(dest, name);
+                File file = new File(dest, entry.getName());
+                File canonicalDestDir = dest.getCanonicalFile();
+                File canonicalFile = file.getCanonicalFile();
+
+                // Check for Zip Slip vulnerability
+                if (!canonicalFile.getPath().startsWith(canonicalDestDir.getPath())) {
+                    throw new IOException("Detected Zip Slip vulnerability: " + entry.getName());
+                }
+
                 if (entry.isDirectory()) {
                     FileUtils.forceMkdir(file);
                 } else {
@@ -108,6 +115,14 @@ public final class ZipUtils {
             while ((entry = tis.getNextEntry()) != null) {
                 String name = entry.getName().substring(entry.getName().indexOf('/') + 1);
                 File file = new File(dest, name);
+                File canonicalDestDir = dest.getCanonicalFile();
+                File canonicalFile = file.getCanonicalFile();
+
+                // Check for Zip Slip vulnerability
+                if (!canonicalFile.getPath().startsWith(canonicalDestDir.getPath())) {
+                    throw new IOException("Detected Zip Slip vulnerability: " + entry.getName());
+                }
+
                 if (entry.isDirectory()) {
                     FileUtils.forceMkdir(file);
                 } else {

programming_language: Java