[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[XENVBD PATCH 1/2] Add RegistryOpenParametersKey



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@xxxxxxxxx>
---
 src/xendisk/driver.c   | 19 ++---------
 src/xendisk/registry.c | 72 ++++++++++++++++++++++++++++++++++++++++-
 src/xendisk/registry.h |  9 +++++-
 src/xenvbd/driver.c    | 27 +++-------------
 src/xenvbd/registry.c  | 73 +++++++++++++++++++++++++++++++++++++++++-
 src/xenvbd/registry.h  | 10 +++++-
 6 files changed, 167 insertions(+), 43 deletions(-)

diff --git a/src/xendisk/driver.c b/src/xendisk/driver.c
index e24a1a4..e30b75c 100644
--- a/src/xendisk/driver.c
+++ b/src/xendisk/driver.c
@@ -221,7 +221,6 @@ DriverEntry(
     IN  PUNICODE_STRING RegistryPath
     )
 {
-    HANDLE              ServiceKey;
     HANDLE              ParametersKey;
     ULONG               Index;
     NTSTATUS            status;
@@ -246,25 +245,16 @@ DriverEntry(
             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++) {
@@ -277,11 +267,6 @@ DriverEntry(
 
     return STATUS_SUCCESS;
 
-fail3:
-    Error("fail3\n");
-
-    RegistryCloseKey(ServiceKey);
-
 fail2:
     Error("fail2\n");
 
diff --git a/src/xendisk/registry.c b/src/xendisk/registry.c
index 173b6b2..9773d7d 100644
--- a/src/xendisk/registry.c
+++ b/src/xendisk/registry.c
@@ -38,8 +38,13 @@
 
 #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
@@ -58,9 +63,12 @@ __RegistryFree(
 
 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);
@@ -69,6 +77,16 @@ RegistryInitialize(
     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:
@@ -82,6 +100,10 @@ RegistryTeardown(
     VOID
     )
 {
+    __IoOpenDriverRegistryKey = NULL;
+
+    RegistryDriverObject = NULL;
+
     RtlFreeUnicodeString(&RegistryPath);
     RegistryPath.Buffer = NULL;
     RegistryPath.MaximumLength = RegistryPath.Length = 0;
@@ -266,6 +288,54 @@ RegistryCreateServiceKey(
     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,
diff --git a/src/xendisk/registry.h b/src/xendisk/registry.h
index 7516e51..b33eb81 100644
--- a/src/xendisk/registry.h
+++ b/src/xendisk/registry.h
@@ -37,7 +37,8 @@
 
 extern NTSTATUS
 RegistryInitialize(
-    IN PUNICODE_STRING  Path
+    IN  PDRIVER_OBJECT  DriverObject,
+    IN  PUNICODE_STRING Path
     );
 
 extern VOID
@@ -72,6 +73,12 @@ RegistryCreateServiceKey(
     OUT PHANDLE     Key
     );
 
+extern NTSTATUS
+RegistryOpenParametersKey(
+    IN  ACCESS_MASK DesiredAccess,
+    OUT PHANDLE     Key
+    );
+
 extern NTSTATUS
 RegistryOpenSoftwareKey(
     IN  PDEVICE_OBJECT  DeviceObject,
diff --git a/src/xenvbd/driver.c b/src/xenvbd/driver.c
index 0d6c21d..ba0ad33 100644
--- a/src/xenvbd/driver.c
+++ b/src/xenvbd/driver.c
@@ -351,7 +351,6 @@ DriverEntry(
     IN  PUNICODE_STRING     RegistryPath
     )
 {
-    HANDLE                  ServiceKey;
     HANDLE                  ParametersKey;
     NTSTATUS                status;
 
@@ -371,21 +370,14 @@ DriverEntry(
          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;
 
@@ -394,7 +386,7 @@ DriverEntry(
     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];
@@ -404,22 +396,13 @@ DriverEntry(
     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");
diff --git a/src/xenvbd/registry.c b/src/xenvbd/registry.c
index 811701f..069c62a 100644
--- a/src/xenvbd/registry.c
+++ b/src/xenvbd/registry.c
@@ -38,8 +38,13 @@
 
 #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
@@ -58,9 +63,12 @@ __RegistryFree(
 
 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);
@@ -69,6 +77,16 @@ RegistryInitialize(
     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:
@@ -82,6 +100,10 @@ RegistryTeardown(
     VOID
     )
 {
+    __IoOpenDriverRegistryKey = NULL;
+
+    RegistryDriverObject = NULL;
+
     RtlFreeUnicodeString(&RegistryPath);
     RegistryPath.Buffer = NULL;
     RegistryPath.MaximumLength = RegistryPath.Length = 0;
@@ -270,6 +292,55 @@ RegistryCreateServiceKey(
     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(
diff --git a/src/xenvbd/registry.h b/src/xenvbd/registry.h
index d8a2df5..8dac63e 100644
--- a/src/xenvbd/registry.h
+++ b/src/xenvbd/registry.h
@@ -37,7 +37,8 @@
 
 extern NTSTATUS
 RegistryInitialize(
-    IN PUNICODE_STRING  Path
+    IN  PDRIVER_OBJECT  DriverObject,
+    IN  PUNICODE_STRING Path
     );
 
 extern VOID
@@ -76,6 +77,13 @@ RegistryCreateServiceKey(
     OUT PHANDLE     Key
     );
 
+__drv_requiresIRQL(PASSIVE_LEVEL)
+extern NTSTATUS
+RegistryOpenParametersKey(
+    IN  ACCESS_MASK DesiredAccess,
+    OUT PHANDLE     Key
+    );
+
 __drv_requiresIRQL(PASSIVE_LEVEL)
 extern NTSTATUS
 RegistryOpenSoftwareKey(
-- 
2.44.0.windows.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.