From nobody Fri Dec 19 16:03:36 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1744882943; cv=none; d=zohomail.com; s=zohoarc; b=EGmYu1yVsh1kY7s+2WMWK8cjCZcJuMdrXnPnh1jNpIbxunL+MPWvog+mcc7+/M/AK9JW1H0P5Nzx2ErcMa+yIpT1SBJ8bdfZyXjarN5v06E/O21ydlAYrq6rZjbtgqExWAH1/vvMA7Rnh4AlRH57xchWySapb3bxRWQRjrcESF8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744882943; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=CJoboHjY6J+jdcgbJlQKuZWK2SjqrcVgmsi0cNijwro=; b=dja6HYM4X1N+ruVyzQqpER3+HMmq004yVEitVoJXw+pFrJdXb2kvezZNge0q4NM42mkJ3vrB5oHfWCBGPFcguX0cdxKInkEhwneZaKdnxtrg1E86DuPd0+/YeKO3+Zvs+Mn+y4ouH3cD8x4Kql99KBhDCu0NQCi/5TpAVwkelr8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 174488294296587.53731063823068; Thu, 17 Apr 2025 02:42:22 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u5LlD-0007k1-CN; Thu, 17 Apr 2025 05:42:20 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u5Ll9-0007dg-E3 for qemu-devel@nongnu.org; Thu, 17 Apr 2025 05:42:16 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u5Ll7-0007xF-Bl for qemu-devel@nongnu.org; Thu, 17 Apr 2025 05:42:15 -0400 Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-22435603572so5923305ad.1 for ; Thu, 17 Apr 2025 02:42:12 -0700 (PDT) Received: from localhost ([2400:4050:b783:b00:9e36:5f4c:928c:4ec2]) by smtp.gmail.com with UTF8SMTPSA id d2e1a72fcca58-73bd21e1957sm11841296b3a.78.2025.04.17.02.42.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 17 Apr 2025 02:42:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1744882931; x=1745487731; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=CJoboHjY6J+jdcgbJlQKuZWK2SjqrcVgmsi0cNijwro=; b=pPSbt1yWWep+U2iuYUU7AZ+ir5Al7huE9NuorQmyNTmJl+0FKyqzheAB9iQ7Aq1i13 d0PeSV8TDJEiiTAAebr2XRL385TiK08l+gXeLKJLdq+/Op69sQ3dkvg4cWHk8AS0UV9O y1cZQBypmVEWFNKolNNLVzBGbeoymYis4v/f0VgCOijtHOGpffPEVYcNRkpS9aLzczAn Yt2XXx/NzA7RNQf/EGP43m/x4u/yGQ21NEqVKsTbPYPbf/xVG9uvw1rzNvd+GqdCqUsb DIRgvIUmp8kJNGWPQM1wv5jkOS/6dEpRXSqvzn4lcZe9q/N7UgLqv7p9EtLZG/rkOx8f B7xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744882931; x=1745487731; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CJoboHjY6J+jdcgbJlQKuZWK2SjqrcVgmsi0cNijwro=; b=TZQV/1WaGHm9Kh2wbzw9BsWJco4Gj4DBkhdX7CqmC0E+RLlRLCL9UP7qI7ZxnD4BcH qxzIMZfozu7iiJSQKozbh9Aij79Vi/XPy1d4BI6X7/5x1lU8iSNFDE4UPmyzho1oH4vi qxz0Dv905FGcIKiNzLAwr0BXit5A1aKSv5t5pNu6/sgz9Y80rBZS3jEv0qDKkbr30P0j VnkUHgnT4SX73cn9aVDJjWEB5cBLXuaGeblXnPK2NMvOV5YWILHIMRs8pvo0Msq74zu1 a+voofdmsd5waPmsjLh35CSjWEE3KjAQmexXjr8GrjFqbQLSm4JFDyZyzX/jw5rV/yFB O8ug== X-Gm-Message-State: AOJu0YyG5/dma6Q/1vdsc8/jrLqPo1slWPkyOgNAP/KlKvygn43uk4dB njyDmOuuD7lbfx+XIHze3QjpPD7a29d2e3BrJdbko+o0VROi2lO7UA0IBPVXt7fNvQ9GXdfKm2O W+/k= X-Gm-Gg: ASbGncuchKf+flIbYH1u62HCJnor+o3/YqHbezUPN5CQhlacnogEHFzTOFk26dGBDvI XNpuqLbSQJxUF0m9k40EbQi6IvTYq2yor6oq0Z5zuO+McIrr5TsoLMwhtmuj/ZefOtciSDqcqbc I57cGNhthXSNQgw59dW4b4xJlzwOv8R6cqmc7IjkmrZNuZhnnIB92Ub/9tq5VYSCMqV8tcR0Nak T9P4/zeAFvTJrwyAxmNvrDbTZnc4bWx7xAAydpXEMT+WS3kHRMB1zXAZZvbLjXBU70ShjKj8byp xe6QaA5U2v5SOgNnit0TTzdorHkd8pSYPjfJ9fCAgf4wzK9NqCVthrflAw== X-Google-Smtp-Source: AGHT+IFPKpVZ7B6Q+BslnifdAlqBMI/sddIoY1J+TEctzVr3eF8QK49v8kuJgaaYXmsLslVrdjhATA== X-Received: by 2002:a17:902:f68b:b0:211:e812:3948 with SMTP id d9443c01a7336-22c3579bae9mr84711395ad.0.1744882931337; Thu, 17 Apr 2025 02:42:11 -0700 (PDT) From: Akihiko Odaki Date: Thu, 17 Apr 2025 18:42:01 +0900 Subject: [PATCH 2/2] ui/vnc: Do not copy z_stream MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250417-zlib-v1-2-34fad73b843b@daynix.com> References: <20250417-zlib-v1-0-34fad73b843b@daynix.com> In-Reply-To: <20250417-zlib-v1-0-34fad73b843b@daynix.com> To: qemu-devel@nongnu.org Cc: =?utf-8?q?Marc-Andr=C3=A9_Lureau?= , devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.15-dev-edae6 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62a; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x62a.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1744882944460019000 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: bd023f953e5e ("vnc: threaded VNC server") Signed-off-by: Akihiko Odaki --- ui/vnc.h | 2 +- ui/vnc-enc-zlib.c | 30 +++++++++++++++--------------- ui/vnc-jobs.c | 2 -- ui/vnc.c | 2 +- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/ui/vnc.h b/ui/vnc.h index 1dc76c270c35..78a5fdfdc0d0 100644 --- a/ui/vnc.h +++ b/ui/vnc.h @@ -265,6 +265,7 @@ typedef struct VncWorker { uint8_t lossy_rect[VNC_STAT_ROWS][VNC_STAT_COLS]; =20 VncTight tight; + VncZlib zlib; VncZrle zrle; } VncWorker; =20 @@ -345,7 +346,6 @@ struct VncState /* Encoding specific, if you add something here, don't forget to * update vnc_async_encoding_start() */ - VncZlib zlib; VncHextile hextile; VncZywrle zywrle; =20 diff --git a/ui/vnc-enc-zlib.c b/ui/vnc-enc-zlib.c index 605030730a44..d8b4cf038b02 100644 --- a/ui/vnc-enc-zlib.c +++ b/ui/vnc-enc-zlib.c @@ -48,21 +48,21 @@ void vnc_zlib_zfree(void *x, void *addr) =20 static void vnc_zlib_start(VncState *vs) { - buffer_reset(&vs->zlib.zlib); + buffer_reset(&vs->worker->zlib.zlib); =20 // make the output buffer be the zlib buffer, so we can compress it la= ter - vs->zlib.tmp =3D vs->output; - vs->output =3D vs->zlib.zlib; + vs->worker->zlib.tmp =3D vs->output; + vs->output =3D vs->worker->zlib.zlib; } =20 static int vnc_zlib_stop(VncState *vs) { - z_streamp zstream =3D &vs->zlib.stream; + z_streamp zstream =3D &vs->worker->zlib.stream; int previous_out; =20 // switch back to normal output/zlib buffers - vs->zlib.zlib =3D vs->output; - vs->output =3D vs->zlib.tmp; + vs->worker->zlib.zlib =3D vs->output; + vs->output =3D vs->worker->zlib.tmp; =20 // compress the zlib buffer =20 @@ -85,24 +85,24 @@ static int vnc_zlib_stop(VncState *vs) return -1; } =20 - vs->zlib.level =3D vs->worker->tight.compression; + vs->worker->zlib.level =3D vs->worker->tight.compression; zstream->opaque =3D vs; } =20 - if (vs->worker->tight.compression !=3D vs->zlib.level) { + if (vs->worker->tight.compression !=3D vs->worker->zlib.level) { if (deflateParams(zstream, vs->worker->tight.compression, Z_DEFAULT_STRATEGY) !=3D Z_OK) { return -1; } - vs->zlib.level =3D vs->worker->tight.compression; + vs->worker->zlib.level =3D vs->worker->tight.compression; } =20 // reserve memory in output buffer - buffer_reserve(&vs->output, vs->zlib.zlib.offset + 64); + buffer_reserve(&vs->output, vs->worker->zlib.zlib.offset + 64); =20 // set pointers - zstream->next_in =3D vs->zlib.zlib.buffer; - zstream->avail_in =3D vs->zlib.zlib.offset; + zstream->next_in =3D vs->worker->zlib.zlib.buffer; + zstream->avail_in =3D vs->worker->zlib.zlib.offset; zstream->next_out =3D vs->output.buffer + vs->output.offset; zstream->avail_out =3D vs->output.capacity - vs->output.offset; previous_out =3D zstream->avail_out; @@ -147,8 +147,8 @@ int vnc_zlib_send_framebuffer_update(VncState *vs, int = x, int y, int w, int h) =20 void vnc_zlib_clear(VncState *vs) { - if (vs->zlib.stream.opaque) { - deflateEnd(&vs->zlib.stream); + if (vs->worker->zlib.stream.opaque) { + deflateEnd(&vs->worker->zlib.stream); } - buffer_free(&vs->zlib.zlib); + buffer_free(&vs->worker->zlib.zlib); } diff --git a/ui/vnc-jobs.c b/ui/vnc-jobs.c index d019d88536b7..dbbf62212c79 100644 --- a/ui/vnc-jobs.c +++ b/ui/vnc-jobs.c @@ -189,7 +189,6 @@ static void vnc_async_encoding_start(VncState *orig, Vn= cState *local) local->write_pixels =3D orig->write_pixels; local->client_pf =3D orig->client_pf; local->client_be =3D orig->client_be; - local->zlib =3D orig->zlib; local->hextile =3D orig->hextile; local->client_width =3D orig->client_width; local->client_height =3D orig->client_height; @@ -198,7 +197,6 @@ static void vnc_async_encoding_start(VncState *orig, Vn= cState *local) static void vnc_async_encoding_end(VncState *orig, VncState *local) { buffer_free(&local->output); - orig->zlib =3D local->zlib; orig->hextile =3D local->hextile; } =20 diff --git a/ui/vnc.c b/ui/vnc.c index ce0538f3654f..2203312fcbc9 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -3260,7 +3260,7 @@ static void vnc_connect(VncDisplay *vd, QIOChannelSoc= ket *sioc, #ifdef CONFIG_PNG buffer_init(&vs->worker->tight.png, "vnc-tight-png/%p", sioc); #endif - buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); + buffer_init(&vs->worker->zlib.zlib, "vnc-zlib/%p", sioc); buffer_init(&vs->worker->zrle.zrle, "vnc-zrle/%p", sioc); buffer_init(&vs->worker->zrle.fb, "vnc-zrle-fb/%p", sioc); buffer_init(&vs->worker->zrle.zlib, "vnc-zrle-zlib/%p", sioc); --=20 2.49.0