From nobody Fri May 3 03:32:47 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1547800739082320.5542240095119; Fri, 18 Jan 2019 00:38:59 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0319788E76; Fri, 18 Jan 2019 08:38:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id CEDB75D737; Fri, 18 Jan 2019 08:38:54 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id AD4073F954; Fri, 18 Jan 2019 08:38:50 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x0I8cnJD004323 for ; Fri, 18 Jan 2019 03:38:49 -0500 Received: by smtp.corp.redhat.com (Postfix) id 20EAE600C9; Fri, 18 Jan 2019 08:38:49 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 16C10600CC for ; Fri, 18 Jan 2019 08:38:45 +0000 (UTC) Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C03A4A0A57 for ; Fri, 18 Jan 2019 08:38:43 +0000 (UTC) Received: from [10.94.3.220] (helo=dim-vz7.qa.sw.ru) by relay.sw.ru with esmtp (Exim 4.91) (envelope-from ) id 1gkPfl-0008TE-0Z for libvir-list@redhat.com; Fri, 18 Jan 2019 11:38:41 +0300 From: Nikolay Shirokovskiy To: libvir-list@redhat.com Date: Fri, 18 Jan 2019 11:38:39 +0300 Message-Id: <1547800719-619233-1-git-send-email-nshirokovskiy@virtuozzo.com> X-Greylist: Sender passed SPF test, ACL 242 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 18 Jan 2019 08:38:44 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 18 Jan 2019 08:38:44 +0000 (UTC) for IP:'185.231.240.75' DOMAIN:'relay.sw.ru' HELO:'relay.sw.ru' FROM:'nshirokovskiy@virtuozzo.com' RCPT:'' X-RedHat-Spam-Score: -0.001 (SPF_PASS) 185.231.240.75 relay.sw.ru 185.231.240.75 relay.sw.ru X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] rpc: virNetClientNew: fix socket leak on error path X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Fri, 18 Jan 2019 08:38:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" if virNetClientNew finishes with error before sock is set to client object then sock does not get unrefed. This is unexpected by function clients like virNetClientNewUNIX. Let's make sure sock gets unrefed on any error path. Next some clients like virNetClientNewLibSSH2 try to unref sock on virNetClientNew errors. This is not correct even before this patch because in some cases virNetClientNew unrefed sock on error path by itself. Let's give up sock managment to virNetClientNew entirely. Signed-off-by: Nikolay Shirokovskiy Reviewed-by: Daniel P. Berrang=C3=A9 --- src/rpc/virnetclient.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index 2aced79..b7ffdcb 100644 --- a/src/rpc/virnetclient.c +++ b/src/rpc/virnetclient.c @@ -299,7 +299,7 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr = sock, int wakeupFD[2] =3D { -1, -1 }; =20 if (virNetClientInitialize() < 0) - return NULL; + goto error; =20 if (pipe2(wakeupFD, O_CLOEXEC) < 0) { virReportSystemError(errno, "%s", @@ -311,6 +311,7 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr = sock, goto error; =20 client->sock =3D sock; + sock =3D NULL; client->wakeupReadFD =3D wakeupFD[0]; client->wakeupSendFD =3D wakeupFD[1]; wakeupFD[0] =3D wakeupFD[1] =3D -1; @@ -327,6 +328,7 @@ static virNetClientPtr virNetClientNew(virNetSocketPtr = sock, VIR_FORCE_CLOSE(wakeupFD[0]); VIR_FORCE_CLOSE(wakeupFD[1]); virObjectUnref(client); + virObjectUnref(sock); return NULL; } =20 @@ -513,7 +515,6 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host, =20 if (!(ret =3D virNetClientNew(sock, NULL))) goto cleanup; - sock =3D NULL; =20 cleanup: VIR_FREE(command); @@ -522,7 +523,6 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host, VIR_FREE(homedir); VIR_FREE(confdir); VIR_FREE(nc); - virObjectUnref(sock); return ret; =20 no_memory: @@ -619,7 +619,6 @@ virNetClientPtr virNetClientNewLibssh(const char *host, =20 if (!(ret =3D virNetClientNew(sock, NULL))) goto cleanup; - sock =3D NULL; =20 cleanup: VIR_FREE(command); @@ -628,7 +627,6 @@ virNetClientPtr virNetClientNewLibssh(const char *host, VIR_FREE(homedir); VIR_FREE(confdir); VIR_FREE(nc); - virObjectUnref(sock); return ret; =20 no_memory: --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list