CVE-2018-25046 (published 2022-12-27T22:15Z)

Due to improper path sanitization, archives containing relative file paths can cause files to be written (or overwritten) outside of the target directory.

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

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/cloudfoundry/archiver
commit_details: repo_url: https://github.com/cloudfoundry/archiver
author: Stephane Jolicoeur
author_date: 2018-05-23 15:22:29-07:00
msg: Refactored the path resolution to use securejoin

[#157757626]

Signed-off-by: Edwin Xie <exie@pivotal.io>
repository: repo_url: https://github.com/cloudfoundry/archiver
repo_name: cloudfoundry/archiver
description: Utilities for extracting and compressing tgz and zip files.
repo_language: Go
owner: cloudfoundry
file_changes: filename: extractor_test.go
change_type: ModificationType.MODIFY
diff: @@ -120,6 +120,24 @@ var _ = Describe("Extractor", func() {
 			})
 
 			It("extracts the ZIP's files, generating directories, and honoring file permissions and symlinks", extractionTest)
+
+			Context("with a bad zip archive", func() {
+				BeforeEach(func() {
+					test_helper.CreateZipArchive(extractionSrc, []test_helper.ArchiveFile{
+						{
+							Name: "../some-file",
+							Body: "file-in-bad-dir-contents",
+						},
+					})
+				})
+
+				It("returns an error", func() {
+					subdir := filepath.Join(extractionDest, "subdir")
+					Expect(os.Mkdir(subdir, 0777)).To(Succeed())
+					err := extractor.Extract(extractionSrc, subdir)
+					Expect(err).To(HaveOccurred())
+				})
+			})
 		})
 
 		Context("when 'unzip' is not in the PATH", func() {
@@ -138,6 +156,27 @@ var _ = Describe("Extractor", func() {
 			})
 
 			It("extracts the ZIP's files, generating directories, and honoring file permissions and symlinks", extractionTest)
+
+			Context("with a bad zip archive", func() {
+				BeforeEach(func() {
+					test_helper.CreateZipArchive(extractionSrc, []test_helper.ArchiveFile{
+						{
+							Name: "../some-file",
+							Body: "file-in-bad-dir-contents",
+						},
+					})
+				})
+
+				It("does not insecurely extract the file outside of the provided destination", func() {
+					subdir := filepath.Join(extractionDest, "subdir")
+					Expect(os.Mkdir(subdir, 0777)).To(Succeed())
+					err := extractor.Extract(extractionSrc, subdir)
+					Expect(err).NotTo(HaveOccurred())
+
+					Expect(filepath.Join(extractionDest, "some-file")).NotTo(BeAnExistingFile())
+					Expect(filepath.Join(subdir, "some-file")).To(BeAnExistingFile())
+				})
+			})
 		})
 	})
 
@@ -153,6 +192,24 @@ var _ = Describe("Extractor", func() {
 			})
 
 			It("extracts the TGZ's files, generating directories, and honoring file permissions and symlinks", extractionTest)
+
+			Context("with a bad tgz archive", func() {
+				BeforeEach(func() {
+					test_helper.CreateTarGZArchive(extractionSrc, []test_helper.ArchiveFile{
+						{
+							Name: "../some-file",
+							Body: "file-in-bad-dir-contents",
+						},
+					})
+				})
+
+				It("returns an error", func() {
+					subdir := filepath.Join(extractionDest, "subdir")
+					Expect(os.Mkdir(subdir, 0777)).To(Succeed())
+					err := extractor.Extract(extractionSrc, subdir)
+					Expect(err).To(HaveOccurred())
+				})
+			})
 		})
 
 		Context("when 'tar' is not in the PATH", func() {
@@ -171,6 +228,26 @@ var _ = Describe("Extractor", func() {
 			})
 
 			It("extracts the TGZ's files, generating directories, and honoring file permissions and symlinks", extractionTest)
+
+			Context("with a bad tgz archive", func() {
+				BeforeEach(func() {
+					test_helper.CreateTarGZArchive(extractionSrc, []test_helper.ArchiveFile{
+						{
+							Name: "../some-file",
+							Body: "file-in-bad-dir-contents",
+						},
+					})
+				})
+
+				It("does not insecurely extract the file outside of the provided destination", func() {
+					subdir := filepath.Join(extractionDest, "subdir")
+					Expect(os.Mkdir(subdir, 0777)).To(Succeed())
+					err := extractor.Extract(extractionSrc, subdir)
+					Expect(err).NotTo(HaveOccurred())
+					Expect(filepath.Join(extractionDest, "some-file")).NotTo(BeAnExistingFile())
+					Expect(filepath.Join(subdir, "some-file")).To(BeAnExistingFile())
+				})
+			})
 		})
 	})
 
@@ -181,5 +258,25 @@ var _ = Describe("Extractor", func() {
 		})
 
 		It("extracts the TAR's files, generating directories, and honoring file permissions and symlinks", extractionTest)
+
+		Context("with a bad tar archive", func() {
+			BeforeEach(func() {
+				test_helper.CreateTarArchive(extractionSrc, []test_helper.ArchiveFile{
+					{
+						Name: "../some-file",
+						Body: "file-in-bad-dir-contents",
+					},
+				})
+			})
+
+			It("does not insecurely extract the file outside of the provided destination", func() {
+				subdir := filepath.Join(extractionDest, "subdir")
+				Expect(os.Mkdir(subdir, 0777)).To(Succeed())
+				err := extractor.Extract(extractionSrc, subdir)
+				Expect(err).NotTo(HaveOccurred())
+				Expect(filepath.Join(extractionDest, "some-file")).NotTo(BeAnExistingFile())
+				Expect(filepath.Join(subdir, "some-file")).To(BeAnExistingFile())
+			})
+		})
 	})
 })

programming_language: Go
filename: tgz_extractor.go
change_type: ModificationType.MODIFY
diff: @@ -8,6 +8,8 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
+
+	securejoin "github.com/cyphar/filepath-securejoin"
 )
 
 type tgzExtractor struct{}
@@ -87,14 +89,17 @@ func extractTarArchive(tarReader *tar.Reader, dest string) error {
 }
 
 func extractTarArchiveFile(header *tar.Header, dest string, input io.Reader) error {
-	filePath := filepath.Join(dest, header.Name)
+	filePath, err := securejoin.SecureJoin(dest, header.Name)
+	if err != nil {
+		return err
+	}
 	fileInfo := header.FileInfo()
 
 	if fileInfo.IsDir() {
 		return os.MkdirAll(filePath, fileInfo.Mode())
 	}
 
-	err := os.MkdirAll(filepath.Dir(filePath), 0755)
+	err = os.MkdirAll(filepath.Dir(filePath), 0755)
 	if err != nil {
 		return err
 	}

programming_language: Go
filename: zip_extractor.go
change_type: ModificationType.MODIFY
diff: @@ -8,6 +8,8 @@ import (
 	"os"
 	"os/exec"
 	"path/filepath"
+
+	securejoin "github.com/cyphar/filepath-securejoin"
 )
 
 type zipExtractor struct{}
@@ -77,16 +79,16 @@ func extractZip(src, dest string) error {
 }
 
 func extractZipArchiveFile(file *zip.File, dest string, input io.Reader) error {
-	filePath := filepath.Join(dest, file.Name)
+	filePath, err := securejoin.SecureJoin(dest, file.Name)
 	fileInfo := file.FileInfo()
 
 	if fileInfo.IsDir() {
-		err := os.MkdirAll(filePath, fileInfo.Mode())
+		err = os.MkdirAll(filePath, fileInfo.Mode())
 		if err != nil {
 			return err
 		}
 	} else {
-		err := os.MkdirAll(filepath.Dir(filePath), 0755)
+		err = os.MkdirAll(filepath.Dir(filePath), 0755)
 		if err != nil {
 			return err
 		}

programming_language: Go