adapter: Fix storing IRK causing invalid read 43/322843/2
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Fri, 22 Oct 2021 20:10:09 +0000 (13:10 -0700)
committerWootak Jung <wootak.jung@samsung.com>
Thu, 17 Apr 2025 01:13:22 +0000 (10:13 +0900)
When storing an IRK the storage file may not have been created yet
since that uses the device address which is likely changed to the
identity address causing the following trace:

Invalid read of size 8
   at 0x196452: store_irk.constprop.0 (adapter.c:8679)
   by 0x198C92: new_irk_callback (adapter.c:8737)
   by 0x1CF6DC: queue_foreach (queue.c:207)
   by 0x1D1394: process_notify (mgmt.c:308)
   by 0x1D1394: can_read_data (mgmt.c:374)
   by 0x1E0634: watch_callback (io-glib.c:157)
   by 0x4954A9E: g_main_context_dispatch (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x49A6A97: ??? (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x4954162: g_main_loop_run (in /usr/lib64/libglib-2.0.so.0.6600.8)
   by 0x1E0CD4: mainloop_run (mainloop-glib.c:66)
   by 0x1E10B1: mainloop_run_with_signal (mainloop-notify.c:188)
   by 0x12E3FC: main (main.c:1210)
 Address 0x6534418 is 8 bytes inside a block of size 16 free'd

Change-Id: I55ecfc9864003c6676029a024696addba04757cf
Signed-off-by: Wootak Jung <wootak.jung@samsung.com>
src/adapter.c

index b28385f2f07ae7b2c37b4ae404cf258b4e204133..50b596cc963cb47f0d5b1eb980443bd35cf11a26 100644 (file)
@@ -14607,11 +14607,15 @@ static void store_irk(struct btd_adapter *adapter, const bdaddr_t *peer,
 
        snprintf(filename, PATH_MAX, STORAGEDIR "/%s/%s/info",
                        btd_adapter_get_storage_dir(adapter), device_addr);
+       create_file(filename, 0600);
+
        key_file = g_key_file_new();
        if (!g_key_file_load_from_file(key_file, filename, 0, &gerr)) {
                error("Unable to load key file from %s: (%s)", filename,
                                                                gerr->message);
                g_clear_error(&gerr);
+               g_key_file_free(key_file);
+               return;
        }
 
        for (i = 0; i < 16; i++)