Skip to content

Commit

Permalink
Make ZlibInStream more robust against failures
Browse files Browse the repository at this point in the history
Move the checks around to avoid missing cases where we might access
memory that is no longer valid. Also avoid touching the underlying
stream implicitly (e.g. via the destructor) as it might also no
longer be valid.

A malicious server could theoretically use this for remote code
execution in the client.

Issue found by Pavel Cheremushkin from Kaspersky Lab
  • Loading branch information
CendioOssman committed Nov 15, 2019
1 parent bbbb67e commit d61a767
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 11 deletions.
13 changes: 7 additions & 6 deletions common/rdr/ZlibInStream.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ int ZlibInStream::pos()
return offset + ptr - start;
}

void ZlibInStream::removeUnderlying()
void ZlibInStream::flushUnderlying()
{
ptr = end = start;
if (!underlying) return;

while (bytesIn > 0) {
decompress(true);
end = start; // throw away any data
}
underlying = 0;

setUnderlying(NULL, 0);
}

void ZlibInStream::reset()
Expand Down Expand Up @@ -90,7 +90,7 @@ void ZlibInStream::init()
void ZlibInStream::deinit()
{
assert(zs != NULL);
removeUnderlying();
setUnderlying(NULL, 0);
inflateEnd(zs);
delete zs;
zs = NULL;
Expand All @@ -100,8 +100,6 @@ int ZlibInStream::overrun(int itemSize, int nItems, bool wait)
{
if (itemSize > bufSize)
throw Exception("ZlibInStream overrun: max itemSize exceeded");
if (!underlying)
throw Exception("ZlibInStream overrun: no underlying stream");

if (end - ptr != 0)
memmove(start, ptr, end - ptr);
Expand All @@ -127,6 +125,9 @@ int ZlibInStream::overrun(int itemSize, int nItems, bool wait)

bool ZlibInStream::decompress(bool wait)
{
if (!underlying)
throw Exception("ZlibInStream overrun: no underlying stream");

zs->next_out = (U8*)end;
zs->avail_out = start + bufSize - end;

Expand Down
2 changes: 1 addition & 1 deletion common/rdr/ZlibInStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace rdr {
virtual ~ZlibInStream();

void setUnderlying(InStream* is, int bytesIn);
void removeUnderlying();
void flushUnderlying();
int pos();
void reset();

Expand Down
3 changes: 2 additions & 1 deletion common/rfb/CMsgReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ void CMsgReader::readExtendedClipboard(rdr::S32 len)
num++;
}

zis.removeUnderlying();
zis.flushUnderlying();
zis.setUnderlying(NULL, 0);

handler->handleClipboardProvide(flags, lengths, buffers);

Expand Down
3 changes: 2 additions & 1 deletion common/rfb/SMsgReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ void SMsgReader::readExtendedClipboard(rdr::S32 len)
num++;
}

zis.removeUnderlying();
zis.flushUnderlying();
zis.setUnderlying(NULL, 0);

handler->handleClipboardProvide(flags, lengths, buffers);

Expand Down
3 changes: 2 additions & 1 deletion common/rfb/TightDecoder.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,8 @@ void TightDecoder::decodeRect(const Rect& r, const void* buffer,

zis[streamId].readBytes(netbuf, dataSize);

zis[streamId].removeUnderlying();
zis[streamId].flushUnderlying();
zis[streamId].setUnderlying(NULL, 0);
delete ms;

bufptr = netbuf;
Expand Down
3 changes: 2 additions & 1 deletion common/rfb/zrleDecode.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ void ZRLE_DECODE (const Rect& r, rdr::InStream* is,
}
}

zis->removeUnderlying();
zis->flushUnderlying();
zis->setUnderlying(NULL, 0);
}

#undef ZRLE_DECODE
Expand Down

0 comments on commit d61a767

Please sign in to comment.