Skip to content

Commit 2df7954

Browse files
akihikodakiMichael Tokarev
authored andcommitted
ui/vnc: Do not copy z_stream
vnc_worker_thread_loop() copies z_stream stored in its local VncState to the persistent VncState, and the copied one is freed with deflateEnd() later. However, deflateEnd() refuses to operate with a copied z_stream and returns Z_STREAM_ERROR, leaking the allocated memory. Avoid copying the zlib state to fix the memory leak. Fixes: bd023f9 ("vnc: threaded VNC server") Signed-off-by: Akihiko Odaki <[email protected]> Reviewed-by: Marc-André Lureau <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]> Message-Id: <[email protected]> (cherry picked from commit aef22331b5a4670f42638a5f63a26e93bf779aae) Signed-off-by: Michael Tokarev <[email protected]>
1 parent 12e88c0 commit 2df7954

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

ui/vnc-enc-zlib.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,21 @@ void vnc_zlib_zfree(void *x, void *addr)
4848

4949
static void vnc_zlib_start(VncState *vs)
5050
{
51-
buffer_reset(&vs->zlib.zlib);
51+
buffer_reset(&vs->zlib->zlib);
5252

5353
// make the output buffer be the zlib buffer, so we can compress it later
54-
vs->zlib.tmp = vs->output;
55-
vs->output = vs->zlib.zlib;
54+
vs->zlib->tmp = vs->output;
55+
vs->output = vs->zlib->zlib;
5656
}
5757

5858
static int vnc_zlib_stop(VncState *vs)
5959
{
60-
z_streamp zstream = &vs->zlib.stream;
60+
z_streamp zstream = &vs->zlib->stream;
6161
int previous_out;
6262

6363
// switch back to normal output/zlib buffers
64-
vs->zlib.zlib = vs->output;
65-
vs->output = vs->zlib.tmp;
64+
vs->zlib->zlib = vs->output;
65+
vs->output = vs->zlib->tmp;
6666

6767
// compress the zlib buffer
6868

@@ -85,24 +85,24 @@ static int vnc_zlib_stop(VncState *vs)
8585
return -1;
8686
}
8787

88-
vs->zlib.level = vs->tight->compression;
88+
vs->zlib->level = vs->tight->compression;
8989
zstream->opaque = vs;
9090
}
9191

92-
if (vs->tight->compression != vs->zlib.level) {
92+
if (vs->tight->compression != vs->zlib->level) {
9393
if (deflateParams(zstream, vs->tight->compression,
9494
Z_DEFAULT_STRATEGY) != Z_OK) {
9595
return -1;
9696
}
97-
vs->zlib.level = vs->tight->compression;
97+
vs->zlib->level = vs->tight->compression;
9898
}
9999

100100
// reserve memory in output buffer
101-
buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64);
101+
buffer_reserve(&vs->output, vs->zlib->zlib.offset + 64);
102102

103103
// set pointers
104-
zstream->next_in = vs->zlib.zlib.buffer;
105-
zstream->avail_in = vs->zlib.zlib.offset;
104+
zstream->next_in = vs->zlib->zlib.buffer;
105+
zstream->avail_in = vs->zlib->zlib.offset;
106106
zstream->next_out = vs->output.buffer + vs->output.offset;
107107
zstream->avail_out = vs->output.capacity - vs->output.offset;
108108
previous_out = zstream->avail_out;
@@ -147,8 +147,8 @@ int vnc_zlib_send_framebuffer_update(VncState *vs, int x, int y, int w, int h)
147147

148148
void vnc_zlib_clear(VncState *vs)
149149
{
150-
if (vs->zlib.stream.opaque) {
151-
deflateEnd(&vs->zlib.stream);
150+
if (vs->zlib->stream.opaque) {
151+
deflateEnd(&vs->zlib->stream);
152152
}
153-
buffer_free(&vs->zlib.zlib);
153+
buffer_free(&vs->zlib->zlib);
154154
}

ui/vnc.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@
5656
#include "io/dns-resolver.h"
5757
#include "monitor/monitor.h"
5858

59+
typedef struct VncConnection {
60+
VncState vs;
61+
VncZlib zlib;
62+
} VncConnection;
63+
5964
#define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT
6065
#define VNC_REFRESH_INTERVAL_INC 50
6166
#define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE
@@ -1364,7 +1369,7 @@ void vnc_disconnect_finish(VncState *vs)
13641369
vs->magic = 0;
13651370
g_free(vs->zrle);
13661371
g_free(vs->tight);
1367-
g_free(vs);
1372+
g_free(container_of(vs, VncConnection, vs));
13681373
}
13691374

13701375
size_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err)
@@ -3243,11 +3248,13 @@ static void vnc_refresh(DisplayChangeListener *dcl)
32433248
static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc,
32443249
bool skipauth, bool websocket)
32453250
{
3246-
VncState *vs = g_new0(VncState, 1);
3251+
VncConnection *vc = g_new0(VncConnection, 1);
3252+
VncState *vs = &vc->vs;
32473253
bool first_client = QTAILQ_EMPTY(&vd->clients);
32483254
int i;
32493255

32503256
trace_vnc_client_connect(vs, sioc);
3257+
vs->zlib = &vc->zlib;
32513258
vs->zrle = g_new0(VncZrle, 1);
32523259
vs->tight = g_new0(VncTight, 1);
32533260
vs->magic = VNC_MAGIC;
@@ -3270,7 +3277,7 @@ static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc,
32703277
#ifdef CONFIG_PNG
32713278
buffer_init(&vs->tight->png, "vnc-tight-png/%p", sioc);
32723279
#endif
3273-
buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc);
3280+
buffer_init(&vc->zlib.zlib, "vnc-zlib/%p", sioc);
32743281
buffer_init(&vs->zrle->zrle, "vnc-zrle/%p", sioc);
32753282
buffer_init(&vs->zrle->fb, "vnc-zrle-fb/%p", sioc);
32763283
buffer_init(&vs->zrle->zlib, "vnc-zrle-zlib/%p", sioc);

ui/vnc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ struct VncState
340340
* update vnc_async_encoding_start()
341341
*/
342342
VncTight *tight;
343-
VncZlib zlib;
343+
VncZlib *zlib;
344344
VncHextile hextile;
345345
VncZrle *zrle;
346346
VncZywrle zywrle;

0 commit comments

Comments
 (0)