[Zlib-devel] Compressing volatile data?

glennrp at comcast.net glennrp at comcast.net
Fri Sep 3 10:53:41 EDT 2010


----- Original Message -----
From: "Mark Adler" <madler at madler.net>
To: zlib-devel at madler.net
Sent: Friday, September 3, 2010 12:44:33 AM
Subject: Re: [Zlib-devel] Compressing volatile data?

On Sep 2, 2010, at 11:03 AM, Gary Cameron wrote:
> if parts of the data in the source buffer being compressed is still "active" and changing during the compression, could this cause a decompression failure?

Gary,

Yes.

This code in deflate.c is the only thing that accesses the source buffer:

    if (strm->state->wrap == 1) {
        strm->adler = adler32(strm->adler, strm->next_in, len);
    }
    else if (strm->state->wrap == 2) {
        strm->adler = crc32(strm->adler, strm->next_in, len);
    }
    zmemcpy(buf, strm->next_in, len);

So the buffer is accessed twice: first to compute a check value, and second to copy the data.  If the source buffer is changed between those two, then the check value will not match the data, and an error will be reported when decompressing.

When this happens, the compressed data is not itself corrupted.  The only problem is a mismatch between an earlier check value and later data.

This deflate sensitivity to volatile input data can be solved thusly:

    zmemcpy(buf, strm->next_in, len);
    if (strm->state->wrap == 1) {
        strm->adler = adler32(strm->adler, buf, len);
    }
    else if (strm->state->wrap == 2) {
        strm->adler = crc32(strm->adler, buf, len);
    }

I have just made that change for the next version.

Mark


         Don't you still hit the zmemcpy twice, 
         recreating the buf for each 'wrap'?

         Glenn




More information about the Zlib-devel mailing list