Skip to contentSkip to conversation

util/main-loop: Fix maximum number of wait objects for win32

PATCH In review Bin Meng 3 participants 10 messages Oct 19, 2022 – Nov 1, 2022

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v4 1/3 util/main-loop: Fix maximum number of wait objects for win32

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v4:
- make the out of bounds access protection explicit

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
util/main-loop.c +5 -5
index f00a25451b..de38876064 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
 /* Wait objects support */
 typedef struct WaitObjects {
     int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
 } WaitObjects;
 
 static WaitObjects wait_objects = {0};
@@ -395,7 +395,7 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
         if (w->events[i] == handle) {
             found = 1;
         }
-        if (found) {
+        if (found && i < (MAXIMUM_WAIT_OBJECTS - 1)) {
             w->events[i] = w->events[i + 1];
             w->func[i] = w->func[i + 1];
             w->opaque[i] = w->opaque[i + 1];
Signature · 1 line
2.34.1
author
#3 lore raw

PATCH v4 3/3 util/aio-win32: Correct the event array size in aio_poll()

From: Bin Meng <[email protected]>

WaitForMultipleObjects() can only wait for MAXIMUM_WAIT_OBJECTS object handles. Correct the event array size in aio_poll() and add a assert() to ensure it does not cause out of bound access.

---

(no changes since v2)

Changes in v2:
- change 'count' to unsigned

 util/aio-win32.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
util/aio-win32.c +3 -2
index 44003d645e..80cfe012ad 100644
@@ -326,9 +326,9 @@ void aio_dispatch(AioContext *ctx)
 bool aio_poll(AioContext *ctx, bool blocking)
 {
     AioHandler *node;
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
     bool progress, have_select_revents, first;
-    int count;
+    unsigned count;
     int timeout;
 
     /*
@@ -369,6 +369,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
     QLIST_FOREACH_RCU(node, &ctx->aio_handlers, node) {
         if (!node->deleted && node->io_notify
             && aio_node_check(ctx, node->is_external)) {
+            assert(count < MAXIMUM_WAIT_OBJECTS);
             events[count++] = event_notifier_get_handle(node->e);
         }
     }
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH v4 2/3 util/main-loop: Avoid adding the same HANDLE twice

In reply to Bin Meng

From: Bin Meng <[email protected]>

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice, as the behavior is undefined when passing an array that contains same HANDLEs to WaitForMultipleObjects() API.

---

(no changes since v3)

Changes in v3:
- new patch: avoid adding the same HANDLE twice

 include/qemu/main-loop.h |  2 ++
 util/main-loop.c         | 10 ++++++++++
 2 files changed, 12 insertions(+)
include/qemu/main-loop.h +2 -0
index aac707d073..3c9a9a982d 100644
@@ -157,6 +157,8 @@ typedef void WaitObjectFunc(void *opaque);
  * in the main loop's calls to WaitForMultipleObjects.  When the handle
  * is in a signaled state, QEMU will call @func.
  *
+ * If the same HANDLE is added twice, this function returns -1.
+ *
  * @handle: The Windows handle to be observed.
  * @func: A function to be called when @handle is in a signaled state.
  * @opaque: A pointer-size value that is passed to @func.
util/main-loop.c +10 -0
index de38876064..10fa74c6e3 100644
@@ -373,10 +373,20 @@ static WaitObjects wait_objects = {0};
 
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
 {
+    int i;
     WaitObjects *w = &wait_objects;
+
     if (w->num >= MAXIMUM_WAIT_OBJECTS) {
         return -1;
     }
+
+    for (i = 0; i < w->num; i++) {
+        /* check if the same handle is added twice */
+        if (w->events[i] == handle) {
+            return -1;
+        }
+    }
+
     w->events[w->num] = handle;
     w->func[w->num] = func;
     w->opaque[w->num] = opaque;
Signature · 1 line
2.34.1
author
#5 lore raw
In reply to Bin Meng
Quoted 15 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v4:
- make the out of bounds access protection explicit

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Quoted 7 lines

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Ping?

author
#6 lore raw

Hi Daniel,

Quoted 21 lines · Bin Meng
Quoted 19 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v4:
- make the out of bounds access protection explicit

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

Quoted 5 lines
Quoted 3 lines
 util/main-loop.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Ping?

Would you queue this series? Thanks!

Regards,
Bin

#7 lore raw
Quoted 24 lines · Bin Meng

Hi Daniel,

Quoted 21 lines · Bin Meng
Quoted 19 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v4:
- make the out of bounds access protection explicit

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
 util/main-loop.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Ping?

Would you queue this series? Thanks!

The main loop is not my area as maintainer - it would normally be Paolo IIRC.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v4 3/3
#4 lore raw

Re: [PATCH v4 3/3] util/aio-win32: Correct the event array size in aio_poll()

In reply to Bin Meng
Quoted 12 lines · Bin Meng

From: Bin Meng <[email protected]>

WaitForMultipleObjects() can only wait for MAXIMUM_WAIT_OBJECTS object handles. Correct the event array size in aio_poll() and add a assert() to ensure it does not cause out of bound access.

---

Quoted 7 lines

(no changes since v2)

Changes in v2:
- change 'count' to unsigned

  util/aio-win32.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
Reviewed-by on v4 1/3
#9 lore raw
In reply to Bin Meng
Quoted 41 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v4:
- make the out of bounds access protection explicit

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

  util/main-loop.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
util/main-loop.c +4 -4
index f00a25451b..de38876064 100644
@@ -363,10 +363,10 @@ void qemu_del_polling_cb(PollingFunc *func, void *opaque)
  /* Wait objects support */
  typedef struct WaitObjects {
      int num;
-    int revents[MAXIMUM_WAIT_OBJECTS + 1];
-    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
-    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
-    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
+    int revents[MAXIMUM_WAIT_OBJECTS];
+    HANDLE events[MAXIMUM_WAIT_OBJECTS];
+    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS];
+    void *opaque[MAXIMUM_WAIT_OBJECTS];
  } WaitObjects;
 
  static WaitObjects wait_objects = {0};

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 398

Quoted 5 lines
util/main-loop.c +1 -1
@@ -395,7 +395,7 @@ void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
          if (w->events[i] == handle) {
              found = 1;
          }
-        if (found) {
+        if (found && i < (MAXIMUM_WAIT_OBJECTS - 1)) {

Matter of style, I find this form easier to review (same logic than what follows):

           if (found && i + 1 < MAXIMUM_WAIT_OBJECTS) {

util/main-loop.c void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) · line 401

Quoted 3 lines
util/main-loop.c +0 -0
              w->events[i] = w->events[i + 1];
              w->func[i] = w->func[i + 1];
              w->opaque[i] = w->opaque[i + 1];
author
#8 lore raw
In reply to Daniel P. Berrangé
Quoted 30 lines · Daniel P. Berrangé
Quoted 28 lines · Bin Meng

Hi Daniel,

Quoted 25 lines · Bin Meng
Quoted 22 lines · Bin Meng

From: Bin Meng <[email protected]>

The maximum number of wait objects for win32 should be MAXIMUM_WAIT_OBJECTS, not MAXIMUM_WAIT_OBJECTS + 1.

---

Changes in v4:
- make the out of bounds access protection explicit

Changes in v3:
- move the check of adding the same HANDLE twice to a separete patch

Changes in v2:
- fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice

 util/main-loop.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Ping?

Would you queue this series? Thanks!

The main loop is not my area as maintainer - it would normally be Paolo IIRC.

Thanks, but Paolo has been silent since day 1 ...

Regards,
Bin

Reviewed-by on v4 2/3
#2 lore raw

Re: [PATCH v4 2/3] util/main-loop: Avoid adding the same HANDLE twice

In reply to Bin Meng
Quoted 9 lines · Bin Meng

From: Bin Meng <[email protected]>

Fix the logic in qemu_add_wait_object() to avoid adding the same HANDLE twice, as the behavior is undefined when passing an array that contains same HANDLEs to WaitForMultipleObjects() API.

---

Quoted 8 lines

(no changes since v3)

Changes in v3:
- new patch: avoid adding the same HANDLE twice

  include/qemu/main-loop.h |  2 ++
  util/main-loop.c         | 10 ++++++++++
  2 files changed, 12 insertions(+)