CVE-2023-30620: Arbitrary File Write when Extracting a Remotely retrieved Tarball in mindsdb/mindsdb
Summary
An unsafe extraction is being performed using tarfile.extractall() from a remotely retrieved tarball. Which may lead to the writing of the extracted files to an unintended location. Sometimes, the vulnerability is called a TarSlip or a ZipSlip variant.
Details
I commented the following snippet of code as a vulnerability details. The code is from file.py#L26..L134
python @nsconf.route('/<name>') @nsconf.param('name', "MindsDB's name for file") class File(Resource): @nsconf.doc('putfile') def put(self, name: str): ''' add new file params in FormData: - file - originalfilename [optional] '''
data = {}
... omitted for brevity
url = data['source'] data['file'] = data['name']
... omitted for brevity
with requests.get(url, stream=True) as r: # Source: retrieve the URL which point to a remotely located tarball if r.statuscode != 200: return httperror( 400, "Error getting file", f"Got status code: {r.statuscode}" ) filepath = os.path.join(tempdirpath, data['file']) with open(filepath, 'wb') as f: for chunk in r.itercontent(chunksize=8192): # write with chunks the remote retrieved file into filepath location f.write(chunk)
originalfilename = data.get('originalfilename')
filepath = os.path.join(tempdirpath, data['file']) lp = filepath.lower() if lp.endswith(('.zip', '.tar.gz')): if lp.endswith('.zip'): with zipfile.ZipFile(filepath) as f: f.extractall(tempdirpath) elif lp.endswith('.tar.gz'): with tarfile.open(filepath) as f: # Just after f.extractall(tempdirpath) # Sink: the tarball located by filepath is supposed to be extracted to tempdirpath.
So, a remotely available tarball is being retrieved and written to the server filesystem in chunks, and then, if the extension ends with .tar.gz of a compressed tarball, the mindsdb app applies tarfile.extractall() directly with no checks for the destination.
However, according to the following warning from the official documentation;
> Warning: Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/" or filenames with two dots "..".
PoC
The following PoC is provided for illustration purposes only. It showcases the risk of extracting a non-harmless text file sim4n6.txt to one of the parent locations rather than the intended current folder.
bash > tar --list -v -f archive.tar.gz tar: Removing leading "../../../" from member names ../../../sim4n6.txt
> python3 Python 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tarfile >>> with tarfile.open("archive.tar.gz") as tf: >>> tf.extractall() >>> exit()
> test -f ../../../sim4n6.txt && echo "sim4n6.txt exists" sim4n6.txt exists
Attack Scenario
An attacker could craft a malicious tarball with a filename path, such as ../../../../../../../../etc/passwd, and then serve the archive remotely, proceed to the PUT request of the tarball through mindsdb and overwrite the system files of the hosting server for instance.
Mitigation
Potential mitigation could be to: - Use a safer module, like zipfile. - Use an alternative of tarfile, such as tarsafe. - Validate the location or the absolute path of the extracted files and discard those with malicious paths such as relative path ../../.. or absolute path such as /etc/password. A simple wrapper could be written to raise an exception when a path traversal may be identified.
This is similar to the other report GHSA-7x45-phmr-9wqp.
Other sources
mindsdb is a Machine Learning platform to help developers build AI solutions. In affected versions an unsafe extraction is being performed using tarfile.extractall() from a remotely retrieved tarball. Which may lead to the writing of the extracted files to an unintended location. Sometimes, the vulnerability is called a TarSlip or a ZipSlip variant. An attacker may leverage this vulnerability to overwrite any local file which the server process has access to. There is no risk of file exposure with this vulnerability. This issue has been addressed in release 23.2.1.0 . Users are advised to upgrade. There are no known workarounds for this vulnerability.
Affected Software
Remediation
Event History
Frequently Asked Questions
What is the severity of CVE-2023-30620?
CVE-2023-30620 is considered a high severity vulnerability due to the potential for arbitrary file extraction.
How do I fix CVE-2023-30620?
To fix CVE-2023-30620, upgrade to MindsDB version 23.2.1.0 or later.
What are the consequences of CVE-2023-30620?
The consequences of CVE-2023-30620 include the risk of overwriting critical system files or extracting malicious files to unintended locations.
Which versions of MindsDB are affected by CVE-2023-30620?
MindsDB versions up to and including 23.1.5.0 are affected by CVE-2023-30620.
Is CVE-2023-30620 a type of ZipSlip vulnerability?
Yes, CVE-2023-30620 is often referred to as a TarSlip or ZipSlip variant due to its similar exploitation mechanism.