CVE-2024-45436 - Ollama Zip Slip Vulnerability in extractFromZipFile Function (Prior to .1.47)
A critical vulnerability, dubbed as CVE-2024-45436[^1^], has been discovered in the Ollama library before version .1.47. The vulnerability exists in the extractFromZipFile function within the model.go file[^2^]. This insecure implementation allows malicious actors to potentially conduct a Zip Slip attack[^3^]. The attack enables an attacker to extract files from a ZIP archive outside the intended directory, potentially leading to privilege escalation, directory traversal, and other security issues.
Exploit Details
The vulnerability lies in the extractFromZipFile function in the model.go file and stems from improper sanitization of user-supplied input. The Ollama library uses this function to extract files from a ZIP archive. However, due to the insecure implementation, an attacker can craft a malicious archive that includes files with directory traversal characters such as '..' or '../../'. When processed by the Ollama library, the attacker's files can be extracted outside the intended target directory.
The code snippet below shows the vulnerable extractFromZipFile function
package main
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
func extractFromZipFile(zipFileName string, destDirectory string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(zipFileName)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
fpath := filepath.Join(destDirectory, f.Name)
// This line is insecure!
if !strings.HasPrefix(fpath, filepath.Clean(destDirectory)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
} else {
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
rc, err := f.Open()
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return filenames, err
}
}
}
return filenames, nil
}
Fix and Mitigation
To resolve this vulnerability, developers using the Ollama library version before .1.47 should update their library to version .1.47 or later. The developers of the Ollama library have addressed the vulnerability by properly sanitizing the user-supplied input and validating the extracted file paths.
In addition to updating the library, developers can also mitigate this security risk by ensuring that all user-inputted data, including ZIP archives, undergo proper validation and sanitization before being processed by the application.
Original References
[^1^]: CVE-2024-45436
[^2^]: Ollama GitHub Repository
[^3^]: Zip Slip vulnerability explanation by Snyk
Timeline
Published on: 08/29/2024 03:15:05 UTC
Last modified on: 08/30/2024 16:08:54 UTC