From 83f3d18dee15e22026bc945e82427da3d220944c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Herm=C3=A8s=20B=C3=A9lusca-Ma=C3=AFto?= Date: Sat, 6 Oct 2012 19:50:17 +0000 Subject: [PATCH] [NTOSKRNL] Rearrange the NtQuerySystemEnvironmentValue code to have successive logical checks. svn path=/trunk/; revision=57504 --- reactos/ntoskrnl/ex/sysinfo.c | 45 ++++++++++++++++------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/reactos/ntoskrnl/ex/sysinfo.c b/reactos/ntoskrnl/ex/sysinfo.c index 00053e7c098..28cfcaf46cf 100644 --- a/reactos/ntoskrnl/ex/sysinfo.c +++ b/reactos/ntoskrnl/ex/sysinfo.c @@ -235,44 +235,40 @@ NtQuerySystemEnvironmentValue(IN PUNICODE_STRING VariableName, _SEH2_YIELD(return _SEH2_GetExceptionCode()); } _SEH2_END; - } - /* Allocate a buffer for the value */ - AnsiValueBuffer = ExAllocatePoolWithTag(NonPagedPool, ValueBufferLength, 'pmeT'); - if (AnsiValueBuffer == NULL) + /* According to NTInternals the SeSystemEnvironmentName privilege is required! */ + if (!SeSinglePrivilegeCheck(SeSystemEnvironmentPrivilege, PreviousMode)) { - return STATUS_INSUFFICIENT_RESOURCES; + DPRINT1("NtQuerySystemEnvironmentValue: Caller requires the SeSystemEnvironmentPrivilege privilege!\n"); + return STATUS_PRIVILEGE_NOT_HELD; } - /* - * Copy the name to kernel space if necessary and convert it to ANSI. - */ + /* Copy the name to kernel space if necessary */ Status = ProbeAndCaptureUnicodeString(&WName, PreviousMode, VariableName); if (!NT_SUCCESS(Status)) { return Status; } - /* - * according to ntinternals the SeSystemEnvironmentName privilege is required! - */ - if (!SeSinglePrivilegeCheck(SeSystemEnvironmentPrivilege, PreviousMode)) - { - ReleaseCapturedUnicodeString(&WName, PreviousMode); - DPRINT1("NtQuerySystemEnvironmentValue: Caller requires the SeSystemEnvironmentPrivilege privilege!\n"); - return STATUS_PRIVILEGE_NOT_HELD; - } - - /* Convert the value name to ansi and release the captured unicode string */ + /* Convert the name to ANSI and release the captured UNICODE string */ Status = RtlUnicodeStringToAnsiString(&AName, &WName, TRUE); ReleaseCapturedUnicodeString(&WName, PreviousMode); if (!NT_SUCCESS(Status)) return Status; - /* Get the environment variable */ + /* Allocate a buffer for the ANSI environment variable */ + AnsiValueBuffer = ExAllocatePoolWithTag(NonPagedPool, ValueBufferLength, 'pmeT'); + if (AnsiValueBuffer == NULL) + { + RtlFreeAnsiString(&AName); + return STATUS_INSUFFICIENT_RESOURCES; + } + + /* Get the environment variable and free the ANSI name */ Result = HalGetEnvironmentVariable(AName.Buffer, (USHORT)ValueBufferLength, AnsiValueBuffer); + RtlFreeAnsiString(&AName); /* Check if we had success */ if (Result == ESUCCESS) @@ -280,13 +276,13 @@ NtQuerySystemEnvironmentValue(IN PUNICODE_STRING VariableName, /* Copy the result back to the caller. */ _SEH2_TRY { - /* Initialize ansi string from the result */ + /* Initialize ANSI string from the result */ RtlInitAnsiString(&AValue, AnsiValueBuffer); - /* Initialize a unicode string from the callers buffer */ + /* Initialize a UNICODE string from the callers buffer */ RtlInitEmptyUnicodeString(&WValue, ValueBuffer, ValueBufferLength); - /* Convert the result to unicode */ + /* Convert the result to UNICODE */ Status = RtlAnsiStringToUnicodeString(&WValue, &AValue, FALSE); if (ReturnLength != NULL) @@ -305,8 +301,7 @@ NtQuerySystemEnvironmentValue(IN PUNICODE_STRING VariableName, Status = STATUS_UNSUCCESSFUL; } - /* Cleanup allocated resources. */ - RtlFreeAnsiString(&AName); + /* Free the allocated ANSI value buffer */ ExFreePoolWithTag(AnsiValueBuffer, 'pmeT'); return Status; -- 2.17.1