
A PoC for CVE-2018-7249
An issue was discovered in secdrv.sys as shipped in Microsoft Windows Vista, Windows 7, Windows 8, and Windows 8.1 before KB3086255, and as shipped in Macrovision SafeDisc. Two carefully timed calls to IOCTL 0xCA002813 can cause a race condition that leads to a use-after-free. When exploited, an unprivileged attacker can run arbitrary code in the kernel.
The vulnerability was reported to Microsoft, and since it does not affect an up-to-date Windows machine (only version prior to KB3086255), they will not take any action. Was tested and exploited successfully on Windows 7 x86.
Also related to CVE-2018-7250.

This documents my little research about the secdrv.sys driver. All the described behaviors of the driver were reversed engineered and might be incorrect / inaccurate.
Offset 0x4 of the input buffer to the IOCTL (0x0CA002813) contains a number that I will refer to as the TYPE. The main handler function of this IOCTL (0x0CA002813), sub_11A88 receives 3 different types: 0x96, 0x97 and 0x98.
After IOCTL type 0x96 allocated a new chunk and initialized it, but not fully, it copies the chunk to usermode. 16 bits in the newly allocated chunk were not initialized, and contain data from previous PagedPool allocations. The uninitialized bits are then copies to usermode at .text:00011BE9 by the REP MOVSD instruction. PoC code here.
When IOCTL type 0x97 is called, it finds the needed chunk, that was previously allocated with type 0x96, by its tag. If the allocation was already freed by IOCTL type 0x97, DeviceIoControl returns an error. The vulnerability here is, that the allocation used by type 0x97, can be freed DURING its operation (since no synchronization mechanisms are used) thus being used-after-freed if the race is won. If an attacker manages to free the chunk during the operation of IOCTL type 0x97 (using type 0x98), and allocate a new chunk, controlled by him, in the exact same memory location, he can override a pointer to another structure, that contains a function pointer that can be used to finally hijack the driver's execution flow and execute arbitrary code in ring 0. Because the encryption routine is done on a user supplied buffer, which can be huge in size, the encryption can take a long time to execute, thus providing a perfect time window for IOCTL type 0x98 to free the chunk while still in use. The time windows can be so long (more than 1 second!), that the race can be won reliably on the first attempt. The use-after-free starts at .text:00011B68, and the actual call, that will be hijacked to jump to the shellcode, happens at .text:00011B86.
The steps taken to successfully exploit this vulnerability are as following:
OS: Windows 7 Kernel Version 7600 MP (1 procs) Free x86 compatible Built by: 7600.16385.x86fre.win7_rtm.090713-1255 VM: 4GB RAM, 1 CPU Hardware: Windows 10 Pro 64 bit, Motherboard Gigabyte Z370 HD3, 16GB RAM, Intel i5-8400 2.80GHz (6 CPUs)