From CreateFile to NtCreateFile: Following a Windows System Call into the Kernel

Pavel Yosifovich

Author

Pavel Yosifovich has 25+ years as Software developer, trainer, consultant, author, and speaker. Co-author of “Windows Internals”. Author of “Windows Kernel Programming”, “Windows 10 System Programming, as well as System and kernel programming courses and “Windows Internals” series.

Pavel Yosifovich, co-author of “Windows Internals 7th Edition, Part 1” and author of “Windows Native API Programming”, walks through one of the most common things any Windows program does: opening a file. Not the API documentation version of it, but the actual path, instruction by instruction, from a click in Notepad to the kernel function that does the work. This post is for developers, security researchers and reverse engineers who know that “the API calls into the kernel” and want to see exactly how that happens, and how to follow it themselves in a kernel debugger.

The Path in One Picture

Let’s start with the big picture, then dig into it with debuggers. A process such as Notepad calls CreateFile, the documented Windows API for opening (or creating) a file and getting a handle back. To be more precise, there is no function called CreateFile at all: it is a macro, and the real function is CreateFileW, implemented in kernelbase.dll (kernel32.dll forwards to it). CreateFileW does some checks and parameter massaging, then calls NtCreateFile in ntdll.dll.

The ntdll version of NtCreateFile does almost nothing by itself. Its job is to make the system call: on x64 it puts a number into the EAX register, the system service number, and executes the syscall machine instruction. That is the jump into the kernel. It is still the same thread, but now the processor is in kernel mode, and it lands in a specific function called the system service dispatcher. The dispatcher finds and calls the real NtCreateFile in the kernel, which has exactly the same parameters as the ntdll one but provides the actual implementation. From there, the kernel locates the right driver, NTFS or whatever file system or device is involved, and eventually the result travels back to user mode.

That is the theory. Now let’s see it.

The Setup: Two Debuggers and the Old Notepad

The demo uses a virtual machine with a kernel debugger attached from the host (if you have not done that before, Microsoft documents setting up network kernel debugging), plus a user mode debugger running inside the VM.

The process is the old Notepad, not the new Store one. The reason is simple: Microsoft publishes symbols for the old Notepad, and the new one has none, which makes call stacks noticeably less pleasant to read. Not a big deal, but if you want clean stacks, the old one is the better target.

Catching CreateFileW in User Mode

With Notepad running under the user mode debugger, a breakpoint on kernelbase!CreateFileW fires almost immediately, before you even get to pick a file in the Open dialog. Notepad (and the shell code it hosts) opens files all the time, icon caches and things like that.

This is where knowing the x64 calling convention pays off. The first four integer or pointer arguments are passed in RCX, RDX, R8 and R9, and the first argument of CreateFileW is the file name. So du @rcx shows the Unicode string being opened. Disable the breakpoint, let Notepad reach the Open dialog, re-enable it, pick a file (hello.txt in the demo), and the breakpoint hits again with the expected name in RCX. Nothing was wrong with the icon cache, by the way: any file would do, a file you chose yourself is just easier to recognize.

NtCreateFile in ntdll: EAX and syscall

Next, a breakpoint on ntdll!NtCreateFile. The k command shows the expected chain: CreateFileW, then an internal helper (CreateFileInternal), then NtCreateFile. Disassembling the first few instructions shows exactly the pattern described above: the value 0x55 is moved into EAX, followed by syscall.

So 0x55 is the system service number of NtCreateFile on this system. On this system, because these numbers are not stable. They sometimes change between Windows builds, which is exactly why ntdll exists as the official gateway and why hardcoding syscall numbers is fragile (and, incidentally, why offensive tooling that does direct syscalls has to resolve them at runtime).

Before stepping over syscall, the kernel side needs to be prepared.

Finding the System Service Dispatcher

Break into the VM from the kernel debugger, and first find the Notepad process. System calls happen constantly, from every process on the machine, so any breakpoint in this path without a filter is hopelessly noisy. The bp command in kernel mode accepts a /p switch with an EPROCESS address, so the breakpoint fires only in the process of interest.

Where does syscall land? The target address is stored in a machine-specific register, the LSTAR MSR, number 0xC0000082. That is how the syscall instruction works: the CPU loads RIP from that register. Reading it with rdmsr c0000082 gives an address, and pasting it into the disassembly window shows nt!KiSystemCall64, the system service dispatcher on 64-bit Windows.

The first instruction there is swapgs. It exchanges the GS base, so that instead of pointing to the current thread’s TEB (a user mode structure), GS Base now points to the processor’s KPCR, the kernel’s per-processor control region. Until that instruction runs, the processor is technically in kernel mode but still with a user mode GS. Putting a breakpoint right there is a bad idea: the debugger’s own handling would run with the wrong GS, read garbage instead of the KPCR, and in practice the target tends to freeze or hang. So the breakpoint goes a few instructions later.

Narrowing the Breakpoint: /p, Conditions, and Patience

Even a process-specific breakpoint is not specific enough here, since Notepad itself makes plenty of other system calls. One option is a conditional breakpoint: break only if EAX equals 0x55. It works, but it is slow. The condition is evaluated by the debugger on the host, so every single system call that passes through that address causes a round trip to the debugger to check it. In severe cases that can even hang the debugger. In the demo it took a little while, but it worked: breakpoint hit, the current process is Notepad, and EAX is still 0x55.

The call stack at that point is not great. The stack is in a state of flux this early in the dispatcher, so the debugger has trouble making sense of it, and reloading user symbols does not help. Moving the breakpoint further down, to nt!KiSystemServiceStart, gives a full and readable call stack: the user mode frames, the ntdll NtCreateFile frame, and now the kernel side. A practical tip from the live demo: disable or clear the earlier breakpoints as you go. Leftover breakpoints, especially conditional ones in hot paths, keep firing in unrelated processes and system calls, and it is easy to lose the thread you were following. I did, more than once.

KiSystemServiceRepeat and the Service Tables

Stepping on from there quickly reaches nt!KiSystemServiceRepeat, which starts by loading the two system service descriptor tables into R10 and R11: KeServiceDescriptorTable, used for regular threads, and KeServiceDescriptorTableShadow, used for GUI threads. The shadow one is bigger because it also covers the GUI system calls (the user32 and gdi32 native APIs implemented in win32k.sys). NtCreateFile is available to both kinds of threads, so either table leads to it.

Following R10 leads to the system service table itself, nt!KiServiceTable, and dumping that symbol shows the same values. Each entry is four bytes, one per system call: entry 0 is system call 0, entry 1 is system call 1, and so on.

Decoding KiServiceTable by Hand

Here is the interesting part. These are 32-bit values on a 64-bit system, so they cannot be pointers. Each entry is an offset relative to the start of the table, shifted left by four bits. Take the value, drop the last hex digit (the low four bits), and add the rest to the address of KiServiceTable, and you get the address of the real function.

For entry 0, that lands on nt!NtAccessCheck, so system call 0 is NtAccessCheck. For NtCreateFile, read the entry at KiServiceTable + 0x55 * 4 with dd, apply the same calculation, and the disassembly shows nt!NtCreateFile. A breakpoint there, restricted to Notepad with /p, eventually fires with the full picture on the stack: NtCreateFile in the kernel on top, called from nt!KiSystemServiceCopyEnd, with the ntdll NtCreateFile and CreateFileW frames further down.

KiSystemServiceCopyEnd is the place where the arguments have been copied and the actual call happens. On a kernel without mitigations you would expect a plain indirect call through a register. On current systems the call goes through a mitigation wrapper, part of the changes made after the 2018 speculative execution vulnerabilities (Meltdown and Spectre), but it is still this function that makes the call. After that, NtCreateFile goes on to talk to the file system driver, which is a story for another time.

The Low Four Bits: Stack Arguments

So what about that last hex digit we dropped? It is the number of parameters passed on the stack. For NtAccessCheck it is 4. For NtCreateFile it is 7. On x64 the first four arguments are passed in registers, so NtCreateFile takes 4 + 7 = 11 parameters, which matches the documentation (the kernel side is documented in the WDK as ZwCreateFile, with the same parameters; Microsoft explains the Nt and Zw naming separately). Count them yourself if you like, it is 11.

Why does the dispatcher care? Because it is generic. It does not know which function it is calling, it just calls something, and it has to know how many arguments to copy from the user mode stack. An entry with 0 in those bits means the function takes four parameters or fewer. Four bits also means a maximum of 15 stack arguments, so a system call can have at most 19 parameters in total. That is plenty. I have seen system calls with 14 parameters, which I already consider too many.

The scheme is a neat trick: 64-bit addresses squeezed into 32-bit entries by storing offsets from the table start, with the argument count tucked into the bits that would otherwise be wasted.

What This Means Practically

  • CreateFile is a macro; CreateFileW in kernelbase.dll is the real function, and it calls NtCreateFile in ntdll.dll, which only sets up EAX and executes syscall.
  • System service numbers (0x55 for NtCreateFile on the demo build) change between Windows builds. Never hardcode them.
  • The syscall target is in the LSTAR MSR (0xC0000082); rdmsr in the kernel debugger shows it points to KiSystemCall64.
  • Do not set a breakpoint on the swapgs at the start of KiSystemCall64; set it a few instructions later or at KiSystemServiceStart.
  • Use bp /p to restrict kernel breakpoints to one process. Conditional breakpoints in hot paths work, but they are slow because the host evaluates every hit.
  • KiServiceTable entries are offsets from the table start shifted left by four; the low four bits are the number of stack arguments.
  • Clean up breakpoints as you go. In system call paths, stale breakpoints will put you in the wrong process very quickly.

Keep Learning

If you want to follow paths like this one on your own, these TrainSec courses go deeper:

Related reading in the knowledge library: What is the OBJECT_ATTRIBUTES structure and how is it used?, the structure NtCreateFile uses to name the file it opens, Basic Reversing of a Windows Kernel Driver, another walk through kernel code with a debugger, and Creating a Process: Windows vs. Linux, for how Windows and Linux differ at the API and system call level.


blue depth
Pavel Yosifovich

About the author

Pavel Yosifovich has 25+ years as Software developer, trainer, consultant, author, and speaker. Co-author of “Windows Internals”. Author of “Windows Kernel Programming”, “Windows 10 System Programming, as well as System and kernel programming courses and “Windows Internals” series.