virtio-gpu-virgl: correct parent for blob memory region
PATCH v2 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 is not enough. memory_region_ref has an optimization where it takes a ref only on the owner. It specifically warns against calling unparent on the memory region. So we initialize the memory region first and then patch in the owner with itself.
--- hw/display/virtio-gpu-virgl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/display/virtio-gpu-virgl.c +2 -1
@@ -123,7 +123,8 @@ 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 (g),"blob" , size, data); + mr->owner =OBJECT (mr);memory_region_add_subregion (&b->hostmem, offset, mr);memory_region_set_enabled (mr, true);
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 12 lines · Joelle van Dyne
When
owner==mr,object_unparentwill 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 ong_assert(obj->ref > 0)
Quoted 5 lines
However, passing a different
ownertomemory_region_initis not enough.memory_region_refhas an optimization where it takes a ref only on the owner. It specifically warns against calling unparent on the memory region. So we initialize the memory region first and then patch in the owner with itself.
Patching outside system/memory.c can be fragile.
I think an object is being a child of itself, which doesn't make sense. This can be avoided by passing NULL as name. The object will be an orphan so it will have to be freed with object_unref() instead of object_unparent().
Regards,
Akihiko Odaki
Quoted 20 lines · Akihiko Odaki
Quoted 12 lines · Joelle van Dyne
When
owner==mr,object_unparentwill 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 ong_assert(obj->ref > 0)Quoted 5 lines
However, passing a different
ownertomemory_region_initis not enough.memory_region_refhas an optimization where it takes a ref only on the owner. It specifically warns against calling unparent on the memory region. So we initialize the memory region first and then patch in the owner with itself.
Quoted 6 lines
Patching outside
system/memory.ccan be fragile.I think an object is being a child of itself, which doesn't make sense. This can be avoided by passing NULL as name. The object will be an orphan so it will have to be freed with
object_unref()instead ofobject_unparent().
I didn't want to break anything unintentionally and wanted to be safe by making the change as close to the original logic as possible (having introduced a UAF in v1 after making a one line change). Maybe Antonio or Robert can give more insight on the intention of using self as owner and if making it an orphan is acceptable?
Quoted 3 lines
Regards,
Akihiko Odaki
Quoted 28 lines · Joelle van Dyne
Quoted 20 lines · Akihiko Odaki
Quoted 12 lines · Joelle van Dyne
When
owner==mr,object_unparentwill 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 ong_assert(obj->ref > 0)Quoted 5 lines
However, passing a different
ownertomemory_region_initis not enough.memory_region_refhas an optimization where it takes a ref only on the owner. It specifically warns against calling unparent on the memory region. So we initialize the memory region first and then patch in the owner with itself.Quoted 6 lines
Patching outside
system/memory.ccan be fragile.I think an object is being a child of itself, which doesn't make sense. This can be avoided by passing NULL as name. The object will be an orphan so it will have to be freed with
object_unref()instead ofobject_unparent().
Quoted 5 lines
I didn't want to break anything unintentionally and wanted to be safe by making the change as close to the original logic as possible (having introduced a UAF in v1 after making a one line change). Maybe Antonio or Robert can give more insight on the intention of using self as owner and if making it an orphan is acceptable?
The intention here is that the reference counting of memory regions independent of the device. If you make the device owner, the reference counter of the device will be used for memory region (see memory_region_ref()). But this memory region can go away while the device stays, so its reference counting needs to be done independently.
By making the memory region itself owner, the reference counter of the memory region itself will be used, just like normal objects.
So the owner needs to be the memory region itself. But it is not necessary to have the parent/child relationship, and passing NULL as name only removes the relationship.
Regards,
Akihiko Odaki
Quoted 12 lines · Joelle van Dyne
When
owner==mr,object_unparentwill 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 ong_assert(obj->ref > 0)
Quoted 7 lines
However, passing a different
ownertomemory_region_initis not enough.memory_region_refhas an optimization where it takes a ref only on the owner. It specifically warns against calling unparent on the memory region. So we initialize the memory region first and then patch in the owner with itself.Signed-off-by Joelle van Dyne <[email protected]>
This one is quite a bit confusing.
Is it a qemu-stable material?
I'm *not* picking it up automatically.
Thanks,
/mjt
Quoted 18 lines
--- hw/display/virtio-gpu-virgl.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/display/virtio-gpu-virgl.c +2
-1@@ -123,7 +123,8 @@ 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 (g),"blob" , size, data); + mr->owner =OBJECT (mr);memory_region_add_subregion (&b->hostmem, offset, mr);memory_region_set_enabled (mr, true);
Quoted 19 lines · Michael Tokarev
Quoted 18 lines · Joelle van Dyne
When
owner==mr,object_unparentwill 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 ong_assert(obj->ref > 0)However, passing a different
ownertomemory_region_initis not enough.memory_region_refhas an optimization where it takes a ref only on the owner. It specifically warns against calling unparent on the memory region. So we initialize the memory region first and then patch in the owner with itself.
Quoted 6 lines
Quoted 1 line
Signed-off-by Joelle van Dyne <[email protected]>This one is quite a bit confusing.
Is it a qemu-stable material?
I'm *not* picking it up automatically.
Yes, I think so. It should have had Cc.
Regards,
Akihiko Odaki