]> xenbits.xensource.com Git - pvdrivers/win/xenbus.git/commitdiff
Add RegistryOpenParametersKey
authorOwen Smith <owen.smith@cloud.com>
Fri, 7 Jun 2024 07:16:56 +0000 (08:16 +0100)
committerPaul Durrant <pdurrant@amazon.com>
Mon, 1 Jul 2024 08:49:31 +0000 (09:49 +0100)
Use IoOpenDriverRegistryKey to avoid opening an absolute registry path.
Driver Verifier can detect registry isolation violations when running WHQL
tests on Server 2025. The rule states that a driver may not open an absolute
registry key path. Use the specific API to open the 'Parameters' key with
KEY_READ when querying settings.

Signed-off-by: Owen Smith <owen.smith@cloud.com>
Cosmetic fix-up.

Signed-off-by: Paul Durrant <pdurrant@amazon.com>
src/common/registry.c
src/common/registry.h
src/xen/driver.c
src/xenbus/driver.c
src/xenfilt/driver.c

index 0d29ddc2790cdcb99cf569e98d389bdb984875fe..3f45a238a4e2bb07874ae917fd2b85b1f7d15d2f 100644 (file)
@@ -38,6 +38,7 @@
 
 #define REGISTRY_TAG 'GERX'
 
+static PDRIVER_OBJECT   RegistryDriverObject;
 static UNICODE_STRING   RegistryPath;
 
 static FORCEINLINE PVOID
@@ -58,7 +59,8 @@ __RegistryFree(
 
 NTSTATUS
 RegistryInitialize(
-    IN PUNICODE_STRING  Path
+    IN  PDRIVER_OBJECT  DriverObject,
+    IN  PUNICODE_STRING Path
     )
 {
     NTSTATUS            status;
@@ -69,6 +71,9 @@ RegistryInitialize(
     if (!NT_SUCCESS(status))
         goto fail1;
 
+    ASSERT3P(RegistryDriverObject, ==, NULL);
+    RegistryDriverObject = DriverObject;
+
     return STATUS_SUCCESS;
 
 fail1:
@@ -82,11 +87,26 @@ RegistryTeardown(
     VOID
     )
 {
+    RegistryDriverObject = NULL;
+
     RtlFreeUnicodeString(&RegistryPath);
     RegistryPath.Buffer = NULL;
     RegistryPath.MaximumLength = RegistryPath.Length = 0;
 }
 
+NTSTATUS
+RegistryOpenParametersKey(
+    IN  ACCESS_MASK     DesiredAccess,
+    OUT PHANDLE         Key
+    )
+{
+    return IoOpenDriverRegistryKey(RegistryDriverObject,
+                                   DriverRegKeyParameters,
+                                   DesiredAccess,
+                                   0,
+                                   Key);
+}
+
 NTSTATUS
 RegistryOpenKey(
     IN  HANDLE          Parent,
index cbe9015521aabd7390c0fd7428f8a8b526ea77f3..efa96ea8b7141f37228dab374ecb3f242079186a 100644 (file)
@@ -37,7 +37,8 @@
 
 extern NTSTATUS
 RegistryInitialize(
-    IN PUNICODE_STRING  Path
+    IN  PDRIVER_OBJECT  DrvObj,
+    IN  PUNICODE_STRING Path
     );
 
 extern VOID
@@ -45,6 +46,12 @@ RegistryTeardown(
     VOID
     );
 
+extern NTSTATUS
+RegistryOpenParametersKey(
+    IN  ACCESS_MASK     DesiredAccess,
+    OUT PHANDLE         Key
+    );
+
 extern NTSTATUS
 RegistryOpenKey(
     IN  HANDLE          Parent,
index 8fe6c5cfbf3d7c12350c3a14c442d0ed90919419..e04a772a893d51d6aa9fef504dc0f8f825281806 100644 (file)
@@ -515,7 +515,7 @@ DllInitialize(
     if (!NT_SUCCESS(status))
         goto fail1;
 
-    status = RegistryInitialize(RegistryPath);
+    status = RegistryInitialize(NULL, RegistryPath);
     if (!NT_SUCCESS(status))
         goto fail2;
 
index 522acef0ca676a57547df233692b3d16395541de..d6efe893e8456db6614063d1345cab0ac37f642a 100644 (file)
@@ -811,7 +811,6 @@ DriverEntry(
     IN  PUNICODE_STRING RegistryPath
     )
 {
-    HANDLE              ServiceKey;
     HANDLE              ParametersKey;
     ULONG               Index;
     LOG_LEVEL           LogLevel;
@@ -839,21 +838,14 @@ DriverEntry(
          MONTH,
          YEAR);
 
-    status = RegistryInitialize(RegistryPath);
+    status = RegistryInitialize(DriverObject, RegistryPath);
     if (!NT_SUCCESS(status))
         goto fail1;
 
-    status = RegistryOpenServiceKey(KEY_READ, &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);
 
     status = LogReadLogLevel(ParametersKey,
@@ -864,8 +856,6 @@ DriverEntry(
 
     __DriverSetConsoleLogLevel(LogLevel);
 
-    RegistryCloseKey(ServiceKey);
-
     status = XenTouch(__MODULE__,
                       MAJOR_VERSION,
                       MINOR_VERSION,
@@ -900,11 +890,6 @@ done:
 
     return STATUS_SUCCESS;
 
-fail3:
-    Error("fail3\n");
-
-    RegistryCloseKey(ServiceKey);
-
 fail2:
     Error("fail2\n");
 
index 724d418a5d5c41825426697b957625a767f60669..aee663afa981e9b642146bc11abfa74d668e3db8 100644 (file)
@@ -113,33 +113,6 @@ DriverGetDriverObject(
     return __DriverGetDriverObject();
 }
 
-static FORCEINLINE NTSTATUS
-__DriverOpenParametersKey(
-    OUT PHANDLE     ParametersKey
-    )
-{
-    HANDLE          ServiceKey;
-    NTSTATUS        status;
-
-    status = RegistryOpenServiceKey(KEY_READ, &ServiceKey);
-    if (!NT_SUCCESS(status))
-        goto fail1;
-
-    status = RegistryOpenSubKey(ServiceKey, "Parameters", KEY_READ, ParametersKey);
-    if (!NT_SUCCESS(status))
-        goto fail2;
-
-    RegistryCloseKey(ServiceKey);
-
-    return STATUS_SUCCESS;
-
-fail2:
-    RegistryCloseKey(ServiceKey);
-
-fail1:
-    return status;
-}
-
 static FORCEINLINE VOID
 __DriverSetEmulatedContext(
     IN  PXENFILT_EMULATED_CONTEXT   Context
@@ -247,7 +220,7 @@ __DriverGetActive(
 
     ASSERT3U(KeGetCurrentIrql(), ==, PASSIVE_LEVEL);
 
-    status = __DriverOpenParametersKey(&ParametersKey);
+    status = RegistryOpenParametersKey(KEY_READ, &ParametersKey);
     if (!NT_SUCCESS(status))
         goto fail1;
 
@@ -752,7 +725,7 @@ DriverGetEmulatedType(
     ULONG                           Index;
     NTSTATUS                        status;
 
-    status = __DriverOpenParametersKey(&ParametersKey);
+    status = RegistryOpenParametersKey(KEY_READ, &ParametersKey);
     if (!NT_SUCCESS(status))
         goto fail1;
 
@@ -947,7 +920,7 @@ DriverEntry(
     if (!NT_SUCCESS(status))
         goto done;
 
-    status = RegistryInitialize(RegistryPath);
+    status = RegistryInitialize(DriverObject, RegistryPath);
     if (!NT_SUCCESS(status))
         goto fail1;