From nobody Sat May 4 21:11:38 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=quarantine dis=quarantine) header.from=huawei.com ARC-Seal: i=1; a=rsa-sha256; t=1664522103; cv=none; d=zohomail.com; s=zohoarc; b=JOcJe2h2O2csHnGYK+HzjdIuwoYawqp9kqo53+g9h8Mi+D6G3Fqt0rqa1X0nOvprLvJpgqCwJENZQQyzp8Iihuyg1norq/tzvCgZfgyYyA2wZErlJ/a2J5bZHxDlYkseh4b5vbRg+ataU6r/lR7qVid/Sy++i8x4J+wMYNHCSRc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1664522103; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=TBBbTCFIEoRvbujMWJnUcN/DDxeJorEt0nKA/hQ8/+8=; b=PugSHhmpbNa5pjOp1BuA/XcfZbjLr08worf00uwmt+udVHoTvnc1APjJ3G78d7JFQWDcGqifwgBvVqYNFD7jAz0742WoRLHRPc+5NP1AHy5sLq3K22X+PRX2qBXfquAIcrQS4Ty+zcXw0j0XwZlRNSGu7SkVN1Hk9BNy2pUZ/ac= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail header.from= (p=quarantine dis=quarantine) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.zohomail.com with SMTPS id 1664522103865376.923951648557; Fri, 30 Sep 2022 00:15:03 -0700 (PDT) Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-655-FWvRG02tNWmrMe21-A1EwQ-1; Fri, 30 Sep 2022 03:14:56 -0400 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 07E413C0D873; Fri, 30 Sep 2022 07:14:54 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B7E09477F5E; Fri, 30 Sep 2022 07:14:53 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 804031946A52; Fri, 30 Sep 2022 07:14:53 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 01B1A1946A4E for ; Fri, 30 Sep 2022 07:14:51 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id E67192166B2A; Fri, 30 Sep 2022 07:14:51 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast01.extmail.prod.ext.rdu2.redhat.com [10.11.55.17]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DF2192166B26 for ; Fri, 30 Sep 2022 07:14:51 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id BEE8385A5B6 for ; Fri, 30 Sep 2022 07:14:51 +0000 (UTC) Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-47-Awm18csXOFGc-peY1p0W0Q-1; Fri, 30 Sep 2022 03:14:47 -0400 Received: from dggpeml500022.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Mf1b609Ghz1P6mL for ; Fri, 30 Sep 2022 15:10:26 +0800 (CST) Received: from localhost.localdomain (10.175.124.27) by dggpeml500022.china.huawei.com (7.185.36.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Fri, 30 Sep 2022 15:14:44 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664522102; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=TBBbTCFIEoRvbujMWJnUcN/DDxeJorEt0nKA/hQ8/+8=; b=XoUUmDvtbDfzSQL6Wc2W2+oh/L3mvSbbm94hbe8Bvmh3XojL9ICVURpHJ/R0b9SQ9XQxWU P1H/TIds96nj+F1E2Fr2ZKDXvewMxjPXLpOlwzuKnX00kx6tR/XhJe2XWH5kspk2qDgB5Z HZpsH1Iki4GL8szsUIBe2cHcjSsTao0= X-MC-Unique: FWvRG02tNWmrMe21-A1EwQ-1 X-Original-To: libvir-list@listman.corp.redhat.com X-MC-Unique: Awm18csXOFGc-peY1p0W0Q-1 From: Jiang Jiacheng To: Subject: [PATCH v3] rpc: fix memory leak in virNetServerClientNew and virNetServerProgramDispatchCall Date: Fri, 30 Sep 2022 15:02:47 +0800 Message-ID: <20220930070247.2491875-1-jiangjiacheng@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.175.124.27] X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500022.china.huawei.com (7.185.36.66) X-CFilter-Loop: Reflected X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: yubihong@huawei.com, zhengchuan@huawei.com, linyilu@huawei.com, jiangjiacheng@huawei.com, xiexiangyou@huawei.com Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1664522106047100001 Content-Type: text/plain; charset="utf-8" From: jiangjiacheng In virNetServerProgramDispatchCall, The arg is passed as a void* and used t= o point to a certain struct depended on the dispatcher, so I think it's the memory = of the struct's member that leaks and this memory shuld be freed by xdr_free. In virNetServerClientNew, client->rx is assigned by invoking virNetServerCl= ientNew, but isn't freed if client->privateData's initialization failed, which leads= to a memory leak. Thanks to Liang Peng's suggestion, put virNetMessageFree(clien= t->rx) into virNetServerClientDispose() to release the memory. diff to v2: - virNetServerProgramDispatchCall, only free the memory in successful path = and error label. Signed-off-by: jiangjiacheng Reviewed-by: Michal Privoznik --- src/rpc/virnetserverclient.c | 2 ++ src/rpc/virnetserverprogram.c | 22 ++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/rpc/virnetserverclient.c b/src/rpc/virnetserverclient.c index a7d2dfa795..30f6af7be5 100644 --- a/src/rpc/virnetserverclient.c +++ b/src/rpc/virnetserverclient.c @@ -931,6 +931,8 @@ void virNetServerClientDispose(void *obj) PROBE(RPC_SERVER_CLIENT_DISPOSE, "client=3D%p", client); =20 + if (client->rx) + virNetMessageFree(client->rx); if (client->privateData) client->privateDataFreeFunc(client->privateData); =20 diff --git a/src/rpc/virnetserverprogram.c b/src/rpc/virnetserverprogram.c index 3ddf9f0428..94660f867a 100644 --- a/src/rpc/virnetserverprogram.c +++ b/src/rpc/virnetserverprogram.c @@ -368,7 +368,7 @@ virNetServerProgramDispatchCall(virNetServerProgram *pr= og, g_autofree char *arg =3D NULL; g_autofree char *ret =3D NULL; int rv =3D -1; - virNetServerProgramProc *dispatcher; + virNetServerProgramProc *dispatcher =3D NULL; virNetMessageError rerr; size_t i; g_autoptr(virIdentity) identity =3D NULL; @@ -446,8 +446,6 @@ virNetServerProgramDispatchCall(virNetServerProgram *pr= og, msg->nfds =3D 0; } =20 - xdr_free(dispatcher->arg_filter, arg); - if (rv < 0) goto error; =20 @@ -460,28 +458,28 @@ virNetServerProgramDispatchCall(virNetServerProgram *= prog, /*msg->header.serial =3D msg->header.serial;*/ msg->header.status =3D VIR_NET_OK; =20 - if (virNetMessageEncodeHeader(msg) < 0) { - xdr_free(dispatcher->ret_filter, ret); + if (virNetMessageEncodeHeader(msg) < 0) goto error; - } =20 if (msg->nfds && - virNetMessageEncodeNumFDs(msg) < 0) { - xdr_free(dispatcher->ret_filter, ret); + virNetMessageEncodeNumFDs(msg) < 0) goto error; - } =20 - if (virNetMessageEncodePayload(msg, dispatcher->ret_filter, ret) < 0) { - xdr_free(dispatcher->ret_filter, ret); + if (virNetMessageEncodePayload(msg, dispatcher->ret_filter, ret) < 0) goto error; - } =20 + xdr_free(dispatcher->arg_filter, arg); xdr_free(dispatcher->ret_filter, ret); =20 /* Put reply on end of tx queue to send out */ return virNetServerClientSendMessage(client, msg); =20 error: + if (dispatcher) { + xdr_free(dispatcher->arg_filter, arg); + xdr_free(dispatcher->ret_filter, ret); + } + =20 /* Bad stuff (de-)serializing message, but we have an * RPC error message we can send back to the client */ rv =3D virNetServerProgramSendReplyError(prog, client, msg, &rerr, &ms= g->header); --=20 2.33.0