[PATCH v1] kernel32: make GetEnvironmentStringsW returns a copy of the environment
There are certain applications which try to traverse the environement being returned, but this is problematic since they cannot acquire the PEB Lock (i.e cl.exe on Visual Studio 14.15) . To resolve the issue provide a copy of the current environment same as in GetEnvironmentStringsA . Signed-off-by: Jon Doron <arilou(a)gmail.com> --- dlls/kernel32/environ.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/dlls/kernel32/environ.c b/dlls/kernel32/environ.c index 99bf706e95..c8f32aa0fb 100644 --- a/dlls/kernel32/environ.c +++ b/dlls/kernel32/environ.c @@ -139,7 +139,22 @@ LPSTR WINAPI GetEnvironmentStringsA(void) */ LPWSTR WINAPI GetEnvironmentStringsW(void) { - return NtCurrentTeb()->Peb->ProcessParameters->Environment; + LPWSTR ret, ptrW; + unsigned len; + + RtlAcquirePebLock(); + + ptrW = NtCurrentTeb()->Peb->ProcessParameters->Environment; + while (*ptrW) + ptrW += strlenW(ptrW) + 1; + + len = (ULONG_PTR)ptrW - (ULONG_PTR)NtCurrentTeb()->Peb->ProcessParameters->Environment; + ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len + (sizeof(WCHAR) * 2)); + if (ret) + memcpy(ret, NtCurrentTeb()->Peb->ProcessParameters->Environment, len); + + RtlReleasePebLock(); + return ret; } @@ -157,7 +172,7 @@ BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr ) */ BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr ) { - return TRUE; + return HeapFree( GetProcessHeap(), 0, ptr ); } -- 2.19.2
On 3/16/19 9:57 PM, Jon Doron wrote:
There are certain applications which try to traverse the environement being returned, but this is problematic since they cannot acquire the PEB Lock (i.e cl.exe on Visual Studio 14.15) . To resolve the issue provide a copy of the current environment same as in GetEnvironmentStringsA . Please add a test to confirm this change.
participants (2)
-
Jon Doron -
Nikolay Sivov