Skip to contentSkip to conversation

util/cutils: Fix heap corruption under Windows

PATCH In review Bernhard Beschow 4 participants 4 messages Apr 14

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/cutils: Fix heap corruption under Windows

Under Windows, QEMU would only sporadically start successfully. In the G_OS_WIN32 case, get_relocated_path() first determines a cursor to the end of the "result" string and then increases its size with g_string_set_size(). Since g_string_set_size() may reallocate, the cursor may become dangling. Windows may detect this and crash the QEMU process with the following message:

HEAP: Free Heap block 000000000499B640 modified at 000000000499B684 after it was freed

Furthermore, QEMU crashes spontaneously, even long after the guest has booted. For example, it presumably chrashes due to the guest setting a new cursor icon which may be a result of the heap corruption.

Fix this by determining the cursor on the resized string.

Fixes cf60ccc3306c ("cutils: Introduce bundle mechanism")
---
 util/cutils.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/cutils.c +2 -1
index 9803f11a59..76a9442085 100644
@@ -1165,9 +1165,10 @@ char *get_relocated_path(const char *dir)
 
         PCWSTR wdir_skipped_root;
         if (PathCchSkipRoot(wdir, &wdir_skipped_root) == S_OK) {
+            char *cursor;
             size = wcsrtombs(NULL, &wdir_skipped_root, 0, &(mbstate_t){0});
-            char *cursor = result->str + result->len;
             g_string_set_size(result, result->len + size);
+            cursor = result->str + result->len - size;
             wcsrtombs(cursor, &wdir_skipped_root, size + 1, &(mbstate_t){0});
         } else {
             g_string_append(result, dir);
Signature · 1 line
2.53.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Bernhard Beschow

Under Windows, QEMU would only sporadically start successfully. In the G_OS_WIN32 case, get_relocated_path() first determines a cursor to the end of the "result" string and then increases its size with g_string_set_size(). Since g_string_set_size() may reallocate, the cursor may become dangling. Windows may detect this and crash the QEMU process with the following message:

HEAP: Free Heap block 000000000499B640 modified at 000000000499B684 after it was freed

Furthermore, QEMU crashes spontaneously, even long after the guest has booted. For example, it presumably chrashes due to the guest setting a

(Typo "crashes")

Quoted 17 lines

new cursor icon which may be a result of the heap corruption.

Fix this by determining the cursor on the resized string.

Fixes cf60ccc3306c ("cutils: Introduce bundle mechanism")
---
  util/cutils.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
util/cutils.c +0 -0
index 9803f11a59..76a9442085 100644
@@ -1165,9 +1165,10 @@ char *get_relocated_path(const char *dir)
 

util/cutils.c char *get_relocated_path(const char *dir) · line 1174

Quoted 10 lines
util/cutils.c +2 -1
          PCWSTR wdir_skipped_root;
          if (PathCchSkipRoot(wdir, &wdir_skipped_root) == S_OK) {
+            char *cursor;
              size = wcsrtombs(NULL, &wdir_skipped_root, 0, &(mbstate_t){0});
-            char *cursor = result->str + result->len;
              g_string_set_size(result, result->len + size);
+            cursor = result->str + result->len - size;
              wcsrtombs(cursor, &wdir_skipped_root, size + 1, &(mbstate_t){0});
          } else {
              g_string_append(result, dir);
Reviewed-by on v1 1/1
#2 lore raw
In reply to Bernhard Beschow
Quoted 13 lines · Bernhard Beschow

Under Windows, QEMU would only sporadically start successfully. In the G_OS_WIN32 case, get_relocated_path() first determines a cursor to the end of the "result" string and then increases its size with g_string_set_size(). Since g_string_set_size() may reallocate, the cursor may become dangling. Windows may detect this and crash the QEMU process with the following message:

HEAP: Free Heap block 000000000499B640 modified at 000000000499B684 after it was freed

Furthermore, QEMU crashes spontaneously, even long after the guest has booted. For example, it presumably chrashes due to the guest setting a new cursor icon which may be a result of the heap corruption.

Quoted 5 lines

Fix this by determining the cursor on the resized string.

Fixes cf60ccc3306c ("cutils: Introduce bundle mechanism")

Regards,
Akihiko Odaki

#3 lore raw
In reply to Bernhard Beschow
Quoted 14 lines · Bernhard Beschow

Under Windows, QEMU would only sporadically start successfully. In the G_OS_WIN32 case, get_relocated_path() first determines a cursor to the end of the "result" string and then increases its size with g_string_set_size(). Since g_string_set_size() may reallocate, the cursor may become dangling. Windows may detect this and crash the QEMU process with the following message:

HEAP: Free Heap block 000000000499B640 modified at 000000000499B684 after it was freed

Furthermore, QEMU crashes spontaneously, even long after the guest has booted. For example, it presumably chrashes due to the guest setting a new cursor icon which may be a result of the heap corruption.

Quoted 5 lines

Fix this by determining the cursor on the resized string.

Fixes cf60ccc3306c ("cutils: Introduce bundle mechanism")

Thanks -- this isn't strictly a regression from 10.2 but it seems worth getting into rc4, and the fix is clear and small, so I'm going to apply it to git.

-- PMM