[CSRSRV] CsrUnhandledExceptionFilter: Check RtlAdjustPrivilege() result (#2864)
[reactos.git] / subsystems / win32 / csrsrv / server.c
index 15d2e59..3fce78b 100644 (file)
 
 #include "srv.h"
 
+#include <ndk/mmfuncs.h>
+
 #define NDEBUG
 #include <debug.h>
 
 /* DATA ***********************************************************************/
 
+PCSR_SERVER_DLL CsrLoadedServerDll[CSR_SERVER_DLL_MAX];
+PVOID CsrSrvSharedSectionHeap = NULL;
+PVOID CsrSrvSharedSectionBase = NULL;
+PVOID *CsrSrvSharedStaticServerData = NULL;
+ULONG CsrSrvSharedSectionSize = 0;
+HANDLE CsrSrvSharedSection = NULL;
+
 PCSR_API_ROUTINE CsrServerApiDispatchTable[CsrpMaxApiNumber] =
 {
     CsrSrvClientConnect,
@@ -33,6 +42,11 @@ BOOLEAN CsrServerApiServerValidTable[CsrpMaxApiNumber] =
     TRUE
 };
 
+/*
+ * On Windows Server 2003, CSR Servers contain
+ * the API Names Table only in Debug Builds.
+ */
+#ifdef CSR_DBG
 PCHAR CsrServerApiNameTable[CsrpMaxApiNumber] =
 {
     "ClientConnect",
@@ -41,13 +55,7 @@ PCHAR CsrServerApiNameTable[CsrpMaxApiNumber] =
     "IdentifyAlertableThread",
     "SetPriorityClass"
 };
-
-PCSR_SERVER_DLL CsrLoadedServerDll[CSR_SERVER_DLL_MAX];
-PVOID CsrSrvSharedSectionHeap = NULL;
-PVOID CsrSrvSharedSectionBase = NULL;
-PVOID *CsrSrvSharedStaticServerData = NULL;
-ULONG CsrSrvSharedSectionSize = 0;
-HANDLE CsrSrvSharedSection = NULL;
+#endif
 
 /* PRIVATE FUNCTIONS **********************************************************/
 
@@ -69,11 +77,13 @@ HANDLE CsrSrvSharedSection = NULL;
 CSR_SERVER_DLL_INIT(CsrServerDllInitialization)
 {
     /* Setup the DLL Object */
-    LoadedServerDll->ApiBase = 0;
+    LoadedServerDll->ApiBase = CSRSRV_FIRST_API_NUMBER;
     LoadedServerDll->HighestApiSupported = CsrpMaxApiNumber;
     LoadedServerDll->DispatchTable = CsrServerApiDispatchTable;
     LoadedServerDll->ValidTable = CsrServerApiServerValidTable;
+#ifdef CSR_DBG
     LoadedServerDll->NameTable = CsrServerApiNameTable;
+#endif
     LoadedServerDll->SizeOfProcessData = 0;
     LoadedServerDll->ConnectCallback = NULL;
     LoadedServerDll->DisconnectCallback = NULL;
@@ -167,8 +177,8 @@ CsrLoadServerDll(IN PCHAR DllString,
 
     /* Set up the Object */
     ServerDll->Length = Size;
+    ServerDll->SizeOfProcessData = 0;
     ServerDll->SharedSection = CsrSrvSharedSectionHeap; // Send to the server dll our shared heap pointer.
-    ServerDll->Event = CsrInitializationEvent;
     ServerDll->Name.Length = DllName.Length;
     ServerDll->Name.MaximumLength = DllName.MaximumLength;
     ServerDll->Name.Buffer = (PCHAR)(ServerDll + 1);
@@ -195,6 +205,9 @@ CsrLoadServerDll(IN PCHAR DllString,
     else
     {
         /* No handle, so we are loading ourselves */
+#ifdef CSR_DBG
+        RtlInitAnsiString(&EntryPointString, "CsrServerDllInitialization");
+#endif
         ServerDllInitProcedure = CsrServerDllInitialization;
         Status = STATUS_SUCCESS;
     }
@@ -202,8 +215,21 @@ CsrLoadServerDll(IN PCHAR DllString,
     /* Check if we got the pointer, and call it */
     if (NT_SUCCESS(Status))
     {
-        /* Get the result from the Server DLL */
-        Status = ServerDllInitProcedure(ServerDll);
+        /* Call the Server DLL entrypoint */
+        _SEH2_TRY
+        {
+            Status = ServerDllInitProcedure(ServerDll);
+        }
+        _SEH2_EXCEPT(CsrUnhandledExceptionFilter(_SEH2_GetExceptionInformation()))
+        {
+            Status = _SEH2_GetExceptionCode();
+#ifdef CSR_DBG
+            DPRINT1("CSRSS: Exception 0x%lx while calling Server DLL entrypoint %Z!%Z()\n",
+                    Status, &DllName, &EntryPointString);
+#endif
+        }
+        _SEH2_END;
+
         if (NT_SUCCESS(Status))
         {
             /*
@@ -328,7 +354,7 @@ CsrSrvCreateSharedSection(IN PCHAR ParameterValue)
     ULONG Size;
     NTSTATUS Status;
     LARGE_INTEGER SectionSize;
-    ULONG ViewSize = 0;
+    SIZE_T ViewSize = 0;
     PPEB Peb = NtCurrentPeb();
 
     /* If there's no parameter, fail */
@@ -444,10 +470,10 @@ CsrSrvCreateSharedSection(IN PCHAR ParameterValue)
 NTSTATUS
 NTAPI
 CsrSrvAttachSharedSection(IN PCSR_PROCESS CsrProcess OPTIONAL,
-                          OUT PCSR_CONNECTION_INFO ConnectInfo)
+                          OUT PCSR_API_CONNECTINFO ConnectInfo)
 {
     NTSTATUS Status;
-    ULONG ViewSize = 0;
+    SIZE_T ViewSize = 0;
 
     /* Check if we have a process */
     if (CsrProcess)
@@ -469,7 +495,7 @@ CsrSrvAttachSharedSection(IN PCSR_PROCESS CsrProcess OPTIONAL,
     /* Write the values in the Connection Info structure */
     ConnectInfo->SharedSectionBase = CsrSrvSharedSectionBase;
     ConnectInfo->SharedSectionHeap = CsrSrvSharedSectionHeap;
-    ConnectInfo->SharedSectionData = CsrSrvSharedStaticServerData;
+    ConnectInfo->SharedStaticServerData = CsrSrvSharedStaticServerData;
 
     /* Return success */
     return STATUS_SUCCESS;
@@ -633,6 +659,11 @@ CsrUnhandledExceptionFilter(IN PEXCEPTION_POINTERS ExceptionInfo)
                                             FALSE,
                                             &OldValue);
             }
+            if (!NT_SUCCESS(Status))
+            {
+                DPRINT1("CsrUnhandledExceptionFilter(): RtlAdjustPrivilege(SE_SHUTDOWN_PRIVILEGE) failed, Status = 0x%08lx\n", Status);
+                goto NoPrivilege;
+            }
 
             /* Initialize our Name String */
             RtlInitUnicodeString(&ErrorSource, L"Windows SubSystem");
@@ -644,14 +675,15 @@ CsrUnhandledExceptionFilter(IN PEXCEPTION_POINTERS ExceptionInfo)
             ErrorParameters[3] = (ULONG_PTR)ExceptionInfo->ContextRecord;
 
             /* Bugcheck */
-            Status = NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED,
-                                      4,
-                                      1,
-                                      ErrorParameters,
-                                      OptionShutdownSystem,
-                                      &Response);
+            NtRaiseHardError(STATUS_SYSTEM_PROCESS_TERMINATED,
+                             4,
+                             1,
+                             ErrorParameters,
+                             OptionShutdownSystem,
+                             &Response);
         }
 
+NoPrivilege:
         /* Just terminate us */
         NtTerminateProcess(NtCurrentProcess(),
                            ExceptionInfo->ExceptionRecord->ExceptionCode);