1 | /* $Id: SUPDrv-win.cpp 38321 2011-08-05 10:08:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxDrv - The VirtualBox Support Driver - Windows NT specifics.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 | /*******************************************************************************
|
---|
28 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define LOG_GROUP LOG_GROUP_SUP_DRV
|
---|
31 | #include "../SUPDrvInternal.h"
|
---|
32 | #include <excpt.h>
|
---|
33 | #include <ntimage.h>
|
---|
34 |
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/initterm.h>
|
---|
37 | #include <iprt/mem.h>
|
---|
38 | #include <iprt/process.h>
|
---|
39 | #include <iprt/power.h>
|
---|
40 | #include <iprt/string.h>
|
---|
41 | #include <VBox/log.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Defined Constants And Macros *
|
---|
46 | *******************************************************************************/
|
---|
47 | /** The support service name. */
|
---|
48 | #define SERVICE_NAME "VBoxDrv"
|
---|
49 | /** Win32 Device name. */
|
---|
50 | #define DEVICE_NAME "\\\\.\\VBoxDrv"
|
---|
51 | /** NT Device name. */
|
---|
52 | #define DEVICE_NAME_NT L"\\Device\\VBoxDrv"
|
---|
53 | /** Win Symlink name. */
|
---|
54 | #define DEVICE_NAME_DOS L"\\DosDevices\\VBoxDrv"
|
---|
55 | /** The Pool tag (VBox). */
|
---|
56 | #define SUPDRV_NT_POOL_TAG 'xoBV'
|
---|
57 |
|
---|
58 |
|
---|
59 | /*******************************************************************************
|
---|
60 | * Structures and Typedefs *
|
---|
61 | *******************************************************************************/
|
---|
62 | #if 0 //def RT_ARCH_AMD64
|
---|
63 | typedef struct SUPDRVEXECMEM
|
---|
64 | {
|
---|
65 | PMDL pMdl;
|
---|
66 | void *pvMapping;
|
---|
67 | void *pvAllocation;
|
---|
68 | } SUPDRVEXECMEM, *PSUPDRVEXECMEM;
|
---|
69 | #endif
|
---|
70 |
|
---|
71 |
|
---|
72 | /*******************************************************************************
|
---|
73 | * Internal Functions *
|
---|
74 | *******************************************************************************/
|
---|
75 | static void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj);
|
---|
76 | static NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
77 | static NTSTATUS _stdcall VBoxDrvNtCleanup(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
78 | static NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
79 | static NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
80 | static int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack);
|
---|
81 | static NTSTATUS _stdcall VBoxDrvNtInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
82 | static VOID _stdcall VBoxPowerDispatchCallback(PVOID pCallbackContext, PVOID pArgument1, PVOID pArgument2);
|
---|
83 | static NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp);
|
---|
84 | static NTSTATUS VBoxDrvNtErr2NtStatus(int rc);
|
---|
85 |
|
---|
86 |
|
---|
87 | /*******************************************************************************
|
---|
88 | * Exported Functions *
|
---|
89 | *******************************************************************************/
|
---|
90 | RT_C_DECLS_BEGIN
|
---|
91 | ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath);
|
---|
92 | RT_C_DECLS_END
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Driver entry point.
|
---|
97 | *
|
---|
98 | * @returns appropriate status code.
|
---|
99 | * @param pDrvObj Pointer to driver object.
|
---|
100 | * @param pRegPath Registry base path.
|
---|
101 | */
|
---|
102 | ULONG _stdcall DriverEntry(PDRIVER_OBJECT pDrvObj, PUNICODE_STRING pRegPath)
|
---|
103 | {
|
---|
104 | NTSTATUS rc;
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Create device.
|
---|
108 | * (That means creating a device object and a symbolic link so the DOS
|
---|
109 | * subsystems (OS/2, win32, ++) can access the device.)
|
---|
110 | */
|
---|
111 | UNICODE_STRING DevName;
|
---|
112 | RtlInitUnicodeString(&DevName, DEVICE_NAME_NT);
|
---|
113 | PDEVICE_OBJECT pDevObj;
|
---|
114 | rc = IoCreateDevice(pDrvObj, sizeof(SUPDRVDEVEXT), &DevName, FILE_DEVICE_UNKNOWN, 0, FALSE, &pDevObj);
|
---|
115 | if (NT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | UNICODE_STRING DosName;
|
---|
118 | RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);
|
---|
119 | rc = IoCreateSymbolicLink(&DosName, &DevName);
|
---|
120 | if (NT_SUCCESS(rc))
|
---|
121 | {
|
---|
122 | int vrc = RTR0Init(0);
|
---|
123 | if (RT_SUCCESS(vrc))
|
---|
124 | {
|
---|
125 | Log(("VBoxDrv::DriverEntry\n"));
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * Initialize the device extension.
|
---|
129 | */
|
---|
130 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
131 | memset(pDevExt, 0, sizeof(*pDevExt));
|
---|
132 |
|
---|
133 | vrc = supdrvInitDevExt(pDevExt, sizeof(SUPDRVSESSION));
|
---|
134 | if (!vrc)
|
---|
135 | {
|
---|
136 | /*
|
---|
137 | * Setup the driver entry points in pDrvObj.
|
---|
138 | */
|
---|
139 | pDrvObj->DriverUnload = VBoxDrvNtUnload;
|
---|
140 | pDrvObj->MajorFunction[IRP_MJ_CREATE] = VBoxDrvNtCreate;
|
---|
141 | pDrvObj->MajorFunction[IRP_MJ_CLEANUP] = VBoxDrvNtCleanup;
|
---|
142 | pDrvObj->MajorFunction[IRP_MJ_CLOSE] = VBoxDrvNtClose;
|
---|
143 | pDrvObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = VBoxDrvNtDeviceControl;
|
---|
144 | pDrvObj->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] = VBoxDrvNtInternalDeviceControl;
|
---|
145 | pDrvObj->MajorFunction[IRP_MJ_READ] = VBoxDrvNtNotSupportedStub;
|
---|
146 | pDrvObj->MajorFunction[IRP_MJ_WRITE] = VBoxDrvNtNotSupportedStub;
|
---|
147 |
|
---|
148 | /* more? */
|
---|
149 |
|
---|
150 | /* Register ourselves for power state changes. */
|
---|
151 | UNICODE_STRING CallbackName;
|
---|
152 | OBJECT_ATTRIBUTES Attr;
|
---|
153 |
|
---|
154 | RtlInitUnicodeString(&CallbackName, L"\\Callback\\PowerState");
|
---|
155 | InitializeObjectAttributes(&Attr, &CallbackName, OBJ_CASE_INSENSITIVE, NULL, NULL);
|
---|
156 |
|
---|
157 | rc = ExCreateCallback(&pDevExt->pObjPowerCallback, &Attr, TRUE, TRUE);
|
---|
158 | if (rc == STATUS_SUCCESS)
|
---|
159 | pDevExt->hPowerCallback = ExRegisterCallback(pDevExt->pObjPowerCallback, VBoxPowerDispatchCallback, pDevObj);
|
---|
160 |
|
---|
161 | Log(("VBoxDrv::DriverEntry returning STATUS_SUCCESS\n"));
|
---|
162 | return STATUS_SUCCESS;
|
---|
163 | }
|
---|
164 |
|
---|
165 | Log(("supdrvInitDevExit failed with vrc=%d!\n", vrc));
|
---|
166 | rc = VBoxDrvNtErr2NtStatus(vrc);
|
---|
167 |
|
---|
168 | IoDeleteSymbolicLink(&DosName);
|
---|
169 | RTR0Term();
|
---|
170 | }
|
---|
171 | else
|
---|
172 | {
|
---|
173 | Log(("RTR0Init failed with vrc=%d!\n", vrc));
|
---|
174 | rc = VBoxDrvNtErr2NtStatus(vrc);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | else
|
---|
178 | Log(("IoCreateSymbolicLink failed with rc=%#x!\n", rc));
|
---|
179 |
|
---|
180 | IoDeleteDevice(pDevObj);
|
---|
181 | }
|
---|
182 | else
|
---|
183 | Log(("IoCreateDevice failed with rc=%#x!\n", rc));
|
---|
184 |
|
---|
185 | if (NT_SUCCESS(rc))
|
---|
186 | rc = STATUS_INVALID_PARAMETER;
|
---|
187 | Log(("VBoxDrv::DriverEntry returning %#x\n", rc));
|
---|
188 | return rc;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Unload the driver.
|
---|
194 | *
|
---|
195 | * @param pDrvObj Driver object.
|
---|
196 | */
|
---|
197 | void _stdcall VBoxDrvNtUnload(PDRIVER_OBJECT pDrvObj)
|
---|
198 | {
|
---|
199 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDrvObj->DeviceObject->DeviceExtension;
|
---|
200 |
|
---|
201 | Log(("VBoxDrvNtUnload at irql %d\n", KeGetCurrentIrql()));
|
---|
202 |
|
---|
203 | /* Clean up the power callback registration. */
|
---|
204 | if (pDevExt->hPowerCallback)
|
---|
205 | ExUnregisterCallback(pDevExt->hPowerCallback);
|
---|
206 | if (pDevExt->pObjPowerCallback)
|
---|
207 | ObDereferenceObject(pDevExt->pObjPowerCallback);
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * We ASSUME that it's not possible to unload a driver with open handles.
|
---|
211 | * Start by deleting the symbolic link
|
---|
212 | */
|
---|
213 | UNICODE_STRING DosName;
|
---|
214 | RtlInitUnicodeString(&DosName, DEVICE_NAME_DOS);
|
---|
215 | NTSTATUS rc = IoDeleteSymbolicLink(&DosName);
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * Terminate the GIP page and delete the device extension.
|
---|
219 | */
|
---|
220 | supdrvDeleteDevExt(pDevExt);
|
---|
221 | RTR0Term();
|
---|
222 | IoDeleteDevice(pDrvObj->DeviceObject);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Create (i.e. Open) file entry point.
|
---|
228 | *
|
---|
229 | * @param pDevObj Device object.
|
---|
230 | * @param pIrp Request packet.
|
---|
231 | */
|
---|
232 | NTSTATUS _stdcall VBoxDrvNtCreate(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
233 | {
|
---|
234 | Log(("VBoxDrvNtCreate\n"));
|
---|
235 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
236 | PFILE_OBJECT pFileObj = pStack->FileObject;
|
---|
237 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * We are not remotely similar to a directory...
|
---|
241 | * (But this is possible.)
|
---|
242 | */
|
---|
243 | if (pStack->Parameters.Create.Options & FILE_DIRECTORY_FILE)
|
---|
244 | {
|
---|
245 | pIrp->IoStatus.Status = STATUS_NOT_A_DIRECTORY;
|
---|
246 | pIrp->IoStatus.Information = 0;
|
---|
247 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
248 | return STATUS_NOT_A_DIRECTORY;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /*
|
---|
252 | * Call common code for the rest.
|
---|
253 | */
|
---|
254 | pFileObj->FsContext = NULL;
|
---|
255 | PSUPDRVSESSION pSession;
|
---|
256 | //#if 0 /** @todo check if this works, consider OBJ_KERNEL_HANDLE too. */
|
---|
257 | bool fUser = pIrp->RequestorMode != KernelMode;
|
---|
258 | //#else
|
---|
259 | // bool fUser = true;
|
---|
260 | //#endif
|
---|
261 | int rc = supdrvCreateSession(pDevExt, fUser, &pSession);
|
---|
262 | if (!rc)
|
---|
263 | pFileObj->FsContext = pSession;
|
---|
264 |
|
---|
265 | NTSTATUS rcNt = pIrp->IoStatus.Status = VBoxDrvNtErr2NtStatus(rc);
|
---|
266 | pIrp->IoStatus.Information = 0;
|
---|
267 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
268 |
|
---|
269 | return rcNt;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * Clean up file handle entry point.
|
---|
275 | *
|
---|
276 | * @param pDevObj Device object.
|
---|
277 | * @param pIrp Request packet.
|
---|
278 | */
|
---|
279 | NTSTATUS _stdcall VBoxDrvNtCleanup(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
280 | {
|
---|
281 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
282 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
283 | PFILE_OBJECT pFileObj = pStack->FileObject;
|
---|
284 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pFileObj->FsContext;
|
---|
285 |
|
---|
286 | Log(("VBoxDrvNtCleanup: pDevExt=%p pFileObj=%p pSession=%p\n", pDevExt, pFileObj, pSession));
|
---|
287 | if (pSession)
|
---|
288 | {
|
---|
289 | supdrvCloseSession(pDevExt, (PSUPDRVSESSION)pFileObj->FsContext);
|
---|
290 | pFileObj->FsContext = NULL;
|
---|
291 | }
|
---|
292 |
|
---|
293 | pIrp->IoStatus.Information = 0;
|
---|
294 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
295 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
296 |
|
---|
297 | return STATUS_SUCCESS;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Close file entry point.
|
---|
303 | *
|
---|
304 | * @param pDevObj Device object.
|
---|
305 | * @param pIrp Request packet.
|
---|
306 | */
|
---|
307 | NTSTATUS _stdcall VBoxDrvNtClose(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
308 | {
|
---|
309 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
310 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
311 | PFILE_OBJECT pFileObj = pStack->FileObject;
|
---|
312 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pFileObj->FsContext;
|
---|
313 |
|
---|
314 | Log(("VBoxDrvNtClose: pDevExt=%p pFileObj=%p pSession=%p\n", pDevExt, pFileObj, pSession));
|
---|
315 | if (pSession)
|
---|
316 | {
|
---|
317 | supdrvCloseSession(pDevExt, (PSUPDRVSESSION)pFileObj->FsContext);
|
---|
318 | pFileObj->FsContext = NULL;
|
---|
319 | }
|
---|
320 |
|
---|
321 | pIrp->IoStatus.Information = 0;
|
---|
322 | pIrp->IoStatus.Status = STATUS_SUCCESS;
|
---|
323 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
324 |
|
---|
325 | return STATUS_SUCCESS;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | /**
|
---|
330 | * Device I/O Control entry point.
|
---|
331 | *
|
---|
332 | * @param pDevObj Device object.
|
---|
333 | * @param pIrp Request packet.
|
---|
334 | */
|
---|
335 | NTSTATUS _stdcall VBoxDrvNtDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
336 | {
|
---|
337 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
338 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
339 | PSUPDRVSESSION pSession = (PSUPDRVSESSION)pStack->FileObject->FsContext;
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * Deal with the two high-speed IOCtl that takes it's arguments from
|
---|
343 | * the session and iCmd, and only returns a VBox status code.
|
---|
344 | *
|
---|
345 | * Note: The previous method of returning the rc prior to IOC version
|
---|
346 | * 7.4 has been abandond, we're no longer compatible with that
|
---|
347 | * interface.
|
---|
348 | */
|
---|
349 | ULONG ulCmd = pStack->Parameters.DeviceIoControl.IoControlCode;
|
---|
350 | if ( ulCmd == SUP_IOCTL_FAST_DO_RAW_RUN
|
---|
351 | || ulCmd == SUP_IOCTL_FAST_DO_HWACC_RUN
|
---|
352 | || ulCmd == SUP_IOCTL_FAST_DO_NOP)
|
---|
353 | {
|
---|
354 | #ifdef VBOX_WITH_VMMR0_DISABLE_PREEMPTION
|
---|
355 | int rc = supdrvIOCtlFast(ulCmd, (unsigned)(uintptr_t)pIrp->UserBuffer /* VMCPU id */, pDevExt, pSession);
|
---|
356 | #else
|
---|
357 | /* Raise the IRQL to DISPATCH_LEVEL to prevent Windows from rescheduling us to another CPU/core. */
|
---|
358 | Assert(KeGetCurrentIrql() <= DISPATCH_LEVEL);
|
---|
359 | KIRQL oldIrql;
|
---|
360 | KeRaiseIrql(DISPATCH_LEVEL, &oldIrql);
|
---|
361 | int rc = supdrvIOCtlFast(ulCmd, (unsigned)(uintptr_t)pIrp->UserBuffer /* VMCPU id */, pDevExt, pSession);
|
---|
362 | KeLowerIrql(oldIrql);
|
---|
363 | #endif
|
---|
364 |
|
---|
365 | /* Complete the I/O request. */
|
---|
366 | NTSTATUS rcNt = pIrp->IoStatus.Status = RT_SUCCESS(rc) ? STATUS_SUCCESS : STATUS_INVALID_PARAMETER;
|
---|
367 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
368 | return rcNt;
|
---|
369 | }
|
---|
370 |
|
---|
371 | return VBoxDrvNtDeviceControlSlow(pDevExt, pSession, pIrp, pStack);
|
---|
372 | }
|
---|
373 |
|
---|
374 |
|
---|
375 | /**
|
---|
376 | * Worker for VBoxDrvNtDeviceControl that takes the slow IOCtl functions.
|
---|
377 | *
|
---|
378 | * @returns NT status code.
|
---|
379 | *
|
---|
380 | * @param pDevObj Device object.
|
---|
381 | * @param pSession The session.
|
---|
382 | * @param pIrp Request packet.
|
---|
383 | * @param pStack The stack location containing the DeviceControl parameters.
|
---|
384 | */
|
---|
385 | static int VBoxDrvNtDeviceControlSlow(PSUPDRVDEVEXT pDevExt, PSUPDRVSESSION pSession, PIRP pIrp, PIO_STACK_LOCATION pStack)
|
---|
386 | {
|
---|
387 | NTSTATUS rcNt;
|
---|
388 | unsigned cbOut = 0;
|
---|
389 | int rc = 0;
|
---|
390 | Log2(("VBoxDrvNtDeviceControlSlow(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
|
---|
391 | pDevExt, pIrp, pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
392 | pIrp->AssociatedIrp.SystemBuffer, pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
393 | pStack->Parameters.DeviceIoControl.OutputBufferLength, pSession));
|
---|
394 |
|
---|
395 | #ifdef RT_ARCH_AMD64
|
---|
396 | /* Don't allow 32-bit processes to do any I/O controls. */
|
---|
397 | if (!IoIs32bitProcess(pIrp))
|
---|
398 | #endif
|
---|
399 | {
|
---|
400 | /* Verify that it's a buffered CTL. */
|
---|
401 | if ((pStack->Parameters.DeviceIoControl.IoControlCode & 0x3) == METHOD_BUFFERED)
|
---|
402 | {
|
---|
403 | /* Verify that the sizes in the request header are correct. */
|
---|
404 | PSUPREQHDR pHdr = (PSUPREQHDR)pIrp->AssociatedIrp.SystemBuffer;
|
---|
405 | if ( pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr)
|
---|
406 | && pStack->Parameters.DeviceIoControl.InputBufferLength == pHdr->cbIn
|
---|
407 | && pStack->Parameters.DeviceIoControl.OutputBufferLength == pHdr->cbOut)
|
---|
408 | {
|
---|
409 | /*
|
---|
410 | * Do the job.
|
---|
411 | */
|
---|
412 | rc = supdrvIOCtl(pStack->Parameters.DeviceIoControl.IoControlCode, pDevExt, pSession, pHdr);
|
---|
413 | if (!rc)
|
---|
414 | {
|
---|
415 | rcNt = STATUS_SUCCESS;
|
---|
416 | cbOut = pHdr->cbOut;
|
---|
417 | if (cbOut > pStack->Parameters.DeviceIoControl.OutputBufferLength)
|
---|
418 | {
|
---|
419 | cbOut = pStack->Parameters.DeviceIoControl.OutputBufferLength;
|
---|
420 | OSDBGPRINT(("VBoxDrvLinuxIOCtl: too much output! %#x > %#x; uCmd=%#x!\n",
|
---|
421 | pHdr->cbOut, cbOut, pStack->Parameters.DeviceIoControl.IoControlCode));
|
---|
422 | }
|
---|
423 | }
|
---|
424 | else
|
---|
425 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
426 | Log2(("VBoxDrvNtDeviceControlSlow: returns %#x cbOut=%d rc=%#x\n", rcNt, cbOut, rc));
|
---|
427 | }
|
---|
428 | else
|
---|
429 | {
|
---|
430 | Log(("VBoxDrvNtDeviceControlSlow: Mismatching sizes (%#x) - Hdr=%#lx/%#lx Irp=%#lx/%#lx!\n",
|
---|
431 | pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
432 | pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbIn : 0,
|
---|
433 | pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cbOut : 0,
|
---|
434 | pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
435 | pStack->Parameters.DeviceIoControl.OutputBufferLength));
|
---|
436 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | else
|
---|
440 | {
|
---|
441 | Log(("VBoxDrvNtDeviceControlSlow: not buffered request (%#x) - not supported\n",
|
---|
442 | pStack->Parameters.DeviceIoControl.IoControlCode));
|
---|
443 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
444 | }
|
---|
445 | }
|
---|
446 | #ifdef RT_ARCH_AMD64
|
---|
447 | else
|
---|
448 | {
|
---|
449 | Log(("VBoxDrvNtDeviceControlSlow: WOW64 req - not supported\n"));
|
---|
450 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
451 | }
|
---|
452 | #endif
|
---|
453 |
|
---|
454 | /* complete the request. */
|
---|
455 | pIrp->IoStatus.Status = rcNt;
|
---|
456 | pIrp->IoStatus.Information = cbOut;
|
---|
457 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
458 | return rcNt;
|
---|
459 | }
|
---|
460 |
|
---|
461 |
|
---|
462 | /**
|
---|
463 | * Internal Device I/O Control entry point, used for IDC.
|
---|
464 | *
|
---|
465 | * @param pDevObj Device object.
|
---|
466 | * @param pIrp Request packet.
|
---|
467 | */
|
---|
468 | NTSTATUS _stdcall VBoxDrvNtInternalDeviceControl(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
469 | {
|
---|
470 | PSUPDRVDEVEXT pDevExt = (PSUPDRVDEVEXT)pDevObj->DeviceExtension;
|
---|
471 | PIO_STACK_LOCATION pStack = IoGetCurrentIrpStackLocation(pIrp);
|
---|
472 | PFILE_OBJECT pFileObj = pStack ? pStack->FileObject : NULL;
|
---|
473 | PSUPDRVSESSION pSession = pFileObj ? (PSUPDRVSESSION)pFileObj->FsContext : NULL;
|
---|
474 | NTSTATUS rcNt;
|
---|
475 | unsigned cbOut = 0;
|
---|
476 | int rc = 0;
|
---|
477 | Log2(("VBoxDrvNtInternalDeviceControl(%p,%p): ioctl=%#x pBuf=%p cbIn=%#x cbOut=%#x pSession=%p\n",
|
---|
478 | pDevExt, pIrp, pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
479 | pIrp->AssociatedIrp.SystemBuffer, pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
480 | pStack->Parameters.DeviceIoControl.OutputBufferLength, pSession));
|
---|
481 |
|
---|
482 | /* Verify that it's a buffered CTL. */
|
---|
483 | if ((pStack->Parameters.DeviceIoControl.IoControlCode & 0x3) == METHOD_BUFFERED)
|
---|
484 | {
|
---|
485 | /* Verify the pDevExt in the session. */
|
---|
486 | if ( pStack->Parameters.DeviceIoControl.IoControlCode != SUPDRV_IDC_REQ_CONNECT
|
---|
487 | ? VALID_PTR(pSession) && pSession->pDevExt == pDevExt
|
---|
488 | : !pSession
|
---|
489 | )
|
---|
490 | {
|
---|
491 | /* Verify that the size in the request header is correct. */
|
---|
492 | PSUPDRVIDCREQHDR pHdr = (PSUPDRVIDCREQHDR)pIrp->AssociatedIrp.SystemBuffer;
|
---|
493 | if ( pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr)
|
---|
494 | && pStack->Parameters.DeviceIoControl.InputBufferLength == pHdr->cb
|
---|
495 | && pStack->Parameters.DeviceIoControl.OutputBufferLength == pHdr->cb)
|
---|
496 | {
|
---|
497 | /*
|
---|
498 | * Do the job.
|
---|
499 | */
|
---|
500 | rc = supdrvIDC(pStack->Parameters.DeviceIoControl.IoControlCode, pDevExt, pSession, pHdr);
|
---|
501 | if (!rc)
|
---|
502 | {
|
---|
503 | if (pStack->Parameters.DeviceIoControl.IoControlCode == SUPDRV_IDC_REQ_CONNECT)
|
---|
504 | pFileObj->FsContext = ((PSUPDRVIDCREQCONNECT)pHdr)->u.Out.pSession;
|
---|
505 | /** @todo Handle SUPDRV_IDC_REQ_DISCONNECT and drop the
|
---|
506 | * windows hack in the generic code. */
|
---|
507 |
|
---|
508 | rcNt = STATUS_SUCCESS;
|
---|
509 | cbOut = pHdr->cb;
|
---|
510 | }
|
---|
511 | else
|
---|
512 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
513 | Log2(("VBoxDrvNtInternalDeviceControl: returns %#x/rc=%#x\n", rcNt, rc));
|
---|
514 | }
|
---|
515 | else
|
---|
516 | {
|
---|
517 | Log(("VBoxDrvNtInternalDeviceControl: Mismatching sizes (%#x) - Hdr=%#lx Irp=%#lx/%#lx!\n",
|
---|
518 | pStack->Parameters.DeviceIoControl.IoControlCode,
|
---|
519 | pStack->Parameters.DeviceIoControl.InputBufferLength >= sizeof(*pHdr) ? pHdr->cb : 0,
|
---|
520 | pStack->Parameters.DeviceIoControl.InputBufferLength,
|
---|
521 | pStack->Parameters.DeviceIoControl.OutputBufferLength));
|
---|
522 | rcNt = STATUS_INVALID_PARAMETER;
|
---|
523 | }
|
---|
524 | }
|
---|
525 | else
|
---|
526 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
527 | }
|
---|
528 | else
|
---|
529 | {
|
---|
530 | Log(("VBoxDrvNtInternalDeviceControl: not buffered request (%#x) - not supported\n",
|
---|
531 | pStack->Parameters.DeviceIoControl.IoControlCode));
|
---|
532 | rcNt = STATUS_NOT_SUPPORTED;
|
---|
533 | }
|
---|
534 |
|
---|
535 | /* complete the request. */
|
---|
536 | pIrp->IoStatus.Status = rcNt;
|
---|
537 | pIrp->IoStatus.Information = cbOut;
|
---|
538 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
539 | return rcNt;
|
---|
540 | }
|
---|
541 |
|
---|
542 |
|
---|
543 | /**
|
---|
544 | * Stub function for functions we don't implemented.
|
---|
545 | *
|
---|
546 | * @returns STATUS_NOT_SUPPORTED
|
---|
547 | * @param pDevObj Device object.
|
---|
548 | * @param pIrp IRP.
|
---|
549 | */
|
---|
550 | NTSTATUS _stdcall VBoxDrvNtNotSupportedStub(PDEVICE_OBJECT pDevObj, PIRP pIrp)
|
---|
551 | {
|
---|
552 | Log(("VBoxDrvNtNotSupportedStub\n"));
|
---|
553 | pDevObj = pDevObj;
|
---|
554 |
|
---|
555 | pIrp->IoStatus.Information = 0;
|
---|
556 | pIrp->IoStatus.Status = STATUS_NOT_SUPPORTED;
|
---|
557 | IoCompleteRequest(pIrp, IO_NO_INCREMENT);
|
---|
558 |
|
---|
559 | return STATUS_NOT_SUPPORTED;
|
---|
560 | }
|
---|
561 |
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * ExRegisterCallback handler for power events
|
---|
565 | *
|
---|
566 | * @param pCallbackContext User supplied parameter (pDevObj)
|
---|
567 | * @param pArgument1 First argument
|
---|
568 | * @param pArgument2 Second argument
|
---|
569 | */
|
---|
570 | VOID _stdcall VBoxPowerDispatchCallback(PVOID pCallbackContext, PVOID pArgument1, PVOID pArgument2)
|
---|
571 | {
|
---|
572 | PDEVICE_OBJECT pDevObj = (PDEVICE_OBJECT)pCallbackContext;
|
---|
573 |
|
---|
574 | Log(("VBoxPowerDispatchCallback: %x %x\n", pArgument1, pArgument2));
|
---|
575 |
|
---|
576 | /* Power change imminent? */
|
---|
577 | if ((unsigned)pArgument1 == PO_CB_SYSTEM_STATE_LOCK)
|
---|
578 | {
|
---|
579 | if ((unsigned)pArgument2 == 0)
|
---|
580 | Log(("VBoxPowerDispatchCallback: about to go into suspend mode!\n"));
|
---|
581 | else
|
---|
582 | Log(("VBoxPowerDispatchCallback: resumed!\n"));
|
---|
583 |
|
---|
584 | /* Inform any clients that have registered themselves with IPRT. */
|
---|
585 | RTPowerSignalEvent(((unsigned)pArgument2 == 0) ? RTPOWEREVENT_SUSPEND : RTPOWEREVENT_RESUME);
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * Initializes any OS specific object creator fields.
|
---|
592 | */
|
---|
593 | void VBOXCALL supdrvOSObjInitCreator(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession)
|
---|
594 | {
|
---|
595 | NOREF(pObj);
|
---|
596 | NOREF(pSession);
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Checks if the session can access the object.
|
---|
602 | *
|
---|
603 | * @returns true if a decision has been made.
|
---|
604 | * @returns false if the default access policy should be applied.
|
---|
605 | *
|
---|
606 | * @param pObj The object in question.
|
---|
607 | * @param pSession The session wanting to access the object.
|
---|
608 | * @param pszObjName The object name, can be NULL.
|
---|
609 | * @param prc Where to store the result when returning true.
|
---|
610 | */
|
---|
611 | bool VBOXCALL supdrvOSObjCanAccess(PSUPDRVOBJ pObj, PSUPDRVSESSION pSession, const char *pszObjName, int *prc)
|
---|
612 | {
|
---|
613 | NOREF(pObj);
|
---|
614 | NOREF(pSession);
|
---|
615 | NOREF(pszObjName);
|
---|
616 | NOREF(prc);
|
---|
617 | return false;
|
---|
618 | }
|
---|
619 |
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Force async tsc mode (stub).
|
---|
623 | */
|
---|
624 | bool VBOXCALL supdrvOSGetForcedAsyncTscMode(PSUPDRVDEVEXT pDevExt)
|
---|
625 | {
|
---|
626 | return false;
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | #define MY_SystemLoadGdiDriverInSystemSpaceInformation 54
|
---|
631 | #define MY_SystemUnloadGdiDriverInformation 27
|
---|
632 |
|
---|
633 | typedef struct MYSYSTEMGDIDRIVERINFO
|
---|
634 | {
|
---|
635 | UNICODE_STRING Name; /**< In: image file name. */
|
---|
636 | PVOID ImageAddress; /**< Out: the load address. */
|
---|
637 | PVOID SectionPointer; /**< Out: section object. */
|
---|
638 | PVOID EntryPointer; /**< Out: entry point address. */
|
---|
639 | PVOID ExportSectionPointer; /**< Out: export directory/section. */
|
---|
640 | ULONG ImageLength; /**< Out: SizeOfImage. */
|
---|
641 | } MYSYSTEMGDIDRIVERINFO;
|
---|
642 |
|
---|
643 | extern "C" __declspec(dllimport) NTSTATUS NTAPI ZwSetSystemInformation(ULONG, PVOID, ULONG);
|
---|
644 |
|
---|
645 | int VBOXCALL supdrvOSLdrOpen(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const char *pszFilename)
|
---|
646 | {
|
---|
647 | pImage->pvNtSectionObj = NULL;
|
---|
648 | pImage->hMemLock = NIL_RTR0MEMOBJ;
|
---|
649 |
|
---|
650 | #ifdef VBOX_WITHOUT_NATIVE_R0_LOADER
|
---|
651 | # ifndef RT_ARCH_X86
|
---|
652 | # error "VBOX_WITHOUT_NATIVE_R0_LOADER is only safe on x86."
|
---|
653 | # endif
|
---|
654 | NOREF(pDevExt); NOREF(pszFilename); NOREF(pImage);
|
---|
655 | return VERR_NOT_SUPPORTED;
|
---|
656 |
|
---|
657 | #else
|
---|
658 | /*
|
---|
659 | * Convert the filename from DOS UTF-8 to NT UTF-16.
|
---|
660 | */
|
---|
661 | size_t cwcFilename;
|
---|
662 | int rc = RTStrCalcUtf16LenEx(pszFilename, RTSTR_MAX, &cwcFilename);
|
---|
663 | if (RT_FAILURE(rc))
|
---|
664 | return rc;
|
---|
665 |
|
---|
666 | PRTUTF16 pwcsFilename = (PRTUTF16)RTMemTmpAlloc((4 + cwcFilename + 1) * sizeof(RTUTF16));
|
---|
667 | if (!pwcsFilename)
|
---|
668 | return VERR_NO_TMP_MEMORY;
|
---|
669 |
|
---|
670 | pwcsFilename[0] = '\\';
|
---|
671 | pwcsFilename[1] = '?';
|
---|
672 | pwcsFilename[2] = '?';
|
---|
673 | pwcsFilename[3] = '\\';
|
---|
674 | PRTUTF16 pwcsTmp = &pwcsFilename[4];
|
---|
675 | rc = RTStrToUtf16Ex(pszFilename, RTSTR_MAX, &pwcsTmp, cwcFilename + 1, NULL);
|
---|
676 | if (RT_SUCCESS(rc))
|
---|
677 | {
|
---|
678 | /*
|
---|
679 | * Try load it.
|
---|
680 | */
|
---|
681 | MYSYSTEMGDIDRIVERINFO Info;
|
---|
682 | RtlInitUnicodeString(&Info.Name, pwcsFilename);
|
---|
683 | Info.ImageAddress = NULL;
|
---|
684 | Info.SectionPointer = NULL;
|
---|
685 | Info.EntryPointer = NULL;
|
---|
686 | Info.ExportSectionPointer = NULL;
|
---|
687 | Info.ImageLength = 0;
|
---|
688 |
|
---|
689 | NTSTATUS rcNt = ZwSetSystemInformation(MY_SystemLoadGdiDriverInSystemSpaceInformation, &Info, sizeof(Info));
|
---|
690 | if (NT_SUCCESS(rcNt))
|
---|
691 | {
|
---|
692 | pImage->pvImage = Info.ImageAddress;
|
---|
693 | pImage->pvNtSectionObj = Info.SectionPointer;
|
---|
694 | Log(("ImageAddress=%p SectionPointer=%p ImageLength=%#x cbImageBits=%#x rcNt=%#x '%ls'\n",
|
---|
695 | Info.ImageAddress, Info.SectionPointer, Info.ImageLength, pImage->cbImageBits, rcNt, Info.Name.Buffer));
|
---|
696 | # ifdef DEBUG_bird
|
---|
697 | SUPR0Printf("ImageAddress=%p SectionPointer=%p ImageLength=%#x cbImageBits=%#x rcNt=%#x '%ws'\n",
|
---|
698 | Info.ImageAddress, Info.SectionPointer, Info.ImageLength, pImage->cbImageBits, rcNt, Info.Name.Buffer);
|
---|
699 | # endif
|
---|
700 | if (pImage->cbImageBits == Info.ImageLength)
|
---|
701 | {
|
---|
702 | /*
|
---|
703 | * Lock down the entire image, just to be on the safe side.
|
---|
704 | */
|
---|
705 | rc = RTR0MemObjLockKernel(&pImage->hMemLock, pImage->pvImage, pImage->cbImageBits, RTMEM_PROT_READ);
|
---|
706 | if (RT_FAILURE(rc))
|
---|
707 | {
|
---|
708 | pImage->hMemLock = NIL_RTR0MEMOBJ;
|
---|
709 | supdrvOSLdrUnload(pDevExt, pImage);
|
---|
710 | }
|
---|
711 | }
|
---|
712 | else
|
---|
713 | {
|
---|
714 | supdrvOSLdrUnload(pDevExt, pImage);
|
---|
715 | rc = VERR_LDR_MISMATCH_NATIVE;
|
---|
716 | }
|
---|
717 | }
|
---|
718 | else
|
---|
719 | {
|
---|
720 | Log(("rcNt=%#x '%ls'\n", rcNt, pwcsFilename));
|
---|
721 | SUPR0Printf("VBoxDrv: rcNt=%x '%ws'\n", rcNt, pwcsFilename);
|
---|
722 | switch (rcNt)
|
---|
723 | {
|
---|
724 | case /* 0xc0000003 */ STATUS_INVALID_INFO_CLASS:
|
---|
725 | # ifdef RT_ARCH_AMD64
|
---|
726 | /* Unwind will crash and BSOD, so no fallback here! */
|
---|
727 | rc = VERR_NOT_IMPLEMENTED;
|
---|
728 | # else
|
---|
729 | /*
|
---|
730 | * Use the old way of loading the modules.
|
---|
731 | *
|
---|
732 | * Note! We do *NOT* try class 26 because it will probably
|
---|
733 | * not work correctly on terminal servers and such.
|
---|
734 | */
|
---|
735 | rc = VERR_NOT_SUPPORTED;
|
---|
736 | # endif
|
---|
737 | break;
|
---|
738 | case /* 0xc0000034 */ STATUS_OBJECT_NAME_NOT_FOUND:
|
---|
739 | rc = VERR_MODULE_NOT_FOUND;
|
---|
740 | break;
|
---|
741 | case /* 0xC0000263 */ STATUS_DRIVER_ENTRYPOINT_NOT_FOUND:
|
---|
742 | rc = VERR_LDR_IMPORTED_SYMBOL_NOT_FOUND;
|
---|
743 | break;
|
---|
744 | case 0xC0000428 /* STATUS_INVALID_IMAGE_HASH */ :
|
---|
745 | rc = VERR_LDR_IMAGE_HASH;
|
---|
746 | break;
|
---|
747 | case 0xC000010E /* STATUS_IMAGE_ALREADY_LOADED */ :
|
---|
748 | Log(("WARNING: see #4853 for cause of this failure on Windows 7 x64\n"));
|
---|
749 | rc = VERR_ALREADY_LOADED;
|
---|
750 | break;
|
---|
751 | default:
|
---|
752 | rc = VERR_LDR_GENERAL_FAILURE;
|
---|
753 | break;
|
---|
754 | }
|
---|
755 |
|
---|
756 | pImage->pvNtSectionObj = NULL;
|
---|
757 | }
|
---|
758 | }
|
---|
759 |
|
---|
760 | RTMemTmpFree(pwcsFilename);
|
---|
761 | NOREF(pDevExt);
|
---|
762 | return rc;
|
---|
763 | #endif
|
---|
764 | }
|
---|
765 |
|
---|
766 |
|
---|
767 | int VBOXCALL supdrvOSLdrValidatePointer(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, void *pv, const uint8_t *pbImageBits)
|
---|
768 | {
|
---|
769 | NOREF(pDevExt); NOREF(pImage); NOREF(pv); NOREF(pbImageBits);
|
---|
770 | return VINF_SUCCESS;
|
---|
771 | }
|
---|
772 |
|
---|
773 |
|
---|
774 | /**
|
---|
775 | * memcmp + log.
|
---|
776 | *
|
---|
777 | * @returns Same as memcmp.
|
---|
778 | * @param pImage The image.
|
---|
779 | * @param pbImageBits The image bits ring-3 uploads.
|
---|
780 | * @param uRva The RVA to start comparing at.
|
---|
781 | * @param cb The number of bytes to compare.
|
---|
782 | */
|
---|
783 | static int supdrvNtCompare(PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, uint32_t uRva, uint32_t cb)
|
---|
784 | {
|
---|
785 | int iDiff = memcmp((uint8_t const *)pImage->pvImage + uRva, pbImageBits + uRva, cb);
|
---|
786 | if (iDiff)
|
---|
787 | {
|
---|
788 | uint32_t cbLeft = cb;
|
---|
789 | const uint8_t *pbNativeBits = (const uint8_t *)pImage->pvImage;
|
---|
790 | for (size_t off = uRva; cbLeft > 0; off++, cbLeft--)
|
---|
791 | if (pbNativeBits[off] != pbImageBits[off])
|
---|
792 | {
|
---|
793 | char szBytes[128];
|
---|
794 | RTStrPrintf(szBytes, sizeof(szBytes), "native: %.*Rhxs our: %.*Rhxs",
|
---|
795 | RT_MIN(12, cbLeft), &pbNativeBits[off],
|
---|
796 | RT_MIN(12, cbLeft), &pbImageBits[off]);
|
---|
797 | SUPR0Printf("VBoxDrv: Mismatch at %#x of %s: %s\n", off, pImage->szName, szBytes);
|
---|
798 | break;
|
---|
799 | }
|
---|
800 | }
|
---|
801 | return iDiff;
|
---|
802 | }
|
---|
803 |
|
---|
804 | int VBOXCALL supdrvOSLdrLoad(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage, const uint8_t *pbImageBits, PSUPLDRLOAD pReq)
|
---|
805 | {
|
---|
806 | NOREF(pDevExt); NOREF(pReq);
|
---|
807 | if (pImage->pvNtSectionObj)
|
---|
808 | {
|
---|
809 | /*
|
---|
810 | * Usually, the entire image matches exactly.
|
---|
811 | */
|
---|
812 | if (!memcmp(pImage->pvImage, pbImageBits, pImage->cbImageBits))
|
---|
813 | return VINF_SUCCESS;
|
---|
814 |
|
---|
815 | /*
|
---|
816 | * However, on Windows Server 2003 (sp2 x86) both import thunk tables
|
---|
817 | * are fixed up and we typically get a mismatch in the INIT section.
|
---|
818 | *
|
---|
819 | * So, lets see if everything matches when excluding the
|
---|
820 | * OriginalFirstThunk tables. To make life simpler, set the max number
|
---|
821 | * of imports to 16 and just record and sort the locations that needs
|
---|
822 | * to be excluded from the comparison.
|
---|
823 | */
|
---|
824 | IMAGE_NT_HEADERS const *pNtHdrs;
|
---|
825 | pNtHdrs = (IMAGE_NT_HEADERS const *)(pbImageBits
|
---|
826 | + ( *(uint16_t *)pbImageBits == IMAGE_DOS_SIGNATURE
|
---|
827 | ? ((IMAGE_DOS_HEADER const *)pbImageBits)->e_lfanew
|
---|
828 | : 0));
|
---|
829 | if ( pNtHdrs->Signature == IMAGE_NT_SIGNATURE
|
---|
830 | && pNtHdrs->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR_MAGIC
|
---|
831 | && pNtHdrs->OptionalHeader.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_IMPORT
|
---|
832 | && pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size >= sizeof(IMAGE_IMPORT_DESCRIPTOR)
|
---|
833 | && pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress > sizeof(IMAGE_NT_HEADERS)
|
---|
834 | && pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress < pImage->cbImageBits
|
---|
835 | )
|
---|
836 | {
|
---|
837 | struct MyRegion
|
---|
838 | {
|
---|
839 | uint32_t uRva;
|
---|
840 | uint32_t cb;
|
---|
841 | } aExcludeRgns[16];
|
---|
842 | unsigned cExcludeRgns = 0;
|
---|
843 | uint32_t cImpsLeft = pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].Size
|
---|
844 | / sizeof(IMAGE_IMPORT_DESCRIPTOR);
|
---|
845 | IMAGE_IMPORT_DESCRIPTOR const *pImp;
|
---|
846 | pImp = (IMAGE_IMPORT_DESCRIPTOR const *)(pbImageBits
|
---|
847 | + pNtHdrs->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
|
---|
848 | while ( cImpsLeft-- > 0
|
---|
849 | && cExcludeRgns < RT_ELEMENTS(aExcludeRgns))
|
---|
850 | {
|
---|
851 | uint32_t uRvaThunk = pImp->OriginalFirstThunk;
|
---|
852 | if ( uRvaThunk > sizeof(IMAGE_NT_HEADERS)
|
---|
853 | && uRvaThunk <= pImage->cbImageBits - sizeof(IMAGE_THUNK_DATA)
|
---|
854 | && uRvaThunk != pImp->FirstThunk)
|
---|
855 | {
|
---|
856 | /* Find the size of the thunk table. */
|
---|
857 | IMAGE_THUNK_DATA const *paThunk = (IMAGE_THUNK_DATA const *)(pbImageBits + uRvaThunk);
|
---|
858 | uint32_t cMaxThunks = (pImage->cbImageBits - uRvaThunk) / sizeof(IMAGE_THUNK_DATA);
|
---|
859 | uint32_t cThunks = 0;
|
---|
860 | while (cThunks < cMaxThunks && paThunk[cThunks].u1.Function != 0)
|
---|
861 | cThunks++;
|
---|
862 |
|
---|
863 | /* Ordered table insert. */
|
---|
864 | unsigned i = 0;
|
---|
865 | for (; i < cExcludeRgns; i++)
|
---|
866 | if (uRvaThunk < aExcludeRgns[i].uRva)
|
---|
867 | break;
|
---|
868 | if (i != cExcludeRgns)
|
---|
869 | memmove(&aExcludeRgns[i + 1], &aExcludeRgns[i], (cExcludeRgns - i) * sizeof(aExcludeRgns[0]));
|
---|
870 | aExcludeRgns[i].uRva = uRvaThunk;
|
---|
871 | aExcludeRgns[i].cb = cThunks * sizeof(IMAGE_THUNK_DATA);
|
---|
872 | cExcludeRgns++;
|
---|
873 | }
|
---|
874 |
|
---|
875 | /* advance */
|
---|
876 | pImp++;
|
---|
877 | }
|
---|
878 |
|
---|
879 | /*
|
---|
880 | * Ok, do the comparison.
|
---|
881 | */
|
---|
882 | int iDiff = 0;
|
---|
883 | uint32_t uRvaNext = 0;
|
---|
884 | for (unsigned i = 0; !iDiff && i < cExcludeRgns; i++)
|
---|
885 | {
|
---|
886 | if (uRvaNext < aExcludeRgns[i].uRva)
|
---|
887 | iDiff = supdrvNtCompare(pImage, pbImageBits, uRvaNext, aExcludeRgns[i].uRva - uRvaNext);
|
---|
888 | uRvaNext = aExcludeRgns[i].uRva + aExcludeRgns[i].cb;
|
---|
889 | }
|
---|
890 | if (!iDiff && uRvaNext < pImage->cbImageBits)
|
---|
891 | iDiff = supdrvNtCompare(pImage, pbImageBits, uRvaNext, pImage->cbImageBits - uRvaNext);
|
---|
892 | if (!iDiff)
|
---|
893 | return VINF_SUCCESS;
|
---|
894 | }
|
---|
895 | else
|
---|
896 | supdrvNtCompare(pImage, pbImageBits, 0, pImage->cbImageBits);
|
---|
897 | return VERR_LDR_MISMATCH_NATIVE;
|
---|
898 | }
|
---|
899 | return VERR_INTERNAL_ERROR_4;
|
---|
900 | }
|
---|
901 |
|
---|
902 |
|
---|
903 | void VBOXCALL supdrvOSLdrUnload(PSUPDRVDEVEXT pDevExt, PSUPDRVLDRIMAGE pImage)
|
---|
904 | {
|
---|
905 | if (pImage->pvNtSectionObj)
|
---|
906 | {
|
---|
907 | if (pImage->hMemLock != NIL_RTR0MEMOBJ)
|
---|
908 | {
|
---|
909 | RTR0MemObjFree(pImage->hMemLock, false /*fFreeMappings*/);
|
---|
910 | pImage->hMemLock = NIL_RTR0MEMOBJ;
|
---|
911 | }
|
---|
912 |
|
---|
913 | NTSTATUS rcNt = ZwSetSystemInformation(MY_SystemUnloadGdiDriverInformation,
|
---|
914 | &pImage->pvNtSectionObj, sizeof(pImage->pvNtSectionObj));
|
---|
915 | if (rcNt != STATUS_SUCCESS)
|
---|
916 | SUPR0Printf("VBoxDrv: failed to unload '%s', rcNt=%#x\n", pImage->szName, rcNt);
|
---|
917 | pImage->pvNtSectionObj = NULL;
|
---|
918 | }
|
---|
919 | NOREF(pDevExt);
|
---|
920 | }
|
---|
921 |
|
---|
922 |
|
---|
923 | /**
|
---|
924 | * Converts an IPRT error code to an nt status code.
|
---|
925 | *
|
---|
926 | * @returns corresponding nt status code.
|
---|
927 | * @param rc IPRT error status code.
|
---|
928 | */
|
---|
929 | static NTSTATUS VBoxDrvNtErr2NtStatus(int rc)
|
---|
930 | {
|
---|
931 | switch (rc)
|
---|
932 | {
|
---|
933 | case VINF_SUCCESS: return STATUS_SUCCESS;
|
---|
934 | case VERR_GENERAL_FAILURE: return STATUS_NOT_SUPPORTED;
|
---|
935 | case VERR_INVALID_PARAMETER: return STATUS_INVALID_PARAMETER;
|
---|
936 | case VERR_INVALID_MAGIC: return STATUS_UNKNOWN_REVISION;
|
---|
937 | case VERR_INVALID_HANDLE: return STATUS_INVALID_HANDLE;
|
---|
938 | case VERR_INVALID_POINTER: return STATUS_INVALID_ADDRESS;
|
---|
939 | case VERR_LOCK_FAILED: return STATUS_NOT_LOCKED;
|
---|
940 | case VERR_ALREADY_LOADED: return STATUS_IMAGE_ALREADY_LOADED;
|
---|
941 | case VERR_PERMISSION_DENIED: return STATUS_ACCESS_DENIED;
|
---|
942 | case VERR_VERSION_MISMATCH: return STATUS_REVISION_MISMATCH;
|
---|
943 | }
|
---|
944 |
|
---|
945 | return STATUS_UNSUCCESSFUL;
|
---|
946 | }
|
---|
947 |
|
---|
948 |
|
---|
949 |
|
---|
950 | /** @todo use the nocrt stuff? */
|
---|
951 | int VBOXCALL mymemcmp(const void *pv1, const void *pv2, size_t cb)
|
---|
952 | {
|
---|
953 | const uint8_t *pb1 = (const uint8_t *)pv1;
|
---|
954 | const uint8_t *pb2 = (const uint8_t *)pv2;
|
---|
955 | for (; cb > 0; cb--, pb1++, pb2++)
|
---|
956 | if (*pb1 != *pb2)
|
---|
957 | return *pb1 - *pb2;
|
---|
958 | return 0;
|
---|
959 | }
|
---|
960 |
|
---|
961 |
|
---|
962 | #if 0 /* See alternative in SUPDrvA-win.asm */
|
---|
963 | /**
|
---|
964 | * Alternative version of SUPR0Printf for Windows.
|
---|
965 | *
|
---|
966 | * @returns 0.
|
---|
967 | * @param pszFormat The format string.
|
---|
968 | */
|
---|
969 | SUPR0DECL(int) SUPR0Printf(const char *pszFormat, ...)
|
---|
970 | {
|
---|
971 | va_list va;
|
---|
972 | char szMsg[512];
|
---|
973 |
|
---|
974 | va_start(va, pszFormat);
|
---|
975 | size_t cch = RTStrPrintfV(szMsg, sizeof(szMsg) - 1, pszFormat, va);
|
---|
976 | szMsg[sizeof(szMsg) - 1] = '\0';
|
---|
977 | va_end(va);
|
---|
978 |
|
---|
979 | RTLogWriteDebugger(szMsg, cch);
|
---|
980 | return 0;
|
---|
981 | }
|
---|
982 | #endif
|
---|