Repository navigation
hidapi/windows: fix use-after-free SBH corruption due to overlapped ReadFile in hidapi not being canceled for all threads before device close - #576
Conversation
slouken
commented
May 26, 2023
- hidapi already called CancelIo on hid_close but that only cancels pending IO for the current thread. Controller read/writes originate from multiple threads (serialized, but on a different thread nonetheless) but device destruction was always done on the main device thread which left any pending overlapped reads still running after hidapi's internal read buffer is deallocated leading to intermittent free list corruption.
|
There is a warning treated as error so that Windows-cmake github action build failed. Please help to fix it. Thanks. |
9310903 to
79c0f8b
Compare
…eadFile in hidapi not being canceled for all threads before device close - hidapi already called CancelIo on hid_close but that only cancels pending IO for the current thread. Controller read/writes originate from multiple threads (serialized, but on a different thread nonetheless) but device destruction was always done on the main device thread which left any pending overlapped reads still running after hidapi's internal read buffer is deallocated leading to intermittent free list corruption. Signed-off-by: Sam Lantinga <[email protected]>
79c0f8b to
3ad2bbd
Compare
|
calling I've described this problem in details here: #133 (comment) There is also a related discussion: #237 |
|
The only long-going blocking call is |
|
We're using the correct model that you're describing above. The problem here is that we're doing a read with a timeout of 0 on one thread, which can start an overlapping I/O operation. The reading thread then exits, but the overlapping I/O operation is still pending, then the main thread calls hid_close(), then the OS completes the I/O operation, writing to the memory that was just freed. It doesn't happen often, but it is something that was causing crashes for customers here. |
Those two statements contradict each other. You actually cannot use the
That is undefined behavior even with this fix. The hid_close may successfully interrupt the IO operation, then free the |