Skip to contentSkip to conversation

qtest/libqtest.c: fix heap-buffer-overflow in qtest_cb_for_every_machine()

PATCH Superseded Gan Qixin 2 participants 3 messages Jan 4, 2021 – Jan 5, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Gan Qixin

author
#0 lore raw

PATCH 1/1 qtest/libqtest.c: fix heap-buffer-overflow in qtest_cb_for_every_machine()

When the length of mname is less than 5, memcpy ("xenfv", mname, 5) will cause heap buffer overflow. Therefore, use strcmp to avoid this problem.

The asan showed stack:

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000f2f4 at pc 0x7f65d8cc2225 bp 0x7ffe93cc5a60 sp 0x7ffe93cc5208 READ of size 5 at 0x60200000f2f4 thread T0

    #0 0x7f65d8cc2224 in memcmp (/lib64/libasan.so.5+0xdf224)
    #1 0x5632c20be95b in qtest_cb_for_every_machine tests/qtest/libqtest.c:1282
    #2 0x5632c20b7995 in main tests/qtest/test-hmp.c:160
    #3 0x7f65d88fed42 in __libc_start_main (/lib64/libc.so.6+0x26d42)
    #4 0x5632c20b72cd in _start (build/tests/qtest/test-hmp+0x542cd)

---
Cc: Thomas Huth <[email protected]>
Cc: Laurent Vivier <[email protected]>

---
 tests/qtest/libqtest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/libqtest.c +1 -1
index e49f3a1e45..e8179a3509 100644
@@ -1281,7 +1281,7 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
         g_assert(qstr);
         mname = qstring_get_str(qstr);
         /* Ignore machines that cannot be used for qtests */
-        if (!memcmp("xenfv", mname, 5) || g_str_equal("xenpv", mname)) {
+        if (!strcmp("xenfv", mname) || g_str_equal("xenpv", mname)) {
             continue;
         }
         if (!skip_old_versioned || !qtest_is_old_versioned_machine(mname)) {
Signature · 1 line
2.23.0
#1 lore raw
Quoted 28 lines · Gan Qixin

When the length of mname is less than 5, memcpy ("xenfv", mname, 5) will cause heap buffer overflow. Therefore, use strcmp to avoid this problem.

The asan showed stack:

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000f2f4 at pc 0x7f65d8cc2225 bp 0x7ffe93cc5a60 sp 0x7ffe93cc5208 READ of size 5 at 0x60200000f2f4 thread T0

     #0 0x7f65d8cc2224 in memcmp (/lib64/libasan.so.5+0xdf224)
     #1 0x5632c20be95b in qtest_cb_for_every_machine tests/qtest/libqtest.c:1282
     #2 0x5632c20b7995 in main tests/qtest/test-hmp.c:160
     #3 0x7f65d88fed42 in __libc_start_main (/lib64/libc.so.6+0x26d42)
     #4 0x5632c20b72cd in _start (build/tests/qtest/test-hmp+0x542cd)

---
Cc: Thomas Huth <[email protected]>
Cc: Laurent Vivier <[email protected]>

---
  tests/qtest/libqtest.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/libqtest.c +0 -0
index e49f3a1e45..e8179a3509 100644

tests/qtest/libqtest.c void qtest_cb_for_every_machine(void (*cb)(const char *machine), · line 1284

Quoted 5 lines
tests/qtest/libqtest.c +1 -1
@@ -1281,7 +1281,7 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine),
          g_assert(qstr);
          mname = qstring_get_str(qstr);
          /* Ignore machines that cannot be used for qtests */
-        if (!memcmp("xenfv", mname, 5) || g_str_equal("xenpv", mname)) {
+        if (!strcmp("xenfv", mname) || g_str_equal("xenpv", mname)) {

Using strcmp() is likely wrong here, since we're talking about strings like "xenfv-4.2" here ... so I guess strncmp(..., 5) would be the right way to go?

Thomas

author
#2 lore raw
Quoted 47 lines

From: Thomas Huth [mailto:[email protected]]
Sent: Tuesday, January 5, 2021 5:47 PM
To: ganqixin <[email protected]>; [email protected]; [email protected]
Cc: Chenqun (kuhn) <[email protected]>; Zhanghailiang <[email protected]>; Euler Robot <[email protected]>; Laurent Vivier <[email protected]>
Subject: Re: [PATCH] qtest/libqtest.c: fix heap-buffer-overflow in qtest_cb_for_every_machine()

Quoted 2 lines · Gan Qixin

When the length of mname is less than 5, memcpy ("xenfv", mname, 5) will cause heap buffer overflow. Therefore, use strcmp to avoid this

problem.

Quoted 9 lines

The asan showed stack:

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x60200000f2f4 at pc 0x7f65d8cc2225 bp 0x7ffe93cc5a60 sp 0x7ffe93cc5208 READ of size 5 at
0x60200000f2f4 thread T0

     #0 0x7f65d8cc2224 in memcmp (/lib64/libasan.so.5+0xdf224)
     #1 0x5632c20be95b in qtest_cb_for_every_machine

tests/qtest/libqtest.c:1282

tests/qtest/libqtest.c void qtest_cb_for_every_machine(void

Quoted 18 lines
     #2 0x5632c20b7995 in main tests/qtest/test-hmp.c:160
     #3 0x7f65d88fed42 in __libc_start_main (/lib64/libc.so.6+0x26d42)
     #4 0x5632c20b72cd in _start (build/tests/qtest/test-hmp+0x542cd)

---
Cc: Thomas Huth <[email protected]>
Cc: Laurent Vivier <[email protected]>

---
  tests/qtest/libqtest.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tests/qtest/libqtest.c index renamed from tests/qtest/libqtest.c +0 -0

e49f3a1e45..e8179a3509 100644

tests/qtest/libqtest.c +0 -0
@@ -1281,7 +1281,7 @@ void qtest_cb_for_every_machine(void

(*cb)(const char *machine),

tests/qtest/libqtest.c void qtest_cb_for_every_machine(void (*cb)(const char *machine),

Quoted 4 lines
tests/qtest/libqtest.c +0 -1
          g_assert(qstr);
          mname = qstring_get_str(qstr);
          /* Ignore machines that cannot be used for qtests */
-        if (!memcmp("xenfv", mname, 5) || g_str_equal("xenpv",
Quoted 5 lines

mname)) {

tests/qtest/libqtest.c void qtest_cb_for_every_machine(void (*cb)(const char *machine), · line 1284

Quoted 1 line
tests/qtest/libqtest.c +1 -0
+        if (!strcmp("xenfv", mname) || g_str_equal("xenpv", mname)) {

Using strcmp() is likely wrong here, since we're talking about strings like "xenfv-4.2" here ... so I guess strncmp(..., 5) would be the right way to go?

Yes, using strcmp() is wrong, I will modify this patch. Thanks for your reply!