1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/* $Id$ */
/** @file
* Driver Dispatch Table Hooking API
*/
/*
* Copyright (C) 2011-2016 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#include "VBoxUsbMon.h"
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
#define VBOXUSBHOOK_MEMTAG 'HUBV'
NTSTATUS VBoxUsbHookInstall(PVBOXUSBHOOK_ENTRY pHook)
{
KIRQL Irql;
KeAcquireSpinLock(&pHook->Lock, &Irql);
if (pHook->fIsInstalled)
{
WARN(("hook is marked installed, returning failure"));
KeReleaseSpinLock(&pHook->Lock, Irql);
return STATUS_UNSUCCESSFUL;
}
pHook->pfnOldHandler = (PDRIVER_DISPATCH)InterlockedExchangePointer((PVOID*)&pHook->pDrvObj->MajorFunction[pHook->iMjFunction], pHook->pfnHook);
Assert(pHook->pfnOldHandler);
Assert(pHook->pfnHook != pHook->pfnOldHandler);
pHook->fIsInstalled = TRUE;
KeReleaseSpinLock(&pHook->Lock, Irql);
return STATUS_SUCCESS;
}
NTSTATUS VBoxUsbHookUninstall(PVBOXUSBHOOK_ENTRY pHook)
{
KIRQL Irql;
KeAcquireSpinLock(&pHook->Lock, &Irql);
if (!pHook->fIsInstalled)
{
KeReleaseSpinLock(&pHook->Lock, Irql);
return STATUS_SUCCESS;
}
PDRIVER_DISPATCH pfnOldVal = (PDRIVER_DISPATCH)InterlockedCompareExchangePointer((PVOID*)&pHook->pDrvObj->MajorFunction[pHook->iMjFunction], pHook->pfnOldHandler, pHook->pfnHook);
Assert(pfnOldVal == pHook->pfnHook);
if (pfnOldVal != pHook->pfnHook)
{
AssertMsgFailed(("unhook failed!!!\n"));
/* this is bad! this could happen if someone else has chained another hook,
* or (which is even worse) restored the "initial" entry value it saved when doing a hooking before us
* return the failure and don't do anything else
* the best thing to do if this happens is to leave everything as is
* and to prevent the driver from being unloaded to ensure no one references our unloaded hook routine */
KeReleaseSpinLock(&pHook->Lock, Irql);
return STATUS_UNSUCCESSFUL;
}
pHook->fIsInstalled = FALSE;
KeReleaseSpinLock(&pHook->Lock, Irql);
/* wait for the current handlers to exit */
VBoxDrvToolRefWaitEqual(&pHook->HookRef, 1);
return STATUS_SUCCESS;
}
BOOLEAN VBoxUsbHookIsInstalled(PVBOXUSBHOOK_ENTRY pHook)
{
KIRQL Irql;
BOOLEAN fIsInstalled;
KeAcquireSpinLock(&pHook->Lock, &Irql);
fIsInstalled = pHook->fIsInstalled;
KeReleaseSpinLock(&pHook->Lock, Irql);
return fIsInstalled;
}
VOID VBoxUsbHookInit(PVBOXUSBHOOK_ENTRY pHook, PDRIVER_OBJECT pDrvObj, UCHAR iMjFunction, PDRIVER_DISPATCH pfnHook)
{
Assert(pDrvObj);
Assert(iMjFunction <= IRP_MJ_MAXIMUM_FUNCTION);
Assert(pfnHook);
memset(pHook, 0, sizeof (*pHook));
InitializeListHead(&pHook->RequestList);
KeInitializeSpinLock(&pHook->Lock);
VBoxDrvToolRefInit(&pHook->HookRef);
pHook->pDrvObj = pDrvObj;
pHook->iMjFunction = iMjFunction;
pHook->pfnHook = pfnHook;
Assert(!pHook->pfnOldHandler);
Assert(!pHook->fIsInstalled);
}
static void vboxUsbHookRequestRegisterCompletion(PVBOXUSBHOOK_ENTRY pHook, PDEVICE_OBJECT pDevObj, PIRP pIrp, PIO_COMPLETION_ROUTINE pfnCompletion, PVBOXUSBHOOK_REQUEST pRequest)
{
Assert(pfnCompletion);
Assert(pRequest);
Assert(pDevObj);
Assert(pIrp);
PIO_STACK_LOCATION pSl = IoGetCurrentIrpStackLocation(pIrp);
memset(pRequest, 0, sizeof (*pRequest));
pRequest->pHook = pHook;
pRequest->OldLocation = *pSl;
pRequest->pDevObj = pDevObj;
pRequest->pIrp = pIrp;
pRequest->bCompletionStopped = FALSE;
pSl->CompletionRoutine = pfnCompletion;
pSl->Context = pRequest;
pSl->Control = SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_ERROR | SL_INVOKE_ON_CANCEL;
KIRQL oldIrql;
KeAcquireSpinLock(&pHook->Lock, &oldIrql);
InsertTailList(&pHook->RequestList, &pRequest->ListEntry);
KeReleaseSpinLock(&pHook->Lock, oldIrql);
}
NTSTATUS VBoxUsbHookRequestPassDownHookCompletion(PVBOXUSBHOOK_ENTRY pHook, PDEVICE_OBJECT pDevObj, PIRP pIrp, PIO_COMPLETION_ROUTINE pfnCompletion, PVBOXUSBHOOK_REQUEST pRequest)
{
Assert(pfnCompletion);
vboxUsbHookRequestRegisterCompletion(pHook, pDevObj, pIrp, pfnCompletion, pRequest);
return pHook->pfnOldHandler(pDevObj, pIrp);
}
NTSTATUS VBoxUsbHookRequestPassDownHookSkip(PVBOXUSBHOOK_ENTRY pHook, PDEVICE_OBJECT pDevObj, PIRP pIrp)
{
return pHook->pfnOldHandler(pDevObj, pIrp);
}
NTSTATUS VBoxUsbHookRequestMoreProcessingRequired(PVBOXUSBHOOK_ENTRY pHook, PDEVICE_OBJECT pDevObj, PIRP pIrp,
PVBOXUSBHOOK_REQUEST pRequest)
{
RT_NOREF3(pHook, pDevObj, pIrp);
Assert(!pRequest->bCompletionStopped);
pRequest->bCompletionStopped = TRUE;
return STATUS_MORE_PROCESSING_REQUIRED;
}
NTSTATUS VBoxUsbHookRequestComplete(PVBOXUSBHOOK_ENTRY pHook, PDEVICE_OBJECT pDevObj, PIRP pIrp, PVBOXUSBHOOK_REQUEST pRequest)
{
NTSTATUS Status = STATUS_SUCCESS;
if (pRequest->OldLocation.CompletionRoutine && pRequest->OldLocation.Control)
{
Status = pRequest->OldLocation.CompletionRoutine(pDevObj, pIrp, pRequest->OldLocation.Context);
}
if (Status != STATUS_MORE_PROCESSING_REQUIRED)
{
if (pRequest->bCompletionStopped)
{
IoCompleteRequest(pIrp, IO_NO_INCREMENT);
}
}
/*
* else - in case driver returned STATUS_MORE_PROCESSING_REQUIRED,
* it will call IoCompleteRequest itself
*/
KIRQL oldIrql;
KeAcquireSpinLock(&pHook->Lock, &oldIrql);
RemoveEntryList(&pRequest->ListEntry);
KeReleaseSpinLock(&pHook->Lock, oldIrql);
return Status;
}
#define PVBOXUSBHOOK_REQUEST_FROM_LE(_pLe) ( (PVBOXUSBHOOK_REQUEST)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBHOOK_REQUEST, ListEntry) ) )
VOID VBoxUsbHookVerifyCompletion(PVBOXUSBHOOK_ENTRY pHook, PVBOXUSBHOOK_REQUEST pRequest, PIRP pIrp)
{
KIRQL oldIrql;
KeAcquireSpinLock(&pHook->Lock, &oldIrql);
for (PLIST_ENTRY pLe = pHook->RequestList.Flink; pLe != &pHook->RequestList; pLe = pLe->Flink)
{
PVBOXUSBHOOK_REQUEST pCur = PVBOXUSBHOOK_REQUEST_FROM_LE(pLe);
if (pCur != pRequest)
continue;
if (pCur->pIrp != pIrp)
continue;
WARN(("found pending IRP(0x%p) when it should not be", pIrp));
}
KeReleaseSpinLock(&pHook->Lock, oldIrql);
}
|