Skip to content

hidapi/windows: fix use-after-free SBH corruption due to overlapped ReadFile in hidapi not being canceled for all threads before device close - #576

Closed
slouken wants to merge 1 commit into
libusb:masterfrom
slouken:0001-hidapi-windows-fix-use-after-free-SBH-corruption-due.patch
Closed

slouken wants to merge 1 commit into
libusb:masterfrom
slouken:0001-hidapi-windows-fix-use-after-free-SBH-corruption-due.patch

Conversation

@slouken

@slouken slouken commented May 26, 2023

Copy link
Copy Markdown
Contributor
  • 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.

@mcuee mcuee added the Windows Related to Windows backend label May 27, 2023
@mcuee

mcuee commented May 27, 2023

Copy link
Copy Markdown
Member

@slouken

There is a warning treated as error so that Windows-cmake github action build failed. Please help to fix it. Thanks.

@slouken
slouken force-pushed the 0001-hidapi-windows-fix-use-after-free-SBH-corruption-due.patch branch 2 times, most recently from 9310903 to 79c0f8b Compare May 27, 2023 15:50
…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]>
@slouken
slouken force-pushed the 0001-hidapi-windows-fix-use-after-free-SBH-corruption-due.patch branch from 79c0f8b to 3ad2bbd Compare May 27, 2023 17:25
@Youw

Youw commented Jun 1, 2023

Copy link
Copy Markdown
Member

calling hid_close while some other thread is calling any other device-specific hidapi function is undefined behavior, even with this fix.

I've described this problem in details here: #133 (comment)

There is also a related discussion: #237

@Youw Youw closed this Jun 1, 2023
@Youw

Youw commented Jun 1, 2023

Copy link
Copy Markdown
Member

The only long-going blocking call is hid_read, and I've described the only functional and strictly correct workaround for that case.

@slouken

slouken commented Jun 1, 2023

Copy link
Copy Markdown
Contributor Author

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.

@Youw

Youw commented Jun 1, 2023

Copy link
Copy Markdown
Member

We're using the correct model that you're describing above.

with a timeout of 0

Those two statements contradict each other. You actually cannot use the 0 timeout for hid_read_timeout until #146 is implemented.
In which case you'd need:

  • call hid_interrupt_read
  • wait for the read thread to signal that hid_read/_timeout is no longer in use (need to use additional syncronisation mechanism for that)
  • only then call hid_close

then the main thread calls hid_close()

That is undefined behavior even with this fix. The hid_close may successfully interrupt the IO operation, then free the dev object in memory, while hid_read is still trying to access (some of) its fileds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Windows Related to Windows backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants