Skip to contentSkip to conversation

spapr: fix write-past-end-of-array error in cpu core device init code

PATCH In review Greg Kurz 3 participants 5 messages Jun 27, 2016 – Jun 28, 2016

lore
Share & tools
lore

Greg Kurz

author
#0 lore raw

PATCH 1/1 spapr: fix write-past-end-of-array error in cpu core device init code

This fixes a potential QEMU crash introduced by commit 3b542549661.

---
 hw/ppc/spapr_cpu_core.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
hw/ppc/spapr_cpu_core.c +1 -2
index 3a5da09b9902..8b802a6fcf0b 100644
@@ -309,10 +309,9 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     }
 
 err:
-    while (i >= 0) {
+    while (--i >= 0) {
         obj = sc->threads + i * size;
         object_unparent(obj);
-        i--;
     }
     g_free(sc->threads);
     error_propagate(errp, local_err);
#1 lore raw
Quoted 6 lines · Greg Kurz

This fixes a potential QEMU crash introduced by commit 3b542549661.

---
 hw/ppc/spapr_cpu_core.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Ugh. The existing code is wrong in the case where the failure happens after the loop.

But this version is wrong in the case it happens during the loop - it will fail to clean up the last object created.

Quoted 17 lines
hw/ppc/spapr_cpu_core.c +1 -2
index 3a5da09b9902..8b802a6fcf0b 100644
@@ -309,10 +309,9 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     }
 
 err:
-    while (i >= 0) {
+    while (--i >= 0) {
         obj = sc->threads + i * size;
         object_unparent(obj);
-        i--;
     }
     g_free(sc->threads);
     error_propagate(errp, local_err);
Signature · 4 lines
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
author
#2 lore raw
Quoted 8 lines · On Tue, 28 Jun 2016 12:55:07 +1000 David Gibson <[email protected]>…
Quoted 6 lines · Greg Kurz

This fixes a potential QEMU crash introduced by commit 3b542549661.

---
 hw/ppc/spapr_cpu_core.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)  
Quoted 6 lines

Ugh. The existing code is wrong in the case where the failure happens after the loop.

But this version is wrong in the case it happens during the loop - it will fail to clean up the last object created.

Hmm... unless I'm missing something, if object_property_add_child() fails to add object i, we don't want to unparent it, and we should start rollback at index i-1.

Another weirdness is that I see no rollback for the object_child_foreach()
loop: in case of failure, we will unparent realized objects... is it okay ?

Quoted 18 lines
Quoted 17 lines
hw/ppc/spapr_cpu_core.c +1 -2
index 3a5da09b9902..8b802a6fcf0b 100644
@@ -309,10 +309,9 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     }
 
 err:
-    while (i >= 0) {
+    while (--i >= 0) {
         obj = sc->threads + i * size;
         object_unparent(obj);
-        i--;
     }
     g_free(sc->threads);
     error_propagate(errp, local_err);
#3 lore raw
Quoted 14 lines · Greg Kurz
Quoted 11 lines · On Tue, 28 Jun 2016 12:55:07 +1000 David Gibson <[email protected]>…
Quoted 6 lines · Greg Kurz

This fixes a potential QEMU crash introduced by commit 3b542549661.

---
 hw/ppc/spapr_cpu_core.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)  

Ugh. The existing code is wrong in the case where the failure happens after the loop.

Quoted 7 lines
Quoted 3 lines

But this version is wrong in the case it happens during the loop - it will fail to clean up the last object created.

Hmm... unless I'm missing something, if object_property_add_child() fails to add object i, we don't want to unparent it, and we should start rollback at index i-1.

Good point, my mistake. I'll apply this fix.

Quoted 4 lines

Another weirdness is that I see no rollback for the object_child_foreach()
loop: in case of failure, we will unparent realized objects... is it okay ?

Um.. I have no idea. Bharata? Alex?

Quoted 20 lines
Quoted 19 lines
Quoted 18 lines
hw/ppc/spapr_cpu_core.c +1 -2
index 3a5da09b9902..8b802a6fcf0b 100644
@@ -309,10 +309,9 @@ static void spapr_cpu_core_realize(DeviceState *dev, Error **errp)
     }
 
 err:
-    while (i >= 0) {
+    while (--i >= 0) {
         obj = sc->threads + i * size;
         object_unparent(obj);
-        i--;
     }
     g_free(sc->threads);
     error_propagate(errp, local_err);
Signature · 4 lines
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
#4 lore raw
Quoted 23 lines · David Gibson
Quoted 14 lines · Greg Kurz
Quoted 11 lines · On Tue, 28 Jun 2016 12:55:07 +1000 David Gibson <[email protected]>…
Quoted 6 lines · Greg Kurz

This fixes a potential QEMU crash introduced by commit 3b542549661.

---
 hw/ppc/spapr_cpu_core.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)  

Ugh. The existing code is wrong in the case where the failure happens after the loop.

Quoted 7 lines
Quoted 3 lines

But this version is wrong in the case it happens during the loop - it will fail to clean up the last object created.

Hmm... unless I'm missing something, if object_property_add_child() fails to add object i, we don't want to unparent it, and we should start rollback at index i-1.

Quoted 8 lines

Good point, my mistake. I'll apply this fix.

Quoted 4 lines

Another weirdness is that I see no rollback for the object_child_foreach()
loop: in case of failure, we will unparent realized objects... is it okay ?

Um.. I have no idea. Bharata? Alex?

This is similar to how device_add code recovers when there is failure during realize. So I think object_unparent() should be fine. Only other thing I need to verify is whether an additional object_unref() is needed after unparenting.

Regards,
Bharata.