GHSA-2g5w-29q9-w6hx
Vulnerability from github
8.7 (High) - CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:H/VA:N/SC:N/SI:N/SA:N
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
@ns_conf.route('/
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.status_code != 200:
return http_error(
400,
"Error getting file",
f"Got status code: {r.status_code}"
)
file_path = os.path.join(temp_dir_path, data['file'])
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192): # write with chunks the remote retrieved file into file_path location
f.write(chunk)
original_file_name = data.get('original_file_name')
file_path = os.path.join(temp_dir_path, data['file'])
lp = file_path.lower()
if lp.endswith(('.zip', '.tar.gz')):
if lp.endswith('.zip'):
with zipfile.ZipFile(file_path) as f:
f.extractall(temp_dir_path)
elif lp.endswith('.tar.gz'):
with tarfile.open(file_path) as f: # Just after
f.extractall(temp_dir_path) # Sink: the tarball located by file_path is supposed to be extracted to temp_dir_path.
```
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.
{ "affected": [ { "package": { "ecosystem": "PyPI", "name": "mindsdb" }, "ranges": [ { "events": [ { "introduced": "0" }, { "fixed": "23.2.1.0" } ], "type": "ECOSYSTEM" } ] } ], "aliases": [ "CVE-2023-30620" ], "database_specific": { "cwe_ids": [ "CWE-22" ], "github_reviewed": true, "github_reviewed_at": "2023-03-30T21:42:48Z", "nvd_published_at": "2023-04-21T21:15:08Z", "severity": "HIGH" }, "details": "### Summary\n\nAn 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.\n\n### Details\n\nI commented the following snippet of code as a vulnerability details. The code is from [file.py#L26..L134](https://github.com/mindsdb/mindsdb/blob/afedd37c16e579b6dc075b0814e42d0505ccdc07/mindsdb/api/http/namespaces/file.py#L26..L134)\n\n```python\n@ns_conf.route(\u0027/\u003cname\u003e\u0027)\n@ns_conf.param(\u0027name\u0027, \"MindsDB\u0027s name for file\")\nclass File(Resource):\n @ns_conf.doc(\u0027put_file\u0027)\n def put(self, name: str):\n \u0027\u0027\u0027 add new file\n params in FormData:\n - file\n - original_file_name [optional]\n \u0027\u0027\u0027\n\n data = {}\n\n ... omitted for brevity\n\n url = data[\u0027source\u0027]\n data[\u0027file\u0027] = data[\u0027name\u0027]\n\n ... omitted for brevity \n\n with requests.get(url, stream=True) as r: # Source: retrieve the URL which point to a remotely located tarball \n if r.status_code != 200:\n return http_error(\n 400,\n \"Error getting file\",\n f\"Got status code: {r.status_code}\"\n )\n file_path = os.path.join(temp_dir_path, data[\u0027file\u0027])\n with open(file_path, \u0027wb\u0027) as f:\n for chunk in r.iter_content(chunk_size=8192): # write with chunks the remote retrieved file into file_path location \n f.write(chunk)\n\n original_file_name = data.get(\u0027original_file_name\u0027)\n\n file_path = os.path.join(temp_dir_path, data[\u0027file\u0027]) \n lp = file_path.lower()\n if lp.endswith((\u0027.zip\u0027, \u0027.tar.gz\u0027)):\n if lp.endswith(\u0027.zip\u0027):\n with zipfile.ZipFile(file_path) as f:\n f.extractall(temp_dir_path)\n elif lp.endswith(\u0027.tar.gz\u0027):\n with tarfile.open(file_path) as f: # Just after \n f.extractall(temp_dir_path) # Sink: the tarball located by file_path is supposed to be extracted to temp_dir_path. \n```\n\nSo, 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. \n\nHowever, according to the following [warning](https://docs.python.org/3/library/tarfile.html#tarfile.TarFile.extractall) from the official documentation;\n\n\u003e 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 \"..\". \n\n\n### PoC\n\nThe 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.\n\n```bash\n\u003e tar --list -v -f archive.tar.gz\ntar: Removing leading \"../../../\" from member names\n../../../sim4n6.txt\n\n\u003e python3 \nPython 3.10.6 (main, Nov 2 2022, 18:53:38) [GCC 11.3.0] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n\u003e\u003e\u003e import tarfile\n\u003e\u003e\u003e with tarfile.open(\"archive.tar.gz\") as tf:\n\u003e\u003e\u003e tf.extractall()\n\u003e\u003e\u003e exit()\n\n\u003e test -f ../../../sim4n6.txt \u0026\u0026 echo \"sim4n6.txt exists\"\nsim4n6.txt exists\n```\n\n### Attack Scenario\n\nAn 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.\n\n### Mitigation \n\nPotential mitigation could be to:\n - Use a safer module, like `zipfile`.\n - Use an alternative of `tarfile`, such as `tarsafe`. \n - 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.\n\nThis is similar to the other report [GHSA-7x45-phmr-9wqp](https://github.com/mindsdb/mindsdb/security/advisories/GHSA-7x45-phmr-9wqp).", "id": "GHSA-2g5w-29q9-w6hx", "modified": "2024-09-25T19:46:32Z", "published": "2023-03-30T21:42:48Z", "references": [ { "type": "WEB", "url": "https://github.com/mindsdb/mindsdb/security/advisories/GHSA-2g5w-29q9-w6hx" }, { "type": "ADVISORY", "url": "https://nvd.nist.gov/vuln/detail/CVE-2023-30620" }, { "type": "WEB", "url": "https://github.com/mindsdb/mindsdb/commit/4419b0f0019c000db390b54d8b9d06e1d3670039" }, { "type": "PACKAGE", "url": "https://github.com/mindsdb/mindsdb" }, { "type": "WEB", "url": "https://github.com/mindsdb/mindsdb/blob/afedd37c16e579b6dc075b0814e42d0505ccdc07/mindsdb/api/http/namespaces/file.py#L26..L134" }, { "type": "WEB", "url": "https://github.com/mindsdb/mindsdb/releases/tag/v23.2.1.0" }, { "type": "WEB", "url": "https://github.com/pypa/advisory-database/tree/main/vulns/mindsdb/PYSEC-2023-27.yaml" } ], "schema_version": "1.4.0", "severity": [ { "score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:N", "type": "CVSS_V3" }, { "score": "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:N/VI:H/VA:N/SC:N/SI:N/SA:N", "type": "CVSS_V4" } ], "summary": "mindsdb arbitrary file write when extracting a remotely retrieved Tarball" }
Sightings
Author | Source | Type | Date |
---|
Nomenclature
- Seen: The vulnerability was mentioned, discussed, or seen somewhere by the user.
- Confirmed: The vulnerability is confirmed from an analyst perspective.
- Exploited: This vulnerability was exploited and seen by the user reporting the sighting.
- Patched: This vulnerability was successfully patched by the user reporting the sighting.
- Not exploited: This vulnerability was not exploited or seen by the user reporting the sighting.
- Not confirmed: The user expresses doubt about the veracity of the vulnerability.
- Not patched: This vulnerability was not successfully patched by the user reporting the sighting.