Skip to content

Commit 64e16fb

Browse files
committed
util: fix use-after-free in module_load_one
g_hash_table_add always retains ownership of the pointer passed in as the key. Its return status merely indicates whether the added entry was new, or replaced an existing entry. Thus key must never be freed after this method returns. Spotted by ASAN: ==2407186==ERROR: AddressSanitizer: heap-use-after-free on address 0x6020003ac4f0 at pc 0x7ffff766659c bp 0x7fffffffd1d0 sp 0x7fffffffc980 READ of size 1 at 0x6020003ac4f0 thread T0 #0 0x7ffff766659b (/lib64/libasan.so.6+0x8a59b) #1 0x7ffff6bfa843 in g_str_equal ../glib/ghash.c:2303 qemu#2 0x7ffff6bf8167 in g_hash_table_lookup_node ../glib/ghash.c:493 qemu#3 0x7ffff6bf9b78 in g_hash_table_insert_internal ../glib/ghash.c:1598 qemu#4 0x7ffff6bf9c32 in g_hash_table_add ../glib/ghash.c:1689 qemu#5 0x5555596caad4 in module_load_one ../util/module.c:233 qemu#6 0x5555596ca949 in module_load_one ../util/module.c:225 qemu#7 0x5555596ca949 in module_load_one ../util/module.c:225 qemu#8 0x5555596cbdf4 in module_load_qom_all ../util/module.c:349 Typical C bug... Fixes: 9062912 ("module: use g_hash_table_add()") Cc: [email protected] Signed-off-by: Marc-André Lureau <[email protected]> Reviewed-by: Daniel P. Berrangé <[email protected]> Message-Id: <[email protected]> Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
1 parent 6ee55e1 commit 64e16fb

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

util/module.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ bool module_load_one(const char *prefix, const char *lib_name, bool mayfail)
230230
}
231231
}
232232

233-
if (!g_hash_table_add(loaded_modules, module_name)) {
233+
if (g_hash_table_contains(loaded_modules, module_name)) {
234234
g_free(module_name);
235235
return true;
236236
}
237+
g_hash_table_add(loaded_modules, module_name);
237238

238239
search_dir = getenv("QEMU_MODULE_DIR");
239240
if (search_dir != NULL) {

0 commit comments

Comments
 (0)