virtio-dmabuf: Ensure UUID persistence for hash table insertion
PATCH 1/1 virtio-dmabuf: Ensure UUID persistence for hash table insertion
In virtio_add_resource function, the UUID used as a key for g_hash_table_insert was temporary, which could lead to invalid lookups when accessed later. This patch ensures that the UUID remains valid by duplicating it into a newly allocated memory space (persistent_uuid). The value is then inserted into the hash table with this persistent UUID key to ensure that the key stored in the hash table remains valid as long as the hash table entry exists.
--- hw/display/virtio-dmabuf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
hw/display/virtio-dmabuf.c +6 -1
@@ -39,7 +39,12 @@ static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) g_free); }if (g_hash_table_lookup (resource_uuids, uuid) ==NULL ) { -g_hash_table_insert (resource_uuids, uuid, value); +QemuUUID *persistent_uuid =g_memdup2 (uuid,sizeof (QemuUUID)); +if (persistent_uuid ==NULL ) { + result = false; + }else { +g_hash_table_insert (resource_uuids, persistent_uuid, value); + } }else { result = false; }
Signature · 1 line
2.47.0
Quoted 20 lines · Dorinda Bassey
In
virtio_add_resourcefunction, the UUID used as a key forg_hash_table_insertwas temporary, which could lead to invalid lookups when accessed later. This patch ensures that the UUID remains valid by duplicating it into a newly allocated memory space (persistent_uuid). The value is then inserted into the hash table with this persistent UUID key to ensure that the key stored in the hash table remains valid as long as the hash table entry exists.Signed-off-by Dorinda Bassey <[email protected]>--- hw/display/virtio-dmabuf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)hw/display/virtio-dmabuf.c +0
-0
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 47
Quoted 10 lines
hw/display/virtio-dmabuf.c +6
-1@@ -39,7 +39,12 @@ static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) g_free); }if (g_hash_table_lookup (resource_uuids, uuid) ==NULL ) { -g_hash_table_insert (resource_uuids, uuid, value); +QemuUUID *persistent_uuid =g_memdup2 (uuid,sizeof (QemuUUID)); +if (persistent_uuid ==NULL ) { + result = false; + }else { +g_hash_table_insert (resource_uuids, persistent_uuid, value); + }
The description of virtio_add_* functions in the header state:
"""
* Return: true if the UUID did not exist and the resource has been added,
* false if another resource with the same UUID already existed. """
I think it may be worth clarifying that it can also return false if the resource failed
to be added.
Quoted 6 lines
hw/display/virtio-dmabuf.c +0
-1}else { result = false; } --2.47.0
Quoted 8 lines · Dorinda Bassey
In
virtio_add_resourcefunction, the UUID used as a key forg_hash_table_insertwas temporary, which could lead to invalid lookups when accessed later. This patch ensures that the UUID remains valid by duplicating it into a newly allocated memory space (persistent_uuid). The value is then inserted into the hash table with this persistent UUID key to ensure that the key stored in the hash table remains valid as long as the hash table entry exists.
It's a fix right, so maybe better to add a Fixes tag:
Quoted 11 lines
Signed-off-by Dorinda Bassey <[email protected]>---
hw/display/virtio-dmabuf.c| 7 ++++++-1 file changed, 6 insertions(+), 1 deletion(-)hw/display/virtio-dmabuf.c +0
-0
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 42
Quoted 5 lines
hw/display/virtio-dmabuf.c +1
-1@@ -39,7 +39,12 @@ static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) g_free); }if (g_hash_table_lookup (resource_uuids, uuid) ==NULL ) { -g_hash_table_insert (resource_uuids, uuid, value); +QemuUUID *persistent_uuid =g_memdup2 (uuid,sizeof (QemuUUID));
Since now we allocate memory for the key, we should provide the key_destroy_func when calling g_hash_table_new_full(), otherwise this new memory will not be de-allocated.
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 43
Quoted 1 line
hw/display/virtio-dmabuf.c +1
-0+if (persistent_uuid ==NULL ) {
IIUC it can be null, only if uuid was null since glib memory API usually terminates the application if memory allocation fails, see https://docs.gtk.org/glib/memory.html
So maybe we can just do:
g_hash_table_insert(resource_uuids, g_memdup2(uuid, sizeof(QemuUUID)),
value);
Quoted 10 lines
hw/display/virtio-dmabuf.c +4
-0+ result = false; + }else { +g_hash_table_insert (resource_uuids, persistent_uuid, value); + } }else { result = false; }Signature · 1 line
2.47.0
Quoted 38 lines · Stefano Garzarella
Quoted 8 lines · Dorinda Bassey
In
virtio_add_resourcefunction, the UUID used as a key forg_hash_table_insertwas temporary, which could lead to invalid lookups when accessed later. This patch ensures that the UUID remains valid by duplicating it into a newly allocated memory space (persistent_uuid). The value is then inserted into the hash table with this persistent UUID key to ensure that the key stored in the hash table remains valid as long as the hash table entry exists.It's a fix right, so maybe better to add a Fixes tag:
Fixes faefdba847 ("hw/display: introduce virtio-dmabuf")Quoted 11 lines
Signed-off-by Dorinda Bassey <[email protected]>---
hw/display/virtio-dmabuf.c| 7 ++++++-1 file changed, 6 insertions(+), 1 deletion(-)hw/display/virtio-dmabuf.c +0
-0hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 42
Quoted 5 lines
hw/display/virtio-dmabuf.c +1
-1@@ -39,7 +39,12 @@ static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) g_free); }if (g_hash_table_lookup (resource_uuids, uuid) ==NULL ) { -g_hash_table_insert (resource_uuids, uuid, value); +QemuUUID *persistent_uuid =g_memdup2 (uuid,sizeof (QemuUUID));Since now we allocate memory for the key, we should provide the
key_destroy_funcwhen callingg_hash_table_new_full(), otherwise this new memory will not be de-allocated.hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 43
Quoted 1 line
hw/display/virtio-dmabuf.c +1
-0+if (persistent_uuid ==NULL ) {
Quoted 7 lines
IIUC it can be null, only if
uuidwas null since glib memory API usually terminates the application if memory allocation fails, see https://docs.gtk.org/glib/memory.htmlSo maybe we can just do:
g_hash_table_insert(resource_uuids,g_memdup2(uuid, sizeof(QemuUUID)),
value);
Or even better:
g_hash_table_insert(resource_uuids, g_memdup2(uuid, sizeof(*uuid)),
value);
Thanks,
Stefano
Quoted 11 lines
Quoted 11 lines
hw/display/virtio-dmabuf.c +4
-1+ result = false; + }else { +g_hash_table_insert (resource_uuids, persistent_uuid, value); + } }else { result = false; } --2.47.0
Hi,
Since now we allocate memory for the key, we should provide the
Quoted 3 lines
key_destroy_funcwhen callingg_hash_table_new_full(), otherwise this new memory will not be de-allocated.
Ack
IIUC it can be null, only if uuid was null since glib memory
Quoted 2 lines
API usually terminates the application if memory allocation fails, see https://docs.gtk.org/glib/memory.html
Ok, but the documentation doesn't explicitly state what g_memdup2 returns on failure, I checked it manually and it terminates the application on failure. So indeed this check is not necessary.
I think it may be worth clarifying that it can also return false if
Quoted 2 lines
the resource failed
to be added.
Hence, I think this is not needed since g_memdup2 will terminate the application if memory allocation fails and no return value is provided then.
Thanks,
Dorinda.
Quoted 72 lines · Stefano Garzarella
Quoted 21 lines · Stefano Garzarella
Quoted 8 lines · Dorinda Bassey
In
virtio_add_resourcefunction, the UUID used as a key forg_hash_table_insertwas temporary, which could lead to invalid lookups when accessed later. This patch ensures that the UUID remains valid by duplicating it into a newly allocated memory space (persistent_uuid). The value is then inserted into the hash table with this persistent UUID key to ensure that the key stored in the hash table remains valid as long as the hash table entry exists.It's a fix right, so maybe better to add a Fixes tag:
Fixes faefdba847 ("hw/display: introduce virtio-dmabuf")Quoted 6 lines
Signed-off-by Dorinda Bassey <[email protected]>---
hw/display/virtio-dmabuf.c| 7 ++++++-1 file changed, 6 insertions(+), 1 deletion(-)Quoted 5 lines
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid,
Quoted 5 lines
hw/display/virtio-dmabuf.c +0
-0@@ -39,7 +39,12 @@ static bool virtio_add_resource(QemuUUID *uuid,VirtioSharedObject *value)
Quoted 10 lines
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 42
Quoted 5 lines
hw/display/virtio-dmabuf.c +1
-1g_free); }if (g_hash_table_lookup (resource_uuids, uuid) ==NULL ) { -g_hash_table_insert (resource_uuids, uuid, value); +QemuUUID *persistent_uuid =g_memdup2 (uuid,sizeof (QemuUUID));Since now we allocate memory for the key, we should provide the
key_destroy_funcwhen callingg_hash_table_new_full(), otherwise this new memory will not be de-allocated.Quoted 8 lines
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value) · line 43
Quoted 1 line
hw/display/virtio-dmabuf.c +1
-0+if (persistent_uuid ==NULL ) {IIUC it can be null, only if
uuidwas null since glib memory API usually terminates the application if memory allocation fails, see https://docs.gtk.org/glib/memory.htmlSo maybe we can just do:
g_hash_table_insert(resource_uuids, g_memdup2(uuid,sizeof(QemuUUID)),
Quoted 1 line
value);Or even better:
g_hash_table_insert(resource_uuids, g_memdup2(uuid,sizeof(*uuid)),value);Thanks,
StefanoQuoted 4 lines
hw/display/virtio-dmabuf.c static bool virtio_add_resource(QemuUUID *uuid, VirtioSharedObject *value)
Quoted 4 lines
hw/display/virtio-dmabuf.c +3
-0+ result = false; + }else { + g_hash_table_insert(resource_uuids, persistent_uuid,value);
Quoted 7 lines
Quoted 7 lines
hw/display/virtio-dmabuf.c +1
-1+ } }else { result = false; } --2.47.0