[WIN32SS:NTUSER] winsta.c: Improve UserCreateWinstaDirectory()

- Don't use `NtCurrentPeb()` to retrieve the current process session ID,
  but `PsGetCurrentProcessSessionId()` instead that doesn't require a PEB.

- Turn the purposely-leaking local `hWinstaDir` variable into a global
  `ghWinStaDir` variable, so that when win32k.sys unloading support will
  be implemented, one could close `ghWinStaDir` so as to delete the per-
  session window-station object directory.
  (It isn't created as an `OBJ_PERMANENT` object for this reason.
  See comment https://github.com/reactos/reactos/pull/621#discussion_r196303521
  in PR #621.)
This commit is contained in:
Hermès Bélusca-Maïto
2026-06-23 21:23:06 +02:00
parent 9d287f3c5a
commit 1968202840
+9 -16
View File
@@ -25,6 +25,7 @@ HWND hwndSAS = NULL;
/* Full path to WindowStations directory */
UNICODE_STRING gustrWindowStationsDir;
HANDLE ghWinStaDir;
/* INITIALIZATION FUNCTIONS ****************************************************/
@@ -51,19 +52,16 @@ NTAPI
UserCreateWinstaDirectory(VOID)
{
NTSTATUS Status;
PPEB Peb;
ULONG SessionId;
OBJECT_ATTRIBUTES ObjectAttributes;
HANDLE hWinstaDir;
WCHAR wstrWindowStationsDir[MAX_PATH];
/* Create the WindowStations directory and cache its path for later use */
Peb = NtCurrentPeb();
if(Peb->SessionId == 0)
SessionId = PsGetCurrentProcessSessionId(); // gSessionId
if (SessionId == 0)
{
if (!RtlCreateUnicodeString(&gustrWindowStationsDir, WINSTA_OBJ_DIR))
{
return STATUS_INSUFFICIENT_RESOURCES;
}
}
else
{
@@ -71,15 +69,13 @@ UserCreateWinstaDirectory(VOID)
sizeof(wstrWindowStationsDir),
L"%ws\\%lu%ws",
SESSION_DIR,
Peb->SessionId,
SessionId,
WINSTA_OBJ_DIR);
if (!NT_SUCCESS(Status))
return Status;
if (!RtlCreateUnicodeString(&gustrWindowStationsDir, wstrWindowStationsDir))
{
return STATUS_INSUFFICIENT_RESOURCES;
}
}
InitializeObjectAttributes(&ObjectAttributes,
@@ -87,14 +83,11 @@ UserCreateWinstaDirectory(VOID)
OBJ_KERNEL_HANDLE,
NULL,
NULL);
Status = ZwCreateDirectoryObject(&hWinstaDir, DIRECTORY_CREATE_OBJECT, &ObjectAttributes);
Status = ZwCreateDirectoryObject(&ghWinStaDir, DIRECTORY_CREATE_OBJECT, &ObjectAttributes);
if (!NT_SUCCESS(Status))
{
ERR("Could not create %wZ directory (Status 0x%X)\n", &gustrWindowStationsDir, Status);
return Status;
}
TRACE("Created directory %wZ for session %lu\n", &gustrWindowStationsDir, Peb->SessionId);
ERR("Could not create %wZ directory (Status 0x%X)\n", &gustrWindowStationsDir, Status);
else
TRACE("Created directory %wZ for session %lu\n", &gustrWindowStationsDir, SessionId);
return Status;
}