Skip to contentSkip to conversation

virtio-gpu-virgl: correct parent for blob memory region

PATCH In review Joelle van Dyne 2 participants 2 messages Jan 3 – Jan 4

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 virtio-gpu-virgl: correct parent for blob memory region

When owner == mr, object_unparent will crash:

object_unparent(mr) ->
object_property_del_child(mr, mr) ->
object_finalize_child_property(mr, name, mr) ->
object_unref(mr) ->
object_finalize(mr) ->
object_property_del_all(mr) ->
object_finalize_child_property(mr, name, mr) ->
object_unref(mr) ->
fail on g_assert(obj->ref > 0)

However, passing a different owner to memory_region_init does not work. memory_region_ref has an optimization where it takes a ref only on the owner. That means when flatviews are created, it does not take a ref on the region and you can get a UAF from flatview_destroy called from RCU.

The correct fix therefore is to use NULL as the name which will set the owner but not the parent (which is still NULL). This allows us to use memory_region_ref on itself while not having to rely on unparent for cleanup.

---
 hw/display/virtio-gpu-virgl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/virtio-gpu-virgl.c +2 -2
index 18404be5892..b8a0af702e0 100644
@@ -123,7 +123,7 @@ virtio_gpu_virgl_map_resource_blob(VirtIOGPU *g,
     vmr->g = g;
 
     mr = &vmr->mr;
-    memory_region_init_ram_ptr(mr, OBJECT(mr), "blob", size, data);
+    memory_region_init_ram_ptr(mr, OBJECT(mr), NULL, size, data);
     memory_region_add_subregion(&b->hostmem, offset, mr);
     memory_region_set_enabled(mr, true);
 
@@ -189,7 +189,7 @@ virtio_gpu_virgl_unmap_resource_blob(VirtIOGPU *g,
         /* memory region owns self res->mr object and frees it by itself */
         memory_region_set_enabled(mr, false);
         memory_region_del_subregion(&b->hostmem, mr);
-        object_unparent(OBJECT(mr));
+        object_unref(OBJECT(mr));
     }
 
     return 0;
Signature · 1 line
2.50.1 (Apple Git-155)
Reviewed-by on v3 1/1
#1 lore raw
Quoted 18 lines · Joelle van Dyne

When owner == mr, object_unparent will crash:

object_unparent(mr) ->
object_property_del_child(mr, mr) ->
object_finalize_child_property(mr, name, mr) ->
object_unref(mr) ->
object_finalize(mr) ->
object_property_del_all(mr) ->
object_finalize_child_property(mr, name, mr) ->
object_unref(mr) ->
fail on g_assert(obj->ref > 0)

However, passing a different owner to memory_region_init does not work. memory_region_ref has an optimization where it takes a ref only on the owner. That means when flatviews are created, it does not take a ref on the region and you can get a UAF from flatview_destroy called from RCU.

Quoted 6 lines

The correct fix therefore is to use NULL as the name which will set the owner but not the parent (which is still NULL). This allows us to use memory_region_ref on itself while not having to rely on unparent for cleanup.