https://git.reactos.org/?p=reactos.git;a=commitdiff;h=aff16663562ac33b8da058baacae2187ba303203

commit aff16663562ac33b8da058baacae2187ba303203
Author:     Timo Kreuzer <[email protected]>
AuthorDate: Sun Oct 22 21:42:11 2023 +0300
Commit:     Timo Kreuzer <[email protected]>
CommitDate: Sun Nov 19 15:32:39 2023 +0200

    [ADVAPI32] Improve handling of unaligned key name in RegOpenKeyExW
    
    Check for unaligned buffer before calling NtOpenKey instead of checking the 
result for STATUS_DATATYPE_MISALIGNMENT.
---
 dll/win32/advapi32/reg/reg.c | 57 ++++++++++++++++++++------------------------
 1 file changed, 26 insertions(+), 31 deletions(-)

diff --git a/dll/win32/advapi32/reg/reg.c b/dll/win32/advapi32/reg/reg.c
index 1ded4b54222..284731628b1 100644
--- a/dll/win32/advapi32/reg/reg.c
+++ b/dll/win32/advapi32/reg/reg.c
@@ -3362,6 +3362,7 @@ RegOpenKeyExW(HKEY hKey,
     NTSTATUS Status;
     ULONG Attributes = OBJ_CASE_INSENSITIVE;
     LONG ErrorCode = ERROR_SUCCESS;
+    BOOLEAN SubKeyStringAllocated = FALSE;
 
     TRACE("RegOpenKeyExW hKey 0x%x lpSubKey %S ulOptions 0x%x samDesired 0x%x 
phkResult %p\n",
           hKey, lpSubKey, ulOptions, samDesired, phkResult);
@@ -3398,10 +3399,31 @@ RegOpenKeyExW(HKEY hKey,
         Attributes |= OBJ_OPENLINK;
 
     if (lpSubKey == NULL || wcscmp(lpSubKey, L"\\") == 0)
+    {
         RtlInitUnicodeString(&SubKeyString, L"");
+    }
     else
+    {
         RtlInitUnicodeString(&SubKeyString, lpSubKey);
 
+        /* Handle unaligned lpSubKey */
+        if ((ULONG_PTR)lpSubKey & 1)
+        {
+            UNICODE_STRING AlignedString;
+
+            Status = 
RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
+                                               &SubKeyString,
+                                               &AlignedString);
+            if (!NT_SUCCESS(Status))
+            {
+                goto Exit;
+            }
+
+            SubKeyString = AlignedString;
+            SubKeyStringAllocated = TRUE;
+        }
+    }
+
     InitializeObjectAttributes(&ObjectAttributes,
                                &SubKeyString,
                                Attributes,
@@ -3412,37 +3434,11 @@ RegOpenKeyExW(HKEY hKey,
                        samDesired,
                        &ObjectAttributes);
 
-    if (Status == STATUS_DATATYPE_MISALIGNMENT)
-    {
-        HANDLE hAligned;
-        UNICODE_STRING AlignedString;
-
-        Status = 
RtlDuplicateUnicodeString(RTL_DUPLICATE_UNICODE_STRING_NULL_TERMINATE,
-                                           &SubKeyString,
-                                           &AlignedString);
-        if (NT_SUCCESS(Status))
-        {
-            /* Try again with aligned parameters */
-            InitializeObjectAttributes(&ObjectAttributes,
-                                       &AlignedString,
-                                       Attributes,
-                                       KeyHandle,
-                                       NULL);
-
-            Status = NtOpenKey(&hAligned,
-                               samDesired,
-                               &ObjectAttributes);
-
-            RtlFreeUnicodeString(&AlignedString);
+Exit:
 
-            if (NT_SUCCESS(Status))
-                *phkResult = hAligned;
-        }
-        else
-        {
-            /* Restore the original error */
-            Status = STATUS_DATATYPE_MISALIGNMENT;
-        }
+    if (SubKeyStringAllocated)
+    {
+        RtlFreeUnicodeString(&SubKeyString);
     }
 
     if (!NT_SUCCESS(Status))
@@ -3450,7 +3446,6 @@ RegOpenKeyExW(HKEY hKey,
         ErrorCode = RtlNtStatusToDosError(Status);
     }
 
-
     ClosePredefKey(KeyHandle);
 
     return ErrorCode;

Reply via email to