Skip to main content
All work
Case Study // Windows kernel

The Kernel Driver Guide

A Windows kernel driver and the program that controls it, written with Oskar Stefanski in early 2024, plus a guide to building and loading your own driver.

Role
Co-author
With
Oskar Stefanski
When
January – February 2024
Stack
C, C++, Windows Driver Kit, Dear ImGui

What it is

It has three parts: a kernel-mode driver, a user-mode controller and a written guide. The driver exposes five control codes that let the controller find a target process and read or write its memory. The guide walks through the toolchain (Visual Studio, the Windows Driver Kit and DebugView), creating the driver project, and loading an unsigned driver in test-signing mode.

The repository demonstrates the driver against the open-source game AssaultCube, and its README pitches it as a starting point for game mods and cheats. This page is about something else: how a request gets from a normal program into a driver and back, and what the driver should have checked on the way.

My part

We built the driver together, and both our names are in its source header. I wrote most of the guide, the install script and the diagram of the control requests, and worked on the driver's entry point and the image-load code. Oskar wrote most of the request handler and the user-mode controller. On the main branch, 55 commits are from my account and 20 from his.

How a request reaches the driver

The controller never touches another process directly. It opens the driver's device, describes what it wants in a small struct, and hands that to DeviceIoControl. The I/O manager turns the call into an I/O request packet and routes it to the driver's dispatch routine, which does the work in kernel mode and completes the request.

Every control code uses METHOD_BUFFERED, so the request is copied into a kernel buffer before the driver sees it. This is the part of the dispatch routine that handles reads, exactly as it is on the main branch (the comments are in Danish):

KernelReadWriteDriver/Communication.c, lines 112–130View on GitHub
    else if (ControlCode == IO_READ_REQUEST)
    {
        // Henter input-data til læseanmodningen.
        PKERNEL_READ_REQUEST ReadInput = (PKERNEL_READ_REQUEST)Irp->AssociatedIrp.SystemBuffer;
        PEPROCESS Process;

        // Slår processen op baseret på dens id.
        if (NT_SUCCESS(PsLookupProcessByProcessId(ReadInput->ProcessId, &Process)))
        {
            // Læser virtuel hukommelse fra processen.
            KernelReadVirtualMemory(Process, ReadInput->Address, ReadInput->pBuff, ReadInput->Size);

            // Opdaterer statussen til succes.
            CurrentStatus = STATUS_SUCCESS;

            // Opdaterer antallet af bytes læst/skrevet.
            ByteIO = sizeof(KERNEL_READ_REQUEST);
        }
    }

Those lines are the whole idea, and they also show where the driver trusts its caller too much:

  • The request is read from SystemBuffer without checking InputBufferLength, so a short buffer would be read past its end.
  • PsLookupProcessByProcessId takes a reference on the process that is never released.
  • pBuff is the caller's own pointer, and the result of the copy is discarded, so the request reports success even when the copy fails.

Decisions

METHOD_BUFFERED for every control code

The I/O manager copies the request into kernel memory before the driver sees it, so the driver never reads the request struct from user memory. It is the simplest transfer method to reason about for small, fixed-size requests.

Trade-offEach request is copied twice. That does not matter at this size, but it is why large transfers use direct I/O instead.

Find the target from the kernel

An image-load callback sees every executable as it is mapped, so the driver learns the target's process ID and base address the moment it starts, instead of the controller searching for it from user mode.

Trade-offThe target's file name is compiled into the driver and matched with a substring search.

Separate projects for driver and controller

The driver and the controller are separate projects in one solution, each with its own copy of the control codes and request structs, so either side can be read on its own.

Trade-offThe definitions are duplicated by hand, and nothing checks that the two copies stay identical.

Document the toolchain next to the code

Most of the difficulty in a first driver is the environment, not the C. The guide covers installing the WDK, creating the project, loading the driver and reading its debug output.

What I'd change

The problems above are still on the main branch. In April 2026 I fixed part of them on a separate branch, experimental/modernize-and-cleanup: it releases the process references, fixes a pointer-type mismatch in the memory routines, and makes the memory copy treat the caller's addresses as user-mode addresses. Still open:

  • Check InputBufferLength and OutputBufferLength before touching SystemBuffer, in every request handler.
  • Return the real status of the copy instead of reporting success.
  • Stop keeping a pointer into SystemBuffer after the request completes. The handler for the image-name request stores one, and the I/O manager frees that buffer on completion.
  • Create the device with IoCreateDeviceSecure so only administrators can open it.

Cryvanta's endpoint sensor runs entirely in user mode, with no kernel driver, so a bug in it can crash the sensor but not the computer.