Skip to content
This repository was archived by the owner on Jun 1, 2026. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ private unsigned int MAGIC_COUNT = 0;
private unsigned int AVAIL_COUNT = 0;
private byte *NEXT_IN = NULL;

/* CWE-409: Decompression bomb protection - max 64 MiB decompressed output */
#define MAX_DECOMPRESS_SIZE (64 * 1024 * 1024)
private unsigned long long total_decompressed = 0;

private int (*d_func1)(byte *, unsigned int);
private int (*d_func2)(byte *, unsigned int);
private int (*d_func3)(byte *, unsigned int);
Expand Down Expand Up @@ -243,7 +247,13 @@ inflate_gzip(byte *p, unsigned int max)
if (err != Z_OK && err != Z_STREAM_END)
warn_exit("zlib inflate error (%d).", err);

inflated = max - z.avail_out;
inflated = max - z.avail_out;

/* CWE-409: Check decompressed size limit */
total_decompressed += (inflated - (max - old));
if (total_decompressed > MAX_DECOMPRESS_SIZE)
warn_exit("decompression bomb detected: output exceeds %d MiB limit.",
MAX_DECOMPRESS_SIZE / (1024 * 1024));

if (old == z.avail_out && z.avail_in != 0)
break;
Expand Down Expand Up @@ -288,7 +298,13 @@ inflate_bzip2(byte *p, unsigned int max)
if (err != BZ_OK && err != BZ_STREAM_END)
warn_exit("bzip2 BZ2_bzDecompress error (%d).", err);

inflated = max - bz.avail_out;
inflated = max - bz.avail_out;

/* CWE-409: Check decompressed size limit */
total_decompressed += (inflated - (max - old));
if (total_decompressed > MAX_DECOMPRESS_SIZE)
warn_exit("decompression bomb detected: output exceeds %d MiB limit.",
MAX_DECOMPRESS_SIZE / (1024 * 1024));

if (old == bz.avail_out && bz.avail_in != 0)
break;
Expand Down Expand Up @@ -377,7 +393,10 @@ Compressed_Data_Packet(int len)
unsigned int alg = Getc();
int err;
private int (*func)(byte *, unsigned int);


/* CWE-409: Reset decompressed size counter */
total_decompressed = 0;

comp_algs(alg);

#ifdef HAVE_LIBZ
Expand Down