Why does the following pure C kernel driver code bluescreen me: HANDLE hThread = NULL;…
- User
Why does the following pure C kernel driver code bluescreen me: HANDLE hThread = NULL; OBJECT_ATTRIBUTES objectAttributes; InitializeObjectAttributes(&objectAttributes, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
NTSTATUS statusDriverLoopThread = PsCreateSystemThread( &hThread, 0, &objectAttributes, NULL, NULL, (PKSTART_ROUTINE)DriverLoop, // C-style cast NULL );whereas calling the function directly via DriverLoop() does not? This is inside a a manual mapped driver
Assistant
The main reason your code is causing a blue screen when creating a system thread to call DriverLoop, but not when calling DriverLoop directly, is likely due to differences in the execution context.
Some key points:
-
When you create a system thread, it executes in kernel mode but in an arbitrary thread context. Things like the IRQL level may be different. The kernel thread also has its own stack and register state that may not be set up properly for calling your DriverLoop function.
-
When you directly call DriverLoop from your driver's initialization code, it is executing on the driver's original thread context. The IRQL, stack, registers, etc. are already configured appropriately for the driver.
So in summary, DriverLoop may be making assumptions about the execution context that are true when called directly from initialization code, but not true when called from an arbitrarily created system thread. This could result in crashes or undefined behavior.
To fix it, DriverLoop needs to either be made fully reentrant and thread-safe, or you need to better control the context when creating the thread (set IRQL, increase stack size, provide known good register state, etc.). Calling kernel APIs directly from a non-hardened thread context is often a blue screen recipe.
-
- User
I map my driver using KDMapper by TheCruZ. Does that change anything?
Assistant
Yes, using a mapper like KDMapper to load your driver does change the context compared to a natively loaded driver, and could explain some of the issues you are seeing.
Some key differences with mapped drivers:
-
They are loaded in an arbitrary process context rather than from the native driver load process. Things like IRQL level, register state, stack pointers, etc. may be in an invalid state for calling kernel functions.
-
They usually run with very limited stack space, often only a single page. This can easily lead to stack overflows when calling into the kernel.
-
They bypass normal driver entry point initialization, so you lose those guarantees about execution order, state setup, etc.
-
They have to manually get the addresses of any kernel functions they want to call, which can be error prone.
So in summary - a mapped driver has very little control over its context when it first runs and calls into the kernel. It's running essentially arbitrary code in a limited user mode process context.
This means you have to be very careful about things like:
- Setting the IRQL before kernel calls
- Expanding the stack
- Restoring expected register state
- Ensuring kernel addresses are valid
So differences in context when creating threads or calling functions directly likely explain the crashes you see. More context hardening is needed in a mapped driver.
-
- User
tried modifying it and using workitems, but I get unnamed prototyped parameters not allowed when body is present now when having the following code: extern VOID NTAPI DriverLoop(VOID*);
VOID NTAPI DriverLoop(VOID*) {
Assistant
The issue with getting the "unnamed prototyped parameters not allowed when body is present" error is because you have both a function prototype and definition for DriverLoop in the same compilation unit.
When you just declare a function prototype like:
extern VOID NTAPI DriverLoop(VOID*);It allows the parameter to be unnamed since there is no function body present. The compiler just knows to expect some parameter.
But then when you actually define the function like:
VOID NTAPI DriverLoop(VOID*) { // ... }Now the compiler requires the parameter to be named since there is a concrete function body implementation.
To fix this, you just need to split the prototype and definition into separate files or compilation units.
For example:
In DriverLoop.h:
extern VOID NTAPI DriverLoop(VOID*);In DriverLoop.c:
VOID NTAPI DriverLoop(VOID* context) { // Implementation here }By separating the two, the compiler will not complain about mixing an unnamed parameter in the prototype vs named parameter in the definition.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.