qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH v2] spapr_pci: Fix potential NULL pointer dereference in spapr_dt_pci_bus()
@ 2019-06-13 21:34 Philippe Mathieu-Daudé
  2019-06-14  1:32 ` David Gibson
  0 siblings, 1 reply; 2+ messages in thread
From: Philippe Mathieu-Daudé @ 2019-06-13 21:34 UTC (permalink / raw)
  To: qemu-devel
  Cc: Michael S . Tsirkin, Greg Kurz, qemu-ppc, Cédric Le Goater,
	Philippe Mathieu-Daudé, David Gibson

Commit 14e714900f6 refactored the call to spapr_dt_drc(),
introducing a potential NULL pointer dereference while
accessing bus->parent_dev.
A trivial audit show 'bus' is not null in the two places
the static function spapr_dt_drc() is called.

Since the 'bus' parameter is not NULL in both callers, remove
remove the test on if (bus), and add an assert() to silent
static analyzers.

This fixes:

  /hw/ppc/spapr_pci.c: 1367 in spapr_dt_pci_bus()
  >>>     CID 1401933:  Null pointer dereferences  (FORWARD_NULL)
  >>>     Dereferencing null pointer "bus".
  1367         ret = spapr_dt_drc(fdt, offset, OBJECT(bus->parent_dev),
  1368                            SPAPR_DR_CONNECTOR_TYPE_PCI);

Fixes: 14e714900f6
Reported-by: Coverity (CID 1401933)
Suggested-by: Greg Kurz <groug@kaod.org>
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
---
 hw/ppc/spapr_pci.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c
index 957ae88bbd..fbeb1c90ee 100644
--- a/hw/ppc/spapr_pci.c
+++ b/hw/ppc/spapr_pci.c
@@ -1356,12 +1356,11 @@ static int spapr_dt_pci_bus(SpaprPhbState *sphb, PCIBus *bus,
     _FDT(fdt_setprop_cell(fdt, offset, "#size-cells",
                           RESOURCE_CELLS_SIZE));
 
-    if (bus) {
-        pci_for_each_device_reverse(bus, pci_bus_num(bus),
-                                    spapr_dt_pci_device_cb, &cbinfo);
-        if (cbinfo.err) {
-            return cbinfo.err;
-        }
+    assert(bus);
+    pci_for_each_device_reverse(bus, pci_bus_num(bus),
+                                spapr_dt_pci_device_cb, &cbinfo);
+    if (cbinfo.err) {
+        return cbinfo.err;
     }
 
     ret = spapr_dt_drc(fdt, offset, OBJECT(bus->parent_dev),
-- 
2.20.1



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH v2] spapr_pci: Fix potential NULL pointer dereference in spapr_dt_pci_bus()
  2019-06-13 21:34 [Qemu-devel] [PATCH v2] spapr_pci: Fix potential NULL pointer dereference in spapr_dt_pci_bus() Philippe Mathieu-Daudé
@ 2019-06-14  1:32 ` David Gibson
  0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2019-06-14  1:32 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Cédric Le Goater, Greg Kurz, qemu-ppc, qemu-devel,
	Michael S . Tsirkin

[-- Attachment #1: Type: text/plain, Size: 2246 bytes --]

On Thu, Jun 13, 2019 at 11:34:06PM +0200, Philippe Mathieu-Daudé wrote:
> Commit 14e714900f6 refactored the call to spapr_dt_drc(),
> introducing a potential NULL pointer dereference while
> accessing bus->parent_dev.
> A trivial audit show 'bus' is not null in the two places
> the static function spapr_dt_drc() is called.
> 
> Since the 'bus' parameter is not NULL in both callers, remove
> remove the test on if (bus), and add an assert() to silent
> static analyzers.
> 
> This fixes:
> 
>   /hw/ppc/spapr_pci.c: 1367 in spapr_dt_pci_bus()
>   >>>     CID 1401933:  Null pointer dereferences  (FORWARD_NULL)
>   >>>     Dereferencing null pointer "bus".
>   1367         ret = spapr_dt_drc(fdt, offset, OBJECT(bus->parent_dev),
>   1368                            SPAPR_DR_CONNECTOR_TYPE_PCI);
> 
> Fixes: 14e714900f6
> Reported-by: Coverity (CID 1401933)
> Suggested-by: Greg Kurz <groug@kaod.org>
> Suggested-by: David Gibson <david@gibson.dropbear.id.au>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>

Applied, thanks.

> ---
>  hw/ppc/spapr_pci.c | 11 +++++------
>  1 file changed, 5 insertions(+), 6 deletions(-)
> 
> diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c
> index 957ae88bbd..fbeb1c90ee 100644
> --- a/hw/ppc/spapr_pci.c
> +++ b/hw/ppc/spapr_pci.c
> @@ -1356,12 +1356,11 @@ static int spapr_dt_pci_bus(SpaprPhbState *sphb, PCIBus *bus,
>      _FDT(fdt_setprop_cell(fdt, offset, "#size-cells",
>                            RESOURCE_CELLS_SIZE));
>  
> -    if (bus) {
> -        pci_for_each_device_reverse(bus, pci_bus_num(bus),
> -                                    spapr_dt_pci_device_cb, &cbinfo);
> -        if (cbinfo.err) {
> -            return cbinfo.err;
> -        }
> +    assert(bus);
> +    pci_for_each_device_reverse(bus, pci_bus_num(bus),
> +                                spapr_dt_pci_device_cb, &cbinfo);
> +    if (cbinfo.err) {
> +        return cbinfo.err;
>      }
>  
>      ret = spapr_dt_drc(fdt, offset, OBJECT(bus->parent_dev),

-- 
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

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-06-14  1:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-06-13 21:34 [Qemu-devel] [PATCH v2] spapr_pci: Fix potential NULL pointer dereference in spapr_dt_pci_bus() Philippe Mathieu-Daudé
2019-06-14  1:32 ` David Gibson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).