From: Michael Tokarev <mjt@tls.msk.ru>
To: Corentin Chary <corentin.chary@gmail.com>
Cc: Roland Dreier <roland@kernel.org>,
Roland Dreier <roland@purestorage.com>,
qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] vnc: tight: Fix crash after 2GB of output
Date: Fri, 04 Mar 2011 14:46:39 +0300 [thread overview]
Message-ID: <4D70D11F.6070303@msgid.tls.msk.ru> (raw)
In-Reply-To: <AANLkTinMmwTW4utS576scg4m=tUR+0amo+sipjq+Enix@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 887 bytes --]
04.03.2011 11:56, Corentin Chary wrote:
>>>
>>> bytes = zstream->total_out - previous_out;
>
> Good catch
>
>> total_out isn't used by zlib internally, so if the resulting
>> "total" counter is not needed in qemu, we can just zero-out
>> the total_out in this function before calling zlib, and
>> use the resulting value directly as "bytes", without
>> saving its previous value in previous_out. Something like
>> the attached patch does.
>
> If you're certain that total_out is not used by zlib, could you also
> send a patch for zlib encoding please ? (vnc-enc-zlib.c)
> Thanks,
Yes, I noticed this too (the same code is in enc-zlib), and mentioned
this in my previous email.
The attached slightly different patch fixes both places and fixes
them for good (hopefully anyway). Runtime-tested for the tight
case, but honestly, I didn't wait for 2G of output ;)
Thanks!
/mjt
[-- Attachment #2: fix-vnc-zlib-overflow.diff --]
[-- Type: text/x-patch, Size: 2847 bytes --]
fix 2Gb integer overflow in in VNC tight and zlib encodings
As found by Roland Dreier <roland@purestorage.com> (excellent
catch!), when amount of VNC compressed data produced by zlib
and sent to client exceeds 2Gb, integer overflow occurs because
currently, we calculate amount of data produced at each step by
comparing saved total_out with new total_out, and total_out is
something which grows without bounds. Compare it with previous
avail_out instead of total_out, and leave total_out alone.
The same code is used in vnc-enc-tight.c and vnc-enc-zlib.c,
so fix both cases.
There, there's no actual need to save previous_out value, since
capacity-offset (which is how that value is calculated) stays
the same so it can be recalculated again after call to deflate(),
but whole thing becomes less readable this way.
Reported-by: Roland Dreier <roland@purestorage.com>
Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
diff --git a/ui/vnc-enc-tight.c b/ui/vnc-enc-tight.c
index 2522936..87fdf35 100644
--- a/ui/vnc-enc-tight.c
+++ b/ui/vnc-enc-tight.c
@@ -868,8 +868,8 @@ static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
zstream->avail_in = vs->tight.tight.offset;
zstream->next_out = vs->tight.zlib.buffer + vs->tight.zlib.offset;
zstream->avail_out = vs->tight.zlib.capacity - vs->tight.zlib.offset;
+ previous_out = zstream->avail_out;
zstream->data_type = Z_BINARY;
- previous_out = zstream->total_out;
/* start encoding */
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
@@ -878,7 +878,8 @@ static int tight_compress_data(VncState *vs, int stream_id, size_t bytes,
}
vs->tight.zlib.offset = vs->tight.zlib.capacity - zstream->avail_out;
- bytes = zstream->total_out - previous_out;
+ /* ...how much data has actually been produced by deflate() */
+ bytes = previous_out - zstream->avail_out;
tight_send_compact_size(vs, bytes);
vnc_write(vs, vs->tight.zlib.buffer, bytes);
diff --git a/ui/vnc-enc-zlib.c b/ui/vnc-enc-zlib.c
index 3c6e6ab..e32e4cd 100644
--- a/ui/vnc-enc-zlib.c
+++ b/ui/vnc-enc-zlib.c
@@ -103,8 +103,8 @@ static int vnc_zlib_stop(VncState *vs)
zstream->avail_in = vs->zlib.zlib.offset;
zstream->next_out = vs->output.buffer + vs->output.offset;
zstream->avail_out = vs->output.capacity - vs->output.offset;
+ previous_out = zstream->avail_out;
zstream->data_type = Z_BINARY;
- previous_out = zstream->total_out;
// start encoding
if (deflate(zstream, Z_SYNC_FLUSH) != Z_OK) {
@@ -113,7 +113,7 @@ static int vnc_zlib_stop(VncState *vs)
}
vs->output.offset = vs->output.capacity - zstream->avail_out;
- return zstream->total_out - previous_out;
+ return previous_out - zstream->avail_out;
}
int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
next prev parent reply other threads:[~2011-03-04 11:46 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-04 0:57 [Qemu-devel] [PATCH] vnc: tight: Fix crash after 2GB of output Roland Dreier
2011-03-04 7:34 ` Michael Tokarev
2011-03-04 8:56 ` Corentin Chary
2011-03-04 11:46 ` Michael Tokarev [this message]
2011-03-04 21:08 ` Corentin Chary
2011-03-05 5:56 ` [Qemu-devel] [PATCH][STABLE-0.14] " Michael Tokarev
2011-03-05 8:33 ` [Qemu-devel] " Corentin Chary
2011-03-05 8:57 ` Michael Tokarev
2011-03-05 9:29 ` Corentin Chary
2011-03-04 16:59 ` [Qemu-devel] [PATCH] " Roland Dreier
2011-03-04 18:46 ` Roland Dreier
2011-03-04 7:36 ` Michael Tokarev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4D70D11F.6070303@msgid.tls.msk.ru \
--to=mjt@tls.msk.ru \
--cc=corentin.chary@gmail.com \
--cc=qemu-devel@nongnu.org \
--cc=roland@kernel.org \
--cc=roland@purestorage.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.