Skip to content
Memory corruption Write-up Advanced

Escalating a Windows driver registry bug to a kernel write primitive

A Windows driver reads registry values without specifying their types, allowing a low-privileged user to cause a type confusion. By placing crafted values in writable keys within trusted system hives, an attacker can overwrite stack memory, including a function pointer, leading to a kernel write primitive and potential local privilege escalation.

blog.trailofbits.com

Published on blog.trailofbits.com

We recently added a C/C++ security checklist to the Testing Handbook and challenged readers to spot the bugs in two code samples: a deceptively simple Linux ping program and a Windows driver registry handler. If you found the inetntoa global buffer gotcha or the missing RTLQUERYREGISTRYTYPECHECK flag, nice work.

The author, on blog.trailofbits.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The driver uses RtlQueryRegistryValues in direct mode without the RTL_QUERY_REGISTRY_TYPECHECK flag, so it does not validate the registry value type against the expected type, leading to type confusion.

What the attacker needed

Ability to call the driver's request handler; writable registry key in a trusted hive; low-privileged user account

The fix

The driver should set the RTL_QUERY_REGISTRY_TYPECHECK flag and specify the expected registry value type when using RTL_QUERY_REGISTRY_DIRECT.

How it was found

  1. Create a registry key under HKEY_CURRENT_USER with a REG_QWORD value named MajorVersion and pass its path to the driver, causing a bugcheck due to missing type check.
  2. Find writable keys in trusted system hives (e.g., using Get-AccessibleKey) that allow low-integrity processes to set values.
  3. Place a REG_QWORD value named MajorVersion in a writable trusted key to overwrite four bytes past readValue on the stack.
  4. Set MajorVersion to a negative value (e.g., 0xFFFFFFF4) and MinorVersion to a REG_BINARY value to control the length and content of a stack overwrite.
  5. Overwrite the handlerCallback function pointer on the stack with controlled data, leading to potential kernel LPE.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

A Windows driver reads version numbers from the registry but doesn't check what type of data it gets. An attacker can put a different type of data in a place they control, causing the driver to misinterpret it and write past a small buffer on the stack. This can corrupt memory and potentially let the attacker run code with high privileges.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.