Skip to contentSkip to conversation

aio-win32: avoid out-of-bounds access to the events array

PATCH Applied Paolo Bonzini 3 participants 3 messages Sep 15, 2014 – Sep 16, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 aio-win32: avoid out-of-bounds access to the events array

If ret is WAIT_TIMEOUT and there was an event returned by select(), we can write to a location after the end of the array. But in that case we can retry the WaitForMultipleObjects call with the same set of events, so just move the event[ret - WAIT_OBJECT_0] assignment inside the existin conditional.

---
 aio-win32.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
aio-win32.c +1 -3
index 61e3d2d..89f9e1f 100644
@@ -335,6 +335,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
         event = NULL;
         if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
             event = events[ret - WAIT_OBJECT_0];
+            events[ret - WAIT_OBJECT_0] = events[--count];
         } else if (!have_select_revents) {
             break;
         }
@@ -343,9 +344,6 @@ bool aio_poll(AioContext *ctx, bool blocking)
         blocking = false;
 
         progress |= aio_dispatch_handlers(ctx, event);
-
-        /* Try again, but only call each handler once.  */
-        events[ret - WAIT_OBJECT_0] = events[--count];
     }
 
     progress |= timerlistgroup_run_timers(&ctx->tlg);
Signature · 1 line
2.1.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 35 lines · Paolo Bonzini

If ret is WAIT_TIMEOUT and there was an event returned by select(), we can write to a location after the end of the array. But in that case we can retry the WaitForMultipleObjects call with the same set of events, so just move the event[ret - WAIT_OBJECT_0] assignment inside the existin conditional.

---
 aio-win32.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
aio-win32.c +1 -4
index 61e3d2d..89f9e1f 100644
@@ -335,6 +335,7 @@ bool aio_poll(AioContext *ctx, bool blocking)
         event = NULL;
         if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
             event = events[ret - WAIT_OBJECT_0];
+            events[ret - WAIT_OBJECT_0] = events[--count];
         } else if (!have_select_revents) {
             break;
         }
@@ -343,9 +344,6 @@ bool aio_poll(AioContext *ctx, bool blocking)
         blocking = false;
 
         progress |= aio_dispatch_handlers(ctx, event);
-
-        /* Try again, but only call each handler once.  */
-        events[ret - WAIT_OBJECT_0] = events[--count];
     }
 
     progress |= timerlistgroup_run_timers(&ctx->tlg);
--
Quoted 2 lines

2.1.0

#2 lore raw
In reply to Paolo Bonzini
Quoted 11 lines · Paolo Bonzini

If ret is WAIT_TIMEOUT and there was an event returned by select(), we can write to a location after the end of the array. But in that case we can retry the WaitForMultipleObjects call with the same set of events, so just move the event[ret - WAIT_OBJECT_0] assignment inside the existin conditional.

---
 aio-win32.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Thanks, applied to my block tree:
https://github.com/stefanha/qemu/commits/block

Stefan