Server 2025 WHQL tests enables "verifier.exe /onecheck /rc 33 36" on some drivers
under test, which will detect a violation if drivers attempt to access absolute
registry paths.
IoOpenDriverRegistryKey will open the parameters key for a driver, but its not
defined for Server 2016. Use MmGetSystemRoutineAddress to dynamically find the
function so that a single binary can be used on Server 2016 and Server 2025.
Signed-off-by: Owen Smith <owen.smith@cloud.com>
IN PUNICODE_STRING RegistryPath
)
{
- HANDLE ServiceKey;
HANDLE ParametersKey;
ULONG Index;
NTSTATUS status;
MONTH,
YEAR);
- status = RegistryInitialize(RegistryPath);
+ status = RegistryInitialize(DriverObject, RegistryPath);
if (!NT_SUCCESS(status))
goto fail1;
- status = RegistryOpenServiceKey(KEY_ALL_ACCESS, &ServiceKey);
+ status = RegistryOpenParametersKey(KEY_READ, &ParametersKey);
if (!NT_SUCCESS(status))
goto fail2;
- status = RegistryOpenSubKey(ServiceKey,
- "Parameters",
- KEY_READ,
- &ParametersKey);
- if (!NT_SUCCESS(status))
- goto fail3;
-
__DriverSetParametersKey(ParametersKey);
- RegistryCloseKey(ServiceKey);
-
DriverObject->DriverExtension->AddDevice = AddDevice;
for (Index = 0; Index <= IRP_MJ_MAXIMUM_FUNCTION; Index++) {
return STATUS_SUCCESS;
-fail3:
- Error("fail3\n");
-
- RegistryCloseKey(ServiceKey);
-
fail2:
Error("fail2\n");
#define REGISTRY_TAG 'GERX'
+static PDRIVER_OBJECT RegistryDriverObject;
static UNICODE_STRING RegistryPath;
+typedef NTSTATUS(*IOOPENDRIVERREGISTRYKEY)(PDRIVER_OBJECT, DRIVER_REGKEY_TYPE, ACCESS_MASK, ULONG, PHANDLE);
+
+static IOOPENDRIVERREGISTRYKEY __IoOpenDriverRegistryKey;
+
static FORCEINLINE PVOID
__RegistryAllocate(
IN ULONG Length
NTSTATUS
RegistryInitialize(
- IN PUNICODE_STRING Path
+ IN PDRIVER_OBJECT DriverObject,
+ IN PUNICODE_STRING Path
)
{
+ UNICODE_STRING Unicode;
+ PVOID Func;
NTSTATUS status;
ASSERT3P(RegistryPath.Buffer, ==, NULL);
if (!NT_SUCCESS(status))
goto fail1;
+ ASSERT3P(RegistryDriverObject, ==, NULL);
+ RegistryDriverObject = DriverObject;
+
+ ASSERT3P(__IoOpenDriverRegistryKey, ==, NULL);
+ RtlInitUnicodeString(&Unicode, L"IoOpenDriverRegistryKey");
+
+ Func = MmGetSystemRoutineAddress(&Unicode);
+ if (Func != NULL)
+ __IoOpenDriverRegistryKey = (IOOPENDRIVERREGISTRYKEY)Func;
+
return STATUS_SUCCESS;
fail1:
VOID
)
{
+ __IoOpenDriverRegistryKey = NULL;
+
+ RegistryDriverObject = NULL;
+
RtlFreeUnicodeString(&RegistryPath);
RegistryPath.Buffer = NULL;
RegistryPath.MaximumLength = RegistryPath.Length = 0;
return RegistryCreateKey(NULL, &RegistryPath, REG_OPTION_NON_VOLATILE, Key);
}
+NTSTATUS
+RegistryOpenParametersKey(
+ IN ACCESS_MASK DesiredAccess,
+ OUT PHANDLE Key
+ )
+{
+ HANDLE ServiceKey;
+ NTSTATUS status;
+
+ if (__IoOpenDriverRegistryKey != NULL) {
+ status = __IoOpenDriverRegistryKey(RegistryDriverObject,
+ DriverRegKeyParameters,
+ DesiredAccess,
+ 0,
+ Key);
+ if (!NT_SUCCESS(status))
+ goto fail1;
+
+ goto done;
+ }
+
+ status = RegistryOpenKey(NULL, &RegistryPath, DesiredAccess, &ServiceKey);
+ if (!NT_SUCCESS(status))
+ goto fail2;
+
+ status = RegistryOpenSubKey(ServiceKey, "Parameters", DesiredAccess, Key);
+ if (!NT_SUCCESS(status))
+ goto fail3;
+
+ RegistryCloseKey(ServiceKey);
+
+done:
+ return STATUS_SUCCESS;
+
+fail3:
+ Error("fail3\n");
+
+ RegistryCloseKey(ServiceKey);
+
+fail2:
+ Error("fail2\n");
+
+fail1:
+ Error("fail1 %08x\n", status);
+
+ return status;
+}
+
NTSTATUS
RegistryOpenSoftwareKey(
IN PDEVICE_OBJECT DeviceObject,
extern NTSTATUS
RegistryInitialize(
- IN PUNICODE_STRING Path
+ IN PDRIVER_OBJECT DriverObject,
+ IN PUNICODE_STRING Path
);
extern VOID
OUT PHANDLE Key
);
+extern NTSTATUS
+RegistryOpenParametersKey(
+ IN ACCESS_MASK DesiredAccess,
+ OUT PHANDLE Key
+ );
+
extern NTSTATUS
RegistryOpenSoftwareKey(
IN PDEVICE_OBJECT DeviceObject,
IN PUNICODE_STRING RegistryPath
)
{
- HANDLE ServiceKey;
HANDLE ParametersKey;
NTSTATUS status;
MONTH,
YEAR);
- status = RegistryInitialize(RegistryPath);
+ status = RegistryInitialize(DriverObject, RegistryPath);
if (!NT_SUCCESS(status))
goto fail1;
- status = RegistryOpenServiceKey(KEY_ALL_ACCESS, &ServiceKey);
+ status = RegistryOpenParametersKey(KEY_READ, &ParametersKey);
if (!NT_SUCCESS(status))
goto fail2;
- status = RegistryOpenSubKey(ServiceKey,
- "Parameters",
- KEY_READ,
- &ParametersKey);
- if (!NT_SUCCESS(status))
- goto fail3;
-
Driver.ParametersKey = ParametersKey;
Driver.Adapter = NULL;
status = AdapterDriverEntry(RegistryPath,
DriverObject);
if (!NT_SUCCESS(status))
- goto fail4;
+ goto fail3;
Driver.StorPortDispatchPnp = DriverObject->MajorFunction[IRP_MJ_PNP];
Driver.StorPortDispatchPower = DriverObject->MajorFunction[IRP_MJ_POWER];
DriverObject->MajorFunction[IRP_MJ_POWER] = DispatchPower;
DriverObject->DriverUnload = DriverUnload;
- RegistryCloseKey(ServiceKey);
- ServiceKey = NULL;
-
return STATUS_SUCCESS;
-fail4:
- Error("fail4\n");
-
- RegistryCloseKey(Driver.ParametersKey);
- Driver.ParametersKey = NULL;
-
fail3:
Error("fail3\n");
- RegistryCloseKey(ServiceKey);
- ServiceKey = NULL;
+ RegistryCloseKey(Driver.ParametersKey);
+ Driver.ParametersKey = NULL;
fail2:
Error("fail2\n");
#define REGISTRY_TAG 'GERX'
+static PDRIVER_OBJECT RegistryDriverObject;
static UNICODE_STRING RegistryPath;
+typedef NTSTATUS(*IOOPENDRIVERREGISTRYKEY)(PDRIVER_OBJECT, DRIVER_REGKEY_TYPE, ACCESS_MASK, ULONG, PHANDLE);
+
+static IOOPENDRIVERREGISTRYKEY __IoOpenDriverRegistryKey;
+
static FORCEINLINE PVOID
__RegistryAllocate(
IN ULONG Length
NTSTATUS
RegistryInitialize(
- IN PUNICODE_STRING Path
+ IN PDRIVER_OBJECT DriverObject,
+ IN PUNICODE_STRING Path
)
{
+ UNICODE_STRING Unicode;
+ PVOID Func;
NTSTATUS status;
ASSERT3P(RegistryPath.Buffer, ==, NULL);
if (!NT_SUCCESS(status))
goto fail1;
+ ASSERT3P(RegistryDriverObject, ==, NULL);
+ RegistryDriverObject = DriverObject;
+
+ ASSERT3P(__IoOpenDriverRegistryKey, ==, NULL);
+ RtlInitUnicodeString(&Unicode, L"IoOpenDriverRegistryKey");
+
+ Func = MmGetSystemRoutineAddress(&Unicode);
+ if (Func != NULL)
+ __IoOpenDriverRegistryKey = (IOOPENDRIVERREGISTRYKEY)Func;
+
return STATUS_SUCCESS;
fail1:
VOID
)
{
+ __IoOpenDriverRegistryKey = NULL;
+
+ RegistryDriverObject = NULL;
+
RtlFreeUnicodeString(&RegistryPath);
RegistryPath.Buffer = NULL;
RegistryPath.MaximumLength = RegistryPath.Length = 0;
return RegistryCreateKey(NULL, &RegistryPath, REG_OPTION_NON_VOLATILE, Key);
}
+__drv_requiresIRQL(PASSIVE_LEVEL)
+NTSTATUS
+RegistryOpenParametersKey(
+ IN ACCESS_MASK DesiredAccess,
+ OUT PHANDLE Key
+ )
+{
+ HANDLE ServiceKey;
+ NTSTATUS status;
+
+ if (__IoOpenDriverRegistryKey != NULL) {
+ status = __IoOpenDriverRegistryKey(RegistryDriverObject,
+ DriverRegKeyParameters,
+ DesiredAccess,
+ 0,
+ Key);
+ if (!NT_SUCCESS(status))
+ goto fail1;
+
+ goto done;
+ }
+
+ status = RegistryOpenKey(NULL, &RegistryPath, DesiredAccess, &ServiceKey);
+ if (!NT_SUCCESS(status))
+ goto fail2;
+
+ status = RegistryOpenSubKey(ServiceKey, "Parameters", DesiredAccess, Key);
+ if (!NT_SUCCESS(status))
+ goto fail3;
+
+ RegistryCloseKey(ServiceKey);
+
+done:
+ return STATUS_SUCCESS;
+
+fail3:
+ Error("fail3\n");
+
+ RegistryCloseKey(ServiceKey);
+
+fail2:
+ Error("fail2\n");
+
+fail1:
+ Error("fail1 %08x\n", status);
+
+ return status;
+}
+
__drv_requiresIRQL(PASSIVE_LEVEL)
NTSTATUS
RegistryOpenSoftwareKey(
extern NTSTATUS
RegistryInitialize(
- IN PUNICODE_STRING Path
+ IN PDRIVER_OBJECT DriverObject,
+ IN PUNICODE_STRING Path
);
extern VOID
OUT PHANDLE Key
);
+__drv_requiresIRQL(PASSIVE_LEVEL)
+extern NTSTATUS
+RegistryOpenParametersKey(
+ IN ACCESS_MASK DesiredAccess,
+ OUT PHANDLE Key
+ );
+
__drv_requiresIRQL(PASSIVE_LEVEL)
extern NTSTATUS
RegistryOpenSoftwareKey(