CVE-2021-27077: Selecting Bitmaps into Mismatched Device Contexts
A bitmap could be associated with a printer device context and then selected into a screen device context, leaving a user-mode pointer in the bitmap's dhpdev field while the display driver expected kernel memory. An attacker could craft that user-mode memory to make the display driver write a palette handle to a chosen kernel address, escalating privileges.
zerodayinitiative.com
Published on zerodayinitiative.com
To handle devices on which drawing can be performed (pixel-based devices), Windows provides so-called device contexts (DCs). A device context object encapsulates a device object where a device object represents either a screen or a printer.
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
Likely selecting a bitmap into a different device context updated only its hdev field and left the stale dhpdev pointer and hook flags from the earlier association, so the display driver used attacker-controlled user-mode memory as its own kernel structure.
Impact
An attacker could escalate privileges to create a maximally powerful process, though the described write primitive targets the multi-monitor display driver and so is limited to multi-monitor machines.
What the attacker needed
Local code execution on Windows; a printer driver installed; multi-monitor configuration for the write primitive
The fix
Microsoft released a patch for the GDI subsystem that addresses the mismatched device context handling.
How it was found
- Create a printer-related device context and a screen-related device context.
- Create a bitmap compatible with the printer device context.
- Associate the bitmap with the printer's hdev using EngAssociateSurface with the text-out hook flag set.
- Select the bitmap into the screen device context, which changes hdev but not dhpdev.
- Call the wide text-out function on the screen device context so the display driver uses the fake dhpdev block.
- Repeat the write to set both the present and enabled privilege bitfields, then create a privileged token.
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.
Windows drawing code keeps a small record for each bitmap that says which device it belongs to. The bug let a program attach a bitmap to a printer and then move it to a screen without clearing that printer information, so the screen driver trusted a pointer that actually pointed at memory the attacker controls. By shaping that memory, the attacker could make the kernel write to a place of their choosing and gain higher 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.