Exploit-DB #45436
Description: Microsoft Windows - Double Dereference in NtEnumerateKey Elevation of Privilege
Type: dos
Platform: windows
Date: 2018-09-19
Author: Google Security Research

Exploit:
Windows: Double Dereference in NtEnumerateKey Elevation of Privilege
Platform: Windows 10 1803 (not vulnerable in earlier versions)
Class: Elevation of Privilege

Summary: A number of registry system calls do not correctly handle pre-defined keys resulting in a double dereference which can lead to EoP.

Description:

The registry contains a couple of predefined keys, to generate performance information. These actually exist in the the machine hive under \Registry\Machine\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib. When these keys are opened the kernel returns a status code of STATUS_PREDEFINED_KEY, but it also returns a handle to the key. 

The kernel doesn‚Äôt allow these keys to be used for many operations such as enumeration of subkeys and values, so these system calls check before the key is used and returns STATUS_INVALID_HANDLE. The code for this in NtEnumerateKey looks like the following:

status = ObReferenceObjectByHandle(Handle, KEY_ENUMERATE, CmKeyObjectType, &Object);
if ( status >= 0 && Object->Type != 'ky02' )  {
      status = STATUS_INVALID_HANDLE;
      ObfDereferenceObject(Object); <-- Dereference object,
 }
 if (status < 0) {
   goto EXIT_LABEL;
 }

This code in itself is fine, but in 1803 at the exit label we find the following additional code:

if (Object)
    ObfDereferenceObject(Object);

This results in the object being dereferenced twice. Due the way the object reference counting works this will not be noticed until the key handle is closed, which results in a REFERENCE_BY_POINTER bugcheck being generated. This might only be a local DoS if the issue was caught earlier, but because the caller can do other things with the corrupted object we can potentially turn this into a UaF and from there elevate privileges. For example the provided PoC passes the handle to NtDuplicateObject which results in the kernel modifying a free pool allocation.

I‚Äôd recommend ensuring you check all similar functions such as NtEnumerateValueKey as there seems to be a common issue, perhaps it‚Äôs a macro or template which is generating the bad code.

The following is an example dump from a crash, at the end the !pool command is used on the object address to demonstrate the memory allocation was freed before being modified.

Use !analyze -v to get detailed debugging information.

BugCheck 18, {0, ffff8e0db3a0f7a0, 2, ffffffffffffffff}

Probably caused by : ntkrnlmp.exe ( nt!ObfDereferenceObjectWithTag+155dd9 )

Followup:     MachineOwner
---------

0: kd> !analyze -v
*******************************************************************************
*                                                                             *
*                        Bugcheck Analysis                                    *
*                                                                             *
*******************************************************************************

REFERENCE_BY_POINTER (18)
Arguments:
Arg1: [PII_REDACTED], Object type of the object whose reference count is being lowered
Arg2: ffff8e0db3a0f7a0, Object whose reference count is being lowered
Arg3: [PII_REDACTED], Reserved
Arg4: ffffffffffffffff, Reserved
	The reference count of an object is illegal for the current state of the object.
	Each time a driver uses a pointer to an object the driver calls a kernel routine
	to increment the reference count of the object. When the driver is done with the
	pointer the driver calls another kernel routine to decrement the reference count.
	Drivers must match calls to the increment and decrement routines. This bugcheck
	can occur because an object's reference count goes to zero while there are still
	open handles to the object, in which case the fourth parameter indicates the number
	of opened handles. It may also occur when the object¬ís reference count drops below zero
	whether or not there are open handles to the object, and in that case the fourth parameter
	contains the actual value of the pointer references count.

Debugging Details:
------------------


DUMP_CLASS: 1

DUMP_QUALIFIER: 401

BUILD_VERSION_STRING:  17134.1.amd64fre.rs4_release[PII_REDACTED]

SYSTEM_MANUFACTURER:  Microsoft Corporation

VIRTUAL_MACHINE:  HyperV

SYSTEM_PRODUCT_NAME:  Virtual Machine

SYSTEM_SKU:  None

SYSTEM_VERSION:  Hyper-V UEFI Release v3.0

BIOS_VENDOR:  Microsoft Corporation

BIOS_VERSION:  Hyper-V UEFI Release v3.0

BIOS_DATE:  03/02/2018

BASEBOARD_MANUFACTURER:  Microsoft Corporation

BASEBOARD_PRODUCT:  Virtual Machine

BASEBOARD_VERSION:  Hyper-V UEFI Release v3.0

DUMP_TYPE:  1

BUGCHECK_P1: 0

BUGCHECK_P2: ffff8e0db3a0f7a0

BUGCHECK_P3: 2

BUGCHECK_P4: ffffffffffffffff

CPU_COUNT: 2

CPU_MHZ: a98

CPU_VENDOR:  GenuineIntel

CPU_FAMILY: 6

CPU_MODEL: 8e

CPU_STEPPING: 9

CPU_MICROCODE: 6,8e,9,0 (F,M,S,R)  SIG: FFFFFFFF'00000000 (cache) FFFFFFFF'00000000 (init)

DEFAULT_BUCKET_ID:  WIN8_DRIVER_FAULT

BUGCHECK_STR:  0x18

PROCESS_NAME:  PoC_NtEnumerateKey_EoP.exe

CURRENT_IRQL:  0

ANALYSIS_SESSION_HOST:  DESKTOP-JA4I3EF

ANALYSIS_SESSION_TIME:  06-19-2018 13:36:38.0158

ANALYSIS_VERSION: 10.0.15063.468 amd64fre

LAST_CONTROL_TRANSFER:  from fffff80357473ab9 to fffff8035742c330

STACK_TEXT:  
ffffb78e`5a91f678 fffff803`57473ab9 : 00000000`[PII_REDACTED]`00000000 ffff8e0d`b3a0f7a0 00000000`00000002 : nt!KeBugCheckEx
ffffb78e`5a91f680 fffff803`57751b9b : 00000000`[PII_REDACTED]`[PII_REDACTED]`00000000 ffffb78e`5a91f7c0 : nt!ObfDereferenceObjectWithTag+0x155dd9
ffffb78e`5a91f6c0 fffff803`5775157d : ffffe58b`763cf580 00000a50`00000040 ffffe58b`75c75f20 00000000`00000001 : nt!ObDuplicateObject+0x58b
ffffb78e`5a91f980 fffff803`5743c943 : ffffe58b`763c4[PII_REDACTED]0`008fe098 ffffb78e`5a91fa28 00000000`00000000 : nt!NtDuplicateObject+0x12d
ffffb78e`5a91fa10 00007ffa`f3cda634 : 00000000`[PII_REDACTED]`[PII_REDACTED]`[PII_REDACTED]`000000