Skip to content

gh-155649: Bound LZMA dictionary size to prevent memory DoS - #155650

Open
DeseretSaint wants to merge 1 commit into
python:mainfrom
DeseretSaint:main
Open

gh-155649: Bound LZMA dictionary size to prevent memory DoS#155650
DeseretSaint wants to merge 1 commit into
python:mainfrom
DeseretSaint:main

Conversation

@DeseretSaint

@DeseretSaint DeseretSaint commented Aug 13, 2026

Copy link
Copy Markdown

liblzma allocates the full LZMA dictionary up front when a decompressor is created, and the dictionary size comes from the stream header. An untrusted stream can declare a multi-gigabyte dictionary (up to 4 GiB) and force that allocation from a handful of bytes — a memory denial of service.

Both crash paths were reported by OSS-Fuzz (python3-libraries, public):

Directly reachable too: lzma.decompress() / LZMADecompressor(FORMAT_AUTO) on untrusted bytes, and LZMADecompressor(FORMAT_RAW, filters=[{"dict_size": ...}]).

Changes

  1. parse_filter_spec_lzma() rejects dict_size above liblzma's own maximum (LZMA_DICT_SIZE_MAX, 1.5 GiB) with LZMAError — covers raw LZMA1/LZMA2 filter chains, including zipfile's LZMA members (the dictionary is parsed from the 5-byte properties header before any data is decompressed).
  2. LZMADecompressor defaults memlimit to LZMA_DICT_SIZE_MAX instead of UINT64_MAX — bounds the FORMAT_AUTO/FORMAT_ALONE/FORMAT_XZ paths where the header is parsed inside liblzma (tarfile compression detection, lzma.decompress(), LZMAFile).

An explicit memlimit can still be passed to raise or lower the bound.

Compatibility: liblzma presets cap at 64 MiB dictionaries; 1.5 GiB is liblzma's own maximum supported dictionary size, so no legitimate stream is affected.

Tests: new regression tests in test_lzma.py, test_zipfile (crafted bomb zip → LZMAError), and test_tarfile (13-byte fake-LZMA input → ReadError). test_lzma test_zipfile test_tarfile all pass (1,487 tests).

Companion fuzzer-harness change: https://github.com/python/library-fuzzers/pull/… (zipfile fuzzer now tolerates lzma.LZMAError).

liblzma allocates the full LZMA dictionary up front when a decompressor
is created, and the dictionary size comes from the stream header.  An
untrusted stream could declare a multi-gigabyte dictionary (up to 4 GiB)
and force that allocation from a handful of bytes, exhausting memory.

Two gaps are closed:
- parse_filter_spec_lzma() now rejects dict_size above liblzma's own
  maximum (LZMA_DICT_SIZE_MAX, 1.5 GiB) with LZMAError.  This covers
  raw LZMA1/LZMA2 filter chains, including zipfile's LZMA members.
- LZMADecompressor now defaults memlimit to LZMA_DICT_SIZE_MAX instead
  of UINT64_MAX, bounding the FORMAT_AUTO/FORMAT_ALONE/FORMAT_XZ paths
  (e.g. tarfile's compression detection and lzma.decompress()).

An explicit memlimit can still be passed to adjust the bound.

Fixes OSS-Fuzz reports (python3-libraries):
- fuzzer-zipfile OOM: https://issues.oss-fuzz.com/issues/495472861
- fuzzer-tarfile OOM: https://issues.oss-fuzz.com/issues/482161128
@python-cla-bot

Copy link
Copy Markdown

The following commit authors need to sign the Contributor License Agreement:

CLA not signed

@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #34043724 | 📁 Comparing c7a05bd against main (ee4fe00)

  🔍 Preview build  

2 files changed
± library/lzma.html
± whatsnew/changelog.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant