ntdll: Fix possible race-condition in iocp poller code

Sebastian Lackner sebastian at fds-team.de
Mon Mar 10 12:59:54 CDT 2014


The iocp_poller() callback expects that compl_port is set on startup,
but RtlSetIoCompletionCallback() sets this element AFTER starting the
thread - this introduces a race condition. The easiest way to fix it is
to pass the compl_port handle as the "Context" parameter.
-------------- next part --------------
>From 486111cfb730524a8b34e5738a262ff1f7f22576 Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian at fds-team.de>
Date: Mon, 10 Mar 2014 18:51:21 +0100
Subject: ntdll: Fix possible race-condition in iocp poller code

---
 dlls/ntdll/threadpool.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/dlls/ntdll/threadpool.c b/dlls/ntdll/threadpool.c
index 5bf3385..e2fc6a5 100644
--- a/dlls/ntdll/threadpool.c
+++ b/dlls/ntdll/threadpool.c
@@ -235,12 +235,14 @@ NTSTATUS WINAPI RtlQueueWorkItem(PRTL_WORK_ITEM_ROUTINE Function, PVOID Context,
  */
 static DWORD CALLBACK iocp_poller(LPVOID Arg)
 {
+    HANDLE cport = Arg;
+
     while( TRUE )
     {
         PRTL_OVERLAPPED_COMPLETION_ROUTINE callback;
         LPVOID overlapped;
         IO_STATUS_BLOCK iosb;
-        NTSTATUS res = NtRemoveIoCompletion( compl_port, (PULONG_PTR)&callback, (PULONG_PTR)&overlapped, &iosb, NULL );
+        NTSTATUS res = NtRemoveIoCompletion( cport, (PULONG_PTR)&callback, (PULONG_PTR)&overlapped, &iosb, NULL );
         if (res)
         {
             ERR("NtRemoveIoCompletion failed: 0x%x\n", res);
@@ -297,7 +299,7 @@ NTSTATUS WINAPI RtlSetIoCompletionCallback(HANDLE FileHandle, PRTL_OVERLAPPED_CO
             if (!res)
             {
                 /* FIXME native can start additional threads in case of e.g. hung callback function. */
-                res = RtlQueueWorkItem( iocp_poller, NULL, WT_EXECUTEDEFAULT );
+                res = RtlQueueWorkItem( iocp_poller, cport, WT_EXECUTEDEFAULT );
                 if (!res)
                     compl_port = cport;
                 else
-- 
1.7.9.5



More information about the wine-patches mailing list