Skip to contentSkip to conversation

Cleanup and fix @errp dereference

PATCH In review Zhao Liu 7 participants 18 messages Feb 23, 2024 – Mar 12, 2024

lore
Share & tools
lore

Zhao Liu

author
#0 lore raw

v2 cover Cleanup and fix @errp dereference

From: Zhao Liu <[email protected]>

Hi all,

This is my v2 series to clean and fix @errp dereference.

Introduction
============

The patches 1-6 fix the cases that deference @errp without ERRP_GUARD(), and they are based on my previsous v1 [1].

The patch 7 is merged in this series from another single patch [2].

Based on v1, v2 mainly add more description about problematic codes to make review easier (with Markus' suggestion) and avoid dereferencing @errp in patch 3's special case.

Future Work
===========

Additionally, I realized that my journey to fix @errp with ERRP_GUARD() is still not over, because @errp's second restriction said:

* Without ERRP_GUARD(), use of the @errp parameter is restricted:
...
* - It should not be passed to error_prepend() or
* error_append_hint(), because that doesn't work with &error_fatal.
* ERRP_GUARD() lifts these restrictions.

With this restrication, there're still many places need to add missing ERRP_GUARD().

[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks and Best Regards,
Zhao

---
Zhao Liu (7):
hw/cxl/cxl-host: Fix missing ERRP_GUARD() in
cxl_fixed_memory_window_config()
hw/display/macfb: Fix missing ERRP_GUARD() in macfb_nubus_realize() hw/mem/cxl_type3: Fix missing ERRP_GUARD() in ct3_realize() hw/misc/xlnx-versal-trng: Check returned bool in
trng_prop_fault_event_set()
hw/pci-bridge/cxl_upstream: Fix missing ERRP_GUARD() in
cxl_usp_realize()
hw/vfio/iommufd: Fix missing ERRP_GUARD() in iommufd_cdev_getfd() hw/intc: Check @errp to handle the error of
IOAPICCommonClass.realize()

 hw/cxl/cxl-host.c            | 1 +
 hw/display/macfb.c           | 1 +
 hw/intc/ioapic_common.c      | 4 ++++
 hw/mem/cxl_type3.c           | 1 +
 hw/misc/xlnx-versal-trng.c   | 3 +--
 hw/pci-bridge/cxl_upstream.c | 1 +
 hw/vfio/iommufd.c            | 1 +
 7 files changed, 10 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v2 1/7 hw/cxl/cxl-host: Fix missing ERRP_GUARD() in cxl_fixed_memory_window_config()

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in cxl_fixed_memory_window_config(), @errp is dereferenced in 2 places without ERRP_GUARD():

fw->enc_int_ways = cxl_interleave_ways_enc(fw->num_targets, errp);

if (*errp) {
    return;
}

and

fw->enc_int_gran =

    cxl_interleave_granularity_enc(object->interleave_granularity,
                                   errp);
if (*errp) {
    return;
}

For the above 2 places, we check "*errp", because neither function returns a suitable error code. And since machine_set_cfmw() - the caller of cxl_fixed_memory_window_config() - doesn't get the NULL @errp parameter as the "set" method of object property, cxl_fixed_memory_window_config() hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in cxl_fixed_memory_window_config().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/cxl/cxl-host.c | 1 +
 1 file changed, 1 insertion(+)
hw/cxl/cxl-host.c +1 -0
index 2aa776c79c74..c5f5fcfd64d0 100644
@@ -26,6 +26,7 @@ static void cxl_fixed_memory_window_config(CXLState *cxl_state,
                                            CXLFixedMemoryWindowOptions *object,
                                            Error **errp)
 {
+    ERRP_GUARD();
     g_autofree CXLFixedWindow *fw = g_malloc0(sizeof(*fw));
     strList *target;
     int i;
Signature · 1 line
2.34.1
Acked-by Jonathan Cameron on v2 1/7
#2 lore raw

Re: [PATCH v2 1/7] hw/cxl/cxl-host: Fix missing ERRP_GUARD() in cxl_fixed_memory_window_config()

Quoted 43 lines · On Fri, 23 Feb 2024 16:56:47 +0800 Zhao Liu <[email protected]> wrote:

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in cxl_fixed_memory_window_config(), @errp is dereferenced in 2 places without ERRP_GUARD():

fw->enc_int_ways = cxl_interleave_ways_enc(fw->num_targets, errp);

if (*errp) {
    return;
}

and

fw->enc_int_gran =

    cxl_interleave_granularity_enc(object->interleave_granularity,
                                   errp);
if (*errp) {
    return;
}

For the above 2 places, we check "*errp", because neither function returns a suitable error code. And since machine_set_cfmw() - the caller of cxl_fixed_memory_window_config() - doesn't get the NULL @errp parameter as the "set" method of object property, cxl_fixed_memory_window_config() hasn't triggered the bug that dereferencing the NULL @errp.

Quoted 6 lines

To follow the requirement of @errp, add missing ERRP_GUARD() in cxl_fixed_memory_window_config().

Quoted 23 lines

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/cxl/cxl-host.c | 1 +
 1 file changed, 1 insertion(+)
hw/cxl/cxl-host.c +1 -0
index 2aa776c79c74..c5f5fcfd64d0 100644
@@ -26,6 +26,7 @@ static void cxl_fixed_memory_window_config(CXLState *cxl_state,
                                            CXLFixedMemoryWindowOptions *object,
                                            Error **errp)
 {
+    ERRP_GUARD();
     g_autofree CXLFixedWindow *fw = g_malloc0(sizeof(*fw));
     strList *target;
     int i;
#3 lore raw

Re: [PATCH v2 1/7] hw/cxl/cxl-host: Fix missing ERRP_GUARD() in cxl_fixed_memory_window_config()

In reply to Zhao Liu
Quoted 27 lines · Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in cxl_fixed_memory_window_config(), @errp is dereferenced in 2 places without ERRP_GUARD():

fw->enc_int_ways = cxl_interleave_ways_enc(fw->num_targets, errp);

if (*errp) {
    return;
}
Quoted 8 lines

and

fw->enc_int_gran =

    cxl_interleave_granularity_enc(object->interleave_granularity,
                                   errp);
if (*errp) {
    return;
}

No need to quote the dereferences in full.

But in cxl_fixed_memory_window_config(), @errp is dereferenced in 2 places without ERRP_GUARD().

In these two places, we check "*errp", because neither function

would do.

Same for the other commit messages.

Hardly worth a respin, though :)

Quoted 36 lines

For the above 2 places, we check "*errp", because neither function returns a suitable error code. And since machine_set_cfmw() - the caller of cxl_fixed_memory_window_config() - doesn't get the NULL @errp parameter as the "set" method of object property, cxl_fixed_memory_window_config() hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in cxl_fixed_memory_window_config().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/cxl/cxl-host.c | 1 +
 1 file changed, 1 insertion(+)
hw/cxl/cxl-host.c +1 -0
index 2aa776c79c74..c5f5fcfd64d0 100644
@@ -26,6 +26,7 @@ static void cxl_fixed_memory_window_config(CXLState *cxl_state,
                                            CXLFixedMemoryWindowOptions *object,
                                            Error **errp)
 {
+    ERRP_GUARD();
     g_autofree CXLFixedWindow *fw = g_malloc0(sizeof(*fw));
     strList *target;
     int i;
author
#4 lore raw

Re: [PATCH v2 1/7] hw/cxl/cxl-host: Fix missing ERRP_GUARD() in cxl_fixed_memory_window_config()

Quoted 5 lines

No need to quote the dereferences in full.

But in cxl_fixed_memory_window_config(), @errp is dereferenced in 2 places without ERRP_GUARD().

Quoted 8 lines

In these two places, we check "*errp", because neither function

would do.

Same for the other commit messages.

Hardly worth a respin, though :)

I over-understood your reply. I'll pay attention to that in the follow up.

Thanks,
Zhao

author
#5 lore raw

PATCH v2 2/7 hw/display/macfb: Fix missing ERRP_GUARD() in macfb_nubus_realize()

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in macfb_nubus_realize(), @errp is dereferenced without ERRP_GUARD():

ndc->parent_realize(dev, errp);

if (*errp) {
    return;
}

Here we check *errp, because the ndc->parent_realize(), as a DeviceClass.realize() callback, returns void. And since macfb_nubus_realize(), also as a DeviceClass.realize(), doesn't get the NULL @errp parameter, it hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in macfb_nubus_realize().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/display/macfb.c | 1 +
 1 file changed, 1 insertion(+)
hw/display/macfb.c +1 -0
index 418e99c8e18e..1ace341a0ff4 100644
@@ -714,6 +714,7 @@ static void macfb_nubus_set_irq(void *opaque, int n, int level)
 
 static void macfb_nubus_realize(DeviceState *dev, Error **errp)
 {
+    ERRP_GUARD();
     NubusDevice *nd = NUBUS_DEVICE(dev);
     MacfbNubusState *s = NUBUS_MACFB(dev);
     MacfbNubusDeviceClass *ndc = NUBUS_MACFB_GET_CLASS(dev);
Signature · 1 line
2.34.1
author
#6 lore raw

PATCH v2 3/7 hw/mem/cxl_type3: Fix missing ERRP_GUARD() in ct3_realize()

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in ct3_realize(), @errp is dereferenced without ERRP_GUARD():

cxl_doe_cdat_init(cxl_cstate, errp);

if (*errp) {
    goto err_free_special_ops;
}

Here we check *errp, because cxl_doe_cdat_init() returns void. And ct3_realize() - as a PCIDeviceClass.realize() method - doesn't get the NULL @errp parameter, it hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in ct3_realize().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/mem/cxl_type3.c | 1 +
 1 file changed, 1 insertion(+)
hw/mem/cxl_type3.c +1 -0
index e8801805b90f..a3b0761f843b 100644
@@ -645,6 +645,7 @@ static DOEProtocol doe_cdat_prot[] = {
 
 static void ct3_realize(PCIDevice *pci_dev, Error **errp)
 {
+    ERRP_GUARD();
     CXLType3Dev *ct3d = CXL_TYPE3(pci_dev);
     CXLComponentState *cxl_cstate = &ct3d->cxl_cstate;
     ComponentRegisters *regs = &cxl_cstate->crb;
Signature · 1 line
2.34.1
Acked-by Jonathan Cameron on v2 3/7
#7 lore raw

Re: [PATCH v2 3/7] hw/mem/cxl_type3: Fix missing ERRP_GUARD() in ct3_realize()

Quoted 31 lines · On Fri, 23 Feb 2024 16:56:49 +0800 Zhao Liu <[email protected]> wrote:

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in ct3_realize(), @errp is dereferenced without ERRP_GUARD():

cxl_doe_cdat_init(cxl_cstate, errp);

if (*errp) {
    goto err_free_special_ops;
}

Here we check *errp, because cxl_doe_cdat_init() returns void. And ct3_realize() - as a PCIDeviceClass.realize() method - doesn't get the NULL @errp parameter, it hasn't triggered the bug that dereferencing the NULL @errp.

Quoted 6 lines

To follow the requirement of @errp, add missing ERRP_GUARD() in ct3_realize().

Quoted 23 lines

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/mem/cxl_type3.c | 1 +
 1 file changed, 1 insertion(+)
hw/mem/cxl_type3.c +1 -0
index e8801805b90f..a3b0761f843b 100644
@@ -645,6 +645,7 @@ static DOEProtocol doe_cdat_prot[] = {
 
 static void ct3_realize(PCIDevice *pci_dev, Error **errp)
 {
+    ERRP_GUARD();
     CXLType3Dev *ct3d = CXL_TYPE3(pci_dev);
     CXLComponentState *cxl_cstate = &ct3d->cxl_cstate;
     ComponentRegisters *regs = &cxl_cstate->crb;
author
#8 lore raw

PATCH v2 4/7 hw/misc/xlnx-versal-trng: Check returned bool in trng_prop_fault_event_set()

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in trng_prop_fault_event_set, @errp is dereferenced without ERRP_GUARD():

visit_type_uint32(v, name, events, errp);

if (*errp) {
    return;
}

Currently, since trng_prop_fault_event_set() doesn't get the NULL @errp parameter as a "set" method of object property, it hasn't triggered the bug that dereferencing the NULL @errp.

And since visit_type_uint32() returns bool, check the returned bool directly instead of dereferencing @errp, then we needn't the add missing ERRP_GUARD().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)
* Check the returned bool instead of dereferencing @errp. (Markus)

---
 hw/misc/xlnx-versal-trng.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/misc/xlnx-versal-trng.c +1 -2
index b8111b8b6626..6495188dc748 100644
@@ -644,8 +644,7 @@ static void trng_prop_fault_event_set(Object *obj, Visitor *v,
     Property *prop = opaque;
     uint32_t *events = object_field_prop_ptr(obj, prop);
 
-    visit_type_uint32(v, name, events, errp);
-    if (*errp) {
+    if (!visit_type_uint32(v, name, events, errp)) {
         return;
     }
 
Signature · 1 line
2.34.1
Reviewed-by on v2 4/7
#9 lore raw

Re: [PATCH v2 4/7] hw/misc/xlnx-versal-trng: Check returned bool in trng_prop_fault_event_set()

Quoted 45 lines · Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in trng_prop_fault_event_set, @errp is dereferenced without ERRP_GUARD():

visit_type_uint32(v, name, events, errp);

if (*errp) {
     return;
}

Currently, since trng_prop_fault_event_set() doesn't get the NULL @errp parameter as a "set" method of object property, it hasn't triggered the bug that dereferencing the NULL @errp.

And since visit_type_uint32() returns bool, check the returned bool directly instead of dereferencing @errp, then we needn't the add missing ERRP_GUARD().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)
* Check the returned bool instead of dereferencing @errp. (Markus)

Quoted 3 lines
---
  hw/misc/xlnx-versal-trng.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
author
#10 lore raw

PATCH v2 5/7 hw/pci-bridge/cxl_upstream: Fix missing ERRP_GUARD() in cxl_usp_realize()

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in cxl_usp_realize(), @errp is dereferenced without ERRP_GUARD():

cxl_doe_cdat_init(cxl_cstate, errp);

if (*errp) {
    goto err_cap;
}

Here we check *errp, because cxl_doe_cdat_init() returns void. And since cxl_usp_realize() - as a PCIDeviceClass.realize() method - doesn't get the NULL @errp parameter, it hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in cxl_usp_realize().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/pci-bridge/cxl_upstream.c | 1 +
 1 file changed, 1 insertion(+)
hw/pci-bridge/cxl_upstream.c +1 -0
index e87eb4017713..03d123cca0ef 100644
@@ -289,6 +289,7 @@ static void free_default_cdat_table(CDATSubHeader **cdat_table, int num,
 
 static void cxl_usp_realize(PCIDevice *d, Error **errp)
 {
+    ERRP_GUARD();
     PCIEPort *p = PCIE_PORT(d);
     CXLUpstreamPort *usp = CXL_USP(d);
     CXLComponentState *cxl_cstate = &usp->cxl_cstate;
Signature · 1 line
2.34.1
Acked-by Jonathan Cameron on v2 5/7
#11 lore raw

Re: [PATCH v2 5/7] hw/pci-bridge/cxl_upstream: Fix missing ERRP_GUARD() in cxl_usp_realize()

Quoted 31 lines · On Fri, 23 Feb 2024 16:56:51 +0800 Zhao Liu <[email protected]> wrote:

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in cxl_usp_realize(), @errp is dereferenced without ERRP_GUARD():

cxl_doe_cdat_init(cxl_cstate, errp);

if (*errp) {
    goto err_cap;
}

Here we check *errp, because cxl_doe_cdat_init() returns void. And since cxl_usp_realize() - as a PCIDeviceClass.realize() method - doesn't get the NULL @errp parameter, it hasn't triggered the bug that dereferencing the NULL @errp.

Quoted 6 lines

To follow the requirement of @errp, add missing ERRP_GUARD() in cxl_usp_realize().

Quoted 23 lines

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/pci-bridge/cxl_upstream.c | 1 +
 1 file changed, 1 insertion(+)
hw/pci-bridge/cxl_upstream.c +1 -0
index e87eb4017713..03d123cca0ef 100644
@@ -289,6 +289,7 @@ static void free_default_cdat_table(CDATSubHeader **cdat_table, int num,
 
 static void cxl_usp_realize(PCIDevice *d, Error **errp)
 {
+    ERRP_GUARD();
     PCIEPort *p = PCIE_PORT(d);
     CXLUpstreamPort *usp = CXL_USP(d);
     CXLComponentState *cxl_cstate = &usp->cxl_cstate;
Reviewed-by on v2 5/7
#12 lore raw

Re: [PATCH v2 5/7] hw/pci-bridge/cxl_upstream: Fix missing ERRP_GUARD() in cxl_usp_realize()

In reply to Zhao Liu
Quoted 53 lines · Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in cxl_usp_realize(), @errp is dereferenced without ERRP_GUARD():

cxl_doe_cdat_init(cxl_cstate, errp);

if (*errp) {
     goto err_cap;
}

Here we check *errp, because cxl_doe_cdat_init() returns void. And since cxl_usp_realize() - as a PCIDeviceClass.realize() method - doesn't get the NULL @errp parameter, it hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in cxl_usp_realize().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
  hw/pci-bridge/cxl_upstream.c | 1 +
  1 file changed, 1 insertion(+)
hw/pci-bridge/cxl_upstream.c +0 -0
index e87eb4017713..03d123cca0ef 100644

hw/pci-bridge/cxl_upstream.c static void free_default_cdat_table(CDATSubHeader **cdat_table, int num, · line 295

Quoted 7 lines
hw/pci-bridge/cxl_upstream.c +1 -0
@@ -289,6 +289,7 @@ static void free_default_cdat_table(CDATSubHeader **cdat_table, int num,
 
  static void cxl_usp_realize(PCIDevice *d, Error **errp)
  {
+    ERRP_GUARD();
      PCIEPort *p = PCIE_PORT(d);
      CXLUpstreamPort *usp = CXL_USP(d);
      CXLComponentState *cxl_cstate = &usp->cxl_cstate;
author
#13 lore raw

PATCH v2 6/7 hw/vfio/iommufd: Fix missing ERRP_GUARD() in iommufd_cdev_getfd()

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

As the comment in qapi/error, dereferencing @errp requires ERRP_GUARD():

* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.

But in iommufd_cdev_getfd(), @errp is dereferenced without ERRP_GUARD():

if (*errp) {
    error_prepend(errp, VFIO_MSG_PREFIX, path);
}

Currently, since vfio_attach_device() - the caller of iommufd_cdev_getfd() - is always called in DeviceClass.realize() context and doesn't get the NULL @errp parameter, iommufd_cdev_getfd() hasn't triggered the bug that dereferencing the NULL @errp.

To follow the requirement of @errp, add missing ERRP_GUARD() in iommufd_cdev_getfd().

---
Suggested by credit:
Markus: Referred his explanation about ERRP_GUARD().
---
v2:
* Add the @errp dereference code in commit message to make review easier. (Markus)

---
 hw/vfio/iommufd.c | 1 +
 1 file changed, 1 insertion(+)
hw/vfio/iommufd.c +1 -0
index 9bfddc136089..7baf49e6ee9e 100644
@@ -116,6 +116,7 @@ static void iommufd_cdev_unbind_and_disconnect(VFIODevice *vbasedev)
 
 static int iommufd_cdev_getfd(const char *sysfs_path, Error **errp)
 {
+    ERRP_GUARD();
     long int ret = -ENOTTY;
     char *path, *vfio_dev_path = NULL, *vfio_path = NULL;
     DIR *dir = NULL;
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH v2 7/7 hw/intc: Check @errp to handle the error of IOAPICCommonClass.realize()

In reply to Zhao Liu

From: Zhao Liu <[email protected]>

IOAPICCommonClass implements its own private realize(), and this private realize() allows error.

Since IOAPICCommonClass.realize() returns void, to check the error, dereference @errp with ERRP_GUARD().

---
v2:
* Add the missing ERRP_GUARD(). (Markus)
* Move this single patch into @errp fixing series. (Micheal)

---
 hw/intc/ioapic_common.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/intc/ioapic_common.c +4 -0
index cb9bf6214608..efbe6958c8d7 100644
@@ -152,6 +152,7 @@ static int ioapic_dispatch_post_load(void *opaque, int version_id)
 
 static void ioapic_common_realize(DeviceState *dev, Error **errp)
 {
+    ERRP_GUARD();
     IOAPICCommonState *s = IOAPIC_COMMON(dev);
     IOAPICCommonClass *info;
 
@@ -162,6 +163,9 @@ static void ioapic_common_realize(DeviceState *dev, Error **errp)
 
     info = IOAPIC_COMMON_GET_CLASS(s);
     info->realize(dev, errp);
+    if (*errp) {
+        return;
+    }
 
     sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->io_memory);
     ioapic_no++;
Signature · 1 line
2.34.1
Reviewed-by on v2 7/7
#15 lore raw

Re: [PATCH v2 7/7] hw/intc: Check @errp to handle the error of IOAPICCommonClass.realize()

Quoted 8 lines · Zhao Liu

From: Zhao Liu <[email protected]>

IOAPICCommonClass implements its own private realize(), and this private realize() allows error.

Since IOAPICCommonClass.realize() returns void, to check the error, dereference @errp with ERRP_GUARD().

Quoted 8 lines

---
v2:
* Add the missing ERRP_GUARD(). (Markus)
* Move this single patch into @errp fixing series. (Micheal)

---
  hw/intc/ioapic_common.c | 4 ++++
  1 file changed, 4 insertions(+)
author
#16 lore raw
In reply to Zhao Liu

Hi Michael & Philippe,

Could this series catch the last train of next releases? ;-)

Thanks,
Zhao

Quoted 72 lines · Zhao Liu

Date: Fri, 23 Feb 2024 16:56:46 +0800
From: Zhao Liu <[email protected]>
Subject: [PATCH v2 0/7] Cleanup and fix @errp dereference
X-Mailer: git-send-email 2.34.1

From: Zhao Liu <[email protected]>

Hi all,

This is my v2 series to clean and fix @errp dereference.

Introduction
============

The patches 1-6 fix the cases that deference @errp without ERRP_GUARD(), and they are based on my previsous v1 [1].

The patch 7 is merged in this series from another single patch [2].

Based on v1, v2 mainly add more description about problematic codes to make review easier (with Markus' suggestion) and avoid dereferencing @errp in patch 3's special case.

Future Work
===========

Additionally, I realized that my journey to fix @errp with ERRP_GUARD() is still not over, because @errp's second restriction said:

* Without ERRP_GUARD(), use of the @errp parameter is restricted:
...
* - It should not be passed to error_prepend() or
* error_append_hint(), because that doesn't work with &error_fatal.
* ERRP_GUARD() lifts these restrictions.

With this restrication, there're still many places need to add missing ERRP_GUARD().

[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/

Thanks and Best Regards,
Zhao

---
Zhao Liu (7):
hw/cxl/cxl-host: Fix missing ERRP_GUARD() in
cxl_fixed_memory_window_config()
hw/display/macfb: Fix missing ERRP_GUARD() in macfb_nubus_realize() hw/mem/cxl_type3: Fix missing ERRP_GUARD() in ct3_realize() hw/misc/xlnx-versal-trng: Check returned bool in
trng_prop_fault_event_set()
hw/pci-bridge/cxl_upstream: Fix missing ERRP_GUARD() in
cxl_usp_realize()
hw/vfio/iommufd: Fix missing ERRP_GUARD() in iommufd_cdev_getfd() hw/intc: Check @errp to handle the error of
IOAPICCommonClass.realize()

 hw/cxl/cxl-host.c            | 1 +
 hw/display/macfb.c           | 1 +
 hw/intc/ioapic_common.c      | 4 ++++
 hw/mem/cxl_type3.c           | 1 +
 hw/misc/xlnx-versal-trng.c   | 3 +--
 hw/pci-bridge/cxl_upstream.c | 1 +
 hw/vfio/iommufd.c            | 1 +
 7 files changed, 10 insertions(+), 2 deletions(-)
Signature · 1 line
2.34.1
#17 lore raw
Quoted 6 lines · Zhao Liu

Hi Michael & Philippe,

Could this series catch the last train of next releases? ;-)

Thanks,
Zhao

yes it will be in subject to last minute testing.