Skip to content

Commit

Permalink
Second part of kvojacheck fix for daynix#105.
Browse files Browse the repository at this point in the history
  • Loading branch information
gwgill committed Nov 10, 2023
1 parent 0969455 commit 440dfa1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
24 changes: 22 additions & 2 deletions UsbDk/FilterDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,32 @@ NTSTATUS CUsbDkFilterDeviceInit::Configure(ULONG InstanceNumber)
SetFileEventCallbacks([](_In_ WDFDEVICE Device, _In_ WDFREQUEST Request, _In_ WDFFILEOBJECT FileObject)
{
UNREFERENCED_PARAMETER(FileObject);
UsbDkFilterGetContext(Device)->UsbDkFilter->OnFileCreate(Request);
auto filter = UsbDkFilterGetContext(Device)->UsbDkFilter;
filter->m_open_count.AddRef();
filter->OnFileCreate(Request);
},
[](_In_ WDFFILEOBJECT FileObject)
{
WDFDEVICE Device = WdfFileObjectGetDevice(FileObject);
ULONG pid = (ULONG)(ULONG_PTR)PsGetCurrentProcessId();
auto filter = UsbDkFilterGetContext(Device)->UsbDkFilter;
ULONG pid = 0; // zero means always match

// Check PID only if there are multiple open references to the file.
// If this was the last reference, always close the redirection.
//
// This callback function might run in a different process-context
// than the initiator process, therefore the 'current process ID'
// isn't always the ID of the 'owning' process.
//
// In the worst case, the USB redirection will be kept until the last
// open file handle to the device is closed.
//
// From KMDF 1.21, there's a new method that should give us the expected ID:
// WdfFileObjectGetInitiatorProcessId(FileObject)

if (filter->m_open_count.Release()) {
pid = (ULONG)(ULONG_PTR)PsGetCurrentProcessId();
}
Strategy(Device)->OnClose(pid);
},
WDF_NO_EVENT_CALLBACK);
Expand Down
3 changes: 3 additions & 0 deletions UsbDk/FilterDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class CUsbDkFilterDevice : public CWdfDevice,
{ m_SerialNumber = Number; }

void OnFileCreate(WDFREQUEST Request);

CWdmRefCounter m_open_count;

private:
~CUsbDkFilterDevice()
{
Expand Down

0 comments on commit 440dfa1

Please sign in to comment.