From nobody Fri Apr 19 12:39:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.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 63.128.21.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=1609977351; cv=none; d=zohomail.com; s=zohoarc; b=iNT3c8Fq3GaBzV8vyT1vx5VgBo/Da1F5xLWYqRT/8M5NRuYMnPLYYfOoJdiWRwmAVlgpgOrOPK7yzrkj96NV/NL4BqjJkuvtM7c6CS8Xgv3d9+MJrCkSn0b0MnKm0F+Gn6gWKA7Q8v1z3jV86uuo85tMef2i297lKRfp08QIeIU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609977351; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Qr7AYKvyVq4N8VYhBknR37ROfkc2r2aZL8385aIx1yk=; b=ULsyGIZp67fq9zrtYXxk17YIMsubSQICeWm/LrZ+PTsW8QndVPzEMTgM2c6+8JYIPAxkG9Gbupt+22Gv/vcf9rEQKRknpqzxi43FTgI26bfXcrzz4sJkfPRcAModLfgZjzWiwZUxnPZVLuAia0YmXx3OQ+4LG+f1qZzKOAIGxew= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1609977351890174.04352492920987; Wed, 6 Jan 2021 15:55:51 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-444-6i_dyW3jOYa1NnXNs-wduA-1; Wed, 06 Jan 2021 18:55:48 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id E156D800D53; Wed, 6 Jan 2021 23:55:42 +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 AFAEB1840B; Wed, 6 Jan 2021 23:55:42 +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 8F0BB4BB7B; Wed, 6 Jan 2021 23:55:41 +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 106Ntex5002582 for ; Wed, 6 Jan 2021 18:55:40 -0500 Received: by smtp.corp.redhat.com (Postfix) id 4DCC96064B; Wed, 6 Jan 2021 23:55:40 +0000 (UTC) Received: from vhost2.laine.org (ovpn-112-226.phx2.redhat.com [10.3.112.226]) by smtp.corp.redhat.com (Postfix) with ESMTP id EB4B1197F2; Wed, 6 Jan 2021 23:55:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1609977350; 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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Qr7AYKvyVq4N8VYhBknR37ROfkc2r2aZL8385aIx1yk=; b=GRqw7P+70TOjNfYNWvhCFoZeG04ucLLtMCwa4gPm6IzTbwe6HrBtdptUvAuIKiRHFaPvjU 2BHVKCtLDIaLxfRX2JJcvBh7O4l9Epimg9/091KaeJbqvae2OCCDITpQ4PY5mCRmJpxYVe tAfAN9Nu7Py3b01z1Z9DqBCJ5dQeruQ= X-MC-Unique: 6i_dyW3jOYa1NnXNs-wduA-1 From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH 1/2] lxc: remove unnecessary call to virNetDevReserveName() Date: Wed, 6 Jan 2021 18:55:34 -0500 Message-Id: <20210106235535.881120-2-laine@redhat.com> In-Reply-To: <20210106235535.881120-1-laine@redhat.com> References: <20210106235535.881120-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Shi Lei 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" In all cases *except* when parsing status XML as libvirt is being restarted, the XML parser will delete any manually specified interface name (aka "" aka net->ifname) that could have been generated by virNetDevGenerateName(). This means that during the setup when a domain is being started (e.g. during virLXCProcessSetupInterfaceTap()) it is pointless to call virNetDevReserveName() with any setting of net->ifname that has come from the XML parser - it is guaranteed to not fit the pattern of any auto-generated name, and so the call is just a NOP anyway. Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/lxc/lxc_process.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index a842ac91c5..f6932c248b 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -308,8 +308,6 @@ virLXCProcessSetupInterfaceTap(virDomainDefPtr vm, =20 VIR_DEBUG("calling vethCreate()"); parentVeth =3D net->ifname; - if (parentVeth) - virNetDevReserveName(parentVeth); =20 if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0) return NULL; --=20 2.29.2 From nobody Fri Apr 19 12:39:13 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.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 63.128.21.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=1609977358; cv=none; d=zohomail.com; s=zohoarc; b=FjNaxQ4C0gMTEwwztuJUB5YNqc3mMW2lgVKqhJ/Kq3pYO0myfRVIR8fdT86yFnJ4Z2DECpxo1dZTt9JROdcZe7Ev3HWR6TTyKYrKmLgUT92t4RTVbK5nitm1qomqVuDiQv0Y3IAE3f6ZAMC9tf7i9BrbEymHVfPdXL8Vhw8epQQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1609977358; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=oMsGl01dUCgH/O7i9MbhRq5Ku2OElTPvSE06t9hJyds=; b=A4cQTdNqhehWaCUOj7K2V6ft5eqKdKGW3dnbH9tX+fAiBG3ChV06eIZzxDiC0q/jYPALvFgGEUcKYKM6Zs5WYfgi1giKxlwe7MaKjpoffwt5YH2QHkTAZ+nNV0iYF2D87sK4YRW9inPYGYr0Fcqzn+FxBlJaKWU7pmYErBPuNdI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1609977358218326.71228237681544; Wed, 6 Jan 2021 15:55:58 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-158-amX4I9rENzylvpzsvxEdVg-1; Wed, 06 Jan 2021 18:55:52 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B1EE01005D4C; Wed, 6 Jan 2021 23:55:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9167C60C0F; Wed, 6 Jan 2021 23:55:47 +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 2DEB0180954D; Wed, 6 Jan 2021 23:55:47 +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 106Nteqa002587 for ; Wed, 6 Jan 2021 18:55:40 -0500 Received: by smtp.corp.redhat.com (Postfix) id C6D15197F2; Wed, 6 Jan 2021 23:55:40 +0000 (UTC) Received: from vhost2.laine.org (ovpn-112-226.phx2.redhat.com [10.3.112.226]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F1076064B; Wed, 6 Jan 2021 23:55:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1609977356; 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: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=oMsGl01dUCgH/O7i9MbhRq5Ku2OElTPvSE06t9hJyds=; b=E1n9NiY6CalMfgnis0jzde6umogxcjoDL+EjtZL7mO2D5a3w0OvbYIP7bD3vLiVPSa6Q/K xKDVgXeyEhfq4sBRjCT95lN89AT8NrVQBRVFVbCsr2TRpeJO2YMFGZT/hWUKsk7FyeylkN kUmz1ZQA6qdKrZrfDMQGlmACG1z7Cfc= X-MC-Unique: amX4I9rENzylvpzsvxEdVg-1 From: Laine Stump To: libvir-list@redhat.com Subject: [libvirt PATCH 2/2] lxc: eliminate leaked and dangling pointers in virLXCProcessSetupInterfaceTap Date: Wed, 6 Jan 2021 18:55:35 -0500 Message-Id: <20210106235535.881120-3-laine@redhat.com> In-Reply-To: <20210106235535.881120-1-laine@redhat.com> References: <20210106235535.881120-1-laine@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Cc: Shi Lei 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" The two scenarios were found by Coverity after a seemingly-unrelated change to virLXCProcessSetupInterfaceTap() (in commit ecfc2d5f43), and explained by John Ferlan here: https://www.redhat.com/archives/libvir-list/2020-December/msg00810.html To re-explain: a) On entry to virLXCProcessSetupInterfaceTap() if net->ifname !=3D NULL then a copy of net->ifname is made into parentVeth, and a reference to *that* pointer is sent down to virNetDevVethCreate(). b) If parentVeth (aka net->ifname) is a template name (e.g. "blah%d"), then virNetDevVethCreate() calls virNetDevGenerateName(), and if virNetDevGenerateName() successfully generates a usable name (e.g. "blah27") then it will free the original template string (which is pointed to by net->ifname and by parentVeth), then replace the pointer in parentVeth with a pointer to the new string. Note that net->ifname still points to the now-freed template string. c) returning back up to virLXCProcessSetupInterfaceTap(), we check if net->ifname =3D=3D NULL - it *isn't* (still contains stale pointer to template string), so we don't replace it with the pointer to the new string that is in parentVeth. d) Result: the new string is leaked once we return from virLXCProcessSetupInterfaceTap(), while there is a dangling pointer to the old string in net->ifname. There is also a leak if there is a failure somewhere between steps (b) and (c) above - the failure cleanup in virNetDevVethCreate() will only free the newly-generated parentVeth string if the original pointer was NULL (narrator: "It wasn't."). But it's a new string allocated by virNetDevGenerateName(), not the original string from net->ifname, so it really does need to be freed. The solution is to make a copy of the entire original string into a g_autofree pointer, then iff everything is successful we g_free() the original net->ifname and replace it by stealing the string returned by virNetDevVethCreate(). Signed-off-by: Laine Stump Reviewed-by: Michal Privoznik --- src/lxc/lxc_process.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c index f6932c248b..2266e2cc40 100644 --- a/src/lxc/lxc_process.c +++ b/src/lxc/lxc_process.c @@ -302,20 +302,17 @@ virLXCProcessSetupInterfaceTap(virDomainDefPtr vm, virDomainNetDefPtr net, const char *brname) { - char *parentVeth; + g_autofree char *parentVeth =3D NULL; g_autofree char *containerVeth =3D NULL; const virNetDevVPortProfile *vport =3D virDomainNetGetActualVirtPortPr= ofile(net); =20 VIR_DEBUG("calling vethCreate()"); - parentVeth =3D net->ifname; + parentVeth =3D g_strdup(net->ifname); =20 if (virNetDevVethCreate(&parentVeth, &containerVeth) < 0) return NULL; VIR_DEBUG("parentVeth: %s, containerVeth: %s", parentVeth, containerVe= th); =20 - if (net->ifname =3D=3D NULL) - net->ifname =3D parentVeth; - if (virNetDevSetMAC(containerVeth, &net->mac) < 0) return NULL; =20 @@ -355,6 +352,10 @@ virLXCProcessSetupInterfaceTap(virDomainDefPtr vm, virDomainConfNWFilterInstantiate(vm->name, vm->uuid, net, false) <= 0) return NULL; =20 + /* success is guaranteed, so update the interface object */ + g_free(net->ifname); + net->ifname =3D g_steal_pointer(&parentVeth); + return g_steal_pointer(&containerVeth); } =20 --=20 2.29.2