Skip to content

Python "zipfile" can't detect "quoted-overlap" zipbomb that can be used as a DoS attack #109858

Description

@dyingc

Bug report

Bug description:

Just found this vulnerability in the latest Python 3.11.5 (and previous 3.10.10).

If we craft a zipbomb using the "quoted-overlap" way (as mentioned https://www.bamsoftware.com/hacks/zipbomb/), this can't be detected by Python's zip file and the zip will be extracted and thus potentially cause a DoS attack by consuming all the storage.

This issue is related to CVE-2019-9674 but not the same. CVE-2019-9674 is talking about the "normal" overlap-zipbomb which is a "full" overlap. This can already be detected by Python's new version of zipfile. However, when we craft a "quoted-overlap" zip, as indicated by https://www.bamsoftware.com/hacks/zipbomb/, python can't detect and happily starts to extract.

For example, the following is the python to extract a zip file, 116 KB before extraction, goes to as large as 17GB after extraction. The size after extraction can be easily increased to multi TBs or even PBs by adjusting the zip-creation.

import zipfile
import sys
import os

def extract_zip(zip_path):
    """
    Extracts the contents of a ZIP file to the current directory.

    :param zip_path: Path to the ZIP file
    """
    if not os.path.exists(zip_path):
        print(f"Error: {zip_path} does not exist.")
        return

    with zipfile.ZipFile(zip_path, 'r') as zip_ref:
        zip_ref.extractall()
        print(f"Extracted contents of {zip_path} to the current directory.")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python extract_zip.py <path_to_zip_file>")
        sys.exit(1)

    zip_file_path = sys.argv[1]
    extract_zip(zip_file_path)

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Sep 25, 2023
  2. added
    3.11only security fixes
    3.12only security fixes
    on Sep 25, 2023
  3. gpshead commented on Sep 25, 2023

    @gpshead
    Member

    Additional context: This was reported to us on the Python Security Response Team on 2023-09-06. We ultimately concluded it should be handled in public. The form of zip file attack is not "new" (as evidence by that zipbomb link) and several other zip implementations have had to deal with this over the years. Ours still needs to.

    I'll summarize more of our discussions there in a followup comment.

  4. gpshead commented on Sep 25, 2023

    @gpshead
    Member

    While we do at least have an existing warning on ZipFile.extractall() in our docs "Warning: Never extract archives from untrusted sources without prior inspection." - that text goes on to focus on other reasons. We expect that there are people who don't actually understand the full implications of handling untrusted compressed complex-referential data like zips and just call an API like .extractall() without sufficient checks regardless.

    @SethMichaelLarson & @di looked into PyPI's warehouse to check how PyPI handles this situation with regards to uploaded packages. It already has a decent seeming zip file checking loop in place that rejects zip files who's extracted size would be huge compared to the input as seen in pypi/warehouse#13877 - that seems to reject the example zipbombs already. @pradyunsg later confirmed that while pip itself doesn't do such checks (it probably should) "we do benefit from PyPI's checks in the default configuration and those benefits transfer to mirrored PyPI indexes as well." - so in effect we don't currently believe PyPI can host zipbomb wheels - ameliorating this for the community's pip install invocations by default.

    Workarounds & Recommendations

    Document the workaround

    We should centralize our guidance for Python users on how to avoid the problem when processing untrusted zip files, perhaps with a code snippet such as the overall extraction size checking loop from Warehouse as an example of the kinds of protection users of zipfile today should employ. Having applications and libraries do that is good defense in depth and doesn't require a patched Python runtime.

    We also recommend that code handling untrusted zip files always run in a resource constrained container regardless.

    Fixing

    Improving our zipfile internals to do consistency checks to raise an exception when processing zip files with overlapping sections or inconsistent metadata or whatnot (all of the kinds of things that enable this form of malicious structured file).

    How feasible it'll be to backport that is unknown. But as this is only a DoS, for something that most code isn't doing (processing untrusted zips), and it can be worked around, I don't think focusing on designing this backporting this all the way back to 3.8 is worth spending extra time on if it requires any.

    Reaching out to specific applications that process arbitrary zip files to make sure they're aware of this issue is a good idea.

  5. self-assigned this
    on Sep 25, 2023
  6. added a commit that references this issue on Sep 28, 2023
  7. 32 remaining items

  8. serhiy-storchaka commented on Feb 5, 2024

    @serhiy-storchaka
    Member

    No. All necessary backports were merged. Thank you for the reminder.

  9. moved this from In Progress to Done in Zipfile issues 📦on Feb 5, 2024
  10. added 2 commits that reference this issue on Feb 11, 2024
  11. sparrowt commented on Apr 3, 2024

    @sparrowt
    Contributor

    Does anyone here have any contacts in GitHub who know about GHSA?

    Their entry for this (CVE-2024-0450) GHSA-jm46-725r-hh9v says that 3.12.2 and 3.11.8 are vulnerable, but AFAICS those 2 versions contain the fix (see gh-109858 mentioned in 3.12.2 release notes and 3.11.8 release notes).

    I've attempted to open an improvement PR here but I'm not sure if it will be accepted seeing as it says 'Unreviewed' and it seems python is not on the list of things they care about; because the "suggest improvements" button (via which I did it) forced me to select "Affected products" where [c]python was not an option 😢 the nearest thing was 'pip'.

    AFAICS this GHSA data feeds through into tools like Grype which currently say "Fixed?"=unknown and have no "Fixed in" so it would be good to know how to correct the record on which python versions are vulnerable/fixed.

  12. sethmlarson commented on Apr 3, 2024

    @sethmlarson
    Contributor

    Thanks for reporting this @sparrowt, I'll take a look and update the CVE records accordingly. In theory this will trickle down into GitHub Security Advisories as well since they source a lot of their information from that database.

  13. sparrowt commented on Apr 3, 2024

    @sparrowt
    Contributor

    Amazing thank you! I'm not sure how the affected versions / patched versions thing is going to work given GHSA seems to only support packages within certain 'ecosystems' but seemingly not the ecosystem runtime (e.g. python) itself:
    image

  14. sethmlarson commented on Apr 3, 2024

    @sethmlarson
    Contributor

    I've fixed the metadata for the CVE. I believe the "syncing" process will happen outside of the manual update process but could be wrong? The GHSA team is aware of the whole ecosystem issue for updating GHSA records.

  15. added a commit that references this issue on Apr 25, 2024
  16. sparrowt commented on May 15, 2024

    @sparrowt
    Contributor

    Thanks! Sadly it's still wrong on GHSA-jm46-725r-hh9v

  17. added a commit that references this issue on May 16, 2024
  18. added 2 commits that reference this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.10 (EOL)end of life3.11only security fixes3.12only security fixes3.13only security fixes3.8 (EOL)end of life3.9 (EOL)end of liferelease-blockerstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errortype-securityA security issue

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions