[11/21] hw/ipmi: Fix latent realize() error handling bugs
diff mbox series

Message ID 20191130194240.10517-12-armbru@redhat.com
State New
Headers show
Series
  • Error handling fixes, may contain 4.2 material
Related show

Commit Message

Markus Armbruster Nov. 30, 2019, 7:42 p.m. UTC
isa_ipmi_bt_realize(), ipmi_isa_realize(), pci_ipmi_bt_realize(), and
pci_ipmi_kcs_realize() crash when IPMIInterfaceClass method init()
fails and their @errp argument is null.  First messed up in commit
0719029c47 "ipmi: Add an ISA KCS low-level interface", then imitated
in commit a9b74079cb "ipmi: Add a BT low-level interface" and commit
12f983c6aa "ipmi: Add PCI IPMI interfaces".

The bug can't bite as no caller actually passes null, and none of the
init() methods can actually fail.  Fix it anyway.

Cc: Corey Minyard <cminyard@mvista.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 hw/ipmi/isa_ipmi_bt.c  | 7 +++++--
 hw/ipmi/isa_ipmi_kcs.c | 7 +++++--
 hw/ipmi/pci_ipmi_bt.c  | 6 ++++--
 hw/ipmi/pci_ipmi_kcs.c | 6 ++++--
 4 files changed, 18 insertions(+), 8 deletions(-)

Comments

Corey Minyard Dec. 1, 2019, 6:22 p.m. UTC | #1
On Sat, Nov 30, 2019 at 08:42:30PM +0100, Markus Armbruster wrote:
> isa_ipmi_bt_realize(), ipmi_isa_realize(), pci_ipmi_bt_realize(), and
> pci_ipmi_kcs_realize() crash when IPMIInterfaceClass method init()
> fails and their @errp argument is null.  First messed up in commit
> 0719029c47 "ipmi: Add an ISA KCS low-level interface", then imitated
> in commit a9b74079cb "ipmi: Add a BT low-level interface" and commit
> 12f983c6aa "ipmi: Add PCI IPMI interfaces".
> 
> The bug can't bite as no caller actually passes null, and none of the
> init() methods can actually fail.  Fix it anyway.

Well, whatever.  It looks correct and is better style.  I've added this
to my tree.

-corey

> 
> Cc: Corey Minyard <cminyard@mvista.com>
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>  hw/ipmi/isa_ipmi_bt.c  | 7 +++++--
>  hw/ipmi/isa_ipmi_kcs.c | 7 +++++--
>  hw/ipmi/pci_ipmi_bt.c  | 6 ++++--
>  hw/ipmi/pci_ipmi_kcs.c | 6 ++++--
>  4 files changed, 18 insertions(+), 8 deletions(-)
> 
> diff --git a/hw/ipmi/isa_ipmi_bt.c b/hw/ipmi/isa_ipmi_bt.c
> index 9a87ffd3f0..9fba5ed383 100644
> --- a/hw/ipmi/isa_ipmi_bt.c
> +++ b/hw/ipmi/isa_ipmi_bt.c
> @@ -70,6 +70,7 @@ static void isa_ipmi_bt_lower_irq(IPMIBT *ib)
>  
>  static void isa_ipmi_bt_realize(DeviceState *dev, Error **errp)
>  {
> +    Error *err = NULL;
>      ISADevice *isadev = ISA_DEVICE(dev);
>      ISAIPMIBTDevice *iib = ISA_IPMI_BT(dev);
>      IPMIInterface *ii = IPMI_INTERFACE(dev);
> @@ -85,9 +86,11 @@ static void isa_ipmi_bt_realize(DeviceState *dev, Error **errp)
>      iib->bt.bmc->intf = ii;
>      iib->bt.opaque = iib;
>  
> -    iic->init(ii, 0, errp);
> -    if (*errp)
> +    iic->init(ii, 0, &err);
> +    if (err) {
> +        error_propagate(errp, err);
>          return;
> +    }
>  
>      if (iib->isairq > 0) {
>          isa_init_irq(isadev, &iib->irq, iib->isairq);
> diff --git a/hw/ipmi/isa_ipmi_kcs.c b/hw/ipmi/isa_ipmi_kcs.c
> index ca3ea36a3f..cc6bd817f2 100644
> --- a/hw/ipmi/isa_ipmi_kcs.c
> +++ b/hw/ipmi/isa_ipmi_kcs.c
> @@ -69,6 +69,7 @@ static void isa_ipmi_kcs_lower_irq(IPMIKCS *ik)
>  
>  static void ipmi_isa_realize(DeviceState *dev, Error **errp)
>  {
> +    Error *err = NULL;
>      ISADevice *isadev = ISA_DEVICE(dev);
>      ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(dev);
>      IPMIInterface *ii = IPMI_INTERFACE(dev);
> @@ -84,9 +85,11 @@ static void ipmi_isa_realize(DeviceState *dev, Error **errp)
>      iik->kcs.bmc->intf = ii;
>      iik->kcs.opaque = iik;
>  
> -    iic->init(ii, 0, errp);
> -    if (*errp)
> +    iic->init(ii, 0, &err);
> +    if (err) {
> +        error_propagate(errp, err);
>          return;
> +    }
>  
>      if (iik->isairq > 0) {
>          isa_init_irq(isadev, &iik->irq, iik->isairq);
> diff --git a/hw/ipmi/pci_ipmi_bt.c b/hw/ipmi/pci_ipmi_bt.c
> index 6ed925a665..ba9cf016b5 100644
> --- a/hw/ipmi/pci_ipmi_bt.c
> +++ b/hw/ipmi/pci_ipmi_bt.c
> @@ -54,6 +54,7 @@ static void pci_ipmi_lower_irq(IPMIBT *ik)
>  
>  static void pci_ipmi_bt_realize(PCIDevice *pd, Error **errp)
>  {
> +    Error *err = NULL;
>      PCIIPMIBTDevice *pik = PCI_IPMI_BT(pd);
>      IPMIInterface *ii = IPMI_INTERFACE(pd);
>      IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
> @@ -74,8 +75,9 @@ static void pci_ipmi_bt_realize(PCIDevice *pd, Error **errp)
>      pik->bt.raise_irq = pci_ipmi_raise_irq;
>      pik->bt.lower_irq = pci_ipmi_lower_irq;
>  
> -    iic->init(ii, 8, errp);
> -    if (*errp) {
> +    iic->init(ii, 8, &err);
> +    if (err) {
> +        error_propagate(errp, err);
>          return;
>      }
>      pci_register_bar(pd, 0, PCI_BASE_ADDRESS_SPACE_IO, &pik->bt.io);
> diff --git a/hw/ipmi/pci_ipmi_kcs.c b/hw/ipmi/pci_ipmi_kcs.c
> index eeba63baa4..99f46152f4 100644
> --- a/hw/ipmi/pci_ipmi_kcs.c
> +++ b/hw/ipmi/pci_ipmi_kcs.c
> @@ -54,6 +54,7 @@ static void pci_ipmi_lower_irq(IPMIKCS *ik)
>  
>  static void pci_ipmi_kcs_realize(PCIDevice *pd, Error **errp)
>  {
> +    Error *err = NULL;
>      PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(pd);
>      IPMIInterface *ii = IPMI_INTERFACE(pd);
>      IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
> @@ -74,8 +75,9 @@ static void pci_ipmi_kcs_realize(PCIDevice *pd, Error **errp)
>      pik->kcs.raise_irq = pci_ipmi_raise_irq;
>      pik->kcs.lower_irq = pci_ipmi_lower_irq;
>  
> -    iic->init(ii, 8, errp);
> -    if (*errp) {
> +    iic->init(ii, 8, &err);
> +    if (err) {
> +        error_propagate(errp, err);
>          return;
>      }
>      pci_register_bar(pd, 0, PCI_BASE_ADDRESS_SPACE_IO, &pik->kcs.io);
> -- 
> 2.21.0
>
Markus Armbruster Dec. 16, 2019, 9:20 a.m. UTC | #2
Corey Minyard <cminyard@mvista.com> writes:

> On Sat, Nov 30, 2019 at 08:42:30PM +0100, Markus Armbruster wrote:
>> isa_ipmi_bt_realize(), ipmi_isa_realize(), pci_ipmi_bt_realize(), and
>> pci_ipmi_kcs_realize() crash when IPMIInterfaceClass method init()
>> fails and their @errp argument is null.  First messed up in commit
>> 0719029c47 "ipmi: Add an ISA KCS low-level interface", then imitated
>> in commit a9b74079cb "ipmi: Add a BT low-level interface" and commit
>> 12f983c6aa "ipmi: Add PCI IPMI interfaces".
>> 
>> The bug can't bite as no caller actually passes null, and none of the
>> init() methods can actually fail.  Fix it anyway.
>
> Well, whatever.  It looks correct and is better style.  I've added this
> to my tree.

I've since posted v2 with a revamped commit message, and I'm ready to
post a pull request.  I really want the whole thing committed before the
Christmas break, so Vladimir can base on it more easily.  Options:

* You post a pull request before me.

* Ask me to drop this patch from my pull request, so you can take it
  through your tree at your leisure.

* Post your Reviewed-by or Acked-by for me to include in my pull
  request.

* Do nothing; I'll post my pull request later this week.
Corey Minyard Dec. 16, 2019, 2:13 p.m. UTC | #3
On Mon, Dec 16, 2019 at 10:20:04AM +0100, Markus Armbruster wrote:
> Corey Minyard <cminyard@mvista.com> writes:
> 
> I've since posted v2 with a revamped commit message, and I'm ready to
> post a pull request.  I really want the whole thing committed before the
> Christmas break, so Vladimir can base on it more easily.  Options:
> 
> * You post a pull request before me.
> 
> * Ask me to drop this patch from my pull request, so you can take it
>   through your tree at your leisure.
> 
> * Post your Reviewed-by or Acked-by for me to include in my pull
>   request.
> 
> * Do nothing; I'll post my pull request later this week.
> 

Since you already have it ready, I'll choose the "do nothing" patch
and let you post the pull request.  Thanks.

-corey
Markus Armbruster Dec. 17, 2019, 6:30 a.m. UTC | #4
Corey Minyard <cminyard@mvista.com> writes:

> On Mon, Dec 16, 2019 at 10:20:04AM +0100, Markus Armbruster wrote:
>> Corey Minyard <cminyard@mvista.com> writes:
>> 
>> I've since posted v2 with a revamped commit message, and I'm ready to
>> post a pull request.  I really want the whole thing committed before the
>> Christmas break, so Vladimir can base on it more easily.  Options:
>> 
>> * You post a pull request before me.
>> 
>> * Ask me to drop this patch from my pull request, so you can take it
>>   through your tree at your leisure.
>> 
>> * Post your Reviewed-by or Acked-by for me to include in my pull
>>   request.
>> 
>> * Do nothing; I'll post my pull request later this week.
>> 
>
> Since you already have it ready, I'll choose the "do nothing" patch
> and let you post the pull request.  Thanks.

Okay, sent.  Thanks for your quick reply!

Patch
diff mbox series

diff --git a/hw/ipmi/isa_ipmi_bt.c b/hw/ipmi/isa_ipmi_bt.c
index 9a87ffd3f0..9fba5ed383 100644
--- a/hw/ipmi/isa_ipmi_bt.c
+++ b/hw/ipmi/isa_ipmi_bt.c
@@ -70,6 +70,7 @@  static void isa_ipmi_bt_lower_irq(IPMIBT *ib)
 
 static void isa_ipmi_bt_realize(DeviceState *dev, Error **errp)
 {
+    Error *err = NULL;
     ISADevice *isadev = ISA_DEVICE(dev);
     ISAIPMIBTDevice *iib = ISA_IPMI_BT(dev);
     IPMIInterface *ii = IPMI_INTERFACE(dev);
@@ -85,9 +86,11 @@  static void isa_ipmi_bt_realize(DeviceState *dev, Error **errp)
     iib->bt.bmc->intf = ii;
     iib->bt.opaque = iib;
 
-    iic->init(ii, 0, errp);
-    if (*errp)
+    iic->init(ii, 0, &err);
+    if (err) {
+        error_propagate(errp, err);
         return;
+    }
 
     if (iib->isairq > 0) {
         isa_init_irq(isadev, &iib->irq, iib->isairq);
diff --git a/hw/ipmi/isa_ipmi_kcs.c b/hw/ipmi/isa_ipmi_kcs.c
index ca3ea36a3f..cc6bd817f2 100644
--- a/hw/ipmi/isa_ipmi_kcs.c
+++ b/hw/ipmi/isa_ipmi_kcs.c
@@ -69,6 +69,7 @@  static void isa_ipmi_kcs_lower_irq(IPMIKCS *ik)
 
 static void ipmi_isa_realize(DeviceState *dev, Error **errp)
 {
+    Error *err = NULL;
     ISADevice *isadev = ISA_DEVICE(dev);
     ISAIPMIKCSDevice *iik = ISA_IPMI_KCS(dev);
     IPMIInterface *ii = IPMI_INTERFACE(dev);
@@ -84,9 +85,11 @@  static void ipmi_isa_realize(DeviceState *dev, Error **errp)
     iik->kcs.bmc->intf = ii;
     iik->kcs.opaque = iik;
 
-    iic->init(ii, 0, errp);
-    if (*errp)
+    iic->init(ii, 0, &err);
+    if (err) {
+        error_propagate(errp, err);
         return;
+    }
 
     if (iik->isairq > 0) {
         isa_init_irq(isadev, &iik->irq, iik->isairq);
diff --git a/hw/ipmi/pci_ipmi_bt.c b/hw/ipmi/pci_ipmi_bt.c
index 6ed925a665..ba9cf016b5 100644
--- a/hw/ipmi/pci_ipmi_bt.c
+++ b/hw/ipmi/pci_ipmi_bt.c
@@ -54,6 +54,7 @@  static void pci_ipmi_lower_irq(IPMIBT *ik)
 
 static void pci_ipmi_bt_realize(PCIDevice *pd, Error **errp)
 {
+    Error *err = NULL;
     PCIIPMIBTDevice *pik = PCI_IPMI_BT(pd);
     IPMIInterface *ii = IPMI_INTERFACE(pd);
     IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
@@ -74,8 +75,9 @@  static void pci_ipmi_bt_realize(PCIDevice *pd, Error **errp)
     pik->bt.raise_irq = pci_ipmi_raise_irq;
     pik->bt.lower_irq = pci_ipmi_lower_irq;
 
-    iic->init(ii, 8, errp);
-    if (*errp) {
+    iic->init(ii, 8, &err);
+    if (err) {
+        error_propagate(errp, err);
         return;
     }
     pci_register_bar(pd, 0, PCI_BASE_ADDRESS_SPACE_IO, &pik->bt.io);
diff --git a/hw/ipmi/pci_ipmi_kcs.c b/hw/ipmi/pci_ipmi_kcs.c
index eeba63baa4..99f46152f4 100644
--- a/hw/ipmi/pci_ipmi_kcs.c
+++ b/hw/ipmi/pci_ipmi_kcs.c
@@ -54,6 +54,7 @@  static void pci_ipmi_lower_irq(IPMIKCS *ik)
 
 static void pci_ipmi_kcs_realize(PCIDevice *pd, Error **errp)
 {
+    Error *err = NULL;
     PCIIPMIKCSDevice *pik = PCI_IPMI_KCS(pd);
     IPMIInterface *ii = IPMI_INTERFACE(pd);
     IPMIInterfaceClass *iic = IPMI_INTERFACE_GET_CLASS(ii);
@@ -74,8 +75,9 @@  static void pci_ipmi_kcs_realize(PCIDevice *pd, Error **errp)
     pik->kcs.raise_irq = pci_ipmi_raise_irq;
     pik->kcs.lower_irq = pci_ipmi_lower_irq;
 
-    iic->init(ii, 8, errp);
-    if (*errp) {
+    iic->init(ii, 8, &err);
+    if (err) {
+        error_propagate(errp, err);
         return;
     }
     pci_register_bar(pd, 0, PCI_BASE_ADDRESS_SPACE_IO, &pik->kcs.io);