From nobody Sat May 18 10:57:24 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=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1673359079; cv=none; d=zohomail.com; s=zohoarc; b=IGyz8zg2gu0sCUwsv3N7lt1ieaiZdSn0jQOFj8u8pM/mSNdhTjTUkVu3HPu9X57lW4pDWCZX0r/h1701q95A27t6zJtvCCgf67BDU3xrABjOYSy/srjc68RRwECr/l6yTM/TPuxfkWV5A/vPfKsHJzpM2To07sTuqECJBV3fQEA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1673359079; h=Content-Type:Content-Transfer-Encoding:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=dH6iwFUoqy0dx4IxH81lU3ddWpBxrUuWssBbAPbEWQ8=; b=DnlDhcDtPBeKAu05wvAikOtUXv/Vm+zL1z4YHWBWQ/zQ9WQxzXoQKVyC3JQTpM664f+TA6LA1lZbeHmUnsK/ihp0EIY790H0AmNX4zDl3/E4C7eLhN7VNHNG+Ye52x249OYNCTFRqxOaD7chVmObvphXMS4lh0lLTOhzO9lzZBc= 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=pass header.from= (p=none dis=none) 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 1673359079505855.5683406983142; Tue, 10 Jan 2023 05:57:59 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-257-A2wQboBFPGOYzxjsypvgbA-1; Tue, 10 Jan 2023 08:57:48 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A11E0802C1C; Tue, 10 Jan 2023 13:57:45 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (unknown [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8A9FD51E5; Tue, 10 Jan 2023 13:57:45 +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 641D21946A6C; Tue, 10 Jan 2023 13:57:45 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 9229B1946587 for ; Tue, 10 Jan 2023 13:57:44 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 821D040C2005; Tue, 10 Jan 2023 13:57:44 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id F247140C2004 for ; Tue, 10 Jan 2023 13:57:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1673359078; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to: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=dH6iwFUoqy0dx4IxH81lU3ddWpBxrUuWssBbAPbEWQ8=; b=NNVMs9xHrOWVlnCIZipe6kqMhavesjBaCC94X2SIfZ2SZGbzCdTlukfCOR8FVPHuGoN8NI hpDwWzYYN/fqmAjUnUfTWeN6jY9Kz2DhpA6WEbJ8DaNYdKkWTVBUFG21WC4Pa12/NB+W4S 9ZmLe5UCFsQqc8fErAHFYe9pAJN8OCs= X-MC-Unique: A2wQboBFPGOYzxjsypvgbA-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH] qemu: Fix handling of passed FDs in remoteDispatchDomainFdAssociate Date: Tue, 10 Jan 2023 14:57:42 +0100 Message-Id: <67b5b4b733d2b66d4eb9391f99e0e13f526f0095.1673359062.git.pkrempa@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 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: 1673359081434100001 Content-Type: text/plain; charset="utf-8" To ensure same behaviour when remote driver is or is not used we must not steal the FDs and array holding them passed to qemuDomainFDAssociate but rather duplicate them. At the same time the remote driver must close and free them to prevent leak. Pointed out by Coverity as FD leak on error path: *** CID 404348: Resource leaks (RESOURCE_LEAK) /src/remote/remote_daemon_dispatch.c: 7484 in remoteDispatchDomainFdAssoci= ate() 7478 rv =3D 0; 7479 7480 cleanup: 7481 if (rv < 0) 7482 virNetMessageSaveError(rerr); 7483 virObjectUnref(dom); >>> CID 404348: Resource leaks (RESOURCE_LEAK) >>> Variable "fds" going out of scope leaks the storage it points to. 7484 return rv; Fixes: abd9025c2fd Fixes: f762f87534e Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/qemu/qemu_driver.c | 15 ++++++++++++--- src/remote/remote_daemon_dispatch.c | 3 +++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index a88c9ebe64..d6879175fe 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -20442,7 +20442,8 @@ qemuDomainFDAssociate(virDomainPtr domain, { virDomainObj *vm =3D NULL; qemuDomainObjPrivate *priv; - virStorageSourceFDTuple *new; + g_autoptr(virStorageSourceFDTuple) new =3D NULL; + size_t i; int ret =3D -1; virCheckFlags(VIR_DOMAIN_FD_ASSOCIATE_SECLABEL_RESTORE | @@ -20460,8 +20461,16 @@ qemuDomainFDAssociate(virDomainPtr domain, priv =3D vm->privateData; new =3D virStorageSourceFDTupleNew(); - new->fds =3D fds; new->nfds =3D nfds; + new->fds =3D g_new0(int, new->nfds); + for (i =3D 0; i < new->nfds; i++) { + if ((new->fds[i] =3D dup(fds[i])) < 0) { + virReportSystemError(errno, + _("failed to duplicate passed fd with ind= ex '%zu'"), + i); + goto cleanup; + } + } new->conn =3D domain->conn; new->writable =3D flags & VIR_DOMAIN_FD_ASSOCIATE_SECLABEL_WRITABLE; @@ -20469,7 +20478,7 @@ qemuDomainFDAssociate(virDomainPtr domain, virCloseCallbacksDomainAdd(vm, domain->conn, qemuDomainFDHashCloseConn= ect); - g_hash_table_insert(priv->fds, g_strdup(name), new); + g_hash_table_insert(priv->fds, g_strdup(name), g_steal_pointer(&new)); ret =3D 0; diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon= _dispatch.c index 40c734ce6b..6c56e9ec3e 100644 --- a/src/remote/remote_daemon_dispatch.c +++ b/src/remote/remote_daemon_dispatch.c @@ -7478,6 +7478,9 @@ remoteDispatchDomainFdAssociate(virNetServer *server = G_GNUC_UNUSED, rv =3D 0; cleanup: + for (i =3D 0; i < nfds; i++) + VIR_FORCE_CLOSE(fds[i]); + g_free(fds); if (rv < 0) virNetMessageSaveError(rerr); virObjectUnref(dom); --=20 2.38.1