From 1968202840d9029119fddaaf4712b672d88096cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Thu, 11 Jun 2026 18:29:26 +0200 Subject: [PATCH] [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.) --- win32ss/user/ntuser/winsta.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/win32ss/user/ntuser/winsta.c b/win32ss/user/ntuser/winsta.c index 86cfbb7bb58..1c32f99c683 100644 --- a/win32ss/user/ntuser/winsta.c +++ b/win32ss/user/ntuser/winsta.c @@ -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; }