From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9130C432BE for ; Tue, 10 Aug 2021 04:42:34 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2876660E78 for ; Tue, 10 Aug 2021 04:42:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2876660E78 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:58800 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mDJay-00070t-UZ for qemu-devel@archiver.kernel.org; Tue, 10 Aug 2021 00:42:33 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55734) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mDJZE-00068m-Nu; Tue, 10 Aug 2021 00:40:44 -0400 Received: from bilbo.ozlabs.org ([2401:3900:2:1::2]:43431 helo=ozlabs.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mDJZC-0004XG-3W; Tue, 10 Aug 2021 00:40:44 -0400 Received: by ozlabs.org (Postfix, from userid 1007) id 4GkKyD4RL5z9sXV; Tue, 10 Aug 2021 14:40:36 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1628570436; bh=jMrdxJD34kWETxGyLvthGaS+A6TwPpmJ9O4pGElf1e4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=DOPOEr7vc1jbBCZp3m2IRUJnMHrwAWNg/hVanNKHjNz1Sh/6BOEpNrYN56ceiNwIY +taHbNAGFCx2Oo9UsZHjOZ9jKVZHhxBns+hIG/vFu9752sCvS+a1DqXH64FRqHJ74c FrUY4yvvRHa/KJmG/ceqsnD4KxrjTJd8TgeJSQps= Date: Tue, 10 Aug 2021 14:29:35 +1000 From: David Gibson To: Peter Maydell Subject: Re: [PULL 24/30] spapr_pci: populate ibm,loc-code Message-ID: References: <1436284182-5063-1-git-send-email-agraf@suse.de> <1436284182-5063-25-git-send-email-agraf@suse.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="wXfI/tJOKpDi25++" Content-Disposition: inline In-Reply-To: Received-SPF: pass client-ip=2401:3900:2:1::2; envelope-from=dgibson@ozlabs.org; helo=ozlabs.org X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.248, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: qemu-ppc , QEMU Developers , Nikunj A Dadhania , Greg Kurz Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --wXfI/tJOKpDi25++ Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mon, Aug 09, 2021 at 10:57:00AM +0100, Peter Maydell wrote: > On Tue, 7 Jul 2015 at 16:49, Alexander Graf wrote: > > > > From: Nikunj A Dadhania > > > > Each hardware instance has a platform unique location code. The OF > > device tree that describes a part of a hardware entity must include > > the =E2=80=9Cibm,loc-code=E2=80=9D property with a value that represent= s the location > > code for that hardware entity. > > > > Populate ibm,loc-code. >=20 > Ancient patch, but Coverity has just noticed a bug in it > which is still present in current QEMU (CID 1460454): >=20 > > +static char *spapr_phb_vfio_get_loc_code(sPAPRPHBState *sphb, PCIDevi= ce *pdev) > > +{ > > + char *path =3D NULL, *buf =3D NULL, *host =3D NULL; > > + > > + /* Get the PCI VFIO host id */ > > + host =3D object_property_get_str(OBJECT(pdev), "host", NULL); > > + if (!host) { > > + goto err_out; > > + } > > + > > + /* Construct the path of the file that will give us the DT locatio= n */ > > + path =3D g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host); > > + g_free(host); > > + if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) { > > + goto err_out; > > + } > > + g_free(path); >=20 > Here we create a 'path' string, use it as the argument to > g_file_get_contents() and then free it (either here or in the err_out pat= h)... >=20 > > + > > + /* Construct and read from host device tree the loc-code */ > > + path =3D g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf); > > + g_free(buf); > > + if (!path || !g_file_get_contents(path, &buf, NULL, NULL)) { > > + goto err_out; > > + } > > + return buf; >=20 > ...but here we forget to free it before returning in the success case. >=20 > > + > > +err_out: > > + g_free(path); > > + return NULL; > > +} >=20 > Cleanest fix would be to declare 'path' and 'host' as > g_autofree char *path =3D NULL; > g_autofree char *host =3D NULL; > and then you can remove all the manual g_free(path) and g_free(host) call= s. Thanks for the report. I've committed the fix (I hope) below to ppc-for-6.= 1: =46rom 70ae61b510dc571c407b28c46498cae60e60ca66 Mon Sep 17 00:00:00 2001 =46rom: David Gibson Date: Tue, 10 Aug 2021 14:28:19 +1000 Subject: [PATCH] spapr_pci: Fix leak in spapr_phb_vfio_get_loc_code() with g_autofree This uses g_autofree to simplify logic in spapr_phb_vfio_get_loc_code(), in the process fixing a leak in one of the paths. I'm told this fixes Coverity error CID 1460454 Reported-by: Peter Maydell Fixes: 16b0ea1d852 ("spapr_pci: populate ibm,loc-code") Signed-off-by: David Gibson --- hw/ppc/spapr_pci.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index 7a725855f9..13d806f390 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -782,33 +782,28 @@ static AddressSpace *spapr_pci_dma_iommu(PCIBus *bus,= void *opaque, int devfn) =20 static char *spapr_phb_vfio_get_loc_code(SpaprPhbState *sphb, PCIDevice *= pdev) { - char *path =3D NULL, *buf =3D NULL, *host =3D NULL; + g_autofree char *path =3D NULL; + g_autofree char *host =3D NULL; + char *buf =3D NULL; =20 /* Get the PCI VFIO host id */ host =3D object_property_get_str(OBJECT(pdev), "host", NULL); if (!host) { - goto err_out; + return NULL; } =20 /* Construct the path of the file that will give us the DT location */ path =3D g_strdup_printf("/sys/bus/pci/devices/%s/devspec", host); - g_free(host); if (!g_file_get_contents(path, &buf, NULL, NULL)) { - goto err_out; + return NULL; } - g_free(path); =20 /* Construct and read from host device tree the loc-code */ path =3D g_strdup_printf("/proc/device-tree%s/ibm,loc-code", buf); - g_free(buf); if (!g_file_get_contents(path, &buf, NULL, NULL)) { - goto err_out; + return NULL; } return buf; - -err_out: - g_free(path); - return NULL; } =20 static char *spapr_phb_get_loc_code(SpaprPhbState *sphb, PCIDevice *pdev) --=20 2.31.1 --=20 David Gibson | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_ | _way_ _around_! http://www.ozlabs.org/~dgibson --wXfI/tJOKpDi25++ Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEdfRlhq5hpmzETofcbDjKyiDZs5IFAmESAKwACgkQbDjKyiDZ s5LlixAAm2JaY7blgq/I9/zNF5cAX4RHVvuUy7tXUHOfUEnN15A8806lwlWSc6Jl kaEKRmMR7CadlmcTLfm3LxKvjse4kWSQkD4K9FHQ2Oi0OIbMkcKZXtx7oQNqvcq2 NafJezpfU/03tK3UrvBYmdaSH54ecJtSZ1+X/1MVpqCPbCrgTjUy7NJlj29odrL9 AdIs+1w2Yfp0FjoUn8hjSaCvptmX5qhlnl3Me/CnAHzrA67Z+1ACRMj1gHh8kJ78 20KRqyvcKv91e6RvFZsupHo/HF/sBQAoRk5A1Xr+29MwslU7yPtNsxBt9ZksKwmb HDD8JKTtg3XKUXIDTEcPNjSipzaTAieKMA0XkgLd4VxxT6p50i6KLYur0Hx+lskS SnqVGRIqLvb4EXxjYqepVSWxGMbJ8x/rtVv2/PiB853npeS25N0RgnCbyKNioiYb nE7n8zqseLR4q4lSdP+KuvNkdE4lSNzmmvcgsX+hlP+2Bj0uuL72movmanNTwQej WhhGWT+h83sF9f8/IrrYN5i6iyIFKHsuObBPtUY1LYzELBg4O/+GeokkED7ADYw5 sUVcJpZyGdbJ0mlBH9w5+yCYfjRyow2apfQZM3V4zwhkyHOWJS9BU/uTeUMEjDy7 4r3a9u/J++LxGbUumKEV/ssnyjLuSsga5TmcGoa6NSiCVPeONzM= =Hl5c -----END PGP SIGNATURE----- --wXfI/tJOKpDi25++--